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>
748 lines
28 KiB
C#
748 lines
28 KiB
C#
using RailEnterprise.Core.Common;
|
||
using RailEnterprise.Core.Data;
|
||
using RailEnterprise.Core.Economy;
|
||
using RailEnterprise.Core.Map;
|
||
using RailEnterprise.Core.Simulation;
|
||
|
||
namespace RailEnterprise.Core.Persistence;
|
||
|
||
/// <summary>
|
||
/// Serializzazione completa del mondo. I riferimenti alle definizioni sono salvati per id
|
||
/// (stringhe) e rimappati al caricamento: aggiungere o riordinare beni, industrie o veicoli nei
|
||
/// dati non rompe i salvataggi esistenti. Lo stesso formato dello stato dinamico alimenta l'hash.
|
||
/// </summary>
|
||
public static class WorldSerializer
|
||
{
|
||
/// <summary>Versione del corpo del salvataggio (migrazioni in <see cref="Read"/>).</summary>
|
||
public const int BodyVersion = 5;
|
||
|
||
/// <summary>Scrive l'intero mondo (impostazioni, terreno, stato dinamico, eventi).</summary>
|
||
public static void Write(World w, SaveWriter s) => Write(w, s, BodyVersion);
|
||
|
||
/// <summary>
|
||
/// Scrive il corpo nel formato di una versione precedente (le sezioni nate dopo si omettono):
|
||
/// serve a provare le migrazioni dei salvataggi.
|
||
/// </summary>
|
||
public static void Write(World w, SaveWriter s, int bodyVersion)
|
||
{
|
||
if (bodyVersion < 1 || bodyVersion > BodyVersion) throw new ArgumentOutOfRangeException(nameof(bodyVersion));
|
||
s.Write(bodyVersion);
|
||
WriteDictionary(w.Data, s);
|
||
WriteSettings(w.Settings, s, bodyVersion);
|
||
WriteTerrain(w.Terrain, w.SeaSide, s);
|
||
WriteDynamic(w, s, bodyVersion);
|
||
s.Write(w.LastStateHash);
|
||
s.Write(w.LastHashDay);
|
||
WriteEvents(w.Events, s);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Stato dinamico (tutto tranne terreno, impostazioni ed eventi): è ciò che entra nell'hash
|
||
/// giornaliero. Il terreno vi entra attraverso il suo hash.
|
||
/// </summary>
|
||
public static void WriteDynamic(World w, SaveWriter s) => WriteDynamic(w, s, BodyVersion);
|
||
|
||
private static void WriteDynamic(World w, SaveWriter s, int version)
|
||
{
|
||
if (w.TerrainHash == 0) w.TerrainHash = w.Terrain.ComputeHash();
|
||
s.Write(w.TerrainHash);
|
||
s.Write(w.Clock.Tick);
|
||
|
||
var streams = w.Streams.ToList();
|
||
s.Write(streams.Count);
|
||
foreach (var (name, rng) in streams)
|
||
{
|
||
s.Write(name);
|
||
var (a, b, c, d) = rng.State;
|
||
s.Write(a);
|
||
s.Write(b);
|
||
s.Write(c);
|
||
s.Write(d);
|
||
}
|
||
|
||
var ids = w.NextIds;
|
||
s.Write(ids.Contract); s.Write(ids.Loan); s.Write(ids.Industry); s.Write(ids.Station); s.Write(ids.Node);
|
||
s.Write(ids.Edge); s.Write(ids.Signal); s.Write(ids.Train); s.Write(ids.Vehicle); s.Write(ids.Line);
|
||
s.Write(ids.Packet); s.Write(ids.Depot); s.Write(ids.Work); s.Write(ids.Bid); s.Write(ids.Agreement);
|
||
s.Write(w.SpawnedThisYear);
|
||
|
||
s.Write(w.Sites.Count);
|
||
foreach (var site in w.Sites)
|
||
{
|
||
s.Write(site.Id.Value);
|
||
s.Write((byte)site.Kind);
|
||
s.Write(site.CellX);
|
||
s.Write(site.CellY);
|
||
s.Write(site.RadiusCells);
|
||
s.Write(site.InitialReserveTonnes);
|
||
s.Write(site.ReserveTonnes);
|
||
s.Write(site.Discovered);
|
||
}
|
||
|
||
s.Write(w.Towns.Count);
|
||
foreach (var t in w.Towns)
|
||
{
|
||
s.Write(t.Id.Value);
|
||
s.Write(t.Name);
|
||
s.Write(t.CellX);
|
||
s.Write(t.CellY);
|
||
s.Write(t.Position.X);
|
||
s.Write(t.Position.Y);
|
||
s.Write(t.RadiusCells);
|
||
s.Write(t.Population);
|
||
s.Write(t.Stock);
|
||
s.Write(t.DemandToday);
|
||
s.Write(t.DemandAccumulator);
|
||
s.Write(t.Satisfaction);
|
||
s.Write(t.CategoryActive);
|
||
s.Write(t.WorkDissatisfaction);
|
||
s.Write(t.EmployedWorkers);
|
||
s.Write(t.EmployedStaff);
|
||
s.Write(t.ServiceJobs);
|
||
s.Write(t.Services);
|
||
s.Write(t.LastGrowth);
|
||
}
|
||
|
||
s.Write(w.Hubs.Count);
|
||
foreach (var h in w.Hubs)
|
||
{
|
||
s.Write(h.Id.Value);
|
||
s.Write((byte)h.Kind);
|
||
s.Write(h.Name);
|
||
s.Write(h.CellX);
|
||
s.Write(h.CellY);
|
||
s.Write(h.FootprintCells);
|
||
s.Write(h.Position.X);
|
||
s.Write(h.Position.Y);
|
||
s.Write(h.MonthlyCapacity);
|
||
s.Write(h.Duty);
|
||
s.Write(h.UsedThisMonth);
|
||
s.Write(h.ImportedThisMonth);
|
||
s.Write(h.ExportedThisMonth);
|
||
}
|
||
|
||
s.Write(w.Industries.Count);
|
||
foreach (var i in w.Industries)
|
||
{
|
||
s.Write(i.Id.Value);
|
||
s.Write(i.Def.Id);
|
||
s.Write(i.Name);
|
||
s.Write(i.CellX);
|
||
s.Write(i.CellY);
|
||
s.Write(i.FootprintCells);
|
||
s.Write(i.Position.X);
|
||
s.Write(i.Position.Y);
|
||
s.Write(i.Level);
|
||
s.Write(i.Closed);
|
||
s.Write(i.Site.Value);
|
||
s.Write(i.Owner.Value);
|
||
s.Write(i.Cash.Cents);
|
||
s.Write(i.Condition);
|
||
s.Write((byte)i.Budget);
|
||
WriteRecipeStock(i.Def.Recipe.Inputs, i.InputStock, s);
|
||
WriteRecipeStock(i.Def.Recipe.Outputs, i.OutputStock, s);
|
||
s.Write(i.CycleAccumulator);
|
||
s.Write(i.CovInput);
|
||
s.Write(i.CovWorkers);
|
||
s.Write(i.CovStaff);
|
||
s.Write(i.Efficiency);
|
||
s.Write(i.WorkersPresent);
|
||
s.Write(i.StaffPresent);
|
||
s.Write(i.ContractedWorkers);
|
||
s.Write(i.ContractedStaff);
|
||
s.Write(i.RevenueEma);
|
||
s.Write(i.CostEma);
|
||
s.Write(i.QuarterProduced);
|
||
s.Write(i.QuarterShipped);
|
||
s.Write(i.QuarterCoverageSum);
|
||
s.Write(i.QuarterDays);
|
||
s.Write(i.QuartersAboveUpgrade);
|
||
s.Write(i.QuartersAtZero);
|
||
s.Write(i.LastScore);
|
||
s.Write(i.LastServedDay);
|
||
s.Write(i.SalesToday);
|
||
s.Write(i.PurchasesToday);
|
||
s.Write(i.ShippedToday);
|
||
s.Write(i.ProducedToday);
|
||
s.Write(i.InputDemandToday);
|
||
}
|
||
|
||
var m = w.Market;
|
||
s.Write(w.Data.GoodCount);
|
||
for (int g = 0; g < w.Data.GoodCount; g++)
|
||
{
|
||
s.Write(m.GlobalPrice[g]);
|
||
s.Write(m.Shock[g]);
|
||
s.Write(m.DemandToday[g]);
|
||
s.Write(m.SupplyToday[g]);
|
||
s.Write(m.CapacityToday[g]);
|
||
s.Write(m.UnmetDays[g]);
|
||
m.Demand[g].Save(s);
|
||
m.Supply[g].Save(s);
|
||
m.PriceHistory[g].Save(s);
|
||
}
|
||
|
||
s.Write(w.Companies.Count);
|
||
foreach (var c in w.Companies)
|
||
{
|
||
s.Write(c.Id.Value);
|
||
s.Write(c.Name);
|
||
s.Write(c.IsPlayer);
|
||
s.Write(c.AiPersonality);
|
||
s.Write(c.Color);
|
||
s.Write(c.Cash.Cents);
|
||
s.Write(c.BaseCredit.Cents);
|
||
s.Write((byte)c.Rating);
|
||
s.Write(c.Tier);
|
||
s.Write(c.NegativeCashDays);
|
||
s.Write(c.Bankrupt);
|
||
s.Write(c.FoundedDay);
|
||
s.Write(c.MonthEntries);
|
||
s.Write(c.YearEntries);
|
||
s.Write(c.LastYearEntries);
|
||
s.Write(c.AssetBookValue.Cents);
|
||
s.Write(c.Loans.Count);
|
||
foreach (var l in c.Loans)
|
||
{
|
||
s.Write(l.Id.Value);
|
||
s.Write(l.Principal.Cents);
|
||
s.Write(l.AnnualRate);
|
||
s.Write(l.TermMonths);
|
||
s.Write(l.Remaining.Cents);
|
||
s.Write(l.MonthsLeft);
|
||
}
|
||
}
|
||
|
||
s.Write(w.Money.External.Cents);
|
||
s.Write(w.Money.TransfersCount);
|
||
var goods = w.Goods;
|
||
s.Write(goods.Produced);
|
||
s.Write(goods.Consumed);
|
||
s.Write(goods.Decayed);
|
||
s.Write(goods.Imported);
|
||
s.Write(goods.Exported);
|
||
|
||
s.Write(w.Commutes.Count);
|
||
foreach (var c in w.Commutes)
|
||
{
|
||
s.Write(c.Id.Value);
|
||
s.Write(c.Town.Value);
|
||
s.Write((byte)c.Workplace.Kind);
|
||
s.Write(c.Workplace.Index);
|
||
s.Write((byte)c.Category);
|
||
s.Write(c.Headcount);
|
||
s.Write(c.Carrier.Value);
|
||
s.Write(c.HomeStation.Value);
|
||
s.Write(c.WorkStation.Value);
|
||
s.Write(c.Route.Count);
|
||
foreach (var line in c.Route) s.Write(line.Value);
|
||
s.Write(c.ExpectedMinutes);
|
||
s.Write(c.Coverage);
|
||
s.Write(c.LowCoverageDays);
|
||
s.Write(c.ArrivedToday);
|
||
}
|
||
|
||
RailSerializer.Write(w, s);
|
||
if (version >= 2) MaintenanceSerializer.Write(w, s);
|
||
if (version >= 3) CorporateSerializer.Write(w, s);
|
||
if (version >= 4) TradingSerializer.Write(w, s);
|
||
if (version >= 5) EventSerializer.Write(w, s);
|
||
}
|
||
|
||
private static void WriteRecipeStock(List<RecipeItemDef> items, int[] stock, SaveWriter s)
|
||
{
|
||
s.Write(items.Count);
|
||
for (int k = 0; k < items.Count; k++)
|
||
{
|
||
s.Write(items[k].Good);
|
||
s.Write(stock[k]);
|
||
}
|
||
}
|
||
|
||
private static void WriteDictionary(GameData data, SaveWriter s)
|
||
{
|
||
s.Write(data.Hash);
|
||
s.Write(data.GoodCount);
|
||
foreach (var g in data.Goods) s.Write(g.Id);
|
||
}
|
||
|
||
private static void WriteSettings(WorldSettings st, SaveWriter s, int version)
|
||
{
|
||
s.Write(st.Seed);
|
||
s.Write(st.SizeKm);
|
||
s.Write(st.StartYear);
|
||
s.Write(st.RegionId);
|
||
s.Write((ushort)st.Endless);
|
||
var d = st.Difficulty;
|
||
s.Write(d.Id);
|
||
s.Write(d.NameIt);
|
||
s.Write(d.NameEn);
|
||
s.Write(d.InitialCapital);
|
||
s.Write(d.InitialCredit);
|
||
s.Write(d.BaseInterestRate);
|
||
s.Write(d.ConstructionCostMultiplier);
|
||
s.Write(d.TariffMultiplier);
|
||
s.Write(d.WearMultiplier);
|
||
s.Write(d.MarketVolatilityMultiplier);
|
||
s.Write(d.AiCompetitors);
|
||
s.Write(d.AiCapitalMultiplier);
|
||
s.Write(d.AiCostDiscount);
|
||
s.Write((byte)d.PlayerTakeover);
|
||
s.Write(d.TakeoverRansomYears);
|
||
s.Write(d.BankruptcyDays);
|
||
s.Write(d.PublicFromStart);
|
||
s.Write(d.PlayerShareAtStart);
|
||
s.Write((byte)d.Ironman);
|
||
s.Write((byte)d.Depletion);
|
||
// versione 5 (F6): frequenza degli eventi del mondo
|
||
if (version >= 5) s.Write(d.EventFrequencyMultiplier);
|
||
}
|
||
|
||
private static void WriteTerrain(Terrain t, MapSide seaSide, SaveWriter s)
|
||
{
|
||
s.Write(t.Width);
|
||
s.Write(t.Height);
|
||
s.Write(t.CellSizeM);
|
||
s.Write(t.ChunkCells);
|
||
s.Write((byte)seaSide);
|
||
s.Write(t.Heights);
|
||
s.WriteBytes(t.Types);
|
||
s.WriteBytes(t.RiverWidth);
|
||
}
|
||
|
||
private static void WriteEvents(EventLog events, SaveWriter s)
|
||
{
|
||
s.Write(events.Total);
|
||
s.Write(events.Recent.Count);
|
||
foreach (var e in events.Recent)
|
||
{
|
||
s.Write(e.Day);
|
||
s.Write((byte)e.Kind);
|
||
s.Write((byte)e.Severity);
|
||
s.Write(e.Text);
|
||
s.Write((byte)e.Entity);
|
||
s.Write(e.EntityIndex);
|
||
s.Write(e.Company.Value);
|
||
}
|
||
}
|
||
|
||
// ------------------------------------------------------------------ lettura
|
||
|
||
/// <summary>Ricostruisce un mondo da un corpo di salvataggio decompresso.</summary>
|
||
public static World Read(GameData data, SaveReader r)
|
||
{
|
||
int version = r.ReadInt32();
|
||
if (version < 1 || version > BodyVersion) throw new InvalidDataException($"Versione del salvataggio non supportata: {version}");
|
||
|
||
r.ReadUInt64(); // hash dei dati al momento del salvataggio (informativo)
|
||
int savedGoods = r.ReadInt32();
|
||
var goodMap = new int[savedGoods];
|
||
for (int g = 0; g < savedGoods; g++)
|
||
{
|
||
string id = r.ReadRequiredString();
|
||
goodMap[g] = data.TryGoodIndex(id, out int idx) ? idx : -1;
|
||
}
|
||
|
||
var settings = ReadSettings(data, r, version);
|
||
var (terrain, seaSide) = ReadTerrain(r);
|
||
var w = new World(data, settings, terrain, seaSide);
|
||
ReadDynamic(w, r, goodMap, version);
|
||
w.LastStateHash = r.ReadUInt64();
|
||
w.LastHashDay = r.ReadInt64();
|
||
ReadEvents(w.Events, r);
|
||
return w;
|
||
}
|
||
|
||
private static WorldSettings ReadSettings(GameData data, SaveReader r, int version)
|
||
{
|
||
ulong seed = r.ReadUInt64();
|
||
int size = r.ReadInt32();
|
||
int startYear = r.ReadInt32();
|
||
string region = r.ReadRequiredString();
|
||
var endless = (EndlessFlags)r.ReadUInt16();
|
||
var d = new DifficultyDef
|
||
{
|
||
Id = r.ReadRequiredString(),
|
||
NameIt = r.ReadRequiredString(),
|
||
NameEn = r.ReadRequiredString(),
|
||
InitialCapital = r.ReadInt64(),
|
||
InitialCredit = r.ReadInt64(),
|
||
BaseInterestRate = r.ReadDouble(),
|
||
ConstructionCostMultiplier = r.ReadDouble(),
|
||
TariffMultiplier = r.ReadDouble(),
|
||
WearMultiplier = r.ReadDouble(),
|
||
MarketVolatilityMultiplier = r.ReadDouble(),
|
||
AiCompetitors = r.ReadInt32(),
|
||
AiCapitalMultiplier = r.ReadDouble(),
|
||
AiCostDiscount = r.ReadDouble(),
|
||
PlayerTakeover = (TakeoverRule)r.ReadByte(),
|
||
TakeoverRansomYears = r.ReadInt32(),
|
||
BankruptcyDays = r.ReadInt32(),
|
||
PublicFromStart = r.ReadBool(),
|
||
PlayerShareAtStart = r.ReadDouble(),
|
||
Ironman = (IronmanRule)r.ReadByte(),
|
||
Depletion = (DepletionRate)r.ReadByte(),
|
||
EventFrequencyMultiplier = 0,
|
||
};
|
||
// le versioni 1–4 non hanno la frequenza degli eventi: si prende quella del preset con lo
|
||
// stesso id nei dati correnti (1 per una difficoltà personalizzata sconosciuta)
|
||
d = d with
|
||
{
|
||
EventFrequencyMultiplier = version >= 5
|
||
? r.ReadDouble()
|
||
: data.Difficulty.Presets.FirstOrDefault(p => p.Id == d.Id)?.EventFrequencyMultiplier ?? 1.0,
|
||
};
|
||
return new WorldSettings { Seed = seed, SizeKm = size, StartYear = startYear, RegionId = region, Endless = endless, Difficulty = d };
|
||
}
|
||
|
||
private static (Terrain, MapSide) ReadTerrain(SaveReader r)
|
||
{
|
||
int width = r.ReadInt32();
|
||
int height = r.ReadInt32();
|
||
double cell = r.ReadDouble();
|
||
int chunk = r.ReadInt32();
|
||
var side = (MapSide)r.ReadByte();
|
||
var t = new Terrain(width, height, cell, chunk);
|
||
var heights = r.ReadUInt16Array();
|
||
var types = r.ReadBytes();
|
||
var river = r.ReadBytes();
|
||
if (heights.Length != t.Heights.Length || types.Length != t.Types.Length || river.Length != t.RiverWidth.Length)
|
||
throw new InvalidDataException("Dimensioni del terreno incoerenti nel salvataggio");
|
||
heights.CopyTo(t.Heights, 0);
|
||
types.CopyTo(t.Types, 0);
|
||
river.CopyTo(t.RiverWidth, 0);
|
||
return (t, side);
|
||
}
|
||
|
||
private static void ReadDynamic(World w, SaveReader r, int[] goodMap, int version)
|
||
{
|
||
var data = w.Data;
|
||
int goods = data.GoodCount;
|
||
w.TerrainHash = r.ReadUInt64();
|
||
w.Clock.Restore(r.ReadInt64());
|
||
|
||
int streams = r.ReadInt32();
|
||
for (int i = 0; i < streams; i++)
|
||
{
|
||
string name = r.ReadRequiredString();
|
||
w.RestoreStream(name, Rng.FromState(r.ReadUInt64(), r.ReadUInt64(), r.ReadUInt64(), r.ReadUInt64()));
|
||
}
|
||
|
||
var ids = w.NextIds;
|
||
ids.Contract = r.ReadInt32(); ids.Loan = r.ReadInt32(); ids.Industry = r.ReadInt32(); ids.Station = r.ReadInt32(); ids.Node = r.ReadInt32();
|
||
ids.Edge = r.ReadInt32(); ids.Signal = r.ReadInt32(); ids.Train = r.ReadInt32(); ids.Vehicle = r.ReadInt32(); ids.Line = r.ReadInt32();
|
||
ids.Packet = r.ReadInt32(); ids.Depot = r.ReadInt32(); ids.Work = r.ReadInt32(); ids.Bid = r.ReadInt32(); ids.Agreement = r.ReadInt32();
|
||
w.SpawnedThisYear = r.ReadInt32();
|
||
|
||
int sites = r.ReadInt32();
|
||
for (int i = 0; i < sites; i++)
|
||
{
|
||
var site = new ResourceSite
|
||
{
|
||
Id = new ResourceSiteId(r.ReadInt32()),
|
||
Kind = (ResourceKind)r.ReadByte(),
|
||
CellX = r.ReadInt32(),
|
||
CellY = r.ReadInt32(),
|
||
RadiusCells = r.ReadInt32(),
|
||
InitialReserveTonnes = r.ReadInt64(),
|
||
};
|
||
site.ReserveTonnes = r.ReadDouble();
|
||
site.Discovered = r.ReadBool();
|
||
w.Sites.Add(site);
|
||
}
|
||
|
||
int towns = r.ReadInt32();
|
||
for (int i = 0; i < towns; i++)
|
||
{
|
||
var id = new TownId(r.ReadInt32());
|
||
string name = r.ReadRequiredString();
|
||
int cx = r.ReadInt32(), cy = r.ReadInt32();
|
||
var pos = new Vec2(r.ReadDouble(), r.ReadDouble());
|
||
var t = new Town
|
||
{
|
||
Id = id,
|
||
Name = name,
|
||
CellX = cx,
|
||
CellY = cy,
|
||
Position = pos,
|
||
Stock = new int[goods],
|
||
DemandToday = new double[goods],
|
||
DemandAccumulator = new double[goods],
|
||
};
|
||
t.RadiusCells = r.ReadDouble();
|
||
t.Population = r.ReadInt32();
|
||
RemapInto(r.ReadInt32Array(), goodMap, t.Stock);
|
||
RemapInto(r.ReadDoubleArray(), goodMap, t.DemandToday);
|
||
RemapInto(r.ReadDoubleArray(), goodMap, t.DemandAccumulator);
|
||
CopyInto(r.ReadDoubleArray(), t.Satisfaction);
|
||
CopyInto(r.ReadBoolArray(), t.CategoryActive);
|
||
t.WorkDissatisfaction = r.ReadDouble();
|
||
t.EmployedWorkers = r.ReadInt32();
|
||
t.EmployedStaff = r.ReadInt32();
|
||
t.ServiceJobs = r.ReadInt32();
|
||
t.Services = r.ReadInt32();
|
||
t.LastGrowth = r.ReadDouble();
|
||
w.Towns.Add(t);
|
||
}
|
||
|
||
int hubs = r.ReadInt32();
|
||
for (int i = 0; i < hubs; i++)
|
||
{
|
||
var id = new HubId(r.ReadInt32());
|
||
var kind = (HubKind)r.ReadByte();
|
||
string name = r.ReadRequiredString();
|
||
int cx = r.ReadInt32(), cy = r.ReadInt32(), fp = r.ReadInt32();
|
||
var pos = new Vec2(r.ReadDouble(), r.ReadDouble());
|
||
var h = new Hub
|
||
{
|
||
Id = id,
|
||
Kind = kind,
|
||
Name = name,
|
||
CellX = cx,
|
||
CellY = cy,
|
||
FootprintCells = fp,
|
||
Position = pos,
|
||
MonthlyCapacity = r.ReadInt32(),
|
||
Duty = r.ReadDouble(),
|
||
ImportedThisMonth = new int[goods],
|
||
ExportedThisMonth = new int[goods],
|
||
};
|
||
h.UsedThisMonth = r.ReadInt32();
|
||
RemapInto(r.ReadInt32Array(), goodMap, h.ImportedThisMonth);
|
||
RemapInto(r.ReadInt32Array(), goodMap, h.ExportedThisMonth);
|
||
w.Hubs.Add(h);
|
||
}
|
||
|
||
int industries = r.ReadInt32();
|
||
for (int i = 0; i < industries; i++)
|
||
{
|
||
var id = new IndustryId(r.ReadInt32());
|
||
string typeId = r.ReadRequiredString();
|
||
if (!data.TryIndustry(typeId, out var def) || def is null)
|
||
throw new InvalidDataException($"Tipo di industria «{typeId}» assente dai dati correnti");
|
||
string name = r.ReadRequiredString();
|
||
int cx = r.ReadInt32(), cy = r.ReadInt32(), fp = r.ReadInt32();
|
||
var pos = new Vec2(r.ReadDouble(), r.ReadDouble());
|
||
var ind = new Industry
|
||
{
|
||
Id = id,
|
||
Def = def,
|
||
Name = name,
|
||
CellX = cx,
|
||
CellY = cy,
|
||
FootprintCells = fp,
|
||
Position = pos,
|
||
InputStock = new int[def.Recipe.Inputs.Count],
|
||
OutputStock = new int[def.Recipe.Outputs.Count],
|
||
ProducedToday = new int[def.Recipe.Outputs.Count],
|
||
InputDemandToday = new double[def.Recipe.Inputs.Count],
|
||
Level = r.ReadInt32(),
|
||
Closed = r.ReadBool(),
|
||
Site = new ResourceSiteId(r.ReadInt32()),
|
||
};
|
||
ind.Owner = new CompanyId(r.ReadInt32());
|
||
ind.Cash = new Money(r.ReadInt64());
|
||
ind.Condition = r.ReadDouble();
|
||
ind.Budget = (IndustryBudget)r.ReadByte();
|
||
ReadRecipeStock(def.Recipe.Inputs, ind.InputStock, r);
|
||
ReadRecipeStock(def.Recipe.Outputs, ind.OutputStock, r);
|
||
ind.CycleAccumulator = r.ReadDouble();
|
||
ind.CovInput = r.ReadDouble();
|
||
ind.CovWorkers = r.ReadDouble();
|
||
ind.CovStaff = r.ReadDouble();
|
||
ind.Efficiency = r.ReadDouble();
|
||
ind.WorkersPresent = r.ReadDouble();
|
||
ind.StaffPresent = r.ReadDouble();
|
||
ind.ContractedWorkers = r.ReadInt32();
|
||
ind.ContractedStaff = r.ReadInt32();
|
||
ind.RevenueEma = r.ReadDouble();
|
||
ind.CostEma = r.ReadDouble();
|
||
ind.QuarterProduced = r.ReadInt64();
|
||
ind.QuarterShipped = r.ReadInt64();
|
||
ind.QuarterCoverageSum = r.ReadDouble();
|
||
ind.QuarterDays = r.ReadInt32();
|
||
ind.QuartersAboveUpgrade = r.ReadInt32();
|
||
ind.QuartersAtZero = r.ReadInt32();
|
||
ind.LastScore = r.ReadDouble();
|
||
ind.LastServedDay = r.ReadInt64();
|
||
ind.SalesToday = r.ReadDouble();
|
||
ind.PurchasesToday = r.ReadDouble();
|
||
ind.ShippedToday = r.ReadInt32();
|
||
CopyInto(r.ReadInt32Array(), ind.ProducedToday);
|
||
CopyInto(r.ReadDoubleArray(), ind.InputDemandToday);
|
||
w.Industries.Add(ind);
|
||
}
|
||
|
||
var m = w.Market;
|
||
int savedGoods = r.ReadInt32();
|
||
var demand = new RollingWindow[goods];
|
||
var supply = new RollingWindow[goods];
|
||
var history = new RollingWindow[goods];
|
||
for (int g = 0; g < goods; g++)
|
||
{
|
||
demand[g] = m.Demand[g];
|
||
supply[g] = m.Supply[g];
|
||
history[g] = m.PriceHistory[g];
|
||
}
|
||
for (int sg = 0; sg < savedGoods; sg++)
|
||
{
|
||
int g = sg < goodMap.Length ? goodMap[sg] : -1;
|
||
double price = r.ReadDouble(), shock = r.ReadDouble(), dt = r.ReadDouble(), st = r.ReadDouble(), ct = r.ReadDouble();
|
||
int unmet = r.ReadInt32();
|
||
var dw = RollingWindow.Load(r);
|
||
var sw = RollingWindow.Load(r);
|
||
var hw = RollingWindow.Load(r);
|
||
if (g < 0) continue;
|
||
// salvataggi con uno storico più corto dei dati (360 giorni fino alla F5): si allunga
|
||
// conservando i prezzi, senza cambiare il formato (ADR 0015)
|
||
if (hw.Length < m.PriceHistory[g].Length) hw = hw.Extended(m.PriceHistory[g].Length);
|
||
m.GlobalPrice[g] = price;
|
||
m.Shock[g] = shock;
|
||
m.DemandToday[g] = dt;
|
||
m.SupplyToday[g] = st;
|
||
m.CapacityToday[g] = ct;
|
||
m.UnmetDays[g] = unmet;
|
||
demand[g] = dw;
|
||
supply[g] = sw;
|
||
history[g] = hw;
|
||
}
|
||
m.Restore(demand, supply, history);
|
||
|
||
int companies = r.ReadInt32();
|
||
for (int i = 0; i < companies; i++)
|
||
{
|
||
var cid = new CompanyId(r.ReadInt32());
|
||
string cname = r.ReadRequiredString();
|
||
bool isPlayer = r.ReadBool();
|
||
string? personality = r.ReadString();
|
||
string color = r.ReadRequiredString();
|
||
var cash = new Money(r.ReadInt64());
|
||
var baseCredit = new Money(r.ReadInt64());
|
||
var rating = (CreditRating)r.ReadByte();
|
||
int tier = r.ReadInt32();
|
||
int negDays = r.ReadInt32();
|
||
bool bankrupt = r.ReadBool();
|
||
long founded = r.ReadInt64();
|
||
var c = new Company
|
||
{
|
||
Id = cid,
|
||
Name = cname,
|
||
IsPlayer = isPlayer,
|
||
AiPersonality = personality,
|
||
Color = color,
|
||
FoundedDay = founded,
|
||
Cash = cash,
|
||
BaseCredit = baseCredit,
|
||
Rating = rating,
|
||
Tier = tier,
|
||
NegativeCashDays = negDays,
|
||
Bankrupt = bankrupt,
|
||
};
|
||
CopyInto(r.ReadInt64Array(), c.MonthEntries);
|
||
CopyInto(r.ReadInt64Array(), c.YearEntries);
|
||
CopyInto(r.ReadInt64Array(), c.LastYearEntries);
|
||
c.AssetBookValue = new Money(r.ReadInt64());
|
||
int loans = r.ReadInt32();
|
||
for (int k = 0; k < loans; k++)
|
||
{
|
||
var loan = new Loan
|
||
{
|
||
Id = new LoanId(r.ReadInt32()),
|
||
Principal = new Money(r.ReadInt64()),
|
||
AnnualRate = r.ReadDouble(),
|
||
TermMonths = r.ReadInt32(),
|
||
};
|
||
loan.Remaining = new Money(r.ReadInt64());
|
||
loan.MonthsLeft = r.ReadInt32();
|
||
c.Loans.Add(loan);
|
||
}
|
||
w.Companies.Add(c);
|
||
}
|
||
|
||
w.Money.External = new Money(r.ReadInt64());
|
||
w.Money.TransfersCount = r.ReadInt64();
|
||
RemapInto(r.ReadInt64Array(), goodMap, w.Goods.Produced);
|
||
RemapInto(r.ReadInt64Array(), goodMap, w.Goods.Consumed);
|
||
RemapInto(r.ReadInt64Array(), goodMap, w.Goods.Decayed);
|
||
RemapInto(r.ReadInt64Array(), goodMap, w.Goods.Imported);
|
||
RemapInto(r.ReadInt64Array(), goodMap, w.Goods.Exported);
|
||
|
||
int commutes = r.ReadInt32();
|
||
for (int i = 0; i < commutes; i++)
|
||
{
|
||
var id = new ContractId(r.ReadInt32());
|
||
var town = new TownId(r.ReadInt32());
|
||
var wk = (WorkplaceKind)r.ReadByte();
|
||
int widx = r.ReadInt32();
|
||
var cat = (PaxClass)r.ReadByte();
|
||
var c = new CommuteContract
|
||
{
|
||
Id = id,
|
||
Town = town,
|
||
Workplace = new WorkplaceRef(wk, widx),
|
||
Category = cat,
|
||
Headcount = r.ReadInt32(),
|
||
};
|
||
c.Carrier = new CompanyId(r.ReadInt32());
|
||
c.HomeStation = new StationId(r.ReadInt32());
|
||
c.WorkStation = new StationId(r.ReadInt32());
|
||
int route = r.ReadInt32();
|
||
for (int k = 0; k < route; k++) c.Route.Add(new LineId(r.ReadInt32()));
|
||
c.ExpectedMinutes = r.ReadDouble();
|
||
c.Coverage = r.ReadDouble();
|
||
c.LowCoverageDays = r.ReadInt32();
|
||
c.ArrivedToday = r.ReadInt32();
|
||
w.Commutes.Add(c);
|
||
}
|
||
|
||
RailSerializer.Read(w, r, goodMap);
|
||
// versione 2 (F4): manutenzione, officine, convenzioni; la versione 1 si migra
|
||
if (version >= 2) MaintenanceSerializer.Read(w, r);
|
||
else MaintenanceSerializer.Migrate(w);
|
||
// versione 3 (F5): industrie, borsa, offerte, aste, fine partita, agenti IA
|
||
if (version >= 3) CorporateSerializer.Read(w, r);
|
||
else CorporateSerializer.Migrate(w);
|
||
// versione 4 (F6): magazzini della compagnia e merce propria in viaggio
|
||
if (version >= 4) TradingSerializer.Read(w, r);
|
||
else TradingSerializer.Migrate(w);
|
||
// versione 5 (F6): eventi del mondo in corso; prima nessun evento attivo
|
||
if (version >= 5) EventSerializer.Read(w, r);
|
||
}
|
||
|
||
private static void ReadRecipeStock(List<RecipeItemDef> items, int[] stock, SaveReader r)
|
||
{
|
||
int n = r.ReadInt32();
|
||
for (int k = 0; k < n; k++)
|
||
{
|
||
string good = r.ReadRequiredString();
|
||
int qty = r.ReadInt32();
|
||
int idx = items.FindIndex(it => it.Good == good);
|
||
if (idx >= 0) stock[idx] = qty;
|
||
}
|
||
}
|
||
|
||
private static void ReadEvents(EventLog events, SaveReader r)
|
||
{
|
||
long total = r.ReadInt64();
|
||
int n = r.ReadInt32();
|
||
var list = new List<GameEvent>(n);
|
||
for (int i = 0; i < n; i++)
|
||
{
|
||
list.Add(new GameEvent(r.ReadInt64(), (EventKind)r.ReadByte(), (EventSeverity)r.ReadByte(), r.ReadRequiredString(),
|
||
(EntityKind)r.ReadByte(), r.ReadInt32(), new CompanyId(r.ReadInt32())));
|
||
}
|
||
events.Restore(list, total);
|
||
}
|
||
|
||
private static void RemapInto<T>(T[] saved, int[] goodMap, T[] target)
|
||
{
|
||
for (int sg = 0; sg < saved.Length && sg < goodMap.Length; sg++)
|
||
{
|
||
int g = goodMap[sg];
|
||
if (g >= 0) target[g] = saved[sg];
|
||
}
|
||
}
|
||
|
||
private static void CopyInto<T>(T[] source, T[] target) => Array.Copy(source, target, Math.Min(source.Length, target.Length));
|
||
}
|