summaryrefslogtreecommitdiff
path: root/init.lua
blob: 26c9660832d8a52ed287731418ea944b611e8d67 (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
local function schedule(time,pos,channel,message)
	minetest.after(time,function()
		digilines.receptor_send(pos,digilines.rules.default,channel,message)
	end)
end

local emit_signals = function(pos)
	local meta = minetest.get_meta(pos)
	if meta:get_string("locked") ~= "true" then
		meta:set_string("locked","true")
		schedule(0,pos,"main","YELLOW")
		schedule(3,pos,"main","RED")
		schedule(4,pos,"side","GREEN")
		schedule(14,pos,"side","YELLOW")
		schedule(17,pos,"side","RED")
		schedule(18,pos,"main","GREEN")
		minetest.after(30,function()
			if minetest.get_node(pos).name == "lightcontroller:trafficnext" then
				meta:set_string("locked","")
			end
		end)
	end
end

local lc_scan = function(pos)
	local objs = minetest.get_objects_inside_radius(pos, 6)
	if objs then
		for _,obj in ipairs(objs) do
			if obj:is_player() then
				emit_signals(pos)
				return
			end
		end
	end
end

local lc_slave_scan = function(pos)
	local objs = minetest.get_objects_inside_radius(pos, 6)
	if objs then
		for _,obj in ipairs(objs) do
			if obj:is_player() then
				digilines.receptor_send(pos, digilines.rules.default, "detector", "car")
				return
			end
		end
	end
end

local on_digiline_receive = function(pos,_,channel)
	if channel == "detector" then emit_signals(pos) end
end

minetest.register_node("lightcontroller:trafficnext", {
	description = "TrafficNeXt SoC - Master",
	tiles = {
		"streets_tl_bg.png",
		"streets_tl_bg.png",
		"streets_tl_bg.png^streets_tl_green.png",
		"streets_tl_bg.png^streets_tl_green.png",
		"streets_tl_bg.png^streets_tl_green.png",
		"streets_tl_bg.png^streets_tl_green.png"
	},
	groups = {cracky=3, stone=1},
	sounds = default.node_sound_stone_defaults(),
	digiline = {
		receptor = {},
		effector = { action = on_digiline_receive },
	},
	on_punch = emit_signals,
})

minetest.register_abm({
	nodenames = {"lightcontroller:trafficnext"},
	interval = 1.0,
	chance = 1,
	action = lc_scan,
})

minetest.register_node("lightcontroller:trafficnext_slave", {
	description = "TrafficNeXt SoC - Slave",
	tiles = {
		"streets_tl_bg.png",
		"streets_tl_bg.png",
		"streets_tl_bg.png^streets_tl_red.png",
		"streets_tl_bg.png^streets_tl_red.png",
		"streets_tl_bg.png^streets_tl_red.png",
		"streets_tl_bg.png^streets_tl_red.png"
	},
	groups = {cracky=3, stone=1},
	sounds = default.node_sound_stone_defaults(),
	digiline = {
		receptor = {},
	},
})

minetest.register_abm({
	nodenames = {"lightcontroller:trafficnext_slave"},
	interval = 1.0,
	chance = 1,
	action = lc_slave_scan,
})

minetest.register_lbm({
	name = "lightcontroller:reset",
	label = "Reset TrafficNeXt SoCs",
	nodenames = {"lightcontroller:trafficnext"},
	run_at_every_load = true,
	action = function(pos)
		minetest.get_meta(pos):set_string("locked","")
		digilines.receptor_send(pos,digilines.rules.default,"main","GREEN")
		digilines.receptor_send(pos,digilines.rules.default,"side","RED")
	end,
})