Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minetest.generate_biomes and minetest.generate_biome_dust #14260

Closed
wants to merge 3 commits into from

Conversation

gaelysam
Copy link
Contributor

@gaelysam gaelysam commented Jan 14, 2024

Rebase of #11953, fixes #8329, with some additions.

What it does?

This allows to use core biome generator for Lua mapgens, by adding two functions:

  • minetest.generate_biomes(vm, pos1, pos2, nobj_filler_depth) to generate biome nodes in the VoxelManip, and update heatmap, humiditymap and biomemap like a core mapgen would do. A latter minetest.generate_ores or minetest.generate_decorations will take the biomemap into account, so that biome-dependant ores/decos will be placed on the right biome (which was previously not possible in a full Lua mapgen). It must be called during mapgen (register_on_generated callback), and the X/Z extent of pos1-pos2 must match mapgen chunk size, for the buffers to work correctly. It is however possible (and I even recommend it) to offset pos1.y by -1 node to avoid border effects.
  • minetest.generate_biome_dust(vm, pos1, pos2) to generate dust nodes (snow...) above ground and decorations. Nodes above pos2 must be generated, so I also recommend, if the mapgen does not overgenerate 1 node up/down, to offset pos2.y by -1 node.

These functions are available in both server and emerge environments, like most mapgen-related functions.

As stated in the previous PR, they create a temporary MapgenBasic object and call MapgenBasic::generateBiomes() or MapgenBasic::dustTopNodes(). Class definition is modified to allow this.

Why is it interesting/needed?

  • This makes easier to add biomes to a Lua mapgen, without having to reinvent the wheel.
  • Allows compatibility between Lua mapgens and mods acting on the biome system.
  • Faster than Lua-only implementations of the biome system.

I believe Lua Mapgens have a large potential for Minetest, because they are able to adapt the terrain to a game's needs in a way core mapgens can't always offer. But sadly, they are often regarded as second-class mapgens, because of their relative slowness and incompatibility with most mods. They can take months to develop, but rarely end up deployed on servers or used by gamers for anything more than experiments. My PR goes in the direction to change that.

How to use?

I have made a modified version of lvm_example to test this. Do not hesitate to add biome mods (Ethereal, Variety...) to see how it reacts.

To test on emerge environment, I have also made a fork of sfan5's flatgen.

If you want to add it to another mapgen, the full workflow is the following:

vm:set_data(data)
minetest.generate_biomes(vm, {x=minp.x, y=minp.y-1, z=minp.z}, maxp, nobj_filler_depth)
minetest.generate_ores(vm, minp, maxp)
minetest.generate_decorations(vm, minp, maxp)
minetest.generate_biome_dust(vm, minp, {x=maxp.x, y=maxp.y-1, z=maxp.z})
vm:calc_lighting()
vm:write_to_map()
vm:update_liquids()

screenshot_20240114_230500
^ This PR + lvm_example + variety

Status

This PR is ready for review.

@Zughy Zughy added @ Mapgen Feature ✨ PRs that add or enhance a feature Concept approved Approved by a core dev: PRs welcomed! @ Script API labels Jan 14, 2024
@Zughy Zughy added Concept approved Approved by a core dev: PRs welcomed! and removed Concept approved Approved by a core dev: PRs welcomed! labels Jan 21, 2024
If generate_ores/decorations is called after generate_biomes, the biome map
is taken into account.
@gaelysam gaelysam force-pushed the lua_generate_biomes branch from 980178a to 300b57c Compare February 18, 2024 18:16
@gaelysam
Copy link
Contributor Author

I've rebased and adapted my code to mapgen thread feature.
Tested on both server and emerge thread.

For the emerge thread I have forked sfan5's testcase.

@wsor4035
Copy link
Contributor

meta: (assigned due to reviewing the original pr)

biome->depth_filler +
noise_filler_depth->result[index], 0.0f);
biome->depth_filler + (noise_filler_depth ?
noise_filler_depth->result[index] : 0.0f), 0.0f);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the noise_filler_depth ? noise_filler_depth->result[index] : 0.0f does not need to be done more than once and should be moved into a variable in the parent scope

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, will fix that too, thanks

@@ -1557,6 +1560,10 @@ int ModApiMapgen::l_generate_ores(lua_State *L)
mg.vm = checkObject<LuaVoxelManip>(L, 1)->vm;
mg.ndef = emerge->ndef;

auto *bgen = getBiomeGen(L);
if (bgen && bgen->getType() == BIOMEGEN_ORIGINAL)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the type matter here? I don't see any casts to BiomeGenOriginal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to completely understand what BiomeGenOriginal is, so I replicated codes from other places. Is that some "biome system type" that is useless for now but is here because other biome systems could exist? or does it have another use?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a simplified sense the BiomeGen maps positions to biomes, but currently only one class implements this.
Unless you are doing something that is using methods specific to BiomeGenOriginal then there is no reason to perform this manual type check.

{
NO_MAP_LOCK_REQUIRED;

auto *emerge = getEmergeManager(L);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, dead code I forgot to remove, will fix that

@sfan5
Copy link
Collaborator

sfan5 commented Apr 15, 2024

minetest.generate_biome_dust(vm, minp, {x=maxp.x, y=maxp.y-1, z=maxp.z})

why is this Y offset here?

@gaelysam
Copy link
Contributor Author

minetest.generate_biome_dust(vm, minp, {x=maxp.x, y=maxp.y-1, z=maxp.z})

why is this Y offset here?

https://github.com/minetest/minetest/blob/300b57c047b0fec620ef5492626bbd91b23bc32e/src/mapgen/mapgen.cpp#L822-L829
dustTopNodes() may read nodes at node_max.Y + 1 and aborts if ignore, so they must be generated. But it does not dust in this extra row, so I've chosen not to offset automatically, so that it effectively dusts the coordinates you give (provided it is generated above).

Core mapgens do generate one extra row, but AFAIK most Lua mapgens don't do this, so they need to offset 1 node down.

@grorp
Copy link
Member

grorp commented Apr 15, 2024

If you want to add it to another mapgen, the full workflow is the following:

It would be good to also have "the full workflow" documented in lua_api.md

@sfan5
Copy link
Collaborator

sfan5 commented Apr 17, 2024

dustTopNodes() may read nodes at node_max.Y + 1 and aborts if ignore, so they must be generated. But it does not dust in this extra row, so I've chosen not to offset automatically

This is an implementation detail that should IMO not be exposed to Lua. It should automatically subtract the offset.

@sfan5 sfan5 added the Action / change needed Code still needs changes (PR) / more information requested (Issues) label Jun 4, 2024
@Zughy Zughy closed this Aug 12, 2024
@Zughy Zughy added the Adoption needed The pull request needs someone to adopt it. Adoption welcomed! label Aug 12, 2024
@MisterE123
Copy link
Contributor

@gaelysam would it be possible for you to reopen this, with the new rapid release schedule?

@Zughy Zughy removed the Adoption needed The pull request needs someone to adopt it. Adoption welcomed! label Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Action / change needed Code still needs changes (PR) / more information requested (Issues) Concept approved Approved by a core dev: PRs welcomed! Feature ✨ PRs that add or enhance a feature @ Mapgen @ Script API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose MapgenBasic::generateBiomes() to lua
7 participants