]> granicus.if.org Git - nethack/commitdiff
Make lua selection filter_mapchar create a new selection
authorPasi Kallinen <paxed@alt.org>
Sat, 22 Feb 2020 15:51:08 +0000 (17:51 +0200)
committerPasi Kallinen <paxed@alt.org>
Sat, 22 Feb 2020 15:51:29 +0000 (17:51 +0200)
src/nhlsel.c
test/test_sel.lua

index b62472578d75b09772918b761089ac42ef600179..237b75b501d18f9d4673b0289c9ee66c99187b12 100644 (file)
@@ -538,24 +538,32 @@ static int
 l_selection_filter_mapchar(L)
 lua_State *L;
 {
+    int argc = lua_gettop(L);
     struct selectionvar *sel = l_selection_check(L, 1);
     char *mapchr = dupstr(luaL_checkstring(L, 2));
     xchar typ = check_mapchr(mapchr);
     int lit = (int) luaL_optinteger(L, 3, -2); /* TODO: special lit values */
-    struct selectionvar *tmp = selection_filter_mapchar(sel, typ, lit);
+    struct selectionvar *tmp, *tmp2;
 
     if (typ == INVALID_TYPE)
         nhl_error(L, "Erroneous map char");
 
-    if (sel->map)
-        free(sel->map);
-    sel->map = tmp->map;
-    if (tmp)
-        free(tmp);
+    if (argc > 1)
+        lua_pop(L, argc - 1);
+
+    tmp = l_selection_push(L);
+    tmp2 = selection_filter_mapchar(sel, typ, lit);
+
+    free(tmp->map);
+    tmp->map = tmp2->map;
+    tmp2->map = NULL;
+    selection_free(tmp2, TRUE);
+
+    lua_remove(L, 1);
+
     if (mapchr)
         free(mapchr);
 
-    lua_settop(L, 1);
     return 1;
 }
 
index 8892eb3d97ce59ba760e1ad256802a76f3349e0a..b29482253e57571efa51ab53fa8473d44e41c48f 100644 (file)
@@ -322,6 +322,22 @@ function test_sel_grow()
    sel_pt_ne(seld, 6,4, 1, __func__);
 end -- test_sel_grow
 
+function test_sel_filter_mapchar()
+   local __func__ = "test_sel_filter_mapchar";
+   local sela = selection.negate();
+   local sela_clone = sela:clone();
+
+   des.terrain(sela, ".");
+   des.terrain(5,5, "L");
+   des.terrain(15,10, "L");
+
+   local selb = sela:filter_mapchar("L");
+   sel_are_equal(sela, sela_clone, __func__);
+   sel_has_n_points(selb, 2, __func__);
+   sel_pt_ne(selb, 5,5, 1, __func__);
+   sel_pt_ne(selb, 15,10, 1, __func__);
+end -- test_sel_filter_mapchar
+
 test_selection_params();
 test_sel_negate();
 test_sel_logical_and();
@@ -333,3 +349,4 @@ test_sel_rect();
 test_sel_fillrect();
 test_sel_randline();
 test_sel_grow();
+test_sel_filter_mapchar();