SHARE
TWEET

Diggy turtle

omr__ Sep 11th, 2019 (edited) 152 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. K_VERSION = 0.145
  2.  
  3. -- Features: Auto-update, Auto-resume
  4.  
  5. -- Exposed Functions
  6. --      lama.overwrite( [table t] ) - _overwrites G.turtle, or the table provided
  7. --      lama.getPosition() - returns the current position in (x, y, z, facing)
  8. --      lama.setPosition( number x, number y, number z[, string facing] ) - sets the current position
  9. --      lama.turnRight() - turtle.turnRight, with tracking
  10. --      lama.turnLeft() - turtle.turnLeft, with tracking
  11. --      lama.forward() - turtle.forward, but with tracking
  12. --      lama.back() - turtle.back, but with tracking
  13. --      lama.down() - turtle.down, but with tracking
  14. --      lama.up() - turtle.up, but with tracking
  15. --      lama.refuel( [number amount] ) - turtle.refuel, but with tracking
  16. -- Internal Tables
  17. --      fuel - stores fuel level and saving/loading functions for fuel level
  18. --      facing - stores the current facing (north/south/east/west) and the direction (facing/up/down), and saving/loading functions for those values
  19. --      position - stores x, y, z position, and the save/load functions for those values. Also includes update function
  20. --      env - the environment (anything set in it is available at lama.name)
  21. -- Internal Functions
  22. --      facing.load - if previous data is available, it'll load it, otherwise it'll try GPS then default to north
  23. --      facing.save - saves facing
  24. --      facing.turnRight - changes the internal facing value clockwise on a compass (ei north -> east)
  25. --      position.load - if previous data is available, it'll load it, otherwise it'll try GPS then default to {1,1,1}
  26. --      position.save - saves position
  27. --      position.update - updates the position based on current direction and difference in fuel level
  28. --      fuel.load - loads fuel data or defaults to current fuel level
  29. --      fuel.save - saves fuel data
  30.  
  31. local args = {...}
  32. local resume = args[4]
  33. local size = {x = tonumber(args[1]),  y = tonumber(args[2]), z = tonumber(args[3])}
  34. local x, y, z, facing
  35.  
  36. function initStartPos()
  37.     if (resume) then
  38.         x, y, z, facing = lama.getPosition()
  39.     else
  40.         x = 0
  41.         y = 0
  42.         z = 0
  43.         facing = "south"
  44.         lama.setPosition(x, y, z, facing)
  45.     end
  46. end
  47.  
  48. function getPos()
  49.     local _x, _y, _z, _facing = lama.getPosition()
  50.     return {x = tonumber(_x), y = tonumber(_y), z = tonumber(_z), f = _facing}
  51. end
  52.  
  53. function setForward()
  54.     local _facing = getPos().f
  55.     if (_facing == "east") then lama.turnRight()
  56.     elseif (_facing == "north") then lama.turnLeft() lama.turnLeft()
  57.     elseif (_facing == "west") then lama.turnLeft() end
  58. end
  59.  
  60. function setBack()
  61.     local _facing = getPos().f
  62.     if (_facing == "south") then lama.turnLeft() lama.turnLeft()
  63.     elseif (_facing == "east") then lama.turnLeft()
  64.     elseif (_facing == "west") then lama.turnRight() end
  65. end
  66.  
  67. function setLeft()
  68.     local _facing = getPos().f;
  69.     if (_facing == "south") then lama.turnLeft()
  70.     elseif (_facing == "north") then lama.turnRight()
  71.     elseif (_facing== "west") then lama.turnLeft() lama.turnLeft() end
  72. end
  73.  
  74. function setRight()
  75.     local _facing = getPos().f;
  76.     if (_facing == "south") then
  77.         lama.turnRight()
  78.     elseif (_facing == "north") then
  79.         lama.turnLeft()
  80.     elseif (_facing == "east") then
  81.         lama.turnRight()
  82.         lama.turnRight()
  83.     end
  84. end
  85.  
  86. function fForward() turtle.dig() lama.forward() end
  87. function fBack() setBack() fForward() setForward() end
  88. function fUp() turtle.digUp() lama.up() end
  89. function fDown() turtle.digDown() lama.down() end
  90. function fRight() setRight() fForward() setForward() end
  91. function fLeft() setLeft() fForward() setForward() end
  92.  
  93. function mineY()
  94.     while (getPos().y < size.y - 1) do
  95.         fUp()
  96.     end
  97.    
  98.     while (getPos().y > 0) do
  99.         fDown()
  100.     end
  101. end
  102.  
  103. function mineXY()
  104.     while (getPos().x < size.x - 1) do
  105.         mineY()
  106.         fLeft()
  107.     end
  108.     mineY()
  109.  
  110.     while (getPos().x > 0) do
  111.         fRight()
  112.     end
  113. end
  114.  
  115. function mineXYZ()
  116.     if (getPos().z == size.z) then
  117.         mineXY()
  118.     end
  119.  
  120.     while (getPos().z ~= size.z) do
  121.         if (not resume) then
  122.             fForward()
  123.         else
  124.             resume = false
  125.         end
  126.         mineXY()
  127.     end
  128.  
  129.     while (getPos().z > 0) do
  130.         fBack()
  131.     end
  132. end
  133.  
  134. function main()
  135.     mineXYZ()
  136. end
  137.  
  138. initStartPos()
  139. main()
  140. return true
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top