Files
cruiseSimulator/Assets/BigShipTycoon/Runtime/HelmNavigation.cs

157 lines
7.4 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;
}
internal sealed class HelmNavigation
{
private const float RudderLimit = 35f;
private const float RudderSlew = 15f;
private const float IntegrationStep = .1f;
private readonly float duration, cruiseKnots, turnRateLimit, inertia, arrivalRadius;
private float turnRate;
private Vector2 approachWaypoint;
private bool extendingApproach;
public HelmNavigation(float duration, int hullIndex)
{
this.duration = Mathf.Max(1, duration);
cruiseKnots = 17f + 2f * hullIndex;
turnRateLimit = 4.2f - .5f * hullIndex;
inertia = 1f + .16f * 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 float RemainingSeconds => Arrived ? 0 : (new Vector2(duration, 0) - Position).magnitude;
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 / (weather switch
{
WeatherCondition.Swell => 1.14f,
WeatherCondition.Storm => 1.32f,
_ => 1f
});
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;
Position += new Vector2(Mathf.Cos(course), Mathf.Sin(course)) * distance;
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 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
};
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)
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);
}
}