robathome

Untitled

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