techage_modpack/networks/junction.lua

109 lines
2.7 KiB
Lua
Raw Normal View History

2020-05-31 23:31:18 +03:00
--[[
2021-08-01 12:00:22 +03:00
Networks
========
2020-05-31 23:31:18 +03:00
2021-08-01 12:00:22 +03:00
Copyright (C) 2021 Joachim Stolberg
2020-05-31 23:31:18 +03:00
2020-10-25 23:32:47 +03:00
AGPL v3
2020-05-31 23:31:18 +03:00
See LICENSE.txt for more information
]]--
2022-01-28 20:22:52 +03:00
local SideToDir = {B=1, R=2, F=3, L=4, D=5, U=6}
local tubelib2_dir_to_side = tubelib2.dir_to_side
2020-05-31 23:31:18 +03:00
local function bit(p)
return 2 ^ (p - 1) -- 1-based indexing
end
-- Typical call: if hasbit(x, bit(3)) then ...
local function hasbit(x, p)
2022-01-28 20:22:52 +03:00
return x % (p + p) >= p
2020-05-31 23:31:18 +03:00
end
local function setbit(x, p)
return hasbit(x, p) and x or x + p
end
2022-01-28 20:22:52 +03:00
2020-05-31 23:31:18 +03:00
local function get_node_box(val, size, boxes)
local fixed = {{-size, -size, -size, size, size, size}}
for i = 1,6 do
if hasbit(val, bit(i)) then
for _,box in ipairs(boxes[i]) do
table.insert(fixed, box)
end
end
end
return {
type = "fixed",
fixed = fixed,
}
end
-- 'size' is the size of the junction cube without any connection, e.g. 1/8
-- 'boxes' is a table with 6 table elements for the 6 possible connection arms
-- 'tlib2' is the tubelib2 instance
-- 'node' is the node definition with tiles, callback functions, and so on
-- 'index' number for the inventory node (default 0)
2021-08-01 12:00:22 +03:00
function networks.register_junction(name, size, boxes, tlib2, node, index)
2021-05-14 19:50:16 +03:00
local names = {}
2020-05-31 23:31:18 +03:00
for idx = 0,63 do
local ndef = table.copy(node)
if idx == (index or 0) then
ndef.groups.not_in_creative_inventory = 0
else
ndef.groups.not_in_creative_inventory = 1
end
ndef.drawtype = "nodebox"
2021-08-01 12:00:22 +03:00
ndef.paramtype = "light"
ndef.sunlight_propagates = true
2020-05-31 23:31:18 +03:00
ndef.node_box = get_node_box(idx, size, boxes)
ndef.paramtype2 = "facedir"
ndef.on_rotate = screwdriver.disallow
ndef.drop = name..(index or "0")
minetest.register_node(name..idx, ndef)
2021-05-14 19:50:16 +03:00
names[#names + 1] = name..idx
2020-05-31 23:31:18 +03:00
end
2021-05-14 19:50:16 +03:00
return names
2020-05-31 23:31:18 +03:00
end
2021-08-01 12:00:22 +03:00
function networks.junction_type(pos, network, default_side, param2)
local connected = function(self, pos, dir)
if network:is_primary_node(pos, dir) then
local param2, npos = self:get_primary_node_param2(pos, dir)
if param2 then
local d1, d2, _ = self:decode_param2(npos, param2)
dir = networks.Flip[dir]
return d1 == dir or dir == d2
end
end
end
2020-05-31 23:31:18 +03:00
local val = 0
2021-05-14 19:50:16 +03:00
if default_side then
val = setbit(val, bit(SideToDir[default_side]))
end
2020-05-31 23:31:18 +03:00
for dir = 1,6 do
2022-01-28 20:22:52 +03:00
local dir2 = SideToDir[tubelib2_dir_to_side(dir, param2 or 0)]
2020-05-31 23:31:18 +03:00
if network.force_to_use_tubes then
2021-08-01 12:00:22 +03:00
if connected(network, pos, dir) then
2021-05-14 19:50:16 +03:00
val = setbit(val, bit(dir2))
2020-05-31 23:31:18 +03:00
elseif network:is_special_node(pos, dir) then
2021-05-14 19:50:16 +03:00
val = setbit(val, bit(dir2))
2020-05-31 23:31:18 +03:00
end
else
2021-08-01 12:00:22 +03:00
if connected(network, pos, dir) then
val = setbit(val, bit(dir2))
elseif network:is_secondary_node(pos, dir) then
2022-01-28 20:22:52 +03:00
local node = network:get_secondary_node(pos, dir)
if network:is_valid_dir(node, networks.Flip[dir]) then
val = setbit(val, bit(dir2))
end
2020-05-31 23:31:18 +03:00
end
end
end
return val
2022-01-28 20:22:52 +03:00
end
2020-05-31 23:31:18 +03:00