Stairs: Add helper function for textures and simplify code (#3060)

This commit is contained in:
tenplus1 2023-09-11 18:14:16 +01:00 committed by GitHub
parent a132872ad8
commit 03177f1584
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -63,38 +63,11 @@ local function warn_if_exists(nodename)
end end
end end
-- get node settings to use for stairs
local function get_node_vars(nodename)
local def = minetest.registered_nodes[nodename]
if def then
return def.light_source, def.use_texture_alpha, def.sunlight_propagates
end
return nil, nil, nil
end
-- Register stair
-- Node will be called stairs:stair_<subname>
function stairs.register_stair(subname, recipeitem, groups, images, description,
sounds, worldaligntex)
local light_source, texture_alpha, sunlight = get_node_vars(recipeitem)
-- Set backface culling and world-aligned textures -- Set backface culling and world-aligned textures
local function set_textures(images, worldaligntex)
local stair_images = {} local stair_images = {}
for i, image in ipairs(images) do for i, image in ipairs(images) do
if type(image) == "string" then stair_images[i] = type(image) == "string" and {name = image} or table.copy(image)
stair_images[i] = {
name = image,
backface_culling = true,
}
if worldaligntex then
stair_images[i].align_style = "world"
end
else
stair_images[i] = table.copy(image)
if stair_images[i].backface_culling == nil then if stair_images[i].backface_culling == nil then
stair_images[i].backface_culling = true stair_images[i].backface_culling = true
end end
@ -102,7 +75,16 @@ function stairs.register_stair(subname, recipeitem, groups, images, description,
stair_images[i].align_style = "world" stair_images[i].align_style = "world"
end end
end end
return stair_images
end end
-- Register stair
-- Node will be called stairs:stair_<subname>
function stairs.register_stair(subname, recipeitem, groups, images, description,
sounds, worldaligntex)
local def = minetest.registered_nodes[recipeitem] or {}
local stair_images = set_textures(images, worldaligntex)
local new_groups = table.copy(groups) local new_groups = table.copy(groups)
new_groups.stair = 1 new_groups.stair = 1
warn_if_exists("stairs:stair_" .. subname) warn_if_exists("stairs:stair_" .. subname)
@ -110,14 +92,14 @@ function stairs.register_stair(subname, recipeitem, groups, images, description,
description = description, description = description,
drawtype = "nodebox", drawtype = "nodebox",
tiles = stair_images, tiles = stair_images,
use_texture_alpha = texture_alpha, use_texture_alpha = def.use_texture_alpha,
sunlight_propagates = sunlight, sunlight_propagates = def.sunlight_propagates,
light_source = light_source, light_source = def.light_source,
paramtype = "light", paramtype = "light",
paramtype2 = "facedir", paramtype2 = "facedir",
is_ground_content = false, is_ground_content = false,
groups = new_groups, groups = new_groups,
sounds = sounds, sounds = sounds or def.sounds,
node_box = { node_box = {
type = "fixed", type = "fixed",
fixed = { fixed = {
@ -184,25 +166,8 @@ end
function stairs.register_slab(subname, recipeitem, groups, images, description, function stairs.register_slab(subname, recipeitem, groups, images, description,
sounds, worldaligntex) sounds, worldaligntex)
local light_source, texture_alpha, sunlight = get_node_vars(recipeitem) local def = minetest.registered_nodes[recipeitem] or {}
local slab_images = set_textures(images, worldaligntex)
-- Set world-aligned textures
local slab_images = {}
for i, image in ipairs(images) do
if type(image) == "string" then
slab_images[i] = {
name = image,
}
if worldaligntex then
slab_images[i].align_style = "world"
end
else
slab_images[i] = table.copy(image)
if worldaligntex and image.align_style == nil then
slab_images[i].align_style = "world"
end
end
end
local new_groups = table.copy(groups) local new_groups = table.copy(groups)
new_groups.slab = 1 new_groups.slab = 1
warn_if_exists("stairs:slab_" .. subname) warn_if_exists("stairs:slab_" .. subname)
@ -210,14 +175,14 @@ function stairs.register_slab(subname, recipeitem, groups, images, description,
description = description, description = description,
drawtype = "nodebox", drawtype = "nodebox",
tiles = slab_images, tiles = slab_images,
use_texture_alpha = texture_alpha, use_texture_alpha = def.use_texture_alpha,
sunlight_propagates = sunlight, sunlight_propagates = def.sunlight_propagates,
light_source = light_source, light_source = def.light_source,
paramtype = "light", paramtype = "light",
paramtype2 = "facedir", paramtype2 = "facedir",
is_ground_content = false, is_ground_content = false,
groups = new_groups, groups = new_groups,
sounds = sounds, sounds = sounds or def.sounds,
node_box = { node_box = {
type = "fixed", type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5}, fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
@ -324,29 +289,8 @@ end
function stairs.register_stair_inner(subname, recipeitem, groups, images, function stairs.register_stair_inner(subname, recipeitem, groups, images,
description, sounds, worldaligntex, full_description) description, sounds, worldaligntex, full_description)
local light_source, texture_alpha, sunlight = get_node_vars(recipeitem) local def = minetest.registered_nodes[recipeitem] or {}
local stair_images = set_textures(images, worldaligntex)
-- Set backface culling and world-aligned textures
local stair_images = {}
for i, image in ipairs(images) do
if type(image) == "string" then
stair_images[i] = {
name = image,
backface_culling = true,
}
if worldaligntex then
stair_images[i].align_style = "world"
end
else
stair_images[i] = table.copy(image)
if stair_images[i].backface_culling == nil then
stair_images[i].backface_culling = true
end
if worldaligntex and stair_images[i].align_style == nil then
stair_images[i].align_style = "world"
end
end
end
local new_groups = table.copy(groups) local new_groups = table.copy(groups)
new_groups.stair = 1 new_groups.stair = 1
if full_description then if full_description then
@ -359,14 +303,14 @@ function stairs.register_stair_inner(subname, recipeitem, groups, images,
description = description, description = description,
drawtype = "nodebox", drawtype = "nodebox",
tiles = stair_images, tiles = stair_images,
use_texture_alpha = texture_alpha, use_texture_alpha = def.use_texture_alpha,
sunlight_propagates = sunlight, sunlight_propagates = def.sunlight_propagates,
light_source = light_source, light_source = def.light_source,
paramtype = "light", paramtype = "light",
paramtype2 = "facedir", paramtype2 = "facedir",
is_ground_content = false, is_ground_content = false,
groups = new_groups, groups = new_groups,
sounds = sounds, sounds = sounds or def.sounds,
node_box = { node_box = {
type = "fixed", type = "fixed",
fixed = { fixed = {
@ -416,29 +360,8 @@ end
function stairs.register_stair_outer(subname, recipeitem, groups, images, function stairs.register_stair_outer(subname, recipeitem, groups, images,
description, sounds, worldaligntex, full_description) description, sounds, worldaligntex, full_description)
local light_source, texture_alpha, sunlight = get_node_vars(recipeitem) local def = minetest.registered_nodes[recipeitem] or {}
local stair_images = set_textures(images, worldaligntex)
-- Set backface culling and world-aligned textures
local stair_images = {}
for i, image in ipairs(images) do
if type(image) == "string" then
stair_images[i] = {
name = image,
backface_culling = true,
}
if worldaligntex then
stair_images[i].align_style = "world"
end
else
stair_images[i] = table.copy(image)
if stair_images[i].backface_culling == nil then
stair_images[i].backface_culling = true
end
if worldaligntex and stair_images[i].align_style == nil then
stair_images[i].align_style = "world"
end
end
end
local new_groups = table.copy(groups) local new_groups = table.copy(groups)
new_groups.stair = 1 new_groups.stair = 1
if full_description then if full_description then
@ -451,14 +374,14 @@ function stairs.register_stair_outer(subname, recipeitem, groups, images,
description = description, description = description,
drawtype = "nodebox", drawtype = "nodebox",
tiles = stair_images, tiles = stair_images,
use_texture_alpha = texture_alpha, use_texture_alpha = def.use_texture_alpha,
sunlight_propagates = sunlight, sunlight_propagates = def.sunlight_propagates,
light_source = light_source, light_source = def.light_source,
paramtype = "light", paramtype = "light",
paramtype2 = "facedir", paramtype2 = "facedir",
is_ground_content = false, is_ground_content = false,
groups = new_groups, groups = new_groups,
sounds = sounds, sounds = sounds or def.sounds,
node_box = { node_box = {
type = "fixed", type = "fixed",
fixed = { fixed = {