-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquarry.lua
More file actions
609 lines (529 loc) · 15.5 KB
/
Copy pathquarry.lua
File metadata and controls
609 lines (529 loc) · 15.5 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
-- Turtle Quarry by Jabulba
-- Global Variables:
-- integer width
-- integer length
-- integer height
-- string[] ignoredBlocks
-- string[] enderChests
-- integer enderChestSlot
-- boolean hasEnderChest
os.loadAPI("log.lua")
os.loadAPI("fuel.lua")
boolToInt = { [true] = 1, [false] = 0 }
directionMapping = {
["up"] = { ["detect"] = turtle.detectUp, ["dig"] = turtle.digUp, ["place"] = turtle.placeUp, ["inspect"] = turtle.inspectUp, ["drop"] = turtle.dropUp, ["suck"] = turtle.suckUp },
["down"] = { ["detect"] = turtle.detectDown, ["dig"] = turtle.digDown, ["place"] = turtle.placeDown, ["inspect"] = turtle.inspectDown, ["drop"] = turtle.dropDown, ["suck"] = turtle.suckDown },
["forward"] = { ["detect"] = turtle.detect, ["dig"] = turtle.dig, ["place"] = turtle.place, ["inspect"] = turtle.inspect, ["drop"] = turtle.drop, ["suck"] = turtle.suck }
}
local function emptyInventory(directionFunc)
directionFunc = directionFunc or directionMapping["forward"]
if hasEnderChest then
log.trace("Using Ender Chest to clear inventory")
while directionFunc["detect"]() do
local success, err = directionFunc["dig"]()
if not success and err == "Unbreakable block detected" then
-- TODO: backtrack?
log.error("Failed to place Ender Chest: " .. err)
error(err)
end
os.sleep(0.2)
end
turtle.select(enderChestSlot)
while not directionFunc["place"]() do
log.trace("Failed to place Ender Chest, trying again...")
turtle.attackUp()
turtle.attack()
turtle.attackDown()
os.sleep(0.5)
end
do
log.trace("Verifying Ender Chest placement...")
local success, blockData = directionFunc["inspect"]()
if success then
if blockData and enderChests[blockData.name] then
log.trace("Ender Chest placement confirmed!")
else
log.error("Ender Chest placed but '" .. blockData.name .. "' found instead")
checkForEnderChest()
return
end
else
log.error("Error: EnderChest placed but no blocks is there!")
checkForEnderChest()
return
end
end
for slot = 1, 16, 1 do
local itemData = turtle.getItemDetail(slot)
if itemData ~= nil then
if slot == fuel.getBucketSlot() then
log.trace("Skipping bucket slot " .. slot)
else
log.trace("Clearing " .. itemData.name .. " from slot " .. slot)
turtle.select(slot)
directionFunc["drop"]()
end
else
log.trace("Skipping empty slot " .. slot)
end
end
log.trace("Recovering Ender Chest")
turtle.select(enderChestSlot)
repeat
local success, err = directionFunc["dig"]()
if not success then
if err == "Nothing to dig here" then
log.error("Failed to pickup EnderChest! Has it been stolen?")
checkForEnderChest()
else
log.trace("Failed to pickup EnderChest, trying again!")
end
else
local itemData = turtle.getItemDetail(enderChestSlot)
if itemData and enderChests[itemData.name] then
log.trace("Ender Chest recovered")
else
log.error("Picked something other than the Ender Chest! Checking other slots...")
checkForEnderChest()
end
end
until success
else
log.error("The Ender Chest is gone!")
error("The Ender Chest is gone!")
end
end
local function checkInventory(directionFunc)
log.trace("Checking inventory space")
local fullSlots = 0
for i = 1, 16, 1 do
fullSlots = fullSlots + boolToInt[turtle.getItemCount(i) > 0]
end
log.trace("There are " .. 16 - fullSlots .. " free spaces")
if fullSlots > 14 then
emptyInventory(directionFunc)
end
end
function loadIgnoredBlocks()
ignoredBlocks = {}
log.info("Loading ignored blocks IDs")
local ignoredBlocksFileName = "quarry.ignoredblocks"
if not fs.exists(ignoredBlocksFileName) then
log.info("quarry.ignoredblocks not found, creating a new one with default IDs. You can add more IDs to this file, one ID per line!")
local ignoredBlocksFile = fs.open(ignoredBlocksFileName, "w")
ignoredBlocksFile.writeLine("minecraft:air")
ignoredBlocksFile.writeLine("minecraft:andesite")
ignoredBlocksFile.writeLine("minecraft:bedrock")
ignoredBlocksFile.writeLine("minecraft:blackstone")
ignoredBlocksFile.writeLine("minecraft:cobblestone")
ignoredBlocksFile.writeLine("minecraft:diorite")
ignoredBlocksFile.writeLine("minecraft:dirt")
ignoredBlocksFile.writeLine("minecraft:coarse_dirt")
ignoredBlocksFile.writeLine("minecraft:granite")
ignoredBlocksFile.writeLine("minecraft:grass")
ignoredBlocksFile.writeLine("minecraft:grass_block")
ignoredBlocksFile.writeLine("minecraft:grass_path")
ignoredBlocksFile.writeLine("minecraft:gravel")
ignoredBlocksFile.writeLine("minecraft:ice")
ignoredBlocksFile.writeLine("minecraft:packed_ice")
ignoredBlocksFile.writeLine("minecraft:blue_ice")
ignoredBlocksFile.writeLine("minecraft:ladder")
ignoredBlocksFile.writeLine("minecraft:netherrack")
ignoredBlocksFile.writeLine("minecraft:polished_blackstone_bricks")
ignoredBlocksFile.writeLine("minecraft:sand")
ignoredBlocksFile.writeLine("minecraft:sandstone")
ignoredBlocksFile.writeLine("minecraft:seagrass")
ignoredBlocksFile.writeLine("minecraft:snow")
ignoredBlocksFile.writeLine("minecraft:snow_layer")
ignoredBlocksFile.writeLine("minecraft:stone")
ignoredBlocksFile.writeLine("minecraft:tall_grass")
ignoredBlocksFile.writeLine("minecraft:torch")
ignoredBlocksFile.writeLine("betterend:bluestone")
ignoredBlocksFile.writeLine("blockus:bluestone")
ignoredBlocksFile.writeLine("blockus:limestone")
ignoredBlocksFile.writeLine("blockus:marble")
ignoredBlocksFile.writeLine("byg:beach_grass")
ignoredBlocksFile.writeLine("byg:brimstone")
ignoredBlocksFile.writeLine("byg:ether_grass")
ignoredBlocksFile.writeLine("byg:nyliumd_soul_soil")
ignoredBlocksFile.writeLine("byg:meadow_dirt")
ignoredBlocksFile.writeLine("byg:meadow_grass_block")
ignoredBlocksFile.writeLine("byg:prairie_grass")
ignoredBlocksFile.writeLine("byg:rocky_stone")
ignoredBlocksFile.writeLine("byg:scorched_grass")
ignoredBlocksFile.writeLine("byg:short_beach_grass")
ignoredBlocksFile.writeLine("byg:short_grass")
ignoredBlocksFile.writeLine("byg:tall_prairie_grass")
ignoredBlocksFile.writeLine("byg:weed_grass")
ignoredBlocksFile.writeLine("byg:whaling_grass")
ignoredBlocksFile.writeLine("byg:wilted_grass")
ignoredBlocksFile.writeLine("byg:winter_grass")
ignoredBlocksFile.writeLine("wild_explorer:blunite")
ignoredBlocksFile.writeLine("wild_explorer:carbonite")
ignoredBlocksFile.writeLine("terrestria:andisol_grass_path")
ignoredBlocksFile.writeLine("terrestria:basalt")
ignoredBlocksFile.writeLine("terrestria:basalt_cobblestone")
ignoredBlocksFile.writeLine("terrestria:basalt_dirt")
ignoredBlocksFile.writeLine("terrestria:basalt_grass_block")
ignoredBlocksFile.writeLine("terrestria:basalt_podzol")
ignoredBlocksFile.writeLine("terrestria:basalt_sand")
ignoredBlocksFile.writeLine("terrestria:dead_grass")
ignoredBlocksFile:close()
end
for line in io.lines(ignoredBlocksFileName) do
ignoredBlocks[line] = true
log.trace("Loaded ignored block with ID: " .. line)
end
log.info("Finished loading ignored blocks IDs")
return ignoredBlocks
end
function loadEnderChestsList()
log.info("Loading Ender Chest IDs")
enderChests = {}
if not fs.exists("quarry.enderchests") then
log.info("quarry.enderchests not found, creating a new one with default IDs. You can add more IDs to this file, one ID per line!")
local enderChestsFile = fs.open("quarry.enderchests", "w")
enderChestsFile.writeLine("kibe:entangled_chest")
enderChestsFile.writeLine("enderstorage:ender_storage")
enderChestsFile:close()
end
for line in io.lines("quarry.enderchests") do
enderChests[line] = true
log.trace("Loaded compatible Ender Chest with ID: " .. line)
end
log.info("Finished loading Ender Chest IDs")
return enderChests
end
function loadEnderTanksList()
local enderChests = {}
if not fs.exists("quarry.tanks") then
local enderChestsFile = fs.open("quarry.tanks", "w")
enderChestsFile.writeLine("kibe:entangled_tank")
enderChestsFile.writeLine("enderstorage:ender_tank")
enderChestsFile:close()
end
for line in io.lines("quarry.tanks") do
enderChests[line] = true
end
return enderChests
end
function requestValidInput(val, name)
val = tonumber(val)
while val == nil or not 'number' == type(val) or not math.floor(val) == val or val <= 0 do
io.write(name .. ": ")
val = tonumber(io.read())
end
return val
end
function checkForEnderChest()
hasEnderChest = false
local enderChestList = loadEnderChestsList()
log.info("Searching inventory for compatible Ender Chests")
for slot = 1, 16, 1 do
log.trace("Searching slot " .. slot)
local itemData = turtle.getItemDetail(slot)
if itemData then
log.trace("Slot " .. slot .. " has item: " .. itemData.name)
if enderChestList[itemData.name] then
log.info("Ender Chest found in slot " .. slot)
enderChestSlot = slot
hasEnderChest = true
break
end
end
end
log.trace("Finished searching for Ender Chest")
if not hasEnderChest then
log.error("No Ender Chest found")
error("No EnderChest found.")
end
return true
end
function loadPerimeter(args)
if args[1] == nil or args[2] == nil then
io.write("Select the area to mine, all values must be positive integers:\n\n")
end
width = nil
length = nil
height = nil
curWidth = 1
curLength = 1
curHeight = 1
action = "idle"
lengthInverted = false
WidthInverted = false
local configExists = fs.exists("quarry.run")
if configExists then
fs.delete("quarry.run")
configExists = false
end
if configExists then
local configFile = fs.open("quarry.run", "w")
width = configFile.readLine()
length = configFile.readLine()
height = configFile.readLine()
curWidth = configFile.readLine()
curLength = configFile.readLine()
curHeight = configFile.readLine()
action = configFile.readLine()
lengthInverted = configFile.readLine()
WidthInverted = configFile.readLine()
configFile.close()
else
local configFile = fs.open("quarry.run", "w")
width = requestValidInput(args[1], "Width")
length = requestValidInput(args[2], "Length")
height = 256 -- TODO
configFile.write(getRunData())
configFile.close()
end
end
function getRunData()
return width .. "\n" .. length .. "\n" .. height .. "\n" .. curWidth .. "\n" .. curLength .. "\n" .. curHeight .. "\n" .. action
end
function updateRunData()
local configFile = fs.open("quarry.run", "w")
configFile.write(getRunData())
configFile.close()
end
function moveDown()
if curHeight > height then
return
end
dig(directionMapping["down"], true)
action = "moveDown"
updateRunData()
local inspected, blockData = directionMapping["down"]["inspect"]()
if inspected and blockData and blockData.name == "minecraft:bedrock" then
height = curHeight
else
while not turtle.down() do
dig(directionMapping["down"], true)
turtle.attackDown()
end
curHeight = curHeight + 1
end
action = "idle"
updateRunData()
if curHeight > 2 and curHeight < 6 then
local placeBlock = false
local placeSlot
for i = 1, 16, 1 do
local slotData = turtle.getItemDetail(i)
if slotData and ignoredBlocks[slotData.name] then
placeBlock = true
placeSlot = i
break
end
end
if placeBlock then
turtle.select(placeSlot)
turtle.placeUp()
end
end
end
function moveForward(increaseWidth)
fuel.bucketRefuel(directionMapping["forward"])
action = "moveForward"
updateRunData()
while not turtle.forward() do
dig(directionMapping["forward"], true)
turtle.attack()
end
action = "idle"
if increaseWidth then
curLength = curLength + 1 -- TODO: Add Direction for positive/negative
end
updateRunData()
return success
end
function moveUp()
if curHeight <= 1 then
return
end
dig(directionMapping["up"], true)
action = "moveUp"
updateRunData()
while not turtle.up() do
dig(directionMapping["up"], true)
turtle.attackUp()
end
action = "idle"
curHeight = curHeight - 1
updateRunData()
if curHeight < 5 then
local placeBlock = false
local placeSlot
for i = 1, 16, 1 do
local slotData = turtle.getItemDetail(i)
if slotData and ignoredBlocks[slotData.name] then
placeBlock = true
placeSlot = i
break
end
end
if placeBlock then
turtle.select(placeSlot)
turtle.placeDown()
end
end
end
function turnLeft()
action = "turnLeft"
updateRunData()
local success = turtle.turnLeft()
-- TODO check turn
action = "idle"
--TODO: Update Direction
updateRunData()
return success
end
function turnRight()
action = "turnRight"
updateRunData()
local success = turtle.turnRight()
-- TODO check turn
action = "idle"
--TODO: Update Direction
updateRunData()
return success
end
function widthTurn()
if lengthInverted then
turnLeft()
else
turnRight()
end
end
function shouldMineBlock(directionFunc)
local success, blockData = directionFunc["inspect"]()
if success and blockData and ignoredBlocks[blockData.name] then
return false
end
return true
end
function checkChest(directionFunc)
local success, blockData = directionFunc["inspect"]()
if success and blockData and string.match(blockData.name, "chest") or blockData.name == "minecraft:barrel" then
while directionFunc["suck"]() do
checkInventory(directionFunc["up"])
end
end
end
function checkTurtle(directionFunc)
local unlocked = false
repeat
local inspected, blockData = directionFunc["inspect"]()
if inspected and blockData and string.match(blockData.name, "turtle") then
os.sleep(1)
else
unlocked = true
end
until unlocked
end
function dig(directionFunc, force)
if force or shouldMineBlock(directionFunc) then
checkTurtle(directionFunc)
checkChest(directionFunc)
fuel.bucketRefuel(directionFunc)
directionFunc["dig"]()
checkInventory()
end
end
function excavate(forceUp, forceForward, forceDown)
dig(directionMapping["forward"], forceForward)
dig(directionMapping["up"], forceUp)
dig(directionMapping["down"], forceDown)
end
function quarry()
term.clear()
while curHeight ~= height do
moveDown()
end
moveUp()
repeat
if curHeight >= height then
return
end
while curWidth <= width do
while curLength <= length - 1 do
excavate(false, true, false)
moveForward(true)
end
curLength = 1
updateRunData()
if curWidth < width then
widthTurn()
excavate(false, true, false)
moveForward(false)
widthTurn()
end
lengthInverted = not lengthInverted
curWidth = curWidth + 1
updateRunData()
end
dig(directionMapping["down"], false)
widthTurn()
curWidth = 1
widthInverted = not widthInverted
updateRunData()
moveUp()
moveUp()
moveUp()
until curHeight <= 1
end
function returnToStart()
if lengthInverted then
for i = curLength, length - 1, 1 do
moveForward()
end
end
if widthInverted then
if lengthInverted then
widthTurn()
end
for i = curWidth, width - 1, 1 do
moveForward()
end
end
local success, _ = directionMapping["down"]["inspect"]()
if not success then
local placeBlock = false
local placeSlot
for i = 1, 16, 1 do
local slotData = turtle.getItemDetail(i)
if slotData and ignoredBlocks[slotData.name] then
placeBlock = true
placeSlot = i
break
end
end
if placeBlock then
turtle.select(placeSlot)
turtle.placeDown()
end
end
end
function startup()
log.trace("Startup with args: " .. table.concat(args, '; '))
checkForEnderChest()
fuel.checkForBucket()
if args[3] then
ignoredBlocks = {}
else
loadIgnoredBlocks()
end
loadPerimeter(args)
local fuelEstimate = width * length * 60
if not hasEnderChest then
-- Some fuel will be used when returning itens to the surface!
fuelEstimate = math.floor(fuelEstimate * 1.1)
end
fuel.refuelWizard(fuelEstimate)
quarry()
returnToStart()
emptyInventory(directionMapping["up"])
end
log.setLevel(log.LogLevel.INFO)
args = { ... }
startup()