Enable placeable sun deck attractions and finish project rebrand
This commit is contained in:
188
Assets/BigShipTycoon/Runtime/FleetPassageSave.cs
Normal file
188
Assets/BigShipTycoon/Runtime/FleetPassageSave.cs
Normal file
@@ -0,0 +1,188 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BigShipTycoon
|
||||
{
|
||||
[Serializable]
|
||||
internal sealed class FleetPassageSnapshot
|
||||
{
|
||||
public int ShipIndex, Speed;
|
||||
public bool ApproachAnnounced, HasDeparturePlan, HasView;
|
||||
public Voyage Voyage;
|
||||
public HelmSnapshot Helm;
|
||||
public OnboardSessionSnapshot Session;
|
||||
public PassageViewSnapshot View;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
internal sealed class PassageViewSnapshot
|
||||
{
|
||||
public int Deck, ServiceArea;
|
||||
public bool Bridge, Exploring, BusinessOpen, MapOpen;
|
||||
}
|
||||
|
||||
public sealed partial class TycoonApp
|
||||
{
|
||||
private FleetPassageSnapshot[] CaptureFleetPassages()
|
||||
{
|
||||
if (company == null || fleetPassages.Count == 0)
|
||||
return Array.Empty<FleetPassageSnapshot>();
|
||||
|
||||
var snapshots = new List<FleetPassageSnapshot>(fleetPassages.Count);
|
||||
for (int i = 0; i < company.Fleet.Count; i++)
|
||||
{
|
||||
Ship ship = company.Fleet[i];
|
||||
if (!fleetPassages.TryGetValue(ship, out FleetPassage passage))
|
||||
continue;
|
||||
|
||||
bool foreground = ship == Ship && underway == passage.Voyage;
|
||||
bool hasDeparturePlan = passage.Voyage.Plan != null;
|
||||
PassageViewSnapshot view = foreground ? CapturePassageView() : passage.RestoredView;
|
||||
snapshots.Add(new FleetPassageSnapshot
|
||||
{
|
||||
ShipIndex = i, Speed = passage.Speed, Voyage = passage.Voyage.CopyForPassage(hasDeparturePlan),
|
||||
HasDeparturePlan = hasDeparturePlan,
|
||||
ApproachAnnounced = foreground ? passageAudio.ApproachSent : passage.ApproachAnnounced,
|
||||
Helm = passage.Helm.CaptureState(), Session = passage.Session.CaptureState(),
|
||||
HasView = view != null, View = view
|
||||
});
|
||||
}
|
||||
return snapshots.ToArray();
|
||||
}
|
||||
|
||||
private static bool CanRestoreFleetPassages(Company savedCompany, FleetPassageSnapshot[] snapshots) =>
|
||||
TryRestoreFleet(savedCompany, snapshots, out _);
|
||||
|
||||
private bool RestoreFleetPassages(FleetPassageSnapshot[] snapshots, bool focus = false)
|
||||
{
|
||||
if (!TryRestoreFleet(company, snapshots, out Dictionary<Ship, FleetPassage> restored))
|
||||
return false;
|
||||
|
||||
ResetFleetPassages();
|
||||
foreach (var entry in restored)
|
||||
fleetPassages.Add(entry.Key, entry.Value);
|
||||
UpdateVoyageReserves();
|
||||
if (focus)
|
||||
ResumeSelectedSavedPassage();
|
||||
return true;
|
||||
}
|
||||
|
||||
private void ResumeSelectedSavedPassage()
|
||||
{
|
||||
Ship selected = Ship;
|
||||
if (selected == null || underway != null || !fleetPassages.TryGetValue(selected, out FleetPassage passage))
|
||||
return;
|
||||
|
||||
page = 0;
|
||||
FocusPassage(passage, false);
|
||||
PassageViewSnapshot view = passage.RestoredView;
|
||||
passage.RestoredView = null;
|
||||
if (view != null)
|
||||
RestorePassageView(view);
|
||||
else if (passage.Helm.Manual)
|
||||
{
|
||||
passageBridge = true;
|
||||
harbor.ViewBridge();
|
||||
}
|
||||
}
|
||||
|
||||
private PassageViewSnapshot CapturePassageView() => new PassageViewSnapshot
|
||||
{
|
||||
Deck = passageDeck, ServiceArea = passageServiceArea, Bridge = passageBridge, Exploring = passageExploring,
|
||||
BusinessOpen = passageBusinessOpen, MapOpen = voyageMapOpen
|
||||
};
|
||||
|
||||
private void RestorePassageView(PassageViewSnapshot view)
|
||||
{
|
||||
if (view.Bridge || passageHelm.Manual)
|
||||
{
|
||||
passageBridge = true;
|
||||
harbor.ViewBridge();
|
||||
}
|
||||
else if (view.ServiceArea >= 0)
|
||||
{
|
||||
passageServiceArea = view.ServiceArea;
|
||||
passageExploring = true;
|
||||
harbor.ExploreServiceArea(passageServiceArea);
|
||||
}
|
||||
else if (view.Deck >= 0)
|
||||
{
|
||||
passageDeck = view.Deck;
|
||||
passageExploring = view.Exploring;
|
||||
if (passageExploring)
|
||||
harbor.ExploreDeck(passageDeck);
|
||||
else
|
||||
harbor.ShowDeck(passageDeck);
|
||||
}
|
||||
|
||||
passageBusinessOpen = view.BusinessOpen;
|
||||
voyageMapOpen = view.MapOpen;
|
||||
}
|
||||
|
||||
private static bool TryRestoreFleet(Company savedCompany, FleetPassageSnapshot[] snapshots,
|
||||
out Dictionary<Ship, FleetPassage> restored)
|
||||
{
|
||||
restored = new Dictionary<Ship, FleetPassage>();
|
||||
if (savedCompany?.Fleet == null)
|
||||
return false;
|
||||
if (snapshots == null || snapshots.Length == 0)
|
||||
return true;
|
||||
if (snapshots.Length > savedCompany.Fleet.Count)
|
||||
return false;
|
||||
|
||||
long reserved = 0;
|
||||
foreach (FleetPassageSnapshot snapshot in snapshots)
|
||||
{
|
||||
if (snapshot == null || snapshot.ShipIndex < 0 || snapshot.ShipIndex >= savedCompany.Fleet.Count
|
||||
|| (snapshot.Speed != 1 && snapshot.Speed != 2 && snapshot.Speed != 4))
|
||||
return false;
|
||||
|
||||
Ship ship = savedCompany.Fleet[snapshot.ShipIndex];
|
||||
Voyage voyage = snapshot.Voyage;
|
||||
if (ship == null || restored.ContainsKey(ship) || !ValidSavedVoyage(ship, voyage, snapshot.HasDeparturePlan))
|
||||
return false;
|
||||
|
||||
Voyage quote = voyage.CopyForPassage(snapshot.HasDeparturePlan);
|
||||
reserved += (long)quote.Crew + quote.Catering + quote.PortFees + (quote.Plan?.FoodCost ?? 0)
|
||||
+ (quote.Plan?.ProgramCost ?? 0) + quote.LoanInterest + quote.Principal;
|
||||
if (reserved > int.MaxValue)
|
||||
return false;
|
||||
|
||||
var passage = new FleetPassage(ship, quote, snapshot.Session?.Assisted ?? false);
|
||||
if (!passage.Helm.RestoreState(snapshot.Helm) || !passage.Session.RestoreState(snapshot.Session))
|
||||
return false;
|
||||
passage.Speed = snapshot.Speed;
|
||||
passage.ApproachAnnounced = snapshot.ApproachAnnounced;
|
||||
PassageViewSnapshot view = snapshot.View;
|
||||
if (snapshot.HasView && view != null && view.Deck >= -1 && view.Deck < ship.Decks.Count
|
||||
&& view.ServiceArea >= -1 && view.ServiceArea < ServiceDeckModel.Areas.Length)
|
||||
passage.RestoredView = view;
|
||||
|
||||
if (snapshot.ShipIndex != savedCompany.SelectedShip && passage.Helm.Manual)
|
||||
{
|
||||
passage.Helm.Manual = false;
|
||||
passage.Helm.PropulsionStopped = false;
|
||||
}
|
||||
restored.Add(ship, passage);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool ValidSavedVoyage(Ship ship, Voyage voyage, bool hasDeparturePlan)
|
||||
{
|
||||
if (voyage == null || voyage.Origin != ship.Port || voyage.Destination < 0
|
||||
|| voyage.Destination >= Market.Ports.Count || !Market.TryDistance(voyage.Origin, voyage.Destination, out int miles)
|
||||
|| voyage.Miles != miles || voyage.Days < 1 || voyage.Days > 1000 || voyage.Day < 1
|
||||
|| (uint)voyage.Weather > (uint)WeatherCondition.Storm || voyage.Guests < 1 || voyage.Guests > voyage.Capacity
|
||||
|| voyage.RoomDemand < voyage.Guests || voyage.Fare < Market.MinFare || voyage.Fare > Market.MaxFare
|
||||
|| voyage.TicketSales < 0 || voyage.FuelBurn < 1 || voyage.Reserve < 0 || voyage.Crew < 0 || voyage.Catering < 0
|
||||
|| ship.ShoreCall != null || !ShoreExcursions.ValidOffers(voyage.Destination, voyage.ShoreOffers) || !CrewMarket.ValidPayroll(voyage)
|
||||
|| voyage.PortFees < 0 || voyage.LoanInterest < 0 || voyage.Principal < 0)
|
||||
return false;
|
||||
|
||||
DeparturePlan plan = voyage.Plan;
|
||||
return !hasDeparturePlan || (plan != null && plan.FoodUnits >= 0 && plan.FoodUnits <= ship.FoodCapacity && plan.FoodCost >= 0
|
||||
&& plan.ProgramCost >= 0 && (uint)plan.Program <= (uint)EntertainmentProgram.Headline);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user