MineClone5/mods/ITEMS/mcl_anvils/init.lua

234 lines
7.3 KiB
Lua
Raw Normal View History

local MAX_NAME_LENGTH = 30
local MAX_WEAR = 65535
local SAME_TOOL_REPAIR_BOOST = math.ceil(MAX_WEAR * 0.05) -- 5%
local MATERIAL_TOOL_REPAIR_BOOST = math.ceil(MAX_WEAR * 0.25) -- 25%
2018-02-02 04:14:11 +01:00
local function get_anvil_formspec(set_name)
if not set_name then
set_name = ""
end
return "size[9,8.75]"..
"background[-0.19,-0.25;9.41,9.49;mcl_anvils_inventory.png]"..
mcl_vars.inventory_header..
"list[current_player;main;0,4.5;9,3;9]"..
"list[current_player;main;0,7.74;9,1;]"..
"list[context;input;1,2.5;1,1;]"..
"list[context;input;4,2.5;1,1;1]"..
"list[context;output;8,2.5;1,1;]"..
"field[3.25,1;4,1;name;;"..minetest.formspec_escape(set_name).."]"..
"button[7,0.7;2,1;name_button;Set name]"..
"listring[context;output]"..
"listring[current_player;main]"..
"listring[context;input]"..
"listring[current_player;main]"
end
-- Update the inventory slots of an anvil node.
-- meta: Metadata of anvil node
local function update_anvil_slots(meta)
local inv = meta:get_inventory()
local new_name = meta:get_string("set_name")
local input1, input2, output
input1 = inv:get_stack("input", 1)
input2 = inv:get_stack("input", 2)
output = inv:get_stack("output", 1)
local new_output, name_item
local check_rename = false
2018-02-02 04:14:11 +01:00
-- Both input slots occupied
if (not input1:is_empty() and not input2:is_empty()) then
-- Repair, if tool
local def1 = input1:get_definition()
local def2 = input2:get_definition()
-- Same tool twice
if input1:get_name() == input2:get_name() and def1.type == "tool" then
-- Add tool health together plus a 5% bonus
-- TODO: Combine tool enchantments
local new_health = (MAX_WEAR - input1:get_wear()) + (MAX_WEAR - input2:get_wear())
new_health = new_health + SAME_TOOL_REPAIR_BOOST
local new_wear = math.max(0, math.min(MAX_WEAR, MAX_WEAR - new_health))
input1:set_wear(new_wear)
name_item = input1
-- Tool + repair item
else
-- TODO: 25% repair bonus
-- TODO: Combine tool enchantments
new_output = ""
end
-- Exactly 1 input slot occupied
elseif (not input1:is_empty() and input2:is_empty()) or (input1:is_empty() and not input2:is_empty()) then
-- Just rename item
2018-02-02 05:40:22 +01:00
if new_name == nil then
new_name = ""
end
if input1:is_empty() then
name_item = input2
2018-02-02 04:14:11 +01:00
else
2018-02-02 05:40:22 +01:00
name_item = input1
end
end
-- Rename handling
if name_item then
2018-02-02 05:40:22 +01:00
-- No renaming allowed with group no_rename=1
if minetest.get_item_group(name_item:get_name(), "no_rename") == 1 then
2018-02-02 04:14:11 +01:00
new_output = ""
2018-02-02 05:40:22 +01:00
else
local meta = name_item:get_meta()
-- Limit name length
new_name = string.sub(new_name, 1, MAX_NAME_LENGTH)
2018-02-02 05:40:22 +01:00
-- Rename item
meta:set_string("description", new_name)
-- Double-save the name internally, too
meta:set_string("name", new_name)
new_output = name_item
2018-02-02 04:14:11 +01:00
end
else
new_output = ""
end
-- Set the new output slot
if new_output ~= nil then
inv:set_stack("output", 1, new_output)
end
end
2017-02-22 01:05:37 +01:00
local anvildef = {
2017-11-27 07:57:02 +01:00
groups = {pickaxey=1, falling_node=1, crush_after_fall=1, deco_block=1, anvil=1},
2017-07-14 19:54:25 +02:00
tiles = {"mcl_anvils_anvil_top_damaged_0.png^[transformR90", "mcl_anvils_anvil_base.png", "mcl_anvils_anvil_side.png"},
2017-02-22 01:05:37 +01:00
paramtype = "light",
sunlight_propagates = true,
is_ground_content = false,
paramtype2 = "facedir",
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
2018-01-31 19:23:37 +01:00
{-8/16, 2/16, -5/16, 8/16, 8/16, 5/16}, -- top
2018-02-02 17:22:39 +01:00
{-5/16, -4/16, -2/16, 5/16, 5/16, 2/16}, -- middle
2018-01-31 19:23:37 +01:00
{-8/16, -8/16, -5/16, 8/16, -4/16, 5/16}, -- base
2017-02-22 01:05:37 +01:00
}
},
sounds = mcl_sounds.node_sound_metal_defaults(),
_mcl_blast_resistance = 6000,
_mcl_hardness = 5,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
if listname == "output" then
return 0
else
return stack:get_count()
end
end,
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
if to_list == "output" then
return 0
2018-02-02 04:14:11 +01:00
elseif from_list == "output" and to_list == "input" then
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if inv:get_stack(to_list, to_index):is_empty() then
return count
else
return 0
end
else
return count
end
end,
2018-02-02 04:14:11 +01:00
on_metadata_inventory_put = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
update_anvil_slots(meta)
end,
on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
if from_list == "output" and to_list == "input" then
local inv = meta:get_inventory()
inv:set_stack("output", 1, "")
for i=1, inv:get_size("input") do
if i ~= to_index then
inv:set_stack("input", i, "")
end
end
end
update_anvil_slots(meta)
end,
on_metadata_inventory_take = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
if listname == "output" then
local inv = meta:get_inventory()
inv:set_list("input", {"",""})
end
update_anvil_slots(meta)
end,
2018-02-02 02:07:09 +01:00
on_construct = function(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_size("input", 2)
2018-02-02 02:07:09 +01:00
inv:set_size("output", 1)
local form = "size[9,8.75]"..
"background[-0.19,-0.25;9.41,9.49;mcl_anvils_inventory.png]"..
mcl_vars.inventory_header..
"list[current_player;main;0,4.5;9,3;9]"..
"list[current_player;main;0,7.74;9,1;]"..
"list[context;input;1,2.5;1,1;]"..
"list[context;input;4,2.5;1,1;1]"..
"list[context;output;8,2.5;1,1;]"..
2018-02-02 04:14:11 +01:00
"field[3.25,1;4,1;name;;]"..
"button[7,0.7;2,1;name_button;Set name]"..
"listring[context;output]"..
2018-02-02 02:07:09 +01:00
"listring[current_player;main]"..
"listring[context;input]"..
2018-02-02 02:07:09 +01:00
"listring[current_player;main]"
meta:set_string("formspec", form)
end,
2018-02-02 04:14:11 +01:00
on_receive_fields = function(pos, formname, fields, sender)
if fields.name_button then
local set_name
if fields.name == nil then
set_name = ""
else
set_name = fields.name
end
local meta = minetest.get_meta(pos)
-- Limit name length
set_name = string.sub(set_name, 1, MAX_NAME_LENGTH)
2018-02-02 04:14:11 +01:00
meta:set_string("set_name", set_name)
update_anvil_slots(meta)
meta:set_string("formspec", get_anvil_formspec(set_name))
end
end,
2017-02-22 01:05:37 +01:00
}
2017-12-05 14:09:39 +01:00
if minetest.get_modpath("screwdriver") then
anvildef.on_rotate = screwdriver.rotate_simple
end
2017-02-22 01:05:37 +01:00
local anvildef0 = table.copy(anvildef)
anvildef0.description = "Anvil"
local anvildef1 = table.copy(anvildef)
anvildef1.description = "Slightly Damaged Anvil"
anvildef1.groups.not_in_creative_inventory = 1
anvildef1._doc_items_create_entry = false
2017-07-14 19:54:25 +02:00
anvildef1.tiles = {"mcl_anvils_anvil_top_damaged_1.png^[transformR90", "mcl_anvils_anvil_base.png", "mcl_anvils_anvil_side.png"}
2017-02-22 01:05:37 +01:00
local anvildef2 = table.copy(anvildef)
anvildef2.description = "Very Damaged Anvil"
anvildef2.groups.not_in_creative_inventory = 1
anvildef2._doc_items_create_entry = false
2017-07-14 19:54:25 +02:00
anvildef2.tiles = {"mcl_anvils_anvil_top_damaged_2.png^[transformR90", "mcl_anvils_anvil_base.png", "mcl_anvils_anvil_side.png"}
2017-02-22 01:05:37 +01:00
minetest.register_node("mcl_anvils:anvil", anvildef0)
minetest.register_node("mcl_anvils:anvil_damage_1", anvildef1)
minetest.register_node("mcl_anvils:anvil_damage_2", anvildef2)
minetest.register_craft({
output = "mcl_anvils:anvil",
recipe = {
{ "mcl_core:ironblock", "mcl_core:ironblock", "mcl_core:ironblock" },
{ "", "mcl_core:iron_ingot", "" },
{ "mcl_core:iron_ingot", "mcl_core:iron_ingot", "mcl_core:iron_ingot" },
}
})
dofile(minetest.get_modpath(minetest.get_current_modname()).."/falling_anvil.lua")