Advertisement
Base8

minerupdate

Jan 20th, 2025 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.41 KB | None | 0 0
  1. local t = turtle
  2. local FUELBUFFER = 30
  3. comingHome = false
  4.  
  5. local NORTH = 1
  6. local EAST = 2
  7. local SOUTH = 3
  8. local WEST = 4
  9.  
  10. local COAL_SLOT = 1
  11. local TORCH_SLOT = 2
  12.  
  13. local GOOD_ITEMS = {"minecraft:gold_ore","minecraft:iron_ore","minecraft:torch","minecraft:coal","minecraft:diamond","minecraft:redstone","minecraft:emerald"}
  14. local PLACEABLE_ITEMS = {"minecraft:cobblestone","minecraft:stone",}
  15. local TORCH_NAME = "minecraft:torch"
  16. local COAL_NAME = "minecraft:coal"
  17.  
  18. local COAL_DIRECTION = SOUTH
  19. local TORCH_DIRECTION = EAST
  20. --POINT CLASS-----------------------------------------------
  21.  
  22. Point = {}
  23. Point.__index = Point
  24. function Point:new(x,y,z,dir)
  25. local obj = {x=x,y=y,z=z,dir=dir}
  26. setmetatable(obj,Point)
  27. return obj
  28. end
  29.  
  30. function Point:forward()
  31. if self.dir == NORTH then
  32. self.y=self.y+1
  33. elseif self.dir == SOUTH then
  34. self.y=self.y-1
  35. elseif self.dir == EAST then
  36. self.x=self.x+1
  37. elseif self.dir == WEST then
  38. self.x=self.x-1
  39. end
  40. end
  41.  
  42. function Point:back()
  43. if self.dir == NORTH then
  44. self.y=self.y-1
  45. elseif self.dir == South then
  46. self.y=self.y+1
  47. elseif self.dir == EAST then
  48. self.x=self.x-1
  49. elseif self.dir == WEST then
  50. self.x=self.x+1
  51. end
  52. end
  53.  
  54. function Point:up()
  55. self.z=self.z+1
  56. end
  57.  
  58. function Point:down()
  59. self.z=self.z-1
  60. end
  61.  
  62. function Point:right()
  63. self.dir=self.dir+1
  64. if self.dir>WEST then
  65. self.dir = NORTH
  66. end
  67. end
  68.  
  69. function Point:left()
  70. self.dir=self.dir-1
  71. if self.dir<NORTH then
  72. self.dir = WEST
  73. end
  74. end
  75.  
  76. function Point:print()
  77. local dirText=""
  78. if self.dir == NORTH then
  79. dirText="NORTH"
  80. elseif self.dir == SOUTH then
  81. dirText="SOUTH"
  82. elseif self.dir == EAST then
  83. dirText="EAST"
  84. elseif self.dir == WEST then
  85. dirText="WEST"
  86. end
  87. print("Point: ("..self.x..","..self.y..","..self.z..","..dirText..")")
  88. end
  89.  
  90.  
  91. --POINT CLASS END -----------------------------------------------------
  92.  
  93. relativePos=Point:new(0,0,0,NORTH)
  94.  
  95. function onTurnon()
  96. fuel = getFuelLevel()
  97. end
  98.  
  99. function isGoodItem(name)
  100. for good in GOOD_ITEMS do
  101. if good == name then
  102. return true
  103. end
  104. end
  105. return false
  106. end
  107.  
  108. --Returns if the inventory is full. Will try to dump "bad" items before saying its full
  109. function isFull()
  110. local BUFFER_SPACES = 2 --Spaces needing to be cleared after being full to be considered good to continue
  111. --Check if every spot has an item in it
  112. for i = 1, 16 do
  113. if t.getItemCount(i)==0 then
  114. return false
  115. end
  116. end
  117. --If the inventory was full try to drop undesirable items
  118. local slotsDropped=0
  119. for i=0,15 do
  120. if not isGoodItem(getItemName(i)) then
  121. t.select(i)
  122. t.dropDown()
  123. slotsDropped=slotsDropped+1
  124. end
  125. end
  126. if slotsDropped<=BUFFER_SPACES or slotsDropped==0 then
  127. return true
  128. else
  129. return false
  130. end
  131. end
  132.  
  133. function getItemName(i)
  134. if t.getItemCount(i)==0 then
  135. return "nil"
  136. else
  137. return t.getItemDetail(i,false).name
  138. end
  139. end
  140.  
  141. function dumpInventory()
  142. for i = 1, 16 do
  143. t.select(i)
  144. if getItemName(i)==COAL_NAME then
  145. fFace(COAL_DIRECTION)
  146. t.drop()
  147. elseif i==TORCH_SLOT and getItemName(i)~=TORCH_NAME then
  148. t.dropDown()
  149. end
  150. end
  151. end
  152.  
  153. function restockFuel()
  154. fFace(COAL_DIRECTION)
  155. t.select(COAL_SLOT)
  156. t.suck()
  157. t.refuel()
  158. end
  159.  
  160. function restockTorches()
  161. fFace(TORCH_DIRECTION)
  162. t.select(TORCH_SLOT)
  163. t.suck()
  164. if outOfTorches() then
  165. print("Not enough torches to continue")
  166. shell.exit()
  167. end
  168. end
  169.  
  170. function outOfTorches()
  171. if getItemName(TORCH_SLOT)~=TORCH_NAME or t.getItemCount(TORCH_SLOT)==0 then
  172. return true
  173. else
  174. return false
  175. end
  176. end
  177.  
  178. --Return home, restock, and come back
  179. function restockReturn(previousPoint)
  180. comingHome=true
  181. print("Coming Home")
  182. --Fix Z
  183. while relativePos.z>0 do
  184. fDown()
  185. end
  186. while relativePos.z<0 do
  187. fUp()
  188. end
  189.  
  190. --Fix x
  191. if relativePos.x<0 then
  192. fFace(EAST)
  193. elseif relativePos.x>0 then
  194. fFace(WEST)
  195. end
  196. while relativePos.x~=0 do
  197. fForward()
  198. end
  199.  
  200. --Fix y
  201. if relativePos.y<0 then
  202. fFace(NORTH)
  203. elseif relativePos.y>0 then
  204. fFace(SOUTH)
  205. end
  206.  
  207. while relativePos.y~=0 do
  208. fForward()
  209. end
  210. print("At home")
  211. --At home base do everythign we need to
  212. dumpInventory()
  213. restockFuel()
  214. restockTorches()
  215.  
  216. print("Going back to war")
  217.  
  218. local currentFuel = t.getFuelLevel()
  219. local distanceOut = previousPoint.x + previousPoint.y + previousPoint.y
  220. if currentFuel<(distanceOut*3) then
  221. print("Not enough fuel to continue the mission")
  222. shell.exit()
  223. end
  224.  
  225. goBackTo(previousPoint)
  226. comingHome=false
  227. end
  228.  
  229. function goBackTo(pointA)
  230.  
  231. --Fix y
  232. if pointA.y>relativePos.y then
  233. fFace(NORTH)
  234. elseif pointA.y<relativePos.y then
  235. fFace(SOUTH)
  236. end
  237.  
  238. while pointA.y~=relativePos.y do
  239. fForward()
  240. end
  241.  
  242. --Fix x
  243. if pointA.x>relativePos.x then
  244. fFace(EAST)
  245. elseif pointA.x<relativePos.x then
  246. fFace(WEST)
  247. end
  248.  
  249. while pointA.x~=relativePos.x do
  250. fForward()
  251. end
  252.  
  253. --Fix Z
  254. while pointA.z<relativePos.z do
  255. fDown()
  256. end
  257. while pointA.z>relativePos.z do
  258. fUp()
  259. end
  260.  
  261. --Fix dir
  262. fFace(pointA.dir)
  263. end
  264.  
  265. --Get the current fuel level and return home if too close to empty
  266. --If we refuel we come back to the same posisiton/orientation
  267. function updateFuel()
  268. if comingHome then
  269. return
  270. end
  271. fuel = t.getFuelLevel()
  272. --Fuel to get back home is calculated
  273. local blocksBack = relativePos.x+relativePos.y+relativePos.z
  274. if fuel <= blocksBack + FUELBUFFER then
  275. local returnPoint = Point:new(relativePos.x,relativePos.y,relativePos.z,relativePos.dir)
  276. restockReturn(returnPoint)
  277. end
  278. end
  279.  
  280. function fDig()
  281. while t.detect() do
  282. updateFuel()
  283. t.dig()
  284. end
  285. end
  286.  
  287. function fDigDown()
  288. while t.detectDown() do
  289. updateFuel()
  290. t.digDown()
  291. end
  292. end
  293.  
  294. function fDigUp()
  295. while t.detectUp() do
  296. updateFuel()
  297. t.digUp()
  298. end
  299. end
  300.  
  301. function fForward(x)
  302. x = x or 1
  303. for i=1,x do
  304. while not t.forward() do
  305. updateFuel()
  306. fDig()
  307. end
  308. relativePos:forward()
  309. updateFuel()
  310. end
  311. end
  312.  
  313. function fDown()
  314. while not t.down() do
  315. updateFuel()
  316. fDigDown()
  317. end
  318. relativePos:down()
  319. updateFuel()
  320. end
  321.  
  322. function fUp()
  323. while not t.up() do
  324. updateFuel()
  325. fDigUp()
  326. end
  327. relativePos:up()
  328. updateFuel()
  329. end
  330.  
  331. function fRight()
  332. t.turnRight()
  333. relativePos:right()
  334. updateFuel()
  335. end
  336.  
  337. function fLeft()
  338. t.turnLeft()
  339. relativePos:left()
  340. updateFuel()
  341. end
  342.  
  343. function fTurnAround()
  344. fRight()
  345. fRight()
  346. end
  347.  
  348. function fBack()
  349. if not t.back() then
  350. fTurnAround()
  351. fDig()
  352. fTurnAround()
  353. end
  354. relativePos:back()
  355. updateFuel()
  356. end
  357.  
  358. function fFace(d)
  359. local temp = 0
  360. print("turning: "..d)
  361. if d == relativePos.dir then
  362. return
  363. elseif d>relativePos.dir then
  364. temp = d-relativePos.dir
  365. if temp==1 then
  366. fRight()
  367. elseif temp==2 then
  368. fTurnAround()
  369. elseif temp==3 then
  370. fLeft()
  371. end
  372. elseif d<relativePos.dir then
  373. temp = relativePos.dir-d
  374. if temp==1 then
  375. fLeft()
  376. elseif temp==2 then
  377. fTurnAround()
  378. elseif temp==3 then
  379. fRight()
  380. end
  381. end
  382. end
  383.  
  384. function goToZ(a)
  385. while relativePos.z<a do
  386. fUp()
  387. end
  388. while relativePos.z>a do
  389. fDown()
  390. end
  391. end
  392.  
  393. function placeTorch()
  394. if outOfTorches then
  395. local returnPoint = Point:new(relativePos.x,relativePos.y,relativePos.z,relativePos.dir)
  396. restockReturn(returnPoint)
  397. end
  398. local previousZ = relativePos.z
  399. goToZ(1)
  400. t.select(TORCH_SLOT)
  401. t.placeDown()
  402. goToZ(previousZ)
  403. end
  404.  
  405. --Assuming middle start
  406. function mine3By3(x)
  407. for i=0,x do
  408. --dig middle part
  409. fDig()
  410. fForward()
  411. fDigUp()
  412. fDigDown()
  413. --dig left part
  414. fLeft()
  415. fDig()
  416. fForward()
  417. fDigUp()
  418. fDigDown()
  419. --dig right part
  420. fTurnAround()
  421. fForward()
  422. fDig()
  423. fForward()
  424. fDigUp()
  425. fDigDown()
  426. --return
  427. fBack()
  428. fLeft()
  429. if i==5 then
  430. placeTorch()
  431. end
  432. --Checking inventory is slow only do it once per 3by3
  433. if isFull() then
  434. restockFuel()
  435. end
  436. end
  437. end
  438.  
  439. function mine()
  440. fUp()
  441. fFace(NORTH)
  442. while true do
  443. mine3By3(5)
  444. fLeft()
  445. fForward()
  446. mine3By3(5)
  447. fTurnAround()
  448. fForward(5+3)
  449. mine3By3(5)
  450. fTurnAround()
  451. fForward(5+2)
  452. fRight()
  453. end
  454. end
  455.  
  456. mine()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement