Advertisement
BigGamingGamers

Client Dig

Nov 13th, 2020
13,263
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.54 KB | None | 1 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("left")
  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. else
  186. print("INVALID FUEL SOURCE")
  187. os.exit(1)
  188. end
  189.  
  190. return math.floor(totalFuel) + 5
  191. end
  192.  
  193.  
  194.  
  195. modem.transmit(SERVER_PORT, CLIENT_PORT, "CLIENT_DEPLOYED")
  196. event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  197. data = parseParams(msg)
  198.  
  199. -- Pick up coal and refuel
  200. local fuelNeeded = calculateFuel(data[1], data[2], "minecraft:coal")
  201. turtle.suckDown(fuelNeeded)
  202. checkFuel()
  203.  
  204. print(string.format( "Extracting %d fuel...", fuelNeeded))
  205.  
  206. -- Grab Ender Chest
  207. local startCoords = data[1]
  208. local finalHeading = moveTo(startCoords, getOrientation())
  209.  
  210. local NORTH_HEADING = 2
  211. --Turn to face North
  212. turnToFaceHeading(finalHeading, NORTH_HEADING)
  213. finalHeading = NORTH_HEADING
  214. --Now in Starting Position--
  215.  
  216. --------------------------------START MINING CODE-----------------------------------------
  217.  
  218.  
  219.  
  220.  
  221.  
  222. ------------------------------------------------------------------------------------------
  223.  
  224. DROPPED_ITEMS = {
  225. "minecraft:stone",
  226. "minecraft:dirt",
  227. "minecraft:cobblestone",
  228. "minecraft:sand",
  229. "minecraft:gravel",
  230. "minecraft:redstone",
  231. "minecraft:flint",
  232. "railcraft:ore_metal",
  233. "extrautils2:ingredients",
  234. "minecraft:dye",
  235. "thaumcraft:nugget",
  236. "thaumcraft:crystal_essence",
  237. "thermalfoundation:material",
  238. "projectred-core:resource_item",
  239. "thaumcraft:ore_cinnabar",
  240. "deepresonance:resonating_ore",
  241. "forestry:apatite"
  242. }
  243.  
  244.  
  245.  
  246. function getItemIndex(itemName)
  247. for slot = 1, SLOT_COUNT, 1 do
  248. local item = turtle.getItemDetail(slot)
  249. if(item ~= nil) then
  250. if(item["name"] == itemName) then
  251. return slot
  252. end
  253. end
  254. end
  255. end
  256.  
  257. function dropItems()
  258. print("Purging Inventory...")
  259. for slot = 1, SLOT_COUNT, 1 do
  260. local item = turtle.getItemDetail(slot)
  261. if(item ~= nil) then
  262. for filterIndex = 1, #DROPPED_ITEMS, 1 do
  263. if(item["name"] ~= "minecraft:coal_block" and item["name"] ~= "minecraft:coal") then
  264. print("Dropping - " .. item["name"])
  265. turtle.select(slot)
  266. turtle.dropDown()
  267. end
  268. end
  269. end
  270. end
  271. end
  272.  
  273.  
  274. function manageInventory()
  275. dropItems()
  276. end
  277.  
  278. function detectAndDig()
  279. while(turtle.detect()) do
  280. turtle.dig()
  281. end
  282. end
  283.  
  284. function forward()
  285. detectAndDig()
  286. turtle.forward()
  287. end
  288.  
  289. function leftTurn()
  290. turtle.turnLeft()
  291. detectAndDig()
  292. turtle.forward()
  293. turtle.turnLeft()
  294. end
  295.  
  296.  
  297. function rightTurn()
  298. turtle.turnRight()
  299. detectAndDig()
  300. turtle.forward()
  301. turtle.turnRight()
  302. end
  303.  
  304.  
  305. function dropTier(heading)
  306. turtle.turnRight()
  307. turtle.turnRight()
  308. turtle.digDown()
  309. turtle.down()
  310. return flipDirection(heading)
  311. end
  312.  
  313.  
  314. function flipDirection(heading)
  315. return ((heading + 1) % 4) + 1
  316. end
  317.  
  318. function turnAround(tier, heading)
  319. if(tier % 2 == 1) then
  320. if(heading == 2 or heading == 3) then
  321. rightTurn()
  322. elseif(heading == 1 or heading == 4) then
  323. leftTurn()
  324. end
  325. else
  326. if(heading == 2 or heading == 3) then
  327. leftTurn()
  328. elseif(heading == 1 or heading == 4) then
  329. rightTurn()
  330. end
  331. end
  332.  
  333. return flipDirection(heading)
  334. end
  335.  
  336.  
  337.  
  338. function startQuary(width, height, depth, heading)
  339.  
  340. for tier = 1, height, 1 do
  341. for col = 1, width, 1 do
  342. for row = 1, depth - 1, 1 do
  343. if(not checkFuel()) then
  344. print("Turtle is out of fuel, Powering Down...")
  345. return
  346. end
  347. forward()
  348. end
  349. if(col ~= width) then
  350. heading = turnAround(tier, heading)
  351. end
  352. manageInventory()
  353. end
  354. if(tier ~= height) then
  355. heading = dropTier(heading)
  356. end
  357. end
  358.  
  359. return heading
  360. end
  361.  
  362.  
  363. local quary = data[2]
  364. finishedHeading = startQuary(quary.x, quary.y, quary.z, finalHeading)
  365.  
  366.  
  367.  
  368. --------------------------------START RETURN TRIP CODE------------------------------------
  369.  
  370.  
  371.  
  372.  
  373.  
  374. ------------------------------------------------------------------------------------------
  375.  
  376.  
  377. function returnTo(coords, heading)
  378. local currX, currY, currZ = gps.locate()
  379. local xDiff, yDiff, zDiff = coords.x - currX, coords.y - currY, coords.z - currZ
  380. print(string.format("Distances from end: %d %d %d", xDiff, yDiff, zDiff))
  381.  
  382. -- Move to Y start
  383. if(yDiff < 0) then
  384. digAndMoveDown(math.abs(yDiff))
  385. elseif(yDiff > 0) then
  386. digAndMoveUp(math.abs(yDiff))
  387. end
  388.  
  389. -- Move to X start
  390. heading = setHeadingX(xDiff, heading)
  391. digAndMove(math.abs(xDiff))
  392.  
  393. -- Move to Z start
  394. heading = setHeadingZ(zDiff, heading)
  395. digAndMove(math.abs(zDiff))
  396.  
  397.  
  398.  
  399. return heading
  400. end
  401.  
  402. endCoords = data[3]
  403. returnTo(endCoords ,finishedHeading)
  404.  
  405. local timoutWait = 60
  406. for i = 1, timoutWait, 1 do
  407. os.sleep(1)
  408. print(string.format( "Waiting for brothers %d/%d", i, timoutWait))
  409. end
  410.  
  411. modem.transmit(SERVER_PORT, CLIENT_PORT, "cum")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement