I would like to make some neat logistics items:
- Neat requester chest:
When placed it searches for assembling machines in 3x3 radius. For all assembling machines found it takes the ingredients for their recipes needed and sets these items as filter to request.
I already have this code:
require "defines"
require "basic-lua-extensions"
game.on_event(defines.events.on_built_entity, function(event)
if event.created_entity.name == "logistic-chest-requester-neat" then
debug(serpent.block(event))
end
end)
Can you help me how to set the requested items for this chest ? I'm a programmer but I don't know the functions which are available to do this.
if found == 0 then
slotThere = 0
for slot = 1, 10 do
slotInfo = supplier.get_request_slot(slot)
if slotInfo ~= nil then
if slotInfo["name"] == name and slotThere == 0 then
slotThere = 1
end
elseif slotInfo == nil then
--Set where last empty slot was found!
emptySlot = slot
end
end
if slotThere == 0 then
supplier.set_request_slot({name = name, count = need}, emptySlot)
slotThere = 1
end
end
PS just change supplier with the selected entity.
Now here is the part im not so good with:
game.player.print(game.player.selected.recipe.name) -- prints the recipe of the selected assembling machine/furnace
From https://forums.factorio.com/wiki/inde ... Lua/Recipe
Other than that you can do this to check what the ingredients of an entity is using the:
which is a table of the ingredients
so if you select an assembler and selects position one in the ingredient table and then the name you can do it like so:
game.on_event(defines.events.on_built_entity, function(event)
local entity = event.created_entity
local player = game.players[event.player_index]
if entity.name == "logistic-chest-requester-neat" or entity.name=="logistic-chest-requester" then
local surface = entity.surface
local x = entity.position.x
local y = entity.position.y
local assemblers = surface.find_entities_filtered{area = {{x-ros,y-ros},{x+ros,y+ros}},type="assembling-machine"}
local filterForChest = {}
--check all assemblers and add up ingredients to use as filter in the chest
for _,assembler in ipairs(assemblers) do
-- check that the assembler is really only $ros tiles away
local dist = math.abs(assembler.position.x - x) + math.abs(assembler.position.y - y)
if dist <= ros and assembler.recipe ~= nil then
local items = assembler.recipe.ingredients
for _,itemDescription in ipairs(items) do
if itemDescription["type"]=="item" then
local itemName = itemDescription["name"]
if not filterForChest[itemName] then
filterForChest[itemName] = 0
end
filterForChest[itemName] = filterForChest[itemName] + itemDescription["amount"]*multiplier
end
end
end
end
--set the filter slots on the requester chest:
local slot = 1
for itemName,amount in pairs(filterForChest) do
local itemStack = {name = itemName, count = amount}
function setRequestSlot()
entity.set_request_slot(itemStack, slot)
end
local status,err = pcall( setRequestSlot )
if err ~=nil then --catch "Passed index out of range" exception if too many items should be set as filter
player.print("Too few filter slots for (Item: "..amount.."x "..itemName..")")
break
end
slot = slot + 1
end
end
end)
Now another question arose out of this:
I created a new item "logistic-chest-requester-neat" but actually I want an entity for it as well. My idea is to just reuse the standard requester chest entity.
So right now I set in the items.lua:
But obviously then in the "game.on_event(defines.events.on_built_entity," I only get events when the standard chest is built.
Now in the wiki I found the method "on_put_item - Called when players uses item to build something. Called before on_built_entity."
How would you connect those to only execute my function, when the modded chest is placed? Is it save to use a global variable? Or is it better to define my custom entity (the same as the default chest) ?
Thank you for your help
Re: neat requester chest
Posted: Fri Nov 13, 2015 3:31 pm
by SirRichie
Not sure if this might be the wrong question to ask, but why do you place a different entity?
What you would typcially do is create an entity (see the modding tutorial on how to do that in principle) that is based on the chest but has a different name.
Where can I find your tutorial? Is this a good approach I took ?
Cheers
Re: neat requester chest
Posted: Tue Dec 01, 2015 6:12 am
by SirRichie
It is not my tutorial, it is in the wiki: https://forums.factorio.com/wiki/inde ... g_Tutorial
The tutorial is not up to date regarding the modding API, but it is great for getting the idea (focus on Chapter 4).