clean up furnace code a bit by removing duplicate or replacing obsolete code

This commit is contained in:
Tim 2015-08-20 14:27:28 +02:00
parent fc8621da41
commit d7c56bfcdb
1 changed files with 79 additions and 149 deletions

View File

@ -2,22 +2,14 @@
local S = homedecor.gettext
local function hacky_swap_node(pos,name)
local function swap_node(pos, name)
local node = minetest.get_node(pos)
if node.name == name then
return
end
local meta = minetest.get_meta(pos)
local meta0 = meta:to_table()
if node.name == name then return end
node.name = name
local meta0 = meta:to_table()
minetest.set_node(pos,node)
meta = minetest.get_meta(pos)
meta:from_table(meta0)
minetest.swap_node(pos, node)
end
local function make_formspec(furnacedef, percent)
local fire
if percent and (percent > 0) then
@ -75,13 +67,15 @@ local function make_tiles(tiles, fmt, active)
return tiles
end
local furnace_can_dig = function(pos,player)
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
return inv:is_empty("fuel")
and inv:is_empty("dst")
and inv:is_empty("src")
end
function homedecor.register_furnace(name, furnacedef)
local furnacedef = furnacedef
local tiles = make_tiles(furnacedef.tiles, furnacedef.tile_format, false)
local tiles_active = make_tiles(furnacedef.tiles_active, furnacedef.tile_format, true)
furnacedef.fire_fg = furnacedef.fire_bg or "default_furnace_fire_fg.png"
furnacedef.fire_bg = furnacedef.fire_bg or "default_furnace_fire_bg.png"
@ -90,146 +84,80 @@ function homedecor.register_furnace(name, furnacedef)
furnacedef.cook_speed = furnacedef.cook_speed or 1
local name_active = name.."_active"
local description = furnacedef.description or "Furnace"
local desc = furnacedef.description or "Furnace"
local furnace_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", make_formspec(furnacedef, 0))
meta:set_string("infotext", description)
local inv = meta:get_inventory()
inv:set_size("fuel", 1)
inv:set_size("src", 1)
inv:set_size("dst", furnacedef.output_slots)
end
local furnace_allow_put = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if listname == "fuel" then
if minetest.get_craft_result({method="fuel",width=1,items={stack}}).time ~= 0 then
if inv:is_empty("src") then
meta:set_string("infotext", S("%s is empty"):format(description))
end
return stack:get_count()
else
return 0
end
elseif listname == "src" then
return stack:get_count()
elseif listname == "dst" then
return 0
end
end
local furnace_allow_move = function(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local stack = inv:get_stack(from_list, from_index)
if to_list == "fuel" then
if minetest.get_craft_result({method="fuel",width=1,items={stack}}).time ~= 0 then
if inv:is_empty("src") then
meta:set_string("infotext", S("%s is empty"):format(description))
end
return count
else
return 0
end
elseif to_list == "src" then
return count
elseif to_list == "dst" then
return 0
end
end
local def = {
description = furnacedef.description,
tiles = tiles,
description = description,
tiles = make_tiles(furnacedef.tiles, furnacedef.tile_format, false),
groups = furnacedef.groups or {cracky=2},
sounds = furnacedef.sounds or default.node_sound_wood_defaults(),
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", make_formspec(furnacedef, 0))
meta:set_string("infotext", desc)
local inv = meta:get_inventory()
inv:set_size("fuel", 1)
inv:set_size("src", 1)
inv:set_size("dst", furnacedef.output_slots)
end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
if not inv:is_empty("fuel") then
return false
elseif not inv:is_empty("dst") then
return false
elseif not inv:is_empty("src") then
return false
end
return true
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if listname == "fuel" then
if minetest.get_craft_result({method="fuel",width=1,items={stack}}).time ~= 0 then
if inv:is_empty("src") then
meta:set_string("infotext", S("%s is empty"):format(desc))
end
return stack:get_count()
else
return 0
end
elseif listname == "src" then
return stack:get_count()
elseif listname == "dst" then
return 0
end
end,
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local stack = inv:get_stack(from_list, from_index)
if to_list == "fuel" then
if minetest.get_craft_result({method="fuel",width=1,items={stack}}).time ~= 0 then
if inv:is_empty("src") then
meta:set_string("infotext", S("%s is empty"):format(desc))
end
return count
else
return 0
end
elseif to_list == "src" then
return count
elseif to_list == "dst" then
return 0
end
end,
inventory = {
lockable = true
}
on_construct = furnace_construct,
can_dig = furnace_can_dig,
allow_metadata_inventory_put = furnace_allow_put,
allow_metadata_inventory_move = furnace_allow_move,
inventory = { lockable = true }
}
local def_active = {
description = furnacedef.description.." (active)",
tiles = tiles_active,
description = description .. " (active)",
tiles = make_tiles(furnacedef.tiles_active, furnacedef.tile_format, true),
light_source = 8,
drop = "homedecor:" .. name,
groups = furnacedef.groups or {cracky=2, not_in_creative_inventory=1},
sounds = furnacedef.sounds or default.node_sound_stone_defaults(),
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", make_formspec(furnacedef, 0))
meta:set_string("infotext", desc)
local inv = meta:get_inventory()
inv:set_size("fuel", 1)
inv:set_size("src", 1)
inv:set_size("dst", furnacedef.output_slots)
end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
if not inv:is_empty("fuel") then
return false
elseif not inv:is_empty("dst") then
return false
elseif not inv:is_empty("src") then
return false
end
return true
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if listname == "fuel" then
if minetest.get_craft_result({method="fuel",width=1,items={stack}}).time ~= 0 then
if inv:is_empty("src") then
meta:set_string("infotext",S("%s is empty"):format(desc))
end
return stack:get_count()
else
return 0
end
elseif listname == "src" then
return stack:get_count()
elseif listname == "dst" then
return 0
end
end,
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local stack = inv:get_stack(from_list, from_index)
if to_list == "fuel" then
if minetest.get_craft_result({method="fuel",width=1,items={stack}}).time ~= 0 then
if inv:is_empty("src") then
meta:set_string("infotext",S("%s is empty"):format(desc))
end
return count
else
return 0
end
elseif to_list == "src" then
return count
elseif to_list == "dst" then
return 0
end
end,
inventory = {
lockable = true
}
on_construct = furnace_construct,
can_dig = furnace_can_dig,
allow_metadata_inventory_put = furnace_allow_put,
allow_metadata_inventory_move = furnace_allow_move,
inventory = { lockable = true }
}
if furnacedef.extra_nodedef_fields then
@ -239,6 +167,8 @@ function homedecor.register_furnace(name, furnacedef)
end
end
local name_active = name.."_active"
homedecor.register(name, def)
homedecor.register(name_active, def_active)
@ -297,7 +227,7 @@ function homedecor.register_furnace(name, furnacedef)
local percent = math.floor(meta:get_float("fuel_time") /
meta:get_float("fuel_totaltime") * 100)
meta:set_string("infotext",S("%s active: %d%%"):format(desc,percent))
hacky_swap_node(pos,name_active..locked)
swap_node(pos,name_active..locked)
meta:set_string("formspec", make_formspec(furnacedef, percent))
return
end
@ -317,7 +247,7 @@ function homedecor.register_furnace(name, furnacedef)
if (not fuel) or (fuel.time <= 0) then
meta:set_string("infotext",desc..S(": Out of fuel"))
hacky_swap_node(pos,name..locked)
swap_node(pos, name..locked)
meta:set_string("formspec", make_formspec(furnacedef, 0))
return
end
@ -325,7 +255,7 @@ function homedecor.register_furnace(name, furnacedef)
if cooked.item:is_empty() then
if was_active then
meta:set_string("infotext",S("%s is empty"):format(desc))
hacky_swap_node(pos,name..locked)
swap_node(pos, name..locked)
meta:set_string("formspec", make_formspec(furnacedef, 0))
end
return
@ -333,7 +263,7 @@ function homedecor.register_furnace(name, furnacedef)
if not inv:room_for_item("dst", cooked.item) then
meta:set_string("infotext", desc..S(": output bins are full"))
hacky_swap_node(pos, name..locked)
swap_node(pos, name..locked)
meta:set_string("formspec", make_formspec(furnacedef, 0))
return
end