Helltweaks: improve lava river

The lava river will now draw another river, until a certain
amount of map locations have been turned into lava, so you don't
get a teensy "river" made out of 2 lava pools.

Add a lua selection method to count the number of locations
in the selection.
This commit is contained in:
Pasi Kallinen
2023-01-30 17:10:39 +02:00
parent c7464e8aa5
commit 744d64487b
4 changed files with 91 additions and 15 deletions
+30 -15
View File
@@ -57,6 +57,7 @@ end
function hell_tweaks(protected_area) function hell_tweaks(protected_area)
local liquid = "L"; local liquid = "L";
local ground = "."; local ground = ".";
local n_prot = protected_area:numpoints();
local prot = protected_area:negate(); local prot = protected_area:negate();
-- random pools -- random pools
@@ -82,24 +83,38 @@ function hell_tweaks(protected_area)
-- river -- river
if (percent(50)) then if (percent(50)) then
local floor = selection.match(ground); local allrivers = selection.new();
local a = selection.rndcoord(floor); local reqpts = ((nhc.COLNO * nhc.ROWNO) - n_prot) / 12; -- # of lava pools required
local b = selection.rndcoord(floor); local rpts = 0;
local lavariver = selection.randline(selection.new(), a.x, a.y, b.x, b.y, 10); local rivertries = 0;
if (percent(50)) then repeat
lavariver = selection.grow(lavariver, "north"); local floor = selection.match(ground);
end local a = selection.rndcoord(floor);
if (percent(50)) then local b = selection.rndcoord(floor);
lavariver = selection.grow(lavariver, "west"); local lavariver = selection.randline(selection.new(), a.x, a.y, b.x, b.y, 10);
end
if (percent(25)) then if (percent(50)) then
local riverbanks = selection.grow(lavariver); lavariver = selection.grow(lavariver, "north");
end
if (percent(50)) then
lavariver = selection.grow(lavariver, "west");
end
allrivers = allrivers | lavariver;
allrivers = allrivers & prot;
rpts = allrivers:numpoints();
rivertries = rivertries + 1;
until ((rpts > reqpts) or (rivertries > 7));
if (percent(60)) then
local prc = 10 * math.random(1, 6);
local riverbanks = selection.grow(allrivers);
riverbanks = riverbanks & prot; riverbanks = riverbanks & prot;
des.terrain(selection.percentage(riverbanks, 50), ground); des.terrain(selection.percentage(riverbanks, prc), ground);
end end
lavariver = lavariver & prot;
des.terrain(lavariver, liquid); des.terrain(allrivers, liquid);
end end
-- replacing some walls with boulders -- replacing some walls with boulders
+9
View File
@@ -1153,6 +1153,15 @@ Example:
local s = selection.negate(); local s = selection.negate();
=== numpoints
Return the number of points in the selection.
Example:
local n = sel:numpoints();
=== percentage === percentage
Each selected location has a percentage chance of being selected in the new selection. Each selected location has a percentage chance of being selected in the new selection.
+23
View File
@@ -12,6 +12,7 @@ static struct selectionvar *l_selection_push_new(lua_State *);
/* lua_CFunction prototypes */ /* lua_CFunction prototypes */
static int l_selection_new(lua_State *); static int l_selection_new(lua_State *);
static int l_selection_clone(lua_State *); static int l_selection_clone(lua_State *);
static int l_selection_numpoints(lua_State *);
static int l_selection_getpoint(lua_State *); static int l_selection_getpoint(lua_State *);
static int l_selection_setpoint(lua_State *); static int l_selection_setpoint(lua_State *);
static int l_selection_filter_percent(lua_State *); static int l_selection_filter_percent(lua_State *);
@@ -195,6 +196,27 @@ l_selection_setpoint(lua_State *L)
return 1; return 1;
} }
/* local numpoints = selection.numpoints(sel); */
static int
l_selection_numpoints(lua_State *L)
{
struct selectionvar *sel = l_selection_check(L, 1);
coordxy x, y;
int ret = 0;
NhRect rect;
selection_getbounds(sel, &rect);
for (x = rect.lx; x <= rect.hx; x++)
for (y = rect.ly; y <= rect.hy; y++)
if (selection_getpoint(x, y, sel))
ret++;
lua_settop(L, 0);
lua_pushinteger(L, ret);
return 1;
}
/* local value = selection.get(sel, x, y); */ /* local value = selection.get(sel, x, y); */
static int static int
l_selection_getpoint(lua_State *L) l_selection_getpoint(lua_State *L)
@@ -911,6 +933,7 @@ static const struct luaL_Reg l_selection_methods[] = {
{ "clone", l_selection_clone }, { "clone", l_selection_clone },
{ "get", l_selection_getpoint }, { "get", l_selection_getpoint },
{ "set", l_selection_setpoint }, { "set", l_selection_setpoint },
{ "numpoints", l_selection_numpoints },
{ "negate", l_selection_not }, { "negate", l_selection_not },
{ "percentage", l_selection_filter_percent }, { "percentage", l_selection_filter_percent },
{ "rndcoord", l_selection_rndcoord }, { "rndcoord", l_selection_rndcoord },
+29
View File
@@ -495,6 +495,34 @@ function test_sel_map()
sel_are_equal(sela, selb, __func__); sel_are_equal(sela, selb, __func__);
end end
function test_sel_numpoints()
local __func__ = "test_sel_numpoints";
des.reset_level();
des.level_init({ style = "solidfill", fg = " " });
local sela = selection.new();
local npts = sela:numpoints();
if (npts ~= 0) then
error(string.format("numpoints reported %i, should have been 0", npts));
end
des.terrain(5,5, ".");
local selb = selection.match(".");
local npts = selb:numpoints();
if (npts ~= 1) then
error(string.format("numpoints reported %i, should have been 1", npts));
end
des.terrain(6,5, ".");
local selc = selection.match(".");
local npts = selc:numpoints();
if (npts ~= 2) then
error(string.format("numpoints reported %i, should have been 2", npts));
end
end
nh.debug_flags({mongen = false, hunger = false, overwrite_stairs = true }); nh.debug_flags({mongen = false, hunger = false, overwrite_stairs = true });
test_selection_params(); test_selection_params();
test_sel_negate(); test_sel_negate();
@@ -515,3 +543,4 @@ test_sel_match();
test_sel_iterate(); test_sel_iterate();
test_sel_bounds(); test_sel_bounds();
test_sel_map(); test_sel_map();
test_sel_numpoints();