Vanessa Ezekowitz
2013-10-26 4a35d5dd98c8c08dd79cd0d0789b902fd4470edf
commit | author | age
ee0765 1
S 2 technic.cables = {}
3
4
5 function technic.register_cable(tier, size)
6     local ltier = string.lower(tier)
7
8     for x1 = 0, 1 do
9     for x2 = 0, 1 do
10     for y1 = 0, 1 do
11     for y2 = 0, 1 do
12     for z1 = 0, 1 do
13     for z2 = 0, 1 do
14         local id = technic.get_cable_id({x1, x2, y1, y2, z1, z2})
15
16         technic.cables["technic:"..ltier.."_cable"..id] = tier
17
18         local groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2}
19         if id ~= 0 then
20             groups.not_in_creative_inventory = 1
21         end
22
23         minetest.register_node("technic:"..ltier.."_cable"..id, {
24             description = tier.." Cable",
25             tiles = {"technic_"..ltier.."_cable.png"},
26             inventory_image = "technic_"..ltier.."_cable_wield.png",
27             wield_image = "technic_"..ltier.."_cable_wield.png",
28             groups = groups,
29             sounds = default.node_sound_wood_defaults(),
30             drop = "technic:"..ltier.."_cable0",
31             paramtype = "light",
32             sunlight_propagates = true,
33             drawtype = "nodebox",
34             node_box = {
35                 type = "fixed",
36                 fixed = technic.gen_cable_nodebox(x1, y1, z1, x2, y2, z2, size)
37             },
12d29c 38             on_construct = function()
N 39                 technic.networks = {}
40             end,
41             on_destruct = function()
42                 technic.networks = {}
43             end,
ee0765 44             after_place_node = function(pos)
S 45                 local node = minetest.get_node(pos)
46                 technic.update_cables(pos, technic.get_cable_tier(node.name))
47             end,
48             after_dig_node = function(pos, oldnode)
49                 local tier = technic.get_cable_tier(oldnode.name)
50                 technic.update_cables(pos, tier, true)
51             end
52         })
53     end
54     end
55     end
56     end
57     end
58     end
59 end
60
61
62 minetest.register_on_placenode(function(pos, node)
63     for tier, machine_list in pairs(technic.machines) do
64         for machine_name, _ in pairs(machine_list) do
65             if node.name == machine_name then
66                 technic.update_cables(pos, tier, true)
f4ac2b 67                 technic.networks = {}
N 68                 return
ee0765 69             end
S 70         end
71     end
72 end)
73
74
75 minetest.register_on_dignode(function(pos, node)
76     for tier, machine_list in pairs(technic.machines) do
77         for machine_name, _ in pairs(machine_list) do
78             if node.name == machine_name then
79                 technic.update_cables(pos, tier, true)
f4ac2b 80                 technic.networks = {}
N 81                 return
ee0765 82             end
S 83         end
84     end
85 end)
86
87
88 function technic.get_cable_id(links)
89     return (links[6] * 1) + (links[5] * 2)
90             + (links[4] * 4)  + (links[3] * 8)
91             + (links[2] * 16) + (links[1] * 32)
92 end
93
94 function technic.update_cables(pos, tier, no_set, secondrun)
95     local link_positions = {
96         {x=pos.x+1, y=pos.y,   z=pos.z},
97         {x=pos.x-1, y=pos.y,   z=pos.z},
98         {x=pos.x,   y=pos.y+1, z=pos.z},
99         {x=pos.x,   y=pos.y-1, z=pos.z},
100         {x=pos.x,   y=pos.y,   z=pos.z+1},
101         {x=pos.x,   y=pos.y,   z=pos.z-1}}
102
103     local links = {0, 0, 0, 0, 0, 0}
104
105     for i, link_pos in pairs(link_positions) do
106         local connect_type = technic.cables_should_connect(pos, link_pos, tier)
107         if connect_type then
108             links[i] = 1
109             -- Have cables next to us update theirselves,
110             -- but only once. (We don't want to update the entire
111             -- network or start an infinite loop of updates)
112             if not secondrun and connect_type == "cable" then
113                 technic.update_cables(link_pos, tier, false, true)
114             end
115         end
116     end
117     -- We don't want to set ourselves if we have been removed or we are
118     -- updating a machine
119     if not no_set then
120         minetest.set_node(pos, {name="technic:"..string.lower(tier)
121                 .."_cable"..technic.get_cable_id(links)})
122
123     end
124 end
125
126
127 function technic.is_tier_cable(name, tier)
128     return technic.cables[name] and technic.cables[name] == tier
129 end
130
131
132 function technic.get_cable_tier(name)
133     return technic.cables[name]
134 end
135
136
137 function technic.cables_should_connect(pos1, pos2, tier)
138     local name = minetest.get_node(pos2).name
139
140     if technic.is_tier_cable(name, tier) then
141         return "cable"
142     elseif technic.machines[tier][name] then
143         return "machine"
144     end
145     return false
146 end
147
148
149 function technic.gen_cable_nodebox(x1, y1, z1, x2, y2, z2, size)
150     -- Nodeboxes
151     local box_center = {-size, -size, -size, size,  size, size}
152     local box_y1 =     {-size, -size, -size, size,  0.5,  size} -- y+
153     local box_x1 =     {-size, -size, -size, 0.5,   size, size} -- x+
154     local box_z1 =     {-size, -size,  size, size,  size, 0.5}   -- z+
155     local box_z2 =     {-size, -size, -0.5,  size,  size, size} -- z-
156     local box_y2 =     {-size, -0.5,  -size, size,  size, size} -- y-
157     local box_x2 =     {-0.5,  -size, -size, size,  size, size} -- x-
158
159     local box = {box_center}
160     if x1 == 1 then
161         table.insert(box, box_x1)
162     end
163     if y1 == 1 then
164         table.insert(box, box_y1)
165     end
166     if z1 == 1 then
167         table.insert(box, box_z1)
168     end
169     if x2 == 1 then
170         table.insert(box, box_x2)
171     end
172     if y2 == 1 then
173         table.insert(box, box_y2)
174     end
175     if z2 == 1 then
176         table.insert(box, box_z2)
177     end
178     return box
179 end
180