Page 1 of 1

Make chest require energy

Posted: Sat Jun 30, 2018 7:48 pm
by tat3rjr
How do you make a chest require energy to use?

For example have a chest that has a drain of 5kW, but if it does not have any power inserters can not use that chest.

Re: Make chest require energy

Posted: Sat Jun 30, 2018 11:03 pm
by DaveMcW
Not easily.

1. Create an electric-energy-source to drain 5kW.
2. Use a control.lua script to add/remove the drainer when a chest is built/destroyed.
3. Use a control.lua script to enable/disable the chest if the drainer is out of energy, firing every tick.

It's a lot of work to set up, and eats a lot of UPS to run.

Re: Make chest require energy

Posted: Sat Jun 30, 2018 11:57 pm
by eradicator
DaveMcW wrote: and eats a lot of UPS to run.
If the energy buffer is large enough you can check a lot less often, simply by calculating the minimum time it will be able to run off the buffer alone. Also i don't think you can "deactivate the chest", you'll have to deactivate the inserters.

Definetly not a beginners mod, unless you like a really diabolic challenge :).

Re: Make chest require energy

Posted: Sun Jul 01, 2018 3:18 am
by tat3rjr
DaveMcW wrote:Not easily.

1. Create an electric-energy-source to drain 5kW.
2. Use a control.lua script to add/remove the drainer when a chest is built/destroyed.
3. Use a control.lua script to enable/disable the chest if the drainer is out of energy, firing every tick.

It's a lot of work to set up, and eats a lot of UPS to run.
That seems complicated.

Re: Make chest require energy

Posted: Sun Jul 01, 2018 7:38 pm
by Rseding91
tat3rjr wrote:
DaveMcW wrote:Not easily.

1. Create an electric-energy-source to drain 5kW.
2. Use a control.lua script to add/remove the drainer when a chest is built/destroyed.
3. Use a control.lua script to enable/disable the chest if the drainer is out of energy, firing every tick.

It's a lot of work to set up, and eats a lot of UPS to run.
That seems complicated.
It is :P Chests are not meant to take power because that adds a ton of overhead to moving items around which happens frequently so if you want to make the game do it you have to use extreme workarounds.

Re: Make chest require energy

Posted: Mon Jul 02, 2018 8:52 am
by bobingabout
tat3rjr wrote:That seems complicated.
It is, and I would really suggest you not try and do it, however, the fact that someone can tell you the basic steps on how proves that it can be done if you really want it.

Re: Make chest require energy

Posted: Mon Jul 02, 2018 10:36 am
by darkfrei
https://lua-api.factorio.com/latest/events.html

Code: Select all

function on_built_entity OR on_robot_built_entity
  Place on this surface in this position new accumulator, that can take energy only
  Add this chest and this accumulator to the global.elements
end

Code: Select all

function on_tick
  if global.elements isn't empty then take the next element 
  if the accumulator in the element has power, then take some energy, if not, then find all inserters about it and disable them. Set flag disabled = true
  if it was disabled, but now we have power, then find all inserters and enable them. Set flag disabled = false
end

Code: Select all

function on_player_mined_entity OR on_robot_mined
  Find this element, delete accumulator, enable inserters, delete this element from global
end

Re: Make chest require energy

Posted: Mon Jul 02, 2018 10:50 am
by darkfrei
Performance optimization: if you have only one chest and it needs every tick 1 kJ, then it needs 60 kW power supply.
If you have two chests, then one tick will be calculated only one of them, take 2 kJ every
second tick and you have also 60 kW.
If you have 7 of them, then just take 7 kJ every 7th tick.

Just check that your accumulator is much bigger than the energy per tick. For 600 chests you are need at least 600 kJ accumulator, then you can calculate 2 chests per tick. Or just make bigger accumulator.