using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UIElements; namespace Soundings { public sealed partial class SoundingsApp { private static readonly (int HullIndex, bool Used)[] Offers = { (0, true), (0, false), (1, true), (1, false), (2, false) }; private enum DeckTools { GuestFacilities, LargerVenues, LeisureAndRetail, ShipSystems, Gaming } private DeckTools deckTools; private int toolRotation; private Action refreshDeckPlacement; private static readonly Facility[] GuestTools = { Facility.Cabin, Facility.Dining, Facility.Entertainment, Facility.Tank, Facility.Bar, Facility.Amenity, Facility.Empty }; private static readonly Facility[] ExpandedTools = { Facility.Suite, Facility.GrandSuite, Facility.Restaurant, Facility.GrandTheater, Facility.ResortPool, Facility.CocktailLounge, Facility.Empty }; private static readonly Facility[] LeisureTools = { Facility.Shop, Facility.ShoppingArcade, Facility.Spa, Facility.WaterPark, Facility.Empty }; private static readonly Facility[] CasinoTools = { Facility.CasinoLounge, Facility.Casino, Facility.GrandCasino, Facility.Empty }; private static readonly Facility[] SystemsTools = { Facility.Galley, Facility.Stores, Facility.Water, Facility.Waste, Facility.Workshop, Facility.Empty }; private static readonly Color DraftGrid = new Color(.37f, .62f, .65f, .19f); private static readonly Color DraftFine = new Color(.47f, .75f, .74f, .42f); private static readonly Color DraftInk = new Color(.64f, .89f, .82f, .88f); private static readonly Color DraftGold = new Color(.91f, .70f, .42f, .88f); private void DrawDecks(VisualElement workspace) { var work = Add(workspace, "ship-work deck-work"); Ship ship = Ship; if (ship == null) { var empty = Add(work, "ship-work-empty"); AddDisplay(empty, "No vessel in dock", "ship-work-heading"); AddText(empty, "Acquire a hull at the shipyard before fitting a deck.", "ship-work-detail"); empty.Add(Action("Visit shipyard", () => ChangePage(2), "work-primary", "open-shipyard")); return; } deckIndex = Mathf.Clamp(deckIndex, 0, ship.Decks.Count - 1); var titleBar = Add(work, "ship-work-head"); var heading = Add(titleBar, "ship-work-name"); AddText(heading, "DECK PLANS", "work-caption"); AddDisplay(heading, ship.Name, "ship-work-heading"); string deckTitle = sunDeckEditing ? "Open-air sun deck" : $"Deck {deckIndex + 1} of {ship.Decks.Count}"; AddText(heading, $"{ship.Hull.Name} hull · {deckTitle}", "ship-work-detail"); var deckControls = Add(titleBar, "work-deck-controls"); var selector = Add(deckControls, "work-deck-selector"); Button sunDeck = Action("Sun deck", () => { sunDeckEditing = true; Notify(null); Render(); }, "work-deck-tab", "sun-deck-open"); sunDeck.EnableInClassList("work-selected", sunDeckEditing); selector.Add(sunDeck); for (int i = ship.Decks.Count - 1; i >= 0; i--) { int floor = i; Button level = Action($"Deck {i + 1}", () => { deckIndex = floor; sunDeckEditing = false; Notify(null); Render(); }, "work-deck-tab", $"deck-{i}"); level.EnableInClassList("work-selected", !sunDeckEditing && deckIndex == i); selector.Add(level); } int deckPrice = company.DeckPrice(ship); string addLabel = ship.Decks.Count == ship.Hull.MaxDecks ? "Deck limit reached" : $"+ Deck {Money(deckPrice)}"; Button add = Action(addLabel, () => { string failure = company.AddDeck(ship); if (failure == null) { deckIndex = ship.Decks.Count - 1; sunDeckEditing = false; } Apply(failure, "New deck ready to fit out."); }, "work-deck-add", "add-deck"); add.SetEnabled(ship.Decks.Count < ship.Hull.MaxDecks && company.AvailableCash >= deckPrice); deckControls.Add(add); if (sunDeckEditing) { refreshDeckPlacement = null; DrawSunDeckEditor(work, ship); return; } var board = Add(work, "work-draft"); board.generateVisualContent += context => DrawDeckOutline(context, ship.Hull.Columns); var markings = Add(board, "work-draft-markings"); markings.pickingMode = PickingMode.Ignore; AddText(markings, "STERN", "work-caption"); AddText(markings, "TOP VIEW", "work-caption"); AddText(markings, "BOW →", "work-caption"); var layout = Add(board, "work-berth-grid"); var placement = Add(board, "work-placement"); var placementLabel = AddText(placement, PlacementInstruction(ship, tool, toolRotation), "work-placement-text"); placementLabel.name = "berth-placement"; refreshDeckPlacement = DrawBerthGrid(layout, placementLabel, ship); var fittings = Add(work, "work-fittings"); var gauges = Add(fittings, "work-capacity-strip"); if (deckTools == DeckTools.ShipSystems) { AddText(gauges, $"{ship.GalleyCapacity:N0} galley covers", "work-capacity"); AddText(gauges, $"{ship.FoodCapacity:N0} stored portions", "work-capacity"); AddText(gauges, $"{ship.WaterCapacity:N0} L freshwater", "work-capacity"); AddText(gauges, $"{ship.WasteCapacity:N0} L sewage buffer", "work-capacity"); } else { string rooms = ship.SuiteBerths == 0 ? $"{ship.Beds:N0} berths" : $"{ship.Beds:N0} berths · {ship.SuiteBerths:N0} in suites"; AddText(gauges, rooms, "work-capacity"); AddText(gauges, $"{ship.Meals:N0} meal covers", "work-capacity"); AddText(gauges, $"{ship.Entertainment:N0} show seats", "work-capacity"); if (deckTools == DeckTools.LargerVenues || deckTools == DeckTools.LeisureAndRetail) { AddText(gauges, $"{ship.PoolCapacity:N0} amenity admissions", "work-capacity"); if (deckTools == DeckTools.LeisureAndRetail) AddText(gauges, $"{ship.RetailCapacity:N0} retail orders", "work-capacity"); else AddText(gauges, $"{ship.BarCapacity:N0} drink orders", "work-capacity"); } else if (deckTools == DeckTools.Gaming) AddText(gauges, $"{ship.CasinoCapacity:N0} gaming seats", "work-capacity"); else AddText(gauges, $"{ship.Tank:N0} t bunker capacity", "work-capacity"); AddText(gauges, $"{ship.Capacity:N0} guests per sailing", "work-capacity work-capacity-emphasis"); } var toolMode = Add(fittings, "work-fit-modes"); AddBuildToolTab(toolMode, "Guest facilities", "facility-guests", DeckTools.GuestFacilities, Facility.Cabin); AddBuildToolTab(toolMode, "Larger venues", "facility-larger", DeckTools.LargerVenues, Facility.Suite); AddBuildToolTab(toolMode, "Leisure & retail", "facility-leisure", DeckTools.LeisureAndRetail, Facility.Shop); AddBuildToolTab(toolMode, "Casinos", "facility-casino", DeckTools.Gaming, Facility.CasinoLounge); AddBuildToolTab(toolMode, "Ship systems", "facility-systems", DeckTools.ShipSystems, Facility.Galley); FacilitySpec selectedSpec = Market.Facilities[(int)tool]; if (selectedSpec.BayCount > 1) { Button rotate = Action("Rotate R", RotateDeckTool, "work-tool-rotate", "facility-rotate"); rotate.tooltip = "Turn the selected fitting clockwise by 90°"; toolMode.Add(rotate); AddText(toolMode, RotationLabel(selectedSpec), "work-tool-orientation").name = "facility-orientation"; } var tools = Add(fittings, "work-tool-tray"); Facility[] options = deckTools switch { DeckTools.LargerVenues => ExpandedTools, DeckTools.LeisureAndRetail => LeisureTools, DeckTools.Gaming => CasinoTools, DeckTools.ShipSystems => SystemsTools, _ => GuestTools }; foreach (Facility option in options) { FacilitySpec spec = Market.Facilities[(int)option]; bool locked = ship.HullIndex < spec.MinimumHull; Button button = Action("", () => { if (tool != option) toolRotation = 0; tool = option; Notify(null); Render(); }, "work-tool work-tool-" + (int)option, "facility-" + (int)option); button.EnableInClassList("work-selected", option == tool); button.EnableInClassList("work-tool-locked", locked); button.tooltip = locked ? VenueRequirement(spec) : spec.Details; AddFacilityMark(button, option); var copy = Add(button, "work-tool-copy"); copy.pickingMode = PickingMode.Ignore; AddText(copy, spec.Name, "work-tool-name"); string price = option == Facility.Empty ? "25% refund" : Money(spec.Price); if (spec.BayCount > 1) { price = $"{FootprintLabel(spec, 0)} · {price}"; button.AddToClassList("work-tool-larger"); } AddText(copy, price, "work-tool-cost"); if (locked) AddText(copy, $"{Market.Hulls[spec.MinimumHull].Name} hull required", "work-tool-lock"); tools.Add(button); } } private Action DrawBerthGrid(VisualElement layout, Label instruction, Ship ship) { Deck deck = ship.Decks[deckIndex]; int columns = ship.Hull.Columns; var berths = new Button[deck.Cells.Length]; var strips = new VisualElement[ship.Hull.Rows]; int hovered = -1, focused = -1; void UpdatePreview() { int selected = hovered >= 0 ? hovered : focused; for (int i = 0; i < berths.Length; i++) { berths[i].RemoveFromClassList("work-berth-preview"); berths[i].RemoveFromClassList("work-berth-blocked"); } if (selected < 0) { instruction.text = PlacementInstruction(ship, tool, toolRotation); return; } Facility room = deck.FacilityAt(selected, columns); int anchor = room == Facility.Empty ? selected : deck.AnchorAt(selected, columns); bool planning = room == Facility.Empty || room == tool; FacilitySpec spec = Market.Facilities[(int)(planning ? tool : room)]; int rotation = planning ? toolRotation : deck.RotationAt(anchor); string blocked = PlacementBlock(ship, selected); instruction.text = BerthDescription(deck, columns, selected, room, tool, toolRotation, blocked); foreach (Vector2Int offset in spec.Footprint(rotation)) if (deck.TryCellAt(anchor, columns, offset, out int bay)) berths[bay].AddToClassList(blocked == null ? "work-berth-preview" : "work-berth-blocked"); } for (int row = 0; row < ship.Hull.Rows; row++) { var strip = Add(layout, "work-berth-row"); strips[row] = strip; for (int column = 0; column < columns; column++) { int cell = row * columns + column; int anchor = deck.AnchorAt(cell, columns); Facility existing = deck.FacilityAt(cell, columns); FacilitySpec room = Market.Facilities[(int)existing]; var slot = Add(strip, "work-berth-slot"); Button berth = Action("", () => BuildAtBerth(ship, deck, cell), "work-berth work-room-" + (int)existing, "berth-" + cell); berths[cell] = berth; slot.Add(berth); berth.RegisterCallback(_ => { hovered = cell; UpdatePreview(); }); berth.RegisterCallback(_ => { hovered = -1; UpdatePreview(); }); berth.RegisterCallback(_ => { focused = cell; UpdatePreview(); }); berth.RegisterCallback(_ => { focused = -1; UpdatePreview(); }); AddText(berth, BerthName(cell, columns), "work-berth-index"); if (existing == Facility.Empty) AddText(berth, "+", "work-berth-open"); else if (room.BayCount == 1) AddVenueLabel(berth, existing); else { berth.EnableInClassList("work-berth-join-left", column > 0 && SameVenue(cell - 1)); berth.EnableInClassList("work-berth-join-right", column + 1 < columns && SameVenue(cell + 1)); berth.EnableInClassList("work-berth-join-top", row > 0 && SameVenue(cell - columns)); berth.EnableInClassList("work-berth-join-bottom", row + 1 < ship.Hull.Rows && SameVenue(cell + columns)); } bool SameVenue(int other) => deck.AnchorAt(other, columns) == anchor; } } for (int anchor = 0; anchor < deck.Cells.Length; anchor++) { Facility room = deck.Cells[anchor]; FacilitySpec spec = Market.Facilities[(int)room]; if (spec.BayCount <= 1) continue; var (row, start, span) = CaptionRow(spec.Footprint(deck.RotationAt(anchor))); var label = Add(strips[anchor / columns + row], "work-span-caption"); label.pickingMode = PickingMode.Ignore; label.style.left = Length.Percent(100f * (anchor % columns + start) / columns); label.style.width = Length.Percent(100f * span / columns); AddVenueLabel(label, room); } Refresh(); return Refresh; void Refresh() { for (int cell = 0; cell < berths.Length; cell++) { Facility room = deck.FacilityAt(cell, columns); berths[cell].tooltip = BerthDescription(deck, columns, cell, room, tool, toolRotation, PlacementBlock(ship, cell)); } UpdatePreview(); } } private static (int Row, int First, int Span) CaptionRow(IReadOnlyList footprint) { (int row, int start, int span) = (0, 0, 1); for (int first = 0; first < footprint.Count;) { int end = first + 1; while (end < footprint.Count && footprint[end].y == footprint[first].y) end++; if (end - first > span) (row, start, span) = (footprint[first].y, footprint[first].x, end - first); first = end; } return (row, start, span); } private string PlacementBlock(Ship ship, int cell) { FacilitySpec spec = Market.Facilities[(int)tool]; return ship.HullIndex < spec.MinimumHull ? VenueRequirement(spec) : company.BuildBlock(ship, deckIndex, cell, tool, toolRotation); } private void BuildAtBerth(Ship ship, Deck deck, int cell) { int columns = ship.Hull.Columns; int anchor = deck.AnchorAt(cell, columns); Facility previous = deck.FacilityAt(cell, columns); int oldRotation = deck.RotationAt(anchor); FacilitySpec room = Market.Facilities[(int)(tool == Facility.Empty ? previous : tool)]; int footprintRotation = tool == Facility.Empty ? oldRotation : toolRotation; string area = BerthNames(deck, columns, previous == Facility.Empty ? cell : anchor, room.Footprint(footprintRotation)); string failure = company.Build(ship, deckIndex, cell, tool, toolRotation); bool turned = previous == tool && previous != Facility.Empty && oldRotation != deck.RotationAt(anchor); if (failure == null && (previous != tool || turned)) SetRecommendedFuel(); string confirmation = tool == Facility.Empty ? $"Removed {room.Name.ToLowerInvariant()} from {area}." : $"{(turned ? "Rotated" : "Built")} {room.Name.ToLowerInvariant()} at {area}."; Apply(failure, previous == tool && !turned ? null : confirmation); } private void AddBuildToolTab(VisualElement parent, string label, string id, DeckTools group, Facility initial) { Button tab = Action(label, () => { if (deckTools == group) return; deckTools = group; tool = initial; toolRotation = 0; Notify(null); Render(); }, deckTools == group ? "work-selected" : "", id); parent.Add(tab); } private static void AddVenueLabel(VisualElement parent, Facility facility) { var center = Add(parent, "work-berth-center"); center.pickingMode = PickingMode.Ignore; AddFacilityMark(center, facility); AddText(center, Market.Facilities[(int)facility].Name, "work-berth-name").pickingMode = PickingMode.Ignore; } private static string VenueRequirement(FacilitySpec spec) { HullSpec hull = Market.Hulls[spec.MinimumHull]; return $"Requires a {hull.Name} hull. Unlock the hull with {hull.Voyages} completed voyages and {hull.Reputation} reputation."; } private string RotationLabel(FacilitySpec spec) => $"{FootprintLabel(spec, toolRotation)} · {toolRotation * 90}°"; private void RotateDeckTool() { FacilitySpec spec = Market.Facilities[(int)tool]; if (spec.BayCount <= 1 || root.Q