139 lines
5.5 KiB
C#
139 lines
5.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace BigShipTycoon
|
|
{
|
|
[Serializable]
|
|
public sealed class CruiseItinerary
|
|
{
|
|
public int Origin, StartDay, CompletedStops;
|
|
public List<int> Stops = new List<int>();
|
|
|
|
public int TotalStops => Stops?.Count ?? 0;
|
|
public bool HasNext => CompletedStops >= 0 && CompletedStops < TotalStops;
|
|
public int NextPort => HasNext ? Stops[CompletedStops] : -1;
|
|
public int LastPort => TotalStops > 0 ? Stops[TotalStops - 1] : Origin;
|
|
}
|
|
|
|
public sealed partial class Company
|
|
{
|
|
public string ItineraryStopBlock(Ship ship, int port)
|
|
{
|
|
if (ship == null || !Fleet.Contains(ship))
|
|
return "Choose a ship in this fleet.";
|
|
int origin = ship.Itinerary is { HasNext: true } itinerary ? itinerary.LastPort : ship.Port;
|
|
return ItineraryLaneBlock(ship, origin, port);
|
|
}
|
|
|
|
public string AppendItineraryStop(Ship ship, int port)
|
|
{
|
|
string block = ItineraryStopBlock(ship, port);
|
|
if (block != null)
|
|
return block;
|
|
if (ship.Itinerary == null || !ship.Itinerary.HasNext)
|
|
ship.Itinerary = new CruiseItinerary { Origin = ship.Port };
|
|
ship.Itinerary.Stops.Add(port);
|
|
return null;
|
|
}
|
|
|
|
public string ItineraryRemovalBlock(Ship ship, int index)
|
|
{
|
|
if (ship == null || !Fleet.Contains(ship))
|
|
return "Choose a ship in this fleet.";
|
|
CruiseItinerary itinerary = ship.Itinerary;
|
|
if (itinerary == null || index < 0 || index >= itinerary.TotalStops)
|
|
return "Select a planned stop.";
|
|
if (index < itinerary.CompletedStops)
|
|
return "This port call is already recorded.";
|
|
if (index == itinerary.TotalStops - 1)
|
|
return null;
|
|
|
|
int origin = index == 0 ? itinerary.Origin : itinerary.Stops[index - 1];
|
|
return ItineraryLaneBlock(ship, origin, itinerary.Stops[index + 1]);
|
|
}
|
|
|
|
public string RemoveItineraryStop(Ship ship, int index)
|
|
{
|
|
string block = ItineraryRemovalBlock(ship, index);
|
|
if (block != null)
|
|
return block;
|
|
ship.Itinerary.Stops.RemoveAt(index);
|
|
if (ship.Itinerary.TotalStops == 0)
|
|
ship.Itinerary = null;
|
|
return null;
|
|
}
|
|
|
|
public string ClearItinerary(Ship ship)
|
|
{
|
|
if (ship == null || !Fleet.Contains(ship))
|
|
return "Choose a ship in this fleet.";
|
|
ship.Itinerary = null;
|
|
return null;
|
|
}
|
|
|
|
private string ItineraryLaneBlock(Ship ship, int origin, int destination)
|
|
{
|
|
if (destination < 0 || destination >= Market.Ports.Count)
|
|
return "Select a port to add to the itinerary.";
|
|
if (destination == origin)
|
|
return "Choose a different port for the next stop.";
|
|
if (!Market.TryDistance(origin, destination, out int miles))
|
|
return $"There is no direct lane from {Market.Ports[origin].Name} to {Market.Ports[destination].Name}.";
|
|
|
|
PortSpec port = Market.Ports[destination];
|
|
if (!Permits[destination])
|
|
return $"Acquire the permit for {port.Name} first.";
|
|
if (ship.HullIndex < port.MinimumHull)
|
|
return $"{port.Name} requires a {Market.Hulls[port.MinimumHull].Name} hull.";
|
|
var (burn, reserve) = LegFuel(ship, miles);
|
|
if (burn + reserve > ship.Tank)
|
|
return $"This leg needs {burn + reserve} t including reserve. Fit a larger fuel tank first.";
|
|
return null;
|
|
}
|
|
|
|
private bool ValidSavedItinerary(Ship ship)
|
|
{
|
|
CruiseItinerary itinerary = ship.Itinerary;
|
|
if (itinerary == null)
|
|
return true;
|
|
if (itinerary.CompletedStops < 0 || itinerary.CompletedStops > itinerary.TotalStops)
|
|
return false;
|
|
if (itinerary.TotalStops == 0)
|
|
return itinerary.StartDay == 0;
|
|
if (itinerary.Origin < 0 || itinerary.Origin >= Market.Ports.Count)
|
|
return false;
|
|
if (itinerary.CompletedStops == 0 ? itinerary.StartDay != 0 : itinerary.StartDay < 1 || itinerary.StartDay >= Day)
|
|
return false;
|
|
|
|
int origin = itinerary.Origin;
|
|
foreach (int port in itinerary.Stops)
|
|
{
|
|
if (port < 0 || port >= Market.Ports.Count || !Market.TryDistance(origin, port, out _))
|
|
return false;
|
|
origin = port;
|
|
}
|
|
|
|
int berth = itinerary.CompletedStops == 0 ? itinerary.Origin : itinerary.Stops[itinerary.CompletedStops - 1];
|
|
return berth == ship.Port || !itinerary.HasNext;
|
|
}
|
|
|
|
private static (int Burn, int Reserve) LegFuel(Ship ship, int miles)
|
|
{
|
|
int burn = (int)Math.Ceiling(ship.BurnPerMile * miles);
|
|
return (burn, Math.Max(6, (int)Math.Ceiling(burn * .12)));
|
|
}
|
|
|
|
private void RecordItineraryArrival(Ship ship, Voyage quote)
|
|
{
|
|
if (ship.Itinerary is not { HasNext: true } itinerary)
|
|
return;
|
|
if (itinerary.CompletedStops == 0)
|
|
itinerary.StartDay = Day;
|
|
itinerary.CompletedStops++;
|
|
quote.ItineraryStartDay = itinerary.StartDay;
|
|
quote.ItineraryLeg = itinerary.CompletedStops;
|
|
quote.ItineraryLegs = itinerary.TotalStops;
|
|
}
|
|
}
|
|
}
|