forked from psyestorm/SnapAndBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnap.txt
221 lines (198 loc) · 4.95 KB
/
snap.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
local settings={}
settings["debug"]=false
settings["reserveFuelLevel"]=10
settings["nextStep"]=nil
settings["rightNext"]=false
settings["leftNext"]=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 needFuel()
local level=turtle.getFuelLevel()
if level=="unlimited" or level>settings["reserveFuelLevel"] then
sendMessageToPartner(cNo)
else
sendMessageToPartner(cYes)
end
end
function checkStatus()
if turtle.getItemCount(1)>0 then
print("Energy Cell OK")
else
print("Please insert Locked Energy Tesseract or Redstone Energy Cell into slot 1")
while turtle.getItemCount(1)==0 do
turtle.select(1)
sleep(0.1)
end
end
if turtle.getFuelLevel()~="unlimited" and turtle.getFuelLevel()<settings["reserveFuelLevel"] then
if turtle.getItemCount(16)>0 then
print("Fuel Level OK")
else
turtle.select(16)
print("Please insert fuel into slot 16")
while turtle.getItemCount(16)==0 do
sleep(0.1)
end
end
end
end
function waitForPartner()
print("Waiting for Box")
sendMessageToPartner(cPairing)
local message=nil
while message~=cPairing do
event = os.pullEvent("redstone")
message=redstone.getAnalogInput("front")
end
sendMessageToPartner(cPairing)
print("Off we go")
end
function sendMessageToPartner(message)
if debug==true then
print("Sent "..message)
end
redstone.setAnalogOutput("front",message)
sleep(0.1)
redstone.setAnalogOutput("front",cNone)
end
function forward()
sendMessageToPartner(cForward)
while turtle.forward()==false do
sleep(0.1)
end
if settings["rightNext"]==true then
turtle.turnRight()
settings["rightNext"]=false
end
if settings["leftNext"]==true then
turtle.turnLeft()
settings["leftNext"]=false
end
saveSettings()
end
function readyCharge()
sendMessageToPartner(cReadyCharge)
while turtle.back()==false do
sleep(0.1)
end
settings["nextStep"]="wait-forward"
saveSettings()
waitForward()
end
function waitForward()
sleep(2)
while turtle.detect()==true do
sleep(0.1)
end
sleep(2)
while turtle.forward()==false do
sleep(0.1)
end
settings["nextStep"]=nil
saveSettings()
sendMessageToPartner(cComplete)
end
function powerUp()
turtle.select(1)
turtle.digDown()
sendMessageToPartner(cPowerUp)
end
function powerDown()
turtle.select(1)
while turtle.placeDown()==false and turtle.getItemCount(1)>0 do
sleep(0.1)
end
sendMessageToPartner(cPowerDown)
end
function waitForMessageFromPartner()
if settings["debug"]==true then
print("Waiting for message")
end
os.pullEvent("redstone")
--check to see if Box has turned and moved - only works on left turns :(
local msg=redstone.getAnalogInput("front")
if msg==cPowerDown then
powerDown()
elseif msg==cPowerUp then
powerUp()
elseif msg==cRight then
settings["rightNext"]=true
saveSettings()
sendMessageToPartner(cRight)
elseif msg==cLeft then
settings["leftNext"]=true
saveSettings()
sendMessageToPartner(cLeft)
elseif msg==cForward then
forward()
elseif msg==cClearSettings then
shell.run("clearsettings")
elseif msg==cShutdown then
os.shutdown()
elseif msg==cReboot then
os.reboot()
elseif msg==cNeedFuel then
needFuel()
elseif msg==cReadyCharge then
readyCharge()
end
end
function checkResume()
local file=fs.open("SnapBoxSettings","r")
if file==nil then
return false
else
file.close()
return true
end
end
-----MAIN PROGRAM
term.clear()
term.setCursorPos(1,1)
--Check for a resume
if checkResume()==false then
--Check to make sure it has all the items it needs
checkStatus()
--initial refuel
turtle.select(16)
turtle.refuel()
turtle.select(1)
--wait to find partner
waitForPartner()
saveSettings()
else
print("Resuming")
loadSettings()
end
while true do
if settings["nextStep"]=="wait-forward" then
waitForward()
end
waitForMessageFromPartner()
end