mirror of
https://github.com/minetest-mods/moreblocks.git
synced 2025-07-01 23:50:40 +02:00
almost done, mostly documentation left
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
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.scale_groups = {}
|
||||
@ -41,7 +42,8 @@ function api.build_groups(node, shape)
|
||||
|
||||
local groups = {
|
||||
[("shape_%s"):format(shape)] = 1,
|
||||
not_in_creative_inventory = in_creative_inventory and 1 or nil,
|
||||
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]
|
||||
|
@ -97,7 +97,6 @@ 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
|
||||
|
@ -1,6 +1,7 @@
|
||||
-- for registering variants of a specific node
|
||||
local api = stairsplus.api
|
||||
|
||||
local table_equals = stairsplus.util.table_equals
|
||||
local table_set_all = stairsplus.util.table_set_all
|
||||
local table_sort_keys = stairsplus.util.table_sort_keys
|
||||
|
||||
@ -14,6 +15,12 @@ api.shapes_by_node = {}
|
||||
api.node_by_shaped_node = {}
|
||||
api.shape_by_shaped_node = {}
|
||||
|
||||
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
|
||||
@ -64,9 +71,11 @@ function api.register_single(node, shape, overrides, meta)
|
||||
stairsplus.log("info", "registering %s %s", shape, node)
|
||||
meta = meta or {}
|
||||
overrides = overrides or {}
|
||||
|
||||
if not minetest.registered_nodes[node] then
|
||||
error(("%q is not defined"):format(node))
|
||||
error(("node %q is not defined"):format(node))
|
||||
end
|
||||
|
||||
local node_def = table.copy(minetest.registered_nodes[node])
|
||||
check_node_validity(node_def, meta)
|
||||
|
||||
@ -117,7 +126,7 @@ function api.register_single(node, shape, overrides, meta)
|
||||
climbable = node_def.climbable,
|
||||
move_resistance = node_def.move_resistance,
|
||||
|
||||
on_place = api.on_place,
|
||||
on_place = function(...) return api.on_place(...) end,
|
||||
}
|
||||
|
||||
-- see-through nodes tend to look better if we just use the first tile
|
||||
@ -143,7 +152,11 @@ function api.register_single(node, shape, overrides, meta)
|
||||
end
|
||||
end
|
||||
|
||||
overrides.groups = nil
|
||||
if not table_equals(overrides.groups, node_def.groups) then
|
||||
overrides = table.copy(overrides)
|
||||
overrides.groups = nil
|
||||
end
|
||||
|
||||
table_set_all(def, overrides)
|
||||
|
||||
-- set backface_culling and align_style
|
||||
@ -205,6 +218,10 @@ function api.register_single(node, shape, overrides, meta)
|
||||
shapes[shape] = true
|
||||
api.shapes_by_node[node] = shapes
|
||||
|
||||
for _, func in ipairs(api.registered_on_register_singles) do
|
||||
func(node, shaped_name)
|
||||
end
|
||||
|
||||
return shaped_name
|
||||
end
|
||||
|
||||
@ -262,6 +279,9 @@ function api.get_schema_recipe_item(node, shape_or_item)
|
||||
|
||||
elseif name == "node" then
|
||||
name = node
|
||||
|
||||
elseif not name:match(":") then
|
||||
return
|
||||
end
|
||||
|
||||
if count then
|
||||
|
@ -1,10 +1,6 @@
|
||||
-- for registering recipe schemas
|
||||
-- TODO: should register schemas w/ unified_inventory and i3 and whatever else,
|
||||
-- and hide the recipes for the individual nodes (possibly a setting for such)
|
||||
local api = stairsplus.api
|
||||
|
||||
local recipes_in_creative_inventory = stairsplus.settings.recipes_in_creative_inventory
|
||||
|
||||
api.registered_recipe_schemas = {}
|
||||
api.registered_on_register_craft_schemas = {}
|
||||
|
||||
@ -144,11 +140,6 @@ local function register_for_schema(node, schema)
|
||||
|
||||
stairsplus.log("info", "registering recipe %s", minetest.serialize(recipe):sub(#("return ")))
|
||||
|
||||
if not recipes_in_creative_inventory then
|
||||
-- i don't think anything supports this but...
|
||||
recipe.groups = {not_in_creative_inventory = 1}
|
||||
end
|
||||
|
||||
minetest.register_craft(recipe)
|
||||
end
|
||||
|
||||
@ -180,38 +171,36 @@ local function shapes_match(a, b)
|
||||
return true
|
||||
end
|
||||
|
||||
function api.register_crafts_for_shapes(def)
|
||||
if def.type == "cooking" then
|
||||
function api.register_crafts_for_shapes(recipe)
|
||||
if recipe.type == "cooking" then
|
||||
assert(
|
||||
shapes_match(def.output, def.recipe),
|
||||
("error: shapes of %s and %s do not match"):format(def.output, def.recipe)
|
||||
shapes_match(recipe.output, recipe.recipe),
|
||||
("error: shapes of %s and %s do not match"):format(recipe.output, recipe.recipe)
|
||||
)
|
||||
|
||||
local shapes = api.get_shapes(def.recipe)
|
||||
local shapes = api.get_shapes(recipe.recipe)
|
||||
|
||||
for _, shape in ipairs(shapes) do
|
||||
minetest.register_craft({
|
||||
type = "cooking",
|
||||
output = api.get_schema_recipe_item(def.output, shape),
|
||||
recipe = api.get_schema_recipe_item(def.recipe, shape),
|
||||
cooktime = def.cooktime(api.registered_shapes[shape].eighths),
|
||||
groups = (not recipes_in_creative_inventory) and {not_in_creative_inventory = 1} or nil
|
||||
output = api.get_schema_recipe_item(recipe.output, shape),
|
||||
recipe = api.get_schema_recipe_item(recipe.recipe, shape),
|
||||
cooktime = recipe.cooktime(api.registered_shapes[shape].eighths),
|
||||
})
|
||||
end
|
||||
|
||||
elseif def.type == "fuel" then
|
||||
local shapes = api.get_shapes(def.recipe)
|
||||
elseif recipe.type == "fuel" then
|
||||
local shapes = api.get_shapes(recipe.recipe)
|
||||
|
||||
for _, shape in ipairs(shapes) do
|
||||
minetest.register_craft({
|
||||
type = "fuel",
|
||||
recipe = api.get_schema_recipe_item(def.recipe, shape),
|
||||
burntime = def.burntime(api.registered_shapes[shape].eighths),
|
||||
groups = (not recipes_in_creative_inventory) and {not_in_creative_inventory = 1} or nil
|
||||
recipe = api.get_schema_recipe_item(recipe.recipe, shape),
|
||||
burntime = recipe.burntime(api.registered_shapes[shape].eighths),
|
||||
})
|
||||
end
|
||||
|
||||
else
|
||||
error(("unsupported recipe type %s"):format(def.type))
|
||||
error(("unsupported recipe type %s"):format(recipe.type))
|
||||
end
|
||||
end
|
||||
|
@ -2,19 +2,23 @@
|
||||
|
||||
local api = stairsplus.api
|
||||
|
||||
local S = stairsplus.S
|
||||
local F = minetest.formspec_escape
|
||||
|
||||
local default_stack_max = tonumber(minetest.settings:get("default_stack_max")) or 99
|
||||
|
||||
local station = {}
|
||||
|
||||
--[[
|
||||
TODO this isn't actually modular in the right way for adding different kinds of stations
|
||||
TODO e.g. some of this stuff is very particular to the saw itself
|
||||
]]
|
||||
function station.update_infotext(pos)
|
||||
local node = minetest.get_node(pos)
|
||||
local def = minetest.registered_nodes[node.name]
|
||||
return def.update_infotext(pos)
|
||||
end
|
||||
|
||||
local function get_cost(shaped_node)
|
||||
function station.build_formspec(pos)
|
||||
local node = minetest.get_node(pos)
|
||||
local def = minetest.registered_nodes[node.name]
|
||||
return def.build_formspec(pos)
|
||||
end
|
||||
|
||||
function station.get_cost(shaped_node)
|
||||
if shaped_node == "" then
|
||||
return 0
|
||||
end
|
||||
@ -28,7 +32,7 @@ local function get_cost(shaped_node)
|
||||
return shape_def and shape_def.eighths
|
||||
end
|
||||
|
||||
local function get_current_node(pos)
|
||||
function station.get_current_node(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local inv = meta:get_inventory()
|
||||
|
||||
@ -48,29 +52,28 @@ local function get_current_node(pos)
|
||||
end
|
||||
end
|
||||
|
||||
function station.update_infotext(pos)
|
||||
function station.can_dig(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
|
||||
local parts = {}
|
||||
if station.can_dig(pos) then
|
||||
table.insert(parts, S("Circular Saw is empty"))
|
||||
end
|
||||
|
||||
local owner = meta:get_string("owner")
|
||||
if owner ~= "" then
|
||||
table.insert(parts, S("(owned by @1)", meta:get_string("owner")))
|
||||
end
|
||||
|
||||
meta:set_string("infotext", table.concat(parts, " "))
|
||||
local inv = meta:get_inventory()
|
||||
return inv:is_empty("input") and inv:is_empty("micro")
|
||||
end
|
||||
|
||||
function station.on_receive_fields(pos, formname, fields, sender)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local max = tonumber(fields.max_offered)
|
||||
if max and max > 0 then
|
||||
meta:set_int("max_offered", max)
|
||||
-- Update to show the correct number of items:
|
||||
station.update_inventory(pos)
|
||||
station.update_infotext(pos)
|
||||
end
|
||||
end
|
||||
|
||||
-- Player has taken something out of the box or placed something inside
|
||||
-- that amounts to count microblocks:
|
||||
function station.update_inventory(pos, taken_stack)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local inv = meta:get_inventory()
|
||||
|
||||
local node = get_current_node(pos)
|
||||
local node = station.get_current_node(pos)
|
||||
|
||||
if not node then
|
||||
for i = 1, inv:get_size("output") do
|
||||
@ -84,9 +87,9 @@ function station.update_inventory(pos, taken_stack)
|
||||
local recycle_stack = inv:get_stack("recycle", 1)
|
||||
|
||||
local current_value = 8 * input_stack:get_count() + micro_stack:get_count()
|
||||
local new_value = current_value + get_cost(recycle_stack:get_name()) * recycle_stack:get_count()
|
||||
local new_value = current_value + station.get_cost(recycle_stack:get_name()) * recycle_stack:get_count()
|
||||
if taken_stack then
|
||||
new_value = new_value - get_cost(taken_stack:get_name()) * taken_stack:get_count()
|
||||
new_value = new_value - station.get_cost(taken_stack:get_name()) * taken_stack:get_count()
|
||||
end
|
||||
local new_micros = new_value % 8
|
||||
local new_blocks = math.floor(new_value / 8)
|
||||
@ -132,16 +135,19 @@ function station.update_inventory(pos, taken_stack)
|
||||
end
|
||||
end
|
||||
|
||||
-- The amount of items offered per shape can be configured:
|
||||
function station.on_receive_fields(pos, formname, fields, sender)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local max = tonumber(fields.max_offered)
|
||||
if max and max > 0 then
|
||||
meta:set_int("max_offered", max)
|
||||
-- Update to show the correct number of items:
|
||||
function station.on_metadata_inventory_put(pos, listname, index, stack, player)
|
||||
station.update_inventory(pos)
|
||||
station.update_infotext(pos)
|
||||
end
|
||||
|
||||
function station.on_metadata_inventory_take(pos, listname, index, stack, player)
|
||||
if listname == "output" then
|
||||
station.update_inventory(pos, stack)
|
||||
else
|
||||
station.update_inventory(pos)
|
||||
station.update_infotext(pos)
|
||||
end
|
||||
|
||||
station.update_infotext(pos)
|
||||
end
|
||||
|
||||
-- Moving the inventory of the station around is not allowed because it
|
||||
@ -166,14 +172,14 @@ function station.allow_metadata_inventory_put(pos, listname, index, stack, playe
|
||||
|
||||
local meta = minetest.get_meta(pos)
|
||||
local inv = meta:get_inventory()
|
||||
local current_node = get_current_node(pos)
|
||||
local current_node = station.get_current_node(pos)
|
||||
|
||||
if current_node and node ~= current_node then
|
||||
return 0
|
||||
end
|
||||
|
||||
local count = stack:get_count()
|
||||
local cost = get_cost(shaped_node)
|
||||
local cost = station.get_cost(shaped_node)
|
||||
|
||||
local input_stack = inv:get_stack("input", 1)
|
||||
local micro_stack = inv:get_stack("micro", 1)
|
||||
@ -187,61 +193,10 @@ function station.allow_metadata_inventory_put(pos, listname, index, stack, playe
|
||||
return math.min(count, available_count)
|
||||
end
|
||||
|
||||
function station.on_metadata_inventory_put(pos, listname, index, stack, player)
|
||||
station.update_inventory(pos)
|
||||
station.update_infotext(pos)
|
||||
end
|
||||
|
||||
function station.on_metadata_inventory_take(pos, listname, index, stack, player)
|
||||
if listname == "output" then
|
||||
station.update_inventory(pos, stack)
|
||||
else
|
||||
station.update_inventory(pos)
|
||||
end
|
||||
|
||||
station.update_infotext(pos)
|
||||
end
|
||||
|
||||
function station.build_formspec()
|
||||
local fancy_inv = ""
|
||||
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
|
||||
|
||||
return ([[
|
||||
size[11,10]
|
||||
%s
|
||||
label[0,0;%s]
|
||||
list[current_name;input;1.7,0;1,1;]
|
||||
label[0,1;%s]
|
||||
list[current_name;micro;1.7,1;1,1;]
|
||||
label[0,2;%s]
|
||||
list[current_name;recycle;1.7,2;1,1;]
|
||||
field[0.3,3.5;1,1;max_offered;%s:;${max_offered}]
|
||||
button[1,3.2;1.7,1;Set;%s]
|
||||
|
||||
list[current_name;output;2.8,0;8,6;]
|
||||
list[current_player;main;1.5,6.25;8,4;]
|
||||
|
||||
listring[current_name;output]
|
||||
listring[current_player;main]
|
||||
listring[current_name;recycle]
|
||||
|
||||
listring[current_name;micro]
|
||||
listring[current_player;main]
|
||||
|
||||
listring[current_name;input]
|
||||
listring[current_player;main]
|
||||
]]):format(
|
||||
fancy_inv, S("Nodes"), F(S("Microblocks")), F(S("Input")), F(S("Max")), F(S("Set"))
|
||||
)
|
||||
end
|
||||
|
||||
function station.on_construct(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
|
||||
meta:set_string("formspec", station.build_formspec())
|
||||
meta:set_string("formspec", station.build_formspec(pos))
|
||||
meta:set_string("max_offered", default_stack_max) -- How many items of this kind are offered by default?
|
||||
|
||||
local inv = meta:get_inventory()
|
||||
@ -249,7 +204,7 @@ function station.on_construct(pos)
|
||||
inv:set_size("input", 1) -- Input slot for full blocks of material x.
|
||||
inv:set_size("micro", 1) -- Storage for 1-7 surplus microblocks.
|
||||
inv:set_size("recycle", 1) -- Surplus partial blocks can be placed here.
|
||||
inv:set_size("output", 6 * 8) -- 6x8 versions of stair-parts of material x.
|
||||
inv:set_size("output", 6 * 9) -- 6x9 versions of stair-parts of material x.
|
||||
|
||||
station.update_infotext(pos)
|
||||
end
|
||||
@ -261,12 +216,6 @@ function station.after_place_node(pos, placer)
|
||||
end
|
||||
end
|
||||
|
||||
function station.can_dig(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local inv = meta:get_inventory()
|
||||
return inv:is_empty("input") and inv:is_empty("micro")
|
||||
end
|
||||
|
||||
function api.register_station(name, shape_groups, def)
|
||||
def.after_place_node = def.after_place_node or station.after_place_node
|
||||
def.on_construct = def.on_construct or station.on_construct
|
||||
|
Reference in New Issue
Block a user