Aggiunge gli eventi della F6: fatti del mondo che cambiano il gioco per un periodo limitato, leggibili nel notiziario e reversibili. - Catalogo data/events.json con schema e validazione: incidente e boom di un'industria, fiera ed epidemia, boom e crollo della domanda di un bene in una zona, maltempo su un'area, sciopero di città o deposito. - Effetti come moltiplicatori letti da un'unica sorgente (World.WorldEvents) dai sistemi esistenti: capacità delle industrie, consumi delle città, presenze dei pendolari, velocità dei treni e usura del binario. A evento finito il moltiplicatore torna 1. - EventSystem tra le IA e l'hash, con flusso Rng dedicato «events», bersaglio in ordine di id, frequenza per mappa scalata dalla difficoltà (eventFrequencyMultiplier), tetto di 6, niente doppioni, notizie di inizio e fine in italiano (EventKind.World). - Salvataggio corpo versione 5 con gli eventi nell'hash; le versioni 1-4 si caricano senza eventi e con la frequenza del preset. - Protocollo: eventi in corso nel DailyDigest e nel MarketReport; client con il riquadro «Eventi in corso», avvisi nelle schede e «evento in corso» nel Mercato globale. - Lo scenario Valdoria disattiva gli eventi: con gli eventi attivi la flotta F4 senza linea dei ricambi non scendeva più sotto 40, e l'accettazione deve misurare la manutenzione, non la fortuna (ADR 0017). Accettazione F4 invariata: 35 / 20 guasti senza linea, 75 / 2 con. Decisioni in docs/adr/0017-eventi-del-mondo.md; 17 test nuovi. Refs #33. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
231 lines
9.8 KiB
C#
231 lines
9.8 KiB
C#
using RailEnterprise.Core.Common;
|
|
using RailEnterprise.Core.Data;
|
|
using RailEnterprise.Core.Economy;
|
|
|
|
namespace RailEnterprise.Core.Simulation.Systems;
|
|
|
|
/// <summary>
|
|
/// Produzione giornaliera delle industrie (guida 6.3), esaurimento dei giacimenti, salari,
|
|
/// crescita trimestrale delle industrie servite, chiusure e nascita di nuove industrie.
|
|
/// </summary>
|
|
public sealed class IndustrySystem : ISimSystem
|
|
{
|
|
public string Name => "Industries";
|
|
|
|
public void Step(WorldContext ctx)
|
|
{
|
|
if (!ctx.IsNewDay) return;
|
|
var w = ctx.World;
|
|
foreach (var ind in w.Industries)
|
|
{
|
|
if (!ind.Closed) Produce(ctx, ind);
|
|
}
|
|
if (ctx.IsNewQuarter && ctx.Day > 0) Quarter(ctx);
|
|
if (ctx.IsNewYear) w.SpawnedThisYear = 0;
|
|
SpawnForUnmetDemand(ctx);
|
|
}
|
|
|
|
private static void Produce(WorldContext ctx, Industry ind)
|
|
{
|
|
var w = ctx.World;
|
|
var data = w.Data;
|
|
var ib = data.Balance.Industry;
|
|
int dpm = w.Calendar.DaysPerMonth;
|
|
var recipe = ind.Def.Recipe;
|
|
|
|
// coperture di lavoratori e personale (EMA su 30 giorni)
|
|
double covW = ind.JobsWorkers > 0 ? ind.WorkersPresent / ind.JobsWorkers : 1.0;
|
|
double covS = ind.JobsStaff > 0 ? ind.StaffPresent / ind.JobsStaff : 1.0;
|
|
ind.CovWorkers = Ema.Step(ind.CovWorkers, covW, ib.CoverageWindowDays);
|
|
ind.CovStaff = Ema.Step(ind.CovStaff, covS, ib.CoverageWindowDays);
|
|
|
|
double pf = IndustryMath.ProfitFactor(ind, ib);
|
|
double mc = IndustryMath.ConditionMultiplier(ind.Condition, data.Balance.IndustryMaintenance);
|
|
double eLabor = Math.Min(ind.CovWorkers, ind.CovStaff) * pf * mc;
|
|
// fermo per guasto, incidente, revisione o ammodernamento: nessuna produzione (sezione 11)
|
|
bool stopped = ind.IsStopped(ctx.Day);
|
|
if (stopped) eLabor = 0;
|
|
|
|
// giacimento: sotto la soglia la capacità cala linearmente, a zero l'industria chiude
|
|
double depletion = 1.0;
|
|
ResourceSite? site = ind.Site.IsValid ? w.Sites[ind.Site.Value] : null;
|
|
if (ind.Def.Depletable && site is not null)
|
|
{
|
|
double frac = site.ReserveFraction;
|
|
if (frac <= 0)
|
|
{
|
|
Close(ctx, ind, $"{ind.Name}: giacimento esaurito, l'attività chiude");
|
|
return;
|
|
}
|
|
if (frac < ib.DepletionStartFraction) depletion = frac / ib.DepletionStartFraction;
|
|
}
|
|
|
|
// eventi del mondo: incidente (capacità ridotta o nulla) e boom di produzione (ADR 0017)
|
|
double capacity = w.WorldEvents.IndustryCapacity(ind.Id);
|
|
double cycles = ind.CyclesPerDayAtFullCapacity(dpm) * eLabor * depletion * capacity;
|
|
for (int o = 0; o < recipe.Outputs.Count && !stopped; o++)
|
|
w.Market.CapacityToday[recipe.Outputs[o].GoodIndex] += ind.CyclesPerDayAtFullCapacity(dpm) * depletion * capacity * recipe.Outputs[o].PerCycle;
|
|
|
|
// copertura degli input di oggi: limita la produzione alle scorte disponibili
|
|
double covInToday = 1.0;
|
|
for (int i = 0; i < recipe.Inputs.Count; i++)
|
|
{
|
|
double need = cycles * recipe.Inputs[i].PerCycle;
|
|
if (need > 0) covInToday = Math.Min(covInToday, ind.InputStock[i] / need);
|
|
}
|
|
covInToday = Math.Min(1.0, covInToday);
|
|
ind.CovInput = Ema.Step(ind.CovInput, recipe.Inputs.Count == 0 ? 1.0 : covInToday, ib.CoverageWindowDays);
|
|
|
|
double total = cycles * covInToday + ind.CycleAccumulator;
|
|
long whole = (long)Math.Floor(total);
|
|
for (int i = 0; i < recipe.Inputs.Count; i++)
|
|
whole = Math.Min(whole, ind.InputStock[i] / recipe.Inputs[i].PerCycle);
|
|
ind.CycleAccumulator = Math.Clamp(total - whole, 0, 1);
|
|
|
|
for (int i = 0; i < recipe.Inputs.Count; i++)
|
|
{
|
|
int used = (int)(whole * recipe.Inputs[i].PerCycle);
|
|
ind.InputStock[i] -= used;
|
|
w.Goods.Consumed[recipe.Inputs[i].GoodIndex] += used;
|
|
}
|
|
for (int o = 0; o < recipe.Outputs.Count; o++)
|
|
{
|
|
int good = recipe.Outputs[o].GoodIndex;
|
|
int made = (int)(whole * recipe.Outputs[o].PerCycle);
|
|
ind.ProducedToday[o] = made;
|
|
ind.OutputStock[o] += made;
|
|
w.Goods.Produced[good] += made;
|
|
w.Market.SupplyToday[good] += made;
|
|
|
|
// oltre la capienza del magazzino (storageDays) la merce si perde
|
|
int cap = IndustryMath.OutputCapacity(ind, o, dpm);
|
|
if (ind.OutputStock[o] > cap)
|
|
{
|
|
int lost = ind.OutputStock[o] - cap;
|
|
ind.OutputStock[o] = cap;
|
|
w.Goods.Decayed[good] += lost;
|
|
}
|
|
}
|
|
if (site is not null && ind.Def.Depletable)
|
|
{
|
|
double extracted = whole * recipe.Outputs[0].PerCycle * data.Difficulty.DepletionMultipliers.For(w.Difficulty.Depletion);
|
|
site.ReserveTonnes = Math.Max(0, site.ReserveTonnes - extracted);
|
|
}
|
|
ind.Efficiency = eLabor * covInToday * depletion * capacity;
|
|
|
|
// salari giornalieri ai presenti
|
|
double wages = (ind.WorkersPresent * ib.WagePerWorkerPerMonth + ind.StaffPresent * ib.WagePerStaffPerMonth) / dpm;
|
|
var wageMoney = Money.FromCentsRounded(wages);
|
|
w.Money.Transfer(Account.Of(ind.Id), Account.External, wageMoney, LedgerEntry.Wages);
|
|
|
|
ind.RevenueEma = Ema.Step(ind.RevenueEma, ind.SalesToday, ib.MarginWindowDays);
|
|
ind.CostEma = Ema.Step(ind.CostEma, wageMoney.Cents + ind.PurchasesToday, ib.MarginWindowDays);
|
|
ind.SalesToday = 0;
|
|
ind.PurchasesToday = 0;
|
|
|
|
// statistiche del trimestre
|
|
ind.QuarterProduced += whole * recipe.Outputs[0].PerCycle;
|
|
ind.QuarterShipped += ind.ShippedToday;
|
|
ind.ShippedToday = 0;
|
|
ind.QuarterCoverageSum += Math.Min(ind.CovInput, Math.Min(ind.CovWorkers, ind.CovStaff));
|
|
ind.QuarterDays++;
|
|
}
|
|
|
|
private static void Quarter(WorldContext ctx)
|
|
{
|
|
var w = ctx.World;
|
|
var g = w.Data.Balance.Growth;
|
|
foreach (var ind in w.Industries)
|
|
{
|
|
if (ind.Closed) continue;
|
|
double shipped = ind.QuarterProduced > 0 ? Math.Min(1.0, (double)ind.QuarterShipped / ind.QuarterProduced) : 0;
|
|
double coverage = ind.QuarterDays > 0 ? ind.QuarterCoverageSum / ind.QuarterDays : 0;
|
|
double profit = Math.Clamp((ind.Margin + 1) / 2, 0, 1);
|
|
double score = g.WeightShipped * shipped + g.WeightCoverage * coverage + g.WeightProfit * profit;
|
|
ind.LastScore = score;
|
|
ind.QuarterProduced = 0;
|
|
ind.QuarterShipped = 0;
|
|
ind.QuarterCoverageSum = 0;
|
|
ind.QuarterDays = 0;
|
|
|
|
// solo le industrie servite da un vettore crescono o regrediscono (ADR 0004)
|
|
if (!ind.ServedWithin(ctx.Day, g.QuarterDays)) continue;
|
|
|
|
if (ind.Level == 0)
|
|
{
|
|
ind.QuartersAtZero++;
|
|
if (ind.QuartersAtZero >= g.CloseQuartersAtZero)
|
|
{
|
|
Close(ctx, ind, $"{ind.Name} chiude dopo un anno di inattività");
|
|
continue;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ind.QuartersAtZero = 0;
|
|
}
|
|
|
|
if (score > g.UpgradeThreshold)
|
|
{
|
|
ind.QuartersAboveUpgrade++;
|
|
if (ind.QuartersAboveUpgrade >= g.UpgradeQuarters && ind.Level < g.MaxLevel)
|
|
{
|
|
ind.Level++;
|
|
ind.QuartersAboveUpgrade = 0;
|
|
ctx.Emit(EventKind.Industry, EventSeverity.Opportunity, $"{ind.Name} si espande al livello {ind.Level}", EntityKind.Industry, ind.Id.Value);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ind.QuartersAboveUpgrade = 0;
|
|
if (score < g.DowngradeThreshold && ind.Level > 0)
|
|
{
|
|
ind.Level--;
|
|
ctx.Emit(EventKind.Industry, EventSeverity.Warning, $"{ind.Name} riduce l'attività (livello {ind.Level})", EntityKind.Industry, ind.Id.Value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Chiude un'industria: le scorte si perdono, l'impronta torna libera.</summary>
|
|
public static void Close(WorldContext ctx, Industry ind, string reason)
|
|
{
|
|
var w = ctx.World;
|
|
ind.Closed = true;
|
|
ind.Level = 0;
|
|
var recipe = ind.Def.Recipe;
|
|
for (int i = 0; i < recipe.Inputs.Count; i++)
|
|
{
|
|
w.Goods.Decayed[recipe.Inputs[i].GoodIndex] += ind.InputStock[i];
|
|
ind.InputStock[i] = 0;
|
|
}
|
|
for (int o = 0; o < recipe.Outputs.Count; o++)
|
|
{
|
|
w.Goods.Decayed[recipe.Outputs[o].GoodIndex] += ind.OutputStock[o];
|
|
ind.OutputStock[o] = 0;
|
|
}
|
|
IndustryPlacer.ClearFootprint(w, ind);
|
|
w.Commutes.RemoveAll(c => c.Workplace.Kind == WorkplaceKind.Industry && c.Workplace.Index == ind.Id.Value);
|
|
ctx.Emit(EventKind.Industry, EventSeverity.Warning, reason, EntityKind.Industry, ind.Id.Value);
|
|
}
|
|
|
|
private static void SpawnForUnmetDemand(WorldContext ctx)
|
|
{
|
|
var w = ctx.World;
|
|
var g = w.Data.Balance.Growth;
|
|
if (w.SpawnedThisYear >= g.SpawnMaxPerYear) return;
|
|
for (int good = 0; good < w.Data.GoodCount; good++)
|
|
{
|
|
if (w.Market.UnmetDays[good] < g.SpawnUnmetDays) continue;
|
|
w.Market.UnmetDays[good] = 0;
|
|
var ind = IndustryPlacer.TrySpawnProducer(w, good, ctx.Year);
|
|
if (ind is null) continue;
|
|
w.SpawnedThisYear++;
|
|
ctx.Emit(EventKind.Industry, EventSeverity.Opportunity,
|
|
$"Apre {ind.Name}: la domanda di {w.Data.Goods[good].NameIt.ToLowerInvariant()} è rimasta insoddisfatta per un anno",
|
|
EntityKind.Industry, ind.Id.Value);
|
|
if (w.SpawnedThisYear >= g.SpawnMaxPerYear) return;
|
|
}
|
|
}
|
|
}
|