Files
Rail_Enterprise/src/ViaFerrea.Protocol/Messages/Records.cs
T
Claude 7879ac502d F5: manutenzione delle industrie, livelli, borsa e scalate, agenti IA
- Sezione 11: usura e recupero degli impianti, fermi e incidenti, ricambi e macchinari come
  domanda esplicita (treno, trasporto locale, trasbordo nel bacino), revisione e ammodernamento,
  acquisto delle industrie dal livello 3, conguaglio mensile con il proprietario (ADR 0011).
- Livelli della compagnia con soglie ritarate e ritaratura economica misurata con le IA.
- Sezione 12.2–12.3: libro dei soci, prezzo con sentiment, quotazione, riacquisto, dividendi,
  controllo e fusione (comando 39), offerte pubbliche con difese, esiti per difficoltà,
  aste delle IA fallite, nuovi concorrenti, fine partita e Cronaca finale (ADR 0012).
- Sezione 12.1: agenti IA con candidati, Dijkstra sulle celle, TrackPreview e ROI, tre
  personalità, linee dei ricambi, rimedio agli stalli, borsa (ADR 0013).
- Salvataggio versione 3, protocollo (messaggi 126–128, blocchi di estensione), server,
  simulate --trace-ai; test di 14.5 su IA e borsa e accettazione F5 (120 test).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0191R7vXgtbAvSV4V8tj8uzL
2026-10-12 10:36:31 +00:00

