summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/scripting/lua.cpp17
-rw-r--r--src/scripting/luautil.cpp11
-rw-r--r--src/scripting/luautil.h2
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);