ShadowNinja
2013-10-03 562d0db20c5604a15515ca55eee4db6142d03e31
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             },
38             after_place_node = function(pos)
39                 local node = minetest.get_node(pos)
40                 technic.update_cables(pos, technic.get_cable_tier(node.name))
41             end,
42             after_dig_node = function(pos, oldnode)
43                 local tier = technic.get_cable_tier(oldnode.name)
44                 technic.update_cables(pos, tier, true)
45             end
46         })
47     end
48     end
49     end
50     end
51     end
52     end
53 end
54
55
56 minetest.register_on_placenode(function(pos, node)
57     for tier, machine_list in pairs(technic.machines) do
58         for machine_name, _ in pairs(machine_list) do
59             if node.name == machine_name then
60                 technic.update_cables(pos, tier, true)
61             end
62         end
63     end
64 end)
65
66
67 minetest.register_on_dignode(function(pos, node)
68     for tier, machine_list in pairs(technic.machines) do
69         for machine_name, _ in pairs(machine_list) do
70             if node.name == machine_name then
71                 technic.update_cables(pos, tier, true)
72             end
73         end
74     end
75 end)
76
77
78 function technic.get_cable_id(links)
79     return (links[6] * 1) + (links[5] * 2)
80             + (links[4] * 4)  + (links[3] * 8)
81             + (links[2] * 16) + (links[1] * 32)
82 end
83
84 function technic.update_cables(pos, tier, no_set, secondrun)
85     local link_positions = {
86         {x=pos.x+1, y=pos.y,   z=pos.z},
87         {x=pos.x-1, y=pos.y,   z=pos.z},
88         {x=pos.x,   y=pos.y+1, z=pos.z},
89         {x=pos.x,   y=pos.y-1, z=pos.z},
90         {x=pos.x,   y=pos.y,   z=pos.z+1},
91         {x=pos.x,   y=pos.y,   z=pos.z-1}}
92
93     local links = {0, 0, 0, 0, 0, 0}
94
95     for i, link_pos in pairs(link_positions) do
96         local connect_type = technic.cables_should_connect(pos, link_pos, tier)
97         if connect_type then
98             links[i] = 1
99             -- Have cables next to us update theirselves,
100             -- but only once. (We don't want to update the entire
101             -- network or start an infinite loop of updates)
102             if not secondrun and connect_type == "cable" then
103                 technic.update_cables(link_pos, tier, false, true)
104             end
105         end
106     end
107     -- We don't want to set ourselves if we have been removed or we are
108     -- updating a machine
109     if not no_set then
110         minetest.set_node(pos, {name="technic:"..string.lower(tier)
111                 .."_cable"..technic.get_cable_id(links)})
112
113     end
114 end
115
116
117 function technic.is_tier_cable(name, tier)
118     return technic.cables[name] and technic.cables[name] == tier
119 end
120
121
122 function technic.get_cable_tier(name)
123     return technic.cables[name]
124 end
125
126
127 function technic.cables_should_connect(pos1, pos2, tier)
128     local name = minetest.get_node(pos2).name
129
130     if technic.is_tier_cable(name, tier) then
131         return "cable"
132     elseif technic.machines[tier][name] then
133         return "machine"
134     end
135     return false
136 end
137
138
139 function technic.gen_cable_nodebox(x1, y1, z1, x2, y2, z2, size)
140     -- Nodeboxes
141     local box_center = {-size, -size, -size, size,  size, size}
142     local box_y1 =     {-size, -size, -size, size,  0.5,  size} -- y+
143     local box_x1 =     {-size, -size, -size, 0.5,   size, size} -- x+
144     local box_z1 =     {-size, -size,  size, size,  size, 0.5}   -- z+
145     local box_z2 =     {-size, -size, -0.5,  size,  size, size} -- z-
146     local box_y2 =     {-size, -0.5,  -size, size,  size, size} -- y-
147     local box_x2 =     {-0.5,  -size, -size, size,  size, size} -- x-
148
149     local box = {box_center}
150     if x1 == 1 then
151         table.insert(box, box_x1)
152     end
153     if y1 == 1 then
154         table.insert(box, box_y1)
155     end
156     if z1 == 1 then
157         table.insert(box, box_z1)
158     end
159     if x2 == 1 then
160         table.insert(box, box_x2)
161     end
162     if y2 == 1 then
163         table.insert(box, box_y2)
164     end
165     if z2 == 1 then
166         table.insert(box, box_z2)
167     end
168     return box
169 end
170