Advertisement
BigGamingGamers

Client

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