Infrastruttura: nome «Rail Enterprise», CI e rilasci su Gitea, documentazione
Il progetto adotta il nome «Rail Enterprise» ovunque (codice RailEnterprise.*, variabili RE_*, intestazioni RESV/RECL, immagine rail-enterprise-server) per allinearsi al repository Gitea. Aggiunge Gitea Actions sul modello di Chronos: CI su PR e main, rilascio da tag con gioco Windows (client Godot + server in server/), server Windows/Linux e immagine Docker nel registro Gitea, nightly. Aggiunge i riferimenti visivi 06–09, i modelli di PR e issue, l'ADR 0014 e aggiorna README (comandi locali), STATE e CLAUDE.md. 120 test verdi. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f6c2a17b4d
commit
c1a0049bcf
300 files changed
+1602
-1045
No files matched your search
@@ -0,0 +1,352 @@
|
||||
using RailEnterprise.Core.Common;
|
||||
using RailEnterprise.Core.Economy;
|
||||
using RailEnterprise.Core.Simulation;
|
||||
using RailEnterprise.Core.Simulation.Ai;
|
||||
|
||||
namespace RailEnterprise.Core.Persistence;
|
||||
|
||||
/// <summary>
|
||||
/// Stato della fase F5 (corpo del salvataggio dalla versione 3): manutenzione e proprietà delle
|
||||
/// industrie, borsa, controllo e storico delle compagnie, offerte pubbliche, aste, fine partita e
|
||||
/// agenti IA. I salvataggi precedenti si migrano con valori iniziali coerenti (ADR 0012).
|
||||
/// </summary>
|
||||
public static class CorporateSerializer
|
||||
{
|
||||
public static void Write(World w, SaveWriter s)
|
||||
{
|
||||
s.Write(w.Industries.Count);
|
||||
foreach (var ind in w.Industries)
|
||||
{
|
||||
s.Write(ind.SpareParts);
|
||||
s.Write(ind.Machinery);
|
||||
s.Write(ind.PartsOrdered);
|
||||
s.Write(ind.PartsShort);
|
||||
s.Write(ind.StoppedUntilDay);
|
||||
s.Write((byte)ind.StopKind);
|
||||
s.Write((byte)ind.PendingWork);
|
||||
s.Write(ind.MonthsHighMargin);
|
||||
s.Write(ind.MaintenanceSpentYear);
|
||||
s.Write(ind.AcquiredDay);
|
||||
s.Write(ind.StopHistory.Count);
|
||||
foreach (var h in ind.StopHistory)
|
||||
{
|
||||
s.Write(h.Day);
|
||||
s.Write(h.Days);
|
||||
s.Write((byte)h.Kind);
|
||||
}
|
||||
}
|
||||
|
||||
s.Write(w.Companies.Count);
|
||||
foreach (var c in w.Companies)
|
||||
{
|
||||
var sh = c.Shares;
|
||||
s.Write(sh.Total);
|
||||
s.Write(sh.Founder);
|
||||
s.Write(sh.Public);
|
||||
s.Write(sh.Treasury);
|
||||
s.Write(sh.ByCompany);
|
||||
s.Write(c.Listed);
|
||||
s.Write(c.SharePrice);
|
||||
s.Write(c.Sentiment);
|
||||
s.Write(c.ExtraSentiment);
|
||||
s.Write(c.ProfitRing);
|
||||
s.Write(c.ProfitRingIndex);
|
||||
s.Write(c.DividendRing);
|
||||
s.Write(c.LastAnnualProfit);
|
||||
s.Write(c.PortfolioValue.Cents);
|
||||
s.Write(c.ControlledBy.Value);
|
||||
s.Write(c.ControlSinceDay);
|
||||
s.Write(c.Merged);
|
||||
s.Write(c.MergedInto.Value);
|
||||
s.Write(c.Receivership);
|
||||
s.Write(c.ReceivershipUntilDay);
|
||||
s.Write(c.ReceivershipBy.Value);
|
||||
s.Write(c.BankruptDay);
|
||||
s.Write(c.Liquidated);
|
||||
s.Write(c.PriceHistory.ToArray());
|
||||
s.Write(c.History.Count);
|
||||
foreach (var y in c.History)
|
||||
{
|
||||
s.Write(y.Year);
|
||||
s.Write(y.NetWorthCents);
|
||||
s.Write(y.CashCents);
|
||||
s.Write(y.RevenueCents);
|
||||
s.Write(y.ProfitCents);
|
||||
s.Write(y.Tier);
|
||||
s.Write(y.Trains);
|
||||
s.Write(y.TrackKm);
|
||||
s.Write(y.SharePriceCents);
|
||||
}
|
||||
s.Write(c.Milestones.Count);
|
||||
foreach (var m in c.Milestones)
|
||||
{
|
||||
s.Write(m.Day);
|
||||
s.Write(m.Text);
|
||||
}
|
||||
}
|
||||
|
||||
s.Write(w.Bids.Count);
|
||||
foreach (var b in w.Bids)
|
||||
{
|
||||
s.Write(b.Id.Value);
|
||||
s.Write(b.Bidder.Value);
|
||||
s.Write(b.Target.Value);
|
||||
s.Write(b.PricePerShare);
|
||||
s.Write(b.TargetShare);
|
||||
s.Write(b.StartDay);
|
||||
s.Write(b.EndDay);
|
||||
s.Write((byte)b.State);
|
||||
s.Write(b.TenderedPublic);
|
||||
s.Write(b.TenderedFounder);
|
||||
s.Write(b.Accepted.Select(a => a.Value).ToArray());
|
||||
s.Write(b.Defenses);
|
||||
s.Write(b.Knight.Value);
|
||||
s.Write(b.ClosedDay);
|
||||
}
|
||||
|
||||
s.Write(w.Auctions.Count);
|
||||
foreach (var a in w.Auctions)
|
||||
{
|
||||
s.Write(a.Seller.Value);
|
||||
s.Write(a.StartDay);
|
||||
s.Write(a.EndDay);
|
||||
s.Write(a.MinPriceCents);
|
||||
s.Write(a.BestBidder.Value);
|
||||
s.Write(a.BestPriceCents);
|
||||
s.Write(a.Closed);
|
||||
}
|
||||
|
||||
s.Write(w.GameOver is not null);
|
||||
if (w.GameOver is { } g)
|
||||
{
|
||||
s.Write(g.Day);
|
||||
s.Write((byte)g.Reason);
|
||||
s.Write(g.Text);
|
||||
s.Write(g.Winner.Value);
|
||||
}
|
||||
s.Write(w.LastCompetitorYear);
|
||||
s.Write(w.AiPaused);
|
||||
|
||||
s.Write(w.AiStates.Count);
|
||||
foreach (var a in w.AiStates)
|
||||
{
|
||||
s.Write(a.Company.Value);
|
||||
s.Write(a.PlanDay);
|
||||
s.Write(a.Issued);
|
||||
s.Write(a.Rejected);
|
||||
s.Write(a.PolicySet);
|
||||
s.Write(a.NextProjectId);
|
||||
s.Write(a.LastIndustryYear);
|
||||
s.Write(a.LastGridlockDay);
|
||||
s.Write(a.Failed.ToArray());
|
||||
s.Write(a.Responded.ToArray());
|
||||
s.Write(a.Projects.Count);
|
||||
foreach (var p in a.Projects)
|
||||
{
|
||||
s.Write(p.Id);
|
||||
s.Write((byte)p.Kind);
|
||||
s.Write(p.Good);
|
||||
s.Write(p.PaxClass);
|
||||
s.Write(p.Key);
|
||||
s.Write(p.StationA.Value);
|
||||
s.Write(p.StationB.Value);
|
||||
s.Write(p.Depot.Value);
|
||||
s.Write(p.Edges.Select(e => e.Value).ToArray());
|
||||
s.Write(p.Line.Value);
|
||||
s.Write(p.Trains.Select(t => t.Value).ToArray());
|
||||
s.Write((byte)p.Stage);
|
||||
s.Write(p.StartDay);
|
||||
s.Write(p.CapitalCents);
|
||||
s.Write(p.RevenuePerYearCents);
|
||||
s.Write(p.TrainsPlanned);
|
||||
s.Write(p.Loco);
|
||||
s.Write(p.Car);
|
||||
s.Write(p.CarsPerTrain);
|
||||
s.Write(p.GrowAfterDay);
|
||||
s.Write(p.SupplyFor.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Read(World w, SaveReader r)
|
||||
{
|
||||
int industries = r.ReadInt32();
|
||||
for (int i = 0; i < industries; i++)
|
||||
{
|
||||
var ind = w.Industries[i];
|
||||
ind.SpareParts = r.ReadInt32();
|
||||
ind.Machinery = r.ReadInt32();
|
||||
ind.PartsOrdered = r.ReadInt32();
|
||||
ind.PartsShort = r.ReadBool();
|
||||
ind.StoppedUntilDay = r.ReadInt64();
|
||||
ind.StopKind = (IndustryStop)r.ReadByte();
|
||||
ind.PendingWork = (IndustryWork)r.ReadByte();
|
||||
ind.MonthsHighMargin = r.ReadInt32();
|
||||
ind.MaintenanceSpentYear = r.ReadInt64();
|
||||
ind.AcquiredDay = r.ReadInt64();
|
||||
int history = r.ReadInt32();
|
||||
for (int h = 0; h < history; h++) ind.StopHistory.Add(new IndustryStopRecord(r.ReadInt64(), r.ReadInt32(), (IndustryStop)r.ReadByte()));
|
||||
}
|
||||
|
||||
int companies = r.ReadInt32();
|
||||
for (int i = 0; i < companies; i++)
|
||||
{
|
||||
var c = w.Companies[i];
|
||||
var sh = c.Shares;
|
||||
sh.Total = r.ReadInt64();
|
||||
sh.Founder = r.ReadInt64();
|
||||
sh.Public = r.ReadInt64();
|
||||
sh.Treasury = r.ReadInt64();
|
||||
sh.ByCompany = r.ReadInt64Array();
|
||||
c.Listed = r.ReadBool();
|
||||
c.SharePrice = r.ReadDouble();
|
||||
c.Sentiment = r.ReadDouble();
|
||||
c.ExtraSentiment = r.ReadDouble();
|
||||
CopyInto(r.ReadInt64Array(), c.ProfitRing);
|
||||
c.ProfitRingIndex = r.ReadInt32();
|
||||
CopyInto(r.ReadInt64Array(), c.DividendRing);
|
||||
c.LastAnnualProfit = r.ReadInt64();
|
||||
c.PortfolioValue = new Money(r.ReadInt64());
|
||||
c.ControlledBy = new CompanyId(r.ReadInt32());
|
||||
c.ControlSinceDay = r.ReadInt64();
|
||||
c.Merged = r.ReadBool();
|
||||
c.MergedInto = new CompanyId(r.ReadInt32());
|
||||
c.Receivership = r.ReadBool();
|
||||
c.ReceivershipUntilDay = r.ReadInt64();
|
||||
c.ReceivershipBy = new CompanyId(r.ReadInt32());
|
||||
c.BankruptDay = r.ReadInt64();
|
||||
c.Liquidated = r.ReadBool();
|
||||
c.PriceHistory.AddRange(r.ReadDoubleArray());
|
||||
int years = r.ReadInt32();
|
||||
for (int y = 0; y < years; y++)
|
||||
{
|
||||
c.History.Add(new CompanyYear(r.ReadInt32(), r.ReadInt64(), r.ReadInt64(), r.ReadInt64(), r.ReadInt64(),
|
||||
r.ReadInt32(), r.ReadInt32(), r.ReadDouble(), r.ReadInt64()));
|
||||
}
|
||||
int milestones = r.ReadInt32();
|
||||
for (int m = 0; m < milestones; m++) c.Milestones.Add(new CompanyMilestone(r.ReadInt64(), r.ReadRequiredString()));
|
||||
}
|
||||
|
||||
int bids = r.ReadInt32();
|
||||
for (int i = 0; i < bids; i++)
|
||||
{
|
||||
var b = new TakeoverBid
|
||||
{
|
||||
Id = new BidId(r.ReadInt32()),
|
||||
Bidder = new CompanyId(r.ReadInt32()),
|
||||
Target = new CompanyId(r.ReadInt32()),
|
||||
PricePerShare = r.ReadInt64(),
|
||||
TargetShare = r.ReadDouble(),
|
||||
StartDay = r.ReadInt64(),
|
||||
EndDay = r.ReadInt64(),
|
||||
};
|
||||
b.State = (BidState)r.ReadByte();
|
||||
b.TenderedPublic = r.ReadInt64();
|
||||
b.TenderedFounder = r.ReadInt64();
|
||||
foreach (int a in r.ReadInt32Array()) b.Accepted.Add(new CompanyId(a));
|
||||
b.Defenses = r.ReadByte();
|
||||
b.Knight = new CompanyId(r.ReadInt32());
|
||||
b.ClosedDay = r.ReadInt64();
|
||||
w.Bids.Add(b);
|
||||
}
|
||||
|
||||
int auctions = r.ReadInt32();
|
||||
for (int i = 0; i < auctions; i++)
|
||||
{
|
||||
var a = new Auction
|
||||
{
|
||||
Seller = new CompanyId(r.ReadInt32()),
|
||||
StartDay = r.ReadInt64(),
|
||||
EndDay = r.ReadInt64(),
|
||||
MinPriceCents = r.ReadInt64(),
|
||||
};
|
||||
a.BestBidder = new CompanyId(r.ReadInt32());
|
||||
a.BestPriceCents = r.ReadInt64();
|
||||
a.Closed = r.ReadBool();
|
||||
w.Auctions.Add(a);
|
||||
}
|
||||
|
||||
if (r.ReadBool())
|
||||
{
|
||||
w.GameOver = new GameOverState
|
||||
{
|
||||
Day = r.ReadInt64(),
|
||||
Reason = (GameOverReason)r.ReadByte(),
|
||||
Text = r.ReadRequiredString(),
|
||||
Winner = new CompanyId(r.ReadInt32()),
|
||||
};
|
||||
}
|
||||
w.LastCompetitorYear = r.ReadInt32();
|
||||
w.AiPaused = r.ReadBool();
|
||||
|
||||
int states = r.ReadInt32();
|
||||
for (int i = 0; i < states; i++)
|
||||
{
|
||||
var a = new AiState { Company = new CompanyId(r.ReadInt32()), PlanDay = r.ReadInt32() };
|
||||
a.Issued = r.ReadInt64();
|
||||
a.Rejected = r.ReadInt64();
|
||||
a.PolicySet = r.ReadBool();
|
||||
a.NextProjectId = r.ReadInt32();
|
||||
a.LastIndustryYear = r.ReadInt32();
|
||||
a.LastGridlockDay = r.ReadInt64();
|
||||
a.Failed.AddRange(r.ReadInt64Array());
|
||||
a.Responded.AddRange(r.ReadInt32Array());
|
||||
int projects = r.ReadInt32();
|
||||
for (int p = 0; p < projects; p++)
|
||||
{
|
||||
int id = r.ReadInt32();
|
||||
var kind = (AiProjectKind)r.ReadByte();
|
||||
int good = r.ReadInt32(), paxClass = r.ReadInt32();
|
||||
long key = r.ReadInt64();
|
||||
var stationA = new StationId(r.ReadInt32());
|
||||
var stationB = new StationId(r.ReadInt32());
|
||||
var depot = new DepotId(r.ReadInt32());
|
||||
int[] edges = r.ReadInt32Array();
|
||||
var line = new LineId(r.ReadInt32());
|
||||
int[] trains = r.ReadInt32Array();
|
||||
var stage = (AiProjectStage)r.ReadByte();
|
||||
var project = new AiProject
|
||||
{
|
||||
Id = id, Kind = kind, Good = good, PaxClass = paxClass, Key = key,
|
||||
StationA = stationA, StationB = stationB, Depot = depot, Line = line, Stage = stage,
|
||||
StartDay = r.ReadInt64(),
|
||||
CapitalCents = r.ReadInt64(),
|
||||
RevenuePerYearCents = r.ReadInt64(),
|
||||
TrainsPlanned = r.ReadInt32(),
|
||||
Loco = r.ReadRequiredString(),
|
||||
Car = r.ReadRequiredString(),
|
||||
CarsPerTrain = r.ReadInt32(),
|
||||
GrowAfterDay = r.ReadInt64(),
|
||||
SupplyFor = new DepotId(r.ReadInt32()),
|
||||
};
|
||||
foreach (int e in edges) project.Edges.Add(new EdgeId(e));
|
||||
foreach (int t in trains) project.Trains.Add(new TrainId(t));
|
||||
a.Projects.Add(project);
|
||||
}
|
||||
w.AiStates.Add(a);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Migrazione dei salvataggi precedenti a F5: ricambi iniziali delle industrie, libro dei soci
|
||||
/// come in una partita nuova, prezzi delle azioni, agenti IA senza progetti.
|
||||
/// </summary>
|
||||
public static void Migrate(World w)
|
||||
{
|
||||
var im = w.Data.Balance.IndustryMaintenance;
|
||||
foreach (var ind in w.Industries)
|
||||
{
|
||||
ind.SpareParts = (int)Math.Round(Simulation.Systems.IndustryMath.MonthlyPartsUse(ind, im) * im.InitialPartsMonths);
|
||||
w.Goods.Produced[w.Data.SparePartsGood] += ind.SpareParts;
|
||||
}
|
||||
var d = w.Difficulty;
|
||||
foreach (var c in w.Companies)
|
||||
{
|
||||
if (c.IsPlayer) Corporate.InitShares(w, c, d.PublicFromStart, d.PublicFromStart ? d.PlayerShareAtStart : 1.0);
|
||||
else Corporate.InitShares(w, c, listed: true, 1 - w.Data.Balance.StockMarket.AiFloatAtStart);
|
||||
}
|
||||
AiSystem.EnsureStates(w);
|
||||
}
|
||||
|
||||
private static void CopyInto(long[] source, long[] target) => Array.Copy(source, target, Math.Min(source.Length, target.Length));
|
||||
}
|
||||
@@ -0,0 +1,325 @@
|
||||
using RailEnterprise.Core.Common;
|
||||
using RailEnterprise.Core.Data;
|
||||
using RailEnterprise.Core.Economy;
|
||||
using RailEnterprise.Core.Rail;
|
||||
using RailEnterprise.Core.Simulation;
|
||||
using RailEnterprise.Core.Simulation.Systems;
|
||||
|
||||
namespace RailEnterprise.Core.Persistence;
|
||||
|
||||
/// <summary>
|
||||
/// Stato della fase F4 (corpo del salvataggio dalla versione 2): manutenzione di veicoli e treni,
|
||||
/// officine e personale dei depositi, politiche, convenzioni e motivi dei posti scoperti.
|
||||
/// I salvataggi della versione 1 si migrano con valori iniziali coerenti.
|
||||
/// </summary>
|
||||
public static class MaintenanceSerializer
|
||||
{
|
||||
public static void Write(World w, SaveWriter s)
|
||||
{
|
||||
foreach (var c in w.Companies) s.Write((byte)c.MaintenancePolicy);
|
||||
foreach (var l in w.Lines) WritePolicy(s, l.Policy);
|
||||
|
||||
s.Write(w.Vehicles.Count);
|
||||
foreach (var v in w.Vehicles)
|
||||
{
|
||||
s.Write(v.WearEma);
|
||||
s.Write(v.LastInspectionDay);
|
||||
s.Write(v.LastInspectionKm);
|
||||
s.Write(v.LastServiceDay);
|
||||
s.Write(v.LastServiceKm);
|
||||
s.Write(v.LastOverhaulDay);
|
||||
s.Write(v.AgeFactorOffset);
|
||||
s.Write(v.HazardReducedUntilDay);
|
||||
}
|
||||
|
||||
s.Write(w.Trains.Count);
|
||||
foreach (var t in w.Trains)
|
||||
{
|
||||
s.Write(t.DayKm);
|
||||
s.Write(t.DayGradeKm);
|
||||
s.Write(t.DayFastKm);
|
||||
s.Write(t.DayLoadKm);
|
||||
s.Write(t.ServiceKmPerDay);
|
||||
s.Write(t.CapacityT);
|
||||
s.Write(t.SpeedFactor);
|
||||
s.Write(t.SpeedCapKmh);
|
||||
WritePolicy(s, t.Policy);
|
||||
s.Write(t.MaintenanceDepot.Value);
|
||||
s.Write(t.PlannedIntervention);
|
||||
s.Write(t.InWorkshop);
|
||||
s.Write((byte)t.Failure);
|
||||
s.Write(t.FailedVehicle.Value);
|
||||
s.Write(t.FailedComponent);
|
||||
s.Write(t.RescueTick);
|
||||
s.Write((byte)t.StateBeforeFailure);
|
||||
s.Write(t.Towed);
|
||||
s.Write(t.CoveredBy.Value);
|
||||
s.Write(t.Covering.Value);
|
||||
s.Write(t.AlertLevel);
|
||||
s.Write(t.MaintenanceThisYear);
|
||||
s.Write(t.MaintenanceLastYear);
|
||||
s.Write(t.FailuresThisYear);
|
||||
s.Write(t.FailuresTotal);
|
||||
s.Write(t.History.Count);
|
||||
foreach (var h in t.History)
|
||||
{
|
||||
s.Write(h.Day);
|
||||
s.Write(h.Intervention);
|
||||
s.Write(h.CostCents);
|
||||
s.Write(h.Depot.Value);
|
||||
s.Write(h.Parts);
|
||||
}
|
||||
}
|
||||
|
||||
s.Write(w.Network.Depots.Count);
|
||||
foreach (var d in w.Network.Depots)
|
||||
{
|
||||
s.Write(d.PartsTarget);
|
||||
s.Write(d.Stalls);
|
||||
s.Write(d.JobsWorkers);
|
||||
s.Write(d.JobsStaff);
|
||||
WriteWorkplace(s, d);
|
||||
s.Write(d.PartsAlerted);
|
||||
s.Write(d.Jobs.Count);
|
||||
foreach (var j in d.Jobs)
|
||||
{
|
||||
s.Write(j.Train.Value);
|
||||
s.Write(j.Intervention);
|
||||
s.Write(j.QueuedDay);
|
||||
s.Write(j.Days);
|
||||
s.Write(j.Parts);
|
||||
s.Write(j.InStall);
|
||||
s.Write(j.Started);
|
||||
s.Write(j.Progress);
|
||||
s.Write(j.CostCents);
|
||||
}
|
||||
}
|
||||
|
||||
s.Write(w.Network.Stations.Count);
|
||||
foreach (var st in w.Network.Stations)
|
||||
{
|
||||
s.Write(st.Depots.Count);
|
||||
foreach (var d in st.Depots) s.Write(d.Value);
|
||||
}
|
||||
|
||||
s.Write(w.Network.Edges.Count);
|
||||
foreach (var e in w.Network.Edges) s.Write(e.DamagedUntilDay);
|
||||
|
||||
s.Write(w.Industries.Count);
|
||||
foreach (var ind in w.Industries)
|
||||
{
|
||||
s.Write((byte)ind.WorkersReason);
|
||||
s.Write((byte)ind.StaffReason);
|
||||
s.Write(ind.WorkersReasonLine.Value);
|
||||
s.Write(ind.StaffReasonLine.Value);
|
||||
}
|
||||
|
||||
s.Write(w.Agreements.Count);
|
||||
foreach (var a in w.Agreements)
|
||||
{
|
||||
s.Write(a.Id.Value);
|
||||
s.Write(a.Industry.Value);
|
||||
s.Write(a.Carrier.Value);
|
||||
s.Write(a.SeatsPerDay);
|
||||
s.Write(a.MonthlyFee.Cents);
|
||||
s.Write(a.ProposedDay);
|
||||
s.Write((byte)a.State);
|
||||
s.Write(a.StartDay);
|
||||
s.Write(a.EndDay);
|
||||
s.Write(a.Coverage);
|
||||
s.Write(a.ShortDays);
|
||||
s.Write(a.ArrivedToday);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Read(World w, SaveReader r)
|
||||
{
|
||||
foreach (var c in w.Companies) c.MaintenancePolicy = (MaintenancePolicy)r.ReadByte();
|
||||
foreach (var l in w.Lines) l.Policy = ReadPolicy(r);
|
||||
|
||||
Expect(r, w.Vehicles.Count, "veicoli");
|
||||
foreach (var v in w.Vehicles)
|
||||
{
|
||||
v.WearEma = r.ReadDoubleArray();
|
||||
v.LastInspectionDay = r.ReadInt64();
|
||||
v.LastInspectionKm = r.ReadDouble();
|
||||
v.LastServiceDay = r.ReadInt64();
|
||||
v.LastServiceKm = r.ReadDouble();
|
||||
v.LastOverhaulDay = r.ReadInt64();
|
||||
v.AgeFactorOffset = r.ReadDouble();
|
||||
v.HazardReducedUntilDay = r.ReadInt64();
|
||||
}
|
||||
|
||||
Expect(r, w.Trains.Count, "treni");
|
||||
foreach (var t in w.Trains)
|
||||
{
|
||||
t.DayKm = r.ReadDouble();
|
||||
t.DayGradeKm = r.ReadDouble();
|
||||
t.DayFastKm = r.ReadDouble();
|
||||
t.DayLoadKm = r.ReadDouble();
|
||||
t.ServiceKmPerDay = r.ReadDouble();
|
||||
t.CapacityT = r.ReadDouble();
|
||||
t.SpeedFactor = r.ReadDouble();
|
||||
t.SpeedCapKmh = r.ReadDouble();
|
||||
t.Policy = ReadPolicy(r);
|
||||
t.MaintenanceDepot = new DepotId(r.ReadInt32());
|
||||
t.PlannedIntervention = r.ReadInt32();
|
||||
t.InWorkshop = r.ReadBool();
|
||||
t.Failure = (FailureKind)r.ReadByte();
|
||||
t.FailedVehicle = new VehicleId(r.ReadInt32());
|
||||
t.FailedComponent = r.ReadInt32();
|
||||
t.RescueTick = r.ReadInt64();
|
||||
t.StateBeforeFailure = (TrainState)r.ReadByte();
|
||||
t.Towed = r.ReadBool();
|
||||
t.CoveredBy = new TrainId(r.ReadInt32());
|
||||
t.Covering = new TrainId(r.ReadInt32());
|
||||
t.AlertLevel = r.ReadInt32();
|
||||
t.MaintenanceThisYear = r.ReadInt64();
|
||||
t.MaintenanceLastYear = r.ReadInt64();
|
||||
t.FailuresThisYear = r.ReadInt32();
|
||||
t.FailuresTotal = r.ReadInt32();
|
||||
int history = r.ReadInt32();
|
||||
for (int k = 0; k < history; k++)
|
||||
t.History.Add(new MaintenanceRecord(r.ReadInt64(), r.ReadInt32(), r.ReadInt64(), new DepotId(r.ReadInt32()), r.ReadInt32()));
|
||||
}
|
||||
|
||||
Expect(r, w.Network.Depots.Count, "depositi");
|
||||
foreach (var d in w.Network.Depots)
|
||||
{
|
||||
d.PartsTarget = r.ReadInt32();
|
||||
d.Stalls = r.ReadInt32();
|
||||
d.JobsWorkers = r.ReadInt32();
|
||||
d.JobsStaff = r.ReadInt32();
|
||||
ReadWorkplace(r, d);
|
||||
d.PartsAlerted = r.ReadBool();
|
||||
int jobs = r.ReadInt32();
|
||||
for (int k = 0; k < jobs; k++)
|
||||
{
|
||||
d.Jobs.Add(new WorkshopJob
|
||||
{
|
||||
Train = new TrainId(r.ReadInt32()),
|
||||
Intervention = r.ReadInt32(),
|
||||
QueuedDay = r.ReadInt64(),
|
||||
Days = r.ReadDouble(),
|
||||
Parts = r.ReadInt32(),
|
||||
InStall = r.ReadBool(),
|
||||
Started = r.ReadBool(),
|
||||
Progress = r.ReadDouble(),
|
||||
CostCents = r.ReadInt64(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Expect(r, w.Network.Stations.Count, "stazioni");
|
||||
foreach (var st in w.Network.Stations)
|
||||
{
|
||||
int n = r.ReadInt32();
|
||||
for (int k = 0; k < n; k++) st.Depots.Add(new DepotId(r.ReadInt32()));
|
||||
}
|
||||
|
||||
Expect(r, w.Network.Edges.Count, "archi");
|
||||
foreach (var e in w.Network.Edges) e.DamagedUntilDay = r.ReadInt64();
|
||||
|
||||
Expect(r, w.Industries.Count, "industrie");
|
||||
foreach (var ind in w.Industries)
|
||||
{
|
||||
ind.WorkersReason = (UncoveredReason)r.ReadByte();
|
||||
ind.StaffReason = (UncoveredReason)r.ReadByte();
|
||||
ind.WorkersReasonLine = new LineId(r.ReadInt32());
|
||||
ind.StaffReasonLine = new LineId(r.ReadInt32());
|
||||
}
|
||||
|
||||
int agreements = r.ReadInt32();
|
||||
for (int k = 0; k < agreements; k++)
|
||||
{
|
||||
var a = new CommuteAgreement
|
||||
{
|
||||
Id = new AgreementId(r.ReadInt32()),
|
||||
Industry = new IndustryId(r.ReadInt32()),
|
||||
Carrier = new CompanyId(r.ReadInt32()),
|
||||
SeatsPerDay = r.ReadInt32(),
|
||||
MonthlyFee = new Money(r.ReadInt64()),
|
||||
ProposedDay = r.ReadInt64(),
|
||||
};
|
||||
a.State = (AgreementState)r.ReadByte();
|
||||
a.StartDay = r.ReadInt64();
|
||||
a.EndDay = r.ReadInt64();
|
||||
a.Coverage = r.ReadDouble();
|
||||
a.ShortDays = r.ReadInt32();
|
||||
a.ArrivedToday = r.ReadInt32();
|
||||
w.Agreements.Add(a);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Migrazione dalla versione 1: officine e posti di lavoro dai dati, intervalli che partono
|
||||
/// dall'acquisto, scorta iniziale dei depositi contabilizzata, bacini con i depositi.
|
||||
/// </summary>
|
||||
public static void Migrate(World w)
|
||||
{
|
||||
var dp = w.Data.Maintenance.Depots;
|
||||
foreach (var v in w.Vehicles)
|
||||
{
|
||||
v.WearEma = new double[v.Components.Length];
|
||||
v.LastInspectionDay = v.LastServiceDay = v.LastOverhaulDay = v.BoughtDay;
|
||||
v.LastInspectionKm = v.LastServiceKm = 0;
|
||||
}
|
||||
foreach (var t in w.Trains)
|
||||
{
|
||||
if (!t.Removed && t.Vehicles.Count > 0) RailCommandHandlers.RecomputeTrain(w, t);
|
||||
}
|
||||
foreach (var d in w.Network.Depots)
|
||||
{
|
||||
RailCommandHandlers.InitDepot(w.Data, d);
|
||||
d.PartsTarget = dp.TargetSpareParts;
|
||||
w.Goods.Imported[w.Data.SparePartsGood] += d.SpareParts;
|
||||
}
|
||||
double km = w.Data.Construction.Stations.CatchmentKm * 1000;
|
||||
foreach (var st in w.Network.Stations)
|
||||
{
|
||||
if (!w.Network.IsAlive(st)) continue;
|
||||
foreach (var d in w.Network.Depots)
|
||||
{
|
||||
if (d.Owner == st.Owner && w.Network.IsAlive(d) && Vec2.Distance(d.Position, st.Position) <= km) st.Depots.Add(d.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteWorkplace(SaveWriter s, IWorkplace p)
|
||||
{
|
||||
s.Write(p.ContractedWorkers);
|
||||
s.Write(p.ContractedStaff);
|
||||
s.Write(p.WorkersPresent);
|
||||
s.Write(p.StaffPresent);
|
||||
s.Write((byte)p.WorkersReason);
|
||||
s.Write((byte)p.StaffReason);
|
||||
s.Write(p.WorkersReasonLine.Value);
|
||||
s.Write(p.StaffReasonLine.Value);
|
||||
}
|
||||
|
||||
private static void ReadWorkplace(SaveReader r, IWorkplace p)
|
||||
{
|
||||
p.ContractedWorkers = r.ReadInt32();
|
||||
p.ContractedStaff = r.ReadInt32();
|
||||
p.WorkersPresent = r.ReadDouble();
|
||||
p.StaffPresent = r.ReadDouble();
|
||||
p.WorkersReason = (UncoveredReason)r.ReadByte();
|
||||
p.StaffReason = (UncoveredReason)r.ReadByte();
|
||||
p.WorkersReasonLine = new LineId(r.ReadInt32());
|
||||
p.StaffReasonLine = new LineId(r.ReadInt32());
|
||||
}
|
||||
|
||||
private static void WritePolicy(SaveWriter s, MaintenancePolicy? p) => s.Write(p is { } v ? (byte)v : byte.MaxValue);
|
||||
|
||||
private static MaintenancePolicy? ReadPolicy(SaveReader r)
|
||||
{
|
||||
byte b = r.ReadByte();
|
||||
return b == byte.MaxValue ? null : (MaintenancePolicy)b;
|
||||
}
|
||||
|
||||
private static void Expect(SaveReader r, int expected, string what)
|
||||
{
|
||||
int n = r.ReadInt32();
|
||||
if (n != expected) throw new InvalidDataException($"Salvataggio incoerente: {n} {what} nella sezione di manutenzione, {expected} attesi");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,543 @@
|
||||
using RailEnterprise.Core.Common;
|
||||
using RailEnterprise.Core.Rail;
|
||||
using RailEnterprise.Core.Simulation;
|
||||
|
||||
namespace RailEnterprise.Core.Persistence;
|
||||
|
||||
/// <summary>Serializzazione dello stato ferroviario: rete, cantieri, veicoli, treni, linee, pacchetti.</summary>
|
||||
public static class RailSerializer
|
||||
{
|
||||
public static void Write(World w, SaveWriter s)
|
||||
{
|
||||
var net = w.Network;
|
||||
s.Write(net.NextProject);
|
||||
s.Write(net.Nodes.Count);
|
||||
foreach (var n in net.Nodes)
|
||||
{
|
||||
s.Write(n.Position.X);
|
||||
s.Write(n.Position.Y);
|
||||
s.Write(n.HeightM);
|
||||
s.Write(n.Owner.Value);
|
||||
s.Write(n.Station.Value);
|
||||
s.Write(n.Depot.Value);
|
||||
s.Write(n.Removed);
|
||||
s.Write(n.Edges.Count);
|
||||
foreach (var e in n.Edges) s.Write(e.Value);
|
||||
}
|
||||
|
||||
s.Write(net.Edges.Count);
|
||||
foreach (var e in net.Edges)
|
||||
{
|
||||
s.Write(e.A.Value);
|
||||
s.Write(e.B.Value);
|
||||
s.Write(e.Owner.Value);
|
||||
s.Write(e.TrackClass);
|
||||
s.Write(e.DoubleTrack);
|
||||
s.Write(e.Electrified);
|
||||
s.Write((byte)e.State);
|
||||
s.Write(e.S);
|
||||
s.Write(e.X);
|
||||
s.Write(e.Y);
|
||||
s.Write(e.H);
|
||||
s.Write(e.Ground);
|
||||
s.Write(e.Kinds.Length);
|
||||
foreach (var k in e.Kinds) s.Write((byte)k);
|
||||
s.Write(e.SpeedLimitKmh);
|
||||
s.Write(e.Condition);
|
||||
s.Write(e.StructureCondition);
|
||||
s.Write(e.BuiltDay);
|
||||
s.Write(e.LastRenewalDay);
|
||||
s.Write(e.LastInspectionDay);
|
||||
s.Write(e.BuildCostCents);
|
||||
s.Write(e.Project);
|
||||
s.Write(e.Signals.Count);
|
||||
foreach (double sig in e.Signals) s.Write(sig);
|
||||
s.Write(e.LockDir);
|
||||
s.Write(e.LockCount);
|
||||
s.Write(e.BlocksForward);
|
||||
s.Write(e.BlocksBackward);
|
||||
s.Write(e.WearToday);
|
||||
s.Write(e.TonnesThisYear);
|
||||
}
|
||||
|
||||
s.Write(net.Stations.Count);
|
||||
foreach (var st in net.Stations)
|
||||
{
|
||||
s.Write(st.Owner.Value);
|
||||
s.Write(st.Name);
|
||||
s.Write(st.Node.Value);
|
||||
s.Write(st.Position.X);
|
||||
s.Write(st.Position.Y);
|
||||
s.Write(st.Platforms);
|
||||
s.Write(st.BuiltDay);
|
||||
s.Write(st.BuildCostCents);
|
||||
s.Write(st.PlatformTrain);
|
||||
s.Write(st.Incoming);
|
||||
WriteIds(s, st.Industries.Select(i => i.Value));
|
||||
WriteIds(s, st.Towns.Select(i => i.Value));
|
||||
WriteIds(s, st.Hubs.Select(i => i.Value));
|
||||
WritePackets(s, st.Waiting);
|
||||
}
|
||||
|
||||
s.Write(net.Depots.Count);
|
||||
foreach (var d in net.Depots)
|
||||
{
|
||||
s.Write(d.Owner.Value);
|
||||
s.Write(d.Name);
|
||||
s.Write(d.Node.Value);
|
||||
s.Write(d.Position.X);
|
||||
s.Write(d.Position.Y);
|
||||
s.Write(d.Size);
|
||||
s.Write(d.BuiltDay);
|
||||
s.Write(d.BuildCostCents);
|
||||
s.Write(d.SpareParts);
|
||||
}
|
||||
|
||||
s.Write(net.Projects.Count);
|
||||
foreach (var p in net.Projects)
|
||||
{
|
||||
s.Write(p.Id);
|
||||
s.Write(p.Owner.Value);
|
||||
s.Write((byte)p.Kind);
|
||||
WriteIds(s, p.Edges.Select(e => e.Value));
|
||||
s.Write(p.TotalDays);
|
||||
s.Write(p.ProgressDays);
|
||||
s.Write(p.Crew);
|
||||
s.Write(p.QueuedDay);
|
||||
s.Write(p.StartedDay);
|
||||
s.Write(p.Done);
|
||||
s.Write(p.UpgradeClass);
|
||||
s.Write(p.UpgradeDouble);
|
||||
s.Write(p.UpgradeElectrified);
|
||||
s.Write(p.Works.Count);
|
||||
foreach (var work in p.Works)
|
||||
{
|
||||
s.Write((byte)work.Kind);
|
||||
s.Write(work.StartM);
|
||||
s.Write(work.EndM);
|
||||
s.Write(work.MaxHeightM);
|
||||
s.Write(work.CostCents);
|
||||
s.Write(work.Days);
|
||||
s.Write(work.BridgeType);
|
||||
}
|
||||
}
|
||||
|
||||
s.Write(w.Vehicles.Count);
|
||||
foreach (var v in w.Vehicles)
|
||||
{
|
||||
s.Write(v.Def.Id);
|
||||
s.Write(v.Owner.Value);
|
||||
s.Write(v.BoughtDay);
|
||||
s.Write(v.Train.Value);
|
||||
s.Write(v.Depot.Value);
|
||||
s.Write(v.Sold);
|
||||
s.Write(v.Components);
|
||||
s.Write(v.OdometerKm);
|
||||
s.Write(v.Good);
|
||||
s.Write(v.Load);
|
||||
s.Write(v.Pax);
|
||||
}
|
||||
|
||||
s.Write(w.Lines.Count);
|
||||
foreach (var l in w.Lines)
|
||||
{
|
||||
s.Write(l.Owner.Value);
|
||||
s.Write(l.Name);
|
||||
s.Write(l.Color);
|
||||
s.Write(l.Removed);
|
||||
s.Write(l.Stops.Count);
|
||||
foreach (var st in l.Stops)
|
||||
{
|
||||
s.Write(st.Station.Value);
|
||||
s.Write(st.Load);
|
||||
s.Write(st.Unload);
|
||||
s.Write(st.WaitFull);
|
||||
s.Write(st.MaxWaitDays);
|
||||
}
|
||||
}
|
||||
|
||||
s.Write(w.Trains.Count);
|
||||
foreach (var t in w.Trains)
|
||||
{
|
||||
s.Write(t.Owner.Value);
|
||||
s.Write(t.Name);
|
||||
WriteIds(s, t.Vehicles.Select(v => v.Value));
|
||||
s.Write(t.HomeDepot.Value);
|
||||
s.Write(t.Line.Value);
|
||||
s.Write(t.StopIndex);
|
||||
s.Write((byte)t.State);
|
||||
s.Write(t.Removed);
|
||||
s.Write(t.Reserve);
|
||||
s.Write(t.GoingToDepot.Value);
|
||||
s.Write(t.EmptyMassT);
|
||||
s.Write(t.LocoMassT);
|
||||
s.Write(t.PowerKw);
|
||||
s.Write(t.TractiveKn);
|
||||
s.Write(t.MaxSpeedKmh);
|
||||
s.Write(t.LengthM);
|
||||
s.Write(t.Cx);
|
||||
s.Write(t.NeedsElectrification);
|
||||
s.Write(t.Locomotives);
|
||||
s.Write(t.AtNode.Value);
|
||||
s.Write(t.ArrivedVia.Value);
|
||||
s.Write(t.Route.Count);
|
||||
foreach (var step in t.Route)
|
||||
{
|
||||
s.Write(step.Edge.Value);
|
||||
s.Write(step.Forward);
|
||||
}
|
||||
s.Write(t.RouteIndex);
|
||||
s.Write(t.StepPos);
|
||||
s.Write(t.Speed);
|
||||
s.Write(t.Odometer);
|
||||
s.Write(t.ReservedStep);
|
||||
s.Write(t.ReservedBlock);
|
||||
s.Write(t.ArrivalPlatform);
|
||||
s.Write(t.Held.Count);
|
||||
foreach (var h in t.Held)
|
||||
{
|
||||
s.Write(h.Kind);
|
||||
s.Write(h.Id);
|
||||
s.Write(h.Index);
|
||||
s.Write(h.Forward);
|
||||
s.Write(h.LeftAt);
|
||||
}
|
||||
s.Write(t.DwellTicks);
|
||||
s.Write(t.ArrivedTick);
|
||||
s.Write(t.Platform);
|
||||
s.Write(t.StallSeconds);
|
||||
s.Write(t.WaitingSinceTick);
|
||||
s.Write(t.WaitWarned);
|
||||
WritePackets(s, t.Cargo);
|
||||
s.Write(t.RevenueThisYear);
|
||||
s.Write(t.RevenueLastYear);
|
||||
s.Write(t.KmThisYear);
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteIds(SaveWriter s, IEnumerable<int> ids)
|
||||
{
|
||||
var list = ids.ToList();
|
||||
s.Write(list.Count);
|
||||
foreach (int i in list) s.Write(i);
|
||||
}
|
||||
|
||||
private static void WritePackets(SaveWriter s, List<Packet> packets)
|
||||
{
|
||||
s.Write(packets.Count);
|
||||
foreach (var p in packets)
|
||||
{
|
||||
s.Write(p.Id);
|
||||
s.Write((byte)p.Kind);
|
||||
s.Write(p.Good);
|
||||
s.Write(p.Units);
|
||||
s.Write(p.Origin.Value);
|
||||
s.Write(p.Destination.Value);
|
||||
s.Write(p.HopLine.Value);
|
||||
s.Write(p.HopEnd.Value);
|
||||
s.Write(p.CreatedTick);
|
||||
s.Write(p.ExpiresTick);
|
||||
s.Write(p.ExpectedDays);
|
||||
s.Write(p.Contract);
|
||||
s.Write(p.ReturnTrip);
|
||||
s.Write(p.AwaitingConsumer);
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ lettura
|
||||
|
||||
public static void Read(World w, SaveReader r, int[] goodMap)
|
||||
{
|
||||
var net = w.Network;
|
||||
int goods = w.Data.GoodCount;
|
||||
int MapGood(int g) => g < 0 ? g : g < goodMap.Length ? goodMap[g] : -1;
|
||||
|
||||
net.NextProject = r.ReadInt32();
|
||||
int nodes = r.ReadInt32();
|
||||
for (int i = 0; i < nodes; i++)
|
||||
{
|
||||
var n = new TrackNode
|
||||
{
|
||||
Id = new NodeId(i),
|
||||
Position = new Vec2(r.ReadDouble(), r.ReadDouble()),
|
||||
HeightM = r.ReadDouble(),
|
||||
Owner = new CompanyId(r.ReadInt32()),
|
||||
Station = new StationId(r.ReadInt32()),
|
||||
Depot = new DepotId(r.ReadInt32()),
|
||||
Removed = r.ReadBool(),
|
||||
};
|
||||
int count = r.ReadInt32();
|
||||
for (int k = 0; k < count; k++) n.Edges.Add(new EdgeId(r.ReadInt32()));
|
||||
net.Nodes.Add(n);
|
||||
}
|
||||
|
||||
int edges = r.ReadInt32();
|
||||
for (int i = 0; i < edges; i++)
|
||||
{
|
||||
var a = new NodeId(r.ReadInt32());
|
||||
var b = new NodeId(r.ReadInt32());
|
||||
var owner = new CompanyId(r.ReadInt32());
|
||||
int cls = r.ReadInt32();
|
||||
bool dbl = r.ReadBool();
|
||||
bool ele = r.ReadBool();
|
||||
var state = (EdgeState)r.ReadByte();
|
||||
var S = r.ReadDoubleArray();
|
||||
var X = r.ReadDoubleArray();
|
||||
var Y = r.ReadDoubleArray();
|
||||
var H = r.ReadDoubleArray();
|
||||
var ground = r.ReadDoubleArray();
|
||||
var kinds = new WorkKind[r.ReadCount(1)];
|
||||
for (int k = 0; k < kinds.Length; k++) kinds[k] = (WorkKind)r.ReadByte();
|
||||
var limits = r.ReadDoubleArray();
|
||||
var e = new TrackEdge
|
||||
{
|
||||
Id = new EdgeId(i),
|
||||
A = a,
|
||||
B = b,
|
||||
Owner = owner,
|
||||
TrackClass = cls,
|
||||
DoubleTrack = dbl,
|
||||
Electrified = ele,
|
||||
State = state,
|
||||
S = S,
|
||||
X = X,
|
||||
Y = Y,
|
||||
H = H,
|
||||
Ground = ground,
|
||||
Kinds = kinds,
|
||||
SpeedLimitKmh = limits,
|
||||
Condition = r.ReadDouble(),
|
||||
StructureCondition = r.ReadDouble(),
|
||||
BuiltDay = r.ReadInt64(),
|
||||
LastRenewalDay = r.ReadInt64(),
|
||||
LastInspectionDay = r.ReadInt64(),
|
||||
BuildCostCents = r.ReadInt64(),
|
||||
Project = r.ReadInt32(),
|
||||
};
|
||||
int signals = r.ReadInt32();
|
||||
for (int k = 0; k < signals; k++) e.Signals.Add(r.ReadDouble());
|
||||
e.LockDir = r.ReadInt32();
|
||||
e.LockCount = r.ReadInt32();
|
||||
e.BlocksForward = r.ReadInt32Array();
|
||||
e.BlocksBackward = r.ReadInt32Array();
|
||||
e.WearToday = r.ReadDouble();
|
||||
e.TonnesThisYear = r.ReadDouble();
|
||||
net.Edges.Add(e);
|
||||
}
|
||||
|
||||
int stations = r.ReadInt32();
|
||||
for (int i = 0; i < stations; i++)
|
||||
{
|
||||
var st = new Station
|
||||
{
|
||||
Id = new StationId(i),
|
||||
Owner = new CompanyId(r.ReadInt32()),
|
||||
Name = r.ReadRequiredString(),
|
||||
Node = new NodeId(r.ReadInt32()),
|
||||
Position = new Vec2(r.ReadDouble(), r.ReadDouble()),
|
||||
Platforms = r.ReadInt32(),
|
||||
BuiltDay = r.ReadInt64(),
|
||||
BuildCostCents = r.ReadInt64(),
|
||||
PlatformTrain = r.ReadInt32Array(),
|
||||
};
|
||||
var incoming = r.ReadInt32Array();
|
||||
st.Incoming = new int[goods];
|
||||
for (int g = 0; g < incoming.Length; g++)
|
||||
{
|
||||
int m = MapGood(g);
|
||||
if (m >= 0) st.Incoming[m] = incoming[g];
|
||||
}
|
||||
foreach (int id in ReadIds(r)) st.Industries.Add(new IndustryId(id));
|
||||
foreach (int id in ReadIds(r)) st.Towns.Add(new TownId(id));
|
||||
foreach (int id in ReadIds(r)) st.Hubs.Add(new HubId(id));
|
||||
ReadPackets(r, st.Waiting, MapGood);
|
||||
net.Stations.Add(st);
|
||||
}
|
||||
|
||||
int depots = r.ReadInt32();
|
||||
for (int i = 0; i < depots; i++)
|
||||
{
|
||||
net.Depots.Add(new Depot
|
||||
{
|
||||
Id = new DepotId(i),
|
||||
Owner = new CompanyId(r.ReadInt32()),
|
||||
Name = r.ReadRequiredString(),
|
||||
Node = new NodeId(r.ReadInt32()),
|
||||
Position = new Vec2(r.ReadDouble(), r.ReadDouble()),
|
||||
Size = r.ReadRequiredString(),
|
||||
BuiltDay = r.ReadInt64(),
|
||||
BuildCostCents = r.ReadInt64(),
|
||||
SpareParts = r.ReadInt32(),
|
||||
});
|
||||
}
|
||||
|
||||
int projects = r.ReadInt32();
|
||||
for (int i = 0; i < projects; i++)
|
||||
{
|
||||
int id = r.ReadInt32();
|
||||
var owner = new CompanyId(r.ReadInt32());
|
||||
var kind = (ProjectKind)r.ReadByte();
|
||||
var edgeIds = ReadIds(r).Select(x => new EdgeId(x)).ToList();
|
||||
double total = r.ReadDouble();
|
||||
double progress = r.ReadDouble();
|
||||
int crew = r.ReadInt32();
|
||||
long queued = r.ReadInt64();
|
||||
long started = r.ReadInt64();
|
||||
bool done = r.ReadBool();
|
||||
int upClass = r.ReadInt32();
|
||||
bool upDouble = r.ReadBool();
|
||||
bool upElectric = r.ReadBool();
|
||||
int works = r.ReadInt32();
|
||||
var list = new List<Work>(works);
|
||||
for (int k = 0; k < works; k++)
|
||||
{
|
||||
list.Add(new Work((WorkKind)r.ReadByte(), r.ReadDouble(), r.ReadDouble(), r.ReadDouble(), r.ReadInt64(), r.ReadInt32(), r.ReadString()));
|
||||
}
|
||||
net.Projects.Add(new ConstructionProject
|
||||
{
|
||||
Id = id,
|
||||
Owner = owner,
|
||||
Kind = kind,
|
||||
Edges = edgeIds,
|
||||
TotalDays = total,
|
||||
ProgressDays = progress,
|
||||
Crew = crew,
|
||||
QueuedDay = queued,
|
||||
StartedDay = started,
|
||||
Done = done,
|
||||
UpgradeClass = upClass,
|
||||
UpgradeDouble = upDouble,
|
||||
UpgradeElectrified = upElectric,
|
||||
Works = list,
|
||||
});
|
||||
}
|
||||
net.Touch();
|
||||
|
||||
int vehicles = r.ReadInt32();
|
||||
for (int i = 0; i < vehicles; i++)
|
||||
{
|
||||
string defId = r.ReadRequiredString();
|
||||
if (!w.Data.TryVehicle(defId, out var def) || def is null) throw new InvalidDataException($"Veicolo «{defId}» assente dai dati correnti");
|
||||
var v = new Vehicle
|
||||
{
|
||||
Id = new VehicleId(i),
|
||||
Def = def,
|
||||
Owner = new CompanyId(r.ReadInt32()),
|
||||
BoughtDay = r.ReadInt64(),
|
||||
Train = new TrainId(r.ReadInt32()),
|
||||
Depot = new DepotId(r.ReadInt32()),
|
||||
Sold = r.ReadBool(),
|
||||
Components = r.ReadDoubleArray(),
|
||||
OdometerKm = r.ReadDouble(),
|
||||
};
|
||||
v.Good = MapGood(r.ReadInt32());
|
||||
v.Load = r.ReadInt32();
|
||||
v.Pax = r.ReadInt32();
|
||||
w.Vehicles.Add(v);
|
||||
}
|
||||
|
||||
int lines = r.ReadInt32();
|
||||
for (int i = 0; i < lines; i++)
|
||||
{
|
||||
var l = new Line
|
||||
{
|
||||
Id = new LineId(i),
|
||||
Owner = new CompanyId(r.ReadInt32()),
|
||||
Name = r.ReadRequiredString(),
|
||||
Color = r.ReadRequiredString(),
|
||||
Removed = r.ReadBool(),
|
||||
};
|
||||
int stops = r.ReadInt32();
|
||||
for (int k = 0; k < stops; k++)
|
||||
l.Stops.Add(new LineStop(new StationId(r.ReadInt32()), r.ReadBool(), r.ReadBool(), r.ReadBool(), r.ReadInt32()));
|
||||
w.Lines.Add(l);
|
||||
}
|
||||
|
||||
int trains = r.ReadInt32();
|
||||
for (int i = 0; i < trains; i++)
|
||||
{
|
||||
var t = new Train
|
||||
{
|
||||
Id = new TrainId(i),
|
||||
Owner = new CompanyId(r.ReadInt32()),
|
||||
Name = r.ReadRequiredString(),
|
||||
};
|
||||
foreach (int v in ReadIds(r)) t.Vehicles.Add(new VehicleId(v));
|
||||
t.HomeDepot = new DepotId(r.ReadInt32());
|
||||
t.Line = new LineId(r.ReadInt32());
|
||||
t.StopIndex = r.ReadInt32();
|
||||
t.State = (TrainState)r.ReadByte();
|
||||
t.Removed = r.ReadBool();
|
||||
t.Reserve = r.ReadBool();
|
||||
t.GoingToDepot = new DepotId(r.ReadInt32());
|
||||
t.EmptyMassT = r.ReadDouble();
|
||||
t.LocoMassT = r.ReadDouble();
|
||||
t.PowerKw = r.ReadDouble();
|
||||
t.TractiveKn = r.ReadDouble();
|
||||
t.MaxSpeedKmh = r.ReadDouble();
|
||||
t.LengthM = r.ReadDouble();
|
||||
t.Cx = r.ReadDouble();
|
||||
t.NeedsElectrification = r.ReadBool();
|
||||
t.Locomotives = r.ReadInt32();
|
||||
t.AtNode = new NodeId(r.ReadInt32());
|
||||
t.ArrivedVia = new EdgeId(r.ReadInt32());
|
||||
int route = r.ReadInt32();
|
||||
for (int k = 0; k < route; k++) t.Route.Add(new RouteStep(new EdgeId(r.ReadInt32()), r.ReadBool()));
|
||||
t.RouteIndex = r.ReadInt32();
|
||||
t.StepPos = r.ReadDouble();
|
||||
t.Speed = r.ReadDouble();
|
||||
t.Odometer = r.ReadDouble();
|
||||
t.ReservedStep = r.ReadInt32();
|
||||
t.ReservedBlock = r.ReadInt32();
|
||||
t.ArrivalPlatform = r.ReadInt32();
|
||||
int held = r.ReadInt32();
|
||||
for (int k = 0; k < held; k++)
|
||||
t.Held.Add(new HeldResource { Kind = r.ReadByte(), Id = r.ReadInt32(), Index = r.ReadInt32(), Forward = r.ReadBool(), LeftAt = r.ReadDouble() });
|
||||
t.DwellTicks = r.ReadDouble();
|
||||
t.ArrivedTick = r.ReadInt64();
|
||||
t.Platform = r.ReadInt32();
|
||||
t.StallSeconds = r.ReadDouble();
|
||||
t.WaitingSinceTick = r.ReadInt64();
|
||||
t.WaitWarned = r.ReadBool();
|
||||
ReadPackets(r, t.Cargo, MapGood);
|
||||
t.RevenueThisYear = r.ReadInt64();
|
||||
t.RevenueLastYear = r.ReadInt64();
|
||||
t.KmThisYear = r.ReadDouble();
|
||||
w.Trains.Add(t);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<int> ReadIds(SaveReader r)
|
||||
{
|
||||
int n = r.ReadCount(4);
|
||||
var list = new List<int>(n);
|
||||
for (int i = 0; i < n; i++) list.Add(r.ReadInt32());
|
||||
return list;
|
||||
}
|
||||
|
||||
private static void ReadPackets(SaveReader r, List<Packet> target, Func<int, int> mapGood)
|
||||
{
|
||||
int n = r.ReadCount(8);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
var p = new Packet
|
||||
{
|
||||
Id = r.ReadInt32(),
|
||||
Kind = (PacketKind)r.ReadByte(),
|
||||
Good = mapGood(r.ReadInt32()),
|
||||
Units = r.ReadInt32(),
|
||||
Origin = new StationId(r.ReadInt32()),
|
||||
Destination = new StationId(r.ReadInt32()),
|
||||
HopLine = new LineId(r.ReadInt32()),
|
||||
HopEnd = new StationId(r.ReadInt32()),
|
||||
CreatedTick = r.ReadInt64(),
|
||||
ExpiresTick = r.ReadInt64(),
|
||||
ExpectedDays = r.ReadDouble(),
|
||||
Contract = r.ReadInt32(),
|
||||
ReturnTrip = r.ReadBool(),
|
||||
AwaitingConsumer = r.ReadBool(),
|
||||
};
|
||||
if (p.Kind != PacketKind.Freight || p.Good >= 0) target.Add(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
using System.IO.Compression;
|
||||
using System.Text;
|
||||
using RailEnterprise.Core.Data;
|
||||
using RailEnterprise.Core.Simulation;
|
||||
|
||||
namespace RailEnterprise.Core.Persistence;
|
||||
|
||||
/// <summary>Informazioni leggibili dall'intestazione senza decomprimere il salvataggio.</summary>
|
||||
public sealed record SaveInfo(int FormatVersion, string GameVersion, ulong DataHash, ulong StateHash, long Tick,
|
||||
string Date, string CompanyName, ulong Seed, int SizeKm, string Difficulty);
|
||||
|
||||
/// <summary>
|
||||
/// File di salvataggio: intestazione «RESV» in chiaro + corpo binario compresso con Brotli.
|
||||
/// Portabile tra Windows e Linux (little endian esplicito, nessuna dipendenza dalla piattaforma).
|
||||
/// </summary>
|
||||
public static class SaveFile
|
||||
{
|
||||
private static readonly byte[] Magic = "RESV"u8.ToArray();
|
||||
public const ushort FormatVersion = 1;
|
||||
|
||||
/// <summary>Serializza e comprime il mondo.</summary>
|
||||
public static byte[] Save(World w, string gameVersion, CompressionLevel level = CompressionLevel.Optimal)
|
||||
{
|
||||
var body = new SaveWriter(1 << 20);
|
||||
WorldSerializer.Write(w, body);
|
||||
|
||||
using var output = new MemoryStream();
|
||||
output.Write(Magic);
|
||||
var header = new SaveWriter(512);
|
||||
header.Write(FormatVersion);
|
||||
header.Write(gameVersion);
|
||||
header.Write(w.Data.Hash);
|
||||
header.Write(w.LastStateHash);
|
||||
header.Write(w.Clock.Tick);
|
||||
header.Write(w.Clock.Date.ToString());
|
||||
header.Write(w.Companies.FirstOrDefault(c => c.IsPlayer)?.Name ?? "");
|
||||
header.Write(w.Settings.Seed);
|
||||
header.Write(w.Settings.SizeKm);
|
||||
header.Write(w.Settings.Difficulty.Id);
|
||||
header.Write(body.Length);
|
||||
output.Write(header.WrittenSpan);
|
||||
using (var brotli = new BrotliStream(output, level, leaveOpen: true))
|
||||
brotli.Write(body.WrittenSpan);
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>Legge solo l'intestazione.</summary>
|
||||
public static SaveInfo ReadInfo(byte[] file) => ReadHeader(file, out _, out _);
|
||||
|
||||
/// <summary>Decomprime e ricostruisce il mondo.</summary>
|
||||
public static World Load(GameData data, byte[] file)
|
||||
{
|
||||
ReadHeader(file, out int bodyOffset, out int bodyLength);
|
||||
var body = new byte[bodyLength];
|
||||
using (var input = new MemoryStream(file, bodyOffset, file.Length - bodyOffset))
|
||||
using (var brotli = new BrotliStream(input, CompressionMode.Decompress))
|
||||
{
|
||||
int read = 0;
|
||||
while (read < bodyLength)
|
||||
{
|
||||
int n = brotli.Read(body, read, bodyLength - read);
|
||||
if (n == 0) throw new InvalidDataException("Corpo del salvataggio troncato");
|
||||
read += n;
|
||||
}
|
||||
}
|
||||
return WorldSerializer.Read(data, new SaveReader(body));
|
||||
}
|
||||
|
||||
public static void SaveToFile(World w, string path, string gameVersion, CompressionLevel level = CompressionLevel.Optimal)
|
||||
{
|
||||
string tmp = path + ".tmp";
|
||||
File.WriteAllBytes(tmp, Save(w, gameVersion, level));
|
||||
File.Move(tmp, path, overwrite: true);
|
||||
}
|
||||
|
||||
public static World LoadFromFile(GameData data, string path) => Load(data, File.ReadAllBytes(path));
|
||||
|
||||
private static SaveInfo ReadHeader(byte[] file, out int bodyOffset, out int bodyLength)
|
||||
{
|
||||
if (file.Length < 8 || !file.AsSpan(0, 4).SequenceEqual(Magic))
|
||||
throw new InvalidDataException("Non è un salvataggio di Rail Enterprise");
|
||||
var r = new SaveReader(file, 4);
|
||||
ushort version = r.ReadUInt16();
|
||||
if (version > FormatVersion) throw new InvalidDataException($"Formato di salvataggio più recente del gioco ({version})");
|
||||
var info = new SaveInfo(version, r.ReadRequiredString(), r.ReadUInt64(), r.ReadUInt64(), r.ReadInt64(),
|
||||
r.ReadRequiredString(), r.ReadRequiredString(), r.ReadUInt64(), r.ReadInt32(), r.ReadRequiredString());
|
||||
bodyLength = r.ReadInt32();
|
||||
bodyOffset = HeaderLength(file);
|
||||
return info;
|
||||
}
|
||||
|
||||
// ripercorre l'intestazione per trovarne la fine
|
||||
private static int HeaderLength(byte[] file)
|
||||
{
|
||||
int pos = 4 + 2;
|
||||
pos = SkipString(file, pos);
|
||||
pos += 8 + 8 + 8;
|
||||
pos = SkipString(file, pos);
|
||||
pos = SkipString(file, pos);
|
||||
pos += 8 + 4;
|
||||
pos = SkipString(file, pos);
|
||||
pos += 4;
|
||||
return pos;
|
||||
}
|
||||
|
||||
private static int SkipString(byte[] file, int pos)
|
||||
{
|
||||
int len = BitConverter.ToInt32(file, pos);
|
||||
return pos + 4 + Math.Max(0, len);
|
||||
}
|
||||
|
||||
/// <summary>Nome di file sicuro da un nome libero.</summary>
|
||||
public static string SafeFileName(string name)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
foreach (char ch in name)
|
||||
sb.Append(char.IsLetterOrDigit(ch) || ch is '-' or '_' ? ch : '_');
|
||||
return sb.Length == 0 ? "salvataggio" : sb.ToString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
using System.Buffers.Binary;
|
||||
using System.Text;
|
||||
|
||||
namespace RailEnterprise.Core.Persistence;
|
||||
|
||||
/// <summary>
|
||||
/// Scrittore binario little endian su buffer crescente e riutilizzabile (nessuna reflection).
|
||||
/// Usato per i salvataggi e, con lo stesso formato, per l'hash di stato giornaliero.
|
||||
/// </summary>
|
||||
public sealed class SaveWriter
|
||||
{
|
||||
private byte[] _buffer;
|
||||
private int _position;
|
||||
|
||||
public SaveWriter(int initialCapacity = 64 * 1024) => _buffer = new byte[initialCapacity];
|
||||
|
||||
public int Length => _position;
|
||||
|
||||
public ReadOnlySpan<byte> WrittenSpan => _buffer.AsSpan(0, _position);
|
||||
|
||||
public byte[] ToArray() => _buffer.AsSpan(0, _position).ToArray();
|
||||
|
||||
public void Reset() => _position = 0;
|
||||
|
||||
private Span<byte> Take(int count)
|
||||
{
|
||||
if (_position + count > _buffer.Length)
|
||||
{
|
||||
int size = Math.Max(_buffer.Length * 2, _position + count);
|
||||
Array.Resize(ref _buffer, size);
|
||||
}
|
||||
var span = _buffer.AsSpan(_position, count);
|
||||
_position += count;
|
||||
return span;
|
||||
}
|
||||
|
||||
public void Write(byte v) => Take(1)[0] = v;
|
||||
public void Write(bool v) => Take(1)[0] = v ? (byte)1 : (byte)0;
|
||||
public void Write(short v) => BinaryPrimitives.WriteInt16LittleEndian(Take(2), v);
|
||||
public void Write(ushort v) => BinaryPrimitives.WriteUInt16LittleEndian(Take(2), v);
|
||||
public void Write(int v) => BinaryPrimitives.WriteInt32LittleEndian(Take(4), v);
|
||||
public void Write(uint v) => BinaryPrimitives.WriteUInt32LittleEndian(Take(4), v);
|
||||
public void Write(long v) => BinaryPrimitives.WriteInt64LittleEndian(Take(8), v);
|
||||
public void Write(ulong v) => BinaryPrimitives.WriteUInt64LittleEndian(Take(8), v);
|
||||
public void Write(double v) => BinaryPrimitives.WriteInt64LittleEndian(Take(8), BitConverter.DoubleToInt64Bits(v));
|
||||
public void Write(float v) => BinaryPrimitives.WriteInt32LittleEndian(Take(4), BitConverter.SingleToInt32Bits(v));
|
||||
|
||||
public void Write(string? v)
|
||||
{
|
||||
if (v is null)
|
||||
{
|
||||
Write(-1);
|
||||
return;
|
||||
}
|
||||
int count = Encoding.UTF8.GetByteCount(v);
|
||||
Write(count);
|
||||
Encoding.UTF8.GetBytes(v, Take(count));
|
||||
}
|
||||
|
||||
public void WriteBytes(ReadOnlySpan<byte> bytes)
|
||||
{
|
||||
Write(bytes.Length);
|
||||
bytes.CopyTo(Take(bytes.Length));
|
||||
}
|
||||
|
||||
public void Write(ReadOnlySpan<int> values)
|
||||
{
|
||||
Write(values.Length);
|
||||
foreach (int v in values) Write(v);
|
||||
}
|
||||
|
||||
public void Write(ReadOnlySpan<long> values)
|
||||
{
|
||||
Write(values.Length);
|
||||
foreach (long v in values) Write(v);
|
||||
}
|
||||
|
||||
public void Write(ReadOnlySpan<double> values)
|
||||
{
|
||||
Write(values.Length);
|
||||
foreach (double v in values) Write(v);
|
||||
}
|
||||
|
||||
public void Write(ReadOnlySpan<bool> values)
|
||||
{
|
||||
Write(values.Length);
|
||||
foreach (bool v in values) Write(v);
|
||||
}
|
||||
|
||||
public void Write(ReadOnlySpan<ushort> values)
|
||||
{
|
||||
Write(values.Length);
|
||||
var dst = Take(values.Length * 2);
|
||||
for (int i = 0; i < values.Length; i++) BinaryPrimitives.WriteUInt16LittleEndian(dst.Slice(i * 2), values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Lettore corrispondente a <see cref="SaveWriter"/>; lancia InvalidDataException su dati troncati.</summary>
|
||||
public sealed class SaveReader(byte[] buffer, int offset = 0, int? length = null)
|
||||
{
|
||||
private readonly int _end = offset + (length ?? buffer.Length - offset);
|
||||
private int _position = offset;
|
||||
|
||||
public bool AtEnd => _position >= _end;
|
||||
|
||||
private ReadOnlySpan<byte> Take(int count)
|
||||
{
|
||||
if (count < 0 || _position + count > _end) throw new InvalidDataException("Salvataggio troncato o corrotto");
|
||||
var span = buffer.AsSpan(_position, count);
|
||||
_position += count;
|
||||
return span;
|
||||
}
|
||||
|
||||
public byte ReadByte() => Take(1)[0];
|
||||
public bool ReadBool() => Take(1)[0] != 0;
|
||||
public short ReadInt16() => BinaryPrimitives.ReadInt16LittleEndian(Take(2));
|
||||
public ushort ReadUInt16() => BinaryPrimitives.ReadUInt16LittleEndian(Take(2));
|
||||
public int ReadInt32() => BinaryPrimitives.ReadInt32LittleEndian(Take(4));
|
||||
public uint ReadUInt32() => BinaryPrimitives.ReadUInt32LittleEndian(Take(4));
|
||||
public long ReadInt64() => BinaryPrimitives.ReadInt64LittleEndian(Take(8));
|
||||
public ulong ReadUInt64() => BinaryPrimitives.ReadUInt64LittleEndian(Take(8));
|
||||
public double ReadDouble() => BitConverter.Int64BitsToDouble(BinaryPrimitives.ReadInt64LittleEndian(Take(8)));
|
||||
public float ReadSingle() => BitConverter.Int32BitsToSingle(BinaryPrimitives.ReadInt32LittleEndian(Take(4)));
|
||||
|
||||
public string? ReadString()
|
||||
{
|
||||
int count = ReadInt32();
|
||||
if (count < 0) return null;
|
||||
return Encoding.UTF8.GetString(Take(count));
|
||||
}
|
||||
|
||||
public string ReadRequiredString() => ReadString() ?? throw new InvalidDataException("Stringa mancante nel salvataggio");
|
||||
|
||||
public byte[] ReadBytes() => Take(ReadInt32()).ToArray();
|
||||
|
||||
public int[] ReadInt32Array()
|
||||
{
|
||||
var a = new int[ReadCount(4)];
|
||||
for (int i = 0; i < a.Length; i++) a[i] = ReadInt32();
|
||||
return a;
|
||||
}
|
||||
|
||||
public long[] ReadInt64Array()
|
||||
{
|
||||
var a = new long[ReadCount(8)];
|
||||
for (int i = 0; i < a.Length; i++) a[i] = ReadInt64();
|
||||
return a;
|
||||
}
|
||||
|
||||
public double[] ReadDoubleArray()
|
||||
{
|
||||
var a = new double[ReadCount(8)];
|
||||
for (int i = 0; i < a.Length; i++) a[i] = ReadDouble();
|
||||
return a;
|
||||
}
|
||||
|
||||
public bool[] ReadBoolArray()
|
||||
{
|
||||
var a = new bool[ReadCount(1)];
|
||||
for (int i = 0; i < a.Length; i++) a[i] = ReadBool();
|
||||
return a;
|
||||
}
|
||||
|
||||
public ushort[] ReadUInt16Array()
|
||||
{
|
||||
int n = ReadCount(2);
|
||||
var src = Take(n * 2);
|
||||
var a = new ushort[n];
|
||||
for (int i = 0; i < n; i++) a[i] = BinaryPrimitives.ReadUInt16LittleEndian(src.Slice(i * 2));
|
||||
return a;
|
||||
}
|
||||
|
||||
/// <summary>Legge un conteggio e controlla che i dati necessari ci siano davvero.</summary>
|
||||
public int ReadCount(int bytesPerItem)
|
||||
{
|
||||
int n = ReadInt32();
|
||||
if (n < 0 || (long)n * bytesPerItem > _end - _position) throw new InvalidDataException("Conteggio non valido nel salvataggio");
|
||||
return n;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using RailEnterprise.Core.Common;
|
||||
using RailEnterprise.Core.Simulation;
|
||||
|
||||
namespace RailEnterprise.Core.Persistence;
|
||||
|
||||
/// <summary>
|
||||
/// Hash dello stato dinamico: XxHash64 della serializzazione di <see cref="WorldSerializer.WriteDynamic"/>.
|
||||
/// Stesso seme e stessi comandi agli stessi tick devono dare lo stesso hash su ogni piattaforma.
|
||||
/// </summary>
|
||||
public static class StateHasher
|
||||
{
|
||||
[ThreadStatic] private static SaveWriter? _writer;
|
||||
|
||||
public static ulong Compute(World w)
|
||||
{
|
||||
var writer = _writer ??= new SaveWriter(1 << 20);
|
||||
writer.Reset();
|
||||
WorldSerializer.WriteDynamic(w, writer);
|
||||
return XxHash64.Hash(writer.WrittenSpan);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,717 @@
|
||||
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 = 3;
|
||||
|
||||
/// <summary>Scrive l'intero mondo (impostazioni, terreno, stato dinamico, eventi).</summary>
|
||||
public static void Write(World w, SaveWriter s)
|
||||
{
|
||||
s.Write(BodyVersion);
|
||||
WriteDictionary(w.Data, s);
|
||||
WriteSettings(w.Settings, s);
|
||||
WriteTerrain(w.Terrain, w.SeaSide, s);
|
||||
WriteDynamic(w, s);
|
||||
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)
|
||||
{
|
||||
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);
|
||||
MaintenanceSerializer.Write(w, s);
|
||||
CorporateSerializer.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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
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(r);
|
||||
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(SaveReader r)
|
||||
{
|
||||
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(),
|
||||
};
|
||||
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;
|
||||
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);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
Reference in new issue
Block a user