From: Pasi Kallinen Date: Sun, 23 Jan 2022 11:27:00 +0000 (+0200) Subject: Lua: Pass more data to room contents function X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a6816824c7ff97ef009d43b367fcde24ebef3540;p=nethack Lua: Pass more data to room contents function --- diff --git a/doc/lua.adoc b/doc/lua.adoc index 3410cb02b..886ba2d71 100644 --- a/doc/lua.adoc +++ b/doc/lua.adoc @@ -680,7 +680,25 @@ fields: | lit | Is the room lit or unlit? Defaults to -1 (random). | filled | Is the room filled as per the room type. Defaults to 1 (filled). | joined | Is the room joined to the rest of the level with corridors? Default is true. -| contents | A function called with one parameter, a table with "width" and "height", the room width and height, excluding the walls. All coordinates in the function will be relative to the room. +| contents | A function called with one parameter, a table with room data. See <<_room_contents>>. +|=== + +==== room contents + +The room contents function is called when the room is created. +All coordinates in the function will be relative to the room. +The function get passed one parameter, a table with room data: + +[options="header"] +|=== +| parameter | description +| width | room width, excluding the walls. +| height | room height, excluding the walls. +| region | table with 4 elements, the room region coordinates: x1, y1, x2, y2. +| lit | is the room lit or unlit? +| irregular | is the room irregular? +| needjoining | does the room need joining with corridors? +| type | the room type. |=== diff --git a/include/extern.h b/include/extern.h index 32e9a2370..e855fddb1 100644 --- a/include/extern.h +++ b/include/extern.h @@ -1751,6 +1751,9 @@ extern schar get_table_mapchr_opt(lua_State *, const char *, schar); extern void nhl_add_table_entry_int(lua_State *, const char *, int); extern void nhl_add_table_entry_char(lua_State *, const char *, char); extern void nhl_add_table_entry_str(lua_State *, const char *, const char *); +extern void nhl_add_table_entry_bool(lua_State *, const char *, boolean); +extern void nhl_add_table_entry_region(lua_State *, const char *, + xchar, xchar, xchar, xchar); extern schar splev_chr2typ(char); extern schar check_mapchr(const char *); extern int get_table_int(lua_State *, const char *); diff --git a/src/nhlua.c b/src/nhlua.c index 36713a28d..7673a7d69 100644 --- a/src/nhlua.c +++ b/src/nhlua.c @@ -24,7 +24,6 @@ static int nhl_doturn(lua_State *); static int nhl_debug_flags(lua_State *); static int nhl_test(lua_State *); static int nhl_getmap(lua_State *); -static void nhl_add_table_entry_bool(lua_State *, const char *, boolean); static char splev_typ2chr(schar); static int nhl_gettrap(lua_State *); static int nhl_deltrap(lua_State *); @@ -225,6 +224,19 @@ nhl_add_table_entry_bool(lua_State *L, const char *name, boolean value) lua_rawset(L, -3); } +void +nhl_add_table_entry_region(lua_State *L, const char *name, + xchar x1, xchar y1, xchar x2, xchar y2) +{ + lua_pushstring(L, name); + lua_newtable(L); + nhl_add_table_entry_int(L, "x1", x1); + nhl_add_table_entry_int(L, "y1", y1); + nhl_add_table_entry_int(L, "x2", x2); + nhl_add_table_entry_int(L, "y2", y2); + lua_rawset(L, -3); +} + /* converting from special level "map character" to levl location type and back. order here is important. */ const struct { diff --git a/src/sp_lev.c b/src/sp_lev.c index a337a0bd6..0187679fa 100644 --- a/src/sp_lev.c +++ b/src/sp_lev.c @@ -103,7 +103,7 @@ static int floodfillchk_match_under(int, int); static int floodfillchk_match_accessible(int, int); static boolean sel_flood_havepoint(int, int, xchar *, xchar *, int); static long line_dist_coord(long, long, long, long, long, long); -static void l_push_wid_hei_table(lua_State *, int, int); +static void l_push_mkroom_table(lua_State *, struct mkroom *); static int get_table_align(lua_State *); static int get_table_monclass(lua_State *); static int find_montype(lua_State *, const char *, int *); @@ -113,6 +113,7 @@ static int get_table_buc(lua_State *); static int get_table_objclass(lua_State *); static int find_objtype(lua_State *, const char *); static int get_table_objtype(lua_State *); +static const char *get_mkroom_name(int); static int get_table_roomtype_opt(lua_State *, const char *, int); static int get_table_traptype_opt(lua_State *, const char *, int); static int get_traptype_byname(const char *); @@ -2898,14 +2899,23 @@ static void l_push_wid_hei_table(lua_State *L, int wid, int hei) { lua_newtable(L); + nhl_add_table_entry_int(L, "width", wid); + nhl_add_table_entry_int(L, "height", hei); +} - lua_pushstring(L, "width"); - lua_pushinteger(L, wid); - lua_rawset(L, -3); - - lua_pushstring(L, "height"); - lua_pushinteger(L, hei); - lua_rawset(L, -3); +/* push a table on lua stack containing room data */ +static void +l_push_mkroom_table(lua_State *L, struct mkroom *tmpr) +{ + lua_newtable(L); + nhl_add_table_entry_int(L, "width", 1 + (tmpr->hx - tmpr->lx)); + nhl_add_table_entry_int(L, "height", 1 + (tmpr->hy - tmpr->ly)); + nhl_add_table_entry_region(L, "region", tmpr->lx, tmpr->ly, + tmpr->hx, tmpr->hy); + nhl_add_table_entry_bool(L, "lit", (boolean) tmpr->rlit); + nhl_add_table_entry_bool(L, "irregular", tmpr->irregular); + nhl_add_table_entry_bool(L, "needjoining", tmpr->needjoining); + nhl_add_table_entry_str(L, "type", get_mkroom_name(tmpr->rtype)); } /* message("What a strange feeling!"); */ @@ -3683,6 +3693,17 @@ static const struct { { 0, 0 } }; +static const char * +get_mkroom_name(int rtype) +{ + int i; + + for (i = 0; room_types[i].name; i++) + if (room_types[i].type == rtype) + return room_types[i].name; + return NULL; +} + static int get_table_roomtype_opt(lua_State *L, const char *name, int defval) { @@ -3766,8 +3787,7 @@ lspo_room(lua_State *L) lua_getfield(L, 1, "contents"); if (lua_type(L, -1) == LUA_TFUNCTION) { lua_remove(L, -2); - l_push_wid_hei_table(L, 1 + tmpcr->hx - tmpcr->lx, - 1 + tmpcr->hy - tmpcr->ly); + l_push_mkroom_table(L, tmpcr); lua_call(L, 1, 0); } else lua_pop(L, 1);