Update to latest Arbutils.
continuous-integration/drone/push Build is passing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2020-12-13 12:15:40 +01:00
parent 2055837980
commit e642f374b9
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
25 changed files with 69 additions and 120 deletions

View File

@ -116,8 +116,8 @@ Standard: Cpp11
StatementMacros: StatementMacros:
- Q_UNUSED - Q_UNUSED
- QT_REQUIRE_VERSION - QT_REQUIRE_VERSION
- Assert - Ensure
- AssertNotNull - EnsureNotNull
- Try - Try
TabWidth: 8 TabWidth: 8
UseTab: Never UseTab: Never

View File

@ -1,7 +1,7 @@
#include "ChoiceQueue.hpp" #include "ChoiceQueue.hpp"
bool CreatureLib::Battling::ChoiceQueue::MoveCreatureChoiceNext(CreatureLib::Battling::Creature* creature) { bool CreatureLib::Battling::ChoiceQueue::MoveCreatureChoiceNext(CreatureLib::Battling::Creature* creature) {
AssertNotNull(creature) EnsureNotNull(creature)
// Find which index the creature choice is at. // Find which index the creature choice is at.
size_t choiceIndex = SIZE_MAX; size_t choiceIndex = SIZE_MAX;
for (size_t index = _current; index < _queue.size(); index++) { for (size_t index = _current; index < _queue.size(); index++) {

View File

@ -7,16 +7,14 @@
using namespace CreatureLib::Battling; using namespace CreatureLib::Battling;
void TurnHandler::RunTurn(ArbUt::BorrowedPtr<ChoiceQueue> queue) { void TurnHandler::RunTurn(ArbUt::BorrowedPtr<ChoiceQueue> queue) {
AssertNotNull(queue)
for (auto choice : queue->GetInnerQueue()) { for (auto choice : queue->GetInnerQueue()) {
HOOK(OnBeforeTurn, choice, choice.get()); HOOK(OnBeforeTurn, choice, choice.get());
} }
while (queue->HasNext()) { while (queue->HasNext()) {
auto item = queue->Dequeue(); auto item = queue->Dequeue();
AssertNotNull(item) EnsureNotNull(item)
AssertNotNull(item->GetUser()) Ensure(item->GetUser()->GetBattle().HasValue())
Assert(item->GetUser()->GetBattle().HasValue()) Ensure(item->GetUser()->GetBattleSide().HasValue())
Assert(item->GetUser()->GetBattleSide().HasValue())
auto index = (uint32_t)item->GetUser()->GetBattleSide().GetValue()->GetCreatureIndex(item->GetUser()); auto index = (uint32_t)item->GetUser()->GetBattleSide().GetValue()->GetCreatureIndex(item->GetUser());
try_creature(ExecuteChoice(item.get()), try_creature(ExecuteChoice(item.get()),
@ -30,13 +28,11 @@ void TurnHandler::RunTurn(ArbUt::BorrowedPtr<ChoiceQueue> queue) {
} }
void TurnHandler::ExecuteChoice(ArbUt::BorrowedPtr<BaseTurnChoice> choice) { void TurnHandler::ExecuteChoice(ArbUt::BorrowedPtr<BaseTurnChoice> choice) {
AssertNotNull(choice)
auto choiceKind = choice->GetKind(); auto choiceKind = choice->GetKind();
if (choiceKind == TurnChoiceKind::Pass) { if (choiceKind == TurnChoiceKind::Pass) {
return; return;
} }
auto user = choice->GetUser(); auto user = choice->GetUser();
AssertNotNull(user)
if (!user->GetBattle().HasValue()) if (!user->GetBattle().HasValue())
return; return;
auto battle = user->GetBattle().GetValue(); auto battle = user->GetBattle().GetValue();
@ -68,7 +64,6 @@ void TurnHandler::ExecuteChoice(ArbUt::BorrowedPtr<BaseTurnChoice> choice) {
} }
void TurnHandler::ExecuteAttackChoice(const ArbUt::BorrowedPtr<AttackTurnChoice>& choice) { void TurnHandler::ExecuteAttackChoice(const ArbUt::BorrowedPtr<AttackTurnChoice>& choice) {
AssertNotNull(choice)
auto battle = choice->GetUser()->GetBattle(); auto battle = choice->GetUser()->GetBattle();
auto attackName = choice->GetAttack()->GetAttack()->GetName(); auto attackName = choice->GetAttack()->GetAttack()->GetName();
HOOK(ChangeAttack, choice, choice.GetRaw(), &attackName); HOOK(ChangeAttack, choice, choice.GetRaw(), &attackName);
@ -78,7 +73,7 @@ void TurnHandler::ExecuteAttackChoice(const ArbUt::BorrowedPtr<AttackTurnChoice>
auto targetType = choice->GetAttack()->GetAttack()->GetTarget(); auto targetType = choice->GetAttack()->GetAttack()->GetTarget();
ArbUt::List<ArbUt::OptionalBorrowedPtr<Creature>> targets; ArbUt::List<ArbUt::OptionalBorrowedPtr<Creature>> targets;
Assert(battle.HasValue()); Ensure(battle.HasValue());
try_creature(targets = TargetResolver::ResolveTargets(choice->GetTarget(), targetType, battle.GetValue()); try_creature(targets = TargetResolver::ResolveTargets(choice->GetTarget(), targetType, battle.GetValue());
, "Exception during target determination"); , "Exception during target determination");
@ -124,12 +119,10 @@ void TurnHandler::ExecuteAttackChoice(const ArbUt::BorrowedPtr<AttackTurnChoice>
} }
void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::BorrowedPtr<Creature>& target) { void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::BorrowedPtr<Creature>& target) {
AssertNotNull(attack) EnsureNotNull(attack)
auto& user = attack->GetUser(); auto& user = attack->GetUser();
AssertNotNull(user)
AssertNotNull(target)
auto& battle = user->GetBattle(); auto& battle = user->GetBattle();
Assert(battle.HasValue()) Ensure(battle.HasValue())
if (battle.GetValue()->HasEnded()) if (battle.GetValue()->HasEnded())
return; return;
@ -155,18 +148,15 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::Bo
} }
auto& learnedAttack = attack->GetAttack(); auto& learnedAttack = attack->GetAttack();
AssertNotNull(learnedAttack);
auto& attackData = learnedAttack->GetAttack(); auto& attackData = learnedAttack->GetAttack();
AssertNotNull(attackData);
auto& library = battle.GetValue()->GetLibrary(); auto& library = battle.GetValue()->GetLibrary();
AssertNotNull(library)
auto& dmgLibrary = library->GetDamageLibrary(); auto& dmgLibrary = library->GetDamageLibrary();
auto& typeLibrary = library->GetTypeLibrary(); auto& typeLibrary = library->GetTypeLibrary();
auto& miscLibrary = library->GetMiscLibrary(); auto& miscLibrary = library->GetMiscLibrary();
AssertNotNull(dmgLibrary) EnsureNotNull(dmgLibrary)
AssertNotNull(typeLibrary) EnsureNotNull(typeLibrary)
AssertNotNull(miscLibrary) EnsureNotNull(miscLibrary)
auto hitIterator = attack->GetTargetIteratorBegin(target); auto hitIterator = attack->GetTargetIteratorBegin(target);
for (uint8_t hitIndex = 0; hitIndex < numberOfHits; hitIndex++) { for (uint8_t hitIndex = 0; hitIndex < numberOfHits; hitIndex++) {
@ -229,7 +219,7 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::Bo
hasSecondaryEffect = true; hasSecondaryEffect = true;
} else { } else {
auto random = battle.GetValue()->GetRandom(); auto random = battle.GetValue()->GetRandom();
AssertNotNull(random); EnsureNotNull(random);
hasSecondaryEffect = random->EffectChance(effect->GetChance(), attack, target.GetRaw()); hasSecondaryEffect = random->EffectChance(effect->GetChance(), attack, target.GetRaw());
} }
if (hasSecondaryEffect) { if (hasSecondaryEffect) {

View File

@ -15,8 +15,8 @@ public:
if (aKind == TurnChoiceKind::Attack) { if (aKind == TurnChoiceKind::Attack) {
auto aAttack = std::dynamic_pointer_cast<AttackTurnChoice>(a); auto aAttack = std::dynamic_pointer_cast<AttackTurnChoice>(a);
auto bAttack = std::dynamic_pointer_cast<AttackTurnChoice>(b); auto bAttack = std::dynamic_pointer_cast<AttackTurnChoice>(b);
AssertNotNull(aAttack); EnsureNotNull(aAttack);
AssertNotNull(bAttack); EnsureNotNull(bAttack);
auto aPriority = aAttack->GetPriority(); auto aPriority = aAttack->GetPriority();
auto bPriority = bAttack->GetPriority(); auto bPriority = bAttack->GetPriority();
if (aPriority != bPriority) if (aPriority != bPriority)
@ -24,8 +24,6 @@ public:
} }
auto aUser = a->GetUser(); auto aUser = a->GetUser();
auto bUser = b->GetUser(); auto bUser = b->GetUser();
AssertNotNull(aUser);
AssertNotNull(bUser);
auto aSpeed = aUser->GetBoostedStat(Library::Statistic::Speed); auto aSpeed = aUser->GetBoostedStat(Library::Statistic::Speed);
auto bSpeed = bUser->GetBoostedStat(Library::Statistic::Speed); auto bSpeed = bUser->GetBoostedStat(Library::Statistic::Speed);
if (aSpeed != bSpeed) if (aSpeed != bSpeed)
@ -37,10 +35,10 @@ public:
void TurnOrdering::OrderChoices(std::vector<std::shared_ptr<BaseTurnChoice>>& vec) { void TurnOrdering::OrderChoices(std::vector<std::shared_ptr<BaseTurnChoice>>& vec) {
for (const auto& item : vec) { for (const auto& item : vec) {
AssertNotNull(item); EnsureNotNull(item);
if (item->GetKind() == TurnChoiceKind::Attack) { if (item->GetKind() == TurnChoiceKind::Attack) {
auto attackChoice = std::dynamic_pointer_cast<AttackTurnChoice>(item); auto attackChoice = std::dynamic_pointer_cast<AttackTurnChoice>(item);
AssertNotNull(attackChoice); EnsureNotNull(attackChoice);
auto priority = attackChoice->GetPriority(); auto priority = attackChoice->GetPriority();
HOOK(ChangePriority, attackChoice, attackChoice.get(), &priority); HOOK(ChangePriority, attackChoice, attackChoice.get(), &priority);
attackChoice->SetPriority(priority); attackChoice->SetPriority(priority);

View File

@ -7,12 +7,12 @@ BattleLibrary::BattleLibrary(const CreatureLib::Library::DataLibrary* staticLib,
ScriptResolver* scriptResolver, MiscLibrary* miscLibrary) ScriptResolver* scriptResolver, MiscLibrary* miscLibrary)
: _staticLib(staticLib), _statCalculator(statCalculator), _damageLibrary(damageLibrary), : _staticLib(staticLib), _statCalculator(statCalculator), _damageLibrary(damageLibrary),
_experienceLibrary(experienceLibrary), _scriptResolver(scriptResolver), _miscLibrary(miscLibrary) { _experienceLibrary(experienceLibrary), _scriptResolver(scriptResolver), _miscLibrary(miscLibrary) {
AssertNotNull(_staticLib); EnsureNotNull(_staticLib);
AssertNotNull(_statCalculator); EnsureNotNull(_statCalculator);
AssertNotNull(_damageLibrary); EnsureNotNull(_damageLibrary);
AssertNotNull(_experienceLibrary); EnsureNotNull(_experienceLibrary);
AssertNotNull(_scriptResolver); EnsureNotNull(_scriptResolver);
AssertNotNull(_miscLibrary); EnsureNotNull(_miscLibrary);
} }
const std::unique_ptr<const CreatureLib::Library::LibrarySettings>& BattleLibrary::GetSettings() const noexcept { const std::unique_ptr<const CreatureLib::Library::LibrarySettings>& BattleLibrary::GetSettings() const noexcept {

View File

@ -23,14 +23,14 @@ Battling::BattleStatCalculator::CalculateBoostedStats(Battling::Creature* creatu
} }
uint32_t CalculateHealthStat(Battling::Creature* creature) { uint32_t CalculateHealthStat(Battling::Creature* creature) {
AssertNotNull(creature) EnsureNotNull(creature)
auto level = creature->GetLevel(); auto level = creature->GetLevel();
float a = (creature->GetBaseStat(Library::Statistic::Health)) * 2.0 * level; float a = (creature->GetBaseStat(Library::Statistic::Health)) * 2.0 * level;
return static_cast<uint32_t>(floor(a / 100.0) + level + 10); return static_cast<uint32_t>(floor(a / 100.0) + level + 10);
} }
uint32_t CalculateOtherStat(Battling::Creature* creature, Library::Statistic stat) { uint32_t CalculateOtherStat(Battling::Creature* creature, Library::Statistic stat) {
AssertNotNull(creature) EnsureNotNull(creature)
auto level = creature->GetLevel(); auto level = creature->GetLevel();
float a = (creature->GetBaseStat(stat)) * 2.0 * level; float a = (creature->GetBaseStat(stat)) * 2.0 * level;
return static_cast<uint32_t>(floor(a / 100.0) + 5); return static_cast<uint32_t>(floor(a / 100.0) + 5);

View File

@ -4,8 +4,8 @@
using namespace CreatureLib::Battling; using namespace CreatureLib::Battling;
uint32_t DamageLibrary::GetDamage(ExecutingAttack* attack, Creature* target, uint8_t hitIndex, uint32_t DamageLibrary::GetDamage(ExecutingAttack* attack, Creature* target, uint8_t hitIndex,
const ExecutingAttack::HitData& hitData) const { const ExecutingAttack::HitData& hitData) const {
AssertNotNull(attack) EnsureNotNull(attack)
AssertNotNull(target) EnsureNotNull(target)
auto levelMod = static_cast<float>(2 * attack->GetUser()->GetLevel()) / 5 + 2; auto levelMod = static_cast<float>(2 * attack->GetUser()->GetLevel()) / 5 + 2;
auto bp = hitData.GetBasePower(); auto bp = hitData.GetBasePower();
auto statMod = GetStatModifier(attack, target, hitIndex, hitData); auto statMod = GetStatModifier(attack, target, hitIndex, hitData);
@ -18,8 +18,8 @@ uint32_t DamageLibrary::GetDamage(ExecutingAttack* attack, Creature* target, uin
uint8_t DamageLibrary::GetBasePower(ExecutingAttack* attack, Creature* target, uint8_t hitIndex, uint8_t DamageLibrary::GetBasePower(ExecutingAttack* attack, Creature* target, uint8_t hitIndex,
[[maybe_unused]] const ExecutingAttack::HitData& hitData) const { [[maybe_unused]] const ExecutingAttack::HitData& hitData) const {
AssertNotNull(attack) EnsureNotNull(attack)
AssertNotNull(target) EnsureNotNull(target)
auto bp = attack->GetAttack()->GetAttack()->GetBasePower(); auto bp = attack->GetAttack()->GetAttack()->GetBasePower();
HOOK(OverrideBasePower, attack, attack, target, hitIndex, &bp); HOOK(OverrideBasePower, attack, attack, target, hitIndex, &bp);
return bp; return bp;
@ -27,10 +27,10 @@ uint8_t DamageLibrary::GetBasePower(ExecutingAttack* attack, Creature* target, u
float DamageLibrary::GetStatModifier(ExecutingAttack* attack, Creature* target, uint8_t hitIndex, float DamageLibrary::GetStatModifier(ExecutingAttack* attack, Creature* target, uint8_t hitIndex,
const ExecutingAttack::HitData& hitData) const { const ExecutingAttack::HitData& hitData) const {
AssertNotNull(attack) EnsureNotNull(attack)
AssertNotNull(target) EnsureNotNull(target)
auto user = attack->GetUser().GetRaw(); auto user = attack->GetUser().GetRaw();
AssertNotNull(user) EnsureNotNull(user)
HOOK(ChangeDamageStatsUser, attack, attack, target, hitIndex, &user); HOOK(ChangeDamageStatsUser, attack, attack, target, hitIndex, &user);
Library::Statistic offensiveStat; Library::Statistic offensiveStat;
Library::Statistic defensiveStat; Library::Statistic defensiveStat;
@ -66,8 +66,8 @@ float DamageLibrary::GetStatModifier(ExecutingAttack* attack, Creature* target,
float DamageLibrary::GetDamageModifier(ExecutingAttack* attack, Creature* target, uint8_t hitIndex, float DamageLibrary::GetDamageModifier(ExecutingAttack* attack, Creature* target, uint8_t hitIndex,
const ExecutingAttack::HitData& hitData) const { const ExecutingAttack::HitData& hitData) const {
AssertNotNull(attack) EnsureNotNull(attack)
AssertNotNull(target) EnsureNotNull(target)
float mod = 1; float mod = 1;
mod *= hitData.GetEffectiveness(); mod *= hitData.GetEffectiveness();
HOOK(ModifyDamageModifier, attack, attack, target, hitIndex, &mod); HOOK(ModifyDamageModifier, attack, attack, target, hitIndex, &mod);

View File

@ -5,8 +5,6 @@ void CreatureLib::Battling::ExperienceLibrary::HandleExperienceGain(
CreatureLib::Battling::Creature* faintedMon, CreatureLib::Battling::Creature* faintedMon,
const std::unordered_set<ArbUt::BorrowedPtr<Creature>>& opponents) const { const std::unordered_set<ArbUt::BorrowedPtr<Creature>>& opponents) const {
for (auto opponent : opponents) { for (auto opponent : opponents) {
if (opponent == nullptr)
continue;
if (opponent->IsFainted()) if (opponent->IsFainted())
continue; continue;
if (!opponent->AllowedExperienceGain()) if (!opponent->AllowedExperienceGain())

View File

@ -5,8 +5,8 @@
bool CreatureLib::Battling::MiscLibrary::IsCritical([[maybe_unused]] CreatureLib::Battling::ExecutingAttack* attack, bool CreatureLib::Battling::MiscLibrary::IsCritical([[maybe_unused]] CreatureLib::Battling::ExecutingAttack* attack,
CreatureLib::Battling::Creature* target, CreatureLib::Battling::Creature* target,
[[maybe_unused]] uint8_t hit) const { [[maybe_unused]] uint8_t hit) const {
AssertNotNull(target) EnsureNotNull(target)
Assert(target->GetBattle().HasValue()) Ensure(target->GetBattle().HasValue())
auto rand = target->GetBattle().GetValue()->GetRandom(); auto rand = target->GetBattle().GetValue()->GetRandom();
return rand->Get(10) <= 0; return rand->Get(10) <= 0;
} }
@ -35,8 +35,8 @@ static CreatureLib::Battling::LearnedAttack* GetReplacementAttack() {
bool CreatureLib::Battling::MiscLibrary::CanFlee([[maybe_unused]] FleeTurnChoice* switchChoice) const { return true; } bool CreatureLib::Battling::MiscLibrary::CanFlee([[maybe_unused]] FleeTurnChoice* switchChoice) const { return true; }
CreatureLib::Battling::BaseTurnChoice* CreatureLib::Battling::BaseTurnChoice*
CreatureLib::Battling::MiscLibrary::ReplacementAttack(Creature* user, [[maybe_unused]] CreatureIndex target) const { CreatureLib::Battling::MiscLibrary::ReplacementAttack(Creature* user, [[maybe_unused]] CreatureIndex target) const {
AssertNotNull(user) EnsureNotNull(user)
Assert(user->GetBattleSide().HasValue()) Ensure(user->GetBattleSide().HasValue())
auto sideTarget = 0; auto sideTarget = 0;
if (user->GetBattleSide().GetValue()->GetSideIndex() == 0) if (user->GetBattleSide().GetValue()->GetSideIndex() == 0)
sideTarget = 1; sideTarget = 1;

View File

@ -9,7 +9,6 @@ using namespace CreatureLib::Battling;
const ArbUt::BorrowedPtr<const BattleLibrary>& Battle::GetLibrary() const noexcept { return _library; } const ArbUt::BorrowedPtr<const BattleLibrary>& Battle::GetLibrary() const noexcept { return _library; }
bool Battle::CanUse(const ArbUt::BorrowedPtr<BaseTurnChoice>& choice) { bool Battle::CanUse(const ArbUt::BorrowedPtr<BaseTurnChoice>& choice) {
AssertNotNull(choice)
if (choice->GetKind() == TurnChoiceKind::Attack) { if (choice->GetKind() == TurnChoiceKind::Attack) {
// HOOK: change number of uses needed. // HOOK: change number of uses needed.
return choice.ForceAs<AttackTurnChoice>()->GetAttack()->GetRemainingUses() >= 1; return choice.ForceAs<AttackTurnChoice>()->GetAttack()->GetRemainingUses() >= 1;
@ -18,10 +17,10 @@ bool Battle::CanUse(const ArbUt::BorrowedPtr<BaseTurnChoice>& choice) {
} }
bool Battle::TrySetChoice(BaseTurnChoice* choice) { bool Battle::TrySetChoice(BaseTurnChoice* choice) {
AssertNotNull(choice) EnsureNotNull(choice)
if (!CanUse(choice)) if (!CanUse(choice))
return false; return false;
Assert(choice->GetUser()->GetBattleSide().HasValue()) Ensure(choice->GetUser()->GetBattleSide().HasValue())
choice->GetUser()->GetBattleSide().GetValue()->SetChoice(choice); choice->GetUser()->GetBattleSide().GetValue()->SetChoice(choice);
CheckChoicesSetAndRun(); CheckChoicesSetAndRun();
return true; return true;
@ -51,7 +50,7 @@ void Battle::CheckChoicesSetAndRun() {
try { try {
for (auto side : _sides) { for (auto side : _sides) {
for (auto choice : side->GetChoices()) { for (auto choice : side->GetChoices()) {
AssertNotNull(choice) EnsureNotNull(choice)
if (choice->GetKind() == TurnChoiceKind::Attack) { if (choice->GetKind() == TurnChoiceKind::Attack) {
auto attack = std::static_pointer_cast<AttackTurnChoice>(choice); auto attack = std::static_pointer_cast<AttackTurnChoice>(choice);
uint8_t uses = 1; uint8_t uses = 1;
@ -99,7 +98,6 @@ ArbUt::BorrowedPtr<ChoiceQueue> Battle::GetCurrentTurnQueue() const noexcept { r
BattleRandom* Battle::GetRandom() noexcept { return &_random; } BattleRandom* Battle::GetRandom() noexcept { return &_random; }
bool Battle::CreatureInField(ArbUt::BorrowedPtr<Creature> creature) const { bool Battle::CreatureInField(ArbUt::BorrowedPtr<Creature> creature) const {
AssertNotNull(creature)
return std::any_of(_sides.begin(), _sides.end(), [creature](auto c) { return c->CreatureOnSide(creature); }); return std::any_of(_sides.begin(), _sides.end(), [creature](auto c) { return c->CreatureOnSide(creature); });
} }

View File

@ -41,7 +41,7 @@ namespace CreatureLib::Battling {
.count()) .count())
: _library(library), _parties(parties.GetStdList()), _canFlee(canFlee), _numberOfSides(numberOfSides), : _library(library), _parties(parties.GetStdList()), _canFlee(canFlee), _numberOfSides(numberOfSides),
_creaturesPerSide(creaturesPerSide), _sides(numberOfSides), _random(randomSeed) { _creaturesPerSide(creaturesPerSide), _sides(numberOfSides), _random(randomSeed) {
AssertAllNotNull(parties); EnsureAllNotNull(parties);
for (size_t i = 0; i < numberOfSides; i++) { for (size_t i = 0; i < numberOfSides; i++) {
_sides.Append(new BattleSide(i, this, creaturesPerSide)); _sides.Append(new BattleSide(i, this, creaturesPerSide));

View File

@ -11,9 +11,7 @@ namespace CreatureLib::Battling {
public: public:
BattleParty(CreatureParty* party, const ArbUt::List<CreatureIndex>& responsibleIndices) BattleParty(CreatureParty* party, const ArbUt::List<CreatureIndex>& responsibleIndices)
: _party(party), _responsibleIndices(responsibleIndices) { : _party(party), _responsibleIndices(responsibleIndices) {}
AssertNotNull(_party)
}
inline const ArbUt::BorrowedPtr<CreatureParty>& GetParty() const { return _party; } inline const ArbUt::BorrowedPtr<CreatureParty>& GetParty() const { return _party; }
inline const ArbUt::List<CreatureIndex>& GetResponsibleIndices() const { return _responsibleIndices; } inline const ArbUt::List<CreatureIndex>& GetResponsibleIndices() const { return _responsibleIndices; }

View File

@ -7,7 +7,6 @@ using namespace CreatureLib::Battling;
bool BattleSide::AllChoicesSet() const noexcept { return _choicesSet == _creaturesPerSide; } bool BattleSide::AllChoicesSet() const noexcept { return _choicesSet == _creaturesPerSide; }
bool BattleSide::AllPossibleSlotsFilled() const { bool BattleSide::AllPossibleSlotsFilled() const {
AssertNotNull(_battle)
try { try {
for (size_t i = 0; i < _creatures.Count(); i++) { for (size_t i = 0; i < _creatures.Count(); i++) {
auto c = _creatures[i]; auto c = _creatures[i];
@ -30,7 +29,7 @@ void BattleSide::ResetChoices() noexcept {
} }
void BattleSide::SetChoice(BaseTurnChoice* choice) { void BattleSide::SetChoice(BaseTurnChoice* choice) {
AssertNotNull(choice) EnsureNotNull(choice)
try { try {
for (size_t i = 0; i < _creatures.Count(); i++) { for (size_t i = 0; i < _creatures.Count(); i++) {
auto& c = _creatures[i]; auto& c = _creatures[i];
@ -60,9 +59,6 @@ void BattleSide::SetCreature(ArbUt::OptionalBorrowedPtr<Creature> creature, uint
} }
creature.GetValue()->SetBattleData(_battle, this); creature.GetValue()->SetBattleData(_battle, this);
creature.GetValue()->SetOnBattleField(true); creature.GetValue()->SetOnBattleField(true);
if (_battle == nullptr) {
return;
}
for (auto* side : _battle->GetSides()) { for (auto* side : _battle->GetSides()) {
if (side == this) { if (side == this) {
continue; continue;
@ -91,6 +87,5 @@ size_t BattleSide::ScriptCount() const { return _battle->ScriptCount() + 1; }
uint8_t BattleSide::GetRandomCreatureIndex() { uint8_t BattleSide::GetRandomCreatureIndex() {
// TODO: Consider adding parameter to only get index for available creatures. // TODO: Consider adding parameter to only get index for available creatures.
AssertNotNull(_battle)
return _battle->GetRandom()->Get(_creaturesPerSide); return _battle->GetRandom()->Get(_creaturesPerSide);
} }

View File

@ -25,8 +25,6 @@ namespace CreatureLib::Battling {
void Creature::ChangeSpecies(const ArbUt::BorrowedPtr<const Library::CreatureSpecies>& species, void Creature::ChangeSpecies(const ArbUt::BorrowedPtr<const Library::CreatureSpecies>& species,
const ArbUt::BorrowedPtr<const Library::SpeciesVariant>& variant) { const ArbUt::BorrowedPtr<const Library::SpeciesVariant>& variant) {
AssertNotNull(species);
AssertNotNull(variant);
_species = species; _species = species;
// If the creature is genderless, but it's new species is not, we want to set its gender // If the creature is genderless, but it's new species is not, we want to set its gender
@ -51,7 +49,6 @@ namespace CreatureLib::Battling {
} }
void Creature::ChangeVariant(const ArbUt::BorrowedPtr<const Library::SpeciesVariant>& variant) { void Creature::ChangeVariant(const ArbUt::BorrowedPtr<const Library::SpeciesVariant>& variant) {
AssertNotNull(variant)
_variant = variant; _variant = variant;
// Set the types to the new variant. // Set the types to the new variant.
@ -145,8 +142,8 @@ namespace CreatureLib::Battling {
bool Creature::IsFainted() const noexcept { return this->_currentHealth == 0; } bool Creature::IsFainted() const noexcept { return this->_currentHealth == 0; }
void Creature::OnFaint() { void Creature::OnFaint() {
AssertNotNull(_battle) EnsureNotNull(_battle)
AssertNotNull(_side) EnsureNotNull(_side)
// HOOK: On Faint // HOOK: On Faint
if (_battle.HasValue()) { if (_battle.HasValue()) {
_battle.GetValue()->TriggerEventListener<FaintEvent>(this); _battle.GetValue()->TriggerEventListener<FaintEvent>(this);

View File

@ -43,8 +43,6 @@ namespace CreatureLib::Battling {
const std::unique_ptr<Script>& script) const std::unique_ptr<Script>& script)
: _numberHits(numberHits), _hits(std::make_unique<HitData[]>(targets.Count() * numberHits)), _user(user), : _numberHits(numberHits), _hits(std::make_unique<HitData[]>(targets.Count() * numberHits)), _user(user),
_attack(attack), _targets(targets) { _attack(attack), _targets(targets) {
AssertNotNull(user)
AssertNotNull(attack)
// Take ownership of the script of the attack choice, and give attack choice our initial nullptr. // Take ownership of the script of the attack choice, and give attack choice our initial nullptr.
_script.swap(const_cast<std::unique_ptr<Script>&>(script)); _script.swap(const_cast<std::unique_ptr<Script>&>(script));
} }

View File

@ -4,13 +4,11 @@ CreatureLib::Battling::LearnedAttack::LearnedAttack(
const ArbUt::BorrowedPtr<const CreatureLib::Library::AttackData>& attack, uint8_t maxUses, const ArbUt::BorrowedPtr<const CreatureLib::Library::AttackData>& attack, uint8_t maxUses,
AttackLearnMethod learnMethod) AttackLearnMethod learnMethod)
: _attack(attack), _maxUses(maxUses), _remainingUses(maxUses), _learnMethod(learnMethod) { : _attack(attack), _maxUses(maxUses), _remainingUses(maxUses), _learnMethod(learnMethod) {
AssertNotNull(_attack)
} }
CreatureLib::Battling::LearnedAttack::LearnedAttack( CreatureLib::Battling::LearnedAttack::LearnedAttack(
const ArbUt::BorrowedPtr<const CreatureLib::Library::AttackData>& attack, AttackLearnMethod learnMethod) const ArbUt::BorrowedPtr<const CreatureLib::Library::AttackData>& attack, AttackLearnMethod learnMethod)
: _attack(attack), _maxUses(attack->GetBaseUsages()), _remainingUses(_maxUses), _learnMethod(learnMethod) { : _attack(attack), _maxUses(attack->GetBaseUsages()), _remainingUses(_maxUses), _learnMethod(learnMethod) {
AssertNotNull(_attack)
} }
const ArbUt::BorrowedPtr<const CreatureLib::Library::AttackData>& const ArbUt::BorrowedPtr<const CreatureLib::Library::AttackData>&

View File

@ -48,9 +48,7 @@ namespace CreatureLib::Battling {
std::optional<ArbUt::BorrowedPtr<Script>> GetNextNotNull() { std::optional<ArbUt::BorrowedPtr<Script>> GetNextNotNull() {
while (HasNext()) { while (HasNext()) {
auto s = GetNext(); auto s = GetNext();
if (s != nullptr) { return s;
return s;
}
} }
return {}; return {};
} }

View File

@ -4,8 +4,6 @@
auto aggregator = source->GetScriptIterator(); \ auto aggregator = source->GetScriptIterator(); \
while (aggregator.HasNext()) { \ while (aggregator.HasNext()) { \
auto next = aggregator.GetNext(); \ auto next = aggregator.GetNext(); \
if (next == nullptr) \
continue; \
try { \ try { \
next->hookName(__VA_ARGS__); \ next->hookName(__VA_ARGS__); \
} catch (const std::exception& e) { \ } catch (const std::exception& e) { \

View File

@ -19,8 +19,6 @@ namespace CreatureLib::Battling {
if (_attackScript != nullptr) if (_attackScript != nullptr)
return; return;
auto user = GetUser(); auto user = GetUser();
if (user == nullptr)
return;
auto battle = user->GetBattle(); auto battle = user->GetBattle();
if (battle.HasValue()) { if (battle.HasValue()) {
if (_attack->GetAttack()->HasSecondaryEffect()) { if (_attack->GetAttack()->HasSecondaryEffect()) {
@ -38,20 +36,13 @@ namespace CreatureLib::Battling {
public: public:
AttackTurnChoice(Creature* user, const ArbUt::BorrowedPtr<LearnedAttack>& attack, const CreatureIndex& target) AttackTurnChoice(Creature* user, const ArbUt::BorrowedPtr<LearnedAttack>& attack, const CreatureIndex& target)
: BaseTurnChoice(user), _attack(attack), _target(target) { : BaseTurnChoice(user), _attack(attack), _target(target) {
#ifndef TESTS_BUILD EnsureNotNull(user)
AssertNotNull(user)
AssertNotNull(attack)
#endif
ResolveScript(); ResolveScript();
_priority = _attack->GetAttack()->GetPriority(); _priority = _attack->GetAttack()->GetPriority();
} }
AttackTurnChoice(Creature* user, const ArbUt::BorrowedPtr<LearnedAttack>& attack, const CreatureIndex& target, AttackTurnChoice(Creature* user, const ArbUt::BorrowedPtr<LearnedAttack>& attack, const CreatureIndex& target,
Script* script) Script* script)
: BaseTurnChoice(user), _attack(attack), _target(target), _attackScript(script) { : BaseTurnChoice(user), _attack(attack), _target(target), _attackScript(script) {
#ifndef TESTS_BUILD
AssertNotNull(user)
AssertNotNull(attack)
#endif
_priority = _attack->GetAttack()->GetPriority(); _priority = _attack->GetAttack()->GetPriority();
} }
@ -68,18 +59,13 @@ namespace CreatureLib::Battling {
const std::unique_ptr<Script>& GetAttackScript() const noexcept { return _attackScript; } const std::unique_ptr<Script>& GetAttackScript() const noexcept { return _attackScript; }
size_t ScriptCount() const override { size_t ScriptCount() const override {
if (_user == nullptr) {
return 1;
}
return 1 + GetUser()->ScriptCount(); return 1 + GetUser()->ScriptCount();
} }
protected: protected:
void GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) override { void GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) override {
scripts.Append(ScriptWrapper::FromScript(&_attackScript)); scripts.Append(ScriptWrapper::FromScript(&_attackScript));
if (_user != nullptr) { GetUser()->GetActiveScripts(scripts);
GetUser()->GetActiveScripts(scripts);
}
} }
}; };
} }

View File

@ -15,12 +15,12 @@ namespace CreatureLib::Library {
virtual ~BaseLibrary() noexcept { _values.Clear(); } virtual ~BaseLibrary() noexcept { _values.Clear(); }
inline virtual void Insert(const ArbUt::StringView& key, const T* value) { inline virtual void Insert(const ArbUt::StringView& key, const T* value) {
AssertNotNull(value) EnsureNotNull(value)
_values.GetStdMap().insert({key.GetHash(), std::unique_ptr<const T>(value)}); _values.GetStdMap().insert({key.GetHash(), std::unique_ptr<const T>(value)});
_listValues.Append(key); _listValues.Append(key);
} }
inline virtual void Insert(uint32_t hashedKey, const T* value) { inline virtual void Insert(uint32_t hashedKey, const T* value) {
AssertNotNull(value) EnsureNotNull(value)
_values.GetStdMap().insert({hashedKey, std::unique_ptr<const T>(value)}); _values.GetStdMap().insert({hashedKey, std::unique_ptr<const T>(value)});
_listValues.Append(hashedKey); _listValues.Append(hashedKey);
} }

View File

@ -9,7 +9,7 @@ struct CreatureSpecies::impl {
const ArbUt::StringView _growthRate; const ArbUt::StringView _growthRate;
uint8_t _captureRate; uint8_t _captureRate;
ArbUt::Dictionary<uint32_t, std::unique_ptr<const SpeciesVariant>> _variantsLookup; ArbUt::Dictionary<uint32_t, ArbUt::UniquePtr<const SpeciesVariant>> _variantsLookup;
ArbUt::List<ArbUt::BorrowedPtr<const SpeciesVariant>> _variantsList; ArbUt::List<ArbUt::BorrowedPtr<const SpeciesVariant>> _variantsList;
std::unordered_set<uint32_t> _flags; std::unordered_set<uint32_t> _flags;
@ -17,7 +17,7 @@ struct CreatureSpecies::impl {
const ArbUt::StringView& growthRate, uint8_t captureRate, std::unordered_set<uint32_t> flags) const ArbUt::StringView& growthRate, uint8_t captureRate, std::unordered_set<uint32_t> flags)
: _name(name), _id(id), _genderRate(genderRatio), _growthRate(growthRate), _captureRate(captureRate), : _name(name), _id(id), _genderRate(genderRatio), _growthRate(growthRate), _captureRate(captureRate),
_variantsLookup(1), _variantsList(1), _flags(std::move(flags)) { _variantsLookup(1), _variantsList(1), _flags(std::move(flags)) {
AssertNotNull(defaultVariant) EnsureNotNull(defaultVariant)
SetVariant("default"_cnc, defaultVariant); SetVariant("default"_cnc, defaultVariant);
} }
@ -40,13 +40,13 @@ struct CreatureSpecies::impl {
auto find = _variantsLookup.GetStdMap().find(hash); auto find = _variantsLookup.GetStdMap().find(hash);
if (find == _variantsLookup.end()) if (find == _variantsLookup.end())
return {}; return {};
return std::get<1>(*find); return std::get<1>(*find).GetRaw();
} }
[[nodiscard]] inline ArbUt::BorrowedPtr<const SpeciesVariant> GetVariant(const ArbUt::BasicStringView& key) const { [[nodiscard]] inline ArbUt::BorrowedPtr<const SpeciesVariant> GetVariant(const ArbUt::BasicStringView& key) const {
return _variantsLookup.Get(key); return _variantsLookup.Get(key).GetRaw();
} }
[[nodiscard]] inline ArbUt::BorrowedPtr<const SpeciesVariant> GetVariant(uint32_t key) const { [[nodiscard]] inline ArbUt::BorrowedPtr<const SpeciesVariant> GetVariant(uint32_t key) const {
return _variantsLookup.Get(key); return _variantsLookup.Get(key).GetRaw();
} }
[[nodiscard]] Gender GetRandomGender(ArbUt::Random& rand) const noexcept { [[nodiscard]] Gender GetRandomGender(ArbUt::Random& rand) const noexcept {
if (this->_genderRate == -1) { if (this->_genderRate == -1) {
@ -60,10 +60,9 @@ struct CreatureSpecies::impl {
[[nodiscard]] inline const ArbUt::StringView& GetName() const noexcept { return _name; } [[nodiscard]] inline const ArbUt::StringView& GetName() const noexcept { return _name; }
void SetVariant(const ArbUt::StringView& name, const SpeciesVariant* variant) { void SetVariant(const ArbUt::StringView& name, const SpeciesVariant* variant) {
Assert(!name.IsEmpty()) Ensure(!name.IsEmpty())
AssertNotNull(variant)
_variantsList.CreateBack(variant); _variantsList.CreateBack(variant);
_variantsLookup.GetStdMap().insert({name, std::unique_ptr<const SpeciesVariant>(variant)}); _variantsLookup.GetStdMap().emplace(name, variant);
} }
inline const ArbUt::List<ArbUt::BorrowedPtr<const SpeciesVariant>>& GetVariantsIterator() const { inline const ArbUt::List<ArbUt::BorrowedPtr<const SpeciesVariant>>& GetVariantsIterator() const {

View File

@ -7,10 +7,10 @@ CreatureLib::Library::DataLibrary::DataLibrary(LibrarySettings* settings, Creatu
TypeLibrary* typeLibrary) TypeLibrary* typeLibrary)
: _settings(settings), _species(species), _attacks(attacks), _items(items), _growthRates(growthRates), : _settings(settings), _species(species), _attacks(attacks), _items(items), _growthRates(growthRates),
_typeLibrary(typeLibrary) { _typeLibrary(typeLibrary) {
AssertNotNull(_settings) EnsureNotNull(_settings)
AssertNotNull(_species) EnsureNotNull(_species)
AssertNotNull(_attacks) EnsureNotNull(_attacks)
AssertNotNull(_items) EnsureNotNull(_items)
AssertNotNull(_growthRates) EnsureNotNull(_growthRates)
AssertNotNull(_typeLibrary) EnsureNotNull(_typeLibrary)
} }

View File

@ -11,8 +11,8 @@ namespace CreatureLib::Library {
public: public:
inline ExternGrowthRate(level_int_t (*calcLevel)(uint32_t), uint32_t (*calcExperience)(level_int_t level)) inline ExternGrowthRate(level_int_t (*calcLevel)(uint32_t), uint32_t (*calcExperience)(level_int_t level))
: _calcLevel(calcLevel), _calcExperience(calcExperience) { : _calcLevel(calcLevel), _calcExperience(calcExperience) {
AssertNotNull(calcLevel) EnsureNotNull(calcLevel)
AssertNotNull(calcExperience) EnsureNotNull(calcExperience)
} }
level_int_t CalculateLevel(uint32_t experience) const override { return _calcLevel(experience); } level_int_t CalculateLevel(uint32_t experience) const override { return _calcLevel(experience); }

View File

@ -22,9 +22,9 @@
#include <vector> #include <vector>
// Arbutils // Arbutils
#include <Arbutils/Assert.hpp>
#include <Arbutils/Collections/Dictionary.hpp> #include <Arbutils/Collections/Dictionary.hpp>
#include <Arbutils/Collections/List.hpp> #include <Arbutils/Collections/List.hpp>
#include <Arbutils/Ensure.hpp>
#include <Arbutils/Enum.hpp> #include <Arbutils/Enum.hpp>
#include <Arbutils/Exception.hpp> #include <Arbutils/Exception.hpp>
#include <Arbutils/Memory/Memory.hpp> #include <Arbutils/Memory/Memory.hpp>

View File

@ -49,8 +49,7 @@ TEST_CASE("Script source with script ptr being set.") {
auto source = ScriptSourceWithScriptPtr(); auto source = ScriptSourceWithScriptPtr();
source.ScriptPtr = std::make_unique<TestScript>("foobar"); source.ScriptPtr = std::make_unique<TestScript>("foobar");
auto scripts = source.GetScriptIterator(); auto scripts = source.GetScriptIterator();
auto first = scripts.GetNext(); [[maybe_unused]] auto first = scripts.GetNext();
CHECK(first != nullptr);
} }
TEST_CASE("Script source with script ptr being set after first iteration.") { TEST_CASE("Script source with script ptr being set after first iteration.") {
@ -59,8 +58,7 @@ TEST_CASE("Script source with script ptr being set after first iteration.") {
REQUIRE_FALSE(scripts.HasNext()); REQUIRE_FALSE(scripts.HasNext());
source.ScriptPtr = std::make_unique<TestScript>("foobar"); source.ScriptPtr = std::make_unique<TestScript>("foobar");
scripts = source.GetScriptIterator(); scripts = source.GetScriptIterator();
auto first = scripts.GetNext(); [[maybe_unused]] auto first = scripts.GetNext();
CHECK(first != nullptr);
} }
TEST_CASE("Script source with empty script set.") { TEST_CASE("Script source with empty script set.") {