public void RegisterPowerNet(PowerNet newNet) { this.allNets.Add(newNet); newNet.powerNetManager = this; this.map.powerNetGrid.Notify_PowerNetCreated(newNet); PowerNetMaker.UpdateVisualLinkagesFor(newNet); }
public void Notify_PowerNetCreated(PowerNet newNet) { if (powerNetCells.ContainsKey(newNet)) { Log.Warning("Net " + newNet + " is already registered in PowerNetGrid."); powerNetCells.Remove(newNet); } List <IntVec3> list = new List <IntVec3>(); powerNetCells.Add(newNet, list); for (int i = 0; i < newNet.transmitters.Count; i++) { CellRect cellRect = newNet.transmitters[i].parent.OccupiedRect(); for (int j = cellRect.minZ; j <= cellRect.maxZ; j++) { for (int k = cellRect.minX; k <= cellRect.maxX; k++) { int num = map.cellIndices.CellToIndex(k, j); if (netGrid[num] != null) { Log.Warning("Two power nets on the same cell (" + k + ", " + j + "). First transmitters: " + newNet.transmitters[0].parent.LabelCap + " and " + ((!netGrid[num].transmitters.NullOrEmpty()) ? netGrid[num].transmitters[0].parent.LabelCap : "[none]") + "."); } netGrid[num] = newNet; list.Add(new IntVec3(k, 0, j)); } } } }
public void ConnectToTransmitter(CompPower transmitter, bool reconnectingAfterLoading = false) { if (this.connectParent != null && (!reconnectingAfterLoading || this.connectParent != transmitter)) { Log.Error(string.Concat(new object[] { "Tried to connect ", this, " to transmitter ", transmitter, " but it's already connected to ", this.connectParent, "." }), false); } else { this.connectParent = transmitter; if (this.connectParent.connectChildren == null) { this.connectParent.connectChildren = new List <CompPower>(); } transmitter.connectChildren.Add(this); PowerNet powerNet = this.PowerNet; if (powerNet != null) { powerNet.RegisterConnector(this); } } }
private void EnsureBatteriesConnectedAndMakeSense(Map map) { tmpThings.Clear(); tmpThings.AddRange(map.listerThings.ThingsInGroup(ThingRequestGroup.BuildingArtificial)); for (int i = 0; i < tmpThings.Count; i++) { CompPowerBattery compPowerBattery = tmpThings[i].TryGetComp <CompPowerBattery>(); if (compPowerBattery == null) { continue; } PowerNet powerNet = compPowerBattery.PowerNet; if (powerNet != null && HasAnyPowerGenerator(powerNet)) { continue; } map.powerNetManager.UpdatePowerNetsAndConnections_First(); Building newPowerGenerator2; if (TryFindClosestReachableNet(compPowerBattery.parent.Position, (PowerNet x) => HasAnyPowerGenerator(x), map, out PowerNet foundNet, out IntVec3 closestTransmitter)) { map.floodFiller.ReconstructLastFloodFillPath(closestTransmitter, tmpCells); if (canSpawnPowerGenerators) { int count = tmpCells.Count; if (Rand.Chance(Mathf.InverseLerp(MaxDistanceBetweenBatteryAndTransmitter.min, MaxDistanceBetweenBatteryAndTransmitter.max, count)) && TrySpawnPowerGeneratorNear(compPowerBattery.parent.Position, map, compPowerBattery.parent.Faction, out Building newPowerGenerator)) { SpawnTransmitters(compPowerBattery.parent.Position, newPowerGenerator.Position, map, compPowerBattery.parent.Faction); foundNet = null; } } if (foundNet != null) { SpawnTransmitters(tmpCells, map, compPowerBattery.parent.Faction); } }
public void Notify_PowerNetDeleted(PowerNet deadNet) { List <IntVec3> list = default(List <IntVec3>); if (!this.powerNetCells.TryGetValue(deadNet, out list)) { Log.Warning("Net " + deadNet + " does not exist in PowerNetGrid's dictionary."); } else { for (int i = 0; i < list.Count; i++) { int num = this.map.cellIndices.CellToIndex(list[i]); if (this.netGrid[num] == deadNet) { this.netGrid[num] = null; } else { Log.Warning("Multiple nets on the same cell " + list[i] + ". This is probably a result of an earlier error."); } } this.powerNetCells.Remove(deadNet); } }
public void DrawDebugPowerNetGrid() { if (!DebugViewSettings.drawPowerNetGrid) { return; } if (Current.ProgramState != ProgramState.Playing) { return; } if (this.map != Find.CurrentMap) { return; } Rand.PushState(); foreach (IntVec3 current in Find.CameraDriver.CurrentViewRect.ClipInsideMap(this.map)) { PowerNet powerNet = this.netGrid[this.map.cellIndices.CellToIndex(current)]; if (powerNet != null) { Rand.Seed = powerNet.GetHashCode(); CellRenderer.RenderCell(current, Rand.Value); } } Rand.PopState(); }
public virtual void ResetPowerVars() { transNet = null; connectParent = null; connectChildren = null; recentlyConnectedNets.Clear(); lastManualReconnector = null; }
public virtual void ResetPowerVars() { this.transNet = null; this.connectParent = null; this.connectChildren = null; CompPower.recentlyConnectedNets.Clear(); CompPower.lastManualReconnector = null; }
private void EnsurePowerUsersConnected(Map map) { this.tmpThings.Clear(); this.tmpThings.AddRange(map.listerThings.ThingsInGroup(ThingRequestGroup.BuildingArtificial)); for (int i = 0; i < this.tmpThings.Count; i++) { if (this.IsPowerUser(this.tmpThings[i])) { CompPowerTrader powerComp = this.tmpThings[i].TryGetComp <CompPowerTrader>(); PowerNet powerNet = powerComp.PowerNet; if (powerNet != null && powerNet.hasPowerSource) { this.TryTurnOnImmediately(powerComp, map); } else { map.powerNetManager.UpdatePowerNetsAndConnections_First(); PowerNet powerNet2; IntVec3 dest; if (this.TryFindClosestReachableNet(powerComp.parent.Position, (PowerNet x) => x.CurrentEnergyGainRate() - powerComp.Props.basePowerConsumption * CompPower.WattsToWattDaysPerTick > 1E-07f, map, out powerNet2, out dest)) { map.floodFiller.ReconstructLastFloodFillPath(dest, this.tmpCells); bool flag = false; if (this.canSpawnPowerGenerators && this.tmpThings[i] is Building_Turret && this.tmpCells.Count > 13) { flag = this.TrySpawnPowerGeneratorAndBatteryIfCanAndConnect(this.tmpThings[i], map); } if (!flag) { this.SpawnTransmitters(this.tmpCells, map, this.tmpThings[i].Faction); } this.TryTurnOnImmediately(powerComp, map); } else if (this.canSpawnPowerGenerators && this.TrySpawnPowerGeneratorAndBatteryIfCanAndConnect(this.tmpThings[i], map)) { this.TryTurnOnImmediately(powerComp, map); } else if (this.TryFindClosestReachableNet(powerComp.parent.Position, (PowerNet x) => x.CurrentStoredEnergy() > 1E-07f, map, out powerNet2, out dest)) { map.floodFiller.ReconstructLastFloodFillPath(dest, this.tmpCells); this.SpawnTransmitters(this.tmpCells, map, this.tmpThings[i].Faction); } else if (this.canSpawnBatteries) { Building building; if (this.TrySpawnBatteryNear(this.tmpThings[i].Position, map, this.tmpThings[i].Faction, out building)) { this.SpawnTransmitters(this.tmpThings[i].Position, building.Position, map, this.tmpThings[i].Faction); if (building.GetComp <CompPowerBattery>().StoredEnergy > 0f) { this.TryTurnOnImmediately(powerComp, map); } } } } } } }
public static void DoShortCircuit(Building culprit) { PowerNet powerNet = culprit.PowerComp.PowerNet; Map map = culprit.Map; float num = 0f; float num2 = 0f; bool flag = false; if (powerNet.batteryComps.Any((CompPowerBattery x) => x.StoredEnergy > 20f)) { ShortCircuitUtility.DrainBatteriesAndCauseExplosion(powerNet, culprit, out num, out num2); } else { flag = ShortCircuitUtility.TryStartFireNear(culprit); } string value; if (culprit.def == ThingDefOf.PowerConduit) { value = "AnElectricalConduit".Translate(); } else { value = Find.ActiveLanguageWorker.WithIndefiniteArticlePostProcessed(culprit.Label, false, false); } StringBuilder stringBuilder = new StringBuilder(); if (flag) { stringBuilder.Append("ShortCircuitStartedFire".Translate(value)); } else { stringBuilder.Append("ShortCircuit".Translate(value)); } if (num > 0f) { stringBuilder.AppendLine(); stringBuilder.AppendLine(); stringBuilder.Append("ShortCircuitDischargedEnergy".Translate(num.ToString("F0"))); } if (num2 > 5f) { stringBuilder.AppendLine(); stringBuilder.AppendLine(); stringBuilder.Append("ShortCircuitWasLarge".Translate()); } if (num2 > 8f) { stringBuilder.AppendLine(); stringBuilder.AppendLine(); stringBuilder.Append("ShortCircuitWasHuge".Translate()); } Find.LetterStack.ReceiveLetter("LetterLabelShortCircuit".Translate(), stringBuilder.ToString(), LetterDefOf.NegativeEvent, new TargetInfo(culprit.Position, map, false), null, null); }
private void TryDestroyNetAt(IntVec3 cell) { if (cell.InBounds(this.map)) { PowerNet powerNet = this.map.powerNetGrid.TransmittedPowerNetAt(cell); if (powerNet != null) { this.DeletePowerNet(powerNet); } } }
private bool TryFindClosestReachableNet(IntVec3 root, Predicate <PowerNet> predicate, Map map, out PowerNet foundNet, out IntVec3 closestTransmitter) { this.tmpPowerNetPredicateResults.Clear(); PowerNet foundNetLocal = null; IntVec3 closestTransmitterLocal = IntVec3.Invalid; map.floodFiller.FloodFill(root, (IntVec3 x) => this.EverPossibleToTransmitPowerAt(x, map), delegate(IntVec3 x) { Building transmitter = x.GetTransmitter(map); PowerNet powerNet = (transmitter == null) ? null : transmitter.GetComp <CompPower>().PowerNet; bool result2; if (powerNet == null) { result2 = false; } else { bool flag; if (!this.tmpPowerNetPredicateResults.TryGetValue(powerNet, out flag)) { flag = predicate(powerNet); this.tmpPowerNetPredicateResults.Add(powerNet, flag); } if (flag) { foundNetLocal = powerNet; closestTransmitterLocal = x; result2 = true; } else { result2 = false; } } return(result2); }, int.MaxValue, true, null); this.tmpPowerNetPredicateResults.Clear(); bool result; if (foundNetLocal != null) { foundNet = foundNetLocal; closestTransmitter = closestTransmitterLocal; result = true; } else { foundNet = null; closestTransmitter = IntVec3.Invalid; result = false; } return(result); }
private bool HasAnyPowerUser(PowerNet net) { List <CompPowerTrader> powerComps = net.powerComps; for (int i = 0; i < powerComps.Count; i++) { if (this.IsPowerUser(powerComps[i].parent)) { return(true); } } return(false); }
private void EnsureBatteriesConnectedAndMakeSense(Map map) { this.tmpThings.Clear(); this.tmpThings.AddRange(map.listerThings.ThingsInGroup(ThingRequestGroup.BuildingArtificial)); for (int i = 0; i < this.tmpThings.Count; i++) { CompPowerBattery compPowerBattery = this.tmpThings[i].TryGetComp <CompPowerBattery>(); if (compPowerBattery != null) { PowerNet powerNet = compPowerBattery.PowerNet; if (powerNet == null || !this.HasAnyPowerGenerator(powerNet)) { map.powerNetManager.UpdatePowerNetsAndConnections_First(); PowerNet powerNet2; IntVec3 dest; if (this.TryFindClosestReachableNet(compPowerBattery.parent.Position, (PowerNet x) => this.HasAnyPowerGenerator(x), map, out powerNet2, out dest)) { map.floodFiller.ReconstructLastFloodFillPath(dest, this.tmpCells); if (this.canSpawnPowerGenerators) { int count = this.tmpCells.Count; float chance = Mathf.InverseLerp((float)GenStep_Power.MaxDistanceBetweenBatteryAndTransmitter.min, (float)GenStep_Power.MaxDistanceBetweenBatteryAndTransmitter.max, (float)count); if (Rand.Chance(chance)) { Building building; if (this.TrySpawnPowerGeneratorNear(compPowerBattery.parent.Position, map, compPowerBattery.parent.Faction, out building)) { this.SpawnTransmitters(compPowerBattery.parent.Position, building.Position, map, compPowerBattery.parent.Faction); powerNet2 = null; } } } if (powerNet2 != null) { this.SpawnTransmitters(this.tmpCells, map, compPowerBattery.parent.Faction); } } else if (this.canSpawnPowerGenerators) { Building building2; if (this.TrySpawnPowerGeneratorNear(compPowerBattery.parent.Position, map, compPowerBattery.parent.Faction, out building2)) { this.SpawnTransmitters(compPowerBattery.parent.Position, building2.Position, map, compPowerBattery.parent.Faction); } } } } } }
private void TryCreateNetAt(IntVec3 cell) { if (cell.InBounds(map) && map.powerNetGrid.TransmittedPowerNetAt(cell) == null) { Building transmitter = cell.GetTransmitter(map); if (transmitter != null && transmitter.TransmitsPowerNow) { PowerNet powerNet = PowerNetMaker.NewPowerNetStartingFrom(transmitter); RegisterPowerNet(powerNet); for (int i = 0; i < powerNet.transmitters.Count; i++) { PowerConnectionMaker.ConnectAllConnectorsToTransmitter(powerNet.transmitters[i]); } } } }
public static void DoShortCircuit(Building culprit) { PowerNet powerNet = culprit.PowerComp.PowerNet; Map map = culprit.Map; float totalEnergy = 0f; float explosionRadius = 0f; bool flag = false; if (powerNet.batteryComps.Any((CompPowerBattery x) => x.StoredEnergy > 20f)) { DrainBatteriesAndCauseExplosion(powerNet, culprit, out totalEnergy, out explosionRadius); } else { flag = TryStartFireNear(culprit); } string value = (culprit.def != ThingDefOf.PowerConduit) ? Find.ActiveLanguageWorker.WithIndefiniteArticlePostProcessed(culprit.Label) : ((string)"AnElectricalConduit".Translate()); StringBuilder stringBuilder = new StringBuilder(); if (flag) { stringBuilder.Append("ShortCircuitStartedFire".Translate(value)); } else { stringBuilder.Append("ShortCircuit".Translate(value)); } if (totalEnergy > 0f) { stringBuilder.AppendLine(); stringBuilder.AppendLine(); stringBuilder.Append("ShortCircuitDischargedEnergy".Translate(totalEnergy.ToString("F0"))); } if (explosionRadius > 5f) { stringBuilder.AppendLine(); stringBuilder.AppendLine(); stringBuilder.Append("ShortCircuitWasLarge".Translate()); } if (explosionRadius > 8f) { stringBuilder.AppendLine(); stringBuilder.AppendLine(); stringBuilder.Append("ShortCircuitWasHuge".Translate()); } Find.LetterStack.ReceiveLetter("LetterLabelShortCircuit".Translate(), stringBuilder.ToString(), LetterDefOf.NegativeEvent, new TargetInfo(culprit.Position, map)); }
public void DrawDebugPowerNetGrid() { if (DebugViewSettings.drawPowerNetGrid && Current.ProgramState == ProgramState.Playing && map == Find.CurrentMap) { Rand.PushState(); foreach (IntVec3 item in Find.CameraDriver.CurrentViewRect.ClipInsideMap(map)) { PowerNet powerNet = netGrid[map.cellIndices.CellToIndex(item)]; if (powerNet != null) { Rand.Seed = powerNet.GetHashCode(); CellRenderer.RenderCell(item, Rand.Value); } } Rand.PopState(); } }
private static void DrainBatteriesAndCauseExplosion(PowerNet net, Building culprit, out float totalEnergy, out float explosionRadius) { totalEnergy = 0f; for (int i = 0; i < net.batteryComps.Count; i++) { CompPowerBattery compPowerBattery = net.batteryComps[i]; totalEnergy += compPowerBattery.StoredEnergy; compPowerBattery.DrawPower(compPowerBattery.StoredEnergy); } explosionRadius = Mathf.Sqrt(totalEnergy) * 0.05f; explosionRadius = Mathf.Clamp(explosionRadius, 1.5f, 14.9f); GenExplosion.DoExplosion(culprit.Position, net.Map, explosionRadius, DamageDefOf.Flame, null, -1, -1f, null, null, null, null, null, 0f, 1, false, null, 0f, 1, 0f, false); if (explosionRadius > 3.5f) { GenExplosion.DoExplosion(culprit.Position, net.Map, explosionRadius * 0.3f, DamageDefOf.Bomb, null, -1, -1f, null, null, null, null, null, 0f, 1, false, null, 0f, 1, 0f, false); } }
public void Notify_PowerNetDeleted(PowerNet deadNet) { if (!powerNetCells.TryGetValue(deadNet, out List <IntVec3> value)) { Log.Warning("Net " + deadNet + " does not exist in PowerNetGrid's dictionary."); return; } for (int i = 0; i < value.Count; i++) { int num = map.cellIndices.CellToIndex(value[i]); if (netGrid[num] == deadNet) { netGrid[num] = null; } else { Log.Warning("Multiple nets on the same cell " + value[i] + ". This is probably a result of an earlier error."); } } powerNetCells.Remove(deadNet); }
public void ConnectToTransmitter(CompPower transmitter, bool reconnectingAfterLoading = false) { if (this.connectParent != null && (!reconnectingAfterLoading || this.connectParent != transmitter)) { Log.Error("Tried to connect " + this + " to transmitter " + transmitter + " but it's already connected to " + this.connectParent + "."); } else { this.connectParent = transmitter; if (this.connectParent.connectChildren == null) { this.connectParent.connectChildren = new List <CompPower>(); } transmitter.connectChildren.Add(this); PowerNet powerNet = this.PowerNet; if (powerNet != null) { powerNet.RegisterConnector(this); } } }
public void Notify_PowerNetCreated(PowerNet newNet) { if (this.powerNetCells.ContainsKey(newNet)) { Log.Warning("Net " + newNet + " is already registered in PowerNetGrid.", false); this.powerNetCells.Remove(newNet); } List <IntVec3> list = new List <IntVec3>(); this.powerNetCells.Add(newNet, list); for (int i = 0; i < newNet.transmitters.Count; i++) { CellRect cellRect = newNet.transmitters[i].parent.OccupiedRect(); for (int j = cellRect.minZ; j <= cellRect.maxZ; j++) { for (int k = cellRect.minX; k <= cellRect.maxX; k++) { int num = this.map.cellIndices.CellToIndex(k, j); if (this.netGrid[num] != null) { Log.Warning(string.Concat(new object[] { "Two power nets on the same cell (", k, ", ", j, "). First transmitters: ", newNet.transmitters[0].parent.LabelCap, " and ", (!this.netGrid[num].transmitters.NullOrEmpty <CompPower>()) ? this.netGrid[num].transmitters[0].parent.LabelCap : "[none]", "." }), false); } this.netGrid[num] = newNet; list.Add(new IntVec3(k, 0, j)); } } } }
private void EnsureGeneratorsConnectedAndMakeSense(Map map) { this.tmpThings.Clear(); this.tmpThings.AddRange(map.listerThings.ThingsInGroup(ThingRequestGroup.BuildingArtificial)); for (int i = 0; i < this.tmpThings.Count; i++) { if (this.IsPowerGenerator(this.tmpThings[i])) { PowerNet powerNet = this.tmpThings[i].TryGetComp <CompPower>().PowerNet; if (powerNet == null || !this.HasAnyPowerUser(powerNet)) { map.powerNetManager.UpdatePowerNetsAndConnections_First(); PowerNet powerNet2; IntVec3 dest; if (this.TryFindClosestReachableNet(this.tmpThings[i].Position, (PowerNet x) => this.HasAnyPowerUser(x), map, out powerNet2, out dest)) { map.floodFiller.ReconstructLastFloodFillPath(dest, this.tmpCells); this.SpawnTransmitters(this.tmpCells, map, this.tmpThings[i].Faction); } } } } }
private bool <EnsureGeneratorsConnectedAndMakeSense> m__2(PowerNet x) { return(this.HasAnyPowerUser(x)); }
public static void UpdateVisualLinkagesFor(PowerNet net) { }
private bool <EnsureBatteriesConnectedAndMakeSense> m__0(PowerNet x) { return(this.HasAnyPowerGenerator(x)); }
public void DeletePowerNet(PowerNet oldNet) { this.allNets.Remove(oldNet); this.map.powerNetGrid.Notify_PowerNetDeleted(oldNet); }
private static bool <EnsurePowerUsersConnected> m__1(PowerNet x) { return(x.CurrentStoredEnergy() > 1E-07f); }
internal bool <> m__0(PowerNet x) { return(x.CurrentEnergyGainRate() - this.powerComp.Props.basePowerConsumption * CompPower.WattsToWattDaysPerTick > 1E-07f); }