]> granicus.if.org Git - nethack/commitdiff
Lua: diagonals for selection floodfill
authorPasi Kallinen <paxed@alt.org>
Sun, 15 Aug 2021 10:50:28 +0000 (13:50 +0300)
committerPasi Kallinen <paxed@alt.org>
Sun, 15 Aug 2021 10:50:28 +0000 (13:50 +0300)
doc/lua.adoc
src/nhlsel.c
test/test_sel.lua

index 8d71cdd5a68f9d242bf043681d9bebf85cd011b1..1e605bc855125931a4b68a8650b78375ebacabb2 100644 (file)
@@ -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
 
index 58e712f9f4b74461e3820ad9403cfde62960c3b1..9f25c3297380957216ac9215f3494c12b43f159f 100644 (file)
@@ -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;
 }
index b2ad83753f1aa24e0dc9e4b7437f19d47353452c..6ad7d04a49b241c737f4e115c10b1acc6c4ccb5e 100644 (file)
@@ -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()