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

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2020-08-15 15:10:48 +02:00
parent 46ab060b99
commit c921d3127b
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
8 changed files with 104 additions and 78 deletions

View File

@ -22,7 +22,7 @@ export bool CreatureLib_ExecutingAttack_IsCreatureTarget(ExecutingAttack* p, Cre
}
export uint8_t CreatureLib_ExecutingAttack_GetTargetCount(ExecutingAttack* p) { return p->GetTargetCount(); }
export const Creature* const* CreatureLib_ExecutingAttack_GetTargets(ExecutingAttack* p) {
return reinterpret_cast<const Creature* const*>(p->GetTargets());
return reinterpret_cast<const Creature* const*>(p->GetTargets().RawData());
}
export Creature* CreatureLib_ExecutingAttack_GetUser(ExecutingAttack* p) { return p->GetUser().GetRaw(); }

View File

@ -1,6 +1,7 @@
#ifndef CREATURELIB_CORE_HPP
#define CREATURELIB_CORE_HPP
#include <Arbutils/Exception.hpp>
#include <cstring>
#include <exception>
#include <sstream>
@ -14,6 +15,12 @@ class ExceptionHandler {
static std::string _creatureLibLastException;
public:
static void SetLastException(std::string function, const ArbUt::Exception& e) {
std::stringstream ss;
ss << "[" << function << "] " << e.what() << std::endl;
ss << e.GetStacktrace();
_creatureLibLastException = ss.str();
}
static void SetLastException(std::string function, const std::exception& e) {
std::stringstream ss;
ss << "[" << function << "] " << e.what();
@ -26,6 +33,9 @@ public:
try { \
data; \
return 0; \
} catch (const ArbUt::Exception& e) { \
ExceptionHandler::SetLastException(__FUNCTION__, e); \
return CreatureLibException; \
} catch (const std::exception& e) { \
ExceptionHandler::SetLastException(__FUNCTION__, e); \
return CreatureLibException; \

View File

@ -122,24 +122,24 @@ void TurnHandler::ExecuteAttackChoice(const ArbUt::BorrowedPtr<AttackTurnChoice>
}
void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::BorrowedPtr<Creature>& target) {
auto user = attack->GetUser();
AssertNotNull(attack)
auto& user = attack->GetUser();
AssertNotNull(user)
AssertNotNull(target)
if (user->GetBattle()->HasEnded())
auto& battle = user->GetBattle();
AssertNotNull(battle)
if (battle->HasEnded())
return;
const auto& targetSource = target;
auto userSource = attack;
bool fail = false;
HOOK(FailIncomingAttack, targetSource, attack, target.GetRaw(), &fail);
HOOK(FailIncomingAttack, target, attack, target.GetRaw(), &fail);
if (fail) {
// TODO: Fail handling.
return;
}
bool invulnerable = false;
HOOK(IsInvulnerable, targetSource, attack, target.GetRaw(), &invulnerable);
HOOK(IsInvulnerable, target, attack, target.GetRaw(), &invulnerable);
if (invulnerable) {
// TODO: We should probably do something when a target is invulnerable.
return;
@ -147,18 +147,28 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::Bo
auto numberOfHits = attack->GetNumberOfHits();
if (numberOfHits == 0) {
HOOK(OnAttackMiss, targetSource, attack, target.GetRaw());
user->GetBattle()->TriggerEventListener<MissEvent>(user);
HOOK(OnAttackMiss, target, attack, target.GetRaw());
battle->TriggerEventListener<MissEvent>(user);
return;
}
auto attackData = attack->GetAttack()->GetAttack();
auto library = user->GetBattle()->GetLibrary();
auto& learnedAttack = attack->GetAttack();
AssertNotNull(learnedAttack);
auto& attackData = learnedAttack->GetAttack();
AssertNotNull(attackData);
auto& library = battle->GetLibrary();
AssertNotNull(library)
auto& dmgLibrary = library->GetDamageLibrary();
auto hitIterator = attack->GetTargetIteratorBegin(target.GetRaw());
auto& typeLibrary = library->GetTypeLibrary();
auto& miscLibrary = library->GetMiscLibrary();
AssertNotNull(dmgLibrary)
AssertNotNull(typeLibrary)
AssertNotNull(miscLibrary)
auto hitIterator = attack->GetTargetIteratorBegin(target);
for (uint8_t hitIndex = 0; hitIndex < numberOfHits; hitIndex++) {
if (user->GetBattle()->HasEnded())
if (battle->HasEnded())
return;
if (user->IsFainted()) {
break;
@ -167,28 +177,34 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::Bo
break;
}
auto& hit = hitIterator[hitIndex];
uint8_t hitType = attack->GetAttack()->GetAttack()->GetType();
HOOK(ChangeAttackType, targetSource, attack, target.GetRaw(), hitIndex, &hitType);
uint8_t hitType = attackData->GetType();
HOOK(ChangeAttackType, target, attack, target.GetRaw(), hitIndex, &hitType);
hit.SetType(hitType);
auto effectiveness = library->GetTypeLibrary()->GetEffectiveness(hitType, target->GetTypes());
auto effectiveness = typeLibrary->GetEffectiveness(hitType, target->GetTypes());
HOOK(ChangeEffectiveness, attack, attack, target.GetRaw(), hitIndex, &effectiveness)
hit.SetEffectiveness(effectiveness);
hit.SetCritical(library->GetMiscLibrary()->IsCritical(attack, target.GetRaw(), hitIndex));
hit.SetCritical(miscLibrary->IsCritical(attack, target.GetRaw(), hitIndex));
hit.SetBasePower(dmgLibrary->GetBasePower(attack, target.GetRaw(), hitIndex, hit));
hit.SetDamage(dmgLibrary->GetDamage(attack, target.GetRaw(), hitIndex, hit));
if (attackData->GetCategory() == Library::AttackCategory::Status) {
if (attackData->HasSecondaryEffect()) {
auto& effect = attackData->GetSecondaryEffect();
bool hasSecondaryEffect;
if (effect->GetChance() == -1) {
hasSecondaryEffect = true;
} else {
hasSecondaryEffect =
user->GetBattle()->GetRandom()->EffectChance(effect->GetChance(), attack, target.GetRaw());
}
if (hasSecondaryEffect) {
HOOK(OnSecondaryEffect, userSource, attack, target.GetRaw(), hitIndex);
try {
auto& effect = attackData->GetSecondaryEffect();
bool hasSecondaryEffect;
if (effect->GetChance() == -1) {
hasSecondaryEffect = true;
} else {
hasSecondaryEffect =
battle->GetRandom()->EffectChance(effect->GetChance(), attack, target.GetRaw());
}
if (hasSecondaryEffect) {
HOOK(OnSecondaryEffect, user, attack, target.GetRaw(), hitIndex);
}
} catch (const CreatureException& e) {
throw e;
} catch (const std::exception& e) {
THROW_CREATURE("Exception during status attack effect handling: " << e.what() << ".");
}
}
} else {
@ -202,18 +218,26 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::Bo
if (attackData->HasSecondaryEffect() && !user->IsFainted()) {
bool preventSecondary = false;
HOOK(PreventSecondaryEffects, targetSource, attack, target.GetRaw(), hitIndex, &preventSecondary);
HOOK(PreventSecondaryEffects, target, attack, target.GetRaw(), hitIndex, &preventSecondary);
if (!preventSecondary) {
auto& effect = attackData->GetSecondaryEffect();
bool hasSecondaryEffect;
if (effect->GetChance() == -1) {
hasSecondaryEffect = true;
} else {
hasSecondaryEffect = user->GetBattle()->GetRandom()->EffectChance(effect->GetChance(),
attack, target.GetRaw());
}
if (hasSecondaryEffect) {
HOOK(OnSecondaryEffect, userSource, attack, target.GetRaw(), hitIndex);
try {
auto& effect = attackData->GetSecondaryEffect();
bool hasSecondaryEffect;
if (effect->GetChance() == -1) {
hasSecondaryEffect = true;
} else {
auto random = battle->GetRandom();
AssertNotNull(random);
hasSecondaryEffect = random->EffectChance(effect->GetChance(), attack, target.GetRaw());
}
if (hasSecondaryEffect) {
HOOK(OnSecondaryEffect, user, attack, target.GetRaw(), hitIndex);
}
} catch (const CreatureException& e) {
throw e;
} catch (const std::exception& e) {
THROW_CREATURE("Exception during offensive attack secondary effect handling: " << e.what()
<< ".");
}
}
}
@ -222,7 +246,7 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::Bo
}
if (!user->IsFainted()) {
HOOK(OnAfterHits, userSource, attack, target.GetRaw());
HOOK(OnAfterHits, user, attack, target.GetRaw());
}
}

View File

@ -103,13 +103,14 @@ namespace CreatureLib::Battling {
void RegisterEventListener(EventHook::EventHookFunc listener) { this->_eventHook.RegisterListener(listener); }
template <EventDataType T, class... parameters> void TriggerEventListener(parameters... args) {
this->_eventHook.Trigger<T>(args...);
try_creature(this->_eventHook.Trigger<T>(args...);, "Exception occurred during event trigger.");
}
EventHook& GetEventHook() noexcept { return _eventHook; }
const EventHook& GetEventHook() const noexcept { return _eventHook; }
template <HistoryElementType T, class... parameters> void RegisterHistoryElement(parameters... args) {
this->_historyHolder.Register<T>(args...);
try_creature(this->_historyHolder.Register<T>(args...);
, "Exception occurred during history element registration.");
}
const HistoryHolder& GetHistory() const noexcept { return _historyHolder; }
};

View File

@ -36,20 +36,19 @@ namespace CreatureLib::Battling {
};
private:
const ArbUt::BorrowedPtr<Creature>* _targets;
uint8_t _targetCount;
uint8_t _numberHits;
std::unique_ptr<HitData[]> _hits;
ArbUt::BorrowedPtr<Creature> _user;
ArbUt::BorrowedPtr<LearnedAttack> _attack;
std::unique_ptr<Script> _script = nullptr;
ArbUt::List<ArbUt::BorrowedPtr<Creature>> _targets;
public:
ExecutingAttack(const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& targets, uint8_t numberHits,
ArbUt::BorrowedPtr<Creature> user, const ArbUt::BorrowedPtr<LearnedAttack>& attack,
const std::unique_ptr<Script>& script)
: _targets(targets.RawData()), _targetCount(targets.Count()), _numberHits(numberHits),
_hits(std::make_unique<HitData[]>(targets.Count() * numberHits)), _user(user), _attack(attack) {
: _numberHits(numberHits), _hits(std::make_unique<HitData[]>(targets.Count() * numberHits)), _user(user),
_attack(attack), _targets(targets) {
AssertNotNull(user)
AssertNotNull(attack)
// Take ownership of the script of the attack choice, and give attack choice our initial nullptr.
@ -61,7 +60,7 @@ namespace CreatureLib::Battling {
virtual ~ExecutingAttack() noexcept = default;
HitData& GetHitData(ArbUt::BorrowedPtr<Creature> creature, uint8_t hit) {
for (uint8_t i = 0; i < _targetCount; i++) {
for (uint8_t i = 0; i < _targets.Count(); i++) {
if (_targets[i] == creature) {
return _hits[i * _numberHits + hit];
}
@ -70,7 +69,7 @@ namespace CreatureLib::Battling {
}
HitData* GetTargetIteratorBegin(ArbUt::BorrowedPtr<Creature> creature) {
for (uint8_t i = 0; i < _targetCount; i++) {
for (uint8_t i = 0; i < _targets.Count(); i++) {
if (_targets[i] == creature) {
return &_hits[i * _numberHits];
}
@ -79,7 +78,7 @@ namespace CreatureLib::Battling {
}
bool IsCreatureTarget(ArbUt::BorrowedPtr<Creature> creature) noexcept {
for (uint8_t i = 0; i < _targetCount; i++) {
for (uint8_t i = 0; i < _targets.Count(); i++) {
if (_targets[i] == creature) {
return true;
}
@ -87,9 +86,9 @@ namespace CreatureLib::Battling {
return false;
}
inline uint8_t GetTargetCount() const noexcept { return _targetCount; }
inline uint8_t GetTargetCount() const noexcept { return _targets.Count(); }
inline const ArbUt::BorrowedPtr<Creature>* GetTargets() const noexcept { return _targets; }
inline const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& GetTargets() const noexcept { return _targets; }
inline uint8_t GetNumberOfHits() const noexcept { return _numberHits; }
inline const ArbUt::BorrowedPtr<Creature>& GetUser() noexcept { return _user; }

View File

@ -1,29 +1,20 @@
#define HOOK(hookName, source, ...) \
{ \
auto aggregator = source->GetScriptIterator(); \
while (aggregator.HasNext()) { \
auto next = aggregator.GetNext(); \
if (next == nullptr) \
continue; \
try { \
next->hookName(__VA_ARGS__); \
} catch (const std::exception& e) { \
THROW_CREATURE("Exception running script hook '" #hookName "': " << e.what()) \
} \
} \
}
#define HOOK_LOCAL(hookName, source, ...) \
{ \
auto aggregator = source.GetScriptIterator(); \
while (aggregator.HasNext()) { \
auto next = aggregator.GetNext(); \
if (next == nullptr) \
continue; \
try { \
next->hookName(__VA_ARGS__); \
} catch (const std::exception& e) { \
THROW_CREATURE("Exception running script hook '" #hookName "': " << e.what()) \
try { \
auto aggregator = source->GetScriptIterator(); \
while (aggregator.HasNext()) { \
auto next = aggregator.GetNext(); \
if (next == nullptr) \
continue; \
try { \
next->hookName(__VA_ARGS__); \
} catch (const std::exception& e) { \
THROW_CREATURE("Exception running script hook '" #hookName "': " << e.what()) \
} \
} \
} catch (const CreatureException& e) { \
throw e; \
} catch (const std::exception& e) { \
THROW_CREATURE("Exception setting up script hook '" #hookName "': " << e.what()) \
} \
}

View File

@ -1,15 +1,16 @@
#ifndef CREATURELIB_CREATUREEXCEPTION_HPP
#define CREATURELIB_CREATUREEXCEPTION_HPP
#include <Arbutils/Exception.hpp>
#include <cstring>
#include <sstream>
#include <stdexcept>
#include <string>
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
class CreatureException : public std::runtime_error {
class CreatureException : public ArbUt::Exception {
public:
explicit CreatureException(const std::string& error) : std::runtime_error(error) {}
explicit CreatureException(const std::string& error) : ArbUt::Exception(error) {}
virtual ~CreatureException() = default;
};

View File

@ -7,7 +7,7 @@ TEST_CASE("When throwing exception, what() is readable", "[Utilities]") {
bool hasCaught = false;
try {
THROW_CREATURE("foobar");
} catch (const std::exception& e) {
} catch (const ArbUt::Exception& e) {
hasCaught = true;
INFO(e.what());
REQUIRE(std::string(e.what()) == "[ExceptionTests.cpp:9] foobar");