summaryrefslogtreecommitdiff
path: root/mesecons_gates/init.lua
blob: ac1e4c9db470ef6bed4f032043ec4b2ed2cb0fa9 (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
outrules = {
	{x=1, y=0, z=0},
}
gates = {"diode", "not", "nand", "and", "xor"}
for g in ipairs(gates) do gate = gates[g]
	if g < 3 then
		rules = {
			{x=-1, y=0, z=0},
			{x=1, y=0, z=0},
		}
	else
		rules = {
			{x=0, y=0, z=1},
			{x=0, y=0, z=-1},
			{x=1, y=0, z=0},
		}
	end
	for on=0,1 do
		nodename = "mesecons_gates:"..gate
		if on == 1 then
			onoff = "on"
			drop = nodename.."_off"
			groups = {dig_immediate=2, not_in_creative_inventory=1, mesecon = 3}
			description = "You hacker you!"
			nodename = nodename.."_"..onoff
			mesecon:add_receptor_node(nodename, outrules)
		else
			onoff = "off"
			groups = {dig_immediate=2, mesecon = 3}
			description = gate.." Gate"
			nodename = nodename.."_"..onoff
			--mesecon:add_receptor_node_off(nodename, rules)
		end

		node_box = {
			type = "fixed",
			fixed = { -8/16, -8/16, -8/16, 8/16, -7/16, 8/16 },
		}

		tiles = "jeija_microcontroller_bottom.png^"..
			"jeija_gate_"..onoff..".png^"..
			"jeija_gate_"..gate..".png"

		minetest.register_node(nodename, {
			description = description,
			paramtype = "light",
			drawtype = "nodebox",
			tiles = {tiles},
			inventory_image = tiles,
			selection_box = node_box,
			node_box = node_box,
			walkable = true,
			on_construct = function(pos)
				local meta = minetest.env:get_meta(pos)
				meta:set_int("heat", 0)
				update_gate(pos)
			end,
			groups = groups,
			drop = drop,

		})

		mesecon:register_effector(nodename, nodename, rules)
	end
end

function get_gate(pos)
	string = minetest.env:get_node(pos).name
	string = string.gsub(string, "mesecons_gates:", "")
	--gate
	string = string.gsub(string, "_on", "")
	string = string.gsub(string, "_off", "")
	return string
end

function gate_state(pos)
	name = minetest.env:get_node(pos).name
	return string.find(name, "off") == nil
end

function pop_gate(pos)
	gate = get_gate(pos)
	minetest.env:remove_node(pos)
	minetest.after(0.2, yc_overheat_off, pos)
	minetest.env:add_item(pos, "mesecons_gates:"..gate.."_off")
end

function set_gate(pos, on)
	gate = get_gate(pos)
	local meta = minetest.env:get_meta(pos)
	local rules = {{x=1, y=0, z=0}}
	if on ~= gate_state(pos) then
		yc_heat(meta)
		minetest.after(0.5, yc_cool, meta)
		if yc_overheat(meta) then
			pop_gate(pos)
		else
			heat = meta:get_int("heat")
			if on then
				onoff = "_on"
			else
				onoff = "_off"
			end
			minetest.env:add_node(pos, {name="mesecons_gates:"..gate..onoff})
			local meta2 = minetest.env:get_meta(pos)
			meta2:set_int("heat", heat)
			if on then
				mesecon:receptor_on(pos, rules)
			else
				mesecon:receptor_off(pos, rules)
			end
		end
	end
end

function update_gate(pos)
	gate = get_gate(pos)
	L = yc_get_real_portstates(pos)
	if gate == "diode" then
		set_gate(pos, L.a)	
	elseif gate == "not" then
		set_gate(pos, not L.a)
	elseif gate == "nand" then
		set_gate(pos, not(L.b and L.d))
	elseif gate == "and" then
		set_gate(pos, L.b and L.d)
	else--if gate == "xor" then
		set_gate(pos, (L.b and not L.d) or (not L.b and L.d))
	end
end

mesecon:register_on_signal_change(function(pos,node)
	if string.find(node.name, "mesecons_gates:")~=nil then
		update_gate(pos)
	end
end)

minetest.register_craft({
	output = 'mesecons_gates:diode_off',
	recipe = {
		{'', '', ''},
		{'mesecons:mesecon', 'mesecons_torch:mesecon_torch_on', 'mesecons_torch:mesecon_torch_on'},
		{'', '', ''},
	},
})

minetest.register_craft({
	output = 'mesecons_gates:not_off',
	recipe = {
		{'', '', ''},
		{'mesecons:mesecon', 'mesecons_torch:mesecon_torch_on', 'mesecons:mesecon'},
		{'', '', ''},
	},
})

minetest.register_craft({
	output = 'mesecons_gates:and_off',
	recipe = {
		{'mesecons:mesecon', '', ''},
		{'', 'mesecons_materials:silicon', 'mesecons:mesecon'},
		{'mesecons:mesecon', '', ''},
	},
})

minetest.register_craft({
	output = 'mesecons_gates:nand_off',
	recipe = {
		{'mesecons:mesecon', '', ''},
		{'', 'mesecons_materials:silicon', 'mesecons_torch:mesecon_torch_on'},
		{'mesecons:mesecon', '', ''},
	},
})

minetest.register_craft({
	output = 'mesecons_gates:xor_off',
	recipe = {
		{'mesecons:mesecon', '', ''},
		{'', 'mesecons_materials:silicon', 'mesecons_materials:silicon'},
		{'mesecons:mesecon', '', ''},
	},
})