Ejemplo n.º 1
0
        /// <summary>
        /// Draws a path between startCellIndex and endCellIndex
        /// </summary>
        /// <returns><c>true</c>, if path was found and drawn, <c>false</c> otherwise.</returns>
        /// <param name="startCellIndex">Start cell index.</param>
        /// <param name="endCellIndex">End cell index.</param>
        bool DrawPath(int startCellIndex, int endCellIndex)
        {
            List <int> cellIndices = map.FindPath(startCellIndex, endCellIndex);

            map.ClearCells(true, false, false);
            if (cellIndices == null)
            {
                return(false);                                                                                                                  // no path found
            }
            // Color starting cell, end cell and path
            map.SetCellColor(cellIndices, Color.gray, true);
            map.SetCellColor(startCellIndex, Color.green, true);
            map.SetCellColor(endCellIndex, Color.red, true);

            return(true);
        }
        public List <int> FindPath(int startCellIndex, int endCellIndex)
        {
            worldMapGlobe.ClearCells(true, false, false);

            List <int> cellIndices = worldMapGlobe.FindPath(startCellIndex, endCellIndex);

            if (cellIndices == null)
            {
                return(null);   // no path found
            }
            //This might be a redundant check
            if (cellIndices.Count == 0)
            {
                cellIndices = null;
                return(null);
            }

            List <int> finalPath = new List <int>();

            finalPath.Add(cellIndices[0]);

            //Check that there is enough remaining movement to travel path
            //Start by getting the cost between the starting cell and the first cell in the path
            int neighborIndex = worldMapGlobe.GetCellNeighbourIndex(startCellIndex, cellIndices[0]);
            int pathCost      = worldMapGlobe.GetCellNeighbourCost(startCellIndex, neighborIndex);
            int i             = 0;

            //Get the cumlative cost for the rest of the path
            foreach (int cellIndex in cellIndices)
            {
                if (i < (cellIndices.Count - 1))
                {
                    neighborIndex = worldMapGlobe.GetCellNeighbourIndex(cellIndices[i], cellIndices[i + 1]);
                    pathCost      = pathCost + worldMapGlobe.GetCellNeighbourCost(cellIndices[i], neighborIndex);
                    if (pathCost <= TravelRange)
                    {
                        finalPath.Add(cellIndices[i + 1]);
                    }
                    else
                    {
                        return(finalPath);
                    }
                    i++;
                }
            }
            return(finalPath);
        }
Ejemplo n.º 3
0
        public List <Province>[] GetProvincesInRange(Cell startCell, int range)
        {
            if (range < 0 || startCell.index < 0 || worldMapGlobe.cells.Count() < startCell.index)
            {
                errorHandler.ReportError("Invalid input for GetProvincesInRange", ErrorState.close_window);
                return(null);
            }

            List <Cell>[] cellsInRange = globeParser.GetCellsInRange(startCell, range);

            int distance = 0;                                                   //distance measures how many rings of hexes we've moved out

            List <Province>[] provinces      = new List <Province> [range + 1]; //provinces is an array of lists with each list containing
            List <Province>   foundProvinces = new List <Province>();           //the provinces that can be reached at that distance.
            List <Province>   provincesInHex = new List <Province>();

            try
            {
                bool startHex = true;
                foreach (List <Cell> hexRing in cellsInRange)
                {
                    if (startHex)
                    {
                        //Get provinces at start hex
                        provinces[0]   = new List <Province>();
                        provincesInHex = GetProvicesInCell(startCell);
                        foreach (Province province in provincesInHex)
                        {
                            foundProvinces.Add(province);
                            provinces[0].Add(province);
                        }
                        startHex = false;
                    }
                    else
                    {
                        distance++;
                        provinces[distance] = new List <Province>();
                        foreach (Cell cell in hexRing)
                        {
                            //Check if there is a path from the start cell to this one
                            if (worldMapGlobe.FindPath(startCell.index, cell.index) != null)
                            {
                                provincesInHex = GetProvicesInCell(cell);
                                foreach (Province province in provincesInHex)
                                {
                                    //Check that this province hasn't already been added
                                    if (!foundProvinces.Contains(province))
                                    {
                                        foundProvinces.Add(province);
                                        provinces[distance].Add(province);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                errorHandler.CatchException(ex, ErrorState.close_window);
                return(null);
            }
            return(provinces);
        }