Esempio n. 1
0
        public SpaceCenter getSpaceCenter()
        {
            if (_spaceCenter == null)
            {
                _spaceCenter            = staticInstance.gameObject.AddComponent <SpaceCenter>();
                _spaceCenter.cb         = staticInstance.CelestialBody;
                _spaceCenter.name       = SpaceCenterName;
                _spaceCenter.AreaRadius = 3000;
//                _spaceCenter.spaceCenterAreaTrigger = new Collider();

                _spaceCenter.SpaceCenterTransform = staticInstance.gameObject.transform;

                Log.Normal("SpaceCenter Position: " + _spaceCenter.SpaceCenterTransform);


                FieldInfo Latitude = _spaceCenter.GetType().GetField("latitude", BindingFlags.NonPublic | BindingFlags.Instance);
                Latitude.SetValue(_spaceCenter, staticInstance.RefLatitude);
                FieldInfo Longitude = _spaceCenter.GetType().GetField("longitude", BindingFlags.NonPublic | BindingFlags.Instance);
                Longitude.SetValue(_spaceCenter, staticInstance.RefLongitude);


                FieldInfo SrfNVector = _spaceCenter.GetType().GetField("srfNVector", BindingFlags.NonPublic | BindingFlags.Instance);
                SrfNVector.SetValue(_spaceCenter, _spaceCenter.cb.GetRelSurfaceNVector(_spaceCenter.Latitude, _spaceCenter.Longitude));

                FieldInfo altitudeField = _spaceCenter.GetType().GetField("altitude", BindingFlags.NonPublic | BindingFlags.Instance);
                altitudeField.SetValue(_spaceCenter, staticInstance.RadiusOffset);
            }
            else
            {
                // Debug.Log("KK: getSpaceCenter was not null.");
            }

            return(_spaceCenter);
        }
Esempio n. 2
0
        /// <summary>
        /// StageRecovery handle. This is called first.
        /// </summary>
        /// <param name="data"></param>
        public static void PreRecovery(Vessel vessel)
        {
            Log.Normal("OnVesselRecoveryRequested");
            if (!KerbalKonstructs.instance.disableRemoteRecovery)
            {
                if (MiscUtils.CareerStrategyEnabled(HighLogic.CurrentGame))
                {
                    Log.Normal("OnVesselRecoveryRequested is career");
                    // Change the Space Centre to the nearest open base
                    double dist         = 0d;
                    double smallestDist = 0d;
                    string openCloseState;

                    SpaceCenter spaceCenter        = null;
                    SpaceCenter closestSpaceCenter = SpaceCenter.Instance;

                    smallestDist = SpaceCenter.Instance.GreatCircleDistance(SpaceCenter.Instance.cb.GetRelSurfaceNVector(vessel.latitude, vessel.longitude));
                    Log.Normal("Distance to KSC is " + smallestDist);

                    foreach (CustomSpaceCenter csc in SpaceCenterManager.spaceCenters)
                    {
                        // ASH Get openclosestate of launchsite with same name as space centre
                        openCloseState = csc.staticInstance.launchSite.OpenCloseState;

                        StaticInstance myBase = csc.staticInstance;
                        if (csc.staticInstance.launchSite.RecoveryFactor == 0)
                        {
                            continue;
                        }
                        spaceCenter = csc.getSpaceCenter();
                        dist        = spaceCenter.GreatCircleDistance(spaceCenter.cb.GetRelSurfaceNVector(vessel.latitude, vessel.longitude));

                        if (dist < smallestDist)
                        {
                            if (csc.staticInstance.launchSite.isOpen)
                            {
                                closestSpaceCenter = spaceCenter;
                                smallestDist       = dist;
                                // Debug.Log("KK: closest updated to " + closest.SpaceCenterName + ", distance " + smallestDist);
                            }
                        }
                    }

                    // set the Spacecenter to the closest SpaceCenter, because StageRecovery uses this. We revert this later on the PostRecovery function
                    SpaceCenter.Instance = closestSpaceCenter;
                    Log.Normal("SpaceCenter set to: " + closestSpaceCenter.name);

                    if (SpaceCenter.Instance == null)
                    {
                        Log.Normal("no Spacecenter for recovery found");
                        SpaceCenter.Instance = SpaceCenterManager.KSC;
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// StageRecovery handle. This is called first.
        /// </summary>
        /// <param name="data"></param>
        public static void PreRecovery(Vessel vessel)
        {
            Log.Normal("OnVesselRecoveryRequested");
            if (!KerbalKonstructs.instance.disableRemoteRecovery && CareerUtils.isCareerGame)
            {
                Log.Normal("OnVesselRecoveryRequested is career");
                // Change the Space Centre to the nearest open base
                double dist = 0d;

                SpaceCenter spaceCenter        = null;
                SpaceCenter closestSpaceCenter = SpaceCenter.Instance;

                double smallestDist = SpaceCenterManager.KSC.GreatCircleDistance(vessel.mainBody.GetRelSurfaceNVector(vessel.latitude, vessel.longitude));
                Log.Normal("Distance to KSC is " + smallestDist);

                foreach (CustomSpaceCenter csc in SpaceCenterManager.spaceCenters)
                {
                    try
                    {
                        //Log.Normal("Checking LS: " + csc.SpaceCenterName);
                        if (!csc.isOpen)
                        {
                            //Log.Normal("Ignoring closed SC: " + csc.SpaceCenterName);
                            continue;
                        }

                        spaceCenter = csc.GetSpaceCenter();
                        dist        = spaceCenter.GreatCircleDistance(spaceCenter.cb.GetRelSurfaceNVector(vessel.latitude, vessel.longitude));
                        Log.Normal("distance is: " + dist);
                        if (dist < smallestDist)
                        {
                            closestSpaceCenter = spaceCenter;
                            smallestDist       = dist;
                            Log.Normal("KK: closest updated to " + csc.SpaceCenterName + ", distance " + smallestDist);
                        }
                    }
                    catch
                    {
                        Log.UserWarning("Error Processing... " + csc.SpaceCenterName);
                    }
                }

                // set the Spacecenter to the closest SpaceCenter, because StageRecovery uses this. We revert this later on the PostRecovery function
                SpaceCenter.Instance = closestSpaceCenter;
                Log.Normal("SpaceCenter set to: " + closestSpaceCenter.name);

                if (SpaceCenter.Instance == null)
                {
                    Log.Normal("no Spacecenter for recovery found");
                    SpaceCenter.Instance = SpaceCenterManager.KSC;
                }
            }
        }
        // Get nearest base, either open or closed, and the range to it
        public static void getNearestBase(Vector3 position, out string sBase, out float flRange, out LaunchSite lSite)
        {
            SpaceCenter KSC          = SpaceCenter.Instance;
            var         smallestDist = Vector3.Distance(KSC.gameObject.transform.position, position);
            string      sNearestBase = "";
            LaunchSite  lTargetSite  = null;
            LaunchSite  lKSC         = null;

            List <LaunchSite> basesites = LaunchSiteManager.getLaunchSites();

            foreach (LaunchSite site in basesites)
            {
                if (site.GameObject == null)
                {
                    continue;
                }
                //if (site.recoveryfactor == 0) continue;

                var radialposition = site.GameObject.transform.position;
                var dist           = Vector3.Distance(position, radialposition);

                if (site.name == "Runway" || site.name == "LaunchPad")
                {
                    lKSC = site;
                }
                else
                {
                    if ((float)dist < (float)smallestDist)
                    {
                        {
                            sNearestBase = site.name;
                            smallestDist = dist;
                            lTargetSite  = site;
                        }
                    }
                }
            }

            if (sNearestBase == "")
            {
                sNearestBase = "KSC";
                lTargetSite  = lKSC;
            }

            RangeNearestBase = (float)smallestDist;

            sBase   = sNearestBase;
            flRange = RangeNearestBase;
            lSite   = lTargetSite;
        }
Esempio n. 5
0
 public SpaceCenter GetSpaceCenter()
 {
     if (_spaceCenter == null)
     {
         _spaceCenter = gameObject.AddComponent <SpaceCenter>();
         gameObject.SetActive(true);
         _spaceCenter.cb                     = staticInstance.CelestialBody;
         _spaceCenter.name                   = SpaceCenterName;
         _spaceCenter.AreaRadius             = 3000;
         _spaceCenter.spaceCenterAreaTrigger = SpaceCenterManager.KSC.spaceCenterAreaTrigger;
         _spaceCenter.enabled                = true;
         _spaceCenter.Start();
     }
     return(_spaceCenter);
 }
        public static WorldCoordinates GetFacilityCoordinates <T>() where T : SpaceCenterBuilding
        {
            var facilityVectorPosition =
                ((T)(SpaceCenter.FindObjectOfType(typeof(T))))
                .transform
                .position;

            var facilityCoordinates = new WorldCoordinates
            {
                Latitude  = Homeworld.GetLatitude(facilityVectorPosition),
                Longitude = Homeworld.GetLongitude(facilityVectorPosition),
                Altitude  = Homeworld.GetAltitude(facilityVectorPosition),
                World     = Homeworld,
            };

            return(facilityCoordinates);
        }
Esempio n. 7
0
        KerbalKonstructsInfo GetKerbalKonstructsSpaceCenterInfo(CommonInfo info, Type scManager)
        {
            Vector3     position       = info.Vessel.gameObject.transform.position;
            SpaceCenter closestCenter  = null;
            float       distance       = 1000.0f;
            float       recoveryFactor = 0.0f;
            float       recoveryRange  = 0.0f;
            string      baseName       = null;
            var         args           = new object[] { position, closestCenter, distance, recoveryFactor, recoveryRange, baseName };

            Reflect.StaticVoidMethod(scManager, "getClosestSpaceCenter", args);
            var kkInfo = new KerbalKonstructsInfo
            {
                SpaceCenter = (SpaceCenter)args[1],
                Distance    = (float)args[2],
                BaseName    = args[5].ToString()
            };

            if (kkInfo.BaseName == "KSC")
            {
                kkInfo.BaseName = Historian.Instance.GetConfiguration().DefaultSpaceCenterName;
            }
            return(kkInfo);
        }
Esempio n. 8
0
        public SpaceCenter getSpaceCenter()
        {
            if (spaceCenter == null)
            {
                spaceCenter      = gameObject.AddComponent <SpaceCenter>();
                spaceCenter.cb   = (CelestialBody)staticObject.getSetting("CelestialBody");
                spaceCenter.name = SpaceCenterName;

                // Debug.Log("KK: getSpaceCenter set spaceCenter.name to " + SpaceCenterName);

                FieldInfo Latitude = spaceCenter.GetType().GetField("latitude", BindingFlags.NonPublic | BindingFlags.Instance);
                Latitude.SetValue(spaceCenter, spaceCenter.cb.GetLatitude(gameObject.transform.position));
                FieldInfo Longitude = spaceCenter.GetType().GetField("longitude", BindingFlags.NonPublic | BindingFlags.Instance);
                Longitude.SetValue(spaceCenter, spaceCenter.cb.GetLongitude(gameObject.transform.position));
                FieldInfo SrfNVector = spaceCenter.GetType().GetField("srfNVector", BindingFlags.NonPublic | BindingFlags.Instance);
                SrfNVector.SetValue(spaceCenter, spaceCenter.cb.GetRelSurfaceNVector(spaceCenter.Latitude, spaceCenter.Longitude));
            }
            else
            {
                // Debug.Log("KK: getSpaceCenter was not null.");
            }

            return(spaceCenter);
        }
		public SpaceCenter getSpaceCenter()
		{
			if (spaceCenter == null)
			{
				spaceCenter = gameObject.AddComponent<SpaceCenter>();
				spaceCenter.cb = (CelestialBody)staticObject.getSetting("CelestialBody");
				spaceCenter.name = SpaceCenterName;

				// Debug.Log("KK: getSpaceCenter set spaceCenter.name to " + SpaceCenterName);

				FieldInfo lat = spaceCenter.GetType().GetField("\u0002", BindingFlags.NonPublic | BindingFlags.Instance);
				lat.SetValue(spaceCenter, spaceCenter.cb.GetLatitude(gameObject.transform.position));
				FieldInfo lon = spaceCenter.GetType().GetField("\u0003", BindingFlags.NonPublic | BindingFlags.Instance);
				lon.SetValue(spaceCenter, spaceCenter.cb.GetLongitude(gameObject.transform.position));
				FieldInfo srfVector = spaceCenter.GetType().GetField("\u0004", BindingFlags.NonPublic | BindingFlags.Instance);
				srfVector.SetValue(spaceCenter, spaceCenter.cb.GetRelSurfaceNVector(spaceCenter.Latitude, spaceCenter.Longitude));
			}
			else
			{
				// Debug.Log("KK: getSpaceCenter was not null.");
			}

			return spaceCenter;
		}
        // Get the nearest open base and range to it
        public static void getNearestOpenBase(Vector3 position, out string sBase, out float flRange, out LaunchSite lNearest)
        {
            SpaceCenter KSC             = SpaceCenter.Instance;
            var         smallestDist    = Vector3.Distance(KSC.gameObject.transform.position, position);
            string      sNearestBase    = "";
            string      sOpenCloseState = "";
            LaunchSite  lNearestBase    = null;
            LaunchSite  lKSC            = null;

            List <LaunchSite> basesites = LaunchSiteManager.getLaunchSites();

            foreach (LaunchSite site in basesites)
            {
                sOpenCloseState = site.openclosestate;

                if (!MiscUtils.isCareerGame())
                {
                    sOpenCloseState = "Open";
                }

                //if (site.recoveryfactor == 0) continue;

                if (sOpenCloseState == "Open")
                {
                    var radialposition = site.GameObject.transform.position;
                    var dist           = Vector3.Distance(position, radialposition);

                    if (site.name == "Runway")
                    {
                        if (lNearestBase == null)
                        {
                            lNearestBase = site;
                        }
                        lKSC = site;
                    }
                    else
                    if (site.name != "LaunchPad")
                    {
                        if ((float)dist < (float)smallestDist)
                        {
                            {
                                sNearestBase = site.name;
                                lNearestBase = site;
                                smallestDist = dist;
                            }
                        }
                    }
                    else
                    {
                        lKSC = site;
                    }
                }
            }

            if (sNearestBase == "")
            {
                sNearestBase = "KSC";
                lNearestBase = lKSC;
            }

            RangeNearestOpenBase = (float)smallestDist;

            // Air traffic control messaging
            if (KerbalKonstructs.instance.enableATC)
            {
                if (sNearestBase != NearestOpenBase)
                {
                    if (RangeNearestOpenBase < 25000)
                    {
                        NearestOpenBase = sNearestBase;
                        MessageSystemButton.MessageButtonColor color = MessageSystemButton.MessageButtonColor.BLUE;
                        MessageSystem.Message m = new MessageSystem.Message("KK ATC", "You have entered the airspace of " + sNearestBase + " ATC. Please keep this channel open and obey all signal lights. Thank you. " + sNearestBase + " Air Traffic Control out.", color, MessageSystemButton.ButtonIcons.MESSAGE);
                        MessageSystem.Instance.AddMessage(m);
                    }
                    else
                    if (NearestOpenBase != "")
                    {
                        // you have left ...
                        MessageSystemButton.MessageButtonColor color = MessageSystemButton.MessageButtonColor.GREEN;
                        MessageSystem.Message m = new MessageSystem.Message("KK ATC", "You are now leaving the airspace of " + sNearestBase + ". Safe journey. " + sNearestBase + " Air Traffic Control out.", color, MessageSystemButton.ButtonIcons.MESSAGE);
                        MessageSystem.Instance.AddMessage(m);
                        NearestOpenBase = "";
                    }
                }
            }

            sBase    = sNearestBase;
            flRange  = RangeNearestOpenBase;
            lNearest = lNearestBase;
        }
Esempio n. 11
0
        public static void getClosestSpaceCenter(Vector3 position, out SpaceCenter ClosestCenter, out float ClosestDistance,
                                                 out float RecoveryFactor, out float RecoveryRange, out string BaseName)
        {
            CustomSpaceCenter closest = null;

            float smallestDist = Vector3.Distance(KSC.gameObject.transform.position, position);
            // Debug.Log("KK: Distance to KSC is " + smallestDist);

            bool isCareer = false;

            if (HighLogic.CurrentGame.Mode == Game.Modes.CAREER)
            {
                if (!KerbalKonstructs.instance.disableCareerStrategyLayer)
                {
                    isCareer = true;
                    PersistenceFile <LaunchSite> .LoadList(LaunchSiteManager.AllLaunchSites, "LAUNCHSITES", "KK");
                }
            }

            string sOpenCloseState  = "Closed";
            string sBaseName        = "";
            float  fMyBaseRecovFact = 0f;
            float  fMyBaseRecovRang = 0f;

            foreach (CustomSpaceCenter csc in spaceCenters)
            {
                if (isCareer)
                {
                    string OpenCloseState;
                    float  OpenCost;
                    // ASH Get openclosestate of launchsite with same name as space centre
                    LaunchSiteManager.getSiteOpenCloseState(csc.SpaceCenterName, out OpenCloseState, out OpenCost);
                    sOpenCloseState = OpenCloseState;
                }

                StaticObject myBase = csc.getStaticObject();
                if ((float)myBase.getSetting("RecoveryFactor") == 0)
                {
                    continue;
                }

                float dist = Vector3.Distance(position, csc.getStaticObject().gameObject.transform.position);

                if (dist < smallestDist)
                {
                    bool bBaseIsOpen = true;
                    if (sOpenCloseState == "Closed" || sOpenCloseState == "ClosedLocked" || sOpenCloseState == "OpenLocked")
                    {
                        bBaseIsOpen = false;
                    }

                    if (isCareer && !bBaseIsOpen)
                    {
                    }
                    else
                    {
                        closest          = csc;
                        smallestDist     = dist;
                        fMyBaseRecovFact = (float)myBase.getSetting("RecoveryFactor");
                        fMyBaseRecovRang = (float)myBase.getSetting("RecoveryRange");
                        sBaseName        = (string)myBase.getSetting("LaunchSiteName");
                        // Debug.Log("KK: closest updated to " + closest.SpaceCenterName + ", distance " + smallestDist);
                    }
                }
            }

            SpaceCenter sc;

            if (closest == null)
            {
                sc = null;
            }
            else
            {
                // Debug.Log("KK: closest is " + closest.SpaceCenterName);
                sc = closest.getSpaceCenter();
            }

            // Debug.Log("KK: smallestDist is " + smallestDist);
            // Debug.Log("KK: returning closest space centre: " + sc.name);


            if (smallestDist < 1)
            {
                smallestDist = 0;
            }
            if (sc == null)
            {
                sc = KSC;
                fMyBaseRecovFact = 100;
                sBaseName        = "KSC";
            }

            ClosestCenter   = sc;
            ClosestDistance = smallestDist;
            RecoveryFactor  = fMyBaseRecovFact;
            RecoveryRange   = fMyBaseRecovRang;
            BaseName        = sBaseName;
        }
Esempio n. 12
0
 public static void setKSC()
 {
     KSC = SpaceCenter.Instance;
 }
        public static void getClosestSpaceCenter(Vector3 position, out SpaceCenter ClosestCenter, out float ClosestDistance, 
			out float RecoveryFactor, out float RecoveryRange, out string BaseName)
        {
            CustomSpaceCenter closest = null;

            float smallestDist = Vector3.Distance(KSC.gameObject.transform.position, position);
            // Debug.Log("KK: Distance to KSC is " + smallestDist);

            bool isCareer = false;

            if (HighLogic.CurrentGame.Mode == Game.Modes.CAREER)
            {
                if (!KerbalKonstructs.instance.disableCareerStrategyLayer)
                {
                    isCareer = true;
                    PersistenceFile<LaunchSite>.LoadList(LaunchSiteManager.AllLaunchSites, "LAUNCHSITES", "KK");
                }
            }

            string sOpenCloseState = "Closed";
            string sBaseName = "";
            float fMyBaseRecovFact = 0f;
            float fMyBaseRecovRang = 0f;

            foreach (CustomSpaceCenter csc in spaceCenters)
            {
                if (isCareer)
                {
                    string OpenCloseState;
                    float OpenCost;
                    // ASH Get openclosestate of launchsite with same name as space centre
                    LaunchSiteManager.getSiteOpenCloseState(csc.SpaceCenterName, out OpenCloseState, out OpenCost);
                    sOpenCloseState = OpenCloseState;
                }

                StaticObject myBase = csc.getStaticObject();
                if ((float)myBase.getSetting("RecoveryFactor") == 0) continue;

                float dist = Vector3.Distance(position, csc.getStaticObject().gameObject.transform.position);

                if (dist < smallestDist)
                {
                    bool bBaseIsOpen = true;
                    if (sOpenCloseState == "Closed" || sOpenCloseState == "ClosedLocked" || sOpenCloseState == "OpenLocked") bBaseIsOpen = false;

                    if (isCareer && !bBaseIsOpen)
                    { }
                    else
                    {
                        closest = csc;
                        smallestDist = dist;
                        fMyBaseRecovFact = (float)myBase.getSetting("RecoveryFactor");
                        fMyBaseRecovRang = (float)myBase.getSetting("RecoveryRange");
                        sBaseName = (string)myBase.getSetting("LaunchSiteName");
                        // Debug.Log("KK: closest updated to " + closest.SpaceCenterName + ", distance " + smallestDist);
                    }
                }
            }

            SpaceCenter sc;

            if (closest == null)
                sc = null;
            else
            {
                // Debug.Log("KK: closest is " + closest.SpaceCenterName);
                sc = closest.getSpaceCenter();
            }

            // Debug.Log("KK: smallestDist is " + smallestDist);
            // Debug.Log("KK: returning closest space centre: " + sc.name);

            if (smallestDist < 1) smallestDist = 0;
            if (sc == null)
            {
                sc = KSC;
                fMyBaseRecovFact = 100;
                sBaseName = "KSC";
            }

            ClosestCenter = sc;
            ClosestDistance = smallestDist;
            RecoveryFactor = fMyBaseRecovFact;
            RecoveryRange = fMyBaseRecovRang;
            BaseName = sBaseName;
        }
Esempio n. 14
0
        public static void getClosestSpaceCenter(Vessel vessel, out SpaceCenter closestCenter, out float closestDistance,
                                                 out float RecoveryFactor, out float RecoveryRange, out string BaseName)
        {
            CustomSpaceCenter closest = null;
            SpaceCenter       sc      = null;

            var smallestDist = (float)SpaceCenter.Instance.GreatCircleDistance(SpaceCenter.Instance.cb.GetRelSurfaceNVector(vessel.latitude, vessel.longitude));

            Log.Normal("Distance to KSC is " + smallestDist);

            bool isCareer = CareerUtils.isCareerGame;

            string sBaseName        = "";
            float  fMyBaseRecovFact = 0f;
            float  fMyBaseRecovRang = 0f;


            foreach (CustomSpaceCenter csc in spaceCenters)
            {
                if (csc.staticInstance.launchSite.RecoveryFactor == 0)
                {
                    continue;
                }
                sc = csc.getSpaceCenter();
                //float dist = Vector3.Distance(position, csc.getStaticObject().gameObject.transform.position);
                var dist = (float)sc.GreatCircleDistance(sc.cb.GetRelSurfaceNVector(vessel.latitude, vessel.longitude));

                if (dist < smallestDist)
                {
                    if (isCareer && csc.staticInstance.launchSite.isOpen)
                    {
                        closest          = csc;
                        smallestDist     = dist;
                        fMyBaseRecovFact = csc.staticInstance.launchSite.RecoveryFactor;
                        fMyBaseRecovRang = csc.staticInstance.launchSite.RecoveryRange;
                        sBaseName        = csc.staticInstance.launchSite.LaunchSiteName;
                        // Debug.Log("KK: closest updated to " + closest.SpaceCenterName + ", distance " + smallestDist);
                    }
                }
            }



            if (closest == null)
            {
                sc = null;
            }
            else
            {
                Log.Normal("closest Spacecenter is " + closest.SpaceCenterName);
                sc = closest.getSpaceCenter();
            }

            Log.Normal("smallestDist is " + smallestDist);
            Log.Normal("returning closest space centre: " + sc.name);


            if (smallestDist < 1)
            {
                smallestDist = 0;
            }
            if (sc == null)
            {
                sc = KSC;
                fMyBaseRecovFact = 100;
                sBaseName        = "KSC";
            }

            closestCenter   = sc;
            closestDistance = smallestDist;
            RecoveryFactor  = fMyBaseRecovFact;
            RecoveryRange   = fMyBaseRecovRang;
            BaseName        = sBaseName;
        }
		public static void setKSC()
		{
			KSC = SpaceCenter.Instance;
		}
Esempio n. 16
0
        // Returns the nearest Launchsite to a position and range in m to the Launchsite, regardless of whether it is open or closed
        public static KKLaunchSite getNearestBase(GroupCenter center, Vector3 position)
        {
            SpaceCenter  KSC              = SpaceCenter.Instance;
            var          smallestDist     = Vector3.Distance(KSC.gameObject.transform.position, position);
            var          lastSmallestDist = Vector3.Distance(KSC.gameObject.transform.position, position);
            string       sNearestBase     = "";
            KKLaunchSite lTargetSite      = null;
            KKLaunchSite lLastSite        = null;
            KKLaunchSite lKSC             = null;
            string       sLastNearest     = "";


            foreach (KKLaunchSite site in center.launchsites)
            {
                if (site.staticInstance.gameObject == null)
                {
                    continue;
                }

                var radialposition = site.staticInstance.gameObject.transform.position;
                var dist           = Vector3.Distance(position, radialposition);

                if (radialposition == position)
                {
                    continue;
                }

                if (site.LaunchSiteName == "Runway" || site.LaunchSiteName == "LaunchPad")
                {
                    lKSC = site;
                }
                else
                {
                    if ((float)dist < (float)smallestDist)
                    {
                        sLastNearest     = sNearestBase;
                        lLastSite        = lTargetSite;
                        lastSmallestDist = smallestDist;
                        sNearestBase     = site.LaunchSiteName;
                        smallestDist     = dist;
                        lTargetSite      = site;
                    }
                    else if (dist < lastSmallestDist)
                    {
                        sLastNearest     = site.LaunchSiteName;
                        lastSmallestDist = dist;
                        lLastSite        = site;
                    }
                }
            }

            if (sNearestBase.Length == 0)
            {
                sNearestBase = "KSC";
                lTargetSite  = lKSC;
            }
            if (sLastNearest.Length == 0)
            {
                sLastNearest = "KSC";
                lLastSite    = lKSC;
            }

            rangeNearestBase = (float)smallestDist;

            return(lTargetSite);
        }
Esempio n. 17
0
        // Returns the nearest open Launchsite to a position and range to the Launchsite in m
        // The basic ATC feature is in here for now
        public static void GetNearestOpenBase(Vector3 position, out string sBase, out float flRange, out KKLaunchSite lNearest)
        {
            SpaceCenter  KSC          = SpaceCenter.Instance;
            var          smallestDist = Vector3.Distance(KSC.gameObject.transform.position, position);
            string       sNearestBase = "";
            KKLaunchSite lNearestBase = null;
            KKLaunchSite lKSC         = null;

            foreach (KKLaunchSite site in allLaunchSites)
            {
                if (site.isOpen)
                {
                    var radialposition = site.staticInstance.transform.position;
                    var dist           = Vector3.Distance(position, radialposition);

                    if (site.LaunchSiteName == "Runway")
                    {
                        if (lNearestBase == null)
                        {
                            lNearestBase = site;
                        }

                        lKSC = site;
                    }
                    else
                    if (site.LaunchSiteName != "LaunchPad")
                    {
                        if ((float)dist < (float)smallestDist)
                        {
                            {
                                sNearestBase = site.LaunchSiteName;
                                lNearestBase = site;
                                smallestDist = dist;
                            }
                        }
                    }
                    else
                    {
                        lKSC = site;
                    }
                }
            }

            if (sNearestBase.Length == 0)
            {
                sNearestBase = "KSC";
                lNearestBase = lKSC;
            }

            rangeNearestOpenBase = (float)smallestDist;

            // Air traffic control messaging
            if (LandingGuideUI.instance.IsOpen())
            {
                if (sNearestBase != nearestOpenBase)
                {
                    if (rangeNearestOpenBase < 25000)
                    {
                        nearestOpenBase = sNearestBase;
                        MessageSystemButton.MessageButtonColor color = MessageSystemButton.MessageButtonColor.BLUE;
                        MessageSystem.Message m = new MessageSystem.Message("KK ATC", "You have entered the airspace of " + sNearestBase + " ATC. Please keep this channel open and obey all signal lights. Thank you. " + sNearestBase + " Air Traffic Control out.", color, MessageSystemButton.ButtonIcons.MESSAGE);
                        MessageSystem.Instance.AddMessage(m);
                    }
                    else
                    if (nearestOpenBase.Length != 0)
                    {
                        // you have left ...
                        MessageSystemButton.MessageButtonColor color = MessageSystemButton.MessageButtonColor.GREEN;
                        MessageSystem.Message m = new MessageSystem.Message("KK ATC", "You are now leaving the airspace of " + sNearestBase + ". Safe journey. " + sNearestBase + " Air Traffic Control out.", color, MessageSystemButton.ButtonIcons.MESSAGE);
                        MessageSystem.Instance.AddMessage(m);
                        nearestOpenBase = "";
                    }
                }
            }

            sBase    = sNearestBase;
            flRange  = rangeNearestOpenBase;
            lNearest = lNearestBase;
        }
        // Returns the nearest Launchsite to a position and range in m to the Launchsite, regardless of whether it is open or closed
        public static void getNearestBase(Vector3 position, out string sBase, out string sBase2, out float flRange, out KKLaunchSite lSite, out KKLaunchSite lSite2)
        {
            SpaceCenter  KSC              = SpaceCenter.Instance;
            var          smallestDist     = Vector3.Distance(KSC.gameObject.transform.position, position);
            var          lastSmallestDist = Vector3.Distance(KSC.gameObject.transform.position, position);
            string       sNearestBase     = "";
            KKLaunchSite lTargetSite      = null;
            KKLaunchSite lLastSite        = null;
            KKLaunchSite lKSC             = null;
            string       sLastNearest     = "";


            foreach (KKLaunchSite site in allLaunchSites)
            {
                if (site.lsGameObject == null)
                {
                    continue;
                }

                var radialposition = site.lsGameObject.transform.position;
                var dist           = Vector3.Distance(position, radialposition);

                if (radialposition == position)
                {
                    continue;
                }

                if (site.LaunchSiteName == "Runway" || site.LaunchSiteName == "LaunchPad")
                {
                    lKSC = site;
                }
                else
                {
                    if ((float)dist < (float)smallestDist)
                    {
                        sLastNearest     = sNearestBase;
                        lLastSite        = lTargetSite;
                        lastSmallestDist = smallestDist;
                        sNearestBase     = site.LaunchSiteName;
                        smallestDist     = dist;
                        lTargetSite      = site;
                    }
                    else if (dist < lastSmallestDist)
                    {
                        sLastNearest     = site.LaunchSiteName;
                        lastSmallestDist = dist;
                        lLastSite        = site;
                    }
                }
            }

            if (sNearestBase == "")
            {
                sNearestBase = "KSC";
                lTargetSite  = lKSC;
            }
            if (sLastNearest == "")
            {
                sLastNearest = "KSC";
                lLastSite    = lKSC;
            }

            rangeNearestBase = (float)smallestDist;

            sBase   = sNearestBase;
            sBase2  = sLastNearest;
            flRange = rangeNearestBase;
            lSite   = lTargetSite;
            lSite2  = lLastSite;
        }