1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-21 05:09:43 +00:00

Move services to new HPP

This commit is contained in:
Zackhasacat 2023-09-02 14:10:50 -05:00
parent 85d47dd715
commit 44303ed8ca
3 changed files with 59 additions and 49 deletions

View file

@ -0,0 +1,54 @@
#ifndef MWLUA_ACTOR_H
#define MWLUA_ACTOR_H
#include <sol/sol.hpp>
#include <components/esm/defs.hpp>
#include <components/lua/luastate.hpp>
#include <components/esm3/loadclas.hpp>
#include <components/esm3/loadnpc.hpp>
#include "../context.hpp"
#include "../object.hpp"
namespace MWLua
{
template <class T>
void addActorServicesBindings(sol::usertype<T>& record, const Context& context)
{
record["servicesOffered"] = sol::readonly_property([](const T& rec) -> std::vector<std::string> {
std::vector<std::string> providedServices;
std::map<int, std::string> serviceNames = { { ESM::NPC::Spells, "Spells" },
{ ESM::NPC::Spellmaking, "Spellmaking" }, { ESM::NPC::Enchanting, "Enchanting" },
{ ESM::NPC::Training, "Training" }, { ESM::NPC::Repair, "Repair" }, { ESM::NPC::AllItems, "Barter" },
{ ESM::NPC::Weapon, "Weapon" }, { ESM::NPC::Armor, "Armor" }, { ESM::NPC::Clothing, "Clothing" },
{ ESM::NPC::Books, "Books" }, { ESM::NPC::Ingredients, "Ingredients" }, { ESM::NPC::Picks, "Picks" },
{ ESM::NPC::Probes, "Probes" }, { ESM::NPC::Lights, "Lights" }, { ESM::NPC::Apparatus, "Apparatus" },
{ ESM::NPC::RepairItem, "RepairItem" }, { ESM::NPC::Misc, "Misc" }, { ESM::NPC::Potions, "Potions" },
{ ESM::NPC::MagicItems, "MagicItems" } };
int services = rec.mAiData.mServices;
if constexpr (std::is_same_v<T, ESM::NPC>)
{
if (rec.mFlags & ESM::NPC::Autocalc)
services = MWBase::Environment::get()
.getWorld()
->getStore()
.get<ESM::Class>()
.find(rec.mClass)
->mData.mServices;
}
for (const auto& [flag, name] : serviceNames)
{
if (services & flag)
providedServices.push_back(name);
}
if (!rec.getTransport().empty())
providedServices.push_back("Travel");
return providedServices;
});
}
}
#endif // MWLUA_TYPES_H

View file

@ -1,4 +1,5 @@
#include "types.hpp"
#include "actor.hpp"
#include <components/esm3/loadcrea.hpp>
#include <components/esm3/loadnpc.hpp>
@ -49,28 +50,6 @@ namespace MWLua
record["soulValue"] = sol::readonly_property([](const ESM::Creature& rec) -> int { return rec.mData.mSoul; });
record["type"] = sol::readonly_property([](const ESM::Creature& rec) -> int { return rec.mData.mType; });
record["baseGold"] = sol::readonly_property([](const ESM::Creature& rec) -> int { return rec.mData.mGold; });
record["servicesOffered"] = sol::readonly_property([](const ESM::Creature& rec) -> std::vector<std::string> {
std::vector<std::string> providedServices;
std::map<int, std::string> serviceNames = { { ESM::NPC::Spells, "Spells" },
{ ESM::NPC::Spellmaking, "Spellmaking" }, { ESM::NPC::Enchanting, "Enchanting" },
{ ESM::NPC::Training, "Training" }, { ESM::NPC::Repair, "Repair" }, { ESM::NPC::AllItems, "Barter" },
{ ESM::NPC::Weapon, "Weapon" }, { ESM::NPC::Armor, "Armor" }, { ESM::NPC::Clothing, "Clothing" },
{ ESM::NPC::Books, "Books" }, { ESM::NPC::Ingredients, "Ingredients" }, { ESM::NPC::Picks, "Picks" },
{ ESM::NPC::Probes, "Probes" }, { ESM::NPC::Lights, "Lights" }, { ESM::NPC::Apparatus, "Apparatus" },
{ ESM::NPC::RepairItem, "RepairItem" }, { ESM::NPC::Misc, "Misc" }, { ESM::NPC::Potions, "Potions" },
{ ESM::NPC::MagicItems, "MagicItems" } };
int mServices = rec.mAiData.mServices;
for (const auto& entry : serviceNames)
{
if (mServices & entry.first)
{
providedServices.push_back(entry.second);
}
}
if (!rec.getTransport().empty())
providedServices.push_back("Travel");
return providedServices;
});
addActorServicesBindings<ESM::Creature>(record, context);
}
}

View file

@ -1,5 +1,5 @@
#include "types.hpp"
#include "actor.hpp"
#include <components/esm3/loadnpc.hpp>
#include <components/lua/luastate.hpp>
@ -49,31 +49,8 @@ namespace MWLua
= sol::readonly_property([](const ESM::NPC& rec) -> std::string { return rec.mHead.serializeText(); });
record["isMale"] = sol::readonly_property([](const ESM::NPC& rec) -> bool { return rec.isMale(); });
record["baseGold"] = sol::readonly_property([](const ESM::NPC& rec) -> int { return rec.mNpdt.mGold; });
record["servicesOffered"] = sol::readonly_property([](const ESM::NPC& rec) -> std::vector<std::string> {
std::vector<std::string> providedServices;
std::map<int, std::string> serviceNames = { { ESM::NPC::Spells, "Spells" },
{ ESM::NPC::Spellmaking, "Spellmaking" }, { ESM::NPC::Enchanting, "Enchanting" },
{ ESM::NPC::Training, "Training" }, { ESM::NPC::Repair, "Repair" }, { ESM::NPC::AllItems, "Barter" },
{ ESM::NPC::Weapon, "Weapon" }, { ESM::NPC::Armor, "Armor" }, { ESM::NPC::Clothing, "Clothing" },
{ ESM::NPC::Books, "Books" }, { ESM::NPC::Ingredients, "Ingredients" }, { ESM::NPC::Picks, "Picks" },
{ ESM::NPC::Probes, "Probes" }, { ESM::NPC::Lights, "Lights" }, { ESM::NPC::Apparatus, "Apparatus" },
{ ESM::NPC::RepairItem, "RepairItem" }, { ESM::NPC::Misc, "Misc" }, { ESM::NPC::Potions, "Potions" },
{ ESM::NPC::MagicItems, "MagicItems" } };
int mServices = rec.mAiData.mServices;
for (const auto& entry : serviceNames)
{
if (mServices & entry.first)
{
providedServices.push_back(entry.second);
}
}
if (!rec.getTransport().empty())
providedServices.push_back("Travel");
return providedServices;
});
// This function is game-specific, in future we should replace it with something more universal.
addActorServicesBindings<ESM::NPC>(record, context);
// This function is game-specific, in future we should replace it with something more universal.
npc["isWerewolf"] = [](const Object& o) {
const MWWorld::Class& cls = o.ptr().getClass();
if (cls.isNpc())