Exemplo n.º 1
0
        public List <TValue> SearchEntities(double x, double y, double Radius, bool isPrecise = true)
        {
            List <TValue> Objects = new List <TValue>(4000);
            DAreaRect     rect    = QuadTree <TValue> .GetAreaRect(x, y, Radius);

            SearchEntities(rect, x, y, Radius, ref Objects, isPrecise: isPrecise);
            return(Objects);
        }
Exemplo n.º 2
0
        public List <TValue> SearchEntities(DAreaRect AreaRectangle)
        {
            List <TValue> Objects = new List <TValue>(4000);

            //DAreaRect rect = QuadTree<TValue>.GetAreaRect(x, y, Radius);

            SearchEntities(AreaRectangle, 0, 0, 0, ref Objects);

            return(Objects);
        }
Exemplo n.º 3
0
        public static bool InRect(DAreaRect rectIn, DAreaRect rectOut)
        {
            bool result = false;

            if (rectIn.minX >= rectOut.minX && rectIn.maxX <= rectOut.maxX && rectIn.minY >= rectOut.minY && rectIn.maxY <= rectOut.maxY)
            {
                result = true;
            }
            return(result);
        }
Exemplo n.º 4
0
        public static bool InRect(double x, double y, DAreaRect rect)
        {
            bool result = false;

            if ((rect.minX <= x) && (rect.maxX >= x) && (rect.minY <= y) && (rect.maxY >= y))
            {
                result = true;
            }
            return(result);
        }
Exemplo n.º 5
0
        public QuadTree(DAreaRect bounds, int level, QuadTree <TValue> parent)
        {
            if (m_BaseQuad == null)
            {
                m_BaseQuad = this;
            }

            m_Bounds = bounds;
            m_nLevel = level;
            //  m_Parent = parent;
        }
Exemplo n.º 6
0
        private void InitObjects( )
        {
            DAreaRect rect = new DAreaRect(-180.0, -85.0, 180.0, 85.0);

            QuadTreeGroundAtom = new QuadTree <clsGroundAtom>(rect, 0, null);

            m_GameObject.InitObjects();

            //structQuery2Manager Query2Manager = new structQuery2Manager();
            //Query2Manager.QueryStatus = QUERY_SCENARIOSTATUS.QUERY_START_SCENARIO;
            //ChangeScenarioStatus(Query2Manager);
        }
Exemplo n.º 7
0
        public static bool Intersect(DAreaRect Rect1, DAreaRect Rect2)
        {
            bool result = false;


            if (Rect2.maxX >= Rect1.minX && Rect2.minX <= Rect1.maxX)
            {
                if (Rect2.maxY >= Rect1.minY && Rect2.minY <= Rect1.maxY)
                {
                    result = true;
                }
            }


            return(result);
        }
Exemplo n.º 8
0
        public static DAreaRect GetAreaRect(double x, double y, double radius)
        {
            double minX = 0;
            double minY = 0;

            double maxX = 0;
            double maxY = 0;


            double Xm = 0;
            double Ym = 0;



            double Xtest = 0;
            double Ytest = 0;


            // TerrainService.MathEngine.CalcProjectedLocation(x, y, 270, radius, out minX, out Ym, true);
            // TerrainService.MathEngine.CalcProjectedLocation(x, y, 270, radius, out Xtest, out Ytest, true);


            TerrainService.MathEngine.CalcProjectedLocationNew(x, y, 270, radius, out minX, out Ym);

            //VH 22.07.2015

            double Dx = (x - minX);

            maxX = x + Dx;
            //TerrainService.MathEngine.CalcProjectedLocation(x, y, 90, radius, out maxX, out Ym, true);



            maxY = y + Dx;
            // TerrainService.MathEngine.CalcProjectedLocation(x, y, 0, radius, out Xm, out maxY, true);


            minY = y - Dx;
            // TerrainService.MathEngine.CalcProjectedLocation(x, y, 180, radius, out Xm, out minY, true);



            DAreaRect resRect = new DAreaRect(minX, minY, maxX, maxY);


            return(resRect);
        }
