Compare commits

24 Commits

Author SHA1 Message Date
672f055a80 increased sim speed 2026-05-12 00:19:00 +02:00
adf1475fc0 Add editor simulation playback 2026-05-12 00:15:41 +02:00
06d37aac10 Fix editor drag feedback and surface overlays 2026-05-12 00:06:12 +02:00
99482c7011 UI iteration. 2026-05-12 00:01:07 +02:00
fbb7c0490c Refine editor inspector and drag painting 2026-05-11 23:18:47 +02:00
dfe0cb3b6a Add editor image badges and drag moves 2026-05-11 23:03:29 +02:00
884cc4503f cleanup 2026-05-11 22:38:33 +02:00
0651603fd2 Rework Win2D editor layers 2026-05-11 22:34:19 +02:00
69ed79ce86 Update task progress 2026-05-11 22:25:08 +02:00
e1ac56d201 Rework simulation rules 2026-05-11 22:18:43 +02:00
3d406179bf Restore complete design system documentation 2026-05-11 22:02:46 +02:00
787f1e5e85 Document approved design iteration 2026-05-11 21:53:55 +02:00
5ddd1b8ec8 Cleanup 2026-05-11 21:51:18 +02:00
1b9372ff7c Restore dual terrain tilemap rendering 2026-05-10 23:04:48 +02:00
3a52db0071 Finish rewrite task list 2026-05-10 22:35:25 +02:00
5a186fb606 Split simulation systems 2026-05-10 18:49:24 +02:00
6c7fa070f6 cleanup code 2026-05-10 18:37:30 +02:00
d22c4a7528 Update rewrite docs and cleanup 2026-05-10 18:09:43 +02:00
7ffaa140a8 Introduce simulation engine facade 2026-05-10 18:08:03 +02:00
3c5fc60ffe Parameterize surface interactions 2026-05-10 18:07:16 +02:00
9cd9defc0b Unify junction props 2026-05-10 18:05:32 +02:00
1aa9734e08 Split simulation models 2026-05-10 18:03:46 +02:00
a0b10423ac Expand rule event coverage 2026-05-10 17:38:43 +02:00
cb28eee1dd Add branch-aware junction flow 2026-05-10 17:29:19 +02:00
72 changed files with 3860 additions and 2043 deletions

View File

@@ -20,9 +20,10 @@ insert_final_newline = false
# Organize usings
dotnet_separate_import_directive_groups = false
dotnet_sort_system_directives_first = false
file_header_template =
file_header_template = # this. and Me. preferences
# this. and Me. preferences
dotnet_style_qualification_for_event = false:suggestion
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
@@ -125,7 +126,7 @@ csharp_new_line_before_else = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_open_brace = accessors,anonymous_methods,control_blocks,events,indexers,local_functions,methods,properties,types
csharp_new_line_before_open_brace = accessors, anonymous_methods, control_blocks, events, indexers, local_functions, methods, properties, types
csharp_new_line_between_query_expression_clauses = true
# Indentation preferences
@@ -228,27 +229,27 @@ dotnet_naming_rule.local_should_be_camelcase.style = camelcase
dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.interface.required_modifiers =
dotnet_naming_symbols.interface.required_modifiers =
dotnet_naming_symbols.struct.applicable_kinds = struct
dotnet_naming_symbols.struct.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.struct.required_modifiers =
dotnet_naming_symbols.struct.required_modifiers =
dotnet_naming_symbols.delegate.applicable_kinds = delegate
dotnet_naming_symbols.delegate.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.delegate.required_modifiers =
dotnet_naming_symbols.delegate.required_modifiers =
dotnet_naming_symbols.event.applicable_kinds = event
dotnet_naming_symbols.event.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.event.required_modifiers =
dotnet_naming_symbols.event.required_modifiers =
dotnet_naming_symbols.property.applicable_kinds = property
dotnet_naming_symbols.property.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.property.required_modifiers =
dotnet_naming_symbols.property.required_modifiers =
dotnet_naming_symbols.public_or_protected_field.applicable_kinds = field
dotnet_naming_symbols.public_or_protected_field.applicable_accessibilities = public, protected
dotnet_naming_symbols.public_or_protected_field.required_modifiers =
dotnet_naming_symbols.public_or_protected_field.required_modifiers =
dotnet_naming_symbols.static_field.applicable_kinds = field
dotnet_naming_symbols.static_field.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
@@ -256,7 +257,7 @@ dotnet_naming_symbols.static_field.required_modifiers = static
dotnet_naming_symbols.private_or_internal_field.applicable_kinds = field
dotnet_naming_symbols.private_or_internal_field.applicable_accessibilities = internal, private, private_protected
dotnet_naming_symbols.private_or_internal_field.required_modifiers =
dotnet_naming_symbols.private_or_internal_field.required_modifiers =
dotnet_naming_symbols.private_or_internal_static_field.applicable_kinds = field
dotnet_naming_symbols.private_or_internal_static_field.applicable_accessibilities = internal, private, private_protected
@@ -264,19 +265,19 @@ dotnet_naming_symbols.private_or_internal_static_field.required_modifiers = stat
dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.types.required_modifiers =
dotnet_naming_symbols.types.required_modifiers =
dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.non_field_members.required_modifiers =
dotnet_naming_symbols.non_field_members.required_modifiers =
dotnet_naming_symbols.parameter.applicable_kinds = parameter
dotnet_naming_symbols.parameter.applicable_accessibilities = *
dotnet_naming_symbols.parameter.required_modifiers =
dotnet_naming_symbols.parameter.required_modifiers =
dotnet_naming_symbols.local.applicable_kinds = local
dotnet_naming_symbols.local.applicable_accessibilities = local
dotnet_naming_symbols.local.required_modifiers =
dotnet_naming_symbols.local.required_modifiers =
dotnet_naming_symbols.constant_field.applicable_kinds = field
dotnet_naming_symbols.constant_field.applicable_accessibilities = *
@@ -284,49 +285,49 @@ dotnet_naming_symbols.constant_field.required_modifiers = const
# Naming styles
dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case
dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix =
dotnet_naming_style.begins_with_i.word_separator =
dotnet_naming_style.begins_with_i.required_suffix =
dotnet_naming_style.begins_with_i.word_separator =
dotnet_naming_style.begins_with_i.capitalization = pascal_case
dotnet_naming_style.begin_with_s.required_prefix = S
dotnet_naming_style.begin_with_s.required_suffix =
dotnet_naming_style.begin_with_s.word_separator =
dotnet_naming_style.begin_with_s.required_suffix =
dotnet_naming_style.begin_with_s.word_separator =
dotnet_naming_style.begin_with_s.capitalization = pascal_case
dotnet_naming_style.starts_with_m_.required_prefix = m_
dotnet_naming_style.starts_with_m_.required_suffix =
dotnet_naming_style.starts_with_m_.word_separator =
dotnet_naming_style.starts_with_m_.required_suffix =
dotnet_naming_style.starts_with_m_.word_separator =
dotnet_naming_style.starts_with_m_.capitalization = pascal_case
dotnet_naming_style.starts_with_s_.required_prefix = s_
dotnet_naming_style.starts_with_s_.required_suffix =
dotnet_naming_style.starts_with_s_.word_separator =
dotnet_naming_style.starts_with_s_.required_suffix =
dotnet_naming_style.starts_with_s_.word_separator =
dotnet_naming_style.starts_with_s_.capitalization = pascal_case
dotnet_naming_style.end_with_delegate.required_prefix =
dotnet_naming_style.end_with_delegate.required_prefix =
dotnet_naming_style.end_with_delegate.required_suffix = Delegate
dotnet_naming_style.end_with_delegate.word_separator =
dotnet_naming_style.end_with_delegate.word_separator =
dotnet_naming_style.end_with_delegate.capitalization = pascal_case
dotnet_naming_style.end_with_event.required_prefix =
dotnet_naming_style.end_with_event.required_prefix =
dotnet_naming_style.end_with_event.required_suffix = Event
dotnet_naming_style.end_with_event.word_separator =
dotnet_naming_style.end_with_event.word_separator =
dotnet_naming_style.end_with_event.capitalization = pascal_case
dotnet_naming_style.camelcase.required_prefix =
dotnet_naming_style.camelcase.required_suffix =
dotnet_naming_style.camelcase.word_separator =
dotnet_naming_style.camelcase.required_prefix =
dotnet_naming_style.camelcase.required_suffix =
dotnet_naming_style.camelcase.word_separator =
dotnet_naming_style.camelcase.capitalization = camel_case
dotnet_naming_style.begins_with_c_.required_prefix = c_
dotnet_naming_style.begins_with_c_.required_suffix =
dotnet_naming_style.begins_with_c_.word_separator =
dotnet_naming_style.begins_with_c_.required_suffix =
dotnet_naming_style.begins_with_c_.word_separator =
dotnet_naming_style.begins_with_c_.capitalization = pascal_case
# ReSharper properties
@@ -381,7 +382,7 @@ resharper_csharp_wrap_chained_method_calls = chop_if_long
resharper_extra_spaces = remove_all
resharper_for_built_in_types = use_var
resharper_indent_preprocessor_region = no_indent
resharper_instance_members_qualify_declared_in =
resharper_instance_members_qualify_declared_in =
resharper_int_align_switch_expressions = false
resharper_int_align_switch_sections = false
resharper_keep_existing_arrangement = true
@@ -476,7 +477,7 @@ dotnet_naming_rule.types_and_namespaces_rule.import_to_resharper = as_predefined
dotnet_naming_rule.types_and_namespaces_rule.severity = none
dotnet_naming_rule.types_and_namespaces_rule.style = pascal_case
dotnet_naming_rule.types_and_namespaces_rule.symbols = types_and_namespaces_symbols
dotnet_naming_symbols.constants_symbols.applicable_accessibilities = public,internal,protected,protected_internal,private_protected
dotnet_naming_symbols.constants_symbols.applicable_accessibilities = public, internal, protected, protected_internal, private_protected
dotnet_naming_symbols.constants_symbols.applicable_kinds = field
dotnet_naming_symbols.constants_symbols.required_modifiers = const
dotnet_naming_symbols.event_symbols.applicable_accessibilities = *
@@ -498,16 +499,16 @@ dotnet_naming_symbols.private_static_fields_symbols.applicable_kinds = field
dotnet_naming_symbols.private_static_fields_symbols.required_modifiers = static
dotnet_naming_symbols.private_static_readonly_symbols.applicable_accessibilities = private
dotnet_naming_symbols.private_static_readonly_symbols.applicable_kinds = field
dotnet_naming_symbols.private_static_readonly_symbols.required_modifiers = static,readonly
dotnet_naming_symbols.private_static_readonly_symbols.required_modifiers = static, readonly
dotnet_naming_symbols.property_symbols.applicable_accessibilities = *
dotnet_naming_symbols.property_symbols.applicable_kinds = property
dotnet_naming_symbols.public_fields_symbols.applicable_accessibilities = public,internal,protected,protected_internal,private_protected
dotnet_naming_symbols.public_fields_symbols.applicable_accessibilities = public, internal, protected, protected_internal, private_protected
dotnet_naming_symbols.public_fields_symbols.applicable_kinds = field
dotnet_naming_symbols.static_readonly_symbols.applicable_accessibilities = public,internal,protected,protected_internal,private_protected
dotnet_naming_symbols.static_readonly_symbols.applicable_accessibilities = public, internal, protected, protected_internal, private_protected
dotnet_naming_symbols.static_readonly_symbols.applicable_kinds = field
dotnet_naming_symbols.static_readonly_symbols.required_modifiers = static,readonly
dotnet_naming_symbols.static_readonly_symbols.required_modifiers = static, readonly
dotnet_naming_symbols.types_and_namespaces_symbols.applicable_accessibilities = *
dotnet_naming_symbols.types_and_namespaces_symbols.applicable_kinds = namespace,class,struct,enum,delegate
dotnet_naming_symbols.types_and_namespaces_symbols.applicable_kinds = namespace, class, struct, enum, delegate
# ReSharper inspection severities
resharper_arrange_accessor_owner_body_highlighting = suggestion

View File

@@ -1,3 +1,3 @@
# Linux-specific instructions
- After every iteration, run `dotnet jb cleanupcode --build=False '$file1' '$file2' ...` for every C# file you touched.
- After every iteration, run `dotnet jb cleanupcode --verbosity:ERROR ./ReactorMaintenance.slnx`.

View File

@@ -1,4 +1,4 @@
# Windows-specific instructions
- After the implementation is finished, run `python D:\Code\crlf.py $file1 $file2 ...` for changed files you recognize, in order to normalize all line endings of all touched files to CRLF.
- After every iteration, run `jb cleanupcode --build=False '$file1' '$file2' ...` for every C# file you touched.
- After every iteration, run `jb cleanupcode --verbosity:ERROR ReactorMaintenance.slnx`.

View File

@@ -1,20 +1,28 @@
# Reactor Maintenance
C# WinUI 3 + Win2D level editor for the deterministic grid simulation described in `docs/design.md`.
## Projects
- `src/ReactorMaintenance.Simulation`: UI-independent level model, editor operations, validation, forecasts, simulation turns, versioned JSON serialization, and deterministic balancing defaults.
- `src/ReactorMaintenance.Win2D`: Win2D editor app for authoring terrain, underground fuel/coolant/electricity networks, props, leaks, doors, surface hazards, robot start, loading/saving levels, ending turns, interacting with props, and activating a ready reactor.
- `tests/ReactorMaintenance.Simulation.Tests`: unit tests for deterministic simulation behavior.
## Commands
```powershell
dotnet test tests\ReactorMaintenance.Simulation.Tests\ReactorMaintenance.Simulation.Tests.csproj
dotnet build src\ReactorMaintenance.Win2D\ReactorMaintenance.Win2D.csproj -p:Platform=x64 -p:EnableWindowsTargeting=true
dotnet run --project src\ReactorMaintenance.Win2D\ReactorMaintenance.Win2D.csproj -p:Platform=x64
```
The WinUI/XAML compiler is Windows-specific. On Linux, the simulation tests run normally, but the Win2D app build must be verified in a Windows-capable environment.
# Reactor Maintenance
C# WinUI 3 + Win2D level editor for the deterministic grid simulation described in `docs/design.md`.
## Projects
- `src/ReactorMaintenance.Simulation`: UI-independent level model, editor operations, validation, forecasts, simulation turns, versioned JSON serialization, and deterministic balancing defaults.
- `src/ReactorMaintenance.Win2D`: Win2D editor app for authoring terrain, underground fuel/coolant/electricity networks, props, explicit leak access faces, door edges, reactor consumer bindings, rule events, surface hazards, robot start, loading/saving levels, ending turns, interacting with props, and activating a ready reactor.
- `tests/ReactorMaintenance.Simulation.Tests`: unit tests for deterministic simulation behavior, validation, serialization, and editor operations.
## Editor Controls
- Left click selects or paints with the current tool. Right click clears the selected cell's prop, surface hazards, leaks, doors, and reactor control.
- Door authoring is explicit: select the Door tool, click the door cell, then click the adjacent floor cell that defines the blocked edge.
- Electricity wall leaks are explicit: select the Electricity Leak tool, click the wall network cell, then click the adjacent floor access face.
- Reactor bindings are explicit: select or place a reactor control, select a matching consumer cell, then use the Fuel, Coolant, or Electric binding action in the inspector.
- Rule event authoring is available from the inspector for next-turn warnings and selected-cell leak events; authored events are saved in the version 2 JSON schema.
## Commands
```powershell
dotnet test tests\ReactorMaintenance.Simulation.Tests\ReactorMaintenance.Simulation.Tests.csproj
dotnet build src\ReactorMaintenance.Win2D\ReactorMaintenance.Win2D.csproj -p:Platform=x64 -p:EnableWindowsTargeting=true
dotnet run --project src\ReactorMaintenance.Win2D\ReactorMaintenance.Win2D.csproj -p:Platform=x64
```
The WinUI/XAML compiler is Windows-specific. On Linux, the simulation tests run normally, but the Win2D app build must be verified in a Windows-capable environment.

View File

@@ -1,11 +1,11 @@
<Solution>
<Folder Name="/src/">
<Project Path="src/ReactorMaintenance.Simulation/ReactorMaintenance.Simulation.csproj" />
<Project Path="src/ReactorMaintenance.Win2D/ReactorMaintenance.Win2D.csproj">
<Platform Project="x86" />
</Project>
</Folder>
<Folder Name="/tests/">
<Project Path="tests/ReactorMaintenance.Simulation.Tests/ReactorMaintenance.Simulation.Tests.csproj" />
</Folder>
<Folder Name="/src/">
<Project Path="src/ReactorMaintenance.Simulation/ReactorMaintenance.Simulation.csproj"/>
<Project Path="src/ReactorMaintenance.Win2D/ReactorMaintenance.Win2D.csproj">
<Platform Project="x86"/>
</Project>
</Folder>
<Folder Name="/tests/">
<Project Path="tests/ReactorMaintenance.Simulation.Tests/ReactorMaintenance.Simulation.Tests.csproj"/>
</Folder>
</Solution>

115
TASKS.md
View File

@@ -1,49 +1,84 @@
# Reactor Maintenance Rewrite Tasks
# Reactor Maintenance Rewrite Tasks
## Current State
- Branch: `design-rewrite`
- Scope approved: implement `docs/design.md` end-to-end with deterministic defaults and no backward compatibility.
- Simulation core has been replaced with the first design-native model and deterministic engine slice.
- Simulation and test projects now target `net10.0` because this Linux environment only has the .NET 10 runtime.
- Win2D editor has been rewritten against the new design model.
- Win2D project now targets `net10.0-windows10.0.19041.0` to match the simulation project.
- Linux can restore and compile the referenced simulation project, but full WinUI/XAML compilation still requires a Windows-capable XAML compiler environment.
- Approved design iteration targets the simulation model, rule removal, action economy, reactor requirements, and editor layer workflow.
- Work is proceeding on branch `design-iteration-structural-editor` in methodical commits.
- Completed commits:
- `787f1e5` Document approved design iteration.
- `3d40617` Restore complete design system documentation.
- `e1ac56d` Rework simulation rules.
- Design documentation must preserve every existing system-level rule unless a change explicitly supersedes it. Superseded sections must document the replacement behavior with equal detail.
- The next implementation iteration is the Win2D editor overhaul.
## Completed Work
- Read project instructions, Linux instructions, code style, and `docs/design.md`.
- Confirmed deterministic balance defaults should be chosen during implementation.
- Confirmed a full Win2D editor is required.
- Created branch `design-rewrite`.
- Added `TASKS.md` as the required per-commit work tracker.
- Removed the legacy integer hazard/effect/hazard plug-in simulation surface.
- Added design-native terrain, underground carrier layers, surface hazards, props, leaks, doors, reactor bindings, robot inventory, rule events, validation, serialization, and forecasts.
- Added deterministic default balancing values.
- Added a first deterministic simulation pipeline for network propagation, consumers, leaks, surface interactions, robot safety, reactor readiness, rule events, and forecasts.
- Replaced old tests with design-based simulation tests.
- Verified `dotnet test tests/ReactorMaintenance.Simulation.Tests/ReactorMaintenance.Simulation.Tests.csproj` passes: 11 passed.
- Attempted `dotnet jb cleanupcode --build=False ...`; unavailable in this environment because `dotnet-jb` is not installed.
- Reviewed the first slice and fixed an action-resolution maintainability issue before commit.
- Verified `git diff --check` reports no whitespace errors.
- Ran `dotnet jb cleanupcode --build=False ...` successfully after ReSharper install and normalized line endings back to LF.
- Reworked the Win2D editor for the new model: full tool list, layer-aware painting, terrain, underground carriers, surface hazards, props, doors, leaks, robot, forecasts, save validation, starter level, and simple play actions.
- Removed old editor dependencies on legacy props, pressure pipes, smoke, fire, and global power/cooling/core-stability fields.
- Verified `dotnet test tests/ReactorMaintenance.Simulation.Tests/ReactorMaintenance.Simulation.Tests.csproj` passes after the editor rewrite: 11 passed.
- Attempted Win2D build on Linux with `dotnet build src/ReactorMaintenance.Win2D/ReactorMaintenance.Win2D.csproj -p:EnableWindowsTargeting=true -p:Platform=x64`; it fails at Windows `XamlCompiler.exe` with exec format error.
- Attempted managed XAML compiler path with `-p:UseXamlCompilerExecutable=false`; it fails loading the WinUI XAML compiler task dependency under this Linux/.NET 10 setup.
- Updated `README.md` for the new design-model editor, .NET 10 target, and Linux/Windows build expectations.
- Created the approved implementation plan for:
- single multi-service consumers,
- count-based reactor requirements,
- cell-derived doors,
- 0-10 structural integrity,
- fixed automatic rule systems,
- quick/lengthy action economy,
- all-seeing-eye viewing without persistent unlocking,
- layer-aware editor visualization and tools.
- Repaired the design documentation after the hazard-interaction regression:
- restored the complete surface hazard interaction matrix,
- documented that leaked coolant plus leaked fuel directly holds unless mediated by heat/electricity,
- expanded structural integrity, consumer, reactor, all-seeing-eye, and action-economy details.
- Reworked simulation state and systems:
- removed data-driven rule predicates/effects/events from runtime state, validation, forecasts, serialization, and tests,
- replaced explicit reactor consumer bindings with unbound reactor controls plus required fuel/coolant/electricity consumer counts,
- made consumer props carrier-agnostic with per-carrier service state derived from networks beneath the cell,
- moved doors from explicit edge state to door props on floor cells with orientation inferred from opposing wall cells,
- added underground structural integrity, high-pressure degradation, automatic leak creation, structural forecasts, and repair-to-max behavior,
- removed action budgets and made movement quick while mutating interactions resolve one simulation step,
- removed persistent all-seeing-eye unlocking from simulation state,
- bumped serialized level schema to version 3.
- Updated simulation tests for the replacement systems:
- consumer derivation and disabled consumer service state,
- count-based reactor readiness and reactor-under-network positive-flow requirement,
- quick movement versus lengthy door interaction,
- inferred door blocking,
- structural degradation, automatic leaks, repair integrity reset,
- schema version 3 round-tripping and old-schema rejection.
- Kept the Win2D project compiling after the simulation model changes with narrow compatibility edits. Full editor workflow/rendering work remains outstanding.
- Verified after the simulation rework:
- `dotnet test tests\ReactorMaintenance.Simulation.Tests\ReactorMaintenance.Simulation.Tests.csproj` passed with 23 tests,
- `dotnet build ReactorMaintenance.slnx` passed with 0 warnings.
- Reworked the Win2D editor workflow:
- added the Surface/Electricity/Fuel/Coolant layer combobox,
- filtered tools by active layer and fixed exclusive tool selection,
- rendered underground networks as carrier-colored centerline networks with source dots and layer opacity rules,
- removed Rule Events, Reactor Binding, and pending workflow panels from the editor UI,
- replaced two-click electricity leak authoring with electric-layer leak access cycling,
- made Shift+left drag pan in all tools and Cursor drag move the robot or props.
- Added editor-helper tests for electricity leak access cycling and cursor drag movement behavior.
- Verified after the editor overhaul:
- `dotnet test tests\ReactorMaintenance.Simulation.Tests\ReactorMaintenance.Simulation.Tests.csproj` passed with 26 tests,
- `dotnet build ReactorMaintenance.slnx` passed with 0 warnings.
## Current Work
- Editor overhaul implementation is complete; commit is pending.
- Commit the Win2D editor rewrite slice.
## Editor Overhaul Requirements
- Add a layer combobox with Surface, Electricity, Fuel, and Coolant.
- When Surface is active, draw the surface layer at full opacity and all underground layers at 25% opacity.
- When an underground layer is active, draw the surface layer at 50% opacity, other underground layers at 25% opacity, and the active underground layer at full opacity.
- Render coolant blue, fuel red, and electricity yellow.
- Render networks as thick lines connecting adjacent cell centers; render sources as large centered dots.
- Make tools layer-aware:
- Cursor is always available.
- Heat, Floor, Walls, Props, Consumers, Hazards, and Doors are only available for Surface.
- Network painting and Sources are only available on their respective underground layers.
- Selecting a tool must deselect all other tools. The current two-way binding can leave multiple tools selected.
- Shift+LMB should pan the view in all tools, including Cursor mode.
- Cursor LMB drag should move any prop or robot from one cell to another.
- Remove Rule Events UI.
- Remove Reactor Binding UI.
- Remove editor workflow and pending actions.
- Door cells are redesigned as single prop cells.
- Electricity leak neighbour should be toggled by using the electric leak tool on an existing electric leak cell.
## Future Work
1. Expand simulation fidelity where the first slice is intentionally simplified: junction branch inference, ambiguity validation, complete pair table coverage, richer rule predicates/effects, and stronger forecast proof cases.
2. Add advanced editor workflows for explicit reactor binding selection, explicit door edge selection, electricity wall leak face selection, and rule event authoring.
3. Verify and polish the Win2D app on Windows where the XAML compiler can run.
4. Update README and any affected docs to reflect the new schema, .NET target, editor controls, and deterministic defaults.
5. Build the Win2D project on a Windows-capable environment after the editor rewrite.
6. Add broader tests for junction ratios, ambiguous junctions, all rule event families, serialization edge cases, and editor operations.
7. Run cleanup when `dotnet-jb` is available, tests, code review, and iterate until the implementation is clean and maintainable.
- Add authored sample levels once the new schema stabilizes.
- Tune structural integrity balancing after playtesting.
- Extend UI affordances for inspecting per-carrier consumer service state.

View File

