From ab440e19e94c6794d6ecc20f2d2110d27d2c9ebf Mon Sep 17 00:00:00 2001 From: Zenon Seth Date: Tue, 19 Mar 2024 17:44:27 +0000 Subject: [PATCH] Generify groups, fixing a bug with cached network id lookup, and allowing other mods to use group system via api --- api/access_point.lua | 2 +- api/bucket_emptier.lua | 2 +- api/bucket_filler.lua | 2 +- api/cables.lua | 2 +- api/cables_toggleable.lua | 2 +- api/cobble_supplier.lua | 2 +- api/controller.lua | 3 +- api/crafting_supplier.lua | 2 +- api/injector.lua | 2 +- api/item_storage.lua | 2 +- api/lava_furnace_fueler.lua | 2 +- api/mass_storage.lua | 2 +- api/pump.lua | 4 +- api/requester.lua | 2 +- api/reservoir.lua | 4 +- api/supplier.lua | 2 +- api/trashcan.lua | 2 +- api/vaccuum_chest.lua | 2 +- logic/controller.lua | 2 +- logic/groups.lua | 206 ++++++++++++++++++++++++++---------- logic/network_logic.lua | 92 ++++------------ logic/network_storage.lua | 14 +-- logic/pump.lua | 2 +- logic/requester.lua | 2 +- logic/reservoir.lua | 4 +- logic/supplier.lua | 2 +- logic/vaccuum_chest.lua | 2 +- tools/misc.lua | 2 +- util/ui_logic.lua | 2 +- 29 files changed, 210 insertions(+), 161 deletions(-) diff --git a/api/access_point.lua b/api/access_point.lua index 70d322b..9b1770d 100644 --- a/api/access_point.lua +++ b/api/access_point.lua @@ -72,7 +72,7 @@ minetest.register_on_player_receive_fields(logistica.on_receive_access_point_for function logistica.register_access_point(desc, name, tiles) local lname = string.lower(name:gsub(" ", "_")) local access_point_name = "logistica:"..lname - logistica.misc_machines[access_point_name] = true + logistica.GROUPS.misc_machines.register(access_point_name) local grps = {oddly_breakable_by_hand = 3, cracky = 3, handy = 1, pickaxey = 1, [logistica.TIER_ACCESS_POINT] = 1 } grps[logistica.TIER_ALL] = 1 local def = { diff --git a/api/bucket_emptier.lua b/api/bucket_emptier.lua index 9fb48d5..8167085 100644 --- a/api/bucket_emptier.lua +++ b/api/bucket_emptier.lua @@ -125,7 +125,7 @@ function logistica.register_bucket_emptier(desc, name, tiles) local lname = name:gsub("%s", "_"):lower() local emptier_name = "logistica:"..lname - logistica.bucket_emptiers[emptier_name] = true + logistica.GROUPS.bucket_emptiers.register(emptier_name) local def = { description = S(desc), tiles = tiles, diff --git a/api/bucket_filler.lua b/api/bucket_filler.lua index c424dde..c56bda7 100644 --- a/api/bucket_filler.lua +++ b/api/bucket_filler.lua @@ -138,7 +138,7 @@ end) function logistica.register_bucket_filler(desc, name, tiles) local lname = string.lower(name:gsub(" ", "_")) local filler_name = "logistica:"..lname - logistica.bucket_fillers[filler_name] = true + logistica.GROUPS.bucket_fillers.register(filler_name) local grps = {oddly_breakable_by_hand = 3, cracky = 3, handy = 1, pickaxey = 1, } grps[logistica.TIER_ALL] = 1 local def = { diff --git a/api/cables.lua b/api/cables.lua index e031218..5f5ea24 100644 --- a/api/cables.lua +++ b/api/cables.lua @@ -7,7 +7,7 @@ function logistica.register_cable(desc, name, customNodeBox, customTiles, custom local lname = string.lower(name) local cable_name = "logistica:" .. lname local cable_group = logistica.TIER_ALL - logistica.cables[cable_name] = name + logistica.GROUPS.cables.register(cable_name) local cnb = customNodeBox or {} local tiles = customTiles or { "logistica_" .. lname .. ".png" } diff --git a/api/cables_toggleable.lua b/api/cables_toggleable.lua index 87d718d..44071e4 100644 --- a/api/cables_toggleable.lua +++ b/api/cables_toggleable.lua @@ -78,7 +78,7 @@ function logistica.register_cable_toggleable(desc, name, tilesOn, tilesOff) } if state == "on" then - logistica.cables[cable_name] = name + logistica.GROUPS.cables.register(cable_name) def.groups[logistica.TIER_ALL] = 1 def.groups.not_in_creative_inventory = 1 else diff --git a/api/cobble_supplier.lua b/api/cobble_supplier.lua index 9e45213..2de7d27 100644 --- a/api/cobble_supplier.lua +++ b/api/cobble_supplier.lua @@ -175,7 +175,7 @@ end) function logistica.register_cobble_generator_supplier(desc, name, tiles) local lname = string.lower(name:gsub(" ", "_")) local supplier_name = "logistica:"..lname - logistica.suppliers[supplier_name] = true + logistica.GROUPS.suppliers.register(supplier_name) local grps = {oddly_breakable_by_hand = 3, cracky = 3, handy = 1, pickaxey = 1 } grps[logistica.TIER_ALL] = 1 local def = { diff --git a/api/controller.lua b/api/controller.lua index 221b94b..860aa8f 100644 --- a/api/controller.lua +++ b/api/controller.lua @@ -80,7 +80,7 @@ function logistica.register_controller(name, def) local controller_group = logistica.TIER_ALL local tier = logistica.TIER_ALL local controller_name = "logistica:" .. string.lower(name:gsub(" ", "_")) - logistica.controllers[controller_name] = tier + logistica.GROUPS.controllers.register(controller_name) local on_construct = function(pos) logistica.start_controller_timer(pos) @@ -91,7 +91,6 @@ function logistica.register_controller(name, def) def.groups = {} end def.groups[controller_group] = 1 - def.groups[logistica.TIER_CONTROLLER] = 1 def.on_timer = logistica.on_controller_timer def.on_construct = on_construct def.after_dig_node = logistica.on_controller_change diff --git a/api/crafting_supplier.lua b/api/crafting_supplier.lua index 030a1c0..8affb13 100644 --- a/api/crafting_supplier.lua +++ b/api/crafting_supplier.lua @@ -154,7 +154,7 @@ end) function logistica.register_crafting_supplier(desc, name, tiles) local lname = string.lower(name:gsub(" ", "_")) local supplier_name = "logistica:"..lname - logistica.craftsups[supplier_name] = true + logistica.GROUPS.crafting_suppliers.register(supplier_name) local grps = {oddly_breakable_by_hand = 3, cracky = 3, handy = 1, pickaxey = 1, } grps[logistica.TIER_ALL] = 1 local def = { diff --git a/api/injector.lua b/api/injector.lua index f5ef0be..79b5f83 100644 --- a/api/injector.lua +++ b/api/injector.lua @@ -127,7 +127,7 @@ end) function logistica.register_injector(description, name, transferRate, tiles) local lname = string.lower(name:gsub(" ", "_")) local injectorName = "logistica:"..lname - logistica.injectors[injectorName] = true + logistica.GROUPS.injectors.register(injectorName) local grps = {oddly_breakable_by_hand = 3, cracky = 3, handy = 1, pickaxey = 1 } grps[logistica.TIER_ALL] = 1 local def = { diff --git a/api/item_storage.lua b/api/item_storage.lua index 8771f23..b033197 100644 --- a/api/item_storage.lua +++ b/api/item_storage.lua @@ -110,7 +110,7 @@ end) function logistica.register_item_storage(desc, name, tiles) local lname = string.lower(name:gsub(" ", "_")) local item_storage_name = "logistica:"..lname - logistica.item_storage[item_storage_name] = true + logistica.GROUPS.item_storage.register(item_storage_name) local grps = {oddly_breakable_by_hand = 3, cracky = 3, handy = 1, pickaxey = 1 } grps[logistica.TIER_ALL] = 1 local def = { diff --git a/api/lava_furnace_fueler.lua b/api/lava_furnace_fueler.lua index 13e04d6..7c0556a 100644 --- a/api/lava_furnace_fueler.lua +++ b/api/lava_furnace_fueler.lua @@ -119,7 +119,7 @@ end) function logistica.register_lava_furnace_fueler(description, name, tiles) local lname = string.lower(name:gsub(" ", "_")) local fuelerName = "logistica:"..lname - logistica.misc_machines[fuelerName] = true + logistica.GROUPS.misc_machines.register(fuelerName) local grps = {oddly_breakable_by_hand = 3, cracky = 3, handy = 1, pickaxey = 1, } grps[logistica.TIER_ALL] = 1 local def = { diff --git a/api/mass_storage.lua b/api/mass_storage.lua index ce7f36c..b24d0ea 100644 --- a/api/mass_storage.lua +++ b/api/mass_storage.lua @@ -367,7 +367,7 @@ function logistica.register_mass_storage(simpleName, description, numSlots, numI local grps = {cracky = 1, choppy = 1, oddly_breakable_by_hand = 1, pickaxey = 3, axey = 3, handy = 1} numUpgradeSlots = logistica.clamp(numUpgradeSlots, 0, 4) grps[logistica.TIER_ALL] = 1 - logistica.mass_storage[storageName] = true + logistica.GROUPS.mass_storage.register(storageName) local def = { description = description.."\n(Empty)", diff --git a/api/pump.lua b/api/pump.lua index c8e0284..68cdd94 100644 --- a/api/pump.lua +++ b/api/pump.lua @@ -115,8 +115,8 @@ function logistica.register_pump(desc, name, tiles, tilesOn) local pump_name = "logistica:"..lname local pump_name_on = pump_name.."_on" - logistica.pumps[pump_name] = true - logistica.pumps[pump_name_on] = true + logistica.GROUPS.pumps.register(pump_name) + logistica.GROUPS.pumps.register(pump_name_on) local def = { description = S(desc), diff --git a/api/requester.lua b/api/requester.lua index b738608..a562bae 100644 --- a/api/requester.lua +++ b/api/requester.lua @@ -128,7 +128,7 @@ end) function logistica.register_requester(description, name, transferRate, tiles) local lname = string.lower(name:gsub(" ", "_")) local requester_name = "logistica:"..lname - logistica.requesters[requester_name] = true + logistica.GROUPS.requesters.register(requester_name) local grps = {oddly_breakable_by_hand = 3, cracky = 3, handy = 1, pickaxey = 1, } grps[logistica.TIER_ALL] = 1 local def = { diff --git a/api/reservoir.lua b/api/reservoir.lua index 6a0a978..95df893 100644 --- a/api/reservoir.lua +++ b/api/reservoir.lua @@ -159,7 +159,7 @@ for _, variantName in ipairs(variants) do def.drops = nodeName def.logistica.liquidName = LIQUID_NONE minetest.register_node(nodeName, def) - logistica.reservoirs[nodeName] = true + logistica.GROUPS.reservoirs.register(nodeName) end --[[ @@ -185,7 +185,7 @@ function logistica.register_reservoir(liquidName, liquidDesc, bucketItemName, li def.inventory_image = make_inv_image(variantName, liquidTexture) minetest.register_node(nodeName, def) - logistica.reservoirs[nodeName] = true + logistica.GROUPS.reservoirs.register(nodeName) logistica.reservoir_register_names(lname, bucketItemName, optEmptyBucketName, liquidDesc, liquidTexture, sourceNodeName) end diff --git a/api/supplier.lua b/api/supplier.lua index e4456ba..531346e 100644 --- a/api/supplier.lua +++ b/api/supplier.lua @@ -105,7 +105,7 @@ end) function logistica.register_supplier(desc, name, inventorySize, tiles) local lname = string.lower(name:gsub(" ", "_")) local supplier_name = "logistica:"..lname - logistica.suppliers[supplier_name] = true + logistica.GROUPS.suppliers.register(supplier_name) local grps = {oddly_breakable_by_hand = 3, cracky = 3, handy = 1, pickaxey = 1 } grps[logistica.TIER_ALL] = 1 local def = { diff --git a/api/trashcan.lua b/api/trashcan.lua index a31c9f4..cbfd8c3 100644 --- a/api/trashcan.lua +++ b/api/trashcan.lua @@ -72,7 +72,7 @@ end function logistica.register_trashcan(desc, name, tiles) local lname = string.lower(name:gsub(" ", "_")) local trashcan_name = "logistica:"..lname - logistica.trashcans[trashcan_name] = true + logistica.GROUPS.trashcans.register(trashcan_name) local grps = {oddly_breakable_by_hand = 3, cracky = 3, handy = 1, pickaxey = 1, } grps[logistica.TIER_ALL] = 1 local def = { diff --git a/api/vaccuum_chest.lua b/api/vaccuum_chest.lua index 905f79c..9eb4a92 100644 --- a/api/vaccuum_chest.lua +++ b/api/vaccuum_chest.lua @@ -107,7 +107,7 @@ end) function logistica.register_vaccuum_chest(desc, name, inventorySize, tiles) local lname = string.lower(name:gsub(" ", "_")) local vaccuum_name = "logistica:"..lname - logistica.vaccuum_suppliers[vaccuum_name] = true + logistica.GROUPS.vaccuum_suppliers.register(vaccuum_name) local grps = {oddly_breakable_by_hand = 3, cracky = 3, handy = 1, pickaxey = 1 } grps[logistica.TIER_ALL] = 1 local def = { diff --git a/logic/controller.lua b/logic/controller.lua index 795f9c6..c260679 100644 --- a/logic/controller.lua +++ b/logic/controller.lua @@ -9,7 +9,7 @@ end function logistica.on_controller_timer(pos, _) local node = minetest.get_node_or_nil(pos) if not node then return true end -- what? - if not logistica.is_controller(node.name) then return false end + if not logistica.GROUPS.controllers.is(node.name) then return false end local network = logistica.get_network_or_nil(pos) if not network then diff --git a/logic/groups.lua b/logic/groups.lua index f6df6e9..b602ecf 100644 --- a/logic/groups.lua +++ b/logic/groups.lua @@ -1,89 +1,183 @@ -logistica.cables = {} -logistica.controllers = {} -logistica.injectors = {} -logistica.requesters = {} -logistica.suppliers = {} -logistica.craftsups = {} -logistica.bucket_emptiers = {} -logistica.bucket_fillers = {} -logistica.pumps = {} -logistica.mass_storage = {} -logistica.item_storage = {} -logistica.misc_machines = {} -logistica.trashcans = {} -logistica.vaccuum_suppliers = {} -logistica.reservoirs = {} + +local logistica_groups = {} +local network_groups = {} + logistica.TIER_ALL = "logistica_all_tiers" logistica.GROUP_ALL = "group:" .. logistica.TIER_ALL -logistica.TIER_CONTROLLER = "logistica_controller" logistica.TIER_CABLE_OFF = "logistica_cable_off" logistica.GROUP_CABLE_OFF = "group:" .. logistica.TIER_CABLE_OFF logistica.TIER_ACCESS_POINT = "logistica_acspt" -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_bucket_filler(name) - or logistica.is_bucket_emptier(name) or logistica.is_pump(name) +-- default groups, and shortcut for checking them + +local function make_group_table(groupName, networkGroup) + return { + name = groupName, + networkGroup = networkGroup, + -- returns true if the given nodeName belongs to this group, false otherwise + is = function(nodeName) + return logistica.group_check(groupName, nodeName) + end, + -- returns true if registering the node name succeeds, false if the node name already exists + register = function(nodeName) + return logistica.group_register_node(groupName, nodeName) + end, + -- returns which network group this general group maps to + network_group = function() + return logistica.group_get_mapped_network_group(groupName) + end + } end -function logistica.is_cable(name) - return logistica.cables[name] ~= nil +-- The default network groups of Logistica. +-- These have special meaning and are treated with special actions. Each node group above must +-- map to one network group, though multiple node groups can map to the same network group
+-- More can be registered via logistica.network_group_register +logistica.NETWORK_GROUPS = { + cables = "cables", + requesters = "requesters", + injectors = "injectors", + suppliers = "suppliers", + mass_storage = "mass_storage", + item_storage = "item_storage", + misc = "misc", + trashcans = "trashcans", + reservoirs = "reservoirs", +} + +-- The default node groups of Logistica, with utility shorthand attached.
+-- More can be registered via logistica.group_register_type, and other related functions (see API below) +logistica.GROUPS = { + cables = make_group_table("cables", logistica.NETWORK_GROUPS.cables), + controllers = make_group_table("controllers", nil), + injectors = make_group_table("injectors", logistica.NETWORK_GROUPS.injectors), + requesters = make_group_table("requesters", logistica.NETWORK_GROUPS.requesters), + suppliers = make_group_table("suppliers", logistica.NETWORK_GROUPS.suppliers), + crafting_suppliers = make_group_table("craftsups", logistica.NETWORK_GROUPS.suppliers), + bucket_emptiers = make_group_table("bucket_emptiers", logistica.NETWORK_GROUPS.suppliers), + bucket_fillers = make_group_table("bucket_fillers", logistica.NETWORK_GROUPS.suppliers), + pumps = make_group_table("pumps", logistica.NETWORK_GROUPS.misc), + mass_storage = make_group_table("mass_storage", logistica.NETWORK_GROUPS.mass_storage), + item_storage = make_group_table("item_storage", logistica.NETWORK_GROUPS.item_storage), + misc_machines = make_group_table("misc_machines", logistica.NETWORK_GROUPS.misc), + trashcans = make_group_table("trashcans", logistica.NETWORK_GROUPS.trashcans), + vaccuum_suppliers = make_group_table("vaccuum_suppliers", logistica.NETWORK_GROUPS.suppliers), + reservoirs = make_group_table("reservoirs", logistica.NETWORK_GROUPS.reservoirs), +} + +---------------------------------------------------------------- +-- Public API +---------------------------------------------------------------- + +-------------------------------- +-- Helpers +-------------------------------- + +-- this is a utility function that checks if a nodeName is in any logistica node group, except cables +function logistica.is_machine(nodeName) + local group = logistica.group_get_node_group_name(nodeName) + return group ~= nil and group ~= logistica.GROUPS.cables.name end -function logistica.is_requester(name) - return logistica.requesters[name] ~= nil +-- shorthand for calling group_get_node_group_name followed by group_get_mapped_network_group +-- returns nil if it has no node group, or it maps to no network group +function logistica.get_network_group_for_node_name(nodeName) + local nodeGroup = logistica.group_get_node_group_name(nodeName) + if not nodeGroup then return nil end + return logistica.group_get_mapped_network_group(nodeGroup) end -function logistica.is_supplier(name) - return logistica.suppliers[name] ~= nil +-------------------------------- +-- Node Groups +-------------------------------- + +-- Logistica node group registration. Node groups are mostly used for looking up types of nodes, and +-- to also map them to network groups, to allow them to connect to a network.
+-- returns true/false if registering of a new group type succeeds
+-- `groupName` must be non-nil, string and unique - the name of the node group to register
+-- `networGroupItMapsTo` should be an existing network group (e.g. one of the default or a newly registered one) +-- or it could be `nil` if the given node group is not meant to connect to a network +function logistica.group_register_type(groupName, networkGroupItMapsTo) + if type(groupName) ~= "string" or logistica_groups[groupName] ~= nil then return false end + logistica_groups[groupName] = { _networkGroup = networkGroupItMapsTo } + return true end -function logistica.is_crafting_supplier(name) - return logistica.craftsups[name] ~= nil +-- returns true/false if the given group exists and the nodeName wasn't already part of that group +function logistica.group_register_node(groupName, nodeName) + if not logistica_groups[groupName] or logistica_groups[groupName][nodeName] ~= nil then return false end + logistica_groups[groupName][nodeName] = true end -function logistica.is_mass_storage(name) - return logistica.mass_storage[name] ~= nil +-- returns true if groupName exists and nodeName is a registered member of that group +function logistica.group_check(groupName, nodeName) + return logistica_groups[groupName] and logistica_groups[groupName][nodeName] end -function logistica.is_item_storage(name) - return logistica.item_storage[name] ~= nil +-- returns a copy of the group names as a lua set {groupName = true, groupTwo = true, ...} +function logistica.group_get_all_group_names() + local copy = {} + for k, _ in pairs(logistica_groups) do copy[k] = true end + return copy end -function logistica.is_controller(name) - return logistica.controllers[name] ~= nil +-- returns the group name (a string) to which the given nodeName belongs to, or nil if it doesn't belong to any group +function logistica.group_get_node_group_name(nodeName) + for k, v in pairs(logistica_groups) do + if v[nodeName] then return k end + end + return nil end -function logistica.is_injector(name) - return logistica.injectors[name] ~= nil +-------------------------------- +-- Network groups +-------------------------------- + +-- Network Group registration +-- These should be used along side logistica.group.group_register_type to map those general group types +-- to these network-specific group types
+-- You can re-use the default groups if necessary, so long as the new nodes registered provide same interaction capabilities.
+-- Any of these groups can be accessed via logistica.get_network_or_nil(..).network_group_name - +-- which contains a lua set of hashed node positions that are connected to this network
+-- `group_name` must be lowercase only, and contain no spaces - if any are present, they will be removed and then the group registered
+-- returns nil if registration failed, or the actual registered group name if it succeeds +-- (which will be identical to passed in group_name if that followed all the requirements are followed) +function logistica.network_group_register(group_name) + local lname = group_name:lower():gsub(" ", "") + if network_groups[lname] then return nil end + network_groups[lname] = true + return lname end -function logistica.is_misc(name) - return logistica.misc_machines[name] ~= nil +-- returns which network group this general group maps to, +-- or nil if the given group doesn't exist, or it doesn't map to any network group +function logistica.group_get_mapped_network_group(groupName) + if not logistica_groups[groupName] then return nil end + return logistica_groups[groupName]._networkGroup end -function logistica.is_trashcan(name) - return logistica.trashcans[name] ~= nil +-- returns true/false if the network group has already been registered +function logistica.network_group_exists(group_name) + local lname = group_name:lower():gsub(" ", "") + return network_groups[lname] ~= nil end -function logistica.is_vaccuum_supplier(name) - return logistica.vaccuum_suppliers[name] ~= nil +---------------------------------------------------------------- +-- Register all the default/built-in network groups, and then all node groups +---------------------------------------------------------------- + +for _, v in pairs(logistica.NETWORK_GROUPS) do + logistica.network_group_register(v) end -function logistica.is_reservoir(name) - return logistica.reservoirs[name] ~= nil +for _, v in pairs(logistica.GROUPS) do + logistica.group_register_type(v.name, v.networkGroup) end -function logistica.is_bucket_filler(name) - return logistica.bucket_fillers[name] ~= nil -end - -function logistica.is_bucket_emptier(name) - return logistica.bucket_emptiers[name] ~= nil -end - -function logistica.is_pump(name) - return logistica.pumps[name] ~= nil +-- due to this copy, this had to be placed below the registration +local network_groups_copy = table.copy(network_groups) +-- returns a copy of all the network groups as a lua set
+-- WARNING The copy is static, so treat it as read-only! +function logistica.network_group_get_all() + return network_groups_copy end diff --git a/logic/network_logic.lua b/logic/network_logic.lua index 5c1703c..b46d6fd 100644 --- a/logic/network_logic.lua +++ b/logic/network_logic.lua @@ -18,24 +18,15 @@ local adjecent = { local function has_machine(network, id) if not network then return false end - if network.requesters[id] - or network.suppliers[id] - or network.mass_storage[id] - or network.item_storage[id] - or network.injectors[id] - or network.misc[id] - or network.trashcans[id] - or network.reservoirs[id] - then - return true - else - return false + for networkGroup, v in pairs(logistica.network_group_get_all()) do + if network[networkGroup][id] ~= nil then return true end end + return false end local function network_contains_hash(network, hash) if hash == network.controller then return true end - if network.cables[hash] then return true end + if network[logistica.NETWORK_GROUPS.cables][hash] then return true end if has_machine(network, hash) then return true end return false end @@ -141,7 +132,7 @@ local function find_adjecent_networks(pos) for _, adj in pairs(adjecent) do local otherPos = vector.add(pos, adj) local otherNodeName = minetest.get_node(otherPos).name - if logistica.is_cable(otherNodeName) or logistica.is_controller(otherNodeName) then + if logistica.GROUPS.cables.is(otherNodeName) or logistica.GROUPS.controllers.is(otherNodeName) then local otherNetwork = logistica.get_network_or_nil(otherPos) if otherNetwork ~= nil then if currNetwork == nil then currNetwork = otherNetwork @@ -174,56 +165,23 @@ local function recursive_scan_for_nodes_for_controller(network, positionHashes, local otherHash = p2h(otherPos) if network.controller ~= otherHash and not has_machine(network, otherHash) - and network.cables[otherHash] == nil then + and network[logistica.NETWORK_GROUPS.cables][otherHash] == nil then local existingNetwork = logistica.get_network_id_or_nil(otherPos) if existingNetwork ~= nil and existingNetwork ~= network then return CREATE_NETWORK_STATUS_FAIL_OTHER_NETWORK end local valid = false - if logistica.is_cable(otherName) then - network.cables[otherHash] = true + local nodeNetworkGroup = logistica.get_network_group_for_node_name(otherName) + if nodeNetworkGroup == logistica.NETWORK_GROUPS.cables then -- cables get special treatment + network[nodeNetworkGroup][otherHash] = true connections[otherHash] = true valid = true - end - if logistica.is_requester(otherName) then - network.requesters[otherHash] = true - valid = true - end - if logistica.is_injector(otherName) then - network.injectors[otherHash] = true - valid = true - end - if logistica.is_supplier(otherName) - or logistica.is_crafting_supplier(otherName) - or logistica.is_vaccuum_supplier(otherName) - or logistica.is_bucket_filler(otherName) - or logistica.is_bucket_emptier(otherName) - then - network.suppliers[otherHash] = true - valid = true - end - if logistica.is_mass_storage(otherName) then - network.mass_storage[otherHash] = true - valid = true - end - if logistica.is_item_storage(otherName) then - network.item_storage[otherHash] = true - valid = true - end - if logistica.is_misc(otherName) - or logistica.is_pump(otherName) - then - network.misc[otherHash] = true - valid = true - end - if logistica.is_trashcan(otherName) then - network.trashcans[otherHash] = true - valid = true - end - if logistica.is_reservoir(otherName) then - network.reservoirs[otherHash] = true + elseif nodeNetworkGroup ~= nil then + -- all machines, except controllers, should be added to network + network[nodeNetworkGroup][otherHash] = true valid = true end + if valid then newToScan = newToScan + 1 set_cache_network_id(minetest.get_meta(otherPos), network.controller) @@ -251,18 +209,14 @@ local function create_network(controllerPosition, oldNetworkName) meta:set_string("infotext", "Controller of Network: "..networkName) network.controller = controllerHash network.name = networkName - network.cables = {} - network.requesters = {} - network.injectors = {} - network.suppliers = {} - network.mass_storage = {} - network.item_storage = {} - network.misc = {} - network.trashcans = {} + for group, _ in pairs(logistica.network_group_get_all()) do + network[group] = {} + end + -- caches aren't groups, so add them manually network.storage_cache = {} network.supplier_cache = {} network.requester_cache = {} - network.reservoirs = {} + local startPos = {} startPos[controllerHash] = true local status = recursive_scan_for_nodes_for_controller(network, startPos) @@ -399,7 +353,7 @@ local MISC_OPS = { local function cable_can_extend_network_from(pos) local node = minetest.get_node_or_nil(pos) if not node then return false end - return logistica.is_cable(node.name) or logistica.is_controller(node.name) + return logistica.GROUPS.cables.is(node.name) or logistica.GROUPS.controllers.is(node.name) end ---------------------------------------------------------------- @@ -416,7 +370,7 @@ function logistica.try_to_wake_up_network(pos) logistica.load_position(conPos) local node = minetest.get_node(conPos) - if logistica.is_controller(node.name) then + if logistica.GROUPS.controllers.is(node.name) then local nodeDef = minetest.registered_nodes[node.name] if nodeDef.on_timer then nodeDef.on_timer(conPos, 1) @@ -437,11 +391,11 @@ function logistica.on_cable_change(pos, oldNode, optMeta, wasPlacedOverride) if networkEnd then if not placed then -- removed a network end local network = logistica.get_network_or_nil(pos, optMeta) - if network then network.cables[p2h(pos)] = nil end + if network then network[logistica.NETWORK_GROUPS.cables][p2h(pos)] = nil end elseif cable_can_extend_network_from(connections[1]) then local otherNetwork = logistica.get_network_or_nil(connections[1]) if otherNetwork then - otherNetwork.cables[p2h(pos)] = true + otherNetwork[logistica.NETWORK_GROUPS.cables][p2h(pos)] = true set_cache_network_id(minetest.get_meta(pos), otherNetwork.controller) end end @@ -469,7 +423,7 @@ function logistica.on_cable_change(pos, oldNode, optMeta, wasPlacedOverride) if placed and allConnectionsHaveSameNetwork then local addToNetwork = logistica.get_network_by_id_or_nil(firstNetworkId) if addToNetwork then - addToNetwork.cables[p2h(pos)] = true + addToNetwork[logistica.NETWORK_GROUPS.cables][p2h(pos)] = true set_cache_network_id(minetest.get_meta(pos), addToNetwork.controller) end else diff --git a/logic/network_storage.lua b/logic/network_storage.lua index a48ce9c..4e5e2f2 100644 --- a/logic/network_storage.lua +++ b/logic/network_storage.lua @@ -147,17 +147,19 @@ function logistica.take_stack_from_suppliers(stackToTake, network, collectorFunc local pos = h2p(hash) logistica.load_position(pos) local nodeName = minetest.get_node(pos).name - if logistica.is_supplier(nodeName) - or logistica.is_vaccuum_supplier(nodeName) - or logistica.is_bucket_emptier(nodeName) - and (type == nil or type == "normal") + if (type == nil or type == "normal") + and ( + logistica.GROUPS.suppliers.is(nodeName) + or logistica.GROUPS.vaccuum_suppliers.is(nodeName) + or logistica.GROUPS.bucket_emptiers.is(nodeName) + ) then normalSupplierResult = logistica.take_item_from_supplier(pos, takeStack, network, collectorFunc, useMetadata, dryRun) remaining = normalSupplierResult.remaining - elseif logistica.is_crafting_supplier(nodeName) and (type == nil or type == "crafting") then + elseif (type == nil or type == "crafting") and logistica.GROUPS.crafting_suppliers.is(nodeName) then craftingSupplierResult = logistica.take_item_from_crafting_supplier(pos, takeStack, network, collectorFunc, useMetadata, dryRun, depth) remaining = craftingSupplierResult.remaining - elseif logistica.is_bucket_filler(nodeName) and (type == nil or type == "bucket") then + elseif (type == nil or type == "bucket") and logistica.GROUPS.bucket_fillers.is(nodeName) then bucketFillerResult = logistica.take_item_from_bucket_filler(pos, takeStack, network, collectorFunc, isAutomatedRequest, dryRun, depth) remaining = bucketFillerResult.remaining end diff --git a/logic/pump.lua b/logic/pump.lua index 8af7e8c..6ff6291 100644 --- a/logic/pump.lua +++ b/logic/pump.lua @@ -106,7 +106,7 @@ local function put_liquid_in_neighboring_reservoirs(pumpPosition, bucketItemStac local neighborPos = vector.add(pumpPosition, v) logistica.load_position(neighborPos) local neighborNode = minetest.get_node_or_nil(neighborPos) - if neighborNode and logistica.is_reservoir(neighborNode.name) then + if neighborNode and logistica.GROUPS.reservoirs.is(neighborNode.name) then local resultStack = logistica.reservoir_use_item_on(neighborPos, bucketItemStack, neighborNode) if resultStack ~= nil then return true end end diff --git a/logic/requester.lua b/logic/requester.lua index 67d0180..2adacdb 100644 --- a/logic/requester.lua +++ b/logic/requester.lua @@ -27,7 +27,7 @@ local function get_meta(pos) end local function is_prohibited_logistica_machine(name) - return logistica.is_machine(name) and not logistica.is_bucket_emptier(name) + return logistica.is_machine(name) and not logistica.GROUPS.bucket_emptiers.is(name) end local function get_max_rate_for_requester(pos) diff --git a/logic/reservoir.lua b/logic/reservoir.lua index 49feb60..f96d8ac 100644 --- a/logic/reservoir.lua +++ b/logic/reservoir.lua @@ -161,7 +161,7 @@ end -- returns the liquid name for the reservoir; or "" if there's no liquid stored, or nil if its not a reservoir function logistica.reservoir_get_liquid_name(pos) local node = minetest.get_node(pos) - if not logistica.is_reservoir(node.name) then return nil end + if not logistica.GROUPS.reservoirs.is(node.name) then return nil end local def = minetest.registered_nodes[node.name] if not def or not def.logistica or not def.logistica.liquidName then return nil end return def.logistica.liquidName @@ -170,7 +170,7 @@ end -- return {currentLevel, maxLevel} measured in buckets; or nil if its not a reservoir function logistica.reservoir_get_liquid_level(pos) local node = minetest.get_node(pos) - if not logistica.is_reservoir(node.name) then return nil end + if not logistica.GROUPS.reservoirs.is(node.name) then return nil end local def = minetest.registered_nodes[node.name] if not def or not def.logistica or not def.logistica.maxBuckets then return nil end local meta = minetest.get_meta(pos) diff --git a/logic/supplier.lua b/logic/supplier.lua index 613968d..baa68b9 100644 --- a/logic/supplier.lua +++ b/logic/supplier.lua @@ -31,7 +31,7 @@ end -- tries to put the given item in this supplier, returns what's leftover function logistica.put_item_in_supplier(pos, stack) local nodeName = minetest.get_node(pos).name - if not logistica.is_supplier(nodeName) then return stack end + if not logistica.GROUPS.suppliers.is(nodeName) then return stack end local nodeDef = minetest.registered_nodes[nodeName] if not nodeDef or not nodeDef.logistica then return stack end if not nodeDef.logistica.supplierMayAccept then return stack end diff --git a/logic/vaccuum_chest.lua b/logic/vaccuum_chest.lua index 1684076..e6ae8b9 100644 --- a/logic/vaccuum_chest.lua +++ b/logic/vaccuum_chest.lua @@ -33,7 +33,7 @@ local function collect_items_into(pos, distance) local inserted = 0 local nodeName = minetest.get_node(pos).name - if not logistica.is_vaccuum_supplier(nodeName) then return inserted end + if not logistica.GROUPS.vaccuum_suppliers.is(nodeName) then return inserted end local nodeDef = minetest.registered_nodes[nodeName] if not nodeDef or not nodeDef.logistica or not nodeDef.on_metadata_inventory_put then return inserted diff --git a/tools/misc.lua b/tools/misc.lua index feccb37..cd5d35c 100644 --- a/tools/misc.lua +++ b/tools/misc.lua @@ -10,7 +10,7 @@ minetest.register_tool("logistica:hyperspanner",{ local pos = pointed_thing.under if not placer or not pos then return end local node = minetest.get_node_or_nil(pos) - if not node or not (logistica.is_machine(node.name) or logistica.is_cable(node.name)) then return end + if not node or not (logistica.is_machine(node.name) or logistica.GROUPS.cables.is(node.name)) then return end local network = logistica.get_network_name_or_nil(pos) or S("") logistica.show_popup( placer:get_player_name(), diff --git a/util/ui_logic.lua b/util/ui_logic.lua index d7a1e44..675f16d 100644 --- a/util/ui_logic.lua +++ b/util/ui_logic.lua @@ -24,7 +24,7 @@ local function get_lists(targetPosition, usePushLists) local node = minetest.get_node(targetPosition) local allowedLists = {} - if logistica.is_bucket_emptier(node.name) then + if logistica.GROUPS.bucket_emptiers.is(node.name) then if usePushLists then allowedLists = logisticaBucketEmptierAllowedPush else return {} end -- can only push to bucket emptier, it acts as a supplier so no need to pull elseif logistica.is_machine(node.name) then return {}