diff options
author | Erik Schilling <ablu.erikschilling@googlemail.com> | 2013-02-20 15:34:28 +0100 |
---|---|---|
committer | Erik Schilling <ablu.erikschilling@googlemail.com> | 2013-02-20 15:34:28 +0100 |
commit | c70c6d19c1cabf46b595591802bceab63d371788 (patch) | |
tree | 1c153d2bfe560171bad5889d528dc5cf110e8533 /src/scripting/lua.cpp | |
parent | d707495540581f8c1b9ab3d5007c9c4d1ab83b53 (diff) | |
parent | 587b7682e6bf7dd9e616c1d4789a5ed9aa986e6d (diff) | |
download | manaserv-c70c6d19c1cabf46b595591802bceab63d371788.tar.gz manaserv-c70c6d19c1cabf46b595591802bceab63d371788.tar.bz2 manaserv-c70c6d19c1cabf46b595591802bceab63d371788.tar.xz manaserv-c70c6d19c1cabf46b595591802bceab63d371788.zip |
Merge branch 'master' into lpc2012
Conflicts:
gameserver.cbp
src/account-server/accounthandler.cpp
src/game-server/attack.cpp
src/game-server/attack.h
src/game-server/being.cpp
src/game-server/being.h
src/game-server/character.cpp
src/game-server/character.h
src/game-server/inventory.cpp
src/game-server/item.h
src/game-server/monster.cpp
src/game-server/monster.h
Diffstat (limited to 'src/scripting/lua.cpp')
-rw-r--r-- | src/scripting/lua.cpp | 97 |
1 files changed, 64 insertions, 33 deletions
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 6285f382..4e2cd283 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -69,6 +69,30 @@ extern "C" { /** + * on_update_derived_attribute( function(Being*) ): void + * Sets a listener function to handle + * recalculation of derived attributes event. + */ +static int on_update_derived_attribute(lua_State *s) +{ + luaL_checktype(s, 1, LUA_TFUNCTION); + Being::setUpdateDerivedAttributesCallback(getScript(s)); + return 0; +} + + +/** + * on_recalculateBaseAttributeCallback( function(Being*) ): void + * Sets a listener function to the attribute recalculation event. + */ +static int on_recalculate_base_attribute(lua_State *s) +{ + luaL_checktype(s, 1, LUA_TFUNCTION); + Being::setRecalculateBaseAttributeCallback(getScript(s)); + return 0; +} + +/** * on_character_death( function(Character*) ): void * Sets a listener function to the character death event. */ @@ -458,8 +482,6 @@ static int chr_get_inventory(lua_State *s) int firstTableStackPosition = lua_gettop(s); int tableIndex = 1; - std::string itemName = ""; - for (InventoryData::const_iterator it = invData.begin(), it_end = invData.end(); it != it_end; ++it) { @@ -470,20 +492,19 @@ static int chr_get_inventory(lua_State *s) lua_createtable(s, 0, 4); int subTableStackPosition = lua_gettop(s); // Stores the item info in it. - lua_pushstring(s, "slot"); + lua_pushliteral(s, "slot"); lua_pushinteger(s, it->first); // The slot id lua_settable(s, subTableStackPosition); - lua_pushstring(s, "id"); + lua_pushliteral(s, "id"); lua_pushinteger(s, it->second.itemId); lua_settable(s, subTableStackPosition); - lua_pushstring(s, "name"); - itemName = itemManager->getItem(it->second.itemId)->getName(); - lua_pushstring(s, itemName.c_str()); + lua_pushliteral(s, "name"); + push(s, itemManager->getItem(it->second.itemId)->getName()); lua_settable(s, subTableStackPosition); - lua_pushstring(s, "amount"); + lua_pushliteral(s, "amount"); lua_pushinteger(s, it->second.amount); lua_settable(s, subTableStackPosition); @@ -517,7 +538,6 @@ static int chr_get_equipment(lua_State *s) int firstTableStackPosition = lua_gettop(s); int tableIndex = 1; - std::string itemName; std::set<unsigned> itemInstances; for (EquipData::const_iterator it = equipData.begin(), @@ -534,17 +554,16 @@ static int chr_get_equipment(lua_State *s) lua_createtable(s, 0, 3); int subTableStackPosition = lua_gettop(s); // Stores the item info in it. - lua_pushstring(s, "slot"); + lua_pushliteral(s, "slot"); lua_pushinteger(s, it->first); // The slot id lua_settable(s, subTableStackPosition); - lua_pushstring(s, "id"); + lua_pushliteral(s, "id"); lua_pushinteger(s, it->second.itemId); lua_settable(s, subTableStackPosition); - lua_pushstring(s, "name"); - itemName = itemManager->getItem(it->second.itemId)->getName(); - lua_pushstring(s, itemName.c_str()); + lua_pushliteral(s, "name"); + push(s, itemManager->getItem(it->second.itemId)->getName()); lua_settable(s, subTableStackPosition); // Add the sub-table as value of the main one. @@ -1127,7 +1146,7 @@ static int being_remove_attribute_modifier(lua_State *s) static int being_get_name(lua_State *s) { Being *being = checkBeing(s, 1); - lua_pushstring(s, being->getName().c_str()); + push(s, being->getName()); return 1; } @@ -1309,7 +1328,7 @@ static int monster_get_name(lua_State *s) luaL_error(s, "monster_get_name called with unknown monster id."); return 0; } - lua_pushstring(s, spec->getName().c_str()); + push(s, spec->getName()); return 1; } @@ -1386,7 +1405,7 @@ static int chr_get_quest(lua_State *s) bool res = getQuestVar(q, name, value); if (res) { - lua_pushstring(s, value.c_str()); + push(s, value); return 1; } QuestCallback *f = new QuestThreadCallback(&LuaScript::getQuestCallback, @@ -1453,7 +1472,7 @@ static int chr_try_get_quest(lua_State *s) std::string value; bool res = getQuestVar(q, name, value); if (res) - lua_pushstring(s, value.c_str()); + push(s, value); else lua_pushnil(s); return 1; @@ -1469,9 +1488,8 @@ static int getvar_map(lua_State *s) luaL_argcheck(s, name[0] != 0, 1, "empty variable name"); MapComposite *map = checkCurrentMap(s); - std::string value = map->getVariable(name); - lua_pushstring(s, value.c_str()); + push(s, map->getVariable(name)); return 1; } @@ -1500,8 +1518,7 @@ static int getvar_world(lua_State *s) const char *name = luaL_checkstring(s, 1); luaL_argcheck(s, name[0] != 0, 1, "empty variable name"); - std::string value = GameState::getVariable(name); - lua_pushstring(s, value.c_str()); + push(s, GameState::getVariable(name)); return 1; } @@ -1716,7 +1733,11 @@ static int chr_get_post(lua_State *s) static int being_register(lua_State *s) { Being *being = checkBeing(s, 1); - being->addListener(getScript(s)->getScriptListener()); + Script *script = getScript(s); + + being->signal_died.connect(sigc::mem_fun(script, &Script::processDeathEvent)); + being->signal_removed.connect(sigc::mem_fun(script, &Script::processRemoveEvent)); + return 0; } @@ -2128,8 +2149,7 @@ static int get_map_property(lua_State *s) const char *property = luaL_checkstring(s, 1); Map *map = checkCurrentMap(s)->getMap(); - std::string value = map->getProperty(property); - lua_pushstring(s, value.c_str()); + push(s, map->getProperty(property)); return 1; } @@ -2210,7 +2230,7 @@ static int item_get_name(lua_State *s) luaL_error(s, "item_get_name called with unknown item id."); return 0; } - lua_pushstring(s, it->getName().c_str()); + push(s, it->getName()); return 1; } @@ -2314,7 +2334,7 @@ static int map_object_get_property(lua_State *s) std::string property = obj->getProperty(key); if (!property.empty()) { - lua_pushstring(s, property.c_str()); + push(s, property); return 1; } else @@ -2346,7 +2366,7 @@ static int map_object_get_bounds(lua_State *s) static int map_object_get_name(lua_State *s) { MapObject *obj = LuaMapObject::check(s, 1); - lua_pushstring(s, obj->getName().c_str()); + push(s, obj->getName()); return 1; } @@ -2357,7 +2377,7 @@ static int map_object_get_name(lua_State *s) static int map_object_get_type(lua_State *s) { MapObject *obj = LuaMapObject::check(s, 1); - lua_pushstring(s, obj->getType().c_str()); + push(s, obj->getType()); return 1; } @@ -2398,7 +2418,7 @@ static int get_special_info(lua_State *s) static int specialinfo_get_name(lua_State *s) { SpecialManager::SpecialInfo *info = LuaSpecialInfo::check(s, 1); - lua_pushstring(s, info->name.c_str()); + push(s, info->name); return 1; } @@ -2419,7 +2439,7 @@ static int specialinfo_is_rechargeable(lua_State *s) static int specialinfo_get_category(lua_State *s) { SpecialManager::SpecialInfo *info = LuaSpecialInfo::check(s, 1); - lua_pushstring(s, info->setName.c_str()); + push(s, info->setName); return 1; } @@ -2559,7 +2579,7 @@ static int require_loader(lua_State *s) if (!path.empty()) luaL_loadfile(s, path.c_str()); else - lua_pushstring(s, "File not found"); + lua_pushliteral(s, "File not found"); return 1; } @@ -2573,15 +2593,21 @@ LuaScript::LuaScript(): luaL_openlibs(mRootState); // Register package loader that goes through the resource manager - // table.insert(package.loaders, 2, require_loader) + // package.loaders[2] = require_loader lua_getglobal(mRootState, "package"); +#if LUA_VERSION_NUM < 502 lua_getfield(mRootState, -1, "loaders"); +#else + lua_getfield(mRootState, -1, "searchers"); +#endif lua_pushcfunction(mRootState, require_loader); lua_rawseti(mRootState, -2, 2); lua_pop(mRootState, 2); // Put the callback functions in the scripting environment. static luaL_Reg const callbacks[] = { + { "on_update_derived_attribute", &on_update_derived_attribute }, + { "on_recalculate_base_attribute", &on_recalculate_base_attribute }, { "on_character_death", &on_character_death }, { "on_character_death_accept", &on_character_death_accept }, { "on_character_login", &on_character_login }, @@ -2699,8 +2725,13 @@ LuaScript::LuaScript(): { "get_special_info", &get_special_info }, { NULL, NULL } }; +#if LUA_VERSION_NUM < 502 lua_pushvalue(mRootState, LUA_GLOBALSINDEX); luaL_register(mRootState, NULL, callbacks); +#else + lua_pushglobaltable(mRootState); + luaL_setfuncs(mRootState, callbacks, 0); +#endif lua_pop(mRootState, 1); // pop the globals table static luaL_Reg const members_AttackInfo[] = { |