using System; namespace Soundings { [Serializable] internal sealed class OnboardSessionSnapshot { public int Tick, NextEvent, OpenedAt, EventBenefitUntil, FeaturedService, AdditionalCapacity, Decisions; public int Revenue, Expenses; public int[] Prices, ServiceRevenue, SaleUntil, StationReadyAt, StationBoostUntil; public ServiceSampleSnapshot[] Samples; public double Satisfaction, Provisions, Water, Waste, FuelIntegrated, Health, Hydration; public int GalleyUntil, PumpsUntil, TreatmentUntil, EngineMode, RepairCredit; public bool Assisted, StoresOpened; public int NextCrewCall, CrewCallDeadline, CrewCall, CrewRequestsCompleted, PerformanceStarted; public string EventResult; } [Serializable] internal struct ServiceSampleSnapshot { public int Requested, Served, Capacity, Revenue; } public sealed partial class OnboardSession { internal OnboardSessionSnapshot CaptureState() { var samples = new ServiceSampleSnapshot[sample.Length]; for (int i = 0; i < sample.Length; i++) samples[i] = new ServiceSampleSnapshot { Requested = sample[i].Requested, Served = sample[i].Served, Capacity = sample[i].Capacity, Revenue = sample[i].Revenue }; return new OnboardSessionSnapshot { Tick = tick, NextEvent = nextEvent, OpenedAt = openedAt, EventBenefitUntil = eventBenefitUntil, FeaturedService = featuredService, AdditionalCapacity = additionalCapacity, Decisions = decisions, Satisfaction = satisfaction, Revenue = Revenue, Expenses = Expenses, EventResult = EventResult, Prices = (int[])prices.Clone(), ServiceRevenue = (int[])revenue.Clone(), SaleUntil = (int[])saleUntil.Clone(), Samples = samples, Provisions = provisions, Water = water, Waste = waste, FuelIntegrated = fuelIntegrated, Health = health, Hydration = hydration, GalleyUntil = galleyUntil, PumpsUntil = pumpsUntil, TreatmentUntil = treatmentUntil, Assisted = Assisted, EngineMode = EngineMode, RepairCredit = RepairCredit, StoresOpened = StoresOpened, StationReadyAt = (int[])stationReadyAt.Clone(), StationBoostUntil = (int[])stationBoostUntil.Clone(), NextCrewCall = nextCrewCall, CrewCallDeadline = crewCallDeadline, CrewCall = CrewCall is StationKind station ? (int)station : -1, CrewRequestsCompleted = CrewRequestsCompleted, PerformanceStarted = performanceStarted }; } internal bool RestoreState(OnboardSessionSnapshot state) { if (!IsValidSnapshot(state)) return false; tick = state.Tick; nextEvent = state.NextEvent; openedAt = state.OpenedAt; eventBenefitUntil = state.EventBenefitUntil; featuredService = state.FeaturedService; additionalCapacity = state.AdditionalCapacity; decisions = state.Decisions; satisfaction = state.Satisfaction; Revenue = state.Revenue; Expenses = state.Expenses; EventResult = state.EventResult; Array.Copy(state.Prices, prices, state.Prices.Length); Array.Copy(state.ServiceRevenue, revenue, state.ServiceRevenue.Length); Array.Copy(state.SaleUntil, saleUntil, state.SaleUntil.Length); provisions = state.Provisions; water = state.Water; waste = state.Waste; fuelIntegrated = state.FuelIntegrated; health = state.Health; hydration = state.Hydration; galleyUntil = state.GalleyUntil; pumpsUntil = state.PumpsUntil; treatmentUntil = state.TreatmentUntil; Assisted = state.Assisted; EngineMode = state.EngineMode; RepairCredit = state.RepairCredit; StoresOpened = state.StoresOpened; Array.Copy(state.StationReadyAt, stationReadyAt, state.StationReadyAt.Length); Array.Copy(state.StationBoostUntil, stationBoostUntil, state.StationBoostUntil.Length); nextCrewCall = state.NextCrewCall; crewCallDeadline = state.CrewCallDeadline; CrewCall = state.CrewCall < 0 ? null : (StationKind)state.CrewCall; CrewRequestsCompleted = state.CrewRequestsCompleted; performanceStarted = state.PerformanceStarted; // Evaluate newly introduced services before restoring the exact saved readouts for existing ones. Measure(false); for (int i = 0; i < state.Samples.Length; i++) { ServiceSampleSnapshot saved = state.Samples[i]; sample[i] = new OnboardSample(saved.Requested, saved.Served, saved.Capacity, saved.Revenue); } return true; } private bool IsValidSnapshot(OnboardSessionSnapshot state) { const int OriginalServiceCount = 5; const int OriginalStationCount = (int)StationKind.Pool + 1; if (state?.Prices == null || state.Prices.Length < OriginalServiceCount || state.Prices.Length > ServiceKinds || state.ServiceRevenue?.Length != state.Prices.Length || state.SaleUntil?.Length != state.Prices.Length || state.Samples?.Length != state.Prices.Length || state.StationBoostUntil?.Length != state.Prices.Length || state.StationReadyAt == null || state.StationReadyAt.Length < OriginalStationCount || state.StationReadyAt.Length > stationReadyAt.Length) return false; int offerCount = (Quote.Weather == WeatherCondition.Storm ? StormEvents : Events).Length; if (state.Tick < 0 || state.Tick > ticks || state.NextEvent < 0 || state.NextEvent > offerCount || state.OpenedAt < -1 || state.OpenedAt > state.Tick || (state.OpenedAt >= 0 && state.NextEvent >= offerCount) || state.FeaturedService < -1 || state.FeaturedService >= state.Prices.Length || state.AdditionalCapacity < 0 || state.EventBenefitUntil < 0 || state.Decisions < 0 || state.Decisions > offerCount || state.Revenue < 0 || state.Expenses < 0 || state.EngineMode < 0 || state.EngineMode >= FuelRates.Length || state.RepairCredit < 0 || state.RepairCredit > 6 || state.GalleyUntil < 0 || state.PumpsUntil < 0 || state.TreatmentUntil < 0 || state.NextCrewCall < 0 || state.NextCrewCall > CrewCallSchedule.Length || state.CrewCall < -1 || state.CrewCall >= state.StationReadyAt.Length || state.CrewCallDeadline < 0 || state.CrewCallDeadline > ticks || state.CrewRequestsCompleted < 0 || state.CrewRequestsCompleted > CrewCallSchedule.Length || state.PerformanceStarted < -1 || state.PerformanceStarted > state.Tick) return false; if (!AmountInRange(state.Satisfaction, 100) || !AmountInRange(state.Health, 100) || !AmountInRange(state.Hydration, 1) || !AmountInRange(state.Provisions, Ship.FoodCapacity) || !AmountInRange(state.Water, Ship.WaterCapacity) || !AmountInRange(state.Waste, Ship.WasteCapacity * 1.3) || !AmountInRange(state.FuelIntegrated, ticks * 1.23)) return false; for (int i = 0; i < state.Prices.Length; i++) { ServiceSampleSnapshot saved = state.Samples[i]; if (state.Prices[i] < 0 || state.Prices[i] > Defaults[i] * 4 || state.ServiceRevenue[i] < 0 || state.SaleUntil[i] < 0 || state.StationBoostUntil[i] < 0 || saved.Requested < 0 || saved.Served < 0 || saved.Capacity < 0 || saved.Revenue < 0) return false; } foreach (int ready in state.StationReadyAt) if (ready < 0) return false; return true; } private static bool AmountInRange(double value, double maximum) => value >= 0 && value <= maximum + .0001; } }