395 lines
17 KiB
C#
395 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Soundings
|
|
{
|
|
[Serializable]
|
|
public sealed class Deck
|
|
{
|
|
public Facility[] Cells;
|
|
public Deck(int size) => Cells = new Facility[size];
|
|
}
|
|
|
|
[Serializable]
|
|
public sealed class Ship
|
|
{
|
|
public string Name;
|
|
public int HullIndex, Port, Condition, Fare, Fuel;
|
|
public double FuelValue;
|
|
public List<Deck> Decks = new List<Deck>();
|
|
|
|
public HullSpec Hull => Market.Hulls[HullIndex];
|
|
public int Beds => Count(Facility.Cabin) * Market.Facilities[(int)Facility.Cabin].Capacity;
|
|
public int Meals => Count(Facility.Dining) * Market.Facilities[(int)Facility.Dining].Capacity * 3;
|
|
public int Entertainment => Count(Facility.Entertainment) * Market.Facilities[(int)Facility.Entertainment].Capacity;
|
|
public int Capacity => Math.Min(Beds, Meals);
|
|
public int Tank => Hull.Tank + Count(Facility.Tank) * Market.Facilities[(int)Facility.Tank].Capacity;
|
|
public double BurnPerMile => Hull.TonsPerMile * (1 + (100 - Condition) * .003);
|
|
public int SafeRange => Math.Max(0, (int)Math.Floor(Math.Min(Fuel - 6, Fuel * 100 / 112) / BurnPerMile));
|
|
|
|
public int Count(Facility facility)
|
|
{
|
|
int count = 0;
|
|
foreach (Deck deck in Decks)
|
|
foreach (Facility cell in deck.Cells)
|
|
if (cell == facility)
|
|
count++;
|
|
return count;
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public sealed class Voyage
|
|
{
|
|
public int Origin, Destination, Miles, Days, Day, Fare, Guests, Capacity, FuelBurn, Reserve,
|
|
TicketSales, OnboardSales, Crew, Catering, PortFees, FuelExpense, LoanInterest, Principal, ReputationChange;
|
|
public string ShipName;
|
|
public int OperatingCash => Crew + Catering + PortFees;
|
|
public int NetIncome => TicketSales + OnboardSales - OperatingCash - FuelExpense - LoanInterest;
|
|
public int CashChange => TicketSales + OnboardSales - OperatingCash - LoanInterest - Principal;
|
|
public int RequiredFuel => FuelBurn + Reserve;
|
|
}
|
|
|
|
[Serializable]
|
|
public sealed class Company
|
|
{
|
|
public const int SaveVersion = 1;
|
|
public const int MaxHistory = 30;
|
|
public int Version = SaveVersion;
|
|
public int Day = 1, Cash, Debt, Reputation, Voyages;
|
|
public long LifetimeProfit;
|
|
public bool Flagship, Bridge;
|
|
public int SelectedShip, TutorialStep;
|
|
public bool[] Permits = new bool[Market.Ports.Count];
|
|
public List<Ship> Fleet = new List<Ship>();
|
|
public List<Voyage> History = new List<Voyage>();
|
|
|
|
public Ship ActiveShip => SelectedShip >= 0 && SelectedShip < Fleet.Count ? Fleet[SelectedShip] : null;
|
|
|
|
public static Company Start(bool financed)
|
|
{
|
|
var company = new Company
|
|
{
|
|
Cash = financed ? 580000 : 135000,
|
|
Debt = financed ? 500000 : 0,
|
|
Reputation = financed ? 12 : 15,
|
|
Bridge = financed,
|
|
SelectedShip = financed ? -1 : 0
|
|
};
|
|
company.Permits[0] = company.Permits[1] = true;
|
|
if (!financed)
|
|
company.Fleet.Add(CreateShip(0, "MV Meridian", true, true));
|
|
return company;
|
|
}
|
|
|
|
public bool CanLoad()
|
|
{
|
|
if (Version != SaveVersion || Day < 1 || Cash < 0 || Debt < 0 || Reputation < 0 || Reputation > 100
|
|
|| Voyages < 0 || Permits == null || Permits.Length != Market.Ports.Count || !Permits[0] || !Permits[1]
|
|
|| Fleet == null || Fleet.Count > Market.MaxFleet || History == null || History.Count > MaxHistory
|
|
|| (Fleet.Count == 0 ? SelectedShip != -1 : SelectedShip < 0 || SelectedShip >= Fleet.Count))
|
|
return false;
|
|
|
|
foreach (Ship ship in Fleet)
|
|
{
|
|
if (ship == null || string.IsNullOrWhiteSpace(ship.Name) || ship.HullIndex < 0 || ship.HullIndex >= Market.Hulls.Count
|
|
|| ship.Port < 0 || ship.Port >= Permits.Length || !Permits[ship.Port] || ship.Condition < 24
|
|
|| ship.Condition > 100 || ship.Fare < Market.MinFare || ship.Fare > Market.MaxFare
|
|
|| ship.Decks == null || ship.Decks.Count < Market.FirstDecks || ship.Decks.Count > ship.Hull.MaxDecks
|
|
|| ship.Fuel < 0 || double.IsNaN(ship.FuelValue) || double.IsInfinity(ship.FuelValue) || ship.FuelValue < 0)
|
|
return false;
|
|
|
|
foreach (Deck deck in ship.Decks)
|
|
{
|
|
if (deck?.Cells == null || deck.Cells.Length != ship.Hull.Columns * ship.Hull.Rows)
|
|
return false;
|
|
foreach (Facility cell in deck.Cells)
|
|
if ((int)cell < 0 || (int)cell >= Market.Facilities.Count)
|
|
return false;
|
|
}
|
|
|
|
if (ship.Fuel > ship.Tank)
|
|
return false;
|
|
}
|
|
|
|
foreach (Voyage voyage in History)
|
|
if (voyage == null || voyage.Origin < 0 || voyage.Origin >= Permits.Length
|
|
|| voyage.Destination < 0 || voyage.Destination >= Permits.Length)
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
public string ShipyardBlock(int hullIndex, bool used)
|
|
{
|
|
HullSpec hull = Market.Hulls[hullIndex];
|
|
if (used && hullIndex == Market.Ocean)
|
|
return "Ocean Class hulls are available as new commissions.";
|
|
if (Fleet.Count >= Market.MaxFleet)
|
|
return "The fleet has reached its eight-ship limit.";
|
|
if (Voyages < hull.Voyages || Reputation < hull.Reputation)
|
|
return $"Requires {hull.Voyages} voyages and {hull.Reputation} reputation.";
|
|
if (Cash < (used ? Market.UsedPrice(hullIndex) : hull.Price))
|
|
return "The company needs more cash.";
|
|
return null;
|
|
}
|
|
|
|
public string Acquire(int hullIndex, bool used, string name)
|
|
{
|
|
string block = ShipyardBlock(hullIndex, used);
|
|
if (block != null)
|
|
return block;
|
|
if (!used)
|
|
{
|
|
name = name?.Trim();
|
|
if (string.IsNullOrWhiteSpace(name) || name.Length > 24)
|
|
return "Enter a ship name of up to 24 characters.";
|
|
if (!name.StartsWith("MV ", StringComparison.OrdinalIgnoreCase))
|
|
name = "MV " + name;
|
|
}
|
|
else
|
|
name = $"MV Wayfarer {Fleet.Count + 1}";
|
|
|
|
foreach (Ship ship in Fleet)
|
|
if (string.Equals(ship.Name, name, StringComparison.OrdinalIgnoreCase))
|
|
return "A vessel already uses that name.";
|
|
|
|
Cash -= used ? Market.UsedPrice(hullIndex) : Market.Hulls[hullIndex].Price;
|
|
Fleet.Add(CreateShip(hullIndex, name, used, false));
|
|
SelectedShip = Fleet.Count - 1;
|
|
return null;
|
|
}
|
|
|
|
private static Ship CreateShip(int hullIndex, string name, bool used, bool original)
|
|
{
|
|
var ship = new Ship
|
|
{
|
|
Name = name,
|
|
HullIndex = hullIndex,
|
|
Condition = original ? 78 : used ? 72 : 100,
|
|
Fare = hullIndex == 0 ? 550 : hullIndex == 1 ? 850 : 1250,
|
|
Fuel = original ? 48 : used ? 25 : 0
|
|
};
|
|
ship.FuelValue = ship.Fuel * Market.FuelPrice(0, 1);
|
|
for (int i = 0; i < Market.FirstDecks; i++)
|
|
ship.Decks.Add(new Deck(ship.Hull.Columns * ship.Hull.Rows));
|
|
if (used)
|
|
{
|
|
for (int i = 0; i < 8; i++)
|
|
ship.Decks[0].Cells[i] = Facility.Cabin;
|
|
ship.Decks[1].Cells[1] = Facility.Dining;
|
|
ship.Decks[1].Cells[2] = Facility.Entertainment;
|
|
ship.Decks[1].Cells[4] = Facility.Dining;
|
|
}
|
|
return ship;
|
|
}
|
|
|
|
public int DeckPrice(Ship ship) => 29000 + ship.Decks.Count * 8500 + ship.HullIndex * 13000;
|
|
|
|
public string AddDeck(Ship ship)
|
|
{
|
|
int price = DeckPrice(ship);
|
|
if (ship.Decks.Count >= ship.Hull.MaxDecks)
|
|
return "The hull has reached its deck limit.";
|
|
if (Cash < price)
|
|
return "The company needs more cash.";
|
|
Cash -= price;
|
|
ship.Decks.Add(new Deck(ship.Hull.Columns * ship.Hull.Rows));
|
|
return null;
|
|
}
|
|
|
|
public string Build(Ship ship, int deckIndex, int cell, Facility plan)
|
|
{
|
|
Deck deck = ship.Decks[deckIndex];
|
|
Facility previous = deck.Cells[cell];
|
|
if (previous == plan)
|
|
return null;
|
|
if (previous != Facility.Empty && plan != Facility.Empty)
|
|
return "Clear this berth before building another facility.";
|
|
if (previous == Facility.Tank && ship.Fuel > ship.Tank - Market.Facilities[(int)Facility.Tank].Capacity)
|
|
return "Burn more fuel before removing this tank.";
|
|
int price = Market.Facilities[(int)plan].Price;
|
|
if (Cash < price)
|
|
return "The company needs more cash.";
|
|
Cash += Market.Facilities[(int)previous].Price / 4 - price;
|
|
deck.Cells[cell] = plan;
|
|
return null;
|
|
}
|
|
|
|
public string PermitBlock(int destination)
|
|
{
|
|
PortSpec port = Market.Ports[destination];
|
|
if (Permits[destination])
|
|
return "Permit held.";
|
|
if (Reputation < port.Reputation)
|
|
return $"Requires {port.Reputation} reputation.";
|
|
if (Cash < port.Permit)
|
|
return "The company needs more cash.";
|
|
return null;
|
|
}
|
|
|
|
public string Permit(int destination)
|
|
{
|
|
string block = PermitBlock(destination);
|
|
if (block != null)
|
|
return block;
|
|
Cash -= Market.Ports[destination].Permit;
|
|
Permits[destination] = true;
|
|
return null;
|
|
}
|
|
|
|
public int BunkerCost(Ship ship, int tons) => tons * Market.FuelPrice(ship.Port, Day);
|
|
|
|
public string Refuel(Ship ship, int tons)
|
|
{
|
|
if (tons <= 0 || tons > ship.Tank - ship.Fuel)
|
|
return "Select a fuel quantity that fits in the tank.";
|
|
int cost = BunkerCost(ship, tons);
|
|
if (Cash < cost)
|
|
return "The company needs more cash.";
|
|
Cash -= cost;
|
|
ship.Fuel += tons;
|
|
ship.FuelValue += cost;
|
|
return null;
|
|
}
|
|
|
|
public int ServiceCost(Ship ship) => (100 - ship.Condition) * ship.Hull.HullWearCost;
|
|
|
|
public string Service(Ship ship)
|
|
{
|
|
int cost = ServiceCost(ship);
|
|
if (Cash < cost)
|
|
return "The company needs more cash.";
|
|
Cash -= cost;
|
|
ship.Condition = 100;
|
|
return null;
|
|
}
|
|
|
|
public string PayDownDebt()
|
|
{
|
|
int payment = Math.Min(25000, Debt);
|
|
if (payment <= 0)
|
|
return "The company has no outstanding loan.";
|
|
if (Cash < payment)
|
|
return "The company needs more cash.";
|
|
Cash -= payment;
|
|
Debt -= payment;
|
|
return null;
|
|
}
|
|
|
|
public bool CanDrawBridge => Bridge && Cash < 100000;
|
|
|
|
public string DrawBridge()
|
|
{
|
|
if (!CanDrawBridge)
|
|
return "Bridge credit is available once when cash falls below $100,000.";
|
|
Cash += 125000;
|
|
Debt += 125000;
|
|
Bridge = false;
|
|
return null;
|
|
}
|
|
|
|
public Voyage Forecast(Ship ship, int destination)
|
|
{
|
|
if (!Market.TryDistance(ship.Port, destination, out int miles))
|
|
return null;
|
|
|
|
PortSpec from = Market.Ports[ship.Port];
|
|
PortSpec to = Market.Ports[destination];
|
|
int days = 1 + (int)Math.Ceiling(miles / 415.0);
|
|
int fuel = (int)Math.Ceiling(ship.BurnPerMile * miles);
|
|
int reserve = Math.Max(6, (int)Math.Ceiling(fuel * .12));
|
|
double referenceFare = 330 + days * 88 + to.Appeal * 125;
|
|
double fareFactor = Math.Pow(referenceFare / ship.Fare, 1.55);
|
|
double season = 1 + .13 * Math.Cos((Day + destination * 13) * 2 * Math.PI / 112);
|
|
int recentSailings = 0;
|
|
foreach (Voyage voyage in History)
|
|
{
|
|
if (Day - voyage.Day > 12 || Math.Min(voyage.Origin, voyage.Destination) != Math.Min(ship.Port, destination)
|
|
|| Math.Max(voyage.Origin, voyage.Destination) != Math.Max(ship.Port, destination))
|
|
continue;
|
|
recentSailings++;
|
|
}
|
|
|
|
double saturation = Math.Max(.66, 1 - recentSailings * .085);
|
|
double entertainment = Math.Min(1, (double)ship.Entertainment / Math.Max(1, ship.Capacity));
|
|
double marketDemand = (150 + ship.Capacity * .50) * from.MarketSize * to.Appeal * season * saturation;
|
|
double product = (.66 + Reputation * .006 + entertainment * .20) * (.8 + ship.Condition * .002);
|
|
int guests = Market.Clamp((int)Math.Round(marketDemand * fareFactor * product), 0, ship.Capacity);
|
|
double unitCost = ship.Fuel > 0 ? ship.FuelValue / ship.Fuel : Market.FuelPrice(ship.Port, Day);
|
|
|
|
return new Voyage
|
|
{
|
|
Origin = ship.Port,
|
|
Destination = destination,
|
|
ShipName = ship.Name,
|
|
Day = Day + days,
|
|
Days = days,
|
|
Miles = miles,
|
|
Fare = ship.Fare,
|
|
Guests = guests,
|
|
Capacity = ship.Capacity,
|
|
FuelBurn = fuel,
|
|
Reserve = reserve,
|
|
TicketSales = guests * ship.Fare,
|
|
OnboardSales = (int)Math.Round(guests * days * (13 + entertainment * 18)),
|
|
Crew = ship.Hull.CrewPerDay * days + ship.Decks.Count * 500 * days,
|
|
Catering = guests * days * 27,
|
|
PortFees = from.Fee / 2 + to.Fee,
|
|
FuelExpense = (int)Math.Round(Math.Min(fuel, ship.Fuel) * unitCost
|
|
+ Math.Max(0, fuel - ship.Fuel) * Market.FuelPrice(ship.Port, Day)),
|
|
LoanInterest = (int)Math.Ceiling(Debt * .13 * days / 365),
|
|
Principal = Math.Min(Debt, 850 * days),
|
|
ReputationChange = guests == 0 ? -3 : Math.Max(1, (int)Math.Round(2 + 2 * to.Appeal + entertainment * 3))
|
|
};
|
|
}
|
|
|
|
public string DepartureBlock(Ship ship, Voyage quote)
|
|
{
|
|
if (quote == null || quote.Destination == ship.Port)
|
|
return "Select a connected port from your itinerary.";
|
|
if (!Permits[quote.Destination])
|
|
return "Obtain a destination permit in Ports.";
|
|
if (ship.HullIndex < Market.Ports[quote.Destination].MinimumHull)
|
|
return "This destination requires a larger hull class.";
|
|
if (ship.Beds == 0 || ship.Meals == 0)
|
|
return "Build cabins and dining before carrying guests.";
|
|
if (ship.Condition < 25)
|
|
return "An overhaul is required before the ship can sail.";
|
|
if (quote.RequiredFuel > ship.Tank)
|
|
return $"This route needs {quote.RequiredFuel} t. Add a bunker tank or use a larger hull.";
|
|
if (ship.Fuel < quote.RequiredFuel)
|
|
return $"Buy {quote.RequiredFuel - ship.Fuel} t more fuel to cover the crossing and reserve.";
|
|
if (quote.Guests <= 0)
|
|
return "Raise demand or lower the fare to attract passengers.";
|
|
if (Cash < quote.OperatingCash + quote.LoanInterest + quote.Principal)
|
|
return "Cash must cover the voyage expenses and loan payment.";
|
|
return null;
|
|
}
|
|
|
|
public string Sail(Ship ship, int destination)
|
|
{
|
|
Voyage quote = Forecast(ship, destination);
|
|
string block = DepartureBlock(ship, quote);
|
|
if (block != null)
|
|
return block;
|
|
|
|
ship.FuelValue *= (double)(ship.Fuel - quote.FuelBurn) / ship.Fuel;
|
|
ship.Fuel -= quote.FuelBurn;
|
|
ship.Condition = Math.Max(24, ship.Condition - Math.Max(1, quote.Days / 2));
|
|
ship.Port = quote.Destination;
|
|
Day = quote.Day;
|
|
Cash += quote.CashChange;
|
|
Debt -= quote.Principal;
|
|
Reputation = Market.Clamp(Reputation + quote.ReputationChange, 0, 100);
|
|
Voyages++;
|
|
LifetimeProfit += quote.NetIncome;
|
|
Flagship |= ship.HullIndex == Market.Ocean && ship.Capacity >= 320 && ship.Entertainment >= 160
|
|
&& quote.Origin == Market.Bermuda && quote.Destination == Market.Lisbon;
|
|
History.Insert(0, quote);
|
|
if (History.Count > MaxHistory)
|
|
History.RemoveAt(MaxHistory);
|
|
return null;
|
|
}
|
|
}
|
|
}
|