summaryrefslogtreecommitdiff
path: root/struct_register.lua
blob: 1143753ee8f877c705b7e58c01438dd48153a745 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
-- struct_register.lua - registration callbacks for structures.

--[[
The cellworld table contains the following structure-related entries:
cellworld = {
	structures = {
		[st_name] = {...} -- all registered structures.
	}
	structure_set = {
		-- the active structure set config, which determines grid size, mapgen base node a.s.o as passed to config_structure_set()
		name = "foo" -- name of the active structure set (which is actually the first parameter), not specified in table.
		grid_size = 7 -- how many nodes form a cell.
		mapgen_base_node = "air" -- which node to place by default in map, passed to singlenode mg. TODO not implemented yet.
	}
	-- the most important settings from structure set are copied for easier access
	grid_size = structure_set.grid_size,
	
	-- of all registered structures, the maximum extents in x, y and z direction.
	maximum_structure_size = vector.new(1,1,1)
}
]]--

cellworld.structures = {}

function cellworld.config_structure_set(name, definition)
	if cellworld.structure_set then
		error("Cellworld: Trying to register structure set '"..name.."', but '"..cellworld.structure_set.name..
			"' has already been registered. There can only be one active structure set at a time.")
	end
	cellworld.structure_set = {
		name = name,
		grid_size = 9,
		seed_structure = definition.seed_structure,
	}
	if definition.grid_size then
		cellworld.structure_set.grid_size = definition.grid_size
	end
	
	cellworld.grid_size = cellworld.structure_set.grid_size
	
	cellworld.maximum_structure_size = vector.new(1,1,1)
end

function cellworld.register_structure(name, def)
	-- make definition checks
	if not cellworld.structure_set then
		error("Cellworld: can not register structures when config_structure_set() hasn't been called!")
	elseif def.structure_set and def.structure_set ~= cellworld.structure_set.name then
		error("Cellworld: Structure '"..name.."' can not be registered because the structure set it was defined for ('"..name..
			"') doesn't match the active structure set ('"..cellworld.structure_set.name.."')!")
	end
	
	if not def.chance then
		def.chance = 50
	end
	
	-- TODO validate structure of cells
	
	cellworld.structures[name] = def
	
	-- update maxstructsize
	local mss = cellworld.maximum_structure_size
	mss.x = math.max(def.size.x, mss.x)
	mss.y = math.max(def.size.y, mss.y)
	mss.z = math.max(def.size.z, mss.z)
	
end