Update helptext of obsidian
[MineClone/MineClone2.git] / mods / ITEMS / REDSTONE / mesecons_wires / init.lua
blob75acd3ce03d9a11a31689c5c0a67245ace490599
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 local S = minetest.get_translator("mesecons_wires")
9 -- #######################
10 -- ## Update wire looks ##
11 -- #######################
13 local wire_rules =
14 {{x=-1, y= 0, z= 0, spread=true},
15 {x= 1, y= 0, z= 0, spread=true},
16 {x= 0, y=-1, z= 0, spread=true},
17 {x= 0, y= 1, z= 0, spread=true},
18 {x= 0, y= 0, z=-1, spread=true},
19 {x= 0, y= 0, z= 1, spread=true},
21 {x= 1, y= 1, z= 0},
22 {x= 1, y=-1, z= 0},
23 {x=-1, y= 1, z= 0},
24 {x=-1, y=-1, z= 0},
25 {x= 0, y= 1, z= 1},
26 {x= 0, y=-1, z= 1},
27 {x= 0, y= 1, z=-1},
28 {x= 0, y=-1, z=-1}}
30 -- self_pos = pos of any mesecon node, from_pos = pos of conductor to getconnect for
31 local wire_getconnect = function (from_pos, self_pos)
32 local node = minetest.get_node(self_pos)
33 if minetest.registered_nodes[node.name]
34 and minetest.registered_nodes[node.name].mesecons then
35 -- rules of node to possibly connect to
36 local rules = {}
37 if (minetest.registered_nodes[node.name].mesecon_wire) then
38 rules = wire_rules
39 else
40 rules = mesecon.get_any_rules(node)
41 end
43 for _, r in ipairs(mesecon.flattenrules(rules)) do
44 if (vector.equals(vector.add(self_pos, r), from_pos)) then
45 return true
46 end
47 end
48 end
49 return false
50 end
52 -- Update this node
53 local wire_updateconnect = function (pos)
54 local connections = {}
56 for _, r in ipairs(wire_rules) do
57 if wire_getconnect(pos, vector.add(pos, r)) then
58 table.insert(connections, r)
59 end
60 end
62 local nid = {}
63 for _, vec in ipairs(connections) do
64 -- flat component
65 if vec.x == 1 then nid[0] = "1" end
66 if vec.z == 1 then nid[1] = "1" end
67 if vec.x == -1 then nid[2] = "1" end
68 if vec.z == -1 then nid[3] = "1" end
70 -- slopy component
71 if vec.y == 1 then
72 if vec.x == 1 then nid[4] = "1" end
73 if vec.z == 1 then nid[5] = "1" end
74 if vec.x == -1 then nid[6] = "1" end
75 if vec.z == -1 then nid[7] = "1" end
76 end
77 end
79 local nodeid = (nid[0] or "0")..(nid[1] or "0")..(nid[2] or "0")..(nid[3] or "0")
80 ..(nid[4] or "0")..(nid[5] or "0")..(nid[6] or "0")..(nid[7] or "0")
82 local state_suffix = string.find(minetest.get_node(pos).name, "_off") and "_off" or "_on"
83 minetest.set_node(pos, {name = "mesecons:wire_"..nodeid..state_suffix})
84 end
86 local update_on_place_dig = function (pos, node)
87 -- Update placed node (get_node again as it may have been dug)
88 local nn = minetest.get_node(pos)
89 if (minetest.registered_nodes[nn.name])
90 and (minetest.registered_nodes[nn.name].mesecon_wire) then
91 wire_updateconnect(pos)
92 end
94 -- Update nodes around it
95 local rules
96 if minetest.registered_nodes[node.name]
97 and minetest.registered_nodes[node.name].mesecon_wire then
98 rules = wire_rules
99 else
100 rules = mesecon.get_any_rules(node)
102 if (not rules) then return end
104 for _, r in ipairs(mesecon.flattenrules(rules)) do
105 local np = vector.add(pos, r)
106 if minetest.registered_nodes[minetest.get_node(np).name]
107 and minetest.registered_nodes[minetest.get_node(np).name].mesecon_wire then
108 wire_updateconnect(np)
113 mesecon.register_autoconnect_hook("wire", update_on_place_dig)
115 -- ############################
116 -- ## Wire node registration ##
117 -- ############################
118 -- Nodeboxes:
119 local box_center = {-1/16, -.5, -1/16, 1/16, -.5+1/64, 1/16}
120 local box_bump1 = { -2/16, -8/16, -2/16, 2/16, -.5+1/64, 2/16 }
122 local nbox_nid =
124 [0] = {1/16, -.5, -1/16, 8/16, -.5+1/64, 1/16}, -- x positive
125 [1] = {-1/16, -.5, 1/16, 1/16, -.5+1/64, 8/16}, -- z positive
126 [2] = {-8/16, -.5, -1/16, -1/16, -.5+1/64, 1/16}, -- x negative
127 [3] = {-1/16, -.5, -8/16, 1/16, -.5+1/64, -1/16}, -- z negative
129 [4] = {.5-1/16, -.5+1/16, -1/16, .5, .4999+1/64, 1/16}, -- x positive up
130 [5] = {-1/16, -.5+1/16, .5-1/16, 1/16, .4999+1/64, .5}, -- z positive up
131 [6] = {-.5, -.5+1/16, -1/16, -.5+1/16, .4999+1/64, 1/16}, -- x negative up
132 [7] = {-1/16, -.5+1/16, -.5, 1/16, .4999+1/64, -.5+1/16} -- z negative up
135 local selectionbox =
137 type = "fixed",
138 fixed = {-.5, -.5, -.5, .5, -.5+1/16, .5}
141 -- go to the next nodeid (ex.: 01000011 --> 01000100)
142 local nid_inc = function() end
143 nid_inc = function (nid)
144 local i = 0
145 while nid[i-1] ~= 1 do
146 nid[i] = (nid[i] ~= 1) and 1 or 0
147 i = i + 1
150 -- BUT: Skip impossible nodeids:
151 if ((nid[0] == 0 and nid[4] == 1) or (nid[1] == 0 and nid[5] == 1)
152 or (nid[2] == 0 and nid[6] == 1) or (nid[3] == 0 and nid[7] == 1)) then
153 return nid_inc(nid)
156 return i <= 8
159 local function register_wires()
160 local nid = {}
161 while true do
162 -- Create group specifiction and nodeid string (see note above for details)
163 local nodeid = (nid[0] or "0")..(nid[1] or "0")..(nid[2] or "0")..(nid[3] or "0")
164 ..(nid[4] or "0")..(nid[5] or "0")..(nid[6] or "0")..(nid[7] or "0")
166 -- Calculate nodebox
167 local nodebox = {type = "fixed", fixed={box_center}}
168 for i=0,7 do
169 if nid[i] == 1 then
170 table.insert(nodebox.fixed, nbox_nid[i])
174 -- Add bump to nodebox if curved
175 if (nid[0] == 1 and nid[1] == 1) or (nid[1] == 1 and nid[2] == 1)
176 or (nid[2] == 1 and nid[3] == 1) or (nid[3] == 1 and nid[0] == 1) then
177 table.insert(nodebox.fixed, box_bump1)
180 -- If nothing to connect to, still make a nodebox of a straight wire
181 if nodeid == "00000000" then
182 nodebox.fixed = {-8/16, -.5, -1/16, 8/16, -.5+1/16, 1/16}
185 local meseconspec_off = { conductor = {
186 rules = wire_rules,
187 state = mesecon.state.off,
188 onstate = "mesecons:wire_"..nodeid.."_on"
191 local meseconspec_on = { conductor = {
192 rules = wire_rules,
193 state = mesecon.state.on,
194 offstate = "mesecons:wire_"..nodeid.."_off"
197 local groups_on = {dig_immediate = 3, mesecon_conductor_craftable = 1,
198 not_in_creative_inventory = 1, attached_node = 1, dig_by_water = 1,destroy_by_lava_flow=1, dig_by_piston = 1}
199 local groups_off = {dig_immediate = 3, mesecon_conductor_craftable = 1,
200 attached_node = 1, dig_by_water = 1,destroy_by_lava_flow=1, dig_by_piston = 1, craftitem = 1}
201 if nodeid ~= "00000000" then
202 groups_off["not_in_creative_inventory"] = 1
205 -- Wire textures
206 local ratio_off = 128
207 local ratio_on = 192
208 local crossing_off = "(redstone_redstone_dust_dot.png^redstone_redstone_dust_line0.png^(redstone_redstone_dust_line1.png^[transformR90))^[colorize:#FF0000:"..ratio_off
209 local crossing_on = "(redstone_redstone_dust_dot.png^redstone_redstone_dust_line0.png^(redstone_redstone_dust_line1.png^[transformR90))^[colorize:#FF0000:"..ratio_on
210 local straight0_off = "redstone_redstone_dust_line0.png^[colorize:#FF0000:"..ratio_off
211 local straight0_on = "redstone_redstone_dust_line0.png^[colorize:#FF0000:"..ratio_on
212 local straight1_off = "redstone_redstone_dust_line0.png^[colorize:#FF0000:"..ratio_off
213 local straight1_on = "redstone_redstone_dust_line0.png^[colorize:#FF0000:"..ratio_on
214 local dot_off = "redstone_redstone_dust_dot.png^[colorize:#FF0000:"..ratio_off
215 local dot_on = "redstone_redstone_dust_dot.png^[colorize:#FF0000:"..ratio_on
217 local tiles_off = { crossing_off, crossing_off, straight0_off, straight1_off, straight0_off, straight1_off }
218 local tiles_on = { crossing_on, crossing_on, straight0_on, straight1_on, straight0_on, straight1_on }
220 local wirehelp, tt, longdesc, usagehelp, img, desc_off, desc_on
221 if nodeid == "00000000" then
222 -- Non-connected redstone wire
223 nodebox.fixed = {-8/16, -.5, -8/16, 8/16, -.5+1/64, 8/16}
224 -- “Dot” texture
225 tiles_off = { dot_off, dot_off, "blank.png", "blank.png", "blank.png", "blank.png" }
226 tiles_on = { dot_on, dot_on, "blank.png", "blank.png", "blank.png", "blank.png" }
228 tt = S("Transmits redstone power, powers mechanisms")
229 longdesc = S("Redstone is a versatile conductive mineral which transmits redstone power. It can be placed on the ground as a trail.").."\n"..
230 S("A redstone trail can be in two states: Powered or not powered. A powered redstone trail will power (and thus activate) adjacent redstone components.").."\n"..
231 S("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.")
232 usagehelp = S("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.").."\n\n"..
234 S("Read the help entries on the other redstone components to learn how redstone components interact.")
235 img = "redstone_redstone_dust.png"
236 desc_off = S("Redstone")
237 desc_on = S("Powered Redstone Spot (@1)", nodeid)
238 else
239 -- Connected redstone wire
240 table.insert(nodebox, box_center)
241 tiles_off = { crossing_off, crossing_off, straight0_off, straight1_off, straight0_off, straight1_off, }
242 tiles_on = { crossing_on, crossing_on, straight0_on, straight1_on, straight0_on, straight1_on, }
243 wirehelp = false
244 desc_off = S("Redstone Trail (@1)", nodeid)
245 desc_on = S("Powered Redstone Trail (@1)", nodeid)
248 mesecon.register_node(":mesecons:wire_"..nodeid, {
249 drawtype = "nodebox",
250 paramtype = "light",
251 sunlight_propagates = true,
252 selection_box = selectionbox,
253 node_box = nodebox,
254 walkable = false,
255 drop = "mesecons:wire_00000000_off",
256 sounds = mcl_sounds.node_sound_defaults(),
257 is_ground_content = false,
258 mesecon_wire = true
260 description = desc_off,
261 inventory_image = img,
262 wield_image = img,
263 _tt_help = tt,
264 _doc_items_create_entry = wirehelp,
265 _doc_items_longdesc = longdesc,
266 _doc_items_usagehelp = usagehelp,
267 tiles = tiles_off,
268 mesecons = meseconspec_off,
269 groups = groups_off,
271 description = desc_on,
272 _doc_items_create_entry = false,
273 tiles = tiles_on,
274 mesecons = meseconspec_on,
275 groups = groups_on
278 -- Add Help entry aliases for e.g. making it identifiable by the lookup tool [doc_identifier]
279 if minetest.get_modpath("doc") then
280 if nodeid ~= "00000000" then
281 doc.add_entry_alias("nodes", "mesecons:wire_00000000_off", "nodes", "mesecons:wire_"..nodeid.."_off")
283 doc.add_entry_alias("nodes", "mesecons:wire_00000000_off", "nodes", "mesecons:wire_"..nodeid.."_on")
286 if (nid_inc(nid) == false) then return end
289 register_wires()
291 minetest.register_alias("mesecons:redstone", "mesecons:wire_00000000_off")
293 minetest.register_craft({
294 type = "cooking",
295 output = "mesecons:redstone",
296 recipe = "mcl_core:stone_with_redstone",
297 cooktime = 10,