Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool Monster::searchTarget(TargetSearchType_t searchType /*= TARGETSEARCH_DEFAULT*/)
- {
- #ifdef __DEBUG__
- std::cout << "Searching target... " << std::endl;
- #endif
- std::list<Creature*> resultList;
- std::list<Creature*> playerList;
- const Position& myPos = getPosition();
- for(CreatureList::iterator it = targetList.begin(); it != targetList.end(); ++it)
- {
- if(followCreature != (*it) && isTarget(*it) && (searchType == TARGETSEARCH_RANDOM || canUseAttack(myPos, *it)))
- {
- if(Creature* player = (*it)->getPlayer())
- playerList.push_back(*it);
- else
- resultList.push_back(*it);
- }
- }
- switch(searchType)
- {
- case TARGETSEARCH_NEAREST:
- {
- Creature* target = NULL;
- int32_t range = -1;
- if(!playerList.empty()) {
- for(CreatureList::iterator it = playerList.begin(); it != playerList.end(); ++it)
- {
- int32_t tmp = std::max(std::abs(myPos.x - (*it)->getPosition().x),
- std::abs(myPos.y - (*it)->getPosition().y));
- if(range >= 0 && tmp >= range)
- continue;
- target = *it;
- range = tmp;
- }
- }else{
- for(CreatureList::iterator it = resultList.begin(); it != resultList.end(); ++it)
- {
- int32_t tmp = std::max(std::abs(myPos.x - (*it)->getPosition().x),
- std::abs(myPos.y - (*it)->getPosition().y));
- if(range >= 0 && tmp >= range)
- continue;
- target = *it;
- range = tmp;
- }
- }
- if(target && selectTarget(target))
- return target;
- break;
- }
- default:
- {
- if(!playerList.empty()) {
- CreatureList::iterator it = playerList.begin();
- std::advance(it, random_range(0, playerList.size() - 1));
- #ifdef __DEBUG__
- std::cout << "Selecting target " << (*it)->getName() << std::endl;
- #endif
- return selectTarget(*it);
- }
- else if(!resultList.empty()) {
- CreatureList::iterator it = resultList.begin();
- std::advance(it, random_range(0, resultList.size() - 1));
- #ifdef __DEBUG__
- std::cout << "Selecting target " << (*it)->getName() << std::endl;
- #endif
- return selectTarget(*it);
- }
- if(searchType == TARGETSEARCH_ATTACKRANGE)
- return false;
- break;
- }
- }
- //lets just pick the first target in the list
- for(CreatureList::iterator it = targetList.begin(); it != targetList.end(); ++it)
- {
- if(followCreature == (*it) || !selectTarget(*it))
- continue;
- #ifdef __DEBUG__
- std::cout << "Selecting target " << (*it)->getName() << std::endl;
- #endif
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement