Add bucket filler machine

This commit is contained in:
Zenon Seth 2024-03-14 18:31:03 +00:00
parent 9fc02be2a0
commit e52b3cb43d
14 changed files with 399 additions and 19 deletions

View File

@ -19,3 +19,4 @@ dofile(path.."/cobble_supplier.lua")
dofile(path.."/synchronizer.lua")
dofile(path.."/reservoir.lua")
dofile(path.."/lava_furnace_fueler.lua")
dofile(path.."/bucket_filler.lua")

View File

@ -0,0 +1,180 @@
local S = logistica.TRANSLATOR
local FORMSPEC_NAME = "logistica_bktfil"
local INV_MAIN = "main"
local INV_INPUT = "input"
local BTN_NEXT_LIQUID = "btn_next"
local BTN_PREV_LIQUID = "btn_prev"
local TOOLTIP_BUCKET_INPUT = S("If no Empty Buckets are added here,\nthen they will be taken from the network.")
local forms = {}
local function get_filler_formspec(pos, errorMsg)
if not errorMsg then errorMsg = "" else errorMsg = S("Error: ")..errorMsg end
local posForm = "nodemeta:"..pos.x..","..pos.y..","..pos.z
local currLiquid = logistica.filler_get_current_selected_bucket(pos)
local liquidInfo = logistica.get_liquid_info_in_network(pos, currLiquid.liquidName)
local liquidText = ItemStack(currLiquid.bucketName):get_short_description()
return "formspec_version[4]"..
"size["..logistica.inv_size(10.5, 9).."]"..
logistica.ui.background..
"list["..posForm..";"..INV_MAIN..";6.7,1.2;1,1;0]"..
"list["..posForm..";"..INV_INPUT..";0.4,1.2;4,1;0]"..
"label[0.4,0.4;"..S("Supplies the selected filled buckets on-demand, if empty buckets are available").."]"..
"label[0.4,1.0;"..S("Optional Empty Buckets").." \\[?\\]]"..
"button[5.8,1.3;0.8,0.8;"..BTN_PREV_LIQUID..";<]"..
"button[7.8,1.3;0.8,0.8;"..BTN_NEXT_LIQUID..";>]"..
"label[0.4,3.0;"..errorMsg.."]"..
"label[5.7,1.0;"..S("Supply")..": "..liquidText.."]"..
"label[5.7,2.5;"..S("Amount Available")..": "..liquidInfo.curr.." ]"..
"tooltip[0.2,0.4;3.5,1.0;"..TOOLTIP_BUCKET_INPUT.."]"..
"listring[current_player;main]"..
"listring["..posForm..";"..INV_INPUT.."]"..
"listring[current_player;main]"..
logistica.player_inv_formspec(0.4,4)
end
local function show_filler_formspec(playerName, pos)
if not forms[playerName] then forms[playerName] = {position = pos} end
minetest.show_formspec(playerName, FORMSPEC_NAME, get_filler_formspec(pos, forms[playerName].errorMsg))
end
local function on_player_receive_fields(player, formname, fields)
if not player or not player:is_player() then return false end
if formname ~= FORMSPEC_NAME then return false end
local playerName = player:get_player_name()
if not forms[playerName] then return false end
local pos = forms[playerName].position
if minetest.is_protected(pos, playerName) then return true end
if fields.quit then
forms[playerName] = nil
elseif fields[BTN_PREV_LIQUID] then
logistica.filler_change_selected_bucket(pos, -1)
show_filler_formspec(playerName, pos)
elseif fields[BTN_NEXT_LIQUID] then
logistica.filler_change_selected_bucket(pos, 1)
show_filler_formspec(playerName, pos)
end
return true
end
local function on_filler_rightclick(pos, node, clicker, itemstack, pointed_thing)
if not clicker or not clicker:is_player() then return end
if minetest.is_protected(pos, clicker:get_player_name()) then return end
show_filler_formspec(clicker:get_player_name(), pos)
end
local function after_place_filler(pos, placer, itemstack)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_size(INV_MAIN, 1)
inv:set_size(INV_INPUT, 4)
logistica.set_node_tooltip_from_state(pos)
logistica.filler_change_selected_bucket(pos, 1000000) -- makes sure the 1st liquid is selected
end
local function allow_filler_inv_put(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then return 0 end
if listname == INV_MAIN then return 0 end
return stack:get_count()
end
local function allow_filler_inv_take(pos, listname, index, stack, player)
local playerName = player:get_player_name()
if minetest.is_protected(pos, playerName) then return 0 end
if listname == INV_MAIN then
local numTaken = 0
local takeFunc = function(takenStack) numTaken = takenStack:get_count() ; return 0 end
local result = logistica.take_item_from_bucket_filler(pos, stack, logistica.get_network_or_nil(pos), takeFunc, false, false)
if forms[playerName] then
forms[playerName].errorMsg = result.errorMsg
end
if numTaken == 0 then show_filler_formspec(player:get_player_name(), pos) end
return numTaken
end
return stack:get_count()
end
local function allow_filler_inv_move(pos, from_list, from_index, to_list, to_index, count, player)
return 0
end
local function on_filler_inv_take(pos, listname, index, stack, player)
if not player:is_player() then return end
if listname == INV_MAIN then
local inv = minetest.get_meta(pos):get_inventory()
inv:add_item(listname, stack) -- the bucket is an indicator, it needs to remain there
show_filler_formspec(player:get_player_name(), pos)
end
end
local function can_dig_filler(pos, player)
return minetest.get_meta(pos):get_inventory():is_empty(INV_INPUT)
end
----------------------------------------------------------------
-- Minetest registration
----------------------------------------------------------------
minetest.register_on_player_receive_fields(on_player_receive_fields)
minetest.register_on_leaveplayer(function(objRef, timed_out)
if objRef:is_player() then
forms[objRef:get_player_name()] = nil
end
end)
----------------------------------------------------------------
-- Public Registration API
----------------------------------------------------------------
-- `simpleName` is used for the description and for the name (can contain spaces)
function logistica.register_bucket_filler(desc, name, tiles)
local lname = string.lower(name:gsub(" ", "_"))
local filler_name = "logistica:"..lname
logistica.bucketfillers[filler_name] = true
local grps = {oddly_breakable_by_hand = 3, cracky = 3, handy = 1, pickaxey = 1, }
grps[logistica.TIER_ALL] = 1
local def = {
description = desc,
drawtype = "normal",
tiles = tiles,
paramtype = "light",
paramtype2 = "facedir",
is_ground_content = false,
groups = grps,
drop = filler_name,
sounds = logistica.node_sound_metallic(),
after_place_node = after_place_filler,
after_dig_node = logistica.on_supplier_change,
on_rightclick = on_filler_rightclick,
allow_metadata_inventory_put = allow_filler_inv_put,
allow_metadata_inventory_take = allow_filler_inv_take,
allow_metadata_inventory_move = allow_filler_inv_move,
on_metadata_inventory_take = on_filler_inv_take,
can_dig = can_dig_filler,
logistica = {},
_mcl_hardness = 1.5,
_mcl_blast_resistance = 10
}
minetest.register_node(filler_name, def)
local def_disabled = table.copy(def)
local tiles_disabled = {}
for k, v in pairs(def.tiles) do tiles_disabled[k] = v.."^logistica_disabled.png" end
def_disabled.tiles = tiles_disabled
def_disabled.groups = { oddly_breakable_by_hand = 3, cracky = 3, choppy = 3, not_in_creative_inventory = 1, pickaxey = 1, axey = 1, handy = 1 }
def_disabled.on_construct = nil
def_disabled.after_dig_node = nil
def_disabled.on_punch = nil
def_disabled.on_rightclick = nil
def_disabled.on_timer = nil
def_disabled.logistica = nil
minetest.register_node(filler_name.."_disabled", def_disabled)
end

127
logic/bucket_filler.lua Normal file
View File

@ -0,0 +1,127 @@
local S = logistica.TRANSLATOR
local META_CURRENT_BUCKET_INDEX = "filcur"
local INV_MAIN = "main"
local INV_INPUT = "input"
local buckets_to_names = nil
local function get_all_buckets_to_names_list()
if not buckets_to_names then
buckets_to_names = logistica.table_to_list_indexed(
logistica.reservoir_get_all_buckets_to_names_map(),
function (bucketName, liquidName)
return { bucketName = bucketName, liquidName = liquidName }
end
)
end
return buckets_to_names
end
-- Returns a a table of {bucketName = "bucket:name", liquidName = "Name of liquid"}
function logistica.filler_get_current_selected_bucket(pos)
local meta = minetest.get_meta(pos)
local index = meta:get_int(META_CURRENT_BUCKET_INDEX)
local allBuckets = get_all_buckets_to_names_list()
local currBucket = allBuckets[index]
if not currBucket then currBucket = allBuckets[0] or {} end
return {
bucketName = currBucket.bucketName or "",
liquidName = currBucket.liquidName or "",
}
end
function logistica.filler_change_selected_bucket(pos, change)
local allBucketsToNames = get_all_buckets_to_names_list()
local maxSize = #allBucketsToNames
local meta = minetest.get_meta(pos)
local index = meta:get_int(META_CURRENT_BUCKET_INDEX)
index = index + change
if index < 1 then index = maxSize
elseif index > maxSize then index = 1 end
meta:set_int(META_CURRENT_BUCKET_INDEX, index)
-- set the main inventory's list
local inv = meta:get_inventory()
local mainList = {}
if allBucketsToNames[index] then
mainList[1] = ItemStack(allBucketsToNames[index].bucketName)
end
inv:set_list(INV_MAIN, mainList)
logistica.on_supplier_change(pos) -- notify we got new item (well probably)
end
-- return a table of {remaining = int, erroMsg = ""}, indicating how many items remain to be fulfilled, and an optional error msg if any
function logistica.take_item_from_bucket_filler(pos, stackToTake, network, collectorFunc, isAutomatedRequest, dryRun, depth)
if stackToTake:get_count() <= 0 then return { remaining = 0, errorMsg = S("Can't take a stack of size 0") } end
if not network then return { remaining = stackToTake:get_count(), errorMsg = S("No network") } end -- filling happens from network reservoirs, so need a network
if not depth then depth = 1 end
local originalRequestedBuckets = stackToTake:get_count()
local stackToTakeName = stackToTake:get_name()
local liquidName = logistica.reservoir_get_liquid_name_for_bucket(stackToTakeName)
if not liquidName then return { remaining = stackToTake:get_count(), errorMsg = S("Unknown liquid: ")..liquidName } end
local liquidInfo = logistica.get_liquid_info_in_network(pos, liquidName)
local remainingRequest = math.min(liquidInfo.curr, originalRequestedBuckets)
local unfillableBuckets = originalRequestedBuckets - remainingRequest
local filledBucketName = logistica.reservoir_get_full_bucket_for_liquid(liquidName)
local emptyBucketName = logistica.reservoir_get_empty_bucket_for_liquid(liquidName)
-- first try to get the empty bucket from our internal inventory
local inv = minetest.get_meta(pos):get_inventory()
local collectedInternal = remainingRequest
local tookFromInternal = false
while collectedInternal > 0 and not tookFromInternal do -- try to take max, then 1 less, etc, until we take it, or we hit 0
local needed = ItemStack(emptyBucketName); needed:set_count(collectedInternal)
if inv:contains_item(INV_INPUT, needed) then
tookFromInternal = true
else
collectedInternal = collectedInternal - 1
end
end
-- remaining decreased by how many we actually managed to take from internal inv
remainingRequest = remainingRequest - collectedInternal
-- then if necessary, try to take remaining empty buckets from network
local collectedFromNetwork = 0
if remainingRequest > 0 then
local collectEmpty = function(stackToInsert) collectedFromNetwork = stackToInsert:get_count() ; return 0 end
local stackToTakeFromNetwork = ItemStack(emptyBucketName) ; stackToTakeFromNetwork:set_count(remainingRequest)
logistica.take_stack_from_network(stackToTakeFromNetwork, network, collectEmpty, isAutomatedRequest, false, true, depth + 1)
remainingRequest = remainingRequest - collectedFromNetwork
end
local numEmptyBucketsAvailable = collectedInternal + collectedFromNetwork
local emptyBucketStack = ItemStack(emptyBucketName)
for _ = 1, numEmptyBucketsAvailable, 1 do
logistica.fill_bucket_from_network(network, emptyBucketStack, liquidName)
end
local toGive = ItemStack(filledBucketName) ; toGive:set_count(numEmptyBucketsAvailable)
local leftover = collectorFunc(toGive)
if not dryRun then -- actually remove empty buckets from storages
local numAccepted = numEmptyBucketsAvailable - leftover
local actuallyTakeFromInternal = math.min(collectedInternal, numAccepted)
if actuallyTakeFromInternal > 0 then
local stackRemInternal = ItemStack(emptyBucketName) ; stackRemInternal:set_count(actuallyTakeFromInternal)
inv:remove_item(INV_INPUT, stackRemInternal)
end
local actuallyTakeFromNetwork = math.min(collectedFromNetwork, numAccepted - actuallyTakeFromInternal)
local stackToTakeFromNetwork = ItemStack(emptyBucketName) ; stackToTakeFromNetwork:set_count(actuallyTakeFromNetwork)
logistica.take_stack_from_network(stackToTakeFromNetwork, network, function(_) return 0 end, isAutomatedRequest, false, false, depth + 1)
end
local error = nil
if numEmptyBucketsAvailable < originalRequestedBuckets - unfillableBuckets then
error = S("Not enough empty buckets available")
elseif unfillableBuckets > 0 then
error = S("Not enough liquid available in network")
end
return { remaining = unfillableBuckets + remainingRequest, errorMsg = error }
end

View File

@ -4,6 +4,7 @@ logistica.injectors = {}
logistica.requesters = {}
logistica.suppliers = {}
logistica.craftsups = {}
logistica.bucketfillers = {}
logistica.mass_storage = {}
logistica.item_storage = {}
logistica.misc_machines = {}
@ -21,7 +22,7 @@ function logistica.is_machine(name)
return logistica.is_requester(name) or logistica.is_supplier(name) or logistica.is_mass_storage(name)
or logistica.is_item_storage(name) or logistica.is_controller(name) or logistica.is_injector(name)
or logistica.is_crafting_supplier(name) or logistica.is_trashcan(name) or logistica.is_vaccuum_supplier(name)
or logistica.is_misc(name) or logistica.is_reservoir(name)
or logistica.is_misc(name) or logistica.is_reservoir(name) or logistica.is_bucket_filler(name)
end
function logistica.is_cable(name)
@ -71,3 +72,7 @@ end
function logistica.is_reservoir(name)
return logistica.reservoirs[name] ~= nil
end
function logistica.is_bucket_filler(name)
return logistica.bucketfillers[name] ~= nil
end

View File

@ -20,3 +20,4 @@ dofile(path.."/crafting_supplier.lua")
dofile(path.."/synchronizer.lua")
dofile(path.."/reservoir.lua")
dofile(path.."/lava_furnace_fueler.lua")
dofile(path.."/bucket_filler.lua")

View File

@ -185,7 +185,9 @@ local function recursive_scan_for_nodes_for_controller(network, positionHashes,
end
if logistica.is_supplier(otherName)
or logistica.is_crafting_supplier(otherName)
or logistica.is_vaccuum_supplier(otherName) then
or logistica.is_vaccuum_supplier(otherName)
or logistica.is_bucket_filler(otherName)
then
network.suppliers[otherHash] = true
valid = true
end

View File

@ -10,7 +10,12 @@ local function get_meta(pos)
return minetest.get_meta(pos)
end
local function fill_bucket_from_network(network, bucketItemStack, liquidName)
--------------------------------
-- public functions
--------------------------------
-- returns the new stack to replace the empty bucket given, or nil if not successful
function logistica.fill_bucket_from_network(network, bucketItemStack, liquidName)
local lowestReservoirPos = nil
local lowestReservoirLvl = 999999
for hash, _ in pairs(network.reservoirs or {}) do
@ -32,7 +37,8 @@ local function fill_bucket_from_network(network, bucketItemStack, liquidName)
end
end
local function empty_bucket_into_network(network, bucketItemStack)
-- returns the new stack to replace the filled bucket given, or nil if not successful
function logistica.empty_bucket_into_network(network, bucketItemStack)
local bucketName = bucketItemStack:get_name()
local liquidName = logistica.reservoir_get_liquid_name_for_bucket(bucketName)
@ -68,10 +74,6 @@ local function empty_bucket_into_network(network, bucketItemStack)
end
end
--------------------------------
-- public functions
--------------------------------
-- tries to take a stack from the network locations
-- calls the collectorFunc with the stack - collectorFunc needs to return how many were left-over<br>
-- `collectorFunc = function(stackToInsert)`<br>
@ -111,6 +113,9 @@ function logistica.take_stack_from_suppliers(stackToTake, network, collectorFunc
remaining = logistica.take_item_from_supplier(pos, takeStack, network, collectorFunc, useMetadata, dryRun)
elseif logistica.is_crafting_supplier(nodeName) then
remaining = logistica.take_item_from_crafting_supplier(pos, takeStack, network, collectorFunc, useMetadata, dryRun, depth)
elseif logistica.is_bucket_filler(nodeName) then
local result = logistica.take_item_from_bucket_filler(pos, takeStack, network, collectorFunc, isAutomatedRequest, dryRun, depth)
remaining = result.remaining
end
if remaining <= 0 then
return true
@ -286,8 +291,8 @@ function logistica.get_available_liquids_in_network(pos)
local liquidLevels = logistica.reservoir_get_liquid_level(resPos)
if liquidName and liquidLevels then
local info = liquidInfo[liquidName] or {curr = 0, max = 0}
info.curr = info.curr + liquidLevels[1]
info.max = info.max + liquidLevels[2]
info.curr = info.curr + (liquidLevels[1] or 0)
info.max = info.max + (liquidLevels[2] or 0)
liquidInfo[liquidName] = info
end
end
@ -301,6 +306,27 @@ function logistica.get_available_liquids_in_network(pos)
end)
end
-- Returns a table for the given liquidName {curr = int, max = int}
function logistica.get_liquid_info_in_network(pos, liquidName)
local network = logistica.get_network_or_nil(pos)
if not network then return { curr = 0, max = 0 } end
local available = 0
local capacity = 0
for hash, _ in pairs(network.reservoirs or {}) do
local resPos = h2p(hash)
local resLiquid = logistica.reservoir_get_liquid_name(resPos)
local liquidLevels = logistica.reservoir_get_liquid_level(resPos)
if resLiquid == liquidName and liquidLevels then
available = available + (liquidLevels[1] or 0)
capacity = capacity + (liquidLevels[2] or 0)
end
end
return {
curr = available,
max = capacity,
}
end
-- attempts to use, either fill or empty, the given bucket in/from liquid storage on
-- the network.<br>
-- `liquidName` is only used if the bucketItem is a type of empty bucket<br>
@ -316,8 +342,8 @@ function logistica.use_bucket_for_liquid_in_network(pos, bucketItemStack, liquid
local isFullBucket = logistica.reservoir_is_full_bucket(bucketName)
if isEmptyBucket then
if not liquidName then return nil end
return fill_bucket_from_network(network, bucketItemStack, liquidName)
return logistica.fill_bucket_from_network(network, bucketItemStack, liquidName)
elseif isFullBucket then
return empty_bucket_into_network(network, bucketItemStack)
return logistica.empty_bucket_into_network(network, bucketItemStack)
end
end

View File

@ -14,7 +14,6 @@ local LIQUID_NONE = ""
local BUCKET_ANY = "BUCKET_ANY"
local strDescription = S("Reservoir")
local strEmpty = S("Empty")
local getStrContains = function(number, max, ofWhat)
if number == 0 then
return S("@1 / @2 buckets", number, max)
@ -194,3 +193,15 @@ end
function logistica.reservoir_get_liquid_name_for_bucket(bucketName)
return BUCKET_TO_NAME[bucketName]
end
function logistica.reservoir_get_all_buckets_to_names_map()
return table.copy(BUCKET_TO_NAME)
end
function logistica.reservoir_get_empty_bucket_for_liquid(liquidName)
return get_empty_bucket_needed_for(liquidName)
end
function logistica.reservoir_get_full_bucket_for_liquid(liquidName)
return get_full_bucket_needed_for(liquidName)
end

View File

@ -21,6 +21,16 @@ logistica.register_autocrafter("Autocrafter", "autocrafter", {
"logistica_autocrafter.png"
})
--------------------------------
-- Bucket Filler
--------------------------------
logistica.register_bucket_filler("Bucket Filler", "bucket_filler", {
"logistica_bucket_filler_top.png",
"logistica_bucket_filler_top.png",
"logistica_bucket_filler_side.png",
})
--------------------------------
-- Cables
--------------------------------

View File

@ -191,3 +191,12 @@ minetest.register_craft({
{L("silverin_plate"), L("silverin_plate"), L("silverin_plate")}
}
})
minetest.register_craft({
output = L("bucket_filler"),
recipe = {
{L("silverin_plate"), "", L("silverin_plate")},
{L("optic_cable"), L("photonizer"), ""},
{L("silverin_plate"), itemstrings.empty_bucket, L("silverin_plate")},
}
})

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

View File

@ -114,8 +114,8 @@ function logistica.set_node_tooltip_from_state(pos, extraText, overrideState)
logistica.load_position(pos)
local meta = minetest.get_meta(pos)
local node = minetest.get_node(pos)
local text = minetest.registered_nodes[node.name].description..
extraText.."\n"..(isOn and "Running" or "Stopped")
local isOnText = (minetest.registered_nodes[node.name].logistica.on_power and (isOn and "Running" or "Stopped")) or ""
local text = minetest.registered_nodes[node.name].description..extraText.."\n"..isOnText
meta:set_string("infotext", text)
end

View File

@ -4,11 +4,19 @@ local playerHud = {}
local SHORT_TIME = 3
local LONG_TIME = 10
function logistica.show_popup(playerName, text, time)
function logistica.show_popup(playerName, text, time, atBottom, scale)
if not time then time = SHORT_TIME end
local player = minetest.get_player_by_name(playerName)
if not player then return end
local position = {x = 0.5, y = 0.5}
local offset = {x = 0, y = 40}
if atBottom then
position = {x = 0.5, y = 1}
offset = {x = 0, y = -100}
end
if not scale then scale = 1 end
if playerHud[playerName] then
player:hud_remove(playerHud[playerName].hudId)
playerHud[playerName].job:cancel()
@ -17,10 +25,10 @@ function logistica.show_popup(playerName, text, time)
local hudId = player:hud_add({
hud_elem_type = "text",
style = 1,
position = {x = 0.5, y = 0.5},
offset = {x = 0, y = 40},
position = position,
offset = offset,
text = text,
size = { x = 1, y = 1},
size = { x = scale, y = scale},
scale = { x = 1, y = 1},
alignment = { x = 0, y = 0 },
number = 0xDFDFDF,