]> granicus.if.org Git - nethack/commitdiff
Make lua selection grow create a new selection
authorPasi Kallinen <paxed@alt.org>
Sat, 22 Feb 2020 14:58:45 +0000 (16:58 +0200)
committerPasi Kallinen <paxed@alt.org>
Sat, 22 Feb 2020 14:58:45 +0000 (16:58 +0200)
src/nhlsel.c
test/test_sel.lua

index 586a7a790d806f44e655d2314c536cbda3a57e97..b62472578d75b09772918b761089ac42ef600179 100644 (file)
@@ -515,11 +515,18 @@ static int
 l_selection_grow(L)
 lua_State *L;
 {
+    int argc = lua_gettop(L);
     const char *const growdirs[] = { "all", "random", "north", "west", "east", "south", NULL };
     const int growdirs2i[] = { W_ANY, -1, W_NORTH, W_WEST, W_EAST, W_SOUTH, 0 };
 
     struct selectionvar *sel = l_selection_check(L, 1);
     int dir = growdirs2i[luaL_checkoption(L, 2, "all", growdirs)];
+
+    if (argc == 2)
+        lua_pop(L, 1); /* get rid of growdir */
+
+    (void) l_selection_clone(L);
+    sel = l_selection_check(L, 1);
     selection_do_grow(sel, dir);
     lua_settop(L, 1);
     return 1;
index 4e911a5123617fa9f35851acfe9269aec413db00..8892eb3d97ce59ba760e1ad256802a76f3349e0a 100644 (file)
@@ -292,6 +292,36 @@ function test_sel_randline()
    end
 end -- test_sel_randline
 
+function test_sel_grow()
+   local __func__ = "test_sel_grow";
+   local sela = selection.new();
+
+   sela:set(5,5);
+
+   local sela_clone = sela:clone();
+
+   local selb = sela:grow();
+   sel_are_equal(sela, sela_clone, __func__);
+   sel_has_n_points(selb, 9, __func__);
+   for x = 4,6 do
+      for y = 4,6 do
+         sel_pt_ne(selb, x,y, 1, __func__);
+      end
+   end
+
+   local selc = sela:grow("north");
+   sel_has_n_points(selc, 2, __func__);
+   sel_pt_ne(selc, 5,5, 1, __func__);
+   sel_pt_ne(selc, 5,4, 1, __func__);
+
+   local seld = selc:grow("east");
+   sel_has_n_points(seld, 4, __func__);
+   sel_pt_ne(seld, 5,5, 1, __func__);
+   sel_pt_ne(seld, 5,4, 1, __func__);
+   sel_pt_ne(seld, 6,5, 1, __func__);
+   sel_pt_ne(seld, 6,4, 1, __func__);
+end -- test_sel_grow
+
 test_selection_params();
 test_sel_negate();
 test_sel_logical_and();
@@ -302,3 +332,4 @@ test_sel_line();
 test_sel_rect();
 test_sel_fillrect();
 test_sel_randline();
+test_sel_grow();