- Base deterministica: Money, id tipizzati, DMath, XxHash64, calendario - 12 file dati con schemi, validatore di schema interno, validazione semantica e hash dei dati; viaferrea-tools validate-data - Generatore di mappa (rilievo, mare, priority-flood, fiumi, laghi, copertura, giacimenti, città, hub, industrie) e viaferrea-tools genmap - Economia: città, industrie, hub, mercato con componente mondiale, prezzi locali, tariffe, lavoro in sede e a piedi, trasporto locale, registro a partita doppia, prestiti, rating, difficoltà - Salvataggi Brotli con rimappatura per id e hash di stato giornaliero - viaferrea-tools simulate con KPI CSV; 45 test verdi; ADR 0002-0006 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0191R7vXgtbAvSV4V8tj8uzL
152 lines
5.8 KiB
C#
152 lines
5.8 KiB
C#
using ViaFerrea.Core.Common;
|
|
using ViaFerrea.Core.Data;
|
|
using ViaFerrea.Core.Economy;
|
|
|
|
namespace ViaFerrea.Core.Simulation.Systems;
|
|
|
|
/// <summary>
|
|
/// Pendolarismo (guida, sezione 9). In F1 gestisce i servizi cittadini, i lavoratori in sede e i
|
|
/// contratti a piedi verso posti di lavoro entro il raggio pedonale; i contratti su rotaia
|
|
/// arrivano in F4 con il LineGraph. Ogni mese ricostruisce i contratti a piedi; ogni giorno
|
|
/// aggiorna presenze e coperture.
|
|
/// </summary>
|
|
public sealed class EmploymentSystem : ISimSystem
|
|
{
|
|
public string Name => "Commuting";
|
|
|
|
public void Step(WorldContext ctx)
|
|
{
|
|
if (!ctx.IsNewDay) return;
|
|
if (ctx.IsNewMonth) Rematch(ctx);
|
|
UpdatePresence(ctx);
|
|
}
|
|
|
|
/// <summary>Ricostruisce servizi cittadini e contratti a piedi.</summary>
|
|
public static void Rematch(WorldContext ctx)
|
|
{
|
|
var w = ctx.World;
|
|
var tb = w.Data.Balance.Town;
|
|
var ib = w.Data.Balance.Industry;
|
|
var eb = w.Data.Balance.Employment;
|
|
|
|
w.Commutes.RemoveAll(c => c.Walking);
|
|
|
|
foreach (var town in w.Towns)
|
|
{
|
|
int workforce = town.Workforce(tb);
|
|
town.ServiceJobs = Math.Min(workforce, (int)(town.Population * tb.ServiceJobsShare));
|
|
town.EmployedWorkers = town.ServiceJobs;
|
|
town.EmployedStaff = 0;
|
|
}
|
|
// i contratti su rotaia (F4) restano validi e occupano forza lavoro
|
|
foreach (var c in w.Commutes)
|
|
{
|
|
var town = w.Towns[c.Town.Value];
|
|
if (c.Category == PaxClass.Staff) town.EmployedStaff += c.Headcount;
|
|
else town.EmployedWorkers += c.Headcount;
|
|
}
|
|
|
|
foreach (var ind in w.Industries)
|
|
{
|
|
ind.ContractedWorkers = 0;
|
|
ind.ContractedStaff = 0;
|
|
}
|
|
foreach (var c in w.Commutes)
|
|
{
|
|
if (c.Workplace.Kind != WorkplaceKind.Industry) continue;
|
|
var ind = w.Industries[c.Workplace.Index];
|
|
if (c.Category == PaxClass.Staff) ind.ContractedStaff += c.Headcount;
|
|
else ind.ContractedWorkers += c.Headcount;
|
|
}
|
|
|
|
var nearby = new List<(double Km, Town Town)>();
|
|
foreach (var ind in w.Industries)
|
|
{
|
|
if (ind.Closed || ind.Level <= 0) continue;
|
|
nearby.Clear();
|
|
foreach (var town in w.Towns)
|
|
{
|
|
double km = w.TownEdgeDistanceKm(town, ind.Position);
|
|
if (km <= eb.WalkRadiusKm) nearby.Add((km, town));
|
|
}
|
|
if (nearby.Count == 0) continue;
|
|
nearby.Sort((a, b) => a.Km != b.Km ? a.Km.CompareTo(b.Km) : a.Town.Id.Value.CompareTo(b.Town.Id.Value));
|
|
|
|
int openWorkers = ind.JobsWorkers - (int)Math.Ceiling(OnSite(ind.JobsWorkers, ib.OnSiteWorkerShare)) - ind.ContractedWorkers;
|
|
int openStaff = ind.JobsStaff - (int)Math.Ceiling(OnSite(ind.JobsStaff, ib.OnSiteStaffShare)) - ind.ContractedStaff;
|
|
foreach (var (km, town) in nearby)
|
|
{
|
|
if (openWorkers > 0)
|
|
{
|
|
int take = Math.Min(openWorkers, town.FreeWorkers(tb));
|
|
if (take > 0)
|
|
{
|
|
AddWalking(w, town, ind, PaxClass.Worker, take, km, eb);
|
|
town.EmployedWorkers += take;
|
|
ind.ContractedWorkers += take;
|
|
openWorkers -= take;
|
|
}
|
|
}
|
|
if (openStaff > 0)
|
|
{
|
|
int take = Math.Min(openStaff, town.FreeStaff(tb));
|
|
if (take > 0)
|
|
{
|
|
AddWalking(w, town, ind, PaxClass.Staff, take, km, eb);
|
|
town.EmployedStaff += take;
|
|
ind.ContractedStaff += take;
|
|
openStaff -= take;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void AddWalking(World w, Town town, Industry ind, PaxClass category, int headcount, double km, EmploymentBalance eb)
|
|
{
|
|
w.Commutes.Add(new CommuteContract
|
|
{
|
|
Id = new ContractId(w.NextIds.Contract++),
|
|
Town = town.Id,
|
|
Workplace = WorkplaceRef.Of(ind.Id),
|
|
Category = category,
|
|
Headcount = headcount,
|
|
ExpectedMinutes = km / eb.WalkSpeedKmh * 60.0,
|
|
});
|
|
}
|
|
|
|
/// <summary>Posti coperti da chi vive in sede (quota fissa dei posti, frazionaria).</summary>
|
|
public static double OnSite(int jobs, double share) => jobs * share;
|
|
|
|
private static void UpdatePresence(WorldContext ctx)
|
|
{
|
|
var w = ctx.World;
|
|
var ib = w.Data.Balance.Industry;
|
|
var eb = w.Data.Balance.Employment;
|
|
foreach (var ind in w.Industries)
|
|
{
|
|
ind.WorkersPresent = OnSite(ind.JobsWorkers, ib.OnSiteWorkerShare);
|
|
ind.StaffPresent = OnSite(ind.JobsStaff, ib.OnSiteStaffShare);
|
|
}
|
|
foreach (var c in w.Commutes)
|
|
{
|
|
// a piedi si arriva sempre; su rotaia contano i passeggeri consegnati (F4)
|
|
if (c.Walking) c.ArrivedToday = c.Headcount;
|
|
double ratio = c.Headcount > 0 ? Math.Min(1.0, (double)c.ArrivedToday / c.Headcount) : 0;
|
|
c.Coverage = Ema.Step(c.Coverage, ratio, eb.CoverageEmaDays);
|
|
if (c.Workplace.Kind == WorkplaceKind.Industry)
|
|
{
|
|
var ind = w.Industries[c.Workplace.Index];
|
|
if (c.Category == PaxClass.Staff) ind.StaffPresent += c.ArrivedToday;
|
|
else ind.WorkersPresent += c.ArrivedToday;
|
|
}
|
|
if (!c.Walking) c.ArrivedToday = 0;
|
|
}
|
|
foreach (var ind in w.Industries)
|
|
{
|
|
ind.WorkersPresent = Math.Min(ind.WorkersPresent, ind.JobsWorkers);
|
|
ind.StaffPresent = Math.Min(ind.StaffPresent, ind.JobsStaff);
|
|
}
|
|
}
|
|
}
|