Charakterbewegungen hinzugefügt, Deko hinzugefügt, Kochrezepte angepasst

This commit is contained in:
N-Nachtigal 2025-05-14 16:36:42 +02:00
parent 95945c0306
commit a0c893ca0b
1124 changed files with 64294 additions and 763 deletions

View file

@ -0,0 +1,104 @@
local api = stairsplus.api
local table_is_empty = futil.table.is_empty
function api.register_alias_shape(old_node, new_node, shape)
local old_shaped_node = api.format_name(old_node, shape)
local new_shaped_node = api.format_name(new_node, shape)
minetest.register_alias(old_shaped_node, new_shaped_node)
local shape_def = api.registered_shapes[shape]
if shape_def.aliases then
local old_mod, old_name = old_node:match("^([^:]+):(.*)$")
for _, alias in ipairs(shape_def.aliases) do
old_shaped_node = ("%s:%s"):format(old_mod, alias:format(old_name))
minetest.register_alias(old_shaped_node, new_shaped_node)
end
end
end
function api.register_alias_all(old_node, new_node)
for shape in pairs(api.registered_shapes) do
api.register_alias_shape(old_node, new_node, shape)
end
end
function api.register_alias_shapes(old_node, new_node, shapes)
for _, shape in ipairs(shapes) do
api.register_alias_shape(old_node, new_node, shape)
end
end
function api.register_alias_group(old_node, new_node, group)
for _, shape in ipairs(api.shapes_by_group[group] or {}) do
api.register_alias_shape(old_node, new_node, shape)
end
end
function api.register_alias_groups(old_node, new_node, groups)
for _, group in ipairs(groups) do
api.register_alias_group(old_node, new_node, group)
end
end
function api.register_alias_force_shape(old_node, new_node, shape)
local old_shaped_node = api.format_name(old_node, shape)
local new_shaped_node = api.format_name(new_node, shape)
minetest.register_alias_force(old_shaped_node, new_shaped_node)
local shape_def = api.registered_shapes[shape]
if shape_def.aliases then
local old_mod, old_name = old_node:match("^([^:]+):(.*)$")
for _, alias in ipairs(shape_def.aliases) do
old_shaped_node = ("%s:%s"):format(old_mod, alias:format(old_name))
minetest.register_alias_force(old_shaped_node, new_shaped_node)
end
end
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)
for shape in pairs(api.registered_shapes) do
api.register_alias_force_shape(old_node, new_node, shape)
end
end
function api.register_alias_force_shapes(old_node, new_node, shapes)
for _, shape in ipairs(shapes) do
api.register_alias_force_shape(old_node, new_node, shape)
end
end
function api.register_alias_force_group(old_node, new_node, group)
for _, shape in ipairs(api.shapes_by_group[group] or {}) do
api.register_alias_force_shape(old_node, new_node, shape)
end
end
function api.register_alias_force_groups(old_node, new_node, groups)
for _, group in ipairs(groups) do
api.register_alias_force_group(old_node, new_node, group)
end
end

View file

@ -0,0 +1,66 @@
local api = stairsplus.api
local in_creative_inventory = stairsplus.settings.in_creative_inventory
local in_craft_guide = stairsplus.settings.in_craft_guide
api.passthrough_groups = {}
api.scaling_groups = {}
api.ignore_groups = {}
function api.register_passthrough_group(group)
api.passthrough_groups[group] = true
end
function api.register_passthrough_groups(groups)
for _, group in ipairs(groups) do
api.register_passthrough_group(group)
end
end
function api.register_scaling_group(group)
api.scaling_groups[group] = true
end
function api.register_scaling_groups(groups)
for _, group in ipairs(groups) do
api.register_scaling_group(group)
end
end
function api.register_ignore_group(group)
api.ignore_groups[group] = true
end
function api.register_ignore_groups(groups)
for _, group in ipairs(groups) do
api.register_ignore_group(group)
end
end
function api.build_groups(node, shape)
local node_def = minetest.registered_nodes[node]
local groups = {
[("shape_%s"):format(shape)] = 1,
not_in_creative_inventory = (not in_creative_inventory) and 1 or nil,
not_in_craft_guide = (not in_craft_guide) and 1 or nil,
}
local shape_def = api.registered_shapes[shape]
for group, value in pairs(shape_def.groups or {}) do
groups[group] = value
end
for group, value in pairs(node_def.groups) do
if api.passthrough_groups[group] then
groups[group] = value
elseif api.scaling_groups[group] then
groups[group] = (shape_def.eighths / 8) * value
elseif not api.ignore_groups[group] then
groups[shape_def.name_format:format(group)] = value
end
end
return groups
end

