functional, except saw, legacy, other compatability

This commit is contained in:
flux
2022-06-14 11:17:06 -07:00
parent 687de31632
commit b9eed16713
22 changed files with 374 additions and 238 deletions

View File

@ -1,24 +1,50 @@
local api = stairsplus.api
local table_is_empty = stairsplus.util.table_is_empty
function api.register_alias_single(old_node, new_node, shape)
local old_shaped_node = api.format_name(shape, old_node)
local new_shaped_node = api.format_name(shape, new_node)
minetest.register_alias(old_shaped_node, new_shaped_node)
end
function api.register_alias_all(old_node, new_node)
local old_mod, old_name = old_node:match("^([^:]+:(.*)$")
local new_mod, new_name = new_node:match("^([^:]+:(.*)$")
for _, shape_def in pairs(stairsplus.api.registered_shapes) do
minetest.register_alias(
("%s:%s"):format(old_mod, shape_def.name_format:format(old_name)),
("%s:%s"):format(new_mod, shape_def.name_format:format(new_name))
)
for shape in pairs(api.registered_shapes) do
api.register_alias_single(old_node, new_node, shape)
end
end
function api.register_alias_force_single(old_node, new_node, shape)
local old_shaped_node = api.format_name(shape, old_node)
local new_shaped_node = api.format_name(shape, new_node)
minetest.register_alias_force(old_shaped_node, new_shaped_node)
local nodes = api.nodes_by_shape[shape] or {}
if nodes[old_node] then
nodes[old_node] = nil
nodes[new_node] = true
api.nodes_by_shape[shape] = nodes
end
local old_shapes = api.shapes_by_node[old_node] or {}
if old_shapes[shape] then
old_shapes[shape] = nil
if table_is_empty(old_shapes) then
api.shapes_by_node[old_node] = nil
else
api.shapes_by_node[old_node] = old_shapes
end
local new_shapes = api.shapes_by_node[new_node] or {}
new_shapes[new_node] = true
api.shapes_by_node[new_node] = new_shapes
end
end
function api.register_alias_force_all(old_node, new_node)
local old_mod, old_name = old_node:match("^([^:]+:(.*)$")
local new_mod, new_name = new_node:match("^([^:]+:(.*)$")
for _, shape_def in pairs(stairsplus.api.registered_shapes) do
minetest.register_alias_force(
("%s:%s"):format(old_mod, shape_def.name_format:format(old_name)),
("%s:%s"):format(new_mod, shape_def.name_format:format(new_name))
)
for shape in pairs(api.registered_shapes) do
api.register_alias_force_single(old_node, new_node, shape)
end
end

View File

@ -3,4 +3,5 @@ stairsplus.api = {}
stairsplus.dofile("api", "shape")
stairsplus.dofile("api", "node")
stairsplus.dofile("api", "station")
stairsplus.dofile("api", "alias")
stairsplus.dofile("api", "recipe")

View File

