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