Rework AngelScript effect names to be not based on script names, but on attributes instead.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Deukhoofd 2020-04-10 14:57:20 +02:00
parent 72ef9599ed
commit 149b0f08f6
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
15 changed files with 131 additions and 48 deletions

View File

@ -7,7 +7,7 @@ namespace PkmnLib::Battling {
class CreatePokemon {
private:
const BattleLibrary* _library;
Arbutils::CaseInsensitiveConstString _species;
Arbutils::CaseInsensitiveConstString _species = ""_cnc;
Arbutils::CaseInsensitiveConstString _forme = "default"_cnc;
uint8_t _level;
std::string _nickname = "";

View File

@ -23,7 +23,7 @@ namespace PkmnLib::Library {
inline uint8_t GetBaseHappiness() const { return _baseHappiness; }
inline const PokemonForme* GetDefaultForme() const {
return reinterpret_cast<const PokemonForme*>(CreatureSpecies::GetVariant("default"_cnc));
return reinterpret_cast<const PokemonForme*>(CreatureSpecies::GetVariant("default"_cnc.GetHash()));
}
inline bool HasForme(const Arbutils::CaseInsensitiveConstString& key) const { return HasVariant(key); }

View File

@ -1,6 +1,7 @@
#include "AngelScripResolver.hpp"
#include <CreatureLib/Battling/Models/Creature.hpp>
#include <cassert>
#include <regex>
#include "../../../extern/angelscript_addons/scriptarray/scriptarray.h"
#include "../../../extern/angelscript_addons/scripthandle/scripthandle.h"
#include "../../../extern/angelscript_addons/scripthelper/scripthelper.h"
@ -24,6 +25,10 @@ CreatureLib::Battling::ScriptResolver* PkmnLib::Battling::BattleLibrary::CreateS
}
void AngelScripResolver::Initialize(CreatureLib::Battling::BattleLibrary* arg) {
for (auto scriptCategory : ScriptCategoryHelper::GetValues()) {
_typeDatabase.Insert(scriptCategory, {});
}
auto library = (PkmnLib::Battling::BattleLibrary*)arg;
_engine = asCreateScriptEngine();
@ -87,20 +92,6 @@ void AngelScripResolver::RegisterTypes() {
BasicScriptClass::Register(_engine);
}
AngelScriptTypeInfo* AngelScripResolver::GetTypeInfo(const ConstString& name, const std::string& decl) {
auto find = _types.find(name);
if (find != _types.end()) {
return find->second;
}
auto type = _mainModule->GetTypeInfoByDecl(decl.c_str());
if (type == nullptr) {
_types.insert({name, nullptr});
return nullptr;
}
auto typeinfo = new AngelScriptTypeInfo(name, type);
_types.insert({name, typeinfo});
return typeinfo;
}
void AngelScripResolver::MessageCallback(const asSMessageInfo* msg, void* param) {
const char* type = "ERR ";
if (msg->type == asMSGTYPE_WARNING)
@ -110,33 +101,78 @@ void AngelScripResolver::MessageCallback(const asSMessageInfo* msg, void* param)
printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message);
}
static constexpr const char* GetCategoryNamespace(ScriptCategory category) {
switch (category) {
case ScriptCategory::Attack: return "Moves";
case ScriptCategory::Talent: return "Abilities";
case ScriptCategory::Status: return "Status";
case ScriptCategory::Creature: return "Pokemon";
case ScriptCategory::Battle: return "Battle";
case ScriptCategory::Side: return "Side";
default: throw CreatureException("Unknown script category");
}
}
CreatureLib::Battling::Script* AngelScripResolver::LoadScript(ScriptCategory category, const ConstString& scriptName) {
std::stringstream decl;
decl << GetCategoryNamespace(category) << "::" << scriptName.c_str();
auto typeInfo = GetTypeInfo(scriptName, decl.str());
if (typeInfo == nullptr)
Dictionary<uint32_t, AngelScriptTypeInfo*> innerDb;
if (!_typeDatabase.TryGet(category, innerDb)) {
_typeDatabase.Insert(category, innerDb);
return nullptr;
}
AngelScriptTypeInfo* t;
if (!innerDb.TryGet(scriptName, t)) {
innerDb.Insert(scriptName, nullptr);
return nullptr;
}
if (t == nullptr) {
return nullptr;
}
auto ctx = _contextPool->RequestContext();
auto obj = typeInfo->Instantiate(ctx);
auto obj = t->Instantiate(ctx);
_contextPool->ReturnContextToPool(ctx);
return new AngelScriptScript(typeInfo, obj, _contextPool);
return new AngelScriptScript(t, obj, _contextPool);
}
void AngelScripResolver::FinalizeModule() {
int r = _builder.BuildModule();
if (r < 0)
throw CreatureException("Building Script Module failed.");
int count = _mainModule->GetObjectTypeCount();
std::regex metadataMatcher("^\\s*(\\w+)([\\w\\s=]*)$", std::regex_constants::icase);
std::regex variableMatcher("\\s*(\\w+)=(\\w+)", std::regex_constants::icase);
std::smatch base_match;
for (int n = 0; n < count; n++) {
auto typeInfo = _mainModule->GetObjectTypeByIndex(n);
auto metadata = _builder.GetMetadataForType(typeInfo->GetTypeId());
for (int m = 0; m < metadata.size(); m++) {
auto data = metadata[m];
if (std::regex_match(data, base_match, metadataMatcher)) {
auto metadataKind = base_match[1].str();
auto metadataVariables = base_match[2].str();
if (!std::regex_match(metadataVariables, base_match, variableMatcher)) {
continue;
}
ConstString effectName;
for (size_t variableIndex = 1; variableIndex < base_match.size(); variableIndex += 2) {
if (base_match[variableIndex] == "effect") {
auto val = base_match[variableIndex + 1].str();
effectName = ConstString(val);
}
}
if (effectName.Empty()) {
continue;
}
if (metadataKind == "Move") {
_typeDatabase[ScriptCategory::Attack].Insert(effectName,
new AngelScriptTypeInfo(effectName, typeInfo));
} else if (metadataKind == "Pokemon") {
_typeDatabase[ScriptCategory::Creature].Insert(effectName,
new AngelScriptTypeInfo(effectName, typeInfo));
} else if (metadataKind == "Ability") {
_typeDatabase[ScriptCategory::Talent].Insert(effectName,
new AngelScriptTypeInfo(effectName, typeInfo));
} else if (metadataKind == "Status") {
_typeDatabase[ScriptCategory::Status].Insert(effectName,
new AngelScriptTypeInfo(effectName, typeInfo));
} else if (metadataKind == "Battle") {
_typeDatabase[ScriptCategory::Battle].Insert(effectName,
new AngelScriptTypeInfo(effectName, typeInfo));
} else if (metadataKind == "Side") {
_typeDatabase[ScriptCategory::Side].Insert(effectName,
new AngelScriptTypeInfo(effectName, typeInfo));
}
}
}
}
}
void AngelScripResolver::CreateScript(const char* name, const char* script) {
_builder.AddSectionFromMemory(name, script);

View File

@ -10,6 +10,7 @@
#include <iostream>
#include "AngelScriptScript.hpp"
#include "AngelScriptTypeInfo.hpp"
using namespace Arbutils::Collections;
class AngelScripResolver : public CreatureLib::Battling::ScriptResolver {
private:
@ -17,21 +18,21 @@ private:
asIScriptModule* _mainModule = nullptr;
ContextPool* _contextPool = nullptr;
CScriptBuilder _builder;
std::unordered_map<ConstString, AngelScriptTypeInfo*> _types;
static void MessageCallback(const asSMessageInfo* msg, void* param);
static void Print(const std::string& str) { std::cout << str << std::endl; }
AngelScriptTypeInfo* GetTypeInfo(const ConstString& name, const std::string& decl);
Dictionary<ScriptCategory, Dictionary<uint32_t, AngelScriptTypeInfo*>> _typeDatabase;
void RegisterTypes();
public:
~AngelScripResolver() override {
delete _contextPool;
for (const auto& type : _types) {
delete type.second;
for (const auto& category : _typeDatabase) {
for (const auto& type : category.second){
delete type.second;
}
}
_types.clear();
_engine->ShutDownAndRelease();
}

View File

@ -15,7 +15,7 @@ using ConstString = Arbutils::CaseInsensitiveConstString;
class AngelScriptTypeInfo {
private:
asITypeInfo* _type = nullptr;
Arbutils::Collections::Dictionary<ConstString, asIScriptFunction*> _functions;
Arbutils::Collections::Dictionary<uint32_t, asIScriptFunction*> _functions;
ConstString _name;
struct FunctionInfo {

View File

@ -5,7 +5,7 @@
#include "../TestLibrary/TestLibrary.hpp"
#define AS_CLASS(name, contents) \
{ #name, "namespace Pokemon{ class " #name " : PkmnScript { " contents "}}" }
{ #name, "namespace Pokemon{ [Pokemon effect=" #name "] class " #name " : PkmnScript { " contents "}}" }
static std::unordered_map<const char*, const char*> _scripts = std::unordered_map<const char*, const char*>{
AS_CLASS(blankScript, ),
@ -20,6 +20,7 @@ class doubleInheritanceScriptBase : PkmnScript {
int GetValue(){ return value; }
}
[Pokemon effect=doubleInheritanceScript]
class doubleInheritanceScript : doubleInheritanceScriptBase {}
)"},
AS_CLASS(preventAttackScript,
@ -78,12 +79,16 @@ static AngelScriptScript* GetScript(PkmnLib::Battling::BattleLibrary* mainLib, c
auto lib = GetScriptResolver(mainLib);
auto s = lib->LoadScript(ScriptCategory::Creature, scriptName);
auto script = dynamic_cast<AngelScriptScript*>(s);
REQUIRE(script != nullptr);
return script;
}
TEST_CASE("Invoke non-implemented script function") {
auto mainLib = TestLibrary::GetLibrary();
auto script = GetScript(mainLib, "blankScript"_cnc);
REQUIRE(script != nullptr);
script->Stack();
delete script;
}
@ -91,6 +96,8 @@ TEST_CASE("Invoke non-implemented script function") {
TEST_CASE("Invoke Stack script function") {
auto mainLib = TestLibrary::GetLibrary();
auto script = GetScript(mainLib, "stackScript"_cnc);
REQUIRE(script != nullptr);
for (int i = 1; i <= 10; i++) {
script->Stack();
@ -108,6 +115,8 @@ TEST_CASE("Invoke Stack script function") {
TEST_CASE("Invoke OnRemove script function") {
auto mainLib = TestLibrary::GetLibrary();
auto script = GetScript(mainLib, "onRemoveScript"_cnc);
REQUIRE(script != nullptr);
script->OnRemove();
auto ctxPool = script->GetContextPool();
@ -160,6 +169,7 @@ TEST_CASE("Invoke StopBeforeAttack script function") {
TEST_CASE("Invoke OnBeforeAttack script function") {
auto mainLib = TestLibrary::GetLibrary();
auto script = GetScript(mainLib, "OnBeforeAttackScript"_cnc);
REQUIRE(script != nullptr);
script->OnBeforeAttack(nullptr);
@ -176,6 +186,7 @@ TEST_CASE("Invoke OnBeforeAttack script function") {
TEST_CASE("Invoke FailIncomingAttack script function") {
auto mainLib = TestLibrary::GetLibrary();
auto script = GetScript(mainLib, "FailIncomingAttackScript"_cnc);
REQUIRE(script != nullptr);
bool b = false;
script->FailIncomingAttack(nullptr, nullptr, &b);
REQUIRE(b);
@ -186,6 +197,7 @@ TEST_CASE("Invoke FailIncomingAttack script function") {
TEST_CASE("Invoke OnAttackMiss script function") {
auto mainLib = TestLibrary::GetLibrary();
auto script = GetScript(mainLib, "OnAttackMissScript"_cnc);
REQUIRE(script != nullptr);
script->OnAttackMiss(nullptr, nullptr);
@ -202,6 +214,7 @@ TEST_CASE("Invoke OnAttackMiss script function") {
TEST_CASE("Invoke ChangeAttackType script function") {
auto mainLib = TestLibrary::GetLibrary();
auto script = GetScript(mainLib, "ChangeAttackTypeScript"_cnc);
REQUIRE(script != nullptr);
uint8_t b = 0;
script->ChangeAttackType(nullptr, nullptr, 0, &b);
REQUIRE(b == 1);
@ -254,6 +267,7 @@ TEST_CASE("Invoke OnSecondaryEffect script function") {
TEST_CASE("Invoke OnAfterHits script function") {
auto mainLib = TestLibrary::GetLibrary();
auto script = GetScript(mainLib, "OnAfterHitsScript"_cnc);
REQUIRE(script != nullptr);
script->OnAfterHits(nullptr, nullptr);
@ -272,9 +286,21 @@ void TryException(AngelScriptScript* script) {
script->PreventAttack(nullptr, &b);
}
TEST_CASE("Get script name.") {
auto mainLib = TestLibrary::GetLibrary();
auto script = GetScript(mainLib, "throwScript"_cnc);
REQUIRE(script != nullptr);
INFO(script->GetName().c_str());
INFO(script->GetName().std_str());
CHECK(strcmp(script->GetName().c_str(), "throwScript") == 0);
delete script;
}
TEST_CASE("Handle script exceptions.") {
auto mainLib = TestLibrary::GetLibrary();
auto script = GetScript(mainLib, "throwScript"_cnc);
REQUIRE(script != nullptr);
CHECK_THROWS_WITH(
TryException(script),
Catch::Matchers::Equals("Script exception in script 'throwScript', line 1. Message: 'test exception'."));

View File

@ -6,6 +6,7 @@
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"(
namespace Pokemon{
[Pokemon effect=testScript1]
class testScript1 {
int add(int a, int b) {
return a + b;
@ -54,6 +55,7 @@ TEST_CASE("Build script resolver, create object, invoke addition method") {
auto obj =
dynamic_cast<AngelScriptScript*>(lib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc));
REQUIRE(obj != nullptr);
auto ctxPool = obj->GetContextPool();
auto ctx = ctxPool->RequestContext();

View File

@ -7,6 +7,7 @@
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"(
namespace Pokemon{
[Pokemon effect=testScript1]
class testScript1 {
bool testSpecies(Pokemon@ p, const Species@ species){ return p.Species is species; }
bool testForme(Pokemon@ p, const Forme@ forme){ return p.Forme is forme; }
@ -55,6 +56,8 @@ static ScriptData GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const Con
auto lib = GetScriptResolver(mainLib);
auto s = lib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc);
auto script = dynamic_cast<AngelScriptScript*>(s);
REQUIRE(script != nullptr);
auto ctxPool = script->GetContextPool();
auto ctx = ctxPool->RequestContext();

View File

@ -6,6 +6,7 @@
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"(
namespace Pokemon{
[Pokemon effect=testScript1]
class testScript1 {
bool testName(const Forme@ s, const constString &in name){ return s.Name == name; }
bool testWeight(const Forme@ s, float weight){ return s.Weight == weight; }
@ -45,6 +46,8 @@ static ScriptData GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const Con
auto lib = GetScriptResolver(mainLib);
auto s = lib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc);
auto script = dynamic_cast<AngelScriptScript*>(s);
REQUIRE(script != nullptr);
auto ctxPool = script->GetContextPool();
auto ctx = ctxPool->RequestContext();

View File

@ -6,6 +6,7 @@
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"(
namespace Pokemon{
[Pokemon effect=testScript1]
class testScript1 {
bool testName(const Item@ i, const constString &in name){ return i.Name == name; }
bool testCategory(const Item@ i, ItemCategory category){ return i.Category == category; }
@ -41,6 +42,8 @@ static ScriptData GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const Con
auto lib = GetScriptResolver(mainLib);
auto s = lib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc);
auto script = dynamic_cast<AngelScriptScript*>(s);
REQUIRE(script != nullptr);
auto ctxPool = script->GetContextPool();
auto ctx = ctxPool->RequestContext();

View File

@ -6,6 +6,7 @@
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"(
namespace Pokemon{
[Pokemon effect=testScript1]
class testScript1 {
bool testName(const MoveData@ s, const constString &in name){ return s.Name == name; }
bool testType(const MoveData@ s, uint8 type){ return s.Type == type; }
@ -45,6 +46,8 @@ static ScriptData GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const Con
auto lib = GetScriptResolver(mainLib);
auto s = lib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc);
auto script = dynamic_cast<AngelScriptScript*>(s);
REQUIRE(script != nullptr);
auto ctxPool = script->GetContextPool();
auto ctx = ctxPool->RequestContext();

View File

@ -6,6 +6,7 @@
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"(
namespace Pokemon{
[Pokemon effect=testScript1]
class testScript1 {
bool testName(const Species@ s, const constString &in name){ return s.Name == name; }
bool testId(const Species@ s, uint16 id){ return s.Id == id; }
@ -43,6 +44,8 @@ static ScriptData GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const Con
auto lib = GetScriptResolver(mainLib);
auto s = lib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc);
auto script = dynamic_cast<AngelScriptScript*>(s);
REQUIRE(script != nullptr);
auto ctxPool = script->GetContextPool();
auto ctx = ctxPool->RequestContext();

View File

@ -6,6 +6,7 @@
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"(
namespace Pokemon{
[Pokemon effect=testScript1]
class testScript1 {
bool testMaximumLevel(const StaticLibrary@ s, uint8 level){ return s.Settings.MaximalLevel == level; }
bool testMaximumMoves(const StaticLibrary@ s, uint8 moveCount){ return s.Settings.MaximalMoves == moveCount; }
@ -44,6 +45,8 @@ static ScriptData GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const Con
auto lib = GetScriptResolver(mainLib);
auto s = lib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc);
auto script = dynamic_cast<AngelScriptScript*>(s);
REQUIRE(script != nullptr);
auto ctxPool = script->GetContextPool();
auto ctx = ctxPool->RequestContext();

View File

@ -41,11 +41,11 @@ PkmnLib::Library::SpeciesLibrary* TestLibrary::BuildSpeciesLibrary() {
}
PkmnLib::Library::MoveLibrary* TestLibrary::BuildMoveLibrary() {
auto lib = new PkmnLib::Library::MoveLibrary();
lib->Insert("testMove"_cnc,
lib->Insert("testMove"_cnc.GetHash(),
new PkmnLib::Library::MoveData("testMove"_cnc, 0, PkmnLib::Library::MoveCategory::Physical, 50, 100, 20,
CreatureLib::Library::AttackTarget::Adjacent, 0,
CreatureLib::Library::SecondaryEffect(), {}));
lib->Insert("testMove2"_cnc,
lib->Insert("testMove2"_cnc.GetHash(),
new PkmnLib::Library::MoveData("testMove2"_cnc, 0, PkmnLib::Library::MoveCategory::Special, 30, 100, 10,
CreatureLib::Library::AttackTarget::Adjacent, 0,
CreatureLib::Library::SecondaryEffect(), {}));

View File

@ -24,8 +24,7 @@ public:
auto scriptResolver = PkmnLib::Battling::BattleLibrary::CreateScriptResolver();
auto lib = new PkmnLib::Battling::BattleLibrary(
BuildStaticLibrary(), statCalc, new PkmnLib::Battling::DamageLibrary(),
new CreatureLib::Battling::ExperienceLibrary(), scriptResolver,
new CreatureLib::Battling::MiscLibrary());
new CreatureLib::Battling::ExperienceLibrary(), scriptResolver, new CreatureLib::Battling::MiscLibrary());
scriptResolver->Initialize(lib);
return lib;
}
@ -45,7 +44,7 @@ public:
static CreatureLib::Library::GrowthRateLibrary* BuildGrowthRateLibrary() {
auto lib = new CreatureLib::Library::GrowthRateLibrary();
lib->AddGrowthRate(
"testGrowthRate"_cnc,
"testGrowthRate"_cnc.GetHash(),
new CreatureLib::Library::LookupGrowthRate(
{0, 15, 52, 122, 237, 406, 637, 942, 1326, 1800, 2369, 3041, 3822,
4719, 5737, 6881, 8155, 9564, 11111, 12800, 14632, 16610, 18737, 21012, 23437, 26012,
@ -67,8 +66,9 @@ public:
auto lib = new PkmnLib::Library::NatureLibrary();
lib->LoadNature("neutralNature", PkmnLib::Library::Nature(PkmnLib::Library::Statistic::PhysicalAttack,
PkmnLib::Library::Statistic::PhysicalDefense, 1, 1));
lib->LoadNature("buffsAttackNerfsSpeed", PkmnLib::Library::Nature(PkmnLib::Library::Statistic::PhysicalAttack,
PkmnLib::Library::Statistic::Speed, 1.1, 0.9));
lib->LoadNature("buffsAttackNerfsSpeed",
PkmnLib::Library::Nature(PkmnLib::Library::Statistic::PhysicalAttack,
PkmnLib::Library::Statistic::Speed, 1.1, 0.9));
return lib;
}
};