diff options
author | Erik Schilling <ablu.erikschilling@googlemail.com> | 2011-08-19 03:00:02 +0800 |
---|---|---|
committer | Yohann Ferreira <yohann.ferreira@orange.fr> | 2011-08-19 05:20:09 +0800 |
commit | 28c72e31820ca1ccdca927d5484ad4c3a0c28048 (patch) | |
tree | 8dec1e46bc27b4fd76713a8d709d0ccb0479561b | |
parent | 8900f7ccec26fb3e875c922ad5ececf3d6327441 (diff) | |
download | manaserv-28c72e31820ca1ccdca927d5484ad4c3a0c28048.tar.gz manaserv-28c72e31820ca1ccdca927d5484ad4c3a0c28048.tar.bz2 manaserv-28c72e31820ca1ccdca927d5484ad4c3a0c28048.tar.xz manaserv-28c72e31820ca1ccdca927d5484ad4c3a0c28048.zip |
Created function to make getting monsters easier in scripts.
-rw-r--r-- | src/scripting/lua.cpp | 17 | ||||
-rw-r--r-- | src/scripting/luautil.cpp | 11 | ||||
-rw-r--r-- | src/scripting/luautil.h | 2 |
3 files changed, 16 insertions, 14 deletions
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index cfc07f10..7cc09d56 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -1130,12 +1130,7 @@ static int monster_get_name(lua_State *s) static int monster_change_anger(lua_State *s) { const int anger = luaL_checkint(s, 3); - if (!lua_islightuserdata(s, 1)) - { - lua_pushboolean(s, false); - return 1; - } - Monster *m = dynamic_cast<Monster *>((Thing *)lua_touserdata(s, 1)); + Monster *m = getMonster(s, 1); if (!m) { raiseScriptError(s, "monster_change_anger called " @@ -1159,14 +1154,8 @@ static int monster_change_anger(lua_State *s) */ static int monster_remove(lua_State *s) { - if (!lua_islightuserdata(s, 1)) - { - lua_pushboolean(s, false); - return 1; - } - bool monsterEnqueued = false; - Monster *m = dynamic_cast<Monster *>((Thing *)lua_touserdata(s, 1)); + Monster *m = getMonster(s, 1); if (m) { GameState::enqueueRemove(m); @@ -1182,7 +1171,7 @@ static int monster_remove(lua_State *s) */ static int monster_load_script(lua_State *s) { - Monster *m = static_cast< Monster* >(getBeing(s, 1)); + Monster *m = getMonster(s, 1); if (!m) { raiseScriptError(s, "monster_load_script called " diff --git a/src/scripting/luautil.cpp b/src/scripting/luautil.cpp index 4a5bee9a..b7680c6b 100644 --- a/src/scripting/luautil.cpp +++ b/src/scripting/luautil.cpp @@ -23,6 +23,7 @@ #include "game-server/character.h" #include "game-server/npc.h" +#include "game-server/monster.h" #include "utils/logger.h" @@ -78,6 +79,16 @@ Character *getCharacter(lua_State *s, int p) return static_cast<Character *>(t); } +Monster *getMonster(lua_State *s, int p) +{ + if (!lua_islightuserdata(s, p)) + return 0; + Thing *t = static_cast<Thing *>(lua_touserdata(s, p)); + if (t->getType() != OBJECT_MONSTER) + return 0; + return static_cast<Monster *>(t); +} + Being *getBeing(lua_State *s, int p) { if (!lua_islightuserdata(s, p)) diff --git a/src/scripting/luautil.h b/src/scripting/luautil.h index 87185155..c55b04b7 100644 --- a/src/scripting/luautil.h +++ b/src/scripting/luautil.h @@ -34,6 +34,7 @@ extern "C" { class Being; class NPC; class Character; +class Monster; class Thing; // Report script errors and interrupt the script. @@ -43,6 +44,7 @@ void raiseWarning(lua_State *s, const char *format, ...); NPC *getNPC(lua_State *s, int p); Character *getCharacter(lua_State *s, int p); +Monster *getMonster(lua_State *s, int p); Being *getBeing(lua_State *s, int p); |