]> granicus.if.org Git - nethack/commitdiff
Lua: selection get and rndcoord changes
authorPasi Kallinen <paxed@alt.org>
Tue, 22 Mar 2022 08:45:20 +0000 (10:45 +0200)
committerPasi Kallinen <paxed@alt.org>
Tue, 22 Mar 2022 08:45:20 +0000 (10:45 +0200)
doc/lua.adoc
src/nhlsel.c
test/test_sel.lua

index 322f55cb5e30ac1150163c76bea721e648c47c06..a550b35a0c2235fdfe113ae0492005467730083f 100644 (file)
@@ -957,6 +957,7 @@ Get the selection value at (x,y).
 Example:
 
  local value = selection.get(sel, x, y);
+ local value = selection.get(sel, { x = 10, y = 14 });
 
 === gradient
 
index 619156917cce72bf4dd3a2d5cbd02d62181a4c33..edfa5b699700084389787c84732ead79a610ad8a 100644 (file)
@@ -180,11 +180,19 @@ static int
 l_selection_getpoint(lua_State *L)
 {
     struct selectionvar *sel = l_selection_check(L, 1);
-    xchar x = (xchar) luaL_checkinteger(L, 2);
-    xchar y = (xchar) luaL_checkinteger(L, 3);
+    xchar x, y;
+    int ix, iy;
     int val;
     long crd;
 
+    lua_remove(L, 1); /* sel */
+    if (!nhl_get_xy_params(L, &ix, &iy)) {
+        nhl_error(L, "l_selection_getpoint: Incorrect params");
+        return 0;
+    }
+    x = (xchar) ix;
+    y = (xchar) iy;
+
     if (x == -1 && y == -1)
         crd = SP_COORD_PACK_RANDOM(0);
     else
@@ -327,15 +335,17 @@ l_selection_rndcoord(lua_State *L)
 {
     struct selectionvar *sel = l_selection_check(L, 1);
     int removeit = (int) luaL_optinteger(L, 2, 0);
-    xchar x, y;
+    xchar x = -1, y = -1;
     selection_rndcoord(sel, &x, &y, removeit);
-    update_croom();
-    if (g.coder && g.coder->croom) {
-        x -= g.coder->croom->lx;
-        y -= g.coder->croom->ly;
-    } else {
-        x -= g.xstart;
-        y -= g.ystart;
+    if (!(x == -1 && y == -1)) {
+        update_croom();
+        if (g.coder && g.coder->croom) {
+            x -= g.coder->croom->lx;
+            y -= g.coder->croom->ly;
+        } else {
+            x -= g.xstart;
+            y -= g.ystart;
+        }
     }
     lua_settop(L, 0);
     lua_newtable(L);
index eaccba54f69a9d9ca9c248dab7abbd9530d21854..70ff9e0adcec5c0ffbbac00cc1e7196953849ef6 100644 (file)
@@ -66,8 +66,9 @@ function test_selection_params()
       error("sel:rndcoord returned unset coordinate (" .. pt.x .. "," .. pt.y .. ")");
    end
 
+   -- no coordinates in selection, returns -1,-1
    pt = sel:rndcoord(1);
-   if pt.x ~= -2 or pt.y ~= -1 then
+   if pt.x ~= -1 or pt.y ~= -1 then
       error("sel:rndcoord returned (" .. pt.x .. "," .. pt.y .. ") coordinate");
    end
 
@@ -89,6 +90,8 @@ function test_selection_params()
 
    -- variable as param
    selection.get(sel, 1, 2);
+   selection.get(sel, {1, 2});
+   selection.get(sel, { x = 1, y = 2 });
    selection.set(sel, 1, 2);
    selection.negate(sel);
    selection.percentage(sel, 50);