Exemplo n.º 1
0
        public NavigationNode(Vector3 center, float boxSize, ExplorationNode node, WorldSceneCell cell)
        {
            if (node != null)
            {
                _explorationNode = node;
                _scene           = node.Scene;
            }

            if (cell != null)
            {
                NodeFlags = (NodeFlags)cell.NavCellFlags;
            }

            var halfSize = (float)boxSize / 2;

            Center            = center.ToVector2();
            TopLeft           = Center + new Vector2(-(halfSize), -(halfSize));
            BottomLeft        = Center + new Vector2(-(halfSize), halfSize);
            TopRight          = Center + new Vector2(halfSize, -(halfSize));
            BottomRight       = Center + new Vector2(halfSize, halfSize);
            NavigableCenter   = center;
            NavigableCenter2D = Center;
        }
Exemplo n.º 2
0
        //public static AdventurerNode NearestWeightedUnvisitedNodeLocation(HashSet<int> levelAreaIds = null)
        //{

        //    using (new PerformanceLogger("NearestWeightedUnvisitedNodeLocation", true))
        //    {
        //        return NodesStorage.CurrentWorldNodes.Where(n => !n.Visited && n.NavigableCenter.Distance2DSqr(AdvDia.MyPosition) > 100 && n.NavigableCenter.IsInLevelAreas(levelAreaIds))
        //            .OrderByDescending(n => n.NearbyVisitedNodesCount)
        //            .ThenBy(n => n.NavigableCenter.Distance2DSqr(AdvDia.MyPosition))
        //            .FirstOrDefault();
        //    }
        //}


        public static ExplorationNode NearestWeightedUnvisitedNode(HashSet <int> levelAreaIds, List <string> ignoreScenes = null)
        {
            var             dynamicWorldId = AdvDia.CurrentWorldDynamicId;
            var             myPosition     = AdvDia.MyPosition;
            ExplorationNode node           = null;

            using (new PerformanceLogger("NearestUnvisitedNodeLocation", true))
            {
                var nearestNode = ExplorationGrid.Instance.GetNearestWalkableNodeToPosition(myPosition);
                for (var i = 3; i <= 4; i++)
                {
                    var closestUnvisitedNode = ExplorationGrid.Instance.GetNeighbors(nearestNode, i).Cast <ExplorationNode>()
                                               .Where(n => !n.IsIgnored && !n.IsVisited && n.HasEnoughNavigableCells && n.DynamicWorldId == dynamicWorldId && levelAreaIds.Contains(n.LevelAreaId) && NavigationGrid.Instance.CanRayWalk(myPosition, n.Center.ToVector3()))
                                               .OrderBy(n => n.Distance2DSqr)
                                               .FirstOrDefault();
                    if (closestUnvisitedNode != null)
                    {
                        node = closestUnvisitedNode;
                    }
                }

                //if (node != null)
                //{
                //    Logger.Debug("[ExplorationLogic] Picked a node using nearby unvisited nodes method (Node Distance: {0})", node.NavigableCenter.Distance(AdvDia.MyPosition));
                //}


                if (node == null)
                {
                    //var nodes = ExplorationGrid.Instance.WalkableNodes.Where(
                    //    n =>
                    //        !n.IsVisited && n.WorldId == dynamicWorldId &&
                    //        n.NavigableCenter.DistanceSqr(myPosition) > 100 &&
                    //        levelAreaIds.Contains(n.LevelAreaSnoIdId) && n.GetNeighbors(3).Count(nn => n.HasEnoughNavigableCells) > 1)
                    //    .OrderBy(n =>  n.NavigableCenter.DistanceSqr(myPosition))
                    //    .Take(10)
                    //    .ToList();
                    //if (nodes.Count > 1)
                    //{
                    //    var min = nodes.Min(n => n.NavigableCenter.Distance(myPosition));
                    //    var max = nodes.Max(n => n.NavigableCenter.Distance(myPosition));
                    //    var averageDistance = (max + min) / 2 + 1;
                    //    node =
                    //        nodes.Where(n => n.NavigableCenter.Distance(myPosition) < averageDistance)
                    //            .OrderBy(n => n.UnvisitedWeight)
                    //            .FirstOrDefault();
                    //}
                    //else if (nodes.Count == 1)
                    //{
                    //    node = nodes[0];
                    //}
                    node =
                        ExplorationGrid.Instance.WalkableNodes
                        .Where(n =>
                               !n.IsIgnored &&
                               !n.IsVisited &&
                               n.DynamicWorldId == dynamicWorldId &&
                               n.NavigableCenter.DistanceSqr(myPosition) > 100 &&
                               levelAreaIds.Contains(n.LevelAreaId))
                        .OrderByDescending(n => (1 / n.NavigableCenter.Distance(AdvDia.MyPosition)) * n.UnvisitedWeight)
                        .FirstOrDefault();
                    //if (node != null)
                    //{
                    //    Logger.Debug("[ExplorationLogic] Picked a node using unvisited weighting method (Node Distance: {0}, Node Weight: {1})", node.NavigableCenter.Distance(AdvDia.MyPosition), node.UnvisitedWeight);
                    //}
                }

                //if (node == null)
                //{
                //    // Ignore level area match
                //    node = ExplorationGrid.Instance.WalkableNodes
                //    .Where(n =>
                //        !n.IsIgnored &&
                //        !n.IsVisited &&
                //        n.DynamicWorldId == dynamicWorldId &&
                //        n.NavigableCenter.DistanceSqr(myPosition) > 100)
                //    .OrderByDescending(n => (1 / n.NavigableCenter.Distance(AdvDia.MyPosition)) * n.UnvisitedWeight)
                //    .FirstOrDefault();
                //}

                if (node == null)
                {
                    var allNodes       = ExplorationGrid.Instance.WalkableNodes.Count(n => levelAreaIds.Contains(n.LevelAreaId));
                    var unvisitedNodes = ExplorationGrid.Instance.WalkableNodes.Count(n => !n.IsVisited && levelAreaIds.Contains(n.LevelAreaId));

                    Logger.Info("[ExplorationLogic] Couldn't find any unvisited nodes. Current AdvDia.LevelAreaSnoIdId: {0}, " +
                                "ZetaDia.CurrentLevelAreaSnoId: {3}, Total Nodes: {1} Unvisited Nodes: {2} Searching In [{4}]",
                                AdvDia.CurrentLevelAreaId, allNodes, unvisitedNodes, ZetaDia.CurrentLevelAreaSnoId, string.Join(", ", levelAreaIds));
                }
                return(node);
            }
        }
Exemplo n.º 3
0
        private void CreateGrid()
        {
            if (GridCreated)
            {
                return;
            }

            Cells = new List <WorldSceneCell>();

            foreach (var navCell in Scene.Mesh.Zone.NavZoneDef.NavCells)
            {
                //if (navCell.Flags.HasFlag(NavCellFlags.AllowWalk))
                //{
                Cells.Add(new WorldSceneCell(navCell, Min));
                //}
            }
            if (SubScene != null)
            {
                foreach (var navCell in SubScene.Scene.Mesh.Zone.NavZoneDef.NavCells)
                {
                    //if (navCell.Flags.HasFlag(NavCellFlags.AllowWalk))
                    //{
                    Cells.Add(new WorldSceneCell(navCell, SubScene.Min));
                    //}
                }
                if (SubScene.SubScene != null)
                {
                    foreach (var navCell in SubScene.SubScene.Scene.Mesh.Zone.NavZoneDef.NavCells)
                    {
                        //if (navCell.Flags.HasFlag(NavCellFlags.AllowWalk))
                        //{
                        Cells.Add(new WorldSceneCell(navCell, SubScene.SubScene.Min));
                        //}
                    }
                }
            }


            var navBoxSize      = ExplorationData.ExplorationNodeBoxSize;
            var searchBeginning = navBoxSize / 2;

            //var cellCount = _boxSize / navBoxSize;
            //var maxCellsCount = cellCount * cellCount;


            for (var x = Min.X + searchBeginning; x <= Max.X; x = x + navBoxSize)
            {
                for (var y = Min.Y + searchBeginning; y <= Max.Y; y = y + navBoxSize)
                {
                    var navNode = new ExplorationNode(new Vector2(x, y), _boxSize, _boxTolerance, this);
                    Nodes.Add(navNode);
                }
            }


            //var width = (int)(Max.X - Min.X);
            //var height = (int)(Max.Y - Min.Y);
            //var gridSizeX = width / _boxSize;
            //var gridSizeY = height / _boxSize;
            ////var grid = new Node[gridSizeX, gridSizeY];

            //for (var x = 0; x < gridSizeX; x++)
            //{
            //    for (var y = 0; y < gridSizeY; y++)
            //    {
            //        var center = Min + new Vector2(x * _boxSize + _boxSize / 2, y * _boxSize + _boxSize / 2);
            //        Nodes.Add(new ExplorationNode(center, _boxSize, _boxTolerance, this));
            //    }
            //}

            GridCreated = true;
        }