robathome

Untitled

Sep 30th, 2025
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.87 KB | None | 0 0
  1. -- mine_volume.lua (CC:Tweaked Turtle)
  2. -- Mines a prompted rectangular volume from a prompted relative start offset.
  3. -- Optionally fills voids below the lowest Y of that volume with cobble (slot 2).
  4. -- Assumptions:
  5. -- - A fuel container is directly below start (y = -1).
  6. -- - A dump container is directly below that (y = -2).
  7. -- - Slot 2 contains cobblestone at start. Never fully consume or dump it.
  8.  
  9. -- ======= User-tunable constants =======
  10. local MIN_FUEL_BUFFER = 500
  11. local COBBLE_SLOT = 2
  12. local COBBLE_RESERVE = 16
  13. local RETURN_INTERVAL_STEPS = 400
  14. -- ======================================
  15.  
  16. local t = turtle
  17.  
  18. -- state
  19. local pos = {x=0,y=0,z=0} -- start is (0,0,0)
  20. -- heading: 0=+X, 1=+Z, 2=-X, 3=-Z
  21. local heading = 0
  22. local start_heading = heading -- capture initial facing to restore at the end
  23.  
  24. -- utility: safe read
  25. local function read_num(prompt)
  26. while true do
  27. io.write(prompt); local s = read()
  28. local n = tonumber(s)
  29. if n and math.floor(n) == n then return n end
  30. print("Enter an integer.")
  31. end
  32. end
  33.  
  34. local function read_yesno(prompt)
  35. while true do
  36. io.write(prompt.." [y/n]: "); local s = (read() or ""):lower()
  37. if s=="y" or s=="yes" then return true end
  38. if s=="n" or s=="no" then return false end
  39. end
  40. end
  41.  
  42. -- movement wrappers
  43.  
  44. local function select_nonempty()
  45. for i=1,16 do if t.getItemCount(i)>0 then t.select(i); return true end end
  46. return false
  47. end
  48.  
  49. local function ensure_dig_forward()
  50. local tries = 0
  51. while t.detect() do
  52. t.dig(); sleep(0.1); tries = tries + 1
  53. if tries>50 then break end
  54. end
  55. end
  56.  
  57. local function ensure_dig_up()
  58. local tries = 0
  59. while t.detectUp() do
  60. t.digUp(); sleep(0.1); tries = tries + 1
  61. if tries>50 then break end
  62. end
  63. end
  64.  
  65. local function ensure_dig_down()
  66. local tries = 0
  67. while t.detectDown() do
  68. t.digDown(); sleep(0.1); tries = tries + 1
  69. if tries>50 then break end
  70. end
  71. end
  72.  
  73. local function forward()
  74. local tries=0
  75. while not t.forward() do
  76. ensure_dig_forward()
  77. if not t.forward() then
  78. t.attack(); sleep(0.1)
  79. tries=tries+1; if tries>50 then return false end
  80. end
  81. end
  82. if heading==0 then pos.x=pos.x+1
  83. elseif heading==1 then pos.z=pos.z+1
  84. elseif heading==2 then pos.x=pos.x-1
  85. else pos.z=pos.z-1 end
  86. return true
  87. end
  88.  
  89. local function up()
  90. local tries=0
  91. while not t.up() do
  92. ensure_dig_up()
  93. if not t.up() then
  94. t.attackUp(); sleep(0.1)
  95. tries=tries+1; if tries>50 then return false end
  96. end
  97. end
  98. pos.y=pos.y+1; return true
  99. end
  100.  
  101. local function down()
  102. local tries=0
  103. while not t.down() do
  104. ensure_dig_down()
  105. if not t.down() then
  106. t.attackDown(); sleep(0.1)
  107. tries=tries+1; if tries>50 then return false end
  108. end
  109. end
  110. pos.y=pos.y-1; return true
  111. end
  112.  
  113. local function turnLeft() heading=(heading+3)%4; t.turnLeft() end
  114. local function turnRight() heading=(heading+1)%4; t.turnRight() end
  115.  
  116. local function face(dir)
  117. while heading~=dir do
  118. local diff = (dir - heading) % 4
  119. if diff==1 then turnRight()
  120. elseif diff==3 then turnLeft()
  121. else turnRight() end
  122. end
  123. end
  124.  
  125. local function mine_step_forward()
  126. ensure_dig_forward()
  127. forward()
  128. ensure_dig_up()
  129. ensure_dig_down()
  130. end
  131.  
  132. -- inventory helpers
  133.  
  134. local function free_slots()
  135. local free=0
  136. for i=1,16 do
  137. if i~=COBBLE_SLOT and t.getItemCount(i)==0 then free=free+1 end
  138. end
  139. return free
  140. end
  141.  
  142. local function need_dump()
  143. return free_slots()==0
  144. end
  145.  
  146. -- refuel from the fuel container directly below start
  147. local function refuel_from_home()
  148. for _=1,8 do
  149. for i=1,16 do
  150. if i~=COBBLE_SLOT then
  151. t.select(i)
  152. t.suckDown(64)
  153. if t.refuel(64) then
  154. if turtle.getFuelLevel()>=MIN_FUEL_BUFFER then return end
  155. else
  156. t.dropDown(64)
  157. end
  158. end
  159. end
  160. sleep(0.2)
  161. end
  162. end
  163.  
  164. -- go_home with optional final facing
  165. local function go_home(final_face_dir)
  166. while pos.y>0 do up() end
  167. while pos.y<0 do down() end
  168. if pos.x>0 then face(2); for _=1,pos.x do forward() end
  169. elseif pos.x<0 then face(0); for _=1,-pos.x do forward() end end
  170. if pos.z>0 then face(3); for _=1,pos.z do forward() end
  171. elseif pos.z<0 then face(1); for _=1,-pos.z do forward() end end
  172. if final_face_dir~=nil then face(final_face_dir) end
  173. end
  174.  
  175. -- service pocket near dump chest; preserves caller state
  176. local function ensure_dump_service()
  177. local saved = {x=pos.x,y=pos.y,z=pos.z,h=heading}
  178. -- assume starting from home
  179. face(0)
  180. ensure_dig_forward(); forward() -- (1,0,0)
  181. ensure_dig_down(); down() -- (1,-1,0)
  182. ensure_dig_down(); down() -- (1,-2,0)
  183. face(2); if t.detect() then t.dig() end
  184. forward() -- (0,-2,0)
  185. face(0); forward() -- (1,-2,0)
  186. up(); up() -- (1,0,0)
  187. face(2); forward() -- (0,0,0)
  188. face(saved.h)
  189. -- restore explicit pos since we tracked moves
  190. pos = {x=saved.x,y=saved.y,z=saved.z}; heading = saved.h
  191. end
  192.  
  193. -- dump inventory except reserved cobble; preserves caller state and returns to home
  194. local function dump_at_home()
  195. local saved = {x=pos.x,y=pos.y,z=pos.z,h=heading}
  196. -- to cavity
  197. face(0); forward() -- (1,0,0); pos updated
  198. down(); down() -- (1,-2,0)
  199. face(2); forward() -- (0,-2,0)
  200. face(2)
  201. for i=1,16 do
  202. if i~=COBBLE_SLOT then
  203. local cnt = t.getItemCount(i)
  204. if cnt>0 then t.select(i); t.drop(64) end
  205. end
  206. end
  207. -- back to home
  208. face(0); forward() -- (1,-2,0)
  209. up(); up() -- (1,0,0)
  210. face(2); forward() -- (0,0,0)
  211. face(saved.h)
  212. pos = {x=saved.x,y=saved.y,z=saved.z}; heading = saved.h
  213. end
  214.  
  215. local function goto_rel(tx,ty,tz)
  216. while pos.y < ty do up() end
  217. while pos.y > ty do down() end
  218. if pos.x < tx then face(0); for _=1, tx-pos.x do mine_step_forward() end
  219. elseif pos.x > tx then face(2); for _=1, pos.x-tx do mine_step_forward() end end
  220. if pos.z < tz then face(1); for _=1, tz-pos.z do mine_step_forward() end
  221. elseif pos.z > tz then face(3); for _=1, pos.z-tz do mine_step_forward() end end
  222. end
  223.  
  224. local function ensure_fuel()
  225. if t.getFuelLevel()=="unlimited" then return end
  226. if t.getFuelLevel() < MIN_FUEL_BUFFER then
  227. local saved = {x=pos.x,y=pos.y,z=pos.z,h=heading}
  228. go_home()
  229. refuel_from_home()
  230. ensure_dump_service()
  231. goto_rel(saved.x,saved.y,saved.z); face(saved.h)
  232. end
  233. end
  234.  
  235. local function maybe_fill_void_below(filling_enabled)
  236. if not filling_enabled then return end
  237. if t.detectDown() then return end
  238. local cnt = t.getItemCount(COBBLE_SLOT)
  239. if cnt and cnt > COBBLE_RESERVE then
  240. t.select(COBBLE_SLOT)
  241. t.placeDown()
  242. end
  243. end
  244.  
  245. local function mine_cell(floorY, filling_enabled)
  246. ensure_dig_up()
  247. ensure_dig_down()
  248. if pos.y==floorY then maybe_fill_void_below(filling_enabled) end
  249. end
  250.  
  251. local step_counter = 0
  252. local function periodic_service()
  253. step_counter = step_counter + 1
  254. if step_counter >= RETURN_INTERVAL_STEPS or need_dump() or (t.getFuelLevel()~="unlimited" and t.getFuelLevel()<MIN_FUEL_BUFFER) then
  255. local saved = {x=pos.x,y=pos.y,z=pos.z,h=heading}
  256. go_home()
  257. refuel_from_home()
  258. ensure_dump_service()
  259. dump_at_home()
  260. goto_rel(saved.x,saved.y,saved.z); face(saved.h)
  261. step_counter = 0
  262. end
  263. end
  264.  
  265. -- ===== Main =====
  266.  
  267. print("Enter volume dimensions (X width, Z length, Y height) in blocks.")
  268. local X = math.abs(read_num("X (>=1): "))
  269. local Z = math.abs(read_num("Z (>=1): "))
  270. local Y = math.abs(read_num("Y (>=1): "))
  271.  
  272. print("Enter relative start offset vector from current position.")
  273. local sx = read_num("Start offset dx (right +, left -): ")
  274. local sy = read_num("Start offset dy (up +, down -): ")
  275. local sz = read_num("Start offset dz (forward +, back -): ")
  276.  
  277. local fill_voids = read_yesno("Fill empty space below the volume’s lowest Y with cobble from slot 2?")
  278.  
  279. print("Preparing…")
  280. ensure_fuel()
  281. go_home() -- ensure pos=(0,0,0)
  282. ensure_dump_service()
  283. refuel_from_home()
  284.  
  285. -- move to start offset
  286. goto_rel(sx, sy, sz)
  287.  
  288. -- define floorY in current relative coordinates
  289. local floorY = pos.y
  290.  
  291. -- mine by layers from floor up
  292. for layer=0,(Y-1) do
  293. goto_rel(pos.x, floorY + layer, pos.z)
  294. local dirX = 1
  295. goto_rel(sx, floorY + layer, sz)
  296. face(0)
  297. for rz=0,(Z-1) do
  298. if dirX==1 then face(0) else face(2) end
  299. for rx=0,(X-1) do
  300. mine_cell(floorY, fill_voids)
  301. if not (rx==X-1 and rz==Z-1) then
  302. if rx < X-1 then
  303. ensure_fuel()
  304. mine_step_forward()
  305. end
  306. end
  307. periodic_service()
  308. end
  309. if rz < Z-1 then
  310. face(1); mine_step_forward()
  311. dirX = 1 - dirX
  312. end
  313. end
  314. end
  315.  
  316. -- finish: return home, unload/refuel, then restore original facing
  317. go_home()
  318. ensure_dump_service()
  319. dump_at_home()
  320. refuel_from_home()
  321. face(start_heading)
  322. print("Done.")
  323.  
Advertisement
Add Comment
Please, Sign In to add comment