Update to new Arbutils memory model.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2020-12-12 14:25:27 +01:00
parent b6a5e41b51
commit 53bd6e7a94
19 changed files with 87 additions and 74 deletions

View File

@@ -83,7 +83,8 @@ float PkmnLib::Battling::DamageLibrary::GetDamageModifier(CreatureLib::Battling:
PKMN_HOOK(OverrideCriticalModifier, attack, attack, target, hitIndex, &critModifier);
mod *= critModifier;
}
float randPercentage = 85 + attack->GetUser()->GetBattle()->GetRandom()->Get(0, 16);
Assert(attack->GetUser()->GetBattle().GetValue());
float randPercentage = 85 + attack->GetUser()->GetBattle().GetValue()->GetRandom()->Get(0, 16);
mod *= randPercentage / 100.0;
if (attack->GetUser()->HasType(hitData.GetType())) {
float stabModifier = 1.5;

View File

@@ -36,12 +36,12 @@ void PkmnLib::Battling::ExperienceLibrary::HandleExperienceGain(
}
auto battle = fainted->GetBattle();
if (battle == nullptr) {
if (!battle.HasValue()) {
return;
}
std::unordered_set<CreatureLib::Battling::Creature*> shareExperience;
for (const auto& party : battle->GetParties()) {
for (const auto& party : battle.GetValue()->GetParties()) {
for (const auto& mon : party->GetParty()->GetParty()) {
if (mon == nullptr)
continue;

View File

@@ -7,7 +7,8 @@ bool PkmnLib::Battling::MiscLibrary::IsCritical(CreatureLib::Battling::Executing
CreatureLib::Battling::Creature* target, uint8_t hit) const {
uint8_t critStage = 0;
PKMN_HOOK(ModifyCriticalStage, attack, attack, target, hit, &critStage);
auto rand = target->GetBattle()->GetRandom();
Assert(target->GetBattle().HasValue());
auto rand = target->GetBattle().GetValue()->GetRandom();
switch (critStage) {
case 0: return rand->Get(24) == 0;
case 1: return rand->Get(8) == 0;

View File

@@ -36,14 +36,15 @@ namespace PkmnLib::Battling {
Pokemon* CreatePokemon::Build() {
auto rand = ArbUt::Random();
ArbUt::BorrowedPtr<const Library::PokemonSpecies> species = nullptr;
if (!this->_library->GetSpeciesLibrary()->TryGet(this->_species, species)) {
auto species = this->_library->GetSpeciesLibrary()->TryGet(this->_species);
if (!species.has_value()) {
std::stringstream err;
err << "Invalid species '" << _species << "'.";
throw ArbUt::Exception(err.str());
}
ArbUt::BorrowedPtr<const PkmnLib::Library::PokemonForme> forme;
if (!species->TryGetForme(this->_forme, forme)) {
auto forme = species.value()->TryGetForme(this->_forme);
if (!forme.has_value()) {
std::stringstream err;
err << "Invalid forme '" << _forme << "' for species '" << _forme << "'.";
throw ArbUt::Exception(err.str());
@@ -51,9 +52,9 @@ namespace PkmnLib::Battling {
AssertNotNull(forme);
CreatureLib::Library::TalentIndex ability;
if (this->_ability.IsEmpty()) {
ability = forme->GetRandomTalent(rand);
ability = forme.value()->GetRandomTalent(rand);
} else {
ability = forme->GetTalentIndex(this->_ability);
ability = forme.value()->GetTalentIndex(this->_ability);
}
auto identifier = this->_identifier;
if (identifier == 0) {
@@ -61,16 +62,18 @@ namespace PkmnLib::Battling {
}
auto gender = this->_gender;
if (gender == static_cast<CreatureLib::Library::Gender>(-1)) {
gender = species->GetRandomGender(rand);
gender = species.value()->GetRandomGender(rand);
}
ArbUt::BorrowedPtr<const Library::Item> heldItem = nullptr;
ArbUt::OptionalBorrowedPtr<const Library::Item> heldItem = nullptr;
if (!this->_heldItem.IsEmpty()) {
if (!_library->GetItemLibrary()->TryGet(this->_heldItem, heldItem)) {
auto item = _library->GetItemLibrary()->TryGet(this->_heldItem);
if (!item.has_value()) {
THROW("Unknown Item: " << this->_heldItem.std_str());
}
AssertNotNull(heldItem);
heldItem = item.value();
}
auto experience = _library->GetGrowthRateLibrary()->CalculateExperience(species->GetGrowthRate(), _level);
auto experience =
_library->GetGrowthRateLibrary()->CalculateExperience(species.value()->GetGrowthRate(), _level);
auto attacks = std::vector<CreatureLib::Battling::LearnedAttack*>(_attacks.Count());
for (size_t i = 0; i < _attacks.Count(); i++) {
@@ -98,8 +101,8 @@ namespace PkmnLib::Battling {
shiny = rand.Get(_library->GetSettings()->GetShinyRate()) == 0;
}
auto pkmn = new Pokemon(_library, species, forme, _level, experience, identifier, gender, shiny, heldItem,
_nickname, ability, attacks, ivs, evs, nature, _allowedExperienceGain);
auto pkmn = new Pokemon(_library, species.value(), forme.value(), _level, experience, identifier, gender, shiny,
heldItem, _nickname, ability, attacks, ivs, evs, nature, _allowedExperienceGain);
pkmn->Initialize();
return pkmn;
}
@@ -146,15 +149,15 @@ namespace PkmnLib::Battling {
}
CreatePokemon& CreatePokemon::LearnMove(const ArbUt::StringView& moveName,
CreatureLib::Battling::AttackLearnMethod method) {
ArbUt::BorrowedPtr<const PkmnLib::Library::MoveData> move = nullptr;
if (!_library->GetMoveLibrary()->TryGet(moveName, move)) {
auto v = _library->GetMoveLibrary()->TryGet(moveName);
if (!v.has_value()) {
THROW("Invalid Move given: " << moveName.std_str());
}
if (_currentMove >= _library->GetSettings()->GetMaximalAttacks()) {
throw ArbUt::Exception("This pokemon already has the maximal allowed moves.");
}
Assert(move != nullptr);
_attacks.Append(ToLearnMethod(move, method));
Assert(v.value() != nullptr);
_attacks.Append(ToLearnMethod(v.value(), method));
return *this;
}
CreatePokemon& CreatePokemon::IsAllowedExperienceGain(bool value) {

View File

@@ -20,7 +20,6 @@ namespace PkmnLib::Battling {
struct ToLearnMethod {
ArbUt::BorrowedPtr<const Library::MoveData> Move;
CreatureLib::Battling::AttackLearnMethod LearnMethod;
ToLearnMethod() : Move(nullptr), LearnMethod(CreatureLib::Battling::AttackLearnMethod::Unknown){};
ToLearnMethod(ArbUt::BorrowedPtr<const Library::MoveData> move,
CreatureLib::Battling::AttackLearnMethod method)
: Move(move), LearnMethod(method){};

View File

@@ -12,8 +12,8 @@ void PkmnLib::Battling::Pokemon::Evolve(ArbUt::BorrowedPtr<const Library::Pokemo
// If the pokemon is genderless, but it's new evolution is not, we want to set its gender
if (_gender != CreatureLib::Library::Gender::Genderless && _species->GetGenderRate() != -1) {
// If we are currently in battle, use the battle random so we can get predictable events.
if (!_battle.IsNull()) {
_gender = _species->GetRandomGender(_battle->GetRandom()->GetRNG());
if (_battle.HasValue()) {
_gender = _species->GetRandomGender(_battle.GetValue()->GetRandom()->GetRNG());
}
// Else create a new random.
else {
@@ -34,8 +34,8 @@ void PkmnLib::Battling::Pokemon::SetStatus(const ArbUt::StringView& name) {
}
_statusScript = std::unique_ptr<CreatureLib::Battling::Script>(
_library->LoadScript(static_cast<ScriptCategory>(PkmnScriptCategory::Status), name));
if (_battle != nullptr) {
_battle->TriggerEventListener<StatusChangeEvent>(this, name);
if (_battle.HasValue()) {
_battle.GetValue()->TriggerEventListener<StatusChangeEvent>(this, name);
}
}
void PkmnLib::Battling::Pokemon::ClearStatus() {
@@ -43,7 +43,7 @@ void PkmnLib::Battling::Pokemon::ClearStatus() {
return;
_statusScript->OnRemove();
_statusScript = nullptr;
if (_battle != nullptr) {
_battle->TriggerEventListener<StatusChangeEvent>(this, ""_cnc);
if (_battle.HasValue()) {
_battle.GetValue()->TriggerEventListener<StatusChangeEvent>(this, ""_cnc);
}
}

View File

@@ -21,7 +21,7 @@ namespace PkmnLib::Battling {
const ArbUt::BorrowedPtr<const Library::PokemonSpecies>& species,
const ArbUt::BorrowedPtr<const Library::PokemonForme>& forme, level_int_t level, uint32_t experience,
uint32_t uid, CreatureLib::Library::Gender gender, uint8_t coloring,
ArbUt::BorrowedPtr<const Library::Item> heldItem, const std::string& nickname,
ArbUt::OptionalBorrowedPtr<const Library::Item> heldItem, const std::string& nickname,
const CreatureLib::Library::TalentIndex& talent,
const std::vector<CreatureLib::Battling::LearnedAttack*>& moves,
CreatureLib::Library::ClampedStatisticSet<uint8_t, 0, 31> individualValues,