From b6055c76fb497b9fec3eb6904b445e7b70e3c44b Mon Sep 17 00:00:00 2001 From: Bram van den Heuvel Date: Sun, 15 Sep 2024 22:26:54 +0200 Subject: [PATCH] Allow both main thread & mapgen env execution --- API.md | 19 ++++++++++++++++++- README.md | 22 +++++----------------- init.lua | 25 +++++++++++++++++++++++++ script.lua | 23 +++++++++++++++++++++-- 4 files changed, 69 insertions(+), 20 deletions(-) diff --git a/API.md b/API.md index e8d3fd7..1b40b69 100644 --- a/API.md +++ b/API.md @@ -3,10 +3,27 @@ This mod is built in a way to be easy to change the underground world. The API is written in a way to be very similar to the Minetest API. +## Main thread vs. separate thread + +This mod can both run on the main thread or on the mapgen thread. Generally, +the mapgen thread is recommended, but it isn't supported for Minetest versions +5.8 and under. The main thread works for everyone but it is slower. + +This mod automatically determines whether a Minetest user is able to use the +faster mapgen thread. As a result, you should use the function +`ns_cavegen_init.register_cavegen_script`, with the input pointing to a file +containing the script for the cave generator. This mod will ensure that your +script is run on the appropriate thread. + +If you wish to decide for yourself which thread to run the cave generator on, +you can use the `ns_cavegen_mapgen_environment` option in your settings file +(usually `minetest.conf`) to tell the cave generator whether to run the cave +generator in the separate thread or not. + ## Shapes Underground caves have varying shapes, and the variable -`noordstar.registered_shapes` contains all those definitions. +`ns_cavegen.registered_shapes` contains all those definitions. For shapes, the following functions are available: diff --git a/README.md b/README.md index 643a7ac..8d28896 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,14 @@ -# Noordstar caves +# Noordstar cave generator -The Noordstar caves mod helps add more caves to MineClone2 and Mineclonia. +The Noordstar caves mod helps add more caves to Minetest games. For reference, see the [API documentation](API.md). This mod offers no new features to the player, but it allows programmers to add custom caves into the world and to lower the world depth. -## Tested & supported on +## Examples -The mod has been tested on the following games: +The following mods are known to use this cave generator mod: -- [x] VoxeLibre -- [ ] Mineclonia -- [ ] MTG - -The mod has been tested on the following mapgens: - -- [x] v7 -- [ ] valleys -- [ ] carpathian -- [ ] v5 -- [ ] flat -- [ ] fractal -- [ ] singlenode +- [ns_vl_caves](https://content.minetest.net/packages/Noordstar/ns_vl_caves/) for VoxeLibre & Mineclonia diff --git a/init.lua b/init.lua index fba26a7..1d04f09 100644 --- a/init.lua +++ b/init.lua @@ -1,3 +1,28 @@ +ns_cavegen_init = {} + +-- Depending on the Minetest version, mapgen can be executed on the main thread +-- or on the separate thread. + +-- Optionally, you can override this value. +local execute_in_mapgen_environment = false + +function ns_cavegen_init.register_cavegen_script(file_name) + -- As of Minetest 5.9.0, a Minetest server can crash without notice when + -- a script is executed in the mapgen environment. This is likely related + -- to a memory leak error: + -- + -- If you would like to override this setting, you can adjust the + -- "ns_cavegen_mapgen_environment" variable in your `minetest.conf` settings + -- file. + local use_mapgen_env = minetest.settings:get_bool("ns_cavegen_mapgen_environment", false) + + if execute_in_mapgen_environment or use_mapgen_env then + minetest.register_mapgen_script(file_name) + else + dofile(file_name) + end +end + minetest.register_mapgen_script( minetest.get_modpath(minetest.get_current_modname()) .. "/script.lua" ) \ No newline at end of file diff --git a/script.lua b/script.lua index 67f9151..c5f5660 100644 --- a/script.lua +++ b/script.lua @@ -33,6 +33,20 @@ local WORLD_DEPTH = -60 local internal = {} +function internal.init() + local uses_mapgen_env = minetest.save_gen_notify + + if uses_mapgen_env then + minetest.register_on_generated(internal.mapgen) + else + minetest.register_on_generated(function(minp, maxp, blockseed) + local vm = minetest.get_mapgen_object("voxelmanip") + internal.mapgen(vm, minp, maxp, blockseed) + vm:write_to_map() + end) + end +end + ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- -------------------------------- INTERNAL API --------------------------------- @@ -653,7 +667,12 @@ function internal.write_classified_node(vm_data, va, used_biomes, classified_nod if ground_content_nodes[vm_node] == nil then local name = minetest.get_name_from_content_id(content_id) - ground_content_nodes[vm_node] = minetest.registered_nodes[name].is_ground_content or true + + if minetest.registered_nodes[name] == nil then + ground_content_nodes[vm_node] = true + else + ground_content_nodes[vm_node] = minetest.registered_nodes[name].is_ground_content or true + end end if ground_content_nodes[vm_node] then @@ -887,7 +906,6 @@ end ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- -minetest.register_on_generated(internal.mapgen) ns_cavegen = { cave_vastness = function(pos) @@ -923,4 +941,5 @@ ns_cavegen = { unregister_shape = internal.unregister_shape, } +internal.init() -- dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/lua/register.lua") \ No newline at end of file