Пример #1
0
 /// <summary>
 /// Highlights all occupied cells, if appropriate toggle is active at inspector.
 /// </summary>
 public void OnDrawGizmos()
 {
     if (traceCellsInEditor && spaceHandlerInstance != null)
     {
         Gizmos.color = new Color(1, .46f, .46f, 1);
         float cellSize = SpaceGraph.GetLevelSideLength(levelToTrace);
         foreach (Vector3 index in SpaceGraph.occCellsLevels[levelToTrace].Keys)
         {
             Gizmos.DrawWireCube(SpaceGraph.GetCellCenterCoordFromIndexOnLevel(index, levelToTrace),
                                 new Vector3(cellSize * .99f, cellSize * .99f, cellSize * .99f));
         }
     }
 }
Пример #2
0
        // Pathfinding
        public List <Vector3> GetWay()
        {
            Open  = new PriorityQueue();
            Close = new List <IVertex>();
            var s = new List <IVertex>();

            s.Add(start);
            Open.Add(FFunction(s), s);
            while (Open.Count != 0)
            {
                if (CheckForCancellationRequested())
                {
                    return(null);
                }
                var p = Open.First();
                Open.RemoveFirst();
                var x = p.Last();
                if (Close.Contains(x))
                {
                    continue;
                }
                if (x.Equals(goal))
                {
                    spaceManagerInstance.pathfindingThreadsCurCount--;
                    return(p.Select(el => SpaceGraph.GetCellCenterCoordFromIndexOnLevel(((Cube)el).index, pathfindingLevel)).ToList()); //converting List<IVertex> to Lis<Vector3>
                }
                Close.Add(x);
                foreach (var y in graph.Adjacent(x))
                {
                    if (CheckForCancellationRequested())
                    {
                        return(null);
                    }
                    if (!Close.Contains(y))
                    {
                        var newPath = new List <IVertex>(p);
                        newPath.Add(y);
                        Open.Add(FFunction(newPath), newPath);
                    }
                }
            }
            spaceManagerInstance.pathfindingThreadsCurCount--;
            return(null);
        }
Пример #3
0
    List <Vector3> BackwardDescent()
    {
        List <Vector3> way = new List <Vector3>(waves.Count + 1);

        way.Add(goal);

        for (int waveI = waves.Count - 1; waveI >= 0; waveI--)
        {
            if (cTInstance.IsCancellationRequested)
            {
                return(null);
            }
            for (int i = -1; i <= 1; i += 2)
            {
                Vector3 cell = new Vector3(way[way.Count - 1].x + i, way[way.Count - 1].y, way[way.Count - 1].z);
                if (waves[waveI].Contains(cell))
                {
                    way.Add(cell); goto exitLabel;
                }
            }
            for (int j = -1; j <= 1; j += 2)
            {
                Vector3 cell = new Vector3(way[way.Count - 1].x, way[way.Count - 1].y + j, way[way.Count - 1].z);
                if (waves[waveI].Contains(cell))
                {
                    way.Add(cell); goto exitLabel;
                }
            }
            for (int k = -1; k <= 1; k += 2)
            {
                Vector3 cell = new Vector3(way[way.Count - 1].x, way[way.Count - 1].y, way[way.Count - 1].z + k);
                if (waves[waveI].Contains(cell))
                {
                    way.Add(cell); goto exitLabel;
                }
            }
            exitLabel :;
        }

        way = way.Select(index => SpaceGraph.GetCellCenterCoordFromIndexOnLevel(index, 0)).ToList();
        way.Reverse();
        return(way);
    }
Пример #4
0
 static Vector3 GetFreeAdjacentCellCoord(Vector3 index, int level)
 {
     if (IsAdjacentsFree(index, level))
     {
         return(SpaceGraph.GetCellCenterCoordFromIndexOnLevel(index, level));
     }
     for (int x = -1; x <= 1; x++)
     {
         for (int y = -1; y <= 1; y++)
         {
             for (int z = -1; z <= 1; z++)
             {
                 Vector3 tmpIndex = new Vector3(index.x + x, index.y + y, index.z + z);
                 if (IsAdjacentsFree(tmpIndex, level))
                 {
                     return(SpaceGraph.GetCellCenterCoordFromIndexOnLevel(tmpIndex, level));
                 }
             }
         }
     }
     return(SpaceGraph.GetCellCenterCoordFromIndexOnLevel(index, level));
 }
