- Abbinamento mensile dei pendolari sul LineGraph (60/90 minuti con i tratti a piedi, quote proporzionali ai lavoratori liberi, capacità residua delle linee), pacchetti giornalieri, coperture e scioglimento dei contratti. - LineGraph con maschere dei carichi: merci, lavoratori e personale viaggiano solo su linee i cui treni li possono portare; durata dei giri con le soste di carico e scarico. - Industria servita = merce ritirata o consegnata dal vettore. - Parametri residui spostati in data/*.json. - Test di accettazione F2 sullo scenario Valdoria con tre linee, test del pendolarismo (copertura > 0,9 in 60 giorni, capacità, scioglimento). - viaferrea-tools bench: 2.000 treni, tick medio 0,84 ms. - ADR 0007 e 0008, ARCHITECTURE e STATE aggiornati. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0191R7vXgtbAvSV4V8tj8uzL
208 lines
8.4 KiB
C#
208 lines
8.4 KiB
C#
using ViaFerrea.Core.Common;
|
|
using ViaFerrea.Core.Economy;
|
|
using ViaFerrea.Core.Rail;
|
|
|
|
namespace ViaFerrea.Core.Simulation.Systems;
|
|
|
|
/// <summary>
|
|
/// Cantieri (guida 8.2) e usura dei binari (8.3). Ogni giorno le squadre libere di ogni compagnia
|
|
/// prendono i progetti in coda nell'ordine di apertura; un progetto avanza di un giorno per giorno
|
|
/// di lavoro. Poi si applica l'usura del traffico, la manutenzione ordinaria e l'invecchiamento
|
|
/// di ponti e gallerie.
|
|
/// </summary>
|
|
public sealed class ConstructionSystem : ISimSystem
|
|
{
|
|
public string Name => "Construction";
|
|
|
|
public void Step(WorldContext ctx)
|
|
{
|
|
if (!ctx.IsNewDay) return;
|
|
var w = ctx.World;
|
|
Projects(ctx, w);
|
|
Wear(ctx, w);
|
|
}
|
|
|
|
/// <summary>Squadre disponibili per una compagnia secondo il livello.</summary>
|
|
public static int Crews(World w, Company c)
|
|
{
|
|
var byTier = w.Data.Construction.Crews.CrewsByTier;
|
|
return byTier[Math.Clamp(c.Tier, 1, byTier.Length) - 1];
|
|
}
|
|
|
|
private static void Projects(WorldContext ctx, World w)
|
|
{
|
|
var net = w.Network;
|
|
foreach (var company in w.Companies)
|
|
{
|
|
int crews = Crews(w, company);
|
|
int busy = 0;
|
|
foreach (var p in net.Projects)
|
|
{
|
|
if (!p.Done && p.Owner == company.Id && p.Crew >= 0) busy++;
|
|
}
|
|
foreach (var p in net.Projects)
|
|
{
|
|
if (p.Done || p.Owner != company.Id || p.Crew >= 0) continue;
|
|
if (busy >= crews) break;
|
|
p.Crew = FreeCrew(net, company.Id, crews);
|
|
p.StartedDay = ctx.Day;
|
|
busy++;
|
|
}
|
|
}
|
|
foreach (var p in net.Projects)
|
|
{
|
|
if (p.Done || p.Crew < 0) continue;
|
|
p.ProgressDays += 1;
|
|
if (p.ProgressDays + 1e-9 < p.TotalDays) continue;
|
|
Complete(ctx, w, p);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Completa subito tutti i cantieri aperti. Solo per gli strumenti (banco di prova, collaudi):
|
|
/// in partita i cantieri avanzano con le squadre.
|
|
/// </summary>
|
|
public static void CompleteAllNow(World w)
|
|
{
|
|
foreach (var p in w.Network.Projects)
|
|
{
|
|
if (p.Done) continue;
|
|
p.ProgressDays = p.TotalDays;
|
|
Complete(w.Context, w, p);
|
|
}
|
|
}
|
|
|
|
private static int FreeCrew(RailNetwork net, CompanyId company, int crews)
|
|
{
|
|
for (int c = 0; c < crews; c++)
|
|
{
|
|
bool used = false;
|
|
foreach (var p in net.Projects)
|
|
{
|
|
if (!p.Done && p.Owner == company && p.Crew == c) { used = true; break; }
|
|
}
|
|
if (!used) return c;
|
|
}
|
|
return crews - 1;
|
|
}
|
|
|
|
private static void Complete(WorldContext ctx, World w, ConstructionProject p)
|
|
{
|
|
var net = w.Network;
|
|
p.Done = true;
|
|
p.Crew = -1;
|
|
foreach (var id in p.Edges)
|
|
{
|
|
var e = net.Edges[id.Value];
|
|
if (e.State == EdgeState.Demolished) continue;
|
|
e.Project = -1;
|
|
switch (p.Kind)
|
|
{
|
|
case ProjectKind.NewTrack:
|
|
e.BuiltDay = ctx.Day;
|
|
e.LastRenewalDay = ctx.Day;
|
|
e.LastInspectionDay = ctx.Day;
|
|
break;
|
|
case ProjectKind.Renewal:
|
|
e.Condition = 100;
|
|
e.StructureCondition = 100;
|
|
e.LastRenewalDay = ctx.Day;
|
|
e.LastInspectionDay = ctx.Day;
|
|
break;
|
|
case ProjectKind.Upgrade:
|
|
ApplyUpgrade(w, e, p);
|
|
break;
|
|
}
|
|
e.State = EdgeState.Active;
|
|
e.ResetBlocks();
|
|
}
|
|
net.Touch();
|
|
string what = p.Kind switch
|
|
{
|
|
ProjectKind.NewTrack => "Binario completato",
|
|
ProjectKind.Renewal => "Rinnovo completato",
|
|
_ => "Ammodernamento completato",
|
|
};
|
|
double km = p.Edges.Sum(id => net.Edges[id.Value].Length) / 1000;
|
|
ctx.World.Events.Add(new GameEvent(ctx.Day, EventKind.Construction, EventSeverity.Info,
|
|
$"{what}: {km:F1} km in {p.ProgressDays:F0} giorni", EntityKind.None, -1, p.Owner));
|
|
}
|
|
|
|
private static void ApplyUpgrade(World w, TrackEdge e, ConstructionProject p)
|
|
{
|
|
var classes = w.Data.Eras.TrackClasses;
|
|
double oldSpeed = classes[e.TrackClass].SpeedKmh;
|
|
double newSpeed = classes[p.UpgradeClass].SpeedKmh;
|
|
for (int i = 0; i < e.SpeedLimitKmh.Length; i++)
|
|
{
|
|
// i tratti limitati dalle curve restano limitati dal raggio
|
|
e.SpeedLimitKmh[i] = e.SpeedLimitKmh[i] < oldSpeed - 1e-9 ? Math.Min(newSpeed, e.SpeedLimitKmh[i]) : newSpeed;
|
|
}
|
|
e.TrackClass = p.UpgradeClass;
|
|
e.DoubleTrack = p.UpgradeDouble;
|
|
e.Electrified = p.UpgradeElectrified;
|
|
e.Condition = 100;
|
|
}
|
|
|
|
private static void Wear(WorldContext ctx, World w)
|
|
{
|
|
var tw = w.Data.Construction.TrackWear;
|
|
var st = w.Data.Construction.Structures;
|
|
int daysPerYear = w.Calendar.DaysPerYear;
|
|
foreach (var e in w.Network.Edges)
|
|
{
|
|
if (e.State is EdgeState.Demolished or EdgeState.UnderConstruction)
|
|
{
|
|
e.WearToday = 0;
|
|
continue;
|
|
}
|
|
var owner = w.Companies[e.Owner.Value];
|
|
double before = e.Condition;
|
|
double wear = e.WearToday / tw.K * w.Difficulty.WearMultiplier;
|
|
e.TonnesThisYear += e.WearToday;
|
|
e.WearToday = 0;
|
|
// la manutenzione ordinaria pagata annulla la deriva naturale; senza (bancarotta) il binario deperisce
|
|
if (owner.Bankrupt) wear += tw.MaintenanceDriftPerDay;
|
|
else
|
|
{
|
|
var cost = Money.FromCentsRounded(e.Length / 1000 * tw.MaintenanceCostPerKmPerDay * (e.DoubleTrack ? 2 : 1));
|
|
w.Money.Transfer(Account.Of(owner.Id), Account.External, cost, LedgerEntry.TrackMaintenance);
|
|
}
|
|
e.Condition = Math.Max(0, e.Condition - wear);
|
|
if (before >= 60 && e.Condition < 60)
|
|
w.Events.Add(new GameEvent(ctx.Day, EventKind.Maintenance, EventSeverity.Warning,
|
|
$"Binario {e.Id.Value}: condizione sotto 60, velocità ridotta al {tw.LimitBelow60 * 100:F0}%", EntityKind.None, e.Id.Value, owner.Id));
|
|
|
|
// strutture: ispezione annuale obbligatoria e invecchiamento oltre la vita utile
|
|
double structures = e.LengthOf(WorkKind.Bridge) + e.LengthOf(WorkKind.Tunnel);
|
|
if (structures <= 0) continue;
|
|
if (ctx.Day - e.LastInspectionDay >= st.InspectionIntervalDays && !owner.Bankrupt)
|
|
{
|
|
double share = structures / Math.Max(1, e.Length);
|
|
var cost = Money.FromCentsRounded(e.BuildCostCents * share * st.InspectionCostShare);
|
|
w.Money.Transfer(Account.Of(owner.Id), Account.External, cost, LedgerEntry.TrackMaintenance);
|
|
e.LastInspectionDay = ctx.Day;
|
|
}
|
|
double age = (ctx.Day - e.LastRenewalDay) / (double)daysPerYear;
|
|
double structureBefore = e.StructureCondition;
|
|
if (ctx.Day - e.LastInspectionDay > st.InspectionIntervalDays) e.StructureCondition -= st.MissedInspectionWearPerDay;
|
|
if (age > st.RenewalYears) e.StructureCondition -= 100.0 / (st.AgingYearsToZero * daysPerYear);
|
|
e.StructureCondition = Math.Max(0, e.StructureCondition);
|
|
if (structureBefore >= st.CloseBelow && e.StructureCondition < st.CloseBelow)
|
|
w.Events.Add(new GameEvent(ctx.Day, EventKind.Maintenance, EventSeverity.Critical,
|
|
$"Binario {e.Id.Value}: ponti e gallerie da rinnovare, velocità minima", EntityKind.None, e.Id.Value, owner.Id));
|
|
}
|
|
}
|
|
|
|
/// <summary>Fattore di velocità dovuto alla condizione di binario e strutture (8.3).</summary>
|
|
public static double ConditionFactor(World w, TrackEdge e)
|
|
{
|
|
var tw = w.Data.Construction.TrackWear;
|
|
var st = w.Data.Construction.Structures;
|
|
double f = e.Condition < 30 ? tw.LimitBelow30 : e.Condition < 60 ? tw.LimitBelow60 : 1.0;
|
|
if (e.StructureCondition < st.SpeedLimitBelow) f = Math.Min(f, st.SpeedLimitFactor);
|
|
if (e.StructureCondition < st.CloseBelow) f = Math.Min(f, st.ClosedSpeedFactor);
|
|
return f;
|
|
}
|
|
}
|