PkmnLib/src/ScriptResolving/WASM/InterfaceMethods/Battling/WASMPokemonRegistry.cpp

233 lines
13 KiB
C++

#include "WASMPokemonRegistry.hpp"
#include "../../../../Battling/Pokemon/Pokemon.hpp"
#include "../WASMHelperFile.hpp"
#include "wasm.h"
using namespace CreatureLib::Battling;
using namespace PkmnLib::Battling;
wasm_func_t* Pokemon_ChangeForme(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<void, Pokemon*, const CreatureLib::Library::SpeciesVariant*>(
resolver, {[](WebAssemblyScriptResolver*, Pokemon* pokemon, const CreatureLib::Library::SpeciesVariant* forme) {
pokemon->ChangeVariant(forme);
}});
}
wasm_func_t* Pokemon_HasHeldItemByHash(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<bool, Pokemon*, u32>(
resolver, {[](WebAssemblyScriptResolver*, Pokemon* pokemon, u32 itemHash) -> bool {
return pokemon->HasHeldItem(itemHash);
}});
}
wasm_func_t* Pokemon_SetHeldItemByHash(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<void, Pokemon*, u32>(
resolver, {[](WebAssemblyScriptResolver*, Pokemon* pokemon, u32 itemHash) {
return pokemon->SetHeldItemByHash(itemHash);
}});
}
wasm_func_t* Pokemon_ConsumeHeldItem(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<bool, Pokemon*>(
resolver, {[](WebAssemblyScriptResolver*, Pokemon* pokemon) -> bool { return pokemon->ConsumeHeldItem(); }});
}
wasm_func_t* Pokemon_GetNickname(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<u32, Pokemon*>(
resolver, {[](WebAssemblyScriptResolver* res, Pokemon* pokemon) -> u32 {
const auto& nickname_opt = pokemon->GetNickname();
if (!nickname_opt.has_value()) {
return 0;
}
// Allocate a string in wasm memory, and copy the nickname to it.
const auto& nickname = nickname_opt.value();
auto ptrs = res->AllocateMemory(sizeof(char) * nickname.length(), alignof(char));
strncpy((char*)ptrs.first, nickname.data(), nickname.length());
ptrs.first[nickname.length()] = 0;
return ptrs.second;
}});
}
wasm_func_t* Pokemon_GetRealAbility(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<const ArbUt::BorrowedPtr<const CreatureLib::Library::Talent>&, Pokemon*>(
resolver, {[](WebAssemblyScriptResolver*,
Pokemon* pokemon) -> const ArbUt::BorrowedPtr<const CreatureLib::Library::Talent>& {
return pokemon->GetForme()->GetAbility(pokemon->GetRealTalent());
}});
}
wasm_func_t* Pokemon_HasType(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<bool, Pokemon*, u8>(
resolver,
{[](WebAssemblyScriptResolver*, Pokemon* pokemon, u8 typeId) -> bool { return pokemon->HasType(typeId); }});
}
wasm_func_t* Pokemon_SetType(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<void, Pokemon*, u8, u8>(
resolver,
{[](WebAssemblyScriptResolver*, Pokemon* pokemon, u8 index, u8 typeId) { pokemon->SetType(index, typeId); }});
}
wasm_func_t* Pokemon_Damage(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<void, Pokemon*, u32, DamageSource>(
resolver, {[](WebAssemblyScriptResolver*, Pokemon* pokemon, u32 damage, DamageSource source) {
pokemon->Damage(damage, source);
}});
}
wasm_func_t* Pokemon_Heal(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<void, Pokemon*, u32, bool>(
resolver, {[](WebAssemblyScriptResolver*, Pokemon* pokemon, u32 amount, bool canRevive) {
pokemon->Heal(amount, canRevive);
}});
}
wasm_func_t* Pokemon_RestoreAllPP(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<void, Pokemon*>(
resolver, {[](WebAssemblyScriptResolver*, Pokemon* pokemon) { pokemon->RestoreAllAttackUses(); }});
}
wasm_func_t* Pokemon_OverrideActiveAbility(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<void, Pokemon*, const char*, size_t>(
resolver, {[](WebAssemblyScriptResolver*, Pokemon* pokemon, const char* abilityName, size_t abilityNameLength) {
auto sv = ArbUt::StringView(abilityName, abilityNameLength);
pokemon->OverrideActiveTalent(sv);
}});
}
wasm_func_t* Pokemon_AddVolatileScript(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<BattleScript*, Pokemon*, const char*, size_t>(
resolver, {[](WebAssemblyScriptResolver*, Pokemon* pokemon, const char* scriptName, size_t scriptNameLength) {
auto sv = ArbUt::StringView(scriptName, scriptNameLength);
return pokemon->AddVolatileScript(sv);
}});
}
wasm_func_t* Pokemon_RemoveVolatileScriptByHash(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<void, Pokemon*, u32>(
resolver,
{[](WebAssemblyScriptResolver*, Pokemon* pokemon, u32 hash) { pokemon->RemoveVolatileScriptByHash(hash); }});
}
wasm_func_t* Pokemon_HasMoveByHash(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<bool, Pokemon*, u32>(
resolver, {[](WebAssemblyScriptResolver*, Pokemon* pokemon, u32 hash) -> bool {
return pokemon->HasAttackByHash(hash);
}});
}
wasm_func_t* Pokemon_SetStatus(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<void, Pokemon*, const char*, size_t>(
resolver, {[](WebAssemblyScriptResolver*, Pokemon* pokemon, const char* statusName, size_t statusNameLength) {
auto sv = ArbUt::StringView(statusName, statusNameLength);
pokemon->SetStatus(sv);
}});
}
wasm_func_t* Pokemon_ClearStatus(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<void, Pokemon*>(
resolver, {[](WebAssemblyScriptResolver*, Pokemon* pokemon) { pokemon->ClearStatus(); }});
}
wasm_func_t* Pokemon_SetDisplaySpecies(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<void, Pokemon*, CreatureLib::Library::CreatureSpecies*>(
resolver, {[](WebAssemblyScriptResolver*, Pokemon* pokemon, CreatureLib::Library::CreatureSpecies* species) {
pokemon->SetDisplaySpecies(species);
}});
}
wasm_func_t* Pokemon_SetDisplayForme(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<void, Pokemon*, CreatureLib::Library::SpeciesVariant*>(
resolver, {[](WebAssemblyScriptResolver*, Pokemon* pokemon, CreatureLib::Library::SpeciesVariant* variant) {
pokemon->SetDisplayVariant(variant);
}});
}
wasm_func_t* Pokemon_ChangeStatBoost(WebAssemblyScriptResolver* resolver) {
return WasmHelpers::CreateFunc<void, Pokemon*, CreatureLib::Library::Statistic, i8, bool>(
resolver, {[](WebAssemblyScriptResolver*, Pokemon* pokemon, CreatureLib::Library::Statistic stat, i8 diffAmount,
bool selfInflicted) { pokemon->ChangeStatBoost(stat, diffAmount, selfInflicted); }});
}
void WASMPokemonRegistry::Register(ArbUt::Dictionary<std::string, wasm_func_t*>& externs,
WebAssemblyScriptResolver* resolver) {
REGISTER_GETTER("pokemon_get_library", Creature, GetLibrary, resolver)
REGISTER_GETTER("pokemon_get_species", Creature, GetSpecies, resolver)
REGISTER_GETTER("pokemon_get_display_species", Creature, GetDisplaySpecies, resolver)
REGISTER_GETTER("pokemon_get_forme", Creature, GetVariant, resolver)
REGISTER_GETTER("pokemon_get_display_forme", Creature, GetDisplayVariant, resolver)
externs.Insert("pokemon_set_display_species", Pokemon_SetDisplaySpecies(resolver));
externs.Insert("pokemon_set_display_forme", Pokemon_SetDisplayForme(resolver));
REGISTER_GETTER("pokemon_get_level", Creature, GetLevel, resolver)
REGISTER_GETTER("pokemon_get_experience", Creature, GetExperience, resolver)
REGISTER_GETTER("pokemon_get_unique_identifier", Creature, GetUniqueIdentifier, resolver)
REGISTER_GETTER("pokemon_get_gender", Creature, GetGender, resolver)
REGISTER_GETTER("pokemon_get_coloring", Creature, GetColoring, resolver)
REGISTER_GETTER("pokemon_get_held_item", Creature, GetHeldItem, resolver)
REGISTER_GETTER("pokemon_get_current_health", Creature, GetCurrentHealth, resolver)
REGISTER_GETTER("pokemon_get_weight", Creature, GetWeight, resolver)
REGISTER_GETTER("pokemon_get_height", Creature, GetHeight, resolver)
externs.Insert("pokemon_get_nickname", Pokemon_GetNickname(resolver));
externs.Insert("pokemon_get_real_ability", Pokemon_GetRealAbility(resolver));
REGISTER_GETTER("pokemon_get_active_ability", Creature, GetActiveTalent, resolver)
REGISTER_GETTER("pokemon_is_useable", Creature, IsUsable, resolver)
REGISTER_GETTER("pokemon_is_fainted", Creature, IsFainted, resolver)
REGISTER_GETTER("pokemon_get_types", Creature, GetTypes, resolver)
externs.Insert("pokemon_has_type", Pokemon_HasType(resolver));
externs.Insert("pokemon_set_type", Pokemon_SetType(resolver));
REGISTER_GETTER("pokemon_get_max_health", Creature, GetMaxHealth, resolver)
externs.Insert("pokemon_damage", Pokemon_Damage(resolver));
externs.Insert("pokemon_heal", Pokemon_Heal(resolver));
externs.Insert("pokemon_restore_all_pp", Pokemon_RestoreAllPP(resolver));
externs.Insert("pokemon_override_active_ability", Pokemon_OverrideActiveAbility(resolver));
externs.Insert("pokemon_add_volatile_script", Pokemon_AddVolatileScript(resolver));
externs.Insert("pokemon_remove_volatile_script_by_hash", Pokemon_RemoveVolatileScriptByHash(resolver));
externs.Insert("pokemon_has_move_by_hash", Pokemon_HasMoveByHash(resolver));
REGISTER_GETTER("pokemon_get_moves", Creature, GetAttacks, resolver)
externs.Insert("pokemon_set_status", Pokemon_SetStatus(resolver));
externs.Insert("pokemon_clear_status", Pokemon_ClearStatus(resolver));
REGISTER_GETTER("pokemon_get_status_name", Creature, GetStatusName, resolver)
externs.Insert("pokemon_change_forme", Pokemon_ChangeForme(resolver));
externs.Insert("pokemon_has_held_item_by_hash", Pokemon_HasHeldItemByHash(resolver));
externs.Insert("pokemon_set_held_item_by_hash", Pokemon_SetHeldItemByHash(resolver));
externs.Insert("pokemon_consume_held_item", Pokemon_ConsumeHeldItem(resolver));
externs.Insert("pokemon_change_stat_boost", Pokemon_ChangeStatBoost(resolver));
REGISTER_GETTER("pokemon_get_flat_health", Creature, GetFlatStat<CreatureLib::Library::Statistic::Health>, resolver)
REGISTER_GETTER("pokemon_get_flat_attack", Creature, GetFlatStat<CreatureLib::Library::Statistic::PhysicalAttack>,
resolver)
REGISTER_GETTER("pokemon_get_flat_defense", Creature, GetFlatStat<CreatureLib::Library::Statistic::PhysicalDefense>,
resolver)
REGISTER_GETTER("pokemon_get_flat_special_attack", Creature,
GetFlatStat<CreatureLib::Library::Statistic::MagicalAttack>, resolver)
REGISTER_GETTER("pokemon_get_flat_special_defense", Creature,
GetFlatStat<CreatureLib::Library::Statistic::MagicalDefense>, resolver)
REGISTER_GETTER("pokemon_get_flat_speed", Creature, GetFlatStat<CreatureLib::Library::Statistic::Speed>, resolver)
REGISTER_GETTER("pokemon_get_boosted_health", Creature, GetBoostedStat<CreatureLib::Library::Statistic::Health>,
resolver)
REGISTER_GETTER("pokemon_get_boosted_attack", Creature,
GetBoostedStat<CreatureLib::Library::Statistic::PhysicalAttack>, resolver)
REGISTER_GETTER("pokemon_get_boosted_defense", Creature,
GetBoostedStat<CreatureLib::Library::Statistic::PhysicalDefense>, resolver)
REGISTER_GETTER("pokemon_get_boosted_special_attack", Creature,
GetBoostedStat<CreatureLib::Library::Statistic::MagicalAttack>, resolver)
REGISTER_GETTER("pokemon_get_boosted_special_defense", Creature,
GetBoostedStat<CreatureLib::Library::Statistic::MagicalDefense>, resolver)
REGISTER_GETTER("pokemon_get_boosted_speed", Creature, GetBoostedStat<CreatureLib::Library::Statistic::Speed>,
resolver)
REGISTER_GETTER("pokemon_get_stat_boost_health", Creature, GetStatBoost<CreatureLib::Library::Statistic::Health>,
resolver)
REGISTER_GETTER("pokemon_get_stat_boost_attack", Creature,
GetStatBoost<CreatureLib::Library::Statistic::PhysicalAttack>, resolver)
REGISTER_GETTER("pokemon_get_stat_boost_defense", Creature,
GetStatBoost<CreatureLib::Library::Statistic::PhysicalDefense>, resolver)
REGISTER_GETTER("pokemon_get_stat_boost_special_attack", Creature,
GetStatBoost<CreatureLib::Library::Statistic::MagicalAttack>, resolver)
REGISTER_GETTER("pokemon_get_stat_boost_special_defense", Creature,
GetStatBoost<CreatureLib::Library::Statistic::MagicalDefense>, resolver)
REGISTER_GETTER("pokemon_get_stat_boost_speed", Creature, GetStatBoost<CreatureLib::Library::Statistic::Speed>,
resolver)
}