-
Notifications
You must be signed in to change notification settings - Fork 0
/
coord_move.lua
158 lines (127 loc) · 3.57 KB
/
coord_move.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
function generic_goto( t, map, move_fn, xf, yf, zf, x, y, z )
DStarLite( {xf, yf, zf, 0}, {x, y, z, 0}, map, function( v, cost, i )
return move_fn( t, v, cost, i )
end)
return true
end
function gocoord( t, v, opts )
opts = opts or {}
local state = t( 'getState' )
local xf, yf, zf = state.x, state.y, state.z
local move_fn = opts.move_fn or CoordMove
return generic_goto( t, nil, move_fn, xf, yf, zf, v[1], v[2], v[3] )
end
function retrymove( t, v, opts )
opts = opts or {}
opts.move_fn = function( ... )
if opts.retry then
table.insert( arg, opts.retry )
return RetryCoordMove( unpack( arg ) )
end
return CoordMove( unpack( arg ) )
end
return gocoord( t, v, opts )
end
function gostate( t, to, opts )
local ret = gocoord( t, stateToVert( to ), opts )
faceDirection( t, to.dir )
return ret
end
function _faceDirection( t, d, curDir )
-- frame shift so that turtle is facing 0 (north). Delta is the turn direction and count
-- Also, math.mod() is different than mod operator - math.mod handles negatives
local delta = d - curDir
--print( 'face ', d, 'from', curDir )
if delta == 0 then
return
end
local cmd = nil
if math.abs( delta ) == 3 then
delta = delta / -3
end
if delta == 0 then return
elseif delta > 0 then cmd = 'turnRight'
else cmd = 'turnLeft'
end
--print( 'turning: '..cmd )
local ccw = 1
if delta < 0 then ccw = -1 end
for i = ccw, delta, ccw do
t( cmd )
end
end
function faceCoord( t, v )
local state = t( 'getState' )
local dx, dy = v[1], v[2]
local dir = dx + dy - 1 * math.abs( dy )
dir = math.mod( dir + 4, 4 )
if dx == 0 and dy == 0 then
return
end
_faceDirection( t, dir, state.dir )
end
function CoordMoveInstruct( t, v, cost, i )
local state = t( 'getState' )
local dx, dy, dz = v[1], v[2], v[3]
local cmd = 'forward'
if dx == 0 and dy == 0 and dz == 0 then
-- DStarLite sometimes says to move to the current location. I may or may not change that.
return nil
end
if dx ~= 0 or dy ~= 0 then
faceCoord( t, v )
end
if dz ~= 0 then
if dz > 0 then cmd = 'up'
else cmd = 'down'
end
end
return cmd
end
function CoordMove( t, v, cost, i )
local cmd = CoordMoveInstruct( t, v, cost, i )
if cmd == nil then return cost end
if not t( cmd ) then
return 1 / 0
end
return cost
end
function RetryCoordMove( t, v, cost, i, retry )
local cmd = CoordMoveInstruct( t, v, cost, i )
if cmd == nil then return cost end
if not t( cmd ) then
if not retry( t, cmd ) then
return 1 / 0
end
if not t( cmd ) then
return 1 / 0
end
return cost
end
return cost
end
function MkCoordMover( mover )
return function( t, v, cost, i )
local cmd = CoordMoveInstruct( t, v, cost, i )
if cmd == nil then return cost end
if not mover( cmd ) then
return 1 / 0
end
return cost
end
end
function DriveTo( t, v, cost, i )
local state = t( 'getState' )
local dx, dy = v[1], v[2]
local dir = dx + dy - 1 * math.abs( dy )
dir = math.mod( dir + 4, 4 )
if dx == 0 and dy == 0 then
-- DStarLite sometimes says to move to the current location. I may or may not change that.
return cost
end
_faceDirection( t, dir, state.dir )
if not drive( t, 1 ) then
return 1 / 0
end
return cost
end