Files
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

360 lines
17 KiB
C#

using System;
using UnityEngine;
using UnityEngine.UIElements;
namespace BigShipTycoon
{
public sealed partial class TycoonApp
{
private static readonly Vector2[][] FleetWorldBounds = { new[] { new Vector2(-180, -76), new Vector2(180, 61) } };
private static readonly Color FleetSeaColor = new(.41f, .78f, .93f);
private static readonly Color FleetDockColor = new(.43f, .82f, .70f);
private bool fleetWorldView;
private sealed class FleetRow
{
public Ship Ship;
public Button Control;
public Label Status, Readout;
public Voyage Voyage;
public HelmNavigation Helm;
public Vector2[] Route;
public int Group = -1;
}
private void ToggleFleetOverview()
{
fleetOpen = !fleetOpen;
shipActionsOpen = false;
if (fleetOpen)
voyageMapOpen = false;
Render();
string focus = fleetOpen ? "fleet-close" : "fleet-toggle";
root.schedule.Execute(() => root.Q<Button>(focus)?.Focus());
}
private VisualElement DrawFleetOverview(VisualElement shell)
{
var overlay = Add(shell, "fleet-overview-overlay");
var overview = Add(overlay, "fleet-overview");
overview.name = "fleet-overview";
var header = Add(overview, "fleet-overview-header");
var heading = Add(header, "fleet-overview-heading");
AddDisplay(heading, "Fleet overview", "fleet-overview-title");
Label total = AddText(heading, "", "fleet-overview-total");
total.name = "fleet-overview-total";
Label funds = AddText(heading, "", "fleet-overview-funds");
header.Add(Action("Close Esc", ToggleFleetOverview, "fleet-overview-close", "fleet-close"));
var plot = Add(overview, "fleet-overview-chart");
plot.name = "fleet-chart";
var scope = Add(plot, "fleet-chart-scope");
int portCount = Market.Ports.Count;
int[] docked = new int[portCount];
bool[] calls = new bool[portCount];
var portLabels = new Label[portCount];
for (int i = 0; i < portCount; i++)
{
portLabels[i] = AddText(plot, "", "fleet-chart-port");
portLabels[i].pickingMode = PickingMode.Ignore;
}
var legend = Add(plot, "fleet-chart-legend");
MapLegend(legend, "Selected ship", MapCurrent);
MapLegend(legend, "Underway", FleetSeaColor);
MapLegend(legend, "Docked", FleetDockColor);
var rows = new FleetRow[company.Fleet.Count];
var roster = new ScrollView(ScrollViewMode.Vertical);
roster.AddToClassList("fleet-overview-roster");
overview.Add(roster);
if (rows.Length == 0)
AddText(roster.contentContainer, "No ships in service.", "fleet-empty");
var groups = new VisualElement[portCount + 1];
var groupTitles = new Label[groups.Length];
MakeGroup(portCount);
for (int i = 0; i < portCount; i++)
MakeGroup(i);
for (int i = 0; i < rows.Length; i++)
{
Ship ship = company.Fleet[i];
int index = i;
Button entry = Action("", () => SwitchFleetShip(index), "fleet-roster-entry", "fleet-" + i);
var titleBlock = Add(entry, "fleet-ship-title");
titleBlock.pickingMode = PickingMode.Ignore;
AddText(titleBlock, ship.Name, "fleet-ship-name");
AddText(titleBlock, $"{ship.Hull.Name} · {ship.Decks.Count} decks", "fleet-ship-class");
Label status = AddText(entry, "", "fleet-ship-status");
status.name = "fleet-status-" + i;
Label readout = AddText(entry, "", "fleet-ship-readout");
rows[i] = new FleetRow { Ship = ship, Control = entry, Status = status, Readout = readout };
}
MapProjection projection = default;
Button regional = null, global = null;
regional = Action("Fleet area", () => { fleetWorldView = false; Refresh(); }, "", "fleet-area");
global = Action("World", () => { fleetWorldView = true; Refresh(); }, "", "fleet-world");
scope.Add(regional);
scope.Add(global);
plot.generateVisualContent += context => PaintFleetChart(context, rows, docked, calls, projection);
plot.RegisterCallback<GeometryChangedEvent>(_ => Refresh());
overview.schedule.Execute(Refresh).Every(200);
Refresh();
return overlay;
void MakeGroup(int group)
{
VisualElement element = groups[group] = Add(roster.contentContainer, "fleet-roster-group");
groupTitles[group] = AddText(element, "", "fleet-roster-heading");
}
void Refresh()
{
Array.Clear(docked, 0, docked.Length);
Array.Clear(calls, 0, calls.Length);
int sailing = 0;
foreach (FleetRow row in rows)
{
var (voyage, session, helm, dock, speed) = FleetStatus(row.Ship);
if (row.Voyage != voyage)
{
row.Voyage = voyage;
row.Route = voyage == null ? null : MapPaths(new[] { voyage.Origin, voyage.Destination })[0];
}
row.Helm = helm;
int group = voyage == null ? row.Ship.Port : portCount;
if (row.Group != group)
{
groups[group].Add(row.Control);
row.Group = group;
}
row.Control.EnableInClassList("fleet-roster-selected", row.Ship == Ship);
row.Status.EnableInClassList("fleet-ship-at-sea", voyage != null);
row.Status.EnableInClassList("fleet-ship-shore-alert", row.Ship.ShoreCall?.Phase == ShorePhase.AwaitingDecision);
if (voyage == null)
{
docked[group]++;
SetPassageText(row.Status, $"Docked at {Market.Ports[row.Ship.Port].Name}");
SetPassageText(row.Readout, row.Ship.ShoreCall is ShoreCall shore ? shore.Phase switch
{
ShorePhase.AwaitingDecision => $"{shore.LateGuests:N0} guests late · Departure decision needed",
ShorePhase.Holding => $"Holding for {shore.LateGuests:N0} guests",
_ => $"{shore.Guests:N0} guests on shore excursions"
} : $"{row.Ship.Fuel:N0} / {row.Ship.Tank:N0} t fuel · {row.Ship.Condition}% condition");
}
else
{
sailing++;
calls[voyage.Origin] = calls[voyage.Destination] = true;
SetPassageText(row.Status, dock == null
? $"Underway · {Market.Ports[voyage.Origin].Name} → {Market.Ports[voyage.Destination].Name}"
: $"Pilot required · {Market.Ports[dock.Arriving ? voyage.Destination : voyage.Origin].Name}");
string motion = dock == null ? FleetMotion(voyage, session, helm, speed)
: dock.Arriving ? "Waiting for manual docking" : "Waiting for manual departure";
SetPassageText(row.Readout, $"{voyage.Guests:N0} guests · {motion}");
}
}
for (int i = 0; i < groups.Length; i++)
{
bool atSea = i == portCount;
int count = atSea ? sailing : docked[i];
DisplayStyle visibility = count > 0 ? DisplayStyle.Flex : DisplayStyle.None;
if (groups[i].style.display.value != visibility)
groups[i].style.display = visibility;
string location = atSea ? "At sea" : Market.Ports[i].Name;
SetPassageText(groupTitles[i], $"{location} · {count}");
}
SetPassageText(total, $"{sailing} underway · {rows.Length - sailing} docked");
SetPassageText(funds, company.ReservedCash > 0
? $"{Money(company.AvailableCash)} available · {Money(company.ReservedCash)} reserved for active voyages"
: $"{Money(company.AvailableCash)} available");
regional.EnableInClassList("fleet-scope-current", !fleetWorldView);
global.EnableInClassList("fleet-scope-current", fleetWorldView);
Vector2 size = plot.contentRect.size;
if (size.x > 50 && size.y > 50)
{
projection = FleetProjection(size, rows, fleetWorldView);
PlaceFleetLabels(portLabels, docked, calls, projection);
plot.MarkDirtyRepaint();
}
}
}
private static string FleetMotion(Voyage voyage, OnboardSession session, HelmNavigation helm, int speed)
{
if (helm == null)
return "Preparing to sail";
if (helm.Arrived)
return "Arriving";
if (helm.PropulsionStopped)
return "Engine stopped";
int miles = Mathf.CeilToInt(voyage.Miles * helm.RemainingSeconds / PassageDuration(voyage));
int eta = Mathf.CeilToInt(helm.RemainingSeconds / Mathf.Max(.1f, speed * (session?.SpeedFactor ?? 1)));
return $"{helm.Speed:F1} kn · {miles:N0} nm · {eta / 60}:{eta % 60:00} remaining";
}
private static MapProjection FleetProjection(Vector2 size, FleetRow[] rows, bool world)
{
if (world || rows.Length == 0)
return new MapProjection(size, FleetWorldBounds);
Vector2 origin = FleetPortPoint(rows[0].Ship.Port);
Vector2 lower = origin, upper = origin;
foreach (FleetRow row in rows)
{
if (row.Route == null)
{
Include(FleetPortPoint(row.Ship.Port));
continue;
}
float shift = NearLongitude(row.Route[0].x, origin.x) - row.Route[0].x;
foreach (Vector2 point in row.Route)
Include(new Vector2(point.x + shift, point.y), true);
}
return new MapProjection(size, new[] { new[] { lower, upper } });
void Include(Vector2 point, bool longitudeSet = false)
{
if (!longitudeSet)
point.x = NearLongitude(point.x, origin.x);
lower = Vector2.Min(lower, point);
upper = Vector2.Max(upper, point);
}
}
private static float NearLongitude(float longitude, float center) => center + Mathf.DeltaAngle(center, longitude);
private static Vector2 FleetPortPoint(int port)
{
PortSpec location = Market.Ports[port];
return new Vector2(location.Longitude, -location.Latitude);
}
private static Vector2 FleetPortPixel(int port, MapProjection projection)
{
Vector2 point = FleetPortPoint(port);
point.x = NearLongitude(point.x, projection.Center.x);
return projection.Pixel(point);
}
private static void PlaceFleetLabels(Label[] labels, int[] docked, bool[] calls, MapProjection projection)
{
var areas = new Rect[labels.Length];
int used = 0;
for (int i = 0; i < labels.Length; i++)
{
Label label = labels[i];
Vector2 point = FleetPortPixel(i, projection);
Vector2 size = projection.Dimensions;
bool shown = (docked[i] > 0 || calls[i]) && point.x >= 0 && point.x <= size.x && point.y >= 0 && point.y <= size.y;
label.style.display = shown ? DisplayStyle.Flex : DisplayStyle.None;
if (!shown)
continue;
string name = Market.Ports[i].Name;
SetPassageText(label, docked[i] > 0 ? $"{name} · {docked[i]}" : name);
label.EnableInClassList("fleet-chart-port-docked", docked[i] > 0);
float width = label.text.Length * 6.5f + 12;
var area = new Rect(Mathf.Clamp(point.x + 8, 6, size.x - width - 6), Mathf.Clamp(point.y - 23, 6, size.y - 26), width, 19);
for (int j = 0; j < used; j++)
if (area.Overlaps(areas[j]))
area.y = Mathf.Min(size.y - 26, areas[j].yMax + 2);
areas[used++] = area;
label.style.left = area.x;
label.style.top = area.y;
}
}
private void PaintFleetChart(MeshGenerationContext context, FleetRow[] rows, int[] docked, bool[] calls, MapProjection projection)
{
if (projection.Scale <= 0)
return;
Painter2D pen = context.painter2D;
PaintMapGrid(pen, projection);
PaintMapCoast(pen, projection);
foreach (FleetRow row in rows)
if (row.Route != null)
PaintFleetRoute(pen, projection, row.Route, row.Ship == Ship ? MapCurrent : FleetSeaColor, row.Ship == Ship ? 2.7f : 1.7f);
Vector2 size = projection.Dimensions;
for (int i = 0; i < Market.Ports.Count; i++)
{
Vector2 point = FleetPortPixel(i, projection);
if (point.x < 0 || point.x > size.x || point.y < 0 || point.y > size.y)
continue;
pen.fillColor = docked[i] > 0 ? FleetDockColor : calls[i] ? FleetSeaColor : new Color(.51f, .68f, .65f, .40f);
pen.BeginPath();
pen.Arc(point, docked[i] > 0 ? 5.2f : calls[i] ? 3.6f : 1.9f, 0, 360);
pen.Fill();
}
foreach (FleetRow row in rows)
{
if (row.Route != null)
PaintFleetVessel(pen, projection, row, row.Ship == Ship ? MapCurrent : FleetSeaColor);
else if (row.Ship == Ship)
{
pen.strokeColor = MapCurrent;
pen.lineWidth = 1.5f;
pen.BeginPath();
pen.Arc(FleetPortPixel(row.Ship.Port, projection), 8.7f, 0, 360);
pen.Stroke();
}
}
}
private static void PaintFleetRoute(Painter2D pen, MapProjection map, Vector2[] path, Color color, float lineWidth)
{
float anchor = NearLongitude(path[0].x, map.Center.x) - path[0].x;
float limit = map.Dimensions.x / (2 * map.Scale);
pen.strokeColor = color;
pen.lineWidth = lineWidth;
for (int zone = -1; zone <= 1; zone++)
{
float shift = anchor + 360 * zone;
float a = path[0].x + shift, b = path[^1].x + shift;
if (Mathf.Max(a, b) < map.Center.x - limit || Mathf.Min(a, b) > map.Center.x + limit)
continue;
pen.BeginPath();
pen.MoveTo(map.Pixel(path[0] + Vector2.right * shift));
for (int i = 1; i < path.Length; i++)
pen.LineTo(map.Pixel(path[i] + Vector2.right * shift));
pen.Stroke();
}
}
private static void PaintFleetVessel(Painter2D pen, MapProjection map, FleetRow row, Color color)
{
Vector2 travel = row.Helm == null ? Vector2.zero : row.Helm.Position / PassageDuration(row.Voyage);
float step = Mathf.Clamp01(travel.x) * MapSamples;
int index = Mathf.Min(Mathf.FloorToInt(step), MapSamples - 1);
Vector2 a = row.Route[index], b = row.Route[index + 1];
Vector2 tangent = (b - a).normalized;
Vector2 normal = new(-tangent.y, tangent.x);
float courseSpan = Vector2.Distance(row.Route[0], row.Route[^1]);
Vector2 position = Vector2.Lerp(a, b, step - index) + normal * travel.y * courseSpan;
position.x = NearLongitude(position.x, map.Center.x);
Vector2 pixel = map.Pixel(position);
float angle = (row.Helm?.Heading ?? 0) * Mathf.Deg2Rad;
Vector2 forward = tangent * Mathf.Cos(angle) + normal * Mathf.Sin(angle);
Vector2 wing = new(-forward.y, forward.x);
pen.fillColor = color;
pen.strokeColor = new Color(.05f, .13f, .18f);
pen.lineWidth = 1.1f;
pen.BeginPath();
pen.MoveTo(pixel + forward * 9);
pen.LineTo(pixel - forward * 6 + wing * 4.5f);
pen.LineTo(pixel - forward * 3);
pen.LineTo(pixel - forward * 6 - wing * 4.5f);
pen.ClosePath();
pen.Fill();
pen.Stroke();
}
}
}