Skip to content

Commit 5203801

Browse files
authored
Make ShiftExistingBiomes available via API (minetest-mods#40)
* bug fixes Fixes wrong new_y_min when shift_existing_biomes() shifts a biome below the nether floor, find_surface_anchorPos() no longer assumes y=0 will be outside the nether. Nil-reference fixed when a mods tries to register a portal after mods are finished loading, but the portal shape+material was already registered by another mod. * Make ShiftExistingBiomes available via API makes the ShiftExistingBiomes function available to other mods via the nether global, since it's not a simple function and biomes would also need to be shifted if another mod wants to add a second nether layer. * Allow layers to extend the depth of nether effects Mods can set/lower nether.DEPTH_FLOOR_LAYERS when creating a layer under the nether. This allows multiple layer mods to know where their ceiling should start, and to be included in the effects which only happen in the nether. * document nether API More of a tentative interop guide than an API. Use snake_case for API functions.
1 parent 54613d6 commit 5203801

8 files changed

+122
-30
lines changed

init.lua

+19-6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ if nether.DEPTH_FLOOR + 1000 > nether.DEPTH_CEILING then
7676
end
7777
nether.DEPTH = nether.DEPTH_CEILING -- Deprecated, use nether.DEPTH_CEILING instead.
7878

79+
-- DEPTH_FLOOR_LAYERS gives the bottom Y of all locations that wish to be
80+
-- considered part of the Nether.
81+
-- DEPTH_FLOOR_LAYERS Allows mods to insert extra layers below the
82+
-- Nether, by knowing where their layer ceiling should start, and letting
83+
-- the layers be included in effects which only happen in the Nether.
84+
-- If a mod wishes to add a layer below the Nether it should read
85+
-- nether.DEPTH_FLOOR_LAYERS to find the bottom Y of the Nether and any
86+
-- other layers already under the Nether. The mod should leave a small gap
87+
-- between DEPTH_FLOOR_LAYERS and its ceiling (e.g. use DEPTH_FLOOR_LAYERS - 6
88+
-- for its ceiling Y, so there is room to shift edge-case biomes), then set
89+
-- nether.DEPTH_FLOOR_LAYERS to reflect the mod's floor Y value, and call
90+
-- shift_existing_biomes() with DEPTH_FLOOR_LAYERS as the floor_y argument.
91+
nether.DEPTH_FLOOR_LAYERS = nether.DEPTH_FLOOR
7992

8093
-- A debug-print function that understands vectors etc. and does not
8194
-- evaluate when debugging is turned off.
@@ -163,7 +176,7 @@ The expedition parties have found no diamonds or gold, and after an experienced
163176
local destination_pos = vector.divide(surface_anchorPos, nether.FASTTRAVEL_FACTOR)
164177
destination_pos.x = math.floor(0.5 + destination_pos.x) -- round to int
165178
destination_pos.z = math.floor(0.5 + destination_pos.z) -- round to int
166-
destination_pos.y = nether.DEPTH_CEILING - 1000 -- temp value so find_nearest_working_portal() returns nether portals
179+
destination_pos.y = nether.DEPTH_CEILING - 1 -- temp value so find_nearest_working_portal() returns nether portals
167180

168181
-- a y_factor of 0 makes the search ignore the altitude of the portals (as long as they are in the Nether)
169182
local existing_portal_location, existing_portal_orientation =
@@ -188,7 +201,7 @@ The expedition parties have found no diamonds or gold, and after an experienced
188201
local destination_pos = vector.multiply(realm_anchorPos, nether.FASTTRAVEL_FACTOR)
189202
destination_pos.x = math.min(30900, math.max(-30900, destination_pos.x)) -- clip to world boundary
190203
destination_pos.z = math.min(30900, math.max(-30900, destination_pos.z)) -- clip to world boundary
191-
destination_pos.y = 0 -- temp value so find_nearest_working_portal() doesn't return nether portals
204+
destination_pos.y = nether.DEPTH_CEILING + 1 -- temp value so find_nearest_working_portal() doesn't return nether portals
192205

193206
-- a y_factor of 0 makes the search ignore the altitude of the portals (as long as they are outside the Nether)
194207
local existing_portal_location, existing_portal_orientation =
@@ -253,12 +266,12 @@ The expedition parties have found no diamonds or gold, and after an experienced
253266
if pos.y <= nether.DEPTH_CEILING and pos.y >= nether.DEPTH_FLOOR then
254267
result = "nether"
255268

256-
-- since mapgen_nobiomes.lua has no regions it doesn't implement getRegion(),
257-
-- so only use getRegion() if it exists
258-
if nether.mapgen.getRegion ~= nil then
269+
-- since mapgen_nobiomes.lua has no regions it doesn't implement get_region(),
270+
-- so only use get_region() if it exists
271+
if nether.mapgen.get_region ~= nil then
259272
-- the biomes-based mapgen supports 2 extra regions
260273
local regions = nether.mapgen.RegionEnum
261-
local region = nether.mapgen.getRegion(pos)
274+
local region = nether.mapgen.get_region(pos)
262275
if region == regions.CENTER or region == regions.CENTERSHELL then
263276
result = "mantle"
264277
elseif region == regions.NEGATIVE or region == regions.NEGATIVESHELL then

mapgen.lua

+13-12
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ local math_max, math_min, math_abs, math_floor = math.max, math.min, math.abs, m
8181

8282
-- Inject nether_caverns biome
8383

84-
local function override_underground_biomes()
84+
-- Move any existing biomes out of the y-range specified by 'floor_y' and 'ceiling_y'
85+
mapgen.shift_existing_biomes = function(floor_y, ceiling_y)
8586
-- https://forum.minetest.net/viewtopic.php?p=257522#p257522
8687
-- Q: Is there a way to override an already-registered biome so I can get it out of the
8788
-- way of my own underground biomes without disturbing the other biomes registered by
@@ -124,21 +125,21 @@ local function override_underground_biomes()
124125
if type(new_biome_def.y_min) == 'number' then biome_y_min = new_biome_def.y_min end
125126
if type(new_biome_def.y_max) == 'number' then biome_y_max = new_biome_def.y_max end
126127

127-
if biome_y_max > NETHER_FLOOR and biome_y_min < NETHER_CEILING then
128+
if biome_y_max > floor_y and biome_y_min < ceiling_y then
128129
-- This biome occupies some or all of the depth of the Nether, shift/crop it.
129130
local new_y_min, new_y_max
130-
local spaceOccupiedAbove = biome_y_max - NETHER_CEILING
131-
local spaceOccupiedBelow = NETHER_FLOOR - biome_y_min
131+
local spaceOccupiedAbove = biome_y_max - ceiling_y
132+
local spaceOccupiedBelow = floor_y - biome_y_min
132133
if spaceOccupiedAbove >= spaceOccupiedBelow or biome_y_min <= -30000 then
133134
-- place the biome above the Nether
134135
-- We also shift biomes which extend to the bottom of the map above the Nether, since they
135136
-- likely only extend that deep as a catch-all, and probably have a role nearer the surface.
136-
new_y_min = NETHER_CEILING + 1
137-
new_y_max = math_max(biome_y_max, NETHER_CEILING + 2)
137+
new_y_min = ceiling_y + 1
138+
new_y_max = math_max(biome_y_max, ceiling_y + 2)
138139
else
139140
-- shift the biome to below the Nether
140-
new_y_max = NETHER_FLOOR - 1
141-
new_y_min = math_min(biome_y_min, NETHER_CEILING - 2)
141+
new_y_max = floor_y - 1
142+
new_y_min = math_min(biome_y_min, floor_y - 2)
142143
end
143144

144145
debugf("Moving biome \"%s\" from %s..%s to %s..%s", new_biome_def.name, new_biome_def.y_min, new_biome_def.y_max, new_y_min, new_y_max)
@@ -162,7 +163,7 @@ local function override_underground_biomes()
162163
end
163164

164165
-- Shift any overlapping biomes out of the way before we create the Nether biomes
165-
override_underground_biomes()
166+
mapgen.shift_existing_biomes(NETHER_FLOOR, NETHER_CEILING)
166167

167168
-- nether:native_mapgen is used to prevent ores and decorations being generated according
168169
-- to landforms created by the native mapgen.
@@ -259,12 +260,12 @@ mapgen.np_cave = {
259260

260261
local cavePointPerlin = nil
261262

262-
mapgen.getCavePointPerlin = function()
263+
mapgen.get_cave_point_perlin = function()
263264
cavePointPerlin = cavePointPerlin or minetest.get_perlin(mapgen.np_cave)
264265
return cavePointPerlin
265266
end
266267

267-
mapgen.getCavePerlinAt = function(pos)
268+
mapgen.get_cave_perlin_at = function(pos)
268269
cavePointPerlin = cavePointPerlin or minetest.get_perlin(mapgen.np_cave)
269270
return cavePointPerlin:get_3d(pos)
270271
end
@@ -477,7 +478,7 @@ end
477478
-- use knowledge of the nether mapgen algorithm to return a suitable ground level for placing a portal.
478479
-- player_name is optional, allowing a player to spawn a remote portal in their own protected areas.
479480
function nether.find_nether_ground_y(target_x, target_z, start_y, player_name)
480-
local nobj_cave_point = mapgen.getCavePointPerlin()
481+
local nobj_cave_point = mapgen.get_cave_point_perlin()
481482
local air = 0 -- Consecutive air nodes found
482483

483484
local minp_schem, maxp_schem = nether.get_schematic_volume({x = target_x, y = 0, z = target_z}, nil, "nether_portal")

mapgen_mantle.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ mapgen.add_basalt_columns = function(data, area, minp, maxp)
119119
local yStride = area.ystride
120120
local yCaveStride = x1 - x0 + 1
121121

122-
local cavePerlin = mapgen.getCavePointPerlin()
122+
local cavePerlin = mapgen.get_cave_point_perlin()
123123
nobj_basalt = nobj_basalt or minetest.get_perlin_map(np_basalt, {x = yCaveStride, y = yCaveStride})
124124
local nvals_basalt = nobj_basalt:get_2d_map_flat({x=minp.x, y=minp.z}, {x=yCaveStride, y=yCaveStride}, nbuf_basalt)
125125

@@ -431,13 +431,13 @@ mapgen.RegionEnum = {
431431

432432
-- Returns (region, noise) where region is a value from mapgen.RegionEnum
433433
-- and noise is the unadjusted cave perlin value
434-
mapgen.getRegion = function(pos)
434+
mapgen.get_region = function(pos)
435435

436436
if pos.y > nether.DEPTH_CEILING or pos.y < nether.DEPTH_FLOOR then
437437
return mapgen.RegionEnum.OVERWORLD, nil
438438
end
439439

440-
local caveNoise = mapgen.getCavePerlinAt(pos)
440+
local caveNoise = mapgen.get_cave_perlin_at(pos)
441441
local sealevel, cavern_limit_distance = mapgen.find_nearest_lava_sealevel(pos.y)
442442
local tcave_adj, centerRegionLimit_adj = mapgen.get_mapgenblend_adjustments(pos.y)
443443
local tcave = mapgen.TCAVE + tcave_adj
@@ -482,7 +482,7 @@ minetest.register_chatcommand("nether_whereami",
482482
if player == nil then return false, S("Unknown player position") end
483483
local playerPos = vector.round(player:get_pos())
484484

485-
local region, caveNoise = mapgen.getRegion(playerPos)
485+
local region, caveNoise = mapgen.get_region(playerPos)
486486
local seaLevel, cavernLimitDistance = mapgen.find_nearest_lava_sealevel(playerPos.y)
487487
local tcave_adj, centerRegionLimit_adj = mapgen.get_mapgenblend_adjustments(playerPos.y)
488488

nether_api.txt

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
Modding/interop guide to Nether
2+
===============================
3+
4+
For portals API see portal_api.txt
5+
6+
The Nether mod exposes some of its functions and data via the lua global
7+
`nether` and `nether.mapgen`
8+
9+
10+
* `nether.DEPTH_CEILING`: [read-only] Y value of the top of the Nether.
11+
12+
* `nether.DEPTH_FLOOR`: [read-only] Y value of the bottom of the Nether.
13+
14+
* `nether.DEPTH_FLOOR_LAYERS`: [writable] Gives the bottom Y of all
15+
locations that wish to be considered part of the Nether.
16+
DEPTH_FLOOR_LAYERS Allows mods to insert extra layers below the
17+
Nether, by knowing where their layer ceiling should start, and letting
18+
the layers be included in effects which only happen in the Nether.
19+
If a mod wishes to add a layer below the Nether it should read
20+
`nether.DEPTH_FLOOR_LAYERS` to find the bottom Y of the Nether and any
21+
other layers already under the Nether. The mod should leave a small gap
22+
between DEPTH_FLOOR_LAYERS and its ceiling (e.g. use DEPTH_FLOOR_LAYERS - 6
23+
for its ceiling Y, so there is room to shift edge-case biomes), then set
24+
`nether.DEPTH_FLOOR_LAYERS` to reflect the mod's floor Y value, and call
25+
`shift_existing_biomes()` with DEPTH_FLOOR_LAYERS as the `floor_y` argument.
26+
27+
* `nether.NETHER_REALM_ENABLED`: [read-only] Gets the value of the "Enable
28+
Nether realm & portal" setting the nether mod exposes in Minetest's
29+
"All Settings" -> "Mods" -> "nether" options.
30+
When false, the entire nether mapgen is disabled (not run), and the portal
31+
to it is not registered. Reasons someone might disable the Nether realm
32+
include if a nether-layer mod was to be used as the Nether instead, or if
33+
the portal mechanic was desired in a game without the Nether, etc.
34+
35+
* `nether.useBiomes`: [read-only] When this is false, the Nether interop
36+
functions below are not available (nil).
37+
Indicates that the biomes-enabled mapgen is in use. The Nether mod falls back
38+
to older mapgen code for v6 maps and old versions of Minetest, the older
39+
mapgen code doesn't use biomes and doesn't provide API/interop functions.
40+
41+
42+
Mapgen functions available when nether.useBiomes is true
43+
--------------------------------------------------------
44+
45+
The following functions are nil if `nether.useBiomes` is false,
46+
and also nil if `nether.NETHER_REALM_ENABLED` is false.
47+
48+
* `nether.mapgen.shift_existing_biomes(floor_y, ceiling_y)` Move any existing
49+
biomes out of the y-range specified by `floor_y` and `ceiling_y`.
50+
51+
* `nether.mapgen.get_region(pos)`: Returns two values, (region, noise) where
52+
`region` is a value from `nether.mapgen.RegionEnum` and `noise` is the
53+
unadjusted cave perlin value.
54+
* `nether.mapgen.RegionEnum` values are tables which contain an invariant
55+
`name` and a localized `desc`. Current region names include overworld,
56+
positive, positive shell, center, center shell, negative, and negative
57+
shell.
58+
"positive" corresponds to conventional Nether caverns, and "center"
59+
corresponds to the Mantle region.
60+
61+
* `nether.mapgen.get_cave_point_perlin()`: Returns the PerlinNoise object for
62+
the Nether's cavern noise.
63+
64+
* `nether.mapgen.get_cave_perlin_at(pos)`: Returns the Nether cavern noise
65+
value at a given 3D position.
66+
67+
68+
Other mapgen functions
69+
-------------------------------------------
70+
71+
If the Nether realm is enabled, then this function will be available
72+
regardless of whether `nether.useBiomes` is true:
73+
74+
* `nether.find_nether_ground_y(target_x, target_z, start_y, player_name)`
75+
Uses knowledge of the nether mapgen algorithm to return a suitable ground
76+
level for placing a portal.
77+
* `player_name` is optional, allowing a player to spawn a remote portal
78+
in their own protected areas.

nodes.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ nether.cool_lava = function(pos, node)
337337
-- Evaporate water sitting above lava, if it's in the Nether.
338338
-- (we don't want Nether mod to affect overworld lava mechanics)
339339
if minetest.get_item_group(node_above.name, "water") > 0 and
340-
pos.y < nether.DEPTH_CEILING and pos.y > nether.DEPTH_FLOOR then
340+
pos.y < nether.DEPTH_CEILING and pos.y > nether.DEPTH_FLOOR_LAYERS then
341341
-- cools_lava might be a better group to check for, but perhaps there's
342342
-- something in that group that isn't a liquid and shouldn't be evaporated?
343343
minetest.swap_node(pos_above, {name="air"})
@@ -604,7 +604,7 @@ local function fumarole_onTimer(pos, elapsed)
604604

605605
-- Fumaroles in the Nether can catch fire.
606606
-- (if taken to the surface and used as cottage chimneys, they don't catch fire)
607-
local inNether = pos.y <= nether.DEPTH and pos.y >= nether.DEPTH_FLOOR
607+
local inNether = pos.y <= nether.DEPTH and pos.y >= nether.DEPTH_FLOOR_LAYERS
608608
local canCatchFire = inNether and minetest.registered_nodes["fire:permanent_flame"] ~= nil
609609
local smoke_offset = 0
610610
local timeout_factor = 1

portal_api.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -2073,7 +2073,7 @@ function nether.register_portal(name, portaldef)
20732073
end
20742074

20752075
portaldef.name = name
2076-
portaldef.mod_name = minetest.get_current_modname()
2076+
portaldef.mod_name = minetest.get_current_modname() or "<mod name not recorded>"
20772077

20782078
-- use portaldef_default for any values missing from portaldef or portaldef.sounds
20792079
if portaldef.sounds ~= nil then setmetatable(portaldef.sounds, {__index = portaldef_default.sounds}) end

portal_api.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,16 @@ Used by `nether.register_portal`.
250250
-- player_name may be "", e.g. if the portal was ignited by a mesecon,
251251
-- and is provided for use with volume_is_natural_and_unprotected() etc.
252252

253-
on_run_wormhole = function(portalDef, anochorPos, orientation),
253+
on_run_wormhole = function(portalDef, anchorPos, orientation),
254254
-- invoked once per second per portal
255-
on_extinguish = function(portalDef, anochorPos, orientation),
255+
on_extinguish = function(portalDef, anchorPos, orientation),
256256
-- invoked when a portal is extinguished, including when the portal
257257
-- it connected to was extinguished.
258258
on_player_teleported = function(portalDef, player, oldPos, newPos),
259259
-- invoked immediately after a player is teleported
260-
on_ignite = function(portalDef, anochorPos, orientation)
260+
on_ignite = function(portalDef, anchorPos, orientation)
261261
-- invoked when a player or mesecon ignites a portal
262-
on_created = function(portalDef, anochorPos, orientation)
262+
on_created = function(portalDef, anchorPos, orientation)
263263
-- invoked when a portal creates a remote twin, this is usually when
264264
-- a player travels through a portal for the first time.
265265
}

portal_examples.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ end
106106
-- Surface-travel portal, playable code example --
107107
--==============================================--
108108

109-
-- These Moore Curve functions requred by surface_portal's find_surface_anchorPos() will
109+
-- These Moore Curve functions required by surface_portal's find_surface_anchorPos() will
110110
-- be assigned later in this file.
111111
local get_moore_distance -- will be function get_moore_distance(cell_count, x, y): integer
112112
local get_moore_coords -- will be function get_moore_coords(cell_count, distance): pos2d

0 commit comments

Comments
 (0)