Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. bool Monster::searchTarget(TargetSearchType_t searchType /*= TARGETSEARCH_DEFAULT*/)
  2. {
  3.     #ifdef __DEBUG__
  4.         std::cout << "Searching target... " << std::endl;
  5.     #endif
  6.    
  7.     std::list<Creature*> resultList;
  8.     std::list<Creature*> playerList;
  9.     const Position& myPos = getPosition();
  10.     for(CreatureList::iterator it = targetList.begin(); it != targetList.end(); ++it)
  11.     {
  12.         if(followCreature != (*it) && isTarget(*it) && (searchType == TARGETSEARCH_RANDOM || canUseAttack(myPos, *it)))
  13.         {
  14.             if(Creature* player = (*it)->getPlayer())
  15.                 playerList.push_back(*it);
  16.             else
  17.                 resultList.push_back(*it);
  18.         }
  19.     }
  20.  
  21.     switch(searchType)
  22.     {
  23.         case TARGETSEARCH_NEAREST:
  24.         {
  25.             Creature* target = NULL;
  26.             int32_t range = -1;
  27.             if(!playerList.empty()) {
  28.                 for(CreatureList::iterator it = playerList.begin(); it != playerList.end(); ++it)
  29.                 {
  30.                     int32_t tmp = std::max(std::abs(myPos.x - (*it)->getPosition().x),
  31.                         std::abs(myPos.y - (*it)->getPosition().y));
  32.                     if(range >= 0 && tmp >= range)
  33.                         continue;
  34.  
  35.                     target = *it;
  36.                     range = tmp;
  37.                 }
  38.             }else{
  39.                 for(CreatureList::iterator it = resultList.begin(); it != resultList.end(); ++it)
  40.                 {
  41.                     int32_t tmp = std::max(std::abs(myPos.x - (*it)->getPosition().x),
  42.                         std::abs(myPos.y - (*it)->getPosition().y));
  43.                     if(range >= 0 && tmp >= range)
  44.                         continue;
  45.  
  46.                     target = *it;
  47.                     range = tmp;
  48.                 }
  49.             }
  50.            
  51.             if(target && selectTarget(target))
  52.                     return target;
  53.             break;
  54.         }
  55.         default:
  56.         {
  57.             if(!playerList.empty()) {
  58.                 CreatureList::iterator it = playerList.begin();
  59.                 std::advance(it, random_range(0, playerList.size() - 1));
  60.                
  61.                 #ifdef __DEBUG__
  62.                     std::cout << "Selecting target " << (*it)->getName() << std::endl;
  63.                 #endif
  64.                 return selectTarget(*it);
  65.             }
  66.             else if(!resultList.empty()) {
  67.                 CreatureList::iterator it = resultList.begin();
  68.                 std::advance(it, random_range(0, resultList.size() - 1));
  69.                
  70.                 #ifdef __DEBUG__
  71.                     std::cout << "Selecting target " << (*it)->getName() << std::endl;
  72.                 #endif
  73.                 return selectTarget(*it);
  74.             }
  75.            
  76.             if(searchType == TARGETSEARCH_ATTACKRANGE)
  77.                     return false;
  78.             break;
  79.         }
  80.     }
  81.  
  82.  
  83.     //lets just pick the first target in the list
  84.     for(CreatureList::iterator it = targetList.begin(); it != targetList.end(); ++it)
  85.     {
  86.         if(followCreature == (*it) || !selectTarget(*it))
  87.             continue;
  88.  
  89.         #ifdef __DEBUG__
  90.             std::cout << "Selecting target " << (*it)->getName() << std::endl;
  91.         #endif
  92.         return true;
  93.     }
  94.  
  95.     return false;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement