summaryrefslogtreecommitdiff
path: root/mesecons_dev/mesecons_wireless/init.lua
blob: 0d956e8609fc135848cf15029bae9c7b01577747 (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
--COMMON WIRELESS FUNCTIONS

mesecon.wireless_receivers={}

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 == "mesecons_wireless:wireless_receiver_off" or
	   minetest.env:get_node(pos).name == "mesecons_wireless:wireless_receiver_on"  or
	   minetest.env:get_node(pos).name == "mesecons_wireless:wireless_inverter_off" or
	   minetest.env:get_node(pos).name == "mesecons_wireless: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 == "mesecons_wireless:wireless_receiver_on" or
		   minetest.env:get_node(pos).name == "mesecons_wireless:wireless_receiver_off" or
		   minetest.env:get_node(pos).name == "mesecons_wireless:wireless_inverter_on" or
		   minetest.env:get_node(pos).name == "mesecons_wireless:wireless_inverter_off" then
			mesecon:set_wlre_channel(pos, "air")
		end	
	end
)

minetest.register_abm(
	{nodenames = {"mesecons_wireless:wireless_receiver_on", "mesecons_wireless:wireless_receiver_off",
		      "mesecons_wireless:wireless_inverter_on", "mesecons_wireless: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=="mesecons_wireless:wireless_receiver_on" and wlre.requested_state==0  then
			minetest.env:add_node(pos, {name="mesecons_wireless:wireless_receiver_off"})
			mesecon:receptor_off(pos)
		end
		if node.name=="mesecons_wireless:wireless_receiver_off" and wlre.requested_state==1  then
			minetest.env:add_node(pos, {name="mesecons_wireless:wireless_receiver_on"})
			mesecon:receptor_on(pos)
		end
		if node.name=="mesecons_wireless:wireless_inverter_off" and wlre.requested_state==0 and wlre.inverting==1 then
			minetest.env:add_node(pos, {name="mesecons_wireless:wireless_inverter_on"})
			mesecon:receptor_on(pos)
		end
		if node.name=="mesecons_wireless:wireless_inverter_on" and wlre.requested_state==1 and wlre.inverting==1 then
			minetest.env:add_node(pos, {name="mesecons_wireless:wireless_inverter_off"})
			mesecon:receptor_off(pos)
		end
	end,
})

--WIRELESS RECEIVER

minetest.register_node("mesecons_wireless: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"),
	groups = {choppy=2},
    	description="Wireless Receiver",
})

minetest.register_node("mesecons_wireless: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"),
	groups = {choppy=2},
	drop = 'mesecons_wireless:wireless_receiver_off',
    	description="Wireless Receiver",
})

minetest.register_craft({
	output = '"mesecons_wireless:wireless_receiver_off" 2',
	recipe = {
		{'', "mesecons:mesecon_off", ''},
		{'', "mesecons:mesecon_off", ''},
		{'', "mesecons_materials:ic", ''},
	}
})

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

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

minetest.register_abm( -- SAVE WIRELESS RECEIVERS TO FILE
	{nodenames = {"mesecons_wireless:wireless_receiver_off", "mesecons_wireless:wireless_receiver_on", "mesecons_wireless:wireless_inverter_on", "mesecons_wireless: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("mesecons_wireless:wireless_receiver_on")
mesecon:add_receptor_node_off("mesecons_wireless:wireless_receiver_off")

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

minetest.register_node("mesecons_wireless: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"),
	groups = {choppy=2},
	drop = 'mesecons_wireless:wireless_inverter_on',
    	description="Wireless Inverter",
})

minetest.register_node("mesecons_wireless: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"),
	groups = {choppy=2},
    	description="Wireless Inverter",
})

minetest.register_craft({
	output = '"mesecons_wireless:wireless_inverter_off" 2',
	recipe = {
		{'', 'default:steel_ingot', ''},
		{'mesecons_materials:ic', 'mesecons:mesecon_off', 'mesecons_materials:ic'},
		{'', 'mesecons:mesecon_off', ''},
	}
})

minetest.register_on_placenode(function(pos, newnode, placer)
	if newnode.name == "mesecons_wireless: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 == "mesecons_wireless:wireless_inverter_on" then
			mesecon:remove_wireless_receiver(pos)
			mesecon:receptor_off(pos)
		end	
		if oldnode.name == "mesecons_wireless:wireless_inverter_off" then
			mesecon:remove_wireless_receiver(pos)		
		end
	end
)

mesecon:add_receptor_node("mesecons_wireless:wireless_inverter_on")
mesecon:add_receptor_node_off("mesecons_wireless: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("mesecons_wireless: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"),
	groups = {choppy=2},
	drop = {'"mesecons_wireless:wireless_transmitter_off" 1'},
    	description="Wireless Transmitter",
})

minetest.register_node("mesecons_wireless: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"),
	groups = {choppy=2},
    	description="Wireless Transmitter",
})

minetest.register_craft({
	output = '"mesecons_wireless:wireless_transmitter_off" 2',
	recipe = {
		{'default:steel_ingot', 'mesecons:mesecon_off', 'default:steel_ingot'},
		{'', 'mesecons:mesecon_off', ''},
		{'', 'mesecons_materials:ic', ''},
	}
})

mesecon:register_on_signal_on(function(pos, node)
	if node.name=="mesecons_wireless:wireless_transmitter_off" then
		minetest.env:add_node(pos, {name="mesecons_wireless: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=="mesecons_wireless:wireless_transmitter_on" then
		minetest.env:add_node(pos, {name="mesecons_wireless: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)

mesecon:read_wlre_from_file()