-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquarry.lua
More file actions
417 lines (364 loc) · 9.14 KB
/
Copy pathquarry.lua
File metadata and controls
417 lines (364 loc) · 9.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
os.loadAPI("flex.lua")
os.loadAPI("dig.lua")
local fuelValues = {
["minecraft:coal"] = 80,
["minecraft:charcoal"] = 80,
["minecraft:blaze_rod"] = 120,
["minecraft:lava_bucket"] = 1000,
}
local function inventoryFuelUnits()
local total = 0
local slot = turtle.getSelectedSlot()
for i = 1, 16 do
turtle.select(i)
local detail = turtle.getItemDetail()
if detail and detail.name then
local per = fuelValues[detail.name]
if per then
total = total + per * detail.count
end
end
end
turtle.select(slot)
return total
end
local function totalAvailableFuel()
return turtle.getFuelLevel() + inventoryFuelUnits()
end
-- argument parsing
local args = {...}
if #args == 0 then
flex.send("Usage: quarry <x> [z] [depth] [skip]", colors.lightBlue)
return
end
local xmax = tonumber(args[1])
local zmax = tonumber(args[2]) or xmax
local depth = tonumber(args[3]) or math.huge
local skip = nil
if args[4] and args[4] ~= "" then
skip = tonumber(args[4])
if not skip then
flex.send("Warning: skip value invalid, ignoring.", colors.yellow)
skip = nil
end
end
if not xmax or not zmax then
flex.send("Invalid dimensions.", colors.red)
return
end
if fs.exists("startup.lua") and
fs.exists("dig_save.txt") then
dig.loadCoords()
end --if
dig.makeStartup("quarry",args)
-- networking setup
local modemSide
for _, side in ipairs({"top", "bottom", "left", "right", "front", "back"}) do
if peripheral.getType(side) == "modem" then
modemSide = side
break
end
end
if modemSide then
rednet.open(modemSide)
end
local globalCommand = ""
local function pollGlobal()
local senderId, message = rednet.receive(0)
if message then
message = tostring(message):upper():gsub("%s+", "")
if message == "RETURN" then
globalCommand = "RETURN"
end
end
return globalCommand
end
-- crash-safe run flag
local runFlag = "quarry_running.flag"
if fs.exists(runFlag) then
globalCommand = "RETURN"
else
local f = fs.open(runFlag, "w")
f.write("running")
f.close()
end
local function dropNotFuel()
flex.condense()
local a,x
a = false
for x=1,16 do
turtle.select(x)
if turtle.refuel(0) then
if a then turtle.drop() end
a = true
else
turtle.drop()
end --if
end --for
turtle.select(1)
end --function
local function fillFuelStack()
-- try existing fuel slot first
for i=1,16 do
turtle.select(i)
if turtle.getItemCount(i) > 0 and turtle.refuel(0) then
-- top up if possible but don’t block on full stack
if turtle.getItemCount(i) < 64 then
turtle.suck(64 - turtle.getItemCount(i))
end
turtle.select(1)
return true
end
end
-- no existing fuel: grab any available fuel from chest into empty slot
for i=1,16 do
turtle.select(i)
if turtle.getItemCount(i) == 0 then
turtle.suck(64) -- will pull up to available amount
if turtle.getItemCount(i) > 0 and turtle.refuel(0) then
turtle.select(1)
return true
else
if turtle.getItemCount(i) > 0 then turtle.drop() end
end
end
end
turtle.select(1)
return false
end
local xdir, zdir = 1, 1
dig.gotox(0)
if dig.isStuck() and not recoverStuck() then return end
dig.gotoz(0)
if dig.isStuck() and not recoverStuck() then return end
dig.gotoy(dig.getYmin())
if dig.isStuck() and not recoverStuck() then return end
-- remember home location to return for unloading
local home = {
x = dig.getx(),
y = dig.gety(),
z = dig.getz(),
heading = 180,
}
local targetY = home.y - depth
local coalFuelValue = 80
local lowFuelThreshold = 3 * coalFuelValue
local function topUpInternalFuel()
while turtle.getFuelLevel() < lowFuelThreshold and turtle.refuel(1) do
end
end
local function refuelFromChest()
-- try refueling from inventory first
for i = 1, 16 do
turtle.select(i)
if turtle.refuel(1) then
topUpInternalFuel()
turtle.select(1)
return true
end
end
-- attempt to grab a full or partial stack into an empty slot and consume one
for i = 1, 16 do
turtle.select(i)
if turtle.getItemCount(i) == 0 then
if turtle.suck(64) then
if turtle.refuel(1) then
topUpInternalFuel()
turtle.select(1)
return true
else
turtle.drop()
end
end
end
end
-- last resort: single item from chest
turtle.select(1)
if turtle.suck(1) and turtle.refuel(1) then
topUpInternalFuel()
return true
end
turtle.select(1)
return false
end
local function recoverStuck()
flex.send(string.format("Stuck at %d,%d,%d; attempting recovery", dig.getx(), dig.gety(), dig.getz()), colors.red)
local heading = dig.getr()
local attempts = 0
while dig.isStuck() and attempts < 3 do
dig.dig()
dig.gotor(heading + 180)
dig.fwd()
dig.gotor(heading)
dig.fwd()
attempts = attempts + 1
end
if dig.isStuck() then
flex.send(string.format("Unrecoverable stuck at %d,%d,%d after %d attempts", dig.getx(), dig.gety(), dig.getz(), attempts), colors.red)
return false
end
return true
end
local function isContainer(data)
return data and data.name and (data.name:find("chest") or data.name:find("barrel"))
end
local function isNextToChest()
local function check(inspectFunc)
local ok, data = inspectFunc()
return ok and isContainer(data)
end
if check(turtle.inspect) then return true end
if check(turtle.inspectUp) then return true end
if check(turtle.inspectDown) then return true end
turtle.turnLeft()
if check(turtle.inspect) then turtle.turnRight(); return true end
turtle.turnRight()
if check(turtle.inspect) then turtle.turnLeft(); return true end
turtle.turnLeft()
return false
end
local function waitForChest()
local err = "ERROR: No chest detected at home. Staying put."
if flex and colors then
flex.send(err, colors.red)
else
print(err)
end
while not isNextToChest() do
sleep(5)
end
end
local function returnToBase(returnAfter)
local loc
if returnAfter ~= false then
loc = dig.location()
end
dig.goto(home.x, home.y, home.z, home.heading)
if dig.isStuck() and not recoverStuck() then return false end
if not isNextToChest() then
waitForChest()
end
dropNotFuel()
local timeout = 0
local max_timeout = 10 -- seconds
while totalAvailableFuel() < lowFuelThreshold and timeout < max_timeout do
if not refuelFromChest() then
sleep(1)
end
timeout = timeout + 1
end
timeout = 0
while not fillFuelStack() and timeout < max_timeout do
sleep(1)
timeout = timeout + 1
end
topUpInternalFuel()
if returnAfter ~= false then
dig.goto(loc)
if dig.isStuck() and not recoverStuck() then return false end
end
turtle.select(1)
globalCommand = ""
return true
end
if globalCommand == "RETURN" then
returnToBase()
end
-- descend initial levels if skip provided
if skip and skip > 0 then
for i = 1, skip do
if dig.gety() <= targetY then break end
dig.down()
if dig.isStuck() and not recoverStuck() then return end
end
end
local done = false
while not done do
pollGlobal()
if globalCommand == "RETURN" then
returnToBase()
end
local internal = turtle.getFuelLevel()
local inventory = inventoryFuelUnits()
local total = internal + inventory
flex.send(string.format("Fuel: internal=%d inventory=%d total=%d", internal, inventory, total), colors.gray)
if total < lowFuelThreshold then
flex.send("Fuel low; returning to base", colors.yellow)
returnToBase()
end
turtle.select(1)
if zdir == 1 then
dig.gotor(0)
while dig.getz() < zmax-1 do
dig.fwd()
if dig.isStuck() then
if not recoverStuck() then return end
end --if
end --while
elseif zdir == -1 then
dig.gotor(180)
while dig.getz() > 0 do
dig.fwd()
if dig.isStuck() then
if not recoverStuck() then return end
end
end --while
end --if/else
if done then break end
zdir = -zdir
if dig.getx() == 0 and xdir == -1 then
if dig.gety() - 1 < targetY then
done = true
else
dig.down()
if dig.isStuck() and not recoverStuck() then return end
xdir = 1
end
elseif dig.getx() == xmax-1 and xdir == 1 then
if dig.gety() - 1 < targetY then
done = true
else
dig.down()
if dig.isStuck() and not recoverStuck() then return end
xdir = -1
end
else
dig.gotox(dig.getx() + xdir)
if dig.isStuck() and not recoverStuck() then return end
end --if/else
if turtle.getItemCount(15) > 0 then
returnToBase()
end
end --while
dig.goto(home.x, home.y, home.z, home.heading)
if dig.isStuck() and not recoverStuck() then return end
if not isNextToChest() then
waitForChest()
end
for x=1,16 do
turtle.select(x)
turtle.drop()
end
refuelFromChest()
local timeout = 0
local max_timeout = 10 -- seconds
while totalAvailableFuel() < lowFuelThreshold and timeout < max_timeout do
if not refuelFromChest() then
sleep(1)
end
timeout = timeout + 1
end
timeout = 0
while not fillFuelStack() and timeout < max_timeout do
sleep(1)
timeout = timeout + 1
end
topUpInternalFuel()
turtle.select(1)
dig.gotor(0)
if fs.exists(runFlag) then fs.delete(runFlag) end
if fs.exists("startup.lua") then
shell.run("rm startup.lua")
end
os.unloadAPI("dig.lua")
os.unloadAPI("flex.lua")