View file

@ -0,0 +1,10 @@
stairsplus.api = {}
stairsplus.dofile("api", "legacy")
stairsplus.dofile("api", "shape")
stairsplus.dofile("api", "group_filters")
stairsplus.dofile("api", "node")
stairsplus.dofile("api", "alias")
stairsplus.dofile("api", "recipe")
stairsplus.dofile("api", "station")
stairsplus.dofile("api", "whitelist")

View file

@ -0,0 +1,107 @@
local api = stairsplus.api
local legacy_mode = stairsplus.settings.legacy_mode
local legacy_place_mechanic = stairsplus.settings.legacy_place_mechanic
if legacy_place_mechanic then
local wall_right_dirmap = { 9, 18, 7, 12 }
local wall_left_dirmap = { 11, 16, 5, 14 }
local ceil_dirmap = { 20, 23, 22, 21 }
function api.on_place(itemstack, placer, pointed_thing)
if not futil.is_player(placer) then
return minetest.item_place(itemstack, placer, pointed_thing)
end
local controls = placer:get_player_control()
local sneak = controls.sneak
local aux = controls.aux1
local shaped_node_name = itemstack:get_name()
local shape = api.get_shape_of_shaped_node(shaped_node_name)
local under = pointed_thing.under
local under_node = minetest.get_node(under)
local under_shape = api.get_shape_of_shaped_node(under_node.name)
local same_cat = shape == under_shape
-- standard (floor) facedir, also used for sneak placement against the lower half of the wall
local p2 = placer and minetest.dir_to_facedir(placer:get_look_dir()) or 0
-- check which face and which quadrant we are interested in
-- this is used both to check if we're handling parallel placement in the same-category case,
-- and in general for sneak placement
local face_pos = minetest.pointed_thing_to_face_pos(placer, pointed_thing)
local face_off = vector.subtract(face_pos, under)
-- we cannot trust face_off to tell us the correct directionif the
-- under node has a non-standard shape, so use the distance between under and above
local wallmounted = minetest.dir_to_wallmounted(vector.subtract(pointed_thing.above, under))
if same_cat and not aux then
p2 = under_node.param2
-- flip if placing above or below an upright or upside-down node
-- TODO should we also flip when placing next to a side-mounted node?
if wallmounted < 2 then
if p2 < 4 then
p2 = (p2 + 2) % 4
p2 = ceil_dirmap[p2 + 1]
elseif p2 > 19 then
p2 = ceil_dirmap[p2 - 19] - 20
p2 = (p2 + 2) % 4
end
end
else
-- for same-cat placement, aux is used to disable param2 copying
if same_cat then
aux = not aux
end
local remap
-- standard placement against the wall
local use_wallmap = (wallmounted > 1 and not sneak) or (wallmounted < 2 and sneak)
-- standard placement against the ceiling, or sneak placement against the upper half of the wall
local use_ceilmap = wallmounted == 1 and not sneak
use_ceilmap = use_ceilmap or (wallmounted > 1 and sneak and face_off.y > 0)
if use_wallmap then
local left = (p2 == 0 and face_off.x < 0)
or (p2 == 1 and face_off.z > 0)
or (p2 == 2 and face_off.x > 0)
or (p2 == 3 and face_off.z < 0)
if aux then
left = not left
end
remap = left and wall_left_dirmap or wall_right_dirmap
elseif use_ceilmap then
remap = ceil_dirmap
end
if aux then
p2 = (p2 + 2) % 4
end
if remap then
p2 = remap[p2 + 1]
end
end
return minetest.item_place(itemstack, placer, pointed_thing, p2)
end
else
api.on_place = minetest.item_place
end
function api.scale_light(light_source, shape_def)
if not light_source or light_source == 0 then
return 0
elseif legacy_mode then
return light_source - 1
end
return math.max(1, math.min(math.round(light_source * shape_def.eighths / 4), light_source))
end

