robathome

Untitled

Sep 30th, 2025
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. -- resumable signed-volume miner with start offset, slow start, return+dual-dump
  2. local ST="mine_state.txt"
  3. local t=turtle
  4.  
  5. local function rd(p)
  6. io.write(p..": "); local s=read() or ""
  7. s=s:lower()
  8. if s=="quit" then error() end
  9. if s=="resume" then return "resume" end
  10. local n=tonumber(s); if not n then return rd(p) end
  11. return n
  12. end
  13.  
  14. local function save(S) local h=fs.open(ST,"w"); h.write(textutils.serialize(S)); h.close() end
  15. 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
  16.  
  17. local function digf() while t.detect() do t.dig() end end
  18. local function dgu() while t.detectUp() do t.digUp() end end
  19. local function dgd() while t.detectDown() do t.digDown() end end
  20.  
  21. local S=load()
  22. if not S then
  23. local X=rd("X (signed fwd/back)"); if X=="resume" then S=load() end
  24. if not S then
  25. local Y=rd("Y (signed up/down)")
  26. local Z=rd("Z (signed right/left)")
  27. local OX=rd("OffX (signed fwd/back; stand beside first mined)")
  28. local OY=rd("OffY (signed up/down)")
  29. local OZ=rd("OffZ (signed right/left)")
  30. S={
  31. xlen=math.abs(X), ylen=math.abs(Y), zlen=math.abs(Z),
  32. xneg=X<0, yneg=Y<0, zneg=Z<0,
  33. x=1, y=1, z=1, dir=0,
  34. ox=OX, oy=OY, oz=OZ, at_off=false,
  35. slow=10,
  36. px=0,py=0,pz=0, h=0, sh=0
  37. }
  38. save(S)
  39. end
  40. end
  41.  
  42. local function TL() t.turnLeft(); S.h=(S.h+3)%4 end
  43. local function TR() t.turnRight(); S.h=(S.h+1)%4 end
  44. local function T2() TL(); TL() end
  45. local function face(d) while S.h~=d do if (d-S.h)%4==1 then TR() else TL() end end end
  46.  
  47. local function tick() if S.slow and S.slow>0 then sleep(5); S.slow=S.slow-1; save(S) end end
  48.  
  49. local function F()
  50. local n=0
  51. while not t.forward() do t.attack(); digf(); n=n+1; if n>50 then return end end
  52. 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
  53. tick(); save(S)
  54. end
  55. local function U()
  56. local n=0
  57. while not t.up() do t.attackUp(); dgu(); n=n+1; if n>50 then return end end
  58. S.py=S.py+1; tick(); save(S)
  59. end
  60. local function D()
  61. local n=0
  62. while not t.down() do t.attackDown(); dgd(); n=n+1; if n>50 then return end end
  63. S.py=S.py-1; tick(); save(S)
  64. end
  65.  
  66. local function go_off()
  67. if S.at_off then return end
  68. 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
  69. if S.oz>0 then TR(); for i=1,S.oz do digf(); F() end; TL()
  70. elseif S.oz<0 then TL(); for i=1,-S.oz do digf(); F() end; TR() end
  71. if S.ox>0 then for i=1,S.ox do digf(); F() end
  72. elseif S.ox<0 then T2(); for i=1,-S.ox do digf(); F() end; T2() end
  73. S.at_off=true; save(S)
  74. end
  75.  
  76. local function goto_rel(x,y,z)
  77. while S.py<y do dgu(); U() end
  78. while S.py>y do dgd(); D() end
  79. if S.px<x then face(0); for i=1,x-S.px do digf(); F() end
  80. elseif S.px>x then face(2); for i=1,S.px-x do digf(); F() end end
  81. if S.pz<z then face(1); for i=1,z-S.pz do digf(); F() end
  82. elseif S.pz>z then face(3); for i=1,S.pz-z do digf(); F() end end
  83. end
  84.  
  85. local function dump_home()
  86. -- must be at (0,0,0)
  87. local a={["minecraft:cobblestone"]=1,["minecraft:dirt"]=1,["minecraft:gravel"]=1,["minecraft:sand"]=1}
  88. for s=1,16 do local d=t.getItemDetail(s); if d and a[d.name] then t.select(s); t.dropDown() end end
  89. face(0); digf(); F()
  90. dgd(); D(); dgd(); D()
  91. face(2)
  92. 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
  93. face(0); dgu(); U(); dgu(); U()
  94. face(2); digf(); F()
  95. face(0)
  96. -- now near home; ensure exact home:
  97. goto_rel(0,0,0)
  98. end
  99.  
  100. go_off()
  101. if S.xneg then T2() end
  102.  
  103. while S.y<=S.ylen do
  104. if S.dir==1 then T2() end
  105. for i=S.x,S.xlen do
  106. dgu(); dgd()
  107. local last=(i==S.xlen) and (S.z==S.zlen) and (S.y==S.ylen)
  108. if not last and i<S.xlen then digf(); F() end
  109. S.x=i+1; save(S)
  110. end
  111. S.x=1
  112. if S.z<S.zlen then
  113. if not S.zneg then TR(); digf(); F(); TR() else TL(); digf(); F(); TL() end
  114. S.z=S.z+1; S.dir=1-S.dir; save(S)
  115. else
  116. if S.y<S.ylen then
  117. if not S.yneg then dgu(); U() else dgd(); D() end
  118. if (S.z%2)==1 then T2(); for i=1,S.xlen-1 do digf(); F() end; T2() end
  119. S.y=S.y+1; S.z=1; S.dir=0; save(S)
  120. else break end
  121. end
  122. end
  123.  
  124. goto_rel(0,0,0)
  125. dump_home()
  126. goto_rel(0,0,0)
  127. face(S.sh)
  128. if fs.exists(ST) then fs.delete(ST) end
  129. print("done")
  130.  
Advertisement
Add Comment
Please, Sign In to add comment