using System; using UnityEngine; namespace BigShipTycoon { internal enum HelmExercise { EconomyOrder, ChangeHeading, ReturnToCourse, HoldCourse, Qualified } [Serializable] internal sealed class HelmPracticeSnapshot { public HelmExercise Exercise; public float Target, SteadySeconds; } internal sealed class HelmPractice { private readonly float turnDegrees, holdSeconds; private float target, steadySeconds; internal HelmPractice(int hullIndex) { (turnDegrees, holdSeconds) = hullIndex switch { Market.River => (12f, 6f), Market.Coastal => (16f, 8f), Market.Regional => (19f, 9f), Market.Ocean => (22f, 10f), _ => (24f, 12f) }; } internal HelmExercise Exercise { get; private set; } internal bool Qualified => Exercise == HelmExercise.Qualified; internal void OrderEconomy(HelmNavigation helm) { if (!helm.Manual || Exercise != HelmExercise.EconomyOrder) return; target = Mathf.DeltaAngle(0, helm.Heading + turnDegrees); Exercise = HelmExercise.ChangeHeading; } internal void Advance(HelmNavigation helm, float delta, float rudderInput) { if (!helm.Manual || helm.PropulsionStopped || helm.Speed < 2 || delta <= 0 || Qualified) return; float courseError = Mathf.Abs(Mathf.DeltaAngle(helm.Heading, helm.CourseToDestination)); switch (Exercise) { case HelmExercise.ChangeHeading when Mathf.Abs(Mathf.DeltaAngle(helm.Heading, target)) <= 3f: Exercise = HelmExercise.ReturnToCourse; break; case HelmExercise.ReturnToCourse when courseError <= 4f: Exercise = HelmExercise.HoldCourse; break; case HelmExercise.HoldCourse: if (courseError > 8f) { steadySeconds = 0; Exercise = HelmExercise.ReturnToCourse; } else if (courseError <= 6f && Mathf.Abs(helm.Rudder) <= 6f && Mathf.Abs(rudderInput) < .2f) { steadySeconds = Mathf.Min(holdSeconds, steadySeconds + Mathf.Min(.1f, delta)); if (steadySeconds >= holdSeconds) Exercise = HelmExercise.Qualified; } break; } } internal string Instruction(HelmNavigation helm, float trueBearing) { int TrueHeading(float relative) => Mathf.RoundToInt(Mathf.Repeat(trueBearing + relative, 360)) % 360; string Turn(float desired) { float error = Mathf.DeltaAngle(helm.Heading, desired); return Mathf.Abs(error) <= 3f ? "Center the rudder" : error > 0 ? "D to starboard" : "A to port"; } return Exercise switch { HelmExercise.EconomyOrder => "1/3 · Select Economy on the throttle to start.", HelmExercise.ChangeHeading => $"2/3 · Steer to {TrueHeading(target):000}° true\n{Turn(target)}", HelmExercise.ReturnToCourse => $"3/3 · Return to {TrueHeading(helm.CourseToDestination):000}° true\n{Turn(helm.CourseToDestination)}", HelmExercise.HoldCourse => $"3/3 · Center rudder and hold course\n{Mathf.FloorToInt(steadySeconds)} / {holdSeconds:0} s", _ => "Helm check passed. Bring the voyage to port to log it." }; } internal HelmPracticeSnapshot Capture() => new() { Exercise = Exercise, Target = target, SteadySeconds = steadySeconds }; internal bool Restore(HelmPracticeSnapshot snapshot) { if (snapshot == null) return true; if ((uint)snapshot.Exercise > (uint)HelmExercise.Qualified || !(snapshot.Target >= -180 && snapshot.Target <= 180) || !(snapshot.SteadySeconds >= 0 && snapshot.SteadySeconds <= holdSeconds) || snapshot.Exercise == HelmExercise.Qualified && snapshot.SteadySeconds != holdSeconds || (int)snapshot.Exercise < (int)HelmExercise.HoldCourse && snapshot.SteadySeconds != 0) return false; Exercise = snapshot.Exercise; target = snapshot.Target; steadySeconds = snapshot.SteadySeconds; return true; } } }