Allinea i giorni degli eventi a quelli annunciati, toglie le sovrapposizioni
di zona e impedisce che l'IA trasformi un effetto temporaneo in un
investimento permanente.
- Un evento è in vigore nei giorni [inizio, fine) per tutti: le
interrogazioni di WorldEventState contano solo gli eventi in vigore nel
giorno dell'orologio, l'usura conta il maltempo del giorno del traffico.
Il maltempo non rallenta più i treni il giorno dell'estrazione e agisce
per intero l'ultimo giorno; l'evento esce dall'elenco il mattino del
primo giorno libero, con la notizia di fine. {until} è l'ultimo giorno.
- Niente zone sovrapposte tra due maltempi o tra due eventi sulla domanda
dello stesso bene (né due boom né boom e crollo insieme): nessuna città
e nessun arco ricevono due volte lo stesso effetto.
- IA: stime dei candidati sulla domanda senza eventi, sui posti scoperti
senza le assenze di epidemie e scioperi (PresentWithoutEvents) e
sull'efficienza senza il moltiplicatore di capacità (ADR 0017 §9).
- Client: conto alla rovescia dalla durata a 1 e «da domani» per gli
eventi estratti oggi; avviso anche nelle città della zona e nel Registro
officina del deposito in sciopero; dal resoconto il notiziario prende
solo le notizie di fine, le altre arrivano già come Notification.
ADR 0017, STATE, ARCHITECTURE e PROTOCOL aggiornati; 2 test nuovi e
3 adattati alla nuova finestra.
Refs #33.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
217 lines
7.7 KiB
C#
217 lines
7.7 KiB
C#
using RailEnterprise.Core.Common;
|
||
using RailEnterprise.Core.Data;
|
||
using RailEnterprise.Core.Economy;
|
||
using RailEnterprise.Core.Rail;
|
||
using RailEnterprise.Core.Time;
|
||
|
||
namespace RailEnterprise.Core.Simulation;
|
||
|
||
/// <summary>
|
||
/// Evento del mondo in corso (ADR 0017): tipo del catalogo, bersaglio, bene (per la domanda di un
|
||
/// bene), giorno d'inizio e di fine e intensità in [0, 1] da cui si ricavano i moltiplicatori.
|
||
/// </summary>
|
||
public sealed class ActiveWorldEvent
|
||
{
|
||
public required int Id { get; init; }
|
||
public required WorldEventDef Def { get; init; }
|
||
/// <summary>Entità bersaglio: industria, città (anche centro di zona e area) o deposito.</summary>
|
||
public required EntityKind TargetKind { get; init; }
|
||
public required int Target { get; init; }
|
||
/// <summary>Bene coinvolto (TownGood), altrimenti −1.</summary>
|
||
public int Good { get; init; } = -1;
|
||
public required long StartDay { get; init; }
|
||
/// <summary>Primo giorno senza l'evento: dura EndDay − StartDay giorni.</summary>
|
||
public required long EndDay { get; init; }
|
||
public required double Intensity { get; init; }
|
||
|
||
/// <summary>Centro della zona in metri (città bersaglio), per area e domanda di un bene.</summary>
|
||
public Vec2 Center { get; init; }
|
||
|
||
/// <summary>Quadrato del raggio della zona in metri (0 senza zona).</summary>
|
||
public double RadiusSquaredM { get; init; }
|
||
|
||
public int DurationDays => (int)(EndDay - StartDay);
|
||
|
||
/// <summary>Vero se l'evento è in vigore nel giorno indicato: [StartDay, EndDay).</summary>
|
||
public bool InForce(long day) => StartDay <= day && day < EndDay;
|
||
|
||
/// <summary>Moltiplicatore dell'effetto indicato (1 se il tipo non ce l'ha).</summary>
|
||
public double Effect(EventEffectKind kind)
|
||
{
|
||
foreach (var e in Def.Effects)
|
||
{
|
||
if (e.Kind == kind) return e.At(Intensity);
|
||
}
|
||
return 1.0;
|
||
}
|
||
|
||
public bool Has(EventEffectKind kind)
|
||
{
|
||
foreach (var e in Def.Effects)
|
||
{
|
||
if (e.Kind == kind) return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/// <summary>Moltiplicatore dell'effetto principale (il primo del catalogo).</summary>
|
||
public double Primary => Def.Effects[0].At(Intensity);
|
||
|
||
public bool InZone(Vec2 p) => RadiusSquaredM > 0 && (p - Center).LengthSquared <= RadiusSquaredM;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Eventi del mondo in corso: unica sorgente dei moltiplicatori temporanei letti dai sistemi. Ogni
|
||
/// interrogazione restituisce il prodotto degli effetti attivi sull'entità (1 senza eventi), così a
|
||
/// evento scaduto tutto torna come prima. L'elenco è in ordine d'inizio (id crescente) e contiene
|
||
/// gli eventi dall'estrazione fino al mattino del primo giorno libero: le interrogazioni contano solo
|
||
/// quelli in vigore nel giorno dell'orologio (<see cref="ActiveWorldEvent.InForce"/>), così un evento
|
||
/// estratto oggi agisce da domani anche sui treni e l'ultimo giorno agisce per intero (ADR 0017 §5).
|
||
/// </summary>
|
||
public sealed class WorldEventState(GameClock clock)
|
||
{
|
||
private readonly List<ActiveWorldEvent> _active = [];
|
||
private int _areaEvents;
|
||
|
||
private long Today => clock.Day;
|
||
|
||
public IReadOnlyList<ActiveWorldEvent> Active => _active;
|
||
|
||
/// <summary>Prossimo id libero (mai riutilizzato).</summary>
|
||
public int NextId { get; set; }
|
||
|
||
internal void Add(ActiveWorldEvent e)
|
||
{
|
||
_active.Add(e);
|
||
Recount();
|
||
}
|
||
|
||
internal void RemoveAt(int index)
|
||
{
|
||
_active.RemoveAt(index);
|
||
Recount();
|
||
}
|
||
|
||
internal void Clear()
|
||
{
|
||
_active.Clear();
|
||
Recount();
|
||
}
|
||
|
||
private void Recount()
|
||
{
|
||
_areaEvents = 0;
|
||
foreach (var e in _active)
|
||
{
|
||
if (e.Def.Target == EventTarget.Area) _areaEvents++;
|
||
}
|
||
}
|
||
|
||
/// <summary>Vero se un evento dello stesso tipo è già in corso sullo stesso bersaglio.</summary>
|
||
public bool IsActive(WorldEventDef def, EntityKind kind, int target)
|
||
{
|
||
foreach (var e in _active)
|
||
{
|
||
if (e.Def == def && e.TargetKind == kind && e.Target == target) return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/// <summary>Vero se un evento in elenco (anche non ancora iniziato) ha l'effetto indicato.</summary>
|
||
public bool AnyWith(EventEffectKind kind)
|
||
{
|
||
foreach (var e in _active)
|
||
{
|
||
if (e.Has(kind)) return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/// <summary>Eventi il cui bersaglio diretto è l'entità (anche non ancora iniziati).</summary>
|
||
public IEnumerable<ActiveWorldEvent> For(EntityKind kind, int index)
|
||
{
|
||
foreach (var e in _active)
|
||
{
|
||
if (e.TargetKind == kind && e.Target == index) yield return e;
|
||
}
|
||
}
|
||
|
||
/// <summary>Capacità produttiva dell'industria (IndustrySystem, DemandSystem).</summary>
|
||
public double IndustryCapacity(IndustryId industry)
|
||
{
|
||
double f = 1.0;
|
||
long day = Today;
|
||
foreach (var e in _active)
|
||
{
|
||
if (e.TargetKind == EntityKind.Industry && e.Target == industry.Value && e.InForce(day)) f *= e.Effect(EventEffectKind.IndustryCapacity);
|
||
}
|
||
return f;
|
||
}
|
||
|
||
/// <summary>Domanda della città per un bene: eventi sulla città e sul bene nella zona.</summary>
|
||
public double TownDemand(Town town, int good)
|
||
{
|
||
if (_active.Count == 0) return 1.0;
|
||
double f = 1.0;
|
||
long day = Today;
|
||
foreach (var e in _active)
|
||
{
|
||
if (!e.InForce(day)) continue;
|
||
switch (e.Def.Target)
|
||
{
|
||
case EventTarget.Town when e.Target == town.Id.Value:
|
||
f *= e.Effect(EventEffectKind.TownDemand);
|
||
break;
|
||
case EventTarget.TownGood when e.Good == good && (e.Target == town.Id.Value || e.InZone(town.Position)):
|
||
f *= e.Effect(EventEffectKind.GoodDemand);
|
||
break;
|
||
}
|
||
}
|
||
return f;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Quota dei pendolari che si presenta al lavoro: eventi sulla città di provenienza (epidemia,
|
||
/// sciopero) e sul deposito di destinazione (sciopero del personale).
|
||
/// </summary>
|
||
public double Attendance(TownId town, WorkplaceRef workplace)
|
||
{
|
||
if (_active.Count == 0) return 1.0;
|
||
double f = 1.0;
|
||
long day = Today;
|
||
foreach (var e in _active)
|
||
{
|
||
if (!e.InForce(day)) continue;
|
||
bool hit = e.Def.Target switch
|
||
{
|
||
EventTarget.Town => e.Target == town.Value,
|
||
EventTarget.Depot => workplace.Kind == WorkplaceKind.Depot && e.Target == workplace.Index,
|
||
_ => false,
|
||
};
|
||
if (hit) f *= e.Effect(EventEffectKind.Attendance);
|
||
}
|
||
return f;
|
||
}
|
||
|
||
/// <summary>Velocità dei treni sull'arco (TrainSystem, a ogni tick): aree di maltempo in vigore oggi che contengono il suo punto medio.</summary>
|
||
public double EdgeSpeed(TrackEdge edge) => _areaEvents == 0 ? 1.0 : AreaEffect(edge, EventEffectKind.TrainSpeed, Today);
|
||
|
||
/// <summary>
|
||
/// Usura del binario dovuta al traffico sull'arco (ConstructionSystem): il sistema la applica al
|
||
/// mattino al traffico del giorno prima, quindi conta il maltempo in vigore ieri.
|
||
/// </summary>
|
||
public double EdgeWear(TrackEdge edge) => _areaEvents == 0 ? 1.0 : AreaEffect(edge, EventEffectKind.TrackWear, Today - 1);
|
||
|
||
private double AreaEffect(TrackEdge edge, EventEffectKind kind, long day)
|
||
{
|
||
int mid = edge.X.Length / 2;
|
||
var p = new Vec2(edge.X[mid], edge.Y[mid]);
|
||
double f = 1.0;
|
||
foreach (var e in _active)
|
||
{
|
||
if (e.Def.Target == EventTarget.Area && e.InForce(day) && e.InZone(p)) f *= e.Effect(kind);
|
||
}
|
||
return f;
|
||
}
|
||
}
|