First batch

This commit is contained in:
2026-05-08 20:46:17 +02:00
parent 83edcfed8a
commit 07d35a49a3
20 changed files with 2509 additions and 1 deletions

View File

@@ -0,0 +1,65 @@
namespace ReactorMaintenance.Simulation;
public enum EditorTool
{
Floor,
Wall,
Reactor,
CoolingPump,
Generator,
PressureRegulator,
DiagnosticTerminal,
ControlTerminal,
CoolantPipe,
FuelPipe,
PressurePipe,
Leak,
Repair,
Heat,
Fire,
Robot
}
public static class LevelEditor
{
public static LevelState Apply(LevelState level, GridPosition position, EditorTool tool)
{
if (!level.InBounds(position))
{
return level;
}
if (tool == EditorTool.Robot)
{
return level.GetCell(position).IsWalkable ? level with { Robot = position } : level;
}
var cell = level.GetCell(position);
cell = tool switch
{
EditorTool.Floor => cell with { Kind = CellKind.Floor },
EditorTool.Wall => cell with { Kind = CellKind.Wall, Pipe = PipeMedium.None, Powered = false },
EditorTool.Reactor => cell with { Kind = CellKind.Reactor },
EditorTool.CoolingPump => cell with { Kind = CellKind.CoolingPump, Powered = true },
EditorTool.Generator => cell with { Kind = CellKind.Generator, Powered = true },
EditorTool.PressureRegulator => cell with { Kind = CellKind.PressureRegulator },
EditorTool.DiagnosticTerminal => cell with { Kind = CellKind.DiagnosticTerminal, Powered = true },
EditorTool.ControlTerminal => cell with { Kind = CellKind.ControlTerminal, Powered = true },
EditorTool.CoolantPipe => cell with { Pipe = PipeMedium.Coolant, Flow = 4, Pressure = 4, Integrity = Math.Max(cell.Integrity, 8), PipeOpen = true },
EditorTool.FuelPipe => cell with { Pipe = PipeMedium.Fuel, Flow = 4, Pressure = 4, Integrity = Math.Max(cell.Integrity, 8), PipeOpen = true },
EditorTool.PressurePipe => cell with { Pipe = PipeMedium.Pressure, Flow = 5, Pressure = 6, Integrity = Math.Max(cell.Integrity, 8), PipeOpen = true },
EditorTool.Leak => cell with { LeakRate = Math.Max(1, cell.LeakRate), Integrity = Math.Min(cell.Integrity, 4) },
EditorTool.Repair => cell with { LeakRate = 0, Integrity = 10, Hazards = cell.Hazards with { Fire = false, ElectricalCharge = 0 } },
EditorTool.Heat => cell with { Hazards = cell.Hazards with { Heat = Rules.Clamp(cell.Hazards.Heat + 2) } },
EditorTool.Fire => cell with { Hazards = cell.Hazards with { Fire = !cell.Hazards.Fire, Heat = Math.Max(cell.Hazards.Heat, 7), Smoke = Math.Max(cell.Hazards.Smoke, 3) } },
_ => cell
};
if (cell.Kind == CellKind.Wall)
{
cell = cell with { Hazards = new HazardState() };
}
return level.SetCell(position, cell);
}
}

View File

@@ -0,0 +1,28 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace ReactorMaintenance.Simulation;
public static class LevelSerializer
{
private static readonly JsonSerializerOptions Options = new()
{
WriteIndented = true,
Converters = { new JsonStringEnumConverter() }
};
public static string Serialize(LevelState level) => JsonSerializer.Serialize(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.");
if (level.Cells.Length != level.Width * level.Height)
{
throw new InvalidOperationException("Level cell count does not match its dimensions.");
}
return level;
}
}

View File

