/* * 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 */ #include extern "C" { #include #include } #include "game-server/accountconnection.hpp" #include "game-server/buysell.hpp" #include "game-server/character.hpp" #include "game-server/collisiondetection.hpp" #include "game-server/effect.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/postman.hpp" #include "game-server/quest.hpp" #include "game-server/state.hpp" #include "game-server/trigger.hpp" #include "net/messageout.hpp" #include "scripting/luautil.hpp" #include "scripting/luascript.hpp" #include "utils/logger.h" /* * This file includes all script bindings available to LUA scripts. * When you add or change a script binding please document it on * * http://wiki.themanaworld.org/index.php/Scripting */ /** * Callback for sending a NPC_MESSAGE. * tmw.npc_message(npc, character, string) */ static int npc_message(lua_State *s) { NPC *p = getNPC(s, 1); Character *q = getCharacter(s, 2); size_t l; const char *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 npc_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) { if (lua_isstring(s, i)) { msg.writeString(lua_tostring(s, i)); } else if (lua_istable(s, i)) { lua_pushnil(s); while (lua_next(s, i) != 0) { if (lua_isstring(s, -1)) { msg.writeString(lua_tostring(s, -1)); } else { raiseScriptError(s, "npc_Choice called with incorrect parameters."); return 0; } lua_pop(s, 1); } } else { raiseScriptError(s, "npc_Choice called with incorrect parameters."); return 0; } } gameHandler->sendTo(q, msg); return 0; } /** * Callback for sending a NPC_INTEGER. * tmw.npc_integer(npc, character, min, max, defaut) */ static int npc_ask_integer(lua_State *s) { NPC *p = getNPC(s, 1); Character *q = getCharacter(s, 2); if (!p || !q) { raiseScriptError(s, "npc_integer called with incorrect parameters."); return 0; } MessageOut msg(GPMSG_NPC_NUMBER); msg.writeShort(p->getPublicID()); int min = lua_tointeger(s, 3); int max = lua_tointeger(s, 4); int default_num = min; if (lua_gettop(s) == 5) default_num = lua_tointeger(s, 5); msg.writeLong(min); msg.writeLong(max); msg.writeLong(default_num); gameHandler->sendTo(q, msg); return 0; } /** * Callback for sending a NPC_STRING. * tmw.npc_ask_string(npc, character) */ static int npc_ask_string(lua_State *s) { NPC *p = getNPC(s, 1); Character *q = getCharacter(s, 2); if (!p || !q) { raiseScriptError(s, "npc_string called with incorrect parameters."); return 0; } MessageOut msg(GPMSG_NPC_STRING); msg.writeShort(p->getPublicID()); 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 npc_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