diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2008-02-07 09:22:53 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2008-02-07 09:22:53 +0000 |
commit | e9e633a5b78799311ee765944a7aa52201bcef9d (patch) | |
tree | 13a78b9477dd1814ae68ab69e5ff4ec15cf1c413 /src/scripting | |
parent | 05de4c0aa765fb1a448687c068ddae9ecd880542 (diff) | |
download | manaserv-e9e633a5b78799311ee765944a7aa52201bcef9d.tar.gz manaserv-e9e633a5b78799311ee765944a7aa52201bcef9d.tar.xz manaserv-e9e633a5b78799311ee765944a7aa52201bcef9d.zip |
Fail gracefully on invalid monster IDs, don't leak monsters when called without
a map and handle insertion failures.
Diffstat (limited to 'src/scripting')
-rw-r--r-- | src/scripting/lua.cpp | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index d049054..a5de991 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -380,24 +380,34 @@ static int LuaMonster_Create(lua_State *s) LOG_WARN("LuaMonster_Create called with incorrect parameters."); return 0; } + lua_pushlightuserdata(s, (void *)®istryKey); lua_gettable(s, LUA_REGISTRYINDEX); Script *t = static_cast<Script *>(lua_touserdata(s, -1)); - - MonsterClass *spec = MonsterManager::getMonster(lua_tointeger(s, 1)); - Monster *q = new Monster(spec); MapComposite *m = t->getMap(); if (!m) { LOG_WARN("LuaMonster_Create called outside a map."); return 0; } + + int monsterId = lua_tointeger(s, 1); + MonsterClass *spec = MonsterManager::getMonster(monsterId); + if (!spec) + { + LOG_WARN("LuaMonster_Create invalid monster ID: " << monsterId); + return 0; + } + + Monster *q = new Monster(spec); q->setMap(m); q->setPosition(Point(lua_tointeger(s, 2), lua_tointeger(s, 3))); - bool b = GameState::insert(q); - /* Do not try to deal with a failure there. There are some serious issues - if an insertion failed on an almost empty map. */ - assert(b); (void)b; + if (!GameState::insertSafe(q)) + { + LOG_WARN("LuaMonster_Create failed to insert monster"); + return 0; + } + lua_pushlightuserdata(s, q); return 1; }; |