Minetest.place schematic on vmanip: Difference between revisions

From Old MT Dev
Jump to navigationJump to search
>ROllerozxa
No edit summary
 
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 17: Line 17:


== Example ==
== Example ==
The below snippet places the acacia tree from default at some terrain position with a random rotation when the data written at the voxelmanip index is dirt with dry grass. It does not do specific or general node replacements. Note that the schematic is offset by -4 in the x and z directions because the acacia tree is 9 by 9 in x and z:
<source>local data = {}
<source>
local data = {}
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
local area = VoxelArea:new({MinEdge = emin, MaxEdge = emax})
local area = VoxelArea:new({MinEdge = emin, MaxEdge = emax})
Line 34: Line 32:
local ivm = area:index(x, minp.y-1, z)
local ivm = area:index(x, minp.y-1, z)
for y = minp.y-1, maxp.y+1 do
for y = minp.y-1, maxp.y+1 do
if data[ivm] == minetest.get_content_id("default:dirt_with_dry_grass") and math.random(0, 500) then
    if data[ivm] == minetest.get_content_id("default:dirt_with_dry_grass") and math.random(0, 500) then
-- We place the tree if there is grass with dirt, but we could also have retrieved the y from the
-- We place the tree if there is grass with dirt, but we could also have retrieved the y from the
-- terrain heightmap in which case we would not necessarily have to loop from minp.y to maxp.y again  
-- terrain heightmap in which case we would not necessarily have to loop from minp.y to maxp.y again  
minetest.place_schematic_on_vmanip(vm, { x = x - 4, y = y, z = z - 4},
minetest.place_schematic_on_vmanip(vm, { x = x - 4, y = y, z = z - 4},
  minetest.get_modpath("default").."/schematics/acacia_tree.mts",
  minetest.get_modpath("default").."/schematics/acacia_tree.mts",
  "random", nil, false)
"random", nil, false)
end
end
end
end
</source>
</source>
<br/>
<br/>
{{Template:Note|If you place schematics while performing the vmanip of e.g. the terrain from negative y to positive y, you may need to separate your terrain generation loop from the schematic placement loop or risk the schematics to be overwritten}}
{{Template:Note|If you place schematics while performing the vmanip of e.g. the terrain from negative y to positive y, you may need to separate your terrain generation loop from the schematic placement loop or risk the schematics to be overwritten}}


[[Category:Methods|p]]
[[Category:Methods|p]]

Latest revision as of 14:49, 5 August 2023

Mbox warning.png This page contains unofficial, low-quality Lua API documentation and is likely to be outdated or wrong. Do not rely on it!
For the official and up-to-date documentation, see Lua API Documentation.
Mbox warning.png This page has been proposed for deletion for the following reason: "Contains unofficial and potentially outdated, redundant and inconsistent Lua API information"
If you don't think that this page should be deleted, please explain why on the talk page.

Syntax

minetest.place_schematic_on_vmanip(vm, pos, schematic, rotation, replacements, force_placement)

Description

This method works in a similar fashion to minetest.place_schematic, but can be used with a VoxelManip for better performance when manipulating large amounts of nodes.

  • Uses VoxelManip object vm, which is the voxel manipulator you are using to e.g. generate terrain
  • Places the schematic specified by schematic (see: Schematic specifier) at pos.
  • Rotation can be "0", "90", "180", "270", or "random".
  • If the rotation parameter is omitted, the schematic is not rotated.
  • replacements = {["old_name"] = "convert_to", ...}
  • force_placement is a boolean indicating whether nodes other than air and ignore are replaced by the schematic


Note.png
Note: Schematics are volumes and the position where you place the schematic is the corner of the schematic. If for example you have a 5 by 5 by 5 schematic of a tree whose trunk is in the middle, you will need to offset the x and z position by -2. If you don't, you risk misplacing the tree causing them to float in the air or be generated inside blocks on e.g. steep slopes.


Example

local data = {}
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
local area = VoxelArea:new({MinEdge = emin, MaxEdge = emax})
for z = minp.z, maxp.z do
	for x = minp.x, maxp.x do
		for y = minp.y-1, maxp.y+1 do
-- E.g. terrain generation code here
		end
	end
end
-- Decoration
for z = minp.z, maxp.z do
	for x = minp.x, maxp.x do
		local ivm = area:index(x, minp.y-1, z)
		for y = minp.y-1, maxp.y+1 do
		    if data[ivm] == minetest.get_content_id("default:dirt_with_dry_grass") and math.random(0, 500) then
			-- We place the tree if there is grass with dirt, but we could also have retrieved the y from the
			-- terrain heightmap in which case we would not necessarily have to loop from minp.y to maxp.y again 
			minetest.place_schematic_on_vmanip(vm, { x = x - 4, y = y, z = z - 4},
								   minetest.get_modpath("default").."/schematics/acacia_tree.mts",
"random", nil, false)


Note.png
Note: If you place schematics while performing the vmanip of e.g. the terrain from negative y to positive y, you may need to separate your terrain generation loop from the schematic placement loop or risk the schematics to be overwritten