@@ -1,47 +1,60 @@
# Reactor Maintenance Design
# Reactor Maintenance Design
## Concept
The player controls a maintenance robot inside a failing reactor facility. The game is a deterministic, turn-based systems puzzle about reading a visible machine, forecasting failure, and choosing between local stabilization and longer-term network control.
The simulation uses a small formal core:
The simulation core is built from:
- static floor and wall terrain
- underground fuel, coolant, and electricity networks
- surface props for controls, terminals, supplies, doors, and reactor activation
- reachable leaks that project hazards onto floor cells
- explicit reactor requirements bound to consumer props
- deterministic rule events and forecasts
- static floor and wall terrain,
- underground fuel, coolant, and electricity networks,
- surface props for controls, terminals, supplies, doors, and reactor activation,
- consumers that consume whichever underground services exist under their cell,
- reachable leaks that project hazards onto floor cells,
- transport network structural integrity,
- deterministic fixed simulation rules and forecasts.
The game should feel logical, tactical, readable, and systemic. It should avoid randomness, action pressure, and hidden information after the player earns the relevant diagnostic access.
The game should feel logical, tactical, readable, and systemic. It should avoid randomness, action pressure, and hidden information once the player reaches an all-seeing-eye terminal.
## Turn Structure
## Action Economy
Each turn has three phases:
There is no per-turn action budget. Player choices are either quick or lengthy.
1. Player phase: the player spends the level action budget.
2. Simulation phase: networks, consumers, leaks, hazards, doors, robot safety, and level state resolve.
3. Event phase: rule events, remedy block durations, and heat immunity duration advance.
Quick actions do not mutate the level state and do not advance the simulation:
The simulation advances once after the player spends or ends the turn action budget. Individual player actions do not tick the simulation.
- `MoveRobot`: move the robot to an adjacent floor cell, reduce heat immunity movement steps if applicable, and reject movement into walls or out of bounds,
- selection and inspection: change only UI selection state,
- all-seeing-eye viewing: when the robot is at an all-seeing-eye terminal, allow the player to view every surface and underground layer.
Lengthy actions mutate level state and immediately advance one simulation step:
- `InteractProp`: toggle flow props, toggle consumers, cycle junction ratios, open or close doors, pick up remedy supplies, or activate all-seeing-eye viewing from a terminal,
- `InteractLeak`: repair a reachable leak or apply a matching elemental remedy,
- `ApplyHeatShield`: spend one heat shield and set heat immunity movement steps,
- `ActivateReactor`: activate a ready reactor at the current reactor control prop,
- `Wait`: advance one simulation step without applying another player mutation.
Invalid actions report refusal and do not mutate gameplay state.
## Goal And Failure
Each reactor starts offline. A reactor becomes ready when:
- its bound fuel consumer is enabled, supplied, and producing
- its bound coolant consumer is enabled, supplied, and producing
- its bound electricity consumer is enabled, supplied, and producing
- reactor heat is below the terminal condition
- every underground network present beneath the reactor control cell has positive amount and intensity,
- the level has the required number of enabled, fed, producing fuel consumers,
- the level has the required number of enabled, fed, producing coolant consumers,
- the level has the required number of enabled, fed, producing electricity consumers,
- reactor heat is below the terminal condition.
When a reactor is ready, the level shows `REACTOR READY`. The player wins by spending an action at that reactor control site.
The required consumer counts are level properties. The reactor is not explicitly bound to any consumer positions.
When a reactor is ready, the level shows `REACTOR READY`. The player wins by activating the ready reactor at the reactor control site.
The level is lost when:
- reactor heat reaches the terminal threshold
- the robot occupies an unsafe final hazard state without applicable protection
- a rule event marks terminal failure
- level-authored unrecoverable conditions are met
- reactor heat reaches the terminal threshold,
- the robot occupies an unsafe final hazard state without applicable protection,
- fixed simulation rules mark terminal failure.
Consumer starvation blocks readiness but does not directly cause loss.
@@ -49,17 +62,17 @@ Consumer starvation blocks readiness but does not directly cause loss.
The player can always inspect:
- surface terrain
- surface props and visible prop state
- visible leaks and repair faces
- visible surface hazards
- door state
- remedy inventory and supply props
- consumer state: disabled, starved, supplied, producing, or unknown
- level state
- forecasted warnings the simulation can prove
- surface terrain,
- surface props and visible prop state,
- visible leaks and repair faces,
- visible surface hazards,
- door state,
- remedy inventory and supply props,
- consumer state: disabled, starved, supplied, producing, or unknown,
- level state,
- forecasted warnings the simulation can prove.
Underground topology and numeric network values are hidden until all-seeing-eye terminal access is unlocked. With access, the player can inspect underground fuel, coolant, and electricity topology, network structural state, carrier amount, pressure or voltage, and source-to-consumer connectivity.
Underground topology and numeric network values are available through all-seeing-eye viewing after the robot visits an all-seeing-eye terminal. There is no persistent lock or unlock state in the level data.
The editor always sees and authors every layer.
@@ -69,31 +82,41 @@ Safe, caution, and critical labels are display and forecast bands derived from b
Each map coordinate contains:
- one static surface terrain cell: `Floor` or `Wall`
- zero or one underground fuel cell
- zero or one underground coolant cell
- zero or one underground electricity cell
- zero or one surface prop
- visible hazard amounts on floor cells
- optionally the robot, only on a floor cell
- one static surface terrain cell: `Floor` or `Wall`,
- zero or one underground fuel cell,
- zero or one underground coolant cell,
- zero or one underground electricity cell,
- zero or one surface prop,
- visible hazard amounts on floor cells,
- optionally the robot, only on a floor cell.
Terrain is authored and does not change during play. Wall cells are not walkable and do not store surface hazards.
Underground cells use one structural state:
- `Absent`
- `Intact`
- `Leaking`
- `Absent`,
- `Intact`,
- `Leaking`.
Underground cells store carrier amount plus pressure for fuel/coolant or voltage for electricity. Same-carrier underground cells connect by inferred cardinal adjacency.
Underground cells store carrier amount, pressure or voltage intensity, and structural integrity on a 0-10 scale. Max structural integrity supports the highest pressure. Non-max integrity under high pressure worsens proportionally to excess pressure. Low integrity with positive pressure creates a leak. Repairing a leak restores integrity to max.
Structural integrity is persistent authored and runtime state:
- absent cells have no meaningful structural integrity,
- newly authored present cells start at max structural integrity,
- network propagation updates amount and intensity but does not reset integrity,
- structural integrity cannot exceed max and cannot fall below zero,
- existing leaks keep their current integrity until repaired.
Same-carrier underground cells connect by inferred cardinal adjacency.
Surface floor cells store:
- leaked fuel
- leaked coolant
- leaked electricity
- heat
- active elemental remedy blocks
- leaked fuel,
- leaked coolant,
- leaked electricity,
- heat,
- active elemental remedy blocks.
Simulation values use C# `float`. Runtime values are clamped and retain full float precision. UI shows visible values rounded to one decimal plus the safe/caution/critical band.
@@ -112,14 +135,13 @@ The derived level states are:
Surface prop categories:
- flow prop
- consumer prop
- T-junction prop
- cross-junction prop
- door prop
- all-seeing-eye terminal prop
- remedy supply prop
- reactor control prop
- flow prop,
- consumer prop,
- junction prop,
- door prop,
- all-seeing-eye terminal prop,
- remedy supply prop,
- reactor control prop.
Props exist on floor cells. Props do not directly participate in the surface hazard pair table.
@@ -131,65 +153,62 @@ During network flow, an enabled flow prop injects source carrier amount and pres
### Consumer Props
A consumer prop is bound to fuel, coolant, or electricity. It can be `Enabled` or `Disabled`.
A consumer prop can be `Enabled` or `Disabled`.
An enabled consumer derives one service state after network propagation:
An enabled consumer derives one service state per underground network present beneath it:
- `Supplied`: enough carrier and pressure or voltage reaches the bound underground cell.
- `Supplied`: enough carrier and pressure or voltage reaches the underground cell.
- `Starved`: supply predicates fail.
- `Producing`: the consumer was supplied this simulation step and emits service.
A disabled consumer consumes nothing, produces nothing, and cannot satisfy reactor readiness.
A disabled consumer consumes nothing, produces nothing, and cannot satisfy reactor readiness. A consumer on no underground layer is valid but produces no service and contributes no readiness requirement. A consumer on one underground layer consumes that service. A consumer on multiple underground layers consumes all present layers and can satisfy one requirement for each carrier that is producing.
The aggregate consumer prop still exposes a single switch state. Per-carrier service state is derived from the underground layers beneath the prop and is used by reactor readiness, forecasts, and inspection. If any consumed carrier is starved, the consumer contributes a warning for that carrier without blocking other carriers on the same prop from producing.
### Reactor Control Props
A reactor control prop is the activation site for one reactor. Each reactor stores required consumer bindings by grid position:
A reactor control prop is the activation site for one reactor. Reactor readiness is derived from level-level consumer count requirements and the networks beneath the reactor control cell.
- fuel consumer position
- coolant consumer position
- electricity consumer position
The reactor control prop itself is not bound to any individual consumer. It is considered a local consumer for any underground network present beneath its cell:
The level is invalid if any binding is missing, out of bounds, or points to the wrong prop type.
- if no underground layer is present beneath the reactor, the local reactor feed requirement is satisfied,
- if one or more underground layers are present, every present layer must have positive amount and positive intensity after network propagation,
- a present but starved reactor-under-network blocks readiness but does not directly lose the level.
Level properties define `RequiredFuelConsumers`, `RequiredCoolantConsumers`, and `RequiredElectricityConsumers`. For each carrier, readiness requires at least that many enabled consumer props whose per-carrier service state is `Producing`.
### Junction Props
A junction prop must be on a floor cell whose coordinate has exactly one underground carrier. That carrier is the regulated network.
T-junction modes:
- `0/4`
- `1/3`
- `2/2`
- `3/1`
- `4/0`
Cross-junction modes:
- `0/3/3`
- `3/0/3`
- `3/3/0`
- `2/2/2`
The engine infers incoming and outgoing branch directions from valid network topology and enabled source paths. Ambiguous junction flow is invalid. Ratio numbers are weights that divide carrier amount and pressure or voltage by balance-defined math. A zero-weight branch receives no intentional outflow.
The gameplay UI exposes ratio modes.
The engine infers incoming and outgoing branch directions from valid network topology and enabled source paths. A valid junction has one incoming branch and either two or three outgoing branches. Ambiguous junction flow is invalid. Ratio numbers are balance-defined weights that divide carrier amount and pressure or voltage. A zero-weight branch receives no intentional outflow.
### Doors
A door stores one edge between two adjacent floor cells. Door states are `Open` and `Closed`.
A door is a prop on one floor cell. Its orientation is derived from opposing wall cells:
Closed doors block fuel, coolant, electricity, and heat propagation across their edge. They do not block robot movement, underground network flow, source feeding, consumer supply, or hazards already present on either side.
- north and south walls mean the door sits in an east-west corridor and blocks west/east propagation while closed,
- west and east walls mean the door sits in a north-south corridor and blocks north/south propagation while closed.
A door must have exactly one valid opposing wall pair. Closed doors block fuel, coolant, electricity, and heat propagation across the corridor cell. They do not block robot movement, underground network flow, source feeding, consumer supply, or hazards already present on either side.
Door blocking is evaluated by the door cell and its inferred corridor axis:
- east-west corridor doors block surface interaction between the west neighbor, the door cell, and the east neighbor while closed,
- north-south corridor doors block surface interaction between the north neighbor, the door cell, and the south neighbor while closed,
- open doors do not block surface interaction,
- door props on invalid terrain or with ambiguous opposing walls are validation errors.
### Terminals And Supplies
An all-seeing-eye terminal unlocks underground inspection for the level.
An all-seeing-eye terminal allows full underground inspection when visited.
Remedy supply props are single-use pickups:
- `FuelRemedySupply`
- `CoolantRemedySupply`
- `ElectricityRemedySupply`
- `HeatRemedySupply`
- `FuelRemedySupply`,
- `CoolantRemedySupply`,
- `ElectricityRemedySupply`,
- `HeatRemedySupply`.
Each supply provides one matching inventory item and then becomes depleted.
@@ -199,45 +218,30 @@ Each leak stores carrier type, underground coordinate, accessible floor coordina
Fuel and coolant leaks:
- occur under floor cells
- use the same coordinate as their accessible floor coordinate
- can be repaired or remediated by the robot standing on that floor cell
- occur under floor cells,
- use the same coordinate as their accessible floor coordinate,
- can be repaired or remediated by the robot standing on that floor cell.
Electricity leaks:
- occur in wall cells
- store exactly one adjacent floor cell as the emission face
- can be repaired or remediated from that floor cell
- emit only to that stored face
- occur in wall cells,
- store exactly one adjacent floor cell as the emission face,
- can be repaired or remediated from that floor cell,
- emit only to that stored face.
All leaks must have valid floor access. Repair changes the underground cell from `Leaking` to `Intact` and stops future injection. Repair does not clean existing surface hazards.
All leaks must have valid floor access. Repair changes the underground cell from `Leaking` to `Intact`, restores structural integrity to max, and stops future injection. Repair does not clean existing surface hazards.
The robot carries remedial consumables with balance-defined inventory capacity:
- `FuelNeutralizer`
- `CoolantNeutralizer`
- `ElectricityNeutralizer`
- `HeatShield`
- `FuelNeutralizer`,
- `CoolantNeutralizer`,
- `ElectricityNeutralizer`,
- `HeatShield`.
Element neutralizers remove the matching surface element from a target floor cell or reachable leak face, then apply a temporary same-element re-entry block. They do not remove other elements, reduce heat, or repair leaks.
Heat shield gives the robot heat immunity for a balance-defined number of movement steps. It does not remove heat, block heat propagation, or protect against fuel, coolant, or electricity hazards.
## Player Actions
Player actions:
- `MoveRobot`: move to an adjacent floor cell.
- `InteractProp`: use the prop on the robot's current floor cell.
- `InteractLeak`: repair a reachable leak or apply a matching elemental remedy.
- `ApplyHeatShield`: activate a carried heat shield.
- `ActivateReactor`: activate a ready reactor at the current reactor control prop.
- `EndTurn`: forfeit remaining actions and run the simulation/event phases.
Each valid action costs one action unless balance or level data defines otherwise. Invalid actions report refusal and do not mutate gameplay state.
`InteractProp` covers flow toggles, consumer toggles, junction ratio changes, door toggles, terminal unlock, and remedy pickup.
## Network Flow
Network flow runs independently for fuel, coolant, and electricity.
@@ -263,114 +267,127 @@ Leaking underground cells remain part of network propagation.
During leak injection:
- fuel leaks add leaked fuel to the accessible floor cell
- coolant leaks add leaked coolant to the accessible floor cell
- electricity leaks add leaked electricity to the stored floor emission face
- active matching remedy blocks prevent matching element entry
- fuel leaks add leaked fuel to the accessible floor cell,
- coolant leaks add leaked coolant to the accessible floor cell,
- electricity leaks add leaked electricity to the stored floor emission face,
- active matching remedy blocks prevent matching element entry.
Injection magnitude is balance data and may depend on local carrier amount, pressure, or voltage.
Injection magnitude is balance data and may depend on local carrier amount, pressure, or voltage. If the target floor cell has an active matching remedy block, that matching element is not injected into the cell for that step. Remedy blocks do not block other elements or heat.
After injection, the engine evaluates local interactions between leaked fuel, leaked coolant, leaked electricity, and heat on the same floor cell and across unblocked adjacent floor cells.
Surface interaction resolution is deterministic:
- same-cell interactions evaluate every unordered quantity pair on each floor cell,
- adjacent interactions evaluate every unordered pair of adjacent floor cells once,
- same-carrier leaked fuel, coolant, electricity, and heat equalize across adjacent floor cells using `Flow(amount)`,
- wall cells never store surface hazards and do not participate,
- closed doors and remedy blocks gate the interactions they explicitly block,
- deltas accumulate during an interaction pass and are applied together before clamping.
## Hazard Bands And Pair Table
Balance thresholds project numeric values into safe, caution, and critical bands:
- `FuelSafe`, `FuelCaution`, `FuelCritical`
- `CoolantSafe`, `CoolantCaution`, `CoolantCritical`
- `ElectricitySafe`, `ElectricityCaution`, `ElectricityCritical`
- `HeatSafe`, `HeatCaution`, `HeatCritical`
- `FuelSafe`, `FuelCaution`, `FuelCritical`,
- `CoolantSafe`, `CoolantCaution`, `CoolantCritical`,
- `ElectricitySafe`, `ElectricityCaution`, `ElectricityCritical`,
- `HeatSafe`, `HeatCaution`, `HeatCritical`.
The pair table maps projected bands to deterministic effects:
The pair table maps projected bands to parameterized verbs:
- `Hold`: no direct change.
- `FuelFlow`: equalize leaked fuel.
- `CoolFlow`: equalize leaked coolant.
- `ChargeFlow`: equalize leaked electricity.
- `HeatFlow`: transfer heat.
- `HeatFlow2`: stronger heat transfer.
- `Warm1`: small heat increase from fuel exposure.
- `Warm2`: larger heat increase from fuel exposure.
- `Quench1`: small heat reduction from coolant exposure.
- `Quench2`: larger heat reduction from coolant exposure.
- `Short1`: electricity and coolant interaction causing heat and discharge.
- `Short2`: stronger short.
- `Ignite1`: fuel interaction with electricity or high heat.
- `Ignite2`: stronger ignition.
- `Hold`: no direct change,
- `Flow(amount)`: equalize a surface quantity by a balance-defined transfer amount,
- `Warm(amount)`: increase heat by a balance-defined amount,
- `Quench(amount)`: reduce heat by a balance-defined amount,
- `Short(heat, discharge)`: add heat and discharge electricity by balance-defined amounts,
- `Ignite(heat, fuel)`: add heat and consume fuel by balance-defined amounts.
| Row\Col | FuelSafe | FuelCaution | FuelCritical | CoolantSafe | CoolantCaution | CoolantCritical | ElectricitySafe | ElectricityCaution | ElectricityCritical | HeatSafe | HeatCaution | HeatCritical |
| ------- | -------- | ----------- | ------------ | ----------- | -------------- | --------------- | --------------- | ------------------ | ------------------- | -------- | ----------- | ------------ |
| FuelSafe | Hold | FuelFlow | FuelFlow | Hold | Hold | Hold | Hold | Warm1 | Warm2 | Hold | Warm1 | Warm2 |
| FuelCaution | | Hold | FuelFlow | Hold | Hold | Hold | Warm1 | Ignite1 | Ignite2 | Warm1 | Ignite1 | Ignite2 |
| FuelCritical | | | Hold | Hold | Hold | Hold | Warm2 | Ignite2 | Ignite2 | Warm2 | Ignite2 | Ignite2 |
| CoolantSafe | | | | Hold | CoolFlow | CoolFlow | Hold | Short1 | Short2 | Hold | Quench1 | Quench1 |
| CoolantCaution | | | | | Hold | CoolFlow | Short1 | Short1 | Short2 | Hold | Quench1 | Quench2 |
| CoolantCritical | | | | | | Hold | Short2 | Short2 | Short2 | Hold | Quench2 | Quench2 |
| ElectricitySafe | | | | | | | Hold | ChargeFlow | ChargeFlow | Hold | Hold | Hold |
| ElectricityCaution | | | | | | | | Hold | ChargeFlow | Hold | Hold | Hold |
| FuelSafe | Hold | Flow | Flow | Hold | Hold | Hold | Hold | Warm | Ignite | Hold | Warm | Ignite |
| FuelCaution | | Hold | Flow | Hold | Hold | Hold | Warm | Ignite | Ignite | Warm | Ignite | Ignite |
| FuelCritical | | | Hold | Hold | Hold | Hold | Ignite | Ignite | Ignite | Ignite | Ignite | Ignite |
| CoolantSafe | | | | Hold | Flow | Flow | Hold | Short | Short | Hold | Quench | Quench |
| CoolantCaution | | | | | Hold | Flow | Short | Short | Short | Hold | Quench | Quench |
| CoolantCritical | | | | | | Hold | Short | Short | Short | Hold | Quench | Quench |
| ElectricitySafe | | | | | | | Hold | Flow | Flow | Hold | Hold | Hold |
| ElectricityCaution | | | | | | | | Hold | Flow | Hold | Hold | Hold |
| ElectricityCritical | | | | | | | | | Hold | Hold | Hold | Hold |
| HeatSafe | | | | | | | | | | Hold | HeatFlow | HeatFlow2 |
| HeatCaution | | | | | | | | | | | Hold | HeatFlow |
| HeatSafe | | | | | | | | | | Hold | Flow | Flow |
| HeatCaution | | | | | | | | | | | Hold | Flow |
| HeatCritical | | | | | | | | | | | | Hold |
Blank lower-triangle entries mirror the corresponding upper-triangle entry. Fuel and coolant do not directly react with each other; leaked coolant plus leaked fuel is `Hold` unless another pair on that cell or an adjacent cell involves heat, electricity, or same-carrier flow.
Design rules:
- fuel becomes dangerous through electricity or heat
- coolant becomes dangerous through electricity
- coolant opposes heat
- heat equalizes between neighboring floor cells
- same-carrier leaked surface amounts equalize between neighboring floor cells
- doors and remedy blocks gate local interactions
- fuel becomes dangerous through electricity or heat,
- coolant becomes dangerous through electricity,
- coolant opposes heat,
- heat equalizes between neighboring floor cells,
- same-carrier leaked surface amounts equalize between neighboring floor cells,
- doors and remedy blocks gate local interactions.
## Rule Events
## Structural Integrity
Rule events are deterministic and forecastable. Each event stores enabled state, once/repeat behavior, priority, predicates, effects, and optional forecast text. All predicates must pass for the event to trigger.
Structural integrity is resolved after network propagation and before leak injection. It is deterministic and uses balancing values:
Predicate families:
- `MaxStructuralIntegrity`: the maximum integrity value, `10` for the approved scale,
- `StructuralIntegrityHighIntensityThreshold`: the pressure or voltage threshold above which a weakened cell degrades,
- `StructuralIntegrityDamageScale`: difficulty-specific multiplier, initially `0.25` in Normal difficulty,
- `StructuralIntegrityLeakThreshold`: integrity at or below this value can become a leak when positive intensity exists.
- turn comparisons
- level state comparisons
- reactor readiness/loss/win comparisons
- prop type or state at position
- consumer service state at position
- network value band at position
- surface hazard band at position
- robot position or inventory condition
- all-seeing-eye unlocked state
For every present underground cell:
Effects:
1. If the cell is already max integrity, high intensity does not weaken it during that step.
2. If integrity is below max and intensity is greater than the high threshold, integrity is reduced by `(intensity - threshold) * StructuralIntegrityDamageScale`.
3. Integrity is clamped to the 0-10 range.
4. If the final integrity is at or below the leak threshold and intensity is positive, the cell becomes `Leaking` and a `LeakState` is created if one does not already exist.
- start a valid leak
- worsen an existing leak
- repair or disable a network cell
- enable or disable a prop
- add or remove surface hazard
- add or remove heat
- add or remove inventory
- mark terminal loss
- emit warning text
Automatic leak access follows the same rules as authored leaks:
- fuel and coolant leaks use the underground cell as their floor access and can only auto-start under floor cells,
- electricity leaks require one adjacent floor access face; if multiple valid faces exist, the deterministic order is north, east, south, west,
- if no valid floor access exists, the weakened cell remains damaged but no reachable leak state is created.
Repairing a leak sets the underground cell to `Intact`, sets structural integrity to max, marks the leak repaired, and leaves existing surface hazards unchanged.
## Fixed Rule Systems
Data-driven rule predicates and effects are not part of level data. Effects happen through fixed systems:
- player-issued lengthy interactions toggle props, cycle junctions, use inventory, open or close doors, repair leaks, and activate reactors,
- network propagation clears transient amount and intensity, then recomputes flow from enabled sources,
- consumer resolution derives per-carrier service states from present underground layers,
- structural integrity resolution weakens damaged high-pressure cells and creates leaks from low-integrity positive-pressure cells,
- leak injection adds carrier hazards to valid floor access cells,
- surface interaction resolution spreads and reacts hazards according to the hazard pair table,
- robot safety resolves terminal loss from unsafe final hazard states after surface interactions,
- reactor state derives readiness or terminal heat loss,
- duration advancement reduces remedy blocks and heat immunity counters,
- forecast refresh simulates copied state over the configured horizon.
Warnings are generated by fixed forecast and status systems when conditions can be proven.
## Simulation Order
One completed turn resolves in this order:
One lengthy interaction resolves in this order:
1. Apply accepted player actions.
1. Apply the accepted player mutation.
2. Validate runtime state.
3. Apply matching start-of-simulation rule events.
4. Propagate underground networks.
5. Resolve consumers and service production.
3. Propagate underground networks.
4. Resolve consumers and service production.
5. Resolve structural integrity and automatic leak creation.
6. Inject leaks.
7. Evaluate same-cell surface interactions.
8. Evaluate adjacent floor interactions across unblocked edges.
8. Evaluate adjacent floor interactions across unblocked door cells.
9. Accumulate and apply deltas in deterministic priority order.
10. Clamp values.
11. Resolve robot safety.
12. Derive reactor readiness and level state.
13. Apply matching end-of-turn rule events.
14. Advance remedy blocks and heat immunity.
15. Refresh forecasts.
If multiple events modify the same value in one step, deltas accumulate and then clamp. Event priority may consume, reserve, or block a value first.
13. Advance remedy blocks and heat immunity.
14. Refresh forecasts.
## Forecasts
@@ -378,11 +395,11 @@ Forecasts are deterministic simulations over copied state. Forecasting does not
Forecast output includes:
- terminal loss forecasts
- reactor ready forecasts
- starved required consumer warnings
- growing hazard warnings when values cross caution or critical bands
- rule event warnings when predicates can be proven within the forecast horizon
- terminal loss forecasts,
- reactor ready forecasts,
- starved required consumer warnings,
- growing hazard warnings when values cross caution or critical bands,
- structural integrity leak warnings when weakened cells are expected to leak.
The forecast horizon is balance data.
@@ -392,51 +409,60 @@ The editor blocks run and save when validation errors exist. Warnings are visibl
Validation errors:
- invalid dimensions or cell counts
- robot out of bounds or not on floor
- wall cell with surface hazards
- prop on invalid terrain
- missing or invalid reactor consumer binding
- invalid door edge
- invalid leak access
- invalid rule target
- junction without exactly one underground carrier
- ambiguous junction flow
- network loop or equal-source ambiguity at a junction
- malformed required data
- invalid dimensions or cell counts,
- robot out of bounds or not on floor,
- wall cell with surface hazards,
- prop on invalid terrain,
- invalid required consumer counts,
- invalid door cell,
- invalid leak access,
- junction without exactly one underground carrier,
- ambiguous junction flow,
- network loop or equal-source ambiguity at a junction,
- malformed required data.
Validation warnings:
- unreachable non-required consumer
- underground cell with no source path
- initially starved required consumer
- initially unready reactor
- unused remedy supply
- visible hazard with no detectable nearby remedy or route
- unreachable non-required consumer,
- underground cell with no source path,
- initially starved required consumer,
- initially unready reactor,
- unused remedy supply,
- visible hazard with no detectable nearby remedy or route.
## Editor And Schema
The editor authors:
- surface terrain
- underground fuel, coolant, and electricity cells
- flow props and consumer props
- reactor controls and reactor consumer bindings
- junction props and ratio modes
- door props and door edges
- all-seeing-eye terminals
- remedy supplies
- floor leaks and electricity wall leaks
- initial surface hazards and heat
- robot start position
- rule events
- surface terrain,
- underground fuel, coolant, and electricity cells,
- flow props,
- multi-service consumer props,
- required fuel, coolant, and electricity consumer counts,
- junction props and balance-defined ratio mode index,
- door props,
- all-seeing-eye terminals,
- remedy supplies,
- floor leaks and electricity wall leaks with authored access faces,
- initial surface hazards and heat,
- robot start position.
The serialized level schema stores level metadata, dimensions, terrain, underground layers, props and prop state, reactor bindings, leaks, doors, robot state, inventory, rule events, all-seeing-eye state, and dynamic state when saving active play.
The editor includes layer selection for Surface, Electricity, Fuel, and Coolant:
- Surface active: surface is full opacity, all underground layers are 25% opacity.
- Underground active: surface is 50% opacity, inactive underground layers are 25% opacity, active underground layer is full opacity.
- Coolant renders blue, fuel red, electricity yellow.
- Networks render as thick lines connecting adjacent cell centers; sources render as large centered dots.
- Tools are layer-aware. Cursor is always available. Surface terrain, props, consumers, hazards, doors, and heat tools are available only on Surface. Network painting and sources are available only on their matching underground layer.
Editor tool badges and drag previews use stable semantic image keys when assets are available. Assets may be added under `Images/Badges` or `Images/Elements` with filenames such as `tool-door.png`, `prop-reactor.png`, `carrier-fuel-source.png`, `leak-electricity.png`, or `robot.png`; missing assets fall back to compact procedural badges and text labels.
The serialized level schema stores level metadata, dimensions, terrain, underground layers including structural integrity, props and prop state, required reactor consumer counts, leaks, robot state, inventory, forecasts, and dynamic state when saving active play.
The loader accepts only schema-valid level data and returns clear errors for malformed data.
## Balancing And Tests
Balancing defines source strengths, falloff, ratio math, consumer predicates, leak magnitudes, interaction magnitudes, display thresholds, robot safety thresholds, terminal heat thresholds, inventory capacity, remedy duration, heat immunity duration, action costs, and forecast horizon.
Balancing defines source strengths, falloff, ratio math, consumer predicates, leak magnitudes, structural integrity thresholds and damage scale, interaction magnitudes, display thresholds, robot safety thresholds, terminal heat thresholds, inventory capacity, remedy duration, heat immunity duration, and forecast horizon.
Tests assert behavior against configured balance values and bands. Coverage includes validation, inferred connectivity, junction effects, consumer states, reactor readiness and activation, terminal loss, robot hazard loss, heat immunity, leak access, remedies, door blocking, rule events, forecasts, and serialization round trips.
Tests assert behavior against configured balance values and bands. Coverage includes validation, inferred connectivity, junction effects, multi-service consumer states, reactor readiness and activation, terminal loss, robot hazard loss, heat immunity, structural integrity degradation and leak creation, leak access, remedies, door blocking, forecasts, and serialization round trips.

View File

