Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e7f6ca2

Browse files
committedJun 18, 2013
Merge pull request #110 from Novatux/wireless
Add wireless emitters/recievers.
2 parents c14cb33 + 1d11a20 commit e7f6ca2

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed
 

‎mesecons_wireless/depends.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mesecons
2+

‎mesecons_wireless/init.lua

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
2+
local wireless_filename=minetest.get_worldpath() .. "/wireless"
3+
4+
local function read_file()
5+
local f = io.open(wireless_filename, "r")
6+
if f==nil then return {} end
7+
local t = f:read("*all")
8+
f:close()
9+
if t=="" or t==nil then return {} end
10+
return minetest.deserialize(t)
11+
end
12+
13+
local function write_file(tbl)
14+
local f = io.open(wireless_filename, "w")
15+
f:write(minetest.serialize(tbl))
16+
f:close()
17+
end
18+
19+
local function add_wireless_receiver(pos,channel)
20+
local tbl=read_file()
21+
for _,val in ipairs(tbl) do
22+
if val.x==pos.x and val.y==pos.y and val.z==pos.z then
23+
return
24+
end
25+
end
26+
table.insert(tbl,{x=pos.x,y=pos.y,z=pos.z,channel=channel})
27+
write_file(tbl)
28+
end
29+
30+
local function remove_wireless_receiver(pos)
31+
local tbl=read_file()
32+
local newtbl={}
33+
for _,val in ipairs(tbl) do
34+
if val.x~=pos.x or val.y~=pos.y or val.z~=pos.z then
35+
table.insert(newtbl,val)
36+
end
37+
end
38+
write_file(newtbl)
39+
end
40+
41+
local function get_wireless_receivers(channel)
42+
local tbl=read_file()
43+
local newtbl={}
44+
local changed=false
45+
for _,val in ipairs(tbl) do
46+
local node = minetest.env:get_node(val)
47+
local meta = minetest.env:get_meta(val)
48+
if node.name~="ignore" and val.channel~=meta:get_string("channel") then
49+
val.channel=meta:get_string("channel")
50+
changed=true
51+
end
52+
if val.channel==channel and node.name~="ignore" then
53+
table.insert(newtbl,val)
54+
end
55+
end
56+
if changed then write_file(tbl) end
57+
return newtbl
58+
end
59+
60+
minetest.register_node("mesecons_wireless:emitter", {
61+
description = "Wireless emitter",
62+
paramtype = "light",
63+
drawtype = "normal",
64+
tiles = {"mesecons_wireless_emitter.png"},
65+
walkable = true,
66+
on_construct = function(pos)
67+
local meta = minetest.env:get_meta(pos)
68+
meta:set_string("channel","")
69+
meta:set_string("formspec","size[9,1;]field[0,0.5;9,1;channel;Channel:;${channel}]")
70+
end,
71+
on_receive_fields = function(pos,formname,fields,sender)
72+
local meta = minetest.env:get_meta(pos)
73+
meta:set_string("channel",fields.channel)
74+
end,
75+
groups = {dig_immediate=2},
76+
sounds = default.node_sound_stone_defaults(),
77+
mesecons =
78+
{
79+
effector =
80+
{
81+
action_on = function(pos)
82+
local meta = minetest.env:get_meta(pos)
83+
local channel = meta:get_string("channel")
84+
local w = get_wireless_receivers(channel)
85+
for _,i in ipairs(w) do
86+
mesecon:receptor_on(i)
87+
mesecon:swap_node(i, "mesecons_wireless:receiver_on")
88+
end
89+
end,
90+
action_off = function(pos)
91+
local meta = minetest.env:get_meta(pos)
92+
local channel = meta:get_string("channel")
93+
local w = get_wireless_receivers(channel)
94+
for _,i in ipairs(w) do
95+
mesecon:receptor_off(i)
96+
mesecon:swap_node(i, "mesecons_wireless:receiver")
97+
end
98+
end,
99+
}
100+
}
101+
})
102+
103+
minetest.register_node("mesecons_wireless:receiver", {
104+
description = "Wireless receiver",
105+
paramtype = "light",
106+
drawtype = "normal",
107+
tiles = {"mesecons_wireless_receiver.png"},
108+
walkable = true,
109+
on_construct = function(pos)
110+
local meta = minetest.env:get_meta(pos)
111+
meta:set_string("channel","")
112+
meta:set_string("formspec","size[9,1;]field[0,0.5;9,1;channel;Channel:;${channel}]")
113+
add_wireless_receiver(pos,"")
114+
end,
115+
on_receive_fields = function(pos,formname,fields,sender)
116+
local meta = minetest.env:get_meta(pos)
117+
meta:set_string("channel",fields.channel)
118+
remove_wireless_receiver(pos)
119+
add_wireless_receiver(pos,fields.channel)
120+
end,
121+
on_destruct = function(pos)
122+
remove_wireless_receiver(pos)
123+
end,
124+
groups = {dig_immediate=2},
125+
sounds = default.node_sound_stone_defaults(),
126+
mesecons =
127+
{
128+
receptor =
129+
{
130+
state = "off",
131+
},
132+
}
133+
})
134+
135+
minetest.register_node("mesecons_wireless:receiver_on", {
136+
description = "Wireless receiver on (you hacker you)",
137+
paramtype = "light",
138+
drawtype = "normal",
139+
tiles = {"mesecons_wireless_receiver_on.png"},
140+
walkable = true,
141+
on_construct = function(pos)
142+
local meta = minetest.env:get_meta(pos)
143+
meta:set_string("channel","")
144+
meta:set_string("formspec","size[9,1;]field[0,0.5;9,1;channel;Channel:;${channel}]")
145+
add_wireless_receiver(pos,"")
146+
end,
147+
on_receive_fields = function(pos,formname,fields,sender)
148+
local meta = minetest.env:get_meta(pos)
149+
meta:set_string("channel",fields.channel)
150+
remove_wireless_receiver(pos)
151+
add_wireless_receiver(pos,fields.channel)
152+
end,
153+
on_destruct = function(pos)
154+
remove_wireless_receiver(pos)
155+
end,
156+
groups = {dig_immediate=2, not_in_creative_inventory=1},
157+
drop = "mesecons_wireless:receiver",
158+
sounds = default.node_sound_stone_defaults(),
159+
mesecons =
160+
{
161+
receptor =
162+
{
163+
state = "on",
164+
},
165+
}
166+
})
167+

0 commit comments

Comments
 (0)
Please sign in to comment.