Advertisement
louwding

Turtle GPS

Apr 10th, 2023 (edited)
67
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.23 KB | Gaming | 0 0
  1. Directions = {
  2.     Forward = 0,
  3.     Right = 1,
  4.     Backwards = 2,
  5.     Left = 3
  6. }
  7. local file_position = "saves/position"
  8.  
  9. Direction = Directions.Forward -- 0 the default moveForward direction (direction placed), 1 right, 2 back, 3 left
  10. Local_x = 0
  11. Local_y = 0
  12. Local_z = 0
  13.  
  14. Auto_refuel = true
  15.  
  16. LastPos = {
  17.     Local_x = 0,
  18.     Local_y = 0,
  19.     Local_z = 0,
  20.     Direction = Directions.Forward
  21. }
  22.  
  23. function SetLastPosition()
  24.     if LastPos.Local_x == 0 and LastPos.Local_y == 0 and LastPos.Local_z == 0 then
  25.         LastPos.Local_x = Local_x
  26.         LastPos.Local_y = Local_y
  27.         LastPos.Local_z = Local_z
  28.         LastPos.Direction = Direction
  29.     end
  30. end
  31.  
  32. function MoveToLastPos(order)
  33.     if LastPos.Local_x ~= 0 or LastPos.Local_y ~= 0 or LastPos.Local_z ~= 0 then
  34.  
  35.         moveTo(LastPos.Local_x, LastPos.Local_y, LastPos.Local_z, order)
  36.         turnToDirection(LastPos.Direction)
  37.  
  38.         LastPos = {
  39.             Local_x = 0,
  40.             Local_y = 0,
  41.             Local_z = 0,
  42.             Direction = Directions.Forward
  43.         }
  44.     end
  45. end
  46.  
  47. function moveForward()
  48.     while not turtle.forward() do
  49.         print("Obstructed. Couldn't move forward")
  50.         turtle.attack()
  51.         turtle.dig()
  52.         CheckFuel()
  53.     end
  54.  
  55.     if Direction == 0 then
  56.         Local_x = Local_x + 1
  57.     elseif Direction == 1 then
  58.         Local_y = Local_y + 1
  59.     elseif Direction == 2 then
  60.         Local_x = Local_x - 1
  61.     elseif Direction == 3 then
  62.         Local_y = Local_y - 1
  63.     end
  64.    
  65.     _persist_position()
  66. end
  67.  
  68. function turnRight()
  69.     turtle.turnRight()
  70.     Direction = (Direction + 1) % 4
  71.     _persist_position()
  72. end
  73. function turnLeft()
  74.     turtle.turnLeft()
  75.     Direction = (Direction + 3) % 4
  76.     _persist_position()
  77. end
  78.  
  79. function moveUp()
  80.     while not turtle.up() do
  81.         print("Obstructed. Couldn't move up")
  82.         turtle.attackUp()
  83.         turtle.digUp()
  84.         CheckFuel()
  85.     end
  86.     Local_z = Local_z + 1
  87.     _persist_position()
  88. end
  89. function moveDown()
  90.     while not turtle.down() do
  91.         print("Obstructed. Couldn't move down")
  92.         turtle.attackDown()
  93.         turtle.digDown()
  94.         CheckFuel()
  95.     end
  96.     Local_z = Local_z - 1
  97.     _persist_position()
  98. end
  99.  
  100. function moveTo(x, y, z, order)
  101.     for i = 1, string.len(order) do
  102.         char = string.sub(order, i, i)
  103.  
  104.         if char == "x" then
  105.             moveToX(x)
  106.         elseif char == "y" then
  107.             moveToY(y)
  108.         elseif char == "z" then
  109.             moveToZ(z)
  110.         end
  111.     end
  112. end
  113.  
  114. function moveToX(x)
  115.     while Local_x ~= x do
  116.         if Local_x > x then
  117.             turnToDirection(Directions.Backwards)
  118.             moveForward()
  119.         else
  120.             turnToDirection(Directions.Forward)
  121.             moveForward()
  122.         end
  123.     end
  124. end
  125.  
  126. function moveToY(y)
  127.     while Local_y ~= y do
  128.         if Local_y > y then
  129.             turnToDirection(Directions.Left)
  130.             moveForward()
  131.         else
  132.             turnToDirection(Directions.Right)
  133.             moveForward()
  134.         end
  135.     end
  136. end
  137.  
  138. function moveToZ(z)
  139.     while Local_z ~= z do
  140.         if Local_z > z then
  141.             moveDown()
  142.         else
  143.             moveUp()
  144.         end
  145.     end
  146. end
  147.  
  148.  
  149. function turnToDirection(direction)
  150.     if Direction == direction then
  151.         return
  152.     elseif (Direction + 1) % 4 == direction then
  153.         turnRight()
  154.     elseif (Direction + 3) % 4 == direction then
  155.         turnLeft()
  156.     else
  157.         turnRight()
  158.         turnRight()
  159.     end
  160. end
  161.  
  162. function CheckFuel()
  163.     if turtle.getFuelLevel() == 0 and Auto_refuel then
  164.         print("Out of fuel. Feed me!")
  165.         while turtle.getFuelLevel() == 0 do
  166.             for i = 5, 15 do
  167.                 if turtle.getItemCount(i) > 0 then
  168.                     turtle.select(i)
  169.                     turtle.refuel()
  170.                 end
  171.             end
  172.             sleep(5)
  173.         end
  174.     end
  175. end
  176.  
  177. function _persist_position()
  178.     position = {
  179.         Local_x = Local_x,
  180.         Local_y = Local_y,
  181.         Local_z = Local_z,
  182.         Direction = Direction,
  183.         LastPos = LastPos,
  184.         Auto_refuel = Auto_refuel
  185.     }
  186.  
  187.     save_data(file_position, position)
  188. end
  189.  
  190. function read_position()
  191.     position = read_data(file_position)
  192.  
  193.     if position ~= nil then
  194.         if position.Local_x ~= nil then
  195.             Local_x = position.Local_x
  196.         end
  197.         if position.Local_y ~= nil then
  198.             Local_y = position.Local_y
  199.         end
  200.         if position.Local_z ~= nil then
  201.             Local_z = position.Local_z
  202.         end
  203.         if position.Direction ~= nil then
  204.             Direction = position.Direction
  205.         end
  206.         if position.LastPos ~= nil then
  207.             LastPos = position.LastPos
  208.         end
  209.         if position.Auto_refuel ~= nil then
  210.             Auto_refuel = position.Auto_refuel
  211.         end
  212.     end
  213. end
  214.  
  215. function save_data(filename, obj)
  216.     file = io.open(filename, "w")
  217.     file:write(textutils.serialise(obj, { compact = true }))
  218.    
  219.     file:close()
  220. end
  221.  
  222. function read_data(filename)
  223.     file = io.open(filename, "r")
  224.     obj = {}
  225.  
  226.     if file ~= nil then
  227.         obj = textutils.unserialise(file:read())
  228.  
  229.         file:close()
  230.     end
  231.  
  232.     return obj
  233. end
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement