Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 106 additions & 66 deletions src/Core/LaunchSites/LaunchSiteChecks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace KerbalKonstructs.Core
{
class LaunchSiteChecks
internal class LaunchSiteChecks
{
internal static AsmUtils.Detour findVesselDetour;
internal static AsmUtils.Detour findVesselDetour2;
Expand Down Expand Up @@ -58,10 +58,10 @@ public static PreFlightTests.IPreFlightTest GetPartCheck(string launchSiteName)
public class KKPrelaunchSizeCheck : PreFlightTests.IPreFlightTest
{

Vector3 shipSize;
KKLaunchSite launchSite;
private Vector3 shipSize;
private KKLaunchSite launchSite;

bool allowLaunch = false;
private bool allowLaunch = false;


public KKPrelaunchSizeCheck(string launchSiteName)
Expand Down Expand Up @@ -120,7 +120,6 @@ public bool Test()
retval = false;
}

//Log.Normal("Ship dimensions: " + shipSize.ToString() );
return retval;
}

Expand Down Expand Up @@ -155,10 +154,10 @@ public string GetAbortOption()
public class KKPrelaunchMassCheck : PreFlightTests.IPreFlightTest
{

float shipMass;
KKLaunchSite launchSite;
private float shipMass;
private KKLaunchSite launchSite;

bool allowLaunch = false;
private bool allowLaunch = false;


public KKPrelaunchMassCheck(string launchSiteName)
Expand Down Expand Up @@ -238,10 +237,10 @@ public string GetAbortOption()
public class KKPrelaunchPartCheck : PreFlightTests.IPreFlightTest
{

int shipParts;
KKLaunchSite launchSite;
private int shipParts;
private KKLaunchSite launchSite;

bool allowLaunch = false;
private bool allowLaunch = false;


public KKPrelaunchPartCheck(string launchSiteName)
Expand Down Expand Up @@ -302,9 +301,6 @@ public string GetAbortOption()
}
}




/// <summary>
/// ReplaceMent for finding vessels
/// </summary>
Expand All @@ -316,7 +312,6 @@ public string GetAbortOption()
/// <param name="vType"></param>
public static void FindVesselsLandedAtKK(FlightState flightState, string landedAt, out int count, out string name, out int idx, out VesselType vType)
{
// Log.Normal("Called");
float maxDistance = 100f;

int vesselIndex = 0;
Expand All @@ -335,45 +330,19 @@ public static void FindVesselsLandedAtKK(FlightState flightState, string landedA
continue;
}

if (launchSite == null || launchSite.isSquad)
{
if (vessel.landedAt.Contains(landedAt))
{
count++;
name = vessel.vesselName;
idx = vesselIndex;
vType = vessel.vesselType;
break;
}
}
else
if (VesselAtLaunchSite(vessel, launchSite, maxDistance, landedAt))
{
//if (vessel.situation == Vessel.Situations.SPLASHED || vessel.situation == Vessel.Situations.LANDED || vessel.situation == Vessel.Situations.PRELAUNCH )
// {
//// Log.Normal("Body: "+ FlightGlobals.Bodies[vessel.orbitSnapShot.ReferenceBodyIndex].name);
CelestialBody body = FlightGlobals.Bodies[vessel.orbitSnapShot.ReferenceBodyIndex];
Vector3 position = body.GetWorldSurfacePosition(vessel.latitude, vessel.longitude, vessel.altitude);
float distance = Vector3.Distance(position, launchSite.staticInstance.gameObject.transform.position);
//Log.Normal("Vessel with distance: " + distance);
if (distance < maxDistance)
{
Log.Normal("Found Vessel at Launchsite with distance: " + distance);
count++;
name = vessel.vesselName;
idx = vesselIndex;
vType = vessel.vesselType;
break;
}
//}
count++;
name = vessel.vesselName;
idx = vesselIndex;
vType = vessel.vesselType;
break;
}
vesselIndex++;
}
}
}




