Advertisement
hexax2

zigZagTunnels.lua

Apr 7th, 2022
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.63 KB | None | 0 0
  1. json = require("json")
  2.  
  3. -- IO-Functions
  4.  
  5. function fileExists(name)
  6. local f=io.open(name,"r")
  7. if f~=nil then io.close(f) return true else return false end
  8. end
  9.  
  10. --- Saves in pFilePath the pTable as a json-document.
  11. --- Returns the success.
  12. ---@param pFilePath string pointing to the file.
  13. ---@param pTable table with all data that should be saved.
  14. ---@return boolean success of saving.
  15. function saveToJson(pFilePath, pTable)
  16. local file, err = io.open(pFilePath, 'w')
  17. if file then
  18. file:write(json.encode(pTable))
  19. file:close()
  20. return true
  21. else
  22. print("error:", err)
  23. return false
  24. end
  25. end
  26.  
  27. --- Loads the table from a json-document out of pFilePath.
  28. --- Returns the table or nil.
  29. ---@param pFilePath string pointing to the file.
  30. ---@return table that is represented by the files content.
  31. function loadFromJson(pFilePath)
  32. local file, err = io.open(pFilePath, 'r')
  33. if file then
  34. local read = file:read("*a")
  35. file:close()
  36. return json.decode(read) -- document is in JSON-format, so it decodes back to normal table
  37. else
  38. print("error:", err)
  39. return nil
  40. end
  41. saveToJson()
  42. end
  43.  
  44. savePath = "zigZagTunnelsTemp.json"
  45. -- The turtle has to start on top of a chest which has enough space for all the items that the turtle mines
  46. -- absolute Positions relative to starting position
  47.  
  48.  
  49. Direction = {
  50. FORWARD = 1,
  51. RIGHT = 2,
  52. BACKWARD = 3,
  53. LEFT = 4
  54. }
  55.  
  56.  
  57. saveObject = {
  58. absPosForward = 0,
  59. absPosRight = 0,
  60. absPosUp = 0,
  61.  
  62. absDirection = Direction.FORWARD,
  63.  
  64. layers = 0,
  65. tunnelWidth = 0,
  66. finishedLayers = 0,
  67.  
  68. loadingOff = {
  69. currently = false
  70. }
  71. }
  72.  
  73. function moveForward()
  74. if turtle.forward() == false then
  75. -- Failed to move, see if we can dig our way forward
  76. while turtle.dig() do
  77. -- Keep digging till we can't dig any more, in case gravel is falling.
  78. end
  79.  
  80. if turtle.forward() == false then
  81. print("I am either blocked or out of fuel.")
  82. return false
  83. end
  84. end
  85. -- turtle moved the right way
  86. if saveObject.absDirection == Direction.FORWARD then
  87. saveObject.absPosForward = saveObject.absPosForward + 1
  88. elseif saveObject.absDirection == Direction.RIGHT then
  89. saveObject.absPosRight = saveObject.absPosRight + 1
  90. elseif saveObject.absDirection == Direction.BACKWARD then
  91. saveObject.absPosForward = saveObject.absPosForward - 1
  92. elseif saveObject.absDirection == Direction.LEFT then
  93. saveObject.absPosRight = saveObject.absPosRight - 1
  94. end
  95. saveToJson(savePath, saveObject)
  96. return true
  97. end
  98.  
  99. function moveDown()
  100. if turtle.down() == false then
  101. -- Failed to move, see if we can dig our way down
  102. turtle.digDown()
  103. if turtle.down() == false then
  104. print("I am either blocked or out of fuel.")
  105. return false
  106. end
  107. end
  108. -- turtle moved the right way
  109. saveObject.absPosUp = saveObject.absPosUp - 1
  110. saveToJson(savePath, saveObject)
  111. return true
  112. end
  113.  
  114. function moveUp()
  115. if turtle.up() == false then
  116. -- Failed to move, see if we can dig our way down
  117. while turtle.digUp() do
  118. -- Keep digging till we can't dig any more, in case gravel is falling.
  119. end
  120. if turtle.up() == false then
  121. print("I am either blocked or out of fuel.")
  122. return false
  123. end
  124. end
  125. -- turtle moved the right way
  126. saveObject.absPosUp = saveObject.absPosUp + 1
  127. saveToJson(savePath, saveObject)
  128. return true
  129. end
  130.  
  131. function turnRight()
  132. if (turtle.turnRight() == false) then
  133. return false
  134. end
  135. if saveObject.absDirection == Direction.FORWARD then
  136. saveObject.absDirection = Direction.RIGHT
  137. elseif saveObject.absDirection == Direction.RIGHT then
  138. saveObject.absDirection = Direction.BACKWARD
  139. elseif saveObject.absDirection == Direction.BACKWARD then
  140. saveObject.absDirection = Direction.LEFT
  141. elseif saveObject.absDirection == Direction.LEFT then
  142. saveObject.absDirection = Direction.FORWARD
  143. end
  144. saveToJson(savePath, saveObject)
  145. return true
  146. end
  147.  
  148. function turnLeft()
  149. if (turtle.turnLeft() == false) then
  150. return false
  151. end
  152. if saveObject.absDirection == Direction.FORWARD then
  153. saveObject.absDirection = Direction.LEFT
  154. elseif saveObject.absDirection == Direction.RIGHT then
  155. saveObject.absDirection = Direction.FORWARD
  156. elseif saveObject.absDirection == Direction.BACKWARD then
  157. saveObject.absDirection = Direction.RIGHT
  158. elseif saveObject.absDirection == Direction.LEFT then
  159. saveObject.absDirection = Direction.BACKWARD
  160. end
  161. saveToJson(savePath, saveObject)
  162. return true
  163. end
  164.  
  165. -- Turns the turtle to the saveObject.absDirection pDir relative to its starting saveObject.absDirection
  166. function turnDirection(pDir)
  167. if (pDir == Direction.FORWARD) then
  168. if (saveObject.absDirection == Direction.FORWARD) then -- nothing to do
  169. elseif (saveObject.absDirection == Direction.RIGHT) then
  170. turnLeft()
  171. elseif (saveObject.absDirection == Direction.BACKWARD) then
  172. turnLeft()
  173. turnLeft()
  174. elseif (saveObject.absDirection == Direction.LEFT) then
  175. turnRight()
  176. end
  177. return true
  178. elseif (pDir == Direction.RIGHT) then
  179. if (saveObject.absDirection == Direction.FORWARD) then
  180. turnRight()
  181. elseif (saveObject.absDirection == Direction.RIGHT) then -- nothing to do
  182. elseif (saveObject.absDirection == Direction.BACKWARD) then
  183. turnLeft()
  184. elseif (saveObject.absDirection == Direction.LEFT) then
  185. turnLeft()
  186. turnLeft()
  187. end
  188. return true
  189. elseif (pDir == Direction.BACKWARD) then
  190. if (saveObject.absDirection == Direction.FORWARD) then
  191. turnLeft()
  192. turnLeft()
  193. elseif (saveObject.absDirection == Direction.RIGHT) then
  194. turnRight()
  195. elseif (saveObject.absDirection == Direction.BACKWARD) then -- nothing to do
  196. elseif (saveObject.absDirection == Direction.LEFT) then
  197. turnLeft()
  198. end
  199. return true
  200. elseif (pDir == Direction.LEFT) then
  201. if (saveObject.absDirection == Direction.FORWARD) then
  202. turnLeft()
  203. elseif (saveObject.absDirection == Direction.RIGHT) then
  204. turnLeft()
  205. turnLeft()
  206. elseif (saveObject.absDirection == Direction.BACKWARD) then
  207. turnRight()
  208. elseif (saveObject.absDirection == Direction.LEFT) then -- nothing to do
  209. end
  210. return true
  211. else
  212. print("pDir is no valid saveObject.absDirection. Could not orient.")
  213. return false
  214. end
  215. end
  216.  
  217. -- The turtle will destroy all Blocks in its way to get there
  218. -- returns true if successful
  219. function moveToCoordUp(pCoord)
  220. -- vertical to pCoord
  221. if saveObject.absPosUp > pCoord then
  222. while saveObject.absPosUp > pCoord do
  223. if (moveDown() == false) then
  224. return false
  225. end
  226. end
  227. elseif saveObject.absPosUp < pCoord then
  228. while saveObject.absPosUp < pCoord do
  229. if (moveUp() == false) then
  230. return false
  231. end
  232. end
  233. end
  234. return true
  235. end
  236.  
  237. -- The turtle will destroy all Blocks in its way to get there
  238. -- returns true if successful
  239. function moveToCoordForward(pCoord)
  240. -- forward/backward to pCoord
  241. -- first orient the right way
  242. if saveObject.absPosForward > pCoord then
  243. if (turnDirection(Direction.BACKWARD)) == false then
  244. return false
  245. end
  246. while saveObject.absPosForward > pCoord do -- move until at pCoord
  247. if (moveForward() == false) then
  248. return false
  249. end
  250. end
  251. elseif saveObject.absPosForward < pCoord then -- turn to absolute forward
  252. if (turnDirection(Direction.FORWARD)) == false then
  253. return false
  254. end
  255. while saveObject.absPosForward < pCoord do -- move until at pCoord
  256. if (moveForward() == false) then
  257. return false
  258. end
  259. end
  260. end
  261. return true
  262. end
  263.  
  264. -- The turtle will destroy all Blocks in its way to get there
  265. -- returns true if successful
  266. function moveToCoordRight(pCoord)
  267. -- left/right to pCoord
  268. -- first orient the right way
  269. if saveObject.absPosRight > pCoord then
  270. if (turnDirection(Direction.LEFT)) == false then
  271. return false
  272. end
  273. while saveObject.absPosRight > pCoord do -- move until at pCoord
  274. if (moveForward() == false) then
  275. return false
  276. end
  277. end
  278. elseif saveObject.absPosRight < pCoord then -- turn to absolute right
  279. if (turnDirection(Direction.RIGHT)) == false then
  280. return false
  281. end
  282. while saveObject.absPosRight < pCoord do -- move until at pCoord
  283. if (moveForward() == false) then
  284. return false
  285. end
  286. end
  287. end
  288. return true
  289. end
  290.  
  291. -- First move vertically, then forward/backward and lastly right/left
  292. -- Destroys blocks in its path
  293. function moveToStart()
  294. if (moveToCoordUp(0)) == false then
  295. return false
  296. end
  297. if (moveToCoordRight(0)) == false then
  298. return false
  299. end
  300. if (moveToCoordForward(0)) == false then
  301. return false
  302. end
  303. -- Now the turtle is at (0, 0, 0) relative to its initiating position
  304. return true
  305. end
  306.  
  307. function loadOff()
  308. for i = 1, 16 do
  309. turtle.select(i)
  310. turtle.dropDown()
  311. end
  312. turtle.select(1)
  313. end
  314.  
  315. function refuelWithAll()
  316. for i = 1, 16 do
  317. if turtle.getFuelLevel() / turtle.getFuelLimit() > 0.8 then
  318. return
  319. end
  320. turtle.select(i)
  321. turtle.refuel()
  322. end
  323. turtle.select(1)
  324. end
  325.  
  326. -- Loads off all items into chest under (0, 0, 0) position and returns to the position the turtle started this function in
  327. -- It trusts that the block under (0, 0, 0) is a chest or something else that can contain items
  328. -- Returns whether all items could be disposed or not
  329. function loadOffAndReturn()
  330. -- First, loads all information into the saveObject in case the server shuts down.
  331. if(saveObject.loadingOff.currently == false) then
  332. saveObject.loadingOff.currently = true
  333. saveObject.loadingOff.up = saveObject.absPosUp
  334. saveObject.loadingOff.forward = saveObject.absPosForward
  335. saveObject.loadingOff.right = saveObject.absPosRight
  336. saveObject.loadingOff.startDir = saveObject.absDirection
  337. end
  338.  
  339. if (moveToCoordUp(0)) == false then return false end
  340. if (moveToCoordRight(0)) == false then return false end
  341. if (moveToCoordForward(0)) == false then return false end
  342. if (refuelWithAll() == false or loadOff()) == false then return false end
  343. if (moveToCoordForward(saveObject.loadingOff.forward)) == false then return false end
  344. if (moveToCoordRight(saveObject.loadingOff.right)) == false then return false end
  345. if (moveToCoordUp(saveObject.loadingOff.up)) == false then return false end
  346. if (turnDirection(saveObject.loadingOff.startDir)) == false then return false end
  347.  
  348. saveObject.loadingOff = {currently = false} -- replaces the table of loadingOff with new table that only contains the currently = false
  349. return true -- finished without problems.
  350. end
  351.  
  352. -- The inventory is full when last slot is used by something.
  353. -- return true if inventory is full, else false
  354. function ivFull()
  355. return turtle.getItemCount(16) > 0
  356. end
  357.  
  358. -- Checks the inventory and loads off if necessary
  359. function handleLoadOff()
  360. if (ivFull()) then
  361. loadOffAndReturn()
  362. end
  363. end
  364.  
  365. function handleForwardAndMakeHumanPath()
  366. if moveForward() == false then
  367. print("Could not make forward movement. Stopped early.")
  368. return false
  369. end
  370. handleLoadOff()
  371. while turtle.digUp() do -- Dig upwards while there is gravel or sand
  372. end
  373. handleLoadOff()
  374. end
  375.  
  376. function mineHumanTunnel(pDirection, length)
  377. turnDirection(pDirection)
  378. for i = 1, length do -- beginnt mit i = 1 und letzter Durchlauf ist mit i = length
  379. if handleForwardAndMakeHumanPath() == false then -- Jedes mal wird geminet und dabei überprüft, ob das minen Erfolg hat
  380. print("Broke mineTunnel.")
  381. break
  382. end
  383. end
  384. end
  385.  
  386. -- BEGINN -------------------------------------------------------------------------------------------------------
  387.  
  388. local function mineZigZagTunnelForwardAndLeft(pTunnelWidth)
  389. mineHumanTunnel(Direction.FORWARD, 3)
  390. mineHumanTunnel(Direction.LEFT, pTunnelWidth-1)
  391. saveObject.finishedLayers = saveObject.finishedLayers + 1
  392. saveToJson(savePath, saveObject)
  393. end
  394.  
  395. local function mineZigZagTunnelForwardAndRight(pTunnelWidth)
  396. mineHumanTunnel(Direction.FORWARD, 3)
  397. mineHumanTunnel(Direction.RIGHT, pTunnelWidth-1)
  398. saveObject.finishedLayers = saveObject.finishedLayers + 1
  399. saveToJson(savePath, saveObject)
  400. end
  401.  
  402. -- Turtle beginnt unten links, gräbt 3 Blöcke vorwärts, pTunnelWidth-1 nach rechts, 3 vorwärts und wieder pTunnelWidth-1 nach links.
  403. -- Am Ende steht sie richtung Direction.FORWARD
  404. local function mineZigZagTunnels(pTunnelWidth)
  405. mineZigZagTunnelForwardAndRight(pTunnelWidth)
  406. mineZigZagTunnelForwardAndLeft(pTunnelWidth)
  407. end
  408.  
  409. --- The turtle has to understand where it stopped to start at that position.
  410. --- if it was loading off, it should continue that.
  411. local function understandPositionAndContinue()
  412. saveObject = loadFromJson(savePath)
  413. if saveObject.loadingOff.currently == true then
  414. loadOffAndReturn()
  415. end
  416.  
  417. -- Die Turtle ist wieder auf ihrem Platz und bereit, weiterzumachen. Jetzt muss sie wissen, wie sie weiterarbeiten soll.
  418. -- Wenn die Turtle bei Forward == 0, 3, 6, 9 usw. ist, soll sie seitlich arbeiten.
  419. -- Ist sie bei Forward == 0, 6, 12 usw. , soll sie nach links graben bis sie Right == -(tunnelWidth-1)/2 erreicht.
  420. -- Ist sie bei Forward == 3, 9, 15 usw. , soll sie nach rechts graben bis sie Right == (tunnelWidth-1)/2 erreicht.
  421. -- Hat sie den linkesten/rechtesten Spot erreicht, wird finishedLayers um 1 erhöht und sie macht normal weiter.
  422. -- Wenn die Turtle nicht bei Forward == 0, 3, 6, usw. ist, dann gäbt sie rückwärts, bis sie bei Forward == 0, 3, 6 usw. ist.
  423. -- Auch hier macht sie dann normal weiter.
  424. if (saveObject.absPosForward % 6 == 0) then -- Ist sie bei Forward == 0, 6, 12 usw. , soll sie nach links graben bis sie Right == -(tunnelWidth-1)/2 erreicht.
  425. turnDirection(Direction.LEFT)
  426. for i = saveObject.absPosRight, -(saveObject.tunnelWidth-1)/2, -1 do -- Auch noch beim vorletzten Block einmal graben. Danach nicht mehr.
  427. handleForwardAndMakeHumanPath()
  428. end
  429. saveObject.finishedLayers = saveObject.finishedLayers + 1
  430. elseif (saveObject.absPosForward % 6 == 3) then
  431. turnDirection(Direction.RIGHT)
  432. for i = saveObject.absPosRight, (saveObject.tunnelWidth-1)/2, 1 do -- Auch noch beim vorletzten Block einmal graben. Danach nicht mehr.
  433. handleForwardAndMakeHumanPath()
  434. end
  435. saveObject.finishedLayers = saveObject.finishedLayers + 1
  436. else -- Wenn die Turtle nicht bei Forward == 0, 3, 6, usw. ist, dann gäbt sie rückwärts, bis sie bei Forward == 0, 3, 6 usw. ist.
  437. turnDirection(Direction.BACKWARD)
  438. while saveObject.absPosForward % 3 > 0 do
  439. handleForwardAndMakeHumanPath()
  440. end
  441. end
  442.  
  443. -- Nun ist noch zu unterscheiden, ob die Turtle links unten oder rechts unten ist.
  444. if (saveObject.absPosRight > 0 and saveObject.finishedLayers < layers) then
  445. mineZigZagTunnelForwardAndLeft(saveObject.tunnelWidth)
  446. end
  447.  
  448. -- Zuletzt noch die Tunnel fertig graben, die noch gewünscht sind
  449. for i = saveObject.finishedLayers, saveObject.layers - 1, 2 do -- Die Schleife wird zuletzt noch einmal mit i = layers - 1 durchgeführt
  450. mineZigZagTunnels(pTunnelWidth)
  451. end
  452.  
  453. end
  454.  
  455. -- Turtle steht auf der Kiste. Gräbt (tunnelWidth-1)/2 nach links. Danach macht die Turtle layers-viele ZigZagTunnels und kehrt zum Anfang zurück.
  456. local function run(pLayers, pTunnelWidth)
  457. if(fileExists(savePath)) then
  458. understandPositionAndContinue()
  459. else
  460. mineHumanTunnel(Direction.LEFT, (pTunnelWidth-1)/2)
  461. saveObject.finishedLayers = saveObject.finishedLayers + 1
  462. for i = 1, pLayers - 1, 2 do
  463. mineZigZagTunnels(pTunnelWidth)
  464. end
  465. end
  466. moveToStart()
  467. loadOff()
  468. fs.delete(savePath) -- saveFile gets removed to let the script now, that a totally new saveFile has to be created next time
  469. end
  470.  
  471.  
  472.  
  473. local tArgs = {...}
  474.  
  475. if (fileExists(savePath)) then
  476. if (#tArgs >= 1) then
  477. print("Delete "..savePath.." and try again.")
  478. return
  479. end
  480. -- saveObject enthält die nötigen Variablen bereits
  481. else -- saveObject hat noch keine Werte
  482. if (#tArgs <= 1 or #tArgs >= 3) then
  483. print("Usage: <layers> <tunnelWidth>")
  484. return
  485. end
  486.  
  487. if (tonumber(tArgs[1]) < 1 or tonumber(tArgs[2]) < 1 or tonumber(tArgs[1]) % 2 == 0 or tonumber(tArgs[2]) % 2 == 0) then
  488. print("Usage: <layers> <tunnelWidth>")
  489. print("layers and tunnelWidth have to be positive and odd.")
  490. return
  491. end
  492.  
  493. saveObject.layers = tonumber(tArgs[1])
  494. saveObject.tunnelWidth = tonumber(tArgs[2])
  495. end
  496.  
  497. run(saveObject.layers, saveObject.tunnelWidth)
  498.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement