コード例 #1
0
ファイル: EDSystem.cs プロジェクト: Nexam/NexHud
        public void calculDistanceFromCurrent()
        {
            EDSystem _oSys = EDDatas.Instance.getCurrentSystem();

            OpenTK.Vector3d o = new OpenTK.Vector3d(_oSys.x, _oSys.y, _oSys.z);
            OpenTK.Vector3d d = new OpenTK.Vector3d(x, y, z);
            m_distanceFromCurrentSystem = Math.Round(OpenTK.Vector3d.Distance(o, d), 2);
        }
コード例 #2
0
ファイル: UserSearchResult.cs プロジェクト: Nexam/NexHud
 public void addSystem(EDSystem _system)
 {
     if (m_systems.Where(x => x.name == _system.name).FirstOrDefault() == null)
     {
         _system.calculDistanceFromCurrent();
         m_systems.Add(_system);
     }
 }
コード例 #3
0
ファイル: EDDatas.cs プロジェクト: Nexam/NexHud
        private EDDatas()
        {
            m_systems = new Dictionary <string, EDSystem>();

            //Unknows
            _unknowSystem            = new EDSystem();
            _unknowSystem.name       = "Unknow";
            _unknowSystem.security   = EliteSecurity.Lawless;
            _unknowSystem.government = EliteGovernment.None;

            NexHudMain.EliteApi.Events.LocationEvent += Events_LocationEvent;
        }
コード例 #4
0
ファイル: EDDatas.cs プロジェクト: Nexam/NexHud
        public EDSystem getCurrentSystem()
        {
            string _systemName = NexHudMain.EliteApi.Location.StarSystem;

            if (m_systems.ContainsKey(_systemName))
            {
                if (!m_systems[_systemName].isEDSMComplete() || !m_systems[_systemName].receivedEddbInfos)
                {
                    updateCurrentSystemInfos();
                }

                return(m_systems[_systemName]);
            }
            else
            {
                EDSystem _current = new EDSystem();
                _current.name = "Loading...";
                m_systems.Add(_systemName, _current);
                updateCurrentSystemInfos();
                return(m_systems[_systemName]);
            }
        }
コード例 #5
0
ファイル: EDBody.cs プロジェクト: Nexam/NexHud
 public EDBody(EDSystem _system)
 {
     m_system = _system;
 }
