aboutsummaryrefslogtreecommitdiff
path: root/src/emerge.h
blob: cf067714562bfe4dbff71f63bddb2ca4b142e101 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
Minetest
Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>

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.
*/

#ifndef EMERGE_HEADER
#define EMERGE_HEADER

#include <map>
#include "irr_v3d.h"
#include "util/container.h"
#include "mapgen.h" // for MapgenParams
#include "map.h"

#define BLOCK_EMERGE_ALLOW_GEN   (1 << 0)
#define BLOCK_EMERGE_FORCE_QUEUE (1 << 1)

#define EMERGE_DBG_OUT(x) do {                         \
	if (enable_mapgen_debug_info)                      \
		infostream << "EmergeThread: " x << std::endl; \
} while (0)

class EmergeThread;
class INodeDefManager;
class Settings;

class BiomeManager;
class OreManager;
class DecorationManager;
class SchematicManager;

// Structure containing inputs/outputs for chunk generation
struct BlockMakeData {
	MMVManip *vmanip;
	u64 seed;
	v3s16 blockpos_min;
	v3s16 blockpos_max;
	v3s16 blockpos_requested;
	UniqueQueue<v3s16> transforming_liquid;
	INodeDefManager *nodedef;

	BlockMakeData():
		vmanip(NULL),
		seed(0),
		nodedef(NULL)
	{}

	~BlockMakeData() { delete vmanip; }
};

// Result from processing an item on the emerge queue
enum EmergeAction {
	EMERGE_CANCELLED,
	EMERGE_ERRORED,
	EMERGE_FROM_MEMORY,
	EMERGE_FROM_DISK,
	EMERGE_GENERATED,
};

// Callback
typedef void (*EmergeCompletionCallback)(
	v3s16 blockpos, EmergeAction action, void *param);

typedef std::vector<
	std::pair<
		EmergeCompletionCallback,
		void *
	>
> EmergeCallbackList;

struct BlockEmergeData {
	u16 peer_requested;
	u16 flags;
	EmergeCallbackList callbacks;
};

class EmergeManager {
public:
	INodeDefManager *ndef;
	bool enable_mapgen_debug_info;

	// Generation Notify
	u32 gen_notify_on;
	std::set<u32> gen_notify_on_deco_ids;

	// Parameters passed to mapgens owned by ServerMap
	// TODO(hmmmm): Remove this after mapgen helper methods using them
	// are moved to ServerMap
	MapgenParams *mgparams;

	// Hackish workaround:
	// For now, EmergeManager must hold onto a ptr to the Map's setting manager
	// since the Map can only be accessed through the Environment, and the
	// Environment is not created until after script initialization.
	MapSettingsManager *map_settings_mgr;

	// Managers of various map generation-related components
	BiomeManager *biomemgr;
	OreManager *oremgr;
	DecorationManager *decomgr;
	SchematicManager *schemmgr;

	// Methods
	EmergeManager(IGameDef *gamedef);
	~EmergeManager();

	bool initMapgens(MapgenParams *mgparams);

	void startThreads();
	void stopThreads();
	bool isRunning();

	bool enqueueBlockEmerge(
		u16 peer_id,
		v3s16 blockpos,
		bool allow_generate,
		bool ignore_queue_limits=false);

	bool enqueueBlockEmergeEx(
		v3s16 blockpos,
		u16 peer_id,
		u16 flags,
		EmergeCompletionCallback callback,
		void *callback_param);

	v3s16 getContainingChunk(v3s16 blockpos);

	Mapgen *getCurrentMapgen();

	// Mapgen helpers methods
	Biome *getBiomeAtPoint(v3s16 p);
	int getSpawnLevelAtPoint(v2s16 p);
	int getGroundLevelAtPoint(v2s16 p);
	bool isBlockUnderground(v3s16 blockpos);

	static v3s16 getContainingChunk(v3s16 blockpos, s16 chunksize);

private:
	std::vector<Mapgen *> m_mapgens;
	std::vector<EmergeThread *> m_threads;
	bool m_threads_active;

	Mutex m_queue_mutex;
	std::map<v3s16, BlockEmergeData> m_blocks_enqueued;
	std::map<u16, u16> m_peer_queue_count;

	u16 m_qlimit_total;
	u16 m_qlimit_diskonly;
	u16 m_qlimit_generate;

	// Requires m_queue_mutex held
	EmergeThread *getOptimalThread();

