/* * 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/accountconnection.hpp" #include "game-server/buysell.hpp" #include "game-server/character.hpp" #include "game-server/collisiondetection.hpp" #include "game-server/gamehandler.hpp" #include "game-server/inventory.hpp" #include "game-server/item.hpp" #include "game-server/itemmanager.hpp" #include "game-server/mapcomposite.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 "game-server/trigger.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(const std::string &); void push(Thing *); int execute(); static void getQuestCallback(Character *, std::string const &, std::string const &, void *); void processDeathEvent(Being* thing); private: lua_State *mState; int nbArgs; }; static char const registryKey = 0; void raiseScriptError(lua_State *s, const char *format, ...) { va_list args; va_start(args, format); char message[1024]; vsprintf(message, format, args); va_end( args ); LOG_WARN("Lua script error: "<(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); } static Being *getBeing(lua_State *s, int p) { if (!lua_islightuserdata(s, p)) return NULL; Thing *t = static_cast(lua_touserdata(s, p)); 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) { raiseScriptError(s, "npc_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) { raiseScriptError(s, "npc_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) { raiseScriptError(s, "npc_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(string name, int id, int x, int y): npc */ static int LuaNpc_Create(lua_State *s) { if (!lua_isstring(s, 1) || !lua_isnumber(s, 2) || !lua_isnumber(s, 3) || !lua_isnumber(s, 4)) { raiseScriptError(s, "npc_create called with incorrect parameters."); return 0; } lua_pushlightuserdata(s, (void *)®istryKey); lua_gettable(s, LUA_REGISTRYINDEX); Script *t = static_cast