@@ -1,11 +1,9 @@
using ReactorMaintenance.Simulation.Difficulties;
using ReactorMaintenance.Simulation.Difficulties;
namespace ReactorMaintenance.Simulation;
public abstract class Balancing
{
public static Balancing Current { get; set; } = new NormalBalancing();
public float ClampValue(float value)
{
return Math.Clamp(value, MinValue, MaxValue);
@@ -19,10 +17,109 @@ public abstract class Balancing
return value >= caution ? EBand.Caution : EBand.Safe;
}
public IReadOnlyList<JunctionRatioPreset> JunctionRatios(int outflowCount)
{
return outflowCount switch {
2 => TwoOutflowJunctionRatios,
3 => ThreeOutflowJunctionRatios,
_ => Array.Empty<JunctionRatioPreset>()
};
}
public float[] JunctionWeights(int outflowCount, int mode)
{
var ratios = JunctionRatios(outflowCount);
if (ratios.Count == 0)
return Array.Empty<float>();
return ratios[Math.Clamp(mode, 0, ratios.Count - 1)].Weights;
}
public SurfaceInteractionEffect SameCellInteraction(ECarrierType? rowCarrier, EBand rowBand, ECarrierType? colCarrier, EBand colBand)
{
if (rowBand == EBand.Safe && colBand == EBand.Safe)
return SurfaceInteractionEffect.Hold;
if (rowCarrier == ECarrierType.Fuel && colCarrier == ECarrierType.Electricity)
return Ignite(rowBand, colBand);
if (rowCarrier == ECarrierType.Fuel && colCarrier is null)
return rowBand == EBand.Critical || colBand == EBand.Critical ? Ignite(rowBand, colBand) : Warm(rowBand, colBand);
if (rowCarrier == ECarrierType.Coolant && colCarrier == ECarrierType.Electricity)
return Short(rowBand, colBand);
if (rowCarrier == ECarrierType.Coolant && colCarrier is null)
return Quench(rowBand, colBand);
return SurfaceInteractionEffect.Hold;
}
public SurfaceInteractionEffect FlowInteraction(ESurfaceQuantity quantity)
{
return new() { Verb = ESurfaceInteractionVerb.Flow, Quantity = quantity, Amount = FlowTransferRatio };
}
public float StructuralPressureThreshold(ECarrierType carrier)
{
return carrier switch {
ECarrierType.Fuel => FuelCritical,
ECarrierType.Coolant => CoolantCritical,
ECarrierType.Electricity => ElectricityCritical,
_ => MaxValue
};
}
private SurfaceInteractionEffect Warm(EBand rowBand, EBand colBand)
{
return new() {
Verb = ESurfaceInteractionVerb.Warm,
Quantity = ESurfaceQuantity.Heat,
Amount = Strongest(rowBand, colBand) == EBand.Critical ? WarmCriticalAmount : WarmCautionAmount
};
}
private SurfaceInteractionEffect Quench(EBand rowBand, EBand colBand)
{
return new() {
Verb = ESurfaceInteractionVerb.Quench,
Quantity = ESurfaceQuantity.Heat,
Amount = Strongest(rowBand, colBand) == EBand.Critical ? QuenchCriticalAmount : QuenchCautionAmount
};
}
private SurfaceInteractionEffect Short(EBand rowBand, EBand colBand)
{
var critical = Strongest(rowBand, colBand) == EBand.Critical;
return new() {
Verb = ESurfaceInteractionVerb.Short,
Quantity = ESurfaceQuantity.Electricity,
Amount = critical ? ShortCriticalHeat : ShortCautionHeat,
SecondaryAmount = critical ? ShortCriticalDischarge : ShortCautionDischarge
};
}
private SurfaceInteractionEffect Ignite(EBand rowBand, EBand colBand)
{
var critical = Strongest(rowBand, colBand) == EBand.Critical;
return new() {
Verb = ESurfaceInteractionVerb.Ignite,
Quantity = ESurfaceQuantity.Fuel,
Amount = critical ? IgniteCriticalHeat : IgniteCautionHeat,
SecondaryAmount = critical ? IgniteCriticalFuelConsumption : IgniteCautionFuelConsumption
};
}
private static EBand Strongest(EBand a, EBand b)
{
return a > b ? a : b;
}
public static Balancing Current { get; set; } = new NormalBalancing();
public abstract int DefaultLevelWidth { get; }
public abstract int DefaultLevelHeight { get; }
public abstract int MinimumLevelSize { get; }
public abstract int ActionsPerTurn { get; }
public abstract int ForecastHorizon { get; }
public abstract float MinValue { get; }
public abstract float MaxValue { get; }
@@ -47,25 +144,29 @@ public abstract class Balancing
public abstract float SourceIntensity { get; }
public abstract float DistanceAmountFalloff { get; }
public abstract float DistanceIntensityFalloff { get; }
public abstract IReadOnlyList<JunctionRatioPreset> TwoOutflowJunctionRatios { get; }
public abstract IReadOnlyList<JunctionRatioPreset> ThreeOutflowJunctionRatios { get; }
public abstract float ConsumerRequiredAmount { get; }
public abstract float ConsumerRequiredIntensity { get; }
public abstract int MaxStructuralIntegrity { get; }
public abstract int StructuralIntegrityLeakThreshold { get; }
public abstract float StructuralIntegrityDamageScale { get; }
public abstract float LeakBaseAmount { get; }
public abstract float LeakAmountScale { get; }
public abstract float LeakIntensityScale { get; }
public abstract float FlowTransferRatio { get; }
public abstract float StrongFlowTransferRatio { get; }
public abstract float Warm1Amount { get; }
public abstract float Warm2Amount { get; }
public abstract float Quench1Amount { get; }
public abstract float Quench2Amount { get; }
public abstract float Short1Heat { get; }
public abstract float Short1Discharge { get; }
public abstract float Short2Heat { get; }
public abstract float Short2Discharge { get; }
public abstract float Ignite1Heat { get; }
public abstract float Ignite1FuelConsumption { get; }
public abstract float Ignite2Heat { get; }
public abstract float Ignite2FuelConsumption { get; }
public abstract float WarmCautionAmount { get; }
public abstract float WarmCriticalAmount { get; }
public abstract float QuenchCautionAmount { get; }
public abstract float QuenchCriticalAmount { get; }
public abstract float ShortCautionHeat { get; }
public abstract float ShortCautionDischarge { get; }
public abstract float ShortCriticalHeat { get; }
public abstract float ShortCriticalDischarge { get; }
public abstract float IgniteCautionHeat { get; }
public abstract float IgniteCautionFuelConsumption { get; }
public abstract float IgniteCriticalHeat { get; }
public abstract float IgniteCriticalFuelConsumption { get; }
public abstract int RemedyBlockTurns { get; }
public abstract int HeatShieldSteps { get; }
public abstract int InventoryCapacityPerRemedy { get; }

View File

@@ -1,11 +1,10 @@
namespace ReactorMaintenance.Simulation.Difficulties;
namespace ReactorMaintenance.Simulation.Difficulties;
public class NormalBalancing : Balancing
{
public override int DefaultLevelWidth => 16;
public override int DefaultLevelHeight => 12;
public override int MinimumLevelSize => 4;
public override int ActionsPerTurn => 3;
public override int ForecastHorizon => 6;
public override float MinValue => 0;
public override float MaxValue => 10;
@@ -30,25 +29,43 @@ public class NormalBalancing : Balancing
public override float SourceIntensity => 8;
public override float DistanceAmountFalloff => 0.5f;
public override float DistanceIntensityFalloff => 0.4f;
public override IReadOnlyList<JunctionRatioPreset> TwoOutflowJunctionRatios { get; } = [
new("0/4", [0, 1]),
new("1/3", [0.25f, 0.75f]),
new("2/2", [0.5f, 0.5f]),
new("3/1", [0.75f, 0.25f]),
new("4/0", [1, 0])
];
public override IReadOnlyList<JunctionRatioPreset> ThreeOutflowJunctionRatios { get; } = [
new("0/3/3", [0, 0.5f, 0.5f]),
new("3/0/3", [0.5f, 0, 0.5f]),
new("3/3/0", [0.5f, 0.5f, 0]),
new("2/2/2", [1f / 3f, 1f / 3f, 1f / 3f])
];
public override float ConsumerRequiredAmount => 2.5f;
public override float ConsumerRequiredIntensity => 2.5f;
public override int MaxStructuralIntegrity => 10;
public override int StructuralIntegrityLeakThreshold => 2;
public override float StructuralIntegrityDamageScale => 0.35f;
public override float LeakBaseAmount => 0.5f;
public override float LeakAmountScale => 0.15f;
public override float LeakIntensityScale => 0.1f;
public override float FlowTransferRatio => 0.05f;
public override float StrongFlowTransferRatio => 0.1f;
public override float Warm1Amount => 0.5f;
public override float Warm2Amount => 1.0f;
public override float Quench1Amount => 0.6f;
public override float Quench2Amount => 1.2f;
public override float Short1Heat => 0.8f;
public override float Short1Discharge => 0.8f;
public override float Short2Heat => 1.6f;
public override float Short2Discharge => 1.5f;
public override float Ignite1Heat => 1.2f;
public override float Ignite1FuelConsumption => 0.4f;
public override float Ignite2Heat => 2.4f;
public override float Ignite2FuelConsumption => 0.8f;
public override float WarmCautionAmount => 0.5f;
public override float WarmCriticalAmount => 1.0f;
public override float QuenchCautionAmount => 0.6f;
public override float QuenchCriticalAmount => 1.2f;
public override float ShortCautionHeat => 0.8f;
public override float ShortCautionDischarge => 0.8f;
public override float ShortCriticalHeat => 1.6f;
public override float ShortCriticalDischarge => 1.5f;
public override float IgniteCautionHeat => 1.2f;
public override float IgniteCautionFuelConsumption => 0.4f;
public override float IgniteCriticalHeat => 2.4f;
public override float IgniteCriticalFuelConsumption => 0.8f;
public override int RemedyBlockTurns => 2;
public override int HeatShieldSteps => 3;
public override int InventoryCapacityPerRemedy => 3;

View File

@@ -0,0 +1,20 @@
namespace ReactorMaintenance.Simulation;
public enum EEditorTool
{
Cursor,
Floor,
Wall,
Underground,
Flow,
Consumer,
Junction,
Door,
AllSeeingEyeTerminal,
RemedySupply,
ReactorControl,
Leak,
SurfaceHazard,
Heat,
Robot
}

View File

@@ -0,0 +1,8 @@
namespace ReactorMaintenance.Simulation;
public sealed record EditorToolCommand
{
public EEditorTool Tool { get; init; }
public ECarrierType Carrier { get; init; }
public ERemedyType RemedyType { get; init; }
}

View File

@@ -0,0 +1,17 @@
namespace ReactorMaintenance.Simulation;
public static class GridPositionExtensions
{
public static IEnumerable<GridPosition> Neighbors(this GridPosition position)
{
yield return new(position.X, position.Y - 1);
yield return new(position.X + 1, position.Y);
yield return new(position.X, position.Y + 1);
yield return new(position.X - 1, position.Y);
}
public static int ManhattanDistance(this GridPosition position, GridPosition other)
{
return Math.Abs(position.X - other.X) + Math.Abs(position.Y - other.Y);
}
}

View File

@@ -0,0 +1,34 @@
namespace ReactorMaintenance.Simulation;
public sealed record JunctionFlow
{
public float WeightFor(GridPosition outgoingBranch)
{
var index = IndexOfOutgoingBranch(outgoingBranch);
if (index < 0)
return 0;
var weights = Balancing.Current.JunctionWeights(OutgoingBranches.Count, Prop.JunctionMode);
return index < weights.Length ? weights[index] : 0;
}
private int IndexOfOutgoingBranch(GridPosition outgoingBranch)
{
for (var i = 0; i < OutgoingBranches.Count; i++)
{
if (OutgoingBranches[i] == outgoingBranch)
return i;
}
return -1;
}
public GridPosition Position { get; init; } = new(0, 0);
public PropState Prop { get; init; } = new();
public ECarrierType Carrier { get; init; }
public IReadOnlyList<GridPosition> Branches { get; init; } = Array.Empty<GridPosition>();
public GridPosition? IncomingBranch { get; init; }
public IReadOnlyList<GridPosition> OutgoingBranches { get; init; } = Array.Empty<GridPosition>();
public IReadOnlyList<string> Errors { get; init; } = Array.Empty<string>();
public bool IsValid => Errors.Count == 0;
}

View File

@@ -0,0 +1,97 @@
namespace ReactorMaintenance.Simulation;
public static class JunctionFlowAnalyzer
{
private sealed record SourceBranch(GridPosition Position, int? Distance);
public static IReadOnlyList<JunctionFlow> Analyze(LevelState level)
{
var flows = new List<JunctionFlow>();
for (var y = 0; y < level.Height; y++)
{
for (var x = 0; x < level.Width; x++)
{
var position = new GridPosition(x, y);
var prop = level.GetProp(position);
if (prop.Type != EPropType.Junction)
continue;
flows.Add(AnalyzeJunction(level, position, prop));
}
}
return flows;
}
private static JunctionFlow AnalyzeJunction(LevelState level, GridPosition position, PropState prop)
{
var errors = new List<string>();
var carriers = Enum.GetValues<ECarrierType>().Where(carrier => level.GetUnderground(position, carrier).IsPresent).ToArray();
var carrier = carriers.FirstOrDefault();
if (carriers.Length != 1)
errors.Add("Junction must regulate exactly one underground carrier.");
var branches = carriers.Length == 1
? position.Neighbors().Where(level.InBounds).Where(neighbor => level.GetUnderground(neighbor, carrier).CarriesFlow).ToArray()
: Array.Empty<GridPosition>();
if (carriers.Length == 1 && branches.Length is not 3 and not 4)
errors.Add("Junction must have one incoming branch and two or three outgoing branches.");
var sourceBranches = carriers.Length == 1
? branches.Select(branch => new SourceBranch(branch, ShortestDistanceToSource(level, branch, position, carrier)))
.Where(branch => branch.Distance.HasValue)
.ToArray()
: Array.Empty<SourceBranch>();
GridPosition? incomingBranch = null;
if (sourceBranches.Length > 0)
{
var bestDistance = sourceBranches.Min(branch => branch.Distance!.Value);
var bestBranches = sourceBranches.Where(branch => branch.Distance == bestDistance).ToArray();
if (bestBranches.Length != 1 || sourceBranches.Length != 1)
errors.Add("Ambiguous junction flow.");
else
incomingBranch = bestBranches[0].Position;
}
var outgoingBranches = incomingBranch is null
? Array.Empty<GridPosition>()
: branches.Where(branch => branch != incomingBranch).ToArray();
return new() {
Position = position,
Prop = prop,
Carrier = carrier,
Branches = branches,
IncomingBranch = incomingBranch,
OutgoingBranches = outgoingBranches,
Errors = errors
};
}
private static int? ShortestDistanceToSource(LevelState level, GridPosition start, GridPosition blocked, ECarrierType carrier)
{
var visited = new HashSet<GridPosition> { blocked, start };
var open = new Queue<(GridPosition Position, int Distance)>();
open.Enqueue((start, 0));
while (open.Count > 0)
{
var current = open.Dequeue();
if (level.GetProp(current.Position) is { Type: EPropType.Flow, Carrier: var sourceCarrier, SwitchState: EPropSwitchState.Enabled } && sourceCarrier == carrier)
return current.Distance;
foreach (var next in current.Position.Neighbors().Where(level.InBounds))
{
if (!visited.Add(next) || !level.GetUnderground(next, carrier).CarriesFlow)
continue;
open.Enqueue((next, current.Distance + 1));
}
}
return null;
}
}

View File

@@ -0,0 +1,3 @@
namespace ReactorMaintenance.Simulation;
public sealed record JunctionRatioPreset(string Label, float[] Weights);

View File

@@ -1,96 +1,191 @@
namespace ReactorMaintenance.Simulation;
public enum EEditorTool
{
Cursor,
Floor,
Wall,
FuelUnderground,
CoolantUnderground,
ElectricityUnderground,
FuelFlow,
CoolantFlow,
ElectricityFlow,
FuelConsumer,
CoolantConsumer,
ElectricityConsumer,
TJunction,
CrossJunction,
Door,
AllSeeingEyeTerminal,
FuelRemedySupply,
CoolantRemedySupply,
ElectricityRemedySupply,
HeatRemedySupply,
ReactorControl,
FuelLeak,
CoolantLeak,
ElectricityLeak,
FuelHazard,
CoolantHazard,
ElectricityHazard,
Heat,
Robot
}
namespace ReactorMaintenance.Simulation;
public static class LevelEditor
{
public static LevelState Apply(LevelState level, GridPosition position, EEditorTool tool)
public sealed record MoveOccupantResult(bool Success, LevelState Level, string Reason)
{
public static MoveOccupantResult Succeeded(LevelState level)
{
return new(true, level, string.Empty);
}
public static MoveOccupantResult Failed(LevelState level, string reason)
{
return new(false, level, reason);
}
}
public static LevelState MoveOccupant(LevelState level, GridPosition source, GridPosition destination)
{
return TryMoveOccupant(level, source, destination).Level;
}
public static MoveOccupantResult TryMoveOccupant(LevelState level, GridPosition source, GridPosition destination)
{
if (!level.InBounds(source))
return MoveOccupantResult.Failed(level, "Drag start is outside the level.");
if (!level.InBounds(destination))
return MoveOccupantResult.Failed(level, "Drop target is outside the level.");
if (source == destination)
return MoveOccupantResult.Failed(level, "Drop target is the same cell.");
var prop = level.GetProp(source);
if (prop.Type != EPropType.None)
return TryMoveProp(level, source, destination, prop);
var leak = level.Leaks.FirstOrDefault(leak => !leak.Repaired && (leak.AccessPosition == source || leak.UndergroundPosition == source));
if (leak is not null)
return TryMoveLeak(level, leak, destination);
return level.Robot.Position == source
? TryMoveRobot(level, destination)
: MoveOccupantResult.Failed(level, "No movable robot, prop, source, or leak starts here.");
}
private static MoveOccupantResult TryMoveRobot(LevelState level, GridPosition destination)
{
if (!level.IsFloor(destination))
return MoveOccupantResult.Failed(level, "Robot destination must be a floor cell.");
return MoveOccupantResult.Succeeded(level with { Robot = level.Robot with { Position = destination } });
}
private static MoveOccupantResult TryMoveProp(LevelState level, GridPosition source, GridPosition destination, PropState prop)
{
if (!level.IsFloor(destination))
return MoveOccupantResult.Failed(level, "Prop destination must be a floor cell.");
if (level.GetProp(destination).Type != EPropType.None)
return MoveOccupantResult.Failed(level, "Prop destination is already occupied.");
var next = level.SetProp(source, new()).SetProp(destination, prop);
if (prop.Type != EPropType.ReactorControl)
return MoveOccupantResult.Succeeded(next);
return MoveOccupantResult.Succeeded(next with {
Reactors = next.Reactors
.Select(reactor => reactor.ControlPosition == source ? reactor with { ControlPosition = destination } : reactor)
.ToArray()
});
}
private static MoveOccupantResult TryMoveLeak(LevelState level, LeakState leak, GridPosition destination)
{
if (leak.Carrier is ECarrierType.Fuel or ECarrierType.Coolant)
{
if (!level.IsFloor(destination))
return MoveOccupantResult.Failed(level, "Fuel and coolant leaks must move to a floor cell.");
var next = ClearLeak(level, leak)
.SetUnderground(leak.UndergroundPosition, leak.Carrier, new());
return MoveOccupantResult.Succeeded(SetLeak(next, destination, destination, leak.Carrier));
}
if (leak.Carrier == ECarrierType.Electricity)
{
if (!level.IsFloor(destination))
return MoveOccupantResult.Failed(level, "Electric leak destination must be an adjacent floor access cell.");
var undergroundPosition = leak.UndergroundPosition;
if (undergroundPosition.ManhattanDistance(destination) != 1)
return MoveOccupantResult.Failed(level, "Electric leak destination must stay adjacent to its underground wall cell.");
return MoveOccupantResult.Succeeded(SetLeak(ClearLeak(level, leak), undergroundPosition, destination, leak.Carrier));
}
return MoveOccupantResult.Failed(level, "Unsupported leak carrier.");
}
private static LevelState ClearLeak(LevelState level, LeakState leak)
{
return level with {
Leaks = level.Leaks.Where(candidate => candidate != leak).ToArray()
};
}
public static LevelState CycleElectricityLeakAccess(LevelState level, GridPosition undergroundPosition)
{
if (!level.InBounds(undergroundPosition))
return level;
if (!level.GetUnderground(undergroundPosition, ECarrierType.Electricity).IsPresent)
return level;
var accessPositions = undergroundPosition.Neighbors().Where(level.IsFloor).ToArray();
if (accessPositions.Length == 0)
return level;
var existingLeak = level.Leaks.FirstOrDefault(leak => leak.Carrier == ECarrierType.Electricity && leak.UndergroundPosition == undergroundPosition);
var nextAccessPosition = accessPositions[0];
if (existingLeak is not null)
{
var index = Array.IndexOf(accessPositions, existingLeak.AccessPosition);
nextAccessPosition = accessPositions[(index + 1) % accessPositions.Length];
}
return SetLeak(level, undergroundPosition, nextAccessPosition, ECarrierType.Electricity);
}
public static LevelState Apply(LevelState level, GridPosition position, EditorToolCommand command)
{
if (!level.InBounds(position))
return level;
return tool switch {
return command.Tool switch {
EEditorTool.Cursor => level,
EEditorTool.Floor => level.SetTerrain(position, ECellTerrain.Floor),
EEditorTool.Wall => level.SetTerrain(position, ECellTerrain.Wall),
EEditorTool.FuelUnderground => SetUnderground(level, position, ECarrierType.Fuel),
EEditorTool.CoolantUnderground => SetUnderground(level, position, ECarrierType.Coolant),
EEditorTool.ElectricityUnderground => SetUnderground(level, position, ECarrierType.Electricity),
EEditorTool.FuelFlow => SetCarrierProp(level, position, EPropType.Flow, ECarrierType.Fuel),
EEditorTool.CoolantFlow => SetCarrierProp(level, position, EPropType.Flow, ECarrierType.Coolant),
EEditorTool.ElectricityFlow => SetCarrierProp(level, position, EPropType.Flow, ECarrierType.Electricity),
EEditorTool.FuelConsumer => SetCarrierProp(level, position, EPropType.Consumer, ECarrierType.Fuel),
EEditorTool.CoolantConsumer => SetCarrierProp(level, position, EPropType.Consumer, ECarrierType.Coolant),
EEditorTool.ElectricityConsumer => SetCarrierProp(level, position, EPropType.Consumer, ECarrierType.Electricity),
EEditorTool.TJunction => SetFloorProp(level, position, new() { Type = EPropType.TJunction }),
EEditorTool.CrossJunction => SetFloorProp(level, position, new() { Type = EPropType.CrossJunction }),
EEditorTool.Door => SetDoor(level, position),
EEditorTool.Underground => SetUnderground(level, position, command.Carrier),
EEditorTool.Flow => SetCarrierProp(level, position, EPropType.Flow, command.Carrier),
EEditorTool.Consumer => SetFloorProp(level, position, new() { Type = EPropType.Consumer, SwitchState = EPropSwitchState.Enabled }),
EEditorTool.Junction => SetFloorProp(level, position, new() { Type = EPropType.Junction }),
EEditorTool.Door => ToggleOrSetDoor(level, position),
EEditorTool.AllSeeingEyeTerminal => SetFloorProp(level, position, new() { Type = EPropType.AllSeeingEyeTerminal }),
EEditorTool.FuelRemedySupply => SetFloorProp(level, position, new() { Type = EPropType.RemedySupply, RemedyType = ERemedyType.FuelNeutralizer }),
EEditorTool.CoolantRemedySupply => SetFloorProp(level, position, new() { Type = EPropType.RemedySupply, RemedyType = ERemedyType.CoolantNeutralizer }),
EEditorTool.ElectricityRemedySupply => SetFloorProp(level, position, new() { Type = EPropType.RemedySupply, RemedyType = ERemedyType.ElectricityNeutralizer }),
EEditorTool.HeatRemedySupply => SetFloorProp(level, position, new() { Type = EPropType.RemedySupply, RemedyType = ERemedyType.HeatShield }),
EEditorTool.RemedySupply => SetFloorProp(level, position, new() { Type = EPropType.RemedySupply, RemedyType = command.RemedyType }),
EEditorTool.ReactorControl => SetReactorControl(level, position),
EEditorTool.FuelLeak => SetLeak(level, position, ECarrierType.Fuel),
EEditorTool.CoolantLeak => SetLeak(level, position, ECarrierType.Coolant),
EEditorTool.ElectricityLeak => SetLeak(level, position, ECarrierType.Electricity),
EEditorTool.FuelHazard => level.SetSurface(position, level.GetSurface(position) with { Fuel = level.GetSurface(position).Fuel + 1 }),
EEditorTool.CoolantHazard => level.SetSurface(position, level.GetSurface(position) with { Coolant = level.GetSurface(position).Coolant + 1 }),
EEditorTool.ElectricityHazard => level.SetSurface(position, level.GetSurface(position) with { Electricity = level.GetSurface(position).Electricity + 1 }),
EEditorTool.Leak => SetLeak(level, position, command.Carrier),
EEditorTool.SurfaceHazard => AddSurfaceHazard(level, position, command.Carrier),
EEditorTool.Heat => level.SetSurface(position, level.GetSurface(position) with { Heat = level.GetSurface(position).Heat + 1 }),
EEditorTool.Robot => level.IsFloor(position) ? level with { Robot = level.Robot with { Position = position } } : level,
_ => level
};
}
public static LevelState BindFirstReactorToConsumers(LevelState level, GridPosition fuelConsumer, GridPosition coolantConsumer, GridPosition electricityConsumer)
public static LevelState SetLeak(LevelState level, GridPosition undergroundPosition, GridPosition accessPosition, ECarrierType carrier)
{
if (level.Reactors.Count == 0)
if (!level.InBounds(undergroundPosition) || !level.IsFloor(accessPosition))
return level;
var reactors = level.Reactors.ToArray();
reactors[0] = reactors[0] with {
FuelConsumerPosition = fuelConsumer,
CoolantConsumerPosition = coolantConsumer,
ElectricityConsumerPosition = electricityConsumer
if (carrier is ECarrierType.Fuel or ECarrierType.Coolant && undergroundPosition != accessPosition)
return level;
if (carrier == ECarrierType.Electricity && undergroundPosition.ManhattanDistance(accessPosition) != 1)
return level;
var next = level.SetUnderground(undergroundPosition, carrier, new() {
State = EUndergroundState.Leaking,
StructuralIntegrity = Balancing.Current.StructuralIntegrityLeakThreshold
});
return next with {
Leaks = [
.. next.Leaks.Where(leak => leak.UndergroundPosition != undergroundPosition || leak.Carrier != carrier),
new() {
Carrier = carrier,
UndergroundPosition = undergroundPosition,
AccessPosition = accessPosition
}
]
};
return level with { Reactors = reactors };
}
private static LevelState SetUnderground(LevelState level, GridPosition position, ECarrierType carrier)
{
return level.SetUnderground(position, carrier, new() { State = EUndergroundState.Intact });
return level.SetUnderground(position, carrier, new() {
State = EUndergroundState.Intact,
StructuralIntegrity = Balancing.Current.MaxStructuralIntegrity
});
}
private static LevelState SetCarrierProp(LevelState level, GridPosition position, EPropType type, ECarrierType carrier)
@@ -98,23 +193,35 @@ public static class LevelEditor
return SetFloorProp(level, position, new() { Type = type, Carrier = carrier, SwitchState = EPropSwitchState.Enabled });
}
private static LevelState AddSurfaceHazard(LevelState level, GridPosition position, ECarrierType carrier)
{
var surface = level.GetSurface(position);
return carrier switch {
ECarrierType.Fuel => level.SetSurface(position, surface with { Fuel = surface.Fuel + 1 }),
ECarrierType.Coolant => level.SetSurface(position, surface with { Coolant = surface.Coolant + 1 }),
ECarrierType.Electricity => level.SetSurface(position, surface with { Electricity = surface.Electricity + 1 }),
_ => level
};
}
private static LevelState SetFloorProp(LevelState level, GridPosition position, PropState prop)
{
return level.IsFloor(position) ? level.SetProp(position, prop) : level;
}
private static LevelState SetDoor(LevelState level, GridPosition position)
private static LevelState ToggleOrSetDoor(LevelState level, GridPosition position)
{
if (!level.IsFloor(position))
return level;
var neighbor = position.Neighbors().FirstOrDefault(level.IsFloor);
if (neighbor is null)
return SetFloorProp(level, position, new() { Type = EPropType.Door });
var prop = level.GetProp(position);
if (prop.Type == EPropType.Door)
{
var nextState = prop.DoorState == EDoorState.Open ? EDoorState.Closed : EDoorState.Open;
return level.SetProp(position, prop with { DoorState = nextState });
}
return SetFloorProp(level, position, new() { Type = EPropType.Door }) with {
Doors = [.. level.Doors, new DoorState { A = position, B = neighbor }]
};
return level.SetProp(position, new() { Type = EPropType.Door, DoorState = EDoorState.Closed });
}
private static LevelState SetReactorControl(LevelState level, GridPosition position)
@@ -127,12 +234,9 @@ public static class LevelEditor
return levelWithProp with {
Reactors = [
.. level.Reactors,
new ReactorBinding {
new() {
ReactorId = id,
ControlPosition = position,
FuelConsumerPosition = position,
CoolantConsumerPosition = position,
ElectricityConsumerPosition = position
ControlPosition = position
}
]
};
@@ -143,23 +247,6 @@ public static class LevelEditor
if (!level.InBounds(position))
return level;
var accessPosition = carrier == ECarrierType.Electricity && level.GetTerrain(position) == ECellTerrain.Wall
? position.Neighbors().FirstOrDefault(level.IsFloor)
: position;
if (accessPosition is null || !level.IsFloor(accessPosition))
return level;
var next = level.SetUnderground(position, carrier, new() { State = EUndergroundState.Leaking });
return next with {
Leaks = [
.. next.Leaks,
new LeakState {
Carrier = carrier,
UndergroundPosition = position,
AccessPosition = accessPosition
}
]
};
return SetLeak(level, position, position, carrier);
}
}

View File

@@ -1,11 +1,15 @@
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 = 2;
private sealed record LevelFile
{
public int Version { get; init; }
public LevelState? Level { get; init; }
}
public static string Serialize(LevelState level)
{
@@ -29,14 +33,10 @@ public static class LevelSerializer
return level;
}
private const int c_CurrentVersion = 3;
private static readonly JsonSerializerOptions s_Options = new() {
WriteIndented = true,
Converters = { new JsonStringEnumConverter() }
};
private sealed record LevelFile
{
public int Version { get; init; }
public LevelState? Level { get; init; }
}
}

View File

@@ -0,0 +1,136 @@
namespace ReactorMaintenance.Simulation;
public static class LevelStateExtensions
{
public static bool InBounds(this LevelState level, GridPosition position)
{
return position.X >= 0 && position.Y >= 0 && position.X < level.Width && position.Y < level.Height;
}
public static int Index(this LevelState level, GridPosition position)
{
if (!level.InBounds(position))
throw new ArgumentOutOfRangeException(nameof(position), $"Position {position.X},{position.Y} is outside {level.Width}x{level.Height}.");
return (position.Y * level.Width) + position.X;
}
public static ECellTerrain GetTerrain(this LevelState level, GridPosition position)
{
return level.Terrain[level.Index(position)];
}
public static UndergroundCell GetUnderground(this LevelState level, GridPosition position, ECarrierType carrier)
{
return level.Layer(carrier)[level.Index(position)];
}
public static SurfaceState GetSurface(this LevelState level, GridPosition position)
{
return level.Surface[level.Index(position)];
}
public static PropState GetProp(this LevelState level, GridPosition position)
{
return level.Props[level.Index(position)];
}
public static bool IsFloor(this LevelState level, GridPosition position)
{
return level.InBounds(position) && level.GetTerrain(position) == ECellTerrain.Floor;
}
public static bool IsClosedDoorEdge(this LevelState level, GridPosition a, GridPosition b)
{
return DoorBlocksEdge(level, a, b) || DoorBlocksEdge(level, b, a);
}
public static LevelState SetTerrain(this LevelState level, GridPosition position, ECellTerrain terrain)
{
var next = level.Terrain.ToArray();
next[level.Index(position)] = terrain;
var updated = level with { Terrain = next };
return terrain == ECellTerrain.Wall ? updated.ClearFloorOnlyState(position) : updated;
}
public static LevelState SetUnderground(this LevelState level, GridPosition position, ECarrierType carrier, UndergroundCell cell)
{
var next = level.Layer(carrier).ToArray();
next[level.Index(position)] = cell;
return carrier switch {
ECarrierType.Fuel => level with { Fuel = next },
ECarrierType.Coolant => level with { Coolant = next },
ECarrierType.Electricity => level with { Electricity = next },
_ => throw new ArgumentOutOfRangeException(nameof(carrier), carrier, "Unsupported carrier.")
};
}
public static LevelState SetSurface(this LevelState level, GridPosition position, SurfaceState surface)
{
var next = level.Surface.ToArray();
next[level.Index(position)] = surface.Clamp();
return level with { Surface = next };
}
public static LevelState SetProp(this LevelState level, GridPosition position, PropState prop)
{
var next = level.Props.ToArray();
next[level.Index(position)] = prop;
return level with { Props = next };
}
public static LevelState WithRuntimeArrays(this LevelState level, UndergroundCell[] fuel, UndergroundCell[] coolant, UndergroundCell[] electricity, SurfaceState[] surface, PropState[] props)
{
return level with {
Fuel = fuel,
Coolant = coolant,
Electricity = electricity,
Surface = surface,
Props = props
};
}
public static IReadOnlyList<UndergroundCell> Layer(this LevelState level, ECarrierType carrier)
{
return carrier switch {
ECarrierType.Fuel => level.Fuel,
ECarrierType.Coolant => level.Coolant,
ECarrierType.Electricity => level.Electricity,
_ => throw new ArgumentOutOfRangeException(nameof(carrier), carrier, "Unsupported carrier.")
};
}
private static LevelState ClearFloorOnlyState(this LevelState level, GridPosition position)
{
return level.SetSurface(position, new())
.SetProp(position, new());
}
private static bool DoorBlocksEdge(LevelState level, GridPosition doorPosition, GridPosition neighbor)
{
if (!level.InBounds(doorPosition) || !level.InBounds(neighbor))
return false;
var prop = level.GetProp(doorPosition);
if (prop is not { Type: EPropType.Door, DoorState: EDoorState.Closed } || doorPosition.ManhattanDistance(neighbor) != 1)
return false;
var north = new GridPosition(doorPosition.X, doorPosition.Y - 1);
var south = new GridPosition(doorPosition.X, doorPosition.Y + 1);
var west = new GridPosition(doorPosition.X - 1, doorPosition.Y);
var east = new GridPosition(doorPosition.X + 1, doorPosition.Y);
if (IsWall(level, north) && IsWall(level, south))
return neighbor.Y == doorPosition.Y;
if (IsWall(level, west) && IsWall(level, east))
return neighbor.X == doorPosition.X;
return false;
}
private static bool IsWall(LevelState level, GridPosition position)
{
return level.InBounds(position) && level.GetTerrain(position) == ECellTerrain.Wall;
}
}

View File

@@ -0,0 +1,54 @@
namespace ReactorMaintenance.Simulation;
public static class LevelStateFactory
{
public static LevelState Create(string name, int width, int height)
{
if (width < Balancing.Current.MinimumLevelSize || height < Balancing.Current.MinimumLevelSize)
throw new ArgumentOutOfRangeException(nameof(width), $"Levels must be at least {Balancing.Current.MinimumLevelSize}x{Balancing.Current.MinimumLevelSize}.");
return new() {
Name = name,
Width = width,
Height = height,
Terrain = CreateTerrain(width, height),
Fuel = CreateUnderground(width, height),
Coolant = CreateUnderground(width, height),
Electricity = CreateUnderground(width, height),
Surface = CreateSurface(width, height),
Props = CreateProps(width, height),
Robot = new() { Position = new(1, 1) },
Forecasts = Array.Empty<Forecast>()
};
}
public static ECellTerrain[] CreateTerrain(int width, int height)
{
var terrain = Enumerable.Repeat(ECellTerrain.Floor, width * height).ToArray();
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)
terrain[(y * width) + x] = ECellTerrain.Wall;
}
}
return terrain;
}
public static UndergroundCell[] CreateUnderground(int width, int height)
{
return Enumerable.Range(0, width * height).Select(_ => new UndergroundCell()).ToArray();
}
public static SurfaceState[] CreateSurface(int width, int height)
{
return Enumerable.Range(0, width * height).Select(_ => new SurfaceState()).ToArray();
}
public static PropState[] CreateProps(int width, int height)
{
return Enumerable.Range(0, width * height).Select(_ => new PropState()).ToArray();
}
}

