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.
/// Record serializzabile (entità, archi, eventi).
public interface IWritable
{
void Write(ref SpanWriter w);
}
/// Tipo di entità nei riferimenti e nelle rimozioni.
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; }
/// Moltiplicatore dei costi di costruzione della compagnia (difficoltà), per le anteprime.
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;
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(1);
w.U8(MaintenancePolicy);
}
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();
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;
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(0);
}
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(),
};
r.Flags();
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; }
/// Personale presente in percentuale (0–100).
public byte StaffPercent { get; set; }
public int SpareParts { get; set; }
public int PartsTarget { get; set; }
/// Lavori in officina (in coda e negli stalli).
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;
}
}
/// Stato di manutenzione di un treno nel riepilogo (blocco F4 di TrainInfo).
public enum TrainMaintenanceStatus : byte
{
InService = 0,
/// Intervento prenotato: va in officina dopo la fermata corrente.
Booked = 1,
InWorkshop = 2,
/// Guasto lieve: velocità dimezzata fino al deposito.
MinorFailure = 3,
/// Fermo in linea in attesa del soccorso (guasto grave o deragliamento).
Stranded = 4,
/// Rimorchiato verso l'officina.
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)
/// Condizione del componente più debole (0–100).
public byte Condition { get; set; } = 100;
public TrainMaintenanceStatus Maintenance { get; set; }
public bool Reserve { get; set; }
/// Politica propria del treno (255 = quella della linea o della compagnia).
public byte Policy { get; set; } = byte.MaxValue;
/// Prossimo intervento (id di maintenance.json) e giorni alla scadenza (negativi se scaduto).
public string NextIntervention { get; set; } = "";
public int NextDueDays { get; set; }
/// Officina prenotata o in cui si trova (−1 nessuna).
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;
}
}
/// Arco di binario con i campioni del profilo (TrackWorks): precisione piena per le anteprime del client.
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; }
/// Avanzamento del cantiere 0–1 (1 = completato).
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,
};
}
}
/// Evento della simulazione (notifiche, ticker, resoconto giornaliero).
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;
}
}
/// Tratto di moto di un treno: arco percorso nel verso indicato tra due progressive.
public readonly record struct MotionSegment(int Edge, bool Forward, double FromS, double ToS)
{
public double Length => Math.Abs(ToS - FromS);
}