Advertisement
Base8

turleminedon

Jan 19th, 2025 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.66 KB | Software | 0 0
  1. local t = turtle
  2. local fuel = 0
  3. local fuelBuffer = 30
  4. local returning = false
  5.  
  6. local NORTH = 1
  7. local EAST = 2
  8. local SOUTH = 3
  9. local WEST = 4
  10.  
  11. Point = {}
  12. Point.__index = Point
  13. function Point:new(x,y,z,dir)
  14.     local obj = {x=x,y=y,z=z,dir=dir}
  15.     setmetatable(obj,Point)
  16.     return obj
  17. end
  18.  
  19. function Point:forward()
  20.     if self.dir == NORTH then
  21.         self.y=self.y+1
  22.     elseif self.dir == South then
  23.         self.y=self.y-1
  24.     elseif self.dir == EAST then
  25.         self.x=self.x+1
  26.     elseif self.dir == WEST then
  27.         self.x=self.x-1
  28.     end
  29. end
  30.  
  31. function Point:back()
  32.     if self.dir == NORTH then
  33.         self.y=self.y-1
  34.     elseif self.dir == South then
  35.         self.y=self.y+1
  36.     elseif self.dir == EAST then
  37.         self.x=self.x-1
  38.     elseif self.dir == WEST then
  39.         self.x=self.x+1
  40.     end
  41. end
  42.  
  43. function Point:up()
  44.         self.y=self.y+1
  45. end
  46.  
  47. function Point:down()
  48.     self.y=self.y-1
  49. end
  50.  
  51. function Point:right()
  52.     self.dir=self.dir+1
  53.     if self.dir>WEST then
  54.         self.dir = NORTH
  55.     end
  56. end
  57.  
  58. function Point:left()
  59.     self.dir=self.dir-1
  60.     if self.dir<NORTH then
  61.         self.dir = WEST
  62.     end
  63. end
  64.  
  65. function Point:print()
  66.     local dirText=""
  67.     if self.dir == NORTH then
  68.         dirText="NORTH"
  69.     elseif self.dir == South then
  70.         dirText="SOUTH"
  71.     elseif self.dir == EAST then
  72.         dirText="EAST"
  73.     elseif self.dir == WEST then
  74.         dirText="WEST"
  75.     end
  76.     print("Point: ("..self.x..","..self.y..","..self.z..","..dirText..")")
  77. end
  78.  
  79. local relativePos=Point:new(0,0,0,NORTH)
  80.  
  81. function onTurnon()
  82.     fuel = getFuelLevel()
  83. end
  84.  
  85. function dumpInventory()
  86.     for i = 0, 15 do
  87.         t.select(i)
  88.         t.dropDown()
  89.     end
  90. end
  91.  
  92. function restockFuel()
  93.     fFace(SOUTH)
  94.     t.select(0)
  95.     t.suck()
  96.     t.refuel()
  97. end
  98.  
  99. function refuelReturn()
  100.     --Fix Z
  101.     while relativePos.z>0 do
  102.         fDown()
  103.     end
  104.     while relativePos.z<0 do
  105.         fUp()
  106.     end
  107.  
  108.     --Fix x
  109.     if relativePos.x<0 then
  110.         fFace(EAST)
  111.     elseif relativePos.x>0 then
  112.         fFace(WEST)
  113.     end
  114.     while relativePos.x~=0 do
  115.         fForward()
  116.     end
  117.  
  118.     --Fix y
  119.     if relativePos.y<0 then
  120.         fFace(NORTH)
  121.     elseif relativePos.y>0 then
  122.         fFace(SOUTH)
  123.     end
  124.  
  125.     while relativePos.y~=0 do
  126.         fForward()
  127.     end
  128. end
  129.  
  130. function goBackTO(pointA)
  131.     --Fix Z
  132.     while pointA.z>0 do
  133.         fDown()
  134.     end
  135.     while pointA.z<0 do
  136.         fUp()
  137.     end
  138.  
  139.     --Fix y
  140.     if pointA.y<0 then
  141.         fFace(NORTH)
  142.     elseif pointA.y>0 then
  143.         fFace(SOUTH)
  144.     end
  145.  
  146.     while pointA.y~=0 do
  147.         fForward()
  148.     end
  149.  
  150.     --Fix x
  151.     if pointA.x<0 then
  152.         fFace(EAST)
  153.     elseif pointA.x>0 then
  154.         fFace(WEST)
  155.     end
  156.  
  157.     while pointA.x~=0 do
  158.         fForward()
  159.     end
  160.  
  161.     --Fix dir
  162.     fFace(pointA.dir)
  163. end
  164.  
  165. --Get the current fuel level and return home if too close to empty
  166. --If we refuel we come back to the same posisiton/orientation
  167. function updateFuel()
  168.     if returning then
  169.         return
  170.     end
  171.     fuel = t.getFuelLevel()
  172.     --Fuel to get back home is calculated
  173.     local blocksBack = relativePos.x+relativePos.y+relativePos.z
  174.     if fuel <= blocksBack + fuelBuffer then
  175.         returning=true
  176.         local returnPoint = Point:new(relativePos.x,relativePos.y,relativePos.z,relativePos.dir)
  177.         refuelReturn()
  178.         restockFuel()
  179.         goBackTo(returnPoint)
  180.         returning=false
  181.     end
  182. end
  183.  
  184. function fDig()
  185.     while t.detect() do
  186.         updateFuel()
  187.         t.dig()
  188.     end
  189. end
  190.  
  191. function fDigDown()
  192.     while t.detectDown() do
  193.         updateFuel()
  194.         t.digDown()
  195.     end
  196. end
  197.  
  198. function fDigUp()
  199.     while t.detectUp() do
  200.         updateFuel()
  201.         t.digUp()
  202.     end
  203. end
  204.  
  205. function fForward(x)
  206.     x = x or 1
  207.     for i=1,x do
  208.         while not t.forward() do
  209.             updateFuel()
  210.             fDig()
  211.         end
  212.         relativePos:forward()
  213.         updateFuel()
  214.     end
  215. end
  216.  
  217. function fDown()
  218.     while not t.down() do
  219.         updateFuel()
  220.         fDigDown()
  221.     end
  222.     relativePos:down()
  223.     updateFuel()
  224. end
  225.  
  226. function fUp()
  227.     while not t.up() do
  228.         updateFuel()
  229.         fDigUp()
  230.     end
  231.     relativePos:up()
  232.     updateFuel()
  233. end
  234.  
  235. function fRight()
  236.     t.turnRight()
  237.     relativePos:right()
  238.     updateFuel()
  239. end
  240.  
  241. function fLeft()
  242.     t.turnLeft()
  243.     relativePos:left()
  244.     updateFuel()
  245. end
  246.  
  247. function fTurnAround()
  248.     fRight()
  249.     fRight()
  250. end
  251.  
  252. function fBack()
  253.     if not t.back() then
  254.         fTurnAround()
  255.         fDig()
  256.         fTurnAround()
  257.     end
  258.     relativePos:back()
  259.     updateFuel()
  260. end
  261.  
  262. function fFace(d)
  263.     if d == relativePos.dir then
  264.         return
  265.     elseif d-relativePos.dir==1 then
  266.         fLeft()
  267.     elseif relativePos.dir-d==1 then
  268.         fRight()
  269.     else
  270.         fTurnAround()
  271.     end
  272. end
  273.  
  274. --Assuming middle start
  275. function mine3By3(x)
  276.     for i=0,x do
  277.         --dig middle part
  278.         fDig()
  279.         fForward()
  280.         fDigUp()
  281.         fDigDown()
  282.         --dig left part
  283.         fLeft()
  284.         fDig()
  285.         fForward()
  286.         fDigUp()
  287.         fDigDown()
  288.         --dig right part
  289.         fTurnAround()
  290.         fForward()
  291.         fDig()
  292.         fForward()
  293.         fDigUp()
  294.         fDigDown()
  295.         --return
  296.         fBack()
  297.         fLeft()
  298.     end
  299. end
  300.  
  301. function mine()
  302.     fUp()
  303.     fFace(NORTH)
  304.     while true do
  305.         mine3By3(5)
  306.         fLeft()
  307.         fForward()
  308.         mine3By3(5)
  309.         fTurnAround()
  310.         fForward(5+3)
  311.         mine3By3(5)
  312.         fTurnAround()
  313.         fForward(5+2)
  314.         fRight()
  315.     end
  316. end
  317.  
  318. mine()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement