Defensive programming.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-08-17 18:23:25 +02:00
parent f3721ad2a5
commit cc92cf1491
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
10 changed files with 67 additions and 26 deletions

View File

@ -0,0 +1,34 @@
#include "../../src/Library/Species/PokemonForme.hpp"
#include "../../src/Library/Species/LearnableMoves.hpp"
#include "../Core.hpp"
using namespace PkmnLib::Library;
export PokemonForme* PkmnLib_PokemonForme_Construct(const char* name, float height, float weight,
uint32_t baseExperience, uint8_t types[], size_t typeLength,
uint16_t baseHealth, uint16_t baseAttack, uint16_t baseDefense,
uint16_t baseMagicalAttack, uint16_t baseMagicalDefense,
uint16_t baseSpeed, const char* talents[], size_t talentsLength,
const char* secretTalents[], size_t secretTalentsLength,
const LearnableMoves* attacks, const char* flags[],
size_t flagsCount) {
std::unordered_set<uint32_t> conversedFlags(flagsCount);
for (size_t i = 0; i < flagsCount; i++) {
conversedFlags.insert(ArbUt::StringView::CalculateHash(flags[i]));
}
auto talentsWrapped = ArbUt::List<ArbUt::StringView>(talentsLength);
for (size_t i = 0; i < talentsLength; i++) {
talentsWrapped.Append(ArbUt::StringView(talents[i]));
}
auto secretTalentsWrapped = ArbUt::List<ArbUt::StringView>(secretTalentsLength);
for (size_t i = 0; i < secretTalentsLength; i++) {
secretTalentsWrapped.Append(ArbUt::StringView(secretTalents[i]));
}
return new PokemonForme(ArbUt::StringView(name), height, weight, baseExperience,
ArbUt::List<uint8_t>(types, types + typeLength),
CreatureLib::Library::StatisticSet<uint16_t>(
baseHealth, baseAttack, baseDefense, baseMagicalAttack, baseMagicalDefense, baseSpeed),
talentsWrapped, secretTalentsWrapped, attacks, conversedFlags);
}

View File

