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

View File

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