Add Supplier def; generify cache logic

This commit is contained in:
Zenon Seth 2023-11-06 23:42:34 +00:00
parent 991499ed1e
commit 0657728644
19 changed files with 404 additions and 104 deletions

View File

@ -15,8 +15,8 @@ local function get_demander_formspec(pos)
return "formspec_version[4]" ..
"size[10.6,7]" ..
logistica.ui.background..
logistica.ui.push_list_picker(PUSH_LIST_PICKER, 6.5, 0.7, pushPos, selectedList, "Put items in:")..
logistica.ui.on_off_btn(isOn, 9.3, 0.5, ON_OFF_BUTTON)..
logistica.ui.push_list_picker(PUSH_LIST_PICKER, 6.7, 0.7, pushPos, selectedList, "Put items in:")..
logistica.ui.on_off_btn(isOn, 9.3, 0.5, ON_OFF_BUTTON, "Enable")..
"list["..posForm..";filter;0.5,0.5;"..NUM_DEMAND_SLOTS..",1;0]"..
"list[current_player;main;0.5,2;8,4;0]"
end
@ -174,5 +174,5 @@ function logistica.register_demander(simpleName, transferRate)
end
logistica.register_demander("Basic", 1)
logistica.register_demander("Item", 1)
logistica.register_demander("Stack", 99)

View File

@ -122,7 +122,7 @@ local function allow_mass_storage_inv_put(pos, listname, index, stack, player)
copyStack:set_count(1)
local inv = minetest.get_meta(pos):get_inventory()
inv:set_stack("filter", index, copyStack)
logistica.updateStorageCacheFromPosition(pos)
logistica.update_supplier_on_item_added(pos)
return 0
end
return stack:get_count()
@ -131,7 +131,7 @@ end
local function on_mass_storage_inv_move(pos, from_list, from_index, to_list, to_index, count, player)
if minetest.is_protected(pos, player) then return 0 end
return 0
end
local function on_mass_storage_inv_put(pos, listname, index, stack, player)

View File

