using System.Diagnostics;
using ViaFerrea.Core.Simulation.Systems;
namespace ViaFerrea.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];
public static SystemPipeline CreateDefault() => new(
[
new ClockSystem(),
new CommandSystem(),
new ConstructionSystem(),
new TrainSystem(),
new StationSystem(),
new LocalTransportSystem(),
new DemandSystem(),
new EmploymentSystem(),
new IndustrySystem(),
new TownSystem(),
new MarketSystem(),
new FinanceSystem(),
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 start = Stopwatch.GetTimestamp();
Systems[i].Step(ctx);
ElapsedTicks[i] += Stopwatch.GetTimestamp() - start;
}
}
public T Get() where T : ISimSystem => Systems.OfType().First();
}