1
0
mirror of https://github.com/mt-mods/pipeworks.git synced 2024-11-18 00:08:28 +01:00
pipeworks/tube_registration.lua

254 lines
6.8 KiB
Lua
Raw Normal View History

-- This file supplies the various kinds of pneumatic tubes
local S = minetest.get_translator("pipeworks")
2013-12-15 11:46:39 +01:00
2015-01-31 16:48:03 +01:00
local tubenodes = {}
pipeworks.tubenodes = tubenodes
minetest.register_alias("pipeworks:tube", "pipeworks:tube_000000")
-- now, a function to define the tubes
2013-12-31 07:36:04 +01:00
local REGISTER_COMPATIBILITY = true
2013-12-31 07:36:04 +01:00
local vti = {4, 3, 2, 1, 6, 5}
2015-01-31 16:48:03 +01:00
local default_noctrs = { "pipeworks_tube_noctr.png" }
local default_plain = { "pipeworks_tube_plain.png" }
local default_ends = { "pipeworks_tube_end.png" }
local texture_mt = {
__index = function(table, key)
local size, idx = #table, tonumber(key)
if size > 0 then -- avoid endless loops with empty tables
while idx > size do idx = idx - size end
return table[idx]
end
end
}
2013-12-31 07:36:04 +01:00
local register_one_tube = function(name, tname, dropname, desc, plain, noctrs, ends, short, inv, special, connects, style)
2015-01-31 16:48:03 +01:00
noctrs = noctrs or default_noctrs
setmetatable(noctrs, texture_mt)
2015-01-31 16:48:03 +01:00
plain = plain or default_plain
setmetatable(plain, texture_mt)
2015-01-31 16:48:03 +01:00
ends = ends or default_ends
setmetatable(ends, texture_mt)
2015-01-31 16:48:03 +01:00
short = short or "pipeworks_tube_short.png"
inv = inv or "pipeworks_tube_inv.png"
2013-12-31 07:36:04 +01:00
local outboxes = {}
local outsel = {}
local outimgs = {}
2017-04-08 16:47:42 +02:00
2013-12-31 07:36:04 +01:00
for i = 1, 6 do
outimgs[vti[i]] = plain[i]
end
2017-04-08 16:47:42 +02:00
for _, v in ipairs(connects) do
pipeworks.table_extend(outboxes, pipeworks.tube_boxes[v])
2013-12-31 07:36:04 +01:00
table.insert(outsel, pipeworks.tube_selectboxes[v])
outimgs[vti[v]] = noctrs[v]
end
2013-12-31 07:36:04 +01:00
if #connects == 1 then
local v = connects[1]
v = v-1 + 2*(v%2) -- Opposite side
2013-12-31 07:36:04 +01:00
outimgs[vti[v]] = ends[v]
end
2014-08-14 16:22:03 +02:00
local tgroups = {snappy = 3, tube = 1, tubedevice = 1, not_in_creative_inventory = 1}
local tubedesc = string.format("%s %s", desc, dump(connects))
local iimg = plain[1]
local wscale = {x = 1, y = 1, z = 1}
2013-12-31 07:36:04 +01:00
if #connects == 0 then
2014-08-14 16:22:03 +02:00
tgroups = {snappy = 3, tube = 1, tubedevice = 1}
tubedesc = desc
iimg=inv
outimgs = {
short, short,
ends[3],ends[4],
short, short
}
outboxes = { -24/64, -9/64, -9/64, 24/64, 9/64, 9/64 }
outsel = { -24/64, -10/64, -10/64, 24/64, 10/64, 10/64 }
wscale = {x = 1, y = 1, z = 0.01}
end
2017-04-08 16:47:42 +02:00
2015-01-31 16:48:03 +01:00
local rname = string.format("%s_%s", name, tname)
table.insert(tubenodes, rname)
2017-04-08 16:47:42 +02:00
local nodedef = {
description = tubedesc,
drawtype = "nodebox",
tiles = outimgs,
sunlight_propagates = true,
inventory_image = iimg,
wield_image = iimg,
wield_scale = wscale,
paramtype = "light",
selection_box = {
2014-08-14 16:22:03 +02:00
type = "fixed",
fixed = outsel
},
node_box = {
type = "fixed",
fixed = outboxes
},
groups = tgroups,
sounds = default.node_sound_wood_defaults(),
walkable = true,
stack_max = 99,
2013-12-31 07:36:04 +01:00
basename = name,
style = style,
2015-01-31 16:48:03 +01:00
drop = string.format("%s_%s", name, dropname),
tubelike = 1,
2014-08-14 16:22:03 +02:00
tube = {
connect_sides = {front = 1, back = 1, left = 1, right = 1, top = 1, bottom = 1},
priority = 50
},
after_place_node = pipeworks.after_place,
2017-04-08 16:47:42 +02:00
after_dig_node = pipeworks.after_dig,
on_rotate = false,
2017-04-08 16:47:42 +02:00
on_blast = function(pos, intensity)
2017-07-20 07:42:34 +02:00
if not intensity or intensity > 1 + 3^0.5 then
2017-04-08 16:47:42 +02:00
minetest.remove_node(pos)
return {string.format("%s_%s", name, dropname)}
end
minetest.swap_node(pos, {name = "pipeworks:broken_tube_1"})
pipeworks.scan_for_tube_objects(pos)
end,
check_for_pole = pipeworks.check_for_vert_tube,
check_for_horiz_pole = pipeworks.check_for_horiz_tube,
tubenumber = tonumber(tname)
}
2013-12-31 07:36:04 +01:00
if style == "6d" then
nodedef.paramtype2 = "facedir"
end
2017-04-08 16:47:42 +02:00
if special == nil then special = {} end
for key, value in pairs(special) do
2014-08-14 16:22:03 +02:00
--if key == "after_dig_node" or key == "after_place_node" then
-- nodedef[key.."_"] = value
if key == "groups" then
for group, val in pairs(value) do
nodedef.groups[group] = val
2013-01-17 19:49:27 +01:00
end
elseif key == "tube" then
for key, val in pairs(value) do
nodedef.tube[key] = val
end
2013-01-17 19:49:27 +01:00
else
nodedef[key] = pipeworks.table_recursive_replace(value, "#id", tname)
end
end
2014-08-14 16:22:03 +02:00
minetest.register_node(rname, nodedef)
end
2013-12-31 07:36:04 +01:00
local register_all_tubes = function(name, desc, plain, noctrs, ends, short, inv, special, old_registration)
2013-12-31 09:08:18 +01:00
if old_registration then
2013-12-31 07:36:04 +01:00
for xm = 0, 1 do
for xp = 0, 1 do
for ym = 0, 1 do
for yp = 0, 1 do
for zm = 0, 1 do
for zp = 0, 1 do
2013-12-31 09:08:18 +01:00
local connects = {}
if xm == 1 then
connects[#connects+1] = 1
2013-12-31 09:08:18 +01:00
end
if xp == 1 then
connects[#connects+1] = 2
2013-12-31 09:08:18 +01:00
end
if ym == 1 then
connects[#connects+1] = 3
2013-12-31 09:08:18 +01:00
end
if yp == 1 then
connects[#connects+1] = 4
end
if zm == 1 then
connects[#connects+1] = 5
2013-12-31 09:08:18 +01:00
end
if zp == 1 then
connects[#connects+1] = 6
2013-12-31 09:08:18 +01:00
end
2013-12-31 07:36:04 +01:00
local tname = xm..xp..ym..yp..zm..zp
2013-12-31 09:08:18 +01:00
register_one_tube(name, tname, "000000", desc, plain, noctrs, ends, short, inv, special, connects, "old")
end
end
2013-12-31 07:36:04 +01:00
end
end
end
end
2013-12-31 09:08:18 +01:00
else
2013-12-31 10:32:53 +01:00
-- 6d tubes: uses only 10 nodes instead of 64, but the textures must be rotated
local cconnects = {{}, {1}, {1, 2}, {1, 3}, {1, 3, 5}, {1, 2, 3}, {1, 2, 3, 5}, {1, 2, 3, 4}, {1, 2, 3, 4, 5}, {1, 2, 3, 4, 5, 6}}
2013-12-31 09:08:18 +01:00
for index, connects in ipairs(cconnects) do
register_one_tube(name, tostring(index), "1", desc, plain, noctrs, ends, short, inv, special, connects, "6d")
2013-12-31 07:36:04 +01:00
end
2013-12-31 09:08:18 +01:00
if REGISTER_COMPATIBILITY then
local cname = name.."_compatibility"
minetest.register_node(cname, {
drawtype = "airlike",
style = "6d",
basename = name,
inventory_image = inv,
wield_image = inv,
2014-01-02 10:36:52 +01:00
paramtype = "light",
sunlight_propagates = true,
description = S("Pneumatic tube segment (legacy)"),
after_place_node = pipeworks.after_place,
2014-08-14 16:22:03 +02:00
groups = {not_in_creative_inventory = 1, tube_to_update = 1, tube = 1},
tube = {connect_sides = {front = 1, back = 1, left = 1, right = 1, top = 1, bottom = 1}},
drop = name.."_1",
2013-12-31 09:08:18 +01:00
})
2015-01-31 16:48:03 +01:00
table.insert(tubenodes, cname)
2013-12-31 09:08:18 +01:00
for xm = 0, 1 do
for xp = 0, 1 do
for ym = 0, 1 do
for yp = 0, 1 do
for zm = 0, 1 do
for zp = 0, 1 do
local tname = xm..xp..ym..yp..zm..zp
minetest.register_alias(name.."_"..tname, cname)
end
end
end
end
end
end
2013-12-31 07:36:04 +01:00
end
end
end
2013-12-31 07:36:04 +01:00
pipeworks.register_tube = function(name, def, ...)
if type(def) == "table" then
register_all_tubes(name, def.description,
def.plain, def.noctr, def.ends, def.short,
def.inventory_image, def.node_def, def.no_facedir)
else
-- we assert to be the old function with the second parameter being the description
-- function(name, desc, plain, noctrs, ends, short, inv, special, old_registration)
assert(type(def) == "string", "invalid arguments to pipeworks.register_tube")
register_all_tubes(name, def, ...)
end
end
2013-12-31 07:36:04 +01:00
if REGISTER_COMPATIBILITY then
minetest.register_abm({
nodenames = {"group:tube_to_update"},
interval = 1,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
2014-08-14 16:22:03 +02:00
local minp = vector.subtract(pos, 1)
local maxp = vector.add(pos, 1)
if table.getn(minetest.find_nodes_in_area(minp, maxp, "ignore")) == 0 then
pipeworks.scan_for_tube_objects(pos)
end
2013-12-31 07:36:04 +01:00
end
})
end