-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRRMine.lua
More file actions
378 lines (260 loc) · 4.7 KB
/
RRMine.lua
File metadata and controls
378 lines (260 loc) · 4.7 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
--
-- Option Variables
--
mode = 0
length = 0
rows = 0
torch = 0
--
-- Positioning System Variables
--
x = 0 -- X-Coordinate on XY-plane centered on chest at position (0,0).
y = 1 -- Y-Coordinate. The chest is at position (0,0) or behind the turtle when it is started.
dir = 0 -- The directional heading of the turtle. Headings are in the set: {0,1,2,3} representing: {+y,+x,-y,-x} in a clockwise direction.
function output(text, color)
color = color or colors.white
term.setTextColor(color)
write(text)
term.setTextColor(colors.white)
end
function outputln(text, color) -- Dirty Funtion.
color = color or colors.white
term.setTextColor(color)
print(text)
term.setTextColor(colors.white)
end
function bar()
outputln("================", colors.lightGray)
end
function br()
print()
end
function err(...)
-- TODO: Add Error formatter.
br()
bar()
for line in arg do
outputln(text, colors.red)
end
bar()
br()
end
function reset()
term.clear()
term.setCursorPos(1,1)
outputln("RR|Mine", colors.green)
bar()
br()
end
function init()
reset()
output("Enter Mode: ")
mode = io.read()
mode = tonumber(mode)
reset()
output("Enter Length: ")
length = io.read()
length = tonumber(length)
reset()
output("Enter Rows: ")
rows = io.read()
rows = tonumber(rows)
reset()
output("Enter Torch Interval: ")
torch = io.read()
torch = tonumber(torch)
reset()
output("Name? (y/n): ")
if (io.read() == y) then
shell.run(label, "set", "RR|MiningTurtle")
end
end
function pov(dist)
if dir == 0 then
return {x, y + dist}
elseif dir == 1 then
return {x + dist, y}
elseif dir == 2 then
return {x, y - dist}
elseif dir == 3 then
return {x - dist, y}
end
end
function fuel()
if turtle then
while turtle.getFuelLevel() == 0 do
turtle.select(1)
turtle.refuel(1)
if turtle.getFuelLevel() == 0 then
err("Ran out of fuel! Refuel and press any key to continue!")
io.read()
end
end
end
end
function turn()
if mode == 1 then
if turtle then
turtle.turnRight()
end
dir = dir + 1
if dir > 3 then
dir = 0
end
else
if turtle then
turtle.turnLeft()
end
dir = dir - 1
if dir < 0 then
dir = 3
end
end
end
function turnback()
if mode == 1 then
if turtle then
turtle.turnLeft()
end
dir = dir - 1
if dir < 0 then
dir = 3
end
else
if turtle then
turtle.turnRight()
end
dir = dir + 1
if dir > 3 then
dir = 0
end
end
end
function face(d)
while not dir == d do
turn()
end
end
function move()
if (not turtle) or (turtle.forward()) then
if dir == 0 then
y = y + 1
elseif dir == 1 then
x = x + 1
elseif dir == 2 then
y = y - 1
elseif dir == 3 then
x = x - 1
end
if (not turtle) then
output("X: " .. x .. " Y: " .. y)
io.read()
else
outputln("X: " .. x .. " Y: " .. y)
end
end
end
function depositinventory()
if not turtle then
return
end
for i=4,16,1 do
turtle.select(i)
turtle.drop()
end
end
function row(l)
for i=1,l,1 do
fuel()
while turtle and turtle.detect() do
turtle.dig()
end
move()
if turtle and not turtle.detectDown() then
turtle.select(3)
turtle.placeDown()
end
if i % torch == 1 then
turn()
turn()
if turtle then
turtle.select(2)
turtle.place()
else
tp = pov(1)
outputln("Torch at X: " .. tp[1] .. " Y: " .. tp[2], colors.yellow)
end
turnback()
turnback()
end
while turtle and turtle.detectUp() do
turtle.digUp()
end
end
end
function main()
init()
for i=1,rows,1 do
reset()
outputln("Start Row: " .. i)
bar()
row(length)
bar()
outputln("End Row: " .. i)
reset()
outputln("Cutting Over")
bar()
turn()
row(3)
turn()
bar()
outputln("Cut Over")
reset()
outputln("Starting Row Back: " .. i)
bar()
row(length)
bar()
outputln("End Row Back: " .. i)
turn()
reset()
outputln("Begin homing.")
bar()
pos = x
row(2)
while (not (x == 0)) do
while turtle and turtle.detect() do
turtle.dig()
end
move()
end
bar()
outputln("Home.")
turnback()
depositinventory()
turnback()
if (i == rows) then
reset()
outputln("Done.", colors.lime)
return
end
reset()
outputln("Returning to mine.")
bar()
while (not (x == pos)) do
while turtle and turtle.detect() do
turtle.dig()
end
move()
end
bar()
print("Back at mining location.")
reset()
print("Shifting Over.")
bar()
row(3)
turnback()
bar()
print("Shifted Over.")
end
end
main()