cozzimoto

TRCVC - Turtle/Receiver B:7

Nov 11th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.98 KB | None | 0 0
  1. -- Turtle RC Voice Command [OP Edition] - Turtle/Receiver
  2. -- Author: theonlycozzy
  3. -- Nov 25 2013
  4.  
  5. local BUILD = 7
  6. local running = true
  7. local dir = {1,-1,-1,1}
  8. local Args, meta = {...}, {}
  9. ----------------------------------------------------
  10. -- Functions
  11. local logBug = function(TEXT,ERROR)
  12.   local timeStamp = "["..os.day().."] "..os.time().."| "
  13.  
  14.   if meta.deBug then
  15.     local f = fs.open('debug.txt','a')
  16.     f.writeLine(timeStamp..TEXT)
  17.     f.close()
  18.   end
  19.   if ERROR then error(TEXT,0) end
  20. end
  21. local open = function(FILE)
  22.   local f = fs.open(tostring(FILE),"r")
  23.   if not f then return false end
  24.   local data = f.readAll()
  25.   f.close()
  26.   if data then return data end
  27. end
  28. local openT = function(FILE)
  29.   local data = open(FILE)
  30.  
  31.   if data then
  32.     data = textutils.unserialize(data)
  33.     return data
  34.   end
  35. end
  36. local save = function(PATH,DATA,APPEND)
  37.   local f
  38.   if APPEND then
  39.     f = fs.open(tostring(PATH),"a")
  40.     f.writeLine(DATA)
  41.   else
  42.     f = fs.open(tostring(PATH),"w")
  43.     f.write(DATA)
  44.   end
  45.   f.close()
  46. end
  47. local saveT = function(PATH,TABLE)
  48.   save(PATH,textutils.serialize(TABLE))
  49. end
  50. local tLeft = function()
  51.   if turtle.turnLeft() then meta.pos[4] = (meta.pos[4] - 1)%4 end
  52. end
  53. local tRight = function()
  54.   if turtle.turnRight() then meta.pos[4] = (meta.pos[4] + 1)%4 end
  55. end
  56. local tForward = function()
  57.   if turtle.forward() then
  58.     if meta.pos[4] == 0 or meta.pos[4] == 2 then -- NORTH or SOUTH along Z AXIS
  59.       meta.pos[2] = meta.pos[2] + dir[meta.pos[4]+1]
  60.     elseif meta.pos[4] == 1 or meta.pos[4] == 3 then -- EAST or WEST along X AXIS
  61.       meta.pos[1] = meta.pos[1] + dir[meta.pos[4]+1]
  62.     end
  63.     meta.fuelLevel = turtle.getFuelLevel()
  64.     saveT("turtle.properties",meta)
  65.     return true
  66.   else
  67.     return false
  68.   end
  69. end
  70. local tBack = function()
  71.   if turtle.back() then
  72.     if meta.pos[4] == 0 or meta.pos[4] == 2 then -- NORTH or SOUTH along Z AXIS
  73.       meta.pos[2] = meta.pos[2] - dir[meta.pos[4]+1]
  74.     elseif meta.pos[4] == 1 or meta.pos[4] == 3 then -- EAST or WEST along X AXIS
  75.       meta.pos[1] = meta.pos[1] - dir[meta.pos[4]+1]
  76.     end
  77.     return true
  78.   else
  79.     return false
  80.   end
  81. end
  82. local tAround = function()
  83.   local rand = math.random()
  84.   if rand > 0.5 then tRight() tRight() else tLeft() tLeft() end
  85. end
  86. local tUp = function()
  87.   if turtle.up() then
  88.     meta.pos[3] = meta.pos[3] + 1
  89.     return true
  90.   else
  91.     return false
  92.   end
  93. end
  94. local tDown = function()
  95.   if turtle.down() then
  96.     meta.pos[3] = meta.pos[3] - 1
  97.     return true
  98.   else
  99.     return false
  100.   end
  101. end
  102. local walk = function(DIR,ITERATION)
  103.   if ITERATION == nil then ITERATION = 1 end
  104.  
  105.   if DIR == "left" then tLeft() end
  106.   if DIR == "right" then tRight() end
  107.   for i=1,ITERATION do
  108.     local isMoving = false
  109.     while not(isMoving) do
  110.       if DIR == "forward" or DIR == "left" or DIR == "right" then isMoving = tForward() end
  111.       if DIR == "back" then isMoving = tBack() end
  112.       if DIR == "up" then isMoving = tUp() elseif DIR == "down" then isMoving = tDown() end      
  113.     end
  114.   end
  115. end
  116. local orientate = function(tF)
  117.   local cF = meta.pos[4]
  118.   if tF == nil then return false end
  119.   if tF == ((cF-2)%4) then tAround() end
  120.   if tF == ((cF+1)%4) then tRight() end
  121.   if tF == ((cF+3)%4) then tLeft() end
  122. end
  123. local goTo = function(X,Z,Y,F,MODE)
  124.   local targX = tonumber(X)
  125.   local targZ = tonumber(Z)
  126.   local targY = tonumber(Y)
  127.   local targF = tonumber(F)
  128.  
  129.   if meta.pos[1] > targX then -- if Target is West
  130.     orientate(1)
  131.     while meta.pos[1] > targX do tForward() end
  132.   else -- if Target is East
  133.     orientate(3)
  134.     while meta.pos[1] < targX do tForward() end
  135.   end
  136.   if meta.pos[2] > targZ then -- if Target is South
  137.     orientate(2)
  138.     while meta.pos[2] > targZ do tForward() end
  139.   else -- if Target is North
  140.     orientate(0)
  141.     while meta.pos[2] < targZ do tForward() end
  142.   end
  143.   if meta.pos[3] < targY then
  144.     while meta.pos[3] < targY do
  145.       tUp()
  146.     end
  147.   else
  148.     while meta.pos[3] > targY do
  149.       tDown()
  150.     end
  151.   end  
  152.   if targF ~= nil then orientate(targF) end
  153. end
  154. ----------------------------------------------------
  155. -- INIT
  156. if Args[1] == "-u" or Args[1] == "-uninstall" then
  157.   if fs.exists('debug.txt') then
  158.     fs.delete('debug.txt')
  159.   end
  160.   if fs.exists('turtle.properties') then
  161.     fs.delete('turtle.properties')
  162.   end
  163.   return false
  164. end
  165. term.clear() term.setCursorPos(1,1)
  166. if fs.exists("turtle.properties") then
  167.   meta = openT("turtle.properties")
  168.   print("Turtle Properties File Loaded")
  169. else                                  --X Z Y F
  170.   meta = {deBug=false, machineID = os.getComputerID(),pos={0,0,0,0},fuelLevel=turtle.getFuelLevel(),}
  171.   print("Turtle Properties File Created")
  172.   saveT("turtle.properties",meta)
  173. end
  174. if Args[1] == "-d" or Args[1] == "-debug" then
  175.   meta.deBug = true
  176. end
  177. ----------------------------------------------------
  178. -- START
  179. if peripheral.getType("right") == "modem" then
  180.   local MOD = peripheral.wrap("right") MOD.open(meta.machineID)
  181.   if meta.hostChannel then
  182.     print("Host is: "..meta.hostChannel)
  183.   else
  184.     term.setCursorPos(1,2) write("Host Channel:")
  185.     local ans = tonumber(read())
  186.     if type(ans) == "number" then
  187.       meta["hostChannel"] = ans
  188.       saveT("turtle.properties",meta)
  189.       local pakg = {ptl="connect", cType="new", data={fuelLevel=turtle.getFuelLevel()}}
  190.       MOD.transmit(meta.hostChannel,meta.machineID,textutils.serialize(pakg))
  191.       logBug("Establishing new Connection: "..textutils.serialize(meta))
  192.     end
  193.   end
  194.   logBug("-------------------------- RC Turtle Build ["..BUILD.."] INITIATED --------------------------")
  195.   while running do
  196.     local EV = {os.pullEvent()}
  197.     if EV[1] == "modem_message" then
  198.       local pakg = textutils.unserialize(EV[5])
  199.       if type(pakg) == "table" then
  200.         logBug("Received : "..textutils.serialize(pakg))
  201.         if pakg.ptl then
  202.           if pakg.ptl == "movement" then
  203.             if pakg.cType == "walk" then -- Movement Commands
  204.            
  205.               for num,cmd in pairs(pakg.data) do
  206.                 local N = tonumber(cmd)
  207.                 if type(N) == "number" then
  208.                   walk(pakg.data[num-1],N-1)
  209.                 elseif cmd == "turn" then
  210.                   walk(pakg.data[num+1])
  211.                   table.remove(pakg.data,num+1)
  212.                 else
  213.                   walk(cmd)
  214.                 end
  215.               end
  216.               logBug("Walk command successfully returned")
  217.              
  218.             elseif pakg.cType == "goto" then
  219.               goTo(pakg.data[1],pakg.data[2],pakg.data[3],pakg.data[4])
  220.               logBug("goto Command successfully returned")
  221.              
  222.             elseif pakg.cType == "turn" then
  223.               if pakg.data[1] == "right" then tRight() elseif pakg.data[1] == "left" then tLeft() elseif pakg.data[1] == "around" then tAround() end
  224.             end
  225.           -- END protocol movement
  226.           elseif pakg.ptl == "connect" then
  227.             logBug("Connection Protocol Received")
  228.             if pakg.cType == "renew" then
  229.               logBug("Renewing Connection via Server-side")
  230.               sleep(0)
  231.               MOD.transmit(meta.hostChannel,meta.machineID,"pong")
  232.             end
  233.           -- END protocol connect
  234.           elseif pakg.ptl == "terminate" then
  235.             MOD.closeAll()
  236.             running = false
  237.           -- END protocol kill
  238.           else
  239.             -- OTHER Protocol handling
  240.             logBug("Turtle Received unknown Protocol")
  241.           end
  242.         else
  243.           -- Error Handling for invalid protocol
  244.           logBug("Turtle Received invalid protocol")
  245.         end -- END of if pakg.ptl
  246.       else
  247.         -- Error Handling to send back to server for incorrect package
  248.         logBug("Turtle Received incorrect package data type")        
  249.       end -- END OF type(pakg) == TABLE
  250.     end
  251.     sleep(0)
  252.   end
  253. else error("Wireless Modem Required") end
  254. -------- TERMINATION CODE --------
  255. saveT("turtle.properties",meta)
  256. term.clear()
  257. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment