Ticket #2725: AtGoal.patch

File AtGoal.patch, 2.0 KB (added by kanetaka, 10 years ago)
  • source/simulation2/components/CCmpPathfinder_Tile.cpp

     
    3232
    3333typedef PriorityQueueHeap<std::pair<u16, u16>, u32> PriorityQueue;
    3434
    35 #define PATHFIND_STATS 0
     35#define PATHFIND_STATS 1
     36#if PATHFIND_STATS
     37#include "lib/timer.h"
     38TIMER_ADD_CLIENT(tc_AtGoal);
     39#endif
    3640
    3741#define USE_DIAGONAL_MOVEMENT 1
    3842
     
    215219#endif
    216220};
    217221
    218 static bool AtGoal(u16 i, u16 j, const ICmpPathfinder::Goal& goal)
     222static bool AtGoal(u16 i, u16 j, const ICmpPathfinder::Goal& goal, const u32 h)
    219223{
     224#if PATHFIND_STATS
     225    TIMER_ACCRUE(tc_AtGoal);
     226#endif
    220227    // Allow tiles slightly more than sqrt(2) from the actual goal,
    221228    // i.e. adjacent diagonally to the target tile
    222     fixed tolerance = entity_pos_t::FromInt(TERRAIN_TILE_SIZE*3/2);
     229    const fixed tolerance = entity_pos_t::FromInt(TERRAIN_TILE_SIZE*3/2);
    223230
     231    entity_pos_t margin = tolerance;
     232
     233    if (goal.type == ICmpPathfinder::Goal::SQUARE)
     234        // diagonal line is always shorter than max(height, width) * 1.5
     235        margin += (goal.hh > goal.hw ? goal.hh : goal.hw) * 3  / entity_pos_t::FromInt(2 * TERRAIN_TILE_SIZE);
     236   
     237    if (h > g_CostPerTile * margin.ToFloat())
     238        return false;
     239
    224240    entity_pos_t x, z;
    225241    CCmpPathfinder::TileCenter(i, j, x, z);
    226242    fixed dist = CCmpPathfinder::DistanceToGoal(CFixedVector2D(x, z), goal);
     
    377393    NearestTile(goal.x, goal.z, state.iGoal, state.jGoal);
    378394
    379395    // If we're already at the goal tile, then move directly to the exact goal coordinates
    380     if (AtGoal(i0, j0, goal))
     396    if (AtGoal(i0, j0, goal, 0))
    381397    {
    382398        Waypoint w = { goal.x, goal.z };
    383399        path.m_Waypoints.push_back(w);
     
    438454        state.tiles->get(i, j).SetStatusClosed();
    439455
    440456        // If we've reached the destination, stop
    441         if (AtGoal(i, j, goal))
     457        if (AtGoal(i, j, goal, state.tiles->get(i, j).h))
    442458        {
    443459            state.iBest = i;
    444460            state.jBest = j;