Bjornir90

Rc_turtleside

Dec 8th, 2012
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.57 KB | None | 0 0
  1. local position = {}
  2. table.insert(position, 1)
  3. table.insert(position, 1)
  4. table.insert(position, 1)
  5. table.insert(position, 1)
  6. --1 is up, 2 is right, 3 is down and 4 is left
  7.  
  8. local function clear()
  9. term.clear()
  10. term.setCursorPos(1, 1)
  11. end
  12.  
  13. local function detect()
  14.  if turtle.detect() then
  15.   local out = 1
  16.  else
  17.   local out = 0
  18.  end
  19.  return out
  20. end
  21.  
  22. local function goForward()
  23.  if turtle.forward() then
  24.   rednet.send(idToAccept, "<success:forward>")
  25.   if position[4] == 1 then
  26.    local x, y, z, orientation = position[1], position[2], position[3], position[4]
  27.    local y = y-1
  28.    local block = detect()
  29.    position = {
  30.    x,
  31.    y,
  32.    z,
  33.    orientation,
  34.    block
  35.    }
  36.   elseif position[4] == 2 then
  37.    local x, y, z, orientation = position[1], position[2], position[3], position[4]
  38.    local x = x+1
  39.    local block = detect()
  40.    position = {
  41.    x,
  42.    y,
  43.    z,
  44.    orientation,
  45.    block
  46.    }
  47.   elseif position[4] == 3 then
  48.    local x, y, z, orientation = position[1], position[2], position[3], position[4]
  49.    local y = y+1
  50.    local block = detect()
  51.    position = {
  52.    x,
  53.    y,
  54.    z,
  55.    orientation,
  56.    block
  57.    }
  58.   elseif position[4] == 4 then
  59.    local x, y, z, orientation = position[1], position[2], position[3], position[4]
  60.    local x = x-1
  61.    local block = detect()
  62.    position = {
  63.    x,
  64.    y,
  65.    z,
  66.    orientation,
  67.    block
  68.    }
  69.   end
  70.   local positionS = textutils.serialize(position)
  71.   rednet.send(idToAccept, positionS)
  72.   return "true"
  73.  else
  74.   turtle.attack()
  75.   goForward()
  76.  end
  77. end
  78.  
  79. local function goBack()
  80.  if turtle.back() then
  81.   rednet.send(idToAccept, "<success:back>")
  82.   if position[4] == 1 then
  83.    local x, y, z, orientation = position[1], position[2], position[3], position[4]
  84.    local y = y+1
  85.    local block = detect()
  86.    position = {
  87.    x,
  88.    y,
  89.    z,
  90.    orientation,
  91.    block
  92.    }
  93.   elseif position[4] == 2 then
  94.    local x, y, z, orientation = position[1], position[2], position[3], position[4]
  95.    local x = x-1
  96.    local block = detect()
  97.    position = {
  98.    x,
  99.    y,
  100.    z,
  101.    orientation,
  102.    block
  103.    }
  104.   elseif position[4] == 3 then
  105.    local x, y, z, orientation = position[1], position[2], position[3], position[4]
  106.    local y = y-1
  107.    local block = detect()
  108.    position = {
  109.    x,
  110.    y,
  111.    z,
  112.    orientation,
  113.    block
  114.    }
  115.   elseif position[4] == 4 then
  116.    local x, y, z, orientation = position[1], position[2], position[3], position[4]
  117.    local x = x+1
  118.    local block = detect()
  119.    position = {
  120.    x,
  121.    y,
  122.    z,
  123.    orientation,
  124.    block
  125.    }
  126.   end
  127.   local positionS = textutils.serialize(position)
  128.   rednet.send(idToAccept, positionS)
  129.   return "true"
  130.  else
  131.   goBack()
  132.  end
  133. end
  134.  
  135. local function goUp()
  136.  if turtle.up() then
  137.   rednet.send(idToAccept, "<success:up>")
  138.    local x, y, z, orientation = position[1], position[2], position[3], position[4]
  139.    local z = z+1
  140.    local block = detect()
  141.    position = {
  142.    x,
  143.    y,
  144.    z,
  145.    orientation,
  146.    block
  147.    }
  148.    local positionS = textutils.serialize(position)
  149.    rednet.send(idToAccept, positionS)
  150.   return "true"
  151.  else
  152.   turtle.attackUp()
  153.   goUp()
  154.  end
  155. end
  156.  
  157. local function goDown()
  158.  if turtle.down() then
  159.   rednet.send(idToAccept, "<success:down>")
  160.    local x, y, z, orientation = position[1], position[2], position[3], position[4]
  161.    local z = z-1
  162.    local block = detect()
  163.    position = {
  164.    x,
  165.    y,
  166.    z,
  167.    orientation,
  168.    block
  169.    }
  170.    local positionS = textutils.serialize(position)
  171.    rednet.send(idToAccept, positionS)
  172.   return "true"
  173.  else
  174.   turtle.attackDown()
  175.   goDown()
  176.  end
  177. end
  178.  
  179. local function selectInv()
  180.  local idSender, msg = rednet.receive()
  181.  if idSender == idToAccept then
  182.   local slot = tonumber(msg)
  183.   turtle.select(slot)
  184.   rednet.send(idToAccept, "<success:select>")
  185.  else
  186.   rednet.send(tonumber(idSender), "<print$error:you are not the autorised sender$>")
  187.   return
  188.  end
  189. end
  190.  
  191. local function turnRight()
  192.  turtle.turnRight()
  193.   local x, y, z, orientation = position[1], position[2], position[3], position[4]
  194.   if orientation+1 < 5
  195.    orientation = orientation+1
  196.   else
  197.    orientation = 1
  198.   end
  199.   local block = detect()
  200.   position = {
  201.   x,
  202.   y,
  203.   z,
  204.   orientation,
  205.   block
  206.   }
  207.   local positionS = textutils.serialize(position)
  208.   rednet.send(idToAccept, positionS)
  209. end
  210.  
  211. local function turnLeft()
  212.  turtle.turnLeft()
  213.   local x, y, z, orientation = position[1], position[2], position[3], position[4]
  214.   if orientation-1 > 0
  215.    orientation = orientation-1
  216.   else
  217.    orientation = 4
  218.   end
  219.   local block = detect()
  220.   position = {
  221.   x,
  222.   y,
  223.   z,
  224.   orientation,
  225.   block
  226.   }
  227.   local positionS = textutils.serialize(position)
  228.   rednet.send(idToAccept, positionS)
  229. end
  230.  
  231. print("Listening for a ping request...")
  232. local id, ping_message = rednet.receive()
  233. if ping_message == "#/ping#" then
  234.  idToAccept = tonumber(id)
  235.  rednet.send(idToAccept, "<ping>")
  236.  clear()
  237. end
  238. while true do
  239.  local idSender, msg = rednet.receive()
  240.  if idToAccept == tonumber(idSender) then
  241.   if msg == "#/dig#" then
  242.    turtle.dig()
  243.   elseif msg == "#/forward#" then
  244.    print("Going forward ...")
  245.    goForward()
  246.   elseif msg == "#/back#" then
  247.    print("Going back ...")
  248.    goBack()
  249.   elseif msg == "#/down#" then
  250.    goDown()
  251.   elseif msg  == "#/up#" then
  252.    goUp()
  253.   elseif msg == "#/right#" then
  254.    turnRight()
  255.   elseif msg == "#/left#" then
  256.    turnLeft()
  257.   elseif msg == "#/select#" then
  258.    selectInv()
  259.   else
  260.    print("Unknow command")
  261.   end
  262.  else
  263.   print("Wrong sender")
  264.   print(idToAccept)
  265.   print(idSender)
  266.  end
  267. end
Advertisement
Add Comment
Please, Sign In to add comment