summaryrefslogtreecommitdiff
path: root/railtrack/railtrack.lua
blob: 664300ff44fd1beeff106fe54e9268f53552a40f (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
local MAX_SECTION_LEN = 20

railtrack = {
	rotations = "FLR",
	accel_flat = -0.5,
	accel_up = -2,
	accel_down = 2,
}

railtrack.default_rail = {
	description = "Rail",
	drawtype = "raillike",
	tiles = {"default_rail.png", "default_rail_curved.png",
		"default_rail_t_junction.png", "default_rail_crossing.png"},
	paramtype = "light",
	sunlight_propagates = true,
	walkable = false,
	is_ground_content = false,
	selection_box = {
		type = "fixed",
		fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
	},
	groups = {bendy = 2, dig_immediate = 2, attached_node = 1,
		connect_to_raillike = minetest.raillike_group("rail")},
	railtype = "rail",
	after_place_node = function(pos, placer, itemstack)
		local meta = minetest.get_meta(pos)
		local def = itemstack:get_definition() or {}
		if def.acceleration then
			meta:set_string("acceleration", def.acceleration)
		end
		local junc = {}
		local contype = meta:get_string("contype") or ""
		local s_cons = meta:get_string("connections") or ""
		if contype == "section" then
			railtrack:limit_section_len(placer, pos, meta)
		elseif s_cons ~= "" then
			local cons = minetest.deserialize(s_cons)
			for _, con in pairs(cons) do
				if railtrack:limit_section_len(placer, con) then
					break
				end
			end
		end
	end,
	on_construct = function(pos)
		railtrack:update_rails(pos)
	end,
	after_destruct = function(pos, oldnode)
		local cons = railtrack:get_connections(pos)
		for _, p in pairs(cons) do
			railtrack:update_rails(p)
		end
	end,
}

function railtrack:copy(t)
	if type(t) ~= "table" then
		return t
	end
	local copy = {}
	for k, v in pairs(t) do
		copy[k] = v
	end
	return copy
end

function railtrack:register_rail(name, def)
	local udef = {}
	for k, v in pairs(railtrack.default_rail) do
		if not def[k] then
			def[k] = railtrack:copy(v)
		end
		udef[k] = railtrack:copy(def[k])
	end
	def.inventory_image = def.inventory_image or def.tiles[1]
	def.wield_image = def.wield_image or def.tiles[1]
	minetest.register_node(name, def)
	udef.drop = name
	udef.railtype = udef.railtype.."_union"
	udef.groups.not_in_creative_inventory=1
	minetest.register_node(name.."_union", udef)
end

function railtrack:limit_section_len(player, pos, meta)
	meta = meta or minetest.get_meta(pos)
	local contype = meta:get_string("contype") or ""
	if contype == "section" then
		local s_junc = meta:get_string("junctions") or ""
		if s_junc ~= "" then
			local junc = minetest.deserialize(s_junc)
			if #junc == 2 then
				local dist = railtrack:get_distance(junc[1], junc[2])
				if dist > MAX_SECTION_LEN then
					local node = minetest.get_node(pos) or {}
					if node.name then
						minetest.swap_node(pos, {name=node.name.."_union"})
						railtrack:update_rails(pos)
					end
				end
			end
		end
	end
end

function railtrack:is_railnode(pos)
	local node = minetest.get_node(pos)
	if node then
		return minetest.get_item_group(node.name, "connect_to_raillike") > 0
	end
end

function railtrack:get_sign(z)
	if z == 0 then
		return 0
	else
		return z / math.abs(z)
	end
end

function railtrack:get_rotations(s_rots, dir)
	local rots = {}
	for i = 1, string.len(s_rots) do
		local r = string.sub(s_rots, i, i)
		local rot = nil
		if r == "F" then
			rot = {x=dir.x, z=dir.z}
		elseif r == "L" then
			rot = {x=-dir.z, z=dir.x}
		elseif r == "R" then
			rot = {x=dir.z, z=-dir.x}
		elseif r == "B" then
			rot = {x=-dir.x, z=-dir.z}
		end
		if rot then
			table.insert(rots, rot)
		end
	end
	return rots
end

function railtrack:get_acceleration(pos)
	local meta = minetest.get_meta(pos)
	local accel = meta:get_string("acceleration") or ""
	if accel ~= "" then
		return tonumber(accel)
	end
end

function railtrack:get_direction(p1, p2)
	local v = vector.subtract(p1, p2)
	return {
		x = railtrack:get_sign(v.x),
		y = railtrack:get_sign(v.y),
		z = railtrack:get_sign(v.z),
	}
end

function railtrack:get_distance(p1, p2)
	local dx = p1.x - p2.x
	local dz = p1.z - p2.z
	return math.abs(dx) + math.abs(dz)
end


function railtrack:get_railtype(pos)
	local node = minetest.get_node(pos) or {}
	if node.name then
		local ref = minetest.registered_items[node.name] or {}
		return ref.railtype
	end
end

function railtrack:get_connection_type(pos, cons)
	local railtype = railtrack:get_railtype(pos)
	if #cons == 0 then
		return "single"
	elseif #cons == 1 then
		return "junction"
	elseif #cons == 2 then
		if cons[1].x == cons[2].x or cons[1].z == cons[2].z then
			if (cons[1].y == cons[2].y and cons[1].y == pos.y) or
					(math.abs(cons[1].y - cons[2].y) == 2) then
				if railtype == railtrack:get_railtype(cons[1]) and
						railtype == railtrack:get_railtype(cons[2]) then
					return "section"
				end
			end
		end
	end
	return "junction"
end

function railtrack:get_connections(pos)
	local connections = {}
	for y = 1, -1, -1 do
		for x = -1, 1 do
			for z = -1, 1 do
				if math.abs(x) ~= math.abs(z) then
					local p = vector.add(pos, {x=x, y=y, z=z})
					if railtrack:is_railnode(p) then
						table.insert(connections, p)
					end
				end
			end
		end
	end
	return connections
end

function railtrack:get_junctions(pos, last_pos, junctions)
	junctions = junctions or {}
	local cons = railtrack:get_connections(pos)
	local contype = railtrack:get_connection_type(pos, cons)
	if contype == "junction" then
		table.insert(junctions, pos)
	elseif contype == "section" then
		if last_pos then
			for i, p in pairs(cons) do
				if vector.equals(p, last_pos) then
					cons[i] = nil
				end
			end
		end
		for _, p in pairs(cons) do
			railtrack:get_junctions(p, pos, junctions)
		end
	end
	return junctions
end

function railtrack:set_acceleration(pos, accel)
	local meta = minetest.get_meta(pos)
	local contype = meta:get_string("contype")
	if contype == "section" then
		local s_junc = meta:get_string("junctions") or ""
		local junc = minetest.deserialize(s_junc) or {}
		if #junc == 2 then
			local p = vector.new(junc[2])
			local dir = railtrack:get_direction(junc[1], junc[2])
			local dist = railtrack:get_distance(junc[1], junc[2])
			for i = 0, dist do
				local m = minetest.get_meta(p)
				if m then
					m:set_string("acceleration", tostring(accel))
				end
				p = vector.add(dir, p)
			end
		end
	else
		meta:set_string("acceleration", tostring(accel))
	end
end

function railtrack:update_rails(pos, last_pos, level)
	local connections = {}
	local junctions = {}
	local meta = minetest.get_meta(pos)
	local cons = railtrack:get_connections(pos)
	local contype = railtrack:get_connection_type(pos, cons)
	level = level or 0
	for i, p in pairs(cons) do
		connections[i] = p
	end
	if contype == "junction" then
		level = level + 1
	end
	if contype == "section" or level < 2 then
		if last_pos then
			for i, p in pairs(cons) do
				if vector.equals(p, last_pos) then
					cons[i] = nil
				end
			end
		end
		for _, p in pairs(cons) do
			railtrack:update_rails(p, pos, level)
		end
	end
	if contype == "section" then
		junctions = railtrack:get_junctions(pos)
		connections = {}
	end
	meta:set_string("connections", minetest.serialize(connections))
	meta:set_string("junctions", minetest.serialize(junctions))
	meta:set_string("contype", contype)
end