Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle RC Voice Command [OP Edition] - Turtle/Receiver
- -- Author: theonlycozzy
- -- Nov 25 2013
- local BUILD = 7
- local running = true
- local dir = {1,-1,-1,1}
- local Args, meta = {...}, {}
- ----------------------------------------------------
- -- Functions
- local logBug = function(TEXT,ERROR)
- local timeStamp = "["..os.day().."] "..os.time().."| "
- if meta.deBug then
- local f = fs.open('debug.txt','a')
- f.writeLine(timeStamp..TEXT)
- f.close()
- end
- if ERROR then error(TEXT,0) end
- end
- local open = function(FILE)
- local f = fs.open(tostring(FILE),"r")
- if not f then return false end
- local data = f.readAll()
- f.close()
- if data then return data end
- end
- local openT = function(FILE)
- local data = open(FILE)
- if data then
- data = textutils.unserialize(data)
- return data
- end
- end
- local save = function(PATH,DATA,APPEND)
- local f
- if APPEND then
- f = fs.open(tostring(PATH),"a")
- f.writeLine(DATA)
- else
- f = fs.open(tostring(PATH),"w")
- f.write(DATA)
- end
- f.close()
- end
- local saveT = function(PATH,TABLE)
- save(PATH,textutils.serialize(TABLE))
- end
- local tLeft = function()
- if turtle.turnLeft() then meta.pos[4] = (meta.pos[4] - 1)%4 end
- end
- local tRight = function()
- if turtle.turnRight() then meta.pos[4] = (meta.pos[4] + 1)%4 end
- end
- local tForward = function()
- if turtle.forward() then
- if meta.pos[4] == 0 or meta.pos[4] == 2 then -- NORTH or SOUTH along Z AXIS
- meta.pos[2] = meta.pos[2] + dir[meta.pos[4]+1]
- elseif meta.pos[4] == 1 or meta.pos[4] == 3 then -- EAST or WEST along X AXIS
- meta.pos[1] = meta.pos[1] + dir[meta.pos[4]+1]
- end
- meta.fuelLevel = turtle.getFuelLevel()
- saveT("turtle.properties",meta)
- return true
- else
- return false
- end
- end
- local tBack = function()
- if turtle.back() then
- if meta.pos[4] == 0 or meta.pos[4] == 2 then -- NORTH or SOUTH along Z AXIS
- meta.pos[2] = meta.pos[2] - dir[meta.pos[4]+1]
- elseif meta.pos[4] == 1 or meta.pos[4] == 3 then -- EAST or WEST along X AXIS
- meta.pos[1] = meta.pos[1] - dir[meta.pos[4]+1]
- end
- return true
- else
- return false
- end
- end
- local tAround = function()
- local rand = math.random()
- if rand > 0.5 then tRight() tRight() else tLeft() tLeft() end
- end
- local tUp = function()
- if turtle.up() then
- meta.pos[3] = meta.pos[3] + 1
- return true
- else
- return false
- end
- end
- local tDown = function()
- if turtle.down() then
- meta.pos[3] = meta.pos[3] - 1
- return true
- else
- return false
- end
- end
- local walk = function(DIR,ITERATION)
- if ITERATION == nil then ITERATION = 1 end
- if DIR == "left" then tLeft() end
- if DIR == "right" then tRight() end
- for i=1,ITERATION do
- local isMoving = false
- while not(isMoving) do
- if DIR == "forward" or DIR == "left" or DIR == "right" then isMoving = tForward() end
- if DIR == "back" then isMoving = tBack() end
- if DIR == "up" then isMoving = tUp() elseif DIR == "down" then isMoving = tDown() end
- end
- end
- end
- local orientate = function(tF)
- local cF = meta.pos[4]
- if tF == nil then return false end
- if tF == ((cF-2)%4) then tAround() end
- if tF == ((cF+1)%4) then tRight() end
- if tF == ((cF+3)%4) then tLeft() end
- end
- local goTo = function(X,Z,Y,F,MODE)
- local targX = tonumber(X)
- local targZ = tonumber(Z)
- local targY = tonumber(Y)
- local targF = tonumber(F)
- if meta.pos[1] > targX then -- if Target is West
- orientate(1)
- while meta.pos[1] > targX do tForward() end
- else -- if Target is East
- orientate(3)
- while meta.pos[1] < targX do tForward() end
- end
- if meta.pos[2] > targZ then -- if Target is South
- orientate(2)
- while meta.pos[2] > targZ do tForward() end
- else -- if Target is North
- orientate(0)
- while meta.pos[2] < targZ do tForward() end
- end
- if meta.pos[3] < targY then
- while meta.pos[3] < targY do
- tUp()
- end
- else
- while meta.pos[3] > targY do
- tDown()
- end
- end
- if targF ~= nil then orientate(targF) end
- end
- ----------------------------------------------------
- -- INIT
- if Args[1] == "-u" or Args[1] == "-uninstall" then
- if fs.exists('debug.txt') then
- fs.delete('debug.txt')
- end
- if fs.exists('turtle.properties') then
- fs.delete('turtle.properties')
- end
- return false
- end
- term.clear() term.setCursorPos(1,1)
- if fs.exists("turtle.properties") then
- meta = openT("turtle.properties")
- print("Turtle Properties File Loaded")
- else --X Z Y F
- meta = {deBug=false, machineID = os.getComputerID(),pos={0,0,0,0},fuelLevel=turtle.getFuelLevel(),}
- print("Turtle Properties File Created")
- saveT("turtle.properties",meta)
- end
- if Args[1] == "-d" or Args[1] == "-debug" then
- meta.deBug = true
- end
- ----------------------------------------------------
- -- START
- if peripheral.getType("right") == "modem" then
- local MOD = peripheral.wrap("right") MOD.open(meta.machineID)
- if meta.hostChannel then
- print("Host is: "..meta.hostChannel)
- else
- term.setCursorPos(1,2) write("Host Channel:")
- local ans = tonumber(read())
- if type(ans) == "number" then
- meta["hostChannel"] = ans
- saveT("turtle.properties",meta)
- local pakg = {ptl="connect", cType="new", data={fuelLevel=turtle.getFuelLevel()}}
- MOD.transmit(meta.hostChannel,meta.machineID,textutils.serialize(pakg))
- logBug("Establishing new Connection: "..textutils.serialize(meta))
- end
- end
- logBug("-------------------------- RC Turtle Build ["..BUILD.."] INITIATED --------------------------")
- while running do
- local EV = {os.pullEvent()}
- if EV[1] == "modem_message" then
- local pakg = textutils.unserialize(EV[5])
- if type(pakg) == "table" then
- logBug("Received : "..textutils.serialize(pakg))
- if pakg.ptl then
- if pakg.ptl == "movement" then
- if pakg.cType == "walk" then -- Movement Commands
- for num,cmd in pairs(pakg.data) do
- local N = tonumber(cmd)
- if type(N) == "number" then
- walk(pakg.data[num-1],N-1)
- elseif cmd == "turn" then
- walk(pakg.data[num+1])
- table.remove(pakg.data,num+1)
- else
- walk(cmd)
- end
- end
- logBug("Walk command successfully returned")
- elseif pakg.cType == "goto" then
- goTo(pakg.data[1],pakg.data[2],pakg.data[3],pakg.data[4])
- logBug("goto Command successfully returned")
- elseif pakg.cType == "turn" then
- if pakg.data[1] == "right" then tRight() elseif pakg.data[1] == "left" then tLeft() elseif pakg.data[1] == "around" then tAround() end
- end
- -- END protocol movement
- elseif pakg.ptl == "connect" then
- logBug("Connection Protocol Received")
- if pakg.cType == "renew" then
- logBug("Renewing Connection via Server-side")
- sleep(0)
- MOD.transmit(meta.hostChannel,meta.machineID,"pong")
- end
- -- END protocol connect
- elseif pakg.ptl == "terminate" then
- MOD.closeAll()
- running = false
- -- END protocol kill
- else
- -- OTHER Protocol handling
- logBug("Turtle Received unknown Protocol")
- end
- else
- -- Error Handling for invalid protocol
- logBug("Turtle Received invalid protocol")
- end -- END of if pakg.ptl
- else
- -- Error Handling to send back to server for incorrect package
- logBug("Turtle Received incorrect package data type")
- end -- END OF type(pakg) == TABLE
- end
- sleep(0)
- end
- else error("Wireless Modem Required") end
- -------- TERMINATION CODE --------
- saveT("turtle.properties",meta)
- term.clear()
- term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment