mirror of
https://github.com/Sokomine/cottages.git
synced 2025-07-13 13:20:22 +02:00
refactor, cleanup, API, bugfixes (#1)
This commit is contained in:
121
modules/barrel/api.lua
Normal file
121
modules/barrel/api.lua
Normal file
@ -0,0 +1,121 @@
|
||||
local max_liquid_amount = cottages.settings.barrel.max_liquid_amount
|
||||
|
||||
local api = cottages.barrel
|
||||
|
||||
api.bucket_empty_by_bucket_full = {}
|
||||
api.bucket_full_by_empty_and_liquid = {}
|
||||
api.liquid_by_bucket_full = {}
|
||||
|
||||
api.name_by_liquid = {}
|
||||
api.texture_by_liquid = {}
|
||||
|
||||
api.input_sound_by_liquid = {}
|
||||
api.output_sound_by_liquid = {}
|
||||
|
||||
function api.get_barrel_liquid(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
return meta:get("liquid")
|
||||
end
|
||||
|
||||
function api.set_barrel_liquid(pos, liquid)
|
||||
local meta = minetest.get_meta(pos)
|
||||
meta:set_string("liquid", liquid)
|
||||
end
|
||||
|
||||
function api.get_liquid_amount(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
return meta:get_int("amount")
|
||||
end
|
||||
|
||||
function api.increase_liquid_amount(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
meta:set_int("amount", meta:get_int("amount") + 1)
|
||||
end
|
||||
|
||||
function api.decrease_liquid_amount(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local amount = meta:get_int("amount") - 1
|
||||
meta:set_int("amount", amount)
|
||||
if amount == 0 then
|
||||
api.set_barrel_liquid(pos, "")
|
||||
end
|
||||
end
|
||||
|
||||
local function empty_and_liquid(bucket_empty, liquid)
|
||||
return table.concat({bucket_empty, liquid}, "::")
|
||||
end
|
||||
|
||||
function api.register_barrel_liquid(def)
|
||||
api.liquid_by_bucket_full[def.bucket_full] = def.liquid
|
||||
api.bucket_empty_by_bucket_full[def.bucket_full] = def.bucket_empty
|
||||
|
||||
api.bucket_full_by_empty_and_liquid[empty_and_liquid(def.bucket_empty, def.liquid)] = def.bucket_full
|
||||
|
||||
api.name_by_liquid[def.liquid] = def.liquid_name
|
||||
api.texture_by_liquid[def.liquid] = def.liquid_texture
|
||||
api.input_sound_by_liquid[def.liquid] = def.liquid_input_sound
|
||||
api.output_sound_by_liquid[def.liquid] = def.liquid_output_sound
|
||||
end
|
||||
|
||||
function api.get_bucket_liquid(bucket_full)
|
||||
return api.liquid_by_bucket_full[bucket_full]
|
||||
end
|
||||
|
||||
function api.get_bucket_empty(liquid)
|
||||
return api.bucket_empty_by_liquid[liquid]
|
||||
end
|
||||
|
||||
function api.get_bucket_empty(bucket_full)
|
||||
return api.bucket_empty_by_bucket_full[bucket_full]
|
||||
end
|
||||
|
||||
function api.get_bucket_full(bucket_empty, liquid)
|
||||
return api.bucket_full_by_empty_and_liquid[empty_and_liquid(bucket_empty, liquid)]
|
||||
end
|
||||
|
||||
function api.can_fill(pos, bucket_empty)
|
||||
local liquid = api.get_barrel_liquid(pos)
|
||||
return liquid and api.get_bucket_full(bucket_empty, liquid)
|
||||
end
|
||||
|
||||
function api.can_drain(pos, bucket_full)
|
||||
local barrel_liquid = api.get_barrel_liquid(pos)
|
||||
local liquid_amount = api.get_liquid_amount(pos)
|
||||
local bucket_liquid = api.get_bucket_liquid(bucket_full)
|
||||
|
||||
if (not bucket_liquid) or liquid_amount >= max_liquid_amount then
|
||||
return false
|
||||
end
|
||||
|
||||
return bucket_liquid and ((not barrel_liquid) or barrel_liquid == bucket_liquid)
|
||||
end
|
||||
|
||||
function api.add_barrel_liquid(pos, bucket_full)
|
||||
local liquid = api.get_bucket_liquid(bucket_full)
|
||||
|
||||
if not api.get_barrel_liquid(pos) then
|
||||
api.set_barrel_liquid(pos, liquid)
|
||||
end
|
||||
|
||||
api.increase_liquid_amount(pos)
|
||||
|
||||
minetest.sound_play(
|
||||
{name = api.input_sound_by_liquid[liquid]},
|
||||
{pos = pos, loop = false, gain = 0.5, pitch = 2.0}
|
||||
)
|
||||
|
||||
return api.get_bucket_empty(bucket_full)
|
||||
end
|
||||
|
||||
function api.drain_barrel_liquid(pos, bucket_empty)
|
||||
local liquid = api.get_barrel_liquid(pos)
|
||||
|
||||
api.decrease_liquid_amount(pos)
|
||||
|
||||
minetest.sound_play(
|
||||
{name = api.output_sound_by_liquid[liquid]},
|
||||
{pos = pos, loop = false, gain = 0.5, pitch = 2.0}
|
||||
)
|
||||
|
||||
return api.get_bucket_full(bucket_empty, liquid)
|
||||
end
|
168
modules/barrel/barrel.lua
Normal file
168
modules/barrel/barrel.lua
Normal file
@ -0,0 +1,168 @@
|
||||
local S = cottages.S
|
||||
local F = minetest.formspec_escape
|
||||
local FS = function(...) return F(S(...)) end
|
||||
|
||||
local max_liquid_amount = cottages.settings.barrel.max_liquid_amount
|
||||
|
||||
local barrel = cottages.barrel
|
||||
|
||||
function barrel.get_barrel_info(pos)
|
||||
local liquid = barrel.get_barrel_liquid(pos)
|
||||
if liquid then
|
||||
return ("%s (%i/%i)"):format(
|
||||
barrel.name_by_liquid[liquid],
|
||||
barrel.get_liquid_amount(pos),
|
||||
max_liquid_amount
|
||||
)
|
||||
|
||||
else
|
||||
return S("Empty")
|
||||
end
|
||||
end
|
||||
|
||||
function barrel.get_barrel_fs_parts(pos)
|
||||
local parts = {
|
||||
("size[8,9]"),
|
||||
("label[0,0.0;%s]"):format(FS("barrel (liquid storage)")),
|
||||
("label[3,0;%s]"):format(FS("fill:")),
|
||||
("list[context;input;3,0.5;1,1;]"),
|
||||
("label[5,3.3;%s]"):format(FS("drain:")),
|
||||
("list[context;output;5,3.8;1,1;]"),
|
||||
("list[current_player;main;0,5;8,4;]"),
|
||||
("listring[context;output]"),
|
||||
("listring[current_player;main]"),
|
||||
("listring[context;input]"),
|
||||
("listring[current_player;main]"),
|
||||
}
|
||||
|
||||
local liquid = barrel.get_barrel_liquid(pos)
|
||||
local liquid_amount = barrel.get_liquid_amount(pos)
|
||||
|
||||
if liquid then
|
||||
local liquid_texture = barrel.texture_by_liquid[liquid]
|
||||
table.insert(parts, ("image[2.6,2;2,3;%s^[resize:99x99^[lowpart:%s:%s]"):format(
|
||||
F(cottages.textures.furniture),
|
||||
math.floor(99 * liquid_amount / max_liquid_amount),
|
||||
F(liquid_texture .. futil.escape_texture("^[resize:99x99"))
|
||||
))
|
||||
table.insert(parts, ("tooltip[2.6,2;2,3;%s]"):format(
|
||||
F(("%s (%i/%i)"):format(
|
||||
barrel.name_by_liquid[liquid],
|
||||
barrel.get_liquid_amount(pos),
|
||||
max_liquid_amount
|
||||
)))
|
||||
)
|
||||
|
||||
else
|
||||
table.insert(parts, ("image[2.6,2;2,3;%s^[resize:99x99^[lowpart:%s:%s]"):format(
|
||||
F(cottages.textures.furniture),
|
||||
0,
|
||||
F(cottages.textures.furniture .. futil.escape_texture("^[resize:99x99"))
|
||||
))
|
||||
end
|
||||
|
||||
return parts
|
||||
end
|
||||
|
||||
|
||||
function barrel.can_dig(pos, player)
|
||||
return barrel.get_liquid_amount(pos) == 0
|
||||
end
|
||||
|
||||
function barrel.allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local inv = meta:get_inventory()
|
||||
local to_stack = inv:get_stack(to_list, to_index)
|
||||
|
||||
if not to_stack:is_empty() then
|
||||
return 0
|
||||
end
|
||||
|
||||
local from_stack = inv:get_stack(from_list, from_index)
|
||||
local item = from_stack:get_name()
|
||||
|
||||
if to_list == "input" then
|
||||
if barrel.can_drain(pos, item) then
|
||||
return 1
|
||||
end
|
||||
|
||||
elseif to_list == "output" then
|
||||
if barrel.can_fill(pos, item) then
|
||||
return 1
|
||||
end
|
||||
end
|
||||
|
||||
return 0
|
||||
end
|
||||
|
||||
function barrel.allow_metadata_inventory_put(pos, listname, index, stack, player)
|
||||
local item = stack:get_name()
|
||||
|
||||
if listname == "input" then
|
||||
if barrel.can_drain(pos, item) then
|
||||
return 1
|
||||
end
|
||||
|
||||
elseif listname == "output" then
|
||||
if barrel.can_fill(pos, item) then
|
||||
return 1
|
||||
end
|
||||
end
|
||||
|
||||
return 0
|
||||
end
|
||||
|
||||
function barrel.on_metadata_inventory_put(pos, listname, index, stack, player)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local inv = meta:get_inventory()
|
||||
local name = stack:get_name()
|
||||
|
||||
if listname == "input" then
|
||||
local empty = barrel.add_barrel_liquid(pos, name)
|
||||
inv:set_stack(listname, index, empty)
|
||||
|
||||
elseif listname == "output" then
|
||||
local full = barrel.drain_barrel_liquid(pos, name)
|
||||
inv:set_stack(listname, index, full)
|
||||
end
|
||||
end
|
||||
|
||||
function barrel.on_metadata_inventory_move(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(to_list, to_index)
|
||||
barrel.on_metadata_inventory_put(pos, to_list, to_index, stack, player)
|
||||
end
|
||||
|
||||
cottages.api.register_machine("cottages:barrel", {
|
||||
description = S("barrel"),
|
||||
paramtype = "light",
|
||||
paramtype2 = "facedir",
|
||||
drawtype = "mesh",
|
||||
mesh = "cottages_barrel_closed.obj",
|
||||
tiles = {"cottages_barrel.png"},
|
||||
is_ground_content = false,
|
||||
groups = {
|
||||
snappy = 1,
|
||||
choppy = 2,
|
||||
oddly_breakable_by_hand = 1,
|
||||
flammable = 2
|
||||
},
|
||||
sounds = cottages.sounds.wood,
|
||||
|
||||
inv_info = {
|
||||
input = 1,
|
||||
output = 1,
|
||||
},
|
||||
|
||||
can_dig = barrel.can_dig,
|
||||
|
||||
get_fs_parts = barrel.get_barrel_fs_parts,
|
||||
get_info = barrel.get_barrel_info,
|
||||
|
||||
allow_metadata_inventory_move = barrel.allow_metadata_inventory_move,
|
||||
allow_metadata_inventory_put = barrel.allow_metadata_inventory_put,
|
||||
|
||||
on_metadata_inventory_move = barrel.on_metadata_inventory_move,
|
||||
on_metadata_inventory_put = barrel.on_metadata_inventory_put,
|
||||
})
|
41
modules/barrel/compat.lua
Normal file
41
modules/barrel/compat.lua
Normal file
@ -0,0 +1,41 @@
|
||||
local api = cottages.barrel
|
||||
|
||||
if cottages.has.bucket and cottages.has.default then
|
||||
local S = minetest.get_translator("default")
|
||||
|
||||
if minetest.registered_items["bucket:bucket_water"] then
|
||||
api.register_barrel_liquid({
|
||||
liquid = "default:water_source",
|
||||
liquid_name = S("Water"),
|
||||
liquid_texture = "default_water.png",
|
||||
liquid_input_sound = cottages.sounds.water_empty,
|
||||
liquid_output_sound = cottages.sounds.water_fill,
|
||||
bucket_empty = "bucket:bucket_empty",
|
||||
bucket_full = "bucket:bucket_water",
|
||||
})
|
||||
end
|
||||
|
||||
if minetest.registered_items["bucket:bucket_river_water"] then
|
||||
api.register_barrel_liquid({
|
||||
liquid = "default:river_water_source",
|
||||
liquid_name = S("River Water"),
|
||||
liquid_texture = "default_river_water.png",
|
||||
liquid_input_sound = cottages.sounds.water_empty,
|
||||
liquid_output_sound = cottages.sounds.water_fill,
|
||||
bucket_empty = "bucket:bucket_empty",
|
||||
bucket_full = "bucket:bucket_river_water",
|
||||
})
|
||||
end
|
||||
|
||||
if minetest.registered_items["bucket:bucket_lava"] then
|
||||
api.register_barrel_liquid({
|
||||
liquid = "default:lava_source",
|
||||
liquid_name = S("Lava"),
|
||||
liquid_input_sound = cottages.sounds.lava_empty,
|
||||
liquid_output_sound = cottages.sounds.lava_fill,
|
||||
liquid_texture = "default_lava.png",
|
||||
bucket_empty = "bucket:bucket_empty",
|
||||
bucket_full = "bucket:bucket_lava",
|
||||
})
|
||||
end
|
||||
end
|
23
modules/barrel/convert.lua
Normal file
23
modules/barrel/convert.lua
Normal file
@ -0,0 +1,23 @@
|
||||
local api = cottages.barrel
|
||||
|
||||
local rotations = {
|
||||
3 * 4,
|
||||
2 * 4,
|
||||
4 * 4,
|
||||
1 * 4,
|
||||
}
|
||||
|
||||
minetest.register_lbm({
|
||||
label = "Convert lying barrels",
|
||||
name = "cottages:convert_lying_barrels",
|
||||
nodenames = {"cottages:barrel_lying", "cottages:barrel_lying_open"},
|
||||
run_at_every_load = false,
|
||||
action = function(pos, node)
|
||||
node.name = string.gsub(node.name, "_lying", "")
|
||||
node.param2 = rotations[node.param2 + 1] or 0
|
||||
minetest.swap_node(pos, node)
|
||||
|
||||
api.update_infotext(pos)
|
||||
api.update_formspec(pos)
|
||||
end
|
||||
})
|
36
modules/barrel/crafts.lua
Normal file
36
modules/barrel/crafts.lua
Normal file
@ -0,0 +1,36 @@
|
||||
local ci = cottages.craftitems
|
||||
|
||||
if ci.wood and ci.steel then
|
||||
minetest.register_craft({
|
||||
output = "cottages:barrel",
|
||||
recipe = {
|
||||
{ci.wood, ci.wood, ci.wood},
|
||||
{ci.steel, "", ci.steel},
|
||||
{ci.wood, ci.wood, ci.wood},
|
||||
},
|
||||
})
|
||||
|
||||
minetest.register_craft({
|
||||
output = "cottages:barrel_open",
|
||||
recipe = {
|
||||
{ci.wood, "", ci.wood},
|
||||
{ci.steel, "", ci.steel},
|
||||
{ci.wood, ci.wood, ci.wood},
|
||||
},
|
||||
})
|
||||
end
|
||||
|
||||
minetest.register_craft({
|
||||
output = "cottages:tub 2",
|
||||
recipe = {
|
||||
{"cottages:barrel"},
|
||||
},
|
||||
})
|
||||
|
||||
minetest.register_craft({
|
||||
output = "cottages:barrel",
|
||||
recipe = {
|
||||
{"cottages:tub"},
|
||||
{"cottages:tub"},
|
||||
},
|
||||
})
|
8
modules/barrel/init.lua
Normal file
8
modules/barrel/init.lua
Normal file
@ -0,0 +1,8 @@
|
||||
cottages.barrel = {}
|
||||
|
||||
cottages.dofile("modules", "barrel", "api")
|
||||
cottages.dofile("modules", "barrel", "barrel")
|
||||
cottages.dofile("modules", "barrel", "nodes")
|
||||
cottages.dofile("modules", "barrel", "convert")
|
||||
cottages.dofile("modules", "barrel", "crafts")
|
||||
cottages.dofile("modules", "barrel", "compat")
|
44
modules/barrel/nodes.lua
Normal file
44
modules/barrel/nodes.lua
Normal file
@ -0,0 +1,44 @@
|
||||
local S = cottages.S
|
||||
|
||||
-- this barrel is opened at the top
|
||||
minetest.register_node("cottages:barrel_open", {
|
||||
description = S("Barrel (Open)"),
|
||||
drawtype = "mesh",
|
||||
paramtype = "light",
|
||||
paramtype2 = "facedir",
|
||||
mesh = "cottages_barrel.obj",
|
||||
tiles = {"cottages_barrel.png"},
|
||||
is_ground_content = false,
|
||||
groups = {
|
||||
snappy = 1,
|
||||
choppy = 2,
|
||||
oddly_breakable_by_hand = 1,
|
||||
flammable = 2,
|
||||
},
|
||||
})
|
||||
|
||||
-- let's hope "tub" is the correct english word for "bottich"
|
||||
minetest.register_node("cottages:tub", {
|
||||
description = S("tub"),
|
||||
paramtype = "light",
|
||||
drawtype = "mesh",
|
||||
mesh = "cottages_tub.obj",
|
||||
tiles = {"cottages_barrel.png"},
|
||||
selection_box = {
|
||||
type = "fixed",
|
||||
fixed = {
|
||||
{-0.5, -0.5, -0.5, 0.5, -0.1, 0.5},
|
||||
}},
|
||||
collision_box = {
|
||||
type = "fixed",
|
||||
fixed = {
|
||||
{-0.5, -0.5, -0.5, 0.5, -0.1, 0.5},
|
||||
}},
|
||||
groups = {
|
||||
snappy = 1,
|
||||
choppy = 2,
|
||||
oddly_breakable_by_hand = 1,
|
||||
flammable = 2
|
||||
},
|
||||
is_ground_content = false,
|
||||
})
|
Reference in New Issue
Block a user