client-dig.lua

412 lines · 9.3 KB

Open raw

CLIENT DIG--

Copy & run

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