Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion multifurnace/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,45 @@ local function notify_ports_removal(pos)
end
end

local function get_buffer_layers(pos)
local meta = core.get_meta(pos)
local count = meta:get_int("buffers")
local layers = {}

for i = 1, count do
local stack = ItemStack(meta:get_string("buffer" .. i))
if not stack:is_empty() then
table.insert(layers, {
fluid = stack:get_name(),
amount = stack:get_count()
})
end
end

return layers
end

function multifurnace.api.update_fluid_entity(pos)
local info = multifurnace.api.get_controller_info(pos)
if not info then
multifurnace.fluid_entity.remove(pos)
return
end

local layers = get_buffer_layers(pos)
local dimensions = vector.subtract(info.box_max, info.box_min)
local capacity = info.volume * 1000

for _, layer in ipairs(layers) do
layer.fill_ratio = layer.amount / capacity
end

multifurnace.fluid_entity.create_box(pos, vector.subtract(info.box_min, 0.5),
vector.add(dimensions,
{x = 1, y = 0, z = 1}),
layers)
end

function multifurnace.api.structure_detect(node, pos, max_dim)
local back = vector.add(pos, minetest.facedir_to_dir(node.param2))
local center, min, max = detect_center(back, max_dim - 1)
Expand Down Expand Up @@ -211,14 +250,15 @@ function multifurnace.api.check_controller(pos)
end

local def = core.registered_nodes[node.name]
local dimensions, ports, tanks, min =
local dimensions, ports, tanks, center, min =
multifurnace.api.structure_detect(node, pos,
def._multifurnace_max_dimensions or 8)
local key = core.pos_to_string(pos)
local ctrl_meta = core.get_meta(pos)

if not dimensions then
ctrl_meta:set_string("serial", "")
multifurnace.fluid_entity.remove(pos)
notify_ports_removal(pos)
update_timer(pos)
return
Expand All @@ -229,6 +269,7 @@ function multifurnace.api.check_controller(pos)
for other_key, other_ctrl in pairs(multifurnace.loaded_controllers) do
if key ~= other_key and vector.equals(other_ctrl.box_min, min) and
vector.equals(other_ctrl.box_max, bounds_end) then
multifurnace.fluid_entity.remove(pos)
update_timer(pos)
return
end
Expand All @@ -248,11 +289,13 @@ function multifurnace.api.check_controller(pos)
volume = volume,
box_min = min,
box_max = bounds_end,
center = center,
max_dim = def._multifurnace_max_dimensions,
fuel_consumption = def._multifurnace_fuel_consumption,
ports = ports,
tanks = tanks
}
multifurnace.api.update_fluid_entity(pos)
update_timer(pos)
end

Expand All @@ -264,6 +307,7 @@ function multifurnace.api.remove_controller(pos)
end

local key = core.pos_to_string(pos)
multifurnace.fluid_entity.remove(pos)
notify_ports_removal(pos)
multifurnace.loaded_controllers[key] = nil
multifurnace.api.component_changed_nearby(pos)
Expand Down
45 changes: 45 additions & 0 deletions multifurnace/casting_table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,41 @@ local function update_timer(pos)
if not t:is_started() then t:start(1.0) end
end

local function update_fluid_entity(pos)
local meta = core.get_meta(pos)
local liquid = meta:get_string("liquid")
local amount = meta:get_int("liquid_amount")
local total = meta:get_int("liquid_total")
local texture_modifier = nil

if liquid == "" or amount <= 0 or total <= 0 then
multifurnace.fluid_entity.remove(pos)
return
end

local solidify = meta:get_int("solidify")
if solidify > 0 then
local def = core.registered_items[core.get_node(pos).name]
local cooldown = def._multifurnace_casting_cooldown
local colorize = math.floor(140 * solidify / cooldown + 0.5)
texture_modifier = "^[colorize:#000000:" .. colorize
end

multifurnace.fluid_entity.create_box(pos, {
x = pos.x - 0.375,
y = pos.y + 0.44,
z = pos.z - 0.375
}, {
x = 0.75,
y = 0.06,
z = 0.75
}, {{
fluid = liquid,
fill_ratio = amount / total,
texture_modifier = texture_modifier
}})
end

local function create_item_entity(istack, cast, tpos)
local vpos = vector.add(tpos, {x = 0, y = 0.5, z = 0})
local e = core.add_entity(vpos, "multifurnace:table_item")
Expand Down Expand Up @@ -102,6 +137,7 @@ local function on_timer(pos, elapsed)
if solidify < cooldown then
refresh = true
meta:set_int("solidify", solidify + 1)
update_fluid_entity(pos)
else
liquid = ""
liqc = 0
Expand All @@ -113,6 +149,7 @@ local function on_timer(pos, elapsed)
meta:set_string("liquid", liquid)
meta:set_int("liquid_amount", liqc)
meta:set_int("solidify", 0)
update_fluid_entity(pos)

if output_cast then
inv:set_stack("item", 1, "")
Expand Down Expand Up @@ -190,6 +227,7 @@ core.register_node("multifurnace:casting_table", {
meta:set_int("liquid_amount", 0)
meta:set_string("liquid", "")
meta:set_int("solidify", 0)
update_fluid_entity(pos)
end

set_item_entities(inv, pos)
Expand Down Expand Up @@ -223,6 +261,9 @@ core.register_node("multifurnace:casting_table", {
inv:set_size("cast", 1)
inv:set_size("item", 1)
end,
on_destruct = function(pos)
multifurnace.fluid_entity.remove(pos)
end,
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
local i = itemstack:get_name()
local meta = core.get_meta(pos)
Expand Down Expand Up @@ -253,6 +294,7 @@ core.register_node("multifurnace:casting_table", {
meta:set_int("liquid_amount", 0)
meta:set_string("liquid", "")
meta:set_int("solidify", 0)
update_fluid_entity(pos)
end

to_give = inv:get_stack("cast", 1)
Expand Down Expand Up @@ -325,6 +367,7 @@ core.register_node("multifurnace:casting_table", {

meta:set_string("liquid", liquid)
meta:set_int("liquid_amount", liqc + add)
update_fluid_entity(pos)
update_timer(pos)

return leftovers
Expand Down Expand Up @@ -374,6 +417,7 @@ core.register_lbm({
local meta = core.get_meta(pos)
local inv = meta:get_inventory()
set_item_entities(inv, pos)
update_fluid_entity(pos)
end
})

Expand Down Expand Up @@ -420,6 +464,7 @@ if core.get_modpath("tubelib") then
meta:set_int("liquid_amount", 0)
meta:set_string("liquid", "")
meta:set_int("solidify", 0)
update_fluid_entity(pos)
end

local removing = inv:get_stack("cast", 1)
Expand Down
100 changes: 100 additions & 0 deletions multifurnace/fluid_entity.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
multifurnace.fluid_entity = {}

local ENTITY_NAME = "multifurnace:fluid_level"
local META_CENTER = "fluid_entity_center"
local META_COUNT = "fluid_entity_count"

local function get_texture(fluid)
local def = fluid and core.registered_nodes[fluid]
local flowing = def and def.liquid_alternative_flowing and
core.registered_nodes[def.liquid_alternative_flowing]
local texture = flowing and flowing.tiles and flowing.tiles[1]

if not texture then texture = def and def.tiles and def.tiles[1] end

if type(texture) == "table" then texture = texture.name end
if type(texture) == "string" then return texture end
end

local function get_entity_textures(fluid, modifier)
local texture = get_texture(fluid) or get_texture(fluidity.external.items.lava)
if modifier then texture = texture .. modifier end

return {texture, texture, texture, texture, texture, texture}
end

local function remove_near(pos)
local objects = core.get_objects_inside_radius(pos, 0.75)
for _, object in pairs(objects) do
local entity = object:get_luaentity()
if entity and entity.name == ENTITY_NAME then object:remove() end
end
end

function multifurnace.fluid_entity.create_box(owner_pos, box_min, box_size, layers)
if not layers or #layers == 0 then
multifurnace.fluid_entity.remove(owner_pos)
return
end

local meta = core.get_meta(owner_pos)
multifurnace.fluid_entity.remove(owner_pos)

local y_offset = 0
local created = 0

for _, layer in ipairs(layers) do
local height = box_size.y * layer.fill_ratio
if height > 0 then
local center = {
x = box_min.x + box_size.x / 2,
y = box_min.y + y_offset + height / 2,
z = box_min.z + box_size.z / 2
}
local object = core.add_entity(center, ENTITY_NAME)

if object then
created = created + 1
object:set_properties({
visual_size = {x = box_size.x, y = height, z = box_size.z},
textures = get_entity_textures(layer.fluid,
layer.texture_modifier)
})
meta:set_string(META_CENTER .. created, core.pos_to_string(center))
end

y_offset = y_offset + height
end
end

meta:set_int(META_COUNT, created)
end

function multifurnace.fluid_entity.remove(controller_pos)
local meta = core.get_meta(controller_pos)
local count = meta:get_int(META_COUNT)
local center = core.string_to_pos(meta:get_string(META_CENTER))

if center then remove_near(center) end

for i = 1, count do
center = core.string_to_pos(meta:get_string(META_CENTER .. i))
if center then remove_near(center) end
meta:set_string(META_CENTER .. i, "")
end

meta:set_string(META_CENTER, "")
meta:set_int(META_COUNT, 0)
end

core.register_entity(ENTITY_NAME, {
initial_properties = {
physical = false,
collide_with_objects = false,
visual = "cube",
visual_size = {x = 1, y = 1, z = 1},
textures = get_entity_textures(fluidity.external.items.lava),
pointable = false,
static_save = false
}
})
5 changes: 5 additions & 0 deletions multifurnace/furnace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ local function set_hot(pos, buf, empty)
meta:set_string("buffer1", new_one:to_string())
meta:set_string("buffer" .. buf, empty and "" or current_one:to_string())
if empty then meta:set_int("buffers", #stacks - 1) end
multifurnace.api.update_fluid_entity(pos)

return true
end
Expand Down Expand Up @@ -68,6 +69,7 @@ local function can_take_liquid(pos, want_mb)

if found and found:is_empty() then
clean_buffer_list(pos)
multifurnace.api.update_fluid_entity(pos)
return "", 0
end

Expand Down Expand Up @@ -100,6 +102,7 @@ local function take_liquid(pos, want_mb)
meta:set_string("buffer1", new_stack:to_string())

if new_count == 0 then clean_buffer_list(pos) end
multifurnace.api.update_fluid_entity(pos)

return fluid, count
end
Expand Down Expand Up @@ -153,6 +156,8 @@ local function put_liquid(pos, liquid)
meta:set_int("buffers", buf)
end

multifurnace.api.update_fluid_entity(pos)

return true
end

Expand Down
1 change: 1 addition & 0 deletions multifurnace/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local modpath = minetest.get_modpath(minetest.get_current_modname())
multifurnace.modpath = modpath

dofile(modpath .. "/api.lua")
dofile(modpath .. "/fluid_entity.lua")

dofile(modpath .. "/faucet.lua")
dofile(modpath .. "/casting_table.lua")
Expand Down