Skip to content

Commit

Permalink
Merge branch 'lua_class_data' into 'master'
Browse files Browse the repository at this point in the history
Add class records to lua

See merge request OpenMW/openmw!3515
  • Loading branch information
Assumeru committed Nov 18, 2023
2 parents d5906dc + 3e3a395 commit 5a1a54b
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ message(STATUS "Configuring OpenMW...")
set(OPENMW_VERSION_MAJOR 0)
set(OPENMW_VERSION_MINOR 49)
set(OPENMW_VERSION_RELEASE 0)
set(OPENMW_LUA_API_REVISION 50)
set(OPENMW_LUA_API_REVISION 51)
set(OPENMW_POSTPROCESSING_API_REVISION 1)

set(OPENMW_VERSION_COMMITHASH "")
Expand Down
2 changes: 1 addition & 1 deletion apps/openmw/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ add_openmw_dir (mwlua
context globalscripts localscripts playerscripts luabindings objectbindings cellbindings mwscriptbindings
camerabindings vfsbindings uibindings soundbindings inputbindings nearbybindings postprocessingbindings stats debugbindings
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 types/clothing types/levelledlist types/terminal
worker magicbindings factionbindings
worker magicbindings factionbindings classbindings
)

add_openmw_dir (mwsound
Expand Down
80 changes: 80 additions & 0 deletions apps/openmw/mwlua/classbindings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <components/esm3/loadclas.hpp>
#include <components/lua/luastate.hpp>

#include "../mwbase/environment.hpp"
#include "../mwworld/class.hpp"
#include "../mwworld/esmstore.hpp"

#include "classbindings.hpp"
#include "luamanagerimp.hpp"
#include "stats.hpp"
#include "types/types.hpp"

namespace sol
{
template <>
struct is_automagical<ESM::Class> : std::false_type
{
};
template <>
struct is_automagical<MWWorld::Store<ESM::Class>> : std::false_type
{
};
}

namespace MWLua
{

sol::table initCoreClassBindings(const Context& context)
{
sol::state_view& lua = context.mLua->sol();
sol::table classes(context.mLua->sol(), sol::create);
addRecordFunctionBinding<ESM::Class>(classes, context);

auto classT = lua.new_usertype<ESM::Class>("ESM3_Class");
classT[sol::meta_function::to_string]
= [](const ESM::Class& rec) -> std::string { return "ESM3_Class[" + rec.mId.toDebugString() + "]"; };
classT["id"] = sol::readonly_property([](const ESM::Class& rec) { return rec.mId.serializeText(); });
classT["name"] = sol::readonly_property([](const ESM::Class& rec) -> std::string_view { return rec.mName; });
classT["description"]
= sol::readonly_property([](const ESM::Class& rec) -> std::string_view { return rec.mDescription; });

classT["attributes"] = sol::readonly_property([lua](const ESM::Class& rec) -> sol::table {
sol::table res(lua, sol::create);
auto attribute = rec.mData.mAttribute;
for (size_t i = 0; i < attribute.size(); ++i)
{
ESM::RefId attributeId = ESM::Attribute::indexToRefId(attribute[i]);
res[i + 1] = attributeId.serializeText();
}
return res;
});
classT["majorSkills"] = sol::readonly_property([lua](const ESM::Class& rec) -> sol::table {
sol::table res(lua, sol::create);
auto skills = rec.mData.mSkills;
for (size_t i = 0; i < skills.size(); ++i)
{
ESM::RefId skillId = ESM::Skill::indexToRefId(skills[i][1]);
res[i + 1] = skillId.serializeText();
}
return res;
});
classT["minorSkills"] = sol::readonly_property([lua](const ESM::Class& rec) -> sol::table {
sol::table res(lua, sol::create);
auto skills = rec.mData.mSkills;
for (size_t i = 0; i < skills.size(); ++i)
{
ESM::RefId skillId = ESM::Skill::indexToRefId(skills[i][0]);
res[i + 1] = skillId.serializeText();
}
return res;
});

classT["specialization"] = sol::readonly_property([](const ESM::Class& rec) -> std::string_view {
return ESM::Class::specializationIndexToLuaId.at(rec.mData.mSpecialization);
});
classT["isPlayable"]
= sol::readonly_property([](const ESM::Class& rec) -> bool { return rec.mData.mIsPlayable; });
return LuaUtil::makeReadOnly(classes);
}
}
13 changes: 13 additions & 0 deletions apps/openmw/mwlua/classbindings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef MWLUA_CLASSBINDINGS_H
#define MWLUA_CLASSBINDINGS_H

#include <sol/forward.hpp>

#include "context.hpp"

namespace MWLua
{
sol::table initCoreClassBindings(const Context& context);
}

#endif // MWLUA_CLASSBINDINGS_H
3 changes: 3 additions & 0 deletions apps/openmw/mwlua/stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,9 @@ namespace MWLua
skillT["name"] = sol::readonly_property([](const ESM::Skill& rec) -> std::string_view { return rec.mName; });
skillT["description"]
= sol::readonly_property([](const ESM::Skill& rec) -> std::string_view { return rec.mDescription; });
skillT["specialization"] = sol::readonly_property([](const ESM::Skill& rec) -> std::string_view {
return ESM::Class::specializationIndexToLuaId.at(rec.mData.mSpecialization);
});
skillT["icon"] = sol::readonly_property([vfs](const ESM::Skill& rec) -> std::string {
return Misc::ResourceHelpers::correctIconPath(rec.mIcon, vfs);
});
Expand Down
3 changes: 3 additions & 0 deletions apps/openmw/mwlua/types/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <apps/openmw/mwworld/class.hpp>
#include <apps/openmw/mwworld/esmstore.hpp>

#include "../classbindings.hpp"
#include "../localscripts.hpp"
#include "../stats.hpp"

Expand Down Expand Up @@ -81,6 +82,8 @@ 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);

