-
Notifications
You must be signed in to change notification settings - Fork 12
/
relgo
363 lines (337 loc) · 6.91 KB
/
relgo
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
--[[ relgo
A modified version of the 'go' API - functionally identical, but all coordinates are relative to where they were initially set. No GPS required, however, it may be less versatile.
Written by apemanzilla
]]
local pos = {}
local df = {
[0] = { -- South
dx = 0,
dz = 1
},
[1] = { -- West
dx = -1,
dz = 0
},
[2] = { -- North
dx = 0,
dz = -1
},
[3] = { -- East
dx = 1,
dz = 0
}
}
--[[ Utility Functions ]]
local function dirToFacing(dir)
if dir == "north" then
return 2
elseif dir == "south" then
return 0
elseif dir == "east" then
return 3
elseif dir == "west" then
return 1
end
end
function savePos()
if pos.x and pos.y and pos.z and pos.f then
local f = fs.open(".position","w")
f.write(textutils.serialize(pos))
f.close()
end
end
function loadPos()
if fs.exists(".position") then
local f = fs.open(".position","r")
local temppos = textutils.unserialize(f.readAll())
f.close()
if (temppos.x and temppos.y and temppos.z and temppos.f) then
pos.x, pos.y, pos.z, pos.f = temppos.x, temppos.y, temppos.z, temppos.f
else
pos.x = 0
pos.y = 64
pos.z = 0
pos.f = 0
end
else
pos.x = 0
pos.y = 64
pos.z = 0
pos.f = 0
end
end
loadPos()
function getPos()
return pos
end
function setPos(x,y,z,f)
assert(x and y and z and f,"expected number, number, number, number")
pos.x = x
pos.y = y
pos.z = z
pos.f = f
savePos()
end
function adjustCoords(tbl, change, f)
assert(tbl.x and tbl.y and tbl.z, "missing coordinate(s)")
tbl.x = tbl.x + (df[f].dx * change)
tbl.z = tbl.z + (df[f].dz * change)
return tbl
end
local turtle = {}
for k,v in pairs(_G.turtle) do
turtle[k] = v
end
function fuelTo(x, y, z, fromPos)
if type(x) == "table" then
y = x.y
z = x.z
f = x.f
x = x.x
end
local p = fromPos or pos
local needed = math.abs(p.x - x) + math.abs(p.y - y) + math.abs(p.z - z)
return needed
end
function canReach(x, y, z)
if type(x) == "table" then
y = x.y
z = x.z
f = x.f
x = x.x
end
if not (pos.x and pos.y and pos.z) then
return false, "cannot determine position"
end
if not x then x = pos.x end
if not y then y = pos.y end
if not z then z = pos.z end
local fuelNeeded = math.abs(pos.x - x) + math.abs(pos.y - y) + math.abs(pos.z - z)
if turtle.getFuelLevel() < fuelNeeded then
return false, fuelNeeded
end
return true, fuelNeeded
end
function pE(p1, p2)
return p1.x == p2.x and p1.y == p2.y and p1.z == p2.z
end
--[[ Basic Movement Functions ]]
function forward()
local success, err = turtle.forward()
if success then
if pos.x and pos.z and pos.f then
pos.x = pos.x + df[pos.f].dx
pos.z = pos.z + df[pos.f].dz
end
savePos()
return true
else
return false, err
end
end
function back()
local success, err = turtle.back()
if success then
if pos.x and pos.z and pos.f then
pos.x = pos.x - df[pos.f].dx
pos.z = pos.z - df[pos.f].dz
end
savePos()
return true
else
return false, err
end
end
function up()
local success, err = turtle.up()
if success then
if pos.y then
pos.y = pos.y + 1
end
savePos()
return true
else
return false, err
end
end
function down()
local success, err = turtle.down()
if success then
if pos.y then
pos.y = pos.y - 1
end
savePos()
return true
else
return false, err
end
end
function right()
local success, err = turtle.turnRight()
if success then
if pos.f then
pos.f = pos.f + 1
if pos.f > 3 then pos.f = 0 end
end
savePos()
return true
else
return false, err4
end
end
function left()
local success, err = turtle.turnLeft()
if success then
if pos.f then
pos.f = pos.f - 1
if pos.f < 0 then pos.f = 3 end
end
savePos()
return true
else
return false, err
end
end
--[[ Advanced Movement Functions ]]
function rotateTo(f)
if f > 3 or f < 0 then
return false, "invalid facing"
end
if not pos.f then
return false, "cannot determine facing"
end
local function simulateRotateTo(startf, endf)
local right, left = 0, 0
local f = startf
while f ~= endf do
right = right + 1
f = f + 1
if f > 3 then f = 0 end
end
f = startf
while f ~= endf do
left = left + 1
f = f - 1
if f < 0 then f = 3 end
end
return right, left
end
local r,l = simulateRotateTo(pos.f, f)
if r <= l then
for i = 1, r do
right()
end
else
for i = 1, l do
left()
end
end
return true
end
function goto(x, y, z, f)
if type(x) == "table" then
y = x.y
z = x.z
f = x.f
x = x.x
end
-- Validation and inital setup
if f and (f < 0 or f > 3) then
return false, "invalid facing"
end
if not (pos.x and pos.y and pos.z and pos.f) then
local success, err = updatePos()
if not success then return false, err end
end
local start = {}
start.x, start.y, start.z, start.f = pos.x, pos.y, pos.z, pos.f
local dest = {}
dest.x, dest.y, dest.z, dest.f = x, y, z, f
if not dest.x then dest.x = start.x end
if not dest.y then dest.y = start.y end
if not dest.z then dest.z = start.z end
if not dest.f then dest.f = start.f end
if canReach(dest.x, dest.y, dest.z) then
-- Function to determine facing necessary for coordinate change
local function facingForCoordChange(axis, change)
if axis == "x" then
if change > 0 then
return 3
elseif change < 0 then
return 1
else
return
end
elseif axis == "z" then
if change > 0 then
return 0
elseif change < 0 then
return 2
else
return
end
end
return
end
-- Move to specified coordinates
-- Y up
if start.y < dest.y then
for i = 1, math.abs(start.y - dest.y) do
while not up() do
while peripheral.getType("top") == "turtle" do sleep(1) end
turtle.digUp()
turtle.attackUp()
end
end
end
-- Y down
if start.y > dest.y then
for i = 1, math.abs(start.y - dest.y) do
while not down() do
while peripheral.getType("bottom") == "turtle" do sleep(1) end
turtle.digDown()
turtle.attackDown()
end
end
end
-- X
if facingForCoordChange("x", dest.x - start.x) then
rotateTo(facingForCoordChange("x", dest.x - start.x))
end
for i = 1, math.abs(start.x - dest.x) do
while not forward() do
--while peripheral.getType("front") == "turtle" do sleep(1) end
turtle.dig()
turtle.attack()
end
end
-- Z
if facingForCoordChange("z", dest.z - start.z) then
rotateTo(facingForCoordChange("z", dest.z - start.z))
end
for i = 1, math.abs(start.z - dest.z) do
while not forward() do
--while peripheral.getType("front") == "turtle" do sleep(1) end
turtle.dig()
turtle.attack()
end
end
-- Rotate
rotateTo(dest.f)
-- Attempt to validate position
local final = {}
final.x, final.y, final.z = gps.locate(0.1)
if final.x and final.y and final.z and validatePos then
if final.x == dest.x and final.y == dest.y and final.z == dest.z then
return true
else
return false, "final position does not match"
end
else
-- Assume we are in the right spot
return true
end
else
return canReach(dest.x, dest.y, dest.z)
end
end