Il flag giornaliero veniva azzerato dalle stazioni prima della valutazione trimestrale; ora conta l'essere stata servita almeno un giorno nel trimestre. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0191R7vXgtbAvSV4V8tj8uzL
206 lines
7.3 KiB
C#
206 lines
7.3 KiB
C#
using ViaFerrea.Core.Common;
|
|
using ViaFerrea.Core.Data;
|
|
using ViaFerrea.Core.Economy;
|
|
using ViaFerrea.Core.Rail;
|
|
|
|
namespace ViaFerrea.Core.Simulation.Systems;
|
|
|
|
/// <summary>
|
|
/// Stazioni (guida 7): bacini di utenza, industrie servite, merci che passano dalle industrie
|
|
/// del bacino alla stazione con una destinazione scelta sul grafo delle linee, scadenza dei
|
|
/// pacchetti in attesa, nuovi tentativi di consegna ai consumatori.
|
|
/// </summary>
|
|
public sealed class StationSystem : ISimSystem
|
|
{
|
|
public string Name => "Stations";
|
|
|
|
private readonly List<(int Station, double Score)> _candidates = [];
|
|
|
|
public void Step(WorldContext ctx)
|
|
{
|
|
if (!ctx.IsNewDay) return;
|
|
var w = ctx.World;
|
|
var net = w.Network;
|
|
if (ctx.IsNewMonth || ctx.Day == 0)
|
|
{
|
|
foreach (var s in net.Stations) RailCommandHandlers.UpdateCatchment(w, s);
|
|
}
|
|
MarkServed(w, ctx.Day);
|
|
foreach (var s in net.Stations)
|
|
{
|
|
if (net.Nodes[s.Node.Value].Station != s.Id) continue;
|
|
Expire(ctx, s);
|
|
RetryDeliveries(w, s);
|
|
}
|
|
foreach (var s in net.Stations)
|
|
{
|
|
if (net.Nodes[s.Node.Value].Station != s.Id || !LoadsHere(w, s)) continue;
|
|
CollectFreight(ctx, s);
|
|
}
|
|
}
|
|
|
|
/// <summary>Un'industria è servita se sta nel bacino di una stazione fermata di una linea con treni.</summary>
|
|
private static void MarkServed(World w, long day)
|
|
{
|
|
foreach (var s in w.Network.Stations)
|
|
{
|
|
if (!IsServedStation(w, s)) continue;
|
|
foreach (var id in s.Industries) w.Industries[id.Value].LastServedDay = day;
|
|
}
|
|
}
|
|
|
|
private static bool IsServedStation(World w, Station s)
|
|
{
|
|
foreach (var line in w.Lines)
|
|
{
|
|
if (line.Removed || line.Owner != s.Owner) continue;
|
|
bool stops = false;
|
|
foreach (var st in line.Stops) if (st.Station == s.Id) { stops = true; break; }
|
|
if (!stops) continue;
|
|
foreach (var t in w.Trains)
|
|
{
|
|
if (!t.Removed && t.Line == line.Id) return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private static bool LoadsHere(World w, Station s)
|
|
{
|
|
foreach (var line in w.Lines)
|
|
{
|
|
if (line.Removed || line.Owner != s.Owner) continue;
|
|
foreach (var st in line.Stops)
|
|
{
|
|
if (st.Station == s.Id && st.Load) return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private static void Expire(WorldContext ctx, Station s)
|
|
{
|
|
var w = ctx.World;
|
|
for (int i = s.Waiting.Count - 1; i >= 0; i--)
|
|
{
|
|
var p = s.Waiting[i];
|
|
if (p.ExpiresTick > ctx.Tick) continue;
|
|
s.Waiting.RemoveAt(i);
|
|
if (p.Kind != PacketKind.Freight) continue;
|
|
w.Goods.Decayed[p.Good] += p.Units;
|
|
if (!p.AwaitingConsumer)
|
|
{
|
|
var dest = w.Network.Stations[p.Destination.Value];
|
|
dest.Incoming[p.Good] = Math.Max(0, dest.Incoming[p.Good] - p.Units);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void RetryDeliveries(World w, Station s)
|
|
{
|
|
for (int i = s.Waiting.Count - 1; i >= 0; i--)
|
|
{
|
|
var p = s.Waiting[i];
|
|
if (!p.AwaitingConsumer) continue;
|
|
p.Units = StationWork.DeliverToConsumers(w, s, p.Good, p.Units);
|
|
if (p.Units <= 0) s.Waiting.RemoveAt(i);
|
|
else s.Waiting[i] = p;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Le industrie del bacino affidano la produzione alla stazione se esiste una destinazione
|
|
/// raggiungibile con un consumatore; si sceglie quella con più bisogno, a parità la più vicina.
|
|
/// </summary>
|
|
private void CollectFreight(WorldContext ctx, Station s)
|
|
{
|
|
var w = ctx.World;
|
|
var graph = w.LineGraphs.For(w, s.Owner);
|
|
var reach = graph.From(s.Id);
|
|
if (reach.Count == 0) return;
|
|
foreach (var id in s.Industries)
|
|
{
|
|
var ind = w.Industries[id.Value];
|
|
if (ind.Closed) continue;
|
|
var outputs = ind.Def.Recipe.Outputs;
|
|
for (int o = 0; o < outputs.Count; o++)
|
|
{
|
|
int good = outputs[o].GoodIndex;
|
|
int units = ind.OutputStock[o];
|
|
if (units <= 0) continue;
|
|
var dest = BestDestination(w, s, reach, good);
|
|
if (dest is null) continue;
|
|
var p = new Packet
|
|
{
|
|
Id = w.NextIds.Packet++,
|
|
Kind = PacketKind.Freight,
|
|
Good = good,
|
|
Units = units,
|
|
Origin = s.Id,
|
|
Destination = dest.Destination,
|
|
HopLine = dest.First.Line,
|
|
HopEnd = dest.First.To,
|
|
CreatedTick = ctx.Tick,
|
|
ExpiresTick = ctx.Tick + (long)w.Data.Goods[good].DecayDaysAtStation * w.Calendar.TicksPerDay,
|
|
ExpectedDays = w.Calendar.TrainMinutesToDays(dest.Minutes),
|
|
Contract = -1,
|
|
};
|
|
ind.OutputStock[o] = 0;
|
|
s.Waiting.Add(p);
|
|
w.Network.Stations[dest.Destination.Value].Incoming[good] += units;
|
|
var sale = Money.FromCentsRounded(units * w.Market.GlobalPrice[good]);
|
|
w.Money.Transfer(Account.External, Account.Of(ind.Id), sale, LedgerEntry.Other);
|
|
ind.SalesToday += sale.Cents;
|
|
if (o == 0) ind.ShippedToday += units;
|
|
}
|
|
}
|
|
}
|
|
|
|
private Connection? BestDestination(World w, Station from, IReadOnlyDictionary<int, Connection> reach, int good)
|
|
{
|
|
Connection? best = null;
|
|
double bestScore = double.NegativeInfinity;
|
|
int dpm = w.Calendar.DaysPerMonth;
|
|
var tb = w.Data.Balance.Town;
|
|
_candidates.Clear();
|
|
foreach (var key in reach.Keys) _candidates.Add((key, 0));
|
|
_candidates.Sort((a, b) => a.Station.CompareTo(b.Station));
|
|
foreach (var (sid, _) in _candidates)
|
|
{
|
|
var dest = w.Network.Stations[sid];
|
|
var conn = reach[sid];
|
|
double need = 0;
|
|
bool consumer = false;
|
|
foreach (var id in dest.Industries)
|
|
{
|
|
var ind = w.Industries[id.Value];
|
|
int i = ind.InputIndexOf(good);
|
|
if (i < 0 || ind.Closed) continue;
|
|
consumer = true;
|
|
need += IndustryMath.InputCapacity(ind, i, dpm) - ind.InputStock[i];
|
|
}
|
|
foreach (var id in dest.Towns)
|
|
{
|
|
var town = w.Towns[id.Value];
|
|
if (town.DemandToday[good] <= 0) continue;
|
|
consumer = true;
|
|
need += town.DemandToday[good] * tb.StockDays - town.Stock[good];
|
|
}
|
|
foreach (var id in dest.Hubs)
|
|
{
|
|
consumer = true;
|
|
need += w.Hubs[id.Value].RemainingCapacity;
|
|
}
|
|
if (!consumer) continue;
|
|
need -= dest.Incoming[good];
|
|
double score = need / (1 + conn.Minutes / 60);
|
|
if (score > bestScore)
|
|
{
|
|
bestScore = score;
|
|
best = conn;
|
|
}
|
|
}
|
|
return best;
|
|
}
|
|
}
|