Adds WASM type registry for species and formes.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2022-05-21 20:46:04 +02:00
parent 5a545aaa98
commit 9adbd27358
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
9 changed files with 183 additions and 7 deletions

View File

@ -12,10 +12,9 @@ namespace PkmnLib::Library {
ArbUt::List<ArbUt::StringView> _eggGroups;
public:
PokemonSpecies(u16 id, const ArbUt::StringView& name, const PokemonForme* non_null defaultForme, float genderRatio,
const ArbUt::StringView& growthRate, u8 captureRate, u8 baseHappiness,
const ArbUt::List<ArbUt::StringView>& eggGroups,
std::unordered_set<u32> flags = {}) noexcept
PokemonSpecies(u16 id, const ArbUt::StringView& name, const PokemonForme* non_null defaultForme,
float genderRatio, const ArbUt::StringView& growthRate, u8 captureRate, u8 baseHappiness,
const ArbUt::List<ArbUt::StringView>& eggGroups, std::unordered_set<u32> flags = {}) noexcept
: CreatureSpecies(id, name, defaultForme, genderRatio, growthRate, captureRate, flags),
_baseHappiness(baseHappiness), _eggGroups(eggGroups) {}
@ -45,6 +44,14 @@ namespace PkmnLib::Library {
const ArbUt::UniquePtrList<const EvolutionData>& GetEvolutions() const noexcept { return _evolutions; }
bool HasEggGroup(const ArbUt::StringView& sv) const noexcept { return _eggGroups.Contains(sv); }
bool HasEggGroup(u32 hash) const noexcept {
for (auto& eg : _eggGroups) {
if (eg == hash) {
return true;
}
}
return false;
}
const ArbUt::List<ArbUt::StringView>& GetEggGroups() const noexcept { return _eggGroups; }
};
}

View File

@ -0,0 +1,50 @@
#include "WASMFormeRegistry.hpp"
#include "../../../../Battling/Library/BattleLibrary.hpp"
#include "../../WebAssemblyScriptResolver.hpp"
#include "../WASMHelperFile.hpp"
#include "wasm.h"
using namespace CreatureLib::Library;
using namespace PkmnLib::Library;
wasm_func_t* Forme_GetType(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<u8, const SpeciesVariant*, size_t>(
resolver, {[](WebAssemblyScriptResolver*, const SpeciesVariant* forme, size_t index) -> u8 {
return forme->GetType(index);
}});
}
wasm_func_t* Forme_GetBaseStatistic(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<u16, const SpeciesVariant*, CreatureLib::Library::Statistic>(
resolver, {[](WebAssemblyScriptResolver*, const SpeciesVariant* forme,
CreatureLib::Library::Statistic stat) -> u16 { return forme->GetStatistic(stat); }});
}
wasm_func_t* Forme_GetAbility(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<const Talent*, const SpeciesVariant*, bool, u8>(
resolver, {[](WebAssemblyScriptResolver*, const SpeciesVariant* forme, bool hidden, u8 index) -> const Talent* {
return forme->GetTalent(TalentIndex(hidden, index)).GetRaw();
}});
}
wasm_func_t* Forme_HasFlagByHash(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<bool, const SpeciesVariant*, u32>(
resolver, {[](WebAssemblyScriptResolver*, const SpeciesVariant* forme, u32 flag) -> bool {
return forme->HasFlag(flag);
}});
}
void WASMFormeRegistry::Register(ArbUt::Dictionary<std::string, wasm_func_t*>& externs,
WebAssemblyScriptResolver* resolver) {
REGISTER_GETTER("forme_get_name", SpeciesVariant, GetName, resolver)
REGISTER_GETTER("forme_get_height", SpeciesVariant, GetHeight, resolver)
REGISTER_GETTER("forme_get_weight", SpeciesVariant, GetWeight, resolver)
REGISTER_GETTER("forme_get_base_experience", SpeciesVariant, GetBaseExperience, resolver)
REGISTER_GETTER("forme_get_type_count", SpeciesVariant, GetTypeCount, resolver)
externs.Insert("forme_get_type", Forme_GetType(resolver));
externs.Insert("forme_get_statistic", Forme_GetBaseStatistic(resolver));
REGISTER_GETTER("forme_get_ability_count", SpeciesVariant, GetTalentCount, resolver)
REGISTER_GETTER("forme_get_hidden_ability_count", SpeciesVariant, GetSecretTalentCount, resolver)
externs.Insert("forme_get_ability", Forme_GetAbility(resolver));
externs.Insert("forme_has_flag_by_hash", Forme_HasFlagByHash(resolver));
}

View File

@ -0,0 +1,14 @@
#ifndef PKMNLIB_WASMFORMEREGISTRY_HPP
#define PKMNLIB_WASMFORMEREGISTRY_HPP
#include <Arbutils/Collections/Dictionary.hpp>
#include <wasm.h>
class WebAssemblyScriptResolver;
class WASMFormeRegistry {
public:
static void Register(ArbUt::Dictionary<std::string, wasm_func_t*>& externs,
WebAssemblyScriptResolver* resolver);
};
#endif // PKMNLIB_WASMFORMEREGISTRY_HPP

View File

