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