Implement lua selection iteration

... and showcase it by dehardcoding the Fort Ludios treasury.
This commit is contained in:
Pasi Kallinen
2020-02-26 17:25:25 +02:00
parent 1003a8142e
commit 7a54edf91f
3 changed files with 45 additions and 18 deletions

View 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, "|")

View 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;

View 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 }
};