View file

@ -0,0 +1,341 @@
-- for registering variants of a specific node
local api = stairsplus.api
local table_set_all = futil.table.set_all
local table_sort_keys = futil.table.sort_keys
local S = stairsplus.S
local default_align_style = stairsplus.settings.default_align_style
local silence_group_overrides = stairsplus.settings.silence_group_overrides
api.nodes_by_shape = {}
api.shapes_by_node = {}
api.node_by_shaped_node = {}
api.shape_by_shaped_node = {}
api.registered_singles = {}
api.registered_on_register_singles = {}
function api.register_on_register_single(func)
table.insert(api.registered_on_register_singles, func)
end
local function check_node_validity(node_def, meta)
local type_ = node_def.type
if not meta.ignore_type and type_ ~= "node" then
error(("cannot register non-node %q w/ stairsplus"):format(node_def.name))
end
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"
)
then
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"
)
then
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))
end
function api.register_single(node, shape, overrides, meta)
if not minetest.registered_nodes[node] then
error(("node %q is not defined"):format(node))
end
if shape ~= "micro_8" and not (api.nodes_by_shape.micro_8 or {})[node] then
-- always make sure a microblock exists
api.register_single(node, "micro_8", overrides, meta)
end
local shaped_name = api.format_name(node, shape)
if shape ~= "micro_8" and not api.is_whitelisted(shaped_name) then
return false
end
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)
if (api.nodes_by_shape[shape] or {})[node] then
return -- already registered
end
local shape_def = api.registered_shapes[shape]
local paramtype2
if node_def.paramtype2 == "colorfacedir" then
paramtype2 = "colorfacedir"
else
paramtype2 = shape_def.paramtype2 or "facedir"
end
-- shaped_node definition
local def = {
description = S(shape_def.description, node_def.description or node),
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",
paramtype2 = paramtype2,
light_source = api.scale_light(node_def.light_source, shape_def),
groups = api.build_groups(node, shape),
tiles = node_def.tiles,
overlay_tiles = node_def.overlay_tiles,
use_texture_alpha = node_def.use_texture_alpha,
color = node_def.color,
palette = node_def.palette, -- for coloredfacedir
stack_max = node_def.stack_max,
sounds = node_def.sounds,
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,
on_place = function(...)
return api.on_place(...)
end,
}
-- see-through nodes tend to look better if we just use the first tile
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
end
if node_def.short_description then
def.short_description = S(shape_def.description, node_def.short_description)
end
-- if there's a drop defined, and we can drop a shaped version, do so
if meta.legacy_drop then
if type(meta.legacy_drop) == "string" then
local item = api.get_schema_recipe_item(meta.legacy_drop, shape)
if item then
def.drop = item
end
end
elseif node_def.drop and type(node_def.drop) == "string" then
local item = api.get_schema_recipe_item(node_def.drop, shape)
if item then
def.drop = item
end
end
if not silence_group_overrides and not meta.allow_override_groups and overrides.groups then
stairsplus.log(
"warning",
"removing group overrides from %s (was %s, will be %s)",
shaped_name,
minetest.write_json(overrides.groups),
minetest.write_json(def.groups)
)
overrides.groups = nil
end
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
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
if def.overlay_tiles then
for i, tile in ipairs(def.overlay_tiles) do
if type(tile) == "string" then
def.overlay_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
minetest.register_node(":" .. shaped_name, def)
-- alias old name formats
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)), 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
api.node_by_shaped_node[shaped_name] = node
api.shape_by_shaped_node[shaped_name] = shape
api.node_by_shaped_node[node] = node
api.shape_by_shaped_node[node] = "node"
table.insert(api.registered_singles, { node, shaped_name })
for _, func in ipairs(api.registered_on_register_singles) do
func(node, shaped_name)
end
return shaped_name
end
function api.register_all(node, overrides, meta)
for shape in pairs(api.registered_shapes) do
api.register_single(node, shape, overrides, meta)
end
end
function api.register_singles(node, shapes, overrides, meta)
for _, shape in ipairs(shapes) do
api.register_single(node, shape, overrides, meta)
end
end
function api.register_group(node, shape_group, overrides, meta)
for _, shape in ipairs(api.shapes_by_group[shape_group] or {}) do
api.register_single(node, shape, overrides, meta)
end
end
function api.register_groups(node, shape_groups, overrides, meta)
for _, group in ipairs(shape_groups) do
api.register_group(node, group, overrides, meta)
end
end
function api.get_shapes(node)
if api.shapes_by_node[node] then
return table_sort_keys(api.shapes_by_node[node])
end
end
-- warning: don't mutate the return value
function api.get_shapes_hash(node)
return api.shapes_by_node[node]
end
-- turn a recipe item into a shape if possible
function api.get_schema_recipe_item(node, shape_or_item)
if shape_or_item == "" then
return ""
end
local name, count = shape_or_item:match("^([^ ]+) (%d+)")
if not name then
name = shape_or_item
end
count = tonumber(count)
if api.registered_shapes[name] then
name = api.format_name(node, name)
elseif name == "node" then
name = node
elseif not name:match(":") then
return
end
if count then
return ("%s %s"):format(name, count)
else
return name
end
end
function api.get_micronode(node)
return api.get_schema_recipe_item(node, "micro_8")
end
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

