F6: ere e obsolescenza, correzioni della revisione

Due revisori hanno trovato 12 rilievi. I principali:
- Il rinnovo si ricontrolla all'avvio dei lavori: piano, spesa e cassa
  possono cambiare tra prenotazione e avvio. Se la cassa non basta il
  rinnovo si annulla senza pagare e con un avviso, così non va mai in
  scoperto.
- La cassa impegnata dai rinnovi prenotati e non ancora avviati
  (Obsolescence.Committed) conta per il giocatore e per le IA, così la
  stessa cassa non si spende due volte nello stesso mese.
- Le IA comprano il successore solo se non peggiora il treno, e rinnovano
  solo se le prestazioni restano almeno quelle di oggi.
- Dopo un deragliamento la ricostruzione non ringiovanisce nel patrimonio
  un modello fuori produzione.
- Quadro della flotta e pannelli mostrano un rinnovo già prenotato e la
  spesa netta al netto degli altri rinnovi.

Suite completa 210 su 210, client senza avvisi.

Refs #41.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-08 14:22:49 +01:00
1 parent f7c286bf1d
commit b7737455c6
18 files changed
+447 -96

No files matched your search

@@ -196,7 +196,7 @@ internal sealed class AiAgent
var loco = _w.Data.Vehicle(project.Loco);
var car = _w.Data.Vehicle(project.Car);
long price = Estimators.TrainPrice(loco, car, project.CarsPerTrain);
if (_c.Cash.Cents < price + Reserve() / 2 && !Borrow(price + Reserve() / 2 - _c.Cash.Cents, completing: project.Trains.Count == 0)) return false;
if (FreeCash() < price + Reserve() / 2 && !Borrow(price + Reserve() / 2 - FreeCash(), completing: project.Trains.Count == 0)) return false;
var ids = new List<VehicleId>();
var rl = Do(new BuyVehicleCommand(project.Loco, project.Depot, 1));
if (!rl.Ok) return false;
@@ -213,21 +213,27 @@ internal sealed class AiAgent
}
/// <summary>
/// Modelli fuori produzione (ADR 0020): la linea passa al successore consigliato o, se la catena non
/// ne offre, al modello che l'IA sceglierebbe oggi per un progetto nuovo. Così i treni aggiuntivi non
/// chiedono veicoli che non si possono più comprare.
/// Modelli superati o fuori produzione (ADR 0020): la linea passa al successore consigliato appena il
/// modello è superato, se il successore non ha trazione, potenza o velocità inferiori (comprarlo nuovo
/// vorrebbe dire perdere subito valore, pagare di più la manutenzione e rinnovarlo dopo pochi anni)
/// o, se è fuori produzione e la catena non offre un tale successore, al modello che l'IA sceglierebbe
/// oggi per un progetto nuovo. Così i treni aggiuntivi non nascono superati.
/// </summary>
private void UpdateModels(AiProject project)
{
int year = _ctx.Year;
var loco = _w.Data.Vehicle(project.Loco);
if (!Obsolescence.InProduction(loco, year) && (Obsolescence.Recommended(loco, year) ?? Estimators.BestLocomotive(_w, year)) is { } newLoco)
project.Loco = newLoco.Id;
var newLoco = Obsolescence.IsSuperseded(loco, year) && Obsolescence.Recommended(loco, year) is { } rec && Obsolescence.NoWorseThan(rec, loco) ? rec : null;
if (newLoco is null && !Obsolescence.InProduction(loco, year)) newLoco = Estimators.BestLocomotive(_w, year);
if (newLoco is not null) project.Loco = newLoco.Id;
var car = _w.Data.Vehicle(project.Car);
if (Obsolescence.InProduction(car, year)) return;
var newCar = Obsolescence.Recommended(car, year) ?? (car.Kind == VehicleKind.Coach
? Estimators.CoachFor(_w, car.Class ?? PaxClass.Worker, year)
: project.Good >= 0 ? Estimators.WagonFor(_w, project.Good, year) : null);
var newCar = Obsolescence.IsSuperseded(car, year) && Obsolescence.Recommended(car, year) is { } recCar && Obsolescence.NoWorseThan(recCar, car) ? recCar : null;
if (newCar is null && !Obsolescence.InProduction(car, year))
{
newCar = car.Kind == VehicleKind.Coach
? Estimators.CoachFor(_w, car.Class ?? PaxClass.Worker, year)
: project.Good >= 0 ? Estimators.WagonFor(_w, project.Good, year) : null;
}
if (newCar is not null) project.Car = newCar.Id;
}
@@ -308,7 +314,7 @@ internal sealed class AiAgent
sources.Sort((a, b) => a.Km != b.Km ? a.Km.CompareTo(b.Km) : a.Key.CompareTo(b.Key));
var loco = Estimators.BestLocomotive(_w, _ctx.Year);
if (loco is null) return false;
long budget = _c.Cash.Cents - Reserve() / 2 + LoanRoom();
long budget = FreeCash() - Reserve() / 2 + LoanRoom();
int tried = 0;
foreach (var (pos, key, km) in sources)
{
@@ -336,6 +342,12 @@ internal sealed class AiAgent
return false;
}
/// <summary>
/// Cassa libera: la cassa meno la spesa impegnata dai rinnovi prenotati e non ancora avviati, che si
/// paga giorni dopo a officina avviata (ADR 0020). Gli investimenti guardano questa, non la cassa intera.
/// </summary>
private long FreeCash() => _c.Cash.Cents - Obsolescence.Committed(_w, _c.Id).Cents;
/// <summary>Riserva di cassa: mesi di costi d'esercizio stimati dalla flotta e dalla rete.</summary>
private long Reserve()
{
@@ -428,15 +440,16 @@ internal sealed class AiAgent
/// Manda in officina per il rinnovo (intervento «renew», lo stesso comando ScheduleService del
/// giocatore) al più renewal.maxTrainsPerMonth treni in servizio, in ordine di id, che hanno un
/// veicolo superato da almeno renewal.minSupersededYears anni o con almeno renewal.minAgeShareOfLife
/// della vita utile, se dopo la spesa netta (successori − rivendite) la cassa resta sopra
/// renewal.cashReserveMultiple volte la riserva.
/// della vita utile, se il rinnovo non peggiora le prestazioni del treno
/// (<see cref="Obsolescence.KeepsPerformance"/>) e se dopo la spesa (successori − rivendite + lavoro)
/// la cassa libera resta sopra renewal.cashReserveMultiple volte la riserva. La spesa resta impegnata
/// (<see cref="FreeCash"/>) fino all'avvio dei lavori, così gli investimenti dello stesso mese non la riusano.
/// </summary>
private void RenewFleet()
{
var r = _w.Data.Ai.Renewal;
if (r.MaxTrainsPerMonth <= 0) return;
var interventions = _w.Data.Maintenance.Interventions;
string renewal = interventions[interventions.FindIndex(i => i.Effect == InterventionEffect.Renew)].Id;
string renewal = _w.Data.Maintenance.Interventions[Obsolescence.RenewalIntervention(_w.Data)].Id;
double daysPerYear = _w.Calendar.DaysPerYear;
int scheduled = 0;
foreach (var t in _w.Trains)
@@ -456,9 +469,9 @@ internal sealed class AiAgent
break;
}
}
if (!due) continue;
long net = Obsolescence.NetCost(plan).Cents;
if (_c.Cash.Cents - net < (long)(Reserve() * r.CashReserveMultiple)) continue;
if (!due || !Obsolescence.KeepsPerformance(_w, t, plan)) continue;
long net = Obsolescence.RenewalCost(_w, t, plan).Cents;
if (FreeCash() - net < (long)(Reserve() * r.CashReserveMultiple)) continue;
if (Do(new ScheduleServiceCommand(t.Id, renewal)).Ok) scheduled++;
}
}
@@ -474,13 +487,13 @@ internal sealed class AiAgent
if (other.Defunct || other.ControlledBy != _c.Id || other.IsPlayer) continue;
if (_ctx.Day - other.ControlSinceDay < sm.MergeAfterDays) continue;
double minority = (other.Shares.Outstanding - other.Shares.Of(_c.Id)) * other.SharePrice;
if (_c.Cash.Cents > minority + Reserve()) Do(new MergeCompanyCommand(other.Id));
if (FreeCash() > minority + Reserve()) Do(new MergeCompanyCommand(other.Id));
}
if (!_p.BuysShares) return;
// la Predatoria accumula quote dei concorrenti quotati (prima il giocatore) e poi lancia l'offerta;
// solo con una rete che rende e cassa abbondante, e solo in compagnie in utile
if (!_s.Projects.Any(p => p.Stage == AiProjectStage.Running) || _c.AnnualProfit <= 0) return;
long budget = (long)(Math.Max(0, _c.Cash.Cents - 3 * Reserve()) * _mk.SharesCashShare);
long budget = (long)(Math.Max(0, FreeCash() - 3 * Reserve()) * _mk.SharesCashShare);
if (budget <= 0) return;
foreach (var target in _w.Companies.OrderByDescending(t => t.IsPlayer).ThenBy(t => t.Id.Value))
{
@@ -514,7 +527,7 @@ internal sealed class AiAgent
long needed = (long)Math.Ceiling(goal * target.Shares.Outstanding) - target.Shares.Of(_c.Id);
if (needed <= 0) return false;
double cost = (double)needed * price;
if (_c.Cash.Cents < cost * _mk.BidCashMultiple + Reserve()) return false;
if (FreeCash() < cost * _mk.BidCashMultiple + Reserve()) return false;
return Do(new LaunchBidCommand(target.Id, new Money(price), goal)).Ok;
}
@@ -567,7 +580,7 @@ internal sealed class AiAgent
private void BuyIndustries()
{
if (!Tiers.Unlocks(_w, _c).BuyIndustry || _s.LastIndustryYear == _ctx.Year) return;
long budget = (long)(Math.Max(0, _c.Cash.Cents - Reserve()) * _mk.IndustryMaxCashShare);
long budget = (long)(Math.Max(0, FreeCash() - Reserve()) * _mk.IndustryMaxCashShare);
if (budget <= 0) return;
var served = Tiers.ServedStations(_w, _c.Id);
Industry? best = null;
@@ -633,7 +646,7 @@ internal sealed class AiAgent
{
int open = _s.Projects.Count(p => p.Stage is AiProjectStage.Construction or AiProjectStage.Equip);
if (open >= _plan.MaxConcurrentProjects) return;
long budget = _c.Cash.Cents - Reserve() + LoanRoom();
long budget = FreeCash() - Reserve() + LoanRoom();
if (budget <= 0) return;
var loco = Estimators.BestLocomotive(_w, _ctx.Year);
if (loco is null) return;
@@ -1209,7 +1222,7 @@ internal sealed class AiAgent
/// <summary>Apre i cantieri del progetto: stazioni, deposito, binario e raccordo del deposito.</summary>
private void Execute(Proposal p, DepotId? supply = null)
{
long need = p.Capital + Reserve() - _c.Cash.Cents;
long need = p.Capital + Reserve() - FreeCash();
if (need > 0 && !Borrow(need)) return;
var cand = p.Candidate;
var stationA = p.ExistingA;