summaryrefslogtreecommitdiff
path: root/src/mg_decoration.h
blob: fd6e89e798bb94d43204ef139454f6c4b07ed479 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
Minetest
Copyright (C) 2014-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
Copyright (C) 2015-2017 paramat

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#pragma once

#include <unordered_set>
#include "objdef.h"
#include "noise.h"
#include "nodedef.h"

class Mapgen;
class MMVManip;
class PcgRandom;
class Schematic;

enum DecorationType {
	DECO_SIMPLE,
	DECO_SCHEMATIC,
	DECO_LSYSTEM
};

#define DECO_PLACE_CENTER_X  0x01
#define DECO_PLACE_CENTER_Y  0x02
#define DECO_PLACE_CENTER_Z  0x04
#define DECO_USE_NOISE       0x08
#define DECO_FORCE_PLACEMENT 0x10
#define DECO_LIQUID_SURFACE  0x20

extern FlagDesc flagdesc_deco[];


#if 0
struct CutoffData {
	VoxelArea a;
	Decoration *deco;
	//v3s16 p;
	//v3s16 size;
	//s16 height;

	CutoffData(s16 x, s16 y, s16 z, s16 h) {
		p = v3s16(x, y, z);
		height = h;
	}
};
#endif

class Decoration : public ObjDef, public NodeResolver {
public:
	Decoration() = default;
	virtual ~Decoration() = default;

	virtual void resolveNodeNames();

	bool canPlaceDecoration(MMVManip *vm, v3s16 p);
	size_t placeDeco(Mapgen *mg, u32 blockseed,
		v3s16 nmin, v3s16 nmax, s16 deco_zero_level);
	//size_t placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);

	virtual size_t generate(MMVManip *vm, PcgRandom *pr, v3s16 p) = 0;
	virtual int getHeight() = 0;

	u32 flags = 0;
	int mapseed = 0;
	std::vector<content_t> c_place_on;
	s16 sidelen = 1;
	s16 y_min;
	s16 y_max;
	float fill_ratio = 0.0f;
	NoiseParams np;
	std::vector<content_t> c_spawnby;
	s16 nspawnby;

	std::unordered_set<u8> biomes;
};

class DecoSimple : public Decoration {
public:
	virtual void resolveNodeNames();
	virtual size_t generate(MMVManip *vm, PcgRandom *pr, v3s16 p);
	virtual int getHeight();

	std::vector<content_t> c_decos;
	s16 deco_height;
	s16 deco_height_max;
	u8 deco_param2;
};

class DecoSchematic : public Decoration {
public:
	DecoSchematic() = default;

	virtual size_t generate(MMVManip *vm, PcgRandom *pr, v3s16 p);
	virtual int getHeight();

	Rotation rotation;
	Schematic *schematic = nullptr;
};


/*
class DecoLSystem : public Decoration {
public:
	virtual void generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
};
*/

class DecorationManager : public ObjDefManager {
public:
	DecorationManager(IGameDef *gamedef);
	virtual ~DecorationManager() = default;

	const char *getObjectTitle() const
	{
		return "decoration";
	}

	static Decoration *create(DecorationType type)
	{
		switch (type) {
		case DECO_SIMPLE:
			return new DecoSimple;
		case DECO_SCHEMATIC:
			return new DecoSchematic;
		//case DECO_LSYSTEM:
		//	return new DecoLSystem;
		default:
			return NULL;
		}
	}

