Пример #1
0
 /// <summary>
 /// Calculates how many movement points a unit must expend in order to enter a particular map hex
 /// (based on unit's movement type, terrain, weather, assisting unit (engineer), roads, etc).
 /// </summary>
 /// <param name="_destinationNode"></param>
 /// <param name="_direction"></param>
 /// <param name="_moveType"></param>
 /// <returns>integer containing number of movement points</returns>
 public int CalculateCostToEnterNode(MapNode _destinationNode, int _direction, MoveType _moveType)
 {
     return(1);
 }
Пример #2
0
        /// <summary>
        /// Pathfinding algorithm that determines which map hexes a unit can move to
        /// </summary>
        /// <remarks>
        /// Logic Flow:
        /// 1.  Create an open list and a closed list that are both empty
        /// 2.  Add the start node (map hex) to the open list
        /// 3.  Loop until the open list is empty
        ///     a.  Grab first node on Open list
        ///     b.  For all 6 movement directions do the following:
        ///         (1.)  Calculate adjacent map node
        ///         (2.)  If adjacent map node is within playing area AND isn't on the Closed list THEN calculate movement
        ///               cost to enter that node
        ///         (3.)  If cost is less than existing cost, set new cost and add node to Open list (if not already there)
        /// </remarks>
        /// <param name="_unit"></param>
        public void SetAllowableMoves(int _id)
        {
            Unit unit = this.Units[_id];

            // clear out the array that contains whether the selected unit can move to a given hex
            for (int x = 0; x <= m_MAP_MAX_X; x++)
            {
                for (int y = 0; y <= m_MAP_MAX_Y; y++)
                {
                    this.m_AllowableMoves[x, y] = Pathfinding.Prohibited;
                }
            }

            // init lists
            this.m_OpenList.Clear();
            this.m_ClosedList.Clear();

            // add starting location to open list
            MapNode nodeStart = new MapNode(unit.CurrentX, unit.CurrentY);

            this.m_AllowableMoves[unit.CurrentX, unit.CurrentY] = Pathfinding.StartHex;
            nodeStart.cost = 0;
            this.m_OpenList.Add(nodeStart);

            // evaluate open list until empty
            while (this.m_OpenList.Count > 0)
            {
                // pop the top node
                MapNode nodeCurrent = this.m_OpenList[0];
                this.m_OpenList.Remove(nodeCurrent);
                this.m_ClosedList.Add(nodeCurrent);

                // evaluate all 6 adjacent nodes
                for (int dir = 0; dir < 6; dir++)
                {
                    // get node for current direction
                    MapNode nodeDir = this.GetMapNodeForDirection(nodeCurrent, dir);

                    // evaluate node if it is valid (within playable portion of the map) AND it is not on the closed list
                    if (nodeDir.isValid && !this.IsNodeInClosedList(nodeDir.x, nodeDir.y))   //closedList.Contains(nodeDir)) {
                    // get total cost to enter adjacent node (i.e. cost to enter current node + cost to enter new node)
                    {
                        int totalCost = nodeCurrent.cost + CalculateCostToEnterNode(nodeDir, dir, MoveType.Land);

                        // if unit has enough moves to enter new node AND cost to enter new node by this route is less than
                        // cost to enter it by other current routes then make current node its parent and add it to open
                        // list
                        if (unit.Moves >= totalCost && nodeDir.cost > totalCost)
                        {
                            nodeDir.cost = totalCost;
                            this.m_AllowableMoves[nodeDir.x, nodeDir.y] = Pathfinding.Allowed;
                            if (!this.IsNodeInOpenList(nodeDir.x, nodeDir.y))
                            {
                                this.m_OpenList.Add(nodeDir);
                            }
                        }
                    }
                }
            }

            // write array grid to console window
            //Console.WriteLine("X = 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26");
            //for (int y = 0; y <= m_MAP_MAX_Y; y++) {
            //    Console.Write(y.ToString() + " ");
            //    for (int x = 0; x <= m_MAP_MAX_X; x++) {
            //        Console.Write(this.m_AllowableMoves[x, y].ToString() + " ");
            //    }
            //    Console.WriteLine();
            //}
        }