using System; using System.Collections.Generic; namespace Soundings { [Serializable] public sealed class ShoreOffer { public int Tour, Price, Capacity; } public sealed class ShoreTour { public readonly string Name, Description; public readonly int StandardPrice, MinPrice, MaxPrice, MaxCapacity; public readonly float Hours; public readonly bool Outdoor; internal readonly int OperatorRate, BaseRisk; internal readonly double Appeal; internal ShoreTour(string name, string description, int price, int operatorRate, int capacity, float hours, double appeal, int risk, bool outdoor = false) { Name = name; Description = description; StandardPrice = price; MinPrice = operatorRate + 8; MaxPrice = price * 3; MaxCapacity = capacity; Hours = hours; Outdoor = outdoor; OperatorRate = operatorRate; BaseRisk = risk; Appeal = appeal; } } [Serializable] public sealed class ShoreBooking { public int Tour, Price, Capacity, Demand, Guests, Revenue, OperatorCost, DelayRisk; } public enum ShorePhase { Touring = 1, AwaitingDecision, Holding, Complete } public enum ShoreLateResolution { None, Held, AlternateTransport } [Serializable] public sealed class ShoreCall { public int CallId, Port, Guests, Revenue, OperatorCost, LateGuests, LateTour, HoldCost, TransportCost, ExtraCost; public int GuestImpact, ReputationImpact, HoldMinutes; public float RemainingSeconds; public ShorePhase Phase; public ShoreLateResolution Resolution; public ShoreBooking[] Bookings; public int NetIncome => Revenue - OperatorCost - ExtraCost; public bool Advance(float seconds) { if (Phase is not (ShorePhase.Touring or ShorePhase.Holding) || !(seconds > 0)) return false; RemainingSeconds = Math.Max(0, RemainingSeconds - seconds); if (RemainingSeconds > 0) return false; Phase = Phase == ShorePhase.Touring && LateGuests > 0 ? ShorePhase.AwaitingDecision : ShorePhase.Complete; return true; } public bool ResolveLate(bool hold) { if (Phase != ShorePhase.AwaitingDecision) return false; if (hold) { Resolution = ShoreLateResolution.Held; ExtraCost = HoldCost; GuestImpact = Math.Max(-4, GuestImpact - 1); Phase = ShorePhase.Holding; RemainingSeconds = 7 + HoldMinutes / 8f; } else { Resolution = ShoreLateResolution.AlternateTransport; ExtraCost = TransportCost; GuestImpact = Math.Max(-10, GuestImpact - 3 - Math.Min(4, LateGuests / 6)); ReputationImpact = Math.Max(-3, ReputationImpact - 3); Phase = ShorePhase.Complete; } return true; } public ShoreReport Report() => Phase != ShorePhase.Complete ? null : new ShoreReport { CallId = CallId, Port = Port, Guests = Guests, Revenue = Revenue, OperatorCost = OperatorCost, ExtraCost = ExtraCost, GuestImpact = GuestImpact, ReputationImpact = ReputationImpact, LateGuests = LateGuests, Bookings = Bookings, Resolution = Resolution }; public bool CanLoad(int shipPort) { if (CallId < 1 || Port != shipPort || !ShoreExcursions.ValidReceipt(Port, Bookings, Guests, Revenue, OperatorCost, ExtraCost, GuestImpact, ReputationImpact, LateGuests, Resolution)) return false; int margin = Revenue - OperatorCost; if (!(RemainingSeconds >= 0 && RemainingSeconds <= 60) || HoldCost < 0 || HoldCost > margin || TransportCost < 0 || TransportCost > margin) return false; if (LateGuests == 0 ? LateTour != -1 || HoldMinutes != 0 || HoldCost != 0 || TransportCost != 0 : (uint)LateTour >= (uint)ShoreExcursions.Catalog(Port).Count || HoldMinutes < 10 || HoldMinutes > 120) return false; return Phase switch { ShorePhase.Touring => RemainingSeconds > 0 && Resolution == ShoreLateResolution.None && ExtraCost == 0, ShorePhase.AwaitingDecision => RemainingSeconds == 0 && LateGuests > 0 && Resolution == ShoreLateResolution.None && ExtraCost == 0, ShorePhase.Holding => RemainingSeconds > 0 && LateGuests > 0 && Resolution == ShoreLateResolution.Held && ExtraCost == HoldCost, ShorePhase.Complete => RemainingSeconds == 0 && (LateGuests == 0 ? ExtraCost == 0 && Resolution == ShoreLateResolution.None : Resolution == ShoreLateResolution.Held && ExtraCost == HoldCost || Resolution == ShoreLateResolution.AlternateTransport && ExtraCost == TransportCost), _ => false }; } } [Serializable] public sealed class ShoreReport { public int CallId, Port, Guests, Revenue, OperatorCost, ExtraCost, GuestImpact, ReputationImpact, LateGuests; public ShoreBooking[] Bookings; public ShoreLateResolution Resolution; public int NetIncome => Revenue - OperatorCost - ExtraCost; public bool CanLoad(int port, int callId) => Port == port && CallId == callId && CallId > 0 && (LateGuests > 0 ? Resolution != ShoreLateResolution.None : Resolution == ShoreLateResolution.None) && ShoreExcursions.ValidReceipt(Port, Bookings, Guests, Revenue, OperatorCost, ExtraCost, GuestImpact, ReputationImpact, LateGuests, Resolution); } public static class ShoreExcursions { private static readonly IReadOnlyList[] PortTours = { new ShoreTour[] // Miami { new("Little Havana tasting walk", "Cafecito, bakeries and neighborhood music", 50, 24, 104, 2.5f, .33, 15), new("Everglades boardwalk", "Wetland trail and wildlife lookout", 82, 39, 84, 4, .49, 30, true) }, new ShoreTour[] // Nassau { new("Old Nassau food walk", "Market stalls, local kitchens and Bahamian sweets", 54, 28, 114, 2, .39, 12), new("Coral reef catamaran", "Sail out to the reef and snorkel with a local guide", 94, 47, 84, 4, .52, 34, true) }, new ShoreTour[] // Key West { new("Old Town porches", "A guide through lanes and shaded historic homes", 44, 18, 92, 2, .37, 12), new("Mangrove kayak circuit", "Paddle the mangrove channels with a guide", 78, 34, 68, 3.5f, .44, 29, true) }, new ShoreTour[] // Cozumel { new("Island cacao workshop", "Stone-ground chocolate and a guided tasting", 58, 28, 88, 2.5f, .38, 13), new("Reef and beach launch", "A small-boat reef trip and time on the beach", 98, 45, 118, 4.5f, .53, 39, true) }, new ShoreTour[] // San Juan { new("Old San Juan courtyards", "Historic streets and a guided fort visit", 61, 26, 126, 3, .41, 15), new("Rainforest trails", "A coach transfer to forest trails and waterfalls", 104, 51, 104, 5, .53, 39, true) }, new ShoreTour[] // Bermuda { new("Dockyard naval history", "Maritime collections and a dockyard guide", 59, 27, 136, 2.5f, .40, 12), new("Pink-sand coast", "Beach time and a coastal lookout", 85, 42, 116, 4, .56, 30, true) }, new ShoreTour[] // Ponta Delgada { new("Azorean tea and pastries", "Tea tasting and a small bakery stop", 64, 28, 144, 3.5f, .41, 22), new("Sete Cidades overlooks", "Volcanic lakes and high viewpoints", 96, 42, 144, 4.5f, .54, 33, true) }, new ShoreTour[] // Lisbon { new("Belém tile and pastry walk", "A guide through tilework, monuments and pastry shops", 65, 24, 150, 3, .45, 20), new("Tagus sail", "An open-air sailing circuit along the waterfront", 99, 47, 176, 4, .45, 35, true) }, new ShoreTour[] // Barcelona { new("Modernist architecture route", "Architectural interiors and a guide to the city", 88, 40, 176, 3.5f, .52, 20), new("Montjuïc gardens", "Hilltop views, gardens and outdoor galleries", 73, 32, 148, 4, .37, 29, true) }, new ShoreTour[] // Port Said { new("Canal quarter and market", "A dockside guide and a covered market visit", 57, 24, 148, 2.5f, .39, 13), new("Canal lookout circuit", "Coach transfer to waterfront and canal overlooks", 86, 41, 132, 4, .45, 30, true) }, new ShoreTour[] // Salalah { new("Frankincense souk", "Resins, spices and a guided look at local trade", 61, 26, 132, 3, .41, 19), new("Wadi and coastal drive", "Mountain roads, a wadi stop and an ocean lookout", 101, 52, 115, 4.5f, .49, 37, true) }, new ShoreTour[] // Mumbai { new("Market kitchens", "Street snacks and a small-group cookery stop", 66, 30, 132, 3.5f, .47, 24), new("Elephanta ferry visit", "An island ferry and a guided walk to cave temples", 106, 49, 170, 5, .59, 35, true) }, new ShoreTour[] // Singapore { new("Hawker food circuit", "Covered food stalls and a tasting itinerary", 72, 32, 164, 3, .49, 15), new("Harbor gardens at dusk", "Open garden paths and skyline overlooks", 104, 48, 200, 4.5f, .55, 26, true) }, new ShoreTour[] // Benoa { new("Balinese craft workshop", "Woodcarving and textile demonstrations", 78, 36, 144, 3.5f, .43, 21), new("Rice terrace footpaths", "A coach journey and guided terrace walk", 114, 55, 136, 5, .55, 42, true) }, new ShoreTour[] // Sydney { new("Opera House backstage", "A guided look into waterfront performance spaces", 94, 45, 160, 3, .49, 15), new("Harbour headlands", "Outdoor footpaths and panoramic harbor views", 118, 50, 176, 4.5f, .51, 32, true) }, new ShoreTour[] // Auckland { new("Waterfront museum", "A guided maritime history and design visit", 71, 31, 142, 3, .44, 14), new("Volcanic rim walk", "Hilltop tracks and views across the harbors", 105, 48, 136, 4.5f, .50, 32, true) }, new ShoreTour[] // Papeete { new("Island market and pearl studio", "A covered market visit and a pearl workshop", 74, 34, 118, 2.5f, .44, 18), new("Lagoon outrigger", "Outrigger trip with a reef-side swim stop", 126, 58, 120, 4.5f, .58, 41, true) }, new ShoreTour[] // Honolulu { new("Chinatown flavors", "Markets, neighborhood history and local food", 82, 35, 136, 3, .44, 18), new("Diamond Head walk", "A crater trail and a high coastal lookout", 116, 51, 172, 4.5f, .56, 34, true) }, new ShoreTour[] // San Diego { new("Balboa Park collections", "A guided selection of museums and architecture", 69, 29, 160, 3, .43, 17), new("La Jolla coastal walk", "Clifftops, tidal pools and shoreline wildlife", 92, 41, 142, 4, .48, 34, true) }, new ShoreTour[] // Puerto Vallarta { new("Old Town cooking school", "A market ingredient stop and a kitchen lesson", 72, 36, 126, 3.5f, .48, 18), new("Bay wildlife boat", "A guided boat trip along the bay and reefs", 110, 55, 156, 4.5f, .53, 39, true) }, new ShoreTour[] // Balboa { new("Canal locks and exhibits", "A guide to ship traffic and the lock exhibits", 81, 36, 184, 3.5f, .55, 20), new("Rainforest canopy circuit", "Forest trails and a canopy viewing platform", 124, 54, 138, 5, .48, 40, true) }, new ShoreTour[] // Cartagena { new("Walled city kitchens", "Market ingredients, courtyards and regional cooking", 63, 27, 144, 3, .48, 18), new("Island beach launch", "A boat transfer to an offshore beach stop", 102, 51, 140, 4.5f, .54, 39, true) } }; public static IReadOnlyList Catalog(int port) => (uint)port < (uint)PortTours.Length ? PortTours[port] : Array.Empty(); public static bool ValidOffers(int port, IReadOnlyList offers) { if (offers == null || offers.Count == 0) return true; IReadOnlyList catalog = Catalog(port); if (offers.Count > catalog.Count) return false; int seen = 0; foreach (ShoreOffer offer in offers) { if (offer == null || (uint)offer.Tour >= (uint)catalog.Count || (seen & (1 << offer.Tour)) != 0) return false; ShoreTour tour = catalog[offer.Tour]; if (offer.Capacity < 0 || offer.Capacity > tour.MaxCapacity || offer.Capacity > 0 && (offer.Price < tour.MinPrice || offer.Price > tour.MaxPrice)) return false; seen |= 1 << offer.Tour; } return true; } public static ShoreBooking[] Forecast(Voyage quote, IReadOnlyList offers) { if (quote == null || quote.Guests <= 0 || offers == null || offers.Count == 0 || !ValidOffers(quote.Destination, offers)) return Array.Empty(); IReadOnlyList catalog = Catalog(quote.Destination); var bookings = new List(offers.Count); int total = 0; foreach (ShoreOffer offer in offers) { if (offer.Capacity == 0) continue; ShoreTour tour = catalog[offer.Tour]; double priceAppeal = Math.Pow((tour.StandardPrice + 14.0) / (offer.Price + 14.0), 1.35); double weatherAppeal = tour.Outdoor ? quote.Weather switch { WeatherCondition.Swell => .82, WeatherCondition.Storm => .46, _ => 1 } : 1; int demand = Math.Min(quote.Guests, (int)Math.Round(quote.Guests * tour.Appeal * priceAppeal * weatherAppeal)); int guests = Math.Min(demand, offer.Capacity); int trafficRisk = 9 * guests / tour.MaxCapacity; int weatherRisk = quote.Weather switch { WeatherCondition.Swell => tour.Outdoor ? 12 : 3, WeatherCondition.Storm => tour.Outdoor ? 26 : 9, _ => 0 }; bookings.Add(new ShoreBooking { Tour = offer.Tour, Price = offer.Price, Capacity = offer.Capacity, Demand = demand, Guests = guests, DelayRisk = Math.Min(85, tour.BaseRisk + trafficRisk + weatherRisk) }); total += guests; } int guestsLeft = quote.Guests; int projectedLeft = total; foreach (ShoreBooking booking in bookings) { int projected = booking.Guests; if (projectedLeft > guestsLeft) booking.Guests = (int)Math.Round((double)projected * guestsLeft / projectedLeft); guestsLeft -= booking.Guests; projectedLeft -= projected; booking.Revenue = booking.Guests * booking.Price; booking.OperatorCost = booking.Guests * catalog[booking.Tour].OperatorRate; } return bookings.ToArray(); } public static ShoreCall Create(Voyage quote, IReadOnlyList offers) { ShoreBooking[] bookings = Forecast(quote, offers); if (bookings.Length == 0) return null; var call = new ShoreCall { Port = quote.Destination, Bookings = bookings, LateTour = -1, Phase = ShorePhase.Touring }; IReadOnlyList catalog = Catalog(quote.Destination); float longest = 0; int longestDelay = 0; int value = 0; foreach (ShoreBooking booking in bookings) { if (booking.Guests == 0) continue; ShoreTour tour = catalog[booking.Tour]; longest = Math.Max(longest, tour.Hours); call.Guests += booking.Guests; call.Revenue += booking.Revenue; call.OperatorCost += booking.OperatorCost; value += booking.Guests * Math.Max(0, 2 - booking.Price / tour.StandardPrice); uint roll = DelaySeed(quote, booking.Tour); if (roll % 100 >= booking.DelayRisk) continue; int late = Math.Min(booking.Guests, Math.Max(2, booking.Guests / 7) + (int)(roll / 100 % 5)); int minutes = 18 + (int)(roll / 500 % 34) + (tour.Outdoor && quote.Weather != WeatherCondition.Calm ? 12 : 0); call.LateGuests += late; if (minutes > longestDelay) { longestDelay = minutes; call.LateTour = booking.Tour; } } if (call.Guests == 0) return null; call.RemainingSeconds = 13 + longest * 5; call.GuestImpact = Market.Clamp(1 + (value * 3 + call.Guests) / quote.Guests, 1, 7); if (quote.Onboard?.Health is > 0 and < 70) call.GuestImpact = Math.Max(1, call.GuestImpact - 1); call.ReputationImpact = call.Guests >= quote.Guests / 4 ? 2 : 1; if (call.LateGuests > 0) { int margin = call.Revenue - call.OperatorCost; call.HoldMinutes = longestDelay; call.HoldCost = Math.Min(margin * 3 / 4, 145 + longestDelay * 16); call.TransportCost = Math.Min(margin * 45 / 100, 90 + call.LateGuests * 24); } return call; } internal static bool ValidReceipt(int port, ShoreBooking[] bookings, int guests, int revenue, int operatorCost, int extras, int guestImpact, int reputationImpact, int lateGuests, ShoreLateResolution resolution) { IReadOnlyList catalog = Catalog(port); if (bookings == null || bookings.Length == 0 || bookings.Length > catalog.Count || guests <= 0 || extras < 0 || extras > revenue - operatorCost || guestImpact < -10 || guestImpact > 7 || reputationImpact < -3 || reputationImpact > 2 || lateGuests < 0 || lateGuests > guests || (uint)resolution > (uint)ShoreLateResolution.AlternateTransport) return false; int seen = 0; int countedGuests = 0, countedRevenue = 0, countedOperatorCost = 0; foreach (ShoreBooking booking in bookings) { if (booking == null || (uint)booking.Tour >= (uint)catalog.Count || (seen & (1 << booking.Tour)) != 0) return false; ShoreTour tour = catalog[booking.Tour]; if (booking.Price < tour.MinPrice || booking.Price > tour.MaxPrice || booking.Capacity <= 0 || booking.Capacity > tour.MaxCapacity || booking.Guests < 0 || booking.Guests > booking.Capacity || booking.Demand < booking.Guests || booking.Revenue != booking.Guests * booking.Price || booking.OperatorCost != booking.Guests * tour.OperatorRate || booking.DelayRisk is < 0 or > 85) return false; seen |= 1 << booking.Tour; countedGuests += booking.Guests; countedRevenue += booking.Revenue; countedOperatorCost += booking.OperatorCost; } return countedGuests == guests && countedRevenue == revenue && countedOperatorCost == operatorCost; } private static uint DelaySeed(Voyage quote, int tour) { unchecked { uint seed = 2166136261U; foreach (char character in quote.ShipName ?? "") seed = (seed ^ character) * 16777619; seed = (seed ^ (uint)quote.Day) * 16777619; seed = (seed ^ (uint)quote.Origin) * 16777619; seed = (seed ^ (uint)quote.Destination) * 16777619; seed = (seed ^ (uint)tour) * 16777619; seed ^= seed >> 16; seed *= 0x7FEB352D; seed ^= seed >> 15; return seed; } } } }