diff --git a/src/Battling/Models/BattleSide.hpp b/src/Battling/Models/BattleSide.hpp index 6f02c51..1f4b27a 100644 --- a/src/Battling/Models/BattleSide.hpp +++ b/src/Battling/Models/BattleSide.hpp @@ -47,18 +47,28 @@ namespace CreatureLib::Battling { void GetActiveScripts(std::vector& scripts) final; - void MarkSlotAsUnfillable(Creature* creature){ - for (uint8_t i = 0; i < _creaturesPerSide; i++){ - if (_creatures[i] == creature){ + uint8_t GetSideIndex() { return _index; } + uint8_t GetCreatureIndex(Creature* c) { + for (size_t i = 0; i < _creatures.size(); i++) { + if (c == _creatures[i]) + return i; + } + throw CreatureException("Unable to find creature on field."); + } + + void MarkSlotAsUnfillable(Creature* creature) { + for (uint8_t i = 0; i < _creaturesPerSide; i++) { + if (_creatures[i] == creature) { _fillableSlots[i] = false; return; } } } - bool IsDefeated(){ - for (auto b: _fillableSlots){ - if (b) return false; + bool IsDefeated() { + for (auto b : _fillableSlots) { + if (b) + return false; } return true; } diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index aeadebc..95e5289 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -99,8 +99,10 @@ void Battling::Creature::Damage(uint32_t damage, Battling::DamageSource source) // HOOK: On Damage __CurrentHealth -= damage; - if (IsFainted()){ - _side->MarkSlotAsUnfillable(this); + if (IsFainted() && damage > 0) { + if (!_battle->CanSlotBeFilled(_side->GetSideIndex(), _side->GetCreatureIndex(this))) { + _side->MarkSlotAsUnfillable(this); + } _battle->ValidateBattleState(); } } diff --git a/tests/Integration/BattleIntegrations.cpp b/tests/Integration/BattleIntegrations.cpp index b3d2597..b1b8743 100644 --- a/tests/Integration/BattleIntegrations.cpp +++ b/tests/Integration/BattleIntegrations.cpp @@ -106,6 +106,37 @@ TEST_CASE("When creature is dealt enough damage, faint it and mark battle as end REQUIRE(battle.GetResult() == 0); } +TEST_CASE("When another creature is available on faint, make sure the battle hasn't ended", "[Integrations]") { + auto library = TestLibrary::Get(); + auto c1 = CreateCreature(library, "testSpecies1", 100).WithAttack("standard", AttackLearnMethod::Unknown)->Create(); + CreatureParty party1{c1}; + auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)}); + auto c2 = CreateCreature(library, "testSpecies1", 1).WithAttack("standard", AttackLearnMethod::Unknown)->Create(); + auto c3 = CreateCreature(library, "testSpecies1", 1).WithAttack("standard", AttackLearnMethod::Unknown)->Create(); + CreatureParty party2{c2, c3}; + auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)}); + auto battle = Battle(library, {battleParty1, battleParty2}); + + REQUIRE_FALSE(battle.HasEnded()); + + battle.SwitchCreature(0, 0, c1); + battle.SwitchCreature(1, 0, c2); + + REQUIRE_FALSE(battle.HasEnded()); + + battle.TrySetChoice(new AttackTurnChoice(c1, c1->GetAttacks()[0], CreatureIndex(1, 0))); + battle.TrySetChoice(new PassTurnChoice(c2)); + + REQUIRE_FALSE(battle.HasEnded()); + + battle.SwitchCreature(1, 0, c3); + + battle.TrySetChoice(new AttackTurnChoice(c1, c1->GetAttacks()[0], CreatureIndex(1, 0))); + battle.TrySetChoice(new PassTurnChoice(c3)); + + REQUIRE(battle.HasEnded()); + REQUIRE(battle.GetResult() == 0); +} #endif \ No newline at end of file