// 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();
Expand Down
1 change: 1 addition & 0 deletions components/esm3/loadclas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace ESM
{
const std::string_view Class::sGmstSpecializationIds[3]
= { "sSpecializationCombat", "sSpecializationMagic", "sSpecializationStealth" };
const std::array<std::string_view, 3> Class::specializationIndexToLuaId = { "combat", "magic", "stealth" };

int32_t& Class::CLDTstruct::getSkill(int index, bool major)
{
Expand Down
1 change: 1 addition & 0 deletions components/esm3/loadclas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace ESM
};

static const std::string_view sGmstSpecializationIds[3];
static const std::array<std::string_view, 3> specializationIndexToLuaId;

struct CLDTstruct
{
Expand Down
1 change: 1 addition & 0 deletions files/lua_api/openmw/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@
-- @field #string name Human-readable name
-- @field #string description Human-readable description
-- @field #string icon VFS path to the icon
-- @field #string specialization Skill specialization. Either combat, magic, or stealth.
-- @field #MagicSchoolData school Optional magic school
-- @field #string attribute The id of the skill's governing attribute

Expand Down
25 changes: 25 additions & 0 deletions files/lua_api/openmw/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,31 @@
-- @param openmw.core#GameObject actor
-- @return #number

--- @{#Classes}: Class Data
-- @field [parent=#NPC] #Classes classes

---
-- A read-only list of all @{#ClassRecord}s in the world database.
-- @field [parent=#Classes] #list<#ClassRecord> records

---
-- Returns a read-only @{#ClassRecord}
-- @function [parent=#Classes] record
-- @param #string recordId
-- @return #ClassRecord

---
-- Class data record
-- @type ClassRecord
-- @field #string id Class id
-- @field #string name Class name
-- @field #list<#string> attributes A read-only list containing the specialized attributes of the class.
-- @field #list<#string> majorSkills A read-only list containing the major skills of the class.
-- @field #list<#string> minorSkills A read-only list containing the minor skills of the class.
-- @field #string description Class description
-- @field #boolean isPlayable True if the player can play as this class
-- @field #string specialization Class specialization. Either combat, magic, or stealth.

---
-- Whether the NPC or player is in the werewolf form at the moment.
-- @function [parent=#NPC] isWerewolf
Expand Down

0 comments on commit 5a1a54b

Please sign in to comment.