aboutsummaryrefslogtreecommitdiff
path: root/advtrains_signals_japan/init.lua
blob: 84373a966fe35c0c81f123cf1bdfd22e30d54d99 (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
local pole_texture = "advtrains_signals_japan_mast.png"
local signal_face_texture = "advtrains_hud_bg.png^[colorize:#000000:255"
local pole_radius = 1/16
local pole_box = {-pole_radius,-1/2,-pole_radius,pole_radius,1/2,pole_radius}
local light_radius = 1/20
local signal_width = 6*light_radius
local signal_thickness = pole_radius*3
local signal_height = {}
local signal_box = {}
local light_red = "advtrains_hud_bg.png^[colorize:red:255"
local light_yellow = "advtrains_hud_bg.png^[colorize:orange:255"
local light_green = "advtrains_hud_bg.png^[colorize:lime:255"
local light_purple = "advtrains_hud_bg.png^[colorize:purple:255"
local light_distant = light_purple
local light_off = signal_face_texture

do
	local model_path_prefix = table.concat({minetest.get_modpath("advtrains_signals_japan"), "models", "advtrains_signals_japan_"}, DIR_DELIM)

	local function vertex(x, y, z)
		return string.format("v %f %f %f", x, y, z)
	end
	local function texture(u, v)
		return string.format("vt %f %f", u, v)
	end
	local function face_element(v, vt)
		if vt then
			return string.format("%d/%d", v, vt)
		end
		return tonumber(v)
	end
	local function face_elements(...)
		local st = {"f"}
		local args = {...}
		local len = #args
		for i = 1, len, 2 do
			st[(i+3)/2] = face_element(args[i], args[i+1])
		end
		return table.concat(st, " ")
	end
	local function sequential_elements(v0, vt0, count)
		local st = {}
		for i = 1, count do
			st[i] = face_element(v0+i, vt0+i)
		end
		return table.concat(st, " ")
	end
	local function mod_lower(min, a, b)
		return min + (a-min)%b
	end
	local function connect_circular(v0, vt0, count)
		return "f " .. sequential_elements(v0, vt0, count)
	end
	local function connect_cylindrical(v0, vt0, count)
		local st = {}
		for i = 0, count-1 do
			local j = (i+1)%count
			local v1 = v0+i+1
			local v2 = v1+count
			local v3 = v0+j+1
			local v4 = v3+count
			local vt1 = vt0+i+1
			local vt2 = vt1+count+1
			st[i+1] = face_elements(v1, vt1, v3, vt1+1, v4, vt2+1, v2, vt2)
		end
		return table.concat(st, "\n")
	end
	local function circular_textures(u0, v0, r, count, total, angular_offset, direction)
		local st = {}
		if not angular_offset then
			angular_offset = 0
		end
		if not total then
			total = count
		end
		if not direction then
			direction = 1
		end
		for i = 0, count-1 do
			local theta = angular_offset + direction*i/total*2*math.pi
			local u, v = r*math.cos(theta), r*math.sin(theta)
			st[i+1] = texture(u0+u, v0+v)
		end
		return table.concat(st, "\n")
	end
	local function rectangular_textures(u0, v0, u1, v1, count)
		local st = {}
		local width = u1-u0
		for i = 0, count do
			local u = u0+i/count*width
			st[i+1] = texture(u, v0)
			st[i+count+2] = texture(u, v1)
		end
		return table.concat(st, "\n")
	end

	-- generate pole model
	local pole_npolygon = 32
	local pole_vertex_count = pole_npolygon*2
	local pole_uv_count = pole_npolygon*3+2
	local pole_vertices = {}
	local pole_objdef = {
		"g pole",
		"usemtl pole",
		connect_circular(0, 0, pole_npolygon),
		connect_circular(pole_npolygon, 0, pole_npolygon),
		connect_cylindrical(0, pole_npolygon, pole_npolygon),
	}
	local pole_uv = {
		circular_textures(0.5, 0.5, 0.5, pole_npolygon),
		rectangular_textures(0, 0, 1, 1, pole_npolygon),
	}
	for i = 0, pole_npolygon-1 do
		local theta = i*2/pole_npolygon*math.pi
		local r = pole_radius
		local x, z = r*math.sin(theta), r*math.cos(theta)
		local lower_index = i+1
		local upper_index = lower_index+pole_npolygon
		pole_vertices[lower_index] = vertex(x, -0.5, z)
		pole_vertices[upper_index] = vertex(x, 0.5, z)
	end
	pole_vertices = table.concat(pole_vertices, "\n")
	pole_objdef = table.concat(pole_objdef, "\n")
	pole_uv = table.concat(pole_uv, "\n")
	minetest.safe_file_write(model_path_prefix .. "pole.obj", table.concat({pole_vertices, pole_uv, pole_objdef}, "\n"))

	-- generate signals
	for lightcount = 5, 6 do
		for rotname, rot in pairs {["0"] = 0, ["30"] = 26.5, ["45"] = 45, ["60"] = 63.5} do
			local rot = math.rad(rot)
			local lightradius = 0.05
			local lightspacing = 0.04
			local halfwidth = signal_width/2
			local halfheight = (2+lightcount)*lightradius+(lightcount-1)*lightspacing/2
			local halfthickness = signal_thickness/2
			local half_npolygon = pole_npolygon/2
			local quarter_npolygon = pole_npolygon/4
			local boxside = math.max(halfwidth, halfthickness*2)
			signal_height[lightcount] = halfheight*2
			signal_box[lightcount] = {-boxside, -halfheight, -boxside, boxside, halfheight, boxside}

			local _vertex = vertex
			local rv = vector.new(0, rot, 0)
			local function vertex(x, y, z)
				local v = vector.rotate(vector.new(x, y, z), rv)
				return _vertex(v.x, v.y, v.z)
			end

			-- generate signal face
			local face_vertices = {}
			local face_uv = {
				circular_textures(0.5, 0.5+halfheight-3*lightradius, halfwidth, half_npolygon+1, pole_npolygon),
				circular_textures(0.5, 0.5-halfheight+3*lightradius, halfwidth, half_npolygon+1, pole_npolygon, math.pi),
				rectangular_textures(0, 0, 1, 1, 2+pole_npolygon),
			}
			local face_objdef = {
				"g face",
				"usemtl face",
				connect_circular(pole_vertex_count+2+pole_npolygon, pole_uv_count, 2+pole_npolygon),
				connect_circular(pole_vertex_count, pole_uv_count, 2+pole_npolygon),
				connect_cylindrical(pole_vertex_count, pole_uv_count+2+pole_npolygon, 2+pole_npolygon),
			}
			local face_vertex_count = 4*half_npolygon+4
			local face_uv_count = 2*(half_npolygon+1) + 2*(pole_npolygon+3)
			for i = 0, half_npolygon do
				local theta = i/half_npolygon*math.pi
				local r = halfwidth
				local x, y = r*math.cos(theta), halfheight-3*lightradius+r*math.sin(theta)
				face_vertices[i+1] = vertex(x, y, -halfthickness)
				face_vertices[i+2+half_npolygon] = vertex(-x, -y, -halfthickness)
				face_vertices[i+3+2*half_npolygon] = vertex(x, y, halfthickness)
				face_vertices[i+4+3*half_npolygon] = vertex(-x, -y, halfthickness)
			end

			-- generate lights
			local light_vertices = {}
			local light_vertex_count = 8*(half_npolygon+1)+pole_npolygon
			local light_uv = {rectangular_textures(0, 0, 1, 1, half_npolygon)}
			local light_uv_count = 2*(half_npolygon+1)+pole_npolygon*lightcount
			local light_objdef_face = {}
			local light_objdef_main = {
				"g light",
				"usemtl light",
			}
			for i = 1, lightcount do
				local x0, y0 = 0, -halfheight + (2*i+1)*lightradius + (i-1)*lightspacing
				local v0 = light_vertex_count*(i-1)
				for j = 0, half_npolygon do
					local theta = j/half_npolygon*math.pi
					local xs, ys = math.cos(theta), math.sin(theta)
					for k, v in pairs {
						{xm = -1, ym = 1, rm = 1, z = 1},
						{xm = 1, ym = 1, rm = 0.8, z = 1},
						{xm = -1, ym = 1, rm = 1, z = 2},
						{xm = 1, ym = 1, rm = 0.8, z = 2},
						{xm = 1, ym = -1, rm = 1, z = 1},
						{xm = -1, ym = -1, rm = 0.8, z = 1},
						{xm = 1, ym = -1, rm = 1, z = 1.5},
						{xm = -1, ym = -1, rm = 0.8, z = 1.5},
					} do
						local x = x0+xs*lightradius*v.xm*v.rm
						local y = y0+ys*lightradius*v.ym*v.rm
						light_vertices[v0+(k-1)*(half_npolygon+1)+j+1] = vertex(x, y, -halfthickness*v.z)
					end
				end
				for j = 0, pole_npolygon-1 do
					local theta = j/pole_npolygon*2*math.pi
					local x, y = math.cos(theta), math.sin(theta)
					light_vertices[v0+8*(half_npolygon+1)+1+j] = vertex(x0+lightradius*x, y0+lightradius*y, -halfthickness*1.05)
				end
				local v0 = pole_vertex_count+face_vertex_count+v0
				local vt0 = pole_uv_count + face_uv_count
				local ostep = 2*half_npolygon+2
				for j = 1, half_npolygon do
					local dv = 2*(half_npolygon+1)
					local v0 = v0 + dv
					local vn = v0 + dv
					light_objdef_face[i*ostep-j+1] = face_elements(v0+j, vt0+j, v0+j+1, vt0+j+1, vn-j, vt0+half_npolygon+2+j, vn-j+1, vt0+half_npolygon+1+j)
					local v0 = vn + dv
					local vn = v0 + dv
					light_objdef_face[i*ostep-half_npolygon-j+1] = face_elements(v0+j, vt0+j, v0+j+1, vt0+j+1, vn-j, vt0+half_npolygon+2+j, vn-j+1, vt0+half_npolygon+1+j)
				end
				local vt0 = vt0 + 2*(half_npolygon+1) + (i-1)*pole_npolygon
				light_uv[i+1] = circular_textures(0.5, (i-1/2)/lightcount, 0.4/lightcount, pole_npolygon)
				light_objdef_face[(i-1)*ostep+1] = connect_cylindrical(v0, pole_uv_count+2+pole_npolygon, 2+pole_npolygon)
				light_objdef_face[(i-1)*ostep+2] = connect_cylindrical(v0+4*(half_npolygon+1), pole_uv_count+2+pole_npolygon, 2+pole_npolygon)
				light_objdef_main[2+i] = connect_circular(v0+8*(half_npolygon+1), vt0, pole_npolygon)
			end

			-- write file
			face_vertices = table.concat(face_vertices, "\n")
			face_uv = table.concat(face_uv, "\n")
			face_objdef = table.concat(face_objdef, "\n")
			minetest.safe_file_write(model_path_prefix .. lightcount .. "_" .. rotname .. ".obj", table.concat({
				pole_vertices,
				face_vertices,
				table.concat(light_vertices, "\n"),
				pole_uv,
				face_uv,
				table.concat(light_uv, "\n"),
				pole_objdef,
				face_objdef,
				table.concat(light_objdef_face, "\n"),
				table.concat(light_objdef_main, "\n"),
			}, "\n"))
		end
	end
end

local S = attrans

minetest.register_node("advtrains_signals_japan:pole_0", {
	description = S("Japanese signal pole"),
	drawtype = "mesh",
	mesh = "advtrains_signals_japan_pole.obj",
	tiles = {pole_texture},

	paramtype = "light",
	sunlight_propagates = true,

	paramtype2 = "none",
	selection_box = {
		type = "fixed",
		fixed = {pole_box},
	},
	collision_box = {
		type = "fixed",
		fixed = {pole_box},
	},
	groups = {
		cracky = 2,
		not_blocking_trains = 1,
		not_in_creative_inventory = 0,
	},
	drop = "advtrains_signals_japan:pole_0",
})

advtrains.interlocking.aspect.register_group {
	name = "advtrains_signals_japan:5a",
	label = S("Japanese signal"),
	aspects = {
		danger = {
			label = S"Danger (halt)",
			main = 0,
		},
		restrictedspeed = {
			label = S"Restricted speed",
		},
		caution = {
			label = S"Caution",
		},
		reducedspeed = {
			label = S"Reduced speed",
		},
		clear = {
			label = S"Clear (proceed)",
		},
		"clear",
		"reducedspeed",
		"caution",
		"restrictedspeed",
		"danger",
	}
}

local sigdefs = {}
local lightcolors = {
	red = "red",
	green = "lime",
	yellow = "orange",
	distant = "purple",
}
local function process_signal(name, sigdata, isrpt)
	local def = {}
	local tx = {}
	def.textures = tx
	def.desc = sigdata.desc
	def.isdst = isrpt
	local lights = sigdata.lights
	local lightcount = #lights
	if isrpt then
		lightcount = lightcount+1
	end
	def.lightcount = lightcount
	def.suppasp_names = {}
	for idx, asp in ipairs(sigdata.aspects) do
		local aspname = asp.name
		local tt = {
			string.format("[combine:1x%d", lightcount),
			string.format("0,0=(advtrains_hud_bg.png\\^[resize\\:1x%d\\^[colorize\\:#000)", lightcount),
		}
		for _, i in pairs(asp.lights) do
			local color = lightcolors[lights[i]]
			tt[#tt+1] = string.format("0,%d=(advtrains_hud_bg.png\\^[colorize\\:%s)", i-1, color)
		end
		if isrpt then
			local color = lightcolors.distant
			tt[#tt+1] = string.format("0,%d=(advtrains_hud_bg.png\\^[colorize\\:%s)", lightcount-1, color)
		end
		tx[aspname] = table.concat(tt, ":")
		def.suppasp_names[idx] = aspname
	end
	local invimg = {
		string.format("[combine:%dx%d", lightcount*4+1, lightcount*4+1),
		string.format("%d,0=(advtrains_hud_bg.png\\^[resize\\:5x%d\\^[colorize\\:#000)", lightcount*2-2, lightcount*4+1),
	}
	for i, c in pairs(lights) do
		local color = lightcolors[c]
		invimg[i+2] = string.format("%d,%d=(advtrains_hud_bg.png\\^[resize\\:3x3\\^[colorize\\:%s)", 2*lightcount-1, 4*i-3, color)
	end
	if isrpt then
		invimg[lightcount+2] = string.format("%d,%d=(advtrains_hud_bg.png\\^[resize\\:3x3\\^[colorize\\:%s)", 2*lightcount-1, 4*lightcount-3, lightcolors.distant)
	end
	def.inventory_image = table.concat(invimg, ":")
	return def
end
for sigtype, sigdata in pairs {
	["5a"] = {
		desc = "5A",
		lights = {"yellow", "yellow", "red", "yellow", "green"},
		aspects = {
			{name = "clear", lights = {5}, main = -1},
			{name = "reducedspeed", lights = {2, 5}},
			{name = "caution", lights = {4}},
			{name = "restrictedspeed", lights = {1, 4}},
			{name = "danger", lights = {3}, main = 0},
		}
	}
} do
	sigdefs["main_"..sigtype] = process_signal(sigtype, sigdata)
	sigdefs["rpt_"..sigtype] = process_signal(sigtype, sigdata, true)
end

for k in pairs(sigdefs) do
	advtrains.trackplacer.register_tracktype("advtrains_signals_japan:"..k)
end

for _, rtab in ipairs {
	{rot = "0", ici = true},
	{rot = "30"},
	{rot = "45"},
	{rot = "60"},
} do
	local rot = rtab.rot
	for sigtype, siginfo in pairs(sigdefs) do
		local lightcount = siginfo.lightcount
		for asp, texture in pairs(siginfo.textures) do
			minetest.register_node("advtrains_signals_japan:"..sigtype.."_"..asp.."_"..rot, {
				description = attrans(string.format("Japanese%s signal (type %s)", siginfo.isdst and " repeating" or "", siginfo.desc)),
				drawtype = "mesh",
				mesh = string.format("advtrains_signals_japan_%d_%s.obj", lightcount, rot),
				tiles = {pole_texture, signal_face_texture, texture},
				paramtype = "light",
				sunlight_propagates = true,
				light_source = 4,
				paramtype2 = "facedir",
				selection_box = {
					type = "fixed",
					fixed = {pole_box, signal_box[lightcount]},
				},
				collision_box = {
					type = "fixed",
					fixed = {pole_box, signal_box[lightcount]},
				},
				groups = {
					cracky = 2,
					advtrains_signal = 2,
					not_blocking_trains = 1,
					save_in_at_nodedb = 1,
					not_in_creative_inventory = rtab.ici and asp == "danger" and 0 or 1,
				},
				inventory_image = siginfo.inventory_image,
				drop = "advtrains_signals_japan:"..sigtype.."_danger_0",
				advtrains = {
					supported_aspects = {
						group = "advtrains_signals_japan:5a",
						name = siginfo.suppasp_names,
						dst_shift = siginfo.isdst and 0,
						main = (not siginfo.isdst) and {} or false
					},
					get_aspect = function()
						local main
						if siginfo.isdst then
							main = false
						end
						return {group = "advtrains_signals_japan:5a", name = asp, main = main}
					end,
					set_aspect = function(pos, node, asp)
						advtrains.ndb.swap_node(pos, {name = "advtrains_signals_japan:"..sigtype.."_"..(asp.name).."_"..rot, param2 = node.param2})
					end,
				},
				on_rightclick = advtrains.interlocking.signal_rc_handler,
				can_dig = advtrains.interlocking.signal_can_dig,
				after_dig_node = advtrains.interlocking.signal_after_dig,
			})
			advtrains.trackplacer.add_worked("advtrains_signals_japan:"..sigtype, asp, "_"..rot)
		end
	end
end