@ -0,0 +1,169 @@
local NUM_SUPPLY_SLOTS = 8
local PULL_LIST_PICKER = "pull_pick"
local ON_OFF_BUTTON = "on_off_btn"
local FORMSPEC_NAME = "logistica_supplier"
local supplierForms = {}
local function get_supplier_formspec(pos)
local posForm = "nodemeta:"..pos.x..","..pos.y..","..pos.z
local pushPos = logistica.get_supplier_target(pos)
local meta = minetest.get_meta(pos)
local selectedList = meta:get_string("suptarlist")
local isOn = logistica.is_machine_on(pos)
return "formspec_version[4]" ..
"size[10.6,8]" ..
logistica.ui.background..
logistica.ui.pull_list_picker(PULL_LIST_PICKER, 6.7, 0.7, pushPos, selectedList, "Take from:")..
logistica.ui.on_off_btn(isOn, 9.3, 0.5, ON_OFF_BUTTON, "Enable")..
"label[0.6,1.2;Items to Supply (if empty nothing will be taken)]"..
"list["..posForm..";filter;0.5,1.5;"..NUM_SUPPLY_SLOTS..",1;0]"..
"list[current_player;main;0.5,3;8,4;0]"
end
local function show_supplier_formspec(playerName, pos)
local pInfo = {}
pInfo.position = pos
supplierForms[playerName] = pInfo
minetest.show_formspec(playerName, FORMSPEC_NAME, get_supplier_formspec(pos))
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 supplierForms[playerName] then return false end
if fields.quit then
supplierForms[playerName] = nil
elseif fields[ON_OFF_BUTTON] then
local pos = supplierForms[playerName].position
if not pos then return false end
logistica.toggle_machine_on_off(pos)
show_supplier_formspec(player:get_player_name(), pos)
elseif fields[PULL_LIST_PICKER] then
local selected = fields[PULL_LIST_PICKER]
if logistica.is_allowed_pull_list(selected) then
local pos = supplierForms[playerName].position
if not pos then return false end
logistica.set_supplier_target_list(pos, selected)
end
end
return true
end
local function on_supplier_punch(pos, node, puncher, pointed_thing)
local targetPos = logistica.get_supplier_target(pos)
if targetPos and puncher:is_player() and puncher:get_player_control().sneak then
minetest.add_entity(targetPos, "logistica:output_entity")
end
end
local function on_supplier_rightclick(pos, node, clicker, itemstack, pointed_thing)
if not clicker or not clicker:is_player() then return end
show_supplier_formspec(clicker:get_player_name(), pos)
end
local function after_place_supplier(pos, placer, itemstack, numDemandSlots)
local meta = minetest.get_meta(pos)
if placer and placer:is_player() then
meta:set_string("owner", placer:get_player_name())
end
logistica.toggle_machine_on_off(pos)
meta:set_string("suptarlist", "dst")
local inv = meta:get_inventory()
inv:set_size("filter", numDemandSlots)
logistica.on_supplier_change(pos)
end
local function allow_supplier_storage_inv_put(pos, listname, index, stack, player)
if listname ~= "filter" then return 0 end
local inv = minetest.get_meta(pos):get_inventory()
local newStack = ItemStack(stack)
newStack:set_count(1)
inv:set_stack(listname, index, newStack)
logistica.update_supplier_on_item_added(pos)
return 0
end
local function allow_supplier_inv_take(pos, listname, index, stack, player)
if listname ~= "filter" then return 0 end
local inv = minetest.get_meta(pos):get_inventory()
local slotStack = inv:get_stack(listname, index)
slotStack:take_item(stack:get_count())
inv:set_stack(listname, index, slotStack)
return 0
end
local function allow_supplier_inv_move(pos, from_list, from_index, to_list, to_index, count, player)
return 0
end
----------------------------------------------------------------
-- Minetest registration
----------------------------------------------------------------
minetest.register_on_player_receive_fields(on_player_receive_fields)
----------------------------------------------------------------
-- Public Registration API
----------------------------------------------------------------
-- `simpleName` is used for the description and for the name (can contain spaces)
-- `maxTransferRate` indicates how many nodes at a time this supplier can take when asked
function logistica.register_supplier(simpleName, maxTransferRate)
local lname = string.lower(simpleName:gsub(" ", "_"))
local supplier_name = "logistica:supplier_"..lname
logistica.suppliers[supplier_name] = true
local grps = {oddly_breakable_by_hand = 3, cracky = 3 }
grps[logistica.TIER_ALL] = 1
local def = {
description = simpleName.." Supplier",
drawtype = "normal",
tiles = {
"logistica_"..lname.."_supplier_side.png^[transformR270",
"logistica_"..lname.."_supplier_side.png^[transformR90",
"logistica_"..lname.."_supplier_side.png^[transformR180",
"logistica_"..lname.."_supplier_side.png",
"logistica_"..lname.."_supplier_back.png",
"logistica_"..lname.."_supplier_front.png",
},
paramtype = "light",
paramtype2 = "facedir",
is_ground_content = false,
groups = grps,
drop = supplier_name,
sounds = logistica.node_sound_metallic(),
after_place_node = function (pos, placer, itemstack)
after_place_supplier(pos, placer, itemstack, NUM_SUPPLY_SLOTS)
end,
on_punch = on_supplier_punch,
on_rightclick = on_supplier_rightclick,
allow_metadata_inventory_put = allow_supplier_storage_inv_put,
allow_metadata_inventory_take = allow_supplier_inv_take,
allow_metadata_inventory_move = allow_supplier_inv_move,
logistica = {
supplier_transfer_rate = maxTransferRate
}
}
minetest.register_node(supplier_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 }
def_disabled.on_construct = nil
def_disabled.after_desctruct = nil
def_disabled.on_punch = nil
def_disabled.on_rightclick = nil
def_disabled.on_timer = nil
def_disabled.logistica = nil
minetest.register_node(supplier_name.."_disabled", def_disabled)
end
logistica.register_supplier("Item", 1)
logistica.register_supplier("Stack", 99)

