-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdig.lua
More file actions
412 lines (348 loc) · 7.31 KB
/
dig.lua
File metadata and controls
412 lines (348 loc) · 7.31 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
-- This is an API for digging and
-- keeping track of motion
-- Flexico64@gmail.com
-- x is right, y is up, z is forward
-- r is clockwise rotation in degrees
os.loadAPI("flex.lua")
dist_x = 0
xmin = 0
xmax = 0
dist_y = 0
ymin = 0
ymax = 0
dist_z = 0
zmin = 0
zmax = 0
dist_r = 0
lastMove = "?"
function getx() return dist_x end
function gety() return dist_y end
function getz() return dist_z end
function getr() return dist_r end
function setx(x) dist_x = x end
function sety(y) dist_y = y end
function setz(z) dist_z = z end
function setr(r) dist_r = r end
function getLastMove()
return lastMove
end
function setLastMove(lm)
lastMove = lm
end
function getXmin() return xmin end
function getXmax() return xmax end
function getYmin() return ymin end
function getYmax() return ymax end
function getZmin() return zmin end
function getZmax() return zmax end
function setXmin(x) xmin = x end
function setXmax(x) xmax = x end
function setYmin(y) ymin = y end
function setYmax(y) ymax = y end
function setZmin(z) zmin = z end
function setZmax(z) zmax = z end
fuelSlot = {1,16}
function getFuelSlot()
return fuelSlot[1],fuelSlot[2]
end
function setFuelSlot(a,b)
b = b or a
fuelSlot[1] = a
fuelSlot[2] = b
end
function location()
return
{ dist_x, dist_y, dist_z, dist_r,
lastMove, xmin, xmax, ymin,
ymax, zmin, zmax }
end
function saveCoords()
local file,loc,x
file = fs.open("dig_save.txt","w")
loc = location()
for x=1,#loc do
file.writeLine(tostring(loc[x]))
end --for
file.close()
end --function
function loadCoords()
local file
file = fs.open("dig_save.txt","r")
dist_x = tonumber(file.readLine())
dist_y = tonumber(file.readLine())
dist_z = tonumber(file.readLine())
dist_r = tonumber(file.readLine())
lastMove = file.readLine()
xmin = tonumber(file.readLine())
xmax = tonumber(file.readLine())
ymin = tonumber(file.readLine())
ymax = tonumber(file.readLine())
zmin = tonumber(file.readLine())
zmax = tonumber(file.readLine())
file.close()
end
function makeStartup(command, args)
command = tostring(command)
local x
for x=1,#args do
command = command.." "..args[x]
end --for
local file = fs.open("startup.lua","w")
file.writeLine("os.loadAPI(\"flex.lua\")")
file.writeLine("flex.send(\"> "..command.."\")")
file.writeLine("shell.run(\""..command.."\")")
file.writeLine("os.unloadAPI(\"flex.lua\")")
file.close()
end --function
stuck = false
function isStuck()
return stuck
end
function update(n)
if n=="fwd" then
if dist_r == 0 then
dist_z = dist_z + 1
lastMove = "z+"
elseif dist_r == 90 then
dist_x = dist_x + 1
lastMove = "x+"
elseif dist_r == 180 then
dist_z = dist_z - 1
lastMove = "z-"
elseif dist_r == 270 then
dist_x = dist_x - 1
lastMove = "x-"
end
elseif n=="up" then
dist_y = dist_y + 1
lastMove = "y+"
elseif n=="down" then
dist_y = dist_y - 1
lastMove = "y-"
elseif n=="right" then
dist_r = dist_r + 90
elseif n=="left" then
dist_r = dist_r - 90
end
while dist_r >= 360 do
dist_r = dist_r - 360
end
while dist_r < 0 do
dist_r = dist_r + 360
end
if dist_x < xmin then
xmin = dist_x
elseif dist_x > xmax then
xmax = dist_x
end
if dist_y < ymin then
ymin = dist_y
elseif dist_y > ymax then
ymax = dist_y
end
if dist_z < zmin then
zmin = dist_z
elseif dist_z > zmax then
zmax = dist_z
end
saveCoords()
end
-- MOVEMENT FUNCTIONS
function refuel(b)
b = b or 1
local a,x,z,slot
slot = turtle.getSelectedSlot()
a = true
while turtle.getFuelLevel() < b do
for x=fuelSlot[1],fuelSlot[2] do
turtle.select(x)
if turtle.refuel(1) then break end
if x == fuelSlot[2] and a then
flex.send("Waiting for fuel...",colors.orange)
a = false
end --if
end --for
end --while
if not a then
flex.send("Thanks!",colors.lime)
end --if
turtle.select(slot)
end --function
function fwd(n)
local x,a,b
for x=1, n or 1 do
refuel()
if flex.getBlock() == "minecraft:tall_grass" or
flex.getBlock() == "minecraft:tallgrass" or
flex.getBlock() == "minecraft:grass" then
turtle.dig()
end
stuck = false
while not turtle.forward() do
a,b = turtle.dig()
if b == "Unbreakable block detected" then
flex.send(b,colors.orange)
stuck = true
return
end
--turtle.attack()
end
update("fwd")
end
end
function up(n)
local x,a,b
for x=1, n or 1 do
refuel()
stuck = false
while not turtle.up() do
a,b = turtle.digUp()
if b == "Unbreakable block detected" then
flex.send(b,colors.orange)
stuck = true
return
end
--turtle.attackUp()
end
update("up")
end
if (n or 1) < 0 then
down(-n)
end
end
function down(n)
local x
for x=1, n or 1 do
refuel()
stuck = false
while not turtle.down() do
a,b = turtle.digDown()
if b == "Unbreakable block detected" then
flex.send(b,colors.orange)
stuck = true
return
end
--turtle.attackDown()
end
update("down")
end
if (n or 1) < 0 then
up(-n)
end
end
function left(n)
local x
for x=1, n or 1 do
turtle.turnLeft()
update("left")
end
for x=n or 1, -1 do
turtle.turnRight()
update("right")
end
end
function right(n)
local x
for x=1, n or 1 do
turtle.turnRight()
update("right")
end
for x=n or 1, -1 do
turtle.turnLeft()
update("left")
end
end
function dig()
while turtle.dig() do end
end
-- GOTO FUNCTIONS
function gotor(r)
local r = math.floor(r/90)*90
while r>=360 do r = r-360 end
while r<0 do r = r+360 end
local x = r-dist_r
while x < 0 do x = x+360 end
if x == 90 then right()
elseif x == 180 then
if r == 0 then right(2)
else left(2) end
elseif x == 270 then left()
end
end
function gotoy(y)
while dist_y < y do
up()
end
while dist_y > y do
down()
end
end
function gotox(x)
if dist_x < x then
gotor(90)
end
if dist_x > x then
gotor(270)
end
while dist_x ~= x do
fwd()
end
end
function gotoz(z)
if dist_z < z then
gotor(0)
end
if dist_z > z then
gotor(180)
end
while dist_z ~= z do
fwd()
end
end
function goto(x,y,z,r,lm)
if type(x) == "table" then
if #x < 4 then
flex.send("Invalid Goto Table",colors.red)
return
end
y = x[2]
z = x[3]
r = x[4]
lm = x[5]
x = x[1]
end
gotoz(z or 0)
gotox(x or 0)
gotor(r or 0)
gotoy(y or 0)
setLastMove(lm or "?")
end
-------
function place()
while not turtle.place() and (
flex.getBlock()=="minecraft:air" or
flex.getBlock()=="minecraft:water" or
flex.getBlock()=="minecraft:lava" ) do
turtle.attack()
turtle.attackUp()
turtle.attackDown()
end
end
function placeUp()
while not turtle.placeUp() and (
flex.getBlock("up")=="minecraft:air" or
flex.getBlock("up")=="minecraft:water" or
flex.getBlock("up")=="minecraft:lava" ) do
turtle.attack()
turtle.attackUp()
end
end
function placeDown()
while not turtle.placeDown() and (
flex.getBlock("down")=="minecraft:air" or
flex.getBlock("down")=="minecraft:water" or
flex.getBlock("down")=="minecraft:lava" ) do
turtle.attack()
turtle.attackDown()
end
end