/*
* The Mana Server
* Copyright (C) 2007-2010 The Mana World Development Team
*
* 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.hpp"
#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"
#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.writeShort(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.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.
* 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.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.
* 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.writeShort(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