public void PowerNetUpdate(PowerNet net, HashSet <Building_BackupPowerAttachment> brokers) { // get desired power var users = net.powerComps .Select(p => (comp: p, broker: p.parent is Building building ? brokers.FirstOrDefault(b => b.Parent == building) : null, consumption: Consumption(p), currentProduction: CurrentProduction(p), potentialProduction: PotentialProduction(p))); var need = users.Sum(u => u.consumption); var production = users.Sum(u => u.currentProduction); var hasStorage = net.HasStorage(); var storageLevel = net.StorageLevel(); // Log.Debug( $"need: {need}, production: {production}, static: {staticProduction}" ); if (production > need || hasStorage && storageLevel > 0) { // try to shut backups off var overProduction = production - need; var backups = users.Where(u => u.broker != null && u.currentProduction > 0 && u.currentProduction <= overProduction && (!hasStorage || storageLevel >= u.broker.batteryRange.max) && u.broker.CanTurnOff()) .ToList(); if (backups.TryRandomElementByWeight(c => 1 / c.currentProduction, out var backup)) { backup.broker.TurnOff(); } } if (production < need || hasStorage && storageLevel < 1) { // try to turn backups on var backups = users.Where(u => u.broker != null && Math.Abs(u.currentProduction) < Mathf.Epsilon && u.potentialProduction > 0 && (!hasStorage || storageLevel <= u.broker.batteryRange.min)) .ToList(); if (backups.TryRandomElementByWeight(c => c.potentialProduction, out var backup)) { backup.broker.TurnOn(); } } }
public void PowerNetUpdate(PowerNet net, HashSet <Building_BackupPowerAttachment> brokers) { // get desired power IEnumerable <(CompPowerTrader comp, Building_BackupPowerAttachment broker, float consumption, float currentProduction, float potentialProduction)> users = net.powerComps .Select(p => (comp: p, broker: p.parent is Building building ? brokers.FirstOrDefault(b => b.Parent == building) : null, consumption: Consumption(p), currentProduction: CurrentProduction(p), potentialProduction: PotentialProduction(p))); float need = users.Sum(u => u.consumption); float production = users.Sum(u => u.currentProduction); bool hasStorage = net.HasStorage(); float storageLevel = net.StorageLevel(); // Log.Debug( $"need: {need}, production: {production}, static: {staticProduction}" ); if (production > need || (hasStorage && storageLevel > 0)) { // try to shut backups off List <(CompPowerTrader comp, Building_BackupPowerAttachment broker, float consumption, float currentProduction, float potentialProduction)> backups = users.Where(u => u.broker != null && u.currentProduction > 0 && (u.currentProduction <= (production - need) || u.broker.runOnBatteriesOnly) && ((!hasStorage && !u.broker.runOnBatteriesOnly) || storageLevel >= u.broker.batteryRange.max) && u.broker.CanTurnOff()) .ToList(); if (backups.TryRandomElementByWeight(c => 1 / c.currentProduction, out (CompPowerTrader comp, Building_BackupPowerAttachment broker, float consumption, float currentProduction, float potentialProduction)backup)) { backup.broker.TurnOff(); } } if (production < need || (hasStorage && storageLevel < 1)) { // try to turn backups on List <(CompPowerTrader comp, Building_BackupPowerAttachment broker, float consumption, float currentProduction, float potentialProduction)> backups = users.Where(u => u.broker != null && Math.Abs(u.currentProduction) < Mathf.Epsilon && u.potentialProduction > 0 && (!hasStorage || storageLevel <= u.broker.batteryRange.min)) .ToList(); if (backups.TryRandomElementByWeight(c => c.potentialProduction, out (CompPowerTrader comp, Building_BackupPowerAttachment broker, float consumption, float currentProduction, float potentialProduction)backup)) { backup.broker.TurnOn(); } } }