Exemplo n.º 9
0
        private clsGroundAtom[] RandomizeAtomsInArea(clsPolygon Structure, ref QuadTree<clsGroundAtom> quadTree, int Qty)
        {
            double minX = double.MaxValue;
            double maxX = 0;
            double minY = double.MaxValue;
            double maxY = 0;
            IEnumerable<TerrainService.shPoint> PolPnts = Structure.Points;
            clsGroundAtom[] Atoms = new clsGroundAtom[Qty];


            System.Random Rnd = new Random();
       
            foreach(var pnt in PolPnts)
            {
                if (pnt.x > maxX) maxX = pnt.x;
                if (pnt.x < minX) minX = pnt.x;
                if (pnt.y > maxY) maxY = pnt.y;
                if (pnt.y < minY) minY = pnt.y;
            }


            DAreaRect rect = new DAreaRect(minX, minY, maxX, maxY);
            quadTree = new QuadTree<clsGroundAtom>(rect, 0, null);


            for (int i = 0; i < Qty; i++)
            {
                double vRnd = Rnd.NextDouble();
                double randX = minX + (maxX - minX) * vRnd;
                vRnd = Rnd.NextDouble();
                double randY = minY + (maxY - minY) * vRnd;

                // choose a random waypoint between rooms and corridors
                List<PolygonWaypoint> roomsAndCorridors = new List<PolygonWaypoint>();
                roomsAndCorridors.AddRange(Structure.waypointGraph.rooms);
                roomsAndCorridors.AddRange(Structure.waypointGraph.corridors);

                int randomWaypointIndex = Rnd.Next(roomsAndCorridors.Count());
                PolygonWaypoint waypoint = roomsAndCorridors[randomWaypointIndex];

                TerrainService.shPoint[] Pnts=PolPnts.ToArray();

                while (true)
                {
                    bool inPolygon =TerrainService.GeometryHelper.GeometryMath.isPointInPolygon(randX, randY,ref  Pnts);
                    if (inPolygon == true)
                    {
                        clsGroundAtom GroundAtom = new clsGroundAtom(this);
                        GroundAtom = new clsGroundAtom(this);
                        GroundAtom.GUID = Util.CretaeGuid();
                        GroundAtom.MyName = GroundAtom.GUID;
						// YD: generate position by waypoints and not randomly
                        //GroundAtom.curr_X = randX;
                        //GroundAtom.curr_Y = randY;
                        //GroundAtom.currPosition = new TerrainService.Vector(randX, randY, 0);
                        GroundAtom.curr_X = waypoint.x;
                        GroundAtom.curr_Y = waypoint.y;
                        GroundAtom.currPosition = new TerrainService.Vector(waypoint.x, waypoint.y, 0);
                        GroundAtom.currentStructureWaypoint = waypoint;
                        GroundAtom.currentAzimuth = Util.Azimuth2Points(GroundAtom.curr_X, GroundAtom.curr_Y,
                                            GroundAtom.currentStructureWaypoint.x, GroundAtom.currentStructureWaypoint.y);
						
						// set speed randomly and not fixed
                        GroundAtom.currentSpeed = Rnd.Next(3, 8);

                        Atoms[i] = GroundAtom;

                        GroundAtomObjectCollection.TryAdd(GroundAtom.GUID, GroundAtom);

                        GroundAtom.ChangeState(new REGULAR_MOVEMENT_IN_STRUCTURE_STATE(Structure, GroundAtom.currentStructureWaypoint));
                        m_GameManager.QuadTreeStructure1GroundAtom.PositionUpdate(GroundAtom);

                        break;
                    }
                    else
                    {
                        vRnd = Rnd.NextDouble();
                        randX = minX + (maxX - minX) * vRnd;
                        vRnd = Rnd.NextDouble();
                        randY = minY + (maxY - minY) * vRnd;
                    }
                }
            }


            return Atoms;
        }
Exemplo n.º 10
0
        private void InitObjects( )
        {
            DAreaRect rect = new DAreaRect(-180.0, -85.0, 180.0, 85.0);           
            QuadTreeGroundAtom = new QuadTree<clsGroundAtom>(rect, 0, null);

            m_GameObject.InitObjects();

            //structQuery2Manager Query2Manager = new structQuery2Manager();
            //Query2Manager.QueryStatus = QUERY_SCENARIOSTATUS.QUERY_START_SCENARIO;
            //ChangeScenarioStatus(Query2Manager);
        }