View File

@ -1,6 +1,7 @@
local TIMER_DURATION_SHORT = 1.0
local TIMER_DURATION_LONG = 3.0
local META_DEMANDER_LISTNAME = "demtarlist"
local META_SUPPLIER_LISTNAME = "suptarlist"
local TARGET_NODES_REQUIRING_TIMER = {}
TARGET_NODES_REQUIRING_TIMER["default:furnace"] = true
TARGET_NODES_REQUIRING_TIMER["gravelsieve:auto_sieve3"] = true
@ -125,6 +126,18 @@ function logistica.get_demander_target_list(pos)
return meta:get_string(META_DEMANDER_LISTNAME)
end
function logistica.set_supplier_target_list(pos, listName)
local meta = get_meta(pos)
meta:set_string(META_SUPPLIER_LISTNAME, listName)
end
function logistica.get_supplier_target_list(pos)
local meta = get_meta(pos)
return meta:get_string(META_SUPPLIER_LISTNAME)
end
-- function logistica.update_demander_demand(demanderPos)
-- local meta = get_meta(demanderPos)
-- local inventories = get_valid_demander_and_target_inventory(demanderPos)
@ -158,6 +171,16 @@ function logistica.get_demander_target(pos)
z = (pos.z + shift.z)}
end
function logistica.get_supplier_target(pos)
local node = minetest.get_node_or_nil(pos)
if not node then return nil end
local shift = logistica.get_rot_directions(node.param2).backward
if not shift then return nil end
return {x = (pos.x + shift.x),
y = (pos.y + shift.y),
z = (pos.z + shift.z)}
end
-- returns how many items remain from the itemstack after we attempt to insert it
-- `targetInventory` and `targetList` are optional (tied together), if not passed, it will be looked up
function logistica.insert_itemstack_for_demander(demanderPos, itemstack)

View File

@ -2,6 +2,7 @@ local path = logistica.MODPATH .. "/logic"
-- once again, order is important
dofile(path .. "/processing_queue.lua")
dofile(path .. "/groups.lua")
dofile(path .. "/network_cache.lua")
dofile(path .. "/network_logic.lua")
dofile(path .. "/network_storage.lua")
dofile(path .. "/controller.lua")

97
logic/network_cache.lua Normal file
View File

@ -0,0 +1,97 @@
local CACHE_PICKER_MASS_STORAGE = {
listName = "filter",
clear = function (network) network.storage_cache = {} end,
cache = function (network) return network.storage_cache end,
nodes = function (network) return network.mass_storage end,
}
local CACHE_PICKER_SUPPLIER = {
listName = "filter",
clear = function (network) network.supplier_cache = {} end,
cache = function (network) return network.supplier_cache end,
nodes = function (network) return network.suppliers end,
}
--[[ Completely updates the storage cache which holds where items may be found
The cache is in the followiing format:
network.storage_cache = {
itemName = {
storagePositionHash1 = true,
storagePositionHash2 = true,
}
}
]]
local function update_network_cache(network, cacheOps)
cacheOps.clear(network)
local nodes = cacheOps.nodes(network)
local cache = cacheOps.cache(network)
local listName = cacheOps.listName
for hash, _ in pairs(nodes) do
local storagePos = minetest.get_position_from_hash(hash)
logistica.load_position(storagePos)
local filterList = minetest.get_meta(storagePos):get_inventory():get_list(listName) or {}
for _, itemStack in pairs(filterList) do
local name = itemStack:get_name()
if not cache[name] then cache[name] = {} end
cache[name][hash] = true
end
end
end
-- calls updateStorageCache(network) if the current position belongs to a network
-- `pos` the position for which to try and get the network and update
-- `cacheOps` = one of the predefined `logistica.CACHE_PICKER_XXXXX` consts
local function update_network_cache_for_pos(pos, cacheOps)
local network = logistica.get_network_or_nil(pos)
if network then
update_network_cache(network, cacheOps)
end
end
local function update_cache_on_item_added(pos, network, cacheOps)
local nodes = cacheOps.nodes(network)
local cache = cacheOps.cache(network)
local listName = cacheOps.listName
logistica.load_position(pos)
local filterList = minetest.get_meta(pos):get_inventory():get_list(listName) or {}
for _, itemStack in pairs(filterList) do
local name = itemStack:get_name()
if not cache[name] then cache[name] = {} end
cache[name][minetest.hash_node_position(pos)] = true
end
end
local function update_cache_on_item_added_at_pos(pos, cacheOps)
local network = logistica.get_network_or_nil(pos)
if network then
update_cache_on_item_added(pos, network, cacheOps)
end
end
--------------------------------
-- public functions
--------------------------------
function logistica.update_mass_storage_cache_pos(pos)
update_network_cache_for_pos(pos, CACHE_PICKER_MASS_STORAGE)
end
function logistica.update_mass_storage_cache(network)
update_network_cache(network, CACHE_PICKER_MASS_STORAGE)
end
function logistica.update_mass_storage_on_item_added(pos)
update_cache_on_item_added_at_pos(pos, CACHE_PICKER_MASS_STORAGE)
end
function logistica.update_supplier_cache_pos(pos)
update_network_cache_for_pos(pos, CACHE_PICKER_SUPPLIER)
end
function logistica.update_supplier_cache(network)
update_network_cache(network, CACHE_PICKER_SUPPLIER)
end
function logistica.update_supplier_on_item_added(pos)
update_cache_on_item_added_at_pos(pos, CACHE_PICKER_SUPPLIER)
end

