2022-06-13 06:10:36 +02:00
|
|
|
-- for registering variants of a specific node
|
2022-06-14 01:11:20 +02:00
|
|
|
local api = stairsplus.api
|
|
|
|
|
2022-06-14 20:17:06 +02:00
|
|
|
local table_set_all = stairsplus.util.table_set_all
|
|
|
|
local table_sort_keys = stairsplus.util.table_sort_keys
|
|
|
|
|
|
|
|
local S = stairsplus.S
|
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
local default_align_style = stairsplus.settings.default_align_style
|
2022-06-14 01:11:20 +02:00
|
|
|
|
2022-06-14 20:17:06 +02:00
|
|
|
api.nodes_by_shape = {}
|
|
|
|
api.shapes_by_node = {}
|
2022-06-14 01:11:20 +02:00
|
|
|
|
2022-06-17 02:46:09 +02:00
|
|
|
api.node_by_shaped_node = {}
|
|
|
|
api.shape_by_shaped_node = {}
|
2022-06-14 01:11:20 +02:00
|
|
|
|
2022-06-19 01:23:11 +02:00
|
|
|
api.registered_on_register_singles = {}
|
|
|
|
|
|
|
|
function api.register_on_register_single(func)
|
|
|
|
table.insert(api.registered_on_register_singles, func)
|
|
|
|
end
|
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
local function check_node_validity(node_def, meta)
|
|
|
|
local type_ = node_def.type
|
|
|
|
if not meta.ignore_type and type_ ~= "node" then
|
2022-06-15 01:32:48 +02:00
|
|
|
error(("cannot register non-node %q w/ stairsplus"):format(node_def.name))
|
2022-06-15 01:30:41 +02:00
|
|
|
end
|
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
local drawtype = node_def.drawtype
|
|
|
|
if not meta.ignore_drawtype and (
|
|
|
|
drawtype == "airlike" or
|
|
|
|
drawtype == "liquid" or
|
|
|
|
drawtype == "flowingliquid" or
|
|
|
|
drawtype == "torchlike" or
|
|
|
|
drawtype == "signlike" or
|
|
|
|
drawtype == "plantlike" or
|
|
|
|
drawtype == "firelike" or
|
|
|
|
drawtype == "fencelike" or
|
|
|
|
drawtype == "raillike" or
|
|
|
|
drawtype == "nodebox" or
|
|
|
|
drawtype == "mesh" or
|
|
|
|
drawtype == "plantlike_rooted"
|
2022-06-15 01:30:41 +02:00
|
|
|
) then
|
2022-06-16 01:18:11 +02:00
|
|
|
error(("cannot register %q w/ drawtype %q w/ stairsplus"):format(node_def.name, drawtype))
|
|
|
|
end
|
|
|
|
|
|
|
|
local paramtype2 = node_def.paramtype2
|
|
|
|
if not meta.ignore_paramtype2 and (
|
|
|
|
paramtype2 == "flowingliquid" or
|
|
|
|
paramtype2 == "wallmounted" or
|
|
|
|
paramtype2 == "leveled" or
|
|
|
|
paramtype2 == "degrotate" or
|
|
|
|
paramtype2 == "meshoptions" or
|
|
|
|
paramtype2 == "color" or
|
|
|
|
paramtype2 == "colorwallmounted" or
|
|
|
|
paramtype2 == "glasslikeliquidlevel" or
|
|
|
|
paramtype2 == "colordegrotate"
|
2022-06-15 01:30:41 +02:00
|
|
|
) then
|
2022-06-16 01:18:11 +02:00
|
|
|
error(("cannot register %q w/ paramtype2 %q w/ stairsplus"):format(node_def.name, paramtype2))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function api.format_name(node, shape)
|
|
|
|
local mod, name = node:match("^([^:]+):(.*)$")
|
|
|
|
local shape_def = api.registered_shapes[shape]
|
|
|
|
return ("%s:%s"):format(mod, shape_def.name_format:format(name))
|
2022-06-15 01:30:41 +02:00
|
|
|
end
|
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
function api.register_single(node, shape, overrides, meta)
|
2022-06-17 01:32:55 +02:00
|
|
|
if not minetest.registered_nodes[node] then
|
2022-06-19 01:23:11 +02:00
|
|
|
error(("node %q is not defined"):format(node))
|
2022-06-17 01:32:55 +02:00
|
|
|
end
|
2022-06-19 01:23:11 +02:00
|
|
|
|
2022-06-15 01:30:41 +02:00
|
|
|
if shape ~= "micro_8" and not (api.nodes_by_shape.micro_8 or {})[node] then
|
|
|
|
-- always make sure a microblock exists
|
2022-06-16 01:18:11 +02:00
|
|
|
api.register_single(node, "micro_8", overrides, meta)
|
2022-06-15 01:30:41 +02:00
|
|
|
end
|
|
|
|
|
2022-06-21 18:57:12 +02:00
|
|
|
local shaped_name = api.format_name(node, shape)
|
|
|
|
|
|
|
|
stairsplus.log("info", "registering %s", shaped_name)
|
|
|
|
|
|
|
|
meta = meta or {}
|
|
|
|
overrides = table.copy(overrides or {})
|
|
|
|
|
|
|
|
local node_def = table.copy(minetest.registered_nodes[node])
|
|
|
|
|
|
|
|
check_node_validity(node_def, meta)
|
|
|
|
|
2022-06-15 01:30:41 +02:00
|
|
|
if (api.nodes_by_shape[shape] or {})[node] then
|
|
|
|
return -- already registered
|
|
|
|
end
|
|
|
|
|
2022-06-14 01:11:20 +02:00
|
|
|
local shape_def = api.registered_shapes[shape]
|
|
|
|
|
2022-06-17 01:32:55 +02:00
|
|
|
local paramtype2
|
|
|
|
if node_def.paramtype2 == "colorfacedir" then
|
|
|
|
paramtype2 = "colorfacedir"
|
|
|
|
else
|
|
|
|
paramtype2 = shape_def.paramtype2 or "facedir"
|
|
|
|
end
|
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
-- shaped_node definition
|
2022-06-14 01:11:20 +02:00
|
|
|
local def = {
|
|
|
|
description = S(shape_def.description, node_def.description or node),
|
2022-06-15 01:30:41 +02:00
|
|
|
|
2022-06-14 01:11:20 +02:00
|
|
|
drawtype = shape_def.drawtype,
|
|
|
|
mesh = shape_def.mesh,
|
|
|
|
node_box = shape_def.node_box,
|
|
|
|
collision_box = shape_def.collision_box,
|
|
|
|
selection_box = shape_def.selection_box,
|
|
|
|
paramtype = shape_def.paramtype or "light",
|
2022-06-17 01:32:55 +02:00
|
|
|
paramtype2 = paramtype2,
|
2022-06-15 01:30:41 +02:00
|
|
|
|
2022-06-17 02:46:09 +02:00
|
|
|
light_source = api.scale_light(node_def.light_source, shape_def),
|
2022-06-17 01:32:55 +02:00
|
|
|
groups = api.build_groups(node, shape),
|
2022-06-16 01:18:11 +02:00
|
|
|
|
2022-06-15 01:30:41 +02:00
|
|
|
tiles = node_def.tiles,
|
|
|
|
overlay_tiles = node_def.overlay_tiles,
|
|
|
|
use_texture_alpha = node_def.use_texture_alpha,
|
|
|
|
color = node_def.color,
|
2022-06-17 01:32:55 +02:00
|
|
|
palette = node_def.palette, -- for coloredfacedir
|
2022-06-15 01:30:41 +02:00
|
|
|
stack_max = node_def.stack_max,
|
|
|
|
sound = node_def.sound,
|
|
|
|
is_ground_content = node_def.is_ground_content,
|
|
|
|
walkable = node_def.walkable,
|
|
|
|
pointable = node_def.pointable,
|
|
|
|
diggable = node_def.diggable,
|
|
|
|
climbable = node_def.climbable,
|
|
|
|
move_resistance = node_def.move_resistance,
|
2022-06-17 02:46:09 +02:00
|
|
|
|
2022-06-19 01:23:11 +02:00
|
|
|
on_place = function(...) return api.on_place(...) end,
|
2022-06-14 01:11:20 +02:00
|
|
|
}
|
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
-- see-through nodes tend to look better if we just use the first tile
|
2022-06-15 01:30:41 +02:00
|
|
|
if (node_def.drawtype or ""):match("glass") then
|
|
|
|
if #def.tiles > 1 then
|
|
|
|
def.tiles = {def.tiles[1]}
|
|
|
|
end
|
|
|
|
|
|
|
|
if def.overlay_tiles and #def.overlay_tiles > 1 then
|
|
|
|
def.overlay_tiles = {def.overlay_tiles[1]}
|
|
|
|
end
|
2022-06-14 20:17:06 +02:00
|
|
|
end
|
|
|
|
|
2022-06-14 01:11:20 +02:00
|
|
|
if node_def.short_description then
|
|
|
|
def.short_description = S(shape_def.description, node_def.short_description)
|
|
|
|
end
|
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
-- if there's a drop defined, and we can drop a shaped version, do so
|
2022-06-17 01:32:55 +02:00
|
|
|
if node_def.drop and type(node_def.drop) == "string" then
|
2022-06-17 02:46:09 +02:00
|
|
|
local item = api.get_schema_recipe_item(node_def.drop, shape)
|
2022-06-14 01:11:20 +02:00
|
|
|
if item then
|
|
|
|
def.drop = item
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-21 18:57:12 +02:00
|
|
|
if not meta.allow_override_groups and overrides.groups then
|
|
|
|
stairsplus.log("warning", "removing group overrides from %s", shaped_name)
|
2022-06-19 01:23:11 +02:00
|
|
|
overrides.groups = nil
|
|
|
|
end
|
|
|
|
|
2022-06-21 18:57:12 +02:00
|
|
|
if not meta.allow_override_drawtype and overrides.drawtype then
|
|
|
|
stairsplus.log("warning", "removing drawtype override %s from %s", overrides.drawtype, shaped_name)
|
|
|
|
overrides.drawtype = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if not meta.allow_override_paramtype2 and overrides.paramtype2 then
|
|
|
|
stairsplus.log("warning", "removing paramtype2 override %s from %s", overrides.paramtype2, shaped_name)
|
|
|
|
overrides.paramtype2 = nil
|
|
|
|
end
|
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
table_set_all(def, overrides)
|
|
|
|
|
|
|
|
-- set backface_culling and align_style
|
|
|
|
local align_style = meta.align_style or default_align_style
|
|
|
|
for i, tile in ipairs(def.tiles) do
|
|
|
|
if type(tile) == "string" then
|
|
|
|
def.tiles[i] = {
|
|
|
|
name = tile,
|
|
|
|
backface_culling = true,
|
|
|
|
align_style = align_style,
|
|
|
|
}
|
|
|
|
elseif not (tile.animation or tile.color) then
|
|
|
|
tile.backface_culling = true
|
|
|
|
tile.align_style = align_style
|
|
|
|
end
|
|
|
|
end
|
2022-06-14 01:11:20 +02:00
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
if def.overlay_tiles then
|
|
|
|
for i, tile in ipairs(def.overlay_tiles) do
|
|
|
|
if type(tile) == "string" then
|
|
|
|
def.tiles[i] = {
|
|
|
|
name = tile,
|
|
|
|
backface_culling = true,
|
|
|
|
align_style = align_style,
|
|
|
|
}
|
|
|
|
elseif not (tile.animation or tile.color) then
|
|
|
|
tile.backface_culling = true
|
|
|
|
tile.align_style = align_style
|
|
|
|
end
|
|
|
|
def.overlay_tiles[i] = tile
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- register node
|
2022-06-14 20:17:06 +02:00
|
|
|
minetest.register_node(":" .. shaped_name, def)
|
2022-06-14 01:11:20 +02:00
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
-- alias old name formats
|
2022-06-14 01:11:20 +02:00
|
|
|
if shape_def.aliases then
|
2022-06-14 20:17:06 +02:00
|
|
|
local mod, name = node:match("^([^:]+):(.*)$")
|
2022-06-14 01:11:20 +02:00
|
|
|
for _, alias in ipairs(shape_def.aliases) do
|
|
|
|
minetest.register_alias(
|
|
|
|
("%s:%s"):format(mod, alias:format(name)),
|
2022-06-14 20:17:06 +02:00
|
|
|
shaped_name
|
2022-06-14 01:11:20 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2022-06-14 20:17:06 +02:00
|
|
|
|
|
|
|
local nodes = api.nodes_by_shape[shape] or {}
|
|
|
|
nodes[node] = true
|
|
|
|
api.nodes_by_shape[shape] = nodes
|
|
|
|
|
|
|
|
local shapes = api.shapes_by_node[node] or {}
|
|
|
|
shapes[shape] = true
|
|
|
|
api.shapes_by_node[node] = shapes
|
2022-06-16 01:18:11 +02:00
|
|
|
|
2022-06-19 01:23:11 +02:00
|
|
|
for _, func in ipairs(api.registered_on_register_singles) do
|
|
|
|
func(node, shaped_name)
|
|
|
|
end
|
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
return shaped_name
|
2022-06-14 01:11:20 +02:00
|
|
|
end
|
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
function api.register_all(node, overrides, meta)
|
2022-06-14 01:11:20 +02:00
|
|
|
for shape in pairs(api.registered_shapes) do
|
2022-06-16 01:18:11 +02:00
|
|
|
api.register_single(node, shape, overrides, meta)
|
2022-06-14 01:11:20 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-22 00:26:35 +02:00
|
|
|
function api.register_singles(node, shapes, overrides, meta)
|
2022-06-20 23:29:13 +02:00
|
|
|
for _, shape in ipairs(shapes) do
|
2022-06-16 01:18:11 +02:00
|
|
|
api.register_single(node, shape, overrides, meta)
|
2022-06-14 01:11:20 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-20 23:29:13 +02:00
|
|
|
function api.register_group(node, shape_group, overrides, meta)
|
|
|
|
for _, shape in ipairs(api.shapes_by_group[shape_group] or {}) do
|
2022-06-16 01:18:11 +02:00
|
|
|
api.register_single(node, shape, overrides, meta)
|
2022-06-14 01:11:20 +02:00
|
|
|
end
|
|
|
|
end
|
2022-06-14 20:17:06 +02:00
|
|
|
|
2022-06-20 23:29:13 +02:00
|
|
|
function api.register_groups(node, shape_groups, overrides, meta)
|
|
|
|
for _, group in ipairs(shape_groups) do
|
2022-06-16 01:18:11 +02:00
|
|
|
api.register_group(node, group, overrides, meta)
|
2022-06-15 01:30:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-14 20:17:06 +02:00
|
|
|
function api.get_shapes(node)
|
2022-06-17 21:22:47 +02:00
|
|
|
if api.shapes_by_node[node] then
|
|
|
|
return table_sort_keys(api.shapes_by_node[node])
|
|
|
|
end
|
2022-06-14 20:17:06 +02:00
|
|
|
end
|
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
-- warning: don't mutate the return value
|
2022-06-14 20:17:06 +02:00
|
|
|
function api.get_shapes_hash(node)
|
|
|
|
return api.shapes_by_node[node]
|
|
|
|
end
|
|
|
|
|
2022-06-17 02:46:09 +02:00
|
|
|
-- turn a recipe item into a shape if possible
|
|
|
|
function api.get_schema_recipe_item(node, shape_or_item)
|
2022-06-17 01:32:55 +02:00
|
|
|
if shape_or_item == "" then
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
local name, count = shape_or_item:match("^([^ ]+) (%d+)")
|
2022-06-17 01:32:55 +02:00
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
if not name then
|
|
|
|
name = shape_or_item
|
|
|
|
end
|
2022-06-14 20:17:06 +02:00
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
count = tonumber(count)
|
|
|
|
|
|
|
|
if api.registered_shapes[name] then
|
|
|
|
name = api.format_name(node, name)
|
|
|
|
|
|
|
|
elseif name == "node" then
|
|
|
|
name = node
|
2022-06-19 01:23:11 +02:00
|
|
|
|
|
|
|
elseif not name:match(":") then
|
|
|
|
return
|
2022-06-14 20:17:06 +02:00
|
|
|
end
|
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
if count then
|
|
|
|
return ("%s %s"):format(name, count)
|
|
|
|
else
|
|
|
|
return name
|
|
|
|
end
|
2022-06-14 20:17:06 +02:00
|
|
|
end
|
|
|
|
|
2022-06-15 01:30:41 +02:00
|
|
|
function api.get_micronode(node)
|
2022-06-17 02:46:09 +02:00
|
|
|
return api.get_schema_recipe_item(node, "micro_8")
|
2022-06-15 01:30:41 +02:00
|
|
|
end
|
|
|
|
|
2022-06-17 02:46:09 +02:00
|
|
|
-- create some hashes for quickly looking things up at run-time (i.e. the circular saw)
|
|
|
|
-- register schema crafts once, after everything has been registered. otherwise, it's not clear when to do this
|
2022-06-14 20:17:06 +02:00
|
|
|
minetest.register_on_mods_loaded(function()
|
|
|
|
stairsplus.log("info", "registering schema crafts")
|
2022-06-15 01:30:41 +02:00
|
|
|
for node, shapes in pairs(api.shapes_by_node) do
|
|
|
|
for shape in pairs(shapes) do
|
|
|
|
local shaped_node = api.format_name(node, shape)
|
|
|
|
api.node_by_shaped_node[shaped_node] = node
|
|
|
|
api.shape_by_shaped_node[shaped_node] = shape
|
|
|
|
end
|
|
|
|
|
|
|
|
api.node_by_shaped_node[node] = node
|
|
|
|
api.shape_by_shaped_node[node] = "node"
|
2022-06-17 01:32:55 +02:00
|
|
|
|
|
|
|
api.register_schema_crafts_for_node(node)
|
2022-06-14 20:17:06 +02:00
|
|
|
end
|
|
|
|
end)
|
2022-06-17 02:46:09 +02:00
|
|
|
|
|
|
|
function api.get_node_of_shaped_node(shaped_node)
|
|
|
|
return api.node_by_shaped_node[shaped_node]
|
|
|
|
end
|
|
|
|
|
|
|
|
function api.get_shape_of_shaped_node(shaped_node)
|
|
|
|
return api.shape_by_shaped_node[shaped_node]
|
|
|
|
end
|