Ticket #1756: MakeGoalReacheable_improved.patch

File MakeGoalReacheable_improved.patch, 2.7 KB (added by kanetaka, 10 years ago)
  • source/simulation2/components/CCmpPathfinder_Hier.cpp

    (this hunk was shorter than expected)  
    531539
    532540bool CCmpPathfinder_Hier::MakeGoalReachable(u16 i0, u16 j0, PathGoal& goal, pass_class_t passClass)
    533541{
    534542    RegionID source = Get(i0, j0, passClass);
    535543
    536     // Find everywhere that's reachable
    537     std::set<RegionID> reachableRegions;
    538     FindReachableRegions(source, reachableRegions, passClass);
    539 
    540544//  debug_printf(L"\nReachable from (%d,%d): ", i0, j0);
    541545//  for (std::set<RegionID>::iterator it = reachableRegions.begin(); it != reachableRegions.end(); ++it)
    542546//      debug_printf(L"[%d,%d,%d], ", it->ci, it->cj, it->r);
     
    543548//  debug_printf(L"\n");
    544549
    545550    // Check whether any reachable region contains the goal
    546     for (std::set<RegionID>::const_iterator it = reachableRegions.begin(); it != reachableRegions.end(); ++it)
     551
     552    u16 gi0, gj0, gi1, gj1;
     553    m_Pathfinder.NearestNavcell(goal.x - goal.hw - goal.hh, goal.z - goal.hw - goal.hh, gi0, gj0);
     554    m_Pathfinder.NearestNavcell(goal.x + goal.hw + goal.hh, goal.z + goal.hw + goal.hh, gi1, gj1);
     555
     556    u16 ci0 = gi0 / CHUNK_SIZE;
     557    u16 cj0 = gj0 / CHUNK_SIZE;
     558    u16 ci1 = gi1 / CHUNK_SIZE;
     559    u16 cj1 = gj1 / CHUNK_SIZE;
     560
     561    for (u16 ci = ci0; ci <= ci1; ci++)
    547562    {
    548         // Skip region if its chunk doesn't contain the goal area
    549         entity_pos_t x0 = ICmpObstructionManager::NAVCELL_SIZE * (it->ci * CHUNK_SIZE);
    550         entity_pos_t z0 = ICmpObstructionManager::NAVCELL_SIZE * (it->cj * CHUNK_SIZE);
    551         entity_pos_t x1 = x0 + ICmpObstructionManager::NAVCELL_SIZE * CHUNK_SIZE;
    552         entity_pos_t z1 = z0 + ICmpObstructionManager::NAVCELL_SIZE * CHUNK_SIZE;
    553         if (!goal.RectContainsGoal(x0, z0, x1, z1))
    554             continue;
    555 
    556         // If the region contains the goal area, the goal is reachable
    557         // and we don't need to move it
    558         if (GetChunk(it->ci, it->cj, passClass).RegionContainsGoal(it->r, goal))
    559             return false;
     563        for (u16 cj = cj0; cj <= cj1; cj++)
     564        {
     565            Chunk& cnk = GetChunk(ci, cj, passClass);
     566            // If the region contains the goal area, the goal is reachable
     567            // and we don't need to move it
     568            if (cnk.RegionContainsGoal(source.r , goal))
     569                return false;
     570        }
    560571    }
    561572
    562573    // The goal area wasn't reachable,
     
    565576    u16 iGoal, jGoal;
    566577    m_Pathfinder.NearestNavcell(goal.x, goal.z, iGoal, jGoal);
    567578
     579    // Find everywhere that's reachable
     580    std::set<RegionID> reachableRegions;
     581    FindReachableRegions(source, reachableRegions, passClass);
     582
    568583    FindNearestNavcellInRegions(reachableRegions, iGoal, jGoal, passClass);
    569584
    570585    // Construct a new point goal at the nearest reachable navcell