mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-09-08 03:41:11 -04:00
npc stuff
This commit is contained in:
parent
b160cee0b7
commit
2cd4b643d0
@ -388,7 +388,7 @@ namespace MWLua
|
|||||||
objectT["setScale"] = [context](const GObject& object, float scale) {
|
objectT["setScale"] = [context](const GObject& object, float scale) {
|
||||||
context.mLuaManager->addAction(
|
context.mLuaManager->addAction(
|
||||||
[object, scale] { MWBase::Environment::get().getWorld()->scaleObject(object.ptr(), scale); });
|
[object, scale] { MWBase::Environment::get().getWorld()->scaleObject(object.ptr(), scale); });
|
||||||
};
|
};--adjustScale
|
||||||
objectT["addScript"] = [context](const GObject& object, std::string_view path, sol::object initData) {
|
objectT["addScript"] = [context](const GObject& object, std::string_view path, sol::object initData) {
|
||||||
const LuaUtil::ScriptsConfiguration& cfg = context.mLua->getConfiguration();
|
const LuaUtil::ScriptsConfiguration& cfg = context.mLua->getConfiguration();
|
||||||
std::optional<int> scriptId = cfg.findId(VFS::Path::Normalized(path));
|
std::optional<int> scriptId = cfg.findId(VFS::Path::Normalized(path));
|
||||||
|
@ -29,6 +29,138 @@ namespace sol
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
ESM::NPC tableToNPC(const sol::table& rec)
|
||||||
|
{
|
||||||
|
ESM::NPC npc;
|
||||||
|
|
||||||
|
// Start from template if provided
|
||||||
|
if (rec["template"] != sol::nil)
|
||||||
|
npc = LuaUtil::cast<ESM::NPC>(rec["template"]);
|
||||||
|
else
|
||||||
|
npc.blank();
|
||||||
|
|
||||||
|
// Force dummy ID
|
||||||
|
npc.mId = ESM::RefId::deserializeText("blank");
|
||||||
|
|
||||||
|
// Basic fields
|
||||||
|
if (rec["name"] != sol::nil)
|
||||||
|
npc.mName = rec["name"];
|
||||||
|
if (rec["model"] != sol::nil)
|
||||||
|
npc.mModel = Misc::ResourceHelpers::meshPathForESM3(rec["model"].get<std::string_view>());
|
||||||
|
if (rec["mwscript"] != sol::nil)
|
||||||
|
npc.mScript = ESM::RefId::deserializeText(rec["mwscript"].get<std::string_view>());
|
||||||
|
if (rec["race"] != sol::nil)
|
||||||
|
npc.mRace = ESM::RefId::deserializeText(rec["race"].get<std::string_view>());
|
||||||
|
if (rec["class"] != sol::nil)
|
||||||
|
npc.mClass = ESM::RefId::deserializeText(rec["class"].get<std::string_view>());
|
||||||
|
if (rec["head"] != sol::nil)
|
||||||
|
npc.mHead = ESM::RefId::deserializeText(rec["head"].get<std::string_view>());
|
||||||
|
if (rec["hair"] != sol::nil)
|
||||||
|
npc.mHair = ESM::RefId::deserializeText(rec["hair"].get<std::string_view>());
|
||||||
|
|
||||||
|
if (rec["isMale"] != sol::nil)
|
||||||
|
{
|
||||||
|
bool male = rec["isMale"];
|
||||||
|
if (male)
|
||||||
|
npc.mFlags &= ~ESM::NPC::Female;
|
||||||
|
else
|
||||||
|
npc.mFlags |= ESM::NPC::Female;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rec["isEssential"] != sol::nil)
|
||||||
|
{
|
||||||
|
bool essential = rec["isEssential"];
|
||||||
|
if (essential)
|
||||||
|
npc.mFlags |= ESM::NPC::Essential;
|
||||||
|
else
|
||||||
|
npc.mFlags &= ~ESM::NPC::Essential;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rec["isRespawning"] != sol::nil)
|
||||||
|
{
|
||||||
|
bool respawn = rec["isRespawning"];
|
||||||
|
if (respawn)
|
||||||
|
npc.mFlags |= ESM::NPC::Respawn;
|
||||||
|
else
|
||||||
|
npc.mFlags &= ~ESM::NPC::Respawn;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rec["baseDisposition"] != sol::nil)
|
||||||
|
npc.mNpdt.mDisposition = static_cast<int>(rec["baseDisposition"]);
|
||||||
|
|
||||||
|
if (rec["baseGold"] != sol::nil)
|
||||||
|
npc.mNpdt.mGold = static_cast<int>(rec["baseGold"]);
|
||||||
|
|
||||||
|
if (rec["bloodType"] != sol::nil)
|
||||||
|
npc.mBloodType = static_cast<int>(rec["bloodType"]);
|
||||||
|
|
||||||
|
// Services offered
|
||||||
|
if (rec["servicesOffered"] != sol::nil)
|
||||||
|
{
|
||||||
|
const sol::table services = rec["servicesOffered"];
|
||||||
|
int flags = 0;
|
||||||
|
auto setFlag = [&](const char* key, int mask) {
|
||||||
|
if (services[key] != sol::nil && services[key])
|
||||||
|
flags |= mask;
|
||||||
|
};
|
||||||
|
|
||||||
|
setFlag("Spells", ESM::NPC::Spells);
|
||||||
|
setFlag("Spellmaking", ESM::NPC::Spellmaking);
|
||||||
|
setFlag("Enchanting", ESM::NPC::Enchanting);
|
||||||
|
setFlag("Training", ESM::NPC::Training);
|
||||||
|
setFlag("Repair", ESM::NPC::Repair);
|
||||||
|
setFlag("Barter", ESM::NPC::AllItems);
|
||||||
|
setFlag("Weapon", ESM::NPC::Weapon);
|
||||||
|
setFlag("Armor", ESM::NPC::Armor);
|
||||||
|
setFlag("Clothing", ESM::NPC::Clothing);
|
||||||
|
setFlag("Books", ESM::NPC::Books);
|
||||||
|
setFlag("Ingredients", ESM::NPC::Ingredients);
|
||||||
|
setFlag("Picks", ESM::NPC::Picks);
|
||||||
|
setFlag("Probes", ESM::NPC::Probes);
|
||||||
|
setFlag("Lights", ESM::NPC::Lights);
|
||||||
|
setFlag("Apparatus", ESM::NPC::Apparatus);
|
||||||
|
setFlag("RepairItem", ESM::NPC::RepairItem);
|
||||||
|
setFlag("Misc", ESM::NPC::Misc);
|
||||||
|
setFlag("Potions", ESM::NPC::Potions);
|
||||||
|
setFlag("MagicItems", ESM::NPC::MagicItems);
|
||||||
|
|
||||||
|
npc.mAiData.mServices = flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Travel destinations
|
||||||
|
if (rec["travelDestinations"] != sol::nil)
|
||||||
|
{
|
||||||
|
const sol::table travelDests = rec["travelDestinations"];
|
||||||
|
npc.mTransport.clear();
|
||||||
|
for (std::size_t i = 1; i <= travelDests.size(); ++i)
|
||||||
|
{
|
||||||
|
sol::table t = travelDests[i];
|
||||||
|
ESM::Transport destination;
|
||||||
|
|
||||||
|
// Position
|
||||||
|
destination.mPos.pos = t["position"];
|
||||||
|
destination.mPos.rot = Misc::Convert::toRotation(LuaUtil::fromTransform(t["rotation"]));
|
||||||
|
|
||||||
|
// Cell
|
||||||
|
std::string cellId = t["cellId"];
|
||||||
|
destination.mCellName = cellId; // If empty, it will be handled as exterior
|
||||||
|
|
||||||
|
npc.mTransport.push_back(destination);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return npc;
|
||||||
|
}
|
||||||
|
// Blood type
|
||||||
|
if (rec["bloodType"] != sol::nil)
|
||||||
|
npc.mBloodType = static_cast<int>(rec["bloodType"]);
|
||||||
|
|
||||||
|
return npc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
size_t getValidRanksCount(const ESM::Faction* faction)
|
size_t getValidRanksCount(const ESM::Faction* faction)
|
||||||
@ -151,7 +283,7 @@ namespace MWLua
|
|||||||
auto& stats = cls.getNpcStats(o.ptr());
|
auto& stats = cls.getNpcStats(o.ptr());
|
||||||
stats.setBaseDisposition(stats.getBaseDisposition() + value);
|
stats.setBaseDisposition(stats.getBaseDisposition() + value);
|
||||||
};
|
};
|
||||||
|
npc["createRecordDraft"] = tableToNPC;
|
||||||
npc["getFactionRank"] = [](const Object& actor, std::string_view faction) -> size_t {
|
npc["getFactionRank"] = [](const Object& actor, std::string_view faction) -> size_t {
|
||||||
const MWWorld::Ptr ptr = actor.ptr();
|
const MWWorld::Ptr ptr = actor.ptr();
|
||||||
ESM::RefId factionId = parseFactionId(faction);
|
ESM::RefId factionId = parseFactionId(faction);
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
#include <components/esm3/loadmisc.hpp>
|
#include <components/esm3/loadmisc.hpp>
|
||||||
#include <components/esm3/loadskil.hpp>
|
#include <components/esm3/loadskil.hpp>
|
||||||
#include <components/esm3/loadweap.hpp>
|
#include <components/esm3/loadweap.hpp>
|
||||||
|
#include <components/esm3/loadnpc.hpp>
|
||||||
#include <components/lua/luastate.hpp>
|
#include <components/lua/luastate.hpp>
|
||||||
#include <components/misc/finitenumbers.hpp>
|
#include <components/misc/finitenumbers.hpp>
|
||||||
|
|
||||||
@ -155,6 +156,11 @@ namespace MWLua
|
|||||||
MWWorld::Ptr newPtr = ptr.getClass().copyToCell(ptr, cell, count.value_or(1));
|
MWWorld::Ptr newPtr = ptr.getClass().copyToCell(ptr, cell, count.value_or(1));
|
||||||
return GObject(newPtr);
|
return GObject(newPtr);
|
||||||
};
|
};
|
||||||
|
api["advanceTime"] = [context](double hours, bool incremental) {
|
||||||
|
context.mLuaManager->addAction([ hours, incremental] {
|
||||||
|
MWBase::Environment::get().getWorld()->advanceTime(hours, incremental);
|
||||||
|
});
|
||||||
|
};
|
||||||
api["getObjectByFormId"] = [](std::string_view formIdStr) -> GObject {
|
api["getObjectByFormId"] = [](std::string_view formIdStr) -> GObject {
|
||||||
ESM::RefId refId = ESM::RefId::deserializeText(formIdStr);
|
ESM::RefId refId = ESM::RefId::deserializeText(formIdStr);
|
||||||
if (!refId.is<ESM::FormId>())
|
if (!refId.is<ESM::FormId>())
|
||||||
@ -188,6 +194,10 @@ namespace MWLua
|
|||||||
checkGameInitialized(lua);
|
checkGameInitialized(lua);
|
||||||
return MWBase::Environment::get().getESMStore()->insert(potion);
|
return MWBase::Environment::get().getESMStore()->insert(potion);
|
||||||
},
|
},
|
||||||
|
[lua = context.mLua](const ESM::NPC& npc) -> const ESM::NPC* {
|
||||||
|
checkGameInitialized(lua);
|
||||||
|
return MWBase::Environment::get().getESMStore()->insert(npc);
|
||||||
|
},
|
||||||
[lua = context.mLua](const ESM::Weapon& weapon) -> const ESM::Weapon* {
|
[lua = context.mLua](const ESM::Weapon& weapon) -> const ESM::Weapon* {
|
||||||
checkGameInitialized(lua);
|
checkGameInitialized(lua);
|
||||||
return MWBase::Environment::get().getESMStore()->insert(weapon);
|
return MWBase::Environment::get().getESMStore()->insert(weapon);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user