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
This commit is contained in:
Claude committed 2026-10-12 10:36:31 +00:00
1 parent 8d2fdd79e5
commit 7879ac502d
72 files changed
+5851 -122

No files matched your search

@@ -41,6 +41,15 @@ public sealed class ClientWorld(GameData data)
public Dictionary<int, WorkshopRegister> Workshops { get; } = [];
public EmploymentReport? Employment { get; private set; }
/// <summary>Dati della F5: listino di borsa, schede di compagnie e industrie, Cronaca finale.</summary>
public MarketTape? Tape { get; private set; }
public Dictionary<int, CompanyCard> CompanyCards { get; } = [];
public Dictionary<int, IndustryCard> IndustryCards { get; } = [];
public Chronicle? Chronicle { get; private set; }
/// <summary>Contatore dei listini ricevuti (per aggiornare il pannello della borsa).</summary>
public int TapeVersion { get; private set; }
/// <summary>Ultimi avvisi di manutenzione, dal più recente.</summary>
public List<MaintenanceAlert> Alerts { get; } = [];
private const int AlertCapacity = 50;
@@ -102,6 +111,22 @@ public sealed class ClientWorld(GameData data)
Employment = m;
DetailVersion++;
return true;
case MarketTape m:
Tape = m;
TapeVersion++;
return true;
case CompanyCard m:
CompanyCards[m.Company] = m;
DetailVersion++;
return true;
case IndustryCard m:
IndustryCards[m.Industry] = m;
DetailVersion++;
return true;
case Chronicle m:
Chronicle = m;
DetailVersion++;
return true;
case MaintenanceAlert m:
Alerts.Insert(0, m);
if (Alerts.Count > AlertCapacity) Alerts.RemoveAt(Alerts.Count - 1);
@@ -117,6 +142,10 @@ public sealed class ClientWorld(GameData data)
Cards.Clear();
Workshops.Clear();
Employment = null;
Tape = null;
CompanyCards.Clear();
IndustryCards.Clear();
Chronicle = null;
Alerts.Clear();
Summary = m.Summary;
SessionId = m.SessionId;
+28 -2
View File
@@ -160,10 +160,26 @@ public static class CommandCodec
w.String(c.Objective);
w.Money(c.Budget);
break;
case MergeCompanyCommand c: w.I32(c.Subsidiary.Value); break;
default:
throw new ProtocolException($"comando senza codifica: {command.Type}");
}
w.Flags(0);
// blocco 1 (F5): industria destinataria dei ricambi, difesa del bersaglio di un'offerta
switch (command)
{
case OrderPartsCommand c when c.Industry.IsValid:
w.Flags(1);
w.I32(c.Industry.Value);
break;
case RespondBidCommand c when c.Defense != BidDefense.None || c.Knight.IsValid:
w.Flags(1);
w.U8((byte)c.Defense);
w.I32(c.Knight.Value);
break;
default:
w.Flags(0);
break;
}
}
public static GameCommand Read(CommandType type, ref SpanReader r)
@@ -208,9 +224,19 @@ public static class CommandCodec
CommandType.AcceptContract => new AcceptContractCommand(new ContractId(r.I32())),
CommandType.FundResearch => new FundResearchCommand(r.String(), r.Money()),
CommandType.DelegateRegion => new DelegateRegionCommand(new RegionId(r.I32()), r.String(), r.Money()),
CommandType.MergeCompany => new MergeCompanyCommand(new CompanyId(r.I32())),
_ => throw new ProtocolException($"tipo di comando sconosciuto: {(ushort)type}"),
};
r.Flags();
byte flags = r.Flags();
if ((flags & 1) != 0)
{
c = c switch
{
OrderPartsCommand op => op with { Industry = new IndustryId(r.I32()) },
RespondBidCommand rb => rb with { Defense = Enum<BidDefense>(r.U8(), 3), Knight = new CompanyId(r.I32()) },
_ => c,
};
}
return c;
}
+6
View File
@@ -31,6 +31,12 @@ public enum MessageId : ushort
MaintenanceCard = 124,
/// <summary>Registro officina di un deposito (F4).</summary>
WorkshopRegister = 125,
/// <summary>Scheda di una compagnia: soci, storico, prezzi (F5).</summary>
CompanyCard = 126,
/// <summary>Scheda del proprietario di un'industria: manutenzione, fermi, valore (F5).</summary>
IndustryCard = 127,
/// <summary>Cronaca finale della partita del giocatore (F5).</summary>
Chronicle = 128,
CommandResult = 130,
Notification = 131,
Newspaper = 132,
@@ -231,6 +231,12 @@ public enum DetailKind : byte
Workshop = 2,
/// <summary>Registro degli impieghi della compagnia (risposta EmploymentReport; Id ignorato).</summary>
Employment = 3,
/// <summary>Scheda di una compagnia (risposta CompanyCard, F5).</summary>
Company = 4,
/// <summary>Scheda del proprietario di un'industria (risposta IndustryCard, F5).</summary>
Industry = 5,
/// <summary>Cronaca finale (risposta Chronicle, F5; Id ignorato).</summary>
Chronicle = 6,
}
/// <summary>Chiede al server un dettaglio che non viaggia con le entità (aggiunto in F4).</summary>
@@ -0,0 +1,266 @@
using ViaFerrea.Core.Common;
namespace ViaFerrea.Protocol.Messages;
// Messaggi della fase F5: scheda di una compagnia (soci e storico), scheda del proprietario di
// un'industria, Cronaca finale. Arrivano solo su richiesta (RequestDetail) o, la Cronaca, alla fine
// della partita del giocatore.
/// <summary>Quota di un titolare nel libro dei soci (Holder = id compagnia, −1 fondatore, −2 mercato).</summary>
public sealed class HoldingInfo
{
public const int Founder = -1;
public const int Market = -2;
public int Holder { get; init; }
public long Shares { get; init; }
public void Write(ref SpanWriter w)
{
w.I32(Holder); w.I64(Shares);
w.Flags(0);
}
public static HoldingInfo Read(ref SpanReader r)
{
var m = new HoldingInfo { Holder = r.I32(), Shares = r.I64() };
r.Flags();
return m;
}
}
/// <summary>Anno di storia di una compagnia (grafici della scheda e della Cronaca).</summary>
public sealed class CompanyYearInfo
{
public int Year { get; init; }
public Money NetWorth { get; init; }
public Money Cash { get; init; }
public Money Revenue { get; init; }
public Money Profit { get; init; }
public byte Tier { get; init; }
public int Trains { get; init; }
public float TrackKm { get; init; }
public Money SharePrice { get; init; }
public void Write(ref SpanWriter w)
{
w.I32(Year); w.Money(NetWorth); w.Money(Cash); w.Money(Revenue); w.Money(Profit); w.U8(Tier); w.I32(Trains); w.F32(TrackKm); w.Money(SharePrice);
w.Flags(0);
}
public static CompanyYearInfo Read(ref SpanReader r)
{
var m = new CompanyYearInfo
{
Year = r.I32(), NetWorth = r.Money(), Cash = r.Money(), Revenue = r.Money(), Profit = r.Money(), Tier = r.U8(), Trains = r.I32(),
TrackKm = r.F32(), SharePrice = r.Money(),
};
r.Flags();
return m;
}
}
/// <summary>Scheda di una compagnia (borsa e controllo, 12.2).</summary>
public sealed class CompanyCard : IMessage
{
public MessageId Id => MessageId.CompanyCard;
public int Company { get; init; }
public long TotalShares { get; init; }
public long Treasury { get; init; }
public List<HoldingInfo> Holdings { get; init; } = [];
/// <summary>Prezzo dell'azione a fine mese (centesimi), il più recente in coda.</summary>
public List<double> PriceHistory { get; init; } = [];
public List<CompanyYearInfo> History { get; init; } = [];
public Money AnnualProfit { get; init; }
public Money DividendsYear { get; init; }
public Money Debt { get; init; }
public Money MarketCap { get; init; }
/// <summary>Giorni di controllo da parte del titolare di maggioranza (−1 se nessuno).</summary>
public long ControlDays { get; init; } = -1;
/// <summary>Asta in corso degli attivi (compagnia fallita): base, miglior offerta, chiusura.</summary>
public Money AuctionMin { get; init; }
public Money AuctionBest { get; init; }
public int AuctionBestBidder { get; init; } = -1;
public long AuctionEndDay { get; init; } = -1;
public void Write(ref SpanWriter w)
{
w.I32(Company); w.I64(TotalShares); w.I64(Treasury);
w.Count(Holdings.Count);
foreach (var h in Holdings) h.Write(ref w);
w.Count(PriceHistory.Count);
foreach (double p in PriceHistory) w.F64(p);
w.Count(History.Count);
foreach (var y in History) y.Write(ref w);
w.Money(AnnualProfit); w.Money(DividendsYear); w.Money(Debt); w.Money(MarketCap); w.I64(ControlDays);
w.Money(AuctionMin); w.Money(AuctionBest); w.I32(AuctionBestBidder); w.I64(AuctionEndDay);
w.Flags(0);
}
public static CompanyCard Read(ref SpanReader r)
{
int company = r.I32();
long total = r.I64(), treasury = r.I64();
var holdings = new List<HoldingInfo>();
for (int n = r.Count(), i = 0; i < n; i++) holdings.Add(HoldingInfo.Read(ref r));
var prices = new List<double>();
for (int n = r.Count(), i = 0; i < n; i++) prices.Add(r.F64());
var history = new List<CompanyYearInfo>();
for (int n = r.Count(), i = 0; i < n; i++) history.Add(CompanyYearInfo.Read(ref r));
var m = new CompanyCard
{
Company = company, TotalShares = total, Treasury = treasury, Holdings = holdings, PriceHistory = prices, History = history,
AnnualProfit = r.Money(), DividendsYear = r.Money(), Debt = r.Money(), MarketCap = r.Money(), ControlDays = r.I64(),
AuctionMin = r.Money(), AuctionBest = r.Money(), AuctionBestBidder = r.I32(), AuctionEndDay = r.I64(),
};
r.Flags();
return m;
}
}
/// <summary>Fermo passato o in corso di un'industria.</summary>
public sealed class IndustryStopInfo
{
public long Day { get; init; }
public int Days { get; init; }
public byte Kind { get; init; }
public void Write(ref SpanWriter w)
{
w.I64(Day); w.I32(Days); w.U8(Kind);
w.Flags(0);
}
public static IndustryStopInfo Read(ref SpanReader r)
{
var m = new IndustryStopInfo { Day = r.I64(), Days = r.I32(), Kind = r.U8() };
r.Flags();
return m;
}
}
/// <summary>Scheda del proprietario di un'industria (sezione 11).</summary>
public sealed class IndustryCard : IMessage
{
public MessageId Id => MessageId.IndustryCard;
public int Industry { get; init; }
public float Margin { get; init; }
public float ConditionMultiplier { get; init; }
/// <summary>Usura e recupero giornalieri attuali (punti di condizione).</summary>
public float WearPerDay { get; init; }
public float RecoveryPerDay { get; init; }
public Money MaintenanceCostPerDay { get; init; }
public Money MaintenanceSpentYear { get; init; }
public int PartsTarget { get; init; }
public int PartsPerMonth { get; init; }
public int PartsOrdered { get; init; }
public int MachineryRequired { get; init; }
public Money UpgradeCost { get; init; }
/// <summary>Prezzo chiesto dai proprietari privati (per BuyIndustry).</summary>
public Money Valuation { get; init; }
public Money Cash { get; init; }
public float BreakdownChancePerYear { get; init; }
public List<IndustryStopInfo> Stops { get; init; } = [];
public void Write(ref SpanWriter w)
{
w.I32(Industry); w.F32(Margin); w.F32(ConditionMultiplier); w.F32(WearPerDay); w.F32(RecoveryPerDay);
w.Money(MaintenanceCostPerDay); w.Money(MaintenanceSpentYear); w.I32(PartsTarget); w.I32(PartsPerMonth); w.I32(PartsOrdered);
w.I32(MachineryRequired); w.Money(UpgradeCost); w.Money(Valuation); w.Money(Cash); w.F32(BreakdownChancePerYear);
w.Count(Stops.Count);
foreach (var s in Stops) s.Write(ref w);
w.Flags(0);
}
public static IndustryCard Read(ref SpanReader r)
{
var m = new IndustryCard
{
Industry = r.I32(), Margin = r.F32(), ConditionMultiplier = r.F32(), WearPerDay = r.F32(), RecoveryPerDay = r.F32(),
MaintenanceCostPerDay = r.Money(), MaintenanceSpentYear = r.Money(), PartsTarget = r.I32(), PartsPerMonth = r.I32(), PartsOrdered = r.I32(),
MachineryRequired = r.I32(), UpgradeCost = r.Money(), Valuation = r.Money(), Cash = r.Money(), BreakdownChancePerYear = r.F32(),
};
for (int n = r.Count(), i = 0; i < n; i++) m.Stops.Add(IndustryStopInfo.Read(ref r));
r.Flags();
return m;
}
}
/// <summary>Primato della Cronaca finale (descrizione e valore già formattati in italiano).</summary>
public sealed class RecordInfo
{
public string Title { get; init; } = "";
public string Value { get; init; } = "";
public int Year { get; init; }
public void Write(ref SpanWriter w)
{
w.String(Title); w.String(Value); w.I32(Year);
w.Flags(0);
}
public static RecordInfo Read(ref SpanReader r)
{
var m = new RecordInfo { Title = r.String(), Value = r.String(), Year = r.I32() };
r.Flags();
return m;
}
}
/// <summary>
/// Cronaca finale (12.3): storia della compagnia del giocatore, grafici, primati e prestigio.
/// Dopo la Cronaca il giocatore può continuare come osservatore o iniziare una Nuova partita+.
/// </summary>
public sealed class Chronicle : IMessage
{
public MessageId Id => MessageId.Chronicle;
public bool GameOver { get; init; }
/// <summary>Motivo (GameOverReason): 1 fallimento, 2 scalata, 3 riscatto scaduto.</summary>
public byte Reason { get; init; }
public string Text { get; init; } = "";
public int Winner { get; init; } = -1;
public long Day { get; init; }
public string Company { get; init; } = "";
public int FoundedYear { get; init; }
public long Prestige { get; init; }
public List<CompanyYearInfo> History { get; init; } = [];
public List<RecordInfo> Records { get; init; } = [];
/// <summary>Fatti salienti della compagnia, dal più vecchio.</summary>
public List<string> Highlights { get; init; } = [];
public void Write(ref SpanWriter w)
{
w.Bool(GameOver); w.U8(Reason); w.String(Text); w.I32(Winner); w.I64(Day); w.String(Company); w.I32(FoundedYear); w.I64(Prestige);
w.Count(History.Count);
foreach (var y in History) y.Write(ref w);
w.Count(Records.Count);
foreach (var rec in Records) rec.Write(ref w);
w.Count(Highlights.Count);
foreach (var h in Highlights) w.String(h);
w.Flags(0);
}
public static Chronicle Read(ref SpanReader r)
{
bool over = r.Bool();
byte reason = r.U8();
string text = r.String();
int winner = r.I32();
long day = r.I64();
string company = r.String();
int founded = r.I32();
long prestige = r.I64();
var history = new List<CompanyYearInfo>();
for (int n = r.Count(), i = 0; i < n; i++) history.Add(CompanyYearInfo.Read(ref r));
var records = new List<RecordInfo>();
for (int n = r.Count(), i = 0; i < n; i++) records.Add(RecordInfo.Read(ref r));
var highlights = new List<string>();
for (int n = r.Count(), i = 0; i < n; i++) highlights.Add(r.String());
r.Flags();
return new Chronicle
{
GameOver = over, Reason = reason, Text = text, Winner = winner, Day = day, Company = company, FoundedYear = founded,
Prestige = prestige, History = history, Records = records, Highlights = highlights,
};
}
}
@@ -28,6 +28,9 @@ public static class MessageCodec
MessageId.EmploymentReport => EmploymentReport.Read(ref r),
MessageId.MaintenanceCard => MaintenanceCard.Read(ref r),
MessageId.WorkshopRegister => WorkshopRegister.Read(ref r),
MessageId.CompanyCard => CompanyCard.Read(ref r),
MessageId.IndustryCard => IndustryCard.Read(ref r),
MessageId.Chronicle => Chronicle.Read(ref r),
MessageId.MarketTape => MarketTape.Read(ref r),
MessageId.CommandResult => CommandResultMessage.Read(ref r),
MessageId.Notification => Notification.Read(ref r),
+42 -3
View File
@@ -71,12 +71,28 @@ public sealed class CompanyInfo : IWritable
// 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(1);
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)
@@ -88,6 +104,11 @@ public sealed class CompanyInfo : IWritable
};
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;
}
}
@@ -129,11 +150,24 @@ public sealed class IndustryInfo : IWritable
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(0);
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)
@@ -143,7 +177,12 @@ public sealed class IndustryInfo : IWritable
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();
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;
}
}
@@ -739,16 +739,27 @@ public sealed class SharePrice
public float Change { get; init; }
public bool Listed { get; init; }
// blocco 1 (F5): prezzo esatto, capitalizzazione, flottante libero, quota del fondatore
public double PriceCents { get; set; }
public long Outstanding { get; set; }
public long FreeFloat { get; set; }
public float FounderShare { get; set; }
public int ControlledBy { get; set; } = -1;
public void Write(ref SpanWriter w)
{
w.I32(Company); w.Money(Price); w.F32(Change); w.Bool(Listed);
w.Flags(0);
w.Flags(1);
w.F64(PriceCents); w.I64(Outstanding); w.I64(FreeFloat); w.F32(FounderShare); w.I32(ControlledBy);
}
public static SharePrice Read(ref SpanReader r)
{
var m = new SharePrice { Company = r.I32(), Price = r.Money(), Change = r.F32(), Listed = r.Bool() };
r.Flags();
if ((r.Flags() & 1) != 0)
{
m.PriceCents = r.F64(); m.Outstanding = r.I64(); m.FreeFloat = r.I64(); m.FounderShare = r.F32(); m.ControlledBy = r.I32();
}
return m;
}
}
@@ -762,16 +773,28 @@ public sealed class BidInfo
public double TargetShare { get; init; }
public long ExpiresDay { get; init; }
// blocco 1 (F5): stato, quota già impegnata (azioni dell'offerente + adesioni), difese, cavaliere bianco
public byte State { get; set; }
public double CommittedShare { get; set; }
public byte Defenses { get; set; }
public int Knight { get; set; } = -1;
/// <summary>Vero se la compagnia della sessione ha già aderito.</summary>
public bool Accepted { get; set; }
public void Write(ref SpanWriter w)
{
w.I32(Bid); w.I32(Bidder); w.I32(Target); w.Money(PricePerShare); w.F64(TargetShare); w.I64(ExpiresDay);
w.Flags(0);
w.Flags(1);
w.U8(State); w.F64(CommittedShare); w.U8(Defenses); w.I32(Knight); w.Bool(Accepted);
}
public static BidInfo Read(ref SpanReader r)
{
var m = new BidInfo { Bid = r.I32(), Bidder = r.I32(), Target = r.I32(), PricePerShare = r.Money(), TargetShare = r.F64(), ExpiresDay = r.I64() };
r.Flags();
if ((r.Flags() & 1) != 0)
{
m.State = r.U8(); m.CommittedShare = r.F64(); m.Defenses = r.U8(); m.Knight = r.I32(); m.Accepted = r.Bool();
}
return m;
}
}