From e2e706693b11d5dde53134bad9a69af28861ef64 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Tue, 28 Jul 2020 17:22:43 +0200 Subject: [PATCH] Clearer errors for C Interface. --- src/Battling/Flow/TurnHandler.cpp | 4 ++-- src/Battling/Models/Battle.cpp | 28 +++++++++++++++++-------- src/Battling/Models/BattleSide.cpp | 33 ++++++++++++++++++++---------- 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index 2cb9342..b190142 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -18,8 +18,8 @@ void TurnHandler::RunTurn(ArbUt::BorrowedPtr queue) { ExecuteChoice(item.get()); } catch (const std::exception& e) { THROW_CREATURE("Executing choice failed for choice by mon on side " - << item.get()->GetUser()->GetBattleSide()->GetSideIndex() << " and index " - << item.get()->GetUser()->GetBattleSide()->GetCreatureIndex(item->GetUser()) + << ((uint32_t)item.get()->GetUser()->GetBattleSide()->GetSideIndex()) << " and index " + << ((uint32_t)item.get()->GetUser()->GetBattleSide()->GetCreatureIndex(item->GetUser())) << " with message '" << e.what() << "'."); } } diff --git a/src/Battling/Models/Battle.cpp b/src/Battling/Models/Battle.cpp index f99cad2..6694d6a 100644 --- a/src/Battling/Models/Battle.cpp +++ b/src/Battling/Models/Battle.cpp @@ -28,15 +28,21 @@ bool Battle::TrySetChoice(BaseTurnChoice* choice) { } void Battle::CheckChoicesSetAndRun() { - for (auto side : _sides) { - if (!side->AllChoicesSet()) { - return; + try { + for (auto side : _sides) { + if (!side->AllChoicesSet()) { + return; + } } - } - for (auto side : _sides) { - if (!side->AllPossibleSlotsFilled()) { - return; + for (auto side : _sides) { + if (!side->AllPossibleSlotsFilled()) { + return; + } } + } catch (const CreatureException& e) { + throw e; + } catch (const std::exception& e) { + THROW_CREATURE("Exception during choices set validation: '" << e.what() << "'."); } auto choices = std::vector>(_numberOfSides * _creaturesPerSide); @@ -71,7 +77,13 @@ void Battle::CheckChoicesSetAndRun() { THROW_CREATURE("Exception during turn ordering: '" << e.what() << "'.") } this->_currentTurnQueue.reset(new ChoiceQueue(choices)); - TurnHandler::RunTurn(this->_currentTurnQueue); + try { + TurnHandler::RunTurn(this->_currentTurnQueue); + } catch (const CreatureException& e) { + throw e; + } catch (const std::exception& e) { + THROW_CREATURE("Error during running a turn: '" << e.what() << "'."); + } if (this->_currentTurnQueue->HasCompletedQueue) { this->_currentTurnQueue = nullptr; } diff --git a/src/Battling/Models/BattleSide.cpp b/src/Battling/Models/BattleSide.cpp index e386753..c9e84ea 100644 --- a/src/Battling/Models/BattleSide.cpp +++ b/src/Battling/Models/BattleSide.cpp @@ -9,12 +9,16 @@ bool BattleSide::AllChoicesSet() const noexcept { return _choicesSet == _creatur bool BattleSide::AllPossibleSlotsFilled() const { AssertNotNull(_battle) - for (size_t i = 0; i < _creatures.Count(); i++) { - auto c = _creatures[i]; - if (c == nullptr || c->IsFainted()) { - if (_battle->CanSlotBeFilled(_index, i)) - return false; + try { + for (size_t i = 0; i < _creatures.Count(); i++) { + auto c = _creatures[i]; + if (c == nullptr || c->IsFainted()) { + if (_battle->CanSlotBeFilled(_index, i)) + return false; + } } + } catch (const std::exception& e) { + THROW_CREATURE("Exception during AllPossibleSlotsFilled check: '" << e.what() << "'."); } return true; } @@ -28,12 +32,19 @@ void BattleSide::ResetChoices() noexcept { void BattleSide::SetChoice(BaseTurnChoice* choice) { AssertNotNull(choice) - auto find = std::find(_creatures.begin(), _creatures.end(), choice->GetUser()); - if (find == _creatures.end()) - THROW_CREATURE("User not found"); - uint8_t index = std::distance(_creatures.begin(), find); - _choices[index] = std::shared_ptr(choice); - _choicesSet++; + try { + for (size_t i = 0; i < _creatures.Count(); i++) { + auto& c = _creatures[i]; + if (c == choice->GetUser()) { + _choices[i] = std::shared_ptr(choice); + _choicesSet++; + return; + } + } + } catch (const std::exception& e) { + THROW_CREATURE("Error during setting choice: '" << e.what() << "'."); + } + THROW_CREATURE("User not found"); } void BattleSide::SetCreature(ArbUt::BorrowedPtr creature, uint8_t index) {