Beispiel #1
0
        /// <summary>
        /// The heuristic function for doing A* pathfinding for this unit.
        /// Note that to reduce garbage, this heuristic may reuse global variables,
        ///		which means only one Unit can have a heuristic at any time.
        /// </summary>
        protected void AStarEdgeCalc(Pathfinding.Goal <Vector2i> goal,
                                     Pathfinding.Edge <Vector2i> edge,
                                     out float edgeLength, out float heuristic)
        {
            Graph.AStarEdgeCalc(goal, edge, out edgeLength, out heuristic);

            //Subtract enemy distances squared from the heuristic.
            foreach (Unit enemy in _temp_enemies)
            {
                float distSqr = enemy.Pos.Value.DistanceSqr(Pos);
                float distT   = distSqr * One_Over_MaxEnemyDistance;
                distT = Math.Max(0.0f, Math.Min(1.0f, distT));

                heuristic += Units.Consts.EnemyDistanceHeuristicMax * distT;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Does the basic edge length/heuristic calculation
        /// that all units will likely use for their pathing.
        /// </summary>
        public static void AStarEdgeCalc(Pathfinding.Goal <Vector2i> goal,
                                         Pathfinding.Edge <Vector2i> edge,
                                         out float edgeLength, out float heuristic)
        {
            //For performance (and because they'll generally be adjacent),
            //    use manhattan distance between nodes.
            edgeLength = Math.Abs(edge.Start.x - edge.End.x) + Math.Abs(edge.Start.y - edge.End.y);


            heuristic = 0.0f;

            //Manhattan distance to the goal.
            if (goal.SpecificGoal.HasValue)
            {
                Vector2i specificGoal = goal.SpecificGoal.Value;
                float    dist         = Math.Abs(edge.End.x - specificGoal.x) +
                                        Math.Abs(edge.End.y - specificGoal.y);

                //Square it to make it more important.
                heuristic += dist * dist;
            }
        }