Refactor simulation effects and forecast hazards

This commit is contained in:
2026-05-08 21:26:19 +02:00
parent 023d139281
commit 637e9f7fbc
14 changed files with 374 additions and 141 deletions

View File

@@ -2,93 +2,51 @@
public sealed class SimulationEngine
{
private const int c_MaxForecastStepCount = 12;
public SimulationEngine()
: this(
[new PipeLeakEffect(), new MachineEffect(), new FireAndElectricalHazardEffect(), new CellIntegrityEffect()],
[new SmokeSpreadEffect()],
[new PipeBurstHazard(), new IgnitionHazard(), new MeltdownHazard(), new StabilityCollapseHazard()])
{
}
public SimulationEngine(IEnumerable<ISimulationEffect> effects, IEnumerable<IAreaSimulationEffect> areaEffects, IEnumerable<Hazard> hazards)
{
m_Effects = effects.ToArray();
m_AreaEffects = areaEffects.ToArray();
m_Hazards = hazards.ToArray();
}
public LevelState AdvanceTurn(LevelState level)
{
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 is { HasPipe: true, Pressure: > 7 })
integrity -= cell.Pressure - 7;
if (hazards.Heat >= 10 || hazards.Fire)
{
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 = SpreadSmoke(level, cells);
var global = UpdateGlobal(level, cells);
var next = level with {
Cells = cells,
Global = global with { Turn = level.Global.Turn + 1 }
};
return next with { Forecasts = Forecast(next) };
return AdvanceTurn(level, true);
}
public IReadOnlyList<Forecast> Forecast(LevelState level)
{
var forecasts = new List<Forecast>();
var seen = new HashSet<ForecastKey>();
var forecastLevel = level with { Cells = level.Cells.ToArray(), Forecasts = Array.Empty<Forecast>() };
for (var y = 0; y < level.Height; y++)
for (var x = 0; x < level.Width; x++)
if (forecastLevel.Global.Lost)
AddHazardForecasts(forecasts, seen, forecastLevel, 0);
AddReactorReadyForecast(forecasts, seen, forecastLevel, 0);
if (IsReactorReady(forecastLevel) || forecastLevel.Global.Lost || forecastLevel.Global.ReactorActivated)
return forecasts.OrderBy(f => f.Turns).ThenBy(f => f.Message).ToArray();
for (var step = 1; step <= c_MaxForecastStepCount; step++)
{
var position = new GridPosition(x, y);
var cell = level.GetCell(position);
forecastLevel = AdvanceTurn(forecastLevel, false);
AddHazardForecasts(forecasts, seen, forecastLevel, step);
AddReactorReadyForecast(forecasts, seen, forecastLevel, step);
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(EFailureKind.PipeBurst, position, turns, $"PIPE BURST PREDICTED AT {x},{y} IN {turns} TURNS"));
}
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(EFailureKind.Ignition, position, 1, $"FUEL IGNITION PREDICTED AT {x},{y} NEXT TURN"));
if (forecastLevel.Global.Lost || IsReactorReady(forecastLevel) || forecastLevel.Global.ReactorActivated)
break;
}
if (level.Global.CoreHeat >= 8)
forecasts.Add(new(EFailureKind.Meltdown, null, Math.Max(1, 11 - level.Global.CoreHeat), "CORE MELTDOWN APPROACHING"));
if (IsReactorReady(level))
forecasts.Add(new(EFailureKind.ReactorReady, null, 0, "REACTOR READY"));
return forecasts.OrderBy(f => f.Turns).ThenBy(f => f.Message).ToArray();
}
@@ -105,84 +63,59 @@ public sealed class SimulationEngine
};
}
private static HazardState ApplyPipeLeaks(CellState cell)
private LevelState AdvanceTurn(LevelState level, bool updateForecasts)
{
var hazards = cell.Hazards;
if (!cell.HasPipe || cell.LeakRate <= 0)
return hazards;
var cells = level.Cells.ToArray();
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)
},
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)
},
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 {
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
};
}
private static HazardState ApplyFireAndElectricalHazards(CellState cell, HazardState hazards)
{
if (hazards.CoolantPooling >= 3 && cell.Powered)
hazards = hazards with { ElectricalCharge = hazards.ElectricalCharge + 2 };
var hasFuel = hazards.FuelVapor >= 4 || hazards.LiquidFuel >= 6;
var hasIgnition = hazards.Heat >= 8 || hazards.ElectricalCharge >= 4 || cell is { Kind: ECellKind.Generator, Powered: true };
if ((hasFuel && hasIgnition) || hazards.Fire)
{
hazards = hazards with {
Fire = hasFuel || hazards.Fire,
Heat = hazards.Heat + 2,
Smoke = hazards.Smoke + 2,
LiquidFuel = Math.Max(0, hazards.LiquidFuel - 1),
FuelVapor = Math.Max(0, hazards.FuelVapor - 1)
};
}
else if (hazards.Smoke > 0)
hazards = hazards with { Smoke = hazards.Smoke - 1 };
return hazards;
}
private static CellState[] SpreadSmoke(LevelState level, CellState[] cells)
{
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)
var index = level.Index(position);
var cell = cells[index];
if (!cell.IsWalkable)
continue;
foreach (var neighbor in position.Neighbors().Where(level.InBounds))
{
var neighborCell = next[level.Index(neighbor)];
if (!neighborCell.IsWalkable || neighborCell.DoorLocked)
continue;
foreach (var effect in m_Effects)
cell = effect.Apply(cell);
next[level.Index(neighbor)] = neighborCell with { Hazards = neighborCell.Hazards with { Smoke = Rules.Clamp(neighborCell.Hazards.Smoke + 1) } };
}
cells[index] = cell with { Hazards = cell.Hazards.Clamp() };
}
}
return next;
foreach (var areaEffect in m_AreaEffects)
cells = areaEffect.Apply(level, cells);
var global = UpdateGlobal(level, cells);
var next = level with {
Cells = cells,
Global = global with { Turn = level.Global.Turn + 1 }
};
return updateForecasts ? next with { Forecasts = Forecast(next) } : next;
}
private void AddHazardForecasts(List<Forecast> forecasts, HashSet<ForecastKey> seen, LevelState level, int turns)
{
foreach (var hazard in m_Hazards)
{
foreach (var forecast in hazard.Predict(level, turns))
AddForecast(forecasts, seen, forecast);
}
}
private static void AddReactorReadyForecast(List<Forecast> forecasts, HashSet<ForecastKey> seen, LevelState level, int turns)
{
if (IsReactorReady(level))
AddForecast(forecasts, seen, new(EFailureKind.ReactorReady, null, turns, "REACTOR READY"));
}
private static void AddForecast(List<Forecast> forecasts, HashSet<ForecastKey> seen, Forecast forecast)
{
if (seen.Add(new(forecast.Kind, forecast.Position)))
forecasts.Add(forecast);
}
private static GlobalState UpdateGlobal(LevelState level, CellState[] cells)
@@ -214,4 +147,10 @@ public sealed class SimulationEngine
var reactorStable = level.Global.CoreHeat < 8;
return hasReactor && hasStablePower && hasCooling && reactorStable && !level.Global.Lost;
}
private readonly IReadOnlyList<ISimulationEffect> m_Effects;
private readonly IReadOnlyList<IAreaSimulationEffect> m_AreaEffects;
private readonly IReadOnlyList<Hazard> m_Hazards;
private sealed record ForecastKey(EFailureKind Kind, GridPosition? Position);
}