Rest of commit comment only as accurate as my understanding…
Renaming from grd to mapgrd to avoid confusion with grd == env.grid: This grd is the maplines-section of a vault definition.
Also move mapgrd_lib from clua to dlua since it's only used for dungeon building.
clua and dlua need more descriptive names.
OIWB7TPX3LFYDGSVPUUVUGWTY7JB4PE6EBELDFZX7B3HIOWH25GQC
GXSKBPS2YL7D55TD6UN7DV6W2HNYZZCRV5VJQYN2AYMQJEAHUQPAC
WNC5SY6P2PBCU62Q2DB5J2M4HPDS5EJY76ZEPHRGGCLJ4BSLBSSQC
T7CCGLOZ25B7BQKKGR6IA6LWBRKUWTXLTIRXUQ4YKQRVAA7AHZKQC
WQBSKRTHZRKIYIDHROBSIEHEEQX26UAFDTMYY24UOPYNMYWXDFCAC
MQ62OAMLGJVRW2QIL4PAZRAU6PC52ZVGY2FCOBIY6IWGQIHMU5CAC
UL7XFKMUX3WIU4O2LZANK4ECJ654UZPDBFGNXUEYZYOLKBYBCG6AC
CJKLBIIM2ZTWTLGISXEZOGK2JANEYUSLOKG3BBOAYAY7AFG33ELQC
3A5FX3Y4RPKWQEHKKXZKXZJ7RKV6RKWT7GTR4WFE5UBWKV2HT4RQC
K73AS36BODJSSKMT2LRFDKS7BAMETNFLWHZEPQEZFM6KQB6KRA4AC
754PQVFHYQER7P7NEHYNLQVOEALF6I23IQDNGNCOK5IV7OLAEXRQC
LE5U6CTXEIETQN5GOVYF2K2VCISRXR3ULORXDKIKWYDVBG5GS3WAC
7Y5HSDFKA5TPLS2TWTRFMQVX6UXUDHXU5MUMXQSDFAIY4THQ3BIQC
YFJLINBBEHE7RBETTARSYNWSO6QJ6MSPXFMSPSH2742QFC3L55SQC
SDLKLUNFGVKDS55DDJZCBAVIB7NL3RRYPTACAY65SCUQKV6APFSAC
W52PCSHX72WAMWKG6L4BPUBVMO6E72KYYBNKAA7554KNOTY6V7WQC
#include "AppHdr.h"
#include "dlua.h"
#include "l_libs.h"
#include "mapdef.h"
/////////////////////////////////////////////////////////////////////
// grd and grd_col handling (i.e. map_lines in a metatable)
struct mapcolumn
{
map_def* map;
int col;
};
static int grd_get(lua_State *ls)
{
// Return a metatable for this column in the map grid.
map_def *map = *(map_def **) luaL_checkudata(ls, 1, MAPGRD_METATABLE);
int column = luaL_checkint(ls, 2);
mapcolumn *mapref = clua_new_userdata<mapcolumn>(ls, MAPGRD_COL_METATABLE);
mapref->map = map;
mapref->col = column;
return (1);
}
static int grd_set(lua_State *ls)
{
return (luaL_error(ls, "%s", "Cannot assign to read-only table."));
}
static char* grd_glyph(lua_State *ls, int &col, int &row)
{
mapcolumn *mapc = (mapcolumn *)luaL_checkudata(ls, 1, MAPGRD_COL_METATABLE);
row = luaL_checkint(ls, 2);
col = mapc->col;
map_lines &lines = mapc->map->map;
if (row < 1 || col < 1 || col > lines.width() || row > lines.height())
{
return (NULL);
}
coord_def mc(col - 1, row - 1);
return (&lines(mc));
}
static int grd_col_get(lua_State *ls)
{
int col, row;
char *gly = grd_glyph(ls, col, row);
if (!gly)
return (luaL_error(ls, "Invalid coords: %d, %d", col, row));
char buf[2];
buf[0] = *gly;
buf[1] = '\0';
lua_pushstring(ls, buf);
return (1);
}
static int grd_col_set(lua_State *ls)
{
int col, row;
char *gly = grd_glyph(ls, col, row);
if (!gly)
return (luaL_error(ls, "Invalid coords: %d, %d", col, row));
const char *str = luaL_checkstring(ls, 3);
if (!str[0] || str[1])
return (luaL_error(ls, "%s", "grd must be set to a single char."));
(*gly) = str[0];
return (0);
}
void dluaopen_mapgrd(lua_State *ls)
{
// grd table
luaL_newmetatable(ls, MAPGRD_METATABLE);
lua_pushstring(ls, "__index");
lua_pushcfunction(ls, grd_get);
lua_settable(ls, -3);
lua_pushstring(ls, "__newindex");
lua_pushcfunction(ls, grd_set);
lua_settable(ls, -3);
lua_pop(ls, 1);
// grd col table
luaL_newmetatable(ls, MAPGRD_COL_METATABLE);
lua_pushstring(ls, "__index");
lua_pushcfunction(ls, grd_col_get);
lua_settable(ls, -3);
lua_pushstring(ls, "__newindex");
lua_pushcfunction(ls, grd_col_set);
lua_settable(ls, -3);
lua_pop(ls, 1);
}
static int grd_get(lua_State *ls)
{
// Return a metatable for this column in the map grid.
map_def *map = *(map_def **) luaL_checkudata(ls, 1, GRD_METATABLE);
int column = luaL_checkint(ls, 2);
mapcolumn *mapref = clua_new_userdata<mapcolumn>(ls, GRD_COL_METATABLE);
mapref->map = map;
mapref->col = column;
return (1);
}
static int grd_set(lua_State *ls)
{
return (luaL_error(ls, "%s", "Cannot assign to read-only table."));
}
static char* grd_glyph(lua_State *ls, int &col, int &row)
{
mapcolumn *mapc = (mapcolumn *)luaL_checkudata(ls, 1, GRD_COL_METATABLE);
row = luaL_checkint(ls, 2);
col = mapc->col;
map_lines &lines = mapc->map->map;
if (row < 1 || col < 1 || col > lines.width() || row > lines.height())
{
return (NULL);
}
coord_def mc(col - 1, row - 1);
return (&lines(mc));
}
static int grd_col_get(lua_State *ls)
{
int col, row;
char *gly = grd_glyph(ls, col, row);
if (!gly)
return (luaL_error(ls, "Invalid coords: %d, %d", col, row));
char buf[2];
buf[0] = *gly;
buf[1] = '\0';
lua_pushstring(ls, buf);
return (1);
}
static int grd_col_set(lua_State *ls)
{
int col, row;
char *gly = grd_glyph(ls, col, row);
if (!gly)
return (luaL_error(ls, "Invalid coords: %d, %d", col, row));
const char *str = luaL_checkstring(ls, 3);
if (!str[0] || str[1])
return (luaL_error(ls, "%s", "grd must be set to a single char."));
(*gly) = str[0];
return (0);
}
void luaopen_grd(lua_State *ls)
{
// grd table
luaL_newmetatable(ls, GRD_METATABLE);
lua_pushstring(ls, "__index");
lua_pushcfunction(ls, grd_get);
lua_settable(ls, -3);
lua_pushstring(ls, "__newindex");
lua_pushcfunction(ls, grd_set);
lua_settable(ls, -3);
lua_pop(ls, 1);
// grd col table
luaL_newmetatable(ls, GRD_COL_METATABLE);
lua_pushstring(ls, "__index");
lua_pushcfunction(ls, grd_col_get);
lua_settable(ls, -3);
lua_pushstring(ls, "__newindex");
lua_pushcfunction(ls, grd_col_set);
lua_settable(ls, -3);
lua_pop(ls, 1);
}