Fixed battle ending after any faint.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Deukhoofd 2019-12-07 22:52:43 +01:00
parent f5aa168f1b
commit dc9296194e
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
3 changed files with 51 additions and 8 deletions

View File

@ -47,18 +47,28 @@ namespace CreatureLib::Battling {
void GetActiveScripts(std::vector<ScriptWrapper>& 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;
}

View File

@ -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();
}
}

View File

@ -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