More tests for Pokemon class in AngelScript.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Deukhoofd 2020-02-02 13:57:41 +01:00
parent aae96333b1
commit 419e573bbe
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
7 changed files with 112 additions and 16 deletions

View File

@ -40,7 +40,7 @@ class PkmnLibConan(ConanFile):
self.options["AngelScript"].link_std_statically = True
def requirements(self):
self.requires("CreatureLib/bb9b9609a6e595ff8e6d4f4682b4be5b5e8d9d7e@creaturelib/master")
self.requires("CreatureLib/fc5826edb1d164a261a33aebb164af7df8ab7ef4@creaturelib/master")
if self.options.script_handler == "angelscript":
self.requires("AngelScript/2.34@AngelScript/Deukhoofd")
else:

View File

@ -40,7 +40,7 @@ PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::WithEffortVa
PkmnLib::Battling::Pokemon* PkmnLib::Battling::CreatePokemon::Build() {
auto rand = CreatureLib::Core::Random();
const PkmnLib::Library::PokemonSpecies* species = nullptr;
if (!this->_library->GetSpeciesLibrary()->TryGetPkmnSpecies(this->_species, species)){
if (!this->_library->GetSpeciesLibrary()->TryGetPkmnSpecies(this->_species, species)) {
throw CreatureException("Invalid species: " + _species);
}
auto forme = species->GetForme(this->_forme);
@ -60,7 +60,7 @@ PkmnLib::Battling::Pokemon* PkmnLib::Battling::CreatePokemon::Build() {
}
const Library::Item* heldItem = nullptr;
if (!this->_heldItem.empty()) {
if (!_library->GetItemLibrary()->TryGetItem(this->_heldItem, heldItem)){
if (!_library->GetItemLibrary()->TryGetItem(this->_heldItem, heldItem)) {
throw CreatureException("Unknown Item: " + this->_heldItem);
}
}
@ -79,10 +79,9 @@ PkmnLib::Battling::Pokemon* PkmnLib::Battling::CreatePokemon::Build() {
}
auto shiny = false;
if (_shininessSet){
if (_shininessSet) {
shiny = _isShiny;
}
else{
} else {
shiny = rand.Get(_library->GetSettings()->GetShinyRate()) == 0;
}
@ -134,3 +133,15 @@ PkmnLib::Battling::CreatePokemon* PkmnLib::Battling::CreatePokemon::WithHeldItem
_heldItem = item;
return this;
}
PkmnLib::Battling::CreatePokemon*
PkmnLib::Battling::CreatePokemon::LearnMove(const std::string& moveName, CreatureLib::Battling::AttackLearnMethod method) {
const PkmnLib::Library::MoveData* move;
if (!_library->GetMoveLibrary()->TryGetMove(moveName, move)){
throw CreatureException("Invalid Move given: " + moveName);
}
if (_attacks.size() >= _library->GetSettings()->GetMaximalMoves()){
throw CreatureException("This pokemon already has the maximal allowed moves.");
}
_attacks.emplace_back(move, method);
return this;
}

View File

@ -44,6 +44,7 @@ namespace PkmnLib::Battling {
CreatePokemon* WithGender(CreatureLib::Library::Gender gender);
CreatePokemon* IsShiny(bool value);
CreatePokemon* WithHeldItem(const std::string& item);
CreatePokemon* LearnMove(const std::string& move, CreatureLib::Battling::AttackLearnMethod method);
CreatePokemon* WithRandomIndividualValues(CreatureLib::Core::Random rand = CreatureLib::Core::Random());
CreatePokemon* WithIndividualValue(CreatureLib::Core::Statistic stat, uint8_t value);

View File

@ -33,8 +33,10 @@ namespace PkmnLib::Battling {
return dynamic_cast<const Library::PokemonForme*>(GetVariant());
}
const bool IsShiny() const{
return _coloring == 1;
const bool IsShiny() const { return _coloring == 1; }
const std::vector<const LearnedMove*>& GetMoves() const {
return (const std::vector<const LearnedMove*>&)_attacks;
}
const Library::Nature& GetNature() const;

View File

@ -68,6 +68,22 @@ CScriptArray* GetTypes(const PkmnLib::Battling::Pokemon* obj) {
return nullptr;
}
CScriptArray* GetMoves(const PkmnLib::Battling::Pokemon* obj) {
asIScriptContext* ctx = asGetActiveContext();
if (ctx) {
asIScriptEngine* engine = ctx->GetEngine();
asITypeInfo* t = engine->GetTypeInfoByDecl("array<LearnedMove@>");
auto a = obj->GetMoves();
CScriptArray* arr = CScriptArray::Create(t, a.size());
for (size_t i = 0; i < a.size(); i++){
arr->SetValue(i, &a[i]);
}
return arr;
}
return nullptr;
}
void RegisterPokemonClass::RegisterPokemonType(asIScriptEngine* engine) {
[[maybe_unused]] int r = engine->RegisterObjectType("Pokemon", 0, asOBJ_REF | asOBJ_NOCOUNT);
assert(r >= 0);
@ -119,8 +135,8 @@ void RegisterPokemonClass::RegisterPokemonType(asIScriptEngine* engine) {
r = engine->RegisterObjectMethod("Pokemon", "void OverrideActiveAbility(const string &in ability)",
asMETHOD(PkmnLib::Battling::Pokemon, OverrideActiveTalent), asCALL_THISCALL);
assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "LearnedMove@[]@ get_Moves() const property",
asMETHOD(PkmnLib::Battling::Pokemon, GetAttacks), asCALL_THISCALL);
r = engine->RegisterObjectMethod("Pokemon", "LearnedMove@[]@ GetMoves() const",
asFUNCTION(GetMoves), asCALL_CDECL_OBJLAST);
assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "const Species@ get_DisplaySpecies() const property",
asMETHOD(PkmnLib::Battling::Pokemon, GetDisplaySpecies), asCALL_THISCALL);

View File

@ -1,7 +1,7 @@
#ifdef TESTS_BUILD
#include "../../../../extern/catch.hpp"
#include "../../../../src/ScriptResolving/AngelScript/AngelScripResolver.hpp"
#include "../../../../src/Battling/Pokemon/CreatePokemon.hpp"
#include "../../../../src/ScriptResolving/AngelScript/AngelScripResolver.hpp"
#include "../../../TestLibrary/TestLibrary.hpp"
static std::unordered_map<const char*, const char*> _scripts =
@ -19,6 +19,10 @@ class testScript1 {
bool testActiveAbility(Pokemon@ p, const string &in ability){ return p.ActiveAbility == ability; }
bool testIsFainted(Pokemon@ p, bool b){ return p.IsFainted == b; }
bool testType(Pokemon@ p, uint index, uint8 type){ return p.GetTypes()[index] == type; }
bool testHasType(Pokemon@ p, uint8 type){ return p.HasType(type); }
void testDamage(Pokemon@ p, uint32 damage, DamageSource source){ p.Damage(damage, source); }
bool testMove(Pokemon@ p, uint index, LearnedMove@ move){ return p.GetMoves()[index] is move; }
}
)"}};
@ -235,7 +239,7 @@ TEST_CASE("Validate Pokemon GetTypes in Script") {
.WithForme("default")
->WithGender(CreatureLib::Library::Gender::Male)
->Build();
for (size_t i = 0; i < mon->GetTypes().size(); i++){
for (size_t i = 0; i < mon->GetTypes().size(); i++) {
auto data = GetScript(mainLib, "testType");
data.Context->SetArgObject(0, const_cast<PkmnLib::Battling::Pokemon*>(mon));
@ -244,12 +248,71 @@ TEST_CASE("Validate Pokemon GetTypes in Script") {
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
REQUIRE((bool)data.Context->GetReturnWord());
}
delete mon;
}
TEST_CASE("Validate Pokemon HasType in Script") {
auto mainLib = TestLibrary::GetLibrary();
auto mon = PkmnLib::Battling::CreatePokemon(mainLib, "testSpecies3", 30)
.WithForme("default")
->WithGender(CreatureLib::Library::Gender::Male)
->Build();
for (size_t i = 0; i < mon->GetTypes().size(); i++) {
auto data = GetScript(mainLib, "testHasType");
data.Context->SetArgObject(0, const_cast<PkmnLib::Battling::Pokemon*>(mon));
data.Context->SetArgByte(1, mon->GetTypes()[i]);
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
REQUIRE((bool)data.Context->GetReturnWord());
}
delete mon;
}
TEST_CASE("Validate Pokemon Damage in Script") {
auto mainLib = TestLibrary::GetLibrary();
auto mon = PkmnLib::Battling::CreatePokemon(mainLib, "testSpecies3", 30)
.WithForme("default")
->WithGender(CreatureLib::Library::Gender::Male)
->Build();
auto data = GetScript(mainLib, "testDamage");
data.Context->SetArgObject(0, const_cast<PkmnLib::Battling::Pokemon*>(mon));
data.Context->SetArgDWord(1, 30);
data.Context->SetArgDWord(2, (asDWORD)CreatureLib::Battling::DamageSource::AttackDamage);
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
REQUIRE(mon->GetCurrentHealth() == mon->GetBoostedStat(PkmnLib::Library::Statistic::HealthPoints) - 30);
delete mon;
}
TEST_CASE("Validate Pokemon GetMoves in Script") {
auto mainLib = TestLibrary::GetLibrary();
auto mon = PkmnLib::Battling::CreatePokemon(mainLib, "testSpecies3", 30)
.WithForme("default")
->LearnMove("testMove", CreatureLib::Battling::AttackLearnMethod::Level)
->LearnMove("testMove2", CreatureLib::Battling::AttackLearnMethod::Unknown)
->Build();
for (size_t i = 0; i < mon->GetMoves().size(); i++) {
auto data = GetScript(mainLib, "testMove");
data.Context->SetArgObject(0, const_cast<PkmnLib::Battling::Pokemon*>(mon));
data.Context->SetArgDWord(1, i);
data.Context->SetArgObject(2, (void*)mon->GetMoves()[i]);
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
REQUIRE((bool)data.Context->GetReturnWord());
}
delete mon;
}
#endif

View File

@ -43,6 +43,9 @@ PkmnLib::Library::MoveLibrary* TestLibrary::BuildMoveLibrary() {
lib->LoadMove("testMove",
new PkmnLib::Library::MoveData("testMove", 0, PkmnLib::Library::MoveCategory::Physical, 50, 100, 20,
CreatureLib::Library::AttackTarget::Adjacent, 0, {}));
lib->LoadMove("testMove2",
new PkmnLib::Library::MoveData("testMove2", 0, PkmnLib::Library::MoveCategory::Special, 30, 100, 10,
CreatureLib::Library::AttackTarget::Adjacent, 0, {}));
return lib;
}
PkmnLib::Library::ItemLibrary* TestLibrary::BuildItemLibrary() {