@ -21,7 +21,11 @@ namespace PkmnLib::Battling {
void SetWeather(const ArbUt::StringView& name);
void ClearWeather();
const ArbUt::StringView& GetWeatherName() noexcept { return _weatherScript->GetName(); }
const ArbUt::StringView& GetWeatherName() noexcept {
if (_weatherScript == nullptr)
return ArbUt::StringView::EmptyString();
return _weatherScript->GetName();
}
size_t ScriptCount() const override { return CreatureLib::Battling::Battle::ScriptCount() + 1; }
void GetActiveScripts(ArbUt::List<CreatureLib::Battling::ScriptWrapper>& scripts) override {

View File

@ -20,12 +20,15 @@ void PkmnLib::Battling::ExperienceLibrary::HandleExperienceGain(
const std::unordered_set<ArbUt::BorrowedPtr<CreatureLib::Battling::Creature>>& opponents) const {
auto fainted = dynamic_cast<Pokemon*>(faintedMon);
auto expGain = fainted->GetForme()->GetBaseExperience();
AssertNotNull(fainted);
auto& forme = fainted->GetForme();
AssertNotNull(forme);
auto expGain = forme->GetBaseExperience();
auto level = fainted->GetLevel();
float v1 = (expGain * level) / 5;
for (auto op : opponents) {
for (const auto& op : opponents) {
if (!op->AllowedExperienceGain())
continue;
auto experienceGain = CalculateDynamicExperience(level, v1, op, fainted);

View File

@ -37,7 +37,7 @@ namespace PkmnLib::Battling {
_friendship(species->GetBaseHappiness()) {}
const ArbUt::BorrowedPtr<const Library::PokemonForme> GetForme() const {
return _variant.As<const Library::PokemonForme>();
return _variant.ForceAs<const Library::PokemonForme>();
}
inline bool IsShiny() const noexcept { return _coloring == 1; }

View File

@ -7,7 +7,7 @@
#include "../Moves/MoveData.hpp"
namespace PkmnLib::Library {
class LearnableMoves : CreatureLib::Library::LearnableAttacks {
class LearnableMoves : public CreatureLib::Library::LearnableAttacks {
ArbUt::List<ArbUt::BorrowedPtr<const MoveData>> _eggMoves;
public:

View File

@ -5,7 +5,6 @@ PkmnLib::Library::PokemonForme::PokemonForme(const ArbUt::StringView& name, floa
CreatureLib::Library::StatisticSet<uint16_t> baseStats,
const ArbUt::List<ArbUt::StringView>& talents,
const ArbUt::List<ArbUt::StringView>& secretTalents,
const CreatureLib::Library::LearnableAttacks* attacks,
std::unordered_set<uint32_t> flags)
: SpeciesVariant(name, height, weight, baseExperience, types, baseStats, talents, secretTalents, attacks,
const LearnableMoves* moves, std::unordered_set<uint32_t> flags)
: SpeciesVariant(name, height, weight, baseExperience, types, baseStats, talents, secretTalents, moves,
std::move(flags)) {}

View File

@ -3,6 +3,7 @@
#include <CreatureLib/Library/CreatureData/SpeciesVariant.hpp>
#include <cstdint>
#include "LearnableMoves.hpp"
namespace PkmnLib::Library {
class PokemonForme : public CreatureLib::Library::SpeciesVariant {
@ -10,7 +11,7 @@ namespace PkmnLib::Library {
PokemonForme(const ArbUt::StringView& name, float height, float weight, uint32_t baseExperience,
const ArbUt::List<uint8_t>& types, CreatureLib::Library::StatisticSet<uint16_t> baseStats,
const ArbUt::List<ArbUt::StringView>& talents, const ArbUt::List<ArbUt::StringView>& secretTalents,
const CreatureLib::Library::LearnableAttacks* attacks, std::unordered_set<uint32_t> flags = {});
const LearnableMoves* moves, std::unordered_set<uint32_t> flags = {});
private:
public:

View File

@ -16,7 +16,7 @@ TEST_CASE("Able to build, destroy and insert library", "library") {
new PkmnLib::Library::PokemonForme(
"default"_cnc, 1.0f, 1.0f, 100, {0},
CreatureLib::Library::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100), {"testAbility"_cnc},
{"testHiddenAbility"_cnc}, new CreatureLib::Library::LearnableAttacks(100)),
{"testHiddenAbility"_cnc}, new PkmnLib::Library::LearnableMoves(100)),
0.5f, "testGrowthRate"_cnc, 100, 100, {"testEggGroup"_cnc}));
delete lib;
}
@ -29,7 +29,7 @@ TEST_CASE("Able to insert and retrieve from library", "library") {
new PkmnLib::Library::PokemonForme(
"default"_cnc, 1.0f, 1.0f, 100, {0},
CreatureLib::Library::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100), {"testAbility"_cnc},
{"testHiddenAbility"_cnc}, new CreatureLib::Library::LearnableAttacks(100)),
{"testHiddenAbility"_cnc}, new PkmnLib::Library::LearnableMoves(100)),
0.5f, "testGrowthRate"_cnc, 100, 100, {"testEggGroup"_cnc}));
auto val = lib->Get("foo"_cnc);
REQUIRE(val->GetName() == "foo");

View File

@ -8,7 +8,7 @@ TEST_CASE("Able to create and destroy species", "library") {
new PkmnLib::Library::PokemonForme("default"_cnc, 1.0f, 1.0f, 100, {0},
CreatureLib::Library::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100),
{"testAbility"_cnc}, {"testHiddenAbility"_cnc},
new CreatureLib::Library::LearnableAttacks(100)),
new PkmnLib::Library::LearnableMoves(100)),
0.5f, "testGrowthRate"_cnc, 100, 100, {"testEggGroup"_cnc});
delete species;
}
@ -19,7 +19,7 @@ TEST_CASE("Able to get default forme", "library") {
new PkmnLib::Library::PokemonForme("default"_cnc, 1.0f, 1.0f, 100, {0},
CreatureLib::Library::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100),
{"testAbility"_cnc}, {"testHiddenAbility"_cnc},
new CreatureLib::Library::LearnableAttacks(100)),
new PkmnLib::Library::LearnableMoves(100)),
0.5f, "testGrowthRate"_cnc, 100, 100, {"testEggGroup"_cnc});
auto forme = species->GetDefaultForme();
@ -34,7 +34,7 @@ TEST_CASE("Able to get default forme name", "library") {
new PkmnLib::Library::PokemonForme("default"_cnc, 1.0f, 1.0f, 100, {0},
CreatureLib::Library::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100),
{"testAbility"_cnc}, {"testHiddenAbility"_cnc},
new CreatureLib::Library::LearnableAttacks(100)),
new PkmnLib::Library::LearnableMoves(100)),
0.5f, "testGrowthRate"_cnc, 100, 100, {"testEggGroup"_cnc});
auto forme = species->GetDefaultForme();
@ -50,7 +50,7 @@ TEST_CASE("Able to get species name", "library") {
new PkmnLib::Library::PokemonForme("default"_cnc, 1.0f, 1.0f, 100, {0},
CreatureLib::Library::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100),
{"testAbility"_cnc}, {"testHiddenAbility"_cnc},
new CreatureLib::Library::LearnableAttacks(100)),
new PkmnLib::Library::LearnableMoves(100)),
0.5f, "testGrowthRate"_cnc, 100, 100, {"testEggGroup"_cnc});
REQUIRE(species->GetName() == "foo");
@ -64,7 +64,7 @@ TEST_CASE("Able to get species id", "library") {
new PkmnLib::Library::PokemonForme("default"_cnc, 1.0f, 1.0f, 100, {0},
CreatureLib::Library::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100),
{"testAbility"_cnc}, {"testHiddenAbility"_cnc},
new CreatureLib::Library::LearnableAttacks(100)),
new PkmnLib::Library::LearnableMoves(100)),
0.5f, "testGrowthRate"_cnc, 100, 100, {"testEggGroup"_cnc});
REQUIRE(species->GetId() == 1);
@ -78,7 +78,7 @@ TEST_CASE("Able to get species gender ratio", "library") {
new PkmnLib::Library::PokemonForme("default"_cnc, 1.0f, 1.0f, 100, {0},
CreatureLib::Library::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100),
{"testAbility"_cnc}, {"testHiddenAbility"_cnc},
new CreatureLib::Library::LearnableAttacks(100)),
new PkmnLib::Library::LearnableMoves(100)),
0.5f, "testGrowthRate"_cnc, 100, 100, {"testEggGroup"_cnc});
REQUIRE(species->GetGenderRate() == 0.5f);
@ -92,7 +92,7 @@ TEST_CASE("Able to get species growth rate", "library") {
new PkmnLib::Library::PokemonForme("default"_cnc, 1.0f, 1.0f, 100, {0},
CreatureLib::Library::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100),
{"testAbility"_cnc}, {"testHiddenAbility"_cnc},
new CreatureLib::Library::LearnableAttacks(100)),
new PkmnLib::Library::LearnableMoves(100)),
0.5f, "testGrowthRate"_cnc, 100, 100, {"testEggGroup"_cnc});
REQUIRE(species->GetGrowthRate() == "testGrowthRate");
@ -106,7 +106,7 @@ TEST_CASE("Able to get species capture rate", "library") {
new PkmnLib::Library::PokemonForme("default"_cnc, 1.0f, 1.0f, 100, {0},
CreatureLib::Library::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100),
{"testAbility"_cnc}, {"testHiddenAbility"_cnc},
new CreatureLib::Library::LearnableAttacks(100)),
new PkmnLib::Library::LearnableMoves(100)),
0.5f, "testGrowthRate"_cnc, 100, 100, {"testEggGroup"_cnc});
REQUIRE(species->GetCaptureRate() == 100);
@ -120,7 +120,7 @@ TEST_CASE("Able to get species base happiness", "library") {
new PkmnLib::Library::PokemonForme("default"_cnc, 1.0f, 1.0f, 100, {0},
CreatureLib::Library::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100),
{"testAbility"_cnc}, {"testHiddenAbility"_cnc},
new CreatureLib::Library::LearnableAttacks(100)),
new PkmnLib::Library::LearnableMoves(100)),
0.5f, "testGrowthRate"_cnc, 100, 100, {"testEggGroup"_cnc});
REQUIRE(species->GetBaseHappiness() == 100);
@ -134,14 +134,14 @@ TEST_CASE("Able to set and get evolution", "library") {
new PkmnLib::Library::PokemonForme("default"_cnc, 1.0f, 1.0f, 100, {0},
CreatureLib::Library::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100),
{"testAbility"_cnc}, {"testHiddenAbility"_cnc},
new CreatureLib::Library::LearnableAttacks(100)),
new PkmnLib::Library::LearnableMoves(100)),
0.5f, "testGrowthRate"_cnc, 100, 100, {"testEggGroup"_cnc});
auto species2 = new PkmnLib::Library::PokemonSpecies(
2, "bar"_cnc,
new PkmnLib::Library::PokemonForme("default"_cnc, 1.0f, 1.0f, 100, {0},
CreatureLib::Library::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100),
{"testAbility"_cnc}, {"testHiddenAbility"_cnc},
new CreatureLib::Library::LearnableAttacks(100)),
new PkmnLib::Library::LearnableMoves(100)),
0.5f, "testGrowthRate"_cnc, 100, 100, {"testEggGroup"_cnc});
species->AddEvolution(PkmnLib::Library::EvolutionData::CreateLevelEvolution(16, species2));

