-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collective.rb
1341 lines (1054 loc) · 25.1 KB
/
Collective.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
class Collective
def initialize
@ants = []
@safe_count = 0
@assembly_count = 0
@do_reassemble = true
@incomplete_count = 0
evade_init
end
include Evasion
def add a
@ants << a
end
def square
@ants[0].square
end
def size
@ants.length
end
def filled?
size == fullsize
end
#
# Order the given ant to the specified position within the collective.
# If no position given, add to the end of the collective.
#
def rally ant, count = nil
count = size() -1 if count.nil?
unless ant.set_order( leader.square, :ASSEMBLE, relpos(count) )
# Can't reach assembly location; give up
disband
end
end
def to_s
"collective [#{ @ants.join(", ") }]"
end
def leader? a
@ants[0] == a
end
def move dir = nil
# For ant compatibility
unless dir.nil?
move_intern dir
return
end
catch :done do
return if leader.moved?
test_incomplete
test_assembly
reassemble unless assembled?
return if evading
test_safe
move2
end
end
def remove a
is_leader = leader? a
@ants.delete a
if is_leader
$logger.info "Removing leader."
@ants.each do |b|
b.remove_target_from_order a
end
end
if size == 1
catch (:done) do
disband
end
else
@do_reassemble = true
end
end
#
# Break up collective and let the ants go
# their individual way
#
def disband
$logger.info "Disbanding #{ self }"
leader = nil
@ants.each do |a|
if leader.nil?
leader = a
a.clear_orders
a.collective = nil
else
a.collective = nil
a.remove_target_from_order leader
end
end
@ants = []
# Collective doesn't exist any more. Skip any other statements
$logger.info "Doing throw because collective disbanded."
throw :done
end
def assembled? side_effect = true
return false unless filled?
count = 0
okay = true
@ants.each do |a|
if in_location? a, count
# Note: following is a side effect.
# For the logic, this is the best place to put it.
if side_effect
#a.clear_orders if a.orders?
a.clear_order :ASSEMBLE
a.evade_reset if a.evading?
end
else
okay = false
# Don't break here; if-block needs to be done for all members
end
count += 1
end
okay
end
def add_recruit a
return if assembled? false
if filled?
# Check if new recruit is nearer to the leader than one
# of the current members
dista = Distance.get leader, a
# Pick best member to replace
bestn = nil
bestdist = nil
max = size() -1
(1..max).each do |n|
member = @ants[n]
next if in_location? member, n
distn = Distance.get leader, member
if dista.dist < distn.dist
if bestdist.nil? or distn.dist < bestdist
bestn = n
bestdist = distn.dist
end
end
end
unless bestn.nil?
member = @ants[bestn]
$logger.info "Collective #{ leader.to_s } replacing assembling member #{ member.to_s } n #{ bestn }, dist #{ bestdist }, with #{ a.to_s }, dist #{ dista.dist }"
# Replace current member with new ant
member.clear_orders
member.set_collective nil
a.clear_orders
@ants[bestn] = a
a.set_collective self
rally a
end
return assemble
else
add a
a.set_collective self
rally a
return assemble
end
end
###########################
# Top-level strategies
###########################
def self.recruit ant
# recruit near neighbours for a collective
if ant.ai.defensive?
friend_distance = 10
else
friend_distance = 20
end
recruits = ant.neighbor_friends friend_distance
recruits.delete_if { |a| a.collective? }
# If there are enough, make the collective
if recruits.size > 0
recruits.each do |l|
ant.add_collective l, recruits.length
break if ant.collective_assembled?
end
$logger.info "Created #{ ant.collective.to_s}"
else
# If not enough close by, disband the collective
# These may then be used for other incomplete collectives
catch :done do
ant.collective.disband if ant.collective?
end
end
end
#
# Complete existing collectives first
#
def self.complete_collectives ai
ai.my_ants.each do |ant|
next unless ant.collective? and not ant.collective_leader?
coll = ant.collective
catch :done do
# Get rid of collectives if population too small
if ant.ai.my_ants.length < AntConfig::ASSEMBLE_LIMIT
coll.disband
end
next if coll.assembled?
if coll.complete? and coll.furthest_follower_distance < 5
$logger.info { "#{ coll } followers almost there; not re-recruiting" }
next
end
$logger.info "Completing #{ coll }."
recruit ant
end
end
end
#
# Assemble new collectives
#
def self.create_collectives ai
ai.my_ants.each do |ant|
next if ant.collective?
next unless ant.attacked?
#next if ant.has_order :RAZE
# Don't even think about assembling if not enough ants around
next if ant.ai.my_ants.length < AntConfig::ASSEMBLE_LIMIT
if ant.ai.defensive?
# If collective nearby, don't bother creating a new one
collective_near = false
ant.neighbor_friends( 10 ).each do |a|
if a.collective?
$logger.info "#{ ant.to_s } has collective nearby"
collective_near = true
break
end
end
next if collective_near
end
# If too close too an assembling collective,
# don't bother creating a new one
collective_near = false
ant.neighbor_friends( 3 ).each do |a|
if a.collective?
$logger.info "#{ ant.to_s } assembling collective too close "
collective_near = true
break
end
end
next if collective_near
catch :done do
recruit ant
end
end
end
def self.move_collectives ai
# Move collectives as a whole
ai.my_ants.each do |ant|
next unless ant.collective_leader?
ai.turn.check_maxed_out
$logger.info "Moving collective."
ant.move_collective
end
end
###########################
# End Top-level strategies
###########################
#
# Placeholder for assembly
#
def assemble
false
end
# Generate all possible movements of current collective
def all_moves harmless
# NOTE: harmless test disabled
harmless = false
moves = {}
if leader.moved? or next_to_enemy?
moves[ :STAY ] = @ants.collect { |a| a.pos }
return moves
end
lam = lambda do |dir|
# Test movement of collective
if can_pass? dir
moves[ dir ] = @ants.collect { |a| a.square.neighbor dir }
end
end
# Note that we don't bother with orientation so close to conflict
if harmless
$logger.info { "#{ self } attackers harmless; staying is not an option" }
else
moves[ :STAY ] = @ants.collect { |a| a.square }
end
lam.call :N
lam.call :E
lam.call :S
lam.call :W
if moves.empty?
$logger.info "We are stuck, apparently; staying anyway."
moves[ :STAY ] = square
end
$logger.info { "possible moves: #{ moves }" }
moves
end
#
# Determine best move in conflict between single collective2
# and multiple enemies.
#
# No other neighboring friends are taken into account.
#
# Pre: Collective must be assembled.
#
def analyze_attack()
in_enemies = []
leader.enemies_in_view.each do |e|
adist = Distance.get( leader.pos, e.pos)
break unless adist.in_danger?
in_enemies << e
end
$logger.info { "Enemies in danger distance: #{ in_enemies }" }
return false if in_enemies.empty?
harmless, enemies, guess = Analyze.guess_enemy_moves in_enemies, leader.pos
return false if guess.length == 0
moves = all_moves harmless
return false if moves.length == 0
# Init the cache
Analyze.init_hits_cache [[self, moves ]], guess
best_moves = Analyze.determine_best_move guess, [ self ]
if best_moves.nil?
# Let the old logic handle it.
return false
elsif best_moves.length == 1
# Only one option
dir = Distance.new(self.pos, best_moves[0][0]).dir
return dir
else
# Multiple options
# Let the old logic handle it.
return false
end
# NOTE: this currently not reached due to returns in previous block
if false
# Select a move which brings you closer to the (first) enemy
best_dist = nil
best_dir2 = nil
best_dir.each do |dir|
d = Distance.get moves[dir][0], enemies[0]
if best_dist.nil? or best_dist > d.dist
best_dist = d.dist
best_dir2 = dir
end
end
$logger.info { "Selection direction #{ best_dir2 }" }
best_dir2
end
end
def complete?
size == fullsize
end
# Pre: collective complete
def furthest_follower_distance
leader = @ants[0]
#$logger.info " ants: #{ @ants }"
found_dist = nil
@ants[1..-1].each do |ant|
d = Distance.get leader, ant
if found_dist.nil? or d.dist > found_dist
found_dist = d.dist
end
end
found_dist
end
#
# Ant compatibility
#
def id; leader.id; end
def row; leader.row; end
def col; leader.col; end
def pos; leader.pos; end
def moved?; leader.moved?; end
def enemies_in_view; leader.enemies_in_view; end
def closest_enemy; leader.closest_enemy; end
def move_to sq
d = Distance.get leader.square, sq
if not orient d.longest_dir
move_intern d.dir
end
end
def next_to_enemy?
@ants.each do |ant|
return true if ant.next_to_enemy?
end
false
end
def can_pass? dir, water_only = false
return true if dir == :STAY
list = pass_check(dir)
return false if list.empty?
ok = true
list.each do |n|
a = @ants[n]
next if a.nil? # TODO: if ant is missing, can we still pass?
next unless in_location? a, n
#if water_only
# ok =false and break if a.square.neighbor(dir).water?
#else
ok =false and break unless a.can_pass?( dir, !water_only )
#end
end
ok
end
def disband_if_stuck
if assembled?
# Disband if stuck
if all_moves( false).empty?
$logger.info { "collective #{ ant.collective } stuck; disbanding" }
disband
return true
end
end
false
end
private
def leader
@ants[0]
end
def move_intern dir
return false unless can_pass? dir
order dir
true
end
def can_assemble?
sq = @ants[0].square
# Check presence of foreign member on given square
(1..fullsize).each do |n|
sq_rel = sq.rel( relpos(n) )
return false unless ( sq_rel ).land?
if @ants[n].nil? or !in_location? @ants[n], n
return false if sq_rel.ant?
end
end
return true
end
def order dir
if dir == :STAY
@ants.each {|a| a.stay }
return true
end
move_list(dir).each do |n|
a = @ants[n]
next if a.nil?
next unless in_location? a, n
a.move dir
end
# Actually, you would have to check if ants orders
# were accepted. So following line is a little lie.
true
end
def hold_ground dist
$logger.info "#{ self.to_s} holding ground"
# Allow sideways movement for better conflict placement
dir = dist.shortest_dir
if dir.nil?
stay
else
$logger.info "#{ self.to_s } placement to #{ dir }."
# Note: don't do evasion here, we're holding ground
# and don't want to move away
end
throw :done
end
def stay_away enemy, dist
if dist.in_peril?
# retreat if too close for comfort
dir = dist.invert.dir
if enemy.straight_line? and not enemy.advancing? leader.pos
# possibly ignoring you; change direction
$logger.info "Evading ignorer"
dir = left dir
end
else
hold_ground dist
end
dir
end
def move2
dist = attack_distance
$logger.info "#{ self.to_s } dist: #{ dist.to_s }"
if dist and dist.in_view?
# It is possible to approach an anthill completely and
# be right next to the emerging enemy ants. Following ensures
# that the collective will not try to evade.
if dist.dist <= 2
stay
throw :done
end
# Conflict; enemy is in view range
enemy = leader.closest_enemy
unless enemy
# Safefuard; Following problem has occured more than once
$logger.info "WARNING: attack_distance defined but no closest enemy present."
return
end
dir = nil
if !assembled?
$logger.info "#{ self.to_s } not assembled"
# If followers are close, don't move
if size == fullsize and furthest_follower_distance < 3
$logger.info "#{ self.to_s } almost assembled. waiting"
stay
return
end
if not leader.has_order :DEFEND_HILL
dir = stay_away enemy, dist
else
# Advance up to peril distance
hold_ground dist if dist.in_peril?
end
else
if false
# Analyze now done in collective Phase
dir = analyze_attack
$logger.info { "analyze_attack returned #{dir}" }
unless dir === false
if dir == :STAY
stay
return
else
# Note that evading is not taken into account
# It is excluded in analyze_attack with test of can_pass?
move_intern dir
return
end
end
end
dir = dist.attack_dir
$logger.info "Attack dir #{ leader.to_s }: #{ dir }"
# TODO: changing orientation can also be caught by twitchers
# eg. if two twitchers alternate in being the closest
return if orient dist.longest_dir
# If there is only one enemy, attack always.
enemies = leader.enemies_in_view
if enemies.length() > 1 and leader.ai.defensive?
if enemies.length > size() -1 and not leader.has_order :DEFEND_HILL
$logger.info "#{ self } too many enemies"
dir = stay_away enemy, dist
else
# Advance up to peril distance
hold_ground dist if dist.in_peril?
end
else
if enemy.straight_line? and not enemy.advancing? leader.pos
# This is an ant ignoring you and moving in a fixed direction
# Don't bother chasing if it's not moving toward you
$logger.info "Not chasing straight liner. #{ enemy.to_s } going #{ enemy.dir }"
# for good measure, move in the opposite direction of
# the enemy ant; there may be more coming.
# BAD IDEA; better to concentrate on next goal in next turn
#move_intern reverse( enemy.dir )
# TODO: following is wasteful if there is only
# one enemy; find a better solution
stay
throw :done
# TODO: Perhaps concentrate on the next closest ant
end
if enemy.twitch?
# break the twitch, otherwise we'll be twitching in unison forever
$logger.info "Breaking the twitch."
# Just plain attack
dir = dist.longest_dir
end
if dist.in_peril? and not dist.in_danger?
$logger.info "In peril"
# Enemy is now two squares away from attack distance.
# With an attacking enemy, now is a bad time to advance.
# Skip a turn so we can hit with extra force the next turn.
if enemy.advancing? leader.pos
hold_ground dist
end
end
end
end
if !move_intern dir
# Actual dir will be different
# Collectives don't evade any more
# TODO: Find a way for pathfinder to work with collectives
#dir = evade dir
random_move
end
else
$logger.info "#{ leader.to_s } no attacker"
unless assembled?
check_assembly
if !can_assemble?
# Location is not good, move away
random_move
else
# Allow foraging if possible
unless leader.find_order :FORAGE
# Otherwise, if not assembled yet
# wait for the missing ants to join
stay
end
end
throw :done
end
if leader.first_order :RAZE
# This is a special case, using ant orders
# to move collectives.
o = leader.get_first_order
to = o.handle_liaison( leader.square, leader.ai )
d = Distance.get( leader, to )
if d.in_view?
$logger.info { "Moving #{ self } to raze target #{ to}, dir #{ d.dir}" }
move_intern d.dir
throw :done
end
end
if !leader.ai.defensive?
# We're in place but not attacked.
# go pick a fight if possible
$logger.info "picking a fight"
d = nil
enemy = leader.closest_enemy
d = Distance.get( leader, enemy ) unless enemy.nil?
# If more or less close, go for it
if d and d.dist < AntConfig::FIGHT_DISTANCE
if enemy.straight_line? and not enemy.advancing? leader.pos
$logger.info "Not chasing straight liner 2."
move_intern d.invert.dir
throw :done
end
# Don't disband when not threatened
@safe_count = 0
return if orient d.longest_dir
unless move_intern d.dir
# Collectives don't evade any more
# TODO: Find a way for pathfinder to work with collectives
#dir = evade d.dir
random_move
end
end
end
end
end
def stay
@ants.each do |a|
next if a.nil?
next if a.moved?
next if a.orders?
a.stay
end
end
def in_location? a, count
return true if count == 0
if a.abspos.nil?
a.abspos = leader.square.rel( relpos( count) )
end
# NOTE: the from-square of the ant is used!
a.square == a.abspos
end
#
# members may have drifted. Ensure that they are in the right place
#
def check_assembly
ok = true
return false if leader.nil?
count = 1
@ants.clone.each do |a|
next if a === leader
unless a.nil? or a.orders?
unless in_location? a, count
rally a, count
ok = false
end
end
count += 1
end
ok
end
#
# Do a forced reassembly, if the constituency of a collective
# has changed
#
def reassemble
return unless @do_reassemble
@do_reassemble = false
leader = nil
disband and return if size == 1
count = 1
@ants.clone.each do |a|
if leader.nil?
leader = a
leader.clear_orders
next
end
if !in_location? a, count
rally a, count
end
count += 1
end
@assembly_count = 0
end
def test_incomplete
if size < fullsize
@incomplete_count += 1
else
@incomplete_count = 0
end
if @incomplete_count > AntConfig::INCOMPLETE_LIMIT
disband
end
end
def test_assembly
$logger.info "called"
if filled? and assembled? false
@assembly_count = 0
else
@assembly_count += 1
end
if @assembly_count > AntConfig::ASSEMBLY_LIMIT
$logger.info "#{ self }- assembly taking too long"
disband
end
end
def test_safe
tmp = false
@ants.each do |a|
return if a.has_order :ASSEMBLE
if a.attacked?
tmp = true
break
end
end
if tmp
@safe_count = 0
else
@safe_count += 1
end
if @safe_count > AntConfig::SAFE_LIMIT
$logger.info "We're safe"
disband
end
end
def attack_distance
# Do from leader only
ret = nil
if leader.attacked?
ret = leader.attack_distance
end
ret
end
def random_move
$logger.info "Doing random move."
moves = [ :N, :E, :S, :W, :N, :E, :S, :W ]
#
# Given random move may be not passable.
# Following searches for the next passable direction.
# If not found, give up
#
moves[rand(4),4].each do |dir|
return if move_intern dir
end
# Can not move at all - give up
disband
end
end
class Collective4 < Collective
# order of ant movement depends on where they are
# within the collective
@@move_order = {
:N => [0,1,2,3],
:E => [1,3,0,2],
:S => [2,3,0,1],
:W => [0,2,1,3]
}
def initialize
super
end
def fullsize; 4; end
def relpos count
[ count/2 , count % 2 ]
end
def pass_check dir
#$logger.info { "dir #{ dir }" }
unless [:N, :E, :S, :W].include? dir
$logger.info { "ERROR: dir #{ dir } not valid" }
return []
end
@@move_order[dir][0,2]
end