@ -10,7 +10,6 @@ wasm_func_t* Item_HasFlagByHash(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<bool, Item*, u32>(
resolver, {[](WebAssemblyScriptResolver*, Item* item, u32 flag) -> bool { return item->HasFlag(flag); }});
}
void WASMItemRegistry::Register(ArbUt::Dictionary<std::string, wasm_func_t*>& externs,
WebAssemblyScriptResolver* resolver) {
REGISTER_GETTER("item_get_name", Item, GetName, resolver)

View File

@ -0,0 +1,57 @@
#include "WASMSpeciesRegistry.hpp"
#include "../../../../Battling/Library/BattleLibrary.hpp"
#include "../../WebAssemblyScriptResolver.hpp"
#include "../WASMHelperFile.hpp"
#include "wasm.h"
using namespace CreatureLib::Library;
using namespace PkmnLib::Library;
wasm_func_t* Species_HasFormeByHash(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<bool, const CreatureSpecies*, u32>(
resolver, {[](WebAssemblyScriptResolver*, const CreatureSpecies* species, u32 flag) -> bool {
return species->HasVariant(flag);
}});
}
wasm_func_t* Species_GetFormeByHash(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<const SpeciesVariant*, const CreatureSpecies*, u32>(
resolver,
{[](WebAssemblyScriptResolver*, const CreatureSpecies* species, u32 variant) -> const SpeciesVariant* {
auto opt = species->TryGetVariant(variant);
if (opt.has_value()) {
return opt.value();
}
return nullptr;
}});
}
wasm_func_t* Species_HasFlagByHash(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<bool, const CreatureSpecies*, u32>(
resolver, {[](WebAssemblyScriptResolver*, const CreatureSpecies* species, u32 flag) -> bool {
return species->HasFlag(flag);
}});
}
wasm_func_t* Species_HasEggGroupByHash(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<bool, const PokemonSpecies*, u32>(
resolver, {[](WebAssemblyScriptResolver*, const PokemonSpecies* species, u32 flag) -> bool {
return species->HasEggGroup(flag);
}});
}
void WASMSpeciesRegistry::Register(ArbUt::Dictionary<std::string, wasm_func_t*>& externs,
WebAssemblyScriptResolver* resolver) {
REGISTER_GETTER("species_get_id", CreatureSpecies, GetId, resolver)
REGISTER_GETTER("species_get_name", CreatureSpecies, GetName, resolver)
REGISTER_GETTER("species_get_gender_rate", CreatureSpecies, GetGenderRate, resolver)
REGISTER_GETTER("species_get_growth_rate", CreatureSpecies, GetGrowthRate, resolver)
REGISTER_GETTER("species_get_capture_rate", CreatureSpecies, GetCaptureRate, resolver)
REGISTER_GETTER("species_get_base_happiness", PokemonSpecies, GetBaseHappiness, resolver)
externs.Insert("species_has_forme_by_hash", Species_HasFormeByHash(resolver));
externs.Insert("species_get_forme_by_hash", Species_GetFormeByHash(resolver));
externs.Insert("species_has_flag_by_hash", Species_HasFlagByHash(resolver));
externs.Insert("species_has_egg_group_by_hash", Species_HasEggGroupByHash(resolver));
}

View File

@ -0,0 +1,14 @@
#ifndef PKMNLIB_WASMSPECIESREGISTRY_HPP
#define PKMNLIB_WASMSPECIESREGISTRY_HPP
#include <Arbutils/Collections/Dictionary.hpp>
#include <wasm.h>
class WebAssemblyScriptResolver;
class WASMSpeciesRegistry {
public:
static void Register(ArbUt::Dictionary<std::string, wasm_func_t*>& externs,
WebAssemblyScriptResolver* resolver);
};
#endif // PKMNLIB_WASMSPECIESREGISTRY_HPP

View File

@ -5,8 +5,10 @@
#include "../WebAssemblyScriptResolver.hpp"
#include "Arbutils/Collections/Dictionary.hpp"
#include "Library/LibraryMethods.hpp"
#include "Library/WASMMoveDataRegistry.hpp"
#include "Library/WASMItemRegistry.hpp"
#include "Library/WASMMoveDataRegistry.hpp"
#include "Library/WASMSpeciesRegistry.hpp"
#include "Library/WASMFormeRegistry.hpp"
#include "WASMCoreMethods.hpp"
#include "WASMStringView.hpp"
@ -18,6 +20,9 @@ public:
LibraryMethods::Register(externs, resolver);
WASMMoveDataRegistry::Register(externs, resolver);
WASMItemRegistry::Register(externs, resolver);
WASMSpeciesRegistry::Register(externs, resolver);
WASMFormeRegistry::Register(externs, resolver);
}
};

View File

@ -33,6 +33,8 @@ type species = u64
type move = u64
type item = u64
type nature = u64
type forme = u64
type ability = u64
enum Statistic {
Health,
@ -120,4 +122,32 @@ item_get_category: function(item) -> ItemCategory
item_get_battle_category: function(item) -> ItemBattleCategory
item_get_price: function(item) -> s32
item_get_fling_power: function(item) -> u8
item_has_flag_by_hash: function(item, u32 hash) -> bool
item_has_flag_by_hash: function(item, u32 hash) -> bool
// Species class
species_get_id: function(species) -> u16
species_get_name: function(species) -> const_string
species_get_gender_rate: function(species) -> f32
species_get_growth_rate: function(species) -> const_string
species_get_capture_rate: function(species) -> u8
species_get_base_happiness: function(species) -> u8
species_has_forme_by_hash: function(species, u32) -> bool
species_get_forme_by_hash: function(species, u32) -> forme
species_has_flag_by_hash: function(species, u32) -> bool
species_has_egg_group_by_hash: function(species, u32) -> bool
// Forme class
forme_get_name: function(forme) -> const_string
forme_get_height: function(forme) -> float
forme_get_weight: function(forme) -> float
forme_get_base_experience: function(forme) -> u32
forme_get_type_count: function(forme) -> u64
forme_get_type: function(forme, u64) -> u8
forme_get_statistic: function(forme, Statistic) -> u16
forme_get_ability_count: function(forme) -> u64
forme_get_hidden_ability_count: function(forme) -> u64
forme_get_ability: function(forme, bool hidden, u8 index) -> ability
forme_has_flag_by_hash: function(forme, u32) -> bool