	size_t placeAllDecos(Mapgen *mg, u32 blockseed,
		v3s16 nmin, v3s16 nmax, s16 deco_zero_level = 0);
};
ss="hl opt">[name]) end } setmetatable(core.registered_items, alias_metatable) setmetatable(core.registered_nodes, alias_metatable) setmetatable(core.registered_craftitems, alias_metatable) setmetatable(core.registered_tools, alias_metatable) -- These item names may not be used because they would interfere -- with legacy itemstrings local forbidden_item_names = { MaterialItem = true, MaterialItem2 = true, MaterialItem3 = true, NodeItem = true, node = true, CraftItem = true, craft = true, MBOItem = true, ToolItem = true, tool = true, } local function check_modname_prefix(name) if name:sub(1,1) == ":" then -- Escape the modname prefix enforcement mechanism return name:sub(2) else -- Modname prefix enforcement local expected_prefix = core.get_current_modname() .. ":" if name:sub(1, #expected_prefix) ~= expected_prefix then error("Name " .. name .. " does not follow naming conventions: " .. "\"modname:\" or \":\" prefix required") end local subname = name:sub(#expected_prefix+1) if subname:find("[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]") then error("Name " .. name .. " does not follow naming conventions: " .. "contains unallowed characters") end return name end end function core.register_abm(spec) -- Add to core.registered_abms core.registered_abms[#core.registered_abms+1] = spec end function core.register_entity(name, prototype) -- Check name if name == nil then error("Unable to register entity: Name is nil") end name = check_modname_prefix(tostring(name)) prototype.name = name prototype.__index = prototype -- so that it can be used as a metatable -- Add to core.registered_entities core.registered_entities[name] = prototype end function core.register_item(name, itemdef) -- Check name if name == nil then error("Unable to register item: Name is nil") end name = check_modname_prefix(tostring(name)) if forbidden_item_names[name] then error("Unable to register item: Name is forbidden: " .. name) end itemdef.name = name -- Apply defaults and add to registered_* table if itemdef.type == "node" then -- Use the nodebox as selection box if it's not set manually if itemdef.drawtype == "nodebox" and not itemdef.selection_box then itemdef.selection_box = itemdef.node_box elseif itemdef.drawtype == "fencelike" and not itemdef.selection_box then itemdef.selection_box = { type = "fixed", fixed = {-1/8, -1/2, -1/8, 1/8, 1/2, 1/8}, } end setmetatable(itemdef, {__index = core.nodedef_default}) core.registered_nodes[itemdef.name] = itemdef elseif itemdef.type == "craft" then setmetatable(itemdef, {__index = core.craftitemdef_default}) core.registered_craftitems[itemdef.name] = itemdef elseif itemdef.type == "tool" then setmetatable(itemdef, {__index = core.tooldef_default}) core.registered_tools[itemdef.name] = itemdef elseif itemdef.type == "none" then setmetatable(itemdef, {__index = core.noneitemdef_default}) else error("Unable to register item: Type is invalid: " .. dump(itemdef)) end -- Flowing liquid uses param2 if itemdef.type == "node" and itemdef.liquidtype == "flowing" then itemdef.paramtype2 = "flowingliquid" end -- BEGIN Legacy stuff if itemdef.cookresult_itemstring ~= nil and itemdef.cookresult_itemstring ~= "" then core.register_craft({ type="cooking", output=itemdef.cookresult_itemstring, recipe=itemdef.name, cooktime=itemdef.furnace_cooktime }) end if itemdef.furnace_burntime ~= nil and itemdef.furnace_burntime >= 0 then core.register_craft({ type="fuel", recipe=itemdef.name, burntime=itemdef.furnace_burntime }) end -- END Legacy stuff -- Disable all further modifications getmetatable(itemdef).__newindex = {} --core.log("Registering item: " .. itemdef.name) core.registered_items[itemdef.name] = itemdef core.registered_aliases[itemdef.name] = nil register_item_raw(itemdef) end function core.register_node(name, nodedef) nodedef.type = "node" core.register_item(name, nodedef) end function core.register_craftitem(name, craftitemdef) craftitemdef.type = "craft" -- BEGIN Legacy stuff if craftitemdef.inventory_image == nil and craftitemdef.image ~= nil then craftitemdef.inventory_image = craftitemdef.image end -- END Legacy stuff core.register_item(name, craftitemdef) end function core.register_tool(name, tooldef) tooldef.type = "tool" tooldef.stack_max = 1 -- BEGIN Legacy stuff if tooldef.inventory_image == nil and tooldef.image ~= nil then tooldef.inventory_image = tooldef.image end if tooldef.tool_capabilities == nil and (tooldef.full_punch_interval ~= nil or tooldef.basetime ~= nil or tooldef.dt_weight ~= nil or tooldef.dt_crackiness ~= nil or tooldef.dt_crumbliness ~= nil or tooldef.dt_cuttability ~= nil or tooldef.basedurability ~= nil or tooldef.dd_weight ~= nil or tooldef.dd_crackiness ~= nil or tooldef.dd_crumbliness ~= nil or tooldef.dd_cuttability ~= nil) then tooldef.tool_capabilities = { full_punch_interval = tooldef.full_punch_interval, basetime = tooldef.basetime, dt_weight = tooldef.dt_weight, dt_crackiness = tooldef.dt_crackiness, dt_crumbliness = tooldef.dt_crumbliness, dt_cuttability = tooldef.dt_cuttability, basedurability = tooldef.basedurability, dd_weight = tooldef.dd_weight, dd_crackiness = tooldef.dd_crackiness, dd_crumbliness = tooldef.dd_crumbliness, dd_cuttability = tooldef.dd_cuttability, } end -- END Legacy stuff core.register_item(name, tooldef) end function core.register_alias(name, convert_to) if forbidden_item_names[name] then error("Unable to register alias: Name is forbidden: " .. name) end if core.registered_items[name] ~= nil then core.log("WARNING: Not registering alias, item with same name" .. " is already defined: " .. name .. " -> " .. convert_to) else --core.log("Registering alias: " .. name .. " -> " .. convert_to) core.registered_aliases[name] = convert_to register_alias_raw(name, convert_to) end end function core.on_craft(itemstack, player, old_craft_list, craft_inv) for _, func in ipairs(core.registered_on_crafts) do itemstack = func(itemstack, player, old_craft_list, craft_inv) or itemstack end return itemstack end function core.craft_predict(itemstack, player, old_craft_list, craft_inv) for _, func in ipairs(core.registered_craft_predicts) do itemstack = func(itemstack, player, old_craft_list, craft_inv) or itemstack end return itemstack end -- Alias the forbidden item names to "" so they can't be -- created via itemstrings (e.g. /give) local name for name in pairs(forbidden_item_names) do