summaryrefslogtreecommitdiff
path: root/mesecons_gates/init.lua
blob: 58872c8efc11adf91bbf162d50b44d388449003b (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
local selection_box = {
	type = "fixed",
	fixed = { -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 }
}

local nodebox = {
	type = "fixed",
	fixed = {
		{ -8/16, -8/16, -8/16, 8/16, -7/16, 8/16 }, -- bottom slab
		{ -6/16, -7/16, -6/16, 6/16, -6/16, 6/16 }
	},
}

local function gate_rotate_rules(node, rules)
	for rotations = 0, node.param2 - 1 do
		rules = mesecon.rotate_rules_left(rules)
	end
	return rules
end

local function gate_get_output_rules(node)
	return gate_rotate_rules(node, {{x=1, y=0, z=0}})
end

local function gate_get_input_rules_oneinput(node)
	return gate_rotate_rules(node, {{x=-1, y=0, z=0}})
end

local function gate_get_input_rules_twoinputs(node)
	return gate_rotate_rules(node, {{x=0, y=0, z=1, name="input1"},
		{x=0, y=0, z=-1, name="input2"}})
end

local function set_gate(pos, node, state)
	local gate = minetest.registered_nodes[node.name]

	if mesecon.do_overheat(pos) then
		minetest.remove_node(pos)
		mesecon.receptor_off(pos, gate_get_output_rules(node))
		minetest.add_item(pos, gate.drop)
	elseif state then
		minetest.swap_node(pos, {name = gate.onstate, param2=node.param2})
		mesecon.receptor_on(pos, gate_get_output_rules(node))
	else
		minetest.swap_node(pos, {name = gate.offstate, param2=node.param2})
		mesecon.receptor_off(pos, gate_get_output_rules(node))
	end
end

local function update_gate(pos, node, link, newstate)
	local gate = minetest.registered_nodes[node.name]

	if gate.inputnumber == 1 then
		set_gate(pos, node, gate.assess(newstate == "on"))
	elseif gate.inputnumber == 2 then
		local meta = minetest.get_meta(pos)
		meta:set_int(link.name, newstate == "on" and 1 or 0)

		local val1 = meta:get_int("input1") == 1
		local val2 = meta:get_int("input2") == 1
		set_gate(pos, node, gate.assess(val1, val2))
	end
end

local function register_gate(name, inputnumber, assess, recipe, description)
	local get_inputrules = inputnumber == 2 and gate_get_input_rules_twoinputs or
		gate_get_input_rules_oneinput
	description = "Logic Gate: "..name

	local basename = "mesecons_gates:"..name
	mesecon.register_node(basename, {
		description = description,
		inventory_image = "jeija_gate_off.png^jeija_gate_"..name..".png",
		paramtype = "light",
		paramtype2 = "facedir",
		is_ground_content = false,
		drawtype = "nodebox",
		drop = basename.."_off",
		selection_box = selection_box,
		node_box = nodebox,
		walkable = true,
		sounds = default.node_sound_stone_defaults(),
		assess = assess,
		onstate = basename.."_on",
		offstate = basename.."_off",
		inputnumber = inputnumber,
		after_dig_node = mesecon.do_cooldown,
	},{
		tiles = {
			"jeija_microcontroller_bottom.png^".."jeija_gate_off.png^"..
			"jeija_gate_output_off.png^".."jeija_gate_"..name..".png",
			"jeija_microcontroller_bottom.png^".."jeija_gate_output_off.png^"..
			"[transformFY",
			"jeija_gate_side.png^".."jeija_gate_side_output_off.png",
			"jeija_gate_side.png",
			"jeija_gate_side.png",
			"jeija_gate_side.png"
		},
		groups = {dig_immediate = 2, overheat = 1},
		mesecons = { receptor = {
			state = "off",
			rules = gate_get_output_rules
		}, effector = {
			rules = get_inputrules,
			action_change = update_gate
		}}
	},{
		tiles = {
			"jeija_microcontroller_bottom.png^".."jeija_gate_on.png^"..
			"jeija_gate_output_on.png^".."jeija_gate_"..name..".png",
			"jeija_microcontroller_bottom.png^".."jeija_gate_output_on.png^"..
			"[transformFY",
			"jeija_gate_side.png^".."jeija_gate_side_output_on.png",
			"jeija_gate_side.png",
			"jeija_gate_side.png",
			"jeija_gate_side.png"
		},
		groups = {dig_immediate = 2, not_in_creative_inventory = 1, overheat = 1},
		mesecons = { receptor = {
			state = "on",
			rules = gate_get_output_rules
		}, effector = {
			rules = get_inputrules,
			action_change = update_gate
		}}
	})

	minetest.register_craft({output = basename.."_off", recipe = recipe})
end

register_gate("diode", 1, function (input) return input end,
	{{"mesecons:mesecon", "mesecons_torch:mesecon_torch_on", "mesecons_torch:mesecon_torch_on"}},
	"Diode")

register_gate("not", 1, function (input) return not input end,
	{{"mesecons:mesecon", "mesecons_torch:mesecon_torch_on", "mesecons:mesecon"}},
	"NOT Gate")

register_gate("and", 2, function (val1, val2) return val1 and val2 end,
	{{"mesecons:mesecon", "", ""},
	 {"", "mesecons_materials:silicon", "mesecons:mesecon"},
	 {"mesecons:mesecon", "", ""}},
	"AND Gate")

register_gate("nand", 2, function (val1, val2) return not (val1 and val2) end,
	{{"mesecons:mesecon", "", ""},
	 {"", "mesecons_materials:silicon", "mesecons_torch:mesecon_torch_on"},
	 {"mesecons:mesecon", "", ""}},
	"NAND Gate")

register_gate("xor", 2, function (val1, val2) return (val1 or val2) and not (val1 and val2) end,
	{{"mesecons:mesecon", "", ""},
	 {"", "mesecons_materials:silicon", "mesecons_materials:silicon"},
	 {"mesecons:mesecon", "", ""}},
	"XOR Gate")

register_gate("nor", 2, function (val1, val2) return not (val1 or val2) end,
	{{"mesecons:mesecon", "", ""},
	 {"", "mesecons:mesecon", "mesecons_torch:mesecon_torch_on"},
	 {"mesecons:mesecon", "", ""}},
	"NOR Gate")

register_gate("or", 2, function (val1, val2) return (val1 or val2) end,
	{{"mesecons:mesecon", "", ""},
	 {"", "mesecons:mesecon", "mesecons:mesecon"},
	 {"mesecons:mesecon", "", ""}},
	"OR Gate")
	
local pulsetime = { 0.1, 0.3, 0.5, 1.0 } --Same as the delayer

local function pulsegate_on(pos,node)
	local basename = "mesecons_gates:pulse_"
	if string.sub(node.name,1,#basename) ~= basename or string.sub(node.name,-2,-1) == "on" then
		--Gate no longer exists or is already on
		return
	end
	local delay = tonumber(string.sub(node.name,#basename+1,#basename+1))
	node.name = basename..delay.."_on"
	minetest.swap_node(pos,node)
	mesecon.receptor_on(pos,gate_get_output_rules(node))
	if delay and pulsetime[delay] then minetest.get_node_timer(pos):start(pulsetime[delay]) end
end

local function pulsegate_off(pos)
	local node = minetest.get_node(pos)
	local basename = "mesecons_gates:pulse_"
	if string.sub(node.name,1,#basename) ~= basename or string.sub(node.name,-3,-1) == "off" then
		--Gate no longer exists or is already off
		return
	end
	local delay = string.sub(node.name,#basename+1,#basename+1)
	node.name = basename..delay.."_off"
	minetest.swap_node(pos,node)
	mesecon.receptor_off(pos,gate_get_output_rules(node))
end

for i=1,4,1 do
	mesecon.register_node("mesecons_gates:pulse_"..i, {
		description = "Logic Gate: Pulse",
		inventory_image = "jeija_gate_off.png^jeija_gate_pulse.png",
		paramtype = "light",
		paramtype2 = "facedir",
		is_ground_content = false,
		drawtype = "nodebox",
		drop = "mesecons_gates:pulse_1_off",
		selection_box = selection_box,
		node_box = nodebox,
		walkable = true,
		sounds = default.node_sound_stone_defaults(),
		onstate = "mesecons_gates:pulse_"..i.."_on",
		offstate = "mesecons_gates:pulse_"..i.."_off",
		after_dig_node = mesecon.do_cooldown,
	},{
		tiles = {
			"jeija_microcontroller_bottom.png^".."jeija_gate_off.png^"..
			"jeija_gate_output_off.png^".."jeija_gate_pulse.png^".."mesecons_gates_pulse_setting_"..i..".png",
			"jeija_microcontroller_bottom.png^".."jeija_gate_output_off.png^"..
			"[transformFY",
			"jeija_gate_side.png^".."jeija_gate_side_output_off.png",
			"jeija_gate_side.png",
			"jeija_gate_side.png",
			"jeija_gate_side.png"
		},
		on_punch = function(pos, node, puncher)
			if minetest.is_protected(pos, puncher and puncher:get_player_name() or "") then
				return
			end
			minetest.swap_node(pos, {
				name = "mesecons_gates:pulse_"..tostring(i % 4 + 1).."_off",
				param2 = node.param2
			})
		end,
		groups = i == 1 and {dig_immediate = 2} or {dig_immediate = 2,not_in_creative_inventory = 1},
		mesecons = { receptor = {
			state = "off",
			rules = gate_get_output_rules
		}, effector = {
			rules = gate_get_input_rules_oneinput,
			action_on = pulsegate_on,
		}}
	},{
		tiles = {
			"jeija_microcontroller_bottom.png^".."jeija_gate_on.png^"..
			"jeija_gate_output_on.png^".."jeija_gate_pulse.png^".."mesecons_gates_pulse_setting_"..i..".png",
			"jeija_microcontroller_bottom.png^".."jeija_gate_output_on.png^"..
			"[transformFY",
			"jeija_gate_side.png^".."jeija_gate_side_output_on.png",
			"jeija_gate_side.png",
			"jeija_gate_side.png",
			"jeija_gate_side.png"
		},
		on_timer = pulsegate_off,
		groups = {dig_immediate = 2, not_in_creative_inventory = 1},
		mesecons = { receptor = {
			state = "on",
			rules = gate_get_output_rules
		}}
	})
end

minetest.register_craft({
	output = "mesecons_gates:pulse_1_off",
	recipe = {
		{"group:mesecon_conductor_craftable","",""},
		{"mesecons_materials:silicon","mesecons_materials:silicon","group:mesecon_conductor_craftable"},
		{"group:mesecon_conductor_craftable","",""},
	},
})