Skip to content

Commit

Permalink
attack the cloest enemy (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaudeZsb authored Sep 13, 2023
1 parent ec0e12e commit c97b2a2
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/contracts/src/systems/PieceDecisionMakeSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,24 @@ contract PieceDecisionMakeSystem is System {

function exploreAttack(RTPiece[] memory _pieces, uint256 _index) internal returns (uint256 action) {
uint256 length = _pieces.length;
PriorityQueue memory pq = PQ.New(length);
RTPiece memory attacker = _pieces[_index];
for (uint256 i; i < length; ++i) {
RTPiece memory enemy = _pieces[i];
if (enemy.health == 0 || enemy.owner == attacker.owner) {
continue;
}
if (Coord.distance(attacker.x, attacker.y, enemy.x, enemy.y) <= attacker.range) {
console.log(" attack piece %x at (%d,%d)", uint256(enemy.id), enemy.x, enemy.y);
return PieceActionLib.generateAttackAction(_index, i);
uint256 dist = Coord.distance(attacker.x, attacker.y, enemy.x, enemy.y);
if (dist <= attacker.range) {
console.log(" attackable piece %x, distance %d", uint256(enemy.id), dist);
pq.AddTask(i, dist);
}
}
if (!pq.IsEmpty()) {
uint256 target = pq.PopTask();
console.log(" attack piece %x", uint256(_pieces[target].id));
return PieceActionLib.generateAttackAction(_index, target);
}
console.log(" no enemy in attack range");
}

Expand Down

0 comments on commit c97b2a2

Please sign in to comment.