From 762b2d7838777973a21d7a0ba785b73713de8517 Mon Sep 17 00:00:00 2001 From: mckaygerhard Date: Mon, 29 Apr 2024 09:53:28 -0400 Subject: [PATCH] fix perlin noise returns null on some rarte cases as reported in minetest * i was first checked at https://forum.minetest.net/viewtopic.php?t=8157 as "minetest.get_perlin() and minetest.get_perlin_map() Return nil" by prestidigitator The `minetest.get_perlin()` function returns nil since 0.4.8+ until 5.1.0 The `PerlinNoise()` constructor does return a valid object, on the other hand, barely documented as alternatrive cos is internally used only and not api public cos can only call it after the environment is created, so the code must bne inside `minetest.after(0, ...)` it only works the first time, or must be used an alternative * was reported also https://github.com/minetest/minetest/issues/10385 that does not properly handle large world seeds * Close https://codeberg.org/Wuzzy/minetest_tsm_pyramids/issues/2 * Close https://codeberg.org/minetest-stuffs/minetest-mod-tsm_pyramids/issues/2 * Also uses older get2d vs get_2d for newer engines older pre 5.0.0 as seen on https://github.com/minetest-LOTR/Lord-of-the-Test/issues/139 detect newer engine and uses proper method to get agains perlin object Closes https://codeberg.org/minetest-stuffs/minetest-mod-tsm_pyramids/issues/2 --- init.lua | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/init.lua b/init.lua index 6ae0f76..05ebfb8 100644 --- a/init.lua +++ b/init.lua @@ -38,7 +38,7 @@ local is_50 = minetest.has_feature("object_use_texture_alpha") tsm_pyramids = {} tsm_pyramids.is_50 = is_50 tsm_pyramids.S = S -tsm_pyramids.perlin1 -- perlin noise buffer, make it global cos we need to acess in 5.0 after load all the rest of mods +tsm_pyramids.perlin1 = nil -- perlin noise buffer, make it global cos we need to acess in 5.0 after load all the rest of mods dofile(minetest.get_modpath(modpath).."/mummy.lua") dofile(minetest.get_modpath(modpath).."/nodes.lua") @@ -270,9 +270,15 @@ local function make(pos, brick, sandstone, stone, sand, ptype, room_id) end local perl1 --- if mg v6 set this {SEED1 = 9130, OCTA1 = 3, PERS1 = 0.5, SCAL1 = 250} -- Values should match minetest mapgen V6 desert noise. +-- if mg v6 set this {SEED1 = 9130, OCTA1 = 3, PERS1 = 0.5, SCAL1 = 250} -- Values should match minetest mapgen V6 desert noise. perl1 = { SEED1 = 9130, OCTA1 = 1, PERS1 = 0.5, SCAL1 = 25 } -- Values should match minetest mapgen V7 desert noise. +if tsm_pyramids.is_50 then + tsm_pyramids.perlin1 = minetest.get_perlin(perl1.SEED1, perl1.OCTA1, perl1.PERS1, perl1.SCAL1) +else + tsm_pyramids.perlin1 = PerlinNoise(perl1.SEED1, perl1.OCTA1, perl1.PERS1, perl1.SCAL1) +end + local function hlp_fnct(pos, name) local n = minetest.get_node_or_nil(pos) if n and n.name and n.name == name then @@ -281,6 +287,7 @@ local function hlp_fnct(pos, name) return false end end + local function ground(pos, old) local p2 = table.copy(pos) while hlp_fnct(p2, "air") do @@ -361,12 +368,18 @@ minetest.register_on_generated(function(minp, maxp, seed) return pos end - if tsm_pyramids.perlin1 == nil then - tsm_pyramids.perlin1 = minetest.get_perlin(perl1.SEED1, perl1.OCTA1, perl1.PERS1, perl1.SCAL1) + local noise1 = nil + if not tsm_pyramids.perlin1 or tsm_pyramids.perlin1 == nil then + if tsm_pyramids.is_50 then + tsm_pyramids.perlin1 = minetest.get_perlin(perl1.SEED1, perl1.OCTA1, perl1.PERS1, perl1.SCAL1) + noise1 = tsm_pyramids.perlin1:get_2d({x=minp.x,y=minp.y}) + else + tsm_pyramids.perlin1 = PerlinNoise(perl1.SEED1, perl1.OCTA1, perl1.PERS1, perl1.SCAL1) + noise1 = tsm_pyramids.perlin1:get2d({x=minp.x,y=minp.y}) + end end - local noise1 = tsm_pyramids.perlin1:get_2d({x=minp.x,y=minp.y}) - if noise1 == nil then - minetest.log("warning", "[tsm_pyramids] canot get 2d noise from perlin buffer---------------------------- ") + + if not tsm_pyramids.perlin1 or tsm_pyramids.perlin1 == nil or not noise1 or noise1 == nil then return end