local fragments_per_ingot = 4

minetest.register_lbm {
	label = "delete duranium ore";
	name = "sorcery:delete_duranium_ore";
	nodenames = {'sorcery:stone_with_duranium'};
	action = function(pos,node)
		minetest.set_node(pos, {name = 'default:stone'})
	end
}

sorcery.data.alloys = {}
sorcery.data.metallookup = {
	-- compat bullshit
	['moreores:silver_ingot'] = {
		id = 'silver'; data = sorcery.data.metals.silver;
		value = fragments_per_ingot;
	};
	['moreores:silver_block'] = {
		id = 'silver'; data = sorcery.data.metals.silver;
		value = fragments_per_ingot * 9;
	};

	['basic_materials:brass_ingot'] = {
		id = 'brass'; data = sorcery.data.metals.brass;
		value = fragments_per_ingot;
	};
	['basic_materials:brass_block'] = {
		id = 'brass'; data = sorcery.data.metals.brass;
		value = fragments_per_ingot * 9;
	};
	['morelights_vintage:brass_ingot'] = {
		id = 'brass'; data = sorcery.data.metals.brass;
		value = fragments_per_ingot;
	};
	['morelights_vintage:brass_block'] = {
		id = 'brass'; data = sorcery.data.metals.brass;
		value = fragments_per_ingot * 9;
	};
}

local tools, armors = sorcery.matreg.tools, sorcery.matreg.armors
for name, metal in pairs(sorcery.data.metals) do
	local ingot = metal.ingot or 'sorcery:' .. name .. '_ingot'
	local block = metal.block or 'sorcery:' .. name .. '_block'
	local fragment = 'sorcery:fragment_' .. name
	if not metal.no_tools then for _,t in pairs(tools) do
		sorcery.matreg.lookup[(metal.items and metal.items[t]) or ('sorcery:' .. t .. '_' .. name)] = {
			metal = true;
			id = name; data = metal;
		}
	end end
	if not metal.no_armor then for _,a in pairs(armors) do
		sorcery.matreg.lookup[(metal.items and metal.items[t]) or ('sorcery:' .. a .. '_' .. name)] = {
			metal = true;
			id = name; data = metal;
		}
	end end
	sorcery.data.metallookup[ingot] = {
		id = name; data = metal;
		value = fragments_per_ingot;
	}
	sorcery.data.metallookup[block] = {
		id = name; data = metal;
		value = fragments_per_ingot * 9;
	}
	sorcery.data.metallookup[fragment] = {
		id = name; data = metal;
		value = 1;
	}
	if not sorcery.compat.defp(ingot) then
		-- TODO: remove instant_ores dependency
		instant_ores.register_metal {
			name = 'sorcery:' .. name;
			description = sorcery.lib.str.capitalize(name);
			color = sorcery.lib.color(metal.tone):hex() .. ':' .. ((metal.alpha and tostring(metal.alpha)) or '45');
			rarity = metal.rarity;
			depth = metal.depth;
			no_armor = metal.no_armor;
			no_tools = metal.no_tools;
			durability = metal.durability;
			power = metal.power;
			speed = metal.speed;
			artificial = metal.artificial;
			cooktime = metal.cooktime;
			hardness = metal.hardness;
			ingot_image = (metal.image and metal.image.ingot) or nil;
			lump_image = (metal.image and metal.image.lump) or nil;
			armor_weight = metal.armor_weight;
			armor_protection = metal.armor_protection;
		}
	end
	minetest.register_craftitem(fragment, {
		inventory_image = 'sorcery_' .. name .. '_fragment.png';
		description = sorcery.lib.str.capitalize(name) .. ' fragment';
	})
	minetest.register_craft {
		type = 'cooking';
		recipe = ingot;
		cooktime = (metal.cooktime or 4) / 2;
		output = fragment .. ' ' .. tostring(fragments_per_ingot);
	}
	do local rec = {}
		for i=1,fragments_per_ingot do
			rec[#rec+1]=fragment 
		end
		minetest.register_craft {
			type = 'shapeless';
			recipe = rec;
			output = ingot;
		}
	end
	if metal.mix then
		sorcery.data.register.alloy(sorcery.lib.tbl.merge(metal.mix, {
			output = name;
			cooktime = metal.cooktime or 10;
		}))
	end
end
