]> granicus.if.org Git - nethack/commitdiff
Make lua selection line create a new selection
authorPasi Kallinen <paxed@alt.org>
Sat, 22 Feb 2020 12:23:33 +0000 (14:23 +0200)
committerPasi Kallinen <paxed@alt.org>
Sat, 22 Feb 2020 12:23:36 +0000 (14:23 +0200)
... instead of modifying the one given as a parameter.

Also add some tests for it.

dat/bigrm-1.lua
src/nhlsel.c
test/test_sel.lua

index 9c16f1984576ea406224a6f07d4482d1027c9461..f449e6210846d0750f59afd191e64f7692269b45 100644 (file)
@@ -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
 
index 6a13d72e4d4355b8b424e7f07582495d4cd388d0..ed84ac5322cc2cbd3501014ab05027f550d4d676 100644 (file)
@@ -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;
 }
 
index 43518b54a8e3e08e93e3c699084f3b0833385caf..c35ae96a4ef99b495d1ee3507234bffc098f0d10 100644 (file)
@@ -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();