@ -4,10 +4,16 @@
]]
local api = stairsplus.api
local table_set_all = stairsplus.util.table_set_all
local table_sort_keys = stairsplus.util.table_sort_keys
local S = stairsplus.S
local legacy_mode = stairsplus.settings.legacy_mode
local in_creative_inventory = stairsplus.settings.in_creative_inventory
local S = stairsplus.S
api.nodes_by_shape = {}
api.shapes_by_node = {}
local function scale_light(light_source, shape_def)
if not light_source or light_source == 0 then
@ -19,14 +25,19 @@ local function scale_light(light_source, shape_def)
return math.max(1, math.min(math.round(light_source * shape_def.eighths / 4), light_source))
end
function api.register_single(node, shape, overrides)
function api.format_name(shape, node)
local mod, name = node:match("^([^:]+):(.*)$")
local shape_def = api.registered_shapes[shape]
return ("%s:%s"):format(mod, shape_def.name_format:format(name))
end
function api.register_single(node, shape, overrides)
local node_def = table.copy(minetest.registered_nodes[node])
local shape_def = api.registered_shapes[shape]
local groups = {
[shape] = 1,
not_in_creative_inventory = in_creative_inventory,
not_in_creative_inventory = in_creative_inventory and 1 or 0,
}
for group, value in pairs(node_def.groups) do
@ -47,6 +58,13 @@ function api.register_single(node, shape, overrides)
light_source = scale_light(node_def.light_source, shape_def),
}
local tiles = node_def.tiles
if #tiles > 1 and (node_def.drawtype or ""):match("glass") then
def.tiles = {tiles[1]}
else
def.tiles = tiles
end
if node_def.short_description then
def.short_description = S(shape_def.description, node_def.short_description)
end
@ -62,39 +80,72 @@ function api.register_single(node, shape, overrides)
end
end
for k, v in pairs(overrides or {}) do
def[k] = v
end
table_set_all(def, overrides or {})
minetest.register_node((":%s:%s"):format(mod, shape_def.name_format:format(name)), def)
local shaped_name = api.format_name(shape, node)
minetest.register_node(":" .. shaped_name, def)
if shape_def.aliases then
local mod, name = node:match("^([^:]+):(.*)$")
for _, alias in ipairs(shape_def.aliases) do
minetest.register_alias(
("%s:%s"):format(mod, alias:format(name)),
("%s:%s"):format(mod, shape_def.name_format:format(name))
shaped_name
)
end
end
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
end
function api.register_all(node, overrides)
for shape in pairs(api.registered_shapes) do
api.register_single(node, shape, overrides)
end
api.register_schema_crafts_for_node(node)
end
function api.register_custom(node, list, overrides)
for _, shape in ipairs(list) do
api.register_single(node, shape, overrides)
end
api.register_schema_crafts_for_node(node)
end
function api.register_group(node, group, overrides)
for _, shape in ipairs(api.shapes_by_group[group] or {}) do
api.register_single(node, shape, overrides)
end
api.register_schema_crafts_for_node(node)
end
function api.get_shapes(node)
return table_sort_keys(api.shapes_by_node[node])
end
function api.get_shapes_hash(node)
return api.shapes_by_node[node]
end
function api.get_shaped_name(node, shape_or_item)
local t = ItemStack(shape_or_item):to_table()
if api.registered_shapes[t.name] then
t.name = api.format_name(t.name, node)
elseif t.name == "node" then
t.name = node
end
return ItemStack(t):to_string()
end
minetest.register_on_mods_loaded(function()
stairsplus.log("info", "registering schema crafts")
for node in pairs(api.shapes_by_node) do
api.register_schema_crafts_for_node(node)
end
end)

View File