View File

@@ -0,0 +1,13 @@
namespace ReactorMaintenance.Simulation;
internal static class LevelTraversal
{
public static IEnumerable<GridPosition> AllPositions(LevelState level)
{
for (var y = 0; y < level.Height; y++)
{
for (var x = 0; x < level.Width; x++)
yield return new(x, y);
}
}
}

View File

@@ -1,4 +1,4 @@
namespace ReactorMaintenance.Simulation;
namespace ReactorMaintenance.Simulation;
public sealed class LevelValidator
{
@@ -14,7 +14,6 @@ public sealed class LevelValidator
ValidateLeaks(level, errors);
ValidateReactors(level, errors, warnings);
ValidateJunctions(level, errors);
ValidateRuleEvents(level, errors);
ValidateWarnings(level, warnings);
return new() { Errors = errors, Warnings = warnings };
@@ -60,10 +59,21 @@ public sealed class LevelValidator
private static void ValidateDoors(LevelState level, List<ValidationIssue> errors)
{
foreach (var door in level.Doors)
foreach (var position in LevelTraversal.AllPositions(level))
{
if (!level.IsFloor(door.A) || !level.IsFloor(door.B) || door.A.ManhattanDistance(door.B) != 1)
errors.Add(new("Door edge must connect two adjacent floor cells.", door.A));
if (level.GetProp(position).Type != EPropType.Door)
continue;
if (!level.IsFloor(position))
{
errors.Add(new("Door prop must be placed on a floor cell.", position));
continue;
}
var northSouthWalls = IsWall(level, new(position.X, position.Y - 1)) && IsWall(level, new(position.X, position.Y + 1));
var westEastWalls = IsWall(level, new(position.X - 1, position.Y)) && IsWall(level, new(position.X + 1, position.Y));
if (northSouthWalls == westEastWalls)
errors.Add(new("Door must be surrounded by one opposing pair of wall cells.", position));
}
}
@@ -94,51 +104,20 @@ public sealed class LevelValidator
foreach (var reactor in level.Reactors)
{
if (!IsProp(level, reactor.ControlPosition, EPropType.ReactorControl))
errors.Add(new("Reactor binding control position must point to a reactor control prop.", reactor.ControlPosition));
ValidateConsumerBinding(level, reactor.FuelConsumerPosition, ECarrierType.Fuel, errors);
ValidateConsumerBinding(level, reactor.CoolantConsumerPosition, ECarrierType.Coolant, errors);
ValidateConsumerBinding(level, reactor.ElectricityConsumerPosition, ECarrierType.Electricity, errors);
errors.Add(new("Reactor control position must point to a reactor control prop.", reactor.ControlPosition));
if (!reactor.Ready)
warnings.Add(new("Reactor is initially unready.", reactor.ControlPosition));
}
}
private static void ValidateConsumerBinding(LevelState level, GridPosition position, ECarrierType carrier, List<ValidationIssue> errors)
{
if (!level.InBounds(position) || level.GetProp(position) is not { Type: EPropType.Consumer } prop || prop.Carrier != carrier)
errors.Add(new($"Missing or invalid {carrier} consumer binding.", position));
if (level.RequiredFuelConsumers < 0 || level.RequiredCoolantConsumers < 0 || level.RequiredElectricityConsumers < 0)
errors.Add(new("Required consumer counts cannot be negative."));
}
private static void ValidateJunctions(LevelState level, List<ValidationIssue> errors)
{
for (var y = 0; y < level.Height; y++)
{
for (var x = 0; x < level.Width; x++)
{
var position = new GridPosition(x, y);
var prop = level.GetProp(position);
if (prop.Type is not (EPropType.TJunction or EPropType.CrossJunction))
continue;
var carrierCount = Enum.GetValues<ECarrierType>().Count(carrier => level.GetUnderground(position, carrier).IsPresent);
if (carrierCount != 1)
errors.Add(new("Junction must regulate exactly one underground carrier.", position));
}
}
}
private static void ValidateRuleEvents(LevelState level, List<ValidationIssue> errors)
{
foreach (var ruleEvent in level.RuleEvents)
{
foreach (var effect in ruleEvent.Effects)
{
if (!level.InBounds(effect.Position) && effect.Kind != ERuleEffectKind.EmitWarning && effect.Kind != ERuleEffectKind.MarkTerminalLoss && effect.Kind != ERuleEffectKind.AddInventory)
errors.Add(new("Rule effect target is out of bounds.", effect.Position));
}
}
foreach (var junction in JunctionFlowAnalyzer.Analyze(level))
errors.AddRange(junction.Errors.Select(error => new ValidationIssue(error, junction.Position)));
}
private static void ValidateWarnings(LevelState level, List<ValidationIssue> warnings)
@@ -162,12 +141,31 @@ public sealed class LevelValidator
{
var position = new GridPosition(x, y);
var prop = level.GetProp(position);
if (prop.Type == EPropType.Consumer && prop.SwitchState == EPropSwitchState.Enabled && !HasSourcePath(level, position, prop.Carrier))
warnings.Add(new("Enabled consumer is initially starved.", position));
if (prop.Type != EPropType.Consumer || prop.SwitchState != EPropSwitchState.Enabled)
continue;
var hasPresentNetwork = false;
foreach (var carrier in Enum.GetValues<ECarrierType>())
{
if (!level.GetUnderground(position, carrier).IsPresent)
continue;
hasPresentNetwork = true;
if (!HasSourcePath(level, position, carrier))
warnings.Add(new($"Enabled consumer has no {carrier} source path.", position));
}
if (!hasPresentNetwork)
warnings.Add(new("Enabled consumer has no underground network beneath it.", position));
}
}
}
private static bool IsWall(LevelState level, GridPosition position)
{
return level.InBounds(position) && level.GetTerrain(position) == ECellTerrain.Wall;
}
private static bool HasSourcePath(LevelState level, GridPosition start, ECarrierType carrier)
{
if (!level.GetUnderground(start, carrier).CarriesFlow)

View File

@@ -1,534 +0,0 @@
namespace ReactorMaintenance.Simulation;
public enum ECellTerrain
{
Floor,
Wall
}
public enum ECarrierType
{
Fuel,
Coolant,
Electricity
}
public enum EUndergroundState
{
Absent,
Intact,
Leaking
}
public enum EPropType
{
None,
Flow,
Consumer,
TJunction,
CrossJunction,
Door,
AllSeeingEyeTerminal,
RemedySupply,
ReactorControl
}
public enum EPropSwitchState
{
Disabled,
Enabled
}
public enum EConsumerServiceState
{
Unknown,
Disabled,
Starved,
Supplied,
Producing
}
public enum ETJunctionMode
{
ZeroFour,
OneThree,
TwoTwo,
ThreeOne,
FourZero
}
public enum ECrossJunctionMode
{
ZeroThreeThree,
ThreeZeroThree,
ThreeThreeZero,
TwoTwoTwo
}
public enum EDoorState
{
Open,
Closed
}
public enum ERemedyType
{
FuelNeutralizer,
CoolantNeutralizer,
ElectricityNeutralizer,
HeatShield
}
public enum ELevelState
{
Stable,
Caution,
Critical,
Ready,
Lost,
Won
}
public enum EForecastKind
{
TerminalLoss,
ReactorReady,
ConsumerStarved,
HazardGrowth,
RuleEvent
}
public enum ERuleEventPhase
{
StartOfSimulation,
EndOfTurn
}
public enum ERulePredicateKind
{
TurnAtLeast,
LevelStateIs,
PropStateAt,
ConsumerStateAt,
SurfaceBandAt,
RobotAt,
AllSeeingEyeUnlocked
}
public enum ERuleEffectKind
{
StartLeak,
WorsenLeak,
RepairNetworkCell,
DisableNetworkCell,
SetPropEnabled,
AddSurfaceHazard,
AddHeat,
AddInventory,
MarkTerminalLoss,
EmitWarning
}
public enum EBand
{
Safe,
Caution,
Critical
}
public enum EPairEffect
{
Hold,
FuelFlow,
CoolFlow,
ChargeFlow,
HeatFlow,
HeatFlow2,
Warm1,
Warm2,
Quench1,
Quench2,
Short1,
Short2,
Ignite1,
Ignite2
}
public sealed record GridPosition(int X, int Y)
{
public IEnumerable<GridPosition> Neighbors()
{
yield return new(X, Y - 1);
yield return new(X + 1, Y);
yield return new(X, Y + 1);
yield return new(X - 1, Y);
}
public int ManhattanDistance(GridPosition other)
{
return Math.Abs(X - other.X) + Math.Abs(Y - other.Y);
}
}
public sealed record UndergroundCell
{
public EUndergroundState State { get; init; }
public float Amount { get; init; }
public float Intensity { get; init; }
public bool IsPresent => State != EUndergroundState.Absent;
public bool CarriesFlow => State is EUndergroundState.Intact or EUndergroundState.Leaking;
}
public sealed record SurfaceState
{
public float Fuel { get; init; }
public float Coolant { get; init; }
public float Electricity { get; init; }
public float Heat { get; init; }
public int FuelBlockTurns { get; init; }
public int CoolantBlockTurns { get; init; }
public int ElectricityBlockTurns { get; init; }
public SurfaceState Clamp()
{
var balancing = Balancing.Current;
return this with {
Fuel = balancing.ClampValue(Fuel),
Coolant = balancing.ClampValue(Coolant),
Electricity = balancing.ClampValue(Electricity),
Heat = balancing.ClampValue(Heat),
FuelBlockTurns = Math.Max(0, FuelBlockTurns),
CoolantBlockTurns = Math.Max(0, CoolantBlockTurns),
ElectricityBlockTurns = Math.Max(0, ElectricityBlockTurns)
};
}
public bool Blocks(ECarrierType carrier)
{
return carrier switch {
ECarrierType.Fuel => FuelBlockTurns > 0,
ECarrierType.Coolant => CoolantBlockTurns > 0,
ECarrierType.Electricity => ElectricityBlockTurns > 0,
_ => throw new ArgumentOutOfRangeException(nameof(carrier), carrier, "Unsupported carrier.")
};
}
}
public sealed record PropState
{
public EPropType Type { get; init; }
public ECarrierType Carrier { get; init; }
public EPropSwitchState SwitchState { get; init; } = EPropSwitchState.Enabled;
public EConsumerServiceState ServiceState { get; init; } = EConsumerServiceState.Unknown;
public ETJunctionMode TJunctionMode { get; init; } = ETJunctionMode.TwoTwo;
public ECrossJunctionMode CrossJunctionMode { get; init; } = ECrossJunctionMode.TwoTwoTwo;
public ERemedyType RemedyType { get; init; }
public bool Depleted { get; init; }
public int ReactorId { get; init; }
public bool IsEnabled => SwitchState == EPropSwitchState.Enabled;
}
public sealed record DoorState
{
public GridPosition A { get; init; } = new(0, 0);
public GridPosition B { get; init; } = new(0, 0);
public EDoorState State { get; init; } = EDoorState.Closed;
}
public sealed record LeakState
{
public ECarrierType Carrier { get; init; }
public GridPosition UndergroundPosition { get; init; } = new(0, 0);
public GridPosition AccessPosition { get; init; } = new(0, 0);
public bool Repaired { get; init; }
}
public sealed record ReactorBinding
{
public int ReactorId { get; init; }
public GridPosition ControlPosition { get; init; } = new(0, 0);
public GridPosition FuelConsumerPosition { get; init; } = new(0, 0);
public GridPosition CoolantConsumerPosition { get; init; } = new(0, 0);
public GridPosition ElectricityConsumerPosition { get; init; } = new(0, 0);
public bool Ready { get; init; }
public bool Activated { get; init; }
}
public sealed record RobotState
{
public GridPosition Position { get; init; } = new(1, 1);
public int FuelNeutralizers { get; init; }
public int CoolantNeutralizers { get; init; }
public int ElectricityNeutralizers { get; init; }
public int HeatShields { get; init; }
public int HeatImmunitySteps { get; init; }
public int Count(ERemedyType remedy)
{
return remedy switch {
ERemedyType.FuelNeutralizer => FuelNeutralizers,
ERemedyType.CoolantNeutralizer => CoolantNeutralizers,
ERemedyType.ElectricityNeutralizer => ElectricityNeutralizers,
ERemedyType.HeatShield => HeatShields,
_ => throw new ArgumentOutOfRangeException(nameof(remedy), remedy, "Unsupported remedy.")
};
}
public RobotState Add(ERemedyType remedy, int amount)
{
return remedy switch {
ERemedyType.FuelNeutralizer => this with { FuelNeutralizers = FuelNeutralizers + amount },
ERemedyType.CoolantNeutralizer => this with { CoolantNeutralizers = CoolantNeutralizers + amount },
ERemedyType.ElectricityNeutralizer => this with { ElectricityNeutralizers = ElectricityNeutralizers + amount },
ERemedyType.HeatShield => this with { HeatShields = HeatShields + amount },
_ => throw new ArgumentOutOfRangeException(nameof(remedy), remedy, "Unsupported remedy.")
};
}
public RobotState Spend(ERemedyType remedy)
{
return Count(remedy) <= 0 ? this : Add(remedy, -1);
}
}
public sealed record RulePredicate
{
public ERulePredicateKind Kind { get; init; }
public GridPosition Position { get; init; } = new(0, 0);
public int Turn { get; init; }
public ELevelState LevelState { get; init; }
public EPropSwitchState PropSwitchState { get; init; }
public EConsumerServiceState ConsumerServiceState { get; init; }
public ECarrierType Carrier { get; init; }
public EBand Band { get; init; }
public bool BoolValue { get; init; }
}
public sealed record RuleEffect
{
public ERuleEffectKind Kind { get; init; }
public GridPosition Position { get; init; } = new(0, 0);
public ECarrierType Carrier { get; init; }
public ERemedyType Remedy { get; init; }
public float Amount { get; init; }
public EPropSwitchState PropSwitchState { get; init; }
public string Message { get; init; } = string.Empty;
}
public sealed record RuleEventState
{
public string Id { get; init; } = string.Empty;
public bool Enabled { get; init; } = true;
public bool Repeat { get; init; }
public bool Triggered { get; init; }
public int Priority { get; init; }
public ERuleEventPhase Phase { get; init; }
public IReadOnlyList<RulePredicate> Predicates { get; init; } = Array.Empty<RulePredicate>();
public IReadOnlyList<RuleEffect> Effects { get; init; } = Array.Empty<RuleEffect>();
public string ForecastText { get; init; } = string.Empty;
}
public sealed record Forecast(EForecastKind Kind, GridPosition? Position, int Turns, string Message);
public sealed record ValidationIssue(string Message, GridPosition? Position = null);
public sealed record ValidationReport
{
public IReadOnlyList<ValidationIssue> Errors { get; init; } = Array.Empty<ValidationIssue>();
public IReadOnlyList<ValidationIssue> Warnings { get; init; } = Array.Empty<ValidationIssue>();
public bool IsValid => Errors.Count == 0;
}
public sealed record GlobalState
{
public int Turn { get; init; }
public int ActionsRemaining { get; init; } = Balancing.Current.ActionsPerTurn;
public ELevelState LevelState { get; init; } = ELevelState.Stable;
public string Status { get; init; } = "STABLE";
public bool AllSeeingEyeUnlocked { get; init; }
public bool TerminalLoss { get; init; }
public IReadOnlyList<string> Warnings { get; init; } = Array.Empty<string>();
}
public sealed record LevelState
{
public static LevelState Create(string name, int width, int height)
{
if (width < Balancing.Current.MinimumLevelSize || height < Balancing.Current.MinimumLevelSize)
throw new ArgumentOutOfRangeException(nameof(width), $"Levels must be at least {Balancing.Current.MinimumLevelSize}x{Balancing.Current.MinimumLevelSize}.");
var terrain = Enumerable.Repeat(ECellTerrain.Floor, width * height).ToArray();
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)
terrain[y * width + x] = ECellTerrain.Wall;
}
}
var level = new LevelState {
Name = name,
Width = width,
Height = height,
Terrain = terrain,
Fuel = CreateUnderground(width, height),
Coolant = CreateUnderground(width, height),
Electricity = CreateUnderground(width, height),
Surface = CreateSurface(width, height),
Props = CreateProps(width, height),
Robot = new() { Position = new(1, 1) }
};
return level with { Forecasts = Array.Empty<Forecast>() };
}
public bool InBounds(GridPosition position)
{
return position.X >= 0 && position.Y >= 0 && position.X < Width && position.Y < Height;
}
public int Index(GridPosition position)
{
EnsureInBounds(position);
return position.Y * Width + position.X;
}
public ECellTerrain GetTerrain(GridPosition position)
{
return Terrain[Index(position)];
}
public UndergroundCell GetUnderground(GridPosition position, ECarrierType carrier)
{
return Layer(carrier)[Index(position)];
}
public SurfaceState GetSurface(GridPosition position)
{
return Surface[Index(position)];
}
public PropState GetProp(GridPosition position)
{
return Props[Index(position)];
}
public bool IsFloor(GridPosition position)
{
return InBounds(position) && GetTerrain(position) == ECellTerrain.Floor;
}
public bool IsClosedDoorEdge(GridPosition a, GridPosition b)
{
return Doors.Any(door => door.State == EDoorState.Closed && SameEdge(door.A, door.B, a, b));
}
public LevelState SetTerrain(GridPosition position, ECellTerrain terrain)
{
var next = Terrain.ToArray();
next[Index(position)] = terrain;
var level = this with { Terrain = next };
return terrain == ECellTerrain.Wall ? level.ClearFloorOnlyState(position) : level;
}
public LevelState SetUnderground(GridPosition position, ECarrierType carrier, UndergroundCell cell)
{
var next = Layer(carrier).ToArray();
next[Index(position)] = cell;
return carrier switch {
ECarrierType.Fuel => this with { Fuel = next },
ECarrierType.Coolant => this with { Coolant = next },
ECarrierType.Electricity => this with { Electricity = next },
_ => throw new ArgumentOutOfRangeException(nameof(carrier), carrier, "Unsupported carrier.")
};
}
public LevelState SetSurface(GridPosition position, SurfaceState surface)
{
var next = Surface.ToArray();
next[Index(position)] = surface.Clamp();
return this with { Surface = next };
}
public LevelState SetProp(GridPosition position, PropState prop)
{
var next = Props.ToArray();
next[Index(position)] = prop;
return this with { Props = next };
}
public LevelState WithRuntimeArrays(UndergroundCell[] fuel, UndergroundCell[] coolant, UndergroundCell[] electricity, SurfaceState[] surface, PropState[] props)
{
return this with {
Fuel = fuel,
Coolant = coolant,
Electricity = electricity,
Surface = surface,
Props = props
};
}
public IReadOnlyList<UndergroundCell> Layer(ECarrierType carrier)
{
return carrier switch {
ECarrierType.Fuel => Fuel,
ECarrierType.Coolant => Coolant,
ECarrierType.Electricity => Electricity,
_ => throw new ArgumentOutOfRangeException(nameof(carrier), carrier, "Unsupported carrier.")
};
}
private LevelState ClearFloorOnlyState(GridPosition position)
{
return SetSurface(position, new())
.SetProp(position, new())
.SetUnderground(position, ECarrierType.Fuel, new())
.SetUnderground(position, ECarrierType.Coolant, new())
.SetUnderground(position, ECarrierType.Electricity, new());
}
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 bool SameEdge(GridPosition edgeA, GridPosition edgeB, GridPosition a, GridPosition b)
{
return edgeA == a && edgeB == b || edgeA == b && edgeB == a;
}
private static UndergroundCell[] CreateUnderground(int width, int height)
{
return Enumerable.Range(0, width * height).Select(_ => new UndergroundCell()).ToArray();
}
private static SurfaceState[] CreateSurface(int width, int height)
{
return Enumerable.Range(0, width * height).Select(_ => new SurfaceState()).ToArray();
}
private static PropState[] CreateProps(int width, int height)
{
return Enumerable.Range(0, width * height).Select(_ => new PropState()).ToArray();
}
public string Name { get; init; } = "New Reactor";
public int Width { get; init; } = Balancing.Current.DefaultLevelWidth;
public int Height { get; init; } = Balancing.Current.DefaultLevelHeight;
public ECellTerrain[] Terrain { get; init; } = Enumerable.Repeat(ECellTerrain.Floor, Balancing.Current.DefaultLevelWidth * Balancing.Current.DefaultLevelHeight).ToArray();
public UndergroundCell[] Fuel { get; init; } = CreateUnderground(Balancing.Current.DefaultLevelWidth, Balancing.Current.DefaultLevelHeight);
public UndergroundCell[] Coolant { get; init; } = CreateUnderground(Balancing.Current.DefaultLevelWidth, Balancing.Current.DefaultLevelHeight);
public UndergroundCell[] Electricity { get; init; } = CreateUnderground(Balancing.Current.DefaultLevelWidth, Balancing.Current.DefaultLevelHeight);
public SurfaceState[] Surface { get; init; } = CreateSurface(Balancing.Current.DefaultLevelWidth, Balancing.Current.DefaultLevelHeight);
public PropState[] Props { get; init; } = CreateProps(Balancing.Current.DefaultLevelWidth, Balancing.Current.DefaultLevelHeight);
public IReadOnlyList<DoorState> Doors { get; init; } = Array.Empty<DoorState>();
public IReadOnlyList<LeakState> Leaks { get; init; } = Array.Empty<LeakState>();
public IReadOnlyList<ReactorBinding> Reactors { get; init; } = Array.Empty<ReactorBinding>();
public IReadOnlyList<RuleEventState> RuleEvents { get; init; } = Array.Empty<RuleEventState>();
public RobotState Robot { get; init; } = new();
public GlobalState Global { get; init; } = new();
public IReadOnlyList<Forecast> Forecasts { get; init; } = Array.Empty<Forecast>();
}

View File

@@ -0,0 +1,8 @@
namespace ReactorMaintenance.Simulation;
public enum EBand
{
Safe,
Caution,
Critical
}

View File

@@ -0,0 +1,8 @@
namespace ReactorMaintenance.Simulation;
public enum ECarrierType
{
Fuel,
Coolant,
Electricity
}

View File

@@ -0,0 +1,7 @@
namespace ReactorMaintenance.Simulation;
public enum ECellTerrain
{
Floor,
Wall
}

View File

@@ -0,0 +1,10 @@
namespace ReactorMaintenance.Simulation;
public enum EConsumerServiceState
{
Unknown,
Disabled,
Starved,
Supplied,
Producing
}

View File

@@ -0,0 +1,7 @@
namespace ReactorMaintenance.Simulation;
public enum EDoorState
{
Open,
Closed
}

View File

@@ -0,0 +1,10 @@
namespace ReactorMaintenance.Simulation;
public enum EForecastKind
{
TerminalLoss,
ReactorReady,
ConsumerStarved,
HazardGrowth,
StructuralIntegrity
}

View File

@@ -0,0 +1,11 @@
namespace ReactorMaintenance.Simulation;
public enum ELevelState
{
Stable,
Caution,
Critical,
Ready,
Lost,
Won
}

View File

@@ -0,0 +1,7 @@
namespace ReactorMaintenance.Simulation;
public enum ENetworkValueKind
{
Amount,
Intensity
}

View File

@@ -0,0 +1,7 @@
namespace ReactorMaintenance.Simulation;
public enum EPropSwitchState
{
Disabled,
Enabled
}

View File

@@ -0,0 +1,13 @@
namespace ReactorMaintenance.Simulation;
public enum EPropType
{
None,
Flow,
Consumer,
Junction,
Door,
AllSeeingEyeTerminal,
RemedySupply,
ReactorControl
}

View File

@@ -0,0 +1,9 @@
namespace ReactorMaintenance.Simulation;
public enum ERemedyType
{
FuelNeutralizer,
CoolantNeutralizer,
ElectricityNeutralizer,
HeatShield
}

View File

@@ -0,0 +1,11 @@
namespace ReactorMaintenance.Simulation;
public enum ESurfaceInteractionVerb
{
Hold,
Flow,
Warm,
Quench,
Short,
Ignite
}

View File

@@ -0,0 +1,9 @@
namespace ReactorMaintenance.Simulation;
public enum ESurfaceQuantity
{
Fuel,
Coolant,
Electricity,
Heat
}

View File

@@ -0,0 +1,8 @@
namespace ReactorMaintenance.Simulation;
public enum EUndergroundState
{
Absent,
Intact,
Leaking
}

View File

@@ -0,0 +1,3 @@
namespace ReactorMaintenance.Simulation;
public sealed record Forecast(EForecastKind Kind, GridPosition? Position, int Turns, string Message);

View File

