internal bool GetILSGlideSlopeValid(int radioId, double glideSlope)
        {
            NavRadio radio;

            if (navRadio.TryGetValue(radioId, out radio) && radio.beaconIndex >= 0)
            {
                if (radio.isILS && radio.GlidePathInRange(vessel))
                {
                    NavAid beacon          = radio.beacon[radio.beaconIndex];
                    double absoluteBearing = radio.GetBearing(vessel.latitude, vessel.longitude);

                    // Don't detect vertical deviation if we can't see horizontal deviation.
                    double deviation = Utility.NormalizeLongitude(beacon.approachHeadingILS - absoluteBearing);
                    if (Math.Abs(deviation) <= beacon.localizerSectorILS)
                    {
                        Vector3d vesselPosition = vc.mainBody.GetRelSurfacePosition(vessel.CoMD);
                        Vector3d displacement   = vesselPosition - beacon.worldPosition;
                        double   angle          = 90.0 - Vector3d.Angle(beacon.worldNormal, displacement);

                        if (angle >= glideSlope * 0.45 && angle <= glideSlope * 1.75)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Пример #2
0
        /// <summary>
        /// Load our assets through the asset bundle system.
        /// </summary>
        private void LoadAssets()
        {
            StringBuilder sb = StringBuilderCache.Acquire();

            sb.Append("file://").Append(KSPUtil.ApplicationRootPath).Append("GameData/MOARdV/AvionicsSystems/mas-{0}.assetbundle");
            string assetFormat = sb.ToStringAndRelease();

            string platform = string.Empty;

            switch (Application.platform)
            {
            case RuntimePlatform.LinuxPlayer:
                platform = "linux";
                break;

            case RuntimePlatform.OSXPlayer:
                platform = "osx";
                break;

            case RuntimePlatform.WindowsPlayer:
                platform = "windows";
                break;

            default:
                Utility.LogError(this, "Unsupported/unexpected platform {0}", Application.platform);
                return;
            }

            shaders.Clear();
            AssetBundle bundle = LoadAssetBundle(assetFormat, platform);

            if (bundle == null)
            {
                return;
            }

            string[] assetNames = bundle.GetAllAssetNames();
            int      len        = assetNames.Length;

            Shader shader;

            for (int i = 0; i < len; i++)
            {
                if (assetNames[i].EndsWith(".shader"))
                {
                    shader = bundle.LoadAsset <Shader>(assetNames[i]);
                    if (!shader.isSupported)
                    {
                        Utility.LogError(this, "Shader {0} - unsupported in this configuration", shader.name);
                    }
                    shaders[shader.name] = shader;
                }
            }

            bundle.Unload(false);

            fonts.Clear();
            bundle = LoadAssetBundle(assetFormat, "font");
            if (bundle == null)
            {
                return;
            }

            assetNames = bundle.GetAllAssetNames();
            len        = assetNames.Length;

            Font font;

            for (int i = 0; i < len; i++)
            {
                if (assetNames[i].EndsWith(".ttf"))
                {
                    font = bundle.LoadAsset <Font>(assetNames[i]);

                    string[] fnames = font.fontNames;
                    if (fnames.Length == 0)
                    {
                        Utility.LogError(this, "Font {0} - did not find fontName.", font.name);
                    }
                    else
                    {
                        if (fonts.ContainsKey(fnames[0]))
                        {
                            // TODO: Do I need to keep all of the fonts in this dictionary?  Or is one
                            // adequate?
                            fonts[fnames[0]].Add(font);
                        }
                        else
                        {
                            Utility.LogMessage(this, "Adding font \"{0}\" from asset bundle.", fnames[0]);
                            List <Font> fontList = new List <Font>();
                            fontList.Add(font);
                            fonts[fnames[0]] = fontList;
                        }
                    }
                }
            }
            bundle.Unload(false);

            Utility.LogInfo(this, "Found {0} RPM shaders and {1} fonts.", shaders.Count, fonts.Count);

            // User fonts.  We put them here to make sure that internal
            // shaders exist already.
            ConfigNode[] masBitmapFont = GameDatabase.Instance.GetConfigNodes("MAS_BITMAP_FONT");
            for (int masFontIdx = 0; masFontIdx < masBitmapFont.Length; ++masFontIdx)
            {
                LoadBitmapFont(masBitmapFont[masFontIdx]);
            }

            // Generate our list of radio navigation beacons.
            navaids.Clear();
            ConfigNode[] navaidGroupNode = GameDatabase.Instance.GetConfigNodes("MAS_NAVAID");
            for (int navaidGroupIdx = 0; navaidGroupIdx < navaidGroupNode.Length; ++navaidGroupIdx)
            {
                ConfigNode[] navaidNode = navaidGroupNode[navaidGroupIdx].GetNodes("NAVAID");
                for (int navaidIdx = 0; navaidIdx < navaidNode.Length; ++navaidIdx)
                {
                    bool   canAdd = true;
                    NavAid navaid = new NavAid();
                    navaid.maximumRange    = -1.0;
                    navaid.maximumRangeDME = -1.0;

                    navaid.name = string.Empty;
                    if (!navaidNode[navaidIdx].TryGetValue("name", ref navaid.name))
                    {
                        Utility.LogError(this, "Did not get 'name' for NAVAID");
                        canAdd = false;
                    }

                    navaid.identifier = string.Empty;
                    if (!navaidNode[navaidIdx].TryGetValue("id", ref navaid.identifier))
                    {
                        Utility.LogError(this, "Did not get 'id' for NAVAID {0}", navaid.name);
                        canAdd = false;
                    }

                    navaid.celestialName = string.Empty;
                    if (!navaidNode[navaidIdx].TryGetValue("celestialName", ref navaid.celestialName))
                    {
                        Utility.LogError(this, "Did not get 'celestialName' for NAVAID {0}", navaid.name);
                        canAdd = false;
                    }

                    navaid.frequency = 0.0f;
                    if (!navaidNode[navaidIdx].TryGetValue("frequency", ref navaid.frequency))
                    {
                        Utility.LogError(this, "Did not get 'frequency' for NAVAID {0}", navaid.name);
                        canAdd = false;
                    }

                    navaid.latitude = 0.0;
                    if (!navaidNode[navaidIdx].TryGetValue("latitude", ref navaid.latitude))
                    {
                        Utility.LogError(this, "Did not get 'latitude' for NAVAID {0}", navaid.name);
                        canAdd = false;
                    }

                    navaid.longitude = 0.0;
                    if (!navaidNode[navaidIdx].TryGetValue("longitude", ref navaid.longitude))
                    {
                        Utility.LogError(this, "Did not get 'longitude' for NAVAID {0}", navaid.name);
                        canAdd = false;
                    }

                    navaid.altitude = 0.0;
                    if (!navaidNode[navaidIdx].TryGetValue("altitude", ref navaid.altitude))
                    {
                        Utility.LogError(this, "Did not get 'altitude' for NAVAID {0}", navaid.name);
                        canAdd = false;
                    }

                    string type = string.Empty;
                    if (!navaidNode[navaidIdx].TryGetValue("type", ref type))
                    {
                        Utility.LogError(this, "Did not get 'type' for NAVAID {0}", navaid.name);
                        canAdd = false;
                    }
                    switch (type)
                    {
                    case "NDB":
                        navaid.type = NavAidType.NDB;
                        break;

                    case "NDB DME":
                        navaid.type = NavAidType.NDB_DME;
                        break;

                    case "VOR":
                        navaid.type = NavAidType.VOR;
                        break;

                    case "VOR DME":
                        navaid.type = NavAidType.VOR_DME;
                        break;

                    case "ILS":
                        navaid.type = NavAidType.ILS;
                        break;

                    case "ILS DME":
                        navaid.type = NavAidType.ILS_DME;
                        break;

                    default:
                        Utility.LogError(this, "Did not get valid 'type' for NAVAID {0}", navaid.name);
                        canAdd = false;
                        break;
                    }

                    navaid.maximumRangeLocalizer = 0.0;
                    navaid.maximumRangeGlidePath = 0.0;
                    navaid.glidePathDefault      = 3.0;
                    navaid.approachHeadingILS    = 0.0f;
                    navaid.localizerSectorILS    = 0.0f;
                    if (navaid.type == NavAidType.ILS || navaid.type == NavAidType.ILS_DME)
                    {
                        if (!navaidNode[navaidIdx].TryGetValue("maximumRangeLocalizer", ref navaid.maximumRangeLocalizer))
                        {
                            Utility.LogError(this, "Did not get 'maximumRangeLocalizer' for {1} {0}", navaid.name, navaid.type);
                            canAdd = false;
                        }
                        if (!navaidNode[navaidIdx].TryGetValue("maximumRangeGlidePath", ref navaid.maximumRangeGlidePath))
                        {
                            Utility.LogError(this, "Did not get 'maximumRangeGlidePath' for {1} {0}", navaid.name, navaid.type);
                            canAdd = false;
                        }
                        if (!navaidNode[navaidIdx].TryGetValue("glidePathDefault", ref navaid.glidePathDefault))
                        {
                            Utility.LogError(this, "Did not get 'glidePathDefault' for {1} {0}", navaid.name, navaid.type);
                            canAdd = false;
                        }
                        if (!navaidNode[navaidIdx].TryGetValue("approachHeadingILS", ref navaid.approachHeadingILS))
                        {
                            Utility.LogError(this, "Did not get 'approachBearingILS' for {1} {0}", navaid.name, navaid.type);
                            canAdd = false;
                        }
                        if (!navaidNode[navaidIdx].TryGetValue("localizerSectorILS", ref navaid.localizerSectorILS))
                        {
                            Utility.LogError(this, "Did not get 'horizontalSectorILS' for {1} {0}", navaid.name, navaid.type);
                            canAdd = false;
                        }
                    }

                    if (canAdd)
                    {
                        navaids.Add(navaid);
                    }
                }
            }

            morseCode.Clear();
            ConfigNode[] morseCodeNode = GameDatabase.Instance.GetConfigNodes("MAS_MORSE_CODE");
            for (int morseCodeGroupIdx = 0; morseCodeGroupIdx < morseCodeNode.Length; ++morseCodeGroupIdx)
            {
                int numEntries = morseCodeNode[morseCodeGroupIdx].CountValues;
                for (int entry = 0; entry < numEntries; ++entry)
                {
                    ConfigNode.Value val = morseCodeNode[morseCodeGroupIdx].values[entry];
                    if (val.name.Length > 1)
                    {
                        Utility.LogError(this, "Got an invalid morse code of '{0}'", val.name);
                    }
                    else
                    {
                        AudioClip clip = GameDatabase.Instance.GetAudioClip(val.value);
                        if (clip == null)
                        {
                            Utility.LogError(this, "Could not load audio clip {0} for morse code '{1}", val.value, val.name);
                        }
                        else
                        {
                            morseCode[val.value[0]] = clip;
                        }
                    }
                }
            }

            subPages.Clear();
            ConfigNode[] subPageNode = GameDatabase.Instance.GetConfigNodes("MAS_SUB_PAGE");
            for (int subPageIdx = 0; subPageIdx < subPageNode.Length; ++subPageIdx)
            {
                string subPageName = string.Empty;
                if (subPageNode[subPageIdx].TryGetValue("name", ref subPageName))
                {
                    List <ConfigNode> subPageNodes = new List <ConfigNode>();
                    ConfigNode[]      nodes        = subPageNode[subPageIdx].GetNodes();

                    subPageNodes.AddRange(nodes);

                    subPages.Add(subPageName, subPageNodes);
                }
                else
                {
                    Utility.LogError(this, "Found a MAS_SUB_PAGE missing 'name'.  Skipping.");
                }
            }
        }