	bool pushBlockEmergeData(
		v3s16 pos,
		u16 peer_requested,
		u16 flags,
		EmergeCompletionCallback callback,
		void *callback_param,
		bool *entry_already_exists);

	bool popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata);

	friend class EmergeThread;

	DISABLE_CLASS_COPY(EmergeManager);
};

#endif
ro", "_14"}, {"panel", "_14"}, {"micro", "_15"}, {"panel", "_15"}, {"stair", "_outer"}, {"stair", ""}, {"stair", "_inner"}, {"slab", "_1"}, {"slab", "_2"}, {"slab", "_quarter"}, {"slab", ""}, {"slab", "_three_quarter"}, {"slab", "_14"}, {"slab", "_15"}, {"stair", "_half"}, {"stair", "_alt_1"}, {"stair", "_alt_2"}, {"stair", "_alt_4"}, {"stair", "_alt"}, {"slope", ""}, {"slope", "_half"}, {"slope", "_half_raised"}, {"slope", "_inner"}, {"slope", "_inner_half"}, {"slope", "_inner_half_raised"}, {"slope", "_outer"}, {"slope", "_outer_half"}, {"slope", "_outer_half_raised"}, {"slope", "_outer_cut"}, {"slope", "_outer_cut_half"}, } function circular_saw:get_cost(inv, stackname) for i, item in pairs(inv:get_list("output")) do if item:get_name() == stackname then return circular_saw.cost_in_microblocks[i] end end end function circular_saw:get_output_inv(modname, material, amount, max) if (not max or max < 1 or max > 99) then max = 99 end local list = {} local pos = #list -- If there is nothing inside, display empty inventory: if amount < 1 then return list end for i = 1, #circular_saw.names do local t = circular_saw.names[i] local cost = circular_saw.cost_in_microblocks[i] local balance = math.min(math.floor(amount/cost), max) pos = pos + 1 list[pos] = modname .. ":" .. t[1] .. "_" .. material .. t[2] .. " " .. balance end return list end -- Reset empty circular_saw after last full block has been taken out -- (or the circular_saw has been placed the first time) -- Note: max_offered is not reset: function circular_saw:reset(pos) local meta = minetest.get_meta(pos) local inv = meta:get_inventory() inv:set_list("input", {}) inv:set_list("micro", {}) inv:set_list("output", {}) meta:set_int("anz", 0) meta:set_string("infotext", S("Circular Saw is empty (owned by %s)") :format(meta:get_string("owner") or "")) end -- Player has taken something out of the box or placed something inside -- that amounts to count microblocks: function circular_saw:update_inventory(pos, amount) local meta = minetest.get_meta(pos) local inv = meta:get_inventory() amount = meta:get_int("anz") + amount -- The material is recycled automaticly. inv:set_list("recycle", {}) if amount < 1 then -- If the last block is taken out. self:reset(pos) return end local stack = inv:get_stack("input", 1) -- At least one "normal" block is necessary to see what kind of stairs are requested. if stack:is_empty() then -- Any microblocks not taken out yet are now lost. -- (covers material loss in the machine) self:reset(pos) return end local node_name = stack:get_name() or "" local name_parts = circular_saw.known_nodes[node_name] or "" local modname = name_parts[1] or "" local material = name_parts[2] or "" inv:set_list("input", { -- Display as many full blocks as possible: node_name.. " " .. math.floor(amount / 8) }) -- The stairnodes made of default nodes use moreblocks namespace, other mods keep own: if modname == "default" then modname = "moreblocks" end -- print("circular_saw set to " .. modname .. " : " -- .. material .. " with " .. (amount) .. " microblocks.") -- 0-7 microblocks may remain left-over: inv:set_list("micro", { modname .. ":micro_" .. material .. "_bottom " .. (amount % 8) }) -- Display: inv:set_list("output", self:get_output_inv(modname, material, amount, meta:get_int("max_offered"))) -- Store how many microblocks are available: meta:set_int("anz", amount) meta:set_string("infotext", S("Circular Saw is working on %s (owned by %s)") :format(material, meta:get_string("owner") or "")) end -- The amount of items offered per shape can be configured: function circular_saw.on_receive_fields(pos, formname, fields, sender) local meta = minetest.get_meta(pos) local max = tonumber(fields.max_offered) if max and max > 0 then meta:set_string("max_offered", max) -- Update to show the correct number of items: circular_saw:update_inventory(pos, 0) end end -- Moving the inventory of the circular_saw around is not allowed because it -- is a fictional inventory. Moving inventory around would be rather -- impractical and make things more difficult to calculate: function circular_saw.allow_metadata_inventory_move( pos, from_list, from_index, to_list, to_index, count, player) return 0 end -- Only input- and recycle-slot are intended as input slots: function circular_saw.allow_metadata_inventory_put( pos, listname, index, stack, player) -- The player is not allowed to put something in there: if listname == "output" or listname == "micro" then return 0 end local meta = minetest.get_meta(pos) local inv = meta:get_inventory() local stackname = stack:get_name() local count = stack:get_count() -- Only alow those items that are offered in the output inventory to be recycled: if listname == "recycle" then if not inv:contains_item("output", stackname) then return 0 end local stackmax = stack:get_stack_max() local instack = inv:get_stack("input", 1) local microstack = inv:get_stack("micro", 1) local incount = instack:get_count() local incost = (incount * 8) + microstack:get_count() local maxcost = (stackmax * 8) + 7 local cost = circular_saw:get_cost(inv, stackname) if (incost + cost) > maxcost then return math.max((maxcost - incost) / cost, 0) end return count end -- Only accept certain blocks as input which are known to be craftable into stairs: if listname == "input" then if not inv:is_empty("input") then if inv:get_stack("input", index):get_name() ~= stackname then return 0 end end if not inv:is_empty("micro") then local microstackname = inv:get_stack("micro", 1):get_name():gsub("^.+:micro_", "", 1) local cutstackname = stackname:gsub("^.+:", "", 1) if microstackname ~= cutstackname then return 0 end end for name, t in pairs(circular_saw.known_nodes) do if name == stackname and inv:room_for_item("input", stack) then return count end end return 0 end end -- Taking is allowed from all slots (even the internal microblock slot). -- Putting something in is slightly more complicated than taking anything -- because we have to make sure it is of a suitable material: function circular_saw.on_metadata_inventory_put( pos, listname, index, stack, player) -- We need to find out if the circular_saw is already set to a -- specific material or not: local meta = minetest.get_meta(pos) local inv = meta:get_inventory() local stackname = stack:get_name() local count = stack:get_count() -- Putting something into the input slot is only possible if that had -- been empty before or did contain something of the same material: if listname == "input" then -- Each new block is worth 8 microblocks: circular_saw:update_inventory(pos, 8 * count) elseif listname == "recycle" then -- Lets look which shape this represents: local cost = circular_saw:get_cost(inv, stackname) circular_saw:update_inventory(pos, cost * count) end end function circular_saw.on_metadata_inventory_take( pos, listname, index, stack, player) -- If it is one of the offered stairs: find out how many -- microblocks have to be substracted: if listname == "output" then -- We do know how much each block at each position costs: local cost = circular_saw.cost_in_microblocks[index] * stack:get_count() circular_saw:update_inventory(pos, -cost) elseif listname == "micro" then -- Each microblock costs 1 microblock: circular_saw:update_inventory(pos, -stack:get_count()) elseif listname == "input" then -- Each normal (= full) block taken costs 8 microblocks: circular_saw:update_inventory(pos, 8 * -stack:get_count()) end -- The recycle field plays no role here since it is processed immediately. end gui_slots = "listcolors[#606060AA;#808080;#101010;#202020;#FFF]" function circular_saw.on_construct(pos) local meta = minetest.get_meta(pos) local fancy_inv = default.gui_bg..default.gui_bg_img..default.gui_slots meta:set_string("formspec", "size[11,9]"..fancy_inv.. "label[0,0;" ..S("Input\nmaterial").. "]" .. "list[current_name;input;1.5,0;1,1;]" .. "label[0,1;" ..S("Left-over").. "]" .. "list[current_name;micro;1.5,1;1,1;]" .. "label[0,2;" ..S("Recycle\noutput").. "]" .. "list[current_name;recycle;1.5,2;1,1;]" .. "field[0.3,3.5;1,1;max_offered;" ..S("Max").. ":;${max_offered}]" .. "button[1,3.2;1,1;Set;" ..S("Set").. "]" .. "list[current_name;output;2.8,0;8,5;]" .. "list[current_player;main;1.5,5.25;8,4;]") meta:set_int("anz", 0) -- No microblocks inside yet.