aboutsummaryrefslogtreecommitdiff
path: root/init.lua
blob: 94019d7fcf375b3878e64bfb765d5cb048a81cdb (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
local function escape_texture(str)
	return str:gsub("[%[%()^:]", "\\%1")
end

departureboards = {}

departureboards.contents = {}

local cts = departureboards.contents

local function render_depboard(pos, objref)
	local c = cts[minetest.hash_node_position(pos)]
	local meta = minetest.get_meta(pos)
	local changed = false
	local tb = {}
	if meta then
		tb = minetest.deserialize(meta:get_string("contents") or "") or {}
		if not c then
			c = tb
		end
		for k,v in pairs(c) do
			if not tb[k] or tb[k] ~= v then
				changed = true
			end
			tb[k] = tostring(v)
		end
		meta:set_string("contents", minetest.serialize(tb))
	end
	if not c then
		c = {}
	end
	local font = font_api.get_font("metro")
	local line = font:render(tb.line or "", font:get_height(1)*15, font:get_height(1), {halign="left", color=tb.lfg or "#ff0000"})
	local dest = font:render(tb.dest or "", font:get_height(1)*15, font:get_height(1), {halign="left", color="#ffffff"})
	local remark = font:render(tb.rem or "", font:get_height(1)*15, font:get_height(1), {halign="left", color=tb.remcolor or "#ff0000"})
	local m = font:render(tb.m or "", font:get_height(1)*15, font:get_height(1), {halign="left", color="#ffffff"})
	local s = font:render(tb.s or "", font:get_height(1)*15, font:get_height(1), {halign="left", color="#ffffff"})
	local line_color = tb.lbg or "#00ffff"
	local delay = 0
	if changed then
		delay = 1  -- add here splitflap sound
	end
	objref:set_properties({ is_visible=false })
	minetest.after(delay, function () objref:set_properties({ textures= {"[combine:256x256:0,0=station_sign.png\\^(line_bg.png\\^\\[colorize\\:"..line_color.."\\:150)\\^\\[resize\\:256x256:200,100="..escape_texture(line)..":10,100="..escape_texture(dest)..":200,127="..escape_texture(m)..":228,127="..escape_texture(s)..":10,127="..escape_texture(remark)},
																			visual_size = {x=3, y=3}, is_visible=true }) end)
end



local function on_digiline_receive(pos, _, channel, msg)
	cts[minetest.hash_node_position(pos)] = msg
	display_api.update_entities(pos)
end

display_api.register_display_entity("departureboards:display")

minetest.register_node("departureboards:departureboard", {
		sunlight_propagates = true,
		paramtype = "light",
		paramtype2 = "facedir",
		drawtype = "nodebox",
		node_box = {
			type = "fixed",
			fixed = {-3/6, -1/6, 0.5/3,
					 3/6, 1/6, 0.5/3 - 0.03},
		},
		selection_box = {
			type = "fixed",
			fixed={-1.5,-1/2, 0.5, 1.5, 1/2, 0.5-0.1}},
		visual_scale=3,
		tiles = { "signs_road_sides.png", "signs_road_sides.png", 
			          "signs_road_sides.png", "signs_road_sides.png", "signs_road_sides.png",
			          {name="animation_frames.png", animation={type="vertical_frames", aspect_w=96, aspect_h=96, length=1.0}}},
		groups = {choppy=2, dig_immediate=2, not_blocking_trains=1, display_api=1},
		sounds = default.node_sound_defaults(),
		display_entities = {
			["departureboards:display"] = {
					on_display_update = render_depboard,
					depth = 0.5 - display_api.entity_spacing - 0.1,
					size = { x = 3, y = 1 },
					aspect_ratio = 1/2,
					maxlines = 1,
			},
		},
		light_source = 3,
		on_place = display_api.on_place,
		on_construct = 	function(pos)
				local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
				local meta = minetest.get_meta(pos)
				meta:set_string("contents", minetest.serialize({}))
				meta:set_string("formspec", "field[channel;Channel;${channel}]")
				display_api.on_construct(pos)
			end,
		on_destruct = display_api.on_destruct,
		on_rotate = signs_api.on_rotate,
		on_receive_fields =  function(pos, _, fields, sender)
			local name = sender:get_player_name()
			if minetest.is_protected(pos, name) and not minetest.check_player_privs(name, {protection_bypass=true}) then
				return
			end
			if (fields.channel) then
				minetest.get_meta(pos):set_string("channel", fields.channel)
			end
		end,
		on_punch = function(pos, node, player, pointed_thing)
				signs_api.set_formspec(pos)
				display_api.update_entities(pos)
		end,
		digiline = {
				receptor = {},
				effector = {
					action = on_digiline_receive
				},
		},
})