Currently, the only way to add blocks in Lua is with the minetest.env:add_node() function. Using it to spawn a lot of blocks at once is very laggy and even unsafe. I assume it's because it sends a lot of signals to create, notify and store a lot of blocks in a short amount of time... which the code can't properly group and doesn't have the time to handle.
My suggested alternative is allowing a list of blocks to be sent. The reason why trees and landscapes generated by the mapgen are so fast is that spawning a lot of blocks simultaneously is a dozen times faster in C++, given they can be organized and grouped accordingly while the hard work is done once for all nodes rather than per-node. With such a function, adding structures from Lua wouldn't require spamming the code with nodes, but sending a single list which the engine can queue and manage optimally.
- Code: Select all
# Example of the current way (slow and with chances of failure):
minetest.env:add_node(pos1, { name = "dirt_with_grass" })
minetest.env:add_node(pos2, { name = "cobblestone" })
# Example of the suggested way (quicker and safer):
group = { }
table.insert(group, { pos1, { name = "dirt_with_grass" } })
table.insert(group, { pos2, { name = "cobblestone" } })
minetest.env:add_node_group(group)