Document code style and normalize files

This commit is contained in:
2026-05-08 21:10:46 +02:00
parent 2e813962c9
commit 5a261f5fe2
20 changed files with 1033 additions and 388 deletions

View File

@@ -1,4 +1,4 @@
namespace ReactorMaintenance.Simulation;
namespace ReactorMaintenance.Simulation;
public sealed class SimulationEngine
{
@@ -7,52 +7,51 @@ public sealed class SimulationEngine
var cells = level.Cells.ToArray();
for (var y = 0; y < level.Height; y++)
for (var x = 0; x < level.Width; x++)
{
var position = new GridPosition(x, y);
var index = level.Index(position);
var cell = cells[index];
if (!cell.IsWalkable)
continue;
var hazards = ApplyPipeLeaks(cell);
hazards = ApplyMachineEffects(cell, hazards);
hazards = ApplyFireAndElectricalHazards(cell, hazards);
hazards = hazards.Clamp();
var integrity = cell.Integrity;
if (cell.HasPipe && cell.Pressure > 7)
integrity -= cell.Pressure - 7;
if (hazards.Heat >= 10 || hazards.Fire)
for (var x = 0; x < level.Width; x++)
{
integrity -= cell.HasPipe ? 1 : 0;
hazards = hazards with { Stability = hazards.Stability - 1 };
}
var position = new GridPosition(x, y);
var index = level.Index(position);
var cell = cells[index];
if (integrity <= 0 && cell.HasPipe)
{
cell = cell with
if (!cell.IsWalkable)
continue;
var hazards = ApplyPipeLeaks(cell);
hazards = ApplyMachineEffects(cell, hazards);
hazards = ApplyFireAndElectricalHazards(cell, hazards);
hazards = hazards.Clamp();
var integrity = cell.Integrity;
if (cell is { HasPipe: true, Pressure: > 7 })
integrity -= cell.Pressure - 7;
if (hazards.Heat >= 10 || hazards.Fire)
{
LeakRate = Math.Max(cell.LeakRate, 3),
Flow = 0,
PipeOpen = false
integrity -= cell.HasPipe ? 1 : 0;
hazards = hazards with { Stability = hazards.Stability - 1 };
}
if (integrity <= 0 && cell.HasPipe)
{
cell = cell with {
LeakRate = Math.Max(cell.LeakRate, 3),
Flow = 0,
PipeOpen = false
};
}
cells[index] = cell with {
Integrity = Rules.Clamp(integrity),
Hazards = hazards.Clamp()
};
}
cells[index] = cell with
{
Integrity = Rules.Clamp(integrity),
Hazards = hazards.Clamp()
};
}
cells = SpreadSmoke(level, cells);
var global = UpdateGlobal(level, cells);
var next = level with
{
var next = level with {
Cells = cells,
Global = global with { Turn = level.Global.Turn + 1 }
};
@@ -70,25 +69,25 @@ public sealed class SimulationEngine
var position = new GridPosition(x, y);
var cell = level.GetCell(position);
if (cell.HasPipe && cell.Pressure > 7 && cell.Integrity > 0)
if (cell is { HasPipe: true, Pressure: > 7, Integrity: > 0 })
{
var damagePerTurn = Math.Max(1, cell.Pressure - 7);
var turns = (int)Math.Ceiling(cell.Integrity / (double)damagePerTurn);
if (turns <= 4)
forecasts.Add(new(FailureKind.PipeBurst, position, turns, $"PIPE BURST PREDICTED AT {x},{y} IN {turns} TURNS"));
forecasts.Add(new(EFailureKind.PipeBurst, position, turns, $"PIPE BURST PREDICTED AT {x},{y} IN {turns} TURNS"));
}
var fuelLeakNearIgnition = cell.Pipe == PipeMedium.Fuel && cell.LeakRate > 0 && (cell.Pressure >= 7 || cell.Kind == CellKind.Generator);
var ignitionRisk = (cell.Hazards.FuelVapor >= 4 || cell.Hazards.LiquidFuel >= 6 || fuelLeakNearIgnition) && (cell.Hazards.Heat >= 8 || cell.Hazards.ElectricalCharge >= 4 || cell.Kind == CellKind.Generator);
var fuelLeakNearIgnition = cell is { Pipe: EPipeMedium.Fuel, LeakRate: > 0 } && (cell.Pressure >= 7 || cell.Kind == ECellKind.Generator);
var ignitionRisk = (cell.Hazards.FuelVapor >= 4 || cell.Hazards.LiquidFuel >= 6 || fuelLeakNearIgnition) && (cell.Hazards.Heat >= 8 || cell.Hazards.ElectricalCharge >= 4 || cell.Kind == ECellKind.Generator);
if (ignitionRisk && !cell.Hazards.Fire)
forecasts.Add(new(FailureKind.Ignition, position, 1, $"FUEL IGNITION PREDICTED AT {x},{y} NEXT TURN"));
forecasts.Add(new(EFailureKind.Ignition, position, 1, $"FUEL IGNITION PREDICTED AT {x},{y} NEXT TURN"));
}
if (level.Global.CoreHeat >= 8)
forecasts.Add(new(FailureKind.Meltdown, null, Math.Max(1, 11 - level.Global.CoreHeat), "CORE MELTDOWN APPROACHING"));
forecasts.Add(new(EFailureKind.Meltdown, null, Math.Max(1, 11 - level.Global.CoreHeat), "CORE MELTDOWN APPROACHING"));
if (IsReactorReady(level))
forecasts.Add(new(FailureKind.ReactorReady, null, 0, "REACTOR READY"));
forecasts.Add(new(EFailureKind.ReactorReady, null, 0, "REACTOR READY"));
return forecasts.OrderBy(f => f.Turns).ThenBy(f => f.Message).ToArray();
}
@@ -98,10 +97,8 @@ public sealed class SimulationEngine
if (!IsReactorReady(level))
return level with { Global = level.Global with { Status = "REACTOR NOT READY" } };
return level with
{
Global = level.Global with
{
return level with {
Global = level.Global with {
ReactorActivated = true,
Status = "REACTOR ONLINE"
}
@@ -114,32 +111,28 @@ public sealed class SimulationEngine
if (!cell.HasPipe || cell.LeakRate <= 0)
return hazards;
return cell.Pipe switch
{
PipeMedium.Fuel => hazards with
{
return cell.Pipe switch {
EPipeMedium.Fuel => hazards with {
LiquidFuel = hazards.LiquidFuel + cell.LeakRate,
FuelVapor = hazards.FuelVapor + (cell.Pressure >= 7 ? cell.LeakRate : Math.Max(0, hazards.Heat - 3) / 3)
},
PipeMedium.Coolant => hazards with
{
EPipeMedium.Coolant => hazards with {
CoolantPooling = hazards.CoolantPooling + cell.LeakRate,
Heat = hazards.Heat - Math.Max(1, cell.LeakRate / 2),
Smoke = hazards.Smoke + (hazards.Heat >= 7 ? 2 : 0)
},
PipeMedium.Pressure => hazards with { Smoke = hazards.Smoke + (cell.Pressure >= 8 ? 1 : 0) },
_ => hazards
EPipeMedium.Pressure => hazards with { Smoke = hazards.Smoke + (cell.Pressure >= 8 ? 1 : 0) },
_ => hazards
};
}
private static HazardState ApplyMachineEffects(CellState cell, HazardState hazards)
{
return cell.Kind switch
{
CellKind.Generator when cell.Powered => hazards with { Heat = hazards.Heat + 1 },
CellKind.CoolingPump when cell.Powered => hazards with { Heat = hazards.Heat - 2 },
CellKind.Reactor => hazards with { Heat = hazards.Heat + 1 },
_ => hazards
return cell.Kind switch {
ECellKind.Generator when cell.Powered => hazards with { Heat = hazards.Heat + 1 },
ECellKind.CoolingPump when cell.Powered => hazards with { Heat = hazards.Heat - 2 },
ECellKind.Reactor => hazards with { Heat = hazards.Heat + 1 },
_ => hazards
};
}
@@ -149,11 +142,10 @@ public sealed class SimulationEngine
hazards = hazards with { ElectricalCharge = hazards.ElectricalCharge + 2 };
var hasFuel = hazards.FuelVapor >= 4 || hazards.LiquidFuel >= 6;
var hasIgnition = hazards.Heat >= 8 || hazards.ElectricalCharge >= 4 || (cell.Kind == CellKind.Generator && cell.Powered);
var hasIgnition = hazards.Heat >= 8 || hazards.ElectricalCharge >= 4 || cell is { Kind: ECellKind.Generator, Powered: true };
if ((hasFuel && hasIgnition) || hazards.Fire)
{
hazards = hazards with
{
hazards = hazards with {
Fire = hasFuel || hazards.Fire,
Heat = hazards.Heat + 2,
Smoke = hazards.Smoke + 2,
@@ -171,20 +163,22 @@ public sealed class SimulationEngine
{
var next = cells.ToArray();
for (var y = 0; y < level.Height; y++)
for (var x = 0; x < level.Width; x++)
{
var position = new GridPosition(x, y);
var cell = cells[level.Index(position)];
if (cell.Hazards.Smoke < 6)
continue;
foreach (var neighbor in position.Neighbors().Where(level.InBounds))
for (var x = 0; x < level.Width; x++)
{
var neighborCell = next[level.Index(neighbor)];
if (!neighborCell.IsWalkable || neighborCell.DoorLocked)
var position = new GridPosition(x, y);
var cell = cells[level.Index(position)];
if (cell.Hazards.Smoke < 6)
continue;
next[level.Index(neighbor)] = neighborCell with { Hazards = neighborCell.Hazards with { Smoke = Rules.Clamp(neighborCell.Hazards.Smoke + 1) } };
foreach (var neighbor in position.Neighbors().Where(level.InBounds))
{
var neighborCell = next[level.Index(neighbor)];
if (!neighborCell.IsWalkable || neighborCell.DoorLocked)
continue;
next[level.Index(neighbor)] = neighborCell with { Hazards = neighborCell.Hazards with { Smoke = Rules.Clamp(neighborCell.Hazards.Smoke + 1) } };
}
}
}
@@ -193,18 +187,14 @@ public sealed class SimulationEngine
private static GlobalState UpdateGlobal(LevelState level, CellState[] cells)
{
var reactorHeat = cells.Where(c => c.Kind == CellKind.Reactor).Select(c => c.Hazards.Heat).DefaultIfEmpty(level.Global.CoreHeat).Max();
var poweredGenerators = cells.Count(c => c.Kind == CellKind.Generator && c.Powered && !c.Hazards.Fire);
var poweredPumps = cells.Count(c => c.Kind == CellKind.CoolingPump && c.Powered && !c.Hazards.Fire);
var damagedCriticalCells = cells.Count(c => c.Kind is CellKind.Reactor or CellKind.Generator or CellKind.CoolingPump && c.Hazards.Stability <= 3);
var reactorHeat = cells.Where(c => c.Kind == ECellKind.Reactor).Select(c => c.Hazards.Heat).DefaultIfEmpty(level.Global.CoreHeat).Max();
var poweredGenerators = cells.Count(c => c is { Kind: ECellKind.Generator, Powered: true, Hazards.Fire: false });
var poweredPumps = cells.Count(c => c is { Kind: ECellKind.CoolingPump, Powered: true, Hazards.Fire: false });
var damagedCriticalCells = cells.Count(c => c.Kind is ECellKind.Reactor or ECellKind.Generator or ECellKind.CoolingPump && c.Hazards.Stability <= 3);
var stability = Rules.Clamp(level.Global.FacilityStability - damagedCriticalCells);
var lost = reactorHeat >= 10 || stability <= 0;
var status = lost ? reactorHeat >= 10 ? "CORE MELTDOWN" : "FACILITY STABILITY COLLAPSE" : "STABILIZE SYSTEMS";
var global = level.Global with
{
var global = level.Global with {
CoreHeat = Rules.Clamp(reactorHeat - poweredPumps),
Power = Rules.Clamp(poweredGenerators * 3),
Cooling = Rules.Clamp(poweredPumps * 3),
@@ -213,20 +203,14 @@ public sealed class SimulationEngine
Status = status
};
return IsReactorReady(level with
{
Cells = cells,
Global = global
})
? global with { Status = "REACTOR READY" }
: global;
return IsReactorReady(level with { Cells = cells, Global = global }) ? global with { Status = "REACTOR READY" } : global;
}
private static bool IsReactorReady(LevelState level)
{
var hasReactor = level.Cells.Any(c => c.Kind == CellKind.Reactor);
var hasStablePower = level.Global.Power >= 3 || level.Cells.Any(c => c.Kind == CellKind.Generator && c.Powered && !c.Hazards.Fire);
var hasCooling = level.Global.Cooling >= 3 || level.Cells.Any(c => c.Kind == CellKind.CoolingPump && c.Powered && !c.Hazards.Fire);
var hasReactor = level.Cells.Any(c => c.Kind == ECellKind.Reactor);
var hasStablePower = level.Global.Power >= 3 || level.Cells.Any(c => c is { Kind: ECellKind.Generator, Powered: true, Hazards.Fire: false });
var hasCooling = level.Global.Cooling >= 3 || level.Cells.Any(c => c is { Kind: ECellKind.CoolingPump, Powered: true, Hazards.Fire: false });
var reactorStable = level.Global.CoreHeat < 8;
return hasReactor && hasStablePower && hasCooling && reactorStable && !level.Global.Lost;
}