forked from Nivekk/KOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVesselTarget.cs
More file actions
117 lines (101 loc) · 4.86 KB
/
VesselTarget.cs
File metadata and controls
117 lines (101 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace kOS
{
public class VesselTarget : SpecialValue
{
public ExecutionContext context;
public Vessel target;
public static String[] ShortCuttableShipSuffixes;
static VesselTarget()
{
ShortCuttableShipSuffixes = new String[]
{
"HEADING", "PROGRADE", "RETROGRADE", "FACING", "MAXTHRUST", "VELOCITY", "GEOPOSITION", "LATITUDE", "LONGITUDE",
"UP", "NORTH", "BODY", "ANGULARMOMENTUM", "ANGULARVEL", "MASS", "VERTICALSPEED", "SURFACESPEED", "AIRSPEED", "VESSELNAME",
"ALTITUDE", "APOAPSIS", "PERIAPSIS", "SENSOR"
};
}
public VesselTarget(Vessel target, ExecutionContext context)
{
this.context = context;
this.target = target;
}
public bool IsInRange(double range)
{
if (GetDistance() <= range) return true;
return false;
}
public double GetDistance()
{
return Vector3d.Distance(context.Vessel.GetWorldPos3D(), target.GetWorldPos3D());
}
public override string ToString()
{
return "VESSEL(\"" + target.vesselName + "\")";
}
public Direction GetPrograde()
{
var up = (target.findLocalMOI(target.findWorldCenterOfMass()) - target.mainBody.position).normalized;
Direction d = new Direction();
d.Rotation = Quaternion.LookRotation(target.orbit.GetVel().normalized, up);
return d;
}
public Direction GetRetrograde()
{
var up = (target.findLocalMOI(target.findWorldCenterOfMass()) - target.mainBody.position).normalized;
Direction d = new Direction();
d.Rotation = Quaternion.LookRotation(target.orbit.GetVel().normalized * -1, up);
return d;
}
public Direction GetFacing()
{
var facing = target.transform.up;
return new Direction(new Vector3d(facing.x, facing.y, facing.z).normalized, false);
}
public override object GetSuffix(string suffixName)
{
if (suffixName == "DIRECTION")
{
var vector = (target.GetWorldPos3D() - context.Vessel.GetWorldPos3D());
return new Direction(vector, false);
}
if (suffixName == "DISTANCE") return (float)GetDistance();
if (suffixName == "BEARING") return VesselUtils.GetTargetBearing(context.Vessel, target);
if (suffixName == "HEADING") return VesselUtils.GetTargetHeading(context.Vessel, target);
if (suffixName == "PROGRADE") return GetPrograde();
if (suffixName == "RETROGRADE") return GetRetrograde();
if (suffixName == "MAXTHRUST") return VesselUtils.GetMaxThrust(target);
if (suffixName == "VELOCITY") return new VesselVelocity(target);
if (suffixName == "GEOPOSITION") return new GeoCoordinates(target);
if (suffixName == "LATITUDE") return VesselUtils.GetVesselLattitude(target);
if (suffixName == "LONGITUDE") return VesselUtils.GetVesselLongitude(target);
if (suffixName == "FACING") return GetFacing();
if (suffixName == "UP") return new Direction(target.upAxis, false);
if (suffixName == "NORTH") return new Direction(VesselUtils.GetNorthVector(target), false);
if (suffixName == "BODY") return target.mainBody.bodyName;
if (suffixName == "ANGULARMOMENTUM") return new Direction(target.angularMomentum, true);
if (suffixName == "ANGULARVEL") return new Direction(target.angularVelocity, true);
if (suffixName == "MASS") return target.GetTotalMass();
if (suffixName == "VERTICALSPEED") return target.verticalSpeed;
if (suffixName == "SURFACESPEED") return target.horizontalSrfSpeed;
if (suffixName == "AIRSPEED") return (target.orbit.GetVel() - FlightGlobals.currentMainBody.getRFrmVel(target.GetWorldPos3D())).magnitude; //the velocity of the vessel relative to the air);
if (suffixName == "VESSELNAME") return target.vesselName;
if (suffixName == "ALTITUDE") return target.altitude;
if (suffixName == "APOAPSIS") return target.orbit.ApA;
if (suffixName == "PERIAPSIS") return target.orbit.PeA;
if (suffixName == "SENSOR") return new VesselSensors(target);
if (suffixName == "TERMVELOCITY") return VesselUtils.GetTerminalVelocity(target);
// Is this a resource?
double dblValue;
if (VesselUtils.TryGetResource(target, suffixName, out dblValue))
{
return dblValue;
}
return base.GetSuffix(suffixName);
}
}
}