219 lines
10 KiB
C#
219 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace BigShipTycoon
|
|
{
|
|
internal sealed class SunDeckGrid
|
|
{
|
|
private const float Cell = 3f, RailClearance = .65f;
|
|
private readonly Ship ship;
|
|
private readonly float length, offset, halfBeam;
|
|
private readonly Vector2 radome;
|
|
private IReadOnlyList<SunDeckPlacement> existing;
|
|
|
|
internal SunDeckGrid(Ship ship, int deckCount = 0)
|
|
{
|
|
this.ship = ship;
|
|
deckCount = deckCount > 0 ? deckCount : ship.Decks.Count;
|
|
int top = deckCount - 1;
|
|
length = VesselExterior.DeckLength(ship.Hull.Length, top);
|
|
offset = VesselExterior.DeckOffset(ship.Hull.Length, top);
|
|
halfBeam = VesselExterior.DeckHalfBeam(ship.Hull.Beam * .5f, top);
|
|
Columns = Mathf.FloorToInt((length - 2 * RailClearance) / Cell);
|
|
Rows = Mathf.FloorToInt((2 * halfBeam - 2 * RailClearance) / Cell);
|
|
Area = new Rect(offset - Columns * Cell * .5f, -Rows * Cell * .5f, Columns * Cell, Rows * Cell);
|
|
|
|
float funnel = offset - length * .24f;
|
|
float funnelHalfWidth = halfBeam * .23f + 1.27f;
|
|
Funnels = new Rect(funnel - 2.4f, -funnelHalfWidth, 4.8f, 2 * funnelHalfWidth);
|
|
radome = new Vector2(funnel + 6.10f, halfBeam * .50f);
|
|
float bridge = VesselExterior.BridgeCenter(ship.Hull.Length, deckCount);
|
|
Bridge = new Rect(bridge - 4.85f, -halfBeam, 9.7f, 2 * halfBeam);
|
|
|
|
SunPoolStyle pool = ship.SunDeckPlan.Pool;
|
|
bool automatic = pool == SunPoolStyle.Automatic && !ship.SunDeckPlan.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 => Cell;
|
|
internal float CellWidth => Cell;
|
|
internal Rect Area { get; }
|
|
internal Rect Bridge { get; }
|
|
internal Rect Funnels { get; }
|
|
internal SunPoolStyle SelectedPool { get; }
|
|
internal IReadOnlyList<SunDeckPlacement> Existing => existing ??= ship.SunDeckPlan.CustomLayout
|
|
? ship.SunDeckPlan.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.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.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),
|
|
_ => 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 / Cell), Mathf.CeilToInt(2 * half.y / Cell));
|
|
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 * Cell, Area.yMin + item.Row * Cell, size.x * Cell, size.y * Cell);
|
|
}
|
|
|
|
internal bool IsBlocked(int column, int row)
|
|
{
|
|
if ((uint)column >= (uint)Columns || (uint)row >= (uint)Rows)
|
|
return true;
|
|
var cell = new Rect(Area.xMin + column * Cell, Area.yMin + row * Cell, Cell, Cell);
|
|
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
|
|
|| 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) * Cell;
|
|
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.Pool || (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 && Normalize(pool) == SunPoolStyle.Resort ? Market.Regional
|
|
: 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);
|
|
}
|
|
}
|
|
}
|
|
}
|