156 lines
5.2 KiB
C++
156 lines
5.2 KiB
C++
#include "BattleSide.hpp"
|
|
#include "../EventHooks/EventDataClasses.hpp"
|
|
#include "../ScriptHandling/ScriptMacros.hpp"
|
|
#include "Battle.hpp"
|
|
|
|
using namespace CreatureLib::Battling;
|
|
|
|
bool BattleSide::AllChoicesSet() const noexcept { return _choicesSet == _creaturesPerSide; }
|
|
|
|
bool BattleSide::AllPossibleSlotsFilled() const {
|
|
try {
|
|
for (size_t i = 0; i < _creatures.Count(); i++) {
|
|
auto c = _creatures[i];
|
|
if (!c.HasValue() || !c.GetValue()->IsUsable()) {
|
|
if (_battle->CanSlotBeFilled(_index, i)) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
} catch (const std::exception& e) {
|
|
THROW("Exception during AllPossibleSlotsFilled check: '", e.what(), "'.");
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void BattleSide::ResetChoices() noexcept {
|
|
_choicesSet = 0;
|
|
for (u8 i = 0; i < _creaturesPerSide; i++) {
|
|
_choices[i] = nullptr;
|
|
}
|
|
}
|
|
|
|
void BattleSide::SetChoice(BaseTurnChoice* choice) {
|
|
EnsureNotNull(choice)
|
|
try {
|
|
for (size_t i = 0; i < _creatures.Count(); i++) {
|
|
auto& c = _creatures[i];
|
|
if (!c.HasValue()) {
|
|
continue;
|
|
}
|
|
if (c.GetValue() == choice->GetUser()) {
|
|
_choices[i] = std::shared_ptr<BaseTurnChoice>(choice);
|
|
_choicesSet++;
|
|
return;
|
|
}
|
|
}
|
|
} catch (const std::exception& e) {
|
|
THROW("Error during setting choice: '", e.what(), "'.");
|
|
}
|
|
THROW("User not found");
|
|
}
|
|
|
|
void BattleSide::SetCreature(ArbUt::OptionalBorrowedPtr<Creature> creature, u8 index) {
|
|
auto old = _creatures[index];
|
|
if (old.HasValue()) {
|
|
HOOK(OnRemove, old.GetValue());
|
|
old.GetValue()->SetOnBattleField(false);
|
|
}
|
|
_creatures[index] = creature;
|
|
if (!creature.HasValue()) {
|
|
return;
|
|
}
|
|
creature.GetValue()->SetBattleData(_battle, this);
|
|
creature.GetValue()->SetOnBattleField(true);
|
|
creature.GetValue()->SetBattleIndex(CreatureIndex(_index, index));
|
|
for (auto* side : _battle->GetSides()) {
|
|
if (side == this) {
|
|
continue;
|
|
}
|
|
for (const auto& c : side->GetCreatures()) {
|
|
if (c.HasValue()) {
|
|
c.GetValue()->MarkOpponentAsSeen(creature.GetValue());
|
|
creature.GetValue()->MarkOpponentAsSeen(c.GetValue());
|
|
}
|
|
}
|
|
}
|
|
_battle->TriggerEventListener<SwitchEvent>(CreatureIndex(this->_index, index), creature.GetValue());
|
|
HOOK(OnSwitchIn, creature.GetValue(), creature.GetValue());
|
|
}
|
|
|
|
bool BattleSide::CreatureOnSide(const ArbUt::BorrowedPtr<Creature>& creature) const {
|
|
return std::find(_creatures.begin(), _creatures.end(), creature.GetRaw()) != _creatures.end();
|
|
}
|
|
|
|
const ArbUt::OptionalBorrowedPtr<Creature>& BattleSide::GetCreature(u8 index) const { return _creatures[index]; }
|
|
|
|
void BattleSide::GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) {
|
|
GetOwnScripts(scripts);
|
|
_battle->GetActiveScripts(scripts);
|
|
}
|
|
void BattleSide::GetOwnScripts(ArbUt::List<ScriptWrapper>& scripts) {
|
|
scripts.Append(ScriptWrapper::FromSet(&_volatile));
|
|
}
|
|
size_t BattleSide::ScriptCount() const { return _battle->ScriptCount() + 1; }
|
|
|
|
u8 BattleSide::GetRandomCreatureIndex() {
|
|
// TODO: Consider adding parameter to only get index for available creatures.
|
|
return _battle->GetRandom()->Get(_creaturesPerSide);
|
|
}
|
|
bool BattleSide::SwapPositions(u8 a, u8 b) {
|
|
// If out of range, don't allow swapping.
|
|
if (a >= _creaturesPerSide || b >= _creaturesPerSide) {
|
|
return false;
|
|
}
|
|
|
|
// If the two indices are the same, don't allow swapping.
|
|
if (a == b) {
|
|
return false;
|
|
}
|
|
|
|
// Fetch parties for the two indices.
|
|
BattleParty* partyA = nullptr;
|
|
BattleParty* partyB = nullptr;
|
|
for (auto* party : this->_battle->GetParties()) {
|
|
if (party->IsResponsibleForIndex(_index, a)) {
|
|
partyA = party;
|
|
}
|
|
if (party->IsResponsibleForIndex(_index, b)) {
|
|
partyB = party;
|
|
}
|
|
}
|
|
// Don't allow swapping if different parties are responsible for the indices.
|
|
if (partyA != partyB) {
|
|
return false;
|
|
}
|
|
|
|
auto creatureA = _creatures[a];
|
|
_creatures[a] = _creatures[b];
|
|
_creatures[b] = creatureA;
|
|
_battle->TriggerEventListener<SwapEvent>(_index, a, b);
|
|
return true;
|
|
}
|
|
|
|
BattleSide* BattleSide::CloneWithoutCreatures(ArbUt::BorrowedPtr<Battle> battle) const {
|
|
auto* side = new BattleSide(_index, battle, _creaturesPerSide);
|
|
side->_choicesSet = _choicesSet;
|
|
_volatile.Clone(side, side->_volatile);
|
|
side->_hasFled = _hasFled;
|
|
return side;
|
|
}
|
|
|
|
BattleScript* BattleSide::AddVolatileScript(const ArbUt::StringView& key) {
|
|
auto script = _volatile.Get(key);
|
|
if (script.HasValue()) {
|
|
script.GetValue()->Stack();
|
|
return script.GetValue();
|
|
}
|
|
script = _battle->GetLibrary()->LoadScript(this, ScriptCategory::Side, key);
|
|
if (!script.HasValue()) {
|
|
THROW("Invalid volatile script requested for battleside: '", key.c_str(), "'.");
|
|
}
|
|
return _volatile.Add(script.GetValue());
|
|
}
|
|
BattleScript* BattleSide::AddVolatileScript(BattleScript* script) { return _volatile.Add(script); }
|
|
void BattleSide::RemoveVolatileScript(BattleScript* script) { _volatile.Remove(script->GetName()); }
|