]> granicus.if.org Git - nethack/commitdiff
Expose selection bounds to lua
authorPasi Kallinen <paxed@alt.org>
Fri, 26 Aug 2022 10:07:52 +0000 (13:07 +0300)
committerPasi Kallinen <paxed@alt.org>
Fri, 26 Aug 2022 10:07:52 +0000 (13:07 +0300)
doc/lua.adoc
src/nhlsel.c
test/test_sel.lua

index cb0996719984d124ebe41451979c7b62ba33faad..e3f571a7a4a69394f2c5f1d45b484fb7a635deb0 100644 (file)
@@ -891,6 +891,16 @@ Example:
 
 Alias for <<_fillrect>>.
 
+=== bounds
+
+Get the bounding box for the selection. Returns a table with lx, ly, hx, hy integer fields.
+
+Example:
+
+ local rect = sel:bounds();
+ local s = string.format("(%i,%i)-(%i,%i)", rect.lx, rect.ly, rect.hx, rect.hy));
+
+
 === circle
 
 Example:
index 25ebed2d4d8f3a105664e1eea40a92508f207a3b..3f766abc94ed2de18ac818ca2f70601347f867a6 100644 (file)
@@ -17,6 +17,7 @@ static int l_selection_getpoint(lua_State *);
 static int l_selection_setpoint(lua_State *);
 static int l_selection_filter_percent(lua_State *);
 static int l_selection_rndcoord(lua_State *);
+static int l_selection_getbounds(lua_State *);
 static boolean params_sel_2coords(lua_State *, struct selectionvar **,
                                   coordxy *, coordxy *, coordxy *, coordxy *);
 static int l_selection_line(lua_State *);
@@ -382,6 +383,23 @@ l_selection_rndcoord(lua_State *L)
     return 1;
 }
 
+/* local rect = sel:bounds(); */
+static int
+l_selection_getbounds(lua_State *L)
+{
+    struct selectionvar *sel = l_selection_check(L, 1);
+    NhRect rect;
+
+    selection_getbounds(sel, &rect);
+    lua_settop(L, 0);
+    lua_newtable(L);
+    nhl_add_table_entry_int(L, "lx", rect.lx);
+    nhl_add_table_entry_int(L, "ly", rect.ly);
+    nhl_add_table_entry_int(L, "hx", rect.hx);
+    nhl_add_table_entry_int(L, "hy", rect.hy);
+    return 1;
+}
+
 /* 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.
@@ -875,6 +893,7 @@ static const struct luaL_Reg l_selection_methods[] = {
     { "ellipse", l_selection_ellipse },
     { "gradient", l_selection_gradient },
     { "iterate", l_selection_iterate },
+    { "bounds", l_selection_getbounds },
     { NULL, NULL }
 };
 
index 70ff9e0adcec5c0ffbbac00cc1e7196953849ef6..72c40896cd7c2a438263dd57b9e05711ece32429 100644 (file)
@@ -467,6 +467,20 @@ function test_sel_iterate()
    is_map_at(5,5, "L");
    is_map_at(7,5, "L");
    is_map_at(9,5, "L");
+
+end
+
+function test_sel_bounds()
+   local __func__ = "test_sel_bounds";
+   local sel = selection.new();
+   sel:set(5, 5);
+   sel:set(7, 5);
+   sel:set(5, 6);
+
+   local rect = sel:bounds();
+   if (rect.lx ~= (5 + 1) or rect.ly ~= 5 or rect.hx ~= (7 + 1) or rect.hy ~= 6) then
+      error(string.format("selection bounds error:(%i,%i-%i,%i)", rect.lx, rect.ly, rect.hx, rect.hy));
+   end
 end
 
 nh.debug_flags({mongen = false, hunger = false, overwrite_stairs = true });
@@ -487,3 +501,4 @@ test_sel_filter_mapchar();
 test_sel_flood();
 test_sel_match();
 test_sel_iterate();
+test_sel_bounds();