minetest.place schematic on vmanip

From Minetest Developer Wiki
Revision as of 13:00, 13 August 2017 by Beerholder (talk | contribs)
Jump to navigation Jump to search

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

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:

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)
			end
		end
	end
end


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


See also

minetest.place_schematic