440 lines
19 KiB
C#
440 lines
19 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace Soundings
|
|
{
|
|
public sealed partial class SoundingsApp
|
|
{
|
|
private int inspectedPort = 1;
|
|
private int selectedVoyage;
|
|
private bool worldChart;
|
|
private float chartScrollPosition;
|
|
|
|
private static readonly Vector2[][] AtlanticCoastlines =
|
|
{
|
|
new[]
|
|
{
|
|
new Vector2(0, 0), new Vector2(.33f, 0), new Vector2(.30f, .09f), new Vector2(.28f, .19f),
|
|
new Vector2(.25f, .21f), new Vector2(.26f, .26f), new Vector2(.22f, .31f), new Vector2(.21f, .37f),
|
|
new Vector2(.19f, .40f), new Vector2(.18f, .47f), new Vector2(.15f, .51f), new Vector2(.14f, .57f),
|
|
new Vector2(.15f, .64f), new Vector2(.13f, .68f), new Vector2(.12f, .64f), new Vector2(.11f, .56f),
|
|
new Vector2(.08f, .54f), new Vector2(.06f, .51f), new Vector2(.03f, .55f), new Vector2(0, .51f)
|
|
},
|
|
new[]
|
|
{
|
|
new Vector2(.75f, 0), new Vector2(.96f, 0), new Vector2(1, .08f), new Vector2(1, .41f),
|
|
new Vector2(.97f, .38f), new Vector2(.93f, .34f), new Vector2(.91f, .30f), new Vector2(.88f, .29f),
|
|
new Vector2(.84f, .25f), new Vector2(.85f, .18f), new Vector2(.88f, .16f), new Vector2(.86f, .11f),
|
|
new Vector2(.81f, .09f)
|
|
},
|
|
new[]
|
|
{
|
|
new Vector2(1, .44f), new Vector2(.94f, .37f), new Vector2(.90f, .42f), new Vector2(.89f, .47f),
|
|
new Vector2(.86f, .54f), new Vector2(.85f, .65f), new Vector2(.87f, .71f), new Vector2(.91f, .74f),
|
|
new Vector2(.94f, .79f), new Vector2(1, .82f)
|
|
},
|
|
new[]
|
|
{
|
|
new Vector2(0, .79f), new Vector2(.026f, .83f), new Vector2(.05f, .90f), new Vector2(.062f, .91f),
|
|
new Vector2(.078f, .95f), new Vector2(.12f, .96f), new Vector2(.16f, 1), new Vector2(0, 1)
|
|
},
|
|
new[]
|
|
{
|
|
new Vector2(.11f, .78f), new Vector2(.15f, .75f), new Vector2(.23f, .76f), new Vector2(.27f, .78f),
|
|
new Vector2(.23f, .79f), new Vector2(.17f, .781f), new Vector2(.13f, .80f)
|
|
},
|
|
new[]
|
|
{
|
|
new Vector2(.33f, .84f), new Vector2(.36f, .82f), new Vector2(.39f, .83f), new Vector2(.39f, .86f),
|
|
new Vector2(.36f, .86f), new Vector2(.34f, .85f)
|
|
}
|
|
};
|
|
|
|
private void OpenVoyageChart()
|
|
{
|
|
itineraryEditorShip = null;
|
|
int planned = Ship?.Itinerary?.NextPort ?? -1;
|
|
int port = planned >= 0 ? planned : destination >= 0 ? destination : Ship?.Port ?? 0;
|
|
if (planned >= 0 && destination != planned)
|
|
{
|
|
destination = planned;
|
|
SetRecommendedFuel();
|
|
}
|
|
worldChart = port >= Market.StarterPortCount || (Ship?.Port ?? 0) >= Market.StarterPortCount;
|
|
inspectedPort = port;
|
|
chartScrollPosition = 0;
|
|
ChangePage(3);
|
|
}
|
|
|
|
private void DrawPorts(VisualElement pageRoot)
|
|
{
|
|
var board = Add(pageRoot, "navigation-chart");
|
|
board.styleSheets.Add(Resources.Load<StyleSheet>("Chart"));
|
|
var heading = Add(board, "chart-heading");
|
|
AddText(heading, Ship == null ? "PORT DIRECTORY" : $"ABOARD {Ship.Name.ToUpperInvariant()}", "chart-kicker");
|
|
AddDisplay(heading, "Shipping chart", "chart-heading-title");
|
|
|
|
var plot = Add(board, "chart-plot");
|
|
plot.generateVisualContent += DrawChart;
|
|
DrawChartLabels(plot);
|
|
|
|
var switches = Add(board, "chart-scopes");
|
|
switches.Add(Action("Atlantic", () => ChangeChart(false), worldChart ? "chart-scope" : "chart-scope chart-scope-current", "chart-atlantic"));
|
|
switches.Add(Action("World", () => ChangeChart(true), worldChart ? "chart-scope chart-scope-current" : "chart-scope", "chart-world"));
|
|
if (Ship != null)
|
|
DrawItineraryButton(switches, Ship);
|
|
|
|
for (int i = 0; i < ChartPortCount; i++)
|
|
{
|
|
int portId = i;
|
|
PortSpec port = Market.Ports[i];
|
|
bool berth = Ship?.Port == i;
|
|
bool cleared = company.Permits[i];
|
|
bool selected = inspectedPort == i;
|
|
string styles = "chart-port-pin";
|
|
if (berth)
|
|
styles += " chart-port-berth";
|
|
else if (!cleared)
|
|
styles += " chart-port-locked";
|
|
if (selected)
|
|
styles += " chart-port-selected";
|
|
if (Ship?.Itinerary is CruiseItinerary itinerary)
|
|
{
|
|
if (IsPendingCall(itinerary, i))
|
|
styles += " chart-port-planned";
|
|
if (i == itinerary.NextPort)
|
|
styles += " chart-port-next";
|
|
}
|
|
|
|
var marker = Action("", () => InspectChartPort(portId), styles, "port-" + i);
|
|
marker.tooltip = $"{port.Name} · {(berth ? "Current port" : cleared ? "Permit held" : "Permit required")}";
|
|
Vector2 point = ChartPoint(port);
|
|
marker.style.left = Length.Percent(point.x * 100);
|
|
marker.style.top = Length.Percent(point.y * 100);
|
|
var light = Add(marker, "chart-pin-light");
|
|
light.pickingMode = PickingMode.Ignore;
|
|
var caption = AddText(marker, port.Name, point.x > .77f ? "chart-pin-label chart-pin-label-west" : "chart-pin-label");
|
|
caption.EnableInClassList("chart-pin-label-on-focus", worldChart && !selected && !berth && Ship?.Itinerary?.NextPort != i);
|
|
plot.Add(marker);
|
|
}
|
|
|
|
var legend = Add(board, "chart-legends");
|
|
ChartKey(legend, "Current berth", "chart-key-berth");
|
|
ChartKey(legend, "Open port", "chart-key-open");
|
|
ChartKey(legend, "Permit required", "chart-key-locked");
|
|
|
|
var folio = new ScrollView(ScrollViewMode.Vertical) { horizontalScrollerVisibility = ScrollerVisibility.Hidden };
|
|
folio.AddToClassList("chart-folio");
|
|
float scrollPosition = chartScrollPosition;
|
|
folio.verticalScroller.valueChanged += value => chartScrollPosition = value;
|
|
if (scrollPosition > 0)
|
|
folio.schedule.Execute(() => folio.scrollOffset = new Vector2(0, scrollPosition));
|
|
board.Add(folio);
|
|
var contents = Add(folio, "chart-folio-content");
|
|
AddText(contents, "LOOK UP PORT", "chart-paper-kicker");
|
|
AddChartDirectory(contents);
|
|
if (EditingItinerary)
|
|
DrawItineraryFolio(contents, Ship);
|
|
else
|
|
DrawPortFolio(board, contents);
|
|
}
|
|
|
|
private static void ChartKey(VisualElement legend, string name, string color)
|
|
{
|
|
var key = Add(legend, "chart-key");
|
|
Add(key, "chart-key-light " + color);
|
|
AddText(key, name, "chart-key-label");
|
|
}
|
|
|
|
private void AddChartDirectory(VisualElement parent)
|
|
{
|
|
var choices = new List<string>(ChartPortCount);
|
|
for (int i = 0; i < ChartPortCount; i++)
|
|
choices.Add(Market.Ports[i].Name);
|
|
|
|
var selector = new DropdownField(choices, inspectedPort) { name = "chart-directory" };
|
|
selector.AddToClassList("chart-directory");
|
|
selector.RegisterValueChangedCallback(evt =>
|
|
{
|
|
int port = choices.IndexOf(evt.newValue);
|
|
if (port >= 0)
|
|
InspectChartPort(port);
|
|
});
|
|
parent.Add(selector);
|
|
}
|
|
|
|
private void DrawPortFolio(VisualElement board, VisualElement parent)
|
|
{
|
|
Ship ship = Ship;
|
|
int selected = inspectedPort;
|
|
PortSpec port = Market.Ports[selected];
|
|
bool berth = ship?.Port == selected;
|
|
Voyage quote = ship == null ? null : company.Forecast(ship, selected);
|
|
bool permitted = company.Permits[selected];
|
|
bool eligible = ship == null || ship.HullIndex >= port.MinimumHull;
|
|
int nextCall = ship?.Itinerary?.NextPort ?? -1;
|
|
|
|
AddDisplay(parent, port.Name, "chart-port-name");
|
|
AddText(parent, $"{port.Country} · {port.Region}", "chart-paper-caption");
|
|
|
|
if (nextCall >= 0)
|
|
DrawNextCall(parent, ship.Itinerary, selected);
|
|
|
|
if (quote != null && permitted && eligible && (nextCall < 0 || selected == nextCall))
|
|
{
|
|
DrawVoyageFolio(board, parent, ship, quote);
|
|
return;
|
|
}
|
|
|
|
AddText(parent, berth ? "CURRENT BERTH" : "PORT CONDITIONS", "chart-paper-kicker chart-paper-section");
|
|
ChartLine(parent, "Fuel at this port", $"{Money(Market.FuelPrice(selected, company.Day))} / t");
|
|
ChartLine(parent, "Arrival dues", Money(port.Fee));
|
|
ChartLine(parent, "Destination appeal", $"{port.Appeal:P0}");
|
|
ChartLine(parent, "Minimum hull", Market.Hulls[port.MinimumHull].Name);
|
|
if (quote != null)
|
|
ChartLine(parent, $"From {Market.Ports[ship.Port].Name}", $"{quote.Miles:N0} nm");
|
|
if (berth)
|
|
{
|
|
ChartLine(parent, "Provisions at this berth", $"{FoodUnitPrice(selected)} / portion");
|
|
DrawChartBunkering(parent, ship);
|
|
return;
|
|
}
|
|
|
|
if (!permitted)
|
|
{
|
|
string blocker = company.PermitBlock(selected);
|
|
Button permit = Action($"Acquire permit · {Money(port.Permit)}", () =>
|
|
{
|
|
string failure = company.Permit(selected);
|
|
Apply(failure, $"{port.Name} is now licensed.");
|
|
}, "chart-ink-button chart-wide-button", "permit");
|
|
permit.SetEnabled(blocker == null);
|
|
parent.Add(permit);
|
|
AddText(parent, blocker ?? $"Open {port.Name} to your fleet.", "chart-paper-note");
|
|
}
|
|
else if (!eligible)
|
|
{
|
|
AddText(parent, $"This sailing requires a {Market.Hulls[port.MinimumHull].Name} hull.", "chart-paper-note");
|
|
}
|
|
else if (ship == null)
|
|
{
|
|
AddText(parent, "Assign a vessel to plan a sailing.", "chart-paper-note");
|
|
parent.Add(Action("Visit shipyard", () => ChangePage(2), "chart-ink-button chart-wide-button"));
|
|
}
|
|
else if (nextCall < 0)
|
|
{
|
|
AddText(parent, $"There is no direct route from {Market.Ports[ship.Port].Name}. Select a linked port to plan this sailing.", "chart-paper-note");
|
|
}
|
|
}
|
|
|
|
private int ChartPortCount => worldChart ? Market.Ports.Count : Market.StarterPortCount;
|
|
|
|
private void InspectChartPort(int port)
|
|
{
|
|
if (port >= Market.StarterPortCount)
|
|
worldChart = true;
|
|
if (port != inspectedPort)
|
|
chartScrollPosition = 0;
|
|
inspectedPort = port;
|
|
if (Ship != null && !EditingItinerary && Ship.Itinerary?.HasNext != true
|
|
&& Market.TryDistance(Ship.Port, port, out _) && destination != port)
|
|
{
|
|
destination = port;
|
|
SetRecommendedFuel();
|
|
}
|
|
Notify(null);
|
|
Render();
|
|
}
|
|
|
|
private void ChangeChart(bool global)
|
|
{
|
|
worldChart = global;
|
|
if (inspectedPort >= ChartPortCount)
|
|
{
|
|
InspectChartPort(Market.Lisbon);
|
|
return;
|
|
}
|
|
Notify(null);
|
|
Render();
|
|
}
|
|
|
|
private Vector2 ChartPoint(PortSpec port)
|
|
{
|
|
Vector2 point = worldChart
|
|
? new Vector2((port.Longitude + 180) / 360, (76 - port.Latitude) / 137)
|
|
: new Vector2(port.MapX, port.MapY);
|
|
return PlotInset(point);
|
|
}
|
|
|
|
private static Vector2 PlotInset(Vector2 point) => new Vector2(.035f + point.x * .93f, .035f + point.y * .93f);
|
|
|
|
private void DrawChartLabels(VisualElement plot)
|
|
{
|
|
if (worldChart)
|
|
{
|
|
OceanName("NORTH PACIFIC", .15f, .62f);
|
|
OceanName("ATLANTIC", .37f, .56f);
|
|
OceanName("INDIAN OCEAN", .62f, .72f);
|
|
OceanName("SOUTH PACIFIC", .11f, .88f);
|
|
}
|
|
else
|
|
{
|
|
OceanName("NORTH ATLANTIC", .48f, .29f);
|
|
OceanName("CARIBBEAN SEA", .25f, .95f);
|
|
}
|
|
|
|
void OceanName(string name, float x, float y)
|
|
{
|
|
Label label = AddText(plot, name, "chart-water-name");
|
|
label.style.left = Length.Percent(x * 100);
|
|
label.style.top = Length.Percent(y * 100);
|
|
}
|
|
}
|
|
|
|
private void DrawChart(MeshGenerationContext context)
|
|
{
|
|
float width = context.visualElement.contentRect.width;
|
|
float height = context.visualElement.contentRect.height;
|
|
if (width < 32 || height < 32)
|
|
return;
|
|
Vector2 Point(Vector2 point) => new Vector2(width * point.x, height * point.y);
|
|
Painter2D painter = context.painter2D;
|
|
|
|
painter.strokeColor = new Color(.23f, .39f, .44f, .44f);
|
|
painter.lineWidth = 1;
|
|
painter.BeginPath();
|
|
for (int i = 0; i <= 12; i++)
|
|
{
|
|
painter.MoveTo(Point(PlotInset(new Vector2(i / 12f, 0))));
|
|
painter.LineTo(Point(PlotInset(new Vector2(i / 12f, 1))));
|
|
}
|
|
for (int i = 0; i <= 8; i++)
|
|
{
|
|
painter.MoveTo(Point(PlotInset(new Vector2(0, i / 8f))));
|
|
painter.LineTo(Point(PlotInset(new Vector2(1, i / 8f))));
|
|
}
|
|
painter.Stroke();
|
|
|
|
painter.fillColor = new Color(.139f, .255f, .276f);
|
|
painter.strokeColor = new Color(.49f, .55f, .49f, .72f);
|
|
painter.lineWidth = 1.15f;
|
|
foreach (Vector2[] outline in worldChart ? WorldCoastlines.Shapes : AtlanticCoastlines)
|
|
{
|
|
painter.BeginPath();
|
|
painter.MoveTo(Point(PlotInset(outline[0])));
|
|
for (int i = 1; i < outline.Length; i++)
|
|
painter.LineTo(Point(PlotInset(outline[i])));
|
|
painter.ClosePath();
|
|
painter.Fill();
|
|
painter.Stroke();
|
|
}
|
|
|
|
Ship ship = Ship;
|
|
int berth = ship?.Port ?? -1;
|
|
int selectionOrigin = EditingItinerary && ship != null ? ItineraryTail(ship) : berth;
|
|
bool CanShow(Lane lane) => lane.A < ChartPortCount && lane.B < ChartPortCount;
|
|
bool IsSelected(Lane lane) => selectionOrigin >= 0 && lane.Other(selectionOrigin) == inspectedPort;
|
|
|
|
foreach (Lane lane in Market.Lanes)
|
|
{
|
|
if (!CanShow(lane) || IsSelected(lane))
|
|
continue;
|
|
bool active = company.Permits[lane.A] && company.Permits[lane.B];
|
|
DrawLane(lane, active ? new Color(.40f, .73f, .71f, .68f) : new Color(.60f, .52f, .39f, .62f), active ? 1.55f : 1.1f);
|
|
}
|
|
foreach (Lane lane in Market.Lanes)
|
|
{
|
|
if (!CanShow(lane) || !IsSelected(lane))
|
|
continue;
|
|
DrawLane(lane, new Color(.92f, .65f, .28f, .18f), 9);
|
|
DrawLane(lane, new Color(.98f, .75f, .38f, 1), 2.3f);
|
|
}
|
|
|
|
if (ship?.Itinerary is CruiseItinerary itinerary)
|
|
{
|
|
int from = itinerary.Origin;
|
|
for (int i = 0; i < itinerary.TotalStops; i++)
|
|
{
|
|
int to = itinerary.Stops[i];
|
|
var route = new Lane(from, to, 0);
|
|
if (i != itinerary.CompletedStops && CanShow(route))
|
|
{
|
|
Color color = i < itinerary.CompletedStops ? new Color(.48f, .64f, .61f, .74f) : new Color(.48f, .88f, .78f);
|
|
DrawLane(route, color, 2.4f);
|
|
}
|
|
from = to;
|
|
}
|
|
|
|
var nextRoute = new Lane(berth, itinerary.NextPort, 0);
|
|
if (itinerary.HasNext && CanShow(nextRoute))
|
|
{
|
|
DrawLane(nextRoute, new Color(.94f, .74f, .40f, .23f), 8);
|
|
DrawLane(nextRoute, new Color(1f, .85f, .55f), 3f);
|
|
}
|
|
}
|
|
|
|
DrawCompass(painter, new Vector2(width - 69, height - 76), 34);
|
|
|
|
void DrawLane(Lane lane, Color color, float thickness)
|
|
{
|
|
Vector2 a = ChartPoint(Market.Ports[lane.A]);
|
|
Vector2 b = ChartPoint(Market.Ports[lane.B]);
|
|
painter.strokeColor = color;
|
|
painter.lineWidth = thickness;
|
|
if (worldChart && Mathf.Abs(a.x - b.x) > .465f)
|
|
{
|
|
if (a.x > b.x)
|
|
(a, b) = (b, a);
|
|
float toEdge = a.x - .035f;
|
|
float y = Mathf.Lerp(a.y, b.y, toEdge / (toEdge + .965f - b.x));
|
|
Curve(a, new Vector2(.035f, y));
|
|
Curve(new Vector2(.965f, y), b);
|
|
}
|
|
else
|
|
Curve(a, b);
|
|
|
|
void Curve(Vector2 from, Vector2 to)
|
|
{
|
|
painter.BeginPath();
|
|
painter.MoveTo(Point(from));
|
|
float rise = (worldChart ? .10f : .145f) * Vector2.Distance(from, to);
|
|
painter.QuadraticCurveTo(Point((from + to) / 2 - new Vector2(0, rise)), Point(to));
|
|
painter.Stroke();
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void DrawCompass(Painter2D painter, Vector2 center, float radius)
|
|
{
|
|
painter.lineWidth = 1;
|
|
painter.strokeColor = new Color(.64f, .58f, .44f, .68f);
|
|
painter.BeginPath();
|
|
painter.MoveTo(center + Vector2.right * radius);
|
|
const int segments = 48;
|
|
for (int i = 1; i <= segments; i++)
|
|
{
|
|
float angle = i * Mathf.PI * 2 / segments;
|
|
painter.LineTo(center + new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * radius);
|
|
}
|
|
painter.ClosePath();
|
|
painter.Stroke();
|
|
|
|
painter.fillColor = new Color(.80f, .68f, .43f, .72f);
|
|
for (int i = 0; i < 4; i++)
|
|
{
|
|
float angle = i * Mathf.PI * .5f;
|
|
Vector2 axis = new Vector2(Mathf.Sin(angle), -Mathf.Cos(angle));
|
|
Vector2 wing = new Vector2(-axis.y, axis.x);
|
|
painter.BeginPath();
|
|
painter.MoveTo(center);
|
|
painter.LineTo(center + axis * radius * (i == 0 ? 1.28f : .95f));
|
|
painter.LineTo(center + wing * radius * .16f);
|
|
painter.ClosePath();
|
|
painter.Fill();
|
|
painter.Stroke();
|
|
}
|
|
}
|
|
}
|
|
}
|