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.
This commit is contained in:
josh
2026-11-29 21:34:36 -05:00
parent 290e4cb45d
commit 86ee1b99bf
57 changed files with 2454 additions and 62 deletions

View File

@@ -0,0 +1,278 @@
using System.Collections.Generic;
using UnityEngine;
namespace BigShipTycoon
{
public sealed partial class Harbor
{
private readonly List<Mesh> dockMeshes = new();
private readonly List<Material> dockFinishes = new();
private readonly Vector3[] ropePoints = new Vector3[9];
private GameObject dockSite;
private PortScenery dockWaterfront;
private LineRenderer dockBowRope, dockSternRope;
private Vector3 dockBowBollard, dockSternBollard;
private float dockSeaSeverity, dockOrbitYaw;
internal bool DockManeuverVisible => dockSite != null && dockSite.activeSelf && portQuay != null && portQuay.activeSelf;
internal (bool Bow, bool Stern) DockLinesVisible => (dockBowRope != null && dockBowRope.enabled, dockSternRope != null && dockSternRope.enabled);
internal void BeginDockManeuver(int port, DockManeuver maneuver)
{
if (vessel == null || maneuver == null || (uint)port >= (uint)PortProfiles.Length)
return;
EndDockManeuver();
if (!sailing)
BeginPassage();
HideRiverBanks();
portScenery?.SetVisible(false);
portMatteAspect = 0;
dockSite = new GameObject(Market.Ports[port].Name + " maneuvering area");
dockSite.transform.SetParent(transform, false);
float berthLength = maneuver.PierHalfLength * 2f;
if (portQuay == null || !Mathf.Approximately(quayLength, berthLength))
{
ReleaseCinematicQuay();
quayLength = berthLength;
portQuay = BuildPortQuay();
}
float quayZ = -maneuver.PierY - QuayWidth * .5f;
portQuay.transform.SetLocalPositionAndRotation(new Vector3(maneuver.Berth.x, 0, quayZ),
Quaternion.Euler(0, 180f, 0));
portQuay.SetActive(true);
Vector3 quay = portQuay.transform.position;
water.SetVector("_PortBerth", new Vector4(quay.x, quay.z, quayLength * .5f, QuayWidth * .5f));
PortProfile profile = PortProfiles[port];
BuildDockWaterfront(port, profile, maneuver);
BuildDockChannel(maneuver);
quayFinishes[0].color = Color.Lerp(new Color(.41f, .44f, .45f), profile.Facade, .35f);
quayFinishes[1].color = Color.Lerp(new Color(.53f, .52f, .47f), profile.Facade, .5f);
water.SetColor("_PortSky", profile.Sky);
water.SetFloat("_PortShallows", port is 1 or 3 or 13 or 16 ? .72f : .25f);
water.SetFloat("_PortMatteActive", 0);
water.SetFloat("_PortMotion", 1);
dockOrbitYaw = maneuver.Arriving ? 153f : -153f;
dockSeaSeverity = seaSeverity * .37f;
water.SetFloat("_WeatherSeverity", dockSeaSeverity);
Shader.SetGlobalFloat("_TycoonSeaWeather", dockSeaSeverity);
hullMotion = hullMotionVelocity = Vector3.zero;
wake.Reset();
PresentDockManeuver(maneuver, 0);
ResetOrbit(true);
}
internal void PresentDockManeuver(DockManeuver maneuver, float delta)
{
if (dockSite == null || vessel == null)
return;
seaTime += delta;
Vector2 center = new(maneuver.Position.x, -maneuver.Position.y);
float heading = maneuver.Heading;
float speed = maneuver.SpeedKnots;
bool river = displayedShip?.HullIndex == Market.River;
if (delta > 0)
{
float wash = Mathf.Lerp(.16f, river ? .41f : .30f, Mathf.Clamp01(maneuver.SpeedOverGroundKnots / 5f));
Vector3 target = SampleSeaResponse(center, seaTime, heading, vesselLength, vesselBeam, dockSeaSeverity, river) * wash;
float response = Mathf.Lerp(.39f, .92f, Mathf.InverseLerp(32f, 300f, vesselLength));
hullMotion = Vector3.SmoothDamp(hullMotion, target, ref hullMotionVelocity, response, Mathf.Infinity, delta);
}
passageHeading = heading;
vessel.SetLocalPositionAndRotation(new Vector3(center.x, hullMotion.x, center.y),
Quaternion.AngleAxis(heading, Vector3.up) * Quaternion.Euler(hullMotion.y, 0, hullMotion.z));
Vector2 bow = PortBow(heading);
water.SetVector("_HullSpan", new Vector4(vesselLength, vesselBeam, center.x, center.y));
water.SetVector("_HullCourse", new Vector4(bow.x, -bow.y, Mathf.Clamp01(Mathf.Max(0, speed) / 19f), 0));
water.SetFloat("_SeaTime", seaTime);
waterSurface.localPosition = new Vector3(center.x, 0, center.y);
Shader.SetGlobalFloat("_TycoonSeaTime", seaTime);
float wakeSpeed = (speed >= 0 ? speed : Mathf.Abs(speed) * .60f) * (river ? .72f : 1f);
wake.Update(center, bow, wakeSpeed, vesselLength, vesselBeam, seaTime, dockSeaSeverity, sheltered: true);
SetRudder(maneuver.Rudder);
SetDockRope(dockBowRope, dockBowBollard, maneuver.BowLine, true, maneuver.Arriving);
SetDockRope(dockSternRope, dockSternBollard, maneuver.SternLine, false, maneuver.Arriving);
}
internal void EndDockManeuver()
{
if (dockSite == null)
return;
ReleaseDockManeuver();
if (portQuay != null)
{
portQuay.SetActive(false);
portQuay.transform.localRotation = Quaternion.identity;
}
water.SetFloat("_PortMotion", 0);
water.SetFloat("_WeatherSeverity", sailing ? seaSeverity : 0);
Shader.SetGlobalFloat("_TycoonSeaWeather", sailing ? seaSeverity : 0);
wake.Reset();
}
private void ReleaseDockManeuver()
{
dockWaterfront?.Release();
dockWaterfront = null;
if (dockSite != null)
{
dockSite.SetActive(false);
Destroy(dockSite);
dockSite = null;
}
foreach (Mesh mesh in dockMeshes)
Destroy(mesh);
foreach (Material material in dockFinishes)
Destroy(material);
dockMeshes.Clear();
dockFinishes.Clear();
dockBowRope = dockSternRope = null;
}
private void BuildDockWaterfront(int port, PortProfile profile, DockManeuver maneuver)
{
var shore = new GameObject("Port shoreline orientation").transform;
shore.SetParent(dockSite.transform, false);
float shoreDatum = Market.IsRiverPort(port) ? 27f : 46f;
float apronDepth = profile.Terrain is PortTerrain.Cliffs or PortTerrain.Volcanic ? 25f : 16f;
float shoreZ = -maneuver.PierY - QuayWidth - apronDepth + shoreDatum;
shore.SetLocalPositionAndRotation(new Vector3(0, 0, shoreZ), Quaternion.Euler(0, 180f, 0));
dockWaterfront = new PortScenery(shore, port, profile);
var apron = new HarborMesh();
float apronWidth = Mathf.Clamp(vesselLength * .35f, 12f, 52f);
float edge = -maneuver.PierY - QuayWidth;
apron.Box(new Vector3(maneuver.Berth.x, .53f, edge - apronDepth * .5f),
new Vector3(apronWidth, 1.55f, apronDepth + 1.1f));
DockBatch(apron, "Cargo apron between quay and shore", Color.Lerp(profile.Facade, new Color(.43f, .45f, .44f), .67f));
}
private void BuildDockChannel(DockManeuver maneuver)
{
var red = new HarborMesh();
var green = new HarborMesh();
var white = new HarborMesh();
var work = new HarborMesh();
var shadows = new HarborMesh();
float signal = Mathf.Clamp(vesselLength * .024f, .84f, 2.25f);
for (int mark = 0; mark < 4; mark++)
{
float x = maneuver.ChannelEnd * Mathf.Lerp(.26f, 1f, mark / 3f);
float lateral = maneuver.ChannelHalfWidth + signal * .46f;
float shoreSide = x < maneuver.PierHalfLength ? Mathf.Min(lateral, maneuver.PierY - signal * .74f) : lateral;
BuildDockMark(green, white, shadows, x, -shoreSide, signal, false, mark == 3);
BuildDockMark(red, white, shadows, x, lateral, signal, true, mark == 3);
}
float mooringX = vesselLength * .365f;
float sign = maneuver.Arriving ? -1f : 1f;
float bollardZ = -maneuver.PierY - 1.29f;
dockBowBollard = new Vector3(maneuver.Berth.x + sign * mooringX, 1.76f, bollardZ);
dockSternBollard = new Vector3(maneuver.Berth.x - sign * mooringX, 1.76f, bollardZ);
BuildDockBollard(work, shadows, dockBowBollard);
BuildDockBollard(work, shadows, dockSternBollard);
DockBatch(red, "Red channel marks", new Color(.82f, .18f, .105f));
DockBatch(green, "Green channel marks", new Color(.11f, .57f, .36f));
DockBatch(white, "Channel lamps and range boards", new Color(.89f, .86f, .73f), .12f);
DockBatch(work, "Mooring bollard jackets and deck paint", new Color(.87f, .63f, .22f));
DockBatch(shadows, "Channel mark metalwork", new Color(.20f, .23f, .25f));
var rope = new Material(surface) { color = new Color(.94f, .79f, .52f), name = "Mooring line fibre" };
rope.SetFloat("_Roughness", .83f);
rope.SetFloat("_Finish", (float)SurfaceFinish.Fabric);
rope.SetFloat("_Emissive", .05f);
dockFinishes.Add(rope);
dockBowRope = CreateDockRope("Bow mooring line", rope);
dockSternRope = CreateDockRope("Stern mooring line", rope);
}
private static void BuildDockMark(HarborMesh paint, HarborMesh light, HarborMesh metal, float x, float z,
float size, bool conical, bool gate)
{
Vector3 foot = new(x, -.11f, z);
Vector3 cap = foot + Vector3.up * (size * (gate ? 3.65f : 2.75f));
paint.Rod(foot, foot + Vector3.up * size * .83f, size * .47f, 12, true);
metal.Rod(foot + Vector3.up * size * .64f, cap, size * .105f, 8, true);
float signRadius = size * (gate ? .54f : .44f);
Vector3 board = cap - Vector3.up * size * .69f;
if (conical)
paint.Loft(DockMarkRing(board, signRadius), new Vector2(x, z), true,
new Vector3(1, board.y, 1), new Vector3(.07f, board.y + size * .67f, .07f));
else
paint.Box(board + Vector3.up * size * .24f, new Vector3(signRadius * 1.5f, size * .62f, signRadius * 1.8f));
light.Radome(cap + Vector3.up * size * .059f, size * .16f);
if (gate)
light.Rod(foot + Vector3.up * size * .39f, foot + Vector3.up * size * .61f, size * .475f, 12, true);
}
private static Vector2[] DockMarkRing(Vector3 point, float radius)
{
var ring = new Vector2[10];
for (int i = 0; i < ring.Length; i++)
{
float angle = i * Mathf.PI * 2f / ring.Length;
ring[i] = new Vector2(point.x + Mathf.Cos(angle) * radius, point.z - Mathf.Sin(angle) * radius);
}
return ring;
}
private static void BuildDockBollard(HarborMesh paint, HarborMesh metal, Vector3 foot)
{
metal.Rod(foot - Vector3.up * .44f, foot, .24f, 10, true);
paint.Rod(foot - Vector3.up * .04f, foot + Vector3.up * .14f, .31f, 10, true);
paint.Box(foot + new Vector3(0, -.406f, .77f), new Vector3(1.52f, .023f, .21f));
}
private LineRenderer CreateDockRope(string label, Material material)
{
var gameObject = new GameObject(label, typeof(LineRenderer)) { layer = 8 };
gameObject.transform.SetParent(dockSite.transform, false);
var renderer = gameObject.GetComponent<LineRenderer>();
renderer.sharedMaterial = material;
renderer.positionCount = ropePoints.Length;
renderer.useWorldSpace = true;
renderer.generateLightingData = true;
renderer.widthMultiplier = Mathf.Clamp(vesselLength * .0036f, .093f, .26f);
renderer.SetPositions(ropePoints);
return renderer;
}
private void SetDockRope(LineRenderer rope, Vector3 bollard, bool attached, bool bow, bool arriving)
{
rope.enabled = attached;
if (!attached)
return;
float side = arriving ? 1f : -1f;
float deckHeight = VesselExterior.BaseDeck(displayedShip);
float reach = bow ? .67f : .93f;
Vector3 cleat = vessel.TransformPoint(new Vector3(vesselLength * (bow ? .365f : -.365f), deckHeight + .18f, side * vesselBeam * reach));
Vector3 pier = transform.TransformPoint(bollard);
float sag = Mathf.Clamp(Vector3.Distance(cleat, pier) * .054f, .10f, .47f);
for (int i = 0; i < ropePoints.Length; i++)
{
float t = i / (ropePoints.Length - 1f);
ropePoints[i] = Vector3.Lerp(cleat, pier, t) - Vector3.up * (4f * t * (1f - t) * sag);
}
rope.SetPositions(ropePoints);
}
private void DockBatch(HarborMesh batch, string name, Color color, float emission = 0)
{
var model = new GameObject(name, typeof(MeshFilter), typeof(MeshRenderer)) { layer = 8 };
model.transform.SetParent(dockSite.transform, false);
Mesh mesh = batch.Bake();
var material = new Material(surface) { color = color, name = name };
material.SetFloat("_Roughness", .76f);
material.SetFloat("_Weather", .32f);
material.SetFloat("_Emissive", emission);
model.GetComponent<MeshFilter>().sharedMesh = mesh;
model.GetComponent<MeshRenderer>().sharedMaterial = material;
dockMeshes.Add(mesh);
dockFinishes.Add(material);
}
}
}