BigGamingGamers

Untitled

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