public static List<ProtoVessel> FindVesselsLandedAt2(FlightState flightState, string landedAt)
{
float maxDistance = 100f;
Expand All @@ -389,38 +358,109 @@ public static List<ProtoVessel> FindVesselsLandedAt2(FlightState flightState, st
continue;
}

if (launchSite == null || launchSite.isSquad)
if (VesselAtLaunchSite(vessel, launchSite, maxDistance, landedAt))
{
if (vessel.landedAt.Contains(landedAt))
{
list.Add(vessel);
list.Add(vessel);
}
}
}
return list;
}

}
public static bool VesselAtLaunchSite(ProtoVessel vessel, KKLaunchSite launchSite, float maxDistance, string landedAt)
{
if (launchSite == null || launchSite.isSquad)
{
if (vessel.landedAt.Contains(landedAt))
{
return true;
}
}

if (vessel.orbitSnapShot.ReferenceBodyIndex == launchSite.body.flightGlobalsIndex)
{
if (vessel.situation == Vessel.Situations.SPLASHED || vessel.situation == Vessel.Situations.LANDED || vessel.situation == Vessel.Situations.PRELAUNCH)
{
CelestialBody body = FlightGlobals.Bodies[vessel.orbitSnapShot.ReferenceBodyIndex];
Vector3 position = body.GetWorldSurfacePosition(vessel.latitude, vessel.longitude, vessel.altitude);
float distance = Vector3.Distance(position, launchSite.staticInstance.gameObject.transform.position);

if (body == null)
{
Log.Normal("Could not find body for vessel");
return false;
}
else

if (body != launchSite.body)
{
Log.Normal("Vessel is on different body than Launchsite");
return false;
}

if (distance == 0)
{
//if (vessel.situation == Vessel.Situations.SPLASHED || vessel.situation == Vessel.Situations.LANDED || vessel.situation == Vessel.Situations.PRELAUNCH)
//{
CelestialBody body = FlightGlobals.Bodies[vessel.orbitSnapShot.ReferenceBodyIndex];
Vector3 position = body.GetWorldSurfacePosition(vessel.latitude, vessel.longitude, vessel.altitude);
float distance = Vector3.Distance(position, launchSite.staticInstance.transform.position);
Log.Normal("Vessel has same position as Launchsite, probably floating point error. Using gps checks instead.");

if (distance < maxDistance)
Vector2d tol = GetLatLonTolerance(launchSite.staticInstance.groupCenter.RefLatitude, launchSite.staticInstance.groupCenter.RefLongitude, maxDistance, body.Radius + launchSite.staticInstance.groupCenter.RadiusOffset);

if (Math.Abs(vessel.latitude - launchSite.staticInstance.groupCenter.RefLatitude) > tol.y || Math.Abs(vessel.longitude - launchSite.staticInstance.groupCenter.RefLongitude) > tol.x)
{
Log.Normal("Vessel is outside lat/lon tolerance");
Log.Debug($"Lat/Lon tolerance: {tol.y}, {tol.x}");
Log.Debug($"Vessel Lat/Lon: {vessel.latitude}, {vessel.longitude}");
Log.Debug($"Launchsite Lat/Lon: {launchSite.staticInstance.groupCenter.RefLatitude}, {launchSite.staticInstance.groupCenter.RefLongitude}");
return false;
}
else
{
Log.Normal("Found Vessel at Launchsite with distance: " + distance);
list.Add(vessel);
Log.Normal("Found Vessel at Launchsite with:");
Log.Debug($"Lat/Lon tolerance: {tol.y}, {tol.x}");
Log.Debug($"Vessel Lat/Lon: {vessel.latitude}, {vessel.longitude}");
Log.Debug($"Launchsite Lat/Lon: {launchSite.staticInstance.groupCenter.RefLatitude}, {launchSite.staticInstance.groupCenter.RefLongitude}");
return true;
}
//}
}

else if (distance < maxDistance)
{
Log.Normal("Found Vessel at Launchsite with distance: " + distance);
return true;
}
}

}
return list;
return false;
}

