Rework clearing battle from creatures.
continuous-integration/drone/push Build is passing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2020-12-28 15:10:49 +01:00
parent a284641b5a
commit 3742e07b03
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 47 additions and 4 deletions

View File

@ -85,5 +85,6 @@ export uint8_t CreatureLib_Battle_RegisterEventListener(Battle* p, void (*func)(
export const HistoryHolder* CreatureLib_Battle_GetHistory(Battle* p) { return &p->GetHistory(); } export const HistoryHolder* CreatureLib_Battle_GetHistory(Battle* p) { return &p->GetHistory(); }
export uint8_t CreatureLib_Battle_EndBattle(Battle* p) { Try(p->EndBattle()) } export uint8_t CreatureLib_Battle_EndBattle(Battle* p) { Try(p->EndBattle()) }
export uint8_t CreatureLib_Battle_ClearBattle(Battle* p) { Try(p->ClearBattle()) }
SIMPLE_GET_FUNC(Battle, GetLastTurnTimeMicroseconds, long); SIMPLE_GET_FUNC(Battle, GetLastTurnTimeMicroseconds, long);

View File

@ -48,7 +48,7 @@ namespace CreatureLib::Battling {
} }
} }
virtual ~Battle() = default; virtual ~Battle() { ClearBattle(); }
[[nodiscard]] const ArbUt::BorrowedPtr<const BattleLibrary>& GetLibrary() const noexcept; [[nodiscard]] const ArbUt::BorrowedPtr<const BattleLibrary>& GetLibrary() const noexcept;
[[nodiscard]] uint32_t GetCurrentTurn() const noexcept { return _currentTurn; } [[nodiscard]] uint32_t GetCurrentTurn() const noexcept { return _currentTurn; }
@ -117,13 +117,16 @@ namespace CreatureLib::Battling {
long GetLastTurnTimeMicroseconds() const noexcept { return _lastTurnTime; } long GetLastTurnTimeMicroseconds() const noexcept { return _lastTurnTime; }
void EndBattle() { void EndBattle() { this->_hasEnded = true; }
this->_hasEnded = true;
void ClearBattle() {
for (size_t i = 0; i < _numberOfSides; i++) { for (size_t i = 0; i < _numberOfSides; i++) {
auto side = _sides[i]; auto side = _sides[i];
for (auto c : side->GetCreatures()) { for (auto c : side->GetCreatures()) {
if (c.HasValue()) { if (c.HasValue()) {
c.GetValue()->ClearBattleData(); if (c.GetValue()->GetBattle() == this) {
c.GetValue()->ClearBattleData();
}
} }
} }
} }

View File

@ -334,4 +334,43 @@ TEST_CASE("Flee Battle") {
REQUIRE_FALSE(result.IsConclusiveResult()); REQUIRE_FALSE(result.IsConclusiveResult());
} }
TEST_CASE("Heal creature after battle") {
auto library = TestLibrary::Get();
auto c1 =
CreateCreature(library, "testSpecies1"_cnc, 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown).Create();
CreatureParty party1{c1};
auto battleParty1 = new BattleParty(&party1, {CreatureIndex(0, 0)});
auto c2 =
CreateCreature(library, "testSpecies1"_cnc, 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown).Create();
CreatureParty party2{c2};
auto battleParty2 = new BattleParty(&party2, {CreatureIndex(1, 0)});
auto battle = new Battle(library, {battleParty1, battleParty2});
std::vector<const EventData*> events;
battle->RegisterEventListener([&](const EventData* evt) mutable -> void { events.push_back(evt); });
REQUIRE_FALSE(battle->HasEnded());
battle->SwitchCreature(0, 0, c1);
battle->SwitchCreature(1, 0, c2);
REQUIRE_FALSE(battle->HasEnded());
REQUIRE(battle->TrySetChoice(new AttackTurnChoice(c1, c1->GetAttacks()[0].GetValue(), CreatureIndex(1, 0))));
REQUIRE(battle->TrySetChoice(new PassTurnChoice(c2)));
REQUIRE_FALSE(battle->HasEnded());
REQUIRE(c2->GetCurrentHealth() < c2->GetBoostedStat(Statistic::Health));
c2->Damage(c2->GetCurrentHealth(), DamageSource::AttackDamage);
REQUIRE(battle->HasEnded());
auto result = battle->GetResult();
REQUIRE(result.IsConclusiveResult());
REQUIRE(result.GetWinningSide() == 0);
delete battle;
c2->Heal(1000, true);
REQUIRE(c2->GetCurrentHealth() == c2->GetMaxHealth());
}
#endif #endif