View file

@ -0,0 +1,257 @@
-- for registering recipe schemas
local api = stairsplus.api
api.registered_recipe_schemas = {}
api.registered_on_register_craft_schemas = {}
local registered_schemas_by_node = {}
local function is_valid_output(item, shapes)
local item_name = item:match("^([^ ]+)")
return item_name and (shapes[item_name] or item_name == "node" or item_name:match(":"))
end
local function is_valid_item(item, shapes)
return is_valid_output(item, shapes) or item == ""
end
local function verify_schema(schema)
local problems = {}
if not (schema.type == "shaped" or schema.type == "shapeless" or schema.type == nil) then
table.insert(problems, ("unimplemented schema type %q"):format(schema.type))
end
if not is_valid_output(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(item, api.registered_shapes) then
table.insert(problems, ("don't know how to handle replacement item %q"):format(item))
end
end
end
end
if schema.type == "shapeless" then
for _, item in ipairs(schema.recipe) do
if not is_valid_item(item, 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.recipe) do
for _, item in ipairs(row) do
if not is_valid_item(item, api.registered_shapes) then
table.insert(problems, ("don't know how to handle craft item %q"):format(item))
end
end
end
end
if #problems > 0 then
return table.concat(problems, ", ")
end
end
local function has_the_right_shapes(schema, shapes)
if not is_valid_output(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 is_valid_item(item, shapes) then
return false
end
end
end
end
if schema.type == "shapeless" 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.recipe) do
for _, item in ipairs(row) do
if not is_valid_item(item, shapes) then
return false
end
end
end
end
return true
end
local function register_for_schema(node, schema)
stairsplus.log("verbose", "using schema %s w/ node %s", minetest.write_json(schema), node)
local recipe = table.copy(schema)
recipe.output = api.get_schema_recipe_item(node, recipe.output)
if recipe.replacements then
for _, replacement in ipairs(recipe.replacements) do
for i, item in ipairs(replacement) do
replacement[i] = api.get_schema_recipe_item(node, item)
end
end
end
if recipe.type == "shapeless" then
for i, item in ipairs(recipe.recipe) do
recipe.recipe[i] = api.get_schema_recipe_item(node, item)
end
elseif recipe.type == "shaped" or recipe.type == nil then
for _, row in ipairs(recipe.recipe) do
for i, item in ipairs(row) do
row[i] = api.get_schema_recipe_item(node, item)
end
end
end
stairsplus.log("info", "registering recipe %s", minetest.write_json(recipe))
minetest.register_craft(recipe)
end
function api.register_on_register_craft_schema(func)
table.insert(api.registered_on_register_craft_schemas, func)
end
function api.register_craft_schema(schema)
local problems = verify_schema(schema)
if problems then
error(problems)
end
stairsplus.log("info", "registering craft schema %s", minetest.write_json(schema))
table.insert(api.registered_recipe_schemas, schema)
for node, shapes in pairs(api.shapes_by_node) do
local registered_schemas = registered_schemas_by_node[node] or {}
if has_the_right_shapes(schema, shapes) and not registered_schemas[schema] then
register_for_schema(node, schema)
registered_schemas[schema] = true
end
registered_schemas_by_node[node] = registered_schemas
end
for _, func in ipairs(api.registered_on_register_craft_schemas) do
func(schema)
end
end
function api.register_schema_crafts_for_node(node)
local registered_schemas = registered_schemas_by_node[node] or {}
local shapes = api.get_shapes_hash(node)
for _, schema in ipairs(api.registered_recipe_schemas) do
if has_the_right_shapes(schema, shapes) and not registered_schemas[schema] then
register_for_schema(node, schema)
registered_schemas[schema] = true
end
end
registered_schemas_by_node[node] = registered_schemas
end
api.register_on_register_single(api.register_schema_crafts_for_node)
local function get_shape_intersection(a, b)
local a_shapes = api.get_shapes(a)
local b_shapes = api.get_shapes(b)
local filter = {}
for i = 1, #b_shapes do
filter[b_shapes[i]] = true
end
local intersection = {}
for i = 1, #a_shapes do
local shape = a_shapes[i]
if filter[shape] then
intersection[#intersection + 1] = shape
end
end
return intersection
end
local function register_cooking_for_shapes(recipe)
local mod, name = recipe.recipe:match("^([^:]+):(.*)$")
local shapes
if mod == "group" then
shapes = api.get_shapes(recipe.output)
else
shapes = get_shape_intersection(recipe.output, recipe.recipe)
end
for _, shape in ipairs(shapes) do
local shape_def = api.registered_shapes[shape]
local input
if mod == "group" then
input = "group:" .. shape_def.name_format:format(name)
else
input = api.get_schema_recipe_item(recipe.recipe, shape)
end
minetest.register_craft({
type = "cooking",
output = api.get_schema_recipe_item(recipe.output, shape),
recipe = input,
cooktime = math.max(1.0, (recipe.cooktime or 3) * (shape_def.eighths / 8)),
})
end
end
local function register_fuel_for_shapes(recipe)
local mod, name = recipe.recipe:match("^([^:]+):(.*)$")
local shapes
if mod == "group" then
shapes = api.registered_shapes
else
shapes = api.get_shapes_hash(recipe.recipe)
end
if not shapes then
error(("don't know how to handle fuel %s"):format(recipe.recipe))
end
for shape in pairs(shapes) do
local shape_def = api.registered_shapes[shape]
local input
if mod == "group" then
input = "group:" .. shape_def.name_format:format(name)
else
input = api.get_schema_recipe_item(recipe.recipe, shape)
end
minetest.register_craft({
type = "fuel",
recipe = input,
burntime = math.max(1.0, (recipe.burntime or 1) * (shape_def.eighths / 8)),
})
end
end
function api.register_crafts_for_shapes(recipe)
if recipe.type == "cooking" then
register_cooking_for_shapes(recipe)
elseif recipe.type == "fuel" then
register_fuel_for_shapes(recipe)
else
error(("unsupported recipe type %s"):format(recipe.type))
end
end

View file

@ -0,0 +1,48 @@
local f = string.format
local api = stairsplus.api
api.registered_on_register_shapes = {}
api.registered_shapes = {}
api.shapes_by_group = {}
function api.register_on_register_shape(func)
table.insert(api.registered_on_register_shapes, func)
end
function api.register_shape(name, def)
stairsplus.log("info", "registering shape %q", name)
def.shape_groups = def.shape_groups or {}
api.registered_shapes[name] = def
for group in pairs(def.shape_groups) do
local shapes = api.shapes_by_group[group] or {}
table.insert(shapes, name)
api.shapes_by_group[group] = shapes
end
for _, func in ipairs(api.registered_on_register_shapes) do
func(name, def)
end
end
function api.register_shape_group(shape_group, shapes)
for _, shape in ipairs(shapes) do
api.registered_shapes[shape].shape_groups[shape_group] = 1
end
api.shapes_by_group[shape_group] = shapes
end
function api.guess_shape(node_name)
local mod, namepart = node_name:match("^([^:]+):([^:]+)$")
if not (mod and namepart) then
return
end
for name, def in pairs(api.registered_shapes) do
local pattern = def.name_format:gsub("%%s", "(.*)")
local matched = namepart:match(pattern)
if matched and minetest.registered_nodes[f("%s:%s", mod, matched)] then
return name
end
end
end

View file

@ -0,0 +1,357 @@
-- for creating the circular saw and similar nodes
local api = stairsplus.api
local item_has_metadata = stairsplus.util.item_has_metadata
local resolve_itemstack = futil.resolve_itemstack
local default_stack_max = tonumber(minetest.settings:get("default_stack_max")) or 99
local station = {}
function station.get_cost(shaped_node)
if shaped_node == "" then
return 0
end
local shape = api.get_shape_of_shaped_node(shaped_node)
if shape == "node" then
return 8
end
local shape_def = api.registered_shapes[shape]
return shape_def and shape_def.eighths
end
function station.get_current_node(inv)
local input_stack = inv:get_stack("stairsplus:input", 1)
if not input_stack:is_empty() then
return api.get_node_of_shaped_node(input_stack:get_name())
end
local micro_stack = inv:get_stack("stairsplus:micro", 1)
if not micro_stack:is_empty() then
return api.get_node_of_shaped_node(micro_stack:get_name())
end
local recycle_stack = inv:get_stack("stairsplus:recycle", 1)
if not recycle_stack:is_empty() then
return api.get_node_of_shaped_node(recycle_stack:get_name())
end
end
function station.can_dig(meta, inv)
return inv:is_empty("stairsplus:input") and inv:is_empty("stairsplus:micro")
end
function station.on_receive_fields(meta, inv, formname, fields, sender, build_formspec, update_metadata)
local max = tonumber(fields.max_offered)
if max and max > 0 then
meta:set_int("stairsplus:max_offered", max)
-- Update to show the correct number of items:
station.update_inventory(meta, inv)
if update_metadata then
update_metadata(meta, inv)
end
if build_formspec then
meta:set_string("formspec", build_formspec(meta, inv))
end
end
return not not fields.max_offered
end
local function fix_aliases(inv)
local input = inv:get_stack("stairsplus:input", 1)
input:set_name(resolve_itemstack(input):get_name())
inv:set_stack("stairsplus:input", 1, input)
local micro = inv:get_stack("stairsplus:micro", 1)
micro:set_name(resolve_itemstack(micro):get_name())
inv:set_stack("stairsplus:micro", 1, micro)
local recycle = inv:get_stack("stairsplus:recycle", 1)
recycle:set_name(resolve_itemstack(recycle):get_name())
inv:set_stack("stairsplus:recycle", 1, recycle)
for i = 1, inv:get_size("stairsplus:output") do
local output = inv:get_stack("stairsplus:output", i)
output:set_name(resolve_itemstack(output):get_name())
inv:set_stack("stairsplus:output", i, output)
end
end
function station.update_inventory(meta, inv, taken_stack)
fix_aliases(inv)
local node = station.get_current_node(inv)
local valid_shapes = api.shapes_by_node[node]
if not (node and valid_shapes) then
inv:set_stack("stairsplus:input", 1, ItemStack())
inv:set_stack("stairsplus:micro", 1, ItemStack())
inv:set_stack("stairsplus:recycle", 1, ItemStack())
for i = 1, inv:get_size("stairsplus:output") do
inv:set_stack("stairsplus:output", i, ItemStack())
end
return
end
local input_stack = inv:get_stack("stairsplus:input", 1)
local micro_stack = inv:get_stack("stairsplus:micro", 1)
local recycle_stack = inv:get_stack("stairsplus:recycle", 1)
local input_cost = station.get_cost(input_stack:get_name())
local micro_cost = station.get_cost(micro_stack:get_name())
local recycle_cost = station.get_cost(recycle_stack:get_name())
local total_value = (
(input_stack:get_count() * input_cost)
+ (micro_stack:get_count() * micro_cost)
+ (recycle_stack:get_count() * recycle_cost)
)
if taken_stack then
total_value = total_value - station.get_cost(taken_stack:get_name()) * taken_stack:get_count()
end
local new_micros = total_value % 8
local new_blocks = math.floor(total_value / 8)
local micronode = api.get_micronode(node)
inv:set_stack("stairsplus:input", 1, ItemStack({ name = node, count = new_blocks }))
inv:set_stack("stairsplus:micro", 1, ItemStack({ name = micronode, count = new_micros }))
inv:set_stack("stairsplus:recycle", 1, ItemStack())
if total_value == 0 then
for i = 1, inv:get_size("stairsplus:output") do
inv:set_stack("stairsplus:output", i, "")
end
return
end
local max_offered = meta:get_int("stairsplus:max_offered")
local shape_groups = minetest.parse_json(meta:get_string("stairsplus:shape_groups"))
local i = 1
for _, group in ipairs(shape_groups) do
for _, shape in ipairs(api.shapes_by_group[group]) do
if valid_shapes[shape] then
local shape_def = api.registered_shapes[shape]
local shaped_node = api.format_name(node, shape)
local stack_max = math.min(max_offered, ItemStack(shaped_node):get_stack_max())
local count = math.min(stack_max, math.floor(total_value / shape_def.eighths))
local stack
if count > 0 then
stack = ItemStack({ name = shaped_node, count = count })
else
stack = ""
end
inv:set_stack("stairsplus:output", i, stack)
i = i + 1
end
end
end
for j = i, inv:get_size("stairsplus:output") do
inv:set_stack("stairsplus:output", j, "")
end
end
-- Moving the inventory of the station around is not allowed because it
-- is a fictional inventory. Moving inventory around would be rather
-- impractical and make things more difficult to calculate:
function station.allow_inventory_move(meta, inv, from_list, from_index, to_list, to_index, count, player)
return 0
end
function station.allow_inventory_put(meta, inv, listname, index, stack, player)
if listname == "stairsplus:output" then
return 0
end
if item_has_metadata(stack) then
return 0
end
local to_put_name = resolve_itemstack(stack):get_name()
local node = api.get_node_of_shaped_node(to_put_name)
local shape = api.get_shape_of_shaped_node(to_put_name)
if not (node and shape) then
return 0
end
local current_node = station.get_current_node(inv)
local input_stack = inv:get_stack("stairsplus:input", 1)
local micro_stack = inv:get_stack("stairsplus:micro", 1)
if current_node and node ~= current_node then
if
(input_stack:is_empty() and listname == "stairsplus:micro")
or (micro_stack:is_empty() and listname == "stairsplus:input")
then
return stack:get_count()
else
return 0
end
end
local count = stack:get_count()
local cost = station.get_cost(to_put_name)
local current_value = 8 * input_stack:get_count() + micro_stack:get_count()
local max_value = 8 * ItemStack(node):get_stack_max() + 7
local available_value = max_value - current_value
local available_count = math.floor(available_value / cost)
return math.min(count, available_count)
end
function station.on_inventory_put(meta, inv, listname, index, stack, player, update_metadata)
station.update_inventory(meta, inv)
if update_metadata then
update_metadata(meta, inv)
end
end
function station.on_inventory_take(meta, inv, listname, index, stack, player, update_metadata)
if listname == "stairsplus:output" then
station.update_inventory(meta, inv, stack)
else
station.update_inventory(meta, inv)
end
if update_metadata then
update_metadata(meta, inv)
end
end
function station.initialize_metadata(meta, inv, shape_groups, build_formspec, update_metadata)
meta:set_string("stairsplus:shape_groups", minetest.write_json(shape_groups))
if meta:get_int("max_offered") ~= 0 then
meta:set_int("stairsplus:max_offered", meta:get_int("max_offered"))
elseif meta:get_int("stairsplus:max_offered") == 0 then
meta:set_int("stairsplus:max_offered", default_stack_max)
end
if build_formspec then
meta:set_string("formspec", build_formspec(meta, inv))
end
if update_metadata then
update_metadata(meta, inv)
end
end
function station.initialize_inventory(inv, shape_groups)
local output_size = 0
for _, group in ipairs(shape_groups) do
output_size = output_size + #api.shapes_by_group[group]
end
inv:set_size("stairsplus:input", 1)
inv:set_size("stairsplus:micro", 1)
inv:set_size("stairsplus:recycle", 1)
inv:set_size("stairsplus:output", output_size)
-- get rid of old lists
for _, listname in ipairs({ "input", "micro", "recycle", "output" }) do
if inv:get_size(listname) > 0 then
inv:set_list(("stairsplus:%s"):format(listname), inv:get_list(listname))
inv:set_size(listname, 0)
end
end
end
function station.on_construct(pos, shape_groups, build_formspec, update_metadata)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
station.initialize_inventory(inv, shape_groups)
station.initialize_metadata(meta, inv, shape_groups, build_formspec, update_metadata)
station.update_inventory(meta, inv)
end
function station.after_place_node(pos, placer)
local meta = minetest.get_meta(pos)
if futil.is_player(placer) then
meta:set_string("owner", placer:get_player_name())
end
end
function api.register_station(name, def)
local shape_groups = def.shape_groups
local build_formspec = def.build_formspec
local update_metadata = def.update_metadata
if not shape_groups then
error("station requires shape_groups defined")
end
def.shape_groups = nil
def.build_formspec = nil
def.update_metadata = nil
def.after_place_node = def.after_place_node or station.after_place_node
def.on_construct = def.on_construct
or function(pos)
return station.on_construct(pos, shape_groups, build_formspec, update_metadata)
end
def.can_dig = def.can_dig
or function(pos, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return station.can_dig(meta, inv, player)
end
def.on_receive_fields = def.on_receive_fields
or function(pos, formname, fields, sender)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return station.on_receive_fields(meta, inv, formname, fields, sender, build_formspec, update_metadata)
end
def.allow_metadata_inventory_move = def.allow_metadata_inventory_move
or function(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return station.allow_inventory_move(meta, inv, from_list, from_index, to_list, to_index, count, player)
end
def.allow_metadata_inventory_put = def.allow_metadata_inventory_put
or function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return station.allow_inventory_put(meta, inv, listname, index, stack, player)
end
def.on_metadata_inventory_put = def.on_metadata_inventory_put
or function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return station.on_inventory_put(meta, inv, listname, index, stack, player, update_metadata)
end
def.on_metadata_inventory_take = def.on_metadata_inventory_take
or function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return station.on_inventory_take(meta, inv, listname, index, stack, player, update_metadata)
end
def._stairsplus_shape_groups = shape_groups
def.groups = table.copy(def.groups or {})
def.groups.stairsplus_station = 1
minetest.register_node(name, def)
end
api.station = station

View file

@ -0,0 +1,25 @@
local f = string.format
local s = stairsplus.settings
local api = stairsplus.api
if s.whitelist_mode then
api.whitelisted = {}
local filename = futil.path_concat(minetest.get_worldpath(), "stairsplus.whitelist")
local contents = futil.load_file(filename)
if not contents then
error(f("error initializing stairsplus whitelist: %s does not exist", filename))
end
local items = contents:split("\n")
for i = 1, #items do
api.whitelisted[items[i]] = true
end
stairsplus.log("action", f("%s nodes whitelisted", #api.whitelisted))
end
function api.is_whitelisted(name)
if not s.whitelist_mode then
return true
end
return api.whitelisted[name]
end