caucow

turtlectrl

Dec 28th, 2016 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.33 KB | None | 0 0
  1. local m = peripheral.find("modem")
  2. local senderID = os.getComputerLabel() == nil and "ID:" .. os.getComputerID() or os.getComputerLabel()
  3. local channels = {
  4.       ["stdout"] = 1000,
  5.       ["stdin"] = 1001,
  6.       ["witherout"] = 1010,
  7.       ["witherin"] = 1011
  8.     }
  9. m.open(channels.stdin)
  10.  
  11. local xTime = os.clock()
  12.  
  13. local function checkStop()
  14.   os.startTimer(0.15)
  15.   local a, b, c, d, e
  16.   repeat
  17.     evt, face, sendCh, replyCh, msg, dist = os.pullEvent()
  18.     if evt == "modem_message" and msg.type == "cmd" and msg.cmd == "sigterm" and (msg.receiver == nil or (type(msg.receiver) == "string" and string.match(senderID, msg.receiver))) then
  19.       return true
  20.     end
  21.   until evt == "timer"
  22.   return false
  23. end
  24.  
  25. local function log(...)
  26.   m.transmit(channels.stdout, channels.stdin,
  27.       {
  28.           ["program"] = "control",
  29.           ["type"] = "info",
  30.           ["text"] = {...},
  31.           ["sender"] = senderID
  32.       })
  33. end
  34.  
  35. local function error(...)
  36.   m.transmit(channels.stdout, channels.stdin,
  37.       {
  38.           ["program"] = "control",
  39.           ["type"] = "error",
  40.           ["text"] = {...},
  41.           ["sender"] = senderID
  42.       })
  43. end
  44.  
  45. local dig = false
  46. local digup = false
  47. local digdown = false
  48. local persist = false
  49.  
  50. local function tryRefuel(amount)
  51.   amount = type(amount) == "number" and amount or 1
  52.   if turtle.getFuelLevel() < amount then
  53.     for i = 1, 16 do
  54.       turtle.select(i)
  55.       turtle.refuel(1)
  56.       if turtle.getFuelLevel() >= amount then
  57.         break
  58.       end
  59.     end
  60.     turtle.select(1)
  61.     if turtle.getFuelLevel() < amount then
  62.       error("Out of fuel.")
  63.       return false
  64.     end
  65.   end
  66.   return true
  67. end
  68.  
  69. local function up()
  70.   while not turtle.up() do
  71.     if dig then
  72.       turtle.digUp()
  73.     elseif not persist or checkStop() then
  74.       return false
  75.     end
  76.   end
  77.   return true
  78. end
  79.  
  80. local function down()
  81.   while not turtle.down() do
  82.     if digdown then
  83.       turtle.digDown()
  84.     elseif not persist or checkStop() then
  85.       return false
  86.     end
  87.   end
  88.   return true
  89. end
  90.  
  91. local function forward()
  92.   while not turtle.forward() do
  93.     if dig then
  94.       turtle.dig()
  95.     elseif not persist or checkStop() then
  96.       return false
  97.     end
  98.   end
  99.   if digup then
  100.     turtle.digUp()
  101.   end
  102.   if digdown then
  103.     turtle.digDown()
  104.   end
  105.   return true
  106. end
  107.  
  108. local function back()
  109.   while not turtle.back() do
  110.     if not persist or checkStop() then
  111.       return false
  112.     end
  113.   end
  114.   return true
  115. end
  116.  
  117. while true do
  118.   evt, face, sendCh, replyCh, msg, dist = os.pullEvent("modem_message")
  119.   if type(msg) == "table" and msg.program == "control" and (msg.receiver == nil or (type(msg.receiver) == "string" and string.match(senderID, msg.receiver))) then
  120.     if msg.type == "cmd" then
  121.       if msg.cmd == "run" then
  122.         shell.run(unpack(msg.text))
  123.       elseif msg.cmd == "set" then
  124.         if msg.text[1] == "dig" then
  125.           dig = msg.text[2] == "on"
  126.         elseif msg.text[1] == "digup" then
  127.           digup = msg.text[2] == "on"
  128.         elseif msg.text[1] == "digdown" then
  129.           digdown = msg.text[2] == "on"
  130.         elseif msg.text[1] == "persist" then
  131.           persist = msg.text[2] == "on"
  132.         end
  133.       elseif msg.cmd == "move" then
  134.         if msg.text[1] == "up" and tryRefuel(msg.text[2]) then
  135.           for i = 1, msg.text[2] do
  136.             if not up() then
  137.               break
  138.             end
  139.           end
  140.         elseif msg.text[1] == "down" and tryRefuel(msg.text[2]) then
  141.           for i = 1, msg.text[2] do
  142.             if not down() then
  143.               break
  144.             end
  145.           end
  146.         elseif msg.text[1] == "frwd" and tryRefuel(msg.text[2]) then
  147.           for i = 1, msg.text[2] do
  148.             if not forward() then
  149.               break
  150.             end
  151.           end
  152.         elseif msg.text[1] == "back" and tryRefuel(msg.text[2]) then
  153.           for i = 1, msg.text[2] do
  154.             if not back() then
  155.               break
  156.             end
  157.           end
  158.         elseif msg.text[1] == "left" then
  159.           turtle.turnLeft()
  160.         elseif msg.text[1] == "right" then
  161.           turtle.turnRight()
  162.         end
  163.       elseif msg.cmd == "get" then
  164.         if msg.text[1] == "fuel" then
  165.           log("Fuel:"..turtle.getFuelLevel().." "..turtle.getItemCount(1))
  166.         end
  167.       end
  168.     end
  169.   end
  170. end
Add Comment
Please, Sign In to add comment