@@ -0,0 +1,10 @@
namespace ReactorMaintenance.Simulation;
public sealed record GlobalState
{
public int Turn { get; init; }
public ELevelState LevelState { get; init; } = ELevelState.Stable;
public string Status { get; init; } = "STABLE";
public bool TerminalLoss { get; init; }
public IReadOnlyList<string> Warnings { get; init; } = Array.Empty<string>();
}

View File

@@ -0,0 +1,3 @@
namespace ReactorMaintenance.Simulation;
public sealed record GridPosition(int X, int Y);

View File

@@ -0,0 +1,9 @@
namespace ReactorMaintenance.Simulation;
public sealed record LeakState
{
public ECarrierType Carrier { get; init; }
public GridPosition UndergroundPosition { get; init; } = new(0, 0);
public GridPosition AccessPosition { get; init; } = new(0, 0);
public bool Repaired { get; init; }
}

View File

@@ -0,0 +1,27 @@
namespace ReactorMaintenance.Simulation;
public sealed record LevelState
{
public static LevelState Create(string name, int width, int height)
{
return LevelStateFactory.Create(name, width, height);
}
public string Name { get; init; } = "New Reactor";
public int Width { get; init; } = Balancing.Current.DefaultLevelWidth;
public int Height { get; init; } = Balancing.Current.DefaultLevelHeight;
public ECellTerrain[] Terrain { get; init; } = LevelStateFactory.CreateTerrain(Balancing.Current.DefaultLevelWidth, Balancing.Current.DefaultLevelHeight);
public UndergroundCell[] Fuel { get; init; } = LevelStateFactory.CreateUnderground(Balancing.Current.DefaultLevelWidth, Balancing.Current.DefaultLevelHeight);
public UndergroundCell[] Coolant { get; init; } = LevelStateFactory.CreateUnderground(Balancing.Current.DefaultLevelWidth, Balancing.Current.DefaultLevelHeight);
public UndergroundCell[] Electricity { get; init; } = LevelStateFactory.CreateUnderground(Balancing.Current.DefaultLevelWidth, Balancing.Current.DefaultLevelHeight);
public SurfaceState[] Surface { get; init; } = LevelStateFactory.CreateSurface(Balancing.Current.DefaultLevelWidth, Balancing.Current.DefaultLevelHeight);
public PropState[] Props { get; init; } = LevelStateFactory.CreateProps(Balancing.Current.DefaultLevelWidth, Balancing.Current.DefaultLevelHeight);
public IReadOnlyList<LeakState> Leaks { get; init; } = Array.Empty<LeakState>();
public IReadOnlyList<ReactorState> Reactors { get; init; } = Array.Empty<ReactorState>();
public int RequiredFuelConsumers { get; init; } = 1;
public int RequiredCoolantConsumers { get; init; } = 1;
public int RequiredElectricityConsumers { get; init; } = 1;
public RobotState Robot { get; init; } = new();
public GlobalState Global { get; init; } = new();
public IReadOnlyList<Forecast> Forecasts { get; init; } = Array.Empty<Forecast>();
}

View File

@@ -0,0 +1,29 @@
namespace ReactorMaintenance.Simulation;
public sealed record PropState
{
public EConsumerServiceState ServiceStateFor(ECarrierType carrier)
{
return carrier switch {
ECarrierType.Fuel => FuelServiceState,
ECarrierType.Coolant => CoolantServiceState,
ECarrierType.Electricity => ElectricityServiceState,
_ => EConsumerServiceState.Unknown
};
}
public EPropType Type { get; init; }
public ECarrierType Carrier { get; init; }
public EPropSwitchState SwitchState { get; init; } = EPropSwitchState.Enabled;
public EConsumerServiceState ServiceState { get; init; } = EConsumerServiceState.Unknown;
public EConsumerServiceState FuelServiceState { get; init; } = EConsumerServiceState.Unknown;
public EConsumerServiceState CoolantServiceState { get; init; } = EConsumerServiceState.Unknown;
public EConsumerServiceState ElectricityServiceState { get; init; } = EConsumerServiceState.Unknown;
public int JunctionMode { get; init; }
public ERemedyType RemedyType { get; init; }
public bool Depleted { get; init; }
public int ReactorId { get; init; }
public EDoorState DoorState { get; init; } = EDoorState.Closed;
public bool IsEnabled => SwitchState == EPropSwitchState.Enabled;
}

View File

@@ -0,0 +1,9 @@
namespace ReactorMaintenance.Simulation;
public sealed record ReactorState
{
public int ReactorId { get; init; }
public GridPosition ControlPosition { get; init; } = new(0, 0);
public bool Ready { get; init; }
public bool Activated { get; init; }
}

View File

@@ -0,0 +1,11 @@
namespace ReactorMaintenance.Simulation;
public sealed record RobotState
{
public GridPosition Position { get; init; } = new(1, 1);
public int FuelNeutralizers { get; init; }
public int CoolantNeutralizers { get; init; }
public int ElectricityNeutralizers { get; init; }
public int HeatShields { get; init; }
public int HeatImmunitySteps { get; init; }
}

View File

@@ -0,0 +1,12 @@
namespace ReactorMaintenance.Simulation;
public sealed record SurfaceState
{
public float Fuel { get; init; }
public float Coolant { get; init; }
public float Electricity { get; init; }
public float Heat { get; init; }
public int FuelBlockTurns { get; init; }
public int CoolantBlockTurns { get; init; }
public int ElectricityBlockTurns { get; init; }
}

View File

@@ -0,0 +1,12 @@
namespace ReactorMaintenance.Simulation;
public sealed record UndergroundCell
{
public EUndergroundState State { get; init; }
public float Amount { get; init; }
public float Intensity { get; init; }
public int StructuralIntegrity { get; init; } = Balancing.Current.MaxStructuralIntegrity;
public bool IsPresent => State != EUndergroundState.Absent;
public bool CarriesFlow => State is EUndergroundState.Intact or EUndergroundState.Leaking;
}

View File

@@ -0,0 +1,3 @@
namespace ReactorMaintenance.Simulation;
public sealed record ValidationIssue(string Message, GridPosition? Position = null);

View File

@@ -0,0 +1,8 @@
namespace ReactorMaintenance.Simulation;
public sealed record ValidationReport
{
public IReadOnlyList<ValidationIssue> Errors { get; init; } = Array.Empty<ValidationIssue>();
public IReadOnlyList<ValidationIssue> Warnings { get; init; } = Array.Empty<ValidationIssue>();
public bool IsValid => Errors.Count == 0;
}

View File

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

View File

@@ -0,0 +1,36 @@
namespace ReactorMaintenance.Simulation;
public static class RobotStateExtensions
{
public static int Count(this RobotState robot, ERemedyType remedy)
{
return remedy switch {
ERemedyType.FuelNeutralizer => robot.FuelNeutralizers,
ERemedyType.CoolantNeutralizer => robot.CoolantNeutralizers,
ERemedyType.ElectricityNeutralizer => robot.ElectricityNeutralizers,
ERemedyType.HeatShield => robot.HeatShields,
_ => throw new ArgumentOutOfRangeException(nameof(remedy), remedy, "Unsupported remedy.")
};
}
public static RobotState Add(this RobotState robot, ERemedyType remedy, int amount)
{
return remedy switch {
ERemedyType.FuelNeutralizer => robot with { FuelNeutralizers = ClampInventory(robot.FuelNeutralizers + amount) },
ERemedyType.CoolantNeutralizer => robot with { CoolantNeutralizers = ClampInventory(robot.CoolantNeutralizers + amount) },
ERemedyType.ElectricityNeutralizer => robot with { ElectricityNeutralizers = ClampInventory(robot.ElectricityNeutralizers + amount) },
ERemedyType.HeatShield => robot with { HeatShields = ClampInventory(robot.HeatShields + amount) },
_ => throw new ArgumentOutOfRangeException(nameof(remedy), remedy, "Unsupported remedy.")
};
}
public static RobotState Spend(this RobotState robot, ERemedyType remedy)
{
return robot.Count(remedy) <= 0 ? robot : robot.Add(remedy, -1);
}
private static int ClampInventory(int value)
{
return Math.Clamp(value, 0, Balancing.Current.InventoryCapacityPerRemedy);
}
}

View File

@@ -0,0 +1,45 @@
namespace ReactorMaintenance.Simulation;
internal static class SimulationBands
{
public static EBand SurfaceBand(SurfaceState surface, ECarrierType carrier)
{
return carrier switch {
ECarrierType.Fuel => Fuel(surface.Fuel),
ECarrierType.Coolant => Coolant(surface.Coolant),
ECarrierType.Electricity => Electricity(surface.Electricity),
_ => throw new ArgumentOutOfRangeException(nameof(carrier), carrier, "Unsupported carrier.")
};
}
public static EBand NetworkBand(UndergroundCell underground, ECarrierType carrier, ENetworkValueKind valueKind)
{
var value = valueKind == ENetworkValueKind.Amount ? underground.Amount : underground.Intensity;
return carrier switch {
ECarrierType.Fuel => Fuel(value),
ECarrierType.Coolant => Coolant(value),
ECarrierType.Electricity => Electricity(value),
_ => throw new ArgumentOutOfRangeException(nameof(carrier), carrier, "Unsupported carrier.")
};
}
public static EBand Fuel(float value)
{
return Balancing.Current.Band(value, Balancing.Current.FuelCaution, Balancing.Current.FuelCritical);
}
public static EBand Coolant(float value)
{
return Balancing.Current.Band(value, Balancing.Current.CoolantCaution, Balancing.Current.CoolantCritical);
}
public static EBand Electricity(float value)
{
return Balancing.Current.Band(value, Balancing.Current.ElectricityCaution, Balancing.Current.ElectricityCritical);
}
public static EBand Heat(float value)
{
return Balancing.Current.Band(value, Balancing.Current.HeatCaution, Balancing.Current.HeatCritical);
}
}

View File

@@ -1,737 +1,75 @@
namespace ReactorMaintenance.Simulation;
namespace ReactorMaintenance.Simulation;
public sealed class SimulationEngine
{
public LevelState MoveRobot(LevelState level, GridPosition destination)
{
if (!CanSpendAction(level) || !level.IsFloor(destination) || level.Robot.Position.ManhattanDistance(destination) != 1)
return Refuse(level, "MOVE BLOCKED");
return SpendAction(level with {
Robot = level.Robot with {
Position = destination,
HeatImmunitySteps = Math.Max(0, level.Robot.HeatImmunitySteps - 1)
}
});
return PlayerActionSystem.MoveRobot(level, destination);
}
public LevelState InteractProp(LevelState level)
{
if (!CanSpendAction(level))
return Refuse(level, "NO ACTIONS");
var position = level.Robot.Position;
var prop = level.GetProp(position);
if (prop.Type == EPropType.None)
return Refuse(level, "NO PROP");
var next = prop.Type switch {
EPropType.Flow or EPropType.Consumer => ToggleProp(level, position, prop),
EPropType.TJunction => level.SetProp(position, prop with { TJunctionMode = NextTJunctionMode(prop.TJunctionMode) }),
EPropType.CrossJunction => level.SetProp(position, prop with { CrossJunctionMode = NextCrossJunctionMode(prop.CrossJunctionMode) }),
EPropType.Door => ToggleDoor(level, position),
EPropType.AllSeeingEyeTerminal => level with { Global = level.Global with { AllSeeingEyeUnlocked = true, Status = "ALL-SEEING-EYE ONLINE" } },
EPropType.RemedySupply => PickUpRemedy(level, position, prop),
EPropType.ReactorControl => ActivateReactor(level),
_ => level
};
return SpendAction(next);
return PlayerActionSystem.InteractProp(level, ResolveStep);
}
public LevelState InteractLeak(LevelState level, ECarrierType carrier, bool useRemedy)
{
if (!CanSpendAction(level))
return Refuse(level, "NO ACTIONS");
var leakIndex = level.Leaks.ToList().FindIndex(leak => !leak.Repaired && leak.Carrier == carrier && leak.AccessPosition == level.Robot.Position);
if (leakIndex < 0)
return Refuse(level, "NO REACHABLE LEAK");
var leak = level.Leaks[leakIndex];
var next = useRemedy ? ApplyElementRemedy(level, leak) : RepairLeak(level, leakIndex, leak);
return SpendAction(next);
return PlayerActionSystem.InteractLeak(level, carrier, useRemedy, ResolveStep);
}
public LevelState ApplyHeatShield(LevelState level)
{
if (!CanSpendAction(level) || level.Robot.HeatShields <= 0)
return Refuse(level, "NO HEAT SHIELD");
return PlayerActionSystem.ApplyHeatShield(level, ResolveStep);
}
return SpendAction(level with {
Robot = level.Robot.Spend(ERemedyType.HeatShield) with { HeatImmunitySteps = Balancing.Current.HeatShieldSteps }
});
private LevelState ResolveStep(LevelState level)
{
return ResolveStep(level, true);
}
public LevelState ActivateReactor(LevelState level)
{
var reactorIndex = level.Reactors.ToList().FindIndex(reactor => reactor.ControlPosition == level.Robot.Position);
if (reactorIndex < 0)
return Refuse(level, "NO REACTOR CONTROL");
var reactor = level.Reactors[reactorIndex];
if (!reactor.Ready)
return Refuse(level, "REACTOR NOT READY");
var reactors = level.Reactors.ToArray();
reactors[reactorIndex] = reactor with { Activated = true };
return level with {
Reactors = reactors,
Global = level.Global with { LevelState = ELevelState.Won, Status = "REACTOR ONLINE" }
};
return ReactorSystem.Activate(level);
}
public LevelState EndTurn(LevelState level)
{
return ResolveTurn(level with { Global = level.Global with { ActionsRemaining = 0 } });
return ResolveStep(level);
}
public LevelState AdvanceTurn(LevelState level)
{
return ResolveTurn(level);
return ResolveStep(level);
}
public IReadOnlyList<Forecast> Forecast(LevelState level)
{
var forecasts = new List<Forecast>();
var simulated = CopyForForecast(level);
for (var turn = 0; turn <= Balancing.Current.ForecastHorizon; turn++)
{
AddForecasts(forecasts, simulated, turn);
if (simulated.Global.LevelState is ELevelState.Lost or ELevelState.Ready or ELevelState.Won)
break;
if (turn < Balancing.Current.ForecastHorizon)
simulated = ResolveTurn(simulated, false);
}
return forecasts.DistinctBy(forecast => (forecast.Kind, forecast.Position, forecast.Message)).OrderBy(forecast => forecast.Turns).ThenBy(forecast => forecast.Message).ToArray();
return ForecastSystem.Forecast(level, simulated => ResolveStep(simulated, false));
}
private LevelState ResolveTurn(LevelState level, bool refreshForecasts = true)
private LevelState ResolveStep(LevelState level, bool refreshForecasts)
{
var report = m_Validator.Validate(level);
if (!report.IsValid)
return level with { Global = level.Global with { LevelState = ELevelState.Lost, Status = report.Errors[0].Message } };
var next = ApplyRuleEvents(level, ERuleEventPhase.StartOfSimulation);
next = PropagateNetworks(next);
next = ResolveConsumers(next);
next = InjectLeaks(next);
next = ResolveSurfaceInteractions(next);
next = ResolveRobotSafety(next);
next = DeriveReactorAndLevelState(next);
next = ApplyRuleEvents(next, ERuleEventPhase.EndOfTurn);
next = AdvanceDurations(next);
var next = level;
next = NetworkPropagationSystem.Propagate(next);
next = ConsumerSystem.Resolve(next);
next = StructuralIntegritySystem.Resolve(next);
next = LeakSystem.Inject(next);
next = SurfaceInteractionSystem.Resolve(next);
next = RobotSafetySystem.Resolve(next);
next = ReactorSystem.DeriveState(next);
next = SurfaceInteractionSystem.AdvanceDurations(next);
next = next with {
Global = next.Global with {
Turn = next.Global.Turn + 1,
ActionsRemaining = Balancing.Current.ActionsPerTurn
Turn = next.Global.Turn + 1
}
};
return refreshForecasts ? next with { Forecasts = Forecast(next) } : next;
}
private LevelState PropagateNetworks(LevelState level)
{
var fuel = ClearTransient(level.Fuel);
var coolant = ClearTransient(level.Coolant);
var electricity = ClearTransient(level.Electricity);
var next = level.WithRuntimeArrays(fuel, coolant, electricity, level.Surface.ToArray(), level.Props.ToArray());
foreach (var carrier in Enum.GetValues<ECarrierType>())
next = PropagateCarrier(next, carrier);
return next;
}
private static UndergroundCell[] ClearTransient(IReadOnlyList<UndergroundCell> layer)
{
return layer.Select(cell => cell with { Amount = 0, Intensity = 0 }).ToArray();
}
private LevelState PropagateCarrier(LevelState level, ECarrierType carrier)
{
var layer = level.Layer(carrier).ToArray();
var sources = AllPositions(level).Where(position => level.GetProp(position) is { Type: EPropType.Flow, SwitchState: EPropSwitchState.Enabled, Carrier: var sourceCarrier } && sourceCarrier == carrier && level.GetUnderground(position, carrier).CarriesFlow).ToArray();
foreach (var source in sources)
ApplySourceFlow(level, layer, source, carrier);
return carrier switch {
ECarrierType.Fuel => level with { Fuel = layer },
ECarrierType.Coolant => level with { Coolant = layer },
ECarrierType.Electricity => level with { Electricity = layer },
_ => throw new ArgumentOutOfRangeException(nameof(carrier), carrier, "Unsupported carrier.")
};
}
private static void ApplySourceFlow(LevelState level, UndergroundCell[] layer, GridPosition source, ECarrierType carrier)
{
var open = new Queue<(GridPosition Position, int Distance, float AmountFactor, float IntensityFactor)>();
var best = new Dictionary<GridPosition, float>();
open.Enqueue((source, 0, 1, 1));
best[source] = 1;
while (open.Count > 0)
{
var current = open.Dequeue();
var amount = Balancing.Current.ClampValue((Balancing.Current.SourceAmount * current.AmountFactor) - (current.Distance * Balancing.Current.DistanceAmountFalloff));
var intensity = Balancing.Current.ClampValue((Balancing.Current.SourceIntensity * current.IntensityFactor) - (current.Distance * Balancing.Current.DistanceIntensityFalloff));
var index = level.Index(current.Position);
layer[index] = layer[index] with {
Amount = Math.Max(layer[index].Amount, amount),
Intensity = Math.Max(layer[index].Intensity, intensity)
};
foreach (var next in current.Position.Neighbors().Where(level.InBounds))
{
if (!level.GetUnderground(next, carrier).CarriesFlow)
continue;
var weights = BranchWeights(level, current.Position, next);
var amountFactor = current.AmountFactor * weights.Amount;
var intensityFactor = current.IntensityFactor * weights.Intensity;
if (amountFactor <= 0 || intensityFactor <= 0)
continue;
if (best.TryGetValue(next, out var oldBest) && oldBest >= amountFactor)
continue;
best[next] = amountFactor;
open.Enqueue((next, current.Distance + 1, amountFactor, intensityFactor));
}
}
}
private static (float Amount, float Intensity) BranchWeights(LevelState level, GridPosition from, GridPosition to)
{
var prop = level.GetProp(from);
return prop.Type switch {
EPropType.TJunction => TJunctionWeights(prop.TJunctionMode),
EPropType.CrossJunction => CrossJunctionWeights(prop.CrossJunctionMode),
_ => (1, 1)
};
}
private static (float Amount, float Intensity) TJunctionWeights(ETJunctionMode mode)
{
var weight = mode switch {
ETJunctionMode.ZeroFour => 0,
ETJunctionMode.OneThree => 0.25f,
ETJunctionMode.TwoTwo => 0.5f,
ETJunctionMode.ThreeOne => 0.75f,
ETJunctionMode.FourZero => 1,
_ => throw new ArgumentOutOfRangeException(nameof(mode), mode, "Unsupported T-junction mode.")
};
return (weight, weight);
}
private static (float Amount, float Intensity) CrossJunctionWeights(ECrossJunctionMode mode)
{
var weight = mode switch {
ECrossJunctionMode.ZeroThreeThree => 0,
ECrossJunctionMode.ThreeZeroThree => 0.5f,
ECrossJunctionMode.ThreeThreeZero => 0.5f,
ECrossJunctionMode.TwoTwoTwo => 1f / 3f,
_ => throw new ArgumentOutOfRangeException(nameof(mode), mode, "Unsupported cross-junction mode.")
};
return (weight, weight);
}
private LevelState ResolveConsumers(LevelState level)
{
var props = level.Props.ToArray();
foreach (var position in AllPositions(level))
{
var index = level.Index(position);
var prop = props[index];
if (prop.Type != EPropType.Consumer)
continue;
if (prop.SwitchState == EPropSwitchState.Disabled)
{
props[index] = prop with { ServiceState = EConsumerServiceState.Disabled };
continue;
}
var underground = level.GetUnderground(position, prop.Carrier);
var supplied = underground.Amount >= Balancing.Current.ConsumerRequiredAmount && underground.Intensity >= Balancing.Current.ConsumerRequiredIntensity;
props[index] = prop with { ServiceState = supplied ? EConsumerServiceState.Producing : EConsumerServiceState.Starved };
}
return level with { Props = props };
}
private LevelState InjectLeaks(LevelState level)
{
var surface = level.Surface.ToArray();
foreach (var leak in level.Leaks.Where(leak => !leak.Repaired))
{
var underground = level.GetUnderground(leak.UndergroundPosition, leak.Carrier);
if (underground.State != EUndergroundState.Leaking)
continue;
var accessIndex = level.Index(leak.AccessPosition);
if (surface[accessIndex].Blocks(leak.Carrier))
continue;
var amount = Balancing.Current.LeakBaseAmount + underground.Amount * Balancing.Current.LeakAmountScale + underground.Intensity * Balancing.Current.LeakIntensityScale;
surface[accessIndex] = AddSurfaceCarrier(surface[accessIndex], leak.Carrier, amount);
}
return level with { Surface = surface.Select(cell => cell.Clamp()).ToArray() };
}
private LevelState ResolveSurfaceInteractions(LevelState level)
{
var deltas = Enumerable.Range(0, level.Width * level.Height).Select(_ => new SurfaceDelta()).ToArray();
foreach (var position in AllPositions(level).Where(level.IsFloor))
ApplySameCellInteractions(level, position, deltas);
foreach (var position in AllPositions(level).Where(level.IsFloor))
{
foreach (var neighbor in position.Neighbors().Where(level.IsFloor))
{
if (level.Index(position) >= level.Index(neighbor) || level.IsClosedDoorEdge(position, neighbor))
continue;
ApplyAdjacentInteractions(level, position, neighbor, deltas);
}
}
var surface = level.Surface.ToArray();
for (var i = 0; i < surface.Length; i++)
surface[i] = deltas[i].Apply(surface[i]).Clamp();
return level with { Surface = surface };
}
private static void ApplySameCellInteractions(LevelState level, GridPosition position, SurfaceDelta[] deltas)
{
var surface = level.GetSurface(position);
ApplyPair(level, position, position, ECarrierType.Fuel, BandFuel(surface.Fuel), ECarrierType.Electricity, BandElectricity(surface.Electricity), deltas);
ApplyPair(level, position, position, ECarrierType.Fuel, BandFuel(surface.Fuel), null, BandHeat(surface.Heat), deltas);
ApplyPair(level, position, position, ECarrierType.Coolant, BandCoolant(surface.Coolant), ECarrierType.Electricity, BandElectricity(surface.Electricity), deltas);
ApplyPair(level, position, position, ECarrierType.Coolant, BandCoolant(surface.Coolant), null, BandHeat(surface.Heat), deltas);
}
private static void ApplyAdjacentInteractions(LevelState level, GridPosition a, GridPosition b, SurfaceDelta[] deltas)
{
var surfaceA = level.GetSurface(a);
var surfaceB = level.GetSurface(b);
FlowBetween(level, a, b, surfaceA.Fuel, surfaceB.Fuel, EPairEffect.FuelFlow, deltas);
FlowBetween(level, a, b, surfaceA.Coolant, surfaceB.Coolant, EPairEffect.CoolFlow, deltas);
FlowBetween(level, a, b, surfaceA.Electricity, surfaceB.Electricity, EPairEffect.ChargeFlow, deltas);
FlowBetween(level, a, b, surfaceA.Heat, surfaceB.Heat, EPairEffect.HeatFlow, deltas);
}
private static void ApplyPair(LevelState level, GridPosition a, GridPosition b, ECarrierType? rowCarrier, EBand rowBand, ECarrierType? colCarrier, EBand colBand, SurfaceDelta[] deltas)
{
ApplyEffect(level, a, b, PairEffect(rowCarrier, rowBand, colCarrier, colBand), deltas);
}
private static EPairEffect PairEffect(ECarrierType? rowCarrier, EBand rowBand, ECarrierType? colCarrier, EBand colBand)
{
if (rowBand == EBand.Safe && colBand == EBand.Safe)
return EPairEffect.Hold;
if (rowCarrier == colCarrier)
return rowCarrier switch {
ECarrierType.Fuel => EPairEffect.FuelFlow,
ECarrierType.Coolant => EPairEffect.CoolFlow,
ECarrierType.Electricity => EPairEffect.ChargeFlow,
_ => EPairEffect.HeatFlow
};
if (rowCarrier == ECarrierType.Fuel && colCarrier == ECarrierType.Electricity)
return rowBand == EBand.Critical || colBand == EBand.Critical ? EPairEffect.Ignite2 : EPairEffect.Ignite1;
if (rowCarrier == ECarrierType.Fuel && colCarrier is null)
return rowBand == EBand.Critical || colBand == EBand.Critical ? EPairEffect.Ignite2 : EPairEffect.Warm1;
if (rowCarrier == ECarrierType.Coolant && colCarrier == ECarrierType.Electricity)
return rowBand == EBand.Critical || colBand == EBand.Critical ? EPairEffect.Short2 : EPairEffect.Short1;
if (rowCarrier == ECarrierType.Coolant && colCarrier is null)
return rowBand == EBand.Critical || colBand == EBand.Critical ? EPairEffect.Quench2 : EPairEffect.Quench1;
return EPairEffect.Hold;
}
private static void ApplyEffect(LevelState level, GridPosition a, GridPosition b, EPairEffect effect, SurfaceDelta[] deltas)
{
var index = level.Index(a);
switch (effect)
{
case EPairEffect.Warm1:
deltas[index].Heat += Balancing.Current.Warm1Amount;
break;
case EPairEffect.Warm2:
deltas[index].Heat += Balancing.Current.Warm2Amount;
break;
case EPairEffect.Quench1:
deltas[index].Heat -= Balancing.Current.Quench1Amount;
break;
case EPairEffect.Quench2:
deltas[index].Heat -= Balancing.Current.Quench2Amount;
break;
case EPairEffect.Short1:
deltas[index].Heat += Balancing.Current.Short1Heat;
deltas[index].Electricity -= Balancing.Current.Short1Discharge;
break;
case EPairEffect.Short2:
deltas[index].Heat += Balancing.Current.Short2Heat;
deltas[index].Electricity -= Balancing.Current.Short2Discharge;
break;
case EPairEffect.Ignite1:
deltas[index].Heat += Balancing.Current.Ignite1Heat;
deltas[index].Fuel -= Balancing.Current.Ignite1FuelConsumption;
break;
case EPairEffect.Ignite2:
deltas[index].Heat += Balancing.Current.Ignite2Heat;
deltas[index].Fuel -= Balancing.Current.Ignite2FuelConsumption;
break;
}
}
private static void FlowBetween(LevelState level, GridPosition a, GridPosition b, float valueA, float valueB, EPairEffect effect, SurfaceDelta[] deltas)
{
var difference = valueA - valueB;
if (Math.Abs(difference) < 0.01f)
return;
var amount = difference * (effect == EPairEffect.HeatFlow2 ? Balancing.Current.StrongFlowTransferRatio : Balancing.Current.FlowTransferRatio);
var indexA = level.Index(a);
var indexB = level.Index(b);
switch (effect)
{
case EPairEffect.FuelFlow:
deltas[indexA].Fuel -= amount;
deltas[indexB].Fuel += amount;
break;
case EPairEffect.CoolFlow:
deltas[indexA].Coolant -= amount;
deltas[indexB].Coolant += amount;
break;
case EPairEffect.ChargeFlow:
deltas[indexA].Electricity -= amount;
deltas[indexB].Electricity += amount;
break;
case EPairEffect.HeatFlow:
case EPairEffect.HeatFlow2:
deltas[indexA].Heat -= amount;
deltas[indexB].Heat += amount;
break;
}
}
private LevelState ResolveRobotSafety(LevelState level)
{
var surface = level.GetSurface(level.Robot.Position);
var unsafeElement = surface.Fuel >= Balancing.Current.RobotFuelSafetyThreshold || surface.Coolant >= Balancing.Current.RobotCoolantSafetyThreshold || surface.Electricity >= Balancing.Current.RobotElectricitySafetyThreshold;
var unsafeHeat = surface.Heat >= Balancing.Current.RobotHeatSafetyThreshold && level.Robot.HeatImmunitySteps <= 0;
return unsafeElement || unsafeHeat
? level with { Global = level.Global with { LevelState = ELevelState.Lost, TerminalLoss = true, Status = "ROBOT LOST" } }
: level;
}
private LevelState DeriveReactorAndLevelState(LevelState level)
{
if (level.Global.LevelState is ELevelState.Lost or ELevelState.Won)
return level;
var reactors = level.Reactors.Select(reactor => reactor with { Ready = IsReactorReady(level, reactor) }).ToArray();
if (reactors.Any(reactor => reactor.Ready))
return level with { Reactors = reactors, Global = level.Global with { LevelState = ELevelState.Ready, Status = "REACTOR READY" } };
var maxHeat = level.Surface.Where((_, index) => level.Terrain[index] == ECellTerrain.Floor).Select(surface => surface.Heat).DefaultIfEmpty(0).Max();
if (maxHeat >= Balancing.Current.TerminalHeat)
return level with { Reactors = reactors, Global = level.Global with { LevelState = ELevelState.Lost, TerminalLoss = true, Status = "REACTOR HEAT TERMINAL" } };
var hasCritical = level.Surface.Any(surface => BandFuel(surface.Fuel) == EBand.Critical || BandCoolant(surface.Coolant) == EBand.Critical || BandElectricity(surface.Electricity) == EBand.Critical || BandHeat(surface.Heat) == EBand.Critical);
var hasCaution = hasCritical || level.Props.Any(prop => prop.ServiceState is EConsumerServiceState.Starved or EConsumerServiceState.Disabled) || level.Leaks.Any(leak => !leak.Repaired);
var state = hasCritical ? ELevelState.Critical :
hasCaution ? ELevelState.Caution : ELevelState.Stable;
return level with { Reactors = reactors, Global = level.Global with { LevelState = state, Status = state.ToString().ToUpperInvariant() } };
}
private static bool IsReactorReady(LevelState level, ReactorBinding reactor)
{
return HasProducingConsumer(level, reactor.FuelConsumerPosition, ECarrierType.Fuel)
&& HasProducingConsumer(level, reactor.CoolantConsumerPosition, ECarrierType.Coolant)
&& HasProducingConsumer(level, reactor.ElectricityConsumerPosition, ECarrierType.Electricity)
&& level.GetSurface(reactor.ControlPosition).Heat < Balancing.Current.TerminalHeat;
}
private static bool HasProducingConsumer(LevelState level, GridPosition position, ECarrierType carrier)
{
return level.InBounds(position) && level.GetProp(position) is { Type: EPropType.Consumer, Carrier: var consumerCarrier, ServiceState: EConsumerServiceState.Producing } && consumerCarrier == carrier;
}
private LevelState ApplyRuleEvents(LevelState level, ERuleEventPhase phase)
{
var next = level;
var events = level.RuleEvents.Select((ruleEvent, index) => (Event: ruleEvent, Index: index)).Where(item => item.Event.Enabled && item.Event.Phase == phase && (item.Event.Repeat || !item.Event.Triggered)).OrderBy(item => item.Event.Priority).ToArray();
var ruleEvents = next.RuleEvents.ToArray();
foreach (var item in events)
{
if (!item.Event.Predicates.All(predicate => PredicateMatches(next, predicate)))
continue;
foreach (var effect in item.Event.Effects)
next = ApplyRuleEffect(next, effect);
ruleEvents[item.Index] = item.Event with { Triggered = true };
}
return next with { RuleEvents = ruleEvents };
}
private static bool PredicateMatches(LevelState level, RulePredicate predicate)
{
return predicate.Kind switch {
ERulePredicateKind.TurnAtLeast => level.Global.Turn >= predicate.Turn,
ERulePredicateKind.LevelStateIs => level.Global.LevelState == predicate.LevelState,
ERulePredicateKind.PropStateAt => level.InBounds(predicate.Position) && level.GetProp(predicate.Position).SwitchState == predicate.PropSwitchState,
ERulePredicateKind.ConsumerStateAt => level.InBounds(predicate.Position) && level.GetProp(predicate.Position).ServiceState == predicate.ConsumerServiceState,
ERulePredicateKind.SurfaceBandAt => level.InBounds(predicate.Position) && SurfaceBand(level.GetSurface(predicate.Position), predicate.Carrier) >= predicate.Band,
ERulePredicateKind.RobotAt => level.Robot.Position == predicate.Position,
ERulePredicateKind.AllSeeingEyeUnlocked => level.Global.AllSeeingEyeUnlocked == predicate.BoolValue,
_ => false
};
}
private static LevelState ApplyRuleEffect(LevelState level, RuleEffect effect)
{
return effect.Kind switch {
ERuleEffectKind.StartLeak => StartLeak(level, effect),
ERuleEffectKind.WorsenLeak => level.SetUnderground(effect.Position, effect.Carrier, level.GetUnderground(effect.Position, effect.Carrier) with { State = EUndergroundState.Leaking }),
ERuleEffectKind.RepairNetworkCell => level.SetUnderground(effect.Position, effect.Carrier, level.GetUnderground(effect.Position, effect.Carrier) with { State = EUndergroundState.Intact }),
ERuleEffectKind.DisableNetworkCell => level.SetUnderground(effect.Position, effect.Carrier, new()),
ERuleEffectKind.SetPropEnabled => level.SetProp(effect.Position, level.GetProp(effect.Position) with { SwitchState = effect.PropSwitchState }),
ERuleEffectKind.AddSurfaceHazard => level.SetSurface(effect.Position, AddSurfaceCarrier(level.GetSurface(effect.Position), effect.Carrier, effect.Amount)),
ERuleEffectKind.AddHeat => level.SetSurface(effect.Position, level.GetSurface(effect.Position) with { Heat = level.GetSurface(effect.Position).Heat + effect.Amount }),
ERuleEffectKind.AddInventory => level with { Robot = level.Robot.Add(effect.Remedy, (int)effect.Amount) },
ERuleEffectKind.MarkTerminalLoss => level with { Global = level.Global with { LevelState = ELevelState.Lost, TerminalLoss = true, Status = string.IsNullOrWhiteSpace(effect.Message) ? "TERMINAL FAILURE" : effect.Message } },
ERuleEffectKind.EmitWarning => level with { Global = level.Global with { Warnings = [.. level.Global.Warnings, effect.Message] } },
_ => level
};
}
private static LevelState StartLeak(LevelState level, RuleEffect effect)
{
var leak = new LeakState {
Carrier = effect.Carrier,
UndergroundPosition = effect.Position,
AccessPosition = effect.Position
};
return level.SetUnderground(effect.Position, effect.Carrier, level.GetUnderground(effect.Position, effect.Carrier) with { State = EUndergroundState.Leaking }) with { Leaks = [.. level.Leaks, leak] };
}
private LevelState AdvanceDurations(LevelState level)
{
var surface = level.Surface.Select(cell => cell with {
FuelBlockTurns = Math.Max(0, cell.FuelBlockTurns - 1),
CoolantBlockTurns = Math.Max(0, cell.CoolantBlockTurns - 1),
ElectricityBlockTurns = Math.Max(0, cell.ElectricityBlockTurns - 1)
})
.ToArray();
return level with { Surface = surface };
}
private static LevelState ToggleProp(LevelState level, GridPosition position, PropState prop)
{
var switchState = prop.SwitchState == EPropSwitchState.Enabled ? EPropSwitchState.Disabled : EPropSwitchState.Enabled;
return level.SetProp(position, prop with { SwitchState = switchState });
}
private static LevelState ToggleDoor(LevelState level, GridPosition position)
{
var doors = level.Doors.ToArray();
var index = Array.FindIndex(doors, door => door.A == position || door.B == position);
if (index < 0)
return level;
doors[index] = doors[index] with { State = doors[index].State == EDoorState.Open ? EDoorState.Closed : EDoorState.Open };
return level with { Doors = doors };
}
private static LevelState PickUpRemedy(LevelState level, GridPosition position, PropState prop)
{
if (prop.Depleted || level.Robot.Count(prop.RemedyType) >= Balancing.Current.InventoryCapacityPerRemedy)
return level;
return level.SetProp(position, prop with { Depleted = true }) with { Robot = level.Robot.Add(prop.RemedyType, 1) };
}
private static LevelState RepairLeak(LevelState level, int leakIndex, LeakState leak)
{
var leaks = level.Leaks.ToArray();
leaks[leakIndex] = leak with { Repaired = true };
return level.SetUnderground(leak.UndergroundPosition, leak.Carrier, level.GetUnderground(leak.UndergroundPosition, leak.Carrier) with { State = EUndergroundState.Intact }) with { Leaks = leaks };
}
private static LevelState ApplyElementRemedy(LevelState level, LeakState leak)
{
var remedy = leak.Carrier switch {
ECarrierType.Fuel => ERemedyType.FuelNeutralizer,
ECarrierType.Coolant => ERemedyType.CoolantNeutralizer,
ECarrierType.Electricity => ERemedyType.ElectricityNeutralizer,
_ => throw new ArgumentOutOfRangeException(nameof(leak), leak.Carrier, "Unsupported leak carrier.")
};
if (level.Robot.Count(remedy) <= 0)
return Refuse(level, "NO REMEDY");
var surface = RemoveSurfaceCarrier(level.GetSurface(leak.AccessPosition), leak.Carrier);
return level.SetSurface(leak.AccessPosition, surface) with { Robot = level.Robot.Spend(remedy) };
}
private static SurfaceState AddSurfaceCarrier(SurfaceState surface, ECarrierType carrier, float amount)
{
return carrier switch {
ECarrierType.Fuel => surface with { Fuel = surface.Fuel + amount },
ECarrierType.Coolant => surface with { Coolant = surface.Coolant + amount },
ECarrierType.Electricity => surface with { Electricity = surface.Electricity + amount },
_ => throw new ArgumentOutOfRangeException(nameof(carrier), carrier, "Unsupported carrier.")
};
}
private static SurfaceState RemoveSurfaceCarrier(SurfaceState surface, ECarrierType carrier)
{
return carrier switch {
ECarrierType.Fuel => surface with { Fuel = 0, FuelBlockTurns = Balancing.Current.RemedyBlockTurns },
ECarrierType.Coolant => surface with { Coolant = 0, CoolantBlockTurns = Balancing.Current.RemedyBlockTurns },
ECarrierType.Electricity => surface with { Electricity = 0, ElectricityBlockTurns = Balancing.Current.RemedyBlockTurns },
_ => throw new ArgumentOutOfRangeException(nameof(carrier), carrier, "Unsupported carrier.")
};
}
private LevelState SpendAction(LevelState level)
{
var actions = Math.Max(0, level.Global.ActionsRemaining - 1);
var next = level with { Global = level.Global with { ActionsRemaining = actions } };
return actions == 0 ? ResolveTurn(next) : next;
}
private static bool CanSpendAction(LevelState level)
{
return level.Global.LevelState is not (ELevelState.Lost or ELevelState.Won) && level.Global.ActionsRemaining > 0;
}
private static LevelState Refuse(LevelState level, string message)
{
return level with { Global = level.Global with { Status = message } };
}
private static ETJunctionMode NextTJunctionMode(ETJunctionMode mode)
{
return mode == ETJunctionMode.FourZero ? ETJunctionMode.ZeroFour : mode + 1;
}
private static ECrossJunctionMode NextCrossJunctionMode(ECrossJunctionMode mode)
{
return mode == ECrossJunctionMode.TwoTwoTwo ? ECrossJunctionMode.ZeroThreeThree : mode + 1;
}
private static EBand SurfaceBand(SurfaceState surface, ECarrierType carrier)
{
return carrier switch {
ECarrierType.Fuel => BandFuel(surface.Fuel),
ECarrierType.Coolant => BandCoolant(surface.Coolant),
ECarrierType.Electricity => BandElectricity(surface.Electricity),
_ => throw new ArgumentOutOfRangeException(nameof(carrier), carrier, "Unsupported carrier.")
};
}
private static EBand BandFuel(float value)
{
return Balancing.Current.Band(value, Balancing.Current.FuelCaution, Balancing.Current.FuelCritical);
}
private static EBand BandCoolant(float value)
{
return Balancing.Current.Band(value, Balancing.Current.CoolantCaution, Balancing.Current.CoolantCritical);
}
private static EBand BandElectricity(float value)
{
return Balancing.Current.Band(value, Balancing.Current.ElectricityCaution, Balancing.Current.ElectricityCritical);
}
private static EBand BandHeat(float value)
{
return Balancing.Current.Band(value, Balancing.Current.HeatCaution, Balancing.Current.HeatCritical);
}
private static LevelState CopyForForecast(LevelState level)
{
return level with {
Terrain = level.Terrain.ToArray(),
Fuel = level.Fuel.ToArray(),
Coolant = level.Coolant.ToArray(),
Electricity = level.Electricity.ToArray(),
Surface = level.Surface.ToArray(),
Props = level.Props.ToArray(),
Forecasts = Array.Empty<Forecast>()
};
}
private static void AddForecasts(List<Forecast> forecasts, LevelState level, int turn)
{
if (level.Global.LevelState == ELevelState.Lost)
forecasts.Add(new(EForecastKind.TerminalLoss, level.Robot.Position, turn, level.Global.Status));
if (level.Global.LevelState == ELevelState.Ready)
forecasts.Add(new(EForecastKind.ReactorReady, null, turn, "REACTOR READY"));
foreach (var position in AllPositions(level))
{
var prop = level.GetProp(position);
if (prop.Type == EPropType.Consumer && prop.ServiceState == EConsumerServiceState.Starved)
forecasts.Add(new(EForecastKind.ConsumerStarved, position, turn, $"{prop.Carrier} consumer starved"));
var surface = level.GetSurface(position);
if (BandFuel(surface.Fuel) == EBand.Critical || BandCoolant(surface.Coolant) == EBand.Critical || BandElectricity(surface.Electricity) == EBand.Critical || BandHeat(surface.Heat) == EBand.Critical)
forecasts.Add(new(EForecastKind.HazardGrowth, position, turn, "Critical hazard"));
}
foreach (var ruleEvent in level.RuleEvents.Where(ruleEvent => ruleEvent.Enabled && !string.IsNullOrWhiteSpace(ruleEvent.ForecastText) && ruleEvent.Predicates.All(predicate => PredicateMatches(level, predicate))))
forecasts.Add(new(EForecastKind.RuleEvent, null, turn, ruleEvent.ForecastText));
}
private static IEnumerable<GridPosition> AllPositions(LevelState level)
{
for (var y = 0; y < level.Height; y++)
{
for (var x = 0; x < level.Width; x++)
yield return new(x, y);
}
}
private sealed class SurfaceDelta
{
public SurfaceState Apply(SurfaceState surface)
{
return surface with {
Fuel = surface.Fuel + Fuel,
Coolant = surface.Coolant + Coolant,
Electricity = surface.Electricity + Electricity,
Heat = surface.Heat + Heat
};
}
public float Fuel { get; set; }
public float Coolant { get; set; }
public float Electricity { get; set; }
public float Heat { get; set; }
}
private readonly LevelValidator m_Validator = new();
}

