forked from psyestorm/SnapAndBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbox.txt
462 lines (421 loc) · 11.8 KB
/
box.txt
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
--SETTINGS DEFAULTS
local stop=false
local settings={}
settings["miningTime"]=5
settings["digDelay"]=1
settings["reserveFuelLevel"]=10
settings["chargeTime"]=30
settings["debug"]=false
settings["blocksToTravel"]=0
settings["blocksTraveled"]=0
settings["rowsToTravel"]=0
settings["rowsTraveled"]=0
settings["blocksMined"]=0
local nextStep=nil
settings["turning"]="left"
settings["midTurn"]=false
--CONSTANTS
local cNone=0
local cLeft=1
local cYes=2
local cNo=3
local cPowerDown=4
local cPowerUp=5
local cForward=6
local cReadyCharge=7
local cComplete=8
local cGoHome=9
local cRight=10
local cReboot=11
local cShutdown=12
local cClearSettings=13
local cNeedFuel=14
local cPairing=15
function loadSettings()
local file = fs.open("SnapBoxSettings","r")
local data = file.readAll()
file.close()
settings=textutils.unserialize(data)
end
function saveSettings()
local file = fs.open("SnapBoxSettings","w")
file.write(textutils.serialize(settings))
file.close()
end
function checkStatus()
if turtle.getItemCount(1)>0 then
print("Ender Chest OK")
else
print("Please insert an Ender Chest into slot 1")
while turtle.getItemCount(1)==0 do
turtle.select(1)
sleep(0.1)
end
end
if turtle.getItemCount(2)>0 then
print("Mining Well OK")
else
print("Please insert Mining Well into slot 2")
while turtle.getItemCount(2)==0 do
turtle.select(2)
sleep(0.1)
end
end
if turtle.getFuelLevel()~="unlimited" then
if turtle.getItemCount(3)>0 then
print("Charge Station OK")
else
print("Please insert Advanced Charge Station (or higher) into slot 3")
while turtle.getItemCount(3)==0 do
turtle.select(3)
sleep(0.1)
end
end
end
if needsFuel() then
if turtle.getItemCount(16)>0 then
print("Fuel Level OK")
else
print("Please insert fuel in slot 16")
while turtle.getItemCount(16)==0 do
turtle.select(16)
sleep(0.1)
end
end
end
end
function needsFuel()
local level=turtle.getFuelLevel()
if level=="unlimited" or level>settings["reserveFuelLevel"] then
return false
else
return true
end
end
function waitForPartner()
print("Waiting for Snap")
sendMessageToPartner(cPairing)
waitForMessageFromPartner(cPairing)
sendMessageToPartner(cPairing)
print("Off we go")
end
function sendMessageToPartner(message)
if debug==true then
print("sending "..message)
end
redstone.setAnalogOutput("back",message)
sleep(0.1)
redstone.setAnalogOutput("back",cNone)
end
function waitForMessageFromPartner(message)
if debug==true then
print("waiting for "..message)
end
local input=nil
while input~=message do
event = os.pullEvent("redstone")
input=redstone.getAnalogInput("back")
end
end
function moveForward()
while turtle.detect() do
turtle.dig()
sleep(settings["digDelay"])
end
while turtle.forward()==false do
turtle.dig()
sleep(settings["digDelay"])
turtle.attack()
end
end
function topOff()
--make sure we're not turning soon
if settings["blocksTraveled"]<settings["blocksToTravel"] and settings["blocksTraveled"]>1 then
if needsFuel() or partnerNeedsFuel()==true then
setNextStep("chargepowerdown")
else
setNextStep("placeminingwell")
end
else
setNextStep("placeminingwell")
end
end
function partnerNeedsFuel()
sendMessageToPartner(cNeedFuel)
os.pullEvent("redstone")
if redstone.getAnalogInput("back")==cYes then
return true
else
return false
end
end
function placeCharge()
turtle.select(3)
while turtle.place()==false do
turtle.attack()
end
end
function digCharge()
turtle.select(3)
turtle.dig()
end
function placeChest()
turtle.select(1)
while turtle.placeUp()==false do
turtle.attackUp()
turtle.digUp()
sleep(settings["digDelay"])
end
end
function digChest()
turtle.select(1)
if turtle.getItemCount(1) > 0 then
turtle.drop()
end
turtle.digUp()
end
function placeMiningWell()
turtle.select(2)
while turtle.placeDown()==false do
turtle.digDown()
sleep(settings["digDelay"])
turtle.attackDown()
end
end
function digMiningWell()
turtle.select(2)
if turtle.getItemCount(2) > 0 then
turtle.drop()
end
turtle.digDown()
end
function emptyInventory()
for i=1,16 do
if turtle.getItemCount(i)>0 then
if i~=3 then
--dont empty charge station slot
while turtle.getItemCount(i)>0 do
turtle.select(i)
turtle.dropUp()
end
end
end
end
end
function setNextStep(next)
if debug==true then
print("Set next step "..next)
end
local file=nil
while file==nil do
file=fs.open("SnapBoxStepTracker","w")
end
file.write(next)
file.close()
nextStep=next
end
function getNextStep()
local file=fs.open("SnapBoxStepTracker","r")
if file==nil then
setNextStep("topoff")
else
local returnValue=file.readAll()
file.close()
setNextStep(returnValue)
end
end
function incrementBlockCounter()
settings["blocksTraveled"]=settings["blocksTraveled"]+1
saveSettings()
end
function incrementRowCounter()
settings["rowsTraveled"]=settings["rowsTraveled"]+1
saveSettings()
end
function checkReturn()
if settings["rowsTraveled"]>=settings["rowsToTravel"] and settings["rowsToTravel"]~=0 then
return true
else
return false
end
end
function checkRow()
if settings["midTurn"]==true then
turn()
settings["midTurn"]=false
settings["blocksTraveled"]=1
if settings["turning"]=="left" then
settings["turning"]="right"
else
settings["turning"]="left"
end
saveSettings()
end
if settings["blocksTraveled"]>=settings["blocksToTravel"] and settings["blocksToTravel"]~=0 then
incrementRowCounter()
turn()
settings["midTurn"]=true
saveSettings()
end
end
function partnerRight()
if settings["blocksTraveled"]>=settings["blocksToTravel"] and settings["blocksToTravel"]~=0 and settings["turning"]=="right" then
sendMessageToPartner(cRight)
waitForMessageFromPartner(cRight)
end
end
function partnerLeft()
if settings["blocksTraveled"]>=settings["blocksToTravel"] and settings["blocksToTravel"]~=0 and settings["turning"]=="left" then
sendMessageToPartner(cLeft)
waitForMessageFromPartner(cLeft)
end
end
function turn()
if settings["turning"]=="left" then
turtle.turnLeft()
else
turtle.turnRight()
end
end
function checkResume()
local file=fs.open("SnapBoxStepTracker","r")
if file==nil then
return false
else
file.close()
return true
end
end
function doNextStep()
--is there a better way to do this?
if nextStep=="topoff" then
topOff()
elseif nextStep=="chargepowerdown" then
sendMessageToPartner(cPowerDown)
waitForMessageFromPartner(cPowerDown)
setNextStep("chargepartnerreadycharge")
elseif nextStep=="chargepartnerreadycharge" then
sendMessageToPartner(cReadyCharge)
waitForMessageFromPartner(cReadyCharge)
setNextStep("chargeturnleft1")
elseif nextStep=="chargeturnleft1" then
turtle.turnLeft()
setNextStep("chargeturnleft2")
elseif nextStep=="chargeturnleft2" then
turtle.turnLeft()
setNextStep("chargeplacecharge")
elseif nextStep=="chargeplacecharge" then
placeCharge()
setNextStep("chargewaitforrefuel")
elseif nextStep=="chargewaitforrefuel" then
sleep(settings["chargeTime"])
setNextStep("chargedigcharge")
elseif nextStep=="chargedigcharge" then
digCharge()
setNextStep("chargeturnleft3")
elseif nextStep=="chargeturnleft3" then
turtle.turnLeft()
setNextStep("chargeturnleft4")
elseif nextStep=="chargeturnleft4" then
turtle.turnLeft()
setNextStep("chargeforward")
elseif nextStep=="chargeforward" then
waitForMessageFromPartner(cComplete)
setNextStep("chargepowerup")
elseif nextStep=="chargepowerup" then
sendMessageToPartner(cPowerUp)
waitForMessageFromPartner(cPowerUp)
setNextStep("placeminingwell")
elseif nextStep=="placeminingwell" then
placeMiningWell()
setNextStep("placechest")
elseif nextStep=="placechest" then
placeChest()
setNextStep("partnerpowerdown")
elseif nextStep=="partnerpowerdown" then
sendMessageToPartner(cPowerDown)
waitForMessageFromPartner(cPowerDown)
incrementBlockCounter()
setNextStep("partnerpowerup")
sleep(settings["miningTime"])
elseif nextStep=="partnerpowerup" then
sendMessageToPartner(cPowerUp)
waitForMessageFromPartner(cPowerUp)
setNextStep("emptyinventory")
elseif nextStep=="emptyinventory" then
emptyInventory()
setNextStep("digchest")
elseif nextStep=="digchest" then
digChest()
setNextStep("digminingwell")
elseif nextStep=="digminingwell" then
digMiningWell()
setNextStep("partnerturn")
elseif nextStep=="partnerturn" then
partnerRight()
partnerLeft()
setNextStep("partnerforward")
elseif nextStep=="partnerforward" then
sendMessageToPartner(cForward)
waitForMessageFromPartner(cForward)
setNextStep("checkrow")
elseif nextStep=="checkrow" then
checkRow()
setNextStep("moveforward")
elseif nextStep=="moveforward" then
moveForward()
setNextStep("checkreturn")
elseif nextStep=="checkreturn" then
if checkReturn()==true then
print("Done")
setNextStep("gohome")
else
setNextStep("topoff")
end
elseif nextStep=="gohome" then
sendMessageToPartner(cClearSettings)
sendMessageToPartner(cShutdown)
shell.run("clearsettings")
stop=true
else
print("Unknown Step - Something Went Wrong. Giving Up "..nextStep)
end
end
function clearScreen()
term.clear()
term.setCursorPos(1,1)
end
--------MAIN PROGRAM
clearScreen()
if checkResume()==false then
--check to make sure it has all the items it needs
checkStatus()
print("Enter the number (greater than 2) of blocks to travel before the next row (0 or blank for never)")
local input=io.read()
if input~="" then
settings["blocksToTravel"]=tonumber(input)
end
print("Enter the number of rows to travel before stopping (0 or blank for never)")
input=io.read()
if input~="" then
settings["rowsToTravel"]=tonumber(input)
end
--initial refuel
turtle.select(16)
turtle.refuel()
turtle.select(1)
--wait to find partner
waitForPartner()
nextStep="topoff"
saveSettings()
else
loadSettings()
getNextStep()
--this sleep allows snap to wake up before box
sleep(5)
end
--main loop
while stop==false do
doNextStep()
sleep(0.2)
end