client.lua

437 lines · 9.8 KB

Open raw

CLIENT--

Copy & run

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