@@ -0,0 +1,174 @@
namespace ReactorMaintenance.Simulation;
public enum CellKind
{
Empty,
Floor,
Wall,
Reactor,
CoolingPump,
Generator,
PressureRegulator,
DiagnosticTerminal,
ControlTerminal
}
public enum PipeMedium
{
None,
Pressure,
Coolant,
Fuel
}
public enum FailureKind
{
PipeBurst,
Ignition,
Meltdown,
StabilityCollapse,
ReactorReady
}
public sealed record GridPosition(int X, int Y)
{
public IEnumerable<GridPosition> Neighbors()
{
yield return new GridPosition(X - 1, Y);
yield return new GridPosition(X + 1, Y);
yield return new GridPosition(X, Y - 1);
yield return new GridPosition(X, Y + 1);
}
}
public sealed record HazardState
{
public int Heat { get; init; }
public int Smoke { get; init; }
public int FuelVapor { get; init; }
public int LiquidFuel { get; init; }
public int CoolantPooling { get; init; }
public int ElectricalCharge { get; init; }
public int Stability { get; init; } = 10;
public bool Fire { get; init; }
public HazardState Clamp() => this with
{
Heat = Rules.Clamp(Heat),
Smoke = Rules.Clamp(Smoke),
FuelVapor = Rules.Clamp(FuelVapor),
LiquidFuel = Rules.Clamp(LiquidFuel),
CoolantPooling = Rules.Clamp(CoolantPooling),
ElectricalCharge = Rules.Clamp(ElectricalCharge),
Stability = Rules.Clamp(Stability)
};
}
public sealed record CellState
{
public CellKind Kind { get; init; } = CellKind.Floor;
public PipeMedium Pipe { get; init; }
public int Flow { get; init; }
public int Pressure { get; init; }
public int Integrity { get; init; } = 10;
public int LeakRate { get; init; }
public bool PipeOpen { get; init; } = true;
public bool Powered { get; init; }
public bool DoorLocked { get; init; }
public HazardState Hazards { get; init; } = new();
public bool IsWalkable => Kind != CellKind.Wall && Kind != CellKind.Empty;
public bool HasPipe => Pipe != PipeMedium.None;
}
public sealed record GlobalState
{
public int Turn { get; init; }
public int ActionsPerTurn { get; init; } = 3;
public int CoreHeat { get; init; } = 5;
public int FacilityStability { get; init; } = 10;
public int Power { get; init; } = 5;
public int Cooling { get; init; } = 0;
public bool ReactorActivated { get; init; }
public bool Lost { get; init; }
public string Status { get; init; } = "STABILIZE SYSTEMS";
}
public sealed record Forecast(FailureKind Kind, GridPosition? Position, int Turns, string Message);
public sealed record LevelState
{
public string Name { get; init; } = "New Reactor";
public int Width { get; init; } = 16;
public int Height { get; init; } = 12;
public CellState[] Cells { get; init; } = CreateCells(16, 12);
public GridPosition Robot { get; init; } = new(1, 1);
public GlobalState Global { get; init; } = new();
public IReadOnlyList<Forecast> Forecasts { get; init; } = Array.Empty<Forecast>();
public static LevelState Create(string name, int width, int height)
{
if (width < 4 || height < 4)
{
throw new ArgumentOutOfRangeException(nameof(width), "Levels must be at least 4x4.");
}
var cells = CreateCells(width, height);
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
if (x == 0 || y == 0 || x == width - 1 || y == height - 1)
{
cells[y * width + x] = cells[y * width + x] with { Kind = CellKind.Wall };
}
}
}
return new LevelState
{
Name = name,
Width = width,
Height = height,
Cells = cells,
Robot = new GridPosition(1, 1)
};
}
public CellState GetCell(GridPosition position)
{
EnsureInBounds(position);
return Cells[Index(position)];
}
public LevelState SetCell(GridPosition position, CellState cell)
{
EnsureInBounds(position);
var cells = Cells.ToArray();
cells[Index(position)] = cell;
return this with { Cells = cells };
}
public bool InBounds(GridPosition position) =>
position.X >= 0 && position.Y >= 0 && position.X < Width && position.Y < Height;
public int Index(GridPosition position) => position.Y * Width + position.X;
private void EnsureInBounds(GridPosition position)
{
if (!InBounds(position))
{
throw new ArgumentOutOfRangeException(nameof(position), $"Position {position.X},{position.Y} is outside {Width}x{Height}.");
}
}
private static CellState[] CreateCells(int width, int height) =>
Enumerable.Range(0, width * height)
.Select(_ => new CellState())
.ToArray();
}
internal static class Rules
{
public static int Clamp(int value) => Math.Clamp(value, 0, 10);
}

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,274 @@
namespace ReactorMaintenance.Simulation;
public sealed class SimulationEngine
{
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.HasPipe && cell.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) };
}
public IReadOnlyList<Forecast> Forecast(LevelState level)
{
var forecasts = new List<Forecast>();
for (var y = 0; y < level.Height; y++)
{
for (var x = 0; x < level.Width; x++)
{
var position = new GridPosition(x, y);
var cell = level.GetCell(position);
if (cell.HasPipe && cell.Pressure > 7 && cell.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 Forecast(FailureKind.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);
if (ignitionRisk && !cell.Hazards.Fire)
{
forecasts.Add(new Forecast(FailureKind.Ignition, position, 1, $"FUEL IGNITION PREDICTED AT {x},{y} NEXT TURN"));
}
}
}
if (level.Global.CoreHeat >= 8)
{
forecasts.Add(new Forecast(FailureKind.Meltdown, null, Math.Max(1, 11 - level.Global.CoreHeat), "CORE MELTDOWN APPROACHING"));
}
if (IsReactorReady(level))
{
forecasts.Add(new Forecast(FailureKind.ReactorReady, null, 0, "REACTOR READY"));
}
return forecasts.OrderBy(f => f.Turns).ThenBy(f => f.Message).ToArray();
}
public LevelState ActivateReactor(LevelState level)
{
if (!IsReactorReady(level))
{
return level with { Global = level.Global with { Status = "REACTOR NOT READY" } };
}
return level with
{
Global = level.Global with
{
ReactorActivated = true,
Status = "REACTOR ONLINE"
}
};
}
private static HazardState ApplyPipeLeaks(CellState cell)
{
var hazards = cell.Hazards;
if (!cell.HasPipe || cell.LeakRate <= 0)
{
return hazards;
}
return cell.Pipe switch
{
PipeMedium.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
{
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
};
}
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
};
}
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.Kind == CellKind.Generator && cell.Powered);
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)
{
continue;
}
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) }
};
}
}
}
return next;
}
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 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
{
CoreHeat = Rules.Clamp(reactorHeat - poweredPumps),
Power = Rules.Clamp(poweredGenerators * 3),
Cooling = Rules.Clamp(poweredPumps * 3),
FacilityStability = stability,
Lost = lost,
Status = status
};
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 reactorStable = level.Global.CoreHeat < 8;
return hasReactor && hasStablePower && hasCooling && reactorStable && !level.Global.Lost;
}
}