How dn0903/marginalia-the-hero-s-journey works
1. Project type and runtime
- This is a Unity 2D combat-simulation project (“Hero’s Library”), not a general web app.
- The runtime is built around Unity scenes, ScriptableObject content, and a deterministic combat loop.
- Notable supporting tech: Unity Input System, URP 2D, DOTween, TextMesh Pro.
2. Core product idea
- The system is designed to predict combat outcomes before committing them.
- The same effect compiler is used for both preview/prediction and runtime execution, so parity can be checked instead of assumed.
- The main architectural promise is determinism: same inputs, same seed/state versions, same result.
3. Primary control plane
- CombatManager, BattleReferee, and TimeLineEngine form the core combat orchestration layer.
- CombatManager owns battle state and turn flow.
- BattleReferee and battle-condition systems decide when a battle can continue or end.
- EventManager is the main broadcast mechanism for authoritative state changes.
4. Oracle subsystem: predict-then-execute
- The Oracle subsystem is the most distinctive part of the repo.
- BattleOracle bridges prediction and execution, using:
- OracleSimulationEngine for deterministic simulation,
- ActionPackageBuilder / ActionPackage for compiled action payloads,
- EffectsPipeline for effect resolution,
- SimulationWriteGuard to prevent prediction-side mutation.
- This is the central boundary between “forecasting” and “committing” gameplay.
5. Parity, caching, and drift detection
- OracleRealityVault stores stamped predictions keyed by actor/skill/target plus versioned state.
- OracleParityProbe and DriftReportBuffer enforce runtime equality between predicted and executed results.
- CriticalParityException is the hard-stop boundary for invalid divergence.
- The .dbg files and oracle test harnesses indicate active work on parity failures and drift reproduction.
6. State authority and versioning
- StateAuthority is the authoritative commit layer for gameplay state.
- It maintains commit buffers, sinks, and events, and advances versions only from committed gameplay changes.
- ResourceSnapshot and ResourceTransactionScope support transaction-like state handling.
- This state/version model is part of the determinism anchor used by the Oracle seed logic.
7. Rules as a policy engine
- The rules system is a separate policy layer rather than scattered if statements.
- RuleRegistry, RuleMask256, RuleBit, ObjectTag, and generated BakedRuleMap provide fast rule checks.
- Individual rules like Rule_MatchRole, Rule_OwnerALive, and Rule_BattleOngoing are the concrete gates used by combat logic.
- Editor tooling (RuleBakeProcessor) suggests rules are compiled/baked into lookup structures.
8. Content/effect model
- Combat behavior is data-driven through ScriptableObjects under Assets/Resources/CombatData.
- The main content categories are:
- skills,
- effects,
- status effects,
- passives,
- unit stats.
- Runtime effect types are implemented in Assets/Scripts/Data/Effects, while authored assets live in Resources so they can be loaded by the simulation and Oracle.
9. Status effects and capability masking
- Status effects are treated as a subsystem, not just a visual debuff layer.
- StatusInjector, EffectChannel, EffectSlot, and StatusEffectParams manage how effects are applied and updated.
- There is also a capabilities layer (UnitCapabilities, UnitCapabilityRegistry) that appears to gate what units can do when effects are active.
- This is where policy, stats, and runtime simulation intersect.
10. Timeline and turn forecasting
- The timeline subsystem (TurnData, VirtualEntityData, TurnPredictor, RoundBarrier) handles turn order and future turn projection.
- TimeLineEngine likely uses these data structures to maintain deterministic sequencing.
- This subsystem feeds both gameplay UI and Oracle preview logic.
11. Entity boundaries
- Units are split into player, enemy, and shared combat-unit abstractions.
- Shared unit concerns live under Entities/CombatUnit:
- stats,
- combat behavior,
- passives,
- timeline,
- visuals.
- Enemy-specific logic includes EnemyBrain, EnemySkillConfig, and UnitBreak, while PlayerUnit is separate.
- This suggests a deliberate boundary between generic combat identity and side-specific AI/control.
12. UI and feedback layer
- UI is lightweight and driven by combat state rather than owning gameplay logic.
- CombatUI, HPBarUI, MPBarUI, TurnBarUI, and damage popup prefabs provide feedback from authoritative state changes.
- Visual effects are supported by prefabs and DOTween, but the UI is downstream of combat authority.
13. Editor and build-time support
- The repo includes editor scripts for inspection and generation:
- CombatObjectEditor,
- PassiveEffectEditor,
- StatModifierStatusEffectEditor,
- OracleSystemActivation,
- MarginaliaCleanSweep,
- RuleBakeProcessor.
- This indicates the architecture depends on tooling to keep data, rules, and generated artifacts consistent.
14. Testing, audits, and documentation as architecture artifacts
- There is extensive documentation for Oracle parity, rules, state versioning, skill architecture, and sequential effect execution.
- Tests such as OracleDeterminismTester and OracleParityTestHarness target the determinism contract directly.
- The .kiro, .dbg, and report files show that architecture decisions are tracked as first-class project assets, not just code.
15. Main data flow to diagram
- Player input or AI intent enters via managers/controllers.
- Combat actions are packaged and sent through the Oracle pipeline.
- The Oracle simulates effects against versioned state, validates parity, and emits a commitable result.
- StateAuthority commits the result, EventManager broadcasts it, timeline/state subsystems update, and UI/visuals react.
- Rules and capabilities gate whether contributions are allowed at each step.
16. Useful diagram boundaries
- Treat these as the major boxes:
- Authoring/content: ScriptableObjects, Resources, editor bake tools.
- Combat orchestration: CombatManager, BattleReferee, timeline, conditions.
- Oracle prediction: BattleOracle, simulation engine, write guard, parity vault.
- State authority: StateAuthority, snapshots, transactions, versioning.
- Policy/rules: RuleRegistry and baked rule maps.
- Unit/effect systems: units, effects, status effects, passives, capabilities.
- Presentation: UI, popups, DOTween-driven feedback.
Main entry points: BattleOracle.cs Battle oracle · StateAuthority.cs State authority · RuleRegistry.cs Rule registry · CombatManager.cs Combat manager
Components 26
Authoring & content
ScriptableObject combat data, baked rules, and editor tooling that shape runtime behavior.
- Combat data · Authored skills, effects, statuses, passives, unit stats, and win-condition assets under Resources.
Assets/Resources/CombatData - Editor tools · Editor scripts that validate, bake, and keep combat content and rules aligned.
Assets/Editor - Rule assets · Authorable rule definitions used as policy inputs and baked into runtime lookups.
Assets/Resources/Rules
Combat orchestration
Authoritative combat flow, turn control, battle conditions, and event broadcasting.
- Combat manager · Owns battle state, turn flow, and high-level combat coordination.
Assets/Scripts/Core/CombatManager.cs - Battle referee · Evaluates battle continuation and end conditions with the condition systems.
Assets/Scripts/Core/BattleReferee.cs - Timeline engine · Maintains deterministic turn ordering and forecasting across combat rounds.
Assets/Scripts/Core/TimeLineEngine.cs - Event manager · Broadcasts authoritative combat state changes to downstream systems.
Assets/Scripts/Manager/EventManager.cs
Oracle prediction
Deterministic simulation, action packaging, parity checking, and drift handling.
- Battle oracle · Coordinates prediction versus execution and connects simulation results back to runtime.
Assets/Scripts/Systems/Oracle/CoreOracle/BattleOracle.cs - Sim engine · Runs the oracle simulation over versioned combat state.
Assets/Scripts/Systems/Oracle/CoreOracle/OracleSimulationEngine.cs - Action package · Builds compiled action payloads that feed the same pipeline in preview and runtime.
Assets/Scripts/Systems/Oracle/Pipeline/ActionPackageBuilder.cs - Effects pipeline · Resolves effect execution for both prediction and committed gameplay.
Assets/Scripts/Systems/Oracle/Pipeline/EffectsPipeline.cs - Write guard · Prevents prediction-side writes from mutating authoritative state.
Assets/Scripts/Systems/Oracle/CoreOracle/SimulationWriteGuard.cs - Reality vault · Stores stamped predictions keyed by actors, skills, targets, and state versions.
Assets/Scripts/Systems/Oracle/Infrastructure/OracleRealityVault.cs - Parity probe · Compares predicted and executed outcomes and reports divergence.
Assets/Scripts/Systems/Oracle/Infrastructure/OracleParityProbe.cs
State authority
Committed gameplay state, versioning, snapshots, and transactional resource handling.
- State authority · Authoritatively commits gameplay changes and advances state versions.
Assets/Scripts/Systems/State/StateAuthority.cs - Snapshot tx · Supports snapshotting and transaction-like handling of battle resources.
Assets/Scripts/Systems/State/ResourceSnapshot.cs
Rules & policy
Fast rule gates and generated lookup maps used by combat and effect systems.
- Rule registry · Central rule evaluation layer for combat gates and fast checks.
Assets/Scripts/Systems/Rules/RuleRegistry.cs - Baked map · Generated rule mask lookup used for runtime-efficient policy checks.
Assets/Scripts/Systems/Rules/Generated/BakedRuleMap.cs
Units & effects
Combat entities, skills, effects, status handling, passives, and capability masking.
- Combat unit · Shared unit abstraction covering core combat identity and behavior.
Assets/Scripts/Entities/CombatUnit/CombatUnit.cs - Enemy unit · Enemy-specific brain, skill config, and break behavior.
Assets/Scripts/Entities/EnemyUnit/EnemyUnit.cs - Player unit · Player-facing combat entity boundary.
Assets/Scripts/Entities/PlayerUnit.cs - Effect system · Implements damage, healing, break, stat buffs, status injection, and timeline shifts.
Assets/Scripts/Data/Effects - Status channel · Applies and updates status effects and capability masks across combat state.
Assets/Scripts/Systems/StatusEffects/EffectChannel - Capabilities · Determines what units can do when status and battle policy change.
Assets/Scripts/Systems/Capabilities/UnitCapabilities.cs
Presentation
UI, popups, and scene-facing feedback downstream of authoritative state.
- Combat UI · Surfaces HP, MP, turn order, and combat state feedback to the player.
Assets/Scripts/UI/CombatUI.cs - Damage popup · Floating damage and combat popups driven by authoritative events.
Assets/Prefabs/DamagePopup.prefab
Connections 26
- Combat data → Editor tools: baked by · Authoring content is validated and baked into runtime-friendly artifacts.
- Rule assets → Rule registry: loaded by · Rules assets feed the runtime registry and generated lookup layer.
- Editor tools → Baked map: generates · Editor tooling produces baked rule maps for fast runtime checks.
- Combat manager → Battle referee: consults · Combat flow is constrained by battle continuation and end-condition checks.
- Combat manager → Timeline engine: drives · The manager advances deterministic turn flow through the timeline engine.
- Combat manager → Battle oracle: submits actions · Intent is routed through oracle prediction before committing.
- Battle oracle → Action package: compiles · Oracle packages actor intent into action payloads.
- Action package → Effects pipeline: feeds · Compiled actions enter the shared effects resolution pipeline.
- Battle oracle → Sim engine: simulates · The oracle executes deterministic simulations over versioned state.
- Sim engine → Write guard: protected by · Simulation writes are blocked from mutating authoritative state.
- Battle oracle → Reality vault: caches · Predicted outcomes are stored for later comparison and reuse.
- Battle oracle → Parity probe: verifies · Predicted results are checked against committed runtime results.
- Parity probe → State authority: compares against · Parity checking is anchored to committed state transitions.
- State authority → Snapshot tx: uses · Commit authority relies on snapshots and transactional resource scopes.
- State authority → Event manager: publishes · Committed changes are broadcast to listeners and UI.
- Rule registry → State authority: gates commits · Policy checks decide whether gameplay changes may be applied.
- Rule registry → Capabilities: constrains · Rules and capabilities combine to gate unit actions.
- Combat unit → Effect system: applies · Shared units are modified through the runtime effect system.
- Enemy unit → Rule registry: bounded by · Enemy behavior is constrained by the same policy layer.
- Player unit → Combat manager: inputs to · Player intent enters combat orchestration through controllers.
- Status channel → Capabilities: masks · Status effects alter capability availability through masking.
- Status channel → Effect system: routes · Status application and updates are part of the effect boundary.
- Combat UI → Event manager: subscribes to · UI is updated from authoritative combat events.
- Damage popup → Event manager: reacts to · Visual feedback is spawned from combat state broadcasts.
- Timeline engine → Rule registry: checks · Timeline projection is constrained by battle policy.
- Effects pipeline → State authority: commits via · Resolved effect outcomes are committed through state authority.