@@ -35,21 +35,22 @@ public void handleAttacks() {
3535
3636 for (Colony colony : antRepository .getColonies ()) {
3737 int attackerId = colony .getBaseAttackerId ();
38- runAttack (colony .getId (), colony .getBase ().getX (), colony .getBase ().getY (),
39- ConstConfigs .BASE_ATTACK_DAMAGE , ConstConfigs . BASE_MAX_ATTACK_DISTANCE , attackerId );
38+ runAttack (colony .getId (), colony .getBase ().getX (), colony .getBase ().getY (), ConstConfigs . BASE_ATTACK_DAMAGE ,
39+ ConstConfigs .BASE_MAX_ATTACK_DISTANCE , attackerId );
4040 }
4141
4242 for (Colony colony : antRepository .getColonies ()) {
4343 for (Ant ant : colony .getAnts ()) {
4444 if (ant .getAntType ().equals (AntType .SOLDIER ))
45- runAttack (colony .getId (), ant .getXPosition (), ant .getYPosition (),
46- ConstConfigs .ANT_ATTACK_DAMAGE , ConstConfigs . ANT_MAX_ATTACK_DISTANCE , ant .getId ());
45+ runAttack (colony .getId (), ant .getXPosition (), ant .getYPosition (), ConstConfigs . ANT_ATTACK_DAMAGE ,
46+ ConstConfigs .ANT_MAX_ATTACK_DISTANCE , ant .getId ());
4747 }
4848 }
4949 handleDeadAnts ();
5050 }
5151
52- private void runAttack (int colonyId , int fromXPosition , int fromYPosition , int damage , int maxDistance , int attackerId ) {
52+ private void runAttack (int colonyId , int fromXPosition , int fromYPosition , int damage , int maxDistance ,
53+ int attackerId ) {
5354 Cell [] cells = map .getAttackableCells (fromXPosition , fromYPosition , maxDistance );
5455 List <Ant > workers = new ArrayList <>();
5556 List <Ant > soldiers = new ArrayList <>();
@@ -60,7 +61,8 @@ private void runAttack(int colonyId, int fromXPosition, int fromYPosition, int d
6061 if (colony .getBaseHealth () > 0 ) {
6162 colony .decreaseBaseHealth (damage );
6263 int defenderId = colony .getBaseAttackerId ();
63- AttackSummary attackSummary = new AttackSummary (attackerId , defenderId , fromYPosition , fromXPosition , cell .getY (), cell .getX ());
64+ AttackSummary attackSummary = new AttackSummary (attackerId , defenderId , fromYPosition ,
65+ fromXPosition , cell .getY (), cell .getX ());
6466 attackSummaries .add (attackSummary );
6567 return ;
6668 }
@@ -87,10 +89,22 @@ private void runAttack(int colonyId, int fromXPosition, int fromYPosition, int d
8789
8890 private void runAttack (int fromXPosition , int fromYPosition , int damage , int attackerId , List <Ant > ants ) {
8991 // attackerId = negative attacker id --> base attack
90- int index = rand .nextInt (ants .size ());
92+ // int index = rand.nextInt(ants.size());
93+ if (ants .size () == 0 )
94+ return ;
95+ int index = 0 ;
96+ int minHealth = ants .get (0 ).getHealth ();
97+ // the ant with lower health
98+ for (int i = 0 ; i < ants .size (); i ++) {
99+ if (ants .get (i ).getHealth () < minHealth ) {
100+ index = i ;
101+ minHealth = ants .get (i ).getHealth ();
102+ }
103+ }
91104 Ant defender = ants .get (index );
92105 defender .decreaseHealth (damage );
93- AttackSummary attackSummary = new AttackSummary (attackerId , defender .getId (), fromYPosition , fromXPosition , defender .getYPosition (), defender .getXPosition ());
106+ AttackSummary attackSummary = new AttackSummary (attackerId , defender .getId (), fromYPosition , fromXPosition ,
107+ defender .getYPosition (), defender .getXPosition ());
94108 attackSummaries .add (attackSummary );
95109 }
96110
@@ -112,7 +126,8 @@ private void handleDeadAnts() {
112126 } else {
113127 if (ant .getCarryingResourceType () == ResourceType .NONE )
114128 map .addResource (ResourceType .BREAD ,
115- (int ) Math .ceil (ConstConfigs .RATE_DEATH_RESOURCE * ConstConfigs .GENERATE_WORKER_BREAD_AMOUNT ),
129+ (int ) Math
130+ .ceil (ConstConfigs .RATE_DEATH_RESOURCE * ConstConfigs .GENERATE_WORKER_BREAD_AMOUNT ),
116131 ant .getXPosition (), ant .getYPosition ());
117132 else if (ant .getCarryingResourceType () == ResourceType .BREAD )
118133 map .addResource (ResourceType .BREAD ,
@@ -121,7 +136,8 @@ else if (ant.getCarryingResourceType() == ResourceType.BREAD)
121136 ant .getXPosition (), ant .getYPosition ());
122137 else {
123138 map .addResource (ResourceType .BREAD ,
124- (int ) Math .ceil (ConstConfigs .RATE_DEATH_RESOURCE * ConstConfigs .GENERATE_WORKER_BREAD_AMOUNT ),
139+ (int ) Math
140+ .ceil (ConstConfigs .RATE_DEATH_RESOURCE * ConstConfigs .GENERATE_WORKER_BREAD_AMOUNT ),
125141 ant .getXPosition (), ant .getYPosition ());
126142 map .addResource (ResourceType .GRASS , ant .getCarryingResourceAmount (), ant .getXPosition (),
127143 ant .getYPosition ());
@@ -147,11 +163,10 @@ public List<AttackSummary> getNearByAttacks(int antId) {
147163 int antXPosition = ant .getXPosition ();
148164 int antYPosition = ant .getYPosition ();
149165 List <AttackSummary > attackDTOs = getAttackSummaries ().stream ()
150- .filter (x ->
151- map .getBorderlessManhattanDistance (x .src_col , x .src_row ,
152- antXPosition , antYPosition ) <= ConstConfigs .ANT_MAX_VIEW_DISTANCE ||
153- map .getBorderlessManhattanDistance (x .dst_col , x .dst_row ,
154- antXPosition , antYPosition ) <= ConstConfigs .ANT_MAX_VIEW_DISTANCE )
166+ .filter (x -> map .getBorderlessManhattanDistance (x .src_col , x .src_row , antXPosition ,
167+ antYPosition ) <= ConstConfigs .ANT_MAX_VIEW_DISTANCE
168+ || map .getBorderlessManhattanDistance (x .dst_col , x .dst_row , antXPosition ,
169+ antYPosition ) <= ConstConfigs .ANT_MAX_VIEW_DISTANCE )
155170 .collect (Collectors .toList ());
156171 return attackDTOs ;
157172 }
0 commit comments