2010-08-08 10:06:34 +02:00
|
|
|
#include <platform.h>
|
|
|
|
#include <kernel/config.h>
|
|
|
|
|
2014-08-27 21:09:39 +02:00
|
|
|
#include "spells.h"
|
2014-08-27 06:40:18 +02:00
|
|
|
|
2011-03-07 17:26:50 +01:00
|
|
|
/* kernel includes */
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <kernel/faction.h>
|
|
|
|
#include <kernel/item.h>
|
2014-06-09 18:54:48 +02:00
|
|
|
#include <kernel/messages.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <kernel/region.h>
|
|
|
|
#include <kernel/unit.h>
|
|
|
|
|
2011-03-07 17:26:50 +01:00
|
|
|
/* util includes */
|
2015-02-22 22:31:50 +01:00
|
|
|
#include <util/language.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <util/message.h>
|
2015-02-22 22:31:50 +01:00
|
|
|
#include <util/nrmessage.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2011-03-07 17:26:50 +01:00
|
|
|
/* lua includes */
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <tolua.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
2014-03-15 19:29:11 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
#define E_OK 0
|
|
|
|
#define E_INVALID_MESSAGE 1
|
|
|
|
#define E_INVALID_PARAMETER_NAME 2
|
|
|
|
#define E_INVALID_PARAMETER_TYPE 3
|
|
|
|
#define E_INVALID_PARAMETER_VALUE 4
|
|
|
|
|
|
|
|
typedef struct lua_message {
|
2015-01-30 20:37:14 +01:00
|
|
|
const message_type *mtype;
|
|
|
|
message *msg;
|
|
|
|
variant *args;
|
2010-08-08 10:06:34 +02:00
|
|
|
} lua_message;
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
int mtype_get_param(const message_type * mtype, const char *param)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int i;
|
|
|
|
for (i = 0; i != mtype->nparameters; ++i) {
|
|
|
|
if (strcmp(mtype->pnames[i], param) == 0) {
|
|
|
|
return i;
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return mtype->nparameters;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static lua_message *msg_create_message(const char *type)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_message *lmsg = malloc(sizeof(lua_message));
|
|
|
|
lmsg->msg = 0;
|
|
|
|
lmsg->args = 0;
|
|
|
|
lmsg->mtype = mt_find(type);
|
|
|
|
if (lmsg->mtype) {
|
|
|
|
lmsg->args = (variant *)calloc(lmsg->mtype->nparameters, sizeof(variant));
|
|
|
|
}
|
|
|
|
return lmsg;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
static void
|
2015-01-30 20:37:14 +01:00
|
|
|
msg_destroy_message(lua_message * msg)
|
|
|
|
{
|
|
|
|
if (msg->msg) msg_release(msg->msg);
|
|
|
|
if (msg->mtype) {
|
|
|
|
int i;
|
|
|
|
for (i=0;i!=msg->mtype->nparameters;++i) {
|
|
|
|
if (msg->mtype->types[i]->release) {
|
|
|
|
msg->mtype->types[i]->release(msg->args[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2011-03-07 08:02:35 +01:00
|
|
|
int msg_set_resource(lua_message * msg, const char *param, const char *resname)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
if (msg->mtype) {
|
|
|
|
int i = mtype_get_param(msg->mtype, param);
|
|
|
|
const resource_type * rtype;
|
|
|
|
if (i == msg->mtype->nparameters) {
|
|
|
|
return E_INVALID_PARAMETER_NAME;
|
|
|
|
}
|
|
|
|
if (strcmp(msg->mtype->types[i]->name, "resource") != 0) {
|
|
|
|
return E_INVALID_PARAMETER_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
rtype = rt_find(resname);
|
|
|
|
if (rtype) {
|
|
|
|
msg->args[i].v = (void *)rtype;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return E_INVALID_PARAMETER_VALUE;
|
|
|
|
}
|
|
|
|
return E_OK;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return E_INVALID_MESSAGE;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
int msg_set_unit(lua_message * msg, const char *param, const unit * u)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
if (msg->mtype) {
|
|
|
|
int i = mtype_get_param(msg->mtype, param);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
if (i == msg->mtype->nparameters) {
|
|
|
|
return E_INVALID_PARAMETER_NAME;
|
|
|
|
}
|
|
|
|
if (strcmp(msg->mtype->types[i]->name, "unit") != 0) {
|
|
|
|
return E_INVALID_PARAMETER_TYPE;
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
msg->args[i].v = (void *)u;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
return E_INVALID_MESSAGE;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
int msg_set_region(lua_message * msg, const char *param, const region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
if (msg->mtype) {
|
|
|
|
int i = mtype_get_param(msg->mtype, param);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
if (i == msg->mtype->nparameters) {
|
|
|
|
return E_INVALID_PARAMETER_NAME;
|
|
|
|
}
|
|
|
|
if (strcmp(msg->mtype->types[i]->name, "region") != 0) {
|
|
|
|
return E_INVALID_PARAMETER_TYPE;
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
msg->args[i].v = (void *)r;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
return E_INVALID_MESSAGE;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
int msg_set_string(lua_message * msg, const char *param, const char *value)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
if (msg->mtype) {
|
|
|
|
int i = mtype_get_param(msg->mtype, param);
|
|
|
|
variant var;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
if (i == msg->mtype->nparameters) {
|
|
|
|
return E_INVALID_PARAMETER_NAME;
|
|
|
|
}
|
|
|
|
if (strcmp(msg->mtype->types[i]->name, "string") != 0) {
|
|
|
|
return E_INVALID_PARAMETER_TYPE;
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
var.v = (void *)value;
|
|
|
|
msg->args[i] = msg->mtype->types[i]->copy(var);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
return E_INVALID_MESSAGE;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
int msg_set_int(lua_message * msg, const char *param, int value)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
if (msg->mtype) {
|
|
|
|
int i = mtype_get_param(msg->mtype, param);
|
|
|
|
if (i == msg->mtype->nparameters) {
|
|
|
|
return E_INVALID_PARAMETER_NAME;
|
|
|
|
}
|
|
|
|
if (strcmp(msg->mtype->types[i]->name, "int") != 0) {
|
|
|
|
return E_INVALID_PARAMETER_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->args[i].i = value;
|
|
|
|
|
|
|
|
return E_OK;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return E_INVALID_MESSAGE;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
int msg_send_faction(lua_message * msg, faction * f)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
assert(f);
|
|
|
|
assert(msg);
|
|
|
|
|
|
|
|
if (msg->mtype) {
|
|
|
|
if (msg->msg == NULL) {
|
|
|
|
msg->msg = msg_create(msg->mtype, msg->args);
|
|
|
|
}
|
|
|
|
add_message(&f->msgs, msg->msg);
|
|
|
|
return E_OK;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return E_INVALID_MESSAGE;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
int msg_send_region(lua_message * lmsg, region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
if (lmsg->mtype) {
|
|
|
|
if (lmsg->msg == NULL) {
|
|
|
|
lmsg->msg = msg_create(lmsg->mtype, lmsg->args);
|
|
|
|
}
|
|
|
|
add_message(&r->msgs, lmsg->msg);
|
|
|
|
return E_OK;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return E_INVALID_MESSAGE;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_msg_create(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
const char *type = tolua_tostring(L, 1, 0);
|
|
|
|
lua_message *lmsg = msg_create_message(type);
|
|
|
|
tolua_pushusertype(L, (void *)lmsg, TOLUA_CAST "message");
|
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
|
|
|
static int tolua_msg_set_string(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_message *lmsg = (lua_message *)tolua_tousertype(L, 1, 0);
|
|
|
|
const char *param = tolua_tostring(L, 2, 0);
|
|
|
|
const char *value = tolua_tostring(L, 3, 0);
|
|
|
|
int result = msg_set_string(lmsg, param, value);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, result);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_msg_set_int(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_message *lmsg = (lua_message *)tolua_tousertype(L, 1, 0);
|
|
|
|
const char *param = tolua_tostring(L, 2, 0);
|
|
|
|
int value = (int)tolua_tonumber(L, 3, 0);
|
|
|
|
int result = msg_set_int(lmsg, param, value);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, result);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_msg_set_resource(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_message *lmsg = (lua_message *)tolua_tousertype(L, 1, 0);
|
|
|
|
const char *param = tolua_tostring(L, 2, 0);
|
|
|
|
const char *value = tolua_tostring(L, 3, 0);
|
|
|
|
int result = msg_set_resource(lmsg, param, value);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, result);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_msg_set_unit(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_message *lmsg = (lua_message *)tolua_tousertype(L, 1, 0);
|
|
|
|
const char *param = tolua_tostring(L, 2, 0);
|
|
|
|
unit *value = (unit *)tolua_tousertype(L, 3, 0);
|
|
|
|
int result = msg_set_unit(lmsg, param, value);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, result);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_msg_set_region(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_message *lmsg = (lua_message *)tolua_tousertype(L, 1, 0);
|
|
|
|
const char *param = tolua_tostring(L, 2, 0);
|
|
|
|
region *value = (region *)tolua_tousertype(L, 3, 0);
|
|
|
|
int result = msg_set_region(lmsg, param, value);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, result);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_msg_set(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_Error err;
|
|
|
|
if (tolua_isnumber(L, 3, 0, &err)) {
|
|
|
|
return tolua_msg_set_int(L);
|
|
|
|
}
|
|
|
|
else if (tolua_isusertype(L, 3, TOLUA_CAST "region", 0, &err)) {
|
|
|
|
return tolua_msg_set_region(L);
|
|
|
|
}
|
|
|
|
else if (tolua_isusertype(L, 3, TOLUA_CAST "unit", 0, &err)) {
|
|
|
|
return tolua_msg_set_unit(L);
|
|
|
|
}
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, -1);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_msg_send_region(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_message *lmsg = (lua_message *)tolua_tousertype(L, 1, 0);
|
|
|
|
region *r = (region *)tolua_tousertype(L, 2, 0);
|
|
|
|
int result = msg_send_region(lmsg, r);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, result);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_msg_report_action(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_message *lmsg = (lua_message *)tolua_tousertype(L, 1, 0);
|
|
|
|
region *r = (region *)tolua_tousertype(L, 2, 0);
|
|
|
|
unit *u = (unit *)tolua_tousertype(L, 3, 0);
|
|
|
|
int result, flags = (int)tolua_tonumber(L, 4, 0);
|
|
|
|
if (lmsg->msg == NULL) {
|
|
|
|
lmsg->msg = msg_create(lmsg->mtype, lmsg->args);
|
|
|
|
}
|
|
|
|
result = report_action(r, u, lmsg->msg, flags);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, result);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_msg_send_faction(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_message *lmsg = (lua_message *)tolua_tousertype(L, 1, 0);
|
|
|
|
faction *f = (faction *)tolua_tousertype(L, 2, 0);
|
|
|
|
if (f && lmsg) {
|
|
|
|
int result = msg_send_faction(lmsg, f);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, result);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2015-02-22 22:31:50 +01:00
|
|
|
static int tolua_msg_render(lua_State * L)
|
|
|
|
{
|
|
|
|
lua_message *lmsg = (lua_message *)tolua_tousertype(L, 1, 0);
|
|
|
|
const char * lname = tolua_tostring(L, 2, 0);
|
|
|
|
const struct locale * lang = lname ? get_locale(lname) : default_locale;
|
|
|
|
char name[64];
|
|
|
|
|
|
|
|
if (lmsg->msg == NULL) {
|
|
|
|
lmsg->msg = msg_create(lmsg->mtype, lmsg->args);
|
|
|
|
}
|
|
|
|
nr_render(lmsg->msg, lang, name, sizeof(name), NULL);
|
|
|
|
lua_pushstring(L, name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void tolua_message_open(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
/* register user types */
|
|
|
|
tolua_usertype(L, TOLUA_CAST "message");
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_module(L, NULL, 0);
|
|
|
|
tolua_beginmodule(L, NULL);
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_function(L, TOLUA_CAST "message", tolua_msg_create);
|
|
|
|
|
|
|
|
tolua_cclass(L, TOLUA_CAST "message", TOLUA_CAST "message", TOLUA_CAST "",
|
|
|
|
NULL);
|
|
|
|
tolua_beginmodule(L, TOLUA_CAST "message");
|
|
|
|
{
|
2015-02-22 22:31:50 +01:00
|
|
|
tolua_function(L, TOLUA_CAST "render", tolua_msg_render);
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_function(L, TOLUA_CAST "set", tolua_msg_set);
|
|
|
|
tolua_function(L, TOLUA_CAST "set_unit", tolua_msg_set_unit);
|
|
|
|
tolua_function(L, TOLUA_CAST "set_region", tolua_msg_set_region);
|
|
|
|
tolua_function(L, TOLUA_CAST "set_resource", tolua_msg_set_resource);
|
|
|
|
tolua_function(L, TOLUA_CAST "set_int", tolua_msg_set_int);
|
|
|
|
tolua_function(L, TOLUA_CAST "set_string", tolua_msg_set_string);
|
|
|
|
tolua_function(L, TOLUA_CAST "send_faction", tolua_msg_send_faction);
|
|
|
|
tolua_function(L, TOLUA_CAST "send_region", tolua_msg_send_region);
|
|
|
|
tolua_function(L, TOLUA_CAST "report_action", tolua_msg_report_action);
|
|
|
|
|
|
|
|
tolua_function(L, TOLUA_CAST "create", tolua_msg_create);
|
|
|
|
}
|
|
|
|
tolua_endmodule(L);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
tolua_endmodule(L);
|
|
|
|
}
|