View File

@ -10,7 +10,7 @@ PkmnLib::Library::SpeciesLibrary* TestLibrary::BuildSpeciesLibrary() {
new PkmnLib::Library::PokemonForme(
"default"_cnc, 1.0f, 1.0f, 236, {0},
CreatureLib::Library::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100), {"testAbility"_cnc},
{"testHiddenAbility"_cnc}, new CreatureLib::Library::LearnableAttacks(100)),
{"testHiddenAbility"_cnc}, new PkmnLib::Library::LearnableMoves(100)),
0.5f, "testGrowthRate"_cnc, 100, 100, {"testEggGroup"_cnc}));
lib->Insert("testSpecies2"_cnc,
new PkmnLib::Library::PokemonSpecies(
@ -18,7 +18,7 @@ PkmnLib::Library::SpeciesLibrary* TestLibrary::BuildSpeciesLibrary() {
new PkmnLib::Library::PokemonForme(
"default"_cnc, 1.0f, 1.0f, 306, {0},
CreatureLib::Library::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100), {"testAbility"_cnc},
{"testHiddenAbility"_cnc}, new CreatureLib::Library::LearnableAttacks(100)),
{"testHiddenAbility"_cnc}, new PkmnLib::Library::LearnableMoves(100)),
0.5f, "testGrowthRate"_cnc, 100, 100, {"testEggGroup"_cnc}));
lib->Insert("statTestSpecies1"_cnc,
new PkmnLib::Library::PokemonSpecies(
@ -26,7 +26,7 @@ PkmnLib::Library::SpeciesLibrary* TestLibrary::BuildSpeciesLibrary() {
new PkmnLib::Library::PokemonForme(
"default"_cnc, 1.0f, 1.0f, 236, {0},
CreatureLib::Library::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100), {"testAbility"_cnc},
{"testHiddenAbility"_cnc}, new CreatureLib::Library::LearnableAttacks(100)),
{"testHiddenAbility"_cnc}, new PkmnLib::Library::LearnableMoves(100)),
0.5f, "testGrowthRate"_cnc, 100, 100, {"testEggGroup"_cnc}));
lib->Insert("testSpecies3"_cnc,
new PkmnLib::Library::PokemonSpecies(
@ -34,7 +34,7 @@ PkmnLib::Library::SpeciesLibrary* TestLibrary::BuildSpeciesLibrary() {
new PkmnLib::Library::PokemonForme(
"default"_cnc, 1.0f, 1.0f, 236, {0, 4},
CreatureLib::Library::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100), {"testAbility"_cnc},
{"testHiddenAbility"_cnc}, new CreatureLib::Library::LearnableAttacks(100)),
{"testHiddenAbility"_cnc}, new PkmnLib::Library::LearnableMoves(100)),
0.5f, "testGrowthRate"_cnc, 100, 100, {"testEggGroup"_cnc}));
return lib;