/* * The Mana World Server * Copyright 2007 The Mana World Development Team * * This file is part of The Mana World. * * The Mana World is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * any later version. * * The Mana World is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with The Mana World; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id$ */ #include extern "C" { #include #include } #include "defines.h" #include "game-server/buysell.hpp" #include "game-server/character.hpp" #include "game-server/gamehandler.hpp" #include "game-server/inventory.hpp" #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" #include "net/messageout.hpp" #include "scripting/script.hpp" #include "utils/logger.h" /** * Implementation of the Script class for Lua. */ class LuaScript: public Script { public: /** * Constructor. */ LuaScript(); /** * Destructor. */ ~LuaScript(); void load(char const *); void prepare(std::string const &); void push(int); void push(Thing *); int execute(); static void getQuestCallback(Character *, std::string const &, std::string const &, void *); private: lua_State *mState; int nbArgs; }; static char const registryKey = 0; /* Functions below are unsafe, as they assume the script has passed pointers to objects which have not yet been destroyed. If the script never keeps pointers around, there will be no problem. In order to be safe, the engine should replace pointers by local identifiers and store them in a map. By listening to the death of objects, it could keep track of pointers still valid in the map. TODO: do it. */ static NPC *getNPC(lua_State *s, int p) { if (!lua_islightuserdata(s, p)) return NULL; Thing *t = static_cast(lua_touserdata(s, p)); if (t->getType() != OBJECT_NPC) return NULL; return static_cast(t); } static Character *getCharacter(lua_State *s, int p) { if (!lua_islightuserdata(s, p)) return NULL; Thing *t = static_cast(lua_touserdata(s, p)); if (t->getType() != OBJECT_CHARACTER) return NULL; return static_cast(t); } /** * Callback for sending a NPC_MESSAGE. * tmw.npc_message(npc, character, string) */ static int LuaNpc_Message(lua_State *s) { NPC *p = getNPC(s, 1); Character *q = getCharacter(s, 2); size_t l; char const *m = lua_tolstring(s, 3, &l); if (!p || !q || !m) { LOG_WARN("LuaNpc_Message called with incorrect parameters."); return 0; } MessageOut msg(GPMSG_NPC_MESSAGE); msg.writeShort(p->getPublicID()); msg.writeString(std::string(m), l); gameHandler->sendTo(q, msg); return 0; } /** * Callback for sending a NPC_CHOICE. * tmw.npc_choice(npc, character, string...) */ static int LuaNpc_Choice(lua_State *s) { NPC *p = getNPC(s, 1); Character *q = getCharacter(s, 2); if (!p || !q) { LOG_WARN("LuaNpc_Choice called with incorrect parameters."); return 0; } MessageOut msg(GPMSG_NPC_CHOICE); msg.writeShort(p->getPublicID()); for (int i = 3, i_end = lua_gettop(s); i <= i_end; ++i) { char const *m = lua_tostring(s, i); if (!m) { LOG_WARN("LuaNpc_Choice called with incorrect parameters."); return 0; } msg.writeString(m); } gameHandler->sendTo(q, msg); return 0; } /** * Callback for creating a NPC on the current map with the current script. * tmw.npc_create(int id, int x, int y): npc */ static int LuaNpc_Create(lua_State *s) { if (!lua_isnumber(s, 1) || !lua_isnumber(s, 2) || !lua_isnumber(s, 3)) { LOG_WARN("LuaNpc_Create called with incorrect parameters."); return 0; } lua_pushlightuserdata(s, (void *)®istryKey); lua_gettable(s, LUA_REGISTRYINDEX); Script *t = static_cast