From: Pasi Kallinen Date: Sat, 22 Feb 2020 15:51:08 +0000 (+0200) Subject: Make lua selection filter_mapchar create a new selection X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fe3dae85d5f8cba84a73fd2e6d4d0bf9b81fa9f8;p=nethack Make lua selection filter_mapchar create a new selection --- diff --git a/src/nhlsel.c b/src/nhlsel.c index b62472578..237b75b50 100644 --- a/src/nhlsel.c +++ b/src/nhlsel.c @@ -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; } diff --git a/test/test_sel.lua b/test/test_sel.lua index 8892eb3d9..b29482253 100644 --- a/test/test_sel.lua +++ b/test/test_sel.lua @@ -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();