-
Notifications
You must be signed in to change notification settings - Fork 0
/
Harvesters.rb
229 lines (177 loc) · 5.07 KB
/
Harvesters.rb
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
class Harvesters
def initialize rows, cols
@dist = Distance.view_square_dist
$logger.info { "view square dist: #{ @dist }" }
rowdim = rows/@dist
rowdim += 1 unless rows % @dist == 0
coldim = cols/@dist
coldim += 1 unless cols % @dist == 0
@arr = Array.new( rowdim ) do |row|
row = Array.new coldim
end
end
def to_s
"Harvesters (rows, cols, dist) = ( #{ @arr.length }, #{ @arr[0].length }, #{ @dist } )"
end
private
def move_line_right r,c, rel
return if rel[1] == 0
rrel = rel[0]
rnorm = norm_r( r + rrel )
( rel[1] -1).downto( 0 ) do |crel|
cnorm = norm_c( c + crel)
thisant = @arr[ rnorm][ cnorm ]
thisant.change_order thisant.ai.map[ rnorm*@dist][ norm_c(cnorm + 1) *@dist], :HARVEST
@arr[ rnorm][ norm_c(cnorm + 1) ] = @arr[ rnorm][ cnorm ]
@arr[ rnorm][ cnorm ] = nil
end
end
def move_line_left r,c, rel
return if rel[1] == 0
rrel = rel[0]
rnorm = norm_r( r + rrel )
( rel[1] +1).upto( 0 ) do |crel|
cnorm = norm_c( c + crel)
thisant = @arr[ rnorm][ cnorm ]
thisant.change_order thisant.ai.map[ rnorm*@dist][ norm_c(cnorm - 1) *@dist], :HARVEST
@arr[ rnorm][ norm_c(cnorm - 1) ] = @arr[ rnorm][ cnorm ]
@arr[ rnorm][ cnorm ] = nil
end
end
def move_line_up r,c, rel
return if rel[0] == 0
crel = rel[1]
cnorm = norm_c( c + crel )
( rel[0] + 1 ).upto(0) do |rrel|
rnorm = norm_r( r + rrel)
thisant = @arr[ rnorm][ cnorm ]
thisant.change_order thisant.ai.map[ norm_r(rnorm -1)*@dist][ cnorm*@dist], :HARVEST
@arr[ norm_r( rnorm -1) ][ cnorm ] = @arr[ rnorm][ cnorm ]
@arr[ rnorm][ cnorm ] = nil
end
end
def move_line_down r,c, rel
return if rel[0] == 0
crel = rel[1]
cnorm = norm_c( c + crel )
( rel[0] -1 ).downto(0) do |rrel|
rnorm = norm_r( r + rrel)
thisant = @arr[ rnorm][ cnorm ]
thisant.change_order thisant.ai.map[ norm_r(rnorm +1)*@dist][ cnorm*@dist], :HARVEST
@arr[ norm_r( rnorm +1) ][ cnorm ] = @arr[ rnorm][ cnorm ]
@arr[ rnorm][ cnorm ] = nil
end
end
def move_lines ant, r, c, rel
# Determine quadrant
if rel[0] < 0 and rel[1] >= 0
$logger.info { "quadrant topright" }
move_line_right r,c, rel
move_line_up r,c, [ rel[0], 0 ]
elsif rel[0] >= 0 and rel[1] > 0
$logger.info { "quadrant bottomright" }
move_line_down r,c, rel
move_line_right r,c, [0, rel[1] ]
elsif rel[0] > 0 and rel[1] <= 0
$logger.info { "quadrant bottomleft" }
move_line_left r,c, rel
move_line_down r,c, [ rel[0], 0 ]
elsif rel[0] <= 0 and rel[1] < 0
$logger.info { "quadrant topleft" }
move_line_up r,c, rel
move_line_left r,c, [0, rel[1] ]
end
# Finally, move the new recruit to the nearest place
if ant.set_order ant.ai.map[ r*@dist][ c*@dist], :HARVEST
@arr[r][c] = ant
end
end
public
def enlist ant
# Find nearest location in harvesters grid for given ant
r = norm_r (ant.row*1.0/@dist).round
c = norm_c (ant.col*1.0/@dist).round
$logger.info { "Setting #{ ant.to_s } to a spot at ( #{ r}, #{c} )" }
if @arr[r][c].nil?
$logger.info { "Found #{ ant.to_s } a spot at ( #{ r}, #{c} )" }
if ant.set_order ant.ai.map[ r*@dist][ c*@dist], :HARVEST
@arr[r][c] = ant
end
else
$logger.info { "Harvester spot ( #{ r}, #{c} ) occupied." }
# NOTE: until paths are sorted out, we allow an ant to occupy
# the closest harvest location
return
# TODO: enable following again when paths work and are understood
rel = find_location r,c
unless rel.nil?
$logger.info { "Relative location( #{ rel[0] }, #{ rel[1] } ) is unoccupied." }
move_lines ant, r, c, rel
end
end
end
def remove ant
o = ant.find_order :HARVEST
if o
$logger.info { "Removing ant #{ ant.to_s } from harvesters." }
@arr[ o.square.row/@dist ][ o.square.col/@dist ] = nil
else
$logger.info { "Ant #{ ant.to_s } was not harvester." }
end
end
def rows
@arr.length
end
def cols
@arr[0].length
end
private
def norm_r r
(r + rows ) % rows
end
def norm_c c
(c + cols ) % cols
end
def check_spot r,c, roffs, coffs
rrel = norm_r( r + roffs )
crel = norm_c( c + coffs )
if @arr[ rrel ][ crel ].nil?
# Found a spot
# return relative position
throw:done, [ roffs, coffs ]
end
end
#
# Find the closest empty position for a new recruit
#
def find_location r,c
offset = nil
radius = 1
offset = catch :done do
# TODO: fix this loop to end when entire array has been searched
diameter = 2*radius + 1
while diameter <= rows or diameter <= cols
# Start from 12 o'clock and move clockwise
0.upto(radius) do |n|
check_spot r, c, -radius, n
end if diameter <= cols
(-radius+1).upto(radius).each do |n|
check_spot r, c, n, radius
end if diameter <= rows
( radius -1).downto( -radius ) do |n|
check_spot r, c, radius, n
end if diameter <= cols
( radius - 1).downto( -radius ) do |n|
check_spot r, c, n, -radius
end if diameter <= rows
( -radius + 1).upto( -1 ) do |n|
check_spot r, c, -radius, n
end if diameter <= cols
# End loop
radius += 1
diameter = 2*radius + 1
end
end
offset
end
end