robathome

Untitled

Sep 30th, 2025
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. -- minimal resumable volume miner w/ signed dims and quit/resume
  2. local ST="mine_state.txt"
  3.  
  4. local function rd(p)
  5. io.write(p..": ")
  6. local s=read()
  7. if not s then return nil end
  8. s=s:lower()
  9. if s=="quit" then print("bye") error() end
  10. if s=="resume" then return "resume" end
  11. local n=tonumber(s); if not n then print("num/quit/resume"); return rd(p) end
  12. return n
  13. end
  14.  
  15. local function save(S)
  16. local h=fs.open(ST,"w"); h.write(textutils.serialize(S)); h.close()
  17. end
  18. local function load()
  19. if not fs.exists(ST) then return nil end
  20. local h=fs.open(ST,"r"); local s=h.readAll(); h.close()
  21. return textutils.unserialize(s)
  22. end
  23.  
  24. -- moves
  25. local t=turtle
  26. local function digf() while t.detect() do t.dig() end end
  27. local function df() while not t.forward() do t.attack(); digf() end end
  28. local function dug() while t.detectUp() do t.digUp() end end
  29. local function ddn() while t.detectDown() do t.digDown() end end
  30. local function up() while not t.up() do t.attackUp(); dug() end end
  31. local function dn() while not t.down() do t.attackDown(); ddn() end end
  32.  
  33. -- state (progress)
  34. local S = load()
  35. if S=="resume" then S=nil end
  36. if S then
  37. print("resuming")
  38. else
  39. local X=rd("X (signed, fwd/back)"); if X=="resume" then S=load() end
  40. if not S then
  41. local Y=rd("Y (signed, up/down)")
  42. local Z=rd("Z (signed, right/left serpentine)")
  43. local xlen=math.abs(X); local ylen=math.abs(Y); local zlen=math.abs(Z)
  44. local xneg = X<0; local yneg = Y<0; local zneg = Z<0
  45. -- starting facing: forward mines along +X; flip if X<0
  46. if xneg then t.turnLeft(); t.turnLeft() end
  47. S={xlen=xlen,ylen=ylen,zlen=zlen,x=1,y=1,z=1,dir=0,xneg=xneg,yneg=yneg,zneg=zneg,started=true}
  48. save(S)
  49. end
  50. end
  51.  
  52. -- helpers to go to next line and layer based on signs
  53. local function next_row(S)
  54. if S.z>=S.zlen then return false end
  55. if not S.zneg then
  56. t.turnRight(); digf(); df(); t.turnRight()
  57. else
  58. t.turnLeft(); digf(); df(); t.turnLeft()
  59. end
  60. S.z=S.z+1; S.dir=1-S.dir; save(S)
  61. return true
  62. end
  63.  
  64. local function next_layer(S)
  65. if S.y>=S.ylen then return false end
  66. if not S.yneg then dug(); up() else ddn(); dn() end
  67. -- reset row to 1 and go to row start
  68. if S.z%2==1 then
  69. -- we ended at row end; go back to row start for new layer
  70. t.turnLeft(); t.turnLeft()
  71. for i=1,S.xlen-1 do digf(); df() end
  72. t.turnLeft(); t.turnLeft()
  73. end
  74. -- go to first row start cell in new layer
  75. S.y=S.y+1; S.z=1; S.dir=0; save(S)
  76. return true
  77. end
  78.  
  79. -- main loop: layers -> rows -> cells
  80. while S.y<=S.ylen do
  81. -- at start of a row, face correct dir
  82. if S.dir==0 then
  83. -- face forward
  84. else
  85. t.turnLeft(); t.turnLeft()
  86. end
  87. -- advance along X for remaining cells in this row
  88. local startx = S.x
  89. for i=startx,S.xlen do
  90. -- mine current cell
  91. dug(); ddn()
  92. -- move forward unless last cell of last row/layer
  93. local last = (i==S.xlen) and (S.z==S.zlen) and (S.y==S.ylen)
  94. if not last then
  95. if i<S.xlen then digf(); df() end
  96. end
  97. S.x = i+1; save(S)
  98. end
  99. -- row done
  100. S.x=1
  101. if not next_row(S) then
  102. -- rows done in this layer
  103. if not next_layer(S) then break end
  104. end
  105. end
  106.  
  107. -- finished: delete state and dump dirt/cobble
  108. if fs.exists(ST) then fs.delete(ST) end
  109. for s=1,16 do
  110. local d=t.getItemDetail(s)
  111. if d and (d.name=="minecraft:dirt" or d.name=="minecraft:cobblestone") then
  112. t.select(s); t.dropDown()
  113. end
  114. end
  115. print("done")
  116.  
Advertisement
Add Comment
Please, Sign In to add comment