Combine tools in anvil

This commit is contained in:
Elias Fleckenstein 2020-11-01 15:15:44 +01:00
parent 3fd1963da9
commit 3791f8983e
3 changed files with 58 additions and 20 deletions

View file

@ -3,7 +3,15 @@ function mcl_enchanting.get_enchantments(itemstack)
end end
function mcl_enchanting.set_enchantments(itemstack, enchantments) function mcl_enchanting.set_enchantments(itemstack, enchantments)
return itemstack:get_meta():set_string("mcl_enchanting:enchantments", minetest.serialize(enchantments)) itemstack:get_meta():set_string("mcl_enchanting:enchantments", minetest.serialize(enchantments))
local itemdef = itemstack:get_definition()
for enchantment, level in pairs(enchantments) do
local enchantment_def = mcl_enchanting.enchantments[enchantment]
if enchantment_def.on_enchant then
enchantment_def.on_enchant(itemstack, level, itemdef)
end
end
tt.reload_itemstack_description(itemstack)
end end
function mcl_enchanting.get_enchantment(itemstack, enchantment) function mcl_enchanting.get_enchantment(itemstack, enchantment)
@ -41,8 +49,8 @@ function mcl_enchanting.item_supports_enchantment(itemname, enchantment, early)
return false return false
end end
local enchantment_def = mcl_enchanting.enchantments[enchantment] local enchantment_def = mcl_enchanting.enchantments[enchantment]
local tooldef = minetest.registered_tools[itemname] local itemdef = minetest.registered_items[itemname]
if not tooldef and enchantment_def.requires_tool then if itemdef.type ~= "tool" and enchantment_def.requires_tool then
return false return false
end end
for disallow in pairs(enchantment_def.disallow) do for disallow in pairs(enchantment_def.disallow) do
@ -96,19 +104,49 @@ function mcl_enchanting.enchant(itemstack, enchantment, level)
local enchantments = mcl_enchanting.get_enchantments(itemstack) local enchantments = mcl_enchanting.get_enchantments(itemstack)
enchantments[enchantment] = level enchantments[enchantment] = level
mcl_enchanting.set_enchantments(itemstack, enchantments) mcl_enchanting.set_enchantments(itemstack, enchantments)
mcl_enchanting.reload_enchantments(itemstack, enchantments)
return itemstack return itemstack
end end
function mcl_enchanting.reload_enchantments(itemstack, enchantments) function mcl_enchanting.combine(itemstack, combine_with)
local itemdef = itemstack:get_definition() local itemname = itemstack:get_name()
for enchantment, level in pairs(enchantments) do local enchanted_itemname = mcl_enchanting.get_enchanted_itemstring(itemname)
if enchanted_itemname ~= mcl_enchanting.get_enchanted_itemstring(combine_with:get_name()) then
return false
end
local enchantments = mcl_enchanting.get_enchantments(itemstack)
for enchantment, combine_level in pairs(mcl_enchanting.get_enchantments(combine_with)) do
local enchantment_def = mcl_enchanting.enchantments[enchantment] local enchantment_def = mcl_enchanting.enchantments[enchantment]
if enchantment_def.on_enchant then local enchantment_level = enchantments[combine_enchantment]
enchantment_def.on_enchant(itemstack, level, itemdef) if enchantment_level then
if enchantment_level == combine_level then
enchantment_level = math.min(enchantment_level + 1, enchantment_def.max_level)
end
elseif mcl_enchanting.item_supports_enchantment(itemname, enchantment) then
local supported = true
for incompatible in pairs(enchantment_def.incompatible) do
if enchantments[incompatible] then
supported = false
break
end
end
if supported then
enchantment_level = combine_level
end
end
enchantments[enchantment] = enchantment_level
end
local any_enchantment = false
for enchantment, enchantment_level in pairs(enchantments) do
if enchantment_level > 0 then
any_enchantment = true
break
end end
end end
tt.reload_itemstack_description(itemstack) if any_enchantment then
itemstack:set_name(enchanted_itemname)
end
mcl_enchanting.set_enchantments(itemstack, enchantments)
return true
end end
function mcl_enchanting.initialize() function mcl_enchanting.initialize()
@ -154,7 +192,6 @@ function mcl_enchanting.initialize()
end end
end end
if expensive_test then if expensive_test then
local tooldef = minetest.registered_tools[itemname]
local new_name = itemname .. "_enchanted" local new_name = itemname .. "_enchanted"
minetest.override_item(itemname, {_mcl_enchanting_enchanted_tool = new_name}) minetest.override_item(itemname, {_mcl_enchanting_enchanted_tool = new_name})
local new_def = table.copy(itemdef) local new_def = table.copy(itemdef)
@ -164,7 +201,7 @@ function mcl_enchanting.initialize()
new_def.texture = itemdef.texture or itemname:gsub("%:", "_") new_def.texture = itemdef.texture or itemname:gsub("%:", "_")
new_def._mcl_enchanting_enchanted_tool = new_name new_def._mcl_enchanting_enchanted_tool = new_name
local register_list = item_list local register_list = item_list
if tooldef then if itemdef.type == "tool" then
register_list = tool_list register_list = tool_list
end end
register_list[":" .. new_name] = new_def register_list[":" .. new_name] = new_def

View file

@ -4,3 +4,4 @@ mcl_sounds
mcl_core? mcl_core?
screwdriver? screwdriver?
tt tt
mcl_enchanting

View file

@ -42,9 +42,6 @@ end
-- needs to be used up to repair the tool. -- needs to be used up to repair the tool.
local function get_consumed_materials(tool, material) local function get_consumed_materials(tool, material)
local wear = tool:get_wear() local wear = tool:get_wear()
if wear == 0 then
return 0
end
local health = (MAX_WEAR - wear) local health = (MAX_WEAR - wear)
local matsize = material:get_count() local matsize = material:get_count()
local materials_used = 0 local materials_used = 0
@ -103,12 +100,15 @@ local function update_anvil_slots(meta)
return math.max(0, math.min(MAX_WEAR, MAX_WEAR - new_health)) return math.max(0, math.min(MAX_WEAR, MAX_WEAR - new_health))
end end
-- Same tool twice local can_combine = mcl_enchanting.combine(input1, input2)
if input1:get_name() == input2:get_name() and def1.type == "tool" and (input1:get_wear() > 0 or input2:get_wear() > 0) then
if can_combine then
-- Add tool health together plus a small bonus -- Add tool health together plus a small bonus
-- TODO: Combine tool enchantments if def1.type == "tool" and def2.type == "tool" then
local new_wear = calculate_repair(input1:get_wear(), input2:get_wear(), SAME_TOOL_REPAIR_BOOST) local new_wear = calculate_repair(input1:get_wear(), input2:get_wear(), SAME_TOOL_REPAIR_BOOST)
input1:set_wear(new_wear) input1:set_wear(new_wear)
end
name_item = input1 name_item = input1
new_output = name_item new_output = name_item
-- Tool + repair item -- Tool + repair item