Class Simulation
- Namespace
- TDMaker.Core
- Assembly
- TDMaker.Core.dll
A deterministic fixed-step world. Use commands for player actions and ClearEvents after reading output.
public sealed class Simulation
- Inheritance
-
Simulation
- Inherited Members
Constructors
Simulation(TemplateRegistry, LevelDef, SimConfig, uint)
Creates and prewarms a world; no engine APIs or background clocks are used.
public Simulation(TemplateRegistry templates, LevelDef level, SimConfig config = null, uint seed = 1)
Parameters
templatesTemplateRegistrylevelLevelDefconfigSimConfigseeduint
Properties
ActiveBeamCount
Unfinished budget channels, included in completion accounting.
public int ActiveBeamCount { get; }
Property Value
ActiveChainCount
Unfinished chain executions, included in level completion accounting.
public int ActiveChainCount { get; }
Property Value
Beams
Live and pending-removal budget channels in creation order.
public IReadOnlyList<BeamBinding> Beams { get; }
Property Value
CapacityFailureCount
Number of fixed-pool saturation incidents.
public long CapacityFailureCount { get; }
Property Value
Config
Immutable simulation settings.
public SimConfig Config { get; }
Property Value
DeltaTime
Seconds per fixed step.
public float DeltaTime { get; }
Property Value
DroppedDamageCount
Number of damage requests rejected by the configured per-tick capacity.
public long DroppedDamageCount { get; }
Property Value
DroppedEventCount
Number of notifications dropped because the caller did not drain the fixed event buffer.
public long DroppedEventCount { get; }
Property Value
Effects
Attached effect and region lifecycle service.
public EffectService Effects { get; }
Property Value
Entities
Runtime entity state and deferred queues.
public EntityStore Entities { get; }
Property Value
Events
Events retained until ClearEvents is called.
public IReadOnlyList<SimEvent> Events { get; }
Property Value
Level
Immutable baked level.
public LevelDef Level { get; }
Property Value
PendingRecoveryCount
Pending same-identity returns, successor preparation and body consumption. Unreserved bodies do not hold victory.
public int PendingRecoveryCount { get; }
Property Value
QueuedDamageCount
Current FIFO length, for allocation-free diagnostics.
public int QueuedDamageCount { get; }
Property Value
Random
The seeded simulation random stream.
public SimRandom Random { get; }
Property Value
RangeYScale
Range multiplier after applying the level override.
public float RangeYScale { get; }
Property Value
Remains
Neutral bodies in stable creation order, including exclusive reservations.
public IReadOnlyList<RemainsRecord> Remains { get; }
Property Value
ScheduledActionCount
Pending summon/strike sequences, for diagnostics.
public int ScheduledActionCount { get; }
Property Value
Seed
Original random seed, also stored by command recordings.
public uint Seed { get; }
Property Value
SpatialRebuildCount
Enemy and friendly spatial rebuild count, excluded from deterministic state.
public long SpatialRebuildCount { get; }
Property Value
Spells
Spell states in Level.AvailableSpells order; unresolved legacy slots remain locked.
public IReadOnlyList<SpellState> Spells { get; }
Property Value
State
Read-only global state.
public GameState State { get; }
Property Value
SystemPipeline
Read-only metadata snapshot in actual execution order. Refetch after setup-time RegisterSystem calls; unchanged during stepping.
public IReadOnlyList<SimSystemInfo> SystemPipeline { get; }
Property Value
Templates
Resolved immutable template definitions.
public TemplateRegistry Templates { get; }
Property Value
Time
Game time derived from completed fixed ticks.
public double Time { get; }
Property Value
Traps
Live and pending-removal deployments in creation order.
public IReadOnlyList<TrapBinding> Traps { get; }
Property Value
UpgradeFingerprint
Stable setup fingerprint of the permanent-upgrade snapshot and applied definitions; zero identifies legacy setup.
public ulong UpgradeFingerprint { get; }
Property Value
Methods
AddHeroExperience(int, double)
Queues nonnegative raw hero experience for the progression stage.
public bool AddHeroExperience(int entityId, double amount)
Parameters
Returns
CancelBeam(int)
Cancels a channel at the next mechanism stage.
public bool CancelBeam(int beamId)
Parameters
beamIdint
Returns
ClearEvents()
Clears consumed events while retaining their backing capacity.
public void ClearEvents()
ComputeStateHash()
Hashes authoritative runtime state at a tick boundary, including pending work and RNG. Events and render-clock backlog are excluded.
public ulong ComputeStateHash()
Returns
Examples
ulong checkpoint = sim.ComputeStateHash();
Remarks
Use identical definitions, extension registration and seed. Extensions must contribute their mutable state via IStateHashContributor. Exact floating-point bits diagnose same-runtime divergence; this is not a cross-platform numeric determinism guarantee.
ConfigureSpatialIndex(float, bool)
Configures horizontal grid cell size before stepping. Disabling it selects the Id-ordered linear backend for profiling.
public void ConfigureSpatialIndex(float cellSize = 4, bool enabled = true)
Parameters
Examples
sim.ConfigureSpatialIndex(cellSize: 4);
Deploy(DeploymentPlanDef, int)
Atomically reserves one seeded path-node site and one trap slot. Zero means no candidate, invalid producer, quota or capacity.
public int Deploy(DeploymentPlanDef plan, int sourceId)
Parameters
planDeploymentPlanDefsourceIdint
Returns
Enqueue(ICommand)
Queues an immutable command for the next tick; input allocation is outside Step.
public void Enqueue(ICommand command)
Parameters
commandICommand
FindDefaultTarget(in TargetQuery)
Uses the built-in priority scoring and Id tie-breaks without invoking the custom selector. Safe to call from ITargetSelector.Select.
public Entity FindDefaultTarget(in TargetQuery query)
Parameters
queryTargetQuery
Returns
Examples
return simulation.FindDefaultTarget(query);
Remarks
Uses separate reusable storage and leaves the custom selector's candidate buffer unchanged.
FindTarget(in TargetQuery)
Finds a ranged target using the registered selector or the original scoring and Id tie-breaks.
public Entity FindTarget(in TargetQuery query)
Parameters
queryTargetQuery
Returns
GetTowerAtSlot(int)
Reads a reserved or committed tower at a construction slot, or zero.
public int GetTowerAtSlot(int slotIndex)
Parameters
slotIndexint
Returns
IsEligibleTarget(Entity, in TargetQuery)
Checks membership, lifecycle, faction, flight filters and the query's elliptical range.
public bool IsEligibleTarget(Entity target, in TargetQuery query)
Parameters
targetEntityqueryTargetQuery
Returns
PlaceTrap(TrapDef, Vec2, int)
Places a validated deployment without allocating. Returns zero on invalid placement/source, population limit or pool exhaustion.
public int PlaceTrap(TrapDef definition, Vec2 desired, int sourceId = 0)
Parameters
Returns
PredictTargetPosition(int, float)
Predicts fixed-course aim by traversing path segments from the current position at observed movement speed. Blocked or stationary targets are not led.
public Vec2 PredictTargetPosition(int entityId, float seconds)
Parameters
Returns
Remarks
Uses continuous world distance, including connection gaps, rather than rounding to an authored node. Does not anticipate later control changes.
PreviewEarlyCallReward()
Previews the next manual wave call at current fixed time, including its gold and shared spell time credit.
public EarlyCallRewardPreview PreviewEarlyCallReward()
Returns
Examples
EarlyCallRewardPreview reward = sim.PreviewEarlyCallReward();
PreviewEarlyCallSpellCooldownCut(int)
Previews the exact reduction of one spell's remaining cooldown if the next wave is called now.
public float PreviewEarlyCallSpellCooldownCut(int spellIndex)
Parameters
spellIndexint
Returns
PreviewSellRefund(int)
Returns the same nominal refund reported by TowerSold, before wallet saturation. Invalid, removing or non-tower entities return zero.
public int PreviewSellRefund(int towerId)
Parameters
towerIdint
Returns
Examples
int refund = sim.PreviewSellRefund(tower.Id);
PublishEvent(SimEvent)
Publishes an event in the reserved custom range. Uses the normal fixed-capacity event buffer.
public void PublishEvent(SimEvent value)
Parameters
valueSimEvent
Examples
sim.PublishEvent(new SimEvent((SimEventKind)((int)SimEventKind.CustomStart + 1), actor.Id));
QueryEnemies(Vec2, float, SpatialQueryBuffer)
Queries active enemies using the shared ellipse. Results are ascending Id; the caller owns and reuses its buffer.
public void QueryEnemies(Vec2 center, float range, SpatialQueryBuffer results)
Parameters
centerVec2rangefloatresultsSpatialQueryBuffer
Examples
sim.QueryEnemies(center, 3, cachedBuffer);
RaiseRemains(RaiseRemainsDef, int)
Reserves closest eligible bodies, breaking ties by body Id. Returns accepted count; capacity and population include existing reservations.
public int RaiseRemains(RaiseRemainsDef definition, int actorId)
Parameters
definitionRaiseRemainsDefactorIdint
Returns
RangeDistanceSquared(Vec2, Vec2)
Elliptical distance squared using the product's vertical radius ratio.
public float RangeDistanceSquared(Vec2 a, Vec2 b)
Parameters
Returns
RegisterCommandHandler<T>(ICommandHandler<T>)
Registers an exact custom command type; built-in commands retain their original handlers.
public void RegisterCommandHandler<T>(ICommandHandler<T> handler) where T : class, ICommand
Parameters
handlerICommandHandler<T>
Type Parameters
T
Examples
sim.RegisterCommandHandler<SetStance>(new SetStanceHandler());
RegisterComponent<T>(Func<T>)
Prewarms one unique component instance for every pool bundle. Factories run only here; Reset runs on spawn.
public void RegisterComponent<T>(Func<T> factory) where T : class, ICustomComponent
Parameters
factoryFunc<T>
Type Parameters
T
Examples
sim.RegisterComponent(() => new ShieldState());
RegisterDamageHook(IDamageHook)
Adds a damage hook in deterministic registration order.
public void RegisterDamageHook(IDamageHook hook)
Parameters
hookIDamageHook
Examples
sim.RegisterDamageHook(new ShieldDamageHook());
RegisterStateHashContributor(IStateHashContributor)
Includes external extension state in ComputeStateHash, in registration order.
public void RegisterStateHashContributor(IStateHashContributor contributor)
Parameters
contributorIStateHashContributor
Examples
sim.RegisterStateHashContributor(weatherState);
RegisterSystem(ISimSystem, SimStage, bool)
Inserts a system before or after a built-in stage. Registrations at the same location run in registration order.
public void RegisterSystem(ISimSystem system, SimStage anchor, bool after = true)
Parameters
systemISimSystemanchorSimStageafterbool
Examples
sim.RegisterSystem(new WeatherSystem(), SimStage.Damage, after: false);
RegisterTargetSelector(ITargetSelector)
Registers the optional world-wide ranged selector. Candidate and definition data can distinguish actor types.
public void RegisterTargetSelector(ITargetSelector selector)
Parameters
selectorITargetSelector
Examples
sim.RegisterTargetSelector(new LowestShieldSelector());
RemoveTrap(int)
Marks a deployment for removal in the next mechanism stage.
public bool RemoveTrap(int trapId)
Parameters
trapIdint
Returns
RepositionCompanions(int, Vec2)
Reassigns ground escorts around an approved owner destination. MoveHero and SetRallyPoint call this automatically.
public void RepositionCompanions(int ownerId, Vec2 destination)
Parameters
RequestDamage(DamageContext)
Queues custom raw damage through the same hooks, FIFO, defenses, effects and experience path.
public bool RequestDamage(DamageContext request)
Parameters
requestDamageContext
Returns
Examples
sim.RequestDamage(new DamageContext(actor.Id, target.Id, 10, damage));
SetEntityPosition(int, Vec2, bool)
Changes position without advancing a path cursor; invalidates spatial queries immediately.
public bool SetEntityPosition(int entityId, Vec2 position, bool resetInterpolation = false)
Parameters
Returns
SetProfiler(ISimProfiler)
Installs or clears observational hooks before the first Step.
public void SetProfiler(ISimProfiler value)
Parameters
valueISimProfiler
Examples
simulation.SetProfiler(new MyPreallocatedProfiler());
SpawnEnemy(string, int, int)
Queues an enemy at a validated path entry. Its first action is after the next Flush.
public Entity SpawnEnemy(string templateId, int pathIndex, int laneIndex = 0)
Parameters
Returns
SpawnHero(string, Vec2)
Queues an available independent hero at a validated finite starting position; returns null on pool saturation.
public Entity SpawnHero(string templateId, Vec2 position)
Parameters
Returns
StartRecording(CommandLog)
Begins fixed-capacity recording before the first Step. Only successfully queued external commands are recorded.
public void StartRecording(CommandLog log)
Parameters
logCommandLog
StartReplay(CommandLog)
Injects recorded commands at their original ticks. Configure the same initial world and seed; live external input is rejected.
public void StartReplay(CommandLog log)
Parameters
logCommandLog
Examples
var replay = new Simulation(templates, level, config, log.Seed); replay.StartReplay(log);
Step()
Advances one fixed tick: damage precedes exits, outcomes precede Flush, new entities act next tick.
public void Step()
StopRecording()
Seals and detaches the current recording between ticks; does nothing when not recording.
public void StopRecording()
TryResolvePoint(PointRuleDef, Vec2, out Vec2)
Uses the shared point policy for previews, hero destinations, and spell approvals.
public bool TryResolvePoint(PointRuleDef rule, Vec2 candidate, out Vec2 accepted)
Parameters
rulePointRuleDefcandidateVec2acceptedVec2
Returns
TryResolveRallyPoint(int, Vec2, out Vec2)
Validates a barracks rally point using the same range, finite-coordinate and lane projection rules as SetRallyPoint.
public bool TryResolveRallyPoint(int towerId, Vec2 desired, out Vec2 resolved)
Parameters
Returns
Examples
bool valid = sim.TryResolveRallyPoint(tower.Id, cursor, out Vec2 preview);
TryTransferToPath(int, PathDestinationDef, int)
Atomically changes path/lane/cursor/position, clears interception and refreshes the spatial index. Invalid geometry changes nothing.
public bool TryTransferToPath(int entityId, PathDestinationDef destination, int sourceId = 0)
Parameters
entityIdintdestinationPathDestinationDefsourceIdint
Returns
TryTransferToPath(int, PathDestinationDef, int, string)
Transfers a unit and includes the supplied mechanism identity in PathTransferred.Key. Null preserves the legacy unkeyed notification.
public bool TryTransferToPath(int entityId, PathDestinationDef destination, int sourceId, string mechanismKey)
Parameters
entityIdintdestinationPathDestinationDefsourceIdintmechanismKeystring
Returns
TryValidateSpellPoint(int, Vec2, out Vec2)
Validates spell placement for UI previews without consuming cooldown or randomness.
public bool TryValidateSpellPoint(int spellIndex, Vec2 position, out Vec2 accepted)