Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Turtle Command
- By
- Big SHiny Toys
- ]]--
- -- start of programs
- local tProgs = {
- manCon = function()
- while true do
- local e,e1,e2,e3,e4,e5 = coroutine.yield()
- if e == "char" and ManualControlstatus then
- if e1 == "w" then
- move("F")
- end
- if e1 == "s" then
- move("B")
- end
- if e1 == "a" then
- move("L")
- end
- if e1 == "d" then
- move("R")
- end
- if e1 == "q" then
- move("U")
- end
- if e1 == "e" then
- move("D")
- end
- end
- end
- end,
- location = function()
- while true do
- local x,x1 = coroutine.yield()
- if locationS then
- if not menuStatus then
- -- term.clear()
- term.setCursorPos(1,1)
- print("LOC X:"..turX.." Y:"..turY.." Z:"..turZ.." Compas: "..compas[face].." "..face)
- if gpsX then
- print("GPS X:"..gpsX.." Y:"..gpsY.." Z:"..gpsZ)
- else
- print("gps unavalible")
- end
- end
- if x == "redstone" then
- term.setCursorPos(1,4)
- print("Redstone signal")
- end
- end
- end
- end,
- gps = function()
- local pastX,pastY,pastZ,pastV = turX,turY,turZ,face
- local w,w1,w2,w3,w4,w5
- while true do
- w,w1,w2,w3,w4,w5 = coroutine.yield()
- -- if w == "char" and w1 == "t" then break end
- if pastX == turX and pastY == turY and pastZ == turZ and pastV == face then
- else
- gpsX,gpsY,gpsZ = gps.locate(1)
- pastX,pastY,pastZ,pastV = turX,turY,turZ,face
- end
- end
- end,
- menu = function()
- while true do
- coroutine.yield()
- if menuStatus then
- local a = menu("return","Mineing "..tostring(running).." ","Test "..tostring(test),"Quit")
- if a == 1 then
- menuStatus = false
- end
- if a == 2 then
- if running == true then running = false
- elseif running == false then running = true
- end
- end
- if a == 3 then
- if test == true then test = false
- elseif test == false then test = true
- end
- end
- if a == 4 then
- shutdown = true
- menuStatus = false
- end
- end
- end
- end,
- wifiControll = function()
- while true do
- local a,a1,a2,a3,a4,a5 = coroutine.yield()
- if a == "rednet_message" and wifiControll then
- local x = string.sub(a2,1,4) -- TCMD
- if x == "TCMD" then
- move(string.sub(a2,5,5))
- rednet.send(a1,textutils.serialize({turX,turY,turZ,compas[face]}))
- end
- end
- term.setCursorPos(1,4)
- print(tostring(a).." "..tostring(a1))
- end
- end,
- tunneler = function()
- while true do
- coroutine.yield()
- if running then break end
- end
- local starting = {turX,turY,turZ,face}
- local startW,startL,startH = 10,10,10
- running = true
- wifiControll = false
- while true do
- turtle.dig()
- move("F")
- move("R")
- for i = 1,startL do
- for o = 1,math.floor(startH/3) do
- for p = 1,startW do
- turtle.dig()
- turtle.digUp()
- turtle.digDown()
- move("F")
- end
- move("R")
- move("R")
- turtle.digDown()
- for q = 1,3 do
- turtle.digUp()
- move("U")
- end
- end
- for w = 1,startH do
- turtle.digDown()
- move("D")
- end
- for w = 1,startW do
- turtle.dig()
- move("F")
- end
- move("R")
- turtle.dig()
- move("F")
- move("R")
- turtle.dig()
- end
- end
- wifiControll = true
- end,
- test = function()
- --[[moveADV("H",4)
- while true do
- sleep(2)
- for i = 1,4 do
- moveADV("H",i)
- sleep(1)
- end
- for i = 4,1,-1 do
- moveADV("H",i)
- sleep(1)
- end
- end
- ]]--
- while true do
- coroutine.yield()
- if test then
- goto(-24,-12,2)
- goto(-18,-12,2)
- goto(-18,-16,2)
- goto(-24,-16,2)
- end
- end
- end,
- console = function()
- while true do
- coroutine.yield()
- if consoleRUN then
- term.clear()
- term.setCursorPos(1,5)
- local sInput = readADV()
- if string.sub(sInput,1,3) == "run" then
- coroutine.yield("run",string.sub(sInput,5,#sInput))
- elseif string.sub(sInput,1,4) == "kill" then
- coroutine.yield("kill",string.sub(sInput,6,#sInput))
- end
- consoleRUN = false
- end
- end
- end
- }
- -- end of programs
- -- globals
- gpsX,gpsY,gpsZ = nil,nil,nil
- turX,turY,turZ = 0,0,0
- compas = {"n","e","s","w"}
- moves = {"U","D","L","R","F","B"}
- face = 1 -- 1 north 2 east 3 south 4 west
- routines = {}
- routinNames = {}
- menuStatus = false
- ManualControlstatus = true
- shutdown = false
- wifiControll = true
- running = true
- test = false
- consoleRUN = true
- locationS = true
- -- local vars
- local i = 1
- local e,e1,e2,e3,e4,e5
- -- functions
- function menu(...)
- local sel = 1
- local list = {...}
- local offX,offY = 1,1 -- change this to what ever off set you want
- local curX,curY = term.getCursorPos()
- local tX,tY
- while true do
- if sel > #list then sel = #list end
- if sel < 1 then sel = 1 end
- for i = 1,#list do
- term.setCursorPos(offX,offY+i-1)
- if sel == i then
- print("["..list[i].."]")
- else
- print(" "..list[i].." ")
- end
- end
- while true do
- tX,tY = term.getCursorPos()
- local e,e1,e2,e3,e4,e5 = os.pullEvent()
- term.setCursorPos(tX,tY)
- if e == "key" then
- if e1 == 200 then -- up key
- sel = sel-1
- break
- end
- if e1 == 208 then -- down key
- sel = sel+1
- break
- end
- if e1 == 28 then
- term.setCursorPos(curX,curY)
- return sel
- end
- end
- end
- end
- end
- function goto(thisX,thisY,thisZ)
- moveADV("X",thisX)
- moveADV("Y",nil,thisY)
- moveADV("Z",nil,nil,thisZ)
- end
- function moveADV(ins,rep,tempY,tempZ) -- advanced instructions
- if ins == "H" then
- if rep > 4 or rep < 1 then return false end
- if rep == face then return true end
- if rep == face - 2 or rep == face + 2 then
- move("R")
- move("R")
- else
- local temp = rep-1
- for p = 1,4 do
- temp = temp+1
- if temp > 4 then
- temp = 1
- end
- if face == temp then
- if p == 1 or p == 4 then
- move("R")
- end
- if p == 2 or p == 3 then
- move("L")
- end
- end
- end
- end
- end
- if ins == "X" then
- if rep == turX then return true end
- if rep > turX then
- moveADV("H",2)
- elseif rep < turX then
- moveADV("H",4)
- end
- while rep ~= turX do
- move("F")
- end
- end
- if ins == "Y" then
- if tempY == turY then return true end
- if tempY > turY then
- moveADV("H",1)
- elseif tempY < turY then
- moveADV("H",3)
- end
- while tempY ~= turY do
- move("F")
- end
- end
- if ins == "Z" then
- if tempZ == turZ then return true end
- if tempZ > turZ then
- while tempZ ~= turZ do
- move("U")
- end
- elseif tempZ < turZ then
- while tempZ ~= turZ do
- move("D")
- end
- end
- end
- end
- function move(ins,rep) -- low levle functions
- if not ins and not rep then
- return false,"error no move specified"
- elseif not rep then
- rep = 1
- end
- for i=1,rep do
- if ins == "U" then -- up move
- if turtle.up() then
- turZ = turZ+1
- else
- return false
- end
- end
- if ins == "D" then -- down move
- if turtle.down() then
- turZ = turZ-1
- else
- return false
- end
- end
- if ins == "L" then -- left turn
- if turtle.turnLeft() then
- face = face - 1
- if face < 1 then
- face = 4
- end
- else
- return false
- end
- end
- if ins == "R" then -- right turn
- if turtle.turnRight() then
- face = face + 1
- if face > 4 then
- face = 1
- end
- else
- return false
- end
- end
- if ins == "F" then -- forward move
- if turtle.forward() then
- if face == 1 then
- turY = turY+1
- end
- if face == 2 then
- turX = turX+1
- end
- if face == 3 then
- turY = turY-1
- end
- if face == 4 then
- turX = turX-1
- end
- else
- return false
- end
- end
- if ins == "B" then -- back move
- if turtle.back() then
- if face == 1 then
- turY = turY-1
- end
- if face == 2 then
- turX = turX-1
- end
- if face == 3 then
- turY = turY+1
- end
- if face == 4 then
- turX = turX+1
- end
- else
- return false
- end
- end
- end
- return true
- end
- function run(fFun)
- if fFun == nil then
- return false
- else
- local alreadyRunning = false
- if tProgs[fFun] ~= nil then
- for i = 1, #routinNames do
- if fFun == routinNames[i] then
- alreadyRunning = true
- break
- end
- end
- if not alreadyRunning then
- routines[#routines+1] = coroutine.create(tProgs[fFun])
- routinNames[#routinNames+1] = fFun
- return true
- end
- end
- end
- return false
- end
- function kill(fFun)
- if fFun == nil then
- return false
- else
- for i = 1,#routinNames do
- if routinNames[i] == fFun then
- table.remove(routines,i)
- table.remove(routinNames,i)
- end
- end
- end
- end
- local function fExit()
- while face ~= 1 do
- move("R")
- end
- error()
- end
- function readADV( _sReplaceChar, _tHistory )
- term.setCursorBlink( true )
- local sLine = ""
- local nHistoryPos = nil
- local nPos = 0
- if _sReplaceChar then
- _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
- end
- local w, h = term.getSize()
- local sx, sy = term.getCursorPos()
- local function redraw()
- local nScroll = 0
- if sx + nPos >= w then
- nScroll = (sx + nPos) - w
- end
- term.setCursorPos( sx, sy )
- term.write( string.rep(" ", w - sx + 1) )
- term.setCursorPos( sx, sy )
- if _sReplaceChar then
- term.write( string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
- else
- term.write( string.sub( sLine, nScroll + 1 ) )
- end
- term.setCursorPos( sx + nPos - nScroll, sy )
- end
- while true do
- local sEvent, param = os.pullEvent()
- if sEvent == "char" then
- sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
- nPos = nPos + 1
- redraw()
- elseif sEvent == "key" then
- if param == 28 then
- -- Enter
- break
- elseif param == 203 then
- -- Left
- if nPos > 0 then
- nPos = nPos - 1
- redraw()
- end
- elseif param == 205 then
- -- Right
- if nPos < string.len(sLine) then
- nPos = nPos + 1
- redraw()
- end
- elseif param == 200 or param == 208 then
- -- Up or down
- if _tHistory then
- if param == 200 then
- -- Up
- if nHistoryPos == nil then
- if #_tHistory > 0 then
- nHistoryPos = #_tHistory
- end
- elseif nHistoryPos > 1 then
- nHistoryPos = nHistoryPos - 1
- end
- else
- -- Down
- if nHistoryPos == #_tHistory then
- nHistoryPos = nil
- elseif nHistoryPos ~= nil then
- nHistoryPos = nHistoryPos + 1
- end
- end
- if nHistoryPos then
- sLine = _tHistory[nHistoryPos]
- nPos = string.len( sLine )
- else
- sLine = ""
- nPos = 0
- end
- redraw()
- end
- elseif param == 14 then
- -- Backspace
- if nPos > 0 then
- sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
- nPos = nPos - 1
- redraw()
- end
- end
- else
- redraw()
- end
- end
- term.setCursorBlink( false )
- term.setCursorPos( w + 1, sy )
- return sLine
- end
- -- end of functions
- -- start up sequence
- rednet.open("right")
- gpsX,gpsY,gpsZ = gps.locate(1)
- if gpsX then
- turX,turY,turZ = gpsX,gpsY,gpsZ
- else
- turX,turY,turZ = 0,0,0
- end
- gpsX,gpsY,gpsZ = nil,nil,nil
- --run("manCon")
- --run("location")
- run("gps")
- run("menu")
- run("wifiControll")
- --run("tunneler")
- --run("test")
- run("console")
- -- main loop
- while true do
- e,e1,e2,e3,e4,e5 = os.pullEvent()
- term.clear()
- term.setCursorPos(1,1)
- if shutdown then
- fExit()
- end
- if e == "key" and e1 == 199 then
- if consoleRUN then
- consoleRUN = false
- else
- consoleRUN = true
- end
- end
- i = 1
- while true do
- if i == #routines+1 then
- break
- end
- if coroutine.status(routines[i]) == "deadened" then
- table.remove(routines,i)
- table.remove(routinNames,i)
- else
- local status,request,request1 = coroutine.resume(routines[i],e,e1,e2,e3,e4,e5)
- if status and request then
- term.setCursorPos(1,6)
- term.clearLine()
- print(tostring(request).." "..tostring(request1))
- if request == "kill" then
- kill(request1)
- elseif request == "run" then
- run(request1)
- end
- end
- i = i+1
- end
- end
- end
- --[[
- print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
- move("U")
- print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
- move("D")
- print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
- move("F")
- print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
- move("L")
- print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
- move("F")
- print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
- turX,turY,turZ = gps.locate(2)
- local c1,c2,c3
- local tab = {}
- for i = 1,500 do
- local special,speical2 = moves[math.random(1,6)],math.random(1,6)
- write("Ins:"..special.." For:"..speical2.." Res:")
- move(special,speical2)
- print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
- c1,c2,c3 = gps.locate(2)
- print("X"..c1.." Y:"..c2.." Z:"..c3)
- table.insert(tab,"Ins: "..special.." For: "..speical2.." Res: tur: X:"..turX.." Y:"..turY.." Z:"..turZ.." Face: "..compas[face].." GPS: X:"..c1.." Y:"..c2.." Z:"..c3)
- end
- while face ~= 1 do
- move("R")
- end
- print("X"..turX.." Y"..turY.." Z"..turZ..compas[face])
- c1,c2,c3 = gps.locate(2)
- print("X"..c1.." Y:"..c2.." Z:"..c3)
- table.insert(tab,"LAST".." Res: tur: X:"..turX.." Y:"..turY.." Z:"..turZ.." Face: "..compas[face].." GPS: X:"..c1.." Y:"..c2.." Z:"..c3)
- print("adding to file")
- if fs.exists("log") then
- file = io.open("log","a")
- else
- file = io.open("log","w")
- end
- file:write("\n".."== start of log ==".."\n")
- for i = 1,#tab do
- file:write(tab[i].."\n")
- end
- file:close()
- print("file add compleet")
- ]]--
Advertisement
Add Comment
Please, Sign In to add comment