summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--data/test.lua12
-rw-r--r--src/scripting/lua.cpp36
3 files changed, 54 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index d0c2ac4b..530f4885 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2008-02-06 Philipp Sehmisch <tmw@crushnet.org>
+
+ * src/scripting/lua.cpp: Implemented LUA callback function for
+ creating monsters.
+ * data/test.lua: Added NPC for testing scripted monster creation.
+
2008-02-05 Philipp Sehmisch <tmw@crushnet.org>
* src/game-server/monster.cpp, src/game-server/monster.hpp,
diff --git a/data/test.lua b/data/test.lua
index 47b06519..556ce33d 100644
--- a/data/test.lua
+++ b/data/test.lua
@@ -5,6 +5,7 @@
atinit(function()
create_npc(110, 50 * 32 + 16, 19 * 32 + 16, my_npc1)
create_npc(108, 51 * 32 + 16, 25 * 32 + 16, my_npc4)
+ create_npc(126, 45 * 32 + 16, 25 * 32 + 16, my_npc5)
end)
function my_npc1(npc, ch)
@@ -62,3 +63,14 @@ function my_npc4(npc, ch)
end
end
end
+
+function my_npc5(npc, ch)
+ do_message(npc, ch, "I am the spider tamer. Do you want me to spawn some spiders?")
+ local answer = do_choice(npc, ch, "Yes", "No");
+ if answer == 1 then
+ tmw.monster_create(1012, 44 * 32 + 16, 24 * 32 + 16)
+ tmw.monster_create(1012, 44 * 32 + 16, 26 * 32 + 16)
+ tmw.monster_create(1012, 46 * 32 + 16, 24 * 32 + 16)
+ tmw.monster_create(1012, 46 * 32 + 16, 26 * 32 + 16)
+ end
+end \ No newline at end of file
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);