using RailEnterprise.Core.Common;
using RailEnterprise.Core.Data;
using RailEnterprise.Core.Economy;
namespace RailEnterprise.Core.Simulation;
///
/// Stimatori economici condivisi da IA e tooltip «perché» (12.1): ricavi per unità e per viaggio,
/// tempi di giro, costi di costruzione ed esercizio. Leggono solo informazioni pubbliche.
///
public static class Estimators
{
/// Ricavo per unità di merce su al prezzo globale corrente (centesimi).
public static double FreightPerUnit(World w, int good, double km)
{
var t = w.Data.Balance.Tariffs;
double priceFactor = t.FreightFixedWeight + t.FreightPriceWeight * w.Market.GlobalPrice[good] / w.Data.Goods[good].BasePrice;
return t.FreightBaseRate * priceFactor * DMath.Pow(Math.Max(km, 0.1), t.FreightKmExponent) * w.Difficulty.TariffMultiplier;
}
/// Ricavo per passeggero su nella classe indicata (centesimi).
public static double PassengerPerTrip(World w, PaxClass cls, double km)
{
var t = w.Data.Balance.Tariffs;
double mult = t.ClassMultipliers[cls == PaxClass.Staff ? "staff" : "worker"];
return t.PaxBaseFare * DMath.Pow(Math.Max(km, 0.1), t.PaxKmExponent) * mult * w.Difficulty.TariffMultiplier;
}
/// Durata di un giro completo (andata e ritorno con le soste) in giorni di calendario.
public static double RoundTripDays(World w, double km, double maxSpeedKmh)
{
var p = w.Data.Ai.Planning;
double minutes = 2 * km / Math.Max(1, maxSpeedKmh * p.SpeedFactor) * 60 + 2 * p.DwellMinutes;
return w.Calendar.TrainMinutesToDays(minutes);
}
/// Costo d'esercizio annuo di un treno con la manutenzione stimata.
public static double TrainCostPerYear(World w, VehicleDef loco, VehicleDef car, int cars) =>
(loco.RunningCostPerYear + car.RunningCostPerYear * cars) * (1 + w.Data.Ai.Planning.MaintenanceCostShare);
public static long TrainPrice(VehicleDef loco, VehicleDef car, int cars) => loco.Price + car.Price * cars;
/// Costo di una stazione con i binari indicati per la compagnia.
public static long StationCost(World w, Company c, int platforms) =>
(long)((w.Data.Construction.Costs.StationBase + w.Data.Construction.Costs.StationPerPlatform * platforms) * RailCommandHandlers.CostMultiplier(w, c));
public static long DepotCost(World w, Company c, string size) =>
(long)(w.Data.Construction.Costs.DepotBySize[size] * RailCommandHandlers.CostMultiplier(w, c));
/// Costo di massima di un binario in linea d'aria (senza opere), per ordinare i candidati.
public static long RoughTrackCost(World w, Company c, double km) =>
(long)(w.Data.Construction.Costs.BasePerMeter * 1000 * km * RailCommandHandlers.CostMultiplier(w, c));
/// Locomotiva più recente non elettrica disponibile nell'anno.
public static VehicleDef? BestLocomotive(World w, int year)
{
VehicleDef? best = null;
foreach (var v in w.Data.Vehicles)
{
if (v.Kind != VehicleKind.Locomotive || v.EraFrom > year || (v.EraTo is { } to && year > to)) continue;
if (v.Traction is Traction.Electric or Traction.HighSpeed) continue;
if (best is null || v.EraFrom > best.EraFrom) best = v;
}
return best;
}
/// Carro più economico che trasporta il bene nell'anno.
public static VehicleDef? WagonFor(World w, int good, int year)
{
VehicleDef? best = null;
foreach (var v in w.Data.Vehicles)
{
if (v.Kind != VehicleKind.Wagon || v.EraFrom > year || (v.EraTo is { } to && year > to)) continue;
if (Array.IndexOf(v.GoodIndices, good) < 0) continue;
if (best is null || v.Price < best.Price) best = v;
}
return best;
}
/// Carrozza della classe indicata disponibile nell'anno.
public static VehicleDef? CoachFor(World w, PaxClass cls, int year)
{
VehicleDef? best = null;
foreach (var v in w.Data.Vehicles)
{
if (v.Kind != VehicleKind.Coach || v.Class != cls || v.EraFrom > year || (v.EraTo is { } to && year > to)) continue;
if (best is null || v.Price < best.Price) best = v;
}
return best;
}
}