550 lines
21 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using ViaFerrea.Core.Common;
using ViaFerrea.Core.Rail;
namespace ViaFerrea.Protocol.Messages;
// Record delle entità inviati al client. Ogni record ha Write/Read e un byte di estensione finale.
/// <summary>Record serializzabile (entità, archi, eventi).</summary>
public interface IWritable
{
void Write(ref SpanWriter w);
}
/// <summary>Tipo di entità nei riferimenti e nelle rimozioni.</summary>
public enum EntityType : byte { Company = 0, Town = 1, Industry = 2, Hub = 3, Node = 4, Station = 5, Depot = 6, Line = 7, Vehicle = 8, Train = 9, Edge = 10 }
public sealed class WorldSummary : IWritable
{
public ulong Seed { get; init; }
public int SizeKm { get; init; }
public int StartYear { get; init; }
public long Tick { get; init; }
public string Date { get; init; } = "";
public int CompanyId { get; init; }
public string CompanyName { get; init; } = "";
public string RegionId { get; init; } = "";
public int WidthCells { get; init; }
public int HeightCells { get; init; }
public double CellSizeM { get; init; }
public int ChunkCells { get; init; }
public byte Speed { get; init; }
public ulong TerrainHash { get; init; }
/// <summary>Moltiplicatore dei costi di costruzione della compagnia (difficoltà), per le anteprime.</summary>
public double CostMultiplier { get; init; } = 1;
public void Write(ref SpanWriter w)
{
w.U64(Seed); w.I32(SizeKm); w.I32(StartYear); w.I64(Tick); w.String(Date);
w.I32(CompanyId); w.String(CompanyName); w.String(RegionId);
w.I32(WidthCells); w.I32(HeightCells); w.F64(CellSizeM); w.I32(ChunkCells);
w.U8(Speed); w.U64(TerrainHash); w.F64(CostMultiplier);
w.Flags(0);
}
public static WorldSummary Read(ref SpanReader r)
{
var m = new WorldSummary
{
Seed = r.U64(), SizeKm = r.I32(), StartYear = r.I32(), Tick = r.I64(), Date = r.String(),
CompanyId = r.I32(), CompanyName = r.String(), RegionId = r.String(),
WidthCells = r.I32(), HeightCells = r.I32(), CellSizeM = r.F64(), ChunkCells = r.I32(),
Speed = r.U8(), TerrainHash = r.U64(), CostMultiplier = r.F64(),
};
r.Flags();
return m;
}
}
public sealed class CompanyInfo : IWritable
{
public int Id { get; init; }
public string Name { get; init; } = "";
public string Color { get; init; } = "";
public bool IsPlayer { get; init; }
public Money Cash { get; init; }
public Money NetWorth { get; init; }
public string Rating { get; init; } = "";
public byte Tier { get; init; }
public bool Bankrupt { get; init; }
// blocco 1 (F4): politica di manutenzione della flotta
public byte MaintenancePolicy { get; set; } = 1;
// blocco 2 (F5): borsa, controllo, stato societario
public string Personality { get; set; } = "";
public bool Listed { get; set; }
/// <summary>Prezzo per azione in centesimi (frazionario).</summary>
public double SharePriceCents { get; set; }
public float Sentiment { get; set; } = 1;
/// <summary>Quota del fondatore sulle azioni in circolazione.</summary>
public float FounderShare { get; set; } = 1;
public int ControlledBy { get; set; } = -1;
public bool Merged { get; set; }
public bool Receivership { get; set; }
public long ReceivershipUntilDay { get; set; } = -1;
public Money AnnualProfit { get; set; }
public void Write(ref SpanWriter w)
{
w.I32(Id); w.String(Name); w.String(Color); w.Bool(IsPlayer); w.Money(Cash); w.Money(NetWorth);
w.String(Rating); w.U8(Tier); w.Bool(Bankrupt);
w.Flags(3);
w.U8(MaintenancePolicy);
w.String(Personality); w.Bool(Listed); w.F64(SharePriceCents); w.F32(Sentiment); w.F32(FounderShare);
w.I32(ControlledBy); w.Bool(Merged); w.Bool(Receivership); w.I64(ReceivershipUntilDay); w.Money(AnnualProfit);
}
public static CompanyInfo Read(ref SpanReader r)
{
var m = new CompanyInfo
{
Id = r.I32(), Name = r.String(), Color = r.String(), IsPlayer = r.Bool(), Cash = r.Money(), NetWorth = r.Money(),
Rating = r.String(), Tier = r.U8(), Bankrupt = r.Bool(),
};
byte flags = r.Flags();
if ((flags & 1) != 0) m.MaintenancePolicy = r.U8();
if ((flags & 2) != 0)
{
m.Personality = r.String(); m.Listed = r.Bool(); m.SharePriceCents = r.F64(); m.Sentiment = r.F32(); m.FounderShare = r.F32();
m.ControlledBy = r.I32(); m.Merged = r.Bool(); m.Receivership = r.Bool(); m.ReceivershipUntilDay = r.I64(); m.AnnualProfit = r.Money();
}
return m;
}
}
public sealed class TownInfo : IWritable
{
public int Id { get; init; }
public string Name { get; init; } = "";
public Vec2 Position { get; init; }
public int Population { get; init; }
public double RadiusCells { get; init; }
public float Satisfaction { get; init; }
public int Services { get; init; }
public void Write(ref SpanWriter w)
{
w.I32(Id); w.String(Name); w.Vec2(Position); w.I32(Population); w.F64(RadiusCells); w.F32(Satisfaction); w.I32(Services);
w.Flags(0);
}
public static TownInfo Read(ref SpanReader r)
{
var m = new TownInfo { Id = r.I32(), Name = r.String(), Position = r.Vec2(), Population = r.I32(), RadiusCells = r.F64(), Satisfaction = r.F32(), Services = r.I32() };
r.Flags();
return m;
}
}
public sealed class IndustryInfo : IWritable
{
public int Id { get; init; }
public string Type { get; init; } = "";
public string Name { get; init; } = "";
public Vec2 Position { get; init; }
public int FootprintCells { get; init; }
public int Level { get; init; }
public bool Closed { get; init; }
public float Efficiency { get; init; }
public float Condition { get; init; }
public int Owner { get; init; } = -1;
// blocco 1 (F5): manutenzione degli impianti (sezione 11)
/// <summary>Budget di manutenzione: 0 nessuno, 1 base, 2 alto.</summary>
public byte Budget { get; set; } = 1;
public int SpareParts { get; set; }
public int Machinery { get; set; }
/// <summary>Giorno di fine del fermo in corso (−1 se lavora) e motivo (IndustryStop).</summary>
public long StoppedUntilDay { get; set; } = -1;
public byte StopKind { get; set; }
/// <summary>Lavoro programmato in attesa (IndustryWork).</summary>
public byte PendingWork { get; set; }
public bool PartsShort { get; set; }
public void Write(ref SpanWriter w)
{
w.I32(Id); w.String(Type); w.String(Name); w.Vec2(Position); w.I32(FootprintCells); w.I32(Level); w.Bool(Closed);
w.F32(Efficiency); w.F32(Condition); w.I32(Owner);
w.Flags(1);
w.U8(Budget); w.I32(SpareParts); w.I32(Machinery); w.I64(StoppedUntilDay); w.U8(StopKind); w.U8(PendingWork); w.Bool(PartsShort);
}
public static IndustryInfo Read(ref SpanReader r)
{
var m = new IndustryInfo
{
Id = r.I32(), Type = r.String(), Name = r.String(), Position = r.Vec2(), FootprintCells = r.I32(), Level = r.I32(), Closed = r.Bool(),
Efficiency = r.F32(), Condition = r.F32(), Owner = r.I32(),
};
byte flags = r.Flags();
if ((flags & 1) != 0)
{
m.Budget = r.U8(); m.SpareParts = r.I32(); m.Machinery = r.I32(); m.StoppedUntilDay = r.I64(); m.StopKind = r.U8();
m.PendingWork = r.U8(); m.PartsShort = r.Bool();
}
return m;
}
}
public sealed class HubInfo : IWritable
{
public int Id { get; init; }
public string Kind { get; init; } = "";
public string Name { get; init; } = "";
public Vec2 Position { get; init; }
public void Write(ref SpanWriter w)
{
w.I32(Id); w.String(Kind); w.String(Name); w.Vec2(Position);
w.Flags(0);
}
public static HubInfo Read(ref SpanReader r)
{
var m = new HubInfo { Id = r.I32(), Kind = r.String(), Name = r.String(), Position = r.Vec2() };
r.Flags();
return m;
}
}
public sealed class NodeInfo : IWritable
{
public int Id { get; init; }
public Vec2 Position { get; init; }
public double HeightM { get; init; }
public int Owner { get; init; } = -1;
public int Station { get; init; } = -1;
public int Depot { get; init; } = -1;
public int[] Edges { get; init; } = [];
public void Write(ref SpanWriter w)
{
w.I32(Id); w.Vec2(Position); w.F64(HeightM); w.I32(Owner); w.I32(Station); w.I32(Depot);
w.Count(Edges.Length);
foreach (int e in Edges) w.I32(e);
w.Flags(0);
}
public static NodeInfo Read(ref SpanReader r)
{
int id = r.I32();
var pos = r.Vec2();
double h = r.F64();
int owner = r.I32(), station = r.I32(), depot = r.I32();
var edges = new int[r.Count()];
for (int i = 0; i < edges.Length; i++) edges[i] = r.I32();
r.Flags();
return new NodeInfo { Id = id, Position = pos, HeightM = h, Owner = owner, Station = station, Depot = depot, Edges = edges };
}
}
public sealed class StationInfo : IWritable
{
public int Id { get; init; }
public int Owner { get; init; }
public string Name { get; init; } = "";
public int Node { get; init; }
public Vec2 Position { get; init; }
public int Platforms { get; init; }
public int WaitingUnits { get; init; }
public int[] Industries { get; init; } = [];
public int[] Towns { get; init; } = [];
public void Write(ref SpanWriter w)
{
w.I32(Id); w.I32(Owner); w.String(Name); w.I32(Node); w.Vec2(Position); w.I32(Platforms); w.I32(WaitingUnits);
w.Count(Industries.Length);
foreach (int i in Industries) w.I32(i);
w.Count(Towns.Length);
foreach (int t in Towns) w.I32(t);
w.Flags(0);
}
public static StationInfo Read(ref SpanReader r)
{
int id = r.I32(), owner = r.I32();
string name = r.String();
int node = r.I32();
var pos = r.Vec2();
int platforms = r.I32(), waiting = r.I32();
var industries = new int[r.Count()];
for (int i = 0; i < industries.Length; i++) industries[i] = r.I32();
var towns = new int[r.Count()];
for (int i = 0; i < towns.Length; i++) towns[i] = r.I32();
r.Flags();
return new StationInfo { Id = id, Owner = owner, Name = name, Node = node, Position = pos, Platforms = platforms, WaitingUnits = waiting, Industries = industries, Towns = towns };
}
}
public sealed class DepotInfo : IWritable
{
public int Id { get; init; }
public int Owner { get; init; }
public string Name { get; init; } = "";
public int Node { get; init; }
public Vec2 Position { get; init; }
public string Size { get; init; } = "";
// blocco 1 (F4): officina
public int Stalls { get; set; }
public int JobsStaff { get; set; }
/// <summary>Personale presente in percentuale (0–100).</summary>
public byte StaffPercent { get; set; }
public int SpareParts { get; set; }
public int PartsTarget { get; set; }
/// <summary>Lavori in officina (in coda e negli stalli).</summary>
public int Jobs { get; set; }
public bool WaitingParts { get; set; }
public void Write(ref SpanWriter w)
{
w.I32(Id); w.I32(Owner); w.String(Name); w.I32(Node); w.Vec2(Position); w.String(Size);
w.Flags(1);
w.I32(Stalls); w.I32(JobsStaff); w.U8(StaffPercent); w.I32(SpareParts); w.I32(PartsTarget); w.I32(Jobs); w.Bool(WaitingParts);
}
public static DepotInfo Read(ref SpanReader r)
{
var m = new DepotInfo { Id = r.I32(), Owner = r.I32(), Name = r.String(), Node = r.I32(), Position = r.Vec2(), Size = r.String() };
byte flags = r.Flags();
if ((flags & 1) != 0)
{
m.Stalls = r.I32(); m.JobsStaff = r.I32(); m.StaffPercent = r.U8(); m.SpareParts = r.I32(); m.PartsTarget = r.I32();
m.Jobs = r.I32(); m.WaitingParts = r.Bool();
}
return m;
}
}
public sealed class LineInfo : IWritable
{
public int Id { get; init; }
public int Owner { get; init; }
public string Name { get; init; } = "";
public string Color { get; init; } = "";
public LineStop[] Stops { get; init; } = [];
// blocco 1 (F4): politica di manutenzione propria della linea (255 = quella della compagnia)
public byte Policy { get; set; } = byte.MaxValue;
public void Write(ref SpanWriter w)
{
w.I32(Id); w.I32(Owner); w.String(Name); w.String(Color);
w.Count(Stops.Length);
foreach (var s in Stops)
{
w.I32(s.Station.Value); w.Bool(s.Load); w.Bool(s.Unload); w.Bool(s.WaitFull); w.I32(s.MaxWaitDays);
}
w.Flags(1);
w.U8(Policy);
}
public static LineInfo Read(ref SpanReader r)
{
int id = r.I32(), owner = r.I32();
string name = r.String(), color = r.String();
var stops = new LineStop[r.Count()];
for (int i = 0; i < stops.Length; i++) stops[i] = new LineStop(new StationId(r.I32()), r.Bool(), r.Bool(), r.Bool(), r.I32());
var m = new LineInfo { Id = id, Owner = owner, Name = name, Color = color, Stops = stops };
byte flags = r.Flags();
if ((flags & 1) != 0) m.Policy = r.U8();
return m;
}
}
public sealed class VehicleInfo : IWritable
{
public int Id { get; init; }
public int Owner { get; init; }
public string Type { get; init; } = "";
public int Depot { get; init; } = -1;
public int Train { get; init; } = -1;
public float Condition { get; init; }
public void Write(ref SpanWriter w)
{
w.I32(Id); w.I32(Owner); w.String(Type); w.I32(Depot); w.I32(Train); w.F32(Condition);
w.Flags(0);
}
public static VehicleInfo Read(ref SpanReader r)
{
var m = new VehicleInfo { Id = r.I32(), Owner = r.I32(), Type = r.String(), Depot = r.I32(), Train = r.I32(), Condition = r.F32() };
r.Flags();
return m;
}
}
/// <summary>Stato di manutenzione di un treno nel riepilogo (blocco F4 di TrainInfo).</summary>
public enum TrainMaintenanceStatus : byte
{
InService = 0,
/// <summary>Intervento prenotato: va in officina dopo la fermata corrente.</summary>
Booked = 1,
InWorkshop = 2,
/// <summary>Guasto lieve: velocità dimezzata fino al deposito.</summary>
MinorFailure = 3,
/// <summary>Fermo in linea in attesa del soccorso (guasto grave o deragliamento).</summary>
Stranded = 4,
/// <summary>Rimorchiato verso l'officina.</summary>
Towed = 5,
}
public sealed class TrainInfo : IWritable
{
public int Id { get; init; }
public int Owner { get; init; }
public string Name { get; init; } = "";
public int Line { get; init; } = -1;
public byte State { get; init; }
public int[] Vehicles { get; init; } = [];
public float MaxSpeedKmh { get; init; }
public float LengthM { get; init; }
public int Load { get; init; }
public int Capacity { get; init; }
public int Depot { get; init; } = -1;
// blocco 1 (F4): riepilogo di manutenzione (la cartella completa si chiede con RequestDetail)
/// <summary>Condizione del componente più debole (0–100).</summary>
public byte Condition { get; set; } = 100;
public TrainMaintenanceStatus Maintenance { get; set; }
public bool Reserve { get; set; }
/// <summary>Politica propria del treno (255 = quella della linea o della compagnia).</summary>
public byte Policy { get; set; } = byte.MaxValue;
/// <summary>Prossimo intervento (id di maintenance.json) e giorni alla scadenza (negativi se scaduto).</summary>
public string NextIntervention { get; set; } = "";
public int NextDueDays { get; set; }
/// <summary>Officina prenotata o in cui si trova (−1 nessuna).</summary>
public int MaintenanceDepot { get; set; } = -1;
public void Write(ref SpanWriter w)
{
w.I32(Id); w.I32(Owner); w.String(Name); w.I32(Line); w.U8(State);
w.Count(Vehicles.Length);
foreach (int v in Vehicles) w.I32(v);
w.F32(MaxSpeedKmh); w.F32(LengthM); w.I32(Load); w.I32(Capacity); w.I32(Depot);
w.Flags(1);
w.U8(Condition); w.U8((byte)Maintenance); w.Bool(Reserve); w.U8(Policy); w.String(NextIntervention); w.I32(NextDueDays);
w.I32(MaintenanceDepot);
}
public static TrainInfo Read(ref SpanReader r)
{
int id = r.I32(), owner = r.I32();
string name = r.String();
int line = r.I32();
byte state = r.U8();
var vehicles = new int[r.Count()];
for (int i = 0; i < vehicles.Length; i++) vehicles[i] = r.I32();
var m = new TrainInfo
{
Id = id, Owner = owner, Name = name, Line = line, State = state, Vehicles = vehicles,
MaxSpeedKmh = r.F32(), LengthM = r.F32(), Load = r.I32(), Capacity = r.I32(), Depot = r.I32(),
};
byte flags = r.Flags();
if ((flags & 1) != 0)
{
m.Condition = r.U8(); m.Maintenance = (TrainMaintenanceStatus)r.U8(); m.Reserve = r.Bool(); m.Policy = r.U8();
m.NextIntervention = r.String(); m.NextDueDays = r.I32(); m.MaintenanceDepot = r.I32();
}
return m;
}
}
/// <summary>Arco di binario con i campioni del profilo (TrackWorks): precisione piena per le anteprime del client.</summary>
public sealed class EdgeInfo : IWritable
{
public int Id { get; init; }
public int Owner { get; init; }
public int A { get; init; }
public int B { get; init; }
public byte TrackClass { get; init; }
public bool DoubleTrack { get; init; }
public bool Electrified { get; init; }
public byte State { get; init; }
public float Condition { get; init; }
public float StructureCondition { get; init; }
/// <summary>Avanzamento del cantiere 0–1 (1 = completato).</summary>
public float Progress { get; init; } = 1;
public double[] S { get; init; } = [];
public double[] X { get; init; } = [];
public double[] Y { get; init; } = [];
public double[] H { get; init; } = [];
public double[] Ground { get; init; } = [];
public byte[] Kinds { get; init; } = [];
public double[] SpeedLimitKmh { get; init; } = [];
public double[] Signals { get; init; } = [];
public void Write(ref SpanWriter w)
{
w.I32(Id); w.I32(Owner); w.I32(A); w.I32(B); w.U8(TrackClass); w.Bool(DoubleTrack); w.Bool(Electrified); w.U8(State);
w.F32(Condition); w.F32(StructureCondition); w.F32(Progress);
int n = S.Length;
w.Count(n);
for (int i = 0; i < n; i++)
{
w.F64(S[i]); w.F64(X[i]); w.F64(Y[i]); w.F64(H[i]); w.F64(Ground[i]); w.U8(Kinds[i]); w.F64(SpeedLimitKmh[i]);
}
w.Count(Signals.Length);
foreach (double s in Signals) w.F64(s);
w.Flags(0);
}
public static EdgeInfo Read(ref SpanReader r)
{
int id = r.I32(), owner = r.I32(), a = r.I32(), b = r.I32();
byte cls = r.U8();
bool dbl = r.Bool(), el = r.Bool();
byte state = r.U8();
float cond = r.F32(), sc = r.F32(), progress = r.F32();
int n = r.Count();
var s = new double[n]; var x = new double[n]; var y = new double[n]; var h = new double[n]; var g = new double[n];
var k = new byte[n]; var v = new double[n];
for (int i = 0; i < n; i++)
{
s[i] = r.F64(); x[i] = r.F64(); y[i] = r.F64(); h[i] = r.F64(); g[i] = r.F64(); k[i] = r.U8(); v[i] = r.F64();
}
var signals = new double[r.Count()];
for (int i = 0; i < signals.Length; i++) signals[i] = r.F64();
r.Flags();
return new EdgeInfo
{
Id = id, Owner = owner, A = a, B = b, TrackClass = cls, DoubleTrack = dbl, Electrified = el, State = state,
Condition = cond, StructureCondition = sc, Progress = progress, S = s, X = x, Y = y, H = h, Ground = g, Kinds = k,
SpeedLimitKmh = v, Signals = signals,
};
}
}
/// <summary>Evento della simulazione (notifiche, ticker, resoconto giornaliero).</summary>
public sealed class EventInfo : IWritable
{
public long Day { get; init; }
public byte Kind { get; init; }
public byte Severity { get; init; }
public string Text { get; init; } = "";
public byte Entity { get; init; }
public int EntityIndex { get; init; } = -1;
public int Company { get; init; } = -1;
public void Write(ref SpanWriter w)
{
w.I64(Day); w.U8(Kind); w.U8(Severity); w.String(Text); w.U8(Entity); w.I32(EntityIndex); w.I32(Company);
w.Flags(0);
}
public static EventInfo Read(ref SpanReader r)
{
var m = new EventInfo { Day = r.I64(), Kind = r.U8(), Severity = r.U8(), Text = r.String(), Entity = r.U8(), EntityIndex = r.I32(), Company = r.I32() };
r.Flags();
return m;
}
}
/// <summary>Tratto di moto di un treno: arco percorso nel verso indicato tra due progressive.</summary>
public readonly record struct MotionSegment(int Edge, bool Forward, double FromS, double ToS)
{
public double Length => Math.Abs(ToS - FromS);
}