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