Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Remote Control Turtle
- local args = {...}
- local inputTable = {
- [keys.w] = "forward",
- [keys.a] = "turnLeft",
- [keys.d] = "turnRight",
- [keys.s] = "back",
- [keys.q] = "exit",
- [keys.z] = "digDown",
- [keys.x] = "dig",
- [keys.c] = "digUp",
- [keys.space] = "up",
- [keys.leftShift] = "down",
- [keys.rightShift] = "down",
- [keys.r] = "refuel",
- [keys.y] = "dropDown",
- [keys.u] = "drop",
- [keys.o] = "dropUp",
- [keys.f] = "placeDown",
- [keys.g] = "place",
- [keys.h] = "placeUp",
- [keys.j] = "suckDown",
- [keys.k] = "suck",
- [keys.l] = "suckUp",
- [keys.left] = "selectLeft",
- [keys.right] = "selectRight",
- [keys.t] = "redstoneOn",
- [keys.y] = "redstoneOff"
- }
- local function createTurtleFunctions()
- local i = 1
- return function() i = (i-1)%16 turtle.select(i) end, function() i = (i+1)%16 turtle.select(i) end, function() shell.exit() end,function() for k,v in ipairs(redstone.getSides()) do redstone.setOutput(v,true) end end,function() for k,v in ipairs(redstone.getSides()) do redstone.setOutput(v,false) end end
- end
- if turtle then
- turtle.selectLeft, turtle.selectRight,turtle.exit,turtle.redstoneOn,turtle.redstoneOff = createTurtleFunctions()
- end
- local function getActionFromFile()
- if not fs.exists("action.lua") then return false end
- local file = fs.open("action.lua","r")
- local action = file.readAll()
- file.close()
- fs.delete("action.lua")
- return action
- -- local s,action = pcall(function() require("action") end)
- -- print(s,action)
- -- if not s then return false end
- -- fs.delete("action.lua")
- -- return action
- end
- local function writeActionToFile(key)
- if not key or not inputTable[key] then return false, "invalid input" end
- if fs.exists("action.lua") then return false, "action already exists" end
- local file = fs.open("action.lua","w+")
- file.write(inputTable[key])
- -- file.write('return "' .. inputTable[key] .. '"')
- file.close()
- return true,inputTable[key]
- end
- local function formatData()
- local str = "fuel:"
- str = "f:" .. turtle.getFuelLevel()
- str = str .. "s:" .. turtle.getSelectedSlot()
- if turtle.getItemCount() > 0 then
- str = str .. "i:" .. textutils.serialise(turtle.getItemDetail(),{compact=true})
- end
- os.setComputerLabel(str)
- end
- local function turtleLoop()
- turtle.select(1)
- while true do
- formatData()
- local action = getActionFromFile()
- if action then
- print(action)
- pcall(function() turtle[action]() end)
- else
- sleep(0.1)
- end
- end
- end
- local function pocketLoop()
- term.clear()
- term.setCursorPos(1,1)
- for k,action in pairs(inputTable) do
- print(keys.getName(k),action)
- end
- while true do
- local e,k = os.pullEvent("key")
- writeActionToFile(k)
- if e == keys.q then shell.exit() end
- end
- end
- local function rednetHostLoop()
- peripheral.find("modem",rednet.open)
- if not rednet.isOpen() then
- print("Please place a modem")
- shell.exit()
- end
- rednet.host("stalker",tostring(os.getComputerID()))
- while true do
- local e,msg,prtcl = rednet.receive()
- if prtcl == "key" then
- print(writeActionToFile(msg))
- if msg == "keys.q" then shell.exit() end
- end
- end
- end
- local function pocketRednetLoop()
- peripheral.find("modem",rednet.open)
- if not rednet.isOpen() then
- print("Please place a modem")
- shell.exit()
- end
- local host = rednet.lookup("stalker")
- term.clear()
- term.setCursorPos(1,1)
- local str = ""
- term.setTextColor(colors.green)
- for k,action in pairs(inputTable) do
- str = str .. keys.getName(k) .. ":" .. action .. " "
- --print(keys.getName(k),action)
- end
- print(str)
- while true do
- if host then
- local e,k = os.pullEvent("key")
- rednet.send(host,k,"key")
- if e == keys.q then shell.exit() end
- else
- print("Enter Host>")
- host = tonumber(read())
- sleep(1)
- term.clear()
- term.setCursorPos(1,1)
- print(str)
- --host = rednet.lookup("stalker")
- end
- end
- end
- if turtle then turtleLoop()
- --elseif args[1] ~= "rednet" and pocket then pocketLoop()
- elseif pocket then pocketRednetLoop()
- else rednetHostLoop() end
Advertisement
Add Comment
Please, Sign In to add comment