2017-02-09 07:08:29 +01:00
|
|
|
-- microexpansion/api.lua
|
|
|
|
local BASENAME = "microexpansion"
|
|
|
|
|
2017-02-24 17:28:36 +01:00
|
|
|
-- [function] Register Recipe
|
|
|
|
function microexpansion.register_recipe(output, recipe)
|
2017-07-25 19:47:25 +02:00
|
|
|
-- Check if disabled
|
|
|
|
if recipe.disabled == true then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-03-01 08:23:39 +01:00
|
|
|
for _,r in ipairs(recipe) do
|
2017-07-25 19:47:25 +02:00
|
|
|
local def = {
|
2021-04-05 10:36:01 +02:00
|
|
|
type = type(r[2]) == "string" and r[2] or nil,
|
2020-03-01 08:23:39 +01:00
|
|
|
output = output.." "..(r[1] or 1),
|
|
|
|
recipe = r[3] or r[2]
|
|
|
|
}
|
|
|
|
minetest.register_craft(def)
|
2017-07-25 19:47:25 +02:00
|
|
|
end
|
2017-02-09 07:08:29 +01:00
|
|
|
end
|
|
|
|
|
2017-02-27 02:52:36 +01:00
|
|
|
-- [function] Register oredef
|
2020-02-27 15:33:45 +01:00
|
|
|
function microexpansion.register_oredef(ore, defs)
|
2017-07-25 19:47:25 +02:00
|
|
|
-- Check if disabled
|
2020-02-27 15:33:45 +01:00
|
|
|
if defs.disabled == true then
|
2017-07-25 19:47:25 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-02-27 15:33:45 +01:00
|
|
|
for _,d in ipairs(defs) do
|
|
|
|
d.ore = "microexpansion:"..ore
|
|
|
|
minetest.register_ore(d)
|
2017-07-25 19:47:25 +02:00
|
|
|
end
|
2017-02-27 02:52:36 +01:00
|
|
|
end
|
|
|
|
|
2017-02-25 01:20:05 +01:00
|
|
|
-- [local function] Choose description colour
|
|
|
|
local function desc_colour(status, desc)
|
2017-07-25 19:47:25 +02:00
|
|
|
if status == "unstable" then
|
|
|
|
return minetest.colorize("orange", desc)
|
|
|
|
elseif status == "no" then
|
|
|
|
return minetest.colorize("red", desc)
|
|
|
|
else
|
|
|
|
return minetest.colorize("white", desc)
|
|
|
|
end
|
2017-02-25 01:20:05 +01:00
|
|
|
end
|
|
|
|
|
2017-02-24 17:28:36 +01:00
|
|
|
-- [function] Register Item
|
2017-02-09 07:08:29 +01:00
|
|
|
function microexpansion.register_item(itemstring, def)
|
2017-07-25 19:47:25 +02:00
|
|
|
-- Check if disabled
|
|
|
|
if def.disabled == true then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- Set usedfor
|
|
|
|
if def.usedfor then
|
|
|
|
def.description = def.description .. "\n"..minetest.colorize("grey", def.usedfor)
|
|
|
|
end
|
|
|
|
-- Update inventory image
|
|
|
|
if def.inventory_image then
|
|
|
|
def.inventory_image = BASENAME.."_"..def.inventory_image..".png"
|
|
|
|
else
|
|
|
|
def.inventory_image = BASENAME.."_"..itemstring..".png"
|
|
|
|
end
|
|
|
|
-- Colour description
|
|
|
|
def.description = desc_colour(def.status, def.description)
|
|
|
|
|
|
|
|
-- Register craftitem
|
|
|
|
minetest.register_craftitem(BASENAME..":"..itemstring, def)
|
|
|
|
|
2020-03-04 16:46:22 +01:00
|
|
|
-- if recipe, Register recipe
|
|
|
|
if def.recipe then
|
|
|
|
microexpansion.register_recipe(BASENAME..":"..itemstring, def.recipe)
|
|
|
|
end
|
2017-02-24 17:28:36 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- [function] Register Node
|
|
|
|
function microexpansion.register_node(itemstring, def)
|
2020-03-04 16:46:22 +01:00
|
|
|
-- Check if disabled
|
|
|
|
if def.disabled == true then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- Set usedfor
|
|
|
|
if def.usedfor then
|
|
|
|
def.description = def.description .. "\n"..minetest.colorize("grey", def.usedfor)
|
|
|
|
end
|
|
|
|
-- Update texture
|
|
|
|
if def.auto_complete ~= false then
|
|
|
|
for i,n in ipairs(def.tiles) do
|
|
|
|
if #def.tiles[i]:split("^") <= 1 then
|
|
|
|
local prefix = ""
|
|
|
|
if def.type == "ore" then
|
|
|
|
prefix = "ore_"
|
|
|
|
end
|
2017-07-25 19:47:25 +02:00
|
|
|
|
2020-03-04 16:46:22 +01:00
|
|
|
def.tiles[i] = BASENAME.."_"..prefix..n..".png"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-07-25 19:47:25 +02:00
|
|
|
-- Colour description
|
|
|
|
def.description = desc_colour(def.status, def.description)
|
|
|
|
-- Update connect_sides
|
|
|
|
if def.connect_sides == "nobottom" then
|
|
|
|
def.connect_sides = { "top", "front", "left", "back", "right" }
|
|
|
|
elseif def.connect_sides == "machine" then
|
|
|
|
def.connect_sides = { "top", "bottom", "left", "back", "right" }
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Register node
|
|
|
|
minetest.register_node(BASENAME..":"..itemstring, def)
|
|
|
|
|
|
|
|
-- if recipe, Register recipe
|
|
|
|
if def.recipe then
|
|
|
|
microexpansion.register_recipe(BASENAME..":"..itemstring, def.recipe)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- if oredef, Register oredef
|
|
|
|
if def.oredef then
|
|
|
|
microexpansion.register_oredef(BASENAME..":"..itemstring, def.oredef)
|
|
|
|
end
|
2017-02-09 07:08:29 +01:00
|
|
|
end
|
2019-05-04 13:53:49 +02:00
|
|
|
|
|
|
|
-- get a node, if nessecary load it
|
|
|
|
function microexpansion.get_node(pos)
|
|
|
|
local node = minetest.get_node_or_nil(pos)
|
|
|
|
if node then return node end
|
|
|
|
local vm = VoxelManip()
|
2019-05-18 07:37:46 +02:00
|
|
|
vm:read_from_map(pos, pos)
|
2019-05-04 13:53:49 +02:00
|
|
|
return minetest.get_node(pos)
|
|
|
|
end
|
|
|
|
|
2020-03-07 18:02:14 +01:00
|
|
|
function microexpansion.update_node(pos,event)
|
|
|
|
local node = microexpansion.get_node(pos)
|
2019-05-04 13:53:49 +02:00
|
|
|
local def = minetest.registered_nodes[node.name]
|
2020-03-07 18:02:14 +01:00
|
|
|
local ev = event or {type = "n/a"}
|
|
|
|
if def.me_update then
|
|
|
|
def.me_update(pos,node,ev)
|
2019-05-04 13:53:49 +02:00
|
|
|
end
|
|
|
|
end
|
2023-10-20 18:31:56 +02:00
|
|
|
|
|
|
|
-- [function] Move items from inv to inv
|
|
|
|
function microexpansion.move_inv(inv1, inv2, max, filter)
|
|
|
|
if max <= 0 then return end
|
|
|
|
local finv, tinv = inv1.inv, inv2.inv
|
|
|
|
local fname, tname = inv1.name, inv2.name
|
|
|
|
local huge = inv2.huge
|
|
|
|
local inserted = 0
|
|
|
|
|
|
|
|
for _,v in ipairs(finv:get_list(fname) or {}) do
|
|
|
|
local left = max-inserted
|
|
|
|
if left <= 0 then
|
|
|
|
break;
|
|
|
|
end
|
|
|
|
if not v:is_empty() then
|
|
|
|
if v:get_count() > left then
|
|
|
|
v = v:peek_item(left)
|
|
|
|
end
|
|
|
|
if tinv and tinv:room_for_item(tname, v) and (not filter or not filter(v)) then
|
|
|
|
if huge then
|
|
|
|
microexpansion.insert_item(v, tinv, tname)
|
|
|
|
finv:remove_item(fname, v)
|
|
|
|
else
|
|
|
|
--TODO: continue inserting from the same stack if it is bigger than max
|
|
|
|
if v:get_count() > v:get_stack_max() then
|
|
|
|
v = v:peek_item(v:get_stack_max())
|
|
|
|
end
|
|
|
|
local leftover = tinv:add_item(tname, v)
|
|
|
|
finv:remove_item(fname, v)
|
|
|
|
if leftover and not(leftover:is_empty()) then
|
|
|
|
microexpansion.log("leftover items when transferring inventory","warning")
|
|
|
|
finv:add_item(fname, leftover)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
inserted = inserted + v:get_count()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return inserted
|
|
|
|
end
|