Renaming in prep for update
@ -4,5 +4,5 @@ dofile(path.."/cables.lua")
|
||||
dofile(path.."/controller.lua")
|
||||
dofile(path.."/mass_storage.lua")
|
||||
dofile(path.."/supplier.lua")
|
||||
dofile(path.."/demander.lua")
|
||||
dofile(path.."/requester.lua")
|
||||
dofile(path.."/injector.lua")
|
@ -47,14 +47,6 @@ minetest.register_on_player_receive_fields(on_controller_receive_fields)
|
||||
- on_timer
|
||||
- on_rightclick
|
||||
|
||||
The definition must also provide a `logistica_controller` table. This table should contains:
|
||||
{
|
||||
get_max_demand_processing = function(pos)
|
||||
-- function that will be called to determine how many demand nodes this controller can process per tick
|
||||
|
||||
get_max_storage_
|
||||
}
|
||||
|
||||
simpleName is used for the node registration, and will, if necessary, be converted
|
||||
to lowerspace and all spaces replaced with _
|
||||
|
||||
|
@ -1,102 +1,102 @@
|
||||
|
||||
local NUM_DEMAND_SLOTS = 4 -- maybe at some point make this a param, but why?
|
||||
local NUM_REQUEST_SLOTS = 4 -- maybe at some point make this a param, but why?
|
||||
local PUSH_LIST_PICKER = "push_pick"
|
||||
local ON_OFF_BUTTON = "on_off_btn"
|
||||
local FORMSPEC_NAME = "logistica_demander"
|
||||
local FORMSPEC_NAME = "logistica_requester"
|
||||
|
||||
local demanderForms = {}
|
||||
local requesterForms = {}
|
||||
|
||||
local function get_demander_formspec(pos)
|
||||
local function get_requester_formspec(pos)
|
||||
local posForm = "nodemeta:"..pos.x..","..pos.y..","..pos.z
|
||||
local pushPos = logistica.get_demander_target(pos)
|
||||
local selectedList = logistica.get_demander_target_list(pos)
|
||||
local pushPos = logistica.get_requester_target(pos)
|
||||
local selectedList = logistica.get_requester_target_list(pos)
|
||||
local isOn = logistica.is_machine_on(pos)
|
||||
return "formspec_version[4]" ..
|
||||
"size[10.6,7]" ..
|
||||
logistica.ui.background..
|
||||
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["..posForm..";filter;0.5,0.5;"..NUM_REQUEST_SLOTS..",1;0]"..
|
||||
"list[current_player;main;0.5,2;8,4;0]"
|
||||
end
|
||||
|
||||
local function show_demander_formspec(playerName, pos)
|
||||
local function show_requester_formspec(playerName, pos)
|
||||
local pInfo = {}
|
||||
pInfo.position = pos
|
||||
demanderForms[playerName] = pInfo
|
||||
minetest.show_formspec(playerName, FORMSPEC_NAME, get_demander_formspec(pos))
|
||||
requesterForms[playerName] = pInfo
|
||||
minetest.show_formspec(playerName, FORMSPEC_NAME, get_requester_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 demanderForms[playerName] then return false end
|
||||
if not requesterForms[playerName] then return false end
|
||||
if fields.quit then
|
||||
demanderForms[playerName] = nil
|
||||
requesterForms[playerName] = nil
|
||||
elseif fields[ON_OFF_BUTTON] then
|
||||
local pos = demanderForms[playerName].position
|
||||
local pos = requesterForms[playerName].position
|
||||
if not pos then return false end
|
||||
logistica.toggle_machine_on_off(pos)
|
||||
show_demander_formspec(player:get_player_name(), pos)
|
||||
show_requester_formspec(player:get_player_name(), pos)
|
||||
elseif fields[PUSH_LIST_PICKER] then
|
||||
local selected = fields[PUSH_LIST_PICKER]
|
||||
if logistica.is_allowed_push_list(selected) then
|
||||
local pos = demanderForms[playerName].position
|
||||
local pos = requesterForms[playerName].position
|
||||
if not pos then return false end
|
||||
logistica.set_demander_target_list(pos, selected)
|
||||
logistica.set_requester_target_list(pos, selected)
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local function on_demander_punch(pos, node, puncher, pointed_thing)
|
||||
local targetPos = logistica.get_demander_target(pos)
|
||||
local function on_requester_punch(pos, node, puncher, pointed_thing)
|
||||
local targetPos = logistica.get_requester_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_demander_rightclick(pos, node, clicker, itemstack, pointed_thing)
|
||||
local function on_requester_rightclick(pos, node, clicker, itemstack, pointed_thing)
|
||||
if not clicker or not clicker:is_player() then return end
|
||||
show_demander_formspec(clicker:get_player_name(), pos)
|
||||
show_requester_formspec(clicker:get_player_name(), pos)
|
||||
end
|
||||
|
||||
local function after_place_demander(pos, placer, itemstack, numDemandSlots)
|
||||
local function after_place_requester(pos, placer, itemstack, numRequestSlots)
|
||||
local meta = minetest.get_meta(pos)
|
||||
if placer and placer:is_player() then
|
||||
meta:set_string("owner", placer:get_player_name())
|
||||
end
|
||||
logistica.set_demander_target_list(pos, "main")
|
||||
logistica.set_requester_target_list(pos, "main")
|
||||
local inv = meta:get_inventory()
|
||||
inv:set_size("filter", numDemandSlots)
|
||||
inv:set_size("actual", numDemandSlots)
|
||||
logistica.on_demander_change(pos)
|
||||
logistica.start_demander_timer(pos)
|
||||
inv:set_size("filter", numRequestSlots)
|
||||
inv:set_size("actual", numRequestSlots)
|
||||
logistica.on_requester_change(pos)
|
||||
logistica.start_requester_timer(pos)
|
||||
end
|
||||
|
||||
local function allow_demander_storage_inv_put(pos, listname, index, stack, player)
|
||||
local function allow_requester_storage_inv_put(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:add_item(stack)
|
||||
inv:set_stack(listname, index, slotStack)
|
||||
logistica.update_demander_on_item_added(pos)
|
||||
logistica.start_demander_timer(pos, 1)
|
||||
logistica.update_requester_on_item_added(pos)
|
||||
logistica.start_requester_timer(pos, 1)
|
||||
return 0
|
||||
end
|
||||
|
||||
local function allow_demander_inv_take(pos, listname, index, stack, player)
|
||||
local function allow_requester_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)
|
||||
logistica.update_demander_cache_pos(pos)
|
||||
logistica.update_requester_cache_pos(pos)
|
||||
return 0
|
||||
end
|
||||
|
||||
local function allow_demander_inv_move(_, _, _, _, _, _, _)
|
||||
local function allow_requester_inv_move(_, _, _, _, _, _, _)
|
||||
return 0
|
||||
end
|
||||
|
||||
@ -110,54 +110,54 @@ 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)
|
||||
-- transferRate is how many items per tick this demander can transfer, -1 for unlimited
|
||||
function logistica.register_demander(simpleName, transferRate)
|
||||
-- transferRate is how many items per tick this requester can transfer, -1 for unlimited
|
||||
function logistica.register_requester(simpleName, transferRate)
|
||||
local lname = string.lower(simpleName:gsub(" ", "_"))
|
||||
local demander_name = "logistica:demander_"..lname
|
||||
logistica.demanders[demander_name] = true
|
||||
local requester_name = "logistica:requester_"..lname
|
||||
logistica.requesters[requester_name] = true
|
||||
local grps = {oddly_breakable_by_hand = 3, cracky = 3 }
|
||||
grps[logistica.TIER_ALL] = 1
|
||||
local def = {
|
||||
description = simpleName.." Demander",
|
||||
description = simpleName.." Requester",
|
||||
drawtype = "normal",
|
||||
tiles = {
|
||||
"logistica_"..lname.."_demander_side.png^[transformR270",
|
||||
"logistica_"..lname.."_demander_side.png^[transformR90",
|
||||
"logistica_"..lname.."_demander_side.png^[transformR180",
|
||||
"logistica_"..lname.."_demander_side.png",
|
||||
"logistica_"..lname.."_demander_back.png",
|
||||
"logistica_"..lname.."_demander_front.png",
|
||||
"logistica_"..lname.."_requester_side.png^[transformR270",
|
||||
"logistica_"..lname.."_requester_side.png^[transformR90",
|
||||
"logistica_"..lname.."_requester_side.png^[transformR180",
|
||||
"logistica_"..lname.."_requester_side.png",
|
||||
"logistica_"..lname.."_requester_back.png",
|
||||
"logistica_"..lname.."_requester_front.png",
|
||||
},
|
||||
paramtype = "light",
|
||||
paramtype2 = "facedir",
|
||||
is_ground_content = false,
|
||||
groups = grps,
|
||||
drop = demander_name,
|
||||
drop = requester_name,
|
||||
sounds = logistica.node_sound_metallic(),
|
||||
on_timer = logistica.on_demander_timer,
|
||||
on_timer = logistica.on_requester_timer,
|
||||
after_place_node = function (pos, placer, itemstack)
|
||||
after_place_demander(pos, placer, itemstack, NUM_DEMAND_SLOTS)
|
||||
after_place_requester(pos, placer, itemstack, NUM_REQUEST_SLOTS)
|
||||
end,
|
||||
after_destruct = logistica.on_demander_change,
|
||||
on_punch = on_demander_punch,
|
||||
on_rightclick = on_demander_rightclick,
|
||||
allow_metadata_inventory_put = allow_demander_storage_inv_put,
|
||||
allow_metadata_inventory_take = allow_demander_inv_take,
|
||||
allow_metadata_inventory_move = allow_demander_inv_move,
|
||||
after_destruct = logistica.on_requester_change,
|
||||
on_punch = on_requester_punch,
|
||||
on_rightclick = on_requester_rightclick,
|
||||
allow_metadata_inventory_put = allow_requester_storage_inv_put,
|
||||
allow_metadata_inventory_take = allow_requester_inv_take,
|
||||
allow_metadata_inventory_move = allow_requester_inv_move,
|
||||
logistica = {
|
||||
demander_transfer_rate = transferRate,
|
||||
requester_transfer_rate = transferRate,
|
||||
on_connect_to_network = function(pos, networkId)
|
||||
logistica.start_demander_timer(pos)
|
||||
logistica.start_requester_timer(pos)
|
||||
end,
|
||||
on_power = function(pos, isPoweredOn)
|
||||
if isPoweredOn then
|
||||
logistica.start_demander_timer(pos)
|
||||
logistica.start_requester_timer(pos)
|
||||
end
|
||||
end,
|
||||
}
|
||||
}
|
||||
|
||||
minetest.register_node(demander_name, def)
|
||||
minetest.register_node(requester_name, def)
|
||||
|
||||
local def_disabled = table.copy(def)
|
||||
local tiles_disabled = {}
|
||||
@ -172,9 +172,9 @@ function logistica.register_demander(simpleName, transferRate)
|
||||
def_disabled.on_timer = nil
|
||||
def_disabled.logistica = nil
|
||||
|
||||
minetest.register_node(demander_name.."_disabled", def_disabled)
|
||||
minetest.register_node(requester_name.."_disabled", def_disabled)
|
||||
|
||||
end
|
||||
|
||||
logistica.register_demander("Item", 1)
|
||||
logistica.register_demander("Stack", 99)
|
||||
logistica.register_requester("Item", 1)
|
||||
logistica.register_requester("Stack", 99)
|
@ -51,19 +51,12 @@ local function on_player_receive_fields(player, formname, fields)
|
||||
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 function after_place_supplier(pos, placer, itemstack, numRequestSlots)
|
||||
local meta = minetest.get_meta(pos)
|
||||
if placer and placer:is_player() then
|
||||
meta:set_string("owner", placer:get_player_name())
|
||||
@ -71,7 +64,7 @@ local function after_place_supplier(pos, placer, itemstack, numDemandSlots)
|
||||
logistica.toggle_machine_on_off(pos)
|
||||
logistica.set_supplier_target_list(pos, "dst")
|
||||
local inv = meta:get_inventory()
|
||||
inv:set_size("filter", numDemandSlots)
|
||||
inv:set_size("filter", numRequestSlots)
|
||||
logistica.on_supplier_change(pos)
|
||||
end
|
||||
|
||||
@ -136,7 +129,6 @@ function logistica.register_supplier(simpleName, maxTransferRate)
|
||||
after_place_supplier(pos, placer, itemstack, NUM_SUPPLY_SLOTS)
|
||||
end,
|
||||
after_destruct = logistica.on_supplier_change,
|
||||
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,
|
||||
|
@ -1,8 +1,8 @@
|
||||
--[[
|
||||
Outline of controller tick/s:
|
||||
1. Gather demand from each demander, add them to queue
|
||||
- smart queue needed, unify demand per demander
|
||||
2. For the first N demands, check each supplier and if applicable fulfil demand
|
||||
1. Gather request from each requester, add them to queue
|
||||
- smart queue needed, unify request per requester
|
||||
2. For the first N requests, check each supplier and if applicable fulfil request
|
||||
3. Gather all storage slots
|
||||
- cached, hopefully
|
||||
4. For each storage slot, check each supplier, and pull up to S items per slot into storage
|
||||
@ -18,6 +18,7 @@ function logistica.start_controller_timer(pos, duration)
|
||||
end
|
||||
|
||||
function logistica.on_controller_timer(pos, elapsed)
|
||||
if pos then return false end --
|
||||
local node = minetest.get_node(pos)
|
||||
if not node then return false end -- what?
|
||||
if node.name:find("_disabled") then return false end -- disabled controllers don't do anything
|
||||
@ -31,16 +32,6 @@ function logistica.on_controller_timer(pos, elapsed)
|
||||
network = logistica.get_network_or_nil(pos)
|
||||
if not network then return true end -- something went wrong, retry again
|
||||
|
||||
local nodes_in_demand = {}
|
||||
for demander,_ in pairs(network.demanders) do
|
||||
-- check for demand
|
||||
end
|
||||
|
||||
-- for each demand, check suppliers
|
||||
|
||||
|
||||
-- for each demand, check storage
|
||||
|
||||
if had_demand then
|
||||
logistica.start_controller_timer(pos, TIMER_DURATION_SHORT)
|
||||
else
|
||||
|
@ -2,11 +2,11 @@ logistica.cables = {}
|
||||
logistica.machines = {}
|
||||
logistica.controllers = {}
|
||||
logistica.injectors = {}
|
||||
logistica.demanders = {}
|
||||
logistica.requesters = {}
|
||||
logistica.suppliers = {}
|
||||
logistica.mass_storage = {}
|
||||
logistica.item_storage = {}
|
||||
-- logistica.demand_and_supplier = {}
|
||||
-- logistica.request_and_supplier = {}
|
||||
logistica.tiers = {}
|
||||
logistica.TIER_ALL = "logistica_all_tiers"
|
||||
logistica.GROUP_ALL = "group:" .. logistica.TIER_ALL
|
||||
@ -27,8 +27,8 @@ function logistica.is_machine(name)
|
||||
return logistica.machines[name] ~= nil
|
||||
end
|
||||
|
||||
function logistica.is_demander(name)
|
||||
return logistica.demanders[name] ~= nil
|
||||
function logistica.is_requester(name)
|
||||
return logistica.requesters[name] ~= nil
|
||||
end
|
||||
|
||||
function logistica.is_supplier(name)
|
||||
|
@ -4,9 +4,10 @@ dofile(path .. "/processing_queue.lua")
|
||||
dofile(path .. "/groups.lua")
|
||||
dofile(path .. "/network_cache.lua")
|
||||
dofile(path .. "/network_logic.lua")
|
||||
dofile(path .. "/node_list_cache.lua")
|
||||
dofile(path .. "/network_storage.lua")
|
||||
dofile(path .. "/controller.lua")
|
||||
dofile(path .. "/mass_storage.lua")
|
||||
dofile(path .. "/supplier.lua")
|
||||
dofile(path .. "/demander.lua")
|
||||
dofile(path .. "/requester.lua")
|
||||
dofile(path .. "/injector.lua")
|
||||
|
@ -1,3 +1,7 @@
|
||||
local META_KEY_PREV = "logprev_"
|
||||
local META_KEY_CURR = "logcurr_"
|
||||
|
||||
local SEP = ";"
|
||||
|
||||
local CACHE_PICKER_MASS_STORAGE = {
|
||||
listName = "filter",
|
||||
@ -11,15 +15,15 @@ local CACHE_PICKER_SUPPLIER = {
|
||||
cache = function (network) return network.supplier_cache end,
|
||||
nodes = function (network) return network.suppliers end,
|
||||
}
|
||||
local CACHE_PICKER_DEMANDER = {
|
||||
local CACHE_PICKER_REQUESTER = {
|
||||
listName = "filter",
|
||||
clear = function (network) network.demander_cache = {} end,
|
||||
cache = function (network) return network.demander_cache end,
|
||||
nodes = function (network) return network.demanders end,
|
||||
clear = function (network) network.requester_cache = {} end,
|
||||
cache = function (network) return network.requester_cache end,
|
||||
nodes = function (network) return network.requesters end,
|
||||
}
|
||||
|
||||
-- local function notify_demanders_of_new_cache(network)
|
||||
-- for hash, _ in pairs(network.demanders) do
|
||||
-- local function notify_requesters_of_new_cache(network)
|
||||
-- for hash, _ in pairs(network.requesters) do
|
||||
-- local pos = minetest.get_position_from_hash(hash)
|
||||
-- local node = minetest.get_node_or_nil(pos)
|
||||
-- if node then
|
||||
@ -55,7 +59,7 @@ local function update_network_cache(network, cacheOps)
|
||||
cache[name][hash] = true
|
||||
end
|
||||
end
|
||||
-- notify_demanders_of_new_cache(network)
|
||||
-- notify_requesters_of_new_cache(network)
|
||||
end
|
||||
|
||||
-- calls updateStorageCache(network) if the current position belongs to a network
|
||||
@ -80,7 +84,7 @@ local function update_cache_on_item_added(pos, network, cacheOps)
|
||||
if not cache[name] then cache[name] = {} end
|
||||
cache[name][posHash] = true
|
||||
end
|
||||
-- notify_demanders_of_new_cache(network)
|
||||
-- notify_requesters_of_new_cache(network)
|
||||
end
|
||||
|
||||
local function update_cache_on_item_added_at_pos(pos, cacheOps)
|
||||
@ -90,9 +94,9 @@ local function update_cache_on_item_added_at_pos(pos, cacheOps)
|
||||
end
|
||||
end
|
||||
|
||||
--------------------------------
|
||||
-- public functions
|
||||
--------------------------------
|
||||
---------------------------------
|
||||
--- public functions
|
||||
---------------------------------
|
||||
|
||||
function logistica.update_mass_storage_cache_pos(pos)
|
||||
update_network_cache_for_pos(pos, CACHE_PICKER_MASS_STORAGE)
|
||||
@ -118,14 +122,14 @@ function logistica.update_supplier_on_item_added(pos)
|
||||
update_cache_on_item_added_at_pos(pos, CACHE_PICKER_SUPPLIER)
|
||||
end
|
||||
|
||||
function logistica.update_demander_cache_pos(pos)
|
||||
update_network_cache_for_pos(pos, CACHE_PICKER_DEMANDER)
|
||||
function logistica.update_requester_cache_pos(pos)
|
||||
update_network_cache_for_pos(pos, CACHE_PICKER_REQUESTER)
|
||||
end
|
||||
|
||||
function logistica.update_demander_cache(network)
|
||||
update_network_cache(network, CACHE_PICKER_DEMANDER)
|
||||
function logistica.update_requester_cache(network)
|
||||
update_network_cache(network, CACHE_PICKER_REQUESTER)
|
||||
end
|
||||
|
||||
function logistica.update_demander_on_item_added(pos)
|
||||
update_cache_on_item_added_at_pos(pos, CACHE_PICKER_DEMANDER)
|
||||
function logistica.update_requester_on_item_added(pos)
|
||||
update_cache_on_item_added_at_pos(pos, CACHE_PICKER_REQUESTER)
|
||||
end
|
||||
|
@ -20,7 +20,7 @@ local adjecent = {
|
||||
|
||||
local function has_machine(network, id)
|
||||
if not network then return false end
|
||||
if network.demanders[id]
|
||||
if network.requesters[id]
|
||||
or network.suppliers[id]
|
||||
or network.mass_storage[id]
|
||||
or network.item_storage[id]
|
||||
@ -140,8 +140,8 @@ local function recursive_scan_for_nodes_for_controller(network, positionHashes,
|
||||
network.cables[otherHash] = true
|
||||
connections[otherHash] = true
|
||||
valid = true
|
||||
elseif logistica.is_demander(otherNode.name) then
|
||||
network.demanders[otherHash] = true
|
||||
elseif logistica.is_requester(otherNode.name) then
|
||||
network.requesters[otherHash] = true
|
||||
valid = true
|
||||
elseif logistica.is_injector(otherNode.name) then
|
||||
network.injectors[otherHash] = true
|
||||
@ -184,14 +184,14 @@ local function create_network(controllerPosition, oldNetworkName)
|
||||
network.controller = controllerHash
|
||||
network.name = networkName
|
||||
network.cables = {}
|
||||
network.demanders = {}
|
||||
network.requesters = {}
|
||||
network.injectors = {}
|
||||
network.suppliers = {}
|
||||
network.mass_storage = {}
|
||||
network.item_storage = {}
|
||||
network.storage_cache = {}
|
||||
network.supplier_cache = {}
|
||||
network.demander_cache = {}
|
||||
network.requester_cache = {}
|
||||
local startPos = {}
|
||||
startPos[controllerHash] = true
|
||||
local status = recursive_scan_for_nodes_for_controller(network, startPos)
|
||||
@ -205,7 +205,7 @@ local function create_network(controllerPosition, oldNetworkName)
|
||||
-- controller scan skips updating storage cache, do so now
|
||||
logistica.update_mass_storage_cache(network)
|
||||
logistica.update_supplier_cache(network)
|
||||
logistica.update_demander_cache(network)
|
||||
logistica.update_requester_cache(network)
|
||||
end
|
||||
if errorMsg ~= nil then
|
||||
networks[controllerHash] = nil
|
||||
@ -280,10 +280,10 @@ local function MASS_STORAGE_OPS(pos) return {
|
||||
update_cache_node_removed = function(network) logistica.update_mass_storage_cache(network) end,
|
||||
} end
|
||||
|
||||
local function DEMANDER_OPS(pos) return {
|
||||
get_list = function(network) return network.demanders end,
|
||||
update_cache_node_added = function(_) logistica.update_demander_on_item_added(pos) end,
|
||||
update_cache_node_removed = function(network) logistica.update_demander_cache(network) end,
|
||||
local function REQUESTER_OPS(pos) return {
|
||||
get_list = function(network) return network.requesters end,
|
||||
update_cache_node_added = function(_) logistica.update_requester_on_item_added(pos) end,
|
||||
update_cache_node_removed = function(network) logistica.update_requester_cache(network) end,
|
||||
} end
|
||||
|
||||
local function SUPPLIER_OPS(pos) return {
|
||||
@ -374,12 +374,12 @@ function logistica.on_mass_storage_change(pos, oldNode)
|
||||
end
|
||||
end
|
||||
|
||||
function logistica.on_demander_change(pos, oldNode)
|
||||
function logistica.on_requester_change(pos, oldNode)
|
||||
local placed = (oldNode == nil) -- if oldNode is nil, we placed a new one
|
||||
if placed == true then
|
||||
try_to_add_to_network(pos, DEMANDER_OPS(pos))
|
||||
try_to_add_to_network(pos, REQUESTER_OPS(pos))
|
||||
else
|
||||
remove_from_network(pos, DEMANDER_OPS(pos))
|
||||
remove_from_network(pos, REQUESTER_OPS(pos))
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -44,7 +44,7 @@ end
|
||||
-- returns true if item successfully found and given to collector, false otherwise
|
||||
function logistica.take_stack_from_mass_storage(stackToTake, network, collectorFunc, isAutomatedRequest)
|
||||
local stackToTakeName = stackToTake:get_name()
|
||||
local remainingDemand = stackToTake:get_count()
|
||||
local remainingRequest = stackToTake:get_count()
|
||||
local massLocations = network.storage_cache[stackToTake:get_name()]
|
||||
if massLocations == nil then return end
|
||||
for storageHash, _ in pairs(massLocations) do
|
||||
@ -60,14 +60,14 @@ function logistica.take_stack_from_mass_storage(stackToTake, network, collectorF
|
||||
local available = storageStack:get_count()
|
||||
if isAutomatedRequest then available = math.max(0, available - slotReserve) end
|
||||
if stackToTakeName == storageStack:get_name() and available > 0 then
|
||||
local numTaken = math.min(available, remainingDemand)
|
||||
local numTaken = math.min(available, remainingRequest)
|
||||
local takenStack = ItemStack(stackToTake)
|
||||
takenStack:set_count(numTaken)
|
||||
local leftover = collectorFunc(takenStack)
|
||||
numTaken = numTaken - leftover
|
||||
storageStack:set_count(storageStack:get_count() - numTaken)
|
||||
remainingDemand = remainingDemand - numTaken
|
||||
if remainingDemand <= 0 then
|
||||
remainingRequest = remainingRequest - numTaken
|
||||
if remainingRequest <= 0 then
|
||||
storageInv:set_list(MASS_STORAGE_LIST_NAME, storageList)
|
||||
return true
|
||||
end
|
||||
@ -107,12 +107,12 @@ function logistica.insert_item_in_network(itemstack, networkId)
|
||||
|
||||
local workingStack = ItemStack(itemstack)
|
||||
|
||||
-- check demanders first
|
||||
local listOfDemandersInNeedOfItem = network.demander_cache[itemstack:get_name()] or {}
|
||||
for hash, _ in pairs(listOfDemandersInNeedOfItem) do
|
||||
-- check requesters first
|
||||
local listOfRequestersInNeedOfItem = network.requester_cache[itemstack:get_name()] or {}
|
||||
for hash, _ in pairs(listOfRequestersInNeedOfItem) do
|
||||
local pos = minetest.get_position_from_hash(hash)
|
||||
logistica.load_position(pos)
|
||||
local leftover = logistica.insert_itemstack_for_demander(pos, workingStack, true)
|
||||
local leftover = logistica.insert_itemstack_for_requester(pos, workingStack, true)
|
||||
minetest.chat_send_all("insert_in_network: from: "..itemstack:get_count().." remain "..leftover)
|
||||
if leftover <= 0 then return 0 end -- we took all items
|
||||
workingStack:set_count(leftover)
|
||||
|
0
logic/node_list_cache.lua
Normal file
@ -1,6 +1,6 @@
|
||||
local TIMER_DURATION_SHORT = 1.0
|
||||
local TIMER_DURATION_LONG = 3.0
|
||||
local META_DEMANDER_LISTNAME = "demtarlist"
|
||||
local META_REQUESTER_LISTNAME = "demtarlist"
|
||||
local TARGET_NODES_REQUIRING_TIMER = {}
|
||||
TARGET_NODES_REQUIRING_TIMER["default:furnace"] = true
|
||||
TARGET_NODES_REQUIRING_TIMER["gravelsieve:auto_sieve0"] = true
|
||||
@ -13,24 +13,24 @@ local function get_meta(pos)
|
||||
return minetest.get_meta(pos)
|
||||
end
|
||||
|
||||
local function get_max_rate_for_demander(pos)
|
||||
local function get_max_rate_for_requester(pos)
|
||||
local node = minetest.get_node_or_nil(pos)
|
||||
if not node then return 0 end
|
||||
local nodeDef = minetest.registered_nodes[node.name]
|
||||
if nodeDef and nodeDef.logistica and nodeDef.logistica.demander_transfer_rate then
|
||||
if nodeDef.logistica.demander_transfer_rate <= 0 then return 9999
|
||||
else return nodeDef.logistica.demander_transfer_rate end
|
||||
if nodeDef and nodeDef.logistica and nodeDef.logistica.requester_transfer_rate then
|
||||
if nodeDef.logistica.requester_transfer_rate <= 0 then return 9999
|
||||
else return nodeDef.logistica.requester_transfer_rate end
|
||||
else
|
||||
return 0
|
||||
end
|
||||
end
|
||||
|
||||
local function get_valid_demander_and_target_inventory(demanderPos)
|
||||
local meta = get_meta(demanderPos)
|
||||
local targetList = meta:get_string(META_DEMANDER_LISTNAME)
|
||||
local function get_valid_requester_and_target_inventory(requesterPos)
|
||||
local meta = get_meta(requesterPos)
|
||||
local targetList = meta:get_string(META_REQUESTER_LISTNAME)
|
||||
if not targetList then return end
|
||||
|
||||
local targetPos = logistica.get_demander_target(demanderPos)
|
||||
local targetPos = logistica.get_requester_target(requesterPos)
|
||||
if not targetPos then return end
|
||||
|
||||
-- exclude logistica nodes from this
|
||||
@ -40,26 +40,26 @@ local function get_valid_demander_and_target_inventory(demanderPos)
|
||||
if not targetInv:get_list(targetList) then return end
|
||||
|
||||
return {
|
||||
demanderPos = demanderPos,
|
||||
demanderInventory = meta:get_inventory(),
|
||||
requesterPos = requesterPos,
|
||||
requesterInventory = meta:get_inventory(),
|
||||
targetInventory = targetInv,
|
||||
targetList = targetList,
|
||||
targetPos = targetPos,
|
||||
}
|
||||
end
|
||||
|
||||
local function get_target_missing_item_stack(demandStack, invs)
|
||||
local function get_target_missing_item_stack(requestStack, invs)
|
||||
local storageList = invs.targetInventory:get_list(invs.targetList)
|
||||
local remaining = demandStack:get_count()
|
||||
local remaining = requestStack:get_count()
|
||||
for i,_ in ipairs(storageList) do
|
||||
local stored = storageList[i]
|
||||
if demandStack:get_name() == stored:get_name() then
|
||||
if requestStack:get_name() == stored:get_name() then
|
||||
remaining = remaining - stored:get_count()
|
||||
end
|
||||
if remaining <= 0 then return ItemStack("") end
|
||||
end
|
||||
if remaining > 0 then
|
||||
local missingStack = ItemStack(demandStack)
|
||||
local missingStack = ItemStack(requestStack)
|
||||
missingStack:set_count(remaining)
|
||||
return missingStack
|
||||
else
|
||||
@ -69,59 +69,59 @@ end
|
||||
|
||||
-- returns:
|
||||
-- nil: nothing in inventory
|
||||
-- ItemStack: the next demanded item
|
||||
local function get_next_demanded_stack(pos, inventories)
|
||||
-- ItemStack: the next requested item
|
||||
local function get_next_requested_stack(pos, inventories)
|
||||
if not inventories then return nil end
|
||||
local nextSlot = logistica.get_next_filled_item_slot(get_meta(pos), "actual")
|
||||
if nextSlot <= 0 then return nil end
|
||||
return inventories.demanderInventory:get_list("actual")[nextSlot]
|
||||
return inventories.requesterInventory:get_list("actual")[nextSlot]
|
||||
end
|
||||
|
||||
-- updates the inv list called 'actual' with the latest checked demand
|
||||
local function update_demander_actual_demand(pos)
|
||||
local inventories = get_valid_demander_and_target_inventory(pos)
|
||||
-- updates the inv list called 'actual' with the latest checked request
|
||||
local function update_requester_actual_request(pos)
|
||||
local inventories = get_valid_requester_and_target_inventory(pos)
|
||||
if not inventories then return end
|
||||
local demanderInv = inventories.demanderInventory
|
||||
local actualDemandList = {}
|
||||
local demandStack = nil
|
||||
local requesterInv = inventories.requesterInventory
|
||||
local actualRequestList = {}
|
||||
local requestStack = nil
|
||||
local nextSlot = logistica.get_next_filled_item_slot(get_meta(pos), "filter")
|
||||
local startingSlot = nextSlot
|
||||
repeat
|
||||
if nextSlot <= 0 then return nil end
|
||||
local filterStack = demanderInv:get_list("filter")[nextSlot]
|
||||
demandStack = get_target_missing_item_stack(filterStack, inventories)
|
||||
local demStackCount = demandStack:get_count()
|
||||
local filterStack = requesterInv:get_list("filter")[nextSlot]
|
||||
requestStack = get_target_missing_item_stack(filterStack, inventories)
|
||||
local demStackCount = requestStack:get_count()
|
||||
if demStackCount > 0 then
|
||||
local prev = actualDemandList[demandStack:get_name()] or 0
|
||||
local prev = actualRequestList[requestStack:get_name()] or 0
|
||||
if demStackCount > prev then
|
||||
actualDemandList[demandStack:get_name()] = demStackCount
|
||||
actualRequestList[requestStack:get_name()] = demStackCount
|
||||
end
|
||||
end
|
||||
nextSlot = logistica.get_next_filled_item_slot(get_meta(pos), "filter")
|
||||
until( nextSlot == startingSlot ) -- until we get back to the starting slot
|
||||
local newActualDemandList = {}
|
||||
for itemname, count in pairs(actualDemandList) do
|
||||
table.insert(newActualDemandList, ItemStack(itemname.." "..count))
|
||||
local newActualRequestList = {}
|
||||
for itemname, count in pairs(actualRequestList) do
|
||||
table.insert(newActualRequestList, ItemStack(itemname.." "..count))
|
||||
end
|
||||
demanderInv:set_list("actual", newActualDemandList)
|
||||
requesterInv:set_list("actual", newActualRequestList)
|
||||
end
|
||||
|
||||
local function take_demanded_items_from_network(pos, network)
|
||||
local demandStack = get_next_demanded_stack(pos, get_valid_demander_and_target_inventory(pos))
|
||||
if demandStack == nil then return false end
|
||||
if demandStack == 0 then return true end -- had items but nothing in demand
|
||||
local function take_requested_items_from_network(pos, network)
|
||||
local requestStack = get_next_requested_stack(pos, get_valid_requester_and_target_inventory(pos))
|
||||
if requestStack == nil then return false end
|
||||
if requestStack == 0 then return true end -- had items but nothing in request
|
||||
-- limiting the number of items requested
|
||||
demandStack:set_count(math.min(get_max_rate_for_demander(pos), demandStack:get_count()))
|
||||
local collect = function(st) return logistica.insert_itemstack_for_demander(pos, st) end
|
||||
logistica.take_stack_from_network(demandStack, network, collect, true)
|
||||
requestStack:set_count(math.min(get_max_rate_for_requester(pos), requestStack:get_count()))
|
||||
local collect = function(st) return logistica.insert_itemstack_for_requester(pos, st) end
|
||||
logistica.take_stack_from_network(requestStack, network, collect, true)
|
||||
return true
|
||||
end
|
||||
|
||||
-- returns 0 if no demand, or the count of demanded items
|
||||
local function get_filter_demand_for(demanderInventory, itemStackName)
|
||||
local actualDemandList = demanderInventory:get_list("actual")
|
||||
if not actualDemandList then return 0 end
|
||||
for _, v in ipairs(actualDemandList) do
|
||||
-- returns 0 if no request, or the count of requested items
|
||||
local function get_filter_request_for(requesterInventory, itemStackName)
|
||||
local actualRequestList = requesterInventory:get_list("actual")
|
||||
if not actualRequestList then return 0 end
|
||||
for _, v in ipairs(actualRequestList) do
|
||||
if v:get_name() == itemStackName then
|
||||
return v:get_count()
|
||||
end
|
||||
@ -133,20 +133,20 @@ end
|
||||
-- Storage operation functions
|
||||
----------------------------------------------------------------
|
||||
|
||||
function logistica.start_demander_timer(pos, duration)
|
||||
function logistica.start_requester_timer(pos, duration)
|
||||
if duration == nil then duration = TIMER_DURATION_SHORT end
|
||||
logistica.start_node_timer(pos, duration)
|
||||
logistica.set_node_on_off_state(pos, true)
|
||||
end
|
||||
|
||||
function logistica.on_demander_timer(pos, elapsed)
|
||||
function logistica.on_requester_timer(pos, elapsed)
|
||||
local network = logistica.get_network_or_nil(pos)
|
||||
if not network or not logistica.is_machine_on(pos) then
|
||||
logistica.set_node_on_off_state(pos, false)
|
||||
return false
|
||||
end
|
||||
update_demander_actual_demand(pos)
|
||||
if take_demanded_items_from_network(pos, network) then
|
||||
update_requester_actual_request(pos)
|
||||
if take_requested_items_from_network(pos, network) then
|
||||
logistica.start_node_timer(pos, TIMER_DURATION_SHORT)
|
||||
else
|
||||
logistica.start_node_timer(pos, TIMER_DURATION_LONG)
|
||||
@ -154,28 +154,28 @@ function logistica.on_demander_timer(pos, elapsed)
|
||||
return false
|
||||
end
|
||||
|
||||
function logistica.set_demander_target_list(pos, listName)
|
||||
function logistica.set_requester_target_list(pos, listName)
|
||||
local meta = get_meta(pos)
|
||||
meta:set_string(META_DEMANDER_LISTNAME, listName)
|
||||
meta:set_string(META_REQUESTER_LISTNAME, listName)
|
||||
end
|
||||
|
||||
function logistica.get_demander_target_list(pos)
|
||||
function logistica.get_requester_target_list(pos)
|
||||
local meta = get_meta(pos)
|
||||
return meta:get_string(META_DEMANDER_LISTNAME)
|
||||
return meta:get_string(META_REQUESTER_LISTNAME)
|
||||
end
|
||||
|
||||
-- function logistica.update_demander_demand(demanderPos)
|
||||
-- local meta = get_meta(demanderPos)
|
||||
-- local inventories = get_valid_demander_and_target_inventory(demanderPos)
|
||||
-- function logistica.update_requester_request(requesterPos)
|
||||
-- local meta = get_meta(requesterPos)
|
||||
-- local inventories = get_valid_requester_and_target_inventory(requesterPos)
|
||||
-- if not inventories then return end
|
||||
-- local demandList = logistica.get_demand_based_on_list(
|
||||
-- inventories.demanderInventory, "filter",
|
||||
-- local requestList = logistica.get_request_based_on_list(
|
||||
-- inventories.requesterInventory, "filter",
|
||||
-- inventories.targetInventory, inventories.targetList
|
||||
-- )
|
||||
-- end
|
||||
|
||||
-- returns a list of ItemStacks tha represent the current demand of this demander
|
||||
function logistica.get_demander_demand(pos)
|
||||
-- returns a list of ItemStacks tha represent the current requests of this requester
|
||||
function logistica.get_requester_request(pos)
|
||||
local inv = get_meta(pos):get_inventory()
|
||||
local list = inv:get_list("filter")
|
||||
if not list then return {} end
|
||||
@ -186,8 +186,8 @@ function logistica.get_demander_demand(pos)
|
||||
return ret
|
||||
end
|
||||
|
||||
-- returns the demander's target position or nil if the demander isn't loaded
|
||||
function logistica.get_demander_target(pos)
|
||||
-- returns the requester's target position or nil if the requester isn't loaded
|
||||
function logistica.get_requester_target(pos)
|
||||
local node = minetest.get_node_or_nil(pos)
|
||||
if not node then return nil end
|
||||
return vector.add(pos, logistica.get_rot_directions(node.param2).backward)
|
||||
@ -195,26 +195,24 @@ 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
|
||||
-- `limitByDemand` is optional - if set to true, no more items than needed will be inserted
|
||||
function logistica.insert_itemstack_for_demander(demanderPos, itemstack, limitByDemand)
|
||||
-- `limitByRequest` is optional - if set to true, no more items than needed will be inserted
|
||||
function logistica.insert_itemstack_for_requester(requesterPos, itemstack, limitByRequest)
|
||||
if not itemstack or itemstack:is_empty() then return 0 end
|
||||
if not logistica.is_machine_on(demanderPos) then return itemstack:get_count() end
|
||||
if not logistica.is_machine_on(requesterPos) then return itemstack:get_count() end
|
||||
|
||||
local itemStackCount = itemstack:get_count()
|
||||
local itemStackName = itemstack:get_name()
|
||||
local inventories = get_valid_demander_and_target_inventory(demanderPos)
|
||||
local inventories = get_valid_requester_and_target_inventory(requesterPos)
|
||||
if not inventories then return itemStackCount end
|
||||
local targetInventory = inventories.targetInventory
|
||||
local targetList = inventories.targetList
|
||||
|
||||
local toInsertStack = ItemStack(itemstack)
|
||||
local demand = itemStackCount
|
||||
if limitByDemand then
|
||||
demand = get_filter_demand_for(inventories.demanderInventory, itemStackName)
|
||||
minetest.chat_send_all("-- filterDemand = "..demand)
|
||||
toInsertStack:set_count(demand)
|
||||
local request = itemStackCount
|
||||
if limitByRequest then
|
||||
request = get_filter_request_for(inventories.requesterInventory, itemStackName)
|
||||
toInsertStack:set_count(request)
|
||||
toInsertStack = get_target_missing_item_stack(toInsertStack, inventories)
|
||||
minetest.chat_send_all("-- missing item stack = "..toInsertStack:get_count())
|
||||
end
|
||||
if toInsertStack:is_empty() then return itemStackCount end
|
||||
|
||||
@ -223,5 +221,5 @@ function logistica.insert_itemstack_for_demander(demanderPos, itemstack, limitBy
|
||||
if leftover:get_count() < toInsertStack:get_count() and TARGET_NODES_REQUIRING_TIMER[targetNode.name] then
|
||||
logistica.start_node_timer(inventories.targetPos, 1)
|
||||
end
|
||||
return leftover:get_count() + itemStackCount - demand
|
||||
return leftover:get_count() + itemStackCount - request
|
||||
end
|
Before Width: | Height: | Size: 2.1 KiB After Width: | Height: | Size: 2.1 KiB |
Before Width: | Height: | Size: 2.2 KiB After Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 2.2 KiB After Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 2.1 KiB After Width: | Height: | Size: 2.1 KiB |
Before Width: | Height: | Size: 2.2 KiB After Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 2.3 KiB After Width: | Height: | Size: 2.3 KiB |