aboutsummaryrefslogtreecommitdiff
path: root/src/client/mesh_generator_thread.cpp
blob: 53b980eebbd289d33f393dea1ab836acbff2be49 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
Minetest
Copyright (C) 2013, 2017 celeron55, Perttu Ahola <celeron55@gmail.com>

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

#include "mesh_generator_thread.h"
#include "settings.h"
#include "profiler.h"
#include "client.h"
#include "mapblock.h"
#include "map.h"

/*
	CachedMapBlockData
*/

CachedMapBlockData::~CachedMapBlockData()
{
	assert(refcount_from_queue == 0);

	delete[] data;
}

/*
	QueuedMeshUpdate
*/

QueuedMeshUpdate::~QueuedMeshUpdate()
{
	delete data;
}

/*
	MeshUpdateQueue
*/

MeshUpdateQueue::MeshUpdateQueue(Client *client):
	m_client(client)
{
	m_cache_enable_shaders = g_settings->getBool("enable_shaders");
	m_cache_use_tangent_vertices = m_cache_enable_shaders && (
		g_settings->getBool("enable_bumpmapping") ||
		g_settings->getBool("enable_parallax_occlusion"));
	m_cache_smooth_lighting = g_settings->getBool("smooth_lighting");
	m_meshgen_block_cache_size = g_settings->getS32("meshgen_block_cache_size");
}

MeshUpdateQueue::~MeshUpdateQueue()
{
	MutexAutoLock lock(m_mutex);

	for (auto &i : m_cache) {
		delete i.second;
	}

	for (QueuedMeshUpdate *q : m_queue) {
		delete q;
	}
}

void MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent)
{
	MutexAutoLock lock(m_mutex);

	cleanupCache();

	/*
		Cache the block data (force-update the center block, don't update the
		neighbors but get them if they aren't already cached)
	*/
	std::vector<CachedMapBlockData*> cached_blocks;
	size_t cache_hit_counter = 0;
	cached_blocks.reserve(3*3*3);
	v3s16 dp;
	for (dp.X = -1; dp.X <= 1; dp.X++)
	for (dp.Y = -1; dp.Y <= 1; dp.Y++)
	for (dp.Z = -1; dp.Z <= 1; dp.Z++) {
		v3s16 p1 = p + dp;
		CachedMapBlockData *cached_block;
		if (dp == v3s16(0, 0, 0))
			cached_block = cacheBlock(map, p1, FORCE_UPDATE);
		else
			cached_block = cacheBlock(map, p1, SKIP_UPDATE_IF_ALREADY_CACHED,
					&cache_hit_counter);
		cached_blocks.push_back(cached_block);
	}
	g_profiler->avg("MeshUpdateQueue: MapBlocks from cache [%]",
			100.0f * cache_hit_counter / cached_blocks.size());

	/*
		Mark the block as urgent if requested
	*/
	if (urgent)
		m_urgents.insert(p);

	/*
		Find if block is already in queue.
		If it is, update the data and quit.
	*/
	for (QueuedMeshUpdate *q : m_queue) {
		if (q->p == p) {
			// NOTE: We are not adding a new position to the queue, thus
			//       refcount_from_queue stays the same.
			if(ack_block_to_server)
				q->ack_block_to_server = true;
			q->crack_level = m_client->getCrackLevel();
			q->crack_pos = m_client->getCrackPos();
			return;
		}
	}

	/*
		Add the block
	*/
	QueuedMeshUpdate *q = new QueuedMeshUpdate;
	q->p = p;
	q->ack_block_to_server = ack_block_to_server;
	q->crack_level = m_client->getCrackLevel();
	q->crack_pos = m_client->getCrackPos();
	m_queue.push_back(q);

	// This queue entry is a new reference to the cached blocks
	for (CachedMapBlockData *cached_block : cached_blocks) {
		cached_block->refcount_from_queue++;
	}
}

// Returned pointer must be deleted
// Returns NULL if queue is empty
QueuedMeshUpdate *MeshUpdateQueue::pop()
{
	MutexAutoLock lock(m_mutex);

	bool must_be_urgent = !m_urgents.empty();
	for (std::vector<QueuedMeshUpdate*>::iterator i = m_queue.begin();
			i != m_queue.end(); ++i) {
		QueuedMeshUpdate *q = *i;
		if(must_be_urgent && m_urgents.count(q->p) == 0)
			continue;
		m_queue.erase(i);
		m_urgents.erase(q->p);
		fillDataFromMapBlockCache(q);
		return q;
	}
	return NULL;
}

