Refactor cells for dual tile rendering

This commit is contained in:
2026-05-08 22:05:02 +02:00
parent 9c7d661e8c
commit 40038302de
9 changed files with 225 additions and 76 deletions

View File

@@ -11,7 +11,7 @@ public sealed class FireAndElectricalHazardEffect : ISimulationEffect
hazards = hazards with { ElectricalCharge = hazards.ElectricalCharge + Balancing.Current.ElectricalChargeIncrease };
var hasFuel = hazards.FuelVapor >= Balancing.Current.FuelVaporFireThreshold || hazards.LiquidFuel >= Balancing.Current.LiquidFuelFireThreshold;
var hasIgnition = hazards.Heat >= Balancing.Current.HeatIgnitionThreshold || hazards.ElectricalCharge >= Balancing.Current.ElectricalIgnitionThreshold || cell is { Kind: ECellKind.Generator, Powered: true };
var hasIgnition = hazards.Heat >= Balancing.Current.HeatIgnitionThreshold || hazards.ElectricalCharge >= Balancing.Current.ElectricalIgnitionThreshold || cell is { Prop: ECellProp.Generator, Powered: true };
if ((hasFuel && hasIgnition) || hazards.Fire)
{
hazards = hazards with {

View File

@@ -6,10 +6,10 @@ public sealed class MachineEffect : ISimulationEffect
{
public CellState Apply(CellState cell)
{
var hazards = cell.Kind switch {
ECellKind.Generator when cell.Powered => cell.Hazards with { Heat = cell.Hazards.Heat + Balancing.Current.GeneratorHeatIncrease },
ECellKind.CoolingPump when cell.Powered => cell.Hazards with { Heat = cell.Hazards.Heat - Balancing.Current.CoolingPumpHeatReduction },
ECellKind.Reactor => cell.Hazards with { Heat = cell.Hazards.Heat + Balancing.Current.ReactorHeatIncrease },
var hazards = cell.Prop switch {
ECellProp.Generator when cell.Powered => cell.Hazards with { Heat = cell.Hazards.Heat + Balancing.Current.GeneratorHeatIncrease },
ECellProp.CoolingPump when cell.Powered => cell.Hazards with { Heat = cell.Hazards.Heat - Balancing.Current.CoolingPumpHeatReduction },
ECellProp.Reactor => cell.Hazards with { Heat = cell.Hazards.Heat + Balancing.Current.ReactorHeatIncrease },
_ => cell.Hazards
};

View File

@@ -32,28 +32,37 @@ public static class LevelEditor
var cell = level.GetCell(position);
cell = tool switch {
EEditorTool.Floor => cell with { Kind = ECellKind.Floor },
EEditorTool.Floor => cell with { Terrain = ECellTerrain.Floor },
EEditorTool.Wall => cell with {
Kind = ECellKind.Wall,
Terrain = ECellTerrain.Wall,
Prop = ECellProp.None,
Pipe = EPipeMedium.None,
Flow = Balancing.Current.MinHazardValue,
Pressure = Balancing.Current.MinHazardValue,
LeakRate = Balancing.Current.MinHazardValue,
PipeOpen = false,
Powered = false
},
EEditorTool.Reactor => cell with { Kind = ECellKind.Reactor },
EEditorTool.Reactor => cell with { Terrain = ECellTerrain.Floor, Prop = ECellProp.Reactor },
EEditorTool.CoolingPump => cell with {
Kind = ECellKind.CoolingPump,
Terrain = ECellTerrain.Floor,
Prop = ECellProp.CoolingPump,
Powered = true
},
EEditorTool.Generator => cell with {
Kind = ECellKind.Generator,
Terrain = ECellTerrain.Floor,
Prop = ECellProp.Generator,
Powered = true
},
EEditorTool.PressureRegulator => cell with { Kind = ECellKind.PressureRegulator },
EEditorTool.PressureRegulator => cell with { Terrain = ECellTerrain.Floor, Prop = ECellProp.PressureRegulator },
EEditorTool.DiagnosticTerminal => cell with {
Kind = ECellKind.DiagnosticTerminal,
Terrain = ECellTerrain.Floor,
Prop = ECellProp.DiagnosticTerminal,
Powered = true
},
EEditorTool.ControlTerminal => cell with {
Kind = ECellKind.ControlTerminal,
Terrain = ECellTerrain.Floor,
Prop = ECellProp.ControlTerminal,
Powered = true
},
EEditorTool.CoolantPipe => cell with {
@@ -100,7 +109,7 @@ public static class LevelEditor
_ => cell
};
if (cell.Kind == ECellKind.Wall)
if (cell.Terrain == ECellTerrain.Wall)
cell = cell with { Hazards = new() };
return level.SetCell(position, cell);

View File

@@ -1,18 +1,28 @@
using System.Text.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace ReactorMaintenance.Simulation;
public static class LevelSerializer
{
private const int c_CurrentVersion = 1;
public static string Serialize(LevelState level)
{
return JsonSerializer.Serialize(level, Options);
return JsonSerializer.Serialize(new LevelFile {
Version = c_CurrentVersion,
Level = level
}, Options);
}
public static LevelState Deserialize(string json)
{
var level = JsonSerializer.Deserialize<LevelState>(json, Options) ?? throw new InvalidOperationException("Level file did not contain a level.");
var file = JsonSerializer.Deserialize<LevelFile>(json, Options) ?? throw new InvalidOperationException("Level file did not contain a level.");
var level = file.Version switch {
c_CurrentVersion => file.Level,
_ => throw new InvalidOperationException($"Unsupported level file version {file.Version}.")
};
return level.Cells.Length != level.Width * level.Height ? throw new InvalidOperationException("Level cell count does not match its dimensions.") : level;
}
@@ -20,4 +30,10 @@ public static class LevelSerializer
WriteIndented = true,
Converters = { new JsonStringEnumConverter() }
};
}
private sealed record LevelFile
{
public int Version { get; init; }
public LevelState Level { get; init; } = new();
}
}

View File

@@ -1,10 +1,14 @@
namespace ReactorMaintenance.Simulation;
public enum ECellKind
public enum ECellTerrain
{
Empty,
Floor,
Wall,
Wall
}
public enum ECellProp
{
None,
Reactor,
CoolingPump,
Generator,
@@ -68,7 +72,8 @@ public sealed record HazardState
public sealed record CellState
{
public ECellKind Kind { get; init; } = ECellKind.Floor;
public ECellTerrain Terrain { get; init; } = ECellTerrain.Floor;
public ECellProp Prop { get; init; }
public EPipeMedium Pipe { get; init; }
public int Flow { get; init; }
public int Pressure { get; init; }
@@ -78,7 +83,7 @@ public sealed record CellState
public bool Powered { get; init; }
public bool DoorLocked { get; init; }
public HazardState Hazards { get; init; } = new();
public bool IsWalkable => Kind != ECellKind.Wall && Kind != ECellKind.Empty;
public bool IsWalkable => Terrain != ECellTerrain.Wall;
public bool HasPipe => Pipe != EPipeMedium.None;
}
@@ -110,7 +115,7 @@ public sealed record LevelState
for (var x = Balancing.Current.FirstGridCoordinate; x < width; x++)
{
if (x == Balancing.Current.FirstGridCoordinate || y == Balancing.Current.FirstGridCoordinate || x == width - Balancing.Current.NeighborDistance || y == height - Balancing.Current.NeighborDistance)
cells[y * width + x] = cells[y * width + x] with { Kind = ECellKind.Wall };
cells[y * width + x] = cells[y * width + x] with { Terrain = ECellTerrain.Wall };
}
}

View File

@@ -116,10 +116,10 @@ public sealed class SimulationEngine(IEnumerable<ISimulationEffect> effects, IEn
private static GlobalState UpdateGlobal(LevelState level, CellState[] cells)
{
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 <= Balancing.Current.CriticalCellStabilityThreshold);
var reactorHeat = cells.Where(c => c.Prop == ECellProp.Reactor).Select(c => c.Hazards.Heat).DefaultIfEmpty(level.Global.CoreHeat).Max();
var poweredGenerators = cells.Count(c => c is { Prop: ECellProp.Generator, Powered: true, Hazards.Fire: false });
var poweredPumps = cells.Count(c => c is { Prop: ECellProp.CoolingPump, Powered: true, Hazards.Fire: false });
var damagedCriticalCells = cells.Count(c => c.Prop is ECellProp.Reactor or ECellProp.Generator or ECellProp.CoolingPump && c.Hazards.Stability <= Balancing.Current.CriticalCellStabilityThreshold);
var stability = Rules.Clamp(level.Global.FacilityStability - damagedCriticalCells);
var lost = reactorHeat >= Balancing.Current.MeltdownCoreHeatThreshold || stability <= Balancing.Current.StabilityCollapseThreshold;
var status = lost ? reactorHeat >= Balancing.Current.MeltdownCoreHeatThreshold ? "CORE MELTDOWN" : "FACILITY STABILITY COLLAPSE" : "STABILIZE SYSTEMS";
@@ -137,9 +137,9 @@ public sealed class SimulationEngine(IEnumerable<ISimulationEffect> effects, IEn
private static bool IsReactorReady(LevelState level)
{
var hasReactor = level.Cells.Any(c => c.Kind == ECellKind.Reactor);
var hasStablePower = level.Global.Power >= Balancing.Current.ReactorReadyPowerThreshold || level.Cells.Any(c => c is { Kind: ECellKind.Generator, Powered: true, Hazards.Fire: false });
var hasCooling = level.Global.Cooling >= Balancing.Current.ReactorReadyCoolingThreshold || level.Cells.Any(c => c is { Kind: ECellKind.CoolingPump, Powered: true, Hazards.Fire: false });
var hasReactor = level.Cells.Any(c => c.Prop == ECellProp.Reactor);
var hasStablePower = level.Global.Power >= Balancing.Current.ReactorReadyPowerThreshold || level.Cells.Any(c => c is { Prop: ECellProp.Generator, Powered: true, Hazards.Fire: false });
var hasCooling = level.Global.Cooling >= Balancing.Current.ReactorReadyCoolingThreshold || level.Cells.Any(c => c is { Prop: ECellProp.CoolingPump, Powered: true, Hazards.Fire: false });
var reactorStable = level.Global.CoreHeat < Balancing.Current.ReactorReadyCoreHeatThreshold;
return hasReactor && hasStablePower && hasCooling && reactorStable && !level.Global.Lost;
}