コード例 #6
0
ファイル: EDDatas.cs プロジェクト: Nexam/NexHud
        private void _startResearch(uint _id)
        {
            Stopwatch _watch = new Stopwatch();

            _watch.Start();

            int _statAddedSystem   = 0;
            int _statUpdatedSystem = 0;

            if (!m_lastUSRs.ContainsKey(_id))
            {
                return;
            }

            //Check if search is still valid
            if (m_lastUSRs[_id].searchID != m_lastSearchId)
            {
                m_lastUSRs[_id].Error  = UserSearchResult.UserSearchError.Aborted;
                m_lastUSRs[_id].isDone = true;
                return;
            }
            try
            {
                if (!getCurrentSystem().receivedEdsmBasics)
                {
                    m_lastUSRs[_id].Error = UserSearchResult.UserSearchError.CurrentSystemNotCompleted;
                }
                else if (m_lastUSRs[_id].entry.searchType == NxSearchType.system)
                {
                    //If names are specified we will ignore other parameters
                    if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.name))
                    {
                        string[] _names      = m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.name];
                        string[] _namesNotes = new string[0];
                        if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.nameNotes))
                        {
                            _namesNotes = m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.nameNotes];
                        }


                        for (int i = 0; i < _names.Length; i++)
                        {
                            _names[i] = _names[i].Trim();
                        }


                        for (int i = 0; i < _names.Length; i++)
                        {
                            //Can be optimized with https://www.edsm.net/api-v1/systems
                            EDSMSystemDatas _edsmDatas = ApiConnection.EDSMSystemFullInfos(_names[i]);
                            if (_edsmDatas != null)
                            {
                                EDSystem _system = new EDSystem();
                                _system.updateEDSM(_edsmDatas);
                                NexHudEngine.Log("(EDSM) Found system: " + _system.name);
                                m_lastUSRs[_id].addSystem(_system);
                                if (i < _namesNotes.Length)
                                {
                                    _system.Notes = _namesNotes[i];
                                }
                            }
                        }
                    }
                    else
                    {
                        //Get systems in radius
                        int _radius = m_greatestSearchRadius;

                        int _startRardius = 40;
                        if (m_lastUSRs[_id].entry.searchMaxRadius > 0 && m_lastUSRs[_id].entry.searchMaxRadius < _startRardius)
                        {
                            _startRardius = m_lastUSRs[_id].entry.searchMaxRadius;
                        }


                        int _maxEdsmRadius = 100;
                        if (m_lastUSRs[_id].entry.searchMaxRadius > _startRardius && m_lastUSRs[_id].entry.searchMaxRadius < _maxEdsmRadius)
                        {
                            _maxEdsmRadius = m_lastUSRs[_id].entry.searchMaxRadius;
                        }

                        int[] _increments = new int[]
                        {
                            //Threshold,increment
                            60, 20,
                            90, 10,
                            100, 5
                        };

                        int _pass = 1;
                        if (_radius < _startRardius)
                        {
                            _radius = _startRardius;
                        }

                        int _radiusMinimum = 0;
                        while (m_lastUSRs[_id].Count < UiSearch.MAX_LINE_RESULT && _radius <= _maxEdsmRadius)
                        {
                            m_lastUSRs[_id].ResearchTime  = _watch.ElapsedMilliseconds;
                            m_lastUSRs[_id].CurrentPass   = _pass;
                            m_lastUSRs[_id].CurrentRadius = _radius;
                            //Building list
                            if (_radius > m_greatestSearchRadius)
                            {
                                EDSMSystemDatas[] _systemsInSphereRadius = ApiConnection.EDSMSystemsInSphereRadius(getCurrentSystem().name, m_greatestSearchRadius, _radius);
                                if (_systemsInSphereRadius != null)
                                {
                                    m_greatestSearchRadius = _radius;
                                    for (int i = 0; i < _systemsInSphereRadius.Length; i++)
                                    {
                                        if (!m_systemAround.Contains(_systemsInSphereRadius[i].name))
                                        {
                                            m_systemAround.Add(_systemsInSphereRadius[i].name);
                                        }
                                        if (!m_systems.ContainsKey(_systemsInSphereRadius[i].name))
                                        {
                                            EDSystem _newSystem = new EDSystem();
                                            _newSystem.updateEDSM(_systemsInSphereRadius[i]);
                                            _statAddedSystem++;
                                            m_systems.Add(_systemsInSphereRadius[i].name, _newSystem);
                                        }
                                    }
                                }
                            }

                            List <EDSystem> _systemInRadius = new List <EDSystem>();
                            //Recalculate distance and updating basics...
                            foreach (string _sys in m_systemAround)
                            {
                                if (!m_systems.ContainsKey(_sys))
                                {
                                    _statAddedSystem++;
                                    m_systems.Add(_sys, new EDSystem());
                                }
                                if (!m_systems[_sys].receivedEdsmInfos)
                                {
                                    NexHudEngine.Log("//WARNING// system {0} has not received informations datas. Retrieving...", _sys);
                                    m_systems[_sys].updateEDSM(ApiConnection.EDSMSystem(new ApiConnection.EDSMSystemParameters()
                                    {
                                        name = _sys, showInformation = true
                                    }));
                                    m_systems[_sys].calculDistanceFromCurrent();
                                }
                                if (m_systems[_sys].distanceFromCurrentSystem < _radius && m_systems[_sys].distanceFromCurrentSystem >= _radiusMinimum)
                                {
                                    _systemInRadius.Add(m_systems[_sys]);
                                }
                            }


                            IEnumerable <EDSystem> _ordered = _systemInRadius.OrderBy(s => s.distanceFromCurrentSystem);

                            //Allegiance
                            if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.allegiance))
                            {
                                _ordered = _ordered.Where(x => m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.allegiance].Any(y => x.allegiance.ToStringFormated().Equals(y, StringComparison.InvariantCultureIgnoreCase)));
                            }

                            //Government
                            if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.government))
                            {
                                _ordered = _ordered.Where(x => m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.government].Any(y => x.government.ToStringFormated().Equals(y, StringComparison.InvariantCultureIgnoreCase)));
                            }

                            //Primary economy
                            if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.economy))
                            {
                                _ordered = _ordered.Where(x => m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.economy].Any(y => x.economy.ToStringFormated().Equals(y, StringComparison.InvariantCultureIgnoreCase)));
                            }

                            //Reserve
                            if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.reserve))
                            {
                                _ordered = _ordered.Where(x => m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.reserve].Any(y => x.reserve.ToStringFormated().Equals(y, StringComparison.InvariantCultureIgnoreCase)));
                            }

                            //Security
                            if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.security))
                            {
                                _ordered = _ordered.Where(x => m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.security].Any(y => x.security.ToStringFormated().Equals(y, StringComparison.InvariantCultureIgnoreCase)));
                            }

                            //State
                            if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.state))
                            {
                                _ordered = _ordered.Where(x => m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.state].Any(y => x.state.ToStringFormated().Equals(y, StringComparison.InvariantCultureIgnoreCase)));
                            }



                            NexHudEngine.Log("+ Search total result: {0} . Pass {1}", _ordered.Count(), _pass);
                            m_lastUSRs[_id].ResearchTime = _watch.ElapsedMilliseconds;
                            foreach (EDSystem x in _ordered)
                            {
                                m_lastUSRs[_id].addSystem(x);
                                if (m_lastUSRs[_id].Count >= UiSearch.MAX_LINE_RESULT)
                                {
                                    break;
                                }
                            }
                            _radiusMinimum = _radius;

                            for (int _inc = 0; _inc < _increments.Length; _inc += 2)
                            {
                                if (_radius < _increments[_inc])
                                {
                                    _radius += _increments[_inc + 1];
                                    break;
                                }
                            }
                            if (_radius >= _maxEdsmRadius)
                            {
                                break;
                            }
                            _pass++;
                        }
                    }
                }
                else if (m_lastUSRs[_id].entry.searchType == NxSearchType.body)
                {
                    List <string> _listMaterials = new List <string>();
                    //Make sure the materials are valid
                    if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.rawMaterial))
                    {
                        foreach (string _material in m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.rawMaterial])
                        {
                            if (EngineerHelper.isRawMaterial(_material))
                            {
                                _listMaterials.Add(_material);
                            }
                        }
                    }
                    bool isLandable = true;
                    if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.isLandable))
                    {
                        isLandable = m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.isLandable][0] == "true";
                    }


                    if (_listMaterials.Count > 0)
                    {
                        int maxDistance = 20;

                        if (m_lastUSRs[_id].entry.searchMaxRadius > 0)
                        {
                            maxDistance = Math.Min(m_lastUSRs[_id].entry.searchMaxRadius, 100);
                        }



                        SpanshBodiesResult _spanshResult = ApiConnection.SpanshBodies(getCurrentSystem().name, maxDistance, _listMaterials.ToArray(), isLandable);

                        //Get infos about the targeted systems
                        List <string> _systemWithBodies = new List <string>();
                        foreach (SpanshBody b in _spanshResult.results)
                        {
                            if (!_systemWithBodies.Contains(b.system_name))
                            {
                                _systemWithBodies.Add(b.system_name);
                            }
                        }

                        List <string> _systemToUpdate = new List <string>();
                        //Clean list for system we already received infos
                        foreach (string s in _systemWithBodies)
                        {
                            if (m_systems.ContainsKey(s))
                            {
                                if (!m_systems[s].receivedEdsmBasics && !m_systems[s].receivedEdsmInfos)
                                {
                                    _systemToUpdate.Add(s);
                                }
                            }
                            else
                            {
                                m_systemAround.Add(s);
                                m_systems.Add(s, new EDSystem());
                                _systemToUpdate.Add(s);
                            }
                        }
                        if (_systemToUpdate.Count > 0)
                        {
                            EDSMSystemDatas[] _edsmDatas = ApiConnection.EDSMSystemsList(_systemToUpdate.ToArray(), true);
                            foreach (EDSMSystemDatas _data in _edsmDatas)
                            {
                                m_systems[_data.name].updateEDSM(_data);
                            }
                        }

                        foreach (SpanshBody b in _spanshResult.results)
                        {
                            m_lastUSRs[_id].addBody(m_systems[b.system_name].addOrUpdateBody(b));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException is ThreadAbortException)
                {
                    m_lastUSRs[_id].Error = UserSearchResult.UserSearchError.Aborted;
                    NexHudEngine.Log("Aborting last search thread {0}. Last Id {1}", Thread.CurrentThread.Name, _id);
                }
                else
                {
                    m_lastUSRs[_id].Error = UserSearchResult.UserSearchError.UnknowError;
                    NexHudEngine.Log("ERROR: Search {0} reported an error: {1}", _id, ex.Message);
                }
            }
            finally
            {
                _watch.Stop();
                NexHudEngine.Log("Search was done in {0}ms. Error:{1} Added:{2} Updated:{3}", _watch.ElapsedMilliseconds, m_lastUSRs[_id].Error, _statAddedSystem, _statUpdatedSystem);
                m_lastUSRs[_id].isDone       = true;
                m_lastUSRs[_id].ResearchTime = _watch.ElapsedMilliseconds;
            }
        }