View File

@@ -0,0 +1,24 @@
namespace ReactorMaintenance.Simulation;
internal static class SurfaceCarrierMath
{
public static SurfaceState AddCarrier(SurfaceState surface, ECarrierType carrier, float amount)
{
return carrier switch {
ECarrierType.Fuel => surface with { Fuel = surface.Fuel + amount },
ECarrierType.Coolant => surface with { Coolant = surface.Coolant + amount },
ECarrierType.Electricity => surface with { Electricity = surface.Electricity + amount },
_ => throw new ArgumentOutOfRangeException(nameof(carrier), carrier, "Unsupported carrier.")
};
}
public static SurfaceState RemoveCarrier(SurfaceState surface, ECarrierType carrier)
{
return carrier switch {
ECarrierType.Fuel => surface with { Fuel = 0, FuelBlockTurns = Balancing.Current.RemedyBlockTurns },
ECarrierType.Coolant => surface with { Coolant = 0, CoolantBlockTurns = Balancing.Current.RemedyBlockTurns },
ECarrierType.Electricity => surface with { Electricity = 0, ElectricityBlockTurns = Balancing.Current.RemedyBlockTurns },
_ => throw new ArgumentOutOfRangeException(nameof(carrier), carrier, "Unsupported carrier.")
};
}
}

View File

@@ -0,0 +1,11 @@
namespace ReactorMaintenance.Simulation;
public sealed record SurfaceInteractionEffect
{
public static SurfaceInteractionEffect Hold { get; } = new();
public ESurfaceInteractionVerb Verb { get; init; }
public ESurfaceQuantity Quantity { get; init; }
public float Amount { get; init; }
public float SecondaryAmount { get; init; }
}

View File

@@ -0,0 +1,28 @@
namespace ReactorMaintenance.Simulation;
public static class SurfaceStateExtensions
{
public static SurfaceState Clamp(this SurfaceState surface)
{
var balancing = Balancing.Current;
return surface with {
Fuel = balancing.ClampValue(surface.Fuel),
Coolant = balancing.ClampValue(surface.Coolant),
Electricity = balancing.ClampValue(surface.Electricity),
Heat = balancing.ClampValue(surface.Heat),
FuelBlockTurns = Math.Max(0, surface.FuelBlockTurns),
CoolantBlockTurns = Math.Max(0, surface.CoolantBlockTurns),
ElectricityBlockTurns = Math.Max(0, surface.ElectricityBlockTurns)
};
}
public static bool Blocks(this SurfaceState surface, ECarrierType carrier)
{
return carrier switch {
ECarrierType.Fuel => surface.FuelBlockTurns > 0,
ECarrierType.Coolant => surface.CoolantBlockTurns > 0,
ECarrierType.Electricity => surface.ElectricityBlockTurns > 0,
_ => throw new ArgumentOutOfRangeException(nameof(carrier), carrier, "Unsupported carrier.")
};
}
}

View File

@@ -0,0 +1,72 @@
namespace ReactorMaintenance.Simulation;
internal static class ConsumerSystem
{
public static LevelState Resolve(LevelState level)
{
var props = level.Props.ToArray();
foreach (var position in LevelTraversal.AllPositions(level))
{
var index = level.Index(position);
var prop = props[index];
if (prop.Type != EPropType.Consumer)
continue;
if (prop.SwitchState == EPropSwitchState.Disabled)
{
var disabledFuel = DisabledServiceStateFor(level, position, ECarrierType.Fuel);
var disabledCoolant = DisabledServiceStateFor(level, position, ECarrierType.Coolant);
var disabledElectricity = DisabledServiceStateFor(level, position, ECarrierType.Electricity);
props[index] = prop with {
ServiceState = Aggregate(disabledFuel, disabledCoolant, disabledElectricity),
FuelServiceState = disabledFuel,
CoolantServiceState = disabledCoolant,
ElectricityServiceState = disabledElectricity
};
continue;
}
var fuel = ServiceStateFor(level, position, ECarrierType.Fuel);
var coolant = ServiceStateFor(level, position, ECarrierType.Coolant);
var electricity = ServiceStateFor(level, position, ECarrierType.Electricity);
props[index] = prop with {
ServiceState = Aggregate(fuel, coolant, electricity),
FuelServiceState = fuel,
CoolantServiceState = coolant,
ElectricityServiceState = electricity
};
}
return level with { Props = props };
}
private static EConsumerServiceState ServiceStateFor(LevelState level, GridPosition position, ECarrierType carrier)
{
var underground = level.GetUnderground(position, carrier);
if (!underground.IsPresent)
return EConsumerServiceState.Unknown;
var supplied = underground.Amount >= Balancing.Current.ConsumerRequiredAmount && underground.Intensity >= Balancing.Current.ConsumerRequiredIntensity;
return supplied ? EConsumerServiceState.Producing : EConsumerServiceState.Starved;
}
private static EConsumerServiceState DisabledServiceStateFor(LevelState level, GridPosition position, ECarrierType carrier)
{
return level.GetUnderground(position, carrier).IsPresent ? EConsumerServiceState.Disabled : EConsumerServiceState.Unknown;
}
private static EConsumerServiceState Aggregate(params EConsumerServiceState[] states)
{
var participating = states.Where(state => state != EConsumerServiceState.Unknown).ToArray();
if (participating.Length == 0)
return EConsumerServiceState.Unknown;
if (participating.Any(state => state == EConsumerServiceState.Starved))
return EConsumerServiceState.Starved;
if (participating.Any(state => state == EConsumerServiceState.Disabled))
return EConsumerServiceState.Disabled;
return EConsumerServiceState.Producing;
}
}

View File

@@ -0,0 +1,68 @@
namespace ReactorMaintenance.Simulation;
internal static class ForecastSystem
{
public static IReadOnlyList<Forecast> Forecast(LevelState level, Func<LevelState, LevelState> resolveTurn)
{
var forecasts = new List<Forecast>();
var simulated = CopyForForecast(level);
for (var turn = 0; turn <= Balancing.Current.ForecastHorizon; turn++)
{
AddForecasts(forecasts, simulated, turn);
if (simulated.Global.LevelState is ELevelState.Lost or ELevelState.Ready or ELevelState.Won)
break;
if (turn < Balancing.Current.ForecastHorizon)
simulated = resolveTurn(simulated);
}
return forecasts.DistinctBy(forecast => (forecast.Kind, forecast.Position, forecast.Message)).OrderBy(forecast => forecast.Turns).ThenBy(forecast => forecast.Message).ToArray();
}
private static LevelState CopyForForecast(LevelState level)
{
return level with {
Terrain = level.Terrain.ToArray(),
Fuel = level.Fuel.ToArray(),
Coolant = level.Coolant.ToArray(),
Electricity = level.Electricity.ToArray(),
Surface = level.Surface.ToArray(),
Props = level.Props.ToArray(),
Forecasts = Array.Empty<Forecast>()
};
}
private static void AddForecasts(List<Forecast> forecasts, LevelState level, int turn)
{
if (level.Global.LevelState == ELevelState.Lost)
forecasts.Add(new(EForecastKind.TerminalLoss, level.Robot.Position, turn, level.Global.Status));
if (level.Global.LevelState == ELevelState.Ready)
forecasts.Add(new(EForecastKind.ReactorReady, null, turn, "REACTOR READY"));
foreach (var position in LevelTraversal.AllPositions(level))
{
var prop = level.GetProp(position);
if (prop.Type == EPropType.Consumer)
{
foreach (var carrier in Enum.GetValues<ECarrierType>())
{
if (prop.ServiceStateFor(carrier) == EConsumerServiceState.Starved)
forecasts.Add(new(EForecastKind.ConsumerStarved, position, turn, $"{carrier} consumer starved"));
}
}
foreach (var carrier in Enum.GetValues<ECarrierType>())
{
var underground = level.GetUnderground(position, carrier);
if (underground.IsPresent && underground.StructuralIntegrity <= Balancing.Current.StructuralIntegrityLeakThreshold)
forecasts.Add(new(EForecastKind.StructuralIntegrity, position, turn, $"{carrier} structural integrity failing"));
}
var surface = level.GetSurface(position);
if (SimulationBands.Fuel(surface.Fuel) == EBand.Critical || SimulationBands.Coolant(surface.Coolant) == EBand.Critical || SimulationBands.Electricity(surface.Electricity) == EBand.Critical || SimulationBands.Heat(surface.Heat) == EBand.Critical)
forecasts.Add(new(EForecastKind.HazardGrowth, position, turn, "Critical hazard"));
}
}
}

View File

@@ -0,0 +1,24 @@
namespace ReactorMaintenance.Simulation;
internal static class LeakSystem
{
public static LevelState Inject(LevelState level)
{
var surface = level.Surface.ToArray();
foreach (var leak in level.Leaks.Where(leak => !leak.Repaired))
{
var underground = level.GetUnderground(leak.UndergroundPosition, leak.Carrier);
if (underground.State != EUndergroundState.Leaking)
continue;
var accessIndex = level.Index(leak.AccessPosition);
if (surface[accessIndex].Blocks(leak.Carrier))
continue;
var amount = Balancing.Current.LeakBaseAmount + (underground.Amount * Balancing.Current.LeakAmountScale) + (underground.Intensity * Balancing.Current.LeakIntensityScale);
surface[accessIndex] = SurfaceCarrierMath.AddCarrier(surface[accessIndex], leak.Carrier, amount);
}
return level with { Surface = surface.Select(cell => cell.Clamp()).ToArray() };
}
}

View File

@@ -0,0 +1,86 @@
namespace ReactorMaintenance.Simulation;
internal static class NetworkPropagationSystem
{
public static LevelState Propagate(LevelState level)
{
var fuel = ClearTransient(level.Fuel);
var coolant = ClearTransient(level.Coolant);
var electricity = ClearTransient(level.Electricity);
var next = level.WithRuntimeArrays(fuel, coolant, electricity, level.Surface.ToArray(), level.Props.ToArray());
foreach (var carrier in Enum.GetValues<ECarrierType>())
next = PropagateCarrier(next, carrier);
return next;
}
private static UndergroundCell[] ClearTransient(IReadOnlyList<UndergroundCell> layer)
{
return layer.Select(cell => cell with { Amount = 0, Intensity = 0 }).ToArray();
}
private static LevelState PropagateCarrier(LevelState level, ECarrierType carrier)
{
var layer = level.Layer(carrier).ToArray();
var sources = LevelTraversal.AllPositions(level).Where(position => level.GetProp(position) is { Type: EPropType.Flow, SwitchState: EPropSwitchState.Enabled, Carrier: var sourceCarrier } && sourceCarrier == carrier && level.GetUnderground(position, carrier).CarriesFlow).ToArray();
var junctions = JunctionFlowAnalyzer.Analyze(level).Where(junction => junction.IsValid && junction.Carrier == carrier).ToDictionary(junction => junction.Position);
foreach (var source in sources)
ApplySourceFlow(level, layer, source, carrier, junctions);
return carrier switch {
ECarrierType.Fuel => level with { Fuel = layer },
ECarrierType.Coolant => level with { Coolant = layer },
ECarrierType.Electricity => level with { Electricity = layer },
_ => throw new ArgumentOutOfRangeException(nameof(carrier), carrier, "Unsupported carrier.")
};
}
private static void ApplySourceFlow(LevelState level, UndergroundCell[] layer, GridPosition source, ECarrierType carrier, IReadOnlyDictionary<GridPosition, JunctionFlow> junctions)
{
var open = new Queue<(GridPosition Position, int Distance, float AmountFactor, float IntensityFactor)>();
var best = new Dictionary<GridPosition, float>();
open.Enqueue((source, 0, 1, 1));
best[source] = 1;
while (open.Count > 0)
{
var current = open.Dequeue();
var amount = Balancing.Current.ClampValue((Balancing.Current.SourceAmount * current.AmountFactor) - (current.Distance * Balancing.Current.DistanceAmountFalloff));
var intensity = Balancing.Current.ClampValue((Balancing.Current.SourceIntensity * current.IntensityFactor) - (current.Distance * Balancing.Current.DistanceIntensityFalloff));
var index = level.Index(current.Position);
layer[index] = layer[index] with {
Amount = Math.Max(layer[index].Amount, amount),
Intensity = Math.Max(layer[index].Intensity, intensity)
};
foreach (var next in current.Position.Neighbors().Where(level.InBounds))
{
if (!level.GetUnderground(next, carrier).CarriesFlow)
continue;
var weights = BranchWeights(current.Position, next, junctions);
var amountFactor = current.AmountFactor * weights.Amount;
var intensityFactor = current.IntensityFactor * weights.Intensity;
if (amountFactor <= 0 || intensityFactor <= 0)
continue;
if (best.TryGetValue(next, out var oldBest) && oldBest >= amountFactor)
continue;
best[next] = amountFactor;
open.Enqueue((next, current.Distance + 1, amountFactor, intensityFactor));
}
}
}
private static (float Amount, float Intensity) BranchWeights(GridPosition from, GridPosition to, IReadOnlyDictionary<GridPosition, JunctionFlow> junctions)
{
if (!junctions.TryGetValue(from, out var junction))
return (1, 1);
var weight = junction.WeightFor(to);
return (weight, weight);
}
}

View File