CachedMapBlockData* MeshUpdateQueue::cacheBlock(Map *map, v3s16 p, UpdateMode mode,
			size_t *cache_hit_counter)
{
	CachedMapBlockData *cached_block = nullptr;
	std::map<v3s16, CachedMapBlockData*>::iterator it =
			m_cache.find(p);

	if (it != m_cache.end()) {
		cached_block = it->second;

		if (mode == SKIP_UPDATE_IF_ALREADY_CACHED) {
			if (cache_hit_counter)
				(*cache_hit_counter)++;
			return cached_block;
		}
	}

	if (!cached_block) {
		// Not yet in cache
		cached_block = new CachedMapBlockData();
		m_cache[p] = cached_block;
	}

	MapBlock *b = map->getBlockNoCreateNoEx(p);
	if (b) {
		if (!cached_block->data)
			cached_block->data =
					new MapNode[MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE];
		memcpy(cached_block->data, b->getData(),
				MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE * sizeof(MapNode));
	} else {
		delete[] cached_block->data;
		cached_block->data = nullptr;
	}
	return cached_block;
}

CachedMapBlockData* MeshUpdateQueue::getCachedBlock(const v3s16 &p)
{
	std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.find(p);
	if (it != m_cache.end()) {
		return it->second;
	}
	return NULL;
}

void MeshUpdateQueue::fillDataFromMapBlockCache(QueuedMeshUpdate *q)
{
	MeshMakeData *data = new MeshMakeData(m_client, m_cache_enable_shaders,
			m_cache_use_tangent_vertices);
	q->data = data;

	data->fillBlockDataBegin(q->p);

	std::time_t t_now = std::time(0);

	// Collect data for 3*3*3 blocks from cache
	v3s16 dp;
	for (dp.X = -1; dp.X <= 1; dp.X++)
	for (dp.Y = -1; dp.Y <= 1; dp.Y++)
	for (dp.Z = -1; dp.Z <= 1; dp.Z++) {
		v3s16 p = q->p + dp;
		CachedMapBlockData *cached_block = getCachedBlock(p);
		if (cached_block) {
			cached_block->refcount_from_queue--;
			cached_block->last_used_timestamp = t_now;
			if (cached_block->data)
				data->fillBlockData(dp, cached_block->data);
		}
	}

	data->setCrack(q->crack_level, q->crack_pos);
	data->setSmoothLighting(m_cache_smooth_lighting);
}

void MeshUpdateQueue::cleanupCache()
{
	const int mapblock_kB = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE *
			sizeof(MapNode) / 1000;
	g_profiler->avg("MeshUpdateQueue MapBlock cache size kB",
			mapblock_kB * m_cache.size());

	// The cache size is kept roughly below cache_soft_max_size, not letting
	// anything get older than cache_seconds_max or deleted before 2 seconds.
	const int cache_seconds_max = 10;
	const int cache_soft_max_size = m_meshgen_block_cache_size * 1000 / mapblock_kB;
	int cache_seconds = MYMAX(2, cache_seconds_max -
			m_cache.size() / (cache_soft_max_size / cache_seconds_max));

	int t_now = time(0);

	for (std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.begin();
			it != m_cache.end(); ) {
		CachedMapBlockData *cached_block = it->second;
		if (cached_block->refcount_from_queue == 0 &&
				cached_block->last_used_timestamp < t_now - cache_seconds) {
			m_cache.erase(it++);
			delete cached_block;
		} else {
			++it;
		}
	}
}

/*
	MeshUpdateThread
*/

MeshUpdateThread::MeshUpdateThread(Client *client):
	UpdateThread("Mesh"),
	m_queue_in(client)
{
	m_generation_interval = g_settings->getU16("mesh_generation_interval");
	m_generation_interval = rangelim(m_generation_interval, 0, 50);
}

void MeshUpdateThread::updateBlock(Map *map, v3s16 p, bool ack_block_to_server,
		bool urgent)
{
	// Allow the MeshUpdateQueue to do whatever it wants
	m_queue_in.addBlock(map, p, ack_block_to_server, urgent);
	deferUpdate();
}

