Files
cruiseSimulator/Assets/BigShipTycoon/Runtime/DockManeuver.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

310 lines
15 KiB
C#

using System;
using UnityEngine;
namespace BigShipTycoon
{
internal enum DockOrder
{
HalfAstern = -2, DeadSlowAstern = -1, Stop = 0, DeadSlowAhead = 1, HalfAhead = 2
}
[Serializable]
internal sealed class DockManeuverSnapshot
{
public bool Arriving, BowLine, SternLine, AtChannelEdge;
public Vector2 Position;
public float Heading, Surge, Sway, TurnRate, Engine, Rudder, BowThruster;
public DockOrder Order;
}
internal sealed class DockManeuver
{
private const float MetresPerKnot = .514444f;
private const float RudderLimit = 35f;
private const float IntegrationStep = .05f;
private const float WaterDensity = 1025f;
internal const float LinesSpeedLimit = .43f;
internal const float LinesHeadingLimit = 12f;
internal const float HandoffHeadingLimit = 14f;
internal const float HandoffMinimumSpeed = .65f;
internal const float HandoffMaximumSpeed = 6.8f;
private readonly float length, beam, surgeMass, swayMass, yawInertia, engineForce, bowForce, rudderArea;
private readonly float drag, linearDrag, turnDamping, engineResponse, turnLimit;
private float surge, sway, engine;
internal DockManeuver(int hullIndex, bool arriving, float approachSpeed = 0)
{
HullSpec hull = Market.Hulls[hullIndex];
length = hull.Length;
beam = hull.Beam;
float draft = hullIndex switch
{
Market.River => 1.38f, Market.Coastal => 3.15f, Market.Regional => 4.4f,
Market.Ocean => 5.85f, _ => 8.55f
};
float block = hullIndex switch
{
Market.River => .68f, Market.Coastal => .60f, Market.Regional => .63f,
Market.Ocean => .56f, _ => .69f
};
float mass = (hullIndex == Market.River ? 1000f : WaterDensity) * length * beam * draft * block;
DisplacementTonnes = mass * .001f;
float scale = Mathf.InverseLerp(5.4f, 8f, Mathf.Log10(mass));
// Water pushed and turned with the hull adds more inertia in a sideways manoeuvre.
surgeMass = mass * 1.08f;
swayMass = mass * 1.64f;
yawInertia = mass * (.058f * length * length + .12f * beam * beam);
rudderArea = beam * draft * .245f;
engineForce = surgeMass * Mathf.Lerp(.48f, .20f, scale);
bowForce = swayMass * Mathf.Lerp(.116f, .050f, scale);
drag = Mathf.Lerp(.047f, .030f, scale);
linearDrag = Mathf.Lerp(.051f, .017f, scale);
turnDamping = Mathf.Lerp(.37f, .23f, scale);
engineResponse = Mathf.Lerp(.92f, 3.0f, scale);
turnLimit = Mathf.Lerp(7.6f, 2.6f, scale);
Arriving = arriving;
HasBowThruster = hullIndex is Market.Regional or Market.Ocean or Market.Mega;
Berth = new Vector2(0, Mathf.Max(5f, beam * .42f + 1.2f));
PierY = Berth.y + beam * .5f + Mathf.Max(1f, beam * .07f);
PierHalfLength = length * .57f + 6f;
ChannelEnd = Mathf.Max(length * .42f + 65f, PierHalfLength + Mathf.Max(length * .32f, 18f));
ChannelHalfWidth = beam * 1.30f + 16f;
if (arriving)
{
// Larger ships enter on the berth-side leading line to leave room for the stern to swing.
Position = new Vector2(ChannelEnd, HasBowThruster ? Berth.y * .42f : 0);
Heading = 180f;
surge = Mathf.Clamp(approachSpeed, 0, HandoffMaximumSpeed) * MetresPerKnot;
}
else
{
Position = Berth;
BowLine = SternLine = true;
}
}
internal bool Arriving { get; }
internal bool HasBowThruster { get; }
internal float DisplacementTonnes { get; }
internal Vector2 Berth { get; }
internal float PierY { get; }
internal float PierHalfLength { get; }
internal float ChannelEnd { get; }
internal float ChannelHalfWidth { get; }
internal float BerthAlongTolerance => Mathf.Max(5f, length * .055f);
internal float BerthCrossTolerance => Mathf.Max(3.3f, beam * .21f);
internal float HandoffCrossTolerance => Mathf.Max(4.2f, beam * .36f);
internal float TargetHeading => Arriving ? 180f : 0f;
internal Vector2 Position { get; private set; }
internal float Heading { get; private set; }
internal float SpeedKnots => surge / MetresPerKnot;
internal float LateralSpeedKnots => sway / MetresPerKnot;
internal float SpeedOverGroundKnots => new Vector2(surge, sway).magnitude / MetresPerKnot;
internal float NeutralCoastMetres => Mathf.Abs(surge) <= LinesSpeedLimit * MetresPerKnot ? 0
: Mathf.Log((linearDrag + drag * Mathf.Abs(surge)) / (linearDrag + drag * LinesSpeedLimit * MetresPerKnot)) / drag;
internal float Rudder { get; private set; }
internal float TurnRate { get; private set; }
internal float BowThruster { get; private set; }
internal DockOrder Order { get; private set; }
internal bool BowLine { get; private set; }
internal bool SternLine { get; private set; }
internal bool AtChannelEdge { get; private set; }
internal bool LinesAttached => BowLine || SternLine;
internal bool ChannelClear => !Arriving && !LinesAttached && Position.x >= ChannelEnd;
internal bool AtBerth => IsAtBerth(Position, Heading);
internal bool ReadyForAutopilot => ChannelClear && Mathf.Abs(Position.y) <= HandoffCrossTolerance
&& Mathf.Abs(Mathf.DeltaAngle(Heading, TargetHeading)) <= HandoffHeadingLimit
&& SpeedKnots >= HandoffMinimumSpeed && SpeedKnots <= HandoffMaximumSpeed;
internal bool Secured => Arriving && BowLine && SternLine;
private float MinX => -Mathf.Max(26f, length * .31f);
private float MaxX => ChannelEnd + Mathf.Max(32f, length * .24f);
internal void SetOrder(DockOrder order)
{
if (!LinesAttached && order is >= DockOrder.HalfAstern and <= DockOrder.HalfAhead)
Order = order;
}
internal bool CanToggleLine(bool bow)
{
bool attached = bow ? BowLine : SternLine;
if (!Arriving)
return attached;
return !attached && (LinesAttached || AtBerth && SpeedOverGroundKnots <= LinesSpeedLimit
&& Mathf.Abs(TurnRate) <= .75f && Order == DockOrder.Stop
&& Mathf.Abs(engine) <= .055f && Mathf.Abs(BowThruster) <= .075f);
}
internal bool ToggleLine(bool bow)
{
if (!CanToggleLine(bow))
return false;
if (bow)
BowLine = Arriving;
else
SternLine = Arriving;
if (Arriving)
{
surge = sway = engine = TurnRate = BowThruster = 0;
Order = DockOrder.Stop;
}
return true;
}
internal void Advance(float seconds, float rudderInput, float thrusterInput)
{
if (!(seconds > 0) || !Finite(seconds))
return;
float requestedRudder = Mathf.Clamp(rudderInput, -1f, 1f) * RudderLimit;
float requestedThruster = HasBowThruster ? Mathf.Clamp(thrusterInput, -1f, 1f) : 0f;
for (int step = 0, steps = Mathf.CeilToInt(seconds / IntegrationStep); step < steps; step++)
{
float delta = Mathf.Min(IntegrationStep, seconds - step * IntegrationStep);
AdvanceStep(delta, requestedRudder, requestedThruster);
}
}
private void AdvanceStep(float delta, float rudder, float thruster)
{
Rudder = Mathf.MoveTowards(Rudder, rudder, delta * 25f);
if (LinesAttached)
return;
float engineOrder = Order switch
{
DockOrder.HalfAstern => -.65f, DockOrder.DeadSlowAstern => -.22f,
DockOrder.DeadSlowAhead => .29f, DockOrder.HalfAhead => .86f, _ => 0f
};
engine = Mathf.MoveTowards(engine, engineOrder, delta / engineResponse);
// A transverse jet loses authority once water flows briskly past its tunnel.
float thrusterAuthority = 1f - Mathf.InverseLerp(1.4f, 4.35f, Mathf.Abs(SpeedKnots));
BowThruster = Mathf.MoveTowards(BowThruster, thruster * thrusterAuthority, delta * 2.3f);
float lateralThrust = BowThruster * bowForce;
float propellerWash = engine * (engine > 0 ? 4.95f : .8f);
float rudderFlow = surge + propellerWash;
float rudderForce = .52f * WaterDensity * rudderArea * rudderFlow * Mathf.Abs(rudderFlow) * (Rudder / RudderLimit);
float power = engine * engineForce;
float surgeDamping = surge * (linearDrag + drag * Mathf.Abs(surge));
float swayDamping = sway * (.30f + .18f * Mathf.Abs(sway));
float angularVelocity = TurnRate * Mathf.Deg2Rad;
float previousSurge = surge, previousSway = sway;
surge += (power / surgeMass - surgeDamping + angularVelocity * previousSway) * delta;
sway += ((lateralThrust - rudderForce) / swayMass - swayDamping - angularVelocity * previousSurge) * delta;
float torque = length * (.46f * rudderForce + .39f * lateralThrust);
TurnRate = Mathf.Clamp(TurnRate + (torque / yawInertia * Mathf.Rad2Deg
- TurnRate * (turnDamping + .11f * Mathf.Abs(surge))) * delta, -turnLimit, turnLimit);
float nextHeading = Mathf.DeltaAngle(0, Heading + TurnRate * delta);
Vector2 forward = Forward(Heading + TurnRate * delta * .5f);
Vector2 right = new Vector2(-forward.y, forward.x);
Vector2 next = Position + (forward * (previousSurge + surge) + right * (previousSway + sway)) * (.5f * delta);
Constrain(next, nextHeading, delta);
}
private void Constrain(Vector2 next, float heading, float delta)
{
float x = Mathf.Clamp(next.x, MinX, MaxX);
AtChannelEdge = !Mathf.Approximately(x, next.x);
if (!TryChannelLimits(x, heading, out float left, out float right))
{
heading = Heading;
TurnRate = 0;
AtChannelEdge = true;
if (!TryChannelLimits(x, heading, out left, out right))
{
x = Position.x;
TryChannelLimits(x, heading, out left, out right);
}
}
float y = Mathf.Clamp(next.y, left, right);
if (!Mathf.Approximately(y, next.y) || AtChannelEdge)
{
AtChannelEdge = true;
Vector2 forward = Forward(heading), across = new Vector2(-forward.y, forward.x);
Vector2 velocity = forward * surge + across * sway;
if ((next.y - y) * velocity.y > 0)
velocity.y = 0;
if ((next.x - x) * velocity.x > 0)
velocity.x = 0;
surge = Vector2.Dot(velocity, forward);
sway = Vector2.Dot(velocity, across);
TurnRate *= Mathf.Exp(-3f * delta);
}
Position = new Vector2(x, y);
Heading = heading;
}
private bool TryChannelLimits(float x, float heading, out float left, out float right)
{
Vector2 direction = Forward(heading);
float along = ProjectedHullReach(Mathf.Abs(direction.x) * length, Mathf.Abs(direction.y) * beam);
float across = ProjectedHullReach(Mathf.Abs(direction.y) * length, Mathf.Abs(direction.x) * beam);
left = -ChannelHalfWidth + across;
right = ChannelHalfWidth - across;
if (Mathf.Abs(x) < PierHalfLength + along)
right = Mathf.Min(right, PierY - across);
return left <= right;
}
// The ends taper before the maximum beam. A rectangular footprint would
// stop a bow or stern swinging into clear water beside the fenders.
private static float ProjectedHullReach(float along, float across) =>
Mathf.Max(along * .485f + across * .34f, along * .33f + across * .5f);
private bool IsAtBerth(Vector2 position, float heading) =>
Mathf.Abs(position.x - Berth.x) <= BerthAlongTolerance && Mathf.Abs(position.y - Berth.y) <= BerthCrossTolerance
&& Mathf.Abs(Mathf.DeltaAngle(heading, TargetHeading)) <= LinesHeadingLimit;
private static Vector2 Forward(float heading)
{
float radians = heading * Mathf.Deg2Rad;
return new Vector2(Mathf.Cos(radians), Mathf.Sin(radians));
}
internal DockManeuverSnapshot Capture() => new DockManeuverSnapshot
{
Arriving = Arriving, BowLine = BowLine, SternLine = SternLine, AtChannelEdge = AtChannelEdge,
Position = Position, Heading = Heading, Surge = surge, Sway = sway, TurnRate = TurnRate,
Engine = engine, Rudder = Rudder, BowThruster = BowThruster, Order = Order
};
internal bool Restore(DockManeuverSnapshot state)
{
if (state == null || state.Arriving != Arriving || !Finite(state.Position.x) || !Finite(state.Position.y)
|| !Finite(state.Heading, 180) || !Finite(state.Rudder, RudderLimit)
|| !Finite(state.Surge, 12) || !Finite(state.Sway, 8) || !Finite(state.TurnRate, turnLimit)
|| !Finite(state.Engine, 1) || !Finite(state.BowThruster, 1) || !HasBowThruster && state.BowThruster != 0
|| state.Order is < DockOrder.HalfAstern or > DockOrder.HalfAhead
|| !TryChannelLimits(state.Position.x, state.Heading, out float left, out float right)
|| state.Position.x < MinX - .001f || state.Position.x > MaxX + .001f
|| state.Position.y < left - .001f || state.Position.y > right + .001f)
return false;
if ((state.BowLine || state.SternLine) && (state.Surge != 0 || state.Sway != 0 || state.Engine != 0
|| state.TurnRate != 0 || state.BowThruster != 0 || state.Order != DockOrder.Stop
|| !IsAtBerth(state.Position, state.Heading)))
return false;
BowLine = state.BowLine;
SternLine = state.SternLine;
AtChannelEdge = state.AtChannelEdge;
Position = state.Position;
Heading = state.Heading;
surge = state.Surge;
sway = state.Sway;
TurnRate = state.TurnRate;
engine = state.Engine;
Rudder = state.Rudder;
BowThruster = state.BowThruster;
Order = state.Order;
return true;
}
private static bool Finite(float number) => !float.IsNaN(number) && !float.IsInfinity(number);
private static bool Finite(float number, float magnitude) => Finite(number) && Mathf.Abs(number) <= magnitude;
}
}