Exemplo n.º 11
0
        public QuadTree(DAreaRect bounds, int level, QuadTree <TValue> parent, double x, double y)
        {
            if (m_BaseQuad == null)
            {
                m_BaseQuad = this;
            }

            m_Bounds = bounds;
            m_nLevel = level;
            //  m_Parent = parent;


            //if (bounds.minX == bounds.maxX)
            //{
            //}


            //  m_sName = "L" + level + ":X=" + bounds.minX + "Y=" + bounds.maxY;



            bool isFound = false;



            if (Contains(x, y))
            {
                isFound = true;
            }


            if (isFound == false)
            {
                return;
            }



            double distance = TerrainService.MathEngine.CalcDistance(m_Bounds.minX, m_Bounds.minY, m_Bounds.maxX, m_Bounds.maxY);

            if (level > 29)
            {
                return;
            }

            if (distance > 2000)
            {
                double dx = bounds.maxX - bounds.minX;
                double dy = bounds.maxY - bounds.minY;



                double nHalfHeight = dy / 2;
                double nHalfWidth  = dx / 2;



                DAreaRect DRectNorthWest = new DAreaRect(bounds.minX, bounds.minY + nHalfHeight,
                                                         bounds.minX + nHalfWidth, bounds.maxY);



                m_NorthWest = new QuadTree <TValue>(DRectNorthWest, level + 1, this, x, y);



                DAreaRect DRectNorthEast = new DAreaRect(bounds.minX + nHalfWidth, bounds.minY + nHalfHeight,
                                                         bounds.maxX, bounds.maxY);



                m_NorthEast = new QuadTree <TValue>(DRectNorthEast, level + 1, this, x, y);



                DAreaRect DRectSouthWest = new DAreaRect(bounds.minX, bounds.minY,
                                                         bounds.minX + nHalfWidth, bounds.minY + nHalfHeight);



                m_SouthWest = new QuadTree <TValue>(DRectSouthWest, level + 1, this, x, y);



                DAreaRect DRectSouthEast = new DAreaRect(bounds.minX + nHalfWidth, bounds.minY,
                                                         bounds.maxX, bounds.minY + nHalfHeight);

                if (DRectSouthEast.minX == DRectSouthEast.maxX)
                {
                }


                m_SouthEast = new QuadTree <TValue>(DRectSouthEast, level + 1, this, x, y);
            }
        }