public static Vector2d GetLatLonTolerance(double lat, double lon, double squareSize, double bodySize)
{
Vector2d dest(double latRad, double lonRad, double bearingRad, double angDist)
{
Vector2d res = new Vector2d(0, 0);

}
// lon = x, lat = y
res.x = Math.Asin(Math.Sin(latRad) * Math.Cos(angDist) + Math.Cos(latRad) * Math.Sin(angDist) * Math.Cos(bearingRad));
res.y = lonRad + Math.Atan2(Math.Sin(bearingRad) * Math.Sin(angDist) * Math.Cos(latRad), Math.Cos(angDist) - Math.Sin(latRad) * Math.Sin(res.x));

res.y = (res.y + Math.PI) % (2 * Math.PI) - Math.PI;

return res;
}

double s = squareSize / 2d;

double latRads = lat * Math.PI / 180d;
double lonRads = lon * Math.PI / 180d;
double delta = s / bodySize;

Vector2d north = dest(latRads, lonRads, 0d, delta);
double deltaLat = Math.Abs(north.x - latRads) * 180d / Math.PI;

Vector2d east = dest(latRads, lonRads, Math.PI / 2d, delta);
double deltaLon = Math.Abs(east.y - lonRads) * 180d / Math.PI;

deltaLat = Math.Min(0.2, deltaLat);
deltaLon = Math.Min(0.2, deltaLon);

return new Vector2d(deltaLon, deltaLat);
}
}
}
1 change: 1 addition & 0 deletions src/Core/LaunchSites/LaunchSiteManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ public static void RegisterLaunchSite(KKLaunchSite site)
{

Log.Error("Launch site " + site.LaunchSiteName + " already exists.");
return;
}


Expand Down
5 changes: 5 additions & 0 deletions src/Core/StaticObjects/StaticInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,11 @@ internal void Destroy()
{
if (StaticsEditorGUI.selectedObject == this) StaticsEditorGUI.selectedObject = null;

if (launchSite != null)
{
LaunchSiteManager.DeleteLaunchSite(launchSite);
}

if (groupCenter != null)
{
groupCenter.RemoveInstance(this);
Expand Down
16 changes: 2 additions & 14 deletions src/Modules/Career/CareerScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,8 @@ public class KerbalKonstructsSettings : ScenarioModule
/// <param name="node">The name of the config node</param>
public override void OnLoad(ConfigNode node)
{
// check if we have been in the mainmenue before (gameTime == -1) or if we saved just before we load (scene switch)
if (KerbalKonstructs.gameTime == -1d || KerbalKonstructs.gameTime > HighLogic.CurrentGame.UniversalTime)
{
}
else
{
return;
}
// check if we have been in the mainmenue before (gameTime == -1)
if (KerbalKonstructs.gameTime != -1d) return;

CareerMapDecals.LoadDecals(node);

Expand Down Expand Up @@ -64,11 +58,8 @@ public override void OnLoad(ConfigNode node)

KerbalKonstructs.instance.LoadKKConfig(node);

//if (CareerUtils.isCareerGame)
//{
Log.Normal("KKScenario loading facility states");
CareerState.Load(node);
//}

ConnectionManager.LoadGroundStations();

Expand All @@ -92,11 +83,8 @@ public override void OnSave(ConfigNode node)

KerbalKonstructs.instance.SaveKKConfig(node);

//if (CareerUtils.isCareerGame)
//{
Log.Normal("KKScenario saving career state");
CareerState.Save(node);
//}

CareerMapDecals.SaveDecals(node);

Expand Down
6 changes: 3 additions & 3 deletions src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
[assembly: AssemblyFileVersion("@MAJOR@.@MINOR@.@PATCH@.@BUILD@")]
[assembly: AssemblyInformationalVersion("@MAJOR@.@MINOR@.@PATCH@.@BUILD@")]
#else
[assembly: AssemblyFileVersion("1.11.0.0")]
[assembly: AssemblyInformationalVersion("1.11.0.0")]
[assembly: AssemblyFileVersion("1.12.0.0")]
[assembly: AssemblyInformationalVersion("1.12.0.0")]
#endif

[assembly: KSPAssembly("KerbalKonstructs", 1, 11, 0)]
[assembly: KSPAssembly("KerbalKonstructs", 1, 12, 0)]