@ -5,13 +5,13 @@
api.register_craft_schema({
output = "panel_8 6",
input = {{"node", "node", "node"}},
recipe = {{"node", "node", "node"}},
})
api.register_craft_schema({
type = "shapeless",
output = "micro_8 7",
input = {"stair_inner"},
recipe = {"stair_inner"},
})
api.register_schema_crafts_for_node("default:coalblock")
@ -31,18 +31,11 @@ api.register_crafts_for_shapes({
]]
local api = stairsplus.api
error("TODO: ugh, forgot to handle when output has > 1 item")
local function is_valid_shape(item)
return api.registered_shapes[item]
end
local function is_valid_item(item, shapes)
local item_name = ItemStack(item):get_name()
local function is_normal_item(item)
return item == "" or item:match(":")
end
local function is_valid_item(item)
return is_valid_shape(item) or is_normal_item(item)
return shapes[item_name] or item_name == "" or item_name:match(":")
end
local function verify_schema(schema)
@ -52,14 +45,14 @@ local function verify_schema(schema)
table.insert(problems, ("unimplemented schema type %q"):format(schema.type))
end
if not is_valid_item(schema.output) then
if not is_valid_item(schema.output, api.registered_shapes) then
table.insert(problems, ("don't know how to handle output %q"):format(schema.output))
end
if schema.replacements then
for _, replacement in ipairs(schema.replacements) do
for _, item in ipairs(replacement) do
if not is_valid_item(schema.output) then
if not is_valid_item(schema.output, api.registered_shapes) then
table.insert(problems, ("don't know how to handle replacement item %q"):format(item))
end
end
@ -67,16 +60,16 @@ local function verify_schema(schema)
end
if schema.type == "shapeless" then
for _, item in ipairs(schema.input) do
if not is_valid_item(schema.output) then
for _, item in ipairs(schema.recipe) do
if not is_valid_item(schema.output, api.registered_shapes) then
table.insert(problems, ("don't know how to handle craft item %q"):format(item))
end
end
else
for _, row in ipairs(schema.input) do
for _, row in ipairs(schema.recipe) do
for _, item in ipairs(row) do
if not is_valid_item(schema.output) then
if not is_valid_item(schema.output, api.registered_shapes) then
table.insert(problems, ("don't know how to handle craft item %q"):format(item))
end
end
@ -91,6 +84,7 @@ end
api.registered_recipe_schemas = {}
function api.register_craft_schema(schema)
local problems = verify_schema(schema)
if problems then
error(problems)
end
@ -98,19 +92,15 @@ function api.register_craft_schema(schema)
table.insert(api.registered_recipe_schemas, schema)
end
local function has_the_right_shape(item, shapes)
return shapes[item] or item == "" or item:match(":")
end
local function has_the_right_shapes(schema, shapes)
if not has_the_right_shape(schema.output, shapes) then
if not is_valid_item(schema.output, shapes) then
return false
end
if schema.replacements then
for _, replacement in ipairs(schema.replacements) do
for _, item in ipairs(replacement) do
if not has_the_right_shape(item, shapes) then
if not is_valid_item(item, shapes) then
return false
end
end
@ -118,16 +108,16 @@ local function has_the_right_shapes(schema, shapes)
end
if schema.type == "shapeless" then
for _, item in ipairs(schema.input) do
if not has_the_right_shape(item, shapes) then
for _, item in ipairs(schema.recipe) do
if not is_valid_item(item, shapes) then
return false
end
end
elseif schema.type == "shaped" or schema.type == nil then
for _, row in ipairs(schema.input) do
for _, row in ipairs(schema.recipe) do
for _, item in ipairs(row) do
if not has_the_right_shape(item, shapes) then
if not is_valid_item(item, shapes) then
return false
end
end
@ -155,14 +145,14 @@ local function register_for_schema(node, shapes, schema)
end
if recipe.type == "shapeless" then
for i, item in ipairs(recipe.input) do
for i, item in ipairs(recipe.recipe) do
if shapes[item] then
recipe.input[i] = api.get_shaped_name(node, item)
recipe.recipe[i] = api.get_shaped_name(node, item)
end
end
elseif recipe.type == "shaped" or recipe.type == nil then
for _, row in ipairs(schema.input) do
for _, row in ipairs(schema.recipe) do
for i, item in ipairs(row) do
if shapes[item] then
row[i] = api.get_shaped_name(node, item)

View File

@ -1,14 +1,28 @@
-- register shapes (e.g. 1/16 slab, 1/8 slab, 1/4 slab, etc)
--[[
stairsplus.api.register_shape("micro_1", {
name_format = "micro_%s_1",
description = "@1 1/16 Microblock",
shape_groups = {micro = 1},
eighths = 1,
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {-0.5, -0.5, 0, 0, -0.4375, 0.5},
},
})
]]
local api = stairsplus.api
stairsplus.api.registered_shapes = {}
stairsplus.api.shapes_by_group = {}
api.registered_shapes = {}
api.shapes_by_group = {}
function stairsplus.api.register_shape(name , def)
stairsplus.api.registered_shapes[name] = def
function api.register_shape(name, def)
api.registered_shapes[name] = def
for group in pairs(def.shape_groups or {}) do
local shapes = stairsplus.api.shapes_by_group[group] or {}
local shapes = api.shapes_by_group[group] or {}
table.insert(shapes, name)
stairsplus.api.shapes_by_group[group] = shapes
api.shapes_by_group[group] = shapes
end
end

View File

@ -2,12 +2,12 @@
local api = stairsplus.api
local S = stairsplus.S
function api.register_station(name, shape_groups, def)
minetest.register_node(name, def)
end
local F = minetest.formspec_escape
local circular_saw = {}
@ -171,7 +171,7 @@ end
function circular_saw.on_construct(pos)
local meta = minetest.get_meta(pos)
local fancy_inv = ""
if has_default_mod then
if stairsplus.has.default then
-- prepend background and slot styles from default if available
fancy_inv = default.gui_bg .. default.gui_bg_img .. default.gui_slots
end