-
Notifications
You must be signed in to change notification settings - Fork 0
/
mine.lua
296 lines (232 loc) · 6.06 KB
/
mine.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
-- local t = trackable( turtle )
local makeRepeat = function( fn )
return function( t, n )
if n == nil then n = 1 end
for i = 1, n do
if false == t( fn ) then return i - 1 end
end
return n
end
end
function digMove( t, n )
for i = 1, n do
if not t( 'forward' ) then
t( 'dig' )
if not t( 'forward' ) then
return false
end
end
t( 'digUp' )
end
return true
end
local forward = makeRepeat( 'forward' )
local back = makeRepeat( 'back' )
local left = makeRepeat( 'turnLeft' )
local right = makeRepeat( 'turnRight' )
local up = makeRepeat( 'up' )
local down = makeRepeat( 'down' )
function goback( t, state )
if state.z > 0 then
down( t, state.z )
else
up( t, -state.z )
end
left( t, state.dir )
if state.y > 0 then
forward( t, state.y )
else
back( t, -state.y )
end
left( t )
if state.x > 0 then
forward( t, state.x )
else
back( t, -state.x )
end
right( t )
end
function _gotocoord( t, map, xf,yf,zf, x, y, z )
if z == nil then
x, y, z = xf, yf, zf
xf, yf, zf = 0, 0, 0
end
DStarLite( {xf, yf, zf, 0}, {x, y, z, 0}, map, function( v, cost, i )
return CoordMove( t, v, cost, i )
end)
end
local gotocoord
function mine( t, shaftRad, maxDepth, maxShafts, underground, mapfile )
local xoff = 0
local yoff = 10
local map = nil
local startState = t( 'getState' )
if mapfile then
map = loadmap( t, mapfile )
else
map = NodeMap()
savemap( t, map, mapfile )
end
gotocoord = function( t, xf, yf, zf, x, y, z )
_gotocoord( t, nil, xf, yf, zf, x, y, z )
end
for shaftNum = 0, maxShafts do
fuelTopOff( t )
mineOne( t, xoff, yoff, shaftRad, maxDepth, underground )
local state = t( 'getState' )
while state.dir ~= startState.dir do
t( 'turnLeft' )
state = t( 'getState' )
end
fuelTopOff( t )
emptyInventory( t )
savemap( t, map, mapfile )
if xoff < 0 then
xoff = -xoff
else
xoff = -xoff - ( 2 * shaftRad + 1 )
end
end
end
function savemap( t, m, file )
if file == nil then
file = '/john/data/mine.map'
end
local fh = fs.open( file, 'w' )
local ts = t( 'getState' )
fh.write( ts.x..'\n'..ts.y..'\n'..ts.z..'\n'..ts.dir..'\n' )
for i, n in m.each() do
fh.write( n:serialize().. '\n' )
end
fh.close()
end
function loadmap( t, file )
if file == nil then
file = '/john/data/mine.map'
end
local fh = fs.open( file, 'r' )
local map = NodeMap()
if not fh then
t( 'setState', 0, 0, 0, 0 )
return map
end
local x, y, z, dir = fh.readLine(), fh.readLine, fh.readLine, fh.readLine
t( 'setState', tonumber( x ), tonumber( y ), tonumber( z ), tonumber( dir ) )
local line = fh.readLine()
while line do
map.add( Node( line ) )
line = fh.readLine()
end
fh.close()
return map
end
-- assumes slot 1 has fuel. Never uses the last one.
function fuelTopOff( t )
local minFuel = 1000
local cur = t( 'getFuelLevel' )
for i = 2, 16 do
t( 'select', i )
if cur >= minFuel then
return
end
while cur < minFuel and t( 'refuel', 1 ) do
cur = t( 'getFuelLevel' )
end
end
if cur < minFuel then
t( 'select', 1 )
while t( 'getItemCount', 1 ) > 1 and cur < minFuel and t( 'refuel', 1 ) do
cur = t( 'getFuelLevel' )
end
end
if cur < minFuel then
return false
end
return true
end
function emptyInventory( t )
-- first slot is fuel I'm expecting to find. leave one there.
t( 'select', 1 )
t( 'drop', t( 'getItemCount', 1 ) - 1 )
for i = 2, 16 do
t( 'select', i )
t( 'drop' )
end
end
function mineOne( t, xoff, yoff, shaftRad, maxDepth, underground )
left( t, 2 )
if underground then
if not digMove( t, yoff ) then
print( 'Could not move to dig site' )
return false
end
else
drive( t, yoff )
end
if xoff > 0 then
t( 'turnRight' )
else
t( 'turnLeft' )
end
digMove( t, math.abs( xoff ) )
--t( 'pushState' )
shaft( t, shaftRad, math.floor( maxDepth / 2 ) )
--local undo = t( 'popState' )
local state = t( 'getState' )
gotocoord( t, state.x, state.y, state.z, 0, 0, 0 )
do return end
left( t, 2 )
-- move out of the shaft so drive() doesn't drop us back down
forward( t, shaftRad )
drive( t, 10 - shaftRad )
end
function shaft( t, rad, depth )
if depth < 1 then
return true
end
--t( 'pushState' )
t( 'digDown' )
t( 'down' )
local hitbedrock = false
local refuel = false
local ret = spiralDo( t, rad, function()
if t( 'detect' ) and not t( 'dig' ) or t( 'detectDown' ) and not t( 'digDown' ) then
hitbedrock = true
return false
end
if t( 'getFuelLevel' ) < 100 then
-- running low on fuel. try to go home and refuel.
local state = t( 'getState' )
gotocoord( t, state.x, state.y, state.z, 0, 0, 0 )
if not fuelTopOff( t ) then
return false
end
emptyInventory( t )
gotocoord( t, 0, 0, 0, state.x, state.y, state.z )
local newstate = t( 'getState' )
while newstate.dir ~= state.dir do
t( 'turnRight' )
newstate = t( 'getState' )
end
end
return true
end)
t( 'digDown' )
if ret == false then
t( 'up' )
--local undo = t( 'popState' )
-- undo should now have the coordinates the turtle moved
-- to since starting the shaft. Use that to move
-- back to the center and up the correct distance
--goback( t, undo )
return false
end
t( 'turnLeft' )
forward( t, rad )
t( 'turnRight' )
back( t, rad )
t( 'down' )
-- merge this layer with the total history of the turtle
--t( 'popState' )
return shaft( t, rad, depth - 2 )
end