Maengorn

Client dig (front loaded disk)

Dec 23rd, 2020 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.39 KB | None | 0 0
  1. --CLIENT DIG--
  2.  
  3. local SLOT_COUNT = 16
  4.  
  5. local CLIENT_PORT = 0
  6. local SERVER_PORT = 420
  7.  
  8. local modem = peripheral.wrap("right")
  9. modem.open(CLIENT_PORT)
  10.  
  11. function split (inputstr, sep)
  12. if sep == nil then
  13. sep = "%s"
  14. end
  15. local t={}
  16. for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  17. table.insert(t, str)
  18. end
  19. return t
  20. end
  21.  
  22. function parseParams(data)
  23. coords = {}
  24. params = split(data, " ")
  25.  
  26. coords[1] = vector.new(params[1], params[2], params[3])
  27. coords[2] = vector.new(params[4], params[5], params[6])
  28. coords[3] = vector.new(params[7], params[8], params[9])
  29.  
  30. return (coords)
  31. end
  32.  
  33.  
  34. function checkFuel()
  35. turtle.select(1)
  36.  
  37. if(turtle.getFuelLevel() < 50) then
  38. print("Attempting Refuel...")
  39. for slot = 1, SLOT_COUNT, 1 do
  40. turtle.select(slot)
  41. if(turtle.refuel(1)) then
  42. return true
  43. end
  44. end
  45.  
  46. return false
  47. else
  48. return true
  49. end
  50. end
  51.  
  52.  
  53. function getOrientation()
  54. loc1 = vector.new(gps.locate(2, false))
  55. if not turtle.forward() then
  56. for j=1,6 do
  57. if not turtle.forward() then
  58. turtle.dig()
  59. else
  60. break
  61. end
  62. end
  63. end
  64. loc2 = vector.new(gps.locate(2, false))
  65. heading = loc2 - loc1
  66. turtle.down()
  67. turtle.down()
  68. return ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))
  69. end
  70.  
  71.  
  72. function turnToFaceHeading(heading, destinationHeading)
  73. if(heading > destinationHeading) then
  74. for t = 1, math.abs(destinationHeading - heading), 1 do
  75. turtle.turnLeft()
  76. end
  77. elseif(heading < destinationHeading) then
  78. for t = 1, math.abs(destinationHeading - heading), 1 do
  79. turtle.turnRight()
  80. end
  81. end
  82. end
  83.  
  84. function setHeadingZ(zDiff, heading)
  85. local destinationHeading = heading
  86. if(zDiff < 0) then
  87. destinationHeading = 2
  88. elseif(zDiff > 0) then
  89. destinationHeading = 4
  90. end
  91. turnToFaceHeading(heading, destinationHeading)
  92.  
  93. return destinationHeading
  94. end
  95.  
  96. function setHeadingX(xDiff, heading)
  97. local destinationHeading = heading
  98. if(xDiff < 0) then
  99. destinationHeading = 1
  100. elseif(xDiff > 0) then
  101. destinationHeading = 3
  102. end
  103.  
  104. turnToFaceHeading(heading, destinationHeading)
  105. return destinationHeading
  106. end
  107.  
  108. function digAndMove(n)
  109. for x = 1, n, 1 do
  110. while(turtle.detect()) do
  111. turtle.dig()
  112. end
  113. turtle.forward()
  114. checkFuel()
  115. end
  116. end
  117.  
  118. function digAndMoveDown(n)
  119. for y = 1, n, 1 do
  120. print(y)
  121. while(turtle.detectDown()) do
  122. turtle.digDown()
  123. end
  124. turtle.down()
  125. checkFuel()
  126. end
  127. end
  128.  
  129. function digAndMoveUp(n)
  130. for y = 1, n, 1 do
  131. while(turtle.detectUp()) do
  132. turtle.digUp()
  133. end
  134. turtle.up()
  135. checkFuel()
  136. end
  137. end
  138.  
  139.  
  140. function moveTo(coords, heading)
  141. local currX, currY, currZ = gps.locate()
  142. local xDiff, yDiff, zDiff = coords.x - currX, coords.y - currY, coords.z - currZ
  143. print(string.format("Distances from start: %d %d %d", xDiff, yDiff, zDiff))
  144.  
  145. -- -x = 1
  146. -- -z = 2
  147. -- +x = 3
  148. -- +z = 4
  149.  
  150.  
  151. -- Move to X start
  152. heading = setHeadingX(xDiff, heading)
  153. digAndMove(math.abs(xDiff))
  154.  
  155. -- Move to Z start
  156. heading = setHeadingZ(zDiff, heading)
  157. digAndMove(math.abs(zDiff))
  158.  
  159. -- Move to Y start
  160. if(yDiff < 0) then
  161. digAndMoveDown(math.abs(yDiff))
  162. elseif(yDiff > 0) then
  163. digAndMoveUp(math.abs(yDiff))
  164. end
  165.  
  166.  
  167. return heading
  168. end
  169.  
  170.  
  171. function calculateFuel(travels, digSize, fuelType)
  172. local currX, currY, currZ = gps.locate()
  173. local xDiff, yDiff, zDiff = travels.x - currX, travels.y - currY, travels.z - currZ
  174.  
  175. local volume = digSize.x + digSize.y + digSize.z
  176. local travelDistance = (math.abs(xDiff) + math.abs(yDiff) + math.abs(zDiff)) * 2
  177.  
  178. local totalFuel = volume + travelDistance
  179. print(string.format( "total steps: %d", totalFuel))
  180.  
  181. if(fuelType == "minecraft:coal") then
  182. totalFuel = totalFuel / 80
  183. elseif(fuelType == "minecraft:coal_block") then
  184. totalFuel = totalFuel / 800
  185. elseif(fuelType == "minecraft:charcoal") then
  186. totalFuel = totalFuel / 80
  187. else
  188. print("INVALID FUEL SOURCE")
  189. os.exit(1)
  190. end
  191.  
  192. return math.floor(totalFuel) + 5
  193. end
  194.  
  195.  
  196.  
  197. modem.transmit(SERVER_PORT, CLIENT_PORT, "CLIENT_DEPLOYED")
  198. event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  199. data = parseParams(msg)
  200.  
  201. -- Pick up coal and refuel
  202. local fuelNeeded = calculateFuel(data[1], data[2], "minecraft:coal")
  203. turtle.turnRight(1)
  204. turtle.suck(fuelNeeded)
  205. turtle.turnLeft(1)
  206. checkFuel()
  207.  
  208. print(string.format( "Extracting %d fuel...", fuelNeeded))
  209.  
  210. local startCoords = data[1]
  211.  
  212. -- Grab Ender Chest
  213. turtle.down(1)
  214. turtle.suckDown(1)
  215.  
  216. local finalHeading = moveTo(startCoords, getOrientation())
  217.  
  218. local NORTH_HEADING = 2
  219. --Turn to face North
  220. turnToFaceHeading(finalHeading, NORTH_HEADING)
  221. finalHeading = NORTH_HEADING
  222. --Now in Starting Position--
  223.  
  224. --------------------------------START MINING CODE-----------------------------------------
  225.  
  226.  
  227.  
  228.  
  229.  
  230. ------------------------------------------------------------------------------------------
  231.  
  232. DROPPED_ITEMS = {
  233. "minecraft:stone",
  234. "minecraft:dirt",
  235. "minecraft:basalt",
  236. "minecraft:granite",
  237. "minecraft:cobblestone",
  238. "minecraft:sand",
  239. "minecraft:gravel",
  240. "minecraft:redstone",
  241. "minecraft:flint",
  242. "railcraft:ore_metal",
  243. "extrautils2:ingredients",
  244. "minecraft:dye",
  245. "thaumcraft:nugget",
  246. "thaumcraft:crystal_essence",
  247. "thermalfoundation:material",
  248. "projectred-core:resource_item",
  249. "thaumcraft:ore_cinnabar",
  250. "deepresonance:resonating_ore",
  251. "forestry:apatite",
  252. "biomesoplenty:loamy_dirt",
  253. "chisel:marble",
  254. "chisel:limestone",
  255. }
  256. function dropItems()
  257. print("Purging Inventory...")
  258. for slot = 1, SLOT_COUNT, 1 do
  259. local item = turtle.getItemDetail(slot)
  260. if(item ~= nil) then
  261. for filterIndex = 1, #DROPPED_ITEMS, 1 do
  262. if(item["name"] == DROPPED_ITEMS[filterIndex]) then
  263. print("Dropping - " .. item["name"])
  264. turtle.select(slot)
  265. turtle.dropDown()
  266. end
  267. end
  268. end
  269. end
  270. end
  271.  
  272.  
  273. function getEnderIndex()
  274. for slot = 1, SLOT_COUNT, 1 do
  275. local item = turtle.getItemDetail(slot)
  276. if(item ~= nil) then
  277. if(item["name"] == "enderstorage:ender_storage") then
  278. return slot
  279. end
  280. end
  281. end
  282. return nil
  283. end
  284.  
  285. function manageInventory()
  286. dropItems()
  287. index = getEnderIndex()
  288. if(index ~= nil) then
  289. turtle.select(index)
  290. turtle.digUp()
  291. turtle.placeUp()
  292. end
  293. -- Chest is now deployed
  294. for slot = 1, SLOT_COUNT, 1 do
  295. local item = turtle.getItemDetail(slot)
  296. if(item ~= nil) then
  297. if(item["name"] ~= "minecraft:coal_block" and item["name"] ~= "minecraft:coal" and item["name"] ~= "minecraft:charcoal") then
  298. turtle.select(slot)
  299. turtle.dropUp()
  300. end
  301. end
  302. end
  303. -- Items are now stored
  304.  
  305. turtle.digUp()
  306. end
  307.  
  308.  
  309. function detectAndDig()
  310. while(turtle.detect()) do
  311. turtle.dig()
  312. end
  313. end
  314.  
  315. function forward()
  316. detectAndDig()
  317. turtle.forward()
  318. end
  319.  
  320. function leftTurn()
  321. turtle.turnLeft()
  322. detectAndDig()
  323. turtle.forward()
  324. turtle.turnLeft()
  325. end
  326.  
  327.  
  328. function rightTurn()
  329. turtle.turnRight()
  330. detectAndDig()
  331. turtle.forward()
  332. turtle.turnRight()
  333. end
  334.  
  335.  
  336. function dropTier(heading)
  337. turtle.turnRight()
  338. turtle.turnRight()
  339. turtle.digDown()
  340. turtle.down()
  341. return flipDirection(heading)
  342. end
  343.  
  344.  
  345. function flipDirection(heading)
  346. return ((heading + 1) % 4) + 1
  347. end
  348.  
  349. function turnAround(tier, heading)
  350. if(tier % 2 == 1) then
  351. if(heading == 2 or heading == 3) then
  352. rightTurn()
  353. elseif(heading == 1 or heading == 4) then
  354. leftTurn()
  355. end
  356. else
  357. if(heading == 2 or heading == 3) then
  358. leftTurn()
  359. elseif(heading == 1 or heading == 4) then
  360. rightTurn()
  361. end
  362. end
  363.  
  364. return flipDirection(heading)
  365. end
  366.  
  367.  
  368.  
  369. function startQuary(width, height, depth, heading)
  370.  
  371. for tier = 1, height, 1 do
  372. for col = 1, width, 1 do
  373. for row = 1, depth - 1, 1 do
  374. if(not checkFuel()) then
  375. print("Turtle is out of fuel, Powering Down...")
  376. return
  377. end
  378. forward()
  379. end
  380. if(col ~= width) then
  381. heading = turnAround(tier, heading)
  382. end
  383. manageInventory()
  384. end
  385. if(tier ~= height) then
  386. heading = dropTier(heading)
  387. end
  388. end
  389.  
  390. return heading
  391. end
  392.  
  393.  
  394. local quary = data[2]
  395. finishedHeading = startQuary(quary.x, quary.y, quary.z, finalHeading)
  396.  
  397.  
  398.  
  399. --------------------------------START RETURN TRIP CODE------------------------------------
  400.  
  401.  
  402.  
  403.  
  404.  
  405. ------------------------------------------------------------------------------------------
  406.  
  407.  
  408. function returnTo(coords, heading)
  409. local currX, currY, currZ = gps.locate()
  410. local xDiff, yDiff, zDiff = coords.x - currX, coords.y - currY, coords.z - currZ
  411. print(string.format("Distances from end: %d %d %d", xDiff, yDiff, zDiff))
  412.  
  413. -- Move to Y start
  414. if(yDiff < 0) then
  415. digAndMoveDown(math.abs(yDiff))
  416. elseif(yDiff > 0) then
  417. digAndMoveUp(math.abs(yDiff))
  418. end
  419.  
  420. -- Move to X start
  421. heading = setHeadingX(xDiff, heading)
  422. digAndMove(math.abs(xDiff))
  423.  
  424. -- Move to Z start
  425. heading = setHeadingZ(zDiff, heading)
  426. digAndMove(math.abs(zDiff))
  427.  
  428.  
  429.  
  430. return heading
  431. end
  432.  
  433. endCoords = data[3]
  434. returnTo(endCoords ,finishedHeading)
  435.  
  436. local timoutWait = 90
  437. for i = 1, timoutWait, 1 do
  438. os.sleep(1)
  439. print(string.format( "Waiting for brothers %d/%d", i, timoutWait))
  440. end
  441.  
  442. modem.transmit(SERVER_PORT, CLIENT_PORT, "cum")
Advertisement
Add Comment
Please, Sign In to add comment