diff --git a/Source/lua/metadoc.hpp b/Source/lua/metadoc.hpp index 9f69da4dd5b..d74c98f1c2a 100644 --- a/Source/lua/metadoc.hpp +++ b/Source/lua/metadoc.hpp @@ -18,6 +18,13 @@ inline std::string LuaDocstringKey(std::string_view key) return StrCat("__doc_", key); } +template +void SetDocumented(sol::usertype &table, std::string_view key, std::string_view signature, std::string_view doc, T &&value) +{ + table[key] = std::forward(value); + // TODO: figure out a way to set signature and docstring. +} + template void SetDocumented(sol::table &table, std::string_view key, std::string_view signature, std::string_view doc, T &&value) { diff --git a/Source/lua/modules/player.cpp b/Source/lua/modules/player.cpp index 1629265a514..ad2b72d310e 100644 --- a/Source/lua/modules/player.cpp +++ b/Source/lua/modules/player.cpp @@ -1,5 +1,7 @@ #include "lua/modules/player.hpp" +#include + #include #include "engine/point.hpp" @@ -7,10 +9,37 @@ #include "player.h" namespace devilution { +namespace { +void InitPlayerUserType(sol::state_view &lua) +{ + sol::usertype playerType = lua.new_usertype(sol::no_constructor); + SetDocumented(playerType, "name", "", + "Player's name (readonly)", + sol::readonly_property(&Player::name)); + SetDocumented(playerType, "addExperience", "(experience: integer, monsterLevel: integer = nil)", + "Adds experience to this player based on the current game mode", + [](Player &player, uint32_t experience, std::optional monsterLevel) { + if (monsterLevel.has_value()) { + player.addExperience(experience, *monsterLevel); + } else { + player.addExperience(experience); + } + }); + SetDocumented(playerType, "characterLevel", "", + "Character level (writeable)", + sol::property(&Player::getCharacterLevel, &Player::setCharacterLevel)); +} +} // namespace sol::table LuaPlayerModule(sol::state_view &lua) { + InitPlayerUserType(lua); sol::table table = lua.create_table(); + SetDocumented(table, "self", "()", + "The current player", + []() { + return MyPlayer; + }); SetDocumented(table, "walk_to", "(x: integer, y: integer)", "Walk to the given coordinates", [](int x, int y) { diff --git a/Source/player.h b/Source/player.h index 8e917057e11..28ee469bce7 100644 --- a/Source/player.h +++ b/Source/player.h @@ -10,6 +10,7 @@ #include #include +#include #include "diablo.h" #include "engine/actor_position.hpp" @@ -369,6 +370,11 @@ struct Player { uint16_t wReflections; ItemSpecialEffectHf pDamAcFlags; + [[nodiscard]] std::string_view name() const + { + return _pName; + } + /** * @brief Convenience function to get the base stats/bonuses for this player's class */