Themerooms: Engraving hints the location of buried treasure

Add two new themeroom functions that are called when generating
the level: pre_themerooms_generate and post_themerooms_generate,
calles before and after themerooms_generate.

Allow the buried treasure -themeroom to put down an engraving
anywhere on the level, hinting at the location of the treasure.

des.object contents function now gets the generated object passed
to it as a parameter.
This commit is contained in:
Pasi Kallinen
2022-09-18 12:35:30 +03:00
parent 2f7b202530
commit bb3dc379bc
5 changed files with 82 additions and 12 deletions

View File

@@ -50,3 +50,8 @@ function monkfoodshop()
end end
return "food shop"; return "food shop";
end end
-- pline with variable number of arguments
function pline(fmt, ...)
nh.pline(string.format(fmt, table.unpack({...})));
end

View File

@@ -14,8 +14,15 @@
-- the room only gets what you define in here. -- the room only gets what you define in here.
-- - use type = "themed" to force a room that's never converted -- - use type = "themed" to force a room that's never converted
-- to a special room, such as a shop or a temple. -- to a special room, such as a shop or a temple.
-- core calls themerooms_generate() multiple times per level --
-- to generate a single themed room. -- for each level, the core first calls pre_themerooms_generate(),
-- then it calls themerooms_generate() multiple times until it decides
-- enough rooms have been generated, and then it calls
-- post_themerooms_generate(). The lua state is persistent through
-- the gameplay, but not across saves, so remember to reset any variables.
local buried_treasure = { };
themeroom_fills = { themeroom_fills = {
@@ -74,7 +81,12 @@ themeroom_fills = {
-- Buried treasure -- Buried treasure
function(rm) function(rm)
des.object({ id = "chest", buried = true, contents = function() des.object({ id = "chest", buried = true, contents = function(otmp)
local xobj = otmp:totable();
-- keep track of the last buried treasure
if (xobj.NO_OBJ == nil) then
buried_treasure = { x = xobj.ox, y = xobj.oy };
end
for i = 1, d(3,4) do for i = 1, d(3,4) do
des.object(); des.object();
end end
@@ -649,6 +661,7 @@ function is_eligible(room, mkrm)
return true return true
end end
-- called repeatedly until the core decides there are enough rooms
function themerooms_generate() function themerooms_generate()
local pick = 1; local pick = 1;
local total_frequency = 0; local total_frequency = 0;
@@ -678,6 +691,34 @@ function themerooms_generate()
end end
end end
-- called before any rooms are generated
function pre_themerooms_generate()
-- reset the buried treasure location
buried_treasure = { };
end
-- called after all rooms have been generated
function post_themerooms_generate()
if (buried_treasure.x ~= nil) then
local floors = selection.negate():filter_mapchar(".");
local pos = floors:rndcoord(0);
local tx = buried_treasure.x - pos.x - 1;
local ty = buried_treasure.y - pos.y;
local dig = "";
if (tx == 0 and ty == 0) then
dig = " here";
else
if (tx < 0 or tx > 0) then
dig = string.format(" %i %s", math.abs(tx), (tx > 0) and "east" or "west");
end
if (ty < 0 or ty > 0) then
dig = dig .. string.format(" %i %s", math.abs(ty), (ty > 0) and "south" or "north");
end
end
des.engraving({ coord = pos, type = "burn", text = "Dig" .. dig });
end
end
function themeroom_fill(rm) function themeroom_fill(rm)
local pick = 1; local pick = 1;
local total_frequency = 0; local total_frequency = 0;
@@ -706,3 +747,4 @@ function themeroom_fill(rm)
themeroom_fills[pick](rm); themeroom_fills[pick](rm);
end end
end end

View File

@@ -651,6 +651,7 @@ Example:
des.non_passwall(selection); des.non_passwall(selection);
des.non_passwall(); des.non_passwall();
=== object === object
Create an object. The table parameter accepts the following: Create an object. The table parameter accepts the following:
@@ -682,7 +683,7 @@ Create an object. The table parameter accepts the following:
| male | boolean | Is statue male? | male | boolean | Is statue male?
| female | boolean | Is statue female? | female | boolean | Is statue female?
| laid_by_you | boolean | Is an egg laid by you? | laid_by_you | boolean | Is an egg laid by you?
| contents | function | Container contents | contents | function | Container contents. The container object is given as a parameter. See <<Obj>> class.
|=== |===
Example: Example:
@@ -694,7 +695,8 @@ Example:
des.object("scimitar", {6, 7}); des.object("scimitar", {6, 7});
des.object({ class = "%" }); des.object({ class = "%" });
des.object({ id = "boulder", x = 03, y = 12}); des.object({ id = "boulder", x = 03, y = 12});
des.object({ id = "chest", coord = {03, 12}, locked = true, contents = function() des.object("rock"); end }); des.object({ id = "chest", coord = {03, 12}, locked = true, contents = function(obj) des.object("rock"); end });
=== random_corridors === random_corridors

View File

@@ -289,6 +289,13 @@ makerooms(void)
if (themes) { if (themes) {
create_des_coder(); create_des_coder();
iflags.in_lua = g.in_mk_themerooms = TRUE;
g.themeroom_failed = FALSE;
lua_getglobal(themes, "pre_themerooms_generate");
if ( nhl_pcall(themes, 0, 0)){
impossible("Lua error: %s", lua_tostring(themes, -1));
}
iflags.in_lua = g.in_mk_themerooms = FALSE;
} }
/* make rooms until satisfied */ /* make rooms until satisfied */
@@ -321,6 +328,15 @@ makerooms(void)
} }
} }
if (themes) { if (themes) {
reset_xystart_size();
iflags.in_lua = g.in_mk_themerooms = TRUE;
g.themeroom_failed = FALSE;
lua_getglobal(themes, "post_themerooms_generate");
if ( nhl_pcall(themes, 0, 0)){
impossible("Lua error: %s", lua_tostring(themes, -1));
}
iflags.in_lua = g.in_mk_themerooms = FALSE;
wallification(1, 0, COLNO - 1, ROWNO - 1); wallification(1, 0, COLNO - 1, ROWNO - 1);
free(g.coder); free(g.coder);
g.coder = NULL; g.coder = NULL;

View File

@@ -55,7 +55,7 @@ static boolean m_bad_boulder_spot(coordxy, coordxy);
static int pm_to_humidity(struct permonst *); static int pm_to_humidity(struct permonst *);
static unsigned int sp_amask_to_amask(unsigned int sp_amask); static unsigned int sp_amask_to_amask(unsigned int sp_amask);
static void create_monster(monster *, struct mkroom *); static void create_monster(monster *, struct mkroom *);
static void create_object(object *, struct mkroom *); static struct obj *create_object(object *, struct mkroom *);
static void create_altar(altar *, struct mkroom *); static void create_altar(altar *, struct mkroom *);
static boolean search_door(struct mkroom *, coordxy *, coordxy *, xint16, int); static boolean search_door(struct mkroom *, coordxy *, coordxy *, xint16, int);
static void create_corridor(corridor *); static void create_corridor(corridor *);
@@ -2125,7 +2125,7 @@ create_monster(monster* m, struct mkroom* croom)
/* /*
* Create an object in a room. * Create an object in a room.
*/ */
static void static struct obj *
create_object(object* o, struct mkroom* croom) create_object(object* o, struct mkroom* croom)
{ {
struct obj *otmp; struct obj *otmp;
@@ -2267,7 +2267,7 @@ create_object(object* o, struct mkroom* croom)
artifact_exists(otmp, safe_oname(otmp), FALSE, artifact_exists(otmp, safe_oname(otmp), FALSE,
ONAME_NO_FLAGS); /* flags don't matter */ ONAME_NO_FLAGS); /* flags don't matter */
obfree(otmp, NULL); obfree(otmp, NULL);
return; return NULL;
} }
} }
} }
@@ -2359,11 +2359,14 @@ create_object(object* o, struct mkroom* croom)
boolean dealloced; boolean dealloced;
(void) bury_an_obj(otmp, &dealloced); (void) bury_an_obj(otmp, &dealloced);
if (dealloced && container_idx) { if (dealloced) {
container_obj[container_idx - 1] = NULL; if (container_idx)
container_obj[container_idx - 1] = NULL;
otmp = NULL;
} }
} }
} }
return otmp;
} }
/* /*
@@ -3461,6 +3464,7 @@ lspo_object(lua_State *L)
lua_Integer ox = -1, oy = -1; lua_Integer ox = -1, oy = -1;
int argc = lua_gettop(L); int argc = lua_gettop(L);
int maybe_contents = 0; int maybe_contents = 0;
struct obj *otmp = NULL;
create_des_coder(); create_des_coder();
@@ -3601,14 +3605,15 @@ lspo_object(lua_State *L)
} }
do { do {
create_object(&tmpobj, g.coder->croom); otmp = create_object(&tmpobj, g.coder->croom);
quancnt--; quancnt--;
} while ((quancnt > 0) && ((tmpobj.id > STRANGE_OBJECT) } while ((quancnt > 0) && ((tmpobj.id > STRANGE_OBJECT)
&& !objects[tmpobj.id].oc_merge)); && !objects[tmpobj.id].oc_merge));
if (lua_type(L, -1) == LUA_TFUNCTION) { if (lua_type(L, -1) == LUA_TFUNCTION) {
lua_remove(L, -2); lua_remove(L, -2);
if (nhl_pcall(L, 0, 0)){ nhl_push_obj(L, otmp);
if (nhl_pcall(L, 1, 0)){
impossible("Lua error: %s", lua_tostring(L, -1)); impossible("Lua error: %s", lua_tostring(L, -1));
} }
} else } else