Advertisement
mathiaas

move

Dec 21st, 2019 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.22 KB | None | 0 0
  1. args = {...}
  2.  
  3. function split(_string)
  4.   local _arr = {}
  5.   for i in string.gmatch(_string, '([^,]+)') do
  6.     table.insert(_arr,i)
  7.   end
  8.   return _arr
  9. end
  10.  
  11. position = assert(loadfile("getPos"))()
  12.  
  13. if position == nil then
  14.   x = 1
  15.   y = 1
  16.   z = 1
  17.   f = 1
  18. else
  19.   position = split(position)
  20.   x = tonumber(position[1])
  21.   y = tonumber(position[2])
  22.   z = tonumber(position[3])
  23.   f = tonumber(position[4])
  24. end
  25.  
  26. m_table = {
  27.   ["fd"] = function()
  28.     if(turtle.forward()) then
  29.       if (f == 1) then
  30.         z = z+1
  31.       elseif (f == 2) then
  32.         x = x+1
  33.       elseif (f == 3) then
  34.         z = z-1
  35.       elseif (f == 4) then
  36.         x = x-1
  37.       end
  38.       return true
  39.     else return false end
  40.   end,
  41.   ["bk"] = function()
  42.     if(turtle.back()) then
  43.       if (f == 1) then
  44.         z = z-1
  45.       elseif (f == 2) then
  46.         x = x-1
  47.       elseif (f == 3) then
  48.         z = z+1
  49.       elseif (f == 4) then
  50.         x = x+1
  51.       end
  52.       return true
  53.     else return false end
  54.   end,
  55.   ["rt"] = function()
  56.     if(turtle.turnRight()) then
  57.       if (f < 4) then
  58.         f = f+1
  59.       else
  60.        f = 1
  61.       end
  62.       return true
  63.     else return false end
  64.   end,
  65.   ["lt"] = function()
  66.     if(turtle.turnLeft()) then
  67.       if (f > 1) then
  68.         f = f-1
  69.       else
  70.        f = 4
  71.       end
  72.       return true
  73.     else return false end
  74.   end,
  75.   ["up"] = function()
  76.     if(turtle.up()) then
  77.       y = y + 1
  78.       return true
  79.     else return false end
  80.   end,
  81.   ["dn"] = function()
  82.     if(turtle.down()) then
  83.       y = y - 1
  84.       return true
  85.     else
  86.       return false
  87.     end
  88.   end
  89.  
  90. }
  91.  
  92. function move(direction)
  93.   local _move = m_table[direction]
  94.   if (_move) then
  95.     test = _move()
  96.     return test
  97.   end
  98. end
  99.  
  100. function moveCoords(_x,_y,_z,_f)
  101.   moveY(_x,_y,_z,_f)
  102.   moveX(_x,_y,_z,_f)
  103.   moveZ(_x,_y,_z,_f)
  104.   -- Adjust facing
  105.   if (f ~= _f) then
  106.     repeat
  107.       move("lt")
  108.     until(f == _f)
  109.   end
  110.  
  111.   if (x ~= _x or y ~= _y or z ~= _z) then
  112.     moveCoords(_x,_y,_z,_f)
  113.   end
  114. end
  115.  
  116. function moveY(_x,_y,_z,_f)
  117.     -- Height
  118.     while y < _y and move("up") do end
  119.     while y > _y and move("dn") do end
  120.  
  121.     if (y ~= _y) then
  122.       if (math.abs(x-_x)>math.abs(z-_z)) then
  123.         moveX(x+1,_y,_z,_f)
  124.       else
  125.         moveZ(_x,_y,z+1,_f)
  126.       end
  127.       moveY(_x,_y,_z,_f)
  128.     end
  129. end
  130.  
  131. function moveX(_x,_y,_z,_f)
  132.   --Adjust facing for x axis
  133.   if (f ~= 2 and x ~= _x) then
  134.     repeat
  135.       move("rt")
  136.     until(f == 2)
  137.   end
  138.   -- X axis
  139.   while x < _x and move("fd") do end
  140.   while x > _x and move("bk") do end
  141.   if (x ~= _x) then
  142.       moveY(_x,y+1,_z,_f)
  143.       moveX(_x,_y,_z,_f)
  144.   end
  145. end
  146.  
  147. function moveZ(_x,_y,_z,_f)
  148.   -- Adjust facing for z axis
  149.   if (f ~= 1  and z ~= _z) then
  150.     repeat
  151.       move("lt")
  152.     until(f == 1)
  153.   end
  154.   -- Z axis
  155.   while z < _z and move("fd") do end
  156.   while z > _z and move("bk") do end
  157.   if (z ~= _z) then
  158.       moveY(_x,y+1,_z,_f)
  159.       moveZ(_x,_y,_z,_f)
  160.   end
  161. end
  162.  
  163. if (args[2] == nil) then
  164.   move(args[1])
  165. elseif (args[1] ~= nil) then
  166.   moveCoords(tonumber(args[1]),tonumber(args[2]),tonumber(args[3]),tonumber(args[4]))
  167. end
  168.  
  169. file = fs.open("/.pos", "w")
  170.   file.writeLine(x..","..y..","..z..","..f)
  171. file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement