robathome

Untitled

Sep 30th, 2025
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. -- resumable signed-volume miner with start offset, slow start, return+dual-dump (fixed nil compares)
  2. local ST="mine_state.txt"
  3. local t=turtle
  4.  
  5. -- read number or "quit"/"resume"
  6. local function rd(p)
  7. io.write(p..": ")
  8. local s=read() or ""
  9. s=s:lower()
  10. if s=="quit" then error() end
  11. if s=="resume" then return "resume" end
  12. local n=tonumber(s)
  13. if not n then return rd(p) end
  14. return n
  15. end
  16.  
  17. local function save(S)
  18. local h=fs.open(ST,"w"); h.write(textutils.serialize(S)); h.close()
  19. end
  20. local function load()
  21. if not fs.exists(ST) then return nil end
  22. local h=fs.open(ST,"r"); local s=h.readAll(); h.close()
  23. return textutils.unserialize(s)
  24. end
  25.  
  26. -- dig helpers
  27. local function digf() while t.detect() do t.dig() end end
  28. local function dgu() while t.detectUp() do t.digUp() end end
  29. local function dgd() while t.detectDown() do t.digDown() end end
  30.  
  31. -- state
  32. local S=load()
  33.  
  34. -- start or resume safely
  35. if not S then
  36. local X=rd("X (signed fwd/back)")
  37. if X=="resume" then
  38. S=load()
  39. if not S then print("no saved state"); return end
  40. end
  41. if not S then
  42. local Y=rd("Y (signed up/down)")
  43. local Z=rd("Z (signed right/left)")
  44. local OX=rd("OffX (signed fwd/back; stand beside first mined)")
  45. local OY=rd("OffY (signed up/down)")
  46. local OZ=rd("OffZ (signed right/left)")
  47. S={
  48. xlen=math.abs(X), ylen=math.abs(Y), zlen=math.abs(Z),
  49. xneg=X<0, yneg=Y<0, zneg=Z<0,
  50. x=1, y=1, z=1, dir=0,
  51. ox=OX or 0, oy=OY or 0, oz=OZ or 0, at_off=false,
  52. slow=10,
  53. px=0, py=0, pz=0, h=0, sh=0
  54. }
  55. S.sh=S.h -- capture initial heading
  56. save(S)
  57. end
  58. else
  59. -- ensure required keys exist on old saves
  60. S.ox=S.ox or 0; S.oy=S.oy or 0; S.oz=S.oz or 0
  61. S.px=S.px or 0; S.py=S.py or 0; S.pz=S.pz or 0
  62. S.h=S.h or 0; S.sh=S.sh or 0
  63. S.slow=S.slow or 0
  64. S.at_off = S.at_off or false
  65. end
  66.  
  67. -- turns and facing
  68. local function TL() t.turnLeft(); S.h=(S.h+3)%4 end
  69. local function TR() t.turnRight(); S.h=(S.h+1)%4 end
  70. local function T2() TL(); TL() end
  71. local function face(d) while S.h~=d do if (d-S.h)%4==1 then TR() else TL() end end end
  72.  
  73. -- slow-start pause on first 10 moves
  74. local function tick() if S.slow>0 then sleep(5); S.slow=S.slow-1; save(S) end end
  75.  
  76. -- moves track pos
  77. local function F()
  78. local n=0
  79. while not t.forward() do t.attack(); digf(); n=n+1; if n>50 then return end end
  80. if S.h==0 then S.px=S.px+1 elseif S.h==1 then S.pz=S.pz+1 elseif S.h==2 then S.px=S.px-1 else S.pz=S.pz-1 end
  81. tick(); save(S)
  82. end
  83. local function U()
  84. local n=0
  85. while not t.up() do t.attackUp(); dgu(); n=n+1; if n>50 then return end end
  86. S.py=S.py+1; tick(); save(S)
  87. end
  88. local function D()
  89. local n=0
  90. while not t.down() do t.attackDown(); dgd(); n=n+1; if n>50 then return end end
  91. S.py=S.py-1; tick(); save(S)
  92. end
  93.  
  94. -- go to start offset once (adjacent space)
  95. local function go_off()
  96. if S.at_off then return end
  97. if S.oy>0 then for i=1,S.oy do dgu(); U() end
  98. elseif S.oy<0 then for i=1,-S.oy do dgd(); D() end end
  99. if S.oz>0 then TR(); for i=1,S.oz do digf(); F() end; TL()
  100. elseif S.oz<0 then TL(); for i=1,-S.oz do digf(); F() end; TR() end
  101. if S.ox>0 then for i=1,S.ox do digf(); F() end
  102. elseif S.ox<0 then T2(); for i=1,-S.ox do digf(); F() end; T2() end
  103. S.at_off=true; save(S)
  104. end
  105.  
  106. -- Manhattan goto in carved space
  107. local function goto_rel(x,y,z)
  108. while S.py<y do dgu(); U() end
  109. while S.py>y do dgd(); D() end
  110. if S.px<x then face(0); for i=1,x-S.px do digf(); F() end
  111. elseif S.px>x then face(2); for i=1,S.px-x do digf(); F() end end
  112. if S.pz<z then face(1); for i=1,z-S.pz do digf(); F() end
  113. elseif S.pz>z then face(3); for i=1,S.pz-z do digf(); F() end end
  114. end
  115.  
  116. -- dump at home using two chests: y=-1 for cobble/dirt/gravel/sand; y=-2 for everything else
  117. local function dump_home()
  118. local A={["minecraft:cobblestone"]=1,["minecraft:dirt"]=1,["minecraft:gravel"]=1,["minecraft:sand"]=1}
  119. for s=1,16 do local d=t.getItemDetail(s); if d and A[d.name] then t.select(s); t.dropDown() end end
  120. face(0); digf(); F() -- (1,0,0)
  121. dgd(); D(); dgd(); D() -- (1,-2,0)
  122. face(2) -- toward chests
  123. for s=1,16 do local d=t.getItemDetail(s); if d and not A[d.name] then t.select(s); t.drop() end end
  124. face(0); dgu(); U(); dgu(); U() -- back to y=0
  125. face(2); digf(); F() -- back to (0,0,0)
  126. face(0)
  127. goto_rel(0,0,0)
  128. end
  129.  
  130. -- === run ===
  131. go_off()
  132. if S.xneg then T2() end
  133.  
  134. -- guard missing dims on resume
  135. if not (S.xlen and S.ylen and S.zlen) then print("bad state"); return end
  136. if not (S.x and S.y and S.z) then S.x=1; S.y=1; S.z=1 end
  137. if S.dir==nil then S.dir=0 end
  138.  
  139. while S.y<=S.ylen do
  140. if S.dir==1 then T2() end
  141. for i=S.x,S.xlen do
  142. dgu(); dgd()
  143. local last=(i==S.xlen) and (S.z==S.zlen) and (S.y==S.ylen)
  144. if not last and i<S.xlen then digf(); F() end
  145. S.x=i+1; save(S)
  146. end
  147. S.x=1
  148. if S.z<S.zlen then
  149. if not S.zneg then TR(); digf(); F(); TR() else TL(); digf(); F(); TL() end
  150. S.z=S.z+1; S.dir=1-S.dir; save(S)
  151. else
  152. if S.y<S.ylen then
  153. if not S.yneg then dgu(); U() else dgd(); D() end
  154. if (S.z%2)==1 then T2(); for i=1,S.xlen-1 do digf(); F() end; T2() end
  155. S.y=S.y+1; S.z=1; S.dir=0; save(S)
  156. else
  157. break
  158. end
  159. end
  160. end
  161.  
  162. goto_rel(0,0,0)
  163. dump_home()
  164. goto_rel(0,0,0)
  165. face(S.sh or 0)
  166. if fs.exists(ST) then fs.delete(ST) end
  167. print("done")
  168.  
Advertisement
Add Comment
Please, Sign In to add comment