using RailEnterprise.Core.Commands; using RailEnterprise.Core.Common; using RailEnterprise.Core.Persistence; using RailEnterprise.Core.Simulation; using RailEnterprise.Core.Simulation.Ai; using RailEnterprise.Tests.Harness; namespace RailEnterprise.Tests.Economy; /// Concorrenti IA (guida 12.1, test di 14.5): comandi validi, crescita, determinismo. public static class AiTests { [Test(Slow = true, Description = "Le IA agiscono solo con i comandi e ne vedono rifiutati al massimo il 5 %")] public static void AiCommandsAreRarelyRejected() { var w = TestWorlds.Small(42); w.RunYears(5); Assert.True(w.AiStates.Count >= 2, "uno stato per ogni IA"); long issued = 0; foreach (var s in w.AiStates) { issued += s.Issued; Assert.True(s.RejectedShare <= w.Data.Ai.Planning.MaxRejectedShare, $"{w.Company(s.Company).Name}: {s.Rejected} rifiutati su {s.Issued}"); } Assert.True(issued > 20, $"le IA costruiscono e gestiscono la rete ({issued} comandi)"); Assert.True(w.AiStates.Any(s => s.Projects.Any(p => p.Stage == AiProjectStage.Running)), "almeno una linea in esercizio"); // i binari, i treni e le stazioni delle IA esistono davvero nel mondo foreach (var s in w.AiStates) { foreach (var p in s.Projects.Where(p => p.Stage == AiProjectStage.Running)) { Assert.True(w.Network.IsAlive(w.Network.Stations[p.StationA.Value]) || w.Company(s.Company).Defunct); Assert.True(p.Trains.All(t => w.Trains[t.Value].Owner == s.Company || w.Company(s.Company).Merged)); } } } [Test(Slow = true, Description = "Un'IA con capitale ×2 supera il giocatore passivo in 10 anni")] public static void AiWithDoubleCapitalBeatsAPassivePlayer() { var w = TestWorlds.Small(42, "imprenditore", ("aiCapitalMultiplier", 2)); w.RunYears(10); var player = w.Player; var best = w.Companies.Where(c => !c.IsPlayer).MaxBy(c => c.NetWorth.Cents)!; Assert.True(best.NetWorth > player.NetWorth, $"{best.Name}: {best.NetWorth} contro {player.NetWorth}"); Assert.True(best.NetWorth.Cents > w.Difficulty.InitialCapital * 2, $"{best.Name} ha fatto crescere il capitale: {best.NetWorth}"); } [Test(Slow = true, Description = "Determinismo con le IA: stesso seme e stessi comandi, stesso hash dopo 5 anni (anche passando da un salvataggio)")] public static void SameSeedWithAiGivesSameHash() { static World Make() { var w = TestWorlds.Small(77, "magnate"); w.Enqueue(new TakeLoanCommand(Money.FromLire(50_000)) { Issuer = w.Player.Id, Seq = 1 }); return w; } var a = Make(); var b = Make(); a.RunYears(2); b.RunYears(2); Assert.Equal(StateHasher.Compute(a), StateHasher.Compute(b)); var c = SaveFile.Load(TestData.Data, SaveFile.Save(b, "test")); a.RunYears(3); c.RunYears(3); Assert.Equal(StateHasher.Compute(a), StateHasher.Compute(c)); Assert.True(a.AiStates.Sum(s => s.Issued) > 0, "le IA hanno agito"); } [Test(Slow = true, Description = "Accettazione F5: in «Magnate», con il giocatore passivo, almeno un'IA raggiunge il livello 3 in 15 anni")] public static void OnMagnateAnAiReachesTierThreeIn15Years() { var w = WorldFactory.Create(TestData.Data, new NewGameSettings { Seed = 7, SizeKm = 128, DifficultyId = "magnate" }); int best = 1; for (int y = 0; y < 15 && best < 3; y++) { w.RunYears(1); best = w.Companies.Where(c => !c.IsPlayer).Max(c => c.Tier); } var leader = w.Companies.Where(c => !c.IsPlayer).MaxBy(c => (c.Tier, c.NetWorth.Cents))!; Console.WriteLine($" {leader.Name}: livello {leader.Tier} nel {w.Clock.Year}, patrimonio {leader.NetWorth}, rating {leader.Rating}, " + $"città collegate {Core.Simulation.Systems.Tiers.ConnectedTowns(w, leader.Id, 1000)}"); Assert.True(best >= 3, $"livello massimo delle IA: {best}"); Assert.Equal(1, w.Player.Tier, "il giocatore passivo resta al livello 1"); } }