Page 1 of 1

Calculate inserter arm positions only when the GPU or a mod wants them

Posted: Tue Nov 05, 2019 4:50 pm
by quyxkh
Top lesson from the UPS Wars threads hasn't changed: inserter swings almost completely dominate production cost. I suspect it's because the engine updates the inserter arm position every tick, but why? There's no collision checks, arm position mid-swing is there for us humans to look at, right? So how about storing just the arrival tick and target point, plus swing and extension rates for each inserter swing?

Re: Calculate inserter arm positions only when the GPU or a mod wants them

Posted: Tue Nov 05, 2019 10:31 pm
by Koub
what if power is shut down during the swing ?

Re: Calculate inserter arm positions only when the GPU or a mod wants them

Posted: Tue Nov 05, 2019 10:38 pm
by quyxkh
Power-out's already an exceptional condition, right? Worst case, in a brownout you have to fall back to per-tick inserter updates, I'd think it's just whenever the power supply changes that you have to recalculate the swing.

Re: Calculate inserter arm positions only when the GPU or a mod wants them

Posted: Fri Nov 08, 2019 7:53 pm
by quyxkh
That 92% touch-count reduction kept my monkey chattering until it came up with this:

Swinging inserters go on a head-tail queue, simplest might be one for every connected-power-grids and delivered-joules-required combination. The list anchor has a what's-on-the-list summary and a count of how many joules have ever been delivered to every item on that list; queuing an inserter swing is just updating the summary and four pointers and power delivery is updating the summary then an unsigned-subtract-and-compare-until-done dequeue loop, no updating the list entries themselves required.

For example: a normal stack inserter swing is 13 ticks, 133kW max 1kW min, 1kW*13/60s is 216⅔J/tick drain, so the inserter needs 133000*13/60-216⅔*13/60=26000 delivered joules to complete its swing. Adding an inserter to the 26000J-swing queue means the list append, incrementing the count of inserters on that queue, adding the inserter's drain, and copying the current delivered-joules total plus 26000 to the target joules in the list entry. Power delivery means subtracting the list drain, dividing by the list count, adding to the delivery total, and dequeuing everything from the front that's been fully supplied, updating the list drain and count accordingly. Account for drain in 60ths of a joule and there's no rounding error; account for delivered and target joules as an unsigned and there's no overflow or resetting.

No-touch-until-done, unless the GPU or a mod needs the current position, then the current swing fraction is 1-(target-delivered)/required.

I've learned, when my monkey won't shut up there's something there. I promise, though, _I_ will. I'll just sign off with "have mercy on me".

Re: Calculate inserter arm positions only when the GPU or a mod wants them

Posted: Sat Nov 09, 2019 8:22 am
by ssilk
Interesting idea. Worth trying an implementation.

It would be really, really useful, if we have a power sensor, which has a signal, that rises when power is not 100% satisfied. So we would have a chance to turn power off completely (switch) and save much cpu.

Re: Calculate inserter arm positions only when the GPU or a mod wants them

Posted: Sat Nov 09, 2019 11:29 am
by steinio
Leads to calculate animations only when anybody is watching.
Out of screen entities and smoke could be frozen.

Re: Calculate inserter arm positions only when the GPU or a mod wants them

Posted: Sat Nov 09, 2019 12:23 pm
by Honktown
There's a mod that disables inserter animations. Have you seen that? OP? I wonder if it only disables animations, or completely interferes with their position awareness.

Re: Calculate inserter arm positions only when the GPU or a mod wants them

Posted: Sat Nov 09, 2019 2:13 pm
by eradicator
@quyxkh:

Inserters don't swing in a perfect arc though. They chase items on belts and sometimes wait for the source/target to become unstuck. Does that still work?

Re: Calculate inserter arm positions only when the GPU or a mod wants them

Posted: Sat Nov 09, 2019 2:29 pm
by quyxkh
eradicator wrote:
Sat Nov 09, 2019 2:13 pm
@quyxkh:

Inserters don't swing in a perfect arc though. They chase items on belts and sometimes wait for the source/target to become unstuck. Does that still work?
Worst case this only works for container-to-container swings, but I don't think inserters recalculate their target mid-swing, I think if they get to their target and there's nothing there they try again with a newly-calculated target.

Re: Calculate inserter arm positions only when the GPU or a mod wants them

Posted: Sat Nov 09, 2019 5:34 pm
by AngledLuffa
ssilk wrote:
Sat Nov 09, 2019 8:22 am
It would be really, really useful, if we have a power sensor, which has a signal, that rises when power is not 100% satisfied. So we would have a chance to turn power off completely (switch) and save much cpu.
This is actually quite easy to do. Have a counter circuit connected to a solar panel and accumulator which is separate from the rest of your power so that it is always fully powered. Then have another counter connected to your regular power source which might possibly have low power. If the power drops, the counter circuit will tick slower than the uninterrupted counter, and you can measure that difference. judos made a daytime detector based on this logic:

viewtopic.php?f=193&t=77046

I can think of two problems with the usage you suggested:

1) You need to prepare some other measurement for when to turn the power back on. Otherwise, your measurement device will flicker on and off. Perhaps you could make it like a circuit breaker, where it stays off until human intervention turns it back on.

2) Does this actually save CPU? My understanding was that unpowered devices can never sleep because they are constantly checking to see if power is back.

Re: Calculate inserter arm positions only when the GPU or a mod wants them

Posted: Sat Nov 09, 2019 8:26 pm
by Mylon
Koub wrote:
Tue Nov 05, 2019 10:31 pm
what if power is shut down during the swing ?
Make inserters work like laser turrets: Either they swing or they don't. They deduct the full power to complete their swing at their start.