using RailEnterprise.Core.Commands; using RailEnterprise.Core.Common; using RailEnterprise.Core.Economy; using RailEnterprise.Core.Simulation.Systems; namespace RailEnterprise.Core.Simulation; /// Comandi delle industrie possedute (sezione 11): acquisto, budget, revisione, ammodernamento, ricambi. public static class IndustryCommandHandlers { public static CommandResult BuyIndustry(World w, Company company, BuyIndustryCommand c, RailCommandHandlers.Reply reply) { if (!Tiers.Unlocks(w, company).BuyIndustry) return reply(CommandError.TierLocked, Tiers.LockedText(w, "L'acquisto di industrie", u => u.BuyIndustry)); if (!TryIndustry(w, c.Industry, out var ind) || ind.Closed) return reply(CommandError.NotFound, "Industria inesistente"); if (ind.Owner == company.Id) return reply(CommandError.NotAllowed, $"{ind.Name} è già della compagnia"); if (ind.Owner.IsValid) return reply(CommandError.NotAllowed, $"{ind.Name} appartiene a {w.Company(ind.Owner).Name}"); if (ind.Level <= 0) return reply(CommandError.NotAllowed, $"{ind.Name} è inattiva e non è in vendita"); var price = IndustryMath.Valuation(w, ind); if (c.Offer < price) return reply(CommandError.InvalidArgument, $"Offerta troppo bassa: i proprietari chiedono almeno {price}"); if (company.Cash < c.Offer) return reply(CommandError.InsufficientFunds, "Cassa insufficiente"); w.Money.Transfer(Account.Of(company.Id), Account.External, c.Offer, LedgerEntry.IndustryPurchase); ind.Owner = company.Id; ind.AcquiredDay = w.Clock.Day; ind.MonthsHighMargin = 0; w.Events.Add(new GameEvent(w.Clock.Day, EventKind.Industry, EventSeverity.Info, $"{company.Name} acquista {ind.Name} per {c.Offer}", EntityKind.Industry, ind.Id.Value, company.Id)); return reply(CommandError.None, $"{ind.Name} acquistata per {c.Offer}", ind.Id.Value); } public static CommandResult SetPolicy(World w, Company company, SetIndustryPolicyCommand c, RailCommandHandlers.Reply reply) { if (!TryOwned(w, company, c.Industry, out var ind, out var error)) return reply(error.Error, error.Message); if (c.Budget > IndustryBudget.High) return reply(CommandError.InvalidArgument, "Budget non valido"); ind.Budget = c.Budget; var b = w.Data.Balance.IndustryMaintenance.For(c.Budget); return reply(CommandError.None, $"{ind.Name}: budget {BudgetName(c.Budget)} (recupero {b.RecoveryPerDay:0.00} punti al giorno, {b.CostShareOfRevenue * 100:0} % dei ricavi, {b.SparePartsPerLevelPerMonth * ind.Level} ricambi al mese)"); } public static CommandResult Overhaul(World w, Company company, OverhaulIndustryCommand c, RailCommandHandlers.Reply reply) { if (!TryOwned(w, company, c.Industry, out var ind, out var error)) return reply(error.Error, error.Message); if (ind.Level <= 0) return reply(CommandError.NotAllowed, $"{ind.Name} è inattiva"); if (ind.IsStopped(w.Clock.Day)) return reply(CommandError.NotAllowed, $"{ind.Name} è già ferma"); if (ind.PendingWork != IndustryWork.None) return reply(CommandError.NotAllowed, $"{ind.Name} ha già un lavoro programmato"); var m = w.Data.Balance.IndustryMaintenance; ind.PendingWork = IndustryWork.Overhaul; int need = IndustryMath.MachineryRequired(ind, m); return reply(CommandError.None, $"{ind.Name}: revisione programmata ({m.OverhaulDays} giorni di fermo); servono {need} macchinari (in scorta {ind.Machinery}) e personale almeno all'{m.OverhaulMinStaffCoverage * 100:0} %"); } public static CommandResult Upgrade(World w, Company company, UpgradeIndustryCommand c, RailCommandHandlers.Reply reply) { if (!TryOwned(w, company, c.Industry, out var ind, out var error)) return reply(error.Error, error.Message); if (ind.Level <= 0) return reply(CommandError.NotAllowed, $"{ind.Name} è inattiva"); if (ind.Level >= w.Data.Balance.Growth.MaxLevel) return reply(CommandError.NotAllowed, $"{ind.Name} è già al livello massimo"); if (ind.IsStopped(w.Clock.Day)) return reply(CommandError.NotAllowed, $"{ind.Name} è ferma"); if (ind.PendingWork != IndustryWork.None) return reply(CommandError.NotAllowed, $"{ind.Name} ha già un lavoro programmato"); var cost = IndustryMaintenanceSystem.UpgradeCost(ind); if (company.Cash < cost) return reply(CommandError.InsufficientFunds, $"Servono {cost} per l'ammodernamento"); var m = w.Data.Balance.IndustryMaintenance; ind.PendingWork = IndustryWork.Upgrade; int need = IndustryMath.MachineryRequired(ind, m); return reply(CommandError.None, $"{ind.Name}: ammodernamento al livello {ind.Level + 1} programmato ({cost}, {m.UpgradeDays} giorni); servono {need} macchinari (in scorta {ind.Machinery})"); } public static CommandResult OrderParts(World w, Company company, OrderPartsCommand c, RailCommandHandlers.Reply reply) { if (!TryOwned(w, company, c.Industry, out var ind, out var error)) return reply(error.Error, error.Message); int max = w.Data.Balance.IndustryMaintenance.MaxOrderedParts; if (c.Quantity < 0 || c.Quantity > max) return reply(CommandError.InvalidArgument, $"Ordine tra 0 e {max} unità"); ind.PartsOrdered = c.Quantity; string unit = w.Data.Goods[w.Data.SparePartsGood].Unit; return reply(CommandError.None, $"{ind.Name}: ordinati {c.Quantity} {unit} di ricambi oltre la scorta della politica (scorta attuale {ind.SpareParts})"); } public static string BudgetName(IndustryBudget b) => b switch { IndustryBudget.None => "Nessuno", IndustryBudget.High => "Alto", _ => "Base", }; private static bool TryIndustry(World w, IndustryId id, out Industry ind) { ind = null!; if (!id.IsValid || id.Value >= w.Industries.Count) return false; ind = w.Industries[id.Value]; return true; } private static bool TryOwned(World w, Company company, IndustryId id, out Industry ind, out (CommandError Error, string Message) error) { error = default; if (!TryIndustry(w, id, out ind) || ind.Closed) { error = (CommandError.NotFound, "Industria inesistente"); return false; } if (ind.Owner != company.Id) { error = (CommandError.NotAllowed, "L'industria non appartiene alla compagnia"); return false; } return true; } }