summaryrefslogtreecommitdiff
path: root/init.lua
blob: 27749f7e7bb898704718a13d050b01bbdd85372e (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
local polemount_nodebox = {
	{ -0.35, -0.45, 0.35, 0.35, 0.45, 0.85 }
}


minetest.register_node("emtraffic:controller", {
	tiles = {
		"emtraffic_sides.png",
		"emtraffic_sides.png",
		"emtraffic_sides.png",
		"emtraffic_sides.png",
		"emtraffic_sides.png",
		"emtraffic_front.png",
	},
	description = "Electromechanical Traffic Signal Controller",
	paramtype = "light",
	paramtype2 = "facedir",
	drawtype = "nodebox",
	groups = {dig_immediate=2},
	sounds = default.node_sound_metal_defaults(),
	on_construct = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_string("state","greena")
		digiline:receptor_send(pos,digiline.rules.default,"a","RED")
		digiline:receptor_send(pos,digiline.rules.default,"b","RED")
		digiline:receptor_send(pos,digiline.rules.default,"c","RED")
		digiline:receptor_send(pos,digiline.rules.default,"d","RED")
		local timer = minetest.get_node_timer(pos)
		timer:start(3)
	end,
	on_timer = function(pos)
		minetest.sound_play("emtraffic_solenoid",{gain=0.25,max_hear_distance=8,pos=pos})
		local meta = minetest.get_meta(pos)
		local timer = minetest.get_node_timer(pos)
		local state = meta:get_string("state")
		if state == "allreda" then
			state = "greena"
			minetest.after(0.5,function(pos)
				digiline:receptor_send(pos,digiline.rules.default,"a","RED")
				digiline:receptor_send(pos,digiline.rules.default,"b","RED")
				digiline:receptor_send(pos,digiline.rules.default,"c","RED")
				digiline:receptor_send(pos,digiline.rules.default,"d","RED")
				timer:start(3)
			end,pos)
		elseif state == "yellowa" then
			state = "allredb"
			minetest.after(0.5,function(pos)
				digiline:receptor_send(pos,digiline.rules.default,"a","YELLOW")
				digiline:receptor_send(pos,digiline.rules.default,"b","RED")
				digiline:receptor_send(pos,digiline.rules.default,"c","YELLOW")
				digiline:receptor_send(pos,digiline.rules.default,"d","RED")
			end,pos)
			timer:start(3)
		elseif state == "greena" then
			state = "yellowa"
			minetest.after(0.5,function(pos)
				digiline:receptor_send(pos,digiline.rules.default,"a","GREEN")
				digiline:receptor_send(pos,digiline.rules.default,"b","RED")
				digiline:receptor_send(pos,digiline.rules.default,"c","GREEN")
				digiline:receptor_send(pos,digiline.rules.default,"d","RED")
			end,pos)
			timer:start(15)
		elseif state == "allredb" then
			state = "greenb"
			minetest.after(0.5,function(pos)
				digiline:receptor_send(pos,digiline.rules.default,"a","RED")
				digiline:receptor_send(pos,digiline.rules.default,"b","RED")
				digiline:receptor_send(pos,digiline.rules.default,"c","RED")
				digiline:receptor_send(pos,digiline.rules.default,"d","RED")
			end,pos)
			timer:start(3)
		elseif state == "yellowb" then
			state = "allreda"
			minetest.after(0.5,function(pos)
				digiline:receptor_send(pos,digiline.rules.default,"a","RED")
				digiline:receptor_send(pos,digiline.rules.default,"b","YELLOW")
				digiline:receptor_send(pos,digiline.rules.default,"c","RED")
				digiline:receptor_send(pos,digiline.rules.default,"d","YELLOW")
			end,pos)
			timer:start(3)
		elseif state == "greenb" then
			state = "yellowb"
			minetest.after(0.5,function(pos)
				digiline:receptor_send(pos,digiline.rules.default,"a","RED")
				digiline:receptor_send(pos,digiline.rules.default,"b","GREEN")
				digiline:receptor_send(pos,digiline.rules.default,"c","RED")
				digiline:receptor_send(pos,digiline.rules.default,"d","GREEN")
			end,pos)
			timer:start(10)
		end
		meta:set_string("state",state)
	end,
	node_box = {
		type = "fixed",
		fixed = polemount_nodebox
    	},
	selection_box = {
		type = "fixed",
		fixed = polemount_nodebox
    	},
	digiline = 
	{
		receptor = {},
	},
})