diff options
Diffstat (limited to 'src/scripting')
-rw-r--r-- | src/scripting/lua.cpp | 91 | ||||
-rw-r--r-- | src/scripting/luascript.cpp | 7 | ||||
-rw-r--r-- | src/scripting/luascript.h | 4 | ||||
-rw-r--r-- | src/scripting/luautil.cpp | 17 | ||||
-rw-r--r-- | src/scripting/luautil.h | 8 | ||||
-rw-r--r-- | src/scripting/script.h | 4 |
6 files changed, 101 insertions, 30 deletions
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index c151b9fa..0193d888 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -289,8 +289,10 @@ static int npc_create(lua_State *s) npc->addComponent(beingComponent); npc->addComponent(npcComponent); // some health so it doesn't spawn dead - beingComponent->setAttribute(*npc, ATTR_MAX_HP, 100); - beingComponent->setAttribute(*npc, ATTR_HP, 100); + auto *maxHpAttribute = attributeManager->getAttributeInfo(ATTR_MAX_HP); + beingComponent->setAttribute(*npc, maxHpAttribute, 100); + auto *hpAttribute = attributeManager->getAttributeInfo(ATTR_HP); + beingComponent->setAttribute(*npc, hpAttribute, 100); beingComponent->setName(name); beingComponent->setGender(getGender(gender)); @@ -1367,11 +1369,8 @@ static int entity_walk(lua_State *s) if (lua_gettop(s) >= 4) { const double speedTps = luaL_checknumber(s, 4); - beingComponent->setAttribute(*being, ATTR_MOVE_SPEED_TPS, speedTps); - const double modifiedSpeedTps = - beingComponent->getModifiedAttribute(ATTR_MOVE_SPEED_TPS); - beingComponent->setAttribute(*being, ATTR_MOVE_SPEED_RAW, - utils::tpsToRawSpeed(modifiedSpeedTps)); + auto *tpsSpeedAttribute = attributeManager->getAttributeInfo(ATTR_MOVE_SPEED_TPS); + beingComponent->setAttribute(*being, tpsSpeedAttribute, speedTps); } return 0; @@ -1724,10 +1723,9 @@ static int entity_get_y(lua_State *s) static int entity_get_base_attribute(lua_State *s) { Entity *being = checkBeing(s, 1); - int attr = checkAttribute(s, 2)->id; - luaL_argcheck(s, attr > 0, 2, "invalid attribute id"); + auto *attribute = checkAttribute(s, 2); - lua_pushinteger(s, being->getComponent<BeingComponent>()->getAttributeBase(attr)); + lua_pushinteger(s, being->getComponent<BeingComponent>()->getAttributeBase(attribute)); return 1; } @@ -1742,10 +1740,10 @@ static int entity_get_base_attribute(lua_State *s) static int entity_set_base_attribute(lua_State *s) { Entity *being = checkBeing(s, 1); - int attr = checkAttribute(s, 2)->id; + auto *attribute = checkAttribute(s, 2); double value = luaL_checknumber(s, 3); - being->getComponent<BeingComponent>()->setAttribute(*being, attr, value); + being->getComponent<BeingComponent>()->setAttribute(*being, attribute, value); return 0; } @@ -1772,11 +1770,10 @@ static int entity_set_base_attribute(lua_State *s) static int entity_get_modified_attribute(lua_State *s) { Entity *being = checkBeing(s, 1); - int attr = checkAttribute(s, 2)->id; - luaL_argcheck(s, attr > 0, 2, "invalid attribute id"); + auto *attribute = checkAttribute(s, 2); const double value = - being->getComponent<BeingComponent>()->getModifiedAttribute(attr); + being->getComponent<BeingComponent>()->getModifiedAttribute(attribute); lua_pushinteger(s, value); return 1; } @@ -1802,15 +1799,15 @@ static int entity_get_modified_attribute(lua_State *s) static int entity_apply_attribute_modifier(lua_State *s) { Entity *being = checkBeing(s, 1); - int attr = checkAttribute(s, 2)->id; + auto *attribute = checkAttribute(s, 2); double value = luaL_checknumber(s, 3); int layer = luaL_checkint(s, 4); int duration = luaL_optint(s, 5, 0); int effectId = luaL_optint(s, 6, 0); - being->getComponent<BeingComponent>()->applyModifier(*being, attr, value, - layer, duration, - effectId); + being->getComponent<BeingComponent>()->applyModifier(*being, attribute, + value, layer, + duration, effectId); return 0; } @@ -1825,13 +1822,14 @@ static int entity_apply_attribute_modifier(lua_State *s) static int entity_remove_attribute_modifier(lua_State *s) { Entity *being = checkBeing(s, 1); - int attr = checkAttribute(s, 2)->id; + auto *attribute = checkAttribute(s, 2); double value = luaL_checknumber(s, 3); int layer = luaL_checkint(s, 4); int effectId = luaL_optint(s, 5, 0); - being->getComponent<BeingComponent>()->removeModifier(*being, attr, value, - layer, effectId); + being->getComponent<BeingComponent>()->removeModifier(*being, attribute, + value, layer, + effectId); return 0; } @@ -2910,6 +2908,47 @@ static int abilityinfo_on_recharged(lua_State *s) } +/** LUA_CATEGORY AttributeInfo class (attributeinfoclass) + */ + +/** LUA get_attribute_info (attributeinfoclass) + * local attributeinfo = get_attribute_info(string name) + * local attributeinfo = get_attribute_info(int id) + ** + * **Return value:** The attribute info of the passed attribute. + */ +static int get_attribute_info(lua_State *s) +{ + auto *attributeInfo = checkAttribute(s, 1); + LuaAttributeInfo::push(s, attributeInfo); + return 1; +} + +/** LUA attributeinfo:name (attributeinfoclass) + * local id = attributeinfo:id() + ** + * **Return value:** The id of the `attributeinfo`. + */ +static int attributeinfo_get_id(lua_State *s) +{ + auto *attributeInfo = LuaAttributeInfo::check(s, 1); + lua_pushinteger(s, attributeInfo->id); + return 1; +} + +/** LUA attributeinfo:name (attributeinfoclass) + * local name = attributeinfo:name() + ** + * **Return value:** The name of the `attributeinfo`. + */ +static int attributeinfo_get_name(lua_State *s) +{ + auto *attributeInfo = LuaAttributeInfo::check(s, 1); + lua_pushstring(s, attributeInfo->name.c_str()); + return 1; +} + + /** LUA_CATEGORY Status effect class (statuseffectclass) */ @@ -3317,6 +3356,7 @@ LuaScript::LuaScript(): { "map_get_objects", map_get_objects }, { "announce", announce }, { "get_ability_info", get_ability_info }, + { "get_attribute_info", get_attribute_info }, { nullptr, nullptr } }; #if LUA_VERSION_NUM < 502 @@ -3428,12 +3468,19 @@ LuaScript::LuaScript(): { nullptr, nullptr} }; + static luaL_Reg const members_AttributeInfo[] = { + { "id", attributeinfo_get_id }, + { "name", attributeinfo_get_name }, + { nullptr, nullptr} + }; + LuaEntity::registerType(mRootState, "Entity", members_Entity); LuaItemClass::registerType(mRootState, "ItemClass", members_ItemClass); LuaMapObject::registerType(mRootState, "MapObject", members_MapObject); LuaMonsterClass::registerType(mRootState, "MonsterClass", members_MonsterClass); LuaStatusEffect::registerType(mRootState, "StatusEffect", members_StatusEffect); LuaAbilityInfo::registerType(mRootState, "AbilityInfo", members_AbilityInfo); + LuaAttributeInfo::registerType(mRootState, "AttributeInfo", members_AttributeInfo); // Make script object available to callback functions. lua_pushlightuserdata(mRootState, const_cast<char *>(®istryKey)); diff --git a/src/scripting/luascript.cpp b/src/scripting/luascript.cpp index 1617f74c..5ce55d13 100644 --- a/src/scripting/luascript.cpp +++ b/src/scripting/luascript.cpp @@ -113,6 +113,13 @@ void LuaScript::push(const std::list<InventoryItem> &itemList) ++nbArgs; } +void LuaScript::push(AttributeManager::AttributeInfo *attributeInfo) +{ + assert(nbArgs >= 0); + ::push(mCurrentState, attributeInfo); + ++nbArgs; +} + int LuaScript::execute(const Context &context) { assert(nbArgs >= 0); diff --git a/src/scripting/luascript.h b/src/scripting/luascript.h index b874976a..a30cbf08 100644 --- a/src/scripting/luascript.h +++ b/src/scripting/luascript.h @@ -54,12 +54,10 @@ class LuaScript : public Script void prepareResume(Thread *thread); void push(int); - void push(const std::string &); - void push(Entity *); - void push(const std::list<InventoryItem> &itemList); + void push(AttributeManager::AttributeInfo *); int execute(const Context &context = Context()); diff --git a/src/scripting/luautil.cpp b/src/scripting/luautil.cpp index c9738694..5a0cf883 100644 --- a/src/scripting/luautil.cpp +++ b/src/scripting/luautil.cpp @@ -226,13 +226,22 @@ AbilityManager::AbilityInfo *checkAbility(lua_State *s, int p) return abilityInfo; } -const AttributeManager::AttributeInfo *checkAttribute(lua_State *s, int p) +AttributeManager::AttributeInfo *checkAttribute(lua_State *s, int p) { - const AttributeManager::AttributeInfo *attributeInfo; - if (lua_isnumber(s, p)) + AttributeManager::AttributeInfo *attributeInfo; + + switch (lua_type(s, p)) + { + case LUA_TNUMBER: attributeInfo = attributeManager->getAttributeInfo(luaL_checkint(s, p)); - else + break; + case LUA_TSTRING: attributeInfo = attributeManager->getAttributeInfo(luaL_checkstring(s, p)); + break; + case LUA_TUSERDATA: + attributeInfo = LuaAttributeInfo::check(s, p); + break; + } luaL_argcheck(s, attributeInfo != nullptr, p, "invalid attribute"); return attributeInfo; diff --git a/src/scripting/luautil.h b/src/scripting/luautil.h index 220d4c2f..03c4666e 100644 --- a/src/scripting/luautil.h +++ b/src/scripting/luautil.h @@ -165,6 +165,7 @@ typedef LuaUserData<MapObject> LuaMapObject; typedef LuaUserData<MonsterClass> LuaMonsterClass; typedef LuaUserData<StatusEffect> LuaStatusEffect; typedef LuaUserData<AbilityManager::AbilityInfo> LuaAbilityInfo; +typedef LuaUserData<AttributeManager::AttributeInfo> LuaAttributeInfo; Script * getScript(lua_State *s); @@ -179,7 +180,7 @@ Entity * checkMonster(lua_State *s, int p); MonsterClass * checkMonsterClass(lua_State *s, int p); Entity * checkNpc(lua_State *s, int p); AbilityManager::AbilityInfo * checkAbility(lua_State *s, int p); -const AttributeManager::AttributeInfo *checkAttribute(lua_State *s, int p); +AttributeManager::AttributeInfo * checkAttribute(lua_State *s, int p); unsigned char checkWalkMask(lua_State *s, int p); MapComposite * checkCurrentMap(lua_State *s, Script *script = 0); @@ -219,6 +220,11 @@ inline void push(lua_State *s, MonsterClass *val) LuaMonsterClass::push(s, val); } +inline void push(lua_State *s, AttributeManager::AttributeInfo *val) +{ + LuaAttributeInfo::push(s, val); +} + /* Pushes an STL LIST */ template <typename T> diff --git a/src/scripting/script.h b/src/scripting/script.h index 7cf457cc..1f8c415e 100644 --- a/src/scripting/script.h +++ b/src/scripting/script.h @@ -24,6 +24,8 @@ #include "common/inventorydata.h" #include "common/manaserv_protocol.h" +#include "game-server/attributemanager.h" + #include <list> #include <string> #include <vector> @@ -193,6 +195,8 @@ class Script : public sigc::trackable */ virtual void push(const std::list<InventoryItem> &itemList) = 0; + virtual void push(AttributeManager::AttributeInfo *) = 0; + /** * Executes the function being prepared. * @param context the context that is supposed to be used for executing |