Пример #5
0
    /// <summary>
    /// In-editor highlighting of the wave-trace algorithm executing.
    /// </summary>

    #region Pathfinding trace

#if UNITY_EDITOR
    private void OnDrawGizmos()
    {
        if (showPathfindingZone)
        {
            Gizmos.color = Color.green;
            Vector3 zoneCenter = new Vector3(xMin + .5f * (xMax - xMin), yMin + .5f * (yMax - yMin),
                                             zMin + .5f * (zMax - zMin));
            Gizmos.DrawWireCube(zoneCenter, new Vector3((xMax - xMin), (yMax - yMin), (zMax - zMin)));
        }

        if (!inEditorPathfindingTraverce)
        {
            return;
        }

        Gizmos.color = Color.cyan;

        if (waves == null)
        {
            return;
        }

        lock (waves)
        {
            if (waves.Count <= 0)
            {
                return;
            }
            foreach (Vector3 cell in waves[waves.Count - 1])
            {
                Vector3 coord = SpaceGraph.GetCellCenterCoordFromIndexOnLevel(cell, 0);
                Gizmos.DrawWireCube(coord,
                                    new Vector3(spaceManagerInstance.cellMinSize, spaceManagerInstance.cellMinSize,
                                                spaceManagerInstance.cellMinSize));
            }
        }
    }
Пример #6
0
        bool IsIndexInRange(int x, int y, int z)
        {
            Vector3 v = new Vector3(x, y, z);

            return(constraints.Check(SpaceGraph.GetCellCenterCoordFromIndexOnLevel(v, pathfindingLevel)));
        }
Пример #7
0
    public List <Vector3> GetWay()
    {
        waves.Add(new HashSet <Vector3> {
            start
        });
        waves.Add(new HashSet <Vector3>());

        while (waves[waves.Count - 2].Count != 0)
        {
            foreach (Vector3 index in waves[waves.Count - 2])
            {
                if (cTInstance.IsCancellationRequested)
                {
                    return(null);
                }
                for (int i = -1; i <= 1; i += 2)
                {
                    Vector3 cell = new Vector3(index.x + i, index.y, index.z);
                    if (cell == goal)
                    {
                        return(BackwardDescent());
                    }
                    if (!spaceConstarintsInst.Check(SpaceGraph.GetCellCenterCoordFromIndexOnLevel(cell, 0)))
                    {
                        continue;
                    }
                    if (SpaceGraph.IsCellOccAtIndexOnLevel(cell, 0) || waves[waves.Count - 2].Contains(cell))
                    {
                        continue;
                    }
                    if (waves.Count > 2)
                    {
                        if (waves[waves.Count - 3].Contains(cell))
                        {
                            continue;
                        }
                    }
                    lock (waves)
                        waves[waves.Count - 1].Add(cell);
                }
                for (int j = -1; j <= 1; j += 2)
                {
                    Vector3 cell = new Vector3(index.x, index.y + j, index.z);
                    if (cell == goal)
                    {
                        return(BackwardDescent());
                    }
                    if (!spaceConstarintsInst.Check(SpaceGraph.GetCellCenterCoordFromIndexOnLevel(cell, 0)))
                    {
                        continue;
                    }
                    if (SpaceGraph.IsCellOccAtIndexOnLevel(cell, 0) || waves[waves.Count - 2].Contains(cell))
                    {
                        continue;
                    }
                    if (waves.Count > 2)
                    {
                        if (waves[waves.Count - 3].Contains(cell))
                        {
                            continue;
                        }
                    }
                    lock (waves)
                        waves[waves.Count - 1].Add(cell);
                }
                for (int k = -1; k <= 1; k += 2)
                {
                    Vector3 cell = new Vector3(index.x, index.y, index.z + k);
                    if (cell == goal)
                    {
                        return(BackwardDescent());
                    }
                    if (!spaceConstarintsInst.Check(SpaceGraph.GetCellCenterCoordFromIndexOnLevel(cell, 0)))
                    {
                        continue;
                    }
                    if (SpaceGraph.IsCellOccAtIndexOnLevel(cell, 0) || waves[waves.Count - 2].Contains(cell))
                    {
                        continue;
                    }
                    if (waves.Count > 2)
                    {
                        if (waves[waves.Count - 3].Contains(cell))
                        {
                            continue;
                        }
                    }
                    lock (waves)
                        waves[waves.Count - 1].Add(cell);
                }
            }
            HashSet <Vector3> newHSet = new HashSet <Vector3>();
            waves.Add(newHSet);
        }
        return(null);
    }