@@ -25,6 +25,7 @@ type GoalChange = {
2525export function factoryBalanceStep (
2626 blocks : BlockWithFlows [ ] ,
2727 demandOverrides : Record < string , number > = { } ,
28+ sinkBaselines : Map < string , number > = new Map ( ) ,
2829) {
2930 const kindOf = new Map < string , string > ( ) ;
3031 const produced = new Map < string , number > ( ) ;
@@ -157,11 +158,18 @@ export function factoryBalanceStep(
157158 for ( const [ good , entries ] of sinks ) {
158159 if ( producers . has ( good ) ) continue ;
159160 const available = incidental . get ( good ) ?? 0 ;
160- if ( available <= EPS ) continue ;
161- const current = entries . reduce ( ( sum , entry ) => sum + entry . rate , 0 ) ;
161+ const baseline = entries . map ( ( entry ) =>
162+ Math . abs ( sinkBaselines . get ( `${ entry . block . id } \u0000${ good } ` ) ?? - entry . rate ) ,
163+ ) ;
164+ const baselineTotal = baseline . reduce ( ( sum , rate ) => sum + rate , 0 ) ;
165+ const required = Math . max ( available , baselineTotal ) ;
162166 entries . forEach ( ( entry , index ) => {
163167 const allocation =
164- current > EPS ? available * ( entry . rate / current ) : index === 0 ? available : 0 ;
168+ baselineTotal > EPS
169+ ? required * ( baseline [ index ] ! / baselineTotal )
170+ : index === 0
171+ ? required
172+ : 0 ;
165173 addChange ( entry . block , good , - allocation ) ;
166174 } ) ;
167175 }
0 commit comments