summaryrefslogtreecommitdiff
path: root/jeija/wireless.lua
blob: 3344d47e631acfa4899ef6375bc2fdb6c7b1c20f (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
-- IC
minetest.register_craftitem("jeija:ic", {
	image = "jeija_ic.png",
	on_place_on_ground = minetest.craftitem_place_item,
})

minetest.register_craft({
	output = 'craft "jeija:ic" 2',
	recipe = {
		{'craft "jeija:silicon"', 'craft "jeija:silicon"', 'node "jeija:mesecon_off"'},
		{'craft "jeija:silicon"', 'craft "jeija:silicon"', 'node "jeija:mesecon_off"'},
		{'node "jeija:mesecon_off"', 'node "jeija:mesecon_off"', ''},
	}
})

--COMMON WIRELESS FUNCTIONS

function mesecon:read_wlre_from_file()
	print "[MESEcons] Reading Mesecon Data..."
	mesecon_file=io.open(minetest.get_modpath("jeija").."/mesecon_data", "r")
	if mesecon_file==nil then return end
	local row=mesecon_file:read()
	local i=1
	while row~=nil do
		mesecon.wireless_receivers[i]={}
		mesecon.wireless_receivers[i].pos={}
		mesecon.wireless_receivers[i].pos.x=tonumber(mesecon_file:read())
		mesecon.wireless_receivers[i].pos.y=tonumber(mesecon_file:read())
		mesecon.wireless_receivers[i].pos.z=tonumber(mesecon_file:read())
		mesecon.wireless_receivers[i].channel=mesecon_file:read()
		mesecon.wireless_receivers[i].requested_state=tonumber(mesecon_file:read())
		mesecon.wireless_receivers[i].inverting=tonumber(mesecon_file:read())
		i=i+1
		row=mesecon_file:read()
	end
	mesecon_file:close()	
	print "[MESEcons] Finished Reading Mesecon Data..."
end


function mesecon:register_wireless_receiver(pos, inverting)
	local i	= 1	
	repeat
		if mesecon.wireless_receivers[i]==nil then break end
		i=i+1
	until false


	local node_under_pos={}
	node_under_pos.x=pos.x
	node_under_pos.y=pos.y
	node_under_pos.z=pos.z

	node_under_pos.y=node_under_pos.y-1
	local node_under=minetest.env:get_node(node_under_pos)
	mesecon.wireless_receivers[i]={}
	mesecon.wireless_receivers[i].pos={}
	mesecon.wireless_receivers[i].pos.x=pos.x
	mesecon.wireless_receivers[i].pos.y=pos.y
	mesecon.wireless_receivers[i].pos.z=pos.z
	mesecon.wireless_receivers[i].channel=node_under.name
	mesecon.wireless_receivers[i].requested_state=0
	mesecon.wireless_receivers[i].inverting=inverting
end

function mesecon:remove_wireless_receiver(pos)
	local i = 1
	while mesecon.wireless_receivers[i]~=nil do
		if mesecon.wireless_receivers[i].pos.x==pos.x and
		   mesecon.wireless_receivers[i].pos.y==pos.y and
		   mesecon.wireless_receivers[i].pos.z==pos.z then
			mesecon.wireless_receivers[i]=nil
			break
		end
		i=i+1
	end
end

function mesecon:set_wlre_channel(pos, channel)
	--local i = 1
	--while mesecon.wireless_receivers[i]~=nil do
	--	if tonumber(mesecon.wireless_receivers[i].pos.x)==tonumber(pos.x) and
	--	   tonumber(mesecon.wireless_receivers[i].pos.y)==tonumber(pos.y) and
	--	   tonumber(mesecon.wireless_receivers[i].pos.z)==tonumber(pos.z) then
	--		mesecon.wireless_receivers[i].channel=channel
	--		break
	--	end
	--	i=i+1
	--end
	local wlre=mesecon:get_wlre(pos)
	if wlre~=nil then 
		wlre.channel=channel
	end
end

function mesecon:get_wlre(pos)
	local i=1
	while mesecon.wireless_receivers[i]~=nil do
		if mesecon.wireless_receivers[i].pos.x==pos.x and
		   mesecon.wireless_receivers[i].pos.y==pos.y and
		   mesecon.wireless_receivers[i].pos.z==pos.z then
			return mesecon.wireless_receivers[i]
		end
		i=i+1
	end
end

minetest.register_on_placenode(function(pos, newnode, placer)
	pos.y=pos.y+1
	if minetest.env:get_node(pos).name == "jeija:wireless_receiver_off" or
	   minetest.env:get_node(pos).name == "jeija:wireless_receiver_on"  or
	   minetest.env:get_node(pos).name == "jeija:wireless_inverter_off" or
	   minetest.env:get_node(pos).name == "jeija:wireless_inverter_on" then
		mesecon:set_wlre_channel(pos, newnode.name)
	end
end)

minetest.register_on_dignode(
	function(pos, oldnode, digger)
		local channel
		pos.y=pos.y+1
		if minetest.env:get_node(pos).name == "jeija:wireless_receiver_on" or
		   minetest.env:get_node(pos).name == "jeija:wireless_receiver_off" or
		   minetest.env:get_node(pos).name == "jeija:wireless_inverter_on" or
		   minetest.env:get_node(pos).name == "jeija:wireless_inverter_off" then
			mesecon:set_wlre_channel(pos, "air")
		end	
	end
)

minetest.register_abm(
	{nodenames = {"jeija:wireless_receiver_on", "jeija:wireless_receiver_off",
		      "jeija:wireless_inverter_on", "jeija:wireless_inverter_off"},
	interval = 1.0,
	chance = 1,
	action = function(pos, node, active_object_count, active_object_count_wider)
		local wlre=mesecon:get_wlre(pos)
		if (wlre==nil) then return end
		
		if node.name=="jeija:wireless_receiver_on" and wlre.requested_state==0  then
			minetest.env:add_node(pos, {name="jeija:wireless_receiver_off"})
			mesecon:receptor_off(pos)
		end
		if node.name=="jeija:wireless_receiver_off" and wlre.requested_state==1  then
			minetest.env:add_node(pos, {name="jeija:wireless_receiver_on"})
			mesecon:receptor_on(pos)
		end
		if node.name=="jeija:wireless_inverter_off" and wlre.requested_state==0 and wlre.inverting==1 then
			minetest.env:add_node(pos, {name="jeija:wireless_inverter_on"})
			mesecon:receptor_on(pos)
		end
		if node.name=="jeija:wireless_inverter_on" and wlre.requested_state==1 and wlre.inverting==1 then
			minetest.env:add_node(pos, {name="jeija:wireless_inverter_off"})
			mesecon:receptor_off(pos)
		end
	end,
})

--WIRELESS RECEIVER

minetest.register_node("jeija:wireless_receiver_off", {
	tile_images = {"jeija_wireless_receiver_tb_off.png", "jeija_wireless_receiver_tb_off.png", "jeija_wireless_receiver_off.png", "jeija_wireless_receiver_off.png", "jeija_wireless_receiver_off.png", "jeija_wireless_receiver_off.png"},
	inventory_image = minetest.inventorycube("jeija_wireless_receiver_off.png"),
	material = minetest.digprop_constanttime(0.8),
})

minetest.register_node("jeija:wireless_receiver_on", {
	tile_images = {"jeija_wireless_receiver_tb_on.png", "jeija_wireless_receiver_tb_on.png", "jeija_wireless_receiver_on.png", "jeija_wireless_receiver_on.png", "jeija_wireless_receiver_on.png", "jeija_wireless_receiver_on.png"},
	inventory_image = minetest.inventorycube("jeija_wireless_receiver_on.png"),
	material = minetest.digprop_constanttime(0.8),
	dug_item = 'node "jeija:wireless_receiver_off" 1'
})

minetest.register_craft({
	output = 'node "jeija:wireless_receiver_off" 2',
	recipe = {
		{'', 'node "jeija:mesecon_off"', ''},
		{'', 'node "jeija:mesecon_off"', ''},
		{'', 'craft "jeija:ic"', ''},
	}
})

minetest.register_on_placenode(function(pos, newnode, placer)
	if newnode.name == "jeija:wireless_receiver_off" then
		mesecon:register_wireless_receiver(pos, 0)
	end
end)

minetest.register_on_dignode(
	function(pos, oldnode, digger)
		if oldnode.name == "jeija:wireless_receiver_on" then
			mesecon:remove_wireless_receiver(pos)
			mesecon:receptor_off(pos)
		end	
		if oldnode.name == "jeija:wireless_receiver_off" then
			mesecon:remove_wireless_receiver(pos)		
		end
	end
)

minetest.register_abm( -- SAVE WIRELESS RECEIVERS TO FILE
	{nodenames = {"jeija:wireless_receiver_off", "jeija:wireless_receiver_on", "jeija:wireless_inverter_on", "jeija:wireless_inverter_off"},
	interval = 10,
	chance = 1,
	action = function(pos, node, active_object_count, active_object_count_wider)
		local mesecon_file = io.open(minetest.get_modpath("jeija").."/mesecon_data", "w")
		local i=1
		while mesecon.wireless_receivers[i]~=nil do
			mesecon_file:write("NEXT\n")
			mesecon_file:write(mesecon.wireless_receivers[i].pos.x.."\n")
			mesecon_file:write(mesecon.wireless_receivers[i].pos.y.."\n")
			mesecon_file:write(mesecon.wireless_receivers[i].pos.z.."\n")
			mesecon_file:write(mesecon.wireless_receivers[i].channel.."\n")
			mesecon_file:write(mesecon.wireless_receivers[i].requested_state.."\n")
			mesecon_file:write(mesecon.wireless_receivers[i].inverting.."\n")
			i=i+1
		end
		mesecon_file:close()
	end, 
})

mesecon:add_receptor_node("jeija:wireless_receiver_on")
mesecon:add_receptor_node_off("jeija:wireless_receiver_off")

-- WIRELESS INVERTER OFF/ON BELONGS TO THE OUTPUT STATE (ON=INPUT OFF)

minetest.register_node("jeija:wireless_inverter_off", {
	tile_images = {"jeija_wireless_inverter_tb.png", "jeija_wireless_inverter_tb.png", "jeija_wireless_inverter_off.png", "jeija_wireless_inverter_off.png", "jeija_wireless_inverter_off.png", "jeija_wireless_inverter_off.png"},
	inventory_image = minetest.inventorycube("jeija_wireless_inverter_off.png"),
	material = minetest.digprop_constanttime(0.8),
	dug_item = 'node "jeija:wireless_inverter_on" 1'
})

minetest.register_node("jeija:wireless_inverter_on", {
	tile_images = {"jeija_wireless_inverter_tb.png", "jeija_wireless_inverter_tb.png", "jeija_wireless_inverter_on.png", "jeija_wireless_inverter_on.png", "jeija_wireless_inverter_on.png", "jeija_wireless_inverter_on.png"},
	inventory_image = minetest.inventorycube("jeija_wireless_inverter_on.png"),
	material = minetest.digprop_constanttime(0.8),
})

minetest.register_craft({
	output = 'node "jeija:wireless_inverter_off" 2',
	recipe = {
		{'', 'craft "default:steel_ingot"', ''},
		{'craft "jeija:ic"', 'node "jeija:mesecon_off"', 'craft "jeija:ic"'},
		{'', 'node "jeija:mesecon_off"', ''},
	}
})

minetest.register_on_placenode(function(pos, newnode, placer)
	if newnode.name == "jeija:wireless_inverter_on" then
		mesecon:register_wireless_receiver(pos, 1)
		mesecon:receptor_on(pos)
	end
end)

minetest.register_on_dignode(
	function(pos, oldnode, digger)
		if oldnode.name == "jeija:wireless_inverter_on" then
			mesecon:remove_wireless_receiver(pos)
			mesecon:receptor_off(pos)
		end	
		if oldnode.name == "jeija:wireless_inverter_off" then
			mesecon:remove_wireless_receiver(pos)		
		end
	end
)

mesecon:add_receptor_node("jeija:wireless_inverter_on")
mesecon:add_receptor_node_off("jeija:wireless_inverter_off")

-- WIRELESS TRANSMITTER

function mesecon:wireless_transmit(channel, senderstate)
	local i = 1
	while mesecon.wireless_receivers[i]~=nil do
		if mesecon.wireless_receivers[i].channel==channel then
			if senderstate==1 then
				mesecon.wireless_receivers[i].requested_state=1
			elseif senderstate==0 then
				mesecon.wireless_receivers[i].requested_state=0
			end
		end
		i=i+1
	end
end

minetest.register_node("jeija:wireless_transmitter_on", {
	tile_images = {"jeija_wireless_transmitter_tb.png", "jeija_wireless_transmitter_tb.png", "jeija_wireless_transmitter_on.png", "jeija_wireless_transmitter_on.png", "jeija_wireless_transmitter_on.png", "jeija_wireless_transmitter_on.png"},
	inventory_image = minetest.inventorycube("jeija_wireless_transmitter_on.png"),
	material = minetest.digprop_constanttime(0.8),
	dug_item = 'node "jeija:wireless_transmitter_off" 1',
})

minetest.register_node("jeija:wireless_transmitter_off", {
	tile_images = {"jeija_wireless_transmitter_tb.png", "jeija_wireless_transmitter_tb.png", "jeija_wireless_transmitter_off.png", "jeija_wireless_transmitter_off.png", "jeija_wireless_transmitter_off.png", "jeija_wireless_transmitter_off.png"},
	inventory_image = minetest.inventorycube("jeija_wireless_transmitter_off.png"),
	material = minetest.digprop_constanttime(0.8),
})

minetest.register_craft({
	output = 'node "jeija:wireless_transmitter_off" 2',
	recipe = {
		{'craft "default:steel_ingot"', 'node "jeija:mesecon_off"', 'craft "default:steel_ingot"'},
		{'', 'node "jeija:mesecon_off"', ''},
		{'', 'craft "jeija:ic"', ''},
	}
})

mesecon:register_on_signal_on(function(pos, node)
	if node.name=="jeija:wireless_transmitter_off" then
		minetest.env:add_node(pos, {name="jeija:wireless_transmitter_on"})
		local node_under_pos=pos
		node_under_pos.y=node_under_pos.y-1
		local node_under=minetest.env:get_node(node_under_pos)
		mesecon:wireless_transmit(node_under.name, 1)
	end
end)

mesecon:register_on_signal_off(function(pos, node)
	if node.name=="jeija:wireless_transmitter_on" then
		minetest.env:add_node(pos, {name="jeija:wireless_transmitter_off"})
		local node_under_pos=pos
		node_under_pos.y=node_under_pos.y-1
		local node_under=minetest.env:get_node(node_under_pos)
		mesecon:wireless_transmit(node_under.name, 0)
	end
end)