-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrancher.lua
441 lines (379 loc) · 9.97 KB
/
Brancher.lua
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
-- This Version
-- 06/05/2020
-- DrJoachim
local version = '0.11'
-- Issues
-- If torches are not placable it still deducts them
-- ToDoList
-- Refactor code
-- Make sure turtle returns to start
-- Make turtle eddrop stuff in the main branch, not in side branches
-- Make turtle close off the branches
-- Add Code to place torch each time it starts
-- Add Fuel Code so can know almost how much fuel you need
-- Add second fuel slot if you go allout diggin
-- Mabye add code that make turtle make new line of tunnels
--Local
local curX = -1 -- you start before the main hall
local curZ = 0
local curY = 0
local debug = false
local branchDepth = 0 -- How deep Did User Pick
local branchNumber = 0 -- How many branches does the user want?
local maxX = 3*branchNumber
local maxZ = branchDepth+1
local torchCount = 0 -- Tracks the number of torches
local torchLocations = {}
local chestCount = 0 -- Tracks the number of chests
local chestLocations = {}
local fuelCount = 0 -- Tracks the number of fuelItems
local fuelLocations = {}
local MD = 3 -- How Many Blocks Apart From Each Mine
local emptyLocation = -1
local onlight = 0 -- When to Place Torch
local Fuel = 0 -- if 2 then it is unlimited no fuel needed
local NeedFuel = 0 -- If Fuel Need Then 1 if not Then 0
local Error = 0 -- 0 = No Error and 1 = Error
local Way = 0 -- 0 = Left and 1 = Right
local trash = {
["minecraft:cobblestone"] = true,
["minecraft:dirt"] = true,
["minecraft:gravel"] = true,
["minecraft:andesite"]=true,
["minecraft:diorite"]=true,
["minecraft:granite"]=true
}
local function initializeLocations()
for i=1,16 do
turtle.select(i)
local details = turtle.getItemDetail(i);
if details ~= nil then
if string.find(details.name,"torch") then
torchCount = torchCount + turtle.getItemCount(i)
table.insert(torchLocations,i)
elseif string.find(details.name,"chest") then
chestCount = chestCount + turtle.getItemCount(i)
table.insert(chestLocations,i)
elseif turtle.refuel(0) then
fuelCount = fuelCount + turtle.getItemCount(i)
table.insert(fuelLocations,i)
end
end
end
turtle.select(1)
logger.log("**Locations**")
logger.log("Found "..torchCount.." torches on these locations: "..table.concat(torchLocations,", "))
logger.log("Found "..chestCount.." chests on these locations: "..table.concat(chestLocations,", "))
logger.log("Found "..fuelCount.." fuel items on these locations: "..table.concat(fuelLocations,", "))
logger.log("*************")
end
--Checking
local function checkFuel()
local branchSteps = branchDepth * 2 * branchNumber
local mainSteps = 9 * branchNumber
local totalSteps = branchSteps + mainSteps
logger.log("totalsteps = "..totalSteps)
if turtle.getFuelLevel() == "unlimited" then
logger.log("No need for fuel")
elseif turtle.getFuelLevel() >= totalSteps then
logger.log("fuel level is now "..turtle.getFuelLevel())
logger.log("There should be enough fuel to mine the whole branch")
elseif turtle.getFuelLevel() < totalSteps then
logger.log("No fuel enough, refueling")
local x = 1
while turtle.getFuelLevel() < totalSteps or x<=#fuelLocations do
local v = fuelLocations[x]
logger.log("refueling with location "..x)
turtle.select(v)
turtle.refuel(turtle.getItemCount(v))
x = x + 1
logger.log("fuel level is now "..turtle.getFuelLevel())
end
end
end
local function getNextChest()
local chestLocId = 0
local i = 1
while chestLocId == 0 and i <= #chestLocations do
if turtle.getItemCount(chestLocations[i]) > 0 then
chestLocId = chestLocations[i]
end
i = i+1
end
logger.log("Chestlocation found at "..chestLocId)
return chestLocId
end
local function getNextTorch()
local torchLocId = 0
local i = 1
while torchLocId == 0 and i <= #torchLocations do
if turtle.getItemCount(torchLocations[i]) > 0 then
torchLocId = torchLocations[i]
end
i = i+1
end
logger.log("Torchlocation found at "..torchLocId)
return torchLocId
end
local function placeChestDown()
local tempY = curY
while curY > -1 do
robustTurtle.digDown()
robustTurtle.down()
curY = curY -1
end
robustTurtle.digDown()
turtle.select(getNextChest())
turtle.placeDown()
chestCount = chestCount - 1
if chestCount == 0 then
logger.log("Turtle ran out of chests")
else
logger.log("Chest placed, turtle has "..chestCount.." chests left.")
end
for i=1,16 do
if turtle.getItemCount(i)>0 then
turtle.select(i)
if not (string.find(turtle.getItemDetail(i).name,"chest") or string.find(turtle.getItemDetail(i).name,"torch") or turtle.refuel(0)) then
turtle.dropDown()
end
end
end
turtle.select(1)
while curY < tempY do
robustTurtle.up()
curY = curY + 1
end
end
function initializeAPIs()
--pastebin get J2bF3fpf logger
os.loadAPI("logger")
os.loadAPI("robustTurtle")
end
local function getNextEmptyLocation()
for i= 1,16 do
if turtle.getItemCount(i)==0 then
emptyLocation = i
end
end
if(emptyLocation > -1 and turtle.getItemCount(emptyLocation)>0) then
emptyLocation = -1
end
end
local function goHomeAndBack()
logger.log("Turtle is full, going back home to unload")
end
local function dropAllOresInChest()
end
local function dropTrash()
for i=1,16 do
if turtle.getItemCount(i) > 0 and trash[turtle.getItemDetail(i).name] then
logger.log("[INV] - "..turtle.getItemDetail(i).name.." marked as trash, dropping...")
turtle.select(i)
turtle.drop()
elseif turtle.getItemCount(i) > 0 then
logger.log("[INV] - "..turtle.getItemDetail(i).name.." not marked as trash, keeping it...")
end
end
end
local function checkInventory()
getNextEmptyLocation()
if emptyLocation == -1 then
logger.log("[INV] - Turtle is full, dropping trash")
dropTrash()
getNextEmptyLocation()
if emptyLocation == -1 then
logger.log("[INV] - Turtle is full of usefull things, putting ores in a chests")
placeChestDown()
end
if debug then io.read() end
else
--logger.log("Turtle is not full")
end
turtle.select(1)
end
local function placeTorch(direction)
logger.log("Placing torch at "..curX)
turtle.select(getNextTorch())
if direction == "down" then
turtle.placeDown()
elseif direction == "up" then
turtle.placeUp()
else
turtle.place()
end
torchCount = torchCount - 1
if torchCount == 0 then
logger.log("Turtle ran out of torches, will continue in the dark... (scary)")
else
logger.log("Torch placed, turtle has "..torchCount.." torches left.")
end
turtle.select(1)
end
local function mineFU()
robustTurtle.forward()
robustTurtle.dig()
checkInventory()
robustTurtle.digUp()
checkInventory()
if (curZ % 8 == 0 or curZ==maxZ-1) and torchCount > 0 then
placeTorch("up")
end
end
local function mineFLR(torch)
robustTurtle.forward()
robustTurtle.dig()
checkInventory()
turtle.turnLeft()
robustTurtle.dig()
if curX % 8 == 0 and torchCount > 0 and torch then
placeTorch()
end
checkInventory()
turtle.turnRight()
turtle.turnRight()
robustTurtle.dig()
if curX % 8 == 0 and torchCount > 0 and torch then
placeTorch()
end
checkInventory()
turtle.turnLeft()
end
local function createMainHall()
getNextEmptyLocation()
while curX < maxX do
mineFLR()
curX = curX+1
end
robustTurtle.digUp()
checkInventory()
robustTurtle.up()
curY=curY+1
turtle.turnLeft()
turtle.turnLeft()
while curX > 0 do
mineFLR(true)
curX=curX-1
end
robustTurtle.digDown()
robustTurtle.down()
checkInventory()
curY=curY-1
logger.log("[Hall] - Back home: current location: "..curX..","..curY)
turtle.turnLeft()
turtle.turnLeft()
if chestCount > 0 then
dropTrash()
placeChestDown()
end
end
local function digBranches()
turtle.turnLeft()
-- left branch
while curZ < maxZ do
mineFU()
curZ=curZ+1
end
turtle.turnLeft()
turtle.turnLeft()
while curZ > 0 do
robustTurtle.forward()
curZ=curZ-1
end
-- right branch
while curZ < maxZ do
mineFU()
curZ=curZ+1
end
turtle.turnLeft()
turtle.turnLeft()
while curZ > 0 do
robustTurtle.forward()
curZ=curZ-1
end
turtle.turnRight()
end
local function createBranches()
while curY > -1 do
robustTurtle.digDown()
checkInventory()
robustTurtle.down()
curY = curY -1
end
while curX < maxX do
mineFLR()
curX = curX+1
if curX % 3 == 0 then
digBranches()
end
end
end
local function returnHome()
turtle.turnLeft()
turtle.turnLeft()
while curX > 0 do
robustTurtle.forward()
curX = curX -1
end
while curY < 0 do
robustTurtle.up()
curY = curY +1
end
while curY > 0 do
robustTurtle.down()
curY = curY -1
end
logger.log("Back home: current location: "..curX..","..curY)
end
-- Start
print("Hi There Welcome to Mining Turtle Program v"..version)
initializeAPIs()
print("How deep should the branch mines be?")
input = io.read()
branchDepth = tonumber(input)
print("How many branches do you want?")
input = io.read()
branchNumber = tonumber(input)
print("Do you want to debug (y/n)?")
input = io.read()
while input ~= 'y' and input ~= 'n' do
print("Do you want to debug (y/n)?")
input = io.read()
end
if input=='y' then
debug = true
elseif input =='n' then
debug = false
else
logger.log("error","Faulty response: this can never happen")
io.read()
end
maxX = 3*branchNumber
maxZ = branchDepth+1
logger.log("[MAIN] - Max locations: "..maxX..","..maxZ)
logger.log("[MAIN] - Initializing Locations...")
initializeLocations()
logger.log("[MAIN] - Locations initailized, press any key to continue")
if debug then io.read() end
logger.log("[MAIN] - Checking fuel...")
checkFuel()
logger.log("[MAIN] - Checked fuel, press any key to continue")
if debug then io.read() end
logger.log("[MAIN] - Creating main hall... starting at location "..curX..","..curY)
createMainHall()
logger.log("[MAIN] - Main hall created, press any key to continue")
if debug then io.read() end
logger.log("[MAIN] - Creating branches...")
createBranches()
logger.log("[MAIN] - Branches created, press any key to continue")
if debug then io.read() end
logger.log("[MAIN] - Returning home...")
returnHome()
logger.log("[MAIN] - Returned home, press any key to continue")
if debug then io.read() end
robustTurtle.down()
for i=1,16 do
turtle.select(i)
turtle.dropDown()
end
turtle.select(1)