-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunk_miner.lua
261 lines (234 loc) · 6.19 KB
/
chunk_miner.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
-- Chunk Miner for Mining Turtle (16x16 area)
-- Designed for CC:Tweaked
-- Ensure the turtle has fuel and a chest is placed at the starting point for deposits.
-- Constants
local CHUNK_SIZE = 16
-- Variables to track position and direction
local posX, posY, posZ = 0, 0, 0
local direction = "north"
-- Functions
local function refuel()
for i = 1, 16 do
turtle.select(i)
if turtle.refuel(0) then
turtle.refuel()
print("Refueled using slot " .. i)
end
end
end
local function ensureFuel()
if turtle.getFuelLevel() == "unlimited" then return end
while turtle.getFuelLevel() < 1000 do
print("Low on fuel, attempting to refuel...")
refuel()
if turtle.getFuelLevel() < 1000 then
print("Please add fuel to the turtle and press any key to continue.")
os.pullEvent("key")
end
end
end
local function depositItems()
for i = 1, 16 do
turtle.select(i)
if not turtle.refuel(0) then -- Only drop items that are NOT fuel
turtle.drop()
end
end
turtle.select(1) -- Reset to the first slot
end
local function isInventoryFull()
for i = 1, 16 do
if turtle.getItemCount(i) == 0 then
return false -- Found an empty slot, inventory is not full
end
end
return true -- All slots are filled
end
local function turnLeft()
turtle.turnLeft()
if direction == "north" then
direction = "west"
elseif direction == "west" then
direction = "south"
elseif direction == "south" then
direction = "east"
elseif direction == "east" then
direction = "north"
end
end
local function turnRight()
turtle.turnRight()
if direction == "north" then
direction = "east"
elseif direction == "east" then
direction = "south"
elseif direction == "south" then
direction = "west"
elseif direction == "west" then
direction = "north"
end
end
local function moveForward()
turtle.forward()
if direction == "north" then
posY = posY + 1
elseif direction == "south" then
posY = posY - 1
elseif direction == "east" then
posX = posX + 1
elseif direction == "west" then
posX = posX - 1
end
end
local function moveBack()
turtle.back()
if direction == "north" then
posY = posY - 1
elseif direction == "south" then
posY = posY + 1
elseif direction == "east" then
posX = posX - 1
elseif direction == "west" then
posX = posX + 1
end
end
local function moveUp()
turtle.up()
posZ = posZ + 1
end
local function moveDown()
turtle.down()
posZ = posZ - 1
end
local function returnToStart()
print("Returning to start position...")
-- Move to the starting Z level
while posZ > 0 do
moveUp()
end
while posZ < 0 do
moveDown()
end
-- Move to the starting X position
while posX > 0 do
if direction ~= "west" then
turnLeft()
end
moveForward()
end
while posX < 0 do
if direction ~= "east" then
turnRight()
end
moveForward()
end
-- Move to the starting Y position
while posY > 0 do
if direction ~= "south" then
turnRight()
end
moveForward()
end
while posY < 0 do
if direction ~= "north" then
turnLeft()
end
moveForward()
end
-- Face north
while direction ~= "north" do
turnLeft()
end
end
local function returnToLayer(row, col)
print("Returning to previous position...")
for i = 1, row - 1 do
moveForward()
end
if row % 2 == 1 then
for i = 1, col - 1 do
moveForward()
end
else
for i = 1, CHUNK_SIZE - col do
moveForward()
end
end
end
local function digAndMoveForward()
while turtle.detect() do
turtle.dig()
end
moveForward()
end
local function digAndMoveDown()
while turtle.detectDown() do
turtle.digDown()
end
moveDown()
end
local function digAndMoveUp()
while turtle.detectUp() do
turtle.digUp()
end
moveUp()
end
local function mineLayer()
for row = 1, CHUNK_SIZE do
for col = 1, CHUNK_SIZE - 1 do
digAndMoveForward()
-- Check if inventory is full after every block
if isInventoryFull() then
print("Inventory full! Returning to deposit items...")
returnToStart() -- Go back to the starting position
depositItems() -- Deposit items
returnToLayer(row, col) -- Resume where it left off
end
end
if row < CHUNK_SIZE then
if row % 2 == 1 then
turnRight()
digAndMoveForward()
turnRight()
else
turnLeft()
digAndMoveForward()
turnLeft()
end
end
end
end
local function repositionToStartOfLayer()
print("Repositioning to the starting corner of the new layer...")
turnRight()
-- Move back to the starting block
for i = 1, CHUNK_SIZE - 1 do
digAndMoveForward()
end
-- Align with the start of the chunk again
turnRight()
end
local function mineChunk()
ensureFuel()
for depth = 1, 256 do
print("Mining layer " .. depth)
mineLayer()
if depth < 256 then
-- Move down to the next layer
digAndMoveDown()
-- Reposition to the bottom-left corner and face north
repositionToStartOfLayer()
end
end
print("Returning to surface...")
for i = 1, 255 do
digAndMoveUp()
end
print("Final deposit...")
depositItems()
print("Chunk mining complete!")
end
-- Main Program
print("Starting chunk miner...")
print("Ensure there is enough fuel and a chest for item deposits.")
mineChunk()