2017-12-29 06:13:28 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <platform.h>
|
|
|
|
#endif
|
|
|
|
|
2013-12-29 14:12:48 +01:00
|
|
|
#include <curses.h>
|
2013-12-29 07:58:39 +01:00
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
#include "bind_gmtool.h"
|
2014-02-18 05:45:00 +01:00
|
|
|
#include "gmtool.h"
|
|
|
|
#include "gmtool_structs.h"
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
#include <kernel/region.h>
|
|
|
|
#include <kernel/terrain.h>
|
|
|
|
#include <modules/autoseed.h>
|
|
|
|
#include <util/log.h>
|
2017-12-28 18:55:45 +01:00
|
|
|
#include <util/macros.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2018-09-23 19:44:05 +02:00
|
|
|
#include <lua.h>
|
|
|
|
#include <lauxlib.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <tolua.h>
|
|
|
|
|
2013-12-29 09:21:10 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_run_mapper(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2017-12-28 18:55:45 +01:00
|
|
|
UNUSED_ARG(L);
|
2015-01-30 20:37:14 +01:00
|
|
|
run_mapper();
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_highlight_region(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
region *r = (region *)tolua_tousertype(L, 1, 0);
|
|
|
|
int select = tolua_toboolean(L, 2, 0);
|
2017-12-28 18:55:45 +01:00
|
|
|
UNUSED_ARG(L);
|
2015-01-30 20:37:14 +01:00
|
|
|
highlight_region(r, select);
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_current_region(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
map_region *mr =
|
|
|
|
cursor_region(¤t_state->display, ¤t_state->cursor);
|
|
|
|
tolua_pushusertype(L, mr ? mr->r : NULL, TOLUA_CAST "region");
|
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_select_coordinate(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int nx = (int)tolua_tonumber(L, 1, 0);
|
|
|
|
int ny = (int)tolua_tonumber(L, 2, 0);
|
|
|
|
int select = tolua_toboolean(L, 3, 0);
|
2017-12-28 18:55:45 +01:00
|
|
|
UNUSED_ARG(L);
|
2015-01-30 20:37:14 +01:00
|
|
|
if (current_state) {
|
|
|
|
select_coordinate(current_state->selected, nx, ny, select);
|
|
|
|
}
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_select_region(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
region *r = tolua_tousertype(L, 1, 0);
|
|
|
|
int select = tolua_toboolean(L, 2, 0);
|
|
|
|
if (current_state && r) {
|
|
|
|
select_coordinate(current_state->selected, r->x, r->y, select);
|
|
|
|
}
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct tag_iterator {
|
2015-01-30 20:37:14 +01:00
|
|
|
selection *list;
|
|
|
|
tag *node;
|
|
|
|
region *r;
|
|
|
|
int hash;
|
2010-08-08 10:06:34 +02:00
|
|
|
} tag_iterator;
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void tag_advance(tag_iterator * iter)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
while (iter->hash != MAXTHASH) {
|
|
|
|
if (iter->node) {
|
|
|
|
iter->node = iter->node->nexthash;
|
|
|
|
}
|
|
|
|
while (!iter->node && iter->hash != MAXTHASH) {
|
|
|
|
if (++iter->hash != MAXTHASH) {
|
|
|
|
iter->node = iter->list->tags[iter->hash];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (iter->node) {
|
|
|
|
iter->r = findregion(iter->node->coord.x, iter->node->coord.y);
|
|
|
|
if (iter->r) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void tag_rewind(tag_iterator * iter)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
if (iter->list) {
|
|
|
|
iter->r = NULL;
|
|
|
|
iter->node = iter->list->tags[0];
|
|
|
|
iter->hash = 0;
|
|
|
|
if (iter->node) {
|
|
|
|
iter->r = findregion(iter->node->coord.x, iter->node->coord.y);
|
|
|
|
}
|
|
|
|
if (!iter->r) {
|
|
|
|
tag_advance(iter);
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else {
|
|
|
|
iter->node = 0;
|
|
|
|
iter->hash = MAXTHASH;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_tags_next(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
tag_iterator *iter = (tag_iterator *)lua_touserdata(L, lua_upvalueindex(1));
|
|
|
|
if (iter->node) {
|
|
|
|
tolua_pushusertype(L, (void *)iter->r, TOLUA_CAST "region");
|
|
|
|
tag_advance(iter);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0; /* no more values to return */
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_selected_regions(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
tag_iterator *iter =
|
|
|
|
(tag_iterator *)lua_newuserdata(L, sizeof(tag_iterator));
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
luaL_getmetatable(L, "tag_iterator");
|
|
|
|
lua_setmetatable(L, -2);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
iter->list = current_state->selected;
|
|
|
|
tag_rewind(iter);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
lua_pushcclosure(L, tolua_tags_next, 1);
|
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_state_open(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2017-01-10 16:31:05 +01:00
|
|
|
UNUSED_ARG(L);
|
2015-01-30 20:37:14 +01:00
|
|
|
state_open();
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_state_close(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2017-01-10 16:31:05 +01:00
|
|
|
UNUSED_ARG(L);
|
2015-01-30 20:37:14 +01:00
|
|
|
state_close(current_state);
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_make_island(lua_State * L)
|
2010-08-08 10:06:34 +02: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);
|
|
|
|
int s = (int)tolua_tonumber(L, 3, 0);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2019-04-05 17:14:28 +02:00
|
|
|
s = build_island(x, y, s, NULL, 0);
|
2017-07-27 16:36:45 +02:00
|
|
|
lua_pushinteger(L, s);
|
2015-01-30 20:37:14 +01:00
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int paint_handle;
|
2011-03-07 08:02:35 +01:00
|
|
|
static struct lua_State *paint_state;
|
|
|
|
|
|
|
|
static void lua_paint_info(struct window *wnd, const struct state *st)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
struct lua_State *L = paint_state;
|
|
|
|
int nx = st->cursor.x, ny = st->cursor.y;
|
|
|
|
pnormalize(&nx, &ny, st->cursor.pl);
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, paint_handle);
|
2015-06-08 20:53:40 +02:00
|
|
|
lua_pushinteger(L, nx);
|
|
|
|
lua_pushinteger(L, ny);
|
2015-01-30 20:37:14 +01:00
|
|
|
if (lua_pcall(L, 2, 1, 0) != 0) {
|
|
|
|
const char *error = lua_tostring(L, -1);
|
|
|
|
log_error("paint function failed: %s\n", error);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
tolua_error(L, TOLUA_CAST "event handler call failed", NULL);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const char *result = lua_tostring(L, -1);
|
|
|
|
WINDOW *win = wnd->handle;
|
|
|
|
int size = getmaxx(win) - 2;
|
|
|
|
int line = 0, maxline = getmaxy(win) - 2;
|
|
|
|
const char *str = result;
|
|
|
|
wxborder(win);
|
|
|
|
|
|
|
|
while (*str && line < maxline) {
|
|
|
|
const char *end = strchr(str, '\n');
|
|
|
|
if (!end)
|
|
|
|
break;
|
|
|
|
else {
|
2017-12-28 18:55:45 +01:00
|
|
|
int bytes = (int)(end - str);
|
|
|
|
if (bytes < size) bytes = size;
|
2015-01-30 20:37:14 +01:00
|
|
|
mvwaddnstr(win, line++, 1, str, bytes);
|
|
|
|
wclrtoeol(win);
|
|
|
|
str = end + 1;
|
|
|
|
}
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_set_display(lua_State * L)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int type = lua_type(L, 1);
|
|
|
|
if (type == LUA_TFUNCTION) {
|
|
|
|
lua_pushvalue(L, 1);
|
|
|
|
paint_handle = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
paint_state = L;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
set_info_function(&lua_paint_info);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
set_info_function(NULL);
|
|
|
|
}
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int tolua_make_block(lua_State * L)
|
2010-08-08 10:06:34 +02: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);
|
|
|
|
int r = (int)tolua_tonumber(L, 3, 6);
|
|
|
|
const char *str = tolua_tostring(L, 4, TOLUA_CAST "ocean");
|
|
|
|
const struct terrain_type *ter = get_terrain(str);
|
|
|
|
|
|
|
|
make_block(x, y, r, ter);
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void tolua_gmtool_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 "tag_iterator");
|
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_module(L, TOLUA_CAST "gmtool", 0);
|
|
|
|
tolua_beginmodule(L, TOLUA_CAST "gmtool");
|
|
|
|
{
|
|
|
|
tolua_function(L, TOLUA_CAST "open", &tolua_state_open);
|
|
|
|
tolua_function(L, TOLUA_CAST "close", &tolua_state_close);
|
|
|
|
|
|
|
|
tolua_function(L, TOLUA_CAST "editor", &tolua_run_mapper);
|
|
|
|
tolua_function(L, TOLUA_CAST "get_selection", &tolua_selected_regions);
|
|
|
|
tolua_function(L, TOLUA_CAST "get_cursor", &tolua_current_region);
|
|
|
|
tolua_function(L, TOLUA_CAST "highlight", &tolua_highlight_region);
|
|
|
|
tolua_function(L, TOLUA_CAST "select", &tolua_select_region);
|
|
|
|
tolua_function(L, TOLUA_CAST "select_at", &tolua_select_coordinate);
|
|
|
|
tolua_function(L, TOLUA_CAST "set_display", &tolua_set_display);
|
|
|
|
|
|
|
|
tolua_function(L, TOLUA_CAST "make_block", &tolua_make_block);
|
|
|
|
tolua_function(L, TOLUA_CAST "make_island", &tolua_make_island);
|
|
|
|
}
|
|
|
|
tolua_endmodule(L);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
tolua_endmodule(L);
|
|
|
|
}
|