2015-11-04 11:23:57 +01:00
|
|
|
/*
|
2011-03-07 08:35:23 +01:00
|
|
|
+-------------------+
|
|
|
|
| | Enno Rehling <enno@eressea.de>
|
|
|
|
| Eressea PBEM host | Christian Schlittchen <corwin@amber.kn-bremen.de>
|
|
|
|
| (c) 1998 - 2008 | Katja Zedel <katze@felidae.kn-bremen.de>
|
|
|
|
| | Henning Peters <faroul@beyond.kn-bremen.de>
|
|
|
|
+-------------------+
|
|
|
|
|
|
|
|
This program may not be used, modified or distributed
|
|
|
|
without prior permission by the authors of Eressea.
|
2011-03-08 08:44:20 +01:00
|
|
|
*/
|
|
|
|
|
2011-03-07 08:35:23 +01:00
|
|
|
#include "bindings.h"
|
|
|
|
#include "bind_unit.h"
|
2012-06-07 04:29:36 +02:00
|
|
|
#include "bind_storage.h"
|
|
|
|
#include "bind_building.h"
|
|
|
|
#include "bind_message.h"
|
|
|
|
#include "bind_building.h"
|
2011-03-07 08:35:23 +01:00
|
|
|
#include "bind_faction.h"
|
2015-12-30 21:20:59 +01:00
|
|
|
#include "bind_order.h"
|
2012-06-07 04:29:36 +02:00
|
|
|
#include "bind_ship.h"
|
|
|
|
#include "bind_gmtool.h"
|
2011-03-07 08:35:23 +01:00
|
|
|
#include "bind_region.h"
|
|
|
|
#include "helpers.h"
|
2014-06-12 06:25:24 +02:00
|
|
|
#include "console.h"
|
2014-08-27 06:40:18 +02:00
|
|
|
#include "reports.h"
|
2016-03-08 15:13:49 +01:00
|
|
|
#include "study.h"
|
2015-09-12 12:54:49 +02:00
|
|
|
#include "calendar.h"
|
2011-03-08 08:44:20 +01:00
|
|
|
|
2011-03-07 08:35:23 +01:00
|
|
|
#include <kernel/config.h>
|
2011-03-08 08:44:20 +01:00
|
|
|
|
2011-03-07 08:35:23 +01:00
|
|
|
#include <kernel/alliance.h>
|
2011-03-09 00:25:51 +01:00
|
|
|
#include <kernel/building.h>
|
2012-06-04 03:41:07 +02:00
|
|
|
#include <kernel/curse.h>
|
2011-03-07 08:35:23 +01:00
|
|
|
#include <kernel/equipment.h>
|
|
|
|
#include <kernel/unit.h>
|
|
|
|
#include <kernel/terrain.h>
|
2014-06-09 18:54:48 +02:00
|
|
|
#include <kernel/messages.h>
|
2011-03-07 08:35:23 +01:00
|
|
|
#include <kernel/region.h>
|
|
|
|
#include <kernel/building.h>
|
|
|
|
#include <kernel/plane.h>
|
|
|
|
#include <kernel/race.h>
|
|
|
|
#include <kernel/item.h>
|
|
|
|
#include <kernel/order.h>
|
|
|
|
#include <kernel/ship.h>
|
|
|
|
#include <kernel/faction.h>
|
|
|
|
#include <kernel/save.h>
|
2012-05-24 05:22:12 +02:00
|
|
|
#include <kernel/spell.h>
|
2015-05-05 17:44:58 +02:00
|
|
|
#include <kernel/spellbook.h>
|
2011-03-08 08:44:20 +01:00
|
|
|
|
2014-02-18 05:45:00 +01:00
|
|
|
#include "economy.h"
|
|
|
|
#include "summary.h"
|
2016-01-28 12:25:16 +01:00
|
|
|
#include "teleport.h"
|
2014-02-18 05:45:00 +01:00
|
|
|
#include "laws.h"
|
2016-12-23 18:05:38 +01:00
|
|
|
#include "monsters.h"
|
2014-02-18 05:45:00 +01:00
|
|
|
#include "market.h"
|
2011-03-08 08:44:20 +01:00
|
|
|
|
2011-03-07 08:35:23 +01:00
|
|
|
#include <modules/autoseed.h>
|
|
|
|
#include <modules/score.h>
|
|
|
|
#include <attributes/key.h>
|
2011-03-08 08:44:20 +01:00
|
|
|
|
2011-03-07 08:35:23 +01:00
|
|
|
#include <util/attrib.h>
|
|
|
|
#include <util/base36.h>
|
|
|
|
#include <util/language.h>
|
|
|
|
#include <util/lists.h>
|
|
|
|
#include <util/log.h>
|
2017-12-28 18:55:45 +01:00
|
|
|
#include <util/macros.h>
|
2011-03-07 08:35:23 +01:00
|
|
|
#include <util/rand.h>
|
|
|
|
#include <util/rng.h>
|
2014-10-29 21:08:16 +01:00
|
|
|
#include <util/xml.h>
|
2013-12-31 10:06:28 +01:00
|
|
|
|
2017-12-28 18:55:45 +01:00
|
|
|
#include <selist.h>
|
2013-12-31 10:06:28 +01:00
|
|
|
#include <storage.h>
|
2011-03-08 08:44:20 +01:00
|
|
|
|
2012-05-31 04:17:08 +02:00
|
|
|
#include <iniparser.h>
|
2011-03-07 08:35:23 +01:00
|
|
|
#include <tolua.h>
|
|
|
|
#include <lua.h>
|
2012-06-07 04:29:36 +02:00
|
|
|
#include <lualib.h>
|
|
|
|
#include <lauxlib.h>
|
2011-03-08 08:44:20 +01:00
|
|
|
|
2011-03-07 08:35:23 +01:00
|
|
|
#include <time.h>
|
2017-01-22 18:03:46 +01:00
|
|
|
#include <errno.h>
|
2011-03-07 08:35:23 +01:00
|
|
|
#include <assert.h>
|
2011-03-07 08:40:55 +01:00
|
|
|
|
2017-01-22 18:28:04 +01:00
|
|
|
#define TOLUA_PKG(NAME) void tolua_##NAME##_open(lua_State * L)
|
2012-06-07 04:29:36 +02:00
|
|
|
|
|
|
|
TOLUA_PKG(eressea);
|
|
|
|
TOLUA_PKG(process);
|
|
|
|
TOLUA_PKG(settings);
|
2014-06-13 02:34:57 +02:00
|
|
|
TOLUA_PKG(config);
|
2014-06-14 16:52:32 +02:00
|
|
|
TOLUA_PKG(locale);
|
2014-06-13 22:02:03 +02:00
|
|
|
TOLUA_PKG(log);
|
2014-06-18 08:50:13 +02:00
|
|
|
TOLUA_PKG(game);
|
2012-06-07 04:29:36 +02:00
|
|
|
|
2017-04-30 18:18:47 +02:00
|
|
|
int tolua_toid(lua_State * L, int idx, int def)
|
|
|
|
{
|
|
|
|
int no = 0;
|
|
|
|
int type = lua_type(L, idx);
|
|
|
|
if (type == LUA_TNUMBER) {
|
|
|
|
no = (int)tolua_tonumber(L, idx, def);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const char *str = tolua_tostring(L, idx, NULL);
|
|
|
|
no = str ? atoi36(str) : def;
|
|
|
|
}
|
|
|
|
return no;
|
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
int log_lua_error(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
const char *error = lua_tostring(L, -1);
|
2016-02-09 21:34:24 +01:00
|
|
|
log_fatal("Lua call failed.\n%s\n", error);
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_pop(L, 1);
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2017-01-26 17:41:21 +01:00
|
|
|
static int tolua_selist_iter(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2017-01-26 17:41:21 +01:00
|
|
|
selist **qlp = (selist **)lua_touserdata(L, lua_upvalueindex(1));
|
|
|
|
selist *ql = *qlp;
|
2015-01-30 20:37:14 +01:00
|
|
|
if (ql != NULL) {
|
2015-05-15 20:35:36 +02:00
|
|
|
int index = (int)lua_tointeger(L, lua_upvalueindex(2));
|
2015-01-30 20:37:14 +01:00
|
|
|
const char *type = lua_tostring(L, lua_upvalueindex(3));
|
2017-01-26 17:41:21 +01:00
|
|
|
void *data = selist_get(ql, index);
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_pushusertype(L, data, TOLUA_CAST type);
|
2017-01-26 17:41:21 +01:00
|
|
|
selist_advance(qlp, &index, 1);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, index);
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_replace(L, lua_upvalueindex(2));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
2011-02-25 16:23:02 +01:00
|
|
|
|
2017-01-26 17:41:21 +01:00
|
|
|
int tolua_selist_push(struct lua_State *L, const char *list_type,
|
|
|
|
const char *elem_type, struct selist *list)
|
2015-01-30 20:37:14 +01:00
|
|
|
{
|
|
|
|
if (list) {
|
2017-01-26 17:41:21 +01:00
|
|
|
selist **qlist_ptr =
|
|
|
|
(selist **)lua_newuserdata(L, sizeof(selist *));
|
2015-01-30 20:37:14 +01:00
|
|
|
*qlist_ptr = list;
|
|
|
|
luaL_getmetatable(L, list_type);
|
|
|
|
lua_setmetatable(L, -2);
|
2015-06-08 21:19:17 +02:00
|
|
|
lua_pushinteger(L, 0);
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_pushstring(L, elem_type);
|
2017-01-26 17:41:21 +01:00
|
|
|
lua_pushcclosure(L, tolua_selist_iter, 3); /* OBS: this closure has multiple upvalues (list, index, type_name) */
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
lua_pushnil(L);
|
|
|
|
}
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
int tolua_itemlist_next(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
item **item_ptr = (item **)lua_touserdata(L, lua_upvalueindex(1));
|
|
|
|
item *itm = *item_ptr;
|
|
|
|
if (itm != NULL) {
|
|
|
|
tolua_pushstring(L, itm->type->rtype->_name);
|
|
|
|
*item_ptr = itm->next;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_autoseed(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
const char *filename = tolua_tostring(L, 1, 0);
|
|
|
|
int new_island = tolua_toboolean(L, 2, 0);
|
|
|
|
newfaction *players = read_newfactions(filename);
|
|
|
|
if (players != NULL) {
|
|
|
|
while (players) {
|
|
|
|
int n = listlen(players);
|
|
|
|
int k = (n + ISLANDSIZE - 1) / ISLANDSIZE;
|
|
|
|
k = n / k;
|
|
|
|
n = autoseed(&players, k, new_island ? 0 : TURNS_PER_ISLAND);
|
|
|
|
if (n == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_getkey(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
const char *name = tolua_tostring(L, 1, 0);
|
|
|
|
int flag = atoi36(name);
|
2016-02-09 06:43:19 +01:00
|
|
|
|
|
|
|
lua_pushboolean(L, key_get(global.attribs, flag));
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_translate(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
const char *str = tolua_tostring(L, 1, 0);
|
|
|
|
const char *lang = tolua_tostring(L, 2, 0);
|
|
|
|
struct locale *loc = lang ? get_locale(lang) : default_locale;
|
|
|
|
if (loc) {
|
|
|
|
str = LOC(loc, str);
|
|
|
|
tolua_pushstring(L, str);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_setkey(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
const char *name = tolua_tostring(L, 1, 0);
|
2017-02-13 18:48:38 +01:00
|
|
|
int value = (int)tolua_tonumber(L, 3, 0);
|
2015-01-30 20:37:14 +01:00
|
|
|
int flag = atoi36(name);
|
2016-02-09 06:43:19 +01:00
|
|
|
if (value) {
|
2017-02-13 18:48:38 +01:00
|
|
|
key_set(&global.attribs, flag, value);
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
2016-02-09 06:43:19 +01:00
|
|
|
else {
|
|
|
|
key_unset(&global.attribs, flag);
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2017-05-04 06:44:33 +02:00
|
|
|
static int tolua_random(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-06-08 21:19:17 +02:00
|
|
|
lua_pushinteger(L, rng_int());
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_message_unit(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
unit *sender = (unit *)tolua_tousertype(L, 1, 0);
|
|
|
|
unit *target = (unit *)tolua_tousertype(L, 2, 0);
|
|
|
|
const char *str = tolua_tostring(L, 3, 0);
|
2015-11-04 11:23:57 +01:00
|
|
|
if (!target) {
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_error(L, TOLUA_CAST "target is nil", NULL);
|
2015-11-04 11:23:57 +01:00
|
|
|
}
|
|
|
|
else if (!sender) {
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_error(L, TOLUA_CAST "sender is nil", NULL);
|
2015-11-04 11:23:57 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
deliverMail(target->faction, sender->region, sender, str, target);
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_message_faction(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
unit *sender = (unit *)tolua_tousertype(L, 1, 0);
|
|
|
|
faction *target = (faction *)tolua_tousertype(L, 2, 0);
|
|
|
|
const char *str = tolua_tostring(L, 3, 0);
|
2015-11-04 11:23:57 +01:00
|
|
|
if (!target) {
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_error(L, TOLUA_CAST "target is nil", NULL);
|
2015-11-04 11:23:57 +01:00
|
|
|
}
|
|
|
|
else if (!sender) {
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_error(L, TOLUA_CAST "sender is nil", NULL);
|
2015-11-04 11:23:57 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
deliverMail(target, sender->region, sender, str, NULL);
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_message_region(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
unit *sender = (unit *)tolua_tousertype(L, 1, 0);
|
|
|
|
const char *str = tolua_tostring(L, 2, 0);
|
2015-11-04 11:23:57 +01:00
|
|
|
if (!sender) {
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_error(L, TOLUA_CAST "sender is nil", NULL);
|
2015-11-04 11:23:57 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADDMSG(&sender->region->msgs, msg_message("mail_result", "unit message",
|
|
|
|
sender, str));
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_update_guards(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2017-12-28 18:55:45 +01:00
|
|
|
UNUSED_ARG(L);
|
2015-01-30 20:37:14 +01:00
|
|
|
update_guards();
|
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_set_turn(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
turn = (int)tolua_tonumber(L, 1, 0);
|
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_get_turn(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, turn);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_atoi36(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
const char *s = tolua_tostring(L, 1, 0);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, atoi36(s));
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_itoa36(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int i = (int)tolua_tonumber(L, 1, 0);
|
|
|
|
tolua_pushstring(L, itoa36(i));
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_dice_rand(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
const char *s = tolua_tostring(L, 1, 0);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, dice_rand(s));
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_get_season(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int turnno = (int)tolua_tonumber(L, 1, 0);
|
|
|
|
gamedate gd;
|
|
|
|
get_gamedate(turnno, &gd);
|
|
|
|
tolua_pushstring(L, seasonnames[gd.season]);
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_create_curse(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
unit *u = (unit *)tolua_tousertype(L, 1, 0);
|
|
|
|
tolua_Error tolua_err;
|
|
|
|
attrib **ap = NULL;
|
|
|
|
if (tolua_isusertype(L, 2, TOLUA_CAST "unit", 0, &tolua_err)) {
|
|
|
|
unit *target = (unit *)tolua_tousertype(L, 2, 0);
|
|
|
|
if (target)
|
|
|
|
ap = &target->attribs;
|
|
|
|
}
|
|
|
|
else if (tolua_isusertype(L, 2, TOLUA_CAST "region", 0, &tolua_err)) {
|
|
|
|
region *target = (region *)tolua_tousertype(L, 2, 0);
|
|
|
|
if (target)
|
|
|
|
ap = &target->attribs;
|
|
|
|
}
|
|
|
|
else if (tolua_isusertype(L, 2, TOLUA_CAST "ship", 0, &tolua_err)) {
|
|
|
|
ship *target = (ship *)tolua_tousertype(L, 2, 0);
|
|
|
|
if (target)
|
|
|
|
ap = &target->attribs;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else if (tolua_isusertype(L, 2, TOLUA_CAST "building", 0, &tolua_err)) {
|
|
|
|
building *target = (building *)tolua_tousertype(L, 2, 0);
|
|
|
|
if (target)
|
|
|
|
ap = &target->attribs;
|
|
|
|
}
|
|
|
|
if (ap) {
|
|
|
|
const char *cname = tolua_tostring(L, 3, 0);
|
|
|
|
const curse_type *ctype = ct_find(cname);
|
|
|
|
if (ctype) {
|
|
|
|
float vigour = (float)tolua_tonumber(L, 4, 0);
|
|
|
|
int duration = (int)tolua_tonumber(L, 5, 0);
|
|
|
|
float effect = (float)tolua_tonumber(L, 6, 0);
|
|
|
|
int men = (int)tolua_tonumber(L, 7, 0); /* optional */
|
|
|
|
curse *c = create_curse(u, ap, ctype, vigour, duration, effect, men);
|
|
|
|
if (c) {
|
|
|
|
tolua_pushboolean(L, true);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tolua_pushboolean(L, false);
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_learn_skill(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
unit *u = (unit *)tolua_tousertype(L, 1, 0);
|
|
|
|
const char *skname = tolua_tostring(L, 2, 0);
|
2016-03-17 21:21:23 +01:00
|
|
|
int days = (int)tolua_tonumber(L, 3, 0);
|
2015-01-30 20:37:14 +01:00
|
|
|
skill_t sk = findskill(skname);
|
|
|
|
if (sk != NOSKILL) {
|
2016-03-17 21:21:23 +01:00
|
|
|
learn_skill(u, sk, days);
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_update_scores(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2017-12-28 18:55:45 +01:00
|
|
|
UNUSED_ARG(L);
|
2015-01-30 20:37:14 +01:00
|
|
|
score();
|
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_update_owners(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
region *r;
|
2017-12-28 18:55:45 +01:00
|
|
|
UNUSED_ARG(L);
|
2015-01-30 20:37:14 +01:00
|
|
|
for (r = regions; r; r = r->next) {
|
|
|
|
update_owners(r);
|
|
|
|
}
|
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_update_subscriptions(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2017-12-28 18:55:45 +01:00
|
|
|
UNUSED_ARG(L);
|
2015-01-30 20:37:14 +01:00
|
|
|
update_subscriptions();
|
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_remove_empty_units(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2017-12-28 18:55:45 +01:00
|
|
|
UNUSED_ARG(L);
|
2015-01-30 20:37:14 +01:00
|
|
|
remove_empty_units();
|
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_get_nmrs(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int result = -1;
|
|
|
|
int n = (int)tolua_tonumber(L, 1, 0);
|
|
|
|
if (n >= 0 && n <= NMRTimeout()) {
|
|
|
|
if (nmrs == NULL) {
|
|
|
|
update_nmrs();
|
|
|
|
}
|
|
|
|
result = nmrs[n];
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, result);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_equipunit(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
unit *u = (unit *)tolua_tousertype(L, 1, 0);
|
|
|
|
const char *eqname = tolua_tostring(L, 2, 0);
|
2015-07-05 16:40:49 +02:00
|
|
|
int mask = (int)tolua_tonumber(L, 3, EQUIP_ALL);
|
2016-05-15 23:21:39 +02:00
|
|
|
assert(u && mask > 0);
|
2015-07-05 16:40:49 +02:00
|
|
|
equip_unit_mask(u, get_equipment(eqname), mask);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_equipment_setitem(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int result = -1;
|
|
|
|
const char *eqname = tolua_tostring(L, 1, 0);
|
|
|
|
const char *iname = tolua_tostring(L, 2, 0);
|
|
|
|
const char *value = tolua_tostring(L, 3, 0);
|
|
|
|
if (iname != NULL) {
|
|
|
|
const struct item_type *itype = it_find(iname);
|
|
|
|
if (itype != NULL) {
|
2017-01-22 18:28:04 +01:00
|
|
|
equipment_setitem(get_or_create_equipment(eqname), itype, value);
|
2015-01-30 20:37:14 +01:00
|
|
|
result = 0;
|
|
|
|
}
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, result);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_spawn_braineaters(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
float chance = (float)tolua_tonumber(L, 1, 0);
|
|
|
|
spawn_braineaters(chance);
|
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_init_reports(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int result = init_reports();
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, result);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_write_report(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
faction *f = (faction *)tolua_tousertype(L, 1, 0);
|
|
|
|
if (f) {
|
|
|
|
time_t ltime = time(0);
|
|
|
|
int result = write_reports(f, ltime);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, result);
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
tolua_pushstring(L, "function expects a faction, got nil");
|
|
|
|
}
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_write_reports(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int result;
|
|
|
|
init_reports();
|
|
|
|
result = reports();
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, result);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2017-02-27 19:35:14 +01:00
|
|
|
static int tolua_process_orders(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2017-12-28 18:55:45 +01:00
|
|
|
UNUSED_ARG(L);
|
2017-02-27 19:35:14 +01:00
|
|
|
processorders();
|
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2017-02-27 19:35:14 +01:00
|
|
|
static int tolua_turn_begin(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2017-12-28 18:55:45 +01:00
|
|
|
UNUSED_ARG(L);
|
2017-02-27 19:35:14 +01:00
|
|
|
turn_begin();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tolua_turn_process(lua_State * L)
|
|
|
|
{
|
2017-12-28 18:55:45 +01:00
|
|
|
UNUSED_ARG(L);
|
2017-02-27 19:35:14 +01:00
|
|
|
turn_process();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tolua_turn_end(lua_State * L)
|
|
|
|
{
|
2017-12-28 18:55:45 +01:00
|
|
|
UNUSED_ARG(L);
|
2017-02-27 19:35:14 +01:00
|
|
|
turn_end();
|
2015-01-30 20:37:14 +01:00
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_write_passwords(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int result = writepasswd();
|
2015-06-08 21:19:17 +02:00
|
|
|
lua_pushinteger(L, result);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct summary *sum_begin = 0;
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_init_summary(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2017-12-28 18:55:45 +01:00
|
|
|
UNUSED_ARG(L);
|
2015-01-30 20:37:14 +01:00
|
|
|
sum_begin = make_summary();
|
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_write_summary(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2017-12-28 18:55:45 +01:00
|
|
|
UNUSED_ARG(L);
|
2015-01-30 20:37:14 +01:00
|
|
|
if (sum_begin) {
|
|
|
|
struct summary *sum_end = make_summary();
|
|
|
|
report_summary(sum_end, sum_begin, false);
|
|
|
|
report_summary(sum_end, sum_begin, true);
|
2016-09-09 17:20:09 +02:00
|
|
|
free_summary(sum_end);
|
|
|
|
free_summary(sum_begin);
|
|
|
|
sum_begin = 0;
|
2015-01-30 20:37:14 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2011-03-07 08:35:23 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2016-09-13 21:09:05 +02:00
|
|
|
/*
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_write_map(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
const char *filename = tolua_tostring(L, 1, 0);
|
|
|
|
if (filename) {
|
|
|
|
crwritemap(filename);
|
|
|
|
}
|
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
2016-09-13 21:09:05 +02:00
|
|
|
*/
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_read_turn(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int cturn = current_turn();
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, cturn);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_get_region(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int x = (int)tolua_tonumber(L, 1, 0);
|
|
|
|
int y = (int)tolua_tonumber(L, 2, 0);
|
|
|
|
region *r;
|
|
|
|
assert(!pnormalize(&x, &y, findplane(x, y)));
|
|
|
|
r = findregion(x, y);
|
|
|
|
tolua_pushusertype(L, r, TOLUA_CAST "region");
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_get_region_byid(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int uid = (int)tolua_tonumber(L, 1, 0);
|
|
|
|
region *r = findregionbyid(uid);
|
|
|
|
tolua_pushusertype(L, r, TOLUA_CAST "region");
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_get_building(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int no = tolua_toid(L, 1, 0);
|
|
|
|
building *b = findbuilding(no);
|
|
|
|
tolua_pushusertype(L, b, TOLUA_CAST "building");
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_get_ship(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int no = tolua_toid(L, 1, 0);
|
|
|
|
ship *sh = findship(no);
|
|
|
|
tolua_pushusertype(L, sh, TOLUA_CAST "ship");
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_get_alliance(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int no = tolua_toid(L, 1, 0);
|
|
|
|
alliance *f = findalliance(no);
|
|
|
|
tolua_pushusertype(L, f, TOLUA_CAST "alliance");
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_get_unit(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int no = tolua_toid(L, 1, 0);
|
|
|
|
unit *u = findunit(no);
|
|
|
|
tolua_pushusertype(L, u, TOLUA_CAST "unit");
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_alliance_create(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int id = (int)tolua_tonumber(L, 1, 0);
|
|
|
|
const char *name = tolua_tostring(L, 2, 0);
|
|
|
|
alliance *alli = makealliance(id, name);
|
|
|
|
tolua_pushusertype(L, alli, TOLUA_CAST "alliance");
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_get_regions(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
region **region_ptr = (region **)lua_newuserdata(L, sizeof(region *));
|
|
|
|
luaL_getmetatable(L, "region");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
*region_ptr = regions;
|
|
|
|
lua_pushcclosure(L, tolua_regionlist_next, 1);
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_get_factions(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
faction **faction_ptr = (faction **)lua_newuserdata(L, sizeof(faction *));
|
|
|
|
luaL_getmetatable(L, "faction");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
*faction_ptr = factions;
|
|
|
|
lua_pushcclosure(L, tolua_factionlist_next, 1);
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_get_alliance_factions(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
alliance *self = (alliance *)tolua_tousertype(L, 1, 0);
|
2017-01-26 17:41:21 +01:00
|
|
|
return tolua_selist_push(L, "faction_list", "faction", self->members);
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_get_alliance_id(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
alliance *self = (alliance *)tolua_tousertype(L, 1, 0);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, self->id);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_get_alliance_name(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
alliance *self = (alliance *)tolua_tousertype(L, 1, 0);
|
|
|
|
tolua_pushstring(L, self->name);
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_set_alliance_name(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
alliance *self = (alliance *)tolua_tousertype(L, 1, 0);
|
|
|
|
alliance_setname(self, tolua_tostring(L, 2, 0));
|
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int config_get_ships(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2017-01-26 17:41:21 +01:00
|
|
|
selist *ql;
|
2015-01-30 20:37:14 +01:00
|
|
|
int qi, i = 0;
|
2017-01-26 17:41:21 +01:00
|
|
|
lua_createtable(L, selist_length(shiptypes), 0);
|
|
|
|
for (qi = 0, ql = shiptypes; ql; selist_advance(&ql, &qi, 1)) {
|
|
|
|
ship_type *stype = (ship_type *)selist_get(ql, qi);
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_pushstring(L, TOLUA_CAST stype->_name);
|
|
|
|
lua_rawseti(L, -2, ++i);
|
|
|
|
}
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-09 00:25:51 +01:00
|
|
|
static int config_get_buildings(lua_State * L)
|
|
|
|
{
|
2017-01-26 17:41:21 +01:00
|
|
|
selist *ql;
|
2015-01-30 20:37:14 +01:00
|
|
|
int qi, i = 0;
|
2017-01-26 17:41:21 +01:00
|
|
|
lua_createtable(L, selist_length(buildingtypes), 0);
|
|
|
|
for (qi = 0, ql = buildingtypes; ql; selist_advance(&ql, &qi, 1)) {
|
|
|
|
building_type *btype = (building_type *)selist_get(ql, qi);
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_pushstring(L, TOLUA_CAST btype->_name);
|
|
|
|
lua_rawseti(L, -2, ++i);
|
|
|
|
}
|
|
|
|
return 1;
|
2011-03-09 00:25:51 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int config_get_locales(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2014-06-14 16:52:32 +02:00
|
|
|
const struct locale *lang;
|
|
|
|
int i = 0, n = 0;
|
|
|
|
for (lang = locales; lang; lang = nextlocale(lang)) ++n;
|
|
|
|
lua_createtable(L, n, 0);
|
|
|
|
for (lang = locales; lang; lang = nextlocale(lang)) {
|
|
|
|
tolua_pushstring(L, TOLUA_CAST locale_name(lang));
|
|
|
|
lua_rawseti(L, -2, ++i);
|
|
|
|
}
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int config_get_resource(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
const char *name = tolua_tostring(L, 1, 0);
|
|
|
|
if (name) {
|
|
|
|
const struct item_type *itype = it_find(name);
|
|
|
|
if (itype) {
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_pushstring(L, "weight");
|
|
|
|
lua_pushinteger(L, itype->weight);
|
2011-03-07 08:35:23 +01:00
|
|
|
lua_settable(L, -3);
|
2015-01-30 20:37:14 +01:00
|
|
|
if (itype->capacity > 0) {
|
|
|
|
lua_pushstring(L, "capacity");
|
|
|
|
lua_pushinteger(L, itype->capacity);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
|
|
|
if (itype->construction) {
|
|
|
|
lua_pushstring(L, "build_skill_min");
|
|
|
|
lua_pushinteger(L, itype->construction->minskill);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
lua_pushstring(L, "build_skill_name");
|
|
|
|
lua_pushstring(L, skillnames[itype->construction->skill]);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
if (itype->construction->materials) {
|
2017-12-08 21:08:11 +01:00
|
|
|
int i;
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_pushstring(L, "materials");
|
|
|
|
lua_newtable(L);
|
|
|
|
for (i = 0; itype->construction->materials[i].number; ++i) {
|
|
|
|
lua_pushstring(L,
|
|
|
|
itype->construction->materials[i].rtype->_name);
|
|
|
|
lua_pushinteger(L, itype->construction->materials[i].number);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return 0;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-09 00:25:51 +01:00
|
|
|
static int config_get_btype(lua_State * L)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
const char *name = tolua_tostring(L, 1, 0);
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
const struct building_type *btype = bt_find(name);
|
|
|
|
if (btype) {
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_pushstring(L, "flags");
|
|
|
|
lua_pushinteger(L, btype->flags);
|
2011-03-09 00:25:51 +01:00
|
|
|
lua_settable(L, -3);
|
2015-01-30 20:37:14 +01:00
|
|
|
if (btype->capacity > 0) {
|
|
|
|
lua_pushstring(L, "capacity");
|
|
|
|
lua_pushinteger(L, btype->capacity);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
|
|
|
if (btype->maxcapacity > 0) {
|
|
|
|
lua_pushstring(L, "maxcapacity");
|
|
|
|
lua_pushinteger(L, btype->maxcapacity);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
|
|
|
if (btype->maxsize > 0) {
|
|
|
|
lua_pushstring(L, "maxsize");
|
|
|
|
lua_pushinteger(L, btype->maxsize);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
|
|
|
if (btype->maintenance) {
|
|
|
|
int i;
|
|
|
|
lua_pushstring(L, "maintenance");
|
|
|
|
lua_newtable(L);
|
|
|
|
for (i = 0; btype->maintenance[i].number; ++i) {
|
|
|
|
lua_pushstring(L,
|
|
|
|
btype->maintenance[i].rtype->_name);
|
|
|
|
lua_pushinteger(L, btype->maintenance[i].number);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
|
|
|
if (btype->construction) {
|
|
|
|
lua_pushstring(L, "build_skill_min");
|
|
|
|
lua_pushinteger(L, btype->construction->minskill);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
lua_pushstring(L, "build_skill_name");
|
|
|
|
lua_pushstring(L, skillnames[btype->construction->skill]);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
if (btype->construction->materials) {
|
2017-12-08 21:08:11 +01:00
|
|
|
int i;
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_pushstring(L, "materials");
|
|
|
|
lua_newtable(L);
|
|
|
|
for (i = 0; btype->construction->materials[i].number; ++i) {
|
|
|
|
lua_pushstring(L,
|
|
|
|
btype->construction->materials[i].rtype->_name);
|
|
|
|
lua_pushinteger(L, btype->construction->materials[i].number);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2011-03-09 00:25:51 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return 0;
|
2011-03-09 00:25:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config_get_stype(lua_State * L)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
const char *name = tolua_tostring(L, 1, 0);
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
const struct ship_type *stype = st_find(name);
|
|
|
|
if (stype) {
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_pushstring(L, "range");
|
|
|
|
lua_pushinteger(L, stype->range);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
lua_pushstring(L, "cabins");
|
|
|
|
lua_pushinteger(L, stype->cabins);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
lua_pushstring(L, "cargo");
|
|
|
|
lua_pushinteger(L, stype->cargo);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
lua_pushstring(L, "cptskill");
|
|
|
|
lua_pushinteger(L, stype->cptskill);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
lua_pushstring(L, "minskill");
|
|
|
|
lua_pushinteger(L, stype->minskill);
|
2011-03-09 00:25:51 +01:00
|
|
|
lua_settable(L, -3);
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_pushstring(L, "sumskill");
|
|
|
|
lua_pushinteger(L, stype->sumskill);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
lua_pushstring(L, "fishing");
|
|
|
|
lua_pushinteger(L, stype->fishing);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
if (stype->coasts) {
|
|
|
|
const terrain_type *coast = *stype->coasts;
|
|
|
|
lua_pushstring(L, "coasts");
|
|
|
|
lua_newtable(L);
|
|
|
|
while (coast) {
|
|
|
|
lua_pushstring(L, coast->_name);
|
|
|
|
lua_pushinteger(L, 1);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
coast = coast->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (stype->construction) {
|
|
|
|
lua_pushstring(L, "build_skill_min");
|
|
|
|
lua_pushinteger(L, stype->construction->minskill);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
lua_pushstring(L, "build_skill_name");
|
|
|
|
lua_pushstring(L, skillnames[stype->construction->skill]);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
if (stype->construction->materials) {
|
2017-12-08 21:08:11 +01:00
|
|
|
int i;
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_pushstring(L, "materials");
|
|
|
|
lua_newtable(L);
|
|
|
|
for (i = 0; stype->construction->materials[i].number; ++i) {
|
|
|
|
lua_pushstring(L,
|
|
|
|
stype->construction->materials[i].rtype->_name);
|
|
|
|
lua_pushinteger(L, stype->construction->materials[i].number);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2011-03-09 00:25:51 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return 0;
|
2011-03-09 00:25:51 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_get_spell_text(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
const struct locale *loc = default_locale;
|
|
|
|
spell *self = (spell *)tolua_tousertype(L, 1, 0);
|
|
|
|
lua_pushstring(L, spell_info(self, loc));
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_get_spell_name(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
spell *self = (spell *)tolua_tousertype(L, 1, 0);
|
2015-05-05 17:44:58 +02:00
|
|
|
lua_pushstring(L, self->sname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tolua_get_spell_entry_name(lua_State * L)
|
|
|
|
{
|
|
|
|
spellbook_entry *self = (spellbook_entry*)tolua_tousertype(L, 1, 0);
|
|
|
|
lua_pushstring(L, self->sp->sname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tolua_get_spell_entry_level(lua_State * L)
|
|
|
|
{
|
|
|
|
spellbook_entry *self = (spellbook_entry*)tolua_tousertype(L, 1, 0);
|
|
|
|
lua_pushinteger(L, self->level);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_get_spells(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2017-01-26 17:41:21 +01:00
|
|
|
return tolua_selist_push(L, "spell_list", "spell", spells);
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2014-10-29 21:08:16 +01:00
|
|
|
static int init_data(const char *filename, const char *catalog)
|
|
|
|
{
|
|
|
|
int l;
|
|
|
|
l = read_xml(filename, catalog);
|
|
|
|
reset_locales();
|
|
|
|
if (l) {
|
|
|
|
return l;
|
|
|
|
}
|
2017-05-07 16:11:45 +02:00
|
|
|
if (turn <= 0) {
|
2017-01-22 05:13:44 +01:00
|
|
|
turn = first_turn();
|
2014-10-29 21:08:16 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
int tolua_read_xml(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
const char *filename = tolua_tostring(L, 1, "config.xml");
|
|
|
|
const char *catalog = tolua_tostring(L, 2, "catalog.xml");
|
|
|
|
lua_pushinteger(L, init_data(filename, catalog));
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-08 08:44:20 +01:00
|
|
|
static int tolua_report_unit(lua_State * L)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
char buffer[512];
|
|
|
|
unit *u = (unit *)tolua_tousertype(L, 1, 0);
|
|
|
|
faction *f = (faction *)tolua_tousertype(L, 2, 0);
|
2016-09-13 17:02:51 +02:00
|
|
|
bufunit(f, u, 0, seen_unit, buffer, sizeof(buffer));
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_pushstring(L, buffer);
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
|
|
|
|
2017-01-21 19:53:47 +01:00
|
|
|
static void parse_inifile(lua_State * L, const dictionary * d, const char *section)
|
2011-03-07 08:35:23 +01:00
|
|
|
{
|
2014-07-23 15:44:57 +02:00
|
|
|
int i;
|
|
|
|
const char *arg;
|
|
|
|
size_t len = strlen(section);
|
|
|
|
|
|
|
|
for (i = 0; d && i != d->n; ++i) {
|
|
|
|
const char *key = d->key[i];
|
|
|
|
if (strncmp(section, key, len) == 0 && key[len] == ':') {
|
|
|
|
const char *str_value = d->val[i];
|
|
|
|
char *endp;
|
|
|
|
double num_value = strtod(str_value, &endp);
|
|
|
|
lua_pushstring(L, key + len + 1);
|
|
|
|
if (*endp) {
|
|
|
|
tolua_pushstring(L, str_value);
|
|
|
|
}
|
|
|
|
else {
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushnumber(L, num_value);
|
2014-07-23 15:44:57 +02:00
|
|
|
}
|
|
|
|
lua_rawset(L, -3);
|
|
|
|
}
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
2011-03-08 08:44:20 +01:00
|
|
|
|
2014-07-23 15:44:57 +02:00
|
|
|
/* special case */
|
|
|
|
lua_pushstring(L, "basepath");
|
|
|
|
lua_pushstring(L, basepath());
|
|
|
|
lua_rawset(L, -3);
|
|
|
|
lua_pushstring(L, "reportpath");
|
|
|
|
lua_pushstring(L, reportpath());
|
|
|
|
lua_rawset(L, -3);
|
2015-11-22 10:44:46 +01:00
|
|
|
arg = config_get("config.rules");
|
2014-07-23 15:44:57 +02:00
|
|
|
if (arg) {
|
|
|
|
lua_pushstring(L, "rules");
|
|
|
|
lua_pushstring(L, arg);
|
|
|
|
lua_rawset(L, -3);
|
|
|
|
}
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
2011-03-07 08:24:52 +01:00
|
|
|
|
2017-05-02 08:45:18 +02:00
|
|
|
static int lua_rng_default(lua_State *L) {
|
|
|
|
UNUSED_ARG(L);
|
|
|
|
random_source_inject_constant(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-14 19:39:17 +02:00
|
|
|
void tolua_bind_open(lua_State * L);
|
|
|
|
|
2017-01-21 19:53:47 +01:00
|
|
|
int tolua_bindings_open(lua_State * L, const dictionary *inifile)
|
2011-03-07 08:24:52 +01:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_open(L);
|
|
|
|
|
|
|
|
tolua_bind_open(L);
|
|
|
|
|
|
|
|
/* register user types */
|
2015-05-05 17:44:58 +02:00
|
|
|
tolua_usertype(L, TOLUA_CAST "spellbook");
|
|
|
|
tolua_usertype(L, TOLUA_CAST "spell_entry");
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_usertype(L, TOLUA_CAST "spell");
|
|
|
|
tolua_usertype(L, TOLUA_CAST "spell_list");
|
|
|
|
tolua_usertype(L, TOLUA_CAST "order");
|
|
|
|
tolua_usertype(L, TOLUA_CAST "item");
|
|
|
|
tolua_usertype(L, TOLUA_CAST "alliance");
|
|
|
|
tolua_usertype(L, TOLUA_CAST "event");
|
|
|
|
tolua_usertype(L, TOLUA_CAST "faction_list");
|
|
|
|
tolua_module(L, NULL, 0);
|
|
|
|
tolua_beginmodule(L, NULL);
|
2011-03-07 08:40:55 +01:00
|
|
|
{
|
2017-05-04 06:44:33 +02:00
|
|
|
tolua_module(L, TOLUA_CAST "rng", 1);
|
|
|
|
tolua_beginmodule(L, TOLUA_CAST "rng");
|
|
|
|
{
|
|
|
|
tolua_function(L, TOLUA_CAST "inject", lua_rng_default);
|
|
|
|
tolua_function(L, TOLUA_CAST "random", tolua_random);
|
|
|
|
}
|
|
|
|
tolua_endmodule(L);
|
|
|
|
tolua_function(L, TOLUA_CAST "rng_int", tolua_random);
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_cclass(L, TOLUA_CAST "alliance", TOLUA_CAST "alliance",
|
|
|
|
TOLUA_CAST "", NULL);
|
|
|
|
tolua_beginmodule(L, TOLUA_CAST "alliance");
|
|
|
|
{
|
|
|
|
tolua_variable(L, TOLUA_CAST "name", tolua_get_alliance_name,
|
|
|
|
tolua_set_alliance_name);
|
|
|
|
tolua_variable(L, TOLUA_CAST "id", tolua_get_alliance_id, NULL);
|
|
|
|
tolua_variable(L, TOLUA_CAST "factions", &tolua_get_alliance_factions,
|
|
|
|
NULL);
|
|
|
|
tolua_function(L, TOLUA_CAST "create", tolua_alliance_create);
|
|
|
|
} tolua_endmodule(L);
|
|
|
|
tolua_cclass(L, TOLUA_CAST "spell", TOLUA_CAST "spell", TOLUA_CAST "",
|
|
|
|
NULL);
|
|
|
|
tolua_beginmodule(L, TOLUA_CAST "spell");
|
|
|
|
{
|
|
|
|
tolua_function(L, TOLUA_CAST "__tostring", tolua_get_spell_name);
|
|
|
|
tolua_variable(L, TOLUA_CAST "name", tolua_get_spell_name, 0);
|
|
|
|
tolua_variable(L, TOLUA_CAST "text", tolua_get_spell_text, 0);
|
|
|
|
} tolua_endmodule(L);
|
2015-05-05 17:44:58 +02:00
|
|
|
tolua_cclass(L, TOLUA_CAST "spell_entry", TOLUA_CAST "spell_entry", TOLUA_CAST "",
|
|
|
|
NULL);
|
|
|
|
tolua_beginmodule(L, TOLUA_CAST "spell_entry");
|
|
|
|
{
|
|
|
|
tolua_function(L, TOLUA_CAST "__tostring", tolua_get_spell_entry_name);
|
|
|
|
tolua_variable(L, TOLUA_CAST "name", tolua_get_spell_entry_name, 0);
|
|
|
|
tolua_variable(L, TOLUA_CAST "level", tolua_get_spell_entry_level, 0);
|
|
|
|
} tolua_endmodule(L);
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_module(L, TOLUA_CAST "report", 1);
|
|
|
|
tolua_beginmodule(L, TOLUA_CAST "report");
|
|
|
|
{
|
|
|
|
tolua_function(L, TOLUA_CAST "report_unit", &tolua_report_unit);
|
|
|
|
} tolua_endmodule(L);
|
|
|
|
tolua_module(L, TOLUA_CAST "config", 1);
|
|
|
|
tolua_beginmodule(L, TOLUA_CAST "config");
|
|
|
|
{
|
2017-01-21 19:53:47 +01:00
|
|
|
parse_inifile(L, inifile, "lua");
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_variable(L, TOLUA_CAST "locales", &config_get_locales, 0);
|
|
|
|
tolua_function(L, TOLUA_CAST "get_resource", &config_get_resource);
|
|
|
|
tolua_variable(L, TOLUA_CAST "buildings", &config_get_buildings, 0);
|
|
|
|
tolua_function(L, TOLUA_CAST "get_building", &config_get_btype);
|
|
|
|
tolua_variable(L, TOLUA_CAST "ships", &config_get_ships, 0);
|
|
|
|
tolua_function(L, TOLUA_CAST "get_ship", &config_get_stype);
|
|
|
|
} tolua_endmodule(L);
|
|
|
|
tolua_function(L, TOLUA_CAST "get_region_by_id", tolua_get_region_byid);
|
|
|
|
tolua_function(L, TOLUA_CAST "get_unit", tolua_get_unit);
|
|
|
|
tolua_function(L, TOLUA_CAST "get_alliance", tolua_get_alliance);
|
|
|
|
tolua_function(L, TOLUA_CAST "get_ship", tolua_get_ship);
|
|
|
|
tolua_function(L, TOLUA_CAST "get_building", tolua_get_building);
|
|
|
|
tolua_function(L, TOLUA_CAST "get_region", tolua_get_region);
|
|
|
|
tolua_function(L, TOLUA_CAST "factions", tolua_get_factions);
|
|
|
|
tolua_function(L, TOLUA_CAST "regions", tolua_get_regions);
|
|
|
|
tolua_function(L, TOLUA_CAST "read_turn", tolua_read_turn);
|
2017-02-27 19:35:14 +01:00
|
|
|
tolua_function(L, TOLUA_CAST "turn_begin", tolua_turn_begin);
|
|
|
|
tolua_function(L, TOLUA_CAST "turn_process", tolua_turn_process);
|
|
|
|
tolua_function(L, TOLUA_CAST "turn_end", tolua_turn_end);
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_function(L, TOLUA_CAST "process_orders", tolua_process_orders);
|
|
|
|
tolua_function(L, TOLUA_CAST "init_reports", tolua_init_reports);
|
|
|
|
tolua_function(L, TOLUA_CAST "write_reports", tolua_write_reports);
|
|
|
|
tolua_function(L, TOLUA_CAST "write_report", tolua_write_report);
|
|
|
|
tolua_function(L, TOLUA_CAST "init_summary", tolua_init_summary);
|
|
|
|
tolua_function(L, TOLUA_CAST "write_summary", tolua_write_summary);
|
|
|
|
tolua_function(L, TOLUA_CAST "write_passwords", tolua_write_passwords);
|
|
|
|
tolua_function(L, TOLUA_CAST "message_unit", tolua_message_unit);
|
|
|
|
tolua_function(L, TOLUA_CAST "message_faction", tolua_message_faction);
|
|
|
|
tolua_function(L, TOLUA_CAST "message_region", tolua_message_region);
|
|
|
|
|
|
|
|
/* scripted monsters */
|
|
|
|
tolua_function(L, TOLUA_CAST "spawn_braineaters", tolua_spawn_braineaters);
|
|
|
|
|
|
|
|
tolua_function(L, TOLUA_CAST "update_guards", tolua_update_guards);
|
|
|
|
tolua_function(L, TOLUA_CAST "set_turn", &tolua_set_turn);
|
|
|
|
tolua_function(L, TOLUA_CAST "get_turn", &tolua_get_turn);
|
|
|
|
tolua_function(L, TOLUA_CAST "get_season", tolua_get_season);
|
|
|
|
tolua_function(L, TOLUA_CAST "equipment_setitem", tolua_equipment_setitem);
|
|
|
|
tolua_function(L, TOLUA_CAST "equip_unit", tolua_equipunit);
|
|
|
|
tolua_function(L, TOLUA_CAST "atoi36", tolua_atoi36);
|
|
|
|
tolua_function(L, TOLUA_CAST "itoa36", tolua_itoa36);
|
|
|
|
tolua_function(L, TOLUA_CAST "dice_roll", tolua_dice_rand);
|
|
|
|
tolua_function(L, TOLUA_CAST "get_nmrs", tolua_get_nmrs);
|
|
|
|
tolua_function(L, TOLUA_CAST "remove_empty_units", tolua_remove_empty_units);
|
|
|
|
tolua_function(L, TOLUA_CAST "update_subscriptions", tolua_update_subscriptions);
|
|
|
|
tolua_function(L, TOLUA_CAST "update_scores", tolua_update_scores);
|
|
|
|
tolua_function(L, TOLUA_CAST "update_owners", tolua_update_owners);
|
2016-03-17 21:21:23 +01:00
|
|
|
tolua_function(L, TOLUA_CAST "learn_skill", tolua_learn_skill);
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_function(L, TOLUA_CAST "create_curse", tolua_create_curse);
|
|
|
|
tolua_function(L, TOLUA_CAST "autoseed", tolua_autoseed);
|
|
|
|
tolua_function(L, TOLUA_CAST "get_key", tolua_getkey);
|
|
|
|
tolua_function(L, TOLUA_CAST "set_key", tolua_setkey);
|
|
|
|
tolua_function(L, TOLUA_CAST "translate", &tolua_translate);
|
|
|
|
tolua_function(L, TOLUA_CAST "spells", tolua_get_spells);
|
|
|
|
tolua_function(L, TOLUA_CAST "read_xml", tolua_read_xml);
|
|
|
|
} tolua_endmodule(L);
|
|
|
|
return 1;
|
2011-03-07 08:35:23 +01:00
|
|
|
}
|
2012-06-07 04:29:36 +02:00
|
|
|
|
|
|
|
static void openlibs(lua_State * L)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
luaL_openlibs(L);
|
2012-06-07 04:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void lua_done(lua_State * L) {
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_close(L);
|
2012-06-07 04:29:36 +02:00
|
|
|
}
|
|
|
|
|
2017-01-21 19:53:47 +01:00
|
|
|
lua_State *lua_init(const dictionary *inifile) {
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_State *L = luaL_newstate();
|
2012-06-07 04:29:36 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
openlibs(L);
|
|
|
|
register_tolua_helpers();
|
2017-01-21 19:53:47 +01:00
|
|
|
tolua_bindings_open(L, inifile);
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_eressea_open(L);
|
2017-01-22 12:57:25 +01:00
|
|
|
#ifdef USE_SQLITE
|
|
|
|
tolua_sqlite_open(L);
|
|
|
|
#endif
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_unit_open(L);
|
|
|
|
tolua_building_open(L);
|
|
|
|
tolua_ship_open(L);
|
|
|
|
tolua_region_open(L);
|
|
|
|
tolua_faction_open(L);
|
|
|
|
tolua_unit_open(L);
|
|
|
|
tolua_message_open(L);
|
2015-12-30 21:20:59 +01:00
|
|
|
tolua_order_open(L);
|
2014-06-12 05:30:34 +02:00
|
|
|
#ifdef USE_CURSES
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_gmtool_open(L);
|
2012-06-07 04:29:36 +02:00
|
|
|
#endif
|
2015-01-30 20:37:14 +01:00
|
|
|
tolua_storage_open(L);
|
|
|
|
return L;
|
2012-06-07 04:29:36 +02:00
|
|
|
}
|
|
|
|
|
2017-01-22 18:01:09 +01:00
|
|
|
static int run_script(lua_State *L, const char *luafile) {
|
|
|
|
int err;
|
|
|
|
FILE *F;
|
|
|
|
|
|
|
|
F = fopen(luafile, "r");
|
|
|
|
if (!F) {
|
|
|
|
log_debug("dofile('%s'): %s", luafile, strerror(errno));
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
fclose(F);
|
|
|
|
|
|
|
|
log_debug("executing script %s", luafile);
|
|
|
|
lua_getglobal(L, "dofile");
|
|
|
|
lua_pushstring(L, luafile);
|
|
|
|
err = lua_pcall(L, 1, 1, -3); /* error handler (debug.traceback) is now at stack -3 */
|
|
|
|
if (err != 0) {
|
|
|
|
log_lua_error(L);
|
|
|
|
assert(!"Lua syntax error? check log.");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (lua_isnumber(L, -1)) {
|
|
|
|
err = (int)lua_tonumber(L, -1);
|
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2014-04-24 14:41:04 +02:00
|
|
|
int eressea_run(lua_State *L, const char *luafile)
|
2012-06-07 04:29:36 +02:00
|
|
|
{
|
2017-01-22 18:01:09 +01:00
|
|
|
int err;
|
2014-04-20 18:36:25 +02:00
|
|
|
global.vm_state = L;
|
2017-01-22 18:01:09 +01:00
|
|
|
|
|
|
|
/* push an error handling function on the stack: */
|
|
|
|
lua_getglobal(L, "debug");
|
|
|
|
lua_getfield(L, -1, "traceback");
|
|
|
|
lua_remove(L, -2);
|
|
|
|
|
|
|
|
/* try to run configuration scripts: */
|
|
|
|
err = run_script(L, "custom.lua");
|
|
|
|
|
2014-04-20 18:36:25 +02:00
|
|
|
/* run the main script */
|
|
|
|
if (luafile) {
|
2017-01-22 18:01:09 +01:00
|
|
|
err = run_script(L, luafile);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
err = lua_console(L);
|
2012-06-07 04:29:36 +02:00
|
|
|
}
|
2017-01-22 18:01:09 +01:00
|
|
|
/* pop error handler off the stack: */
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return err;
|
2012-06-17 04:23:45 +02:00
|
|
|
}
|