From: Pasi Kallinen Date: Sat, 22 Feb 2020 12:23:33 +0000 (+0200) Subject: Make lua selection line create a new selection X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=44f7d44e94742164f55fb87cdc03653216b8f5ab;p=nethack Make lua selection line create a new selection ... instead of modifying the one given as a parameter. Also add some tests for it. --- diff --git a/dat/bigrm-1.lua b/dat/bigrm-1.lua index 9c16f1984..f449e6210 100644 --- a/dat/bigrm-1.lua +++ b/dat/bigrm-1.lua @@ -29,26 +29,23 @@ if math.random(0,99) < 75 then local tidx = math.random(1, #terrains); local choice = math.random(0, 4); if choice == 0 then + -- one horizontal line des.terrain(selection.line(10,8, 65,8), terrains[tidx]); elseif choice == 1 then - local sel = selection.new(); - sel:line(15,4, 15, 13); - sel:line(59,4, 59, 13); + -- two vertical lines + local sel = selection.line(15,4, 15, 13) | selection.line(59,4, 59, 13); des.terrain(sel, terrains[tidx]); elseif choice == 2 then - local sel = selection.new(); - sel:line(10,8, 38, 8); - sel:line(37,8, 65, 8); - sel:line(37,3, 37, 8); - sel:line(37,8, 37,14); + -- plus sign + local sel = selection.line(10,8, 64, 8) | selection.line(37,3, 37, 14); des.terrain(sel, terrains[tidx]); elseif choice == 3 then + -- brackets: [ ] des.terrain(selection.rect(4,4, 70,13), terrains[tidx]); - local sel = selection.new(); - sel:line(25,4, 50,4); - sel:line(25,13, 50,13); + local sel = selection.line(25,4, 50,4) | selection.line(25,13, 50,13); des.terrain(sel, '.'); else + -- nothing end end diff --git a/src/nhlsel.c b/src/nhlsel.c index 6a13d72e4..ed84ac532 100644 --- a/src/nhlsel.c +++ b/src/nhlsel.c @@ -334,6 +334,10 @@ lua_State *L; return 2; } +/* internal function to get a selection and 4 integer values from lua stack. + removes the integers from the stack. + returns TRUE if params are good. +*/ /* function(selection, x1,y1, x2,y2) */ /* selection:function(x1,y1, x2,y2) */ static boolean @@ -362,6 +366,7 @@ schar *x1, *y1, *x2, *y2; *y1 = (schar) luaL_checkinteger(L, 3); *x2 = (schar) luaL_checkinteger(L, 4); *y2 = (schar) luaL_checkinteger(L, 5); + lua_pop(L, 4); return TRUE; } return FALSE; @@ -387,8 +392,10 @@ lua_State *L; get_location_coord(&x1, &y1, ANY_LOC, g.coder ? g.coder->croom : NULL, SP_COORD_PACK(x1,y1)); get_location_coord(&x2, &y2, ANY_LOC, g.coder ? g.coder->croom : NULL, SP_COORD_PACK(x2,y2)); - selection_do_line(x1,y1,x2,y2, sel); lua_settop(L, 1); + (void) l_selection_clone(L); + sel = l_selection_check(L, 1); + selection_do_line(x1,y1,x2,y2, sel); return 1; } diff --git a/test/test_sel.lua b/test/test_sel.lua index 43518b54a..c35ae96a4 100644 --- a/test/test_sel.lua +++ b/test/test_sel.lua @@ -227,9 +227,27 @@ function test_sel_filter_percent() -- TODO: Need a predictable rn2 to test for percentage(50) end -- test_sel_filter_percent +function test_sel_line() + local __func__ = "test_sel_line"; + local sela = selection.new(); + local sela_clone = sela:clone(); + + local selb = sela:line(1,1, 5,5); + sel_are_equal(sela, sela_clone, __func__); + sel_has_n_points(selb, 5, __func__); + for x = 1, 5 do + sel_pt_ne(selb, x,x, 1, __func__); + end + + local selc = selb:line(10,1, 10,5); + sel_has_n_points(selc, 10, __func__); + +end -- test_sel_line + test_selection_params(); test_sel_negate(); test_sel_logical_and(); test_sel_logical_or(); test_sel_logical_xor(); test_sel_filter_percent(); +test_sel_line();