summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPhilipp Sehmisch <tmw@crushnet.org>2008-02-07 01:18:39 +0000
committerPhilipp Sehmisch <tmw@crushnet.org>2008-02-07 01:18:39 +0000
commit05de4c0aa765fb1a448687c068ddae9ecd880542 (patch)
tree2654f4eaf1984062d9f7333d09c0064242ce154f /src
parentd4ad5de48f7f1e2a84bccd68b2584ce78fed05a4 (diff)
downloadmanaserv-05de4c0aa765fb1a448687c068ddae9ecd880542.tar.gz
manaserv-05de4c0aa765fb1a448687c068ddae9ecd880542.tar.bz2
manaserv-05de4c0aa765fb1a448687c068ddae9ecd880542.tar.xz
manaserv-05de4c0aa765fb1a448687c068ddae9ecd880542.zip
Implemented LUA callback function for creating monsters.
Diffstat (limited to 'src')
-rw-r--r--src/scripting/lua.cpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp
index 048bc6f0..d0490547 100644
--- a/src/scripting/lua.cpp
+++ b/src/scripting/lua.cpp
@@ -36,6 +36,8 @@ extern "C" {
#include "game-server/item.hpp"
#include "game-server/itemmanager.hpp"
#include "game-server/mapmanager.hpp"
+#include "game-server/monster.hpp"
+#include "game-server/monstermanager.hpp"
#include "game-server/npc.hpp"
#include "game-server/quest.hpp"
#include "game-server/state.hpp"
@@ -368,6 +370,39 @@ static int LuaNpc_Trade(lua_State *s)
}
/**
+ * Callback for creating a monster on the current map.
+ * tmw.monster_create(int type, int x, int y)
+ */
+static int LuaMonster_Create(lua_State *s)
+{
+ if (!lua_isnumber(s, 1) || !lua_isnumber(s, 2) || !lua_isnumber(s, 3))
+ {
+ LOG_WARN("LuaMonster_Create called with incorrect parameters.");
+ return 0;
+ }
+ lua_pushlightuserdata(s, (void *)&registryKey);
+ 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;
+ }
+ 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;
+ lua_pushlightuserdata(s, q);
+ return 1;
+};
+
+/**
* Called when the server has recovered the value of a quest variable.
*/
void LuaScript::getQuestCallback(Character *q, std::string const &name,
@@ -447,6 +482,7 @@ LuaScript::LuaScript():
{ "chr_inv_count", &LuaChr_InvCount },
{ "chr_get_quest", &LuaChr_GetQuest },
{ "chr_set_quest", &LuaChr_SetQuest },
+ { "monster_create", &LuaMonster_Create },
{ NULL, NULL }
};
luaL_register(mState, "tmw", callbacks);