Advertisement
Guest User

startup

a guest
Feb 22nd, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.36 KB | None | 0 0
  1. function updateDir()
  2.   local a = fs.open("direction", "w")
  3.   a.writeLine(dir)
  4.   a.close()
  5. end
  6. function sendUpdate()
  7.   upX, upY, upZ = gps.locate()
  8.   rednet.send(2, "updatePos")
  9.   rednet.send(2, upX)
  10.   rednet.send(2, upY)
  11.   rednet.send(2, upZ)
  12. end
  13. function getUpdate()
  14.   x, y, z = gps.locate()
  15.   local a = fs.open("direction", "r")
  16.   dir = a.readLine()
  17.   print(dir)
  18.   a.close()
  19. end
  20. function turnTo(newDir)
  21.   while dir ~= newDir do
  22.     turn("right")
  23.   end
  24. end
  25. function turn(relDir)
  26.   if relDir == "right" then
  27.     turtle.turnRight()
  28.     if dir == "south" then
  29.       dir = "west"
  30.     elseif dir == "east" then
  31.       dir = "south"
  32.     elseif dir == "north" then
  33.       dir = "east"
  34.     elseif dir == "west" then
  35.       dir = "north"
  36.     end
  37.   elseif relDir == "left" then
  38.     turtle.turnLeft()
  39.     if dir == "south" then
  40.       dir = "east"
  41.     elseif dir == "east" then
  42.       dir = "north"
  43.     elseif dir == "north" then
  44.       dir = "west"
  45.     elseif dir == "west" then
  46.       dir = "south"
  47.     end
  48.   end
  49.   updateDir()
  50. end
  51. function goTo(newX, newY, newZ)
  52.   print(x)
  53.   print(newX)
  54.   newX = tonumber(newX)
  55.   newY = tonumber(newY)
  56.   newZ = tonumber(newZ)
  57.   if x>newX then
  58.       turnTo("west")
  59.     while x>newX do
  60.       turtle.forward()
  61.       x = x - 1
  62.     end
  63.   elseif x<newX then
  64.     turnTo("east")
  65.     while x<newX do
  66.       turtle.forward()
  67.       x = x + 1
  68.     end
  69.   end
  70.   if z>newZ then
  71.     turnTo("north")
  72.     while z>newZ do
  73.       turtle.forward()
  74.       z = z - 1
  75.     end
  76.   elseif z<newZ then
  77.     turnTo("south")
  78.     while z<newZ do
  79.       turtle.forward()
  80.       z = z + 1
  81.     end
  82.   end
  83.   if y>newY then
  84.     while y>newY do
  85.       turtle.down()
  86.       y = y - 1
  87.     end
  88.   elseif y<newY then
  89.     while y<newY do
  90.       turtle.up()
  91.       y = y + 1
  92.     end
  93.   end
  94. end
  95. rednet.open("left")
  96. getUpdate()
  97. local cmd_table =
  98. {
  99.   ["digDown"] = function()
  100.       turtle.digDown()
  101.   end,
  102.   ["digUp"] = function()
  103.       turtle.digUp()
  104.   end,
  105.   ["dig"] = function()
  106.       turtle.dig()
  107.   end,
  108. }
  109. while true do
  110.   id, cmd = rednet.receive()
  111.   print(cmd)
  112.   if cmd == "goTo" then
  113.     id, xyz = rednet.receive()
  114.     print(xyz)
  115.     locX = tonumber(xyz[1])
  116.     locY = tonumber(xyz[2])
  117.     locZ = tonumber(xyz[3])
  118.     print(locX, locY, locZ)
  119.     goTo(locX, locY, locZ)
  120.     sendUpdate()
  121.   else
  122.     cmd_table[cmd]()
  123.   end
  124. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement