Files
cruiseSimulator/Assets/BigShipTycoon/Runtime/HelmNavigation.cs
josh 86ee1b99bf Add manual port pilotage and working bridge instruments
Scale docking momentum by hull, require manual mooring and channel maneuvers in Captain Career, and add rank-specific bridge instruments and voiced training.
2026-11-29 21:34:36 -05:00

187 lines
9.1 KiB
C#

using System;
using UnityEngine;
namespace BigShipTycoon
{
[Serializable]
internal sealed class HelmSnapshot
{
public Vector2 Position, ApproachWaypoint;
public float RouteDuration, Heading, Rudder, Speed, Progress, TurnRate;
public bool Manual, PropulsionStopped, Arrived, ExtendingApproach;
public HelmPracticeSnapshot Practice;
}
internal sealed class HelmNavigation
{
private const float MetresPerKnot = .514444f;
private const float RudderLimit = 35f;
private const float RudderSlew = 15f;
private const float IntegrationStep = .1f;
private readonly float duration, cruiseKnots, turnRateLimit, inertia, arrivalRadius;
private readonly HullSpec riverHull;
private float turnRate;
private Vector2 approachWaypoint;
private bool extendingApproach;
public HelmNavigation(float duration, int hullIndex)
{
this.duration = Mathf.Max(1, duration);
bool river = hullIndex == Market.River;
riverHull = river ? Market.Hulls[hullIndex] : null;
cruiseKnots = river ? 10f : 17f + 2f * hullIndex;
turnRateLimit = river ? 8.5f : 4.2f - .5f * hullIndex;
inertia = river ? .62f : 1f + .16f * hullIndex;
Practice = new HelmPractice(hullIndex);
arrivalRadius = Mathf.Min(this.duration * .15f, Mathf.Clamp(this.duration * .006f, .6f, 1.8f));
}
public bool Manual { get; set; }
public bool PropulsionStopped { get; set; }
public float Heading { get; private set; }
public float Rudder { get; private set; }
public float Speed { get; private set; }
public float Progress { get; private set; }
public Vector2 Position { get; private set; }
public bool Arrived { get; private set; }
public bool AtChannelEdge { get; private set; }
public float RemainingSeconds => Arrived ? 0 : (new Vector2(duration, 0) - Position).magnitude;
internal float CourseToDestination => Mathf.Atan2(-Position.y, duration - Position.x) * Mathf.Rad2Deg;
internal HelmPractice Practice { get; }
public void Advance(float seconds, float rudderInput, float engineSpeedFactor, WeatherCondition weather)
{
if (Arrived || seconds <= 0)
return;
// Simulated X/Y use nominal route seconds. The route duration already includes the sea-state penalty.
float cruising = cruiseKnots / WeatherSlowdown(weather);
float targetSpeed = PropulsionStopped ? 0 : cruising * Mathf.Max(0, engineSpeedFactor);
float requestedRudder = Mathf.Clamp(rudderInput, -1f, 1f) * RudderLimit;
if (Manual)
extendingApproach = false;
for (int step = 0, steps = Mathf.CeilToInt(seconds / IntegrationStep); step < steps && !Arrived; step++)
{
float delta = Mathf.Min(IntegrationStep, seconds - step * IntegrationStep);
Vector2 toPort = new Vector2(duration, 0) - Position;
float rudderDemand = Manual ? requestedRudder : CourseRudder(toPort);
float previousSpeed = Speed;
float acceleration = targetSpeed > Speed ? 1.55f : 1.02f;
Speed = Mathf.MoveTowards(Speed, targetSpeed, delta * acceleration / inertia);
Rudder = Mathf.MoveTowards(Rudder, rudderDemand, delta * RudderSlew);
float waterFlow = Mathf.Clamp01((previousSpeed + Speed) * .5f / cruising);
float desiredTurnRate = Rudder / RudderLimit * turnRateLimit * waterFlow;
turnRate = Mathf.MoveTowards(turnRate, desiredTurnRate, delta * 3.2f / inertia);
float yawStep = turnRate * delta;
float course = (Heading + yawStep * .5f) * Mathf.Deg2Rad;
Heading = Mathf.Repeat(Heading + yawStep + 180f, 360f) - 180f;
float distance = (previousSpeed + Speed) * .5f / cruising * delta;
Vector2 position = Position + new Vector2(Mathf.Cos(course), Mathf.Sin(course)) * distance;
AtChannelEdge = false;
if (riverHull != null)
{
float hullReach = Mathf.Abs(Mathf.Sin(course)) * riverHull.Length * .45f
+ Mathf.Abs(Mathf.Cos(course)) * riverHull.Beam * .5f;
float sideLimit = (33f - hullReach) / (cruising * MetresPerKnot);
AtChannelEdge = Mathf.Abs(position.y) > sideLimit;
position.y = Mathf.Clamp(position.y, -sideLimit, sideLimit);
}
Position = position;
Arrived = RemainingSeconds <= arrivalRadius;
Progress = Arrived ? 1 : Mathf.Max(Progress, Mathf.Clamp01(1 - RemainingSeconds / duration));
}
}
private float CourseRudder(Vector2 toPort)
{
Vector2 toWaypoint = approachWaypoint - Position;
if (extendingApproach && toWaypoint.magnitude < 6)
extendingApproach = false;
float course = Mathf.Atan2(toPort.y, toPort.x) * Mathf.Rad2Deg;
float cruiseTurnRadius = Mathf.Rad2Deg / turnRateLimit;
if (!extendingApproach && toPort.magnitude < cruiseTurnRadius * 1.5f
&& Mathf.Abs(Mathf.DeltaAngle(Heading, course)) > 55f)
{
// Make room for a full turn if the destination is already inside the ship's turning circle.
float heading = Heading * Mathf.Deg2Rad;
toWaypoint = new Vector2(Mathf.Cos(heading), Mathf.Sin(heading)) * (cruiseTurnRadius * 4f);
approachWaypoint = Position + toWaypoint;
extendingApproach = true;
}
if (extendingApproach)
course = Mathf.Atan2(toWaypoint.y, toWaypoint.x) * Mathf.Rad2Deg;
float courseError = Mathf.DeltaAngle(Heading, course);
return Mathf.Clamp(courseError * 1.55f - turnRate * 3.6f, -RudderLimit, RudderLimit);
}
internal void TakeDepartureSpeed(float speedKnots)
{
Manual = PropulsionStopped = false;
Speed = Mathf.Clamp(speedKnots, 0, cruiseKnots);
}
internal float HarborApproachFactor(float cruiseFactor, WeatherCondition weather)
{
float approachFactor = Mathf.Min(cruiseFactor, 4.3f * WeatherSlowdown(weather) / cruiseKnots);
return Mathf.Lerp(cruiseFactor, approachFactor, Mathf.SmoothStep(0, 1, Mathf.InverseLerp(.84f, .99f, Progress)));
}
private static float WeatherSlowdown(WeatherCondition weather) =>
weather switch { WeatherCondition.Swell => 1.14f, WeatherCondition.Storm => 1.32f, _ => 1f };
internal void SetProgress(float progress)
{
Progress = Mathf.Clamp01(progress);
Position = new Vector2(duration * Progress, 0);
Heading = Rudder = turnRate = 0;
extendingApproach = false;
Arrived = Progress >= 1;
}
internal HelmSnapshot CaptureState() => new HelmSnapshot
{
Position = Position, ApproachWaypoint = approachWaypoint, RouteDuration = duration, Heading = Heading,
Rudder = Rudder, Speed = Speed, Progress = Progress, TurnRate = turnRate, Manual = Manual,
PropulsionStopped = PropulsionStopped, Arrived = Arrived, ExtendingApproach = extendingApproach,
Practice = Practice.Capture()
};
internal bool RestoreState(HelmSnapshot state)
{
if (state == null || !Finite(state.RouteDuration) || state.RouteDuration < 1
|| !Finite(state.Position.x) || !Finite(state.Position.y)
|| !Finite(state.ApproachWaypoint.x) || !Finite(state.ApproachWaypoint.y)
|| !Finite(state.Heading) || Mathf.Abs(state.Heading) > 180
|| !Finite(state.Rudder) || Mathf.Abs(state.Rudder) > RudderLimit
|| !Finite(state.Speed) || state.Speed < 0 || state.Speed > 120
|| !Finite(state.Progress) || state.Progress < 0 || state.Progress > 1 || (state.Arrived && state.Progress != 1)
|| !Finite(state.TurnRate) || Mathf.Abs(state.TurnRate) > 40 || !Practice.Restore(state.Practice))
return false;
float scale = duration / state.RouteDuration;
Vector2 position = state.Position * scale;
Vector2 waypoint = state.ApproachWaypoint * scale;
if (!Finite(position.sqrMagnitude) || !Finite(waypoint.sqrMagnitude))
return false;
Position = position;
approachWaypoint = waypoint;
Heading = state.Heading;
Rudder = state.Rudder;
Speed = state.Speed;
Progress = state.Progress;
turnRate = state.TurnRate;
Manual = state.Manual;
PropulsionStopped = state.PropulsionStopped;
Arrived = state.Arrived;
extendingApproach = state.ExtendingApproach;
return true;
}
private static bool Finite(float value) => !float.IsNaN(value) && !float.IsInfinity(value);
}
}