clientdig.lua

440 lines · 9.9 KB

Open raw

CLIENT DIG--

Copy & run

wget https://perlytiara.github.io/turtles.tips/raw/programs/Maengorn/clientdig.lua
1--CLIENT DIG--
2
3local SLOT_COUNT = 16
4
5local CLIENT_PORT = 0
6local SERVER_PORT = 420
7
8local modem = peripheral.wrap("right")
9modem.open(CLIENT_PORT)
10
11function 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
20end
21
22function 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)
31end
32
33
34function 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()) then
42 return true
43 end
44 end
45
46 return false
47 else
48 return true
49 end
50end
51
52
53function 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
72function 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
82end
83
84function 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
94end
95
96function 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
106end
107
108function 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
116end
117
118function 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
127end
128
129function 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
137end
138
139
140function 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
168end
169
170
171function 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 elseif(fuelType == "minecraft:charcoal") then
186 totalFuel = totalFuel / 80
187 else
188 print("INVALID FUEL SOURCE")
189 os.exit(1)
190 end
191
192 return math.floor(totalFuel) + 5
193end
194
195
196
197modem.transmit(SERVER_PORT, CLIENT_PORT, "CLIENT_DEPLOYED")
198event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
199data = parseParams(msg)
200
201-- Pick up coal and refuel
202local fuelNeeded = calculateFuel(data[1], data[2], "minecraft:coal")
203turtle.suckDown(fuelNeeded)
204checkFuel()
205
206print(string.format( "Extracting %d fuel...", fuelNeeded))
207
208-- Grab Ender Chest
209turtle.turnRight(1)
210turtle.suck(1)
211turtle.turnLeft(1)
212
213local startCoords = data[1]
214local finalHeading = moveTo(startCoords, getOrientation())
215
216local NORTH_HEADING = 2
217--Turn to face North
218turnToFaceHeading(finalHeading, NORTH_HEADING)
219finalHeading = NORTH_HEADING
220--Now in Starting Position--
221
222--------------------------------START MINING CODE-----------------------------------------
223
224
225
226
227
228------------------------------------------------------------------------------------------
229
230DROPPED_ITEMS = {
231 "minecraft:stone",
232 "minecraft:dirt",
233 "minecraft:basalt",
234 "minecraft:granite",
235 "minecraft:cobblestone",
236 "minecraft:sand",
237 "minecraft:gravel",
238 "minecraft:redstone",
239 "minecraft:flint",
240 "railcraft:ore_metal",
241 "extrautils2:ingredients",
242 "minecraft:dye",
243 "thaumcraft:nugget",
244 "thaumcraft:crystal_essence",
245 "thermalfoundation:material",
246 "projectred-core:resource_item",
247 "thaumcraft:ore_cinnabar",
248 "deepresonance:resonating_ore",
249 "forestry:apatite",
250 "biomesoplenty:loamy_dirt",
251 "chisel:marble",
252 "chisel:limestone",
253}
254function dropItems()
255 print("Purging Inventory...")
256 for slot = 1, SLOT_COUNT, 1 do
257 local item = turtle.getItemDetail(slot)
258 if(item ~= nil) then
259 for filterIndex = 1, #DROPPED_ITEMS, 1 do
260 if(item["name"] == DROPPED_ITEMS[filterIndex]) then
261 print("Dropping - " .. item["name"])
262 turtle.select(slot)
263 turtle.dropDown()
264 end
265 end
266 end
267 end
268end
269
270
271function getEnderIndex()
272 for slot = 1, SLOT_COUNT, 1 do
273 local item = turtle.getItemDetail(slot)
274 if(item ~= nil) then
275 if(item["name"] == "enderstorage:ender_storage") then
276 return slot
277 end
278 end
279 end
280 return nil
281end
282
283function manageInventory()
284 dropItems()
285 index = getEnderIndex()
286 if(index ~= nil) then
287 turtle.select(index)
288 turtle.digUp()
289 turtle.placeUp()
290 end
291 -- Chest is now deployed
292 for slot = 1, SLOT_COUNT, 1 do
293 local item = turtle.getItemDetail(slot)
294 if(item ~= nil) then
295 if(item["name"] ~= "minecraft:coal_block" and item["name"] ~= "minecraft:coal" and item["name"] ~= "minecraft:charcoal") then
296 turtle.select(slot)
297 turtle.dropUp()
298 end
299 end
300 end
301 -- Items are now stored
302
303 turtle.digUp()
304end
305
306
307function detectAndDig()
308 while(turtle.detect()) do
309 turtle.dig()
310 end
311end
312
313function forward()
314 detectAndDig()
315 turtle.forward()
316end
317
318function leftTurn()
319 turtle.turnLeft()
320 detectAndDig()
321 turtle.forward()
322 turtle.turnLeft()
323end
324
325
326function rightTurn()
327 turtle.turnRight()
328 detectAndDig()
329 turtle.forward()
330 turtle.turnRight()
331end
332
333
334function dropTier(heading)
335 turtle.turnRight()
336 turtle.turnRight()
337 turtle.digDown()
338 turtle.down()
339 return flipDirection(heading)
340end
341
342
343function flipDirection(heading)
344 return ((heading + 1) % 4) + 1
345end
346
347function turnAround(tier, heading)
348 if(tier % 2 == 1) then
349 if(heading == 2 or heading == 3) then
350 rightTurn()
351 elseif(heading == 1 or heading == 4) then
352 leftTurn()
353 end
354 else
355 if(heading == 2 or heading == 3) then
356 leftTurn()
357 elseif(heading == 1 or heading == 4) then
358 rightTurn()
359 end
360 end
361
362 return flipDirection(heading)
363end
364
365
366
367function startQuary(width, height, depth, heading)
368
369 for tier = 1, height, 1 do
370 for col = 1, width, 1 do
371 for row = 1, depth - 1, 1 do
372 if(not checkFuel()) then
373 print("Turtle is out of fuel, Powering Down...")
374 return
375 end
376 forward()
377 end
378 if(col ~= width) then
379 heading = turnAround(tier, heading)
380 end
381 manageInventory()
382 end
383 if(tier ~= height) then
384 heading = dropTier(heading)
385 end
386 end
387
388 return heading
389end
390
391
392local quary = data[2]
393finishedHeading = startQuary(quary.x, quary.y, quary.z, finalHeading)
394
395
396
397--------------------------------START RETURN TRIP CODE------------------------------------
398
399
400
401
402
403------------------------------------------------------------------------------------------
404
405
406function returnTo(coords, heading)
407 local currX, currY, currZ = gps.locate()
408 local xDiff, yDiff, zDiff = coords.x - currX, coords.y - currY, coords.z - currZ
409 print(string.format("Distances from end: %d %d %d", xDiff, yDiff, zDiff))
410
411 -- Move to Y start
412 if(yDiff < 0) then
413 digAndMoveDown(math.abs(yDiff))
414 elseif(yDiff > 0) then
415 digAndMoveUp(math.abs(yDiff))
416 end
417
418 -- Move to X start
419 heading = setHeadingX(xDiff, heading)
420 digAndMove(math.abs(xDiff))
421
422 -- Move to Z start
423 heading = setHeadingZ(zDiff, heading)
424 digAndMove(math.abs(zDiff))
425
426
427
428 return heading
429end
430
431endCoords = data[3]
432returnTo(endCoords ,finishedHeading)
433
434local timoutWait = 90
435for i = 1, timoutWait, 1 do
436 os.sleep(1)
437 print(string.format( "Waiting for brothers %d/%d", i, timoutWait))
438end
439
440modem.transmit(SERVER_PORT, CLIENT_PORT, "cum")