From: Pasi Kallinen Date: Sat, 22 Feb 2020 14:58:45 +0000 (+0200) Subject: Make lua selection grow create a new selection X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=50eaf95c2770123bb4aef04eec44ef2b5fa5d8d3;p=nethack Make lua selection grow create a new selection --- diff --git a/src/nhlsel.c b/src/nhlsel.c index 586a7a790..b62472578 100644 --- a/src/nhlsel.c +++ b/src/nhlsel.c @@ -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; diff --git a/test/test_sel.lua b/test/test_sel.lua index 4e911a512..8892eb3d9 100644 --- a/test/test_sel.lua +++ b/test/test_sel.lua @@ -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();