View File

@ -81,14 +81,6 @@ local function break_logistica_node(pos)
logistica.swap_node(pos, node.name .. "_disabled")
end
-- calls updateStorageCache(network) if the current position belongs to a network
function logistica.updateStorageCacheFromPosition(pos)
local network = logistica.get_network_or_nil(pos)
if network then
logistica.updateStorageCache(network)
end
end
-- returns a naturally numbered list of networks on adjecent nodes
local function find_adjecent_networks(pos)
local connectedNetworks = {}
@ -106,38 +98,13 @@ local function find_adjecent_networks(pos)
return retNetworks
end
--[[ updates the storage cach which holds where items may be found
The cache is in the followiing format:
network.storage_cache = {
itemName = {
storagePositionHash1 = true,
storagePositionHash2 = true,
}
}
]]
function logistica.updateStorageCache(network)
-- this maybe can be improved by doing add/remove operations, but increases complexity
-- for now, do full rescan
network.storage_cache = {}
for storageHash, _ in pairs(network.mass_storage) do
local storagePos = h2p(storageHash)
logistica.load_position(storagePos)
local filterList = minetest.get_meta(storagePos):get_inventory():get_list("filter") or {}
for _, itemStack in pairs(filterList) do
local name = itemStack:get_name()
if not network.storage_cache[name] then network.storage_cache[name] = {} end
network.storage_cache[name][storageHash] = true
end
end
end
local function recursive_scan_for_nodes_for_controller(network, positionHashes, numScanned)
if not numScanned then numScanned = 0 end
if numScanned > HARD_NETWORK_NODE_LIMIT then
return CREATE_NETWORK_STATUS_TOO_MANY_NODES
end
local connections = {}
local newToScan = 0
for posHash, _ in pairs(positionHashes) do
@ -215,6 +182,7 @@ local function create_network(controllerPosition, oldNetworkName)
network.mass_storage = {}
network.item_storage = {}
network.storage_cache = {}
network.supplier_cache = {}
local startPos = {}
startPos[controllerHash] = true
local status = recursive_scan_for_nodes_for_controller(network, startPos)
@ -226,7 +194,8 @@ local function create_network(controllerPosition, oldNetworkName)
errorMsg = "Controller max nodes limit of "..HARD_NETWORK_NODE_LIMIT.." nodes per network exceeded!"
elseif status == STATUS_OK then
-- controller scan skips updating storage cache, do so now
logistica.updateStorageCache(network)
logistica.update_mass_storage_cache(network)
logistica.update_supplier_cache(network)
end
if errorMsg ~= nil then
networks[controllerHash] = nil
@ -275,7 +244,7 @@ local function try_to_add_network(pos)
create_network(pos)
end
local function try_to_add_mass_storage_to_network(pos)
local function try_to_add_to_network(pos, ops)
local connectedNetworks = find_adjecent_networks(pos)
if #connectedNetworks <= 0 then return STATUS_OK end -- nothing to connect to
if #connectedNetworks >= 2 then
@ -283,36 +252,55 @@ local function try_to_add_mass_storage_to_network(pos)
minetest.get_meta(pos):set_string("infotext", "ERROR: cannot connect to multiple networks!")
end
-- else, we have 1 network, add us to it!
connectedNetworks[1].mass_storage[p2h(pos)] = true
logistica.updateStorageCache(connectedNetworks[1])
ops.get_list(connectedNetworks[1])[p2h(pos)] = true
ops.update_cache_node_added(connectedNetworks[1])
end
local function remove_from_network(pos, ops)
local hash = p2h(pos)
local network = logistica.get_network_or_nil(pos)
if not network then return end
ops.get_list(network)[hash] = nil
ops.update_cache_node_removed(network)
end
local function MASS_STORAGE_OPS(pos) return {
get_list = function(network) return network.mass_storage end,
update_cache_node_added = function(_) logistica.update_mass_storage_on_item_added(pos) end,
update_cache_node_removed = function(network) logistica.update_mass_storage_cache(network) end,
} end
local function try_to_add_mass_storage_to_network(pos)
try_to_add_to_network(pos, MASS_STORAGE_OPS(pos))
end
local function remove_mass_storage_from_network(pos)
local hash = p2h(pos)
local network = logistica.get_network_or_nil(pos)
if not network then return end
if not network.mass_storage[hash] then return end
network.mass_storage[hash] = nil
remove_from_network(pos, MASS_STORAGE_OPS(pos))
end
local DEMANDER_OPS = {
get_list = function(network) return network.demanders end,
update_cache_node_added = function(_) end,
update_cache_node_removed = function(_) end,
}
local function try_to_add_demander_to_network(pos)
local connectedNetworks = find_adjecent_networks(pos)
if #connectedNetworks <= 0 then return STATUS_OK end -- nothing to connect to
if #connectedNetworks >= 2 then
break_logistica_node(pos) -- swap out storage node for disabled one
minetest.get_meta(pos):set_string("infotext", "ERROR: cannot connect to multiple networks!")
end
-- else, we have 1 network, add us to it!
connectedNetworks[1].demanders[p2h(pos)] = true
try_to_add_to_network(pos,DEMANDER_OPS)
end
local function remove_demander_from_network(pos)
remove_from_network(pos, DEMANDER_OPS)
end
local function remove_demander_from_network(pos)
local hash = p2h(pos)
local network = logistica.get_network_or_nil(pos)
if not network then return end
if not network.mass_storage[hash] then return end
network.mass_storage[hash] = nil
local function SUPPLIER_OPS(pos) return {
get_list = function(network) return network.suppliers end,
update_cache_node_added = function(_) logistica.update_supplier_on_item_added(pos) end,
update_cache_node_removed = function(network) logistica.update_supplier_cache(network) end,
} end
local function try_to_add_supplier_to_network(pos)
try_to_add_to_network(pos, SUPPLIER_OPS(pos))
end
local function remove_supplier_from_network(pos)
remove_from_network(pos, SUPPLIER_OPS(pos))
end
----------------------------------------------------------------
-- global namespaced functions
----------------------------------------------------------------
@ -383,7 +371,6 @@ function logistica.on_storage_change(pos, oldNode)
end
end
function logistica.on_demander_change(pos, oldNode)
local placed = (oldNode == nil) -- if oldNode is nil, we placed a new one
if placed == true then
@ -391,4 +378,13 @@ function logistica.on_demander_change(pos, oldNode)
else
remove_demander_from_network(pos)
end
end
end
function logistica.on_supplier_change(pos, oldNode)
local placed = (oldNode == nil) -- if oldNode is nil, we placed a new one
if placed == true then
try_to_add_supplier_to_network(pos)
else
remove_supplier_from_network(pos)
end
end

