-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartmove.lua
364 lines (321 loc) · 8.43 KB
/
smartmove.lua
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
local smartmove = {}
local c = require("component")
local ic
local sides = require("sides")
local event = require("event")
-- Utility that keeps track of the robot's movements so it knows where it is relative to the starting location.
-- Coordinates have nothing to do with map coordinates, it does not rely on the map upgrade.
-- X axis: The direction the robot was initially facing
-- Z axis: Right side direction
-- Y axis: Up and down
-- Starting point is 0,0,0 (X,Z,Y)
local SmartMove = {
}
function SmartMove:_tryClimb(direction)
local timeout = self.moveTimeout
local result
repeat
if direction == 1 then
result = self.robot.up()
else
result = self.robot.down()
end
if not result and timeout > 0 then
event.pull(1, "_smartmove")
end
timeout = timeout - 1
until result or timeout <= 0
return result
end
function SmartMove:_climb(direction, distance, atomic)
distance = distance or 1
local moveCount = 0
while moveCount < distance do
local result = self:_tryClimb(direction)
if result then
moveCount = moveCount + 1
self.posY = self.posY + direction
elseif atomic then
-- failed to move, gotta undo
local undid = self:_climb(-direction, moveCount, false)
return false, undid
else
return false
end
end
return true
end
function SmartMove:_tryMove(direction)
local timeout = self.moveTimeout
local result, reason
repeat
if direction == 1 then
result, reason = self.robot.forward()
else
result, reason = self.robot.back()
end
if not result then
print("smartmove: move fail: " .. reason)
end
if not result and timeout > 0 then
if direction == -1 then
direction = 1
self.robot.turnAround()
end
self.robot.swing()
event.pull(1, "_smartmove")
end
timeout = timeout - 1
until result or timeout <= 0
return result
end
function SmartMove:_move(direction)
local result = self:_tryMove(direction)
if result then
if self.orient == 1 or self.orient == -1 then
self.posX = self.posX + (direction*self.orient)
else
self.posZ = self.posZ + (direction*self.orient/2)
end
end
return result
end
function SmartMove:summary()
return '(' .. self.posX .. ',' .. self.posZ .. ',' .. self.posY .. ') orient ' .. self.orient
end
function SmartMove:forward()
return self:_move(1)
end
function SmartMove:backward()
return self:_move(-1)
end
function SmartMove:up(distance, atomic)
return self:_climb(1, distance, atomic)
end
function SmartMove:down(distance, atomic)
return self:_climb(-1, distance, atomic)
end
function SmartMove:advance(direction)
-- see if we can go that way by just moving backward, to avoid having to turnaround
if self.orient == -direction then
return self:backward()
else
return self:faceDirection(direction) and self:forward()
end
end
function SmartMove:faceXZ(x, z)
if x ~= self.posX then
if x < self.posX then
self:faceDirection(-1)
elseif x > self.posX then
self:faceDirection(1)
end
elseif z ~= self.posZ then
if z < self.posZ then
self:faceDirection(-2)
elseif z > self.posZ then
self:faceDirection(2)
end
end
end
function SmartMove:swing(direction)
return self:faceDirection(direction) and self.robot.swing()
end
function SmartMove:_turn(direction)
local result
if direction == 1 then
result = self.robot.turnRight()
else
result = self.robot.turnLeft()
end
if result then
if self.orient == 1 then
self.orient = direction * 2
elseif self.orient == -1 then
self.orient = direction * -2
elseif self.orient == 2 then
self.orient = direction * -1
elseif self.orient == -2 then
self.orient = direction * 1
end
end
return result
end
function SmartMove:turnRight()
return self:_turn(1)
end
function SmartMove:turnLeft()
return self:_turn(-1)
end
function SmartMove:turnAround()
local result = self.robot.turnAround()
if result then
self.orient = -self.orient
end
return result
end
function SmartMove:forwardUntilBlocked()
while self:forward() do
end
end
function SmartMove:faceDirection(o)
-- makes the robot oriented in the desired direction
-- by turning in the appropriate direction
if self.orient == o then
return true
end
if self.orient == -o then
-- 180
self:turnAround()
-- probably could be more clever
elseif o == -1 and self.orient == -2 then
self:turnLeft()
elseif o == -1 and self.orient == 2 then
self:turnRight()
elseif o == 1 and self.orient == -2 then
self:turnRight()
elseif o == 1 and self.orient == 2 then
self:turnLeft()
elseif o == -2 and self.orient == -1 then
self:turnRight()
elseif o == -2 and self.orient == 1 then
self:turnLeft()
elseif o == 2 and self.orient == -1 then
self:turnLeft()
elseif o == 2 and self.orient == 1 then
self:turnRight()
end
return true
end
function SmartMove:facing()
local pos = { x = self.posX, z = self.posZ, y = self.posY }
if self.orient == -2 then
pos.z = pos.z - 1
elseif self.orient == 2 then
pos.z = pos.z + 1
elseif self.orient == 1 then
pos.x = pos.x + 1
elseif self.orient == -1 then
pos.x = pos.x - 1
end
return pos
end
function SmartMove:moveToX(x)
local moved = false
if self.posX ~= x then
local direction
if self.posX < x then
direction = 1
else
direction = -1
end
while self.posX ~= x and self:advance(direction) do
moved = true
end
end
return self.posX == x, moved
end
function SmartMove:moveToZ(z)
local moved = false
local direction
if self.posZ ~= z then
if self.posZ < z then
direction = 2
else
direction = -2
end
while self.posZ ~= z and self:advance(direction) do
moved = true
end
end
return self.posZ == z, moved
end
function SmartMove:moveToY(y)
local moved = false
local direction
if self.posY ~= y then
if self.posY < y then
direction = 1
else
direction = -1
end
while self.posY ~= y and self:_climb(direction) do
moved = true
end
end
return self.posY == y, moved
end
function SmartMove:moveToXZ(x, z)
-- first try X
local resultX, movedX = self:moveToX(x)
-- then try Z
local _, movedZ = self:moveToZ(z)
-- if X failed but we moved Z, we can try X again (might be unblocked now)
if not resultX and movedZ then
local innerResult, innerMoved = self:moveToXZ(x, z)
return innerResult, (innerMoved or movedX or movedZ)
end
-- successful if we ended up where we wanted to be
return (self.posZ == z and self.posX == x), (movedX or movedZ)
end
function SmartMove:moveToXZY(x, z, y)
-- try horizontal movement first
local resultXZ, movedXZ = self:moveToXZ(x, z)
-- now climb
local _, movedY = self:moveToY(y)
-- if XZ failed but we moved Y, we can try XZ again (might be unblocked now)
if not resultXZ and movedY then
local innerResult, innerMoved = self:moveToXZY(x, z, y)
return innerResult, (innerMoved or movedXZ or movedY)
end
-- successful if we ended up where we wanted to be
return (self.posZ == z and self.posX == x and self.posY == y), (movedXZ or movedY)
end
function SmartMove:distanceFromStart()
return math.abs(self.posX) + math.abs(self.posY) + math.abs(self.posZ)
end
function SmartMove:findInventory(strafeDirection, maxBlocks, dontCheckCurrentSpot, minimumInventorySize)
minimumInventorySize = minimumInventorySize or 1
if not dontCheckCurrentSpot then
local invSize = ic.getInventorySize(sides.bottom);
if invSize ~= nil and invSize >= minimumInventorySize then
return invSize
end
end
local wasOrient = self.orient
local wasX = self.posX
local wasZ = self.posZ
self:faceDirection(strafeDirection)
local moved = 0
local invSize
while moved < maxBlocks do
if not self:forward() then
break
end
moved = moved + 1
invSize = ic.getInventorySize(sides.bottom);
if invSize ~= nil and invSize >= minimumInventorySize then
break
end
end
if invSize == nil or invSize < minimumInventorySize then
invSize = nil
self:moveToXZ(wasX, wasZ)
end
self:faceDirection(wasOrient)
return invSize
end
function smartmove.new(o)
o = o or {}
setmetatable(o, { __index = SmartMove })
o.posX = 0
o.posZ = 0
o.posY = 0
o.orient = 1
o.moveTimeout = o.moveTimeout or 0
-- things we actually need
o.robot = o.robot or require("robot")
ic = c.inventory_controller
return o
end
return smartmove