From adb42fa2704343d0a55285ac78df2a456ed1398e Mon Sep 17 00:00:00 2001 From: evrooije Date: Tue, 7 Aug 2018 01:01:05 +0200 Subject: [PATCH] Source code reorganization. Global noises are now in a separate file in order to make the core a bit more readable --- multi_map_core/core.lua | 118 +------------------------------ multi_map_core/global_noises.lua | 112 +++++++++++++++++++++++++++++ multi_map_core/init.lua | 1 + multi_map_generators/init.lua | 2 +- 4 files changed, 117 insertions(+), 116 deletions(-) create mode 100644 multi_map_core/global_noises.lua diff --git a/multi_map_core/core.lua b/multi_map_core/core.lua index b5b30b1..78528a4 100644 --- a/multi_map_core/core.lua +++ b/multi_map_core/core.lua @@ -118,120 +118,7 @@ function multi_map.get_absolute_y(y, layer) end end -local last_used_layer = -1 - --- Mmmmh, might make these local ... -multi_map.global_2d_maps = {} -multi_map.global_2d_params = {} -multi_map.global_2d_map_arrays = {} - -local map_cache = {} - --- Register a named map for use as 2D map. A 3D map could potentially be --- registered using this as well, but since this is specialized for --- multiple different seeds per layer, multi_map provides this mechanism. --- Note: The seed provided in the params is used to seed math.random --- and each layer gets a randomized seed after that. As such, the noise --- is still predictable/ reproducible --- name = the name of the map --- params = the noise parameters as per minetest standard -function multi_map.register_global_2dmap(name, params) - if multi_map.global_3d_params[name] or multi_map.global_2d_params[name] then - minetest.log("error", "[multi_map] Trying to register map "..name..", but it already exists. Aborting registration.") - return - end - math.randomseed(params.seed) - multi_map.global_2d_params[name] = {} - multi_map.global_2d_map_arrays[name] = {} - - for i = 0, multi_map.number_of_layers -1 do - local new_params = { - offset = params.offset, - scale = params.scale, - spread = {x=params.spread.x, y=params.spread.y, z=params.spread.z}, - seed = math.random(-1000000000000, 1000000000000), - octaves = params.octaves, - persist = params.persist, - lacunarity = params.lacunarity, - flags = params.flags, - } - multi_map.global_2d_params[name][i] = new_params - end -end - --- Get the named 2D map as flat array --- name = name of the noise map --- chulenxz = chunk length in 2 dimensions (xz) --- minposxz = minimum 2D position (xz) --- current layer = the layer for which to retrieve the map or nil to use multi_map's current layer -function multi_map.get_global_2dmap_flat(name, chulenxz, minposxz, layer) - if not multi_map.global_2d_map_arrays[name] then - minetest.log("error", "[multi_map] Trying to get an unregistered global 2D map") - end - - if multi_map.wrap_layers then - if layer then - map_cache[name] = multi_map.get_mixed_2dnoise_flat(name, chulenxz, minposxz, layer) - else - map_cache[name] = multi_map.get_mixed_2dnoise_flat(name, chulenxz, minposxz, multi_map.current_layer) - end - end - - if not map_cache[name] then - if not layer then - if multi_map.current_layer ~= last_used_layer then - multi_map.global_2d_maps[name] = minetest.get_perlin_map(multi_map.global_2d_params[name][multi_map.current_layer], chulenxz) - end - map_cache[name] = multi_map.global_2d_maps[name]:get2dMap_flat(minposxz, multi_map.global_2d_map_arrays[name][multi_map.current_layer]) - else - if layer ~= last_used_layer then - multi_map.global_2d_maps[name] = minetest.get_perlin_map(multi_map.global_2d_params[name][layer], chulenxz) - end - map_cache[name] = multi_map.global_2d_maps[name]:get2dMap_flat(minposxz, multi_map.global_2d_map_arrays[name][layer]) - end - end - - return map_cache[name] -end - --- Mmmmh, might make these local ... -multi_map.global_3d_maps = {} -multi_map.global_3d_params = {} -multi_map.global_3d_map_arrays = {} - --- Register a named map for use as 3D map. It is separated from the 2D case as that one --- retruires a layer to be specified in order to get differently seeded noise maps per --- layer. For 3D this is not an issue as the abslute y can be used to retrieve different --- values from layer to layer. Hence the 3D case is separate --- name = the name of the map --- params = the noise parameters as per minetest standard -function multi_map.register_global_3dmap(name, params) - if multi_map.global_3d_params[name] or multi_map.global_2d_params[name] then - minetest.log("error", "[multi_map] Trying to register map "..name..", but it already exists. Aborting registration.") - return - end - multi_map.global_3d_params[name] = params - multi_map.global_3d_map_arrays[name] = {} -end - --- Get the named 2D map as flat array --- name = name of the noise map --- chulenxyz = chunk length in 3 dimensions (xyz) --- minposxyz = minimum 3D position (xyz) -function multi_map.get_global_3dmap_flat(name, chulenxyz, minposxyz) - if not multi_map.global_3d_map_arrays[name] then - minetest.log("error", "[multi_map] Trying to get an unregistered global 3D map") - end - - if not map_cache[name] then - if not multi_map.global_3d_maps[name] then - multi_map.global_3d_maps[name] = minetest.get_perlin_map(multi_map.global_3d_params[name], chulenxyz) - end - map_cache[name] = multi_map.global_3d_maps[name]:get3dMap_flat(minposxyz, multi_map.global_3d_map_arrays[name]) - end - - return map_cache[name] -end +multi_map.last_used_layer = -1 -- Register a fallback generator, which is called in case not suitable generators are found for a layer -- name = the optional name for this generator, e.g. to be used for display on the HUD @@ -524,5 +411,6 @@ minetest.register_on_generated(function(minp, maxp) end - map_cache = {} + multi_map.last_used_layer = multi_map.current_layer + multi_map.map_cache = {} end) diff --git a/multi_map_core/global_noises.lua b/multi_map_core/global_noises.lua new file mode 100644 index 0000000..5902613 --- /dev/null +++ b/multi_map_core/global_noises.lua @@ -0,0 +1,112 @@ +-- Mmmmh, might make these local ... +multi_map.global_2d_maps = {} +multi_map.global_2d_params = {} +multi_map.global_2d_map_arrays = {} + +multi_map.map_cache = {} + +-- Register a named map for use as 2D map. A 3D map could potentially be +-- registered using this as well, but since this is specialized for +-- multiple different seeds per layer, multi_map provides this mechanism. +-- Note: The seed provided in the params is used to seed math.random +-- and each layer gets a randomized seed after that. As such, the noise +-- is still predictable/ reproducible +-- name = the name of the map +-- params = the noise parameters as per minetest standard +function multi_map.register_global_2dmap(name, params) + if multi_map.global_3d_params[name] or multi_map.global_2d_params[name] then + minetest.log("error", "[multi_map] Trying to register map "..name..", but it already exists. Aborting registration.") + return + end + math.randomseed(params.seed) + multi_map.global_2d_params[name] = {} + multi_map.global_2d_map_arrays[name] = {} + + for i = 0, multi_map.number_of_layers -1 do + local new_params = { + offset = params.offset, + scale = params.scale, + spread = {x=params.spread.x, y=params.spread.y, z=params.spread.z}, + seed = math.random(-1000000000000, 1000000000000), + octaves = params.octaves, + persist = params.persist, + lacunarity = params.lacunarity, + flags = params.flags, + } + multi_map.global_2d_params[name][i] = new_params + end +end + +-- Get the named 2D map as flat array +-- name = name of the noise map +-- chulenxz = chunk length in 2 dimensions (xz) +-- minposxz = minimum 2D position (xz) +-- current layer = the layer for which to retrieve the map or nil to use multi_map's current layer +function multi_map.get_global_2dmap_flat(name, chulenxz, minposxz, layer) + if not multi_map.global_2d_map_arrays[name] then + minetest.log("error", "[multi_map] Trying to get an unregistered global 2D map") + end + + if multi_map.wrap_layers then + if layer then + multi_map.map_cache[name] = multi_map.get_mixed_2dnoise_flat(name, chulenxz, minposxz, layer) + else + multi_map.map_cache[name] = multi_map.get_mixed_2dnoise_flat(name, chulenxz, minposxz, multi_map.current_layer) + end + end + + if not multi_map.map_cache[name] then + if not layer then + if multi_map.current_layer ~= multi_map.last_used_layer then + multi_map.global_2d_maps[name] = minetest.get_perlin_map(multi_map.global_2d_params[name][multi_map.current_layer], chulenxz) + end + multi_map.map_cache[name] = multi_map.global_2d_maps[name]:get2dMap_flat(minposxz, multi_map.global_2d_map_arrays[name][multi_map.current_layer]) + else + if layer ~= multi_map.last_used_layer then + multi_map.global_2d_maps[name] = minetest.get_perlin_map(multi_map.global_2d_params[name][layer], chulenxz) + end + multi_map.map_cache[name] = multi_map.global_2d_maps[name]:get2dMap_flat(minposxz, multi_map.global_2d_map_arrays[name][layer]) + end + end + + return multi_map.map_cache[name] +end + +-- Mmmmh, might make these local ... +multi_map.global_3d_maps = {} +multi_map.global_3d_params = {} +multi_map.global_3d_map_arrays = {} + +-- Register a named map for use as 3D map. It is separated from the 2D case as that one +-- retruires a layer to be specified in order to get differently seeded noise maps per +-- layer. For 3D this is not an issue as the abslute y can be used to retrieve different +-- values from layer to layer. Hence the 3D case is separate +-- name = the name of the map +-- params = the noise parameters as per minetest standard +function multi_map.register_global_3dmap(name, params) + if multi_map.global_3d_params[name] or multi_map.global_2d_params[name] then + minetest.log("error", "[multi_map] Trying to register map "..name..", but it already exists. Aborting registration.") + return + end + multi_map.global_3d_params[name] = params + multi_map.global_3d_map_arrays[name] = {} +end + +-- Get the named 2D map as flat array +-- name = name of the noise map +-- chulenxyz = chunk length in 3 dimensions (xyz) +-- minposxyz = minimum 3D position (xyz) +function multi_map.get_global_3dmap_flat(name, chulenxyz, minposxyz) + if not multi_map.global_3d_map_arrays[name] then + minetest.log("error", "[multi_map] Trying to get an unregistered global 3D map") + end + + if not multi_map.map_cache[name] then + if not multi_map.global_3d_maps[name] then + multi_map.global_3d_maps[name] = minetest.get_perlin_map(multi_map.global_3d_params[name], chulenxyz) + end + multi_map.map_cache[name] = multi_map.global_3d_maps[name]:get3dMap_flat(minposxyz, multi_map.global_3d_map_arrays[name]) + end + + return multi_map.map_cache[name] +end diff --git a/multi_map_core/init.lua b/multi_map_core/init.lua index 2af67b9..ecbf3e5 100644 --- a/multi_map_core/init.lua +++ b/multi_map_core/init.lua @@ -10,6 +10,7 @@ local multi_map_core_path = minetest.get_modpath("multi_map_core") -- The various sourced files contain the remainder of the API, the -- settings, node definitions and other core/ helper functionality dofile(multi_map_core_path.."/core.lua") +dofile(multi_map_core_path.."/global_noises.lua") dofile(multi_map_core_path.."/noise_mixer.lua") dofile(multi_map_core_path.."/debug.lua") dofile(multi_map_core_path.."/nodes.lua") diff --git a/multi_map_generators/init.lua b/multi_map_generators/init.lua index 926d921..db30ee3 100644 --- a/multi_map_generators/init.lua +++ b/multi_map_generators/init.lua @@ -29,7 +29,7 @@ multi_map.register_fallback_generator(mmgen_simple.generate) multi_map.register_fallback_generator("Default Levels", mmgen_levels.generate) multi_map.register_generator(18, mmgen_levels.generate) -multi_map.register_generator(19, mmgen_levels.generate) +multi_map.register_generator(19, mmgen_testauri.generate) multi_map.register_generator(20, mmgen_levels.generate) multi_map.set_layer_params(19, { name = "Lowlands Layer" }) multi_map.set_layer_params(19, { name = "Central Layer" })