]> granicus.if.org Git - nethack/commitdiff
Implement lua selection iteration
authorPasi Kallinen <paxed@alt.org>
Wed, 26 Feb 2020 15:25:25 +0000 (17:25 +0200)
committerPasi Kallinen <paxed@alt.org>
Wed, 26 Feb 2020 15:25:28 +0000 (17:25 +0200)
... and showcase it by dehardcoding the Fort Ludios treasury.

dat/knox.lua
src/mkmaze.c
src/nhlsel.c

index 360edce1358467dda638992529ec2ebad93aa18a..cc38b6a44d8d3525333b10e94257e7ea6a92a27f 100644 (file)
@@ -52,10 +52,23 @@ if math.random(0, 99) < 50 then
    des.terrain(47,09, "S")
    des.terrain(47,10, "|")
 end
+
 --   The Vault
---   Using unfilled morgue for
---   identification in mkmaze.c
-des.region({ region={21,08,35,11}, lit=1, type="morgue", prefilled=1 })
+function treasure_spot(x,y)
+   des.gold({ x = x, y = y, amount = 600 + math.random(0, 300) });
+   if (math.random(0,2) == 0) then
+      if (math.random(0,2) == 0) then
+         des.trap("spiked pit", x,y);
+      else
+         des.trap("land mine", x,y);
+      end
+   end
+end
+
+des.region({ region={21,08,35,11}, lit=1, type="orginary" })
+local treasury = selection.area(21,08,35,11);
+treasury:iterate(treasure_spot);
+
 --   Vault entrance also varies
 if math.random(0, 99) < 50 then
    des.terrain(36,09, "|")
index bf1741f62712aa5ff2014a971ac0eff240f55ac0..c7e1ab5fc4ab6445f75ac96b5214973957e4baef 100644 (file)
@@ -565,19 +565,6 @@ fixup_special()
                 set_corpsenm(otmp, rndmonnum());
             }
         }
-    } else if (Is_knox(&u.uz)) {
-        /* using an unfilled morgue for rm id */
-        croom = search_special(MORGUE);
-        /* avoid inappropriate morgue-related messages */
-        g.level.flags.graveyard = g.level.flags.has_morgue = 0;
-        croom->rtype = OROOM; /* perhaps it should be set to VAULT? */
-        /* stock the main vault */
-        for (x = croom->lx; x <= croom->hx; x++)
-            for (y = croom->ly; y <= croom->hy; y++) {
-                (void) mkgold((long) rn1(300, 600), x, y);
-                if (!rn2(3) && !is_pool(x, y))
-                    (void) maketrap(x, y, rn2(3) ? LANDMINE : SPIKED_PIT);
-            }
     } else if (Role_if(PM_PRIEST) && In_quest(&u.uz)) {
         /* less chance for undead corpses (lured from lower morgues) */
         g.level.flags.graveyard = 1;
index 62aa326fd4f8003121dde5fec5766f000131ee3d..e851ca21a4c77d728ad0508cb3068f0a9ee2266a 100644 (file)
@@ -25,6 +25,7 @@ static int FDECL(l_selection_filter_mapchar, (lua_State *));
 static int FDECL(l_selection_flood, (lua_State *));
 static int FDECL(l_selection_circle, (lua_State *));
 static int FDECL(l_selection_ellipse, (lua_State *));
+static int FDECL(l_selection_iterate, (lua_State *));
 static int FDECL(l_selection_gc, (lua_State *));
 static int FDECL(l_selection_not, (lua_State *));
 static int FDECL(l_selection_and, (lua_State *));
@@ -39,7 +40,6 @@ static int FDECL(l_selection_not, (lua_State *));
    function body below.
  */
 static int FDECL(l_selection_gradient, (lua_State *));
-static int FDECL(l_selection_iterate, (lua_State *));
 static int FDECL(l_selection_add, (lua_State *));
 static int FDECL(l_selection_sub, (lua_State *));
 static int FDECL(l_selection_ipairs, (lua_State *));
@@ -701,6 +701,33 @@ lua_State *L;
     return 1;
 }
 
+/* sel:iterate(function(x,y) ... end); */
+static int
+l_selection_iterate(L)
+lua_State *L;
+{
+    int argc = lua_gettop(L);
+    struct selectionvar *sel = (struct selectionvar *) 0;
+    int x, y;
+
+    if (argc == 2 && lua_type(L, 2) == LUA_TFUNCTION) {
+        sel = l_selection_check(L, 1);
+        lua_remove(L, 1);
+        for (y = 0; y < sel->hei; y++)
+            for (x = 0; x < sel->wid; x++)
+                if (selection_getpoint(x, y, sel)) {
+                    lua_pushvalue(L, 1);
+                    lua_pushinteger(L, x - g.xstart);
+                    lua_pushinteger(L, y - g.ystart);
+                    lua_call(L, 2, 0);
+                }
+    } else {
+        nhl_error(L, "wrong parameters");
+        /*NOTREACHED*/
+    }
+    return 0;
+}
+
 
 static const struct luaL_Reg l_selection_methods[] = {
     { "new", l_selection_new },
@@ -720,9 +747,9 @@ static const struct luaL_Reg l_selection_methods[] = {
     { "floodfill", l_selection_flood },
     { "circle", l_selection_circle },
     { "ellipse", l_selection_ellipse },
+    { "iterate", l_selection_iterate },
     /* TODO:
        { "gradient", l_selection_gradient },
-       { "iterate", l_selection_iterate },
     */
     { NULL, NULL }
 };