236 lines
12 KiB
C#
236 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace BigShipTycoon
|
|
{
|
|
internal sealed class SunDeckGrid
|
|
{
|
|
private const float RailClearance = .65f;
|
|
private readonly Ship ship;
|
|
private readonly SunDeckZone zone;
|
|
private readonly float length, offset, halfBeam;
|
|
private readonly Vector2 radome;
|
|
private readonly bool mega;
|
|
private readonly float cellSize;
|
|
private IReadOnlyList<SunDeckPlacement> existing;
|
|
|
|
internal SunDeckGrid(Ship ship, int deckCount = 0, SunDeckZone zone = SunDeckZone.Sky)
|
|
{
|
|
this.ship = ship;
|
|
this.zone = zone;
|
|
mega = ship.HasAftTerrace;
|
|
cellSize = mega ? 4.5f : 3f;
|
|
deckCount = deckCount > 0 ? deckCount : ship.Decks.Count;
|
|
(length, halfBeam, offset) = MegaDeckGeometry.ForShip(ship, deckCount - 1, deckCount);
|
|
Rect footprint = zone == SunDeckZone.AftTerrace ? MegaDeckGeometry.TerraceArea(ship.Hull.Length, ship.Hull.Beam * .5f, deckCount)
|
|
: new Rect(offset - length * .5f, -halfBeam, length, 2 * halfBeam);
|
|
Columns = Mathf.FloorToInt((footprint.width - 2 * RailClearance) / cellSize);
|
|
Rows = Mathf.FloorToInt((footprint.height - 2 * RailClearance) / cellSize);
|
|
Vector2 gridSize = new Vector2(Columns, Rows) * cellSize;
|
|
Area = new Rect(footprint.center - gridSize * .5f, gridSize);
|
|
|
|
float funnel = offset - length * .24f;
|
|
float funnelHalfWidth = mega ? halfBeam * .58f + .35f : halfBeam * .23f + 1.27f;
|
|
radome = new Vector2(funnel + 6.10f, halfBeam * .50f);
|
|
float bridge = MegaDeckGeometry.BridgeCenter(ship, deckCount);
|
|
if (zone == SunDeckZone.Sky)
|
|
{
|
|
float funnelHalfLength = mega ? 9.2f : 2.4f;
|
|
Funnels = new Rect(funnel - funnelHalfLength, -funnelHalfWidth, 2 * funnelHalfLength, 2 * funnelHalfWidth);
|
|
float bridgeHalfLength = mega ? 10.5f : 4.85f;
|
|
float bridgeHalfWidth = mega ? .66f * halfBeam + 1f : halfBeam;
|
|
Bridge = new Rect(bridge - bridgeHalfLength, -bridgeHalfWidth, 2 * bridgeHalfLength, 2 * bridgeHalfWidth);
|
|
}
|
|
|
|
SunDeckPlan plan = ship.SunDeckFor(zone);
|
|
SunPoolStyle pool = plan.Pool;
|
|
bool automatic = pool == SunPoolStyle.Automatic && !plan.CustomLayout;
|
|
Facility[] cells = ship.Decks[^1].Cells;
|
|
bool resort = automatic && (Array.IndexOf(cells, Facility.ResortPool) >= 0 || Array.IndexOf(cells, Facility.WaterPark) >= 0);
|
|
SelectedPool = pool is SunPoolStyle.Automatic or SunPoolStyle.None
|
|
? resort ? SunPoolStyle.Resort : SunPoolStyle.Compact : pool;
|
|
}
|
|
|
|
internal int Columns { get; }
|
|
internal int Rows { get; }
|
|
internal float CellLength => cellSize;
|
|
internal float CellWidth => cellSize;
|
|
internal Rect Area { get; }
|
|
internal Rect Bridge { get; }
|
|
internal Rect Funnels { get; }
|
|
internal SunPoolStyle SelectedPool { get; }
|
|
internal IReadOnlyList<SunDeckPlacement> Existing => existing ??= ship.SunDeckFor(zone).CustomLayout
|
|
? ship.SunDeckFor(zone).Placements ?? Array.Empty<SunDeckPlacement>() : LegacyFittings();
|
|
|
|
internal int PoolIndex
|
|
{
|
|
get
|
|
{
|
|
for (int i = 0; i < Existing.Count; i++)
|
|
if (Existing[i].Kind == SunAttraction.Pool)
|
|
return i;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
internal Vector2 PoolBasinHalfSize(SunPoolStyle pool) => Normalize(pool) switch
|
|
{
|
|
SunPoolStyle.Lagoon => new Vector2(11.8f, 4.7f),
|
|
SunPoolStyle.Resort => new Vector2(Mathf.Min(7.15f, length * .089f), Mathf.Min(2.77f, halfBeam * .39f)),
|
|
SunPoolStyle.Family => new Vector2(Mathf.Min(4.48f, length * .085f), Mathf.Min(2.18f, halfBeam * .38f)),
|
|
_ => new Vector2(VesselExterior.PoolHalfLength, VesselExterior.PoolHalfWidth)
|
|
};
|
|
|
|
internal Vector2 HalfSize(SunAttraction kind, SunPoolStyle pool) => kind switch
|
|
{
|
|
SunAttraction.Pool => Normalize(pool) switch
|
|
{
|
|
SunPoolStyle.Lagoon => new Vector2(14.2f, 6.95f),
|
|
SunPoolStyle.Resort => new Vector2(9.2f, 4.65f),
|
|
SunPoolStyle.Family => new Vector2(6.5f, 4.1f),
|
|
_ => new Vector2(4.65f, 3.45f)
|
|
},
|
|
SunAttraction.MiniGolf => ship.HullIndex < Market.Regional ? new Vector2(4.45f, 1.30f) : new Vector2(4.5f, 2.1f),
|
|
SunAttraction.Theater => new Vector2(4.3f, 2.7f),
|
|
SunAttraction.GoKarts => new Vector2(6.3f, 2.6f),
|
|
SunAttraction.RollerCoaster => new Vector2(7.4f, 3.5f),
|
|
SunAttraction.SkyGarden => new Vector2(8.7f, 5.25f),
|
|
_ => Vector2.zero
|
|
};
|
|
|
|
private SunPoolStyle Normalize(SunPoolStyle pool) => pool is SunPoolStyle.None or SunPoolStyle.Automatic ? SelectedPool : pool;
|
|
|
|
private Vector2Int Bays(SunDeckPlacement item, SunPoolStyle pool)
|
|
{
|
|
Vector2 half = HalfSize(item.Kind, pool);
|
|
Vector2Int size = new(Mathf.CeilToInt(2 * half.x / cellSize), Mathf.CeilToInt(2 * half.y / cellSize));
|
|
return (item.Rotation & 1) == 0 ? size : new Vector2Int(size.y, size.x);
|
|
}
|
|
|
|
internal Rect Bounds(SunDeckPlacement item, SunPoolStyle pool)
|
|
{
|
|
Vector2Int size = Bays(item, pool);
|
|
return new Rect(Area.xMin + item.Column * cellSize, Area.yMin + item.Row * cellSize, size.x * cellSize, size.y * cellSize);
|
|
}
|
|
|
|
internal bool IsBlocked(int column, int row)
|
|
{
|
|
if ((uint)column >= (uint)Columns || (uint)row >= (uint)Rows)
|
|
return true;
|
|
if (zone == SunDeckZone.AftTerrace)
|
|
return false;
|
|
var cell = new Rect(Area.xMin + column * cellSize, Area.yMin + row * cellSize, cellSize, cellSize);
|
|
if (cell.Overlaps(Bridge) || cell.Overlaps(Funnels))
|
|
return true;
|
|
float aft = (cell.xMin - offset) / (length * .994f) + .5f;
|
|
float fore = (cell.xMax - offset) / (length * .994f) + .5f;
|
|
float width = halfBeam * .992f * Mathf.Min(VesselExterior.SideWidth(aft), VesselExterior.SideWidth(fore));
|
|
return Mathf.Max(Mathf.Abs(cell.yMin), Mathf.Abs(cell.yMax)) > width - RailClearance
|
|
|| !mega && (cell.Overlaps(new Rect(radome.x - .7f, radome.y - .7f, 1.4f, 1.4f))
|
|
|| cell.Overlaps(new Rect(radome.x - .7f, -radome.y - .7f, 1.4f, 1.4f)));
|
|
}
|
|
|
|
internal int At(int col, int row, SunPoolStyle pool)
|
|
{
|
|
Vector2 center = Area.min + new Vector2(col + .5f, row + .5f) * cellSize;
|
|
for (int i = 0; i < Existing.Count; i++)
|
|
if (Bounds(Existing[i], pool).Contains(center))
|
|
return i;
|
|
return -1;
|
|
}
|
|
|
|
internal string PlacementBlock(SunDeckPlacement item, SunPoolStyle pool, IReadOnlyList<SunDeckPlacement> items, int replacing = -1)
|
|
{
|
|
if ((uint)item.Kind < (uint)SunAttraction.MiniGolf || (uint)item.Kind > (uint)SunAttraction.SkyGarden || (uint)item.Rotation > 3)
|
|
return "Select a fitting and turn it in quarter turns.";
|
|
if (replacing < -1 || replacing >= items.Count || replacing >= 0 && items[replacing].Kind != item.Kind)
|
|
return "Select a fitting on this sun deck.";
|
|
int hull = item.Kind == SunAttraction.Pool ? SunDeckPlan.RequiredHull(Normalize(pool)) : SunDeckPlan.RequiredHull(item.Kind);
|
|
if (ship.HullIndex < hull)
|
|
return $"Requires a {Market.Hulls[hull].Name} hull or larger.";
|
|
|
|
Vector2Int size = Bays(item, pool);
|
|
if (item.Column < 0 || item.Row < 0 || item.Column > Columns - size.x || item.Row > Rows - size.y)
|
|
return "The footprint extends beyond the sun deck.";
|
|
for (int row = item.Row; row < item.Row + size.y; row++)
|
|
for (int col = item.Column; col < item.Column + size.x; col++)
|
|
if (IsBlocked(col, row))
|
|
return "Keep the rail, funnels, and bridge clear.";
|
|
|
|
Rect footprint = Bounds(item, pool);
|
|
for (int i = 0; i < items.Count; i++)
|
|
{
|
|
if (i == replacing)
|
|
continue;
|
|
var other = items[i];
|
|
if (item.Kind == SunAttraction.Pool && other.Kind == item.Kind)
|
|
return "Move or remove the existing pool first.";
|
|
if (ship.HullIndex < Market.Regional && item.Kind != SunAttraction.Pool && other.Kind != SunAttraction.Pool)
|
|
return "Fit multiple attractions on a Regional or Ocean Class hull.";
|
|
if (footprint.Overlaps(Bounds(other, pool)))
|
|
return "Clear the entire footprint or choose another spot.";
|
|
}
|
|
return null;
|
|
}
|
|
|
|
internal bool Valid(SunDeckPlan plan)
|
|
{
|
|
if (plan.Placements is null || plan.Placements.Length == 0)
|
|
return true;
|
|
if (plan.Placements.Length > Columns * Rows || plan.Pool is SunPoolStyle.Automatic or SunPoolStyle.None)
|
|
return false;
|
|
for (int i = 0; i < plan.Placements.Length; i++)
|
|
if (PlacementBlock(plan.Placements[i], plan.Pool, plan.Placements, i) != null)
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
internal SunDeckPlan Customize(SunDeckPlan next, int append = 0, int omit = -1)
|
|
{
|
|
var fittings = new SunDeckPlacement[Existing.Count + append - (omit >= 0 ? 1 : 0)];
|
|
for (int i = 0, slot = 0; i < Existing.Count; i++)
|
|
if (i != omit)
|
|
fittings[slot++] = Existing[i];
|
|
next.Pool = Normalize(next.Pool);
|
|
next.Attraction = SunAttraction.None;
|
|
next.CustomLayout = true;
|
|
next.Placements = fittings;
|
|
return next;
|
|
}
|
|
|
|
private IReadOnlyList<SunDeckPlacement> LegacyFittings()
|
|
{
|
|
var result = new List<SunDeckPlacement>(2);
|
|
SunDeckPlan prior = ship.SunDeckPlan;
|
|
VesselExterior.SunDeckLayout layout = VesselExterior.SunDeck(ship);
|
|
foreach (VesselExterior.SunDeckVenue venue in layout.Venues)
|
|
if (venue.Kind != SunAttraction.Pool)
|
|
Find(venue.Kind, venue.Center.x, prior.Attraction == SunAttraction.Automatic);
|
|
if (layout.HasPool)
|
|
Find(SunAttraction.Pool, layout.Pool.Center.x, prior.Pool == SunPoolStyle.Automatic);
|
|
return result;
|
|
|
|
void Find(SunAttraction kind, float desired, bool included)
|
|
{
|
|
float bestDistance = float.MaxValue;
|
|
SunDeckPlacement best = default;
|
|
for (int col = 0; col < Columns; col++)
|
|
for (int row = 0; row < Rows; row++)
|
|
{
|
|
var item = new SunDeckPlacement(kind, col, row) { Included = included };
|
|
if (PlacementBlock(item, SelectedPool, result) != null)
|
|
continue;
|
|
float distance = (Bounds(item, SelectedPool).center - new Vector2(desired, 0)).sqrMagnitude;
|
|
if (distance >= bestDistance)
|
|
continue;
|
|
bestDistance = distance;
|
|
best = item;
|
|
}
|
|
if (bestDistance < float.MaxValue)
|
|
result.Add(best);
|
|
}
|
|
}
|
|
}
|
|
}
|