From: Pasi Kallinen Date: Sun, 15 Aug 2021 10:50:28 +0000 (+0300) Subject: Lua: diagonals for selection floodfill X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f43bfc3f71a3ef543e4de24b0bcad74e6be16f75;p=nethack Lua: diagonals for selection floodfill --- diff --git a/doc/lua.adoc b/doc/lua.adoc index 8d71cdd5a..1e605bc85 100644 --- a/doc/lua.adoc +++ b/doc/lua.adoc @@ -809,11 +809,13 @@ Example: Select locations by starting floodfill at (x,y), matching the same map terrain in cardinal directions. +If the optional third parameter is true, also checks diagonals. Example: local s = selection.floodfill(sel, x, y); local s = selection.floodfill(x,y); + local s = selection.floodfill(x,y, true); === get diff --git a/src/nhlsel.c b/src/nhlsel.c index 58e712f9f..9f25c3297 100644 --- a/src/nhlsel.c +++ b/src/nhlsel.c @@ -570,17 +570,21 @@ l_selection_match(lua_State *L) /* local s = selection.floodfill(x,y); */ +/* local s = selection.floodfill(x,y, diagonals); */ static int l_selection_flood(lua_State *L) { int argc = lua_gettop(L); struct selectionvar *sel = (struct selectionvar *) 0; xchar x = 0, y = 0; + boolean diagonals = FALSE; - if (argc == 2) { + if (argc == 2 || argc == 3) { x = (xchar) luaL_checkinteger(L, 1); y = (xchar) luaL_checkinteger(L, 2); - lua_pop(L, 2); + if (argc == 3) + diagonals = lua_toboolean(L, 3); + lua_pop(L, argc); (void) l_selection_new(L); sel = l_selection_check(L, 1); } else { @@ -593,7 +597,7 @@ l_selection_flood(lua_State *L) if (isok(x, y)) { set_floodfillchk_match_under(levl[x][y].typ); - selection_floodfill(sel, x, y, FALSE); + selection_floodfill(sel, x, y, diagonals); } return 1; } diff --git a/test/test_sel.lua b/test/test_sel.lua index b2ad83753..6ad7d04a4 100644 --- a/test/test_sel.lua +++ b/test/test_sel.lua @@ -114,6 +114,7 @@ function test_selection_params() selection.fillrect(1,2, 7,8); selection.area(1,2, 7,8); selection.floodfill(1,1); + selection.floodfill(1,1, true); selection.circle(40, 10, 9); selection.circle(40, 10, 9, 1); selection.ellipse(40, 10, 20, 8); @@ -357,22 +358,35 @@ function test_sel_filter_mapchar() sel_has_n_points(seld, 1659, __func__); end -- test_sel_filter_mapchar -function test_sel_flood() - local __func__ = "test_sel_flood"; - local sela = selection.new(); - local sela_clone = sela:clone(); - +function set_flood_test_pattern() des.terrain(selection.negate(), "."); des.terrain(5,5, "L"); des.terrain(6,5, "L"); des.terrain(7,5, "L"); des.terrain(8,6, "L"); +end + +function test_sel_flood() + local __func__ = "test_sel_flood"; + local sela = selection.new(); + local sela_clone = sela:clone(); + + set_flood_test_pattern(); local selb = selection.floodfill(6,5); sel_has_n_points(selb, 3, __func__); sel_pt_ne(selb, 5,5, 1, __func__); sel_pt_ne(selb, 6,5, 1, __func__); sel_pt_ne(selb, 7,5, 1, __func__); + + set_flood_test_pattern(); + + local selc = selection.floodfill(6,5, true); + sel_has_n_points(selc, 4, __func__); + sel_pt_ne(selc, 5,5, 1, __func__); + sel_pt_ne(selc, 6,5, 1, __func__); + sel_pt_ne(selc, 7,5, 1, __func__); + sel_pt_ne(selc, 8,6, 1, __func__); end -- test_sel_flood function test_sel_match()