void MeshUpdateThread::doUpdate()
{
	QueuedMeshUpdate *q;
	while ((q = m_queue_in.pop())) {
		if (m_generation_interval)
			sleep_ms(m_generation_interval);
		ScopeProfiler sp(g_profiler, "Client: Mesh making (sum)");

		MapBlockMesh *mesh_new = new MapBlockMesh(q->data, m_camera_offset);

		MeshUpdateResult r;
		r.p = q->p;
		r.mesh = mesh_new;
		r.ack_block_to_server = q->ack_block_to_server;

		m_queue_out.push_back(r);

		delete q;
	}
}
l opt">=conns(0,8), cr=conns(0,6), swlst=conns(0,8), swlcr=conns(0,6), swrst=conns(0,8), swrcr=conns(0,10), vst1=conns(8,0,0,0.5,0.25), vst2=conns(8,0,0.5,1,0.75), }, description={ st="straight", cr="curve", swlst="left switch (straight)", swlcr="left switch (curve)", swrst="right switch (straight)", swrcr="right switch (curve)", vst1="vertical lower node", vst2="vertical upper node", }, switch={ swlst="swlcr", swlcr="swlst", swrst="swrcr", swrcr="swrst", }, switchmc={ swlst="on", swlcr="off", swrst="on", swrcr="off", }, switchst={ swlst="st", swlcr="cr", swrst="st", swrcr="cr", }, regtp=true, trackplacer={ st=true, cr=true, }, tpsingle={ st=true, }, tpdefault="st", trackworker={ ["swrcr"]="st", ["swrst"]="st", ["st"]="cr", ["cr"]="swlst", ["swlcr"]="swrcr", ["swlst"]="swrst", }, slopenodes={}, rotation={"", "_45"}, increativeinv={vst1=true, vst2=true} } advtrains.trackpresets = ap --definition format: ([] optional) --[[{ nodename_prefix texture_prefix [shared_texture] models_prefix models_suffix (with dot) [shared_model] formats={ st,cr,swlst,swlcr,swrst,swrcr,vst1,vst2 (each a table with indices 0-3, for if to register a rail with this 'rotation' table entry. nil is assumed as 'all', set {} to not register at all) } common={} change something on common rail appearance }]] function advtrains.register_tracks(tracktype, def, preset) local function make_switchfunc(suffix_target, mesecon_state, is_state) local rcswitchfunc=function(pos, node, player) if minetest.check_player_privs(player:get_player_name(), {train_operator=true}) then advtrains.ndb.swap_node(pos, {name=def.nodename_prefix.."_"..suffix_target, param2=node.param2}) advtrains.invalidate_all_paths(pos) end end local switchfunc=function(pos, node, newstate) if newstate~=is_state then advtrains.ndb.swap_node(pos, {name=def.nodename_prefix.."_"..suffix_target, param2=node.param2}) advtrains.invalidate_all_paths(pos) end end local mesec if mesecon_state then -- if mesecons is not wanted, do not. mesec = {effector = { ["action_"..mesecon_state] = switchfunc, rules=advtrains.meseconrules }} end return rcswitchfunc, mesec, { getstate = is_state, setstate = switchfunc, } end local function make_overdef(suffix, rotation, conns, rcswitchfunc, mesecontbl, luaautomation, in_creative_inv, drop_slope) local img_suffix=suffix..rotation return { mesh = def.shared_model or (def.models_prefix.."_"..img_suffix..def.models_suffix), tiles = {def.shared_texture or (def.texture_prefix.."_"..img_suffix..".png"), def.second_texture}, --inventory_image = def.texture_prefix.."_"..img_suffix..".png", --wield_image = def.texture_prefix.."_"..img_suffix..".png", description=def.description.."("..preset.description[suffix]..rotation..")", connect1=conns.conn1, connect2=conns.conn2, rely1=conns.rely1 or 0, rely2=conns.rely2 or 0, railheight=conns.railheight or 0, on_rightclick=rcswitchfunc, groups = { attached_node=1, ["advtrains_track_"..tracktype]=1, save_in_nodedb=1, dig_immediate=2, not_in_creative_inventory=(not in_creative_inv and 1 or nil), not_blocking_trains=1, }, mesecons=mesecontbl, luaautomation=luaautomation, drop = (drop_slope and def.nodename_prefix.."_slopeplacer" or def.nodename_prefix.."_placer"), } end local function cycle_conns(conns, rotid) local add=(rotid-1)*preset.regstep return { conn1=(conns.conn1+add)%16, conn2=(conns.conn2+add)%16, rely1=conns.rely1 or 0, rely2=conns.rely2 or 0, railheight=conns.railheight or 0, } end local common_def=advtrains.merge_tables({ description = def.description, drawtype = "mesh", paramtype="light", paramtype2="facedir", walkable = false, selection_box = { type = "fixed", fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2}, }, rely1=0, rely2=0, railheight=0, drop=def.nodename_prefix.."_placer", can_dig=function(pos) return not advtrains.get_train_at_pos(pos) end, after_dig_node=function(pos) advtrains.ndb.update(pos) end, after_place_node=function(pos) advtrains.ndb.update(pos) end, }, def.common or {}) --make trackplacer base def advtrains.trackplacer.register_tracktype(def.nodename_prefix, preset.tpdefault) if preset.regtp then advtrains.trackplacer.register_track_placer(def.nodename_prefix, def.texture_prefix, def.description) end if preset.regsp then advtrains.slope.register_placer(def, preset) end for suffix, conns in pairs(preset.variant) do for rotid, rotation in ipairs(preset.rotation) do if not def.formats[suffix] or def.formats[suffix][rotid] then local rcswitchfunc, mesecontbl, luaautomation if preset.switch[suffix] then rcswitchfunc, mesecontbl, luaautomation=make_switchfunc(preset.switch[suffix]..rotation, preset.switchmc[suffix], preset.switchst[suffix]) end local adef={} if def.get_additional_definiton then adef=def.get_additional_definiton(def, preset, suffix, rotation) end minetest.register_node(def.nodename_prefix.."_"..suffix..rotation, advtrains.merge_tables( common_def, make_overdef( suffix, rotation, cycle_conns(conns, rotid), rcswitchfunc, mesecontbl, luaautomation, preset.increativeinv[suffix], preset.slopenodes[suffix] ), adef ) ) --trackplacer if preset.regtp then if preset.trackplacer[suffix] then advtrains.trackplacer.add_double_conn(def.nodename_prefix, suffix, rotation, cycle_conns(conns, rotid)) end if preset.tpsingle[suffix] then advtrains.trackplacer.add_single_conn(def.nodename_prefix, suffix, rotation, cycle_conns(conns, rotid)) end end advtrains.trackplacer.add_worked(def.nodename_prefix, suffix, rotation, preset.trackworker[suffix]) end end end advtrains.all_tracktypes[tracktype]=true end function advtrains.is_track_and_drives_on(nodename, drives_on_p) if not minetest.registered_nodes[nodename] then return false end local nodedef=minetest.registered_nodes[nodename] for k,v in pairs(drives_on_p) do if nodedef.groups["advtrains_track_"..k] then return true end end return false end function advtrains.get_track_connections(name, param2) local nodedef=minetest.registered_nodes[name] if not nodedef then atprint(" get_track_connections couldn't find nodedef for nodename "..(name or "nil")) return 0, 8, 0, 0, 0 end local noderot=param2 if not param2 then noderot=0 end if noderot > 3 then atprint(" get_track_connections: rail has invaild param2 of "..noderot) noderot=0 end local tracktype for k,_ in pairs(nodedef.groups) do local tt=string.match(k, "^advtrains_track_(.+)$") if tt then tracktype=tt end end return (nodedef.connect1 + 4 * noderot)%16, (nodedef.connect2 + 4 * noderot)%16, nodedef.rely1 or 0, nodedef.rely2 or 0, nodedef.railheight or 0, tracktype end --detector code --holds a table with nodes on which trains are on. advtrains.detector = {} advtrains.detector.on_node = {} function advtrains.detector.enter_node(pos, train_id) local ppos=advtrains.round_vector_floor_y(pos) local pts=minetest.pos_to_string(ppos) advtrains.detector.on_node[pts]=train_id advtrains.detector.call_enter_callback(ppos, train_id) end function advtrains.detector.leave_node(pos, train_id) local ppos=advtrains.round_vector_floor_y(pos) advtrains.detector.call_leave_callback(ppos, train_id) end function advtrains.detector.stay_node(pos, train_id) local ppos=advtrains.round_vector_floor_y(pos) local pts=minetest.pos_to_string(ppos) advtrains.detector.on_node[pts]=train_id end function advtrains.detector.call_enter_callback(pos, train_id) --atprint("instructed to call enter calback") local node = advtrains.ndb.get_node(pos) --this spares the check if node is nil, it has a name in any case local mregnode=minetest.registered_nodes[node.name] if mregnode and mregnode.advtrains and mregnode.advtrains.on_train_enter then mregnode.advtrains.on_train_enter(pos, train_id) end end function advtrains.detector.call_leave_callback(pos, train_id) --atprint("instructed to call leave calback") local node = advtrains.ndb.get_node(pos) --this spares the check if node is nil, it has a name in any case local mregnode=minetest.registered_nodes[node.name] if mregnode and mregnode.advtrains and mregnode.advtrains.on_train_leave then mregnode.advtrains.on_train_leave(pos, train_id) end end -- slope placer. Defined in register_tracks. --crafted with rail and gravel local sl={} function sl.register_placer(def, preset) minetest.register_craftitem(def.nodename_prefix.."_slopeplacer",{ description = attrans("@1 Slope", def.description), inventory_image = def.texture_prefix.."_slopeplacer.png", wield_image = def.texture_prefix.."_slopeplacer.png", groups={}, on_place = sl.create_slopeplacer_on_place(def, preset) }) end --(itemstack, placer, pointed_thing) function sl.create_slopeplacer_on_place(def, preset) return function(istack, player, pt) if not pt.type=="node" then minetest.chat_send_player(player:get_player_name(), attrans("Can't place: not pointing at node")) return istack end local pos=pt.above if not pos then minetest.chat_send_player(player:get_player_name(), attrans("Can't place: not pointing at node")) return istack end local node=minetest.get_node(pos) if not minetest.registered_nodes[node.name] or not minetest.registered_nodes[node.name].buildable_to then minetest.chat_send_player(player:get_player_name(), attrans("Can't place: space occupied!")) return istack end if advtrains.is_protected(pos, player:get_player_name()) then minetest.record_protection_violation(pos, player:get_player_name()) return istack end --determine player orientation (only horizontal component) --get_look_horizontal may not be available local yaw=player.get_look_horizontal and player:get_look_horizontal() or (player:get_look_yaw() - math.pi/2) --rounding unit vectors is a nice way for selecting 1 of 8 directions since sin(30°) is 0.5. dirvec={x=math.floor(math.sin(-yaw)+0.5), y=0, z=math.floor(math.cos(-yaw)+0.5)} --translate to direction to look up inside the preset table local param2, rot45=({ [-1]={ [-1]=2, [0]=3, [1]=3, }, [0]={ [-1]=2, [1]=0, }, [1]={ [-1]=1, [0]=1, [1]=0, }, })[dirvec.x][dirvec.z], dirvec.x~=0 and dirvec.z~=0 local lookup=preset.slopeplacer if rot45 then lookup=preset.slopeplacer_45 end --go unitvector forward and look how far the next node is local step=1 while step<=lookup.max do local node=minetest.get_node(vector.add(pos, dirvec)) --next node solid? if not minetest.registered_nodes[node.name] or not minetest.registered_nodes[node.name].buildable_to or advtrains.is_protected(pos, player:get_player_name()) then --do slopes of this distance exist? if lookup[step] then if minetest.setting_getbool("creative_mode") or istack:get_count()>=step then --start placing local placenodes=lookup[step] while step>0 do minetest.set_node(pos, {name=def.nodename_prefix.."_"..placenodes[step], param2=param2}) if not minetest.setting_getbool("creative_mode") then istack:take_item() end step=step-1 pos=vector.subtract(pos, dirvec) end else minetest.chat_send_player(player:get_player_name(), attrans("Can't place: Not enough slope items left (@1 required)", step)) end else minetest.chat_send_player(player:get_player_name(), attrans("Can't place: There's no slope of length @1",step)) end return istack end step=step+1 pos=vector.add(pos, dirvec) end minetest.chat_send_player(player:get_player_name(), attrans("Can't place: no supporting node at upper end.")) return itemstack end end advtrains.slope=sl --END code, BEGIN definition --definition format: ([] optional) --[[{ nodename_prefix texture_prefix [shared_texture] models_prefix models_suffix (with dot) [shared_model] formats={ st,cr,swlst,swlcr,swrst,swrcr,vst1,vst2 (each a table with indices 0-3, for if to register a rail with this 'rotation' table entry. nil is assumed as 'all', set {} to not register at all) } common={} change something on common rail appearance }]] advtrains.register_tracks("regular", { nodename_prefix="advtrains:track", texture_prefix="advtrains_track", shared_model="trackplane.b3d", description=attrans("Deprecated Track"), formats={vst1={}, vst2={}}, }, ap.t_45deg) --flat advtrains.register_tracks("default", { nodename_prefix="advtrains:dtrack", texture_prefix="advtrains_dtrack", models_prefix="advtrains_dtrack", models_suffix=".b3d", shared_texture="advtrains_dtrack_shared.png", description=attrans("Track"), formats={}, }, ap.t_30deg_flat) --slopes advtrains.register_tracks("default", { nodename_prefix="advtrains:dtrack", texture_prefix="advtrains_dtrack", models_prefix="advtrains_dtrack", models_suffix=".obj", shared_texture="advtrains_dtrack_shared.png", second_texture="default_gravel.png", description=attrans("Track"), formats={vst1={true, false, true}, vst2={true, false, true}, vst31={true}, vst32={true}, vst33={true}}, }, ap.t_30deg_slope) --bumpers advtrains.register_tracks("default", { nodename_prefix="advtrains:dtrack_bumper", texture_prefix="advtrains_dtrack_bumper", models_prefix="advtrains_dtrack_bumper", models_suffix=".b3d", shared_texture="advtrains_dtrack_rail.png", --bumpers still use the old texture until the models are redone. description=attrans("Bumper"), formats={}, }, ap.t_30deg_straightonly) --legacy bumpers for _,rot in ipairs({"", "_30", "_45", "_60"}) do minetest.register_alias("advtrains:dtrack_bumper"..rot, "advtrains:dtrack_bumper_st"..rot) end if mesecon then advtrains.register_tracks("default", { nodename_prefix="advtrains:dtrack_detector_off", texture_prefix="advtrains_dtrack_detector", models_prefix="advtrains_dtrack", models_suffix=".b3d", shared_texture="advtrains_dtrack_shared_detector_off.png", description=attrans("Detector Rail"), formats={}, get_additional_definiton = function(def, preset, suffix, rotation) return { mesecons = { receptor = { state = mesecon.state.off, rules = advtrains.meseconrules } }, advtrains = { on_train_enter=function(pos, train_id) advtrains.ndb.swap_node(pos, {name="advtrains:dtrack_detector_on".."_"..suffix..rotation, param2=advtrains.ndb.get_node(pos).param2}) mesecon.receptor_on(pos, advtrains.meseconrules) end } } end }, ap.t_30deg_straightonly) advtrains.register_tracks("default", { nodename_prefix="advtrains:dtrack_detector_on", texture_prefix="advtrains_dtrack", models_prefix="advtrains_dtrack", models_suffix=".b3d", shared_texture="advtrains_dtrack_shared_detector_on.png", description="Detector(on)(you hacker you)", formats={}, get_additional_definiton = function(def, preset, suffix, rotation) return { mesecons = { receptor = { state = mesecon.state.on, rules = advtrains.meseconrules } }, advtrains = { on_train_leave=function(pos, train_id) advtrains.ndb.swap_node(pos, {name="advtrains:dtrack_detector_off".."_"..suffix..rotation, param2=advtrains.ndb.get_node(pos).param2}) mesecon.receptor_off(pos, advtrains.meseconrules) end } } end }, ap.t_30deg_straightonly_noplacer) end --TODO legacy --I know lbms are better for this purpose for name,rep in pairs({swl_st="swlst", swr_st="swrst", swl_cr="swlcr", swr_cr="swrcr", }) do minetest.register_abm({ -- In the following two fields, also group:groupname will work. nodenames = {"advtrains:track_"..name}, interval = 1.0, -- Operation interval in seconds chance = 1, -- Chance of trigger per-node per-interval is 1.0 / this action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:track_"..rep, param2=node.param2}) end, }) minetest.register_abm({ -- In the following two fields, also group:groupname will work. nodenames = {"advtrains:track_"..name.."_45"}, interval = 1.0, -- Operation interval in seconds chance = 1, -- Chance of trigger per-node per-interval is 1.0 / this action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:track_"..rep.."_45", param2=node.param2}) end, }) end if advtrains.register_replacement_lbms then minetest.register_lbm({ name = "advtrains:ramp_replacement_1", -- In the following two fields, also group:groupname will work. nodenames = {"advtrains:track_vert1"}, action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_vst1", param2=(node.param2+2)%4}) end, }) minetest.register_lbm({ name = "advtrains:ramp_replacement_1", -- -- In the following two fields, also group:groupname will work. nodenames = {"advtrains:track_vert2"}, action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_vst2", param2=(node.param2+2)%4}) end, }) minetest.register_abm({ name = "advtrains:st_rep_1", -- In the following two fields, also group:groupname will work. nodenames = {"advtrains:track_st"}, interval=1, chance=1, action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_st", param2=node.param2}) end, }) minetest.register_lbm({ name = "advtrains:st_rep_1", -- -- In the following two fields, also group:groupname will work. nodenames = {"advtrains:track_st_45"},