-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseStrategy.rb
350 lines (277 loc) · 7.56 KB
/
BaseStrategy.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
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
class BaseStrategy
# Default implementation does nothing
def default_move ai
end
def self.target_ants ants, from_r, paths, all_ants
# Now, determine which ants are in the found regions
antlist = []
ants.each do |ant|
unless all_ants
next if ant.collective?
end
paths.each do |path|
if path.length == 0
antlist << ant if from_r == ant.square.region
else
antlist << ant if path[-1] == ant.square.region
end
end
end
$logger.info {
"target_ants found: " + antlist.join(", ")
}
antlist.uniq
end
#
# Determine which ants are within a reasonable
# striking distance of given square
#
def self.nearby_ants_region sq, ants, all_ants = false, max_length = nil, do_search = false
$logger.info "called. max_length: #{ max_length}"
sq_ants = Region.ants_to_squares ants
antlist = []
# Note that the search is actually back to front, from food
# to ants. The distance of course is the same
paths = nil
result = $region.get_neighbors_sorted sq, ants, do_search, max_length
if result.length > 0
# Remove distance info
result.each { |l| antlist << l[0] }
end
antlist
end
def self.closest_ant_region sq, ai
antlist = nearby_ants_region sq, ai.my_ants
# Due to the tests on moved and collective, it is possible that
# the list is empty
return nil if antlist.nil? or antlist.length == 0
# Closest ant is first in list (list is sorted ascending by distance)
best_ant = antlist[0]
$logger.info { "best ant: #{ best_ant}" }
best_ant
end
def find_food ai
count = 0
ai.food.each do |l|
sq = ai.map[ l.row ][ l.col ]
unless l.should_forage?
$logger.info { "Skipping food search for #{ sq.to_s }" }
next
end
count += 1
if count > AntConfig::FOOD_LIMIT
$logger.info { "Hit limit for foraging; did #{ count - 1 }" }
break
end
l.reset
ai.turn.check_maxed_out
$timer.start( :closest_ant_region ) {
ant = ai.nearest_view l.square
unless ant.nil?
# Food is in view; check if still there
if l.square.food?
if ant.pos.neighbor? l.square
$logger.info { "#{ ant } right next to food #{ l.square }; just staying put." }
ant.stay
else
if ant.set_order sq, :FORAGE
l.add_ant ant
end
end
else
# TODO: Check if this is ever reached
$logger.info { "Food #{ l.square } gone." }
ai.food.remove [ l.square.row, l.square.col ]
end
end
}
end
end
def evade ai
$logger.info "=== Evade Phase ==="
ai.my_ants.each do |ant|
next if ant.moved?
ai.turn.check_maxed_out
ant.evading
end
end
#
# return true if move selected this turn; :STAY counts as a move
#
def move_neighbors list
ant = list[-1]
return false if ant.moved?
if ant.collective_assembled?
return false unless ant.collective.disband_if_stuck
end
return false if ant.collective_leader?
$ai.turn.check_maxed_out
move = ant.next_move
$logger.info { "#{ant } move #{ move } " }
unless [true, false].include? move
if ant.handle_orders false
$logger.info { "#{ ant } handle_orders succeeded" }
return true
end
# perhaps the other ant is moving in the opposite direction,
# or going nowhere in particular.
# If so, we can exchange the orders
ant2 = ant.square.neighbor(move).ant
if not ant2.nil? and ant2.mine? and not ant2.moved? and
( [ true, false].include? ant2.next_move or
ant2.next_move == reverse(move) )
$logger.info { "ant2 #{ ant2 } next move: #{ ant2.next_move }" }
if ant2.collective_assembled?
unless ant2.collective.disband_if_stuck
$logger.info "Not exchanging for collective members"
return false
end
end
if ant.orders? and ant2.orders? and
ant.orders[0] == ant2.orders[0]
$logger.info "Orders are the same; giving up"
return false
end
# Note that any sorts for both ants are now off by one
# This will be cumulative for multiple sorts
# We might have also reached our targets, you never know....
$logger.info { "Exchanging #{ ant } and #{ ant2}" }
ant.square, ant2.square = ant2.square, ant.square
ant.square.ant = ant
ant.evade_reset
ant.clear_next_move
ant.clear_targets_reached
ant2.square.ant = ant2
ant2.evade_reset
ant2.clear_next_move
ant2.clear_targets_reached
move2 = ant2.next_move
if move2 == move
$logger.info "#{ ant2 } wants to go back; not redoing"
# In fact, it could happen again this turn; refuse to budge
ant2.stay
return false
else
# Now, redo movement with new ant
# NOTE: recursive call; better watch stack depth
list.pop
list << ant2
$logger.info { "Redoing move with #{ ant2}" }
return move_neighbors list
end
else
# try anything
[ :N, :E, :S, :W ].each do |dir|
if ant.move dir, nil, false
$logger.info { "#{ ant } any move #{ dir } succeeded" }
return true
end
end
end
else
# If we are first mover, don't bother
return true if list.length == 1
# Move is open, try anything
[ :N, :E, :S, :W ].each do |dir|
if ant.move dir, nil, false
$logger.info { "#{ ant } move #{ dir } succeeded" }
return true
end
end
end
# No decent moves...
$logger.info { "#{ ant } fail" }
if $ai.turn.maxed_out?
ant.stay
$logger.info { "#{ ant } staying, maxed out" }
return true
end
moves = []
unless [true, false].include? move
# Continue in the order direction
moves << move
else
# select neighbor squares with unmoved ants
[ :N, :E, :S, :W ].each do |dir|
n = ant.square.neighbor( dir )
if n.land? and not n.moved_here? and n.ant? and n.ant.mine? and not n.ant.moved?
moves << dir
end
end
end
if moves.empty?
# give up
ant.stay
$logger.info { "#{ ant } staying, no moves" }
return true
end
done_dir = nil
moves.each do |dir|
ant2 = ant.square.neighbor( dir ).ant
next if ant2.nil? or list.include? ant2 # Avoid loops
$logger.info "Recursing with #{ ant2 } to #{ dir }"
if move_neighbors list + [ ant2 ]
done_dir = dir
break
end
end
# Try moving current ant again
if done_dir
if ant.move done_dir, nil, false
$logger.info { "#{ ant } move #{ done_dir } succeeded on retry" }
return true
end
end
false
end
def ant_orders ai
ai.my_ants.each do |ant|
next unless ant.orders?
# Following are needed, even if they also star in move_neighbors
next if ant.moved?
next if ant.collective_leader?
if not move_neighbors [ant]
$logger.info "move_neighbors top level fail"
# Perhaps this helps to alleviate the pain
# for the rest of the crowd.
unless ant.first_order :DEFEND or
ant.first_order :DEFEND_HILL
ant.clear_first_order
end
#$logger.info "Forcing handle_orders"
#ant.handle_orders true
end
end
end
#
# Determine which ants are being attacked
#
def check_attacked ai
ai.my_ants.each do |ant|
ant.check_attacked
end
end
def turn ai, do_orders = true, do_food = true
evade ai
find_food ai if do_food
$logger.info "=== Default Move Phase ==="
ai.my_ants.each do |ant|
next if ant.moved?
# DON'T EVER PUT FOLLOWING BACK!
# Ants need to be able to access default moves, even
# if they do have orders, in order to get the next border liaison.
#next if ant.orders?
next if ant.collective?
next if ant.harvesting?
#next if ant.orders?
ai.turn.check_maxed_out
default_move ant
end
if do_orders
ai.my_ants.each do |ant|
ant.clear_targets_reached
end
ant_orders ai
end
end
end