@@ -0,0 +1,133 @@
namespace ReactorMaintenance.Simulation;
internal static class PlayerActionSystem
{
public static LevelState MoveRobot(LevelState level, GridPosition destination)
{
if (!CanAct(level) || !level.IsFloor(destination) || level.Robot.Position.ManhattanDistance(destination) != 1)
return Refuse(level, "MOVE BLOCKED");
return level with {
Robot = level.Robot with {
Position = destination,
HeatImmunitySteps = Math.Max(0, level.Robot.HeatImmunitySteps - 1)
}
};
}
public static LevelState InteractProp(LevelState level, Func<LevelState, LevelState> resolveLengthyAction)
{
if (!CanAct(level))
return Refuse(level, "NO CONTROL");
var position = level.Robot.Position;
var prop = level.GetProp(position);
if (prop.Type == EPropType.None)
return Refuse(level, "NO PROP");
var next = prop.Type switch {
EPropType.Flow or EPropType.Consumer => ToggleProp(level, position, prop),
EPropType.Junction => CycleJunctionMode(level, position, prop),
EPropType.Door => ToggleDoor(level, position, prop),
EPropType.AllSeeingEyeTerminal => level with { Global = level.Global with { Status = "ALL-SEEING-EYE AVAILABLE" } },
EPropType.RemedySupply => PickUpRemedy(level, position, prop),
EPropType.ReactorControl => ReactorSystem.Activate(level),
_ => level
};
return prop.Type == EPropType.AllSeeingEyeTerminal || prop.Type == EPropType.ReactorControl ? next : resolveLengthyAction(next);
}
public static LevelState InteractLeak(LevelState level, ECarrierType carrier, bool useRemedy, Func<LevelState, LevelState> resolveLengthyAction)
{
if (!CanAct(level))
return Refuse(level, "NO CONTROL");
var leakIndex = level.Leaks.ToList().FindIndex(leak => !leak.Repaired && leak.Carrier == carrier && leak.AccessPosition == level.Robot.Position);
if (leakIndex < 0)
return Refuse(level, "NO REACHABLE LEAK");
var leak = level.Leaks[leakIndex];
var next = useRemedy ? ApplyElementRemedy(level, leak) : RepairLeak(level, leakIndex, leak);
return resolveLengthyAction(next);
}
public static LevelState ApplyHeatShield(LevelState level, Func<LevelState, LevelState> resolveLengthyAction)
{
if (!CanAct(level) || level.Robot.HeatShields <= 0)
return Refuse(level, "NO HEAT SHIELD");
return resolveLengthyAction(level with {
Robot = level.Robot.Spend(ERemedyType.HeatShield) with { HeatImmunitySteps = Balancing.Current.HeatShieldSteps }
});
}
private static LevelState ToggleProp(LevelState level, GridPosition position, PropState prop)
{
var switchState = prop.SwitchState == EPropSwitchState.Enabled ? EPropSwitchState.Disabled : EPropSwitchState.Enabled;
return level.SetProp(position, prop with { SwitchState = switchState });
}
private static LevelState ToggleDoor(LevelState level, GridPosition position, PropState prop)
{
var doorState = prop.DoorState == EDoorState.Open ? EDoorState.Closed : EDoorState.Open;
return level.SetProp(position, prop with { DoorState = doorState });
}
private static LevelState PickUpRemedy(LevelState level, GridPosition position, PropState prop)
{
if (prop.Depleted || level.Robot.Count(prop.RemedyType) >= Balancing.Current.InventoryCapacityPerRemedy)
return level;
return level.SetProp(position, prop with { Depleted = true }) with { Robot = level.Robot.Add(prop.RemedyType, 1) };
}
private static LevelState RepairLeak(LevelState level, int leakIndex, LeakState leak)
{
var leaks = level.Leaks.ToArray();
leaks[leakIndex] = leak with { Repaired = true };
return level.SetUnderground(leak.UndergroundPosition, leak.Carrier, level.GetUnderground(leak.UndergroundPosition, leak.Carrier) with {
State = EUndergroundState.Intact,
StructuralIntegrity = Balancing.Current.MaxStructuralIntegrity
}) with {
Leaks = leaks
};
}
private static LevelState ApplyElementRemedy(LevelState level, LeakState leak)
{
var remedy = leak.Carrier switch {
ECarrierType.Fuel => ERemedyType.FuelNeutralizer,
ECarrierType.Coolant => ERemedyType.CoolantNeutralizer,
ECarrierType.Electricity => ERemedyType.ElectricityNeutralizer,
_ => throw new ArgumentOutOfRangeException(nameof(leak), leak.Carrier, "Unsupported leak carrier.")
};
if (level.Robot.Count(remedy) <= 0)
return Refuse(level, "NO REMEDY");
var surface = SurfaceCarrierMath.RemoveCarrier(level.GetSurface(leak.AccessPosition), leak.Carrier);
return level.SetSurface(leak.AccessPosition, surface) with { Robot = level.Robot.Spend(remedy) };
}
private static LevelState CycleJunctionMode(LevelState level, GridPosition position, PropState prop)
{
var flow = JunctionFlowAnalyzer.Analyze(level).FirstOrDefault(junction => junction.Position == position);
var outflowCount = flow?.OutgoingBranches.Count ?? 2;
var ratios = Balancing.Current.JunctionRatios(outflowCount);
if (ratios.Count == 0)
return level;
return level.SetProp(position, prop with { JunctionMode = (prop.JunctionMode + 1) % ratios.Count });
}
private static bool CanAct(LevelState level)
{
return level.Global.LevelState is not (ELevelState.Lost or ELevelState.Won);
}
private static LevelState Refuse(LevelState level, string message)
{
return level with { Global = level.Global with { Status = message } };
}
}

View File

@@ -0,0 +1,91 @@
namespace ReactorMaintenance.Simulation;
internal static class ReactorSystem
{
public static LevelState Activate(LevelState level)
{
var reactorIndex = level.Reactors.ToList().FindIndex(reactor => reactor.ControlPosition == level.Robot.Position);
if (reactorIndex < 0)
return Refuse(level, "NO REACTOR CONTROL");
var reactor = level.Reactors[reactorIndex];
if (!reactor.Ready)
return Refuse(level, "REACTOR NOT READY");
var reactors = level.Reactors.ToArray();
reactors[reactorIndex] = reactor with { Activated = true };
return level with {
Reactors = reactors,
Global = level.Global with { LevelState = ELevelState.Won, Status = "REACTOR ONLINE" }
};
}
public static LevelState DeriveState(LevelState level)
{
if (level.Global.LevelState is ELevelState.Lost or ELevelState.Won)
return level;
var reactors = level.Reactors.Select(reactor => reactor with { Ready = IsReady(level, reactor) }).ToArray();
if (reactors.Any(reactor => reactor.Ready))
return level with { Reactors = reactors, Global = level.Global with { LevelState = ELevelState.Ready, Status = "REACTOR READY" } };
var maxHeat = level.Surface.Where((_, index) => level.Terrain[index] == ECellTerrain.Floor).Select(surface => surface.Heat).DefaultIfEmpty(0).Max();
if (maxHeat >= Balancing.Current.TerminalHeat)
return level with { Reactors = reactors, Global = level.Global with { LevelState = ELevelState.Lost, TerminalLoss = true, Status = "REACTOR HEAT TERMINAL" } };
var hasCritical = level.Surface.Any(surface => SimulationBands.Fuel(surface.Fuel) == EBand.Critical || SimulationBands.Coolant(surface.Coolant) == EBand.Critical || SimulationBands.Electricity(surface.Electricity) == EBand.Critical || SimulationBands.Heat(surface.Heat) == EBand.Critical);
var hasCaution = hasCritical
|| !HasRequiredConsumers(level)
|| level.Props.Any(prop => prop.Type == EPropType.Consumer && HasConsumerTrouble(prop))
|| level.Leaks.Any(leak => !leak.Repaired);
var state = hasCritical ? ELevelState.Critical :
hasCaution ? ELevelState.Caution : ELevelState.Stable;
return level with { Reactors = reactors, Global = level.Global with { LevelState = state, Status = state.ToString().ToUpperInvariant() } };
}
private static bool IsReady(LevelState level, ReactorState reactor)
{
return ReactorFeedsPresentAndProducing(level, reactor.ControlPosition)
&& ProducingConsumerCount(level, ECarrierType.Fuel) >= level.RequiredFuelConsumers
&& ProducingConsumerCount(level, ECarrierType.Coolant) >= level.RequiredCoolantConsumers
&& ProducingConsumerCount(level, ECarrierType.Electricity) >= level.RequiredElectricityConsumers
&& level.GetSurface(reactor.ControlPosition).Heat < Balancing.Current.TerminalHeat;
}
private static bool ReactorFeedsPresentAndProducing(LevelState level, GridPosition position)
{
foreach (var carrier in Enum.GetValues<ECarrierType>())
{
var underground = level.GetUnderground(position, carrier);
if (underground.IsPresent && (underground.Amount <= 0 || underground.Intensity <= 0))
return false;
}
return true;
}
private static int ProducingConsumerCount(LevelState level, ECarrierType carrier)
{
return LevelTraversal.AllPositions(level)
.Count(position => level.GetProp(position) is { Type: EPropType.Consumer } prop && prop.ServiceStateFor(carrier) == EConsumerServiceState.Producing);
}
private static bool HasRequiredConsumers(LevelState level)
{
return ProducingConsumerCount(level, ECarrierType.Fuel) >= level.RequiredFuelConsumers
&& ProducingConsumerCount(level, ECarrierType.Coolant) >= level.RequiredCoolantConsumers
&& ProducingConsumerCount(level, ECarrierType.Electricity) >= level.RequiredElectricityConsumers;
}
private static bool HasConsumerTrouble(PropState prop)
{
return prop.FuelServiceState is EConsumerServiceState.Starved or EConsumerServiceState.Disabled
|| prop.CoolantServiceState is EConsumerServiceState.Starved or EConsumerServiceState.Disabled
|| prop.ElectricityServiceState is EConsumerServiceState.Starved or EConsumerServiceState.Disabled;
}
private static LevelState Refuse(LevelState level, string message)
{
return level with { Global = level.Global with { Status = message } };
}
}

View File

@@ -0,0 +1,14 @@
namespace ReactorMaintenance.Simulation;
internal static class RobotSafetySystem
{
public static LevelState Resolve(LevelState level)
{
var surface = level.GetSurface(level.Robot.Position);
var unsafeElement = surface.Fuel >= Balancing.Current.RobotFuelSafetyThreshold || surface.Coolant >= Balancing.Current.RobotCoolantSafetyThreshold || surface.Electricity >= Balancing.Current.RobotElectricitySafetyThreshold;
var unsafeHeat = surface.Heat >= Balancing.Current.RobotHeatSafetyThreshold && level.Robot.HeatImmunitySteps <= 0;
return unsafeElement || unsafeHeat
? level with { Global = level.Global with { LevelState = ELevelState.Lost, TerminalLoss = true, Status = "ROBOT LOST" } }
: level;
}
}

View File

@@ -0,0 +1,82 @@
namespace ReactorMaintenance.Simulation;
internal static class StructuralIntegritySystem
{
public static LevelState Resolve(LevelState level)
{
foreach (var carrier in Enum.GetValues<ECarrierType>())
level = ResolveCarrier(level, carrier);
return level;
}
private static LevelState ResolveCarrier(LevelState level, ECarrierType carrier)
{
var layer = level.Layer(carrier).ToArray();
var leaks = level.Leaks.ToList();
foreach (var position in LevelTraversal.AllPositions(level))
{
var index = level.Index(position);
var cell = layer[index];
if (!cell.IsPresent)
continue;
var integrity = DegradeIntegrity(cell, carrier);
var state = cell.State;
if (state != EUndergroundState.Leaking && integrity <= Balancing.Current.StructuralIntegrityLeakThreshold && cell.Intensity > 0)
{
state = EUndergroundState.Leaking;
leaks = UpsertLeak(leaks, level, position, carrier);
}
layer[index] = cell with { State = state, StructuralIntegrity = integrity };
}
var next = carrier switch {
ECarrierType.Fuel => level with { Fuel = layer },
ECarrierType.Coolant => level with { Coolant = layer },
ECarrierType.Electricity => level with { Electricity = layer },
_ => throw new ArgumentOutOfRangeException(nameof(carrier), carrier, "Unsupported carrier.")
};
return next with { Leaks = leaks.ToArray() };
}
private static int DegradeIntegrity(UndergroundCell cell, ECarrierType carrier)
{
if (cell.StructuralIntegrity >= Balancing.Current.MaxStructuralIntegrity && cell.Intensity <= Balancing.Current.StructuralPressureThreshold(carrier))
return Balancing.Current.MaxStructuralIntegrity;
var overPressure = Math.Max(0, cell.Intensity - Balancing.Current.StructuralPressureThreshold(carrier));
if (overPressure <= 0 || cell.StructuralIntegrity >= Balancing.Current.MaxStructuralIntegrity)
return Math.Clamp(cell.StructuralIntegrity, 0, Balancing.Current.MaxStructuralIntegrity);
var damage = Math.Max(1, (int)Math.Ceiling(overPressure * Balancing.Current.StructuralIntegrityDamageScale));
return Math.Clamp(cell.StructuralIntegrity - damage, 0, Balancing.Current.MaxStructuralIntegrity);
}
private static List<LeakState> UpsertLeak(List<LeakState> leaks, LevelState level, GridPosition position, ECarrierType carrier)
{
var accessPosition = carrier == ECarrierType.Electricity
? position.Neighbors().FirstOrDefault(level.IsFloor)
: position;
if (accessPosition is null || !level.IsFloor(accessPosition))
return leaks;
var index = leaks.FindIndex(leak => leak.UndergroundPosition == position && leak.Carrier == carrier);
var leakState = new LeakState {
Carrier = carrier,
UndergroundPosition = position,
AccessPosition = accessPosition,
Repaired = false
};
if (index >= 0)
leaks[index] = leakState;
else
leaks.Add(leakState);
return leaks;
}
}

View File

@@ -0,0 +1,135 @@
namespace ReactorMaintenance.Simulation;
internal static class SurfaceInteractionSystem
{
private sealed class SurfaceDelta
{
public SurfaceState Apply(SurfaceState surface)
{
return surface with {
Fuel = surface.Fuel + Fuel,
Coolant = surface.Coolant + Coolant,
Electricity = surface.Electricity + Electricity,
Heat = surface.Heat + Heat
};
}
public float Fuel { get; set; }
public float Coolant { get; set; }
public float Electricity { get; set; }
public float Heat { get; set; }
}
public static LevelState Resolve(LevelState level)
{
var deltas = Enumerable.Range(0, level.Width * level.Height).Select(_ => new SurfaceDelta()).ToArray();
foreach (var position in LevelTraversal.AllPositions(level).Where(level.IsFloor))
ApplySameCellInteractions(level, position, deltas);
foreach (var position in LevelTraversal.AllPositions(level).Where(level.IsFloor))
{
foreach (var neighbor in position.Neighbors().Where(level.IsFloor))
{
if (level.Index(position) >= level.Index(neighbor) || level.IsClosedDoorEdge(position, neighbor))
continue;
ApplyAdjacentInteractions(level, position, neighbor, deltas);
}
}
var surface = level.Surface.ToArray();
for (var i = 0; i < surface.Length; i++)
surface[i] = deltas[i].Apply(surface[i]).Clamp();
return level with { Surface = surface };
}
public static LevelState AdvanceDurations(LevelState level)
{
var surface = level.Surface.Select(cell => cell with {
FuelBlockTurns = Math.Max(0, cell.FuelBlockTurns - 1),
CoolantBlockTurns = Math.Max(0, cell.CoolantBlockTurns - 1),
ElectricityBlockTurns = Math.Max(0, cell.ElectricityBlockTurns - 1)
})
.ToArray();
return level with { Surface = surface };
}
private static void ApplySameCellInteractions(LevelState level, GridPosition position, SurfaceDelta[] deltas)
{
var surface = level.GetSurface(position);
ApplyPair(level, position, ECarrierType.Fuel, SimulationBands.Fuel(surface.Fuel), ECarrierType.Electricity, SimulationBands.Electricity(surface.Electricity), deltas);
ApplyPair(level, position, ECarrierType.Fuel, SimulationBands.Fuel(surface.Fuel), null, SimulationBands.Heat(surface.Heat), deltas);
ApplyPair(level, position, ECarrierType.Coolant, SimulationBands.Coolant(surface.Coolant), ECarrierType.Electricity, SimulationBands.Electricity(surface.Electricity), deltas);
ApplyPair(level, position, ECarrierType.Coolant, SimulationBands.Coolant(surface.Coolant), null, SimulationBands.Heat(surface.Heat), deltas);
}
private static void ApplyAdjacentInteractions(LevelState level, GridPosition a, GridPosition b, SurfaceDelta[] deltas)
{
var surfaceA = level.GetSurface(a);
var surfaceB = level.GetSurface(b);
FlowBetween(level, a, b, surfaceA.Fuel, surfaceB.Fuel, Balancing.Current.FlowInteraction(ESurfaceQuantity.Fuel), deltas);
FlowBetween(level, a, b, surfaceA.Coolant, surfaceB.Coolant, Balancing.Current.FlowInteraction(ESurfaceQuantity.Coolant), deltas);
FlowBetween(level, a, b, surfaceA.Electricity, surfaceB.Electricity, Balancing.Current.FlowInteraction(ESurfaceQuantity.Electricity), deltas);
FlowBetween(level, a, b, surfaceA.Heat, surfaceB.Heat, Balancing.Current.FlowInteraction(ESurfaceQuantity.Heat), deltas);
}
private static void ApplyPair(LevelState level, GridPosition position, ECarrierType? rowCarrier, EBand rowBand, ECarrierType? colCarrier, EBand colBand, SurfaceDelta[] deltas)
{
ApplyEffect(level, position, Balancing.Current.SameCellInteraction(rowCarrier, rowBand, colCarrier, colBand), deltas);
}
private static void ApplyEffect(LevelState level, GridPosition position, SurfaceInteractionEffect effect, SurfaceDelta[] deltas)
{
var index = level.Index(position);
switch (effect.Verb)
{
case ESurfaceInteractionVerb.Warm:
deltas[index].Heat += effect.Amount;
break;
case ESurfaceInteractionVerb.Quench:
deltas[index].Heat -= effect.Amount;
break;
case ESurfaceInteractionVerb.Short:
deltas[index].Heat += effect.Amount;
deltas[index].Electricity -= effect.SecondaryAmount;
break;
case ESurfaceInteractionVerb.Ignite:
deltas[index].Heat += effect.Amount;
deltas[index].Fuel -= effect.SecondaryAmount;
break;
}
}
private static void FlowBetween(LevelState level, GridPosition a, GridPosition b, float valueA, float valueB, SurfaceInteractionEffect effect, SurfaceDelta[] deltas)
{
var difference = valueA - valueB;
if (Math.Abs(difference) < 0.01f)
return;
var amount = difference * effect.Amount;
var indexA = level.Index(a);
var indexB = level.Index(b);
switch (effect.Quantity)
{
case ESurfaceQuantity.Fuel:
deltas[indexA].Fuel -= amount;
deltas[indexB].Fuel += amount;
break;
case ESurfaceQuantity.Coolant:
deltas[indexA].Coolant -= amount;
deltas[indexB].Coolant += amount;
break;
case ESurfaceQuantity.Electricity:
deltas[indexA].Electricity -= amount;
deltas[indexB].Electricity += amount;
break;
case ESurfaceQuantity.Heat:
deltas[indexA].Heat -= amount;
deltas[indexB].Heat += amount;
break;
}
}
}

View File

@@ -0,0 +1,64 @@
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.UI.Xaml;
namespace ReactorMaintenance.Win2D;
public sealed class EditorImageRegistry
{
public async Task LoadAsync(CanvasControl sender)
{
m_Images.Clear();
await LoadFolderAsync(sender, "Images", "Props");
await LoadFolderAsync(sender, "Images", "Pipes");
await LoadFolderAsync(sender, "Images", "Badges");
await LoadFolderAsync(sender, "Images", "Elements");
AddAlias("tool-floor", "floor");
AddAlias("tool-wall", "wall");
AddAlias("tool-heat", "heat");
AddAlias("tool-robot", "robot");
AddAlias("robot", "robot");
AddAlias("prop-reactor", "reactor");
AddAlias("prop-consumer", "generator");
AddAlias("prop-flow", "generator");
AddAlias("carrier-fuel-source", "generator");
AddAlias("carrier-coolant-source", "cooling-pump");
AddAlias("carrier-electricity-source", "generator");
AddAlias("prop-junction", "pressure-regulator");
AddAlias("prop-door", "wall");
AddAlias("prop-eye-terminal", "diagnostic-terminal");
AddAlias("prop-remedy", "repair");
AddAlias("leak-fuel", "leak");
AddAlias("leak-coolant", "leak");
AddAlias("leak-electricity", "leak");
AddAlias("hazard-heat", "heat");
AddAlias("hazard-fuel", "fire");
AddAlias("hazard-coolant", "leak");
AddAlias("hazard-electricity", "heat");
}
public CanvasBitmap? Get(string key)
{
return m_Images.GetValueOrDefault(key);
}
private async Task LoadFolderAsync(CanvasControl sender, params string[] pathParts)
{
var folder = Path.Combine([AppContext.BaseDirectory, .. pathParts]);
if (!Directory.Exists(folder))
return;
foreach (var path in Directory.EnumerateFiles(folder, "*.png"))
{
var key = Path.GetFileNameWithoutExtension(path);
m_Images[key] = await CanvasBitmap.LoadAsync(sender, path);
}
}
private void AddAlias(string alias, string key)
{
if (!m_Images.ContainsKey(alias) && m_Images.TryGetValue(key, out var image))
m_Images[alias] = image;
}
private readonly Dictionary<string, CanvasBitmap> m_Images = new(StringComparer.OrdinalIgnoreCase);
}

View File

@@ -15,10 +15,11 @@
<AppBarButton Icon="OpenFile" Label="Open" Click="Open_Click" />
<AppBarButton Icon="Save" Label="Save" Click="Save_Click" />
<AppBarSeparator />
<AppBarButton Icon="Play" Label="End Turn" Click="EndTurn_Click" />
<AppBarButton Label="Interact" Click="Interact_Click" />
<AppBarButton Label="Heat Shield" Click="HeatShield_Click" />
<AppBarButton Icon="Accept" Label="Activate" Click="Activate_Click" />
<AppBarButton x:Name="PlayPauseButton" Icon="Play" Label="Play" Click="PlayPause_Click" />
<AppBarButton Icon="Forward" Label="End Turn" Click="EndTurn_Click" />
<AppBarButton Label="Interact" Click="Interact_Click" />
<AppBarButton Label="Heat Shield" Click="HeatShield_Click" />
<AppBarButton Icon="Accept" Label="Activate" Click="Activate_Click" />
</CommandBar>
<Grid Grid.Row="1" ColumnSpacing="0">
@@ -30,6 +31,10 @@
<ScrollViewer Grid.Column="0" Background="#1C2126">
<StackPanel Padding="12" Spacing="10">
<TextBlock Text="Layer" FontSize="18" FontWeight="SemiBold" Foreground="#F4F1E8" />
<ComboBox x:Name="LayerPicker"
SelectionChanged="LayerPicker_SelectionChanged"
HorizontalAlignment="Stretch" />
<TextBlock Text="Tools" FontSize="18" FontWeight="SemiBold" Foreground="#F4F1E8" />
<ItemsControl x:Name="ToolPicker">
<ItemsControl.ItemsPanel>
@@ -39,22 +44,23 @@
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ToggleButton IsChecked="{Binding IsSelected, Mode=TwoWay}"
Checked="ToolToggle_Checked" ToolTipService.ToolTip="{Binding Label}"
Width="112" MinHeight="46" Padding="6" Margin="0,0,8,8">
<TextBlock Text="{Binding Label}" TextWrapping="WrapWholeWords" TextAlignment="Center"
FontSize="12" />
</ToggleButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock Text="Left click selects or paints. Right click clears surface prop and hazards."
Foreground="#9EA7AE" TextWrapping="Wrap" />
<TextBlock Text="Door chooses the first adjacent floor edge. Reactor controls auto-bind to the first available consumers."
Foreground="#9EA7AE"
TextWrapping="Wrap" />
</StackPanel>
</ScrollViewer>
<ToggleButton IsChecked="{Binding IsSelected, Mode=TwoWay}"
Checked="ToolToggle_Checked" ToolTipService.ToolTip="{Binding Label}"
Width="112" MinHeight="46" Padding="6" Margin="0,0,8,8">
<TextBlock Text="{Binding Label}" TextWrapping="WrapWholeWords"
TextAlignment="Center"
FontSize="12" />
</ToggleButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock Text="Left click selects or paints. Right click clears surface prop and hazards."
Foreground="#9EA7AE" TextWrapping="Wrap" />
<TextBlock Text="Shift+left drag pans. Cursor drag moves the robot or a prop."
Foreground="#9EA7AE"
TextWrapping="Wrap" />
</StackPanel>
</ScrollViewer>
<Grid Grid.Column="1" Background="#101215">
<canvas:CanvasControl
@@ -88,11 +94,193 @@
</StackPanel>
</Grid>
<TextBlock Text="Global Systems" FontSize="16" FontWeight="SemiBold" Foreground="#F4F1E8" />
<TextBlock x:Name="GlobalText" Foreground="#CCD4DA" TextWrapping="Wrap" />
<TextBlock Text="Inventory" FontSize="16" FontWeight="SemiBold" Foreground="#F4F1E8" />
<ItemsControl x:Name="InventoryGrid">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid MinWidth="55" Margin="0,0,4,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Label}" Foreground="#9EA7AE" FontSize="11" />
<TextBlock Grid.Row="1" Text="{Binding Value}" Foreground="#F4F1E8" FontSize="18" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock Text="Selected Cell" FontSize="16" FontWeight="SemiBold" Foreground="#F4F1E8" />
<TextBlock x:Name="CellText" Foreground="#CCD4DA" TextWrapping="Wrap" />
<TextBlock Text="Required" FontSize="16" FontWeight="SemiBold" Foreground="#F4F1E8" />
<ItemsControl x:Name="RequiredGrid">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Width="84" Margin="0,0,4,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Label}" Foreground="#9EA7AE" FontSize="11" />
<TextBlock Grid.Row="1" Text="{Binding Value}" Foreground="#F4F1E8"
FontSize="18" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock x:Name="HoveredCellText" Text="Hovered Cell:" FontSize="16" FontWeight="SemiBold"
Foreground="#F4F1E8" />
<TextBlock x:Name="SelectedCellText" Text="Selected Cell:" FontSize="16" FontWeight="SemiBold"
Foreground="#F4F1E8" />
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Text="Terrain" Foreground="#9EA7AE" FontSize="11" />
<TextBlock Grid.Column="0" Grid.Row="1" x:Name="TerrainText" Foreground="#F4F1E8" FontSize="16"
Margin="0,0,10,0" />
<TextBlock Grid.Column="1" Grid.Row="0" Text="Prop" Foreground="#9EA7AE" FontSize="11" />
<TextBlock Grid.Column="1" Grid.Row="1" x:Name="PropText" Foreground="#F4F1E8" FontSize="16" />
</Grid>
<TextBlock Text="Consumers:" FontSize="16" FontWeight="SemiBold" Foreground="#F4F1E8" />
<ItemsControl x:Name="ConsumersGrid">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Width="96" Margin="0,0,4,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Label}" Foreground="#9EA7AE" FontSize="11"
TextWrapping="NoWrap" />
<TextBlock Grid.Row="1" Text="{Binding Value}" Foreground="#F4F1E8" FontSize="16" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock Text="Services:" FontSize="16" FontWeight="SemiBold" Foreground="#F4F1E8" />
<ItemsControl x:Name="ServicesGrid">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Width="96" Margin="0,0,4,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Label}" Foreground="#9EA7AE" FontSize="11"
TextWrapping="NoWrap" />
<TextBlock Grid.Row="1" Text="{Binding Value}" Foreground="#F4F1E8" FontSize="16" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock Text="Leaks:" FontSize="16" FontWeight="SemiBold" Foreground="#F4F1E8" />
<ItemsControl x:Name="LeaksGrid">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="0,0,4,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Label}" Foreground="#F4F1E8" FontSize="16"
Margin="0,0,10,0" />
<TextBlock Grid.Column="1" Text="{Binding Value}" Foreground="#F4F1E8"
FontSize="16" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock Text="Surface" FontSize="16" FontWeight="SemiBold" Foreground="#F4F1E8" />
<ItemsControl x:Name="SurfaceGrid">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Width="64" Margin="0,0,4,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Label}" Foreground="#9EA7AE" FontSize="11"
TextWrapping="NoWrap" />
<TextBlock Grid.Row="1" Text="{Binding Value}" Foreground="#F4F1E8" FontSize="16" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock Text="Network" FontSize="16" FontWeight="SemiBold" Foreground="#F4F1E8" />
<Grid ColumnSpacing="6">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="64" />
<ColumnDefinition Width="60" />
<ColumnDefinition Width="34" />
<ColumnDefinition Width="34" />
<ColumnDefinition Width="34" />
</Grid.ColumnDefinitions>
<TextBlock Text="Carrier" Foreground="#9EA7AE" FontSize="11" />
<TextBlock Grid.Column="1" Text="State" Foreground="#9EA7AE" FontSize="11" />
<TextBlock Grid.Column="2" Text="Amt" Foreground="#9EA7AE" FontSize="11" />
<TextBlock Grid.Column="3" Text="Int" Foreground="#9EA7AE" FontSize="11" />
<TextBlock Grid.Column="4" Text="HP" Foreground="#9EA7AE" FontSize="11" />
</Grid>
<ItemsControl x:Name="NetworkGrid">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="0,0,0,8" ColumnSpacing="6">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="64" />
<ColumnDefinition Width="60" />
<ColumnDefinition Width="34" />
<ColumnDefinition Width="34" />
<ColumnDefinition Width="34" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Carrier}" Foreground="#F4F1E8" FontSize="12"
TextWrapping="Wrap" />
<TextBlock Grid.Column="1" Text="{Binding State}" Foreground="#9EA7AE"
FontSize="12" TextWrapping="Wrap" />
<TextBlock Grid.Column="2" Text="{Binding Amount}" Foreground="#F4F1E8"
FontSize="12" />
<TextBlock Grid.Column="3" Text="{Binding Intensity}" Foreground="#F4F1E8"
FontSize="12" />
<TextBlock Grid.Column="4" Text="{Binding Integrity}" Foreground="#F4F1E8"
FontSize="12" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock Text="Forecasts" FontSize="16" FontWeight="SemiBold" Foreground="#F4F1E8" />
<ItemsControl x:Name="ForecastList">
@@ -100,13 +288,13 @@
<DataTemplate>
<Border BorderBrush="#46515A" BorderThickness="1" Padding="8" Margin="0,0,0,8"
CornerRadius="3">
<TextBlock Text="{Binding Message}" Foreground="#F4F1E8" TextWrapping="Wrap" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<TextBlock Text="{Binding Message}" Foreground="#F4F1E8" TextWrapping="Wrap" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
</Grid>
</Grid>
</Window>
</Window>

File diff suppressed because it is too large Load Diff

View File