Exemplo n.º 12
0
        public void SearchEntities(DAreaRect AreaRect, double x, double y, double Radius, ref List <TValue> Objects, bool isPrecise = true)
        {
            if (QuadTree <TValue> .Intersect(m_Bounds, AreaRect))
            {
                if (m_NorthWest != null)
                {
                    m_NorthWest.SearchEntities(AreaRect, x, y, Radius, ref Objects, isPrecise: isPrecise);
                    m_NorthEast.SearchEntities(AreaRect, x, y, Radius, ref Objects, isPrecise: isPrecise);
                    m_SouthWest.SearchEntities(AreaRect, x, y, Radius, ref Objects, isPrecise: isPrecise);
                    m_SouthEast.SearchEntities(AreaRect, x, y, Radius, ref Objects, isPrecise: isPrecise);
                }
                else
                {
                    if (QuadTree <TValue> .InRect(m_Bounds, AreaRect))
                    {
                        lock (AtomPlatformEntities)
                        {
                            if (AtomPlatformEntities.Count > 0)
                            {
                                Objects.AddRange(AtomPlatformEntities.Values);
                            }



                            //   for (int i = 0; i < AtomPlatformEntities.Count; i++)
                            //   foreach (TValue atom in AtomPlatformEntities.Values)



                            //foreach (string key in AtomPlatformEntities.Keys)
                            //{
                            //    TValue atom = AtomPlatformEntities[key];
                            //   // TValue atom = AtomPlatformEntities.Values.ElementAt(i);
                            //   // TValue atom = AtomPlatformEntities.Values[i];

                            //    Objects.Add(atom);
                            //}
                        }
                    }
                    else
                    {
                        if (isPrecise == true && Radius != 0 && (x != 0 || y != 0))
                        {
                            lock (AtomPlatformEntities)
                            {
                                // for (int i = 0; i < AtomPlatformEntities.Count; i++)
                                // foreach (TValue atom in AtomPlatformEntities.Values)
                                foreach (string key in AtomPlatformEntities.Keys)
                                {
                                    TValue atom = AtomPlatformEntities[key];
                                    // TValue atom = AtomPlatformEntities.Values.ElementAt(i);
                                    //                                    TValue atom = AtomPlatformEntities.Values[i];
                                    // if (atom.Platform.PlatformCategoryId == enumPlatformId.SOLDIER) continue;
                                    double dist = TerrainService.MathEngine.CalcDistance(x, y, atom.x, atom.y);
                                    if (dist <= Radius)
                                    {
                                        Objects.Add(atom);
                                    }
                                }
                            }
                        }
                        else
                        {
                            lock (AtomPlatformEntities)
                            {
                                //   for (int i = 0; i < AtomPlatformEntities.Count; i++)
                                //                                foreach (TValue atom in AtomPlatformEntities.Values)
                                foreach (string key in AtomPlatformEntities.Keys)
                                {
                                    TValue atom = AtomPlatformEntities[key];
                                    //  TValue atom = AtomPlatformEntities.Values.ElementAt(i);
                                    //                                    TValue atom = AtomPlatformEntities.Values[i];
                                    if (QuadTree <TValue> .InRect(atom.x, atom.y, AreaRect))
                                    {
                                        Objects.Add(atom);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 13
0
        public void AddObject(double x, double y, string objName, TValue obj)
        {
            //   lock (AtomPlatformEntities)  //    this)
            {
                try
                {
                    if (obj == null)
                    {
                        return;
                    }
                    if (obj.bVisibleToClient == false)
                    {
                        return;
                    }
                    if (Contains(x, y))
                    {
                        // int nIndex = 0;

                        //nIndex = AtomPlatformEntities.IndexOfKey(objName);



                        if (m_NorthWest != null)
                        {
                            m_NorthWest.AddObject(x, y, objName, obj);
                            m_NorthEast.AddObject(x, y, objName, obj);
                            m_SouthWest.AddObject(x, y, objName, obj);
                            m_SouthEast.AddObject(x, y, objName, obj);

                            return;
                        }



                        lock (AtomPlatformEntities)  //    this)
                        {
                            bool isExist = AtomPlatformEntities.ContainsKey(objName);
                            //if (nIndex < 0)
                            if (isExist == false)
                            {
                                AtomPlatformEntities.Add(objName, obj);
                                obj.QuadTreeBounds = m_Bounds;



                                if (AtomPlatformEntities.Count > MAX_PER_NODE && m_nLevel < MAX_NODE_LEVEL)
                                {
                                    double dx          = m_Bounds.maxX - m_Bounds.minX;
                                    double dy          = m_Bounds.maxY - m_Bounds.minY;
                                    double nHalfHeight = dy / 2;
                                    double nHalfWidth  = dx / 2;



                                    DAreaRect DRectNorthWest = new DAreaRect(m_Bounds.minX, m_Bounds.minY + nHalfHeight,
                                                                             m_Bounds.minX + nHalfWidth, m_Bounds.maxY);

                                    m_NorthWest = new QuadTree <TValue>(DRectNorthWest, m_nLevel + 1, this);



                                    DAreaRect DRectNorthEast = new DAreaRect(m_Bounds.minX + nHalfWidth, m_Bounds.minY + nHalfHeight,
                                                                             m_Bounds.maxX, m_Bounds.maxY);

                                    m_NorthEast = new QuadTree <TValue>(DRectNorthEast, m_nLevel + 1, this);



                                    DAreaRect DRectSouthWest = new DAreaRect(m_Bounds.minX, m_Bounds.minY,
                                                                             m_Bounds.minX + nHalfWidth, m_Bounds.minY + nHalfHeight);

                                    m_SouthWest = new QuadTree <TValue>(DRectSouthWest, m_nLevel + 1, this);



                                    DAreaRect DRectSouthEast = new DAreaRect(m_Bounds.minX + nHalfWidth, m_Bounds.minY,
                                                                             m_Bounds.maxX, m_Bounds.minY + nHalfHeight);

                                    m_SouthEast = new QuadTree <TValue>(DRectSouthEast, m_nLevel + 1, this);



                                    foreach (TValue GroundEntity in AtomPlatformEntities.Values)
                                    {
                                        string PName = GroundEntity.Key; //GroundEntity.ParentName + "_" + GroundEntity.Number.ToString();

                                        if (DRectNorthWest.Contains(GroundEntity.x, GroundEntity.y))
                                        {
                                            lock (m_NorthWest.AtomPlatformEntities)
                                            {
                                                m_NorthWest.AtomPlatformEntities.Add(PName, GroundEntity);
                                            }

                                            obj.QuadTreeBounds = m_NorthWest.m_Bounds;
                                        }
                                        else if (DRectNorthEast.Contains(GroundEntity.x, GroundEntity.y))
                                        {
                                            lock (m_NorthEast.AtomPlatformEntities)
                                            {
                                                m_NorthEast.AtomPlatformEntities.Add(PName, GroundEntity);
                                            }

                                            obj.QuadTreeBounds = m_NorthEast.m_Bounds;
                                        }
                                        else if (DRectSouthWest.Contains(GroundEntity.x, GroundEntity.y))
                                        {
                                            lock (m_SouthWest.AtomPlatformEntities)
                                            {
                                                m_SouthWest.AtomPlatformEntities.Add(PName, GroundEntity);
                                            }

                                            obj.QuadTreeBounds = m_SouthWest.m_Bounds;
                                        }
                                        else if (DRectSouthEast.Contains(GroundEntity.x, GroundEntity.y))
                                        {
                                            lock (m_SouthEast.AtomPlatformEntities)
                                            {
                                                m_SouthEast.AtomPlatformEntities.Add(PName, GroundEntity);
                                            }

                                            obj.QuadTreeBounds = m_SouthEast.m_Bounds;
                                        }
                                    }

                                    AtomPlatformEntities.Clear();
                                }
                            }
                            else
                            {
                            }
                        }
                    }
                    else
                    {
                        //RemoveObject(objName, obj);
                        //if (m_Parent != null)
                        //{
                        //    m_Parent.AddObject(x, y, objName, obj);
                        //}
                    }
                }
                catch (Exception ex)
                {
                    //   Log.WriteErrorToLog(ex, "");
                }
            }
        }