/* * The Mana Server * Copyright (C) 2007-2010 The Mana World Development Team * Copyright (C) 2010 The Mana Developers * * This file is part of The Mana Server. * * The Mana Server 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 Server 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 Server. If not, see . */ #include extern "C" { #include #include } #include "common/resourcemanager.h" #include "game-server/accountconnection.h" #include "game-server/buysell.h" #include "game-server/character.h" #include "game-server/collisiondetection.h" #include "game-server/effect.h" #include "game-server/gamehandler.h" #include "game-server/inventory.h" #include "game-server/item.h" #include "game-server/itemmanager.h" #include "game-server/mapcomposite.h" #include "game-server/mapmanager.h" #include "game-server/monster.h" #include "game-server/monstermanager.h" #include "game-server/npc.h" #include "game-server/postman.h" #include "game-server/quest.h" #include "game-server/state.h" #include "game-server/trigger.h" #include "net/messageout.h" #include "scripting/luautil.h" #include "scripting/luascript.h" #include "utils/logger.h" #include "utils/speedconv.h" #include /* * This file includes all script bindings available to LUA scripts. * When you add or change a script binding please document it on * * http://doc.manasource.org/scripting */ /** * Callback for sending a NPC_MESSAGE. * mana.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 = luaL_checklstring(s, 3, &l); if (!p || !q) { raiseScriptError(s, "npc_message called with incorrect parameters."); return 0; } MessageOut msg(GPMSG_NPC_MESSAGE); msg.writeInt16(p->getPublicID()); msg.writeString(std::string(m), l); gameHandler->sendTo(q, msg); return 1; } /** * Callback for sending a NPC_CHOICE. * mana.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.writeInt16(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. * mana.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.writeInt16(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.writeInt32(min); msg.writeInt32(max); msg.writeInt32(default_num); gameHandler->sendTo(q, msg); return 0; } /** * Callback for sending a NPC_STRING. * mana.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.writeInt16(p->getPublicID()); gameHandler->sendTo(q, msg); return 0; } /** * Callback for creating a NPC on the current map with the current script. * mana.npc_create(string name, int id, int x, int y): npc */ static int npc_create(lua_State *s) { const char *name = luaL_checkstring(s, 1); const int id = luaL_checkint(s, 2); const int x = luaL_checkint(s, 3); const int y = luaL_checkint(s, 4); lua_pushlightuserdata(s, (void *)®istryKey); lua_gettable(s, LUA_REGISTRYINDEX); Script *t = static_cast