@@ -17,13 +17,13 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Graphics.Win2D" Version="1.4.0" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.28000.1839" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.8.260317003" />
<PackageReference Include="Microsoft.Graphics.Win2D" Version="1.4.0"/>
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.28000.1839"/>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.8.260317003"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ReactorMaintenance.Simulation\ReactorMaintenance.Simulation.csproj" />
<ProjectReference Include="..\ReactorMaintenance.Simulation\ReactorMaintenance.Simulation.csproj"/>
</ItemGroup>
<ItemGroup>

View File

@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="ReactorMaintenance.Win2D.app"/>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
</windowsSettings>
</application>
<assemblyIdentity version="1.0.0.0" name="ReactorMaintenance.Win2D.app"/>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
</windowsSettings>
</application>
</assembly>

View File

@@ -0,0 +1,201 @@
namespace ReactorMaintenance.Simulation.Tests;
public sealed class LevelEditorTests
{
[Fact]
public void DoorToolPlacesSingleFloorDoorProp()
{
var level = LevelState.Create("Door editor", 6, 6);
var next = LevelEditor.Apply(level, new(2, 2), new() { Tool = EEditorTool.Door });
Assert.Equal(EPropType.Door, next.GetProp(new(2, 2)).Type);
Assert.Equal(EDoorState.Closed, next.GetProp(new(2, 2)).DoorState);
}
[Fact]
public void DoorToolTogglesExistingDoorState()
{
var level = LevelState.Create("Door editor", 6, 6);
level = LevelEditor.Apply(level, new(2, 2), new() { Tool = EEditorTool.Door });
var opened = LevelEditor.Apply(level, new(2, 2), new() { Tool = EEditorTool.Door });
var closed = LevelEditor.Apply(opened, new(2, 2), new() { Tool = EEditorTool.Door });
Assert.Equal(EDoorState.Open, opened.GetProp(new(2, 2)).DoorState);
Assert.Equal(EDoorState.Closed, closed.GetProp(new(2, 2)).DoorState);
}
[Fact]
public void WallToolPreservesUndergroundNetworks()
{
var level = LevelState.Create("Wall editor", 6, 6);
var position = new GridPosition(2, 2);
level = LevelEditor.Apply(level, position, new() { Tool = EEditorTool.Underground, Carrier = ECarrierType.Fuel });
level = LevelEditor.Apply(level, position, new() { Tool = EEditorTool.Underground, Carrier = ECarrierType.Coolant });
level = LevelEditor.Apply(level, position, new() { Tool = EEditorTool.Underground, Carrier = ECarrierType.Electricity });
var next = LevelEditor.Apply(level, position, new() { Tool = EEditorTool.Wall });
Assert.Equal(EUndergroundState.Intact, next.GetUnderground(position, ECarrierType.Fuel).State);
Assert.Equal(EUndergroundState.Intact, next.GetUnderground(position, ECarrierType.Coolant).State);
Assert.Equal(EUndergroundState.Intact, next.GetUnderground(position, ECarrierType.Electricity).State);
}
[Fact]
public void UndergroundToolCanPaintAdjacentCellsRepeatedly()
{
var level = LevelState.Create("Network editor", 6, 6);
var command = new EditorToolCommand { Tool = EEditorTool.Underground, Carrier = ECarrierType.Fuel };
level = LevelEditor.Apply(level, new(1, 1), command);
level = LevelEditor.Apply(level, new(2, 1), command);
level = LevelEditor.Apply(level, new(3, 1), command);
Assert.Equal(EUndergroundState.Intact, level.GetUnderground(new(1, 1), ECarrierType.Fuel).State);
Assert.Equal(EUndergroundState.Intact, level.GetUnderground(new(2, 1), ECarrierType.Fuel).State);
Assert.Equal(EUndergroundState.Intact, level.GetUnderground(new(3, 1), ECarrierType.Fuel).State);
}
[Fact]
public void ConsumerToolPlacesCarrierAgnosticConsumer()
{
var level = LevelState.Create("Consumer editor", 6, 6);
var next = LevelEditor.Apply(level, new(2, 2), new() { Tool = EEditorTool.Consumer, Carrier = ECarrierType.Fuel });
Assert.Equal(EPropType.Consumer, next.GetProp(new(2, 2)).Type);
Assert.Equal(EConsumerServiceState.Unknown, next.GetProp(new(2, 2)).FuelServiceState);
Assert.Equal(EConsumerServiceState.Unknown, next.GetProp(new(2, 2)).CoolantServiceState);
Assert.Equal(EConsumerServiceState.Unknown, next.GetProp(new(2, 2)).ElectricityServiceState);
}
[Fact]
public void ElectricityLeakUsesAuthoredWallAccessFace()
{
var level = LevelState.Create("Electricity leak editor", 6, 6);
level = level.SetTerrain(new(2, 2), ECellTerrain.Wall);
var next = LevelEditor.SetLeak(level, new(2, 2), new(2, 3), ECarrierType.Electricity);
var rejected = LevelEditor.SetLeak(next, new(2, 2), new(4, 4), ECarrierType.Electricity);
Assert.Single(next.Leaks);
Assert.Equal(new(2, 3), next.Leaks[0].AccessPosition);
Assert.Equal(EUndergroundState.Leaking, next.GetUnderground(new(2, 2), ECarrierType.Electricity).State);
Assert.Equal(next.Leaks, rejected.Leaks);
}
[Fact]
public void ElectricityLeakAccessCyclesAcrossAdjacentFloorFaces()
{
var level = LevelState.Create("Electricity leak editor", 6, 6);
level = level.SetTerrain(new(2, 2), ECellTerrain.Wall);
level = level.SetTerrain(new(2, 1), ECellTerrain.Wall);
level = LevelEditor.Apply(level, new(2, 2), new() { Tool = EEditorTool.Underground, Carrier = ECarrierType.Electricity });
var first = LevelEditor.CycleElectricityLeakAccess(level, new(2, 2));
var second = LevelEditor.CycleElectricityLeakAccess(first, new(2, 2));
Assert.Single(first.Leaks);
Assert.Equal(new(3, 2), first.Leaks[0].AccessPosition);
Assert.Single(second.Leaks);
Assert.Equal(new(2, 3), second.Leaks[0].AccessPosition);
}
[Fact]
public void ReactorControlToolCreatesUnboundReactorState()
{
var level = LevelState.Create("Reactor editor", 6, 6);
var next = LevelEditor.Apply(level, new(2, 2), new() { Tool = EEditorTool.ReactorControl });
Assert.Single(next.Reactors);
Assert.Equal(new(2, 2), next.Reactors[0].ControlPosition);
Assert.Equal(1, next.Reactors[0].ReactorId);
}
[Fact]
public void MoveOccupantMovesRobotToFloorDestination()
{
var level = LevelState.Create("Move editor", 6, 6) with { Robot = new() { Position = new(1, 1) } };
var next = LevelEditor.MoveOccupant(level, new(1, 1), new(3, 3));
Assert.Equal(new(3, 3), next.Robot.Position);
}
[Fact]
public void MoveOccupantMovesPropAndUpdatesReactorControlPosition()
{
var level = LevelState.Create("Move editor", 6, 6);
level = LevelEditor.Apply(level, new(1, 1), new() { Tool = EEditorTool.ReactorControl });
var next = LevelEditor.MoveOccupant(level, new(1, 1), new(3, 3));
Assert.Equal(EPropType.None, next.GetProp(new(1, 1)).Type);
Assert.Equal(EPropType.ReactorControl, next.GetProp(new(3, 3)).Type);
Assert.Equal(new(3, 3), next.Reactors[0].ControlPosition);
}
[Fact]
public void MoveOccupantMovesSourcesAsProps()
{
var level = LevelState.Create("Move editor", 6, 6);
level = LevelEditor.Apply(level, new(1, 1), new() { Tool = EEditorTool.Flow, Carrier = ECarrierType.Fuel });
var result = LevelEditor.TryMoveOccupant(level, new(1, 1), new(3, 3));
Assert.True(result.Success, result.Reason);
Assert.Equal(EPropType.None, result.Level.GetProp(new(1, 1)).Type);
Assert.Equal(EPropType.Flow, result.Level.GetProp(new(3, 3)).Type);
Assert.Equal(ECarrierType.Fuel, result.Level.GetProp(new(3, 3)).Carrier);
}
[Fact]
public void MoveOccupantMovesFuelLeakToFloorDestination()
{
var level = LevelState.Create("Move editor", 6, 6);
level = LevelEditor.SetLeak(level, new(1, 1), new(1, 1), ECarrierType.Fuel);
var result = LevelEditor.TryMoveOccupant(level, new(1, 1), new(3, 3));
Assert.True(result.Success, result.Reason);
Assert.Single(result.Level.Leaks);
Assert.Equal(new(3, 3), result.Level.Leaks[0].UndergroundPosition);
Assert.Equal(new(3, 3), result.Level.Leaks[0].AccessPosition);
Assert.Equal(EUndergroundState.Leaking, result.Level.GetUnderground(new(3, 3), ECarrierType.Fuel).State);
Assert.Equal(EUndergroundState.Absent, result.Level.GetUnderground(new(1, 1), ECarrierType.Fuel).State);
}
[Fact]
public void MoveOccupantMovesElectricityLeakAccessFace()
{
var level = LevelState.Create("Move editor", 6, 6);
level = level.SetTerrain(new(2, 2), ECellTerrain.Wall);
level = LevelEditor.SetLeak(level, new(2, 2), new(2, 3), ECarrierType.Electricity);
var result = LevelEditor.TryMoveOccupant(level, new(2, 3), new(3, 2));
Assert.True(result.Success, result.Reason);
Assert.Single(result.Level.Leaks);
Assert.Equal(new(2, 2), result.Level.Leaks[0].UndergroundPosition);
Assert.Equal(new(3, 2), result.Level.Leaks[0].AccessPosition);
}
[Fact]
public void MoveOccupantReportsInvalidStartAndDestinationReasons()
{
var level = LevelState.Create("Move editor", 6, 6);
level = level.SetTerrain(new(3, 3), ECellTerrain.Wall);
level = level.SetProp(new(1, 1), new() { Type = EPropType.Consumer });
level = level.SetProp(new(2, 2), new() { Type = EPropType.Consumer });
var invalidStart = LevelEditor.TryMoveOccupant(level, new(4, 4), new(5, 5));
var invalidDestination = LevelEditor.TryMoveOccupant(level, new(1, 1), new(2, 2));
Assert.False(invalidStart.Success);
Assert.Contains("No movable", invalidStart.Reason);
Assert.False(invalidDestination.Success);
Assert.Contains("occupied", invalidDestination.Reason);
}
}

View File

@@ -1,27 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\ReactorMaintenance.Simulation\ReactorMaintenance.Simulation.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\ReactorMaintenance.Simulation\ReactorMaintenance.Simulation.csproj"/>
</ItemGroup>
</Project>

View File

@@ -1,21 +1,33 @@
namespace ReactorMaintenance.Simulation.Tests;
namespace ReactorMaintenance.Simulation.Tests;
public sealed class SimulationEngineTests
{
[Fact]
public void NetworkPropagationSuppliesBoundConsumersAndReadiesReactor()
public void NetworkPropagationSuppliesConsumerServicesAndReadiesReactor()
{
var level = BuildReadyLevel();
var next = m_Engine.AdvanceTurn(level);
var consumer = next.GetProp(new(3, 3));
Assert.Equal(EConsumerServiceState.Producing, next.GetProp(new(3, 2)).ServiceState);
Assert.Equal(EConsumerServiceState.Producing, next.GetProp(new(3, 3)).ServiceState);
Assert.Equal(EConsumerServiceState.Producing, next.GetProp(new(3, 4)).ServiceState);
Assert.Equal(EConsumerServiceState.Producing, consumer.FuelServiceState);
Assert.Equal(EConsumerServiceState.Producing, consumer.CoolantServiceState);
Assert.Equal(EConsumerServiceState.Producing, consumer.ElectricityServiceState);
Assert.Equal(ELevelState.Ready, next.Global.LevelState);
Assert.Contains(next.Forecasts, forecast => forecast.Kind == EForecastKind.ReactorReady);
}
[Fact]
public void ReactorNeedsPositiveFlowOnlyForNetworksBeneathControl()
{
var level = BuildReadyLevel();
level = level.SetUnderground(new(5, 3), ECarrierType.Fuel, new() { State = EUndergroundState.Intact });
var next = m_Engine.AdvanceTurn(level);
Assert.NotEqual(ELevelState.Ready, next.Global.LevelState);
}
[Fact]
public void ReactorActivatesOnlyAtReadyControl()
{
@@ -30,19 +42,110 @@ public sealed class SimulationEngineTests
}
[Fact]
public void LeakingUndergroundCellInjectsMatchingSurfaceHazard()
public void DisabledConsumerReportsDisabledOnlyForNetworksBeneathIt()
{
var level = LevelState.Create("Leak", 6, 6);
level = level.SetUnderground(new(2, 2), ECarrierType.Fuel, new() { State = EUndergroundState.Leaking, Amount = 5, Intensity = 5 }) with {
Leaks = [new LeakState { Carrier = ECarrierType.Fuel, UndergroundPosition = new(2, 2), AccessPosition = new(2, 2) }]
var level = LevelState.Create("Disabled", 6, 6);
level = level.SetUnderground(new(2, 2), ECarrierType.Fuel, new() { State = EUndergroundState.Intact });
level = level.SetProp(new(2, 2), new() { Type = EPropType.Consumer, SwitchState = EPropSwitchState.Disabled });
var next = m_Engine.AdvanceTurn(level);
var consumer = next.GetProp(new(2, 2));
Assert.Equal(EConsumerServiceState.Disabled, consumer.FuelServiceState);
Assert.Equal(EConsumerServiceState.Unknown, consumer.CoolantServiceState);
Assert.Equal(EConsumerServiceState.Unknown, consumer.ElectricityServiceState);
Assert.Equal(EConsumerServiceState.Disabled, consumer.ServiceState);
}
[Fact]
public void MovementIsQuickAndDoesNotResolveSimulationStep()
{
var level = LevelState.Create("Quick", 6, 6) with {
Robot = new() { Position = new(1, 1) }
};
level = level.SetProp(new(2, 2), new() { Type = EPropType.Flow, Carrier = ECarrierType.Fuel });
var next = m_Engine.MoveRobot(level, new(2, 1));
Assert.Equal(new(2, 1), next.Robot.Position);
Assert.Equal(0, next.Global.Turn);
}
[Fact]
public void DoorInteractionIsLengthyAndResolvesSimulationStep()
{
var level = DoorLevel();
level = level with { Robot = new() { Position = new(3, 2) } };
var next = m_Engine.InteractProp(level);
Assert.Equal(EDoorState.Open, next.GetProp(new(3, 2)).DoorState);
Assert.Equal(1, next.Global.Turn);
}
[Fact]
public void ClosedInferredDoorBlocksAdjacentHeatFlow()
{
var level = DoorLevel();
level = level.SetSurface(new(3, 2), new() { Heat = 8 });
var next = m_Engine.AdvanceTurn(level);
Assert.Equal(0, next.GetSurface(new(4, 2)).Heat);
}
[Fact]
public void StructuralIntegrityCreatesLeakWhenWeakCellHasPositivePressure()
{
var level = LevelState.Create("Integrity leak", 6, 6);
level = AddLine(level, ECarrierType.Fuel, new(1, 2), new(2, 2));
level = level.SetProp(new(1, 2), new() { Type = EPropType.Flow, Carrier = ECarrierType.Fuel });
level = level.SetUnderground(new(2, 2), ECarrierType.Fuel, level.GetUnderground(new(2, 2), ECarrierType.Fuel) with {
StructuralIntegrity = Balancing.Current.StructuralIntegrityLeakThreshold
});
var next = m_Engine.AdvanceTurn(level);
Assert.Equal(EUndergroundState.Leaking, next.GetUnderground(new(2, 2), ECarrierType.Fuel).State);
Assert.Contains(next.Leaks, leak => leak.Carrier == ECarrierType.Fuel && leak.UndergroundPosition == new GridPosition(2, 2));
Assert.True(next.GetSurface(new(2, 2)).Fuel > 0);
}
[Fact]
public void HighPressureWorsensNonMaxStructuralIntegrity()
{
var level = LevelState.Create("Integrity damage", 6, 6);
level = AddLine(level, ECarrierType.Fuel, new(1, 2), new(2, 2));
level = level.SetProp(new(1, 2), new() { Type = EPropType.Flow, Carrier = ECarrierType.Fuel });
level = level.SetUnderground(new(2, 2), ECarrierType.Fuel, level.GetUnderground(new(2, 2), ECarrierType.Fuel) with {
StructuralIntegrity = Balancing.Current.MaxStructuralIntegrity - 1
});
var next = m_Engine.AdvanceTurn(level);
Assert.True(next.GetUnderground(new(2, 2), ECarrierType.Fuel).StructuralIntegrity < Balancing.Current.MaxStructuralIntegrity - 1);
}
[Fact]
public void RepairingLeakRestoresStructuralIntegrity()
{
var level = LevelState.Create("Repair", 6, 6);
level = level.SetUnderground(new(2, 2), ECarrierType.Fuel, new() {
State = EUndergroundState.Leaking,
Amount = 5,
Intensity = 5,
StructuralIntegrity = 0
}) with {
Robot = new() { Position = new(2, 2) },
Leaks = [new() { Carrier = ECarrierType.Fuel, UndergroundPosition = new(2, 2), AccessPosition = new(2, 2) }]
};
var next = m_Engine.InteractLeak(level, ECarrierType.Fuel, false);
Assert.True(next.Leaks[0].Repaired);
Assert.Equal(EUndergroundState.Intact, next.GetUnderground(new(2, 2), ECarrierType.Fuel).State);
Assert.Equal(Balancing.Current.MaxStructuralIntegrity, next.GetUnderground(new(2, 2), ECarrierType.Fuel).StructuralIntegrity);
}
[Fact]
public void ElementRemedyClearsHazardAndBlocksImmediateReentry()
{
@@ -50,7 +153,7 @@ public sealed class SimulationEngineTests
level = level.SetUnderground(new(2, 2), ECarrierType.Fuel, new() { State = EUndergroundState.Leaking, Amount = 5, Intensity = 5 });
level = level.SetSurface(new(2, 2), new() { Fuel = 5 }) with {
Robot = new() { Position = new(2, 2), FuelNeutralizers = 1 },
Leaks = [new LeakState { Carrier = ECarrierType.Fuel, UndergroundPosition = new(2, 2), AccessPosition = new(2, 2) }]
Leaks = [new() { Carrier = ECarrierType.Fuel, UndergroundPosition = new(2, 2), AccessPosition = new(2, 2) }]
};
var next = m_Engine.InteractLeak(level, ECarrierType.Fuel, true);
@@ -60,19 +163,6 @@ public sealed class SimulationEngineTests
Assert.Equal(0, next.Robot.FuelNeutralizers);
}
[Fact]
public void ClosedDoorBlocksAdjacentHeatFlow()
{
var level = LevelState.Create("Door", 6, 6);
level = level.SetSurface(new(2, 2), new() { Heat = 8 }) with {
Doors = [new DoorState { A = new(2, 2), B = new(3, 2), State = EDoorState.Closed }]
};
var next = m_Engine.AdvanceTurn(level);
Assert.Equal(0, next.GetSurface(new(3, 2)).Heat);
}
[Fact]
public void HeatShieldPreventsRobotHeatLoss()
{
@@ -87,54 +177,54 @@ public sealed class SimulationEngineTests
}
[Fact]
public void RobotLosesOnUnsafeElementHazard()
public void JunctionRatioSplitsFlowAcrossInferredOutgoingBranches()
{
var level = LevelState.Create("Unsafe", 6, 6);
level = level.SetSurface(new(2, 2), new() { Electricity = Balancing.Current.MaxValue }) with {
Robot = new() { Position = new(2, 2) }
};
var level = BuildJunctionLevel(2);
var next = m_Engine.AdvanceTurn(level);
Assert.Equal(ELevelState.Lost, next.Global.LevelState);
Assert.True(next.GetUnderground(new(2, 2), ECarrierType.Fuel).Amount > 0);
Assert.Equal(next.GetUnderground(new(2, 2), ECarrierType.Fuel).Amount, next.GetUnderground(new(3, 3), ECarrierType.Fuel).Amount);
Assert.True(next.GetUnderground(new(2, 2), ECarrierType.Fuel).Amount < next.GetUnderground(new(2, 3), ECarrierType.Fuel).Amount);
}
[Fact]
public void RuleEventCanCreateTerminalLossForecast()
public void JunctionZeroWeightBranchReceivesNoIntentionalOutflow()
{
var level = LevelState.Create("Rule", 6, 6) with {
RuleEvents = [
new RuleEventState {
Phase = ERuleEventPhase.EndOfTurn,
ForecastText = "containment failure",
Predicates = [new RulePredicate { Kind = ERulePredicateKind.TurnAtLeast, Turn = 0 }],
Effects = [new RuleEffect { Kind = ERuleEffectKind.MarkTerminalLoss, Message = "CONTAINMENT FAILURE" }]
}
]
};
var level = BuildJunctionLevel(0);
var forecasts = m_Engine.Forecast(level);
var next = m_Engine.AdvanceTurn(level);
Assert.Contains(forecasts, forecast => forecast.Kind == EForecastKind.RuleEvent && forecast.Message == "containment failure");
Assert.Contains(forecasts, forecast => forecast.Kind == EForecastKind.TerminalLoss);
Assert.Equal(0, next.GetUnderground(new(2, 2), ECarrierType.Fuel).Amount);
Assert.True(next.GetUnderground(new(3, 3), ECarrierType.Fuel).Amount > 0);
}
[Fact]
public void ValidatorRejectsWallHazardsAndInvalidReactorBinding()
public void ValidatorRejectsAmbiguousJunctionSourceBranches()
{
var level = LevelState.Create("Invalid", 6, 6);
level = level.SetTerrain(new(2, 2), ECellTerrain.Wall);
level = level with {
Surface = level.Surface.ToArray(),
Reactors = [new ReactorBinding { ControlPosition = new(3, 3), FuelConsumerPosition = new(1, 1), CoolantConsumerPosition = new(1, 1), ElectricityConsumerPosition = new(1, 1) }]
};
level.Surface[level.Index(new(2, 2))] = new() { Heat = 1 };
var level = BuildJunctionLevel(2);
level = level.SetProp(new(3, 3), new() { Type = EPropType.Flow, Carrier = ECarrierType.Fuel });
var report = new LevelValidator().Validate(level);
Assert.False(report.IsValid);
Assert.Contains(report.Errors, error => error.Message.Contains("Ambiguous junction flow", StringComparison.Ordinal));
}
[Fact]
public void ValidatorRejectsInvalidDoorGeometryAndWallHazards()
{
var level = LevelState.Create("Invalid", 6, 6);
level = level.SetProp(new(2, 2), new() { Type = EPropType.Door });
level = level.SetTerrain(new(4, 4), ECellTerrain.Wall);
level = level with { Surface = level.Surface.ToArray() };
level.Surface[level.Index(new(4, 4))] = new() { Heat = 1 };
var report = new LevelValidator().Validate(level);
Assert.False(report.IsValid);
Assert.Contains(report.Errors, error => error.Message.Contains("Door must be surrounded", StringComparison.Ordinal));
Assert.Contains(report.Errors, error => error.Message.Contains("Wall cell", StringComparison.Ordinal));
Assert.Contains(report.Errors, error => error.Message.Contains("Reactor binding", StringComparison.Ordinal));
}
[Fact]
@@ -145,9 +235,27 @@ public sealed class SimulationEngineTests
var json = LevelSerializer.Serialize(level);
var loaded = LevelSerializer.Deserialize(json);
Assert.Contains("\"Version\": 2", json);
Assert.Contains("\"Version\": 3", json);
Assert.Equal(level.Name, loaded.Name);
Assert.Equal(EPropType.Flow, loaded.GetProp(new(2, 2)).Type);
Assert.Equal(EPropType.Consumer, loaded.GetProp(new(3, 3)).Type);
Assert.Equal(level.RequiredFuelConsumers, loaded.RequiredFuelConsumers);
}
[Fact]
public void LevelSerializationRoundTripsPropDoorsAndElectricityLeakFaces()
{
var level = BuildReadyLevel();
level = level.SetTerrain(new(6, 4), ECellTerrain.Wall);
level = level.SetUnderground(new(6, 4), ECarrierType.Electricity, new() { State = EUndergroundState.Leaking }) with {
Leaks = [new() { Carrier = ECarrierType.Electricity, UndergroundPosition = new(6, 4), AccessPosition = new(6, 3) }]
};
level = DoorLevel(level);
var loaded = LevelSerializer.Deserialize(LevelSerializer.Serialize(level));
Assert.Equal(EPropType.Door, loaded.GetProp(new(3, 2)).Type);
Assert.Single(loaded.Leaks);
Assert.Equal(new(6, 3), loaded.Leaks[0].AccessPosition);
}
[Fact]
@@ -155,43 +263,45 @@ public sealed class SimulationEngineTests
{
var json = """
{
"Version": 1,
"Version": 2,
"Level": {}
}
""";
var exception = Assert.Throws<InvalidOperationException>(() => LevelSerializer.Deserialize(json));
Assert.Contains("Unsupported level file version 1", exception.Message);
Assert.Contains("Unsupported level file version 2", exception.Message);
}
private static LevelState BuildReadyLevel()
{
var level = LevelState.Create("Ready", 8, 7);
level = AddLine(level, ECarrierType.Fuel, new(2, 2), new(3, 2));
level = AddLine(level, ECarrierType.Fuel, new(2, 3), new(3, 3));
level = AddLine(level, ECarrierType.Coolant, new(2, 3), new(3, 3));
level = AddLine(level, ECarrierType.Electricity, new(2, 4), new(3, 4));
level = level.SetProp(new(2, 2), new() { Type = EPropType.Flow, Carrier = ECarrierType.Fuel });
level = level.SetProp(new(2, 3), new() { Type = EPropType.Flow, Carrier = ECarrierType.Coolant });
level = AddLine(level, ECarrierType.Electricity, new(2, 3), new(3, 3));
level = level.SetProp(new(2, 3), new() { Type = EPropType.Flow, Carrier = ECarrierType.Fuel });
level = level.SetProp(new(2, 2), new() { Type = EPropType.Flow, Carrier = ECarrierType.Coolant });
level = level.SetUnderground(new(2, 2), ECarrierType.Coolant, new() { State = EUndergroundState.Intact });
level = level.SetUnderground(new(2, 3), ECarrierType.Coolant, new() { State = EUndergroundState.Intact });
level = level.SetProp(new(2, 4), new() { Type = EPropType.Flow, Carrier = ECarrierType.Electricity });
level = level.SetProp(new(3, 2), new() { Type = EPropType.Consumer, Carrier = ECarrierType.Fuel });
level = level.SetProp(new(3, 3), new() { Type = EPropType.Consumer, Carrier = ECarrierType.Coolant });
level = level.SetProp(new(3, 4), new() { Type = EPropType.Consumer, Carrier = ECarrierType.Electricity });
level = level.SetUnderground(new(2, 4), ECarrierType.Electricity, new() { State = EUndergroundState.Intact });
level = level.SetUnderground(new(2, 3), ECarrierType.Electricity, new() { State = EUndergroundState.Intact });
level = level.SetProp(new(3, 3), new() { Type = EPropType.Consumer });
level = level.SetProp(new(5, 3), new() { Type = EPropType.ReactorControl, ReactorId = 1 });
return level with {
Robot = new() { Position = new(5, 3) },
Reactors = [
new ReactorBinding {
ReactorId = 1,
ControlPosition = new(5, 3),
FuelConsumerPosition = new(3, 2),
CoolantConsumerPosition = new(3, 3),
ElectricityConsumerPosition = new(3, 4)
}
]
Reactors = [new() { ReactorId = 1, ControlPosition = new(5, 3) }]
};
}
private static LevelState DoorLevel(LevelState? seed = null)
{
var level = seed ?? LevelState.Create("Door", 6, 6);
level = level.SetTerrain(new(3, 1), ECellTerrain.Wall);
level = level.SetTerrain(new(3, 3), ECellTerrain.Wall);
return level.SetProp(new(3, 2), new() { Type = EPropType.Door, DoorState = EDoorState.Closed });
}
private static LevelState AddLine(LevelState level, ECarrierType carrier, GridPosition a, GridPosition b)
{
level = level.SetUnderground(a, carrier, new() { State = EUndergroundState.Intact });
@@ -199,5 +309,16 @@ public sealed class SimulationEngineTests
return level;
}
private static LevelState BuildJunctionLevel(int mode)
{
var level = LevelState.Create("Junction", 6, 6);
level = level.SetUnderground(new(1, 3), ECarrierType.Fuel, new() { State = EUndergroundState.Intact });
level = level.SetUnderground(new(2, 3), ECarrierType.Fuel, new() { State = EUndergroundState.Intact });
level = level.SetUnderground(new(2, 2), ECarrierType.Fuel, new() { State = EUndergroundState.Intact });
level = level.SetUnderground(new(3, 3), ECarrierType.Fuel, new() { State = EUndergroundState.Intact });
level = level.SetProp(new(1, 3), new() { Type = EPropType.Flow, Carrier = ECarrierType.Fuel });
return level.SetProp(new(2, 3), new() { Type = EPropType.Junction, Carrier = ECarrierType.Fuel, JunctionMode = mode });
}
private readonly SimulationEngine m_Engine = new();
}