mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 08:23:12 +09:00
Mod will now only use as many resources as it has been given
This commit is contained in:
1 parent
1f58458302
commit
e5366ac431
1 file changed
+64
-8
+64
-8
@@ -9,12 +9,10 @@ function OnBuiltEntity(event)
|
|||||||
--only add entities that are not ghosts
|
--only add entities that are not ghosts
|
||||||
if entity.type ~= "entity-ghost" then
|
if entity.type ~= "entity-ghost" then
|
||||||
if entity.name == INPUT_CHEST_NAME then
|
if entity.name == INPUT_CHEST_NAME then
|
||||||
--if the table hasn't been initialized then do it
|
|
||||||
global.inputChests = global.inputChests or {}
|
global.inputChests = global.inputChests or {}
|
||||||
--add the chests to a lists if these chests so they can be interated over
|
--add the chests to a lists if these chests so they can be interated over
|
||||||
global.inputChests[HashPosition(entity.position)] = entity
|
global.inputChests[HashPosition(entity.position)] = entity
|
||||||
elseif entity.name == OUTPUT_CHEST_NAME then
|
elseif entity.name == OUTPUT_CHEST_NAME then
|
||||||
--if the table hasn't been initialized then do it
|
|
||||||
global.outputChests = global.outputChests or {}
|
global.outputChests = global.outputChests or {}
|
||||||
--add the chests to a lists if these chests so they can be interated over
|
--add the chests to a lists if these chests so they can be interated over
|
||||||
global.outputChests[HashPosition(entity.position)] = entity
|
global.outputChests[HashPosition(entity.position)] = entity
|
||||||
@@ -36,6 +34,16 @@ end
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
--[[ Initialize Things ]]--
|
||||||
|
--script.on_configuration_changed(function(data)
|
||||||
|
--if the tables hasn't been initialized then do it
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
--end)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--[[ Thing Creation Events ]]--
|
--[[ Thing Creation Events ]]--
|
||||||
script.on_event(defines.events.on_built_entity, function(event)
|
script.on_event(defines.events.on_built_entity, function(event)
|
||||||
OnBuiltEntity(event)
|
OnBuiltEntity(event)
|
||||||
@@ -104,12 +112,23 @@ function HandleOutputChests()
|
|||||||
local itemsInChest = chestInventory.get_item_count(requestItem.name)
|
local itemsInChest = chestInventory.get_item_count(requestItem.name)
|
||||||
--if there isn't enough items in the chest
|
--if there isn't enough items in the chest
|
||||||
if itemsInChest < requestItem.count then
|
if itemsInChest < requestItem.count then
|
||||||
simpleItemStack.name = requestItem.name
|
local additionalItemRequiredCount = requestItem.count - itemsInChest
|
||||||
simpleItemStack.count = requestItem.count - itemsInChest
|
local itemCountAllowedToInsert = RequestItemsFromStorage(requestItem.name, additionalItemRequiredCount)
|
||||||
--insert the missing items
|
if itemCountAllowedToInsert > 0 then
|
||||||
local insertedItemsCount = chestInventory.insert(simpleItemStack)
|
simpleItemStack.name = requestItem.name
|
||||||
--write how many items was inserted
|
simpleItemStack.count = itemCountAllowedToInsert
|
||||||
linesToWriteToFile[#linesToWriteToFile + 1] = simpleItemStack.name..", "..insertedItemsCount.."\n"
|
--insert the missing items
|
||||||
|
local insertedItemsCount = chestInventory.insert(simpleItemStack)
|
||||||
|
local itemsNotInsertedCount = itemCountAllowedToInsert - insertedItemsCount
|
||||||
|
|
||||||
|
if itemsNotInsertedCount > 0 then
|
||||||
|
GiveItemsToStorage(requestItem.name, itemsNotInsertedCount)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
local missingItems = additionalItemRequiredCount - itemCountAllowedToInsert
|
||||||
|
--write how many items was inserted
|
||||||
|
linesToWriteToFile[#linesToWriteToFile + 1] = requestItem.name..", "..missingItems.."\n"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -123,6 +142,43 @@ function HandleOutputChests()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function RequestItemsFromStorage(itemName, itemCount)
|
||||||
|
global.itemStorage = global.itemStorage or {}
|
||||||
|
--if result is nil then there is no items in storage
|
||||||
|
--which means that no items can be given
|
||||||
|
if global.itemStorage[itemName] == nil then
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
--if the number of items in storage is lower than the number of items
|
||||||
|
--requested then take the number of items there are left otherwise take the requested amount
|
||||||
|
local itemsTakenFromStorage = math.min(global.itemStorage[itemName], itemCount)
|
||||||
|
global.itemStorage[itemName] = global.itemStorage[itemName] - itemsTakenFromStorage
|
||||||
|
|
||||||
|
print(itemsTakenFromStorage)
|
||||||
|
return itemsTakenFromStorage
|
||||||
|
end
|
||||||
|
|
||||||
|
function GiveItemsToStorage(itemName, itemCount)
|
||||||
|
global.itemStorage = global.itemStorage or {}
|
||||||
|
--if this is called for the first time for an item then the result
|
||||||
|
--is nil. if that's the case then set the result to 0 so it can
|
||||||
|
--be used in arithmetic operations
|
||||||
|
global.itemStorage[itemName] = global.itemStorage[itemName] or 0
|
||||||
|
global.itemStorage[itemName] = global.itemStorage[itemName] + itemCount
|
||||||
|
print(itemName.." "..itemCount)
|
||||||
|
print(itemName.." "..global.itemStorage[itemName])
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
--[[ Remote Thing ]]--
|
||||||
|
remote.add_interface("clusterio",
|
||||||
|
{
|
||||||
|
import = function(itemName, itemCount)
|
||||||
|
GiveItemsToStorage(itemName, itemCount)
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user