Measuring belt througput in lua.

Place to get help with not working mods / modding interface.
Post Reply
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Measuring belt througput in lua.

Post by eradicator »

So after a recent discussion about train loading i thought it'd be nice to be able to measure belt throughput without elaborate circuit contraptions. I had an idea, but failed. So... does anybody have an idea how to do it?

(For reference the failed idea was to track LuaItemStack as they enter and leave the belt. But from what i can tell they never do this and the same belt segment always has the same stacks. So i'm assuming they just change content, which isn't detectable on a fully compressed belt.)

Edit: More precisely i want to measure
  1. any belt (bonus points if it works on belts that already have circuitry attached)
  2. without any visible change to the belt (invisible changes are fine)
  3. preferably purely in control.lua stage
  4. performance is not a concern
Last edited by eradicator on Mon Sep 16, 2019 10:31 am, edited 3 times in total.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Measuring belt througput in lua.

Post by DaveMcW »

There are no events that could help you. So you are stuck with doing everything in on_tick.

You can port the circuit contraptions to lua I guess?
Last edited by DaveMcW on Mon Sep 16, 2019 10:04 am, edited 2 times in total.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Measuring belt througput in lua.

Post by eradicator »

DaveMcW wrote:
Mon Sep 16, 2019 9:58 am
There are no events that could help you. So you are stuck with doing everything in on_tick.

You can port the circuit contraptions to lua I guess?
On_tick is obvious, the problem is that there is no API for getting any sort of speed data at all. The contraptions work by wiring up a belt and setting it to pulse mode. But there is no API equivalent to a pulse. So if there even *is* a solution i think it'll have to do some pretty obscure voodoo.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Measuring belt througput in lua.

Post by DaveMcW »

The most efficient way seems to be connecting a wire and checking the circuit network every tick.

So only the combinators are replaced by lua. At that point, is your lua more elaborate than the combinators?

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Measuring belt througput in lua.

Post by eradicator »

DaveMcW wrote:
Mon Sep 16, 2019 10:05 am
The most efficient way seems to be connecting a wire and checking the circuit network every tick.
That is measuring the circuit network, not the belt. I know how to do that already. I'm looking for a magic way to calculate/interpolate/guess the throughput of a belt without changing the state of the belt.

Reason: Proof of concept. I just want to know if it's possible.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Measuring belt througput in lua.

Post by darkfrei »

https://lua-api.factorio.com/latest/Lua ... tLine.html

What the different between moving items and not moving items / not full speed moving items?

You can place in the middle the belt with higher tier and calculate how many items is on it. On the moving line you has less items than on the stopped one.

You can place to the belt one special item and check the position of it.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Measuring belt througput in lua.

Post by eradicator »

darkfrei wrote:
Mon Sep 16, 2019 10:33 am
What the different between moving items and not moving items / not full speed moving items?
I think they're the same.
darkfrei wrote:
Mon Sep 16, 2019 10:33 am
You can place in the middle the belt with higher tier and calculate how many items is on it. On the moving line you has less items than on the stopped one.
A completely stopped belt has 8 items, a fully compressed belt at full speed also has 8 items.
darkfrei wrote:
Mon Sep 16, 2019 10:33 am
You can place to the belt one special item and check the position of it.
LuaStack does not have a "position".
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Measuring belt througput in lua.

Post by darkfrei »

The red belt after yellow belt is not full when it moves and full when the belt is stopped.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Measuring belt througput in lua.

Post by eradicator »

darkfrei wrote:
Mon Sep 16, 2019 10:59 am
The red belt after yellow belt is not full when it moves and full when the belt is stopped.
I don't see how that helps. Modded belts can have any maximum speed. And measurement has to work for any speed. If a blue belt has a throughput of 0.5i/s because the factory is stalled, then i still want to be able to measure that.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

mrvn
Smart Inserter
Smart Inserter
Posts: 5709
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Measuring belt througput in lua.

Post by mrvn »

Note that you can have quite a number of native combinators before the CPU cost approaches the cost of the LUA on_tick() call.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Measuring belt througput in lua.

Post by eradicator »

For the record:

I got it working by swapping the first stack of a transport belt with a "tracker" item, then measuring how long it takes to reach the last stack of the next belt piece. Thus getting a "pulse" equal to a circuit connected belt.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Post Reply

Return to “Modding help”