| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using Lattice.ECS.Core; |
| | | 6 | | using Lattice.ECS.Framework; |
| | | 7 | | using Lattice.Math; |
| | | 8 | | |
| | | 9 | | namespace Lattice.ECS.Session |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// 将输入写入帧状态的最小入口。 |
| | | 13 | | /// </summary> |
| | | 14 | | public delegate void SimulationInputApplier<TInput>(Frame frame, TInput input) where TInput : unmanaged; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// 最小确定性模拟会话。 |
| | | 18 | | /// </summary> |
| | | 19 | | public sealed class SimulationSession<TInput> : IDisposable where TInput : unmanaged |
| | | 20 | | { |
| | | 21 | | private readonly SimulationSessionOptions _options; |
| | | 22 | | private readonly SimulationInputApplier<TInput>? _applyInput; |
| | | 23 | | private readonly TickBuffer _predictedInputs; |
| | | 24 | | private readonly TickBuffer _verifiedInputs; |
| | 4 | 25 | | private readonly SystemScheduler _scheduler = new(); |
| | | 26 | | private bool _disposed; |
| | | 27 | | private Frame? _verifiedFrame; |
| | | 28 | | private Frame? _predictedFrame; |
| | | 29 | | |
| | 4 | 30 | | public SimulationSession( |
| | 4 | 31 | | SimulationSessionOptions options, |
| | 4 | 32 | | SimulationInputApplier<TInput>? applyInput = null) |
| | | 33 | | { |
| | 4 | 34 | | ArgumentNullException.ThrowIfNull(options); |
| | | 35 | | |
| | 4 | 36 | | if (options.MaxEntities <= 0) |
| | | 37 | | { |
| | 0 | 38 | | throw new ArgumentOutOfRangeException(nameof(options.MaxEntities)); |
| | | 39 | | } |
| | | 40 | | |
| | 4 | 41 | | if (options.InputCapacity <= 0) |
| | | 42 | | { |
| | 0 | 43 | | throw new ArgumentOutOfRangeException(nameof(options.InputCapacity)); |
| | | 44 | | } |
| | | 45 | | |
| | 4 | 46 | | if (options.CommandBufferCapacity <= 0) |
| | | 47 | | { |
| | 0 | 48 | | throw new ArgumentOutOfRangeException(nameof(options.CommandBufferCapacity)); |
| | | 49 | | } |
| | | 50 | | |
| | 4 | 51 | | _options = options; |
| | 4 | 52 | | _applyInput = applyInput; |
| | 4 | 53 | | _predictedInputs = new TickBuffer(options.InputCapacity); |
| | 4 | 54 | | _verifiedInputs = new TickBuffer(options.InputCapacity); |
| | 4 | 55 | | Commands = new SimulationCommandBufferHost(); |
| | 4 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary>当前系统调度器。</summary> |
| | 0 | 59 | | public SystemScheduler Scheduler => _scheduler; |
| | | 60 | | |
| | | 61 | | /// <summary>当前 tick 命令缓冲宿主。</summary> |
| | 108 | 62 | | public SimulationCommandBufferHost Commands { get; } |
| | | 63 | | |
| | | 64 | | /// <summary>当前权威帧。</summary> |
| | 2 | 65 | | public Frame? VerifiedFrame => _verifiedFrame; |
| | | 66 | | |
| | | 67 | | /// <summary>当前预测帧。</summary> |
| | 3 | 68 | | public Frame? PredictedFrame => _predictedFrame; |
| | | 69 | | |
| | | 70 | | /// <summary>当前权威 tick。</summary> |
| | 9 | 71 | | public int VerifiedTick => _verifiedFrame?.Tick ?? _options.InitialTick; |
| | | 72 | | |
| | | 73 | | /// <summary>当前预测 tick。</summary> |
| | 0 | 74 | | public int PredictedTick => _predictedFrame?.Tick ?? _options.InitialTick; |
| | | 75 | | |
| | | 76 | | /// <summary>会话是否已启动。</summary> |
| | 29 | 77 | | public bool IsRunning { get; private set; } |
| | | 78 | | |
| | | 79 | | public void Add(ISystem system) |
| | | 80 | | { |
| | 4 | 81 | | _scheduler.Add(system); |
| | 4 | 82 | | } |
| | | 83 | | |
| | | 84 | | public void AddRange(ReadOnlySpan<ISystem> systems) |
| | | 85 | | { |
| | 0 | 86 | | for (int i = 0; i < systems.Length; i++) |
| | | 87 | | { |
| | 0 | 88 | | _scheduler.Add(systems[i]); |
| | | 89 | | } |
| | 0 | 90 | | } |
| | | 91 | | |
| | | 92 | | public void Start(Action<Frame>? bootstrap = null) |
| | | 93 | | { |
| | 4 | 94 | | ThrowIfDisposed(); |
| | | 95 | | |
| | 4 | 96 | | if (IsRunning) |
| | | 97 | | { |
| | 0 | 98 | | throw new InvalidOperationException("SimulationSession 已启动。"); |
| | | 99 | | } |
| | | 100 | | |
| | 4 | 101 | | Frame verified = CreateFrame(_options.InitialTick); |
| | 4 | 102 | | bootstrap?.Invoke(verified); |
| | | 103 | | |
| | 4 | 104 | | _scheduler.Initialize(verified); |
| | | 105 | | |
| | 4 | 106 | | _verifiedFrame = verified; |
| | 4 | 107 | | _predictedFrame = verified.Clone(); |
| | 4 | 108 | | IsRunning = true; |
| | 4 | 109 | | } |
| | | 110 | | |
| | | 111 | | public void SetPredictedInput(int tick, in TInput input) |
| | | 112 | | { |
| | 9 | 113 | | ThrowIfDisposed(); |
| | 9 | 114 | | _predictedInputs.Set(tick, input); |
| | 9 | 115 | | } |
| | | 116 | | |
| | | 117 | | public void SetVerifiedInput(int tick, in TInput input) |
| | | 118 | | { |
| | 10 | 119 | | ThrowIfDisposed(); |
| | 10 | 120 | | _verifiedInputs.Set(tick, input); |
| | 10 | 121 | | } |
| | | 122 | | |
| | | 123 | | public void AdvanceVerifiedTo(int targetTick) |
| | | 124 | | { |
| | 4 | 125 | | ThrowIfDisposed(); |
| | 4 | 126 | | EnsureRunning(); |
| | | 127 | | |
| | 4 | 128 | | if (targetTick < VerifiedTick) |
| | | 129 | | { |
| | 0 | 130 | | throw new InvalidOperationException("目标 verified tick 不能回退。"); |
| | | 131 | | } |
| | | 132 | | |
| | 14 | 133 | | while (_verifiedFrame!.Tick < targetTick) |
| | | 134 | | { |
| | 10 | 135 | | int nextTick = _verifiedFrame.Tick + 1; |
| | 10 | 136 | | TInput input = ResolveVerifiedInput(nextTick); |
| | 10 | 137 | | Frame next = SimulateNext(_verifiedFrame, nextTick, input); |
| | 10 | 138 | | ReplaceVerifiedFrame(next); |
| | | 139 | | } |
| | 4 | 140 | | } |
| | | 141 | | |
| | | 142 | | public void AdvancePredictedTo(int targetTick) |
| | | 143 | | { |
| | 5 | 144 | | ThrowIfDisposed(); |
| | 5 | 145 | | EnsureRunning(); |
| | | 146 | | |
| | 5 | 147 | | if (targetTick < VerifiedTick) |
| | | 148 | | { |
| | 0 | 149 | | targetTick = VerifiedTick; |
| | | 150 | | } |
| | | 151 | | |
| | 5 | 152 | | Frame current = _verifiedFrame!.Clone(); |
| | 15 | 153 | | while (current.Tick < targetTick) |
| | | 154 | | { |
| | 10 | 155 | | int nextTick = current.Tick + 1; |
| | 10 | 156 | | TInput input = ResolveBestInput(nextTick); |
| | 10 | 157 | | Frame next = SimulateNext(current, nextTick, input); |
| | 10 | 158 | | current.Dispose(); |
| | 10 | 159 | | current = next; |
| | | 160 | | } |
| | | 161 | | |
| | 5 | 162 | | ReplacePredictedFrame(current); |
| | 5 | 163 | | } |
| | | 164 | | |
| | | 165 | | public ulong CalculateVerifiedChecksum() |
| | | 166 | | { |
| | 2 | 167 | | ThrowIfDisposed(); |
| | 2 | 168 | | EnsureRunning(); |
| | 2 | 169 | | return _verifiedFrame!.CalculateChecksum(); |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | public ulong CalculatePredictedChecksum() |
| | | 173 | | { |
| | 2 | 174 | | ThrowIfDisposed(); |
| | 2 | 175 | | EnsureRunning(); |
| | 2 | 176 | | return _predictedFrame!.CalculateChecksum(); |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | public void Dispose() |
| | | 180 | | { |
| | 4 | 181 | | if (_disposed) |
| | | 182 | | { |
| | 0 | 183 | | return; |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | try |
| | | 187 | | { |
| | 4 | 188 | | if (IsRunning && _verifiedFrame != null) |
| | | 189 | | { |
| | 4 | 190 | | _scheduler.Shutdown(_verifiedFrame); |
| | | 191 | | } |
| | 4 | 192 | | } |
| | | 193 | | finally |
| | | 194 | | { |
| | 4 | 195 | | DisposeCommandBuffer(); |
| | | 196 | | |
| | 4 | 197 | | Frame? verified = _verifiedFrame; |
| | 4 | 198 | | Frame? predicted = _predictedFrame; |
| | | 199 | | |
| | 4 | 200 | | _verifiedFrame = null; |
| | 4 | 201 | | _predictedFrame = null; |
| | 4 | 202 | | IsRunning = false; |
| | | 203 | | |
| | 4 | 204 | | verified?.Dispose(); |
| | 4 | 205 | | if (!ReferenceEquals(predicted, verified)) |
| | | 206 | | { |
| | 4 | 207 | | predicted?.Dispose(); |
| | | 208 | | } |
| | | 209 | | |
| | 4 | 210 | | _disposed = true; |
| | 4 | 211 | | } |
| | 4 | 212 | | } |
| | | 213 | | |
| | | 214 | | private Frame CreateFrame(int tick) |
| | | 215 | | { |
| | 4 | 216 | | return new Frame(_options.MaxEntities) |
| | 4 | 217 | | { |
| | 4 | 218 | | Tick = tick, |
| | 4 | 219 | | DeltaTime = _options.DeltaTime |
| | 4 | 220 | | }; |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | private Frame SimulateNext(Frame currentFrame, int nextTick, TInput input) |
| | | 224 | | { |
| | 20 | 225 | | Frame next = currentFrame.Clone(); |
| | 20 | 226 | | next.Tick = nextTick; |
| | 20 | 227 | | next.DeltaTime = _options.DeltaTime; |
| | | 228 | | |
| | 20 | 229 | | PrepareCommandBuffer(next); |
| | | 230 | | try |
| | | 231 | | { |
| | 20 | 232 | | _applyInput?.Invoke(next, input); |
| | 20 | 233 | | _scheduler.Update(next, _options.DeltaTime); |
| | 20 | 234 | | Commands.Buffer.Playback(next); |
| | 20 | 235 | | } |
| | | 236 | | finally |
| | | 237 | | { |
| | 20 | 238 | | Commands.Buffer.Dispose(); |
| | 20 | 239 | | } |
| | | 240 | | |
| | 20 | 241 | | return next; |
| | | 242 | | } |
| | | 243 | | |
| | | 244 | | private void PrepareCommandBuffer(Frame frame) |
| | | 245 | | { |
| | 20 | 246 | | DisposeCommandBuffer(); |
| | 20 | 247 | | Commands.Buffer.Initialize(frame, _options.CommandBufferCapacity); |
| | 20 | 248 | | } |
| | | 249 | | |
| | | 250 | | private void DisposeCommandBuffer() |
| | | 251 | | { |
| | 24 | 252 | | Commands.Buffer.Dispose(); |
| | 24 | 253 | | Commands.Buffer = default; |
| | 24 | 254 | | } |
| | | 255 | | |
| | | 256 | | private TInput ResolveVerifiedInput(int tick) |
| | | 257 | | { |
| | 10 | 258 | | if (_verifiedInputs.TryGet(tick, out TInput input)) |
| | | 259 | | { |
| | 10 | 260 | | return input; |
| | | 261 | | } |
| | | 262 | | |
| | 0 | 263 | | throw new InvalidOperationException($"缺少 tick={tick} 的 verified 输入。"); |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | private TInput ResolveBestInput(int tick) |
| | | 267 | | { |
| | 10 | 268 | | if (_verifiedInputs.TryGet(tick, out TInput verified)) |
| | | 269 | | { |
| | 0 | 270 | | return verified; |
| | | 271 | | } |
| | | 272 | | |
| | 10 | 273 | | if (_predictedInputs.TryGet(tick, out TInput predicted)) |
| | | 274 | | { |
| | 10 | 275 | | return predicted; |
| | | 276 | | } |
| | | 277 | | |
| | 0 | 278 | | return default; |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | private void EnsureRunning() |
| | | 282 | | { |
| | 13 | 283 | | if (!IsRunning || _verifiedFrame == null || _predictedFrame == null) |
| | | 284 | | { |
| | 0 | 285 | | throw new InvalidOperationException("SimulationSession 尚未启动。"); |
| | | 286 | | } |
| | 13 | 287 | | } |
| | | 288 | | |
| | | 289 | | private void ThrowIfDisposed() |
| | | 290 | | { |
| | 36 | 291 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 36 | 292 | | } |
| | | 293 | | |
| | | 294 | | private void ReplaceVerifiedFrame(Frame next) |
| | | 295 | | { |
| | 10 | 296 | | Frame? previous = _verifiedFrame; |
| | 10 | 297 | | _verifiedFrame = next; |
| | 10 | 298 | | previous?.Dispose(); |
| | 10 | 299 | | } |
| | | 300 | | |
| | | 301 | | private void ReplacePredictedFrame(Frame next) |
| | | 302 | | { |
| | 5 | 303 | | Frame? previous = _predictedFrame; |
| | 5 | 304 | | _predictedFrame = next; |
| | 5 | 305 | | previous?.Dispose(); |
| | 5 | 306 | | } |
| | | 307 | | |
| | | 308 | | private sealed class TickBuffer |
| | | 309 | | { |
| | | 310 | | private readonly int[] _ticks; |
| | | 311 | | private readonly TInput[] _values; |
| | | 312 | | |
| | 8 | 313 | | public TickBuffer(int capacity) |
| | | 314 | | { |
| | 8 | 315 | | _ticks = new int[capacity]; |
| | 8 | 316 | | _values = new TInput[capacity]; |
| | | 317 | | |
| | 528 | 318 | | for (int i = 0; i < _ticks.Length; i++) |
| | | 319 | | { |
| | 256 | 320 | | _ticks[i] = int.MinValue; |
| | | 321 | | } |
| | 8 | 322 | | } |
| | | 323 | | |
| | | 324 | | public void Set(int tick, TInput value) |
| | | 325 | | { |
| | 19 | 326 | | int index = GetSlot(tick); |
| | 19 | 327 | | _ticks[index] = tick; |
| | 19 | 328 | | _values[index] = value; |
| | 19 | 329 | | } |
| | | 330 | | |
| | | 331 | | public bool TryGet(int tick, out TInput value) |
| | | 332 | | { |
| | 30 | 333 | | int index = GetSlot(tick); |
| | 30 | 334 | | if (_ticks[index] == tick) |
| | | 335 | | { |
| | 20 | 336 | | value = _values[index]; |
| | 20 | 337 | | return true; |
| | | 338 | | } |
| | | 339 | | |
| | 10 | 340 | | value = default; |
| | 10 | 341 | | return false; |
| | | 342 | | } |
| | | 343 | | |
| | | 344 | | private int GetSlot(int tick) |
| | | 345 | | { |
| | 49 | 346 | | int index = tick % _ticks.Length; |
| | 49 | 347 | | return index < 0 ? index + _ticks.Length : index; |
| | | 348 | | } |
| | | 349 | | } |
| | | 350 | | } |
| | | 351 | | } |