Infrastruttura: nome «Rail Enterprise», CI e rilasci su Gitea, documentazione
Il progetto adotta il nome «Rail Enterprise» ovunque (codice RailEnterprise.*, variabili RE_*, intestazioni RESV/RECL, immagine rail-enterprise-server) per allinearsi al repository Gitea. Aggiunge Gitea Actions sul modello di Chronos: CI su PR e main, rilascio da tag con gioco Windows (client Godot + server in server/), server Windows/Linux e immagine Docker nel registro Gitea, nightly. Aggiunge i riferimenti visivi 06–09, i modelli di PR e issue, l'ADR 0014 e aggiorna README (comandi locali), STATE e CLAUDE.md. 120 test verdi. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f6c2a17b4d
commit
c1a0049bcf
300 files changed
+1602
-1045
No files matched your search
@@ -0,0 +1,228 @@
|
||||
using RailEnterprise.Core.Common;
|
||||
using RailEnterprise.Core.Data;
|
||||
using RailEnterprise.Core.Economy;
|
||||
|
||||
namespace RailEnterprise.Core.Simulation.Systems;
|
||||
|
||||
/// <summary>
|
||||
/// Produzione giornaliera delle industrie (guida 6.3), esaurimento dei giacimenti, salari,
|
||||
/// crescita trimestrale delle industrie servite, chiusure e nascita di nuove industrie.
|
||||
/// </summary>
|
||||
public sealed class IndustrySystem : ISimSystem
|
||||
{
|
||||
public string Name => "Industries";
|
||||
|
||||
public void Step(WorldContext ctx)
|
||||
{
|
||||
if (!ctx.IsNewDay) return;
|
||||
var w = ctx.World;
|
||||
foreach (var ind in w.Industries)
|
||||
{
|
||||
if (!ind.Closed) Produce(ctx, ind);
|
||||
}
|
||||
if (ctx.IsNewQuarter && ctx.Day > 0) Quarter(ctx);
|
||||
if (ctx.IsNewYear) w.SpawnedThisYear = 0;
|
||||
SpawnForUnmetDemand(ctx);
|
||||
}
|
||||
|
||||
private static void Produce(WorldContext ctx, Industry ind)
|
||||
{
|
||||
var w = ctx.World;
|
||||
var data = w.Data;
|
||||
var ib = data.Balance.Industry;
|
||||
int dpm = w.Calendar.DaysPerMonth;
|
||||
var recipe = ind.Def.Recipe;
|
||||
|
||||
// coperture di lavoratori e personale (EMA su 30 giorni)
|
||||
double covW = ind.JobsWorkers > 0 ? ind.WorkersPresent / ind.JobsWorkers : 1.0;
|
||||
double covS = ind.JobsStaff > 0 ? ind.StaffPresent / ind.JobsStaff : 1.0;
|
||||
ind.CovWorkers = Ema.Step(ind.CovWorkers, covW, ib.CoverageWindowDays);
|
||||
ind.CovStaff = Ema.Step(ind.CovStaff, covS, ib.CoverageWindowDays);
|
||||
|
||||
double pf = IndustryMath.ProfitFactor(ind, ib);
|
||||
double mc = IndustryMath.ConditionMultiplier(ind.Condition, data.Balance.IndustryMaintenance);
|
||||
double eLabor = Math.Min(ind.CovWorkers, ind.CovStaff) * pf * mc;
|
||||
// fermo per guasto, incidente, revisione o ammodernamento: nessuna produzione (sezione 11)
|
||||
bool stopped = ind.IsStopped(ctx.Day);
|
||||
if (stopped) eLabor = 0;
|
||||
|
||||
// giacimento: sotto la soglia la capacità cala linearmente, a zero l'industria chiude
|
||||
double depletion = 1.0;
|
||||
ResourceSite? site = ind.Site.IsValid ? w.Sites[ind.Site.Value] : null;
|
||||
if (ind.Def.Depletable && site is not null)
|
||||
{
|
||||
double frac = site.ReserveFraction;
|
||||
if (frac <= 0)
|
||||
{
|
||||
Close(ctx, ind, $"{ind.Name}: giacimento esaurito, l'attività chiude");
|
||||
return;
|
||||
}
|
||||
if (frac < ib.DepletionStartFraction) depletion = frac / ib.DepletionStartFraction;
|
||||
}
|
||||
|
||||
double cycles = ind.CyclesPerDayAtFullCapacity(dpm) * eLabor * depletion;
|
||||
for (int o = 0; o < recipe.Outputs.Count && !stopped; o++)
|
||||
w.Market.CapacityToday[recipe.Outputs[o].GoodIndex] += ind.CyclesPerDayAtFullCapacity(dpm) * depletion * recipe.Outputs[o].PerCycle;
|
||||
|
||||
// copertura degli input di oggi: limita la produzione alle scorte disponibili
|
||||
double covInToday = 1.0;
|
||||
for (int i = 0; i < recipe.Inputs.Count; i++)
|
||||
{
|
||||
double need = cycles * recipe.Inputs[i].PerCycle;
|
||||
if (need > 0) covInToday = Math.Min(covInToday, ind.InputStock[i] / need);
|
||||
}
|
||||
covInToday = Math.Min(1.0, covInToday);
|
||||
ind.CovInput = Ema.Step(ind.CovInput, recipe.Inputs.Count == 0 ? 1.0 : covInToday, ib.CoverageWindowDays);
|
||||
|
||||
double total = cycles * covInToday + ind.CycleAccumulator;
|
||||
long whole = (long)Math.Floor(total);
|
||||
for (int i = 0; i < recipe.Inputs.Count; i++)
|
||||
whole = Math.Min(whole, ind.InputStock[i] / recipe.Inputs[i].PerCycle);
|
||||
ind.CycleAccumulator = Math.Clamp(total - whole, 0, 1);
|
||||
|
||||
for (int i = 0; i < recipe.Inputs.Count; i++)
|
||||
{
|
||||
int used = (int)(whole * recipe.Inputs[i].PerCycle);
|
||||
ind.InputStock[i] -= used;
|
||||
w.Goods.Consumed[recipe.Inputs[i].GoodIndex] += used;
|
||||
}
|
||||
for (int o = 0; o < recipe.Outputs.Count; o++)
|
||||
{
|
||||
int good = recipe.Outputs[o].GoodIndex;
|
||||
int made = (int)(whole * recipe.Outputs[o].PerCycle);
|
||||
ind.ProducedToday[o] = made;
|
||||
ind.OutputStock[o] += made;
|
||||
w.Goods.Produced[good] += made;
|
||||
w.Market.SupplyToday[good] += made;
|
||||
|
||||
// oltre la capienza del magazzino (storageDays) la merce si perde
|
||||
int cap = IndustryMath.OutputCapacity(ind, o, dpm);
|
||||
if (ind.OutputStock[o] > cap)
|
||||
{
|
||||
int lost = ind.OutputStock[o] - cap;
|
||||
ind.OutputStock[o] = cap;
|
||||
w.Goods.Decayed[good] += lost;
|
||||
}
|
||||
}
|
||||
if (site is not null && ind.Def.Depletable)
|
||||
{
|
||||
double extracted = whole * recipe.Outputs[0].PerCycle * data.Difficulty.DepletionMultipliers.For(w.Difficulty.Depletion);
|
||||
site.ReserveTonnes = Math.Max(0, site.ReserveTonnes - extracted);
|
||||
}
|
||||
ind.Efficiency = eLabor * covInToday * depletion;
|
||||
|
||||
// salari giornalieri ai presenti
|
||||
double wages = (ind.WorkersPresent * ib.WagePerWorkerPerMonth + ind.StaffPresent * ib.WagePerStaffPerMonth) / dpm;
|
||||
var wageMoney = Money.FromCentsRounded(wages);
|
||||
w.Money.Transfer(Account.Of(ind.Id), Account.External, wageMoney, LedgerEntry.Wages);
|
||||
|
||||
ind.RevenueEma = Ema.Step(ind.RevenueEma, ind.SalesToday, ib.MarginWindowDays);
|
||||
ind.CostEma = Ema.Step(ind.CostEma, wageMoney.Cents + ind.PurchasesToday, ib.MarginWindowDays);
|
||||
ind.SalesToday = 0;
|
||||
ind.PurchasesToday = 0;
|
||||
|
||||
// statistiche del trimestre
|
||||
ind.QuarterProduced += whole * recipe.Outputs[0].PerCycle;
|
||||
ind.QuarterShipped += ind.ShippedToday;
|
||||
ind.ShippedToday = 0;
|
||||
ind.QuarterCoverageSum += Math.Min(ind.CovInput, Math.Min(ind.CovWorkers, ind.CovStaff));
|
||||
ind.QuarterDays++;
|
||||
}
|
||||
|
||||
private static void Quarter(WorldContext ctx)
|
||||
{
|
||||
var w = ctx.World;
|
||||
var g = w.Data.Balance.Growth;
|
||||
foreach (var ind in w.Industries)
|
||||
{
|
||||
if (ind.Closed) continue;
|
||||
double shipped = ind.QuarterProduced > 0 ? Math.Min(1.0, (double)ind.QuarterShipped / ind.QuarterProduced) : 0;
|
||||
double coverage = ind.QuarterDays > 0 ? ind.QuarterCoverageSum / ind.QuarterDays : 0;
|
||||
double profit = Math.Clamp((ind.Margin + 1) / 2, 0, 1);
|
||||
double score = g.WeightShipped * shipped + g.WeightCoverage * coverage + g.WeightProfit * profit;
|
||||
ind.LastScore = score;
|
||||
ind.QuarterProduced = 0;
|
||||
ind.QuarterShipped = 0;
|
||||
ind.QuarterCoverageSum = 0;
|
||||
ind.QuarterDays = 0;
|
||||
|
||||
// solo le industrie servite da un vettore crescono o regrediscono (ADR 0004)
|
||||
if (!ind.ServedWithin(ctx.Day, g.QuarterDays)) continue;
|
||||
|
||||
if (ind.Level == 0)
|
||||
{
|
||||
ind.QuartersAtZero++;
|
||||
if (ind.QuartersAtZero >= g.CloseQuartersAtZero)
|
||||
{
|
||||
Close(ctx, ind, $"{ind.Name} chiude dopo un anno di inattività");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ind.QuartersAtZero = 0;
|
||||
}
|
||||
|
||||
if (score > g.UpgradeThreshold)
|
||||
{
|
||||
ind.QuartersAboveUpgrade++;
|
||||
if (ind.QuartersAboveUpgrade >= g.UpgradeQuarters && ind.Level < g.MaxLevel)
|
||||
{
|
||||
ind.Level++;
|
||||
ind.QuartersAboveUpgrade = 0;
|
||||
ctx.Emit(EventKind.Industry, EventSeverity.Opportunity, $"{ind.Name} si espande al livello {ind.Level}", EntityKind.Industry, ind.Id.Value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ind.QuartersAboveUpgrade = 0;
|
||||
if (score < g.DowngradeThreshold && ind.Level > 0)
|
||||
{
|
||||
ind.Level--;
|
||||
ctx.Emit(EventKind.Industry, EventSeverity.Warning, $"{ind.Name} riduce l'attività (livello {ind.Level})", EntityKind.Industry, ind.Id.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Chiude un'industria: le scorte si perdono, l'impronta torna libera.</summary>
|
||||
public static void Close(WorldContext ctx, Industry ind, string reason)
|
||||
{
|
||||
var w = ctx.World;
|
||||
ind.Closed = true;
|
||||
ind.Level = 0;
|
||||
var recipe = ind.Def.Recipe;
|
||||
for (int i = 0; i < recipe.Inputs.Count; i++)
|
||||
{
|
||||
w.Goods.Decayed[recipe.Inputs[i].GoodIndex] += ind.InputStock[i];
|
||||
ind.InputStock[i] = 0;
|
||||
}
|
||||
for (int o = 0; o < recipe.Outputs.Count; o++)
|
||||
{
|
||||
w.Goods.Decayed[recipe.Outputs[o].GoodIndex] += ind.OutputStock[o];
|
||||
ind.OutputStock[o] = 0;
|
||||
}
|
||||
IndustryPlacer.ClearFootprint(w, ind);
|
||||
w.Commutes.RemoveAll(c => c.Workplace.Kind == WorkplaceKind.Industry && c.Workplace.Index == ind.Id.Value);
|
||||
ctx.Emit(EventKind.Industry, EventSeverity.Warning, reason, EntityKind.Industry, ind.Id.Value);
|
||||
}
|
||||
|
||||
private static void SpawnForUnmetDemand(WorldContext ctx)
|
||||
{
|
||||
var w = ctx.World;
|
||||
var g = w.Data.Balance.Growth;
|
||||
if (w.SpawnedThisYear >= g.SpawnMaxPerYear) return;
|
||||
for (int good = 0; good < w.Data.GoodCount; good++)
|
||||
{
|
||||
if (w.Market.UnmetDays[good] < g.SpawnUnmetDays) continue;
|
||||
w.Market.UnmetDays[good] = 0;
|
||||
var ind = IndustryPlacer.TrySpawnProducer(w, good, ctx.Year);
|
||||
if (ind is null) continue;
|
||||
w.SpawnedThisYear++;
|
||||
ctx.Emit(EventKind.Industry, EventSeverity.Opportunity,
|
||||
$"Apre {ind.Name}: la domanda di {w.Data.Goods[good].NameIt.ToLowerInvariant()} è rimasta insoddisfatta per un anno",
|
||||
EntityKind.Industry, ind.Id.Value);
|
||||
if (w.SpawnedThisYear >= g.SpawnMaxPerYear) return;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user