View File

@ -74,4 +74,39 @@ function logistica.take_stack_from_mass_storage(stackToTake, network, collectorF
storageInv:set_list(MASS_STORAGE_LIST_NAME, storageList)
end
return false
end
-- attempts to insert the given itemstack in the network, returns how many items were inserted
function logistica.insert_item_in_network(itemstack, networkId)
local network = logistica.networks[networkId]
if not itemstack or not network then return 0 end
local workingStack = ItemStack(itemstack)
-- check demanders first
for hash, _ in pairs(network.demanders) do
local pos = minetest.get_position_from_hash(hash)
logistica.load_position(pos)
local taken = 0 -- logistica.try_to_give_item_to_demander(pos, workingStack)
local leftover = workingStack:get_count() - taken
if leftover <= 0 then return itemstack:get_count() end -- we took all items
workingStack:set_count(leftover)
end
-- check storages
local storages = {}
if itemstack:get_stack_max() <= 1 then
storages = network.item_storage
else
storages = network.mass_storage
end
for hash, _ in pairs(storages) do
local pos = minetest.get_position_from_hash(hash)
logistica.load_position(pos)
local taken = logistica.try_to_add_item_to_storage(pos, workingStack)
local leftover = workingStack:get_count() - taken
if leftover <= 0 then return itemstack:get_count() end -- we took all items
workingStack:set_count(leftover)
end
return itemstack:get_count() - workingStack:get_count()
end

View File

@ -7,7 +7,6 @@ function ProQ.new()
return self
end
-- the : syntax here causes a "self" arg to be implicitly added before any other args
function ProQ:add_pos(newval)
self.value = newval
end
@ -15,6 +14,3 @@ end
function ProQ:get_value()
return self.value
end
local instance = ProQ.new()
-- do stuff with instance...

View File

@ -1,38 +1,21 @@
local p2h = minetest.hash_node_position
local h2p = minetest.get_position_from_hash
-- attempts to insert the given itemstack in the network, returns how many items were inserted
function logistica.insert_item_in_network(itemstack, networkId)
local network = logistica.networks[networkId]
if not itemstack or not network then return 0 end
local workingStack = ItemStack(itemstack)
-- check demanders first
for hash, _ in pairs(network.demanders) do
local pos = h2p(hash)
logistica.load_position(pos)
local taken = 0 -- logistica.try_to_give_item_to_demander(pos, workingStack)
local leftover = workingStack:get_count() - taken
if leftover <= 0 then return itemstack:get_count() end -- we took all items
workingStack:set_count(leftover)
end
-- check storages
local storages = {}
if itemstack:get_stack_max() <= 1 then
storages = network.item_storage
else
storages = network.mass_storage
end
for hash, _ in pairs(storages) do
local pos = h2p(hash)
logistica.load_position(pos)
local taken = logistica.try_to_add_item_to_storage(pos, workingStack)
local leftover = workingStack:get_count() - taken
if leftover <= 0 then return itemstack:get_count() end -- we took all items
workingStack:set_count(leftover)
end
return itemstack:get_count() - workingStack:get_count()
function logistica.get_supplier_target(pos)
local node = minetest.get_node_or_nil(pos)
if not node then return nil end
local shift = logistica.get_rot_directions(node.param2).backward
if not shift then return nil end
return {x = (pos.x + shift.x),
y = (pos.y + shift.y),
z = (pos.z + shift.z)}
end
function logistica.get_supplier_max_item_transfer(pos)
local node = minetest.get_node_or_nil(pos)
if not node then return 0 end
local def = minetest.registered_nodes[node]
if def and def.logistica and def.logistica.supplier_transfer_rate then
return def.logistica.supplier_transfer_rate
else
return 0
end
end

View File

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB