F6: eventi del mondo, correzioni della revisione
CI / Build e test (pull_request) Successful in 1m23s
CI / Immagine Docker (pull_request) Successful in 30s

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>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-07 17:23:42 +01:00
1 parent 1f8da87e51
commit 5a4a156d52
14 files changed
+309 -54

No files matched your search

@@ -213,6 +213,14 @@ public static class EventTests
a.RunDays(1);
b.RunDays(1);
CheckAttendance(a, b, town, 0.2, "sciopero");
// le stime dell'IA (ADR 0017 §9) contano i presenti senza lo sciopero: come nel gemello
foreach (var ind in a.Industries)
{
var twin = b.Industries[ind.Id.Value];
Assert.Approx(twin.WorkersPresent, EmploymentSystem.PresentWithoutEvents(a, ind, PaxClass.Worker), 1e-9, $"presenti senza eventi a {ind.Name}");
Assert.Approx(twin.StaffPresent, EmploymentSystem.PresentWithoutEvents(a, ind, PaxClass.Staff), 1e-9, $"personale senza eventi a {ind.Name}");
Assert.Equal(twin.WorkersPresent, EmploymentSystem.PresentWithoutEvents(b, twin, PaxClass.Worker), "senza eventi è il valore del giorno");
}
var sb = b.Towns[town.Id.Value];
Assert.SequenceEqual(sb.DemandToday, town.DemandToday, "lo sciopero non cambia i consumi");
a.RunDays(3);
@@ -271,10 +279,15 @@ public static class EventTests
Assert.Approx(4.0, wear, 1e-12);
Assert.False(ev.InZone(new Vec2(1e7, 1e7)), "lontano dall'area");
// il maltempo è in vigore da oggi: l'usura del traffico di oggi si applica domattina, moltiplicata;
// stessa usura del traffico nei due mondi: con il maltempo l'arco perde «wear» volte di più
a.RunDays(1);
b.RunDays(1);
var edges = a.Network.Edges.Where(e => e.State == EdgeState.Active).ToList();
var before = new Dictionary<int, (double A, double B)>();
foreach (var e in edges)
{
before[e.Id.Value] = (e.Condition, b.Network.Edges[e.Id.Value].Condition);
e.WearToday = 2000;
b.Network.Edges[e.Id.Value].WearToday = 2000;
}
@@ -286,7 +299,7 @@ public static class EventTests
double factor = a.WorldEvents.EdgeWear(e);
Assert.True(factor == 1.0 || Math.Abs(factor - wear) < 1e-12, "fattore dell'arco");
Assert.Approx(factor == 1.0 ? 1.0 : speed, a.WorldEvents.EdgeSpeed(e), 1e-12, "velocità dell'arco");
double dropA = 100 - e.Condition, dropB = 100 - b.Network.Edges[e.Id.Value].Condition;
double dropA = before[e.Id.Value].A - e.Condition, dropB = before[e.Id.Value].B - b.Network.Edges[e.Id.Value].Condition;
Assert.True(dropB > 0, "il traffico consuma il binario");
Assert.Approx(dropB * factor, dropA, 1e-9, $"usura dell'arco {e.Id.Value}");
if (factor > 1) affected++;
@@ -378,16 +391,17 @@ public static class EventTests
Assert.Equal(EventSeverity.Warning, news.Severity);
Assert.Equal(EntityKind.Industry, news.Entity);
Assert.True(news.Text.Contains(mine.Name, StringComparison.Ordinal) && news.Text.Contains("10 giorni", StringComparison.Ordinal) && news.Text.Contains("0%", StringComparison.Ordinal), news.Text);
Assert.Equal(w.Calendar.DateOfDay(ev.EndDay - 1).ToString(), EventSystem.Format(w, ev, "{until}"), "{until} è l'ultimo giorno dell'evento");
int stoppedDays = 0;
for (int d = 0; d < 12; d++)
{
w.RunDays(1);
if (mine.Efficiency == 0) stoppedDays++;
Assert.Equal(d < 9, w.WorldEvents.Active.Contains(ev), $"giorno {d}: evento in corso");
Assert.Equal(d < 10, w.WorldEvents.Active.Contains(ev), $"giorno {d}: evento in elenco");
}
Assert.Equal(10, stoppedDays, "dieci giorni di produzione nulla");
var end = WorldNews(w).Last();
Assert.Equal(start + 9, end.Day, "la notizia di fine arriva la sera dell'ultimo giorno");
Assert.Equal(start + 10, end.Day, "la notizia di fine arriva il mattino del primo giorno libero");
Assert.True(end.Text.Contains("riprende", StringComparison.Ordinal), end.Text);
Assert.Equal(2, WorldNews(w).Count);
}
@@ -409,6 +423,90 @@ public static class EventTests
Assert.Approx(0.5 * 1.5, w.WorldEvents.IndustryCapacity(mine.Id), 1e-12, "incidente e boom insieme: la capacità è il prodotto dei due");
}
[Test(Description = "Zone: niente due maltempi né due eventi sulla domanda dello stesso bene su zone sovrapposte; zone lontane o beni diversi sono ammessi")]
public static void OverlappingZonesAreRefused()
{
var w = Generated(31, 0);
w.RunDays(1);
var boom = w.Data.EventType("good_demand_boom")!;
double r = boom.RadiusKm!.Value * 1000;
// due città abbastanza vicine da condividere la zona e un bene consumato da entrambe
var (x, y, good) = (from p in w.Towns
from q in w.Towns
where p.Id.Value < q.Id.Value && (p.Position - q.Position).LengthSquared <= 4 * r * r
from g in boom.GoodIndices
where DemandSystem.TownDemand(w, p, g, w.Clock.Year) > 0 && DemandSystem.TownDemand(w, q, g, w.Clock.Year) > 0
select (p, q, g)).First();
var first = EventSystem.Force(w, "good_demand_boom", x.Id.Value, good, durationDays: 30)!;
Assert.True(EventSystem.Force(w, "good_demand_boom", y.Id.Value, good, durationDays: 30) is null, "boom dello stesso bene su una zona sovrapposta");
Assert.True(EventSystem.Force(w, "good_demand_slump", y.Id.Value, good, durationDays: 30) is null, "boom e crollo dello stesso bene insieme");
int other = boom.GoodIndices.FirstOrDefault(g => g != good && DemandSystem.TownDemand(w, y, g, w.Clock.Year) > 0, -1);
if (other >= 0) Assert.NotNull(EventSystem.Force(w, "good_demand_boom", y.Id.Value, other, durationDays: 30), "un altro bene è ammesso");
foreach (var t in w.Towns)
{
double expected = (t.Position - x.Position).LengthSquared <= r * r ? first.Primary : 1.0;
Assert.Approx(expected, w.WorldEvents.TownDemand(t, good), 1e-12, $"un solo moltiplicatore a {t.Name}");
}
var storm = w.Data.EventType("storm")!;
double rs = storm.RadiusKm!.Value * 1000;
var near = w.Towns.First(t => t.Id != x.Id && (t.Position - x.Position).LengthSquared <= 4 * rs * rs);
var far = w.Towns.FirstOrDefault(t => (t.Position - x.Position).LengthSquared > 4 * rs * rs);
Assert.NotNull(EventSystem.Force(w, "storm", x.Id.Value, durationDays: 5));
Assert.True(EventSystem.Force(w, "storm", near.Id.Value, durationDays: 5) is null, "due maltempi su zone sovrapposte");
if (far is not null) Assert.NotNull(EventSystem.Force(w, "storm", far.Id.Value, durationDays: 5), "maltempo lontano ammesso");
}
/// <summary>Porta il mondo al primo tick del giorno dopo, già eseguito (come quando il server manda il resoconto).</summary>
private static void NextDayAfterFirstTick(World w)
{
do w.RunTicks(1);
while (w.Clock.TickOfDay != 0);
w.RunTicks(1);
}
[Test(Description = "Maltempo estratto a giornata iniziata: treni, usura e resoconto al client seguono i giorni annunciati, con il conto alla rovescia da n a 1")]
public static void StormWindowMatchesTheAnnouncedDays()
{
var f = Valdoria();
f.BuildFreightNetwork();
var w = f.W;
w.RunDays(1);
w.RunTicks(1); // giorno D, dopo i sistemi giornalieri: l'evento inizia domani
long draw = w.Clock.Day;
var town = TownNamed(w, "Valdoria");
var ev = EventSystem.Force(w, "storm", town.Id.Value, durationDays: 3, intensity: 1.0)!;
Assert.Equal(draw + 1, ev.StartDay);
var edge = w.Network.Edges.First(e => e.State == EdgeState.Active && ev.InZone(new Vec2(e.X[e.X.Length / 2], e.Y[e.Y.Length / 2])));
double speed = ev.Effect(EventEffectKind.TrainSpeed), wear = ev.Effect(EventEffectKind.TrackWear);
Assert.Equal(1.0, w.WorldEvents.EdgeSpeed(edge), "il giorno dell'estrazione i treni non rallentano");
WorldEventInfo Listed() => RailEnterprise.Server.Projection.Digest(w, w.Player, [], false, 1, 0).WorldEvents.Single();
Assert.True(Listed().StartDay > w.Clock.Day, "il resoconto del giorno dell'estrazione lo mostra in partenza");
int Ends() => WorldNews(w).Count(n => n.Text.Contains("sereno", StringComparison.Ordinal));
for (int k = 0; k < 3; k++)
{
NextDayAfterFirstTick(w);
Assert.Equal(speed, w.WorldEvents.EdgeSpeed(edge), $"giorno {k + 1}: treni rallentati");
// l'usura applicata oggi è del traffico di ieri: moltiplicata dal secondo giorno
Assert.Equal(k == 0 ? 1.0 : wear, w.WorldEvents.EdgeWear(edge), $"giorno {k + 1}: usura");
Assert.Equal(3L - k, Listed().EndDay - w.Clock.Day, $"giorno {k + 1}: giorni che restano");
Assert.Equal(0, Ends(), "nessuna notizia di fine prima del tempo");
}
// primo giorno libero, prima del suo primo tick: l'usura dell'ultimo giorno di maltempo è ancora moltiplicata
do w.RunTicks(1);
while (w.Clock.TickOfDay != 0);
Assert.Equal(ev.EndDay, w.Clock.Day);
Assert.Equal(wear, w.WorldEvents.EdgeWear(edge), "usura del traffico dell'ultimo giorno");
Assert.Equal(1.0, w.WorldEvents.EdgeSpeed(edge), "il primo giorno libero i treni vanno normali");
w.RunTicks(1);
Assert.Equal(0, RailEnterprise.Server.Projection.Digest(w, w.Player, [], false, 1, 0).WorldEvents.Count, "fuori dal resoconto");
var end = WorldNews(w).Last();
Assert.Equal(ev.EndDay, end.Day, "la notizia di fine il primo giorno libero");
Assert.Equal(1, Ends(), end.Text);
AssertInvariants(w, "maltempo");
}
[Test(Description = "Con la frequenza a zero (scenari di accettazione) non nasce nessun evento; la difficoltà scala la frequenza")]
public static void FrequencyZeroMeansNoEvents()
{
@@ -511,11 +609,23 @@ public static class EventTests
Assert.True(active.Count <= w.Data.Events.MaxActive, $"giorno {d}: {active.Count} eventi");
var keys = active.Select(e => (e.Def.Id, e.TargetKind, e.Target)).ToList();
Assert.Equal(keys.Count, keys.Distinct().Count(), $"giorno {d}: doppioni");
for (int i = 0; i < active.Count; i++)
{
for (int j = i + 1; j < active.Count; j++)
{
var (x, y) = (active[i], active[j]);
if (x.Def.Target != y.Def.Target || x.Good != y.Good || x.Def.RadiusKm is not { } rx || y.Def.RadiusKm is not { } ry) continue;
double reach = (rx + ry) * 1000;
Assert.True((x.Center - y.Center).LengthSquared > reach * reach, $"giorno {d}: zone sovrapposte di {x.Def.Id} e {y.Def.Id}");
}
}
// giorno appena giocato: Clock.Day − 1; quelli estratti ieri iniziano oggi
long last = w.Clock.Day - 1;
foreach (var e in active)
{
types.Add(e.Def.Id);
Assert.True(e.DurationDays >= e.Def.DurationDays[0] && e.DurationDays <= e.Def.DurationDays[1], $"{e.Def.Id}: durata");
Assert.True(e.StartDay <= w.Clock.Day && w.Clock.Day < e.EndDay, $"{e.Def.Id}: in corso oggi");
Assert.True(e.InForce(last) || e.StartDay == last + 1, $"{e.Def.Id}: in vigore ieri o in partenza oggi");
Assert.InRange(e.Intensity, 0, 1, "intensità");
}
if (d % 30 == 29) AssertInvariants(w, $"giorno {d}");