F2: pianificatore dei binari, profilo altimetrico e scenario Valdoria
- TrackGeometry: polilinea raccordata con il raggio della classe - TrackPlanner: campioni ogni 25 m, due passate di pendenza, smussatura, franchi sui fiumi, classificazione delle opere, costi, giorni, vincoli per era (campate per classe di fiume, lunghezza delle gallerie) - Scenari a mano (ScenarioLoader) e tests/scenarios/valdoria.json - viaferrea-tools profile; industrie al prezzo globale; servizi di base Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0191R7vXgtbAvSV4V8tj8uzL
This commit is contained in:
21 files changed
+1558
-16
No files matched your search
@@ -0,0 +1,103 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using ViaFerrea.Core.Common;
|
||||
using ViaFerrea.Core.Data;
|
||||
using ViaFerrea.Core.Map;
|
||||
using ViaFerrea.Core.Rail;
|
||||
using ViaFerrea.Core.Simulation;
|
||||
using ViaFerrea.Core.Simulation.Scenarios;
|
||||
|
||||
namespace ViaFerrea.Tools.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// profile: pianifica un tracciato e scrive il profilo campionato ogni 25 m in CSV (separatore «;»),
|
||||
/// con il riepilogo delle opere, dei costi e dei giorni.
|
||||
/// </summary>
|
||||
public sealed class ProfileCommand : IToolCommand
|
||||
{
|
||||
public string Name => "profile";
|
||||
|
||||
public string Usage => "profile (--seed <n> [--size 128] | --scenario <file> [--route <nome>]) [--polyline \"x1,y1;x2,y2;…\" (km)] [--offset <m>] [--class c60] [--no-tunnels] [--no-bridges] [--year <anno>] [--out profilo.csv]";
|
||||
|
||||
private static readonly CultureInfo Inv = CultureInfo.InvariantCulture;
|
||||
|
||||
public int Run(CommandLine args)
|
||||
{
|
||||
ulong seed = args.ULong("seed", 1);
|
||||
int size = args.Int("size", 64);
|
||||
string? scenarioPath = args.String("scenario");
|
||||
string? routeName = args.String("route");
|
||||
string? polyline = args.String("polyline");
|
||||
double offset = args.Double("offset", 0);
|
||||
string trackClass = args.String("class", "c60");
|
||||
bool noTunnels = args.Flag("no-tunnels");
|
||||
bool noBridges = args.Flag("no-bridges");
|
||||
int? year = args.String("year") is { } y ? int.Parse(y, Inv) : null;
|
||||
string output = args.String("out", "profilo.csv");
|
||||
args.EnsureAllUsed();
|
||||
|
||||
var data = GameData.LoadDefault();
|
||||
World world;
|
||||
TrackPlanRequest request;
|
||||
if (scenarioPath is not null)
|
||||
{
|
||||
var scenario = ScenarioLoader.LoadFile(data, scenarioPath);
|
||||
world = scenario.World;
|
||||
request = routeName is not null
|
||||
? scenario.Route(routeName, world.Player.Id)
|
||||
: new TrackPlanRequest { Polyline = ParsePolyline(polyline ?? throw new ArgumentException("serve --route o --polyline")) };
|
||||
}
|
||||
else
|
||||
{
|
||||
world = WorldFactory.Create(data, new NewGameSettings { Seed = seed, SizeKm = size });
|
||||
request = new TrackPlanRequest { Polyline = ParsePolyline(polyline ?? throw new ArgumentException("serve --polyline")) };
|
||||
}
|
||||
if (polyline is not null || routeName is null)
|
||||
{
|
||||
request = request with
|
||||
{
|
||||
HeightOffsetM = offset,
|
||||
TrackClass = trackClass,
|
||||
AllowTunnels = !noTunnels,
|
||||
AllowBridges = !noBridges,
|
||||
};
|
||||
}
|
||||
|
||||
var ctx = new TrackPlanContext
|
||||
{
|
||||
Data = data,
|
||||
Sampler = world.Sampler,
|
||||
Year = year ?? world.Clock.Year,
|
||||
CostMultiplier = world.Difficulty.ConstructionCostMultiplier,
|
||||
NearestTownEdgeKm = p => world.Towns.Count == 0 ? double.MaxValue : world.Towns.Min(t => world.TownEdgeDistanceKm(t, p)),
|
||||
};
|
||||
var plan = TrackPlanner.Plan(ctx, request);
|
||||
|
||||
var csv = new StringBuilder();
|
||||
csv.AppendLine("s_m;x_m;y_m;terreno_m;binario_m;differenza_m;opera;cella;velocita_kmh");
|
||||
foreach (var s in plan.Samples)
|
||||
{
|
||||
csv.AppendLine(string.Join(';', F(s.S), F(s.Point.X), F(s.Point.Y), F(s.TerrainM), F(s.TrackM), F(s.TrackM - s.TerrainM),
|
||||
s.Kind.ToString(), s.Cell.ToString(), F(s.SpeedLimitKmh)));
|
||||
}
|
||||
File.WriteAllText(output, csv.ToString(), new UTF8Encoding(true));
|
||||
|
||||
Console.WriteLine($"profile: {(plan.Valid ? "VALIDO" : "RIFIUTATO")} — {plan.LengthM / 1000:F2} km, pendenza max {plan.MaxGradient * 100:F2}% (limite {plan.GradientLimit * 100:F1}%), raggio min {(double.IsInfinity(plan.MinRadiusM) ? "∞" : plan.MinRadiusM.ToString("F0", Inv))} m");
|
||||
foreach (var e in plan.Errors) Console.WriteLine($" errore al km {e.AtM / 1000:F2}: {e.Message}");
|
||||
foreach (var w in plan.Warnings) Console.WriteLine($" avviso: {w.Message}");
|
||||
foreach (var w in plan.Works)
|
||||
Console.WriteLine($" {w.Kind,-10} {w.StartM / 1000,6:F2}–{w.EndM / 1000,6:F2} km h max {w.MaxHeightM,5:F1} m {new Money(w.CostCents),14} {w.Days,4} gg{(w.BridgeType is { } b ? " ponte " + b : "")}");
|
||||
double natural = plan.LengthM * data.Construction.Costs.BasePerMeter * ctx.CostMultiplier;
|
||||
Console.WriteLine($" opere {plan.WorksCost}, terreni {plan.LandCost}, totale {plan.TotalCost} ({plan.TotalCost.Cents / Math.Max(1, natural + plan.LandCost.Cents):F2}× la sede naturale), {plan.Days} giorni");
|
||||
Console.WriteLine($" profilo in {output}");
|
||||
return plan.Valid ? 0 : 3;
|
||||
}
|
||||
|
||||
private static string F(double v) => double.IsInfinity(v) ? "" : v.ToString("0.##", Inv).Replace('.', ',');
|
||||
|
||||
private static List<Vec2> ParsePolyline(string text) =>
|
||||
text.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
.Select(p => p.Split(','))
|
||||
.Select(p => new Vec2(double.Parse(p[0], Inv) * 1000, double.Parse(p[1], Inv) * 1000))
|
||||
.ToList();
|
||||
}
|
||||
@@ -10,6 +10,7 @@ public static class ToolsProgram
|
||||
new ValidateDataCommand(),
|
||||
new GenMapCommand(),
|
||||
new SimulateCommand(),
|
||||
new ProfileCommand(),
|
||||
];
|
||||
|
||||
public static int Main(string[] args)
|
||||
|
||||
@@ -1,13 +1,8 @@
|
||||
using ViaFerrea.Core.Data;
|
||||
using ViaFerrea.Core.Simulation;
|
||||
|
||||
namespace ViaFerrea.Tools.Scenarios;
|
||||
|
||||
/// <summary>Caricamento degli scenari di prova (tests/scenarios/*.json): arriva con la Fase 2.</summary>
|
||||
/// <summary>Accesso agli scenari di Core dagli strumenti.</summary>
|
||||
public static class ScenarioLoader
|
||||
{
|
||||
public sealed record LoadedScenario(World World);
|
||||
|
||||
public static LoadedScenario Load(GameData data, string path) =>
|
||||
throw new ArgumentException($"Gli scenari ({path}) sono supportati dalla Fase 2");
|
||||
public static Core.Simulation.Scenarios.LoadedScenario Load(Core.Data.GameData data, string path) =>
|
||||
Core.Simulation.Scenarios.ScenarioLoader.LoadFile(data, path);
|
||||
}
|
||||
Reference in new issue
Block a user