Skip to content

Commit

Permalink
fix: sold already removed unit on negative wealth
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfremium13 committed Feb 13, 2023
1 parent cae350e commit c421209
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion source/game_management/movemaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ def make_move(self, player: Player, all_players: typing.List[Player], quads: typ
min_pow_health = pow_health, unit
self.move_unit(player, unit, all_units, all_players, all_setls, quads, cfg)
overall_wealth -= unit.plan.cost / 10
if player.wealth + overall_wealth < 0:
if (player.wealth + overall_wealth < 0) and min_pow_health[1] in player.units:
player.wealth += min_pow_health[1].plan.cost
player.units.remove(min_pow_health[1])

Expand Down
18 changes: 18 additions & 0 deletions source/tests/test_movemaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,24 @@ def test_make_move_negative_wealth(self):
self.assertEqual(self.TEST_UNIT.plan.cost, self.TEST_PLAYER.wealth)
self.assertFalse(self.TEST_PLAYER.units)

@patch("source.game_management.movemaker.investigate_relic", lambda *args: None)
def test_make_move_negative_wealth_doesnt_remove_already_removed_units(self):
"""
Ensure that when an AI player would have negative wealth at the end of their turn, the unit will die in
movement, so we cannot sell on negative wealth after unit dies.
"""
# By making the test player defensive, we guarantee that the reason for attack is the other unit's faction.
self.TEST_PLAYER.ai_playstyle.attacking = AttackPlaystyle.DEFENSIVE
self.TEST_PLAYER.units[0] = self.TEST_UNIT_2
wealth_before_combat = self.TEST_PLAYER.wealth
infidel_player = Player("Inf", Faction.INFIDELS, 0, 0, [], [self.TEST_UNIT_3], [], set(), set())

self.movemaker.make_move(self.TEST_PLAYER, [self.TEST_PLAYER, infidel_player], self.QUADS, self.TEST_CONFIG,
False)

self.assertEqual(wealth_before_combat, self.TEST_PLAYER.wealth)
self.assertFalse(self.TEST_PLAYER.units)

def test_move_settler_unit_not_far_enough(self):
"""
Ensure that when a settler unit has not moved far enough away from its original settlement, it does not found a
Expand Down

0 comments on commit c421209

Please sign in to comment.