Merge branch 'acertainsign' into 'master'

Expose birth signs to Lua

See merge request OpenMW/openmw!3850
pull/3234/head
psi29a 11 months ago
commit e9c97b835e

@ -64,7 +64,7 @@ add_openmw_dir (mwlua
context menuscripts globalscripts localscripts playerscripts luabindings objectbindings cellbindings
mwscriptbindings camerabindings vfsbindings uibindings soundbindings inputbindings nearbybindings
postprocessingbindings stats debugbindings corebindings worldbindings worker magicbindings factionbindings
classbindings itemdata inputprocessor animationbindings
classbindings itemdata inputprocessor animationbindings birthsignbindings
types/types types/door types/item types/actor types/container types/lockable types/weapon types/npc
types/creature types/player types/activator types/book types/lockpick types/probe types/apparatus
types/potion types/ingredient types/misc types/repair types/armor types/light types/static

@ -0,0 +1,55 @@
#include <components/esm3/loadbsgn.hpp>
#include <components/lua/luastate.hpp>
#include <components/misc/resourcehelpers.hpp>
#include <components/resource/resourcesystem.hpp>
#include "../mwbase/environment.hpp"
#include "../mwworld/class.hpp"
#include "../mwworld/esmstore.hpp"
#include "birthsignbindings.hpp"
#include "luamanagerimp.hpp"
#include "types/types.hpp"
namespace sol
{
template <>
struct is_automagical<ESM::BirthSign> : std::false_type
{
};
template <>
struct is_automagical<MWWorld::Store<ESM::BirthSign>> : std::false_type
{
};
}
namespace MWLua
{
sol::table initBirthSignRecordBindings(const Context& context)
{
sol::state_view& lua = context.mLua->sol();
sol::table birthSigns(context.mLua->sol(), sol::create);
addRecordFunctionBinding<ESM::BirthSign>(birthSigns, context);
auto signT = lua.new_usertype<ESM::BirthSign>("ESM3_BirthSign");
signT[sol::meta_function::to_string] = [](const ESM::BirthSign& rec) -> std::string {
return "ESM3_BirthSign[" + rec.mId.toDebugString() + "]";
};
signT["id"] = sol::readonly_property([](const ESM::BirthSign& rec) { return rec.mId.serializeText(); });
signT["name"] = sol::readonly_property([](const ESM::BirthSign& rec) -> std::string_view { return rec.mName; });
signT["description"]
= sol::readonly_property([](const ESM::BirthSign& rec) -> std::string_view { return rec.mDescription; });
auto vfs = MWBase::Environment::get().getResourceSystem()->getVFS();
signT["texture"] = sol::readonly_property([vfs](const ESM::BirthSign& rec) -> std::string {
return Misc::ResourceHelpers::correctTexturePath(rec.mTexture, vfs);
});
signT["spells"] = sol::readonly_property([lua](const ESM::BirthSign& rec) -> sol::table {
sol::table res(lua, sol::create);
for (size_t i = 0; i < rec.mPowers.mList.size(); ++i)
res[i + 1] = rec.mPowers.mList[i].serializeText();
return res;
});
return LuaUtil::makeReadOnly(birthSigns);
}
}

@ -0,0 +1,13 @@
#ifndef MWLUA_BIRTHSIGNBINDINGS_H
#define MWLUA_BIRTHSIGNBINDINGS_H
#include <sol/forward.hpp>
#include "context.hpp"
namespace MWLua
{
sol::table initBirthSignRecordBindings(const Context& context);
}
#endif // MWLUA_BIRTHSIGNBINDINGS_H

@ -25,7 +25,7 @@ namespace sol
namespace MWLua
{
sol::table initCoreClassBindings(const Context& context)
sol::table initClassRecordBindings(const Context& context)
{
sol::state_view& lua = context.mLua->sol();
sol::table classes(context.mLua->sol(), sol::create);

@ -7,7 +7,7 @@
namespace MWLua
{
sol::table initCoreClassBindings(const Context& context);
sol::table initClassRecordBindings(const Context& context);
}
#endif // MWLUA_CLASSBINDINGS_H

@ -85,7 +85,7 @@ namespace MWLua
record["baseGold"] = sol::readonly_property([](const ESM::NPC& rec) -> int { return rec.mNpdt.mGold; });
addActorServicesBindings<ESM::NPC>(record, context);
npc["classes"] = initCoreClassBindings(context);
npc["classes"] = initClassRecordBindings(context);
// This function is game-specific, in future we should replace it with something more universal.
npc["isWerewolf"] = [](const Object& o) {

@ -1,5 +1,6 @@
#include "types.hpp"
#include "../birthsignbindings.hpp"
#include "../luamanagerimp.hpp"
#include "apps/openmw/mwbase/inputmanager.hpp"
@ -7,7 +8,11 @@
#include "apps/openmw/mwbase/world.hpp"
#include "apps/openmw/mwmechanics/npcstats.hpp"
#include "apps/openmw/mwworld/class.hpp"
#include "apps/openmw/mwworld/esmstore.hpp"
#include "apps/openmw/mwworld/globals.hpp"
#include "apps/openmw/mwworld/player.hpp"
#include <components/esm3/loadbsgn.hpp>
namespace MWLua
{
@ -34,6 +39,20 @@ namespace sol
};
}
namespace
{
ESM::RefId toBirthSignId(const sol::object& recordOrId)
{
if (recordOrId.is<ESM::BirthSign>())
return recordOrId.as<const ESM::BirthSign*>()->mId;
std::string_view textId = LuaUtil::cast<std::string_view>(recordOrId);
ESM::RefId id = ESM::RefId::deserializeText(textId);
if (!MWBase::Environment::get().getESMStore()->get<ESM::BirthSign>().search(id))
throw std::runtime_error("Failed to find birth sign: " + std::string(textId));
return id;
}
}
namespace MWLua
{
static void verifyPlayer(const Object& player)
@ -170,5 +189,17 @@ namespace MWLua
player["isCharGenFinished"] = [](const Object&) -> bool {
return MWBase::Environment::get().getWorld()->getGlobalFloat(MWWorld::Globals::sCharGenState) == -1;
};
player["birthSigns"] = initBirthSignRecordBindings(context);
player["getBirthSign"] = [](const Object& player) -> std::string {
verifyPlayer(player);
return MWBase::Environment::get().getWorld()->getPlayer().getBirthSign().serializeText();
};
player["setBirthSign"] = [](const Object& player, const sol::object& recordOrId) {
verifyPlayer(player);
if (!dynamic_cast<const GObject*>(&player))
throw std::runtime_error("Only global scripts can change birth signs");
MWBase::Environment::get().getWorld()->getPlayer().setBirthSign(toBirthSignId(recordOrId));
};
}
}

@ -1064,6 +1064,39 @@
-- Values that can be used with getControlSwitch/setControlSwitch.
-- @field [parent=#Player] #CONTROL_SWITCH CONTROL_SWITCH
---
-- @function [parent=#Player] getBirthSign
-- @param openmw.core#GameObject player
-- @return #string The player's birth sign
---
-- Can be used only in global scripts. Note that this does not update the player's spells.
-- @function [parent=#Player] setBirthSign
-- @param openmw.core#GameObject player
-- @param #any recordOrId Record or string ID of the birth sign to assign
--- @{#BirthSigns}: Birth Sign Data
-- @field [parent=#Player] #BirthSigns birthSigns
---
-- A read-only list of all @{#BirthSignRecord}s in the world database.
-- @field [parent=#BirthSigns] #list<#BirthSignRecord> records
---
-- Returns a read-only @{#BirthSignRecord}
-- @function [parent=#BirthSigns] record
-- @param #string recordId
-- @return #BirthSignRecord
---
-- Birth sign data record
-- @type BirthSignRecord
-- @field #string id Birth sign id
-- @field #string name Birth sign name
-- @field #string description Birth sign description
-- @field #string texture Birth sign texture
-- @field #list<#string> spells A read-only list containing the ids of all spells gained from this sign.
---
-- Send an event to menu scripts.
-- @function [parent=#core] sendMenuEvent

Loading…
Cancel
Save