Skip to content

Commit

Permalink
Fix various issues with showing generated waypoint markers
Browse files Browse the repository at this point in the history
  • Loading branch information
siimav committed Aug 6, 2024
1 parent dca670a commit 1cc10a3
Showing 1 changed file with 51 additions and 5 deletions.
56 changes: 51 additions & 5 deletions source/ContractConfigurator/Behaviour/WaypointGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ public void SetContract(Contract contract)
}
private List<WaypointData> waypoints = new List<WaypointData>();
private bool initialized = false;
private bool boundToMapViewEvent = false;
private static System.Random random = new System.Random();

public WaypointGenerator()
{
GameEvents.OnMapViewFiltersModified.Add(OnMapViewFiltersModified);
}

/// <summary>
Expand Down Expand Up @@ -512,13 +512,15 @@ protected override void OnRegister()
{
base.OnRegister();

BindMapViewEventIfNeeded();
GameEvents.onFlightReady.Add(OnFlightReady);
}

protected override void OnUnregister()
{
base.OnUnregister();

UnbindMapViewEventIfNeeded();
GameEvents.onFlightReady.Remove(OnFlightReady);

foreach (WaypointData wpData in waypoints)
Expand All @@ -527,6 +529,39 @@ protected override void OnUnregister()
}
}

protected override void OnDeclined()
{
UnbindMapViewEventIfNeeded();
}

protected override void OnOfferExpired()
{
UnbindMapViewEventIfNeeded();
}

protected override void OnFinished()
{
UnbindMapViewEventIfNeeded();
}

protected void BindMapViewEventIfNeeded()
{
if (!boundToMapViewEvent)
{
GameEvents.OnMapViewFiltersModified.Add(OnMapViewFiltersModified);
boundToMapViewEvent = true;
}
}

protected void UnbindMapViewEventIfNeeded()
{
if (boundToMapViewEvent)
{
GameEvents.OnMapViewFiltersModified.Remove(OnMapViewFiltersModified);
boundToMapViewEvent = false;
}
}

protected void OnMapViewFiltersModified(MapViewFiltering.VesselTypeFilter filter)
{
if (filter == MapViewFiltering.VesselTypeFilter.None)
Expand All @@ -535,8 +570,16 @@ protected void OnMapViewFiltersModified(MapViewFiltering.VesselTypeFilter filter
foreach (WaypointData wpData in waypoints)
{
WaypointManager.RemoveWaypoint(wpData.waypoint);
wpData.waypoint.node = null; // The call above will destroy the node but it doesn't happen immediately. Just set it to null so that it would get created from scratch.
wpData.isAdded = false;
AddWayPoint(wpData);

string paramID = wpData.parameter.FirstOrDefault();
if (wpData.waypoint.visible && wpData.waypoint.contractReference != null &&
(!wpData.parameter.Any() || wpData.waypoint.contractReference.AllParameters.
Any(p => p.ID == paramID && p.State == ParameterState.Complete)))
{
AddWayPoint(wpData);
}
}
}
}
Expand All @@ -555,8 +598,9 @@ protected override void OnOffered()
{
string paramID = wpData.parameter.FirstOrDefault();
if (wpData.waypoint.visible && (!wpData.parameter.Any() || contract.AllParameters.
Where(p => p.ID == paramID && p.State == ParameterState.Complete).Any()))
Any(p => p.ID == paramID && p.State == ParameterState.Complete)))
{
BindMapViewEventIfNeeded();
AddWayPoint(wpData);
}
}
Expand Down Expand Up @@ -618,9 +662,11 @@ protected override void OnLoad(ConfigNode configNode)

// Create additional waypoint details
string paramID = wpData.parameter.FirstOrDefault();
if (wpData.waypoint.visible && (!wpData.parameter.Any() || contract.AllParameters.
Where(p => p.ID == paramID && p.State == ParameterState.Complete).Any()))
if ((contract.ContractState == Contract.State.Active || contract.ContractState == Contract.State.Offered) &&
wpData.waypoint.visible && (!wpData.parameter.Any() || contract.AllParameters.
Any(p => p.ID == paramID && p.State == ParameterState.Complete)))
{
BindMapViewEventIfNeeded();
AddWayPoint(wpData);
}

Expand Down

0 comments on commit 1cc10a3

Please sign in to comment.