Advertisement
mathiaas

s02_movement

Aug 2nd, 2020 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.80 KB | None | 0 0
  1. position = split(getPos())
  2.  
  3. x = tonumber(position[1])
  4. y = tonumber(position[2])
  5. z = tonumber(position[3])
  6. f = tonumber(position[4])
  7.  
  8.  
  9. m_table = {
  10.   ["fd"] = function()
  11.     if(turtle.forward()) then
  12.       if (f == 1) then
  13.         z = z+1
  14.       elseif (f == 2) then
  15.         x = x+1
  16.       elseif (f == 3) then
  17.         z = z-1
  18.       elseif (f == 4) then
  19.         x = x-1
  20.       end
  21.       return true
  22.     else return false end
  23.   end,
  24.   ["bk"] = function()
  25.     if(turtle.back()) then
  26.       if (f == 1) then
  27.         z = z-1
  28.       elseif (f == 2) then
  29.         x = x-1
  30.       elseif (f == 3) then
  31.         z = z+1
  32.       elseif (f == 4) then
  33.         x = x+1
  34.       end
  35.       return true
  36.     else return false end
  37.   end,
  38.   ["rt"] = function()
  39.     if(turtle.turnRight()) then
  40.       if (f < 4) then
  41.         f = f+1
  42.       else
  43.        f = 1
  44.       end
  45.       return true
  46.     else return false end
  47.   end,
  48.   ["lt"] = function()
  49.     if(turtle.turnLeft()) then
  50.       if (f > 1) then
  51.         f = f-1
  52.       else
  53.        f = 4
  54.       end
  55.       return true
  56.     else return false end
  57.   end,
  58.   ["up"] = function()
  59.     if(turtle.up()) then
  60.       y = y + 1
  61.       return true
  62.     else return false end
  63.   end,
  64.   ["dn"] = function()
  65.     if(turtle.down()) then
  66.       y = y - 1
  67.       return true
  68.     else
  69.       return false
  70.     end
  71.   end
  72.  
  73. }
  74.  
  75. function move(direction)
  76.   local _move = m_table[direction]
  77.   if (_move) then
  78.     if _move() then
  79.       setPos(x,y,z,f)
  80.       return true
  81.     else
  82.       return false
  83.     end
  84.   end
  85. end
  86.  
  87. function moveF(destination)
  88.   destination = tonumber(destination)
  89.   path = {}
  90.   if getFPos() == destination then return end
  91.   repeat
  92.     if  getFPos() % destination == 1 then
  93.       move("lt")
  94.       table.insert(path,"lt")
  95.     else
  96.       move("rt")
  97.       table.insert(path,"rt")
  98.     end
  99.    
  100.   until getFPos() == destination
  101.   return path
  102. end
  103.  
  104. function moveY(destination)
  105.   destination = tonumber(destination)
  106.   if getYPos() == destination then return end
  107.   if  (getYPos() < destination) then
  108.     repeat
  109.       if not move("up") then
  110.         return
  111.       end
  112.     until getYPos() == destination
  113.   elseif (getYPos() > destination) then
  114.     repeat
  115.       if not move("dn") then
  116.         return
  117.       end
  118.     until getYPos() == destination
  119.   end
  120. end
  121.  
  122. function moveT(instructions)
  123.   if  type(instructions) ~= "table" then return false end
  124.  
  125.   for k,v in pairs(instructions) do
  126.     move(v)
  127.   end
  128. end
  129.  
  130. function inversePath(path)
  131.   if  type(path) ~= "table" then return end
  132.  
  133.   for k,v in pairs(path) do
  134.     if  path[k] == "fd" then
  135.       path[k] = "bk"
  136.     elseif path[k] == "bk" then
  137.       path[k] = "fd"
  138.     elseif path[k] == "rt" then
  139.       path[k] = "lt"
  140.     elseif path[k] == "lt" then
  141.       path[k] = "rt"
  142.     end
  143.   end
  144.  
  145.   return path
  146. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement