Exemplo n.º 1
0
    /* #endregion */

    /* #region ---- Reset all tiles to none viable movement targets --------------------------- */
    private void setAllTilesNoneViableTarget(PitchManager PitchManager)
    {
        for (int x = 1; x <= MatchManager.PitchGrid.PitchSettings.PitchWidth; x++)
        {
            for (int z = 1; z <= MatchManager.PitchGrid.PitchSettings.PitchLength; z++)
            {
                PitchManager.GetPitchTile(x, z).IsViableTarget = false;
            }
        }
    }
Exemplo n.º 2
0
    /* #endregion */
    /* ======================================================================================== */

    /* #region ==== C R E A T E  G R A P H ==================================================== */
    public void AddNeighbourTiles()
    {
        PitchManager PitchManager = MatchManager.PitchManager;
        int          pitchWidth   = MatchManager.PitchGrid.PitchSettings.PitchWidth;
        int          pitchLength  = MatchManager.PitchGrid.PitchSettings.PitchLength;

        for (int x = 1; x <= pitchWidth; x++)
        {
            for (int z = 1; z <= pitchLength; z++)
            {
                PitchTile PitchTile = PitchManager.GetPitchTile(x, z);
                PitchTile.NeighbourTiles = new List <PitchTile>();
                addNeighbourTiles(x, z, PitchTile);
            }
        }

        void addNeighbourTiles(int x, int z, PitchTile PitchTile)
        {
            /* North */
            if (z < pitchLength)
            {
                PitchTile neighbourPitchTile = PitchManager.GetPitchTile(x, z + 1);
                PitchTile.NeighbourTiles.Add(neighbourPitchTile);
            }

            /* South */
            if (z > 1)
            {
                PitchTile neighbourPitchTile = PitchManager.GetPitchTile(x, z - 1);
                PitchTile.NeighbourTiles.Add(neighbourPitchTile);
            }


            /* West */
            if (x < pitchWidth)
            {
                PitchTile neighbourPitchTile = PitchManager.GetPitchTile(x + 1, z);
                PitchTile.NeighbourTiles.Add(neighbourPitchTile);
            }

            /* East */
            if (x > 1)
            {
                PitchTile neighbourPitchTile = PitchManager.GetPitchTile(x - 1, z);
                PitchTile.NeighbourTiles.Add(neighbourPitchTile);
            }

            /* NorthWest */
            if (x < pitchWidth && z < pitchLength)
            {
                PitchTile neighbourPitchTile = PitchManager.GetPitchTile(x + 1, z + 1);
                PitchTile.NeighbourTiles.Add(neighbourPitchTile);
            }

            /* NorthEast */
            if (x > 1 && z < pitchLength)
            {
                PitchTile neighbourPitchTile = PitchManager.GetPitchTile(x - 1, z + 1);
                PitchTile.NeighbourTiles.Add(neighbourPitchTile);
            }

            /* SouthWest*/
            if (x < pitchWidth && z > 1)
            {
                PitchTile neighbourPitchTile = PitchManager.GetPitchTile(x + 1, z - 1);
                PitchTile.NeighbourTiles.Add(neighbourPitchTile);
            }

            /* SouthEast*/
            if (x > 1 && z > 1)
            {
                PitchTile neighbourPitchTile = PitchManager.GetPitchTile(x - 1, z - 1);
                PitchTile.NeighbourTiles.Add(neighbourPitchTile);
            }
        }
    }
Exemplo n.º 3
0
    /* #endregion */
    /* ======================================================================================== */

    /* #region ==== C A L C U L A T E  &  G E T  P A T H  T O  T A R G E T ==================== */

    public List <PitchTile> GetPathToTarget(PitchTile targetIn, MatchPlayer sourceIn)
    {
        PitchManager PitchManager = MatchManager.PitchManager;

        Dictionary <PitchTile, float>     distanceToSource  = new Dictionary <PitchTile, float>();
        Dictionary <PitchTile, PitchTile> previousPitchTile = new Dictionary <PitchTile, PitchTile>();
        List <PitchTile> unvisitedPitchTiles    = new List <PitchTile>();
        List <PitchTile> CalculatedPathToTarget = null;

        //Set target tile
        PitchTile target = PitchManager.GetPitchTile(targetIn.CoordX, targetIn.CoordZ);

        //Set source tile
        PitchTile source = PitchManager.GetPitchTile(sourceIn.CoordX, sourceIn.CoordZ);

        distanceToSource[source]  = 0;
        previousPitchTile[source] = null;

        //Set all other tiles and add all (incl. source) to list of unvisited tiles
        for (int x = 1; x <= MatchManager.PitchGrid.PitchSettings.PitchWidth; x++)
        {
            for (int z = 1; z <= MatchManager.PitchGrid.PitchSettings.PitchLength; z++)
            {
                PitchTile pitchTile = PitchManager.GetPitchTile(x, z);

                if (pitchTile != source)
                {
                    distanceToSource[pitchTile]  = Mathf.Infinity;
                    previousPitchTile[pitchTile] = null;
                }

                unvisitedPitchTiles.Add(pitchTile);
            }
        }

        while (unvisitedPitchTiles.Count > 0)
        {
            //"u" is going to be the unvisited node with the smallest distance.
            PitchTile u = null;

            //Find the tile with the shortest Distance to Source
            foreach (PitchTile possibleU in unvisitedPitchTiles)
            {
                if (u == null || distanceToSource[possibleU] < distanceToSource[u])
                {
                    u = possibleU;
                }
            }

            if (u == target)
            {
                break;
            }

            unvisitedPitchTiles.Remove(u);

            foreach (PitchTile v in u.NeighbourTiles)
            {
                //float alt = distanceToSource[u] + u.DistanceToTile(v);
                float alt = distanceToSource[u] + v.CostToEnter + costForDiagonalMove(u, v);
                if (alt < distanceToSource[v])
                {
                    distanceToSource[v]  = alt;
                    previousPitchTile[v] = u;
                }
            }
        }

        //If the while loop has ended or breaked, either we found the shortest path or there is no path to the target.
        if (previousPitchTile[target] == null)
        {
            return(CalculatedPathToTarget);
        }
        else
        {
            //Step trough the previousPitchTiles chain and add it to the pathToTarget.
            CalculatedPathToTarget = new List <PitchTile>();
            PitchTile current = target;  //start tile

            while (current != null)
            {
                CalculatedPathToTarget.Add(current);
                current = previousPitchTile[current]; //Reset current to the previous tile in the chain
            }

            CalculatedPathToTarget.Reverse();
            return(CalculatedPathToTarget);
        }
    }