using System.Diagnostics; using RailEnterprise.Core.Simulation.Ai; using RailEnterprise.Core.Simulation.Systems; namespace RailEnterprise.Core.Simulation; /// /// Sistemi nell'ordine fisso della guida (sezione 4). I sistemi delle fasi successive si /// inseriscono nella loro posizione quando vengono implementati: /// Clock → Commands → Construction → Trains → Signals → Stations (+ trasporto locale) /// → Maintenance → Demand → Commuting → Industries → IndustryMaintenance → Towns → Market /// → Finance → StockMarket → AiAgents → Events → Contracts → Hash. /// public sealed class SystemPipeline(IReadOnlyList systems) { public IReadOnlyList Systems { get; } = systems; /// Se attivo, misura il tempo di ogni sistema (diagnostica e bench). public bool Profile { get; set; } /// Tick totali (Stopwatch) spesi per sistema quando Profile è attivo. public long[] ElapsedTicks { get; } = new long[systems.Count]; /// Byte allocati per sistema quando Profile è attivo. public long[] AllocatedBytes { get; } = new long[systems.Count]; public static SystemPipeline CreateDefault() => new( [ new ClockSystem(), new CommandSystem(), new ConstructionSystem(), new TrainSystem(), new StationSystem(), new LocalTransportSystem(), new FleetMaintenanceSystem(), new DemandSystem(), new EmploymentSystem(), new IndustrySystem(), new IndustryMaintenanceSystem(), new TownSystem(), new MarketSystem(), new FinanceSystem(), new StockMarketSystem(), new AiSystem(), new EventSystem(), new HashSystem(), ]); public void Run(WorldContext ctx) { if (!Profile) { for (int i = 0; i < Systems.Count; i++) Systems[i].Step(ctx); return; } for (int i = 0; i < Systems.Count; i++) { long bytes = GC.GetAllocatedBytesForCurrentThread(); long start = Stopwatch.GetTimestamp(); Systems[i].Step(ctx); ElapsedTicks[i] += Stopwatch.GetTimestamp() - start; AllocatedBytes[i] += GC.GetAllocatedBytesForCurrentThread() - bytes; } } public T Get() where T : ISimSystem => Systems.OfType().First(); }