1 -- naming scheme: wire:(xp)(zp)(xm)(zm)(xpyp)(zpyp)(xmyp)(zmyp)_on/off
2 -- where x= x direction, z= z direction, y= y direction, p = +1, m = -1, e.g. xpym = {x=1, y=-1, z=0}
3 -- The (xp)/(zpyp)/.. statements shall be replaced by either 0 or 1
4 -- Where 0 means the wire has no visual connection to that direction and
5 -- 1 means that the wire visually connects to that other node.
7 -- #######################
8 -- ## Update wire looks ##
9 -- #######################
12 {{x
=-1, y
= 0, z
= 0, spread
=true},
13 {x
= 1, y
= 0, z
= 0, spread
=true},
14 {x
= 0, y
=-1, z
= 0, spread
=true},
15 {x
= 0, y
= 1, z
= 0, spread
=true},
16 {x
= 0, y
= 0, z
=-1, spread
=true},
17 {x
= 0, y
= 0, z
= 1, spread
=true},
28 -- self_pos = pos of any mesecon node, from_pos = pos of conductor to getconnect for
29 local wire_getconnect
= function (from_pos
, self_pos
)
30 local node
= minetest
.get_node(self_pos
)
31 if minetest
.registered_nodes
[node
.name
]
32 and minetest
.registered_nodes
[node
.name
].mesecons
then
33 -- rules of node to possibly connect to
35 if (minetest
.registered_nodes
[node
.name
].mesecon_wire
) then
38 rules
= mesecon
.get_any_rules(node
)
41 for _
, r
in ipairs(mesecon
.flattenrules(rules
)) do
42 if (vector
.equals(vector
.add(self_pos
, r
), from_pos
)) then
51 local wire_updateconnect
= function (pos
)
52 local connections
= {}
54 for _
, r
in ipairs(wire_rules
) do
55 if wire_getconnect(pos
, vector
.add(pos
, r
)) then
56 table.insert(connections
, r
)
61 for _
, vec
in ipairs(connections
) do
63 if vec
.x
== 1 then nid
[0] = "1" end
64 if vec
.z
== 1 then nid
[1] = "1" end
65 if vec
.x
== -1 then nid
[2] = "1" end
66 if vec
.z
== -1 then nid
[3] = "1" end
70 if vec
.x
== 1 then nid
[4] = "1" end
71 if vec
.z
== 1 then nid
[5] = "1" end
72 if vec
.x
== -1 then nid
[6] = "1" end
73 if vec
.z
== -1 then nid
[7] = "1" end
77 local nodeid
= (nid
[0] or "0")..(nid
[1] or "0")..(nid
[2] or "0")..(nid
[3] or "0")
78 ..(nid
[4] or "0")..(nid
[5] or "0")..(nid
[6] or "0")..(nid
[7] or "0")
80 local state_suffix
= string.find(minetest
.get_node(pos
).name
, "_off") and "_off" or "_on"
81 minetest
.set_node(pos
, {name
= "mesecons:wire_"..nodeid
..state_suffix
})
84 local update_on_place_dig
= function (pos
, node
)
85 -- Update placed node (get_node again as it may have been dug)
86 local nn
= minetest
.get_node(pos
)
87 if (minetest
.registered_nodes
[nn
.name
])
88 and (minetest
.registered_nodes
[nn
.name
].mesecon_wire
) then
89 wire_updateconnect(pos
)
92 -- Update nodes around it
94 if minetest
.registered_nodes
[node
.name
]
95 and minetest
.registered_nodes
[node
.name
].mesecon_wire
then
98 rules
= mesecon
.get_any_rules(node
)
100 if (not rules
) then return end
102 for _
, r
in ipairs(mesecon
.flattenrules(rules
)) do
103 local np
= vector
.add(pos
, r
)
104 if minetest
.registered_nodes
[minetest
.get_node(np
).name
]
105 and minetest
.registered_nodes
[minetest
.get_node(np
).name
].mesecon_wire
then
106 wire_updateconnect(np
)
111 mesecon
.register_autoconnect_hook("wire", update_on_place_dig
)
113 -- ############################
114 -- ## Wire node registration ##
115 -- ############################
117 local box_center
= {-1/16, -.5, -1/16, 1/16, -.5+1/64, 1/16}
118 local box_bump1
= { -2/16, -8/16, -2/16, 2/16, -.5+1/64, 2/16 }
122 [0] = {1/16, -.5, -1/16, 8/16, -.5+1/64, 1/16}, -- x positive
123 [1] = {-1/16, -.5, 1/16, 1/16, -.5+1/64, 8/16}, -- z positive
124 [2] = {-8/16, -.5, -1/16, -1/16, -.5+1/64, 1/16}, -- x negative
125 [3] = {-1/16, -.5, -8/16, 1/16, -.5+1/64, -1/16}, -- z negative
127 [4] = {.5-1/16, -.5+1/16, -1/16, .5, .4999+1/64, 1/16}, -- x positive up
128 [5] = {-1/16, -.5+1/16, .5-1/16, 1/16, .4999+1/64, .5}, -- z positive up
129 [6] = {-.5, -.5+1/16, -1/16, -.5+1/16, .4999+1/64, 1/16}, -- x negative up
130 [7] = {-1/16, -.5+1/16, -.5, 1/16, .4999+1/64, -.5+1/16} -- z negative up
136 fixed
= {-.5, -.5, -.5, .5, -.5+1/16, .5}
139 -- go to the next nodeid (ex.: 01000011 --> 01000100)
140 local nid_inc
= function() end
141 nid_inc
= function (nid
)
143 while nid
[i
-1] ~= 1 do
144 nid
[i
] = (nid
[i
] ~= 1) and 1 or 0
148 -- BUT: Skip impossible nodeids:
149 if ((nid
[0] == 0 and nid
[4] == 1) or (nid
[1] == 0 and nid
[5] == 1)
150 or (nid
[2] == 0 and nid
[6] == 1) or (nid
[3] == 0 and nid
[7] == 1)) then
157 local function register_wires()
160 -- Create group specifiction and nodeid string (see note above for details)
161 local nodeid
= (nid
[0] or "0")..(nid
[1] or "0")..(nid
[2] or "0")..(nid
[3] or "0")
162 ..(nid
[4] or "0")..(nid
[5] or "0")..(nid
[6] or "0")..(nid
[7] or "0")
165 local nodebox
= {type = "fixed", fixed
={box_center
}}
168 table.insert(nodebox
.fixed
, nbox_nid
[i
])
172 -- Add bump to nodebox if curved
173 if (nid
[0] == 1 and nid
[1] == 1) or (nid
[1] == 1 and nid
[2] == 1)
174 or (nid
[2] == 1 and nid
[3] == 1) or (nid
[3] == 1 and nid
[0] == 1) then
175 table.insert(nodebox
.fixed
, box_bump1
)
178 -- If nothing to connect to, still make a nodebox of a straight wire
179 if nodeid
== "00000000" then
180 nodebox
.fixed
= {-8/16, -.5, -1/16, 8/16, -.5+1/16, 1/16}
183 local meseconspec_off
= { conductor
= {
185 state
= mesecon
.state
.off
,
186 onstate
= "mesecons:wire_"..nodeid
.."_on"
189 local meseconspec_on
= { conductor
= {
191 state
= mesecon
.state
.on
,
192 offstate
= "mesecons:wire_"..nodeid
.."_off"
195 local groups_on
= {dig_immediate
= 3, mesecon_conductor_craftable
= 1,
196 not_in_creative_inventory
= 1, attached_node
= 1, dig_by_water
= 1,destroy_by_lava_flow
=1, dig_by_piston
= 1}
197 local groups_off
= {dig_immediate
= 3, mesecon_conductor_craftable
= 1,
198 attached_node
= 1, dig_by_water
= 1,destroy_by_lava_flow
=1, dig_by_piston
= 1}
199 if nodeid
~= "00000000" then
200 groups_off
["not_in_creative_inventory"] = 1
204 local ratio_off
= 128
206 local crossing_off
= "(redstone_redstone_dust_dot.png^redstone_redstone_dust_line0.png^(redstone_redstone_dust_line1.png^[transformR90))^[colorize:#FF0000:"..ratio_off
207 local crossing_on
= "(redstone_redstone_dust_dot.png^redstone_redstone_dust_line0.png^(redstone_redstone_dust_line1.png^[transformR90))^[colorize:#FF0000:"..ratio_on
208 local straight0_off
= "redstone_redstone_dust_line0.png^[colorize:#FF0000:"..ratio_off
209 local straight0_on
= "redstone_redstone_dust_line0.png^[colorize:#FF0000:"..ratio_on
210 local straight1_off
= "redstone_redstone_dust_line0.png^[colorize:#FF0000:"..ratio_off
211 local straight1_on
= "redstone_redstone_dust_line0.png^[colorize:#FF0000:"..ratio_on
212 local dot_off
= "redstone_redstone_dust_dot.png^[colorize:#FF0000:"..ratio_off
213 local dot_on
= "redstone_redstone_dust_dot.png^[colorize:#FF0000:"..ratio_on
215 local tiles_off
= { crossing_off
, crossing_off
, straight0_off
, straight1_off
, straight0_off
, straight1_off
}
216 local tiles_on
= { crossing_on
, crossing_on
, straight0_on
, straight1_on
, straight0_on
, straight1_on
}
218 local wirehelp
, longdesc
, usagehelp
, img
, desc_off
, desc_on
219 if nodeid
== "00000000" then
220 -- Non-connected redstone wire
221 nodebox
.fixed
= {-8/16, -.5, -8/16, 8/16, -.5+1/64, 8/16}
223 tiles_off
= { dot_off
, dot_off
, "blank.png", "blank.png", "blank.png", "blank.png" }
224 tiles_on
= { dot_on
, dot_on
, "blank.png", "blank.png", "blank.png", "blank.png" }
226 longdesc
= [[Redstone is a versatile conductive mineral which transmits redstone power. It can be placed on the ground as a trail.
227 A redstone trail can be in two states: Powered or not powered. A powered redstone trail will power (and thus activate) adjacent redstone components.
228 Redstone power can be received from various redstone components, such as a block of redstone or a button. Redstone power is used to activate numerous mechanisms, such as redstone lamps or pistons.]]
229 usagehelp
= [[Place redstone on the ground to build a redstone trail. The trails will connect to each other automatically and it can also go over hills. An easy way to power a redstone trail is by placing a redstone torch.
231 Read the help entries on the other redstone components to learn how redstone components interact.]]
232 img
= "redstone_redstone_dust.png"
233 desc_off
= "Redstone"
234 desc_on
= "Powered Redstone Spot ("..nodeid
..")"
236 -- Connected redstone wire
237 table.insert(nodebox
, box_center
)
238 tiles_off
= { crossing_off
, crossing_off
, straight0_off
, straight1_off
, straight0_off
, straight1_off
, }
239 tiles_on
= { crossing_on
, crossing_on
, straight0_on
, straight1_on
, straight0_on
, straight1_on
, }
241 desc_off
= "Redstone Trail ("..nodeid
..")"
242 desc_on
= "Powered Redstone Trail ("..nodeid
..")"
245 mesecon
.register_node(":mesecons:wire_"..nodeid
, {
246 drawtype
= "nodebox",
248 sunlight_propagates
= true,
249 selection_box
= selectionbox
,
252 drop
= "mesecons:wire_00000000_off",
253 sounds
= mcl_sounds
.node_sound_defaults(),
254 is_ground_content
= false,
257 description
= desc_off
,
258 inventory_image
= img
,
260 _doc_items_create_entry
= wirehelp
,
261 _doc_items_longdesc
= longdesc
,
262 _doc_items_usagehelp
= usagehelp
,
264 mesecons
= meseconspec_off
,
267 description
= desc_on
,
268 _doc_items_create_entry
= false,
270 mesecons
= meseconspec_on
,
274 -- Add Help entry aliases for e.g. making it identifiable by the lookup tool [doc_identifier]
275 if minetest
.get_modpath("doc") then
276 if nodeid
~= "00000000" then
277 doc
.add_entry_alias("nodes", "mesecons:wire_00000000_off", "nodes", "mesecons:wire_"..nodeid
.."_off")
279 doc
.add_entry_alias("nodes", "mesecons:wire_00000000_off", "nodes", "mesecons:wire_"..nodeid
.."_on")
282 if (nid_inc(nid
) == false) then return end
287 minetest
.register_alias("mesecons:redstone", "mesecons:wire_00000000_off")
289 minetest
.register_craft({
291 output
= "mesecons:redstone",
292 recipe
= "mcl_core:stone_with_redstone",