client-quarry.lua

430 lines · 9.6 KB

Open raw

CLIENT--

Copy & run

wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/BigBaemingGamers/client/client-quarry.lua
1--CLIENT--
2
3local SLOT_COUNT = 16
4
5local CLIENT_PORT = 0
6local SERVER_PORT = 420
7
8local modem = peripheral.find("modem", function(_, m) return m.isWireless and m.isWireless() end)
9if not modem then error("No wireless modem attached") end
10modem.open(CLIENT_PORT)
11
12function 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
21end
22
23function 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)
32end
33
34
35function checkFuel()
36 turtle.select(1)
37
38 if(turtle.getFuelLevel() < 50) then
39 print("Attempting Refuel...")
40 for slot = 1, SLOT_COUNT, 1 do
41 turtle.select(slot)
42 if(turtle.refuel(1)) then
43 return true
44 end
45 end
46
47 return false
48 else
49 return true
50 end
51end
52
53
54function getOrientation()
55 loc1 = vector.new(gps.locate(2, false))
56 if not turtle.forward() then
57 for j=1,6 do
58 if not turtle.forward() then
59 turtle.dig()
60 else
61 break
62 end
63 end
64 end
65 loc2 = vector.new(gps.locate(2, false))
66 heading = loc2 - loc1
67 turtle.down()
68 turtle.down()
69 return ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))
70 end
71
72
73function turnToFaceHeading(heading, destinationHeading)
74 if(heading > destinationHeading) then
75 for t = 1, math.abs(destinationHeading - heading), 1 do
76 turtle.turnLeft()
77 end
78 elseif(heading < destinationHeading) then
79 for t = 1, math.abs(destinationHeading - heading), 1 do
80 turtle.turnRight()
81 end
82 end
83end
84
85function setHeadingZ(zDiff, heading)
86 local destinationHeading = heading
87 if(zDiff < 0) then
88 destinationHeading = 2
89 elseif(zDiff > 0) then
90 destinationHeading = 4
91 end
92 turnToFaceHeading(heading, destinationHeading)
93
94 return destinationHeading
95end
96
97function setHeadingX(xDiff, heading)
98 local destinationHeading = heading
99 if(xDiff < 0) then
100 destinationHeading = 1
101 elseif(xDiff > 0) then
102 destinationHeading = 3
103 end
104
105 turnToFaceHeading(heading, destinationHeading)
106 return destinationHeading
107end
108
109function digAndMove(n)
110 for x = 1, n, 1 do
111 while(turtle.detect()) do
112 turtle.dig()
113 end
114 turtle.forward()
115 checkFuel()
116 end
117end
118
119function digAndMoveDown(n)
120 for y = 1, n, 1 do
121 print(y)
122 while(turtle.detectDown()) do
123 turtle.digDown()
124 end
125 turtle.down()
126 checkFuel()
127 end
128end
129
130function digAndMoveUp(n)
131 for y = 1, n, 1 do
132 while(turtle.detectUp()) do
133 turtle.digUp()
134 end
135 turtle.up()
136 checkFuel()
137 end
138end
139
140
141function moveTo(coords, heading)
142 local currX, currY, currZ = gps.locate()
143 local xDiff, yDiff, zDiff = coords.x - currX, coords.y - currY, coords.z - currZ
144 print(string.format("Distances from start: %d %d %d", xDiff, yDiff, zDiff))
145
146 -- -x = 1
147 -- -z = 2
148 -- +x = 3
149 -- +z = 4
150
151
152 -- Move to X start
153 heading = setHeadingX(xDiff, heading)
154 digAndMove(math.abs(xDiff))
155
156 -- Move to Z start
157 heading = setHeadingZ(zDiff, heading)
158 digAndMove(math.abs(zDiff))
159
160 -- Move to Y start
161 if(yDiff < 0) then
162 digAndMoveDown(math.abs(yDiff))
163 elseif(yDiff > 0) then
164 digAndMoveUp(math.abs(yDiff))
165 end
166
167
168 return heading
169end
170
171
172function calculateFuel(travels, digSize, fuelType)
173 local currX, currY, currZ = gps.locate()
174 local xDiff, yDiff, zDiff = travels.x - currX, travels.y - currY, travels.z - currZ
175
176 local volume = digSize.x + digSize.y + digSize.z
177 local travelDistance = (xDiff + yDiff + zDiff) * 2
178
179 local totalFuel = volume + travelDistance
180 if(fuelType == "minecraft:coal") then
181 totalFuel = totalFuel / 80
182 elseif(fuelType == "minecraft:coal_block") then
183 totalFuel = totalFuel / 800
184 else
185 print("INVALID FUEL SOURCE")
186 os.exit(1)
187 end
188
189 return math.floor(totalFuel) + 5
190end
191
192
193
194modem.transmit(SERVER_PORT, CLIENT_PORT, "CLIENT_DEPLOYED")
195event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
196data = parseParams(msg)
197
198-- Pick up coal and refuel
199local fuelNeeded = calculateFuel(data[1], data[2], "minecraft:coal")
200turtle.suckDown(fuelNeeded)
201checkFuel()
202
203print(string.format( "Extracting %d fuel...", fuelNeeded))
204
205-- Grab Ender Chest
206turtle.digUp()
207local startCoords = data[1]
208local finalHeading = moveTo(startCoords, getOrientation())
209
210local NORTH_HEADING = 2
211--Turn to face North
212turnToFaceHeading(finalHeading, NORTH_HEADING)
213finalHeading = NORTH_HEADING
214--Now in Starting Position--
215
216--------------------------------START MINING CODE-----------------------------------------
217
218
219
220
221
222------------------------------------------------------------------------------------------
223
224DROPPED_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
246function 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
255end
256
257function 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"] == DROPPED_ITEMS[filterIndex]) then
264 print("Dropping - " .. item["name"])
265 turtle.select(slot)
266 turtle.dropDown()
267 end
268 end
269 end
270 end
271end
272
273
274function manageInventory()
275 dropItems()
276
277
278 turtle.select(getItemIndex("enderstorage:ender_storage"))
279 turtle.digUp()
280 turtle.placeUp()
281
282 -- Chest is now deployed
283 for slot = 1, SLOT_COUNT, 1 do
284 local item = turtle.getItemDetail(slot)
285 if(item ~= nil) then
286 if(item["name"] ~= "minecraft:coal_block" and item["name"] ~= "minecraft:coal") then
287 turtle.select(slot)
288 turtle.dropUp()
289 end
290 end
291 end
292 -- Items are now stored
293
294 turtle.digUp()
295end
296
297function detectAndDig()
298 while(turtle.detect()) do
299 turtle.dig()
300 end
301end
302
303function forward()
304 detectAndDig()
305 turtle.forward()
306end
307
308function leftTurn()
309 turtle.turnLeft()
310 detectAndDig()
311 turtle.forward()
312 turtle.turnLeft()
313end
314
315
316function rightTurn()
317 turtle.turnRight()
318 detectAndDig()
319 turtle.forward()
320 turtle.turnRight()
321end
322
323
324function dropTier(heading)
325 turtle.turnRight()
326 turtle.turnRight()
327 turtle.digDown()
328 turtle.down()
329 return flipDirection(heading)
330end
331
332
333function flipDirection(heading)
334 return ((heading + 1) % 4) + 1
335end
336
337function turnAround(tier, heading)
338 if(tier % 2 == 1) then
339 if(heading == 2 or heading == 3) then
340 rightTurn()
341 elseif(heading == 1 or heading == 4) then
342 leftTurn()
343 end
344 else
345 if(heading == 2 or heading == 3) then
346 leftTurn()
347 elseif(heading == 1 or heading == 4) then
348 rightTurn()
349 end
350 end
351
352 return flipDirection(heading)
353end
354
355
356
357function startQuary(width, height, depth, heading)
358
359 for tier = 1, height, 1 do
360 for col = 1, width, 1 do
361 for row = 1, depth - 1, 1 do
362 if(not checkFuel()) then
363 print("Turtle is out of fuel, Powering Down...")
364 return
365 end
366 forward()
367 end
368 if(col ~= width) then
369 heading = turnAround(tier, heading)
370 end
371 manageInventory()
372 end
373 if(tier ~= height) then
374 heading = dropTier(heading)
375 end
376 end
377
378 return heading
379end
380
381
382local quary = data[2]
383finishedHeading = startQuary(quary.x, quary.y, quary.z, finalHeading)
384
385
386
387--------------------------------START RETURN TRIP CODE------------------------------------
388
389
390
391
392
393------------------------------------------------------------------------------------------
394
395
396function 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 print(string.format("Distances from end: %d %d %d", xDiff, yDiff, zDiff))
400
401 -- Move to Y start
402 if(yDiff < 0) then
403 digAndMoveDown(math.abs(yDiff))
404 elseif(yDiff > 0) then
405 digAndMoveUp(math.abs(yDiff))
406 end
407
408 -- Move to X start
409 heading = setHeadingX(xDiff, heading)
410 digAndMove(math.abs(xDiff))
411
412 -- Move to Z start
413 heading = setHeadingZ(zDiff, heading)
414 digAndMove(math.abs(zDiff))
415
416
417
418 return heading
419end
420
421endCoords = data[3]
422returnTo(endCoords ,finishedHeading)
423
424local timoutWait = 60
425for i = 1, timoutWait, 1 do
426 os.sleep(1)
427 print(string.format( "Waiting for brothers %d/%d", i, timoutWait))
428end
429
430modem.transmit(SERVER_PORT, CLIENT_PORT, "cum")