Working implementation of demander; mass-storage; network fixes; utils
This commit is contained in:
parent
f9164d2e28
commit
b3f8be1eac
@ -52,6 +52,7 @@ function logistica.register_cable(tier, size)
|
||||
def_broken.groups = { cracky = 3, choppy = 3, oddly_breakable_by_hand = 2, not_in_creative_inventory = 1 }
|
||||
def_broken.description = "Broken " .. tier .. " Cable"
|
||||
def_broken.node_box = { type = "fixed", fixed = { -0.5, -size, -size, 0.5, size, size } }
|
||||
def_broken.selection_box = def_broken.node_box
|
||||
def_broken.connects_to = nil
|
||||
def_broken.on_construct = nil
|
||||
def_broken.after_destruct = nil
|
||||
|
@ -54,7 +54,7 @@ function logistica.register_controller(simpleName, def, tier)
|
||||
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 }
|
||||
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_timer = nil
|
||||
@ -66,7 +66,7 @@ logistica.register_controller("Simple Controller", {
|
||||
description = "Simple Controller",
|
||||
tiles = { "logistica_silver_cable.png" },
|
||||
groups = {
|
||||
oddly_breakable_by_hand = 1,
|
||||
oddly_breakable_by_hand = 1, cracky = 2,
|
||||
},
|
||||
sounds = default.node_sound_metal_defaults(),
|
||||
paramtype = "light",
|
||||
|
177
api/demander.lua
177
api/demander.lua
@ -1,28 +1,159 @@
|
||||
--[[
|
||||
Demander nodes should provide a table called `logistica_demander` in their definition
|
||||
This table should define::
|
||||
{
|
||||
get_demanded = function(pos)
|
||||
-- required
|
||||
-- pos: The position of this demander node
|
||||
-- This will be called to check what item demands are required.
|
||||
-- The function must return a table of `itemstack` definitions that are needed, e.g.:
|
||||
-- {"default:cobble 3", "default:stick 2"}
|
||||
-- if the function returns an empty table or nil, no items will be supplied
|
||||
|
||||
receive_items = function(pos, itemstack)
|
||||
-- required
|
||||
-- pos: The position of this demanded node
|
||||
-- itemstack: an ItemStack object instance
|
||||
-- called to provide items, as requested by get_demand
|
||||
-- multiple itemstack requests will result in multiple calls to this function
|
||||
-- each call providing one itemstack (which may contain multiple items in the stack)
|
||||
local NUM_DEMAND_SLOTS = 4 -- maybe at some point make this a param, but why?
|
||||
local PUSH_LIST_PICKER = "push_pick"
|
||||
local FORMSPEC_NAME = "logistica_demander"
|
||||
|
||||
local demanderForms = {}
|
||||
|
||||
local function get_demander_formspec(pos)
|
||||
local posForm = "nodemeta:"..pos.x..","..pos.y..","..pos.z
|
||||
local pushPos = logistica.get_demander_target(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local selectedList = meta:get_string("demtarlist")
|
||||
return "formspec_version[4]" ..
|
||||
"size[10.6,7]" ..
|
||||
logistica.ui.background..
|
||||
logistica.get_push_list_dropdown(PUSH_LIST_PICKER, 8.5, 0.7, pushPos, selectedList)..
|
||||
"list["..posForm..";filter;0.5,0.5;"..NUM_DEMAND_SLOTS..",1;0]"..
|
||||
"list[current_player;main;0.5,2;8,4;0]"
|
||||
|
||||
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 fields.quit then
|
||||
demanderForms[playerName] = nil
|
||||
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
|
||||
if not pos then return false end
|
||||
logistica.set_demander_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)
|
||||
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)
|
||||
if not clicker or not clicker:is_player() then return end
|
||||
local pInfo = {}
|
||||
pInfo.position = pos
|
||||
demanderForms[clicker:get_player_name()] = pInfo
|
||||
minetest.show_formspec(clicker:get_player_name(), FORMSPEC_NAME, get_demander_formspec(pos))
|
||||
end
|
||||
|
||||
local function after_place_demander(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
|
||||
meta:set_string("demtarlist", "main")
|
||||
local inv = meta:get_inventory()
|
||||
inv:set_size("filter", numDemandSlots)
|
||||
logistica.on_demander_change(pos)
|
||||
logistica.start_demander_timer(pos)
|
||||
end
|
||||
|
||||
local function allow_demander_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)
|
||||
return 0
|
||||
end
|
||||
|
||||
local function allow_demander_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_demander_inv_move(pos, from_list, from_index, to_list, to_index, count, player)
|
||||
if from_list ~= "filter" and to_list ~= "filter" then return 0 end
|
||||
return count
|
||||
end
|
||||
|
||||
----------------------------------------------------------------
|
||||
-- Minetest registration
|
||||
----------------------------------------------------------------
|
||||
|
||||
minetest.register_on_player_receive_fields(on_player_receive_fields)
|
||||
|
||||
----------------------------------------------------------------
|
||||
-- Public Registration API
|
||||
----------------------------------------------------------------
|
||||
|
||||
function logistica.register_demander(simpleName)
|
||||
local lname = string.lower(simpleName:gsub(" ", "_"))
|
||||
local demander_name = "logistica:demander_"..lname
|
||||
logistica.demanders[demander_name] = true
|
||||
local grps = {oddly_breakable_by_hand = 3, cracky = 3 }
|
||||
grps[logistica.TIER_ALL] = 1
|
||||
local def = {
|
||||
description = simpleName.." Demander",
|
||||
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",
|
||||
},
|
||||
paramtype = "light",
|
||||
paramtype2 = "facedir",
|
||||
is_ground_content = false,
|
||||
groups = grps,
|
||||
drop = demander_name,
|
||||
sounds = default.node_sound_metal_defaults(),
|
||||
on_timer = logistica.on_demander_timer,
|
||||
after_place_node = function (pos, placer, itemstack)
|
||||
after_place_demander(pos, placer, itemstack, NUM_DEMAND_SLOTS)
|
||||
end,
|
||||
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,
|
||||
logistica = {
|
||||
on_connect_to_network = function(pos, networkId)
|
||||
logistica.start_demander_timer(pos)
|
||||
end
|
||||
}
|
||||
}
|
||||
|
||||
Currently demanders will connect to all network tiers - network tiers only differ
|
||||
]]
|
||||
minetest.register_node(demander_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(demander_name.."_disabled", def_disabled)
|
||||
|
||||
function logistica.register_demander(simpleName, definition)
|
||||
local demander_name = "logistica:demander_"..simpleName
|
||||
|
||||
end
|
||||
|
||||
logistica.register_demander("Basic")
|
||||
|
@ -202,7 +202,7 @@ function logistica.register_mass_storage(simpleName, numSlots, numItemsPerSlot,
|
||||
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 = { cracky = 3, choppy = 3, oddly_breakable_by_hand = 2 }
|
||||
def_disabled.groups = { cracky = 3, choppy = 3, oddly_breakable_by_hand = 3, not_in_creative_inventory = 1 }
|
||||
|
||||
minetest.register_node(storageName.."_disabled", def_disabled)
|
||||
|
||||
|
48
entity/entity.lua
Normal file
48
entity/entity.lua
Normal file
@ -0,0 +1,48 @@
|
||||
local IO_ENTITY_LIFETIME = 3
|
||||
local entityTable = {}
|
||||
|
||||
function logistica.add_entity(key, entity)
|
||||
logistica.remove_entity(key)
|
||||
entityTable[key] = entity
|
||||
end
|
||||
|
||||
function logistica.remove_entity(key)
|
||||
if entityTable[key] then
|
||||
entityTable[key].object:remove()
|
||||
end
|
||||
end
|
||||
|
||||
local ioCommonDef = {
|
||||
physical = false,
|
||||
collide_with_objects = false,
|
||||
visual = "cube",
|
||||
collisionbox = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
|
||||
backface_culling = false,
|
||||
visual_size = {x=1.1, y=1.1},
|
||||
static_save = false,
|
||||
groups = {"immortal"},
|
||||
on_punch = function (self, puncher, time_from_last_punch, tool_capabilities, dir)
|
||||
self.object:remove()
|
||||
end,
|
||||
on_activate = function(self, staticdata, dtime_s)
|
||||
self.data = staticdata
|
||||
end,
|
||||
on_step = function (self, dtime)
|
||||
self.lifeTime = self.lifeTime + dtime
|
||||
if self.lifeTime > IO_ENTITY_LIFETIME then
|
||||
self.object:remove()
|
||||
end
|
||||
end,
|
||||
lifeTime = 0,
|
||||
data = ""
|
||||
}
|
||||
|
||||
local inputDef = table.copy(ioCommonDef)
|
||||
inputDef.textures =
|
||||
{"logistica_entity_input.png", "logistica_entity_input.png", "logistica_entity_input.png", "logistica_entity_input.png", "logistica_entity_input.png", "logistica_entity_input.png"}
|
||||
minetest.register_entity("logistica:input_entity", inputDef)
|
||||
|
||||
local outputDef = table.copy(ioCommonDef)
|
||||
outputDef.textures =
|
||||
{"logistica_entity_output.png", "logistica_entity_output.png", "logistica_entity_output.png", "logistica_entity_output.png", "logistica_entity_output.png", "logistica_entity_output.png"}
|
||||
minetest.register_entity("logistica:output_entity", outputDef)
|
1
init.lua
1
init.lua
@ -5,6 +5,7 @@ logistica.MODPATH = minetest.get_modpath(logistica.MODNAME)
|
||||
|
||||
-- order of loading files DOES matter
|
||||
dofile(logistica.MODPATH.."/util/util.lua")
|
||||
dofile(logistica.MODPATH.."/entity/entity.lua")
|
||||
dofile(logistica.MODPATH.."/logic/logic.lua")
|
||||
dofile(logistica.MODPATH.."/tools/tools.lua")
|
||||
|
||||
|
@ -1,24 +1,200 @@
|
||||
local TIMER_DURATION_SHORT = 2.0
|
||||
local TIMER_DURATION_LONG = 4.0
|
||||
local META_DEMANDER_LISTNAME = "demtarlist"
|
||||
local MASS_STORAGE_LIST_NAME = "storage"
|
||||
local ITEM_STORAGE_LIST_NAME = "main"
|
||||
local TARGET_NODES_REQUIRING_TIMER = {}
|
||||
TARGET_NODES_REQUIRING_TIMER["default:furnace"] = true
|
||||
TARGET_NODES_REQUIRING_TIMER["gravelsieve:auto_sieve3"] = true
|
||||
|
||||
-- Returns a list of ItemStacks from the demandList that are missing in the storageList
|
||||
function logistica.get_demand_based_on_list(demandList, storageList)
|
||||
local missing = {}
|
||||
local storageSize = #storageList
|
||||
for _, demanded in ipairs(demandList) do
|
||||
local remaining = demanded:get_count()
|
||||
local i = 1
|
||||
local checkNext = true
|
||||
while checkNext and i <= storageSize do
|
||||
local function get_meta(pos)
|
||||
logistica.load_position(pos)
|
||||
return minetest.get_meta(pos)
|
||||
end
|
||||
|
||||
local function get_valid_demander_and_target_inventory(demanderPos)
|
||||
local meta = get_meta(demanderPos)
|
||||
local targetList = meta:get_string(META_DEMANDER_LISTNAME)
|
||||
if not targetList then return end
|
||||
|
||||
local targetPos = logistica.get_demander_target(demanderPos)
|
||||
if not targetPos then return end
|
||||
|
||||
-- exclude logistica nodes from this
|
||||
if string.find(minetest.get_node(targetPos).name, "logistica:") then return end
|
||||
|
||||
local targetInv = get_meta(targetPos):get_inventory()
|
||||
if not targetInv:get_list(targetList) then return end
|
||||
|
||||
return {
|
||||
demanderInventory = meta:get_inventory(),
|
||||
targetInventory = targetInv,
|
||||
targetList = targetList,
|
||||
targetPos = targetPos,
|
||||
}
|
||||
end
|
||||
|
||||
local function get_actual_demand_for_item(demandStack, storageInv, storageListName)
|
||||
local storageList = storageInv:get_list(storageListName)
|
||||
local remaining = demandStack:get_count()
|
||||
for i,v in ipairs(storageList) do
|
||||
local stored = storageList[i]
|
||||
if demanded:get_name() == stored:get_name() then
|
||||
if demandStack:get_name() == stored:get_name() then
|
||||
remaining = remaining - stored:get_count()
|
||||
end
|
||||
if remaining <= 0 then checkNext = false end
|
||||
if remaining <= 0 then return ItemStack("") end
|
||||
end
|
||||
if remaining > 0 then
|
||||
local missingStack = ItemStack(demanded)
|
||||
local missingStack = ItemStack(demandStack)
|
||||
missingStack:set_count(remaining)
|
||||
table.insert(missing, missingStack)
|
||||
return missingStack
|
||||
else
|
||||
return ItemStack("")
|
||||
end
|
||||
end
|
||||
|
||||
local function try_to_fulfil_demanded_item_from_network_item_storage(pos, filterStack, network, inventories)
|
||||
for storageHash, _ in pairs(network.item_storage) do
|
||||
local storagePos = minetest.get_position_from_hash(storageHash)
|
||||
local storageInv = get_meta(storagePos):get_inventory()
|
||||
if storageInv:contains_item(ITEM_STORAGE_LIST_NAME, filterStack) then
|
||||
local leftover =
|
||||
logistica.insert_itemstack_for_demander(pos, filterStack)
|
||||
if leftover == 0 then -- stack max is 1, so just take the whole itemstack out
|
||||
storageInv:remove_item(ITEM_STORAGE_LIST_NAME, filterStack)
|
||||
return true
|
||||
end -- otherwise, the insert failed for some reason..
|
||||
end
|
||||
end
|
||||
return missing
|
||||
return false
|
||||
end
|
||||
|
||||
local function try_to_fulfil_demanded_item_from_locations(pos, filterStack, locations, inventories)
|
||||
local filterStackName = filterStack:get_name()
|
||||
local remainingDemand = filterStack:get_count()
|
||||
for storageHash, _ in pairs(locations) do
|
||||
if filterStack:get_count() == 0 then return end
|
||||
local storagePos = minetest.get_position_from_hash(storageHash)
|
||||
local storageInv = get_meta(storagePos):get_inventory()
|
||||
local storageList = storageInv:get_list(MASS_STORAGE_LIST_NAME)
|
||||
-- we can't use the usual take/put methods because mass storage exceeds max stack
|
||||
for i = #storageList, 1, -1 do -- traverse backwards for taking items
|
||||
local storageStack = storageList[i]
|
||||
if filterStackName == storageStack:get_name() then
|
||||
local numTaken = math.min(storageStack:get_count(), remainingDemand)
|
||||
local takenStack = ItemStack(filterStack)
|
||||
takenStack:set_count(numTaken)
|
||||
local leftover =
|
||||
logistica.insert_itemstack_for_demander(pos, takenStack)
|
||||
numTaken = numTaken - leftover
|
||||
storageStack:set_count(storageStack:get_count() - numTaken)
|
||||
remainingDemand = remainingDemand - numTaken
|
||||
if remainingDemand <= 0 then
|
||||
storageInv:set_list(MASS_STORAGE_LIST_NAME, storageList)
|
||||
return true
|
||||
end
|
||||
end
|
||||
i = i - 1
|
||||
end
|
||||
storageInv:set_list(MASS_STORAGE_LIST_NAME, storageList)
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
local function take_demanded_items_from_network(pos, network)
|
||||
local inventories = get_valid_demander_and_target_inventory(pos)
|
||||
if not inventories then return true end
|
||||
for _, filterStack in pairs(inventories.demanderInventory:get_list("filter")) do
|
||||
local demandStack =
|
||||
get_actual_demand_for_item(filterStack, inventories.targetInventory, inventories.targetList)
|
||||
local filterStackName = demandStack:get_name()
|
||||
local isTool = demandStack:get_stack_max() <= 1
|
||||
if isTool then
|
||||
try_to_fulfil_demanded_item_from_network_item_storage(pos, demandStack, network, inventories)
|
||||
else -- check chaced mass-storage
|
||||
local locations = network.storage_cache[filterStackName]
|
||||
if not locations then return true end
|
||||
try_to_fulfil_demanded_item_from_locations(pos, demandStack, locations, inventories)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
----------------------------------------------------------------
|
||||
-- Storage operation functions
|
||||
----------------------------------------------------------------
|
||||
|
||||
function logistica.start_demander_timer(pos, duration)
|
||||
if duration == nil then duration = TIMER_DURATION_SHORT end
|
||||
logistica.start_node_timer(pos, duration)
|
||||
end
|
||||
|
||||
function logistica.on_demander_timer(pos, elapsed)
|
||||
local network = logistica.get_network_or_nil(pos)
|
||||
if not network then
|
||||
return false -- this will get restarted once the demander is added to a network
|
||||
end
|
||||
take_demanded_items_from_network(pos, network)
|
||||
return true
|
||||
end
|
||||
|
||||
function logistica.set_demander_target_list(pos, listName)
|
||||
local meta = get_meta(pos)
|
||||
meta:set_string(META_DEMANDER_LISTNAME, listName)
|
||||
end
|
||||
|
||||
function logistica.get_demander_target_list(pos)
|
||||
local meta = get_meta(pos)
|
||||
return meta:get_string(META_DEMANDER_LISTNAME)
|
||||
end
|
||||
|
||||
-- function logistica.update_demander_demand(demanderPos)
|
||||
-- local meta = get_meta(demanderPos)
|
||||
-- local inventories = get_valid_demander_and_target_inventory(demanderPos)
|
||||
-- if not inventories then return end
|
||||
-- local demandList = logistica.get_demand_based_on_list(
|
||||
-- inventories.demanderInventory, "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)
|
||||
local inv = get_meta(pos):get_inventory()
|
||||
local list = inv:get_list("filter")
|
||||
if not list then return {} end
|
||||
local ret = {}
|
||||
for k, v in list do
|
||||
ret[k] = ItemStack(v)
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
-- returns the demander's target position or nil if the demander isn't loaded
|
||||
function logistica.get_demander_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)
|
||||
if not itemstack or itemstack:is_empty() then return 0 end
|
||||
|
||||
local inventories = get_valid_demander_and_target_inventory(demanderPos)
|
||||
if not inventories then return itemstack:get_count() end
|
||||
local targetInventory = inventories.targetInventory
|
||||
local targetList = inventories.targetList
|
||||
|
||||
local leftover = targetInventory:add_item(targetList, itemstack)
|
||||
local targetNode = minetest.get_node(inventories.targetPos)
|
||||
if leftover:get_count() < itemstack:get_count() and TARGET_NODES_REQUIRING_TIMER[targetNode.name] then
|
||||
logistica.start_node_timer(inventories.targetPos, 1)
|
||||
end
|
||||
return leftover:get_count()
|
||||
end
|
||||
|
49
logic/inv_list_picker.lua
Normal file
49
logic/inv_list_picker.lua
Normal file
@ -0,0 +1,49 @@
|
||||
local ALLOWED_PULL_LISTS = {}
|
||||
ALLOWED_PULL_LISTS["main"] = true
|
||||
ALLOWED_PULL_LISTS["src"] = true
|
||||
ALLOWED_PULL_LISTS["dst"] = true
|
||||
ALLOWED_PULL_LISTS["output"] = true
|
||||
ALLOWED_PULL_LISTS["fuel"] = true
|
||||
|
||||
local ALLOWED_PUSH_LISTS = {}
|
||||
ALLOWED_PUSH_LISTS["main"] = true
|
||||
ALLOWED_PUSH_LISTS["src"] = true
|
||||
ALLOWED_PUSH_LISTS["fuel"] = true
|
||||
ALLOWED_PUSH_LISTS["input"] = true
|
||||
ALLOWED_PUSH_LISTS["shift"] = true
|
||||
|
||||
-- returns a string of comma separated lists we're allowed to push to at the given pushToPos
|
||||
local function get_lists(pushToPos, allowedLists)
|
||||
logistica.load_position(pushToPos)
|
||||
local availableLists = minetest.get_meta(pushToPos):get_inventory():get_lists()
|
||||
local pushLists = {}
|
||||
for name, _ in pairs(availableLists) do
|
||||
if allowedLists[name] then
|
||||
table.insert(pushLists, name)
|
||||
end
|
||||
end
|
||||
return pushLists
|
||||
end
|
||||
|
||||
local function list_dropdown(name, itemTable, x, y, default)
|
||||
local defaultIndex = 0
|
||||
for i, v in ipairs(itemTable) do if default == v then defaultIndex = i end end
|
||||
local items = table.concat(itemTable, ",")
|
||||
return "dropdown["..x..","..y..";2,0.6;"..name..";"..items..";"..defaultIndex..";false]"
|
||||
end
|
||||
|
||||
function logistica.get_pull_list_dropdown(name, x, y, pullFromPos, default)
|
||||
return list_dropdown(name, get_lists(pullFromPos, ALLOWED_PULL_LISTS), x, y, default)
|
||||
end
|
||||
|
||||
function logistica.get_push_list_dropdown(name, x, y, pushToPos, default)
|
||||
return list_dropdown(name, get_lists(pushToPos, ALLOWED_PUSH_LISTS), x, y, default)
|
||||
end
|
||||
|
||||
function logistica.is_allowed_pull_list(listName)
|
||||
return ALLOWED_PULL_LISTS[listName] == true
|
||||
end
|
||||
|
||||
function logistica.is_allowed_push_list(listName)
|
||||
return ALLOWED_PUSH_LISTS[listName] == true
|
||||
end
|
@ -1,7 +1,7 @@
|
||||
local path = logistica.MODPATH .. "/logic"
|
||||
dofile(path .. "/processing_queue.lua")
|
||||
dofile(path .. "/inv_list_picker.lua")
|
||||
dofile(path .. "/groups.lua")
|
||||
dofile(path .. "/push_pull.lua")
|
||||
dofile(path .. "/network_logic.lua")
|
||||
dofile(path .. "/controller.lua")
|
||||
dofile(path .. "/storage.lua")
|
||||
|
@ -50,6 +50,12 @@ function logistica.get_network_id_or_nil(pos)
|
||||
if not network then return nil else return network.controller end
|
||||
end
|
||||
|
||||
local function notify_connected(pos, node, networkId)
|
||||
local def = minetest.registered_nodes[node.name]
|
||||
if def and def.logistica and def.logistica.on_connect_to_network then
|
||||
def.logistica.on_connect_to_network(pos, networkId)
|
||||
end
|
||||
end
|
||||
|
||||
----------------------------------------------------------------
|
||||
-- Network operation functions
|
||||
@ -86,7 +92,7 @@ local function find_adjecent_networks(pos)
|
||||
end
|
||||
local retNetworks = {}
|
||||
for k,_ in pairs(connectedNetworks) do
|
||||
table.insert(networks[k])
|
||||
table.insert(retNetworks, networks[k])
|
||||
end
|
||||
return retNetworks
|
||||
end
|
||||
@ -149,22 +155,27 @@ local function recursive_scan_for_nodes_for_controller(network, positionHashes,
|
||||
if existingNetwork ~= nil and existingNetwork ~= network then
|
||||
return CREATE_NETWORK_STATUS_FAIL_OTHER_NETWORK
|
||||
end
|
||||
local valid = false
|
||||
if logistica.is_cable(otherNode.name) then
|
||||
network.cables[otherHash] = true
|
||||
connections[otherHash] = true
|
||||
newToScan = newToScan + 1
|
||||
valid = true
|
||||
elseif logistica.is_demander(otherNode.name) then
|
||||
network.demanders[otherHash] = true
|
||||
newToScan = newToScan + 1
|
||||
valid = true
|
||||
elseif logistica.is_supplier(otherNode.name) then
|
||||
network.suppliers[otherHash] = true
|
||||
newToScan = newToScan + 1
|
||||
valid = true
|
||||
elseif logistica.is_mass_storage(otherNode.name) then
|
||||
network.mass_storage[otherHash] = true
|
||||
newToScan = newToScan + 1
|
||||
valid = true
|
||||
elseif logistica.is_item_storage(otherNode.name) then
|
||||
network.item_storage[otherHash] = true
|
||||
valid = true
|
||||
end
|
||||
if valid then
|
||||
newToScan = newToScan + 1
|
||||
notify_connected(otherPos, otherNode, network.controller)
|
||||
end
|
||||
end -- end if tiersMatch
|
||||
end -- end of general checks
|
||||
@ -310,7 +321,7 @@ function logistica.on_cable_change(pos, oldNode)
|
||||
else
|
||||
local otherNetwork = logistica.get_network_or_nil(connections[1])
|
||||
if otherNetwork then
|
||||
otherNetwork.cables[p2h(connections[1])] = true
|
||||
otherNetwork.cables[p2h(pos)] = true
|
||||
end
|
||||
end
|
||||
return -- was a network end, no need to do anything else
|
||||
|
@ -1,6 +1,11 @@
|
||||
|
||||
logistica.proq = {}
|
||||
|
||||
local function get_meta(pos)
|
||||
logistica.load_position(pos)
|
||||
return minetest.get_meta(pos)
|
||||
end
|
||||
|
||||
local QUEUE_KEY = "log_proq"
|
||||
local DELIM = "|"
|
||||
|
||||
@ -16,7 +21,7 @@ end
|
||||
|
||||
-- listOfPositions must be a list (naturally numbered table) of position vectors
|
||||
function logistica.proq.add(pos, listOfPositions)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local meta = get_meta(pos)
|
||||
local positions = logistica.proq.get_all(pos)
|
||||
for _, v in ipairs(listOfPositions) do
|
||||
table.insert(positions, v)
|
||||
@ -26,7 +31,7 @@ end
|
||||
|
||||
-- returns a table of up to the next N positions
|
||||
function logistica.proq.pop_next(pos, count)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local meta = get_meta(pos)
|
||||
local positions = logistica.proq.get_all(pos)
|
||||
local ret = {}
|
||||
local rem = {}
|
||||
@ -42,7 +47,7 @@ function logistica.proq.pop_next(pos, count)
|
||||
end
|
||||
|
||||
function logistica.proq.get_all(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local meta = get_meta(pos)
|
||||
if not meta:contains(QUEUE_KEY) then return {} end
|
||||
local compressedString = meta:get_string(QUEUE_KEY)
|
||||
local positionStrings = string.split(compressedString, DELIM, false)
|
||||
|
@ -1,126 +0,0 @@
|
||||
local M_SCAN_POS = "logistica_scanpos"
|
||||
|
||||
-- attempts to take 1 item from the targetMeta, rotating over all slots
|
||||
function logistica.get_item(pullerMeta, targetMeta, listname)
|
||||
if targetMeta == nil or targetMeta.get_inventory == nil then return nil end
|
||||
local inv = targetMeta:get_inventory()
|
||||
if inv:is_empty(listname) then
|
||||
return nil
|
||||
end
|
||||
local size = inv:get_size(listname)
|
||||
local startpos = pullerMeta:get_int(M_SCAN_POS) or 0
|
||||
for i = startpos, startpos + size do
|
||||
i = (i % size) + 1
|
||||
local items = inv:get_stack(listname, inv)
|
||||
if items:get_count() > 0 then
|
||||
local taken = items:take_item(1)
|
||||
inv:set_stack(listname, i, items)
|
||||
pullerMeta:set_int(M_SCAN_POS, i)
|
||||
return taken
|
||||
end
|
||||
end
|
||||
pullerMeta:set_int("tubelib_startpos", 0)
|
||||
return nil
|
||||
end
|
||||
|
||||
function logistica.get_specific_item(meta, listname, slotNumber, numItems)
|
||||
if meta == nil or meta.get_inventory == nil then return nil end
|
||||
local inv = meta:get_inventory()
|
||||
if inv:is_empty(listname) then
|
||||
return nil
|
||||
end
|
||||
|
||||
if numItems == nil then numItems = 1 end
|
||||
local items = inv:get_stack(listname, slotNumber)
|
||||
if items:get_count() > 0 then
|
||||
local taken = items:take_item(numItems)
|
||||
inv:set_stack(listname, slotNumber, items)
|
||||
return taken
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
|
||||
-- Try to put item in list, returns false if failed, true otherwise
|
||||
function logistica.put_item(meta, listname, item)
|
||||
if meta == nil or meta.get_inventory == nil then return false end
|
||||
local inv = meta:get_inventory()
|
||||
if inv:room_for_item(listname, item) then
|
||||
inv:add_item(listname, item)
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- Take the number of items from the given ItemList.
|
||||
-- Returns nil if the requested number is not available.
|
||||
function logistica.get_num_items(meta, listname, num)
|
||||
if meta == nil or meta.get_inventory == nil then return nil end
|
||||
local inv = meta:get_inventory()
|
||||
if inv:is_empty(listname) then
|
||||
return nil
|
||||
end
|
||||
local size = inv:get_size(listname)
|
||||
for idx = 1, size do
|
||||
local items = inv:get_stack(listname, idx)
|
||||
if items:get_count() >= num then
|
||||
local taken = items:take_item(num)
|
||||
inv:set_stack(listname, idx, items)
|
||||
return taken
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
function logistica.get_stack(pullerMeta, targetMeta, listname)
|
||||
local inv = targetMeta:get_inventory()
|
||||
local item = logistica.get_item(pullerMeta, targetMeta, listname)
|
||||
if item and item:get_stack_max() > 1 and inv:contains_item(listname, item) then
|
||||
-- try to remove a complete stack
|
||||
item:set_count(math.min(98, item:get_stack_max() - 1))
|
||||
local taken = inv:remove_item(listname, item)
|
||||
-- add the already removed
|
||||
taken:set_count(taken:get_count() + 1)
|
||||
return taken
|
||||
end
|
||||
return item
|
||||
end
|
||||
|
||||
-- Return "full", "loaded", or "empty" depending
|
||||
-- on the number of fuel stack items.
|
||||
-- Function only works on fuel inventories with one stacks/99 items
|
||||
function logistica.fuelstate(meta, listname, item)
|
||||
if meta == nil or meta.get_inventory == nil then return nil end
|
||||
local inv = meta:get_inventory()
|
||||
if inv:is_empty(listname) then
|
||||
return "empty"
|
||||
end
|
||||
local list = inv:get_list(listname)
|
||||
if #list == 1 and list[1]:get_count() == 99 then
|
||||
return "full"
|
||||
else
|
||||
return "loaded"
|
||||
end
|
||||
end
|
||||
|
||||
-- Return "full", "loaded", or "empty" depending
|
||||
-- on the inventory load.
|
||||
-- Full is returned, when no empty stack is available.
|
||||
function logistica.get_inv_state(meta, listname)
|
||||
if meta == nil or meta.get_inventory == nil then return nil end
|
||||
local inv = meta:get_inventory()
|
||||
local state
|
||||
if inv:is_empty(listname) then
|
||||
state = "empty"
|
||||
else
|
||||
local list = inv:get_list(listname)
|
||||
state = "full"
|
||||
local num = 0
|
||||
for i, item in ipairs(list) do
|
||||
if item:is_empty() then
|
||||
return "loaded"
|
||||
end
|
||||
end
|
||||
end
|
||||
return state
|
||||
end
|
@ -26,6 +26,7 @@ function logistica.try_to_add_item_to_storage(pos, inputStack, dryRun)
|
||||
local node = minetest.get_node(pos)
|
||||
if not logistica.is_mass_storage(node.name) and not logistica.is_item_storage(node.name) then return 0 end
|
||||
local isMassStorage = string.find(node.name, "mass")
|
||||
logistica.load_position(pos)
|
||||
local inv = minetest.get_meta(pos):get_inventory()
|
||||
if isMassStorage then
|
||||
local maxItems = logistica.get_mass_storage_max_size(pos)
|
||||
|
BIN
textures/logistica_basic_demander_back.png
Normal file
BIN
textures/logistica_basic_demander_back.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.1 KiB |
BIN
textures/logistica_basic_demander_front.png
Normal file
BIN
textures/logistica_basic_demander_front.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.3 KiB |
BIN
textures/logistica_basic_demander_side.png
Normal file
BIN
textures/logistica_basic_demander_side.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.3 KiB |
BIN
textures/logistica_entity_input.png
Normal file
BIN
textures/logistica_entity_input.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 201 B |
BIN
textures/logistica_entity_output.png
Normal file
BIN
textures/logistica_entity_output.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 221 B |
@ -11,9 +11,30 @@ minetest.register_craftitem("logistica:network_tool",{
|
||||
if not node or node.name:find("logistica:") == nil then return end
|
||||
local network = logistica.get_network_name_or_nil(pos) or "<NONE>"
|
||||
-- minetest.chat_send_player(placer:get_player_name(), "Network: "..network)
|
||||
logistica.show_short_popup(
|
||||
logistica.show_popup(
|
||||
placer:get_player_name(),
|
||||
"("..pos.x..","..pos.y..","..pos.z..") Network: "..network
|
||||
)
|
||||
end
|
||||
})
|
||||
|
||||
|
||||
minetest.register_craftitem("logistica:wand",{
|
||||
description = "Inv List Scanner",
|
||||
inventory_image = "logistica_wand.png",
|
||||
wield_image = "logistica_wand.png",
|
||||
stack_max = 1,
|
||||
on_place = function(itemstack, placer, pointed_thing)
|
||||
local pos = pointed_thing.under
|
||||
if not placer or not pos then return end
|
||||
local inv = minetest.get_meta(pos):get_inventory()
|
||||
|
||||
local lists = inv:get_lists()
|
||||
local names = ""
|
||||
for name, _ in pairs(lists) do
|
||||
names = names..name..", "
|
||||
end
|
||||
|
||||
logistica.show_popup(placer:get_player_name(), names)
|
||||
end
|
||||
})
|
@ -3,13 +3,13 @@
|
||||
local charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
local function rand_str(length, seed)
|
||||
math.randomseed(seed)
|
||||
local ret = {}
|
||||
local r
|
||||
for i = 1, length do
|
||||
r = math.random(1, #charset)
|
||||
table.insert(ret, charset:sub(r, r))
|
||||
end
|
||||
return table.concat(ret)
|
||||
local ret = {}
|
||||
local r
|
||||
for i = 1, length do
|
||||
r = math.random(1, #charset)
|
||||
table.insert(ret, charset:sub(r, r))
|
||||
end
|
||||
return table.concat(ret)
|
||||
end
|
||||
|
||||
----------------------------------------------------------------
|
||||
@ -17,13 +17,13 @@ end
|
||||
----------------------------------------------------------------
|
||||
|
||||
function logistica.load_position(pos)
|
||||
if pos.x < -30912 or pos.y < -30912 or pos.z < -30912 or
|
||||
pos.x > 30927 or pos.y > 30927 or pos.z > 30927 then return end
|
||||
if minetest.get_node_or_nil(pos) then
|
||||
return
|
||||
end
|
||||
local vm = minetest.get_voxel_manip()
|
||||
vm:read_from_map(pos, pos)
|
||||
if pos.x < -30912 or pos.y < -30912 or pos.z < -30912 or
|
||||
pos.x > 30927 or pos.y > 30927 or pos.z > 30927 then return end
|
||||
if minetest.get_node_or_nil(pos) then
|
||||
return
|
||||
end
|
||||
local vm = minetest.get_voxel_manip()
|
||||
vm:read_from_map(pos, pos)
|
||||
end
|
||||
|
||||
function logistica.swap_node(pos, newName)
|
||||
@ -79,4 +79,12 @@ function logistica.clamp(v, min, max)
|
||||
if v < min then return min end
|
||||
if v > max then return max end
|
||||
return v
|
||||
end
|
||||
|
||||
function logistica.start_node_timer(pos, time)
|
||||
local timer = minetest.get_node_timer(pos)
|
||||
if not timer:is_started() then
|
||||
timer:start(time)
|
||||
end
|
||||
return timer
|
||||
end
|
@ -1,7 +1,11 @@
|
||||
|
||||
local playerHud = {}
|
||||
|
||||
function logistica.show_short_popup(playerName, text)
|
||||
local SHORT_TIME = 3
|
||||
local LONG_TIME = 10
|
||||
|
||||
function logistica.show_popup(playerName, text, time)
|
||||
if not time then time = SHORT_TIME end
|
||||
local player = minetest.get_player_by_name(playerName)
|
||||
if not player then return end
|
||||
|
||||
@ -21,7 +25,7 @@ function logistica.show_short_popup(playerName, text)
|
||||
})
|
||||
playerHud[playerName] = {}
|
||||
playerHud[playerName].hudId = hudId
|
||||
local job = minetest.after(3, function()
|
||||
local job = minetest.after(time, function()
|
||||
local pl = minetest.get_player_by_name(playerName)
|
||||
if not pl then return end
|
||||
if not playerHud[playerName] then return end
|
||||
|
Loading…
x
Reference in New Issue
Block a user