Skip to content

Commit

Permalink
Fixes #1565 - moved the "invalid target" logic.
Browse files Browse the repository at this point in the history
The problem was caused by having the "invalid target" logic at too "deep" a level of the code, such that not only was it preventing you from selecting your own ship as a your target, but it was also preventing you from EVER using your own ship in the Vessel() constructor at all.

I had to move the logic that prevents invalid targets a bit higher up, getting it out of the low level constructor so it's possible to construct a Vessel() of your own ship.
  • Loading branch information
Dunbaratu committed Sep 4, 2016
1 parent 20cba13 commit 2f845e2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
14 changes: 14 additions & 0 deletions src/kOS.Safe/Exceptions/KOSInvalidTargetException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace kOS.Safe.Exceptions
{
/// <summary>
/// Description of KOSInvalidTargetException.
/// </summary>
public class KOSInvalidTargetException : KOSException
{
public KOSInvalidTargetException(string msg) : base(msg)
{
}
}
}
1 change: 1 addition & 0 deletions src/kOS.Safe/kOS.Safe.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Compile Include="Compilation\CompiledObject.cs" />
<Compile Include="Compilation\CompilerOptions.cs" />
<Compile Include="Exceptions\KOSAtmosphereObsoletionException.cs" />
<Compile Include="Exceptions\KOSInvalidTargetException.cs" />
<Compile Include="Exceptions\KOSObsoletionException.cs" />
<Compile Include="Exceptions\KOSPatchesObsoletionException.cs" />
<Compile Include="Exceptions\KOSVolumeOutOfRangeException.cs" />
Expand Down
6 changes: 3 additions & 3 deletions src/kOS/Binding/MissionSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override void AddTo(SharedObjects shared)
var targetable = val as IKOSTargetable;
if (targetable != null)
{
VesselUtils.SetTarget(targetable);
VesselUtils.SetTarget(targetable, shared.Vessel);
return;
}
Expand All @@ -31,14 +31,14 @@ public override void AddTo(SharedObjects shared)
var body = VesselUtils.GetBodyByName(val.ToString());
if (body != null)
{
VesselUtils.SetTarget(body);
VesselUtils.SetTarget(body, shared.Vessel);
return;
}
var vessel = VesselUtils.GetVesselByName(val.ToString(), shared.Vessel);
if (vessel != null)
{
VesselUtils.SetTarget(vessel);
VesselUtils.SetTarget(vessel, shared.Vessel);
return;
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/kOS/Utilities/VesselUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public static double GetMaxThrust(Vessel vessel, double atmPressure = -1.0)

private static Vessel TryGetVesselByName(string name, Vessel origin)
{
return FlightGlobals.Vessels.FirstOrDefault(v => v != origin && v.vesselName.ToUpper() == name.ToUpper());
return FlightGlobals.Vessels.FirstOrDefault(v => v.vesselName.ToUpper() == name.ToUpper());
}

public static CelestialBody GetBodyByName(string name)
Expand All @@ -144,20 +144,25 @@ public static Vessel GetVesselByName(string name, Vessel origin)
return vessel;
}

public static void SetTarget(IKOSTargetable val)
public static void SetTarget(IKOSTargetable val, Vessel currentVessel)
{
if (val.Target != null)
{
SetTarget(val.Target);
SetTarget(val.Target, currentVessel);
}
else
{
throw new Exception("Error on targeting " + val);
}
}

public static void SetTarget(ITargetable val)
public static void SetTarget(ITargetable val, Vessel currentVessel)
{
if (val is Vessel && val == currentVessel)
throw new kOS.Safe.Exceptions.KOSInvalidTargetException("A ship cannot set TARGET to itself.");
else if (val is ITargetable && ((ITargetable)val).GetVessel() == currentVessel)
throw new kOS.Safe.Exceptions.KOSInvalidTargetException("A ship cannot set TARGET to a part of itself.");

FlightGlobals.fetch.SetVesselTarget(val);
}

Expand Down

0 comments on commit 2f845e2

Please sign in to comment.