- Timestamp:
- Oct 31, 2015, 4:07:29 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/AI_HS15/src/orxonox/controllers/WingmanController.cc
r10729 r10731 41 41 this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&WingmanController::action, this))); 42 42 this->myLeader_ = 0; 43 this->rank_ = WINGMAN; 44 43 45 } 44 46 … … 48 50 } 49 51 50 CommonController* WingmanController::findNewLeader()51 {52 53 if (!this->getControllableEntity())54 return 0;55 56 CommonController* closestLeader = 0;57 float minDistance = std::numeric_limits<float>::infinity();58 59 for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it)60 {61 //0ptr?62 if (!it || !it->isLeader() || !(it->getControllableEntity()))63 continue;64 //same team?65 if (this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam())66 continue;67 //is equal to this?68 if (it->getControllableEntity() == this->getControllableEntity())69 continue;70 71 float distance = (it->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length();72 if (distance < minDistance && !(it->hasWingman()))73 {74 closestLeader = *it;75 minDistance = distance;76 }77 78 }79 if (closestLeader)80 {81 if (closestLeader->setWingman(this))82 return closestLeader;83 }84 return 0;85 }86 87 void WingmanController::action()88 {89 if (!this->myLeader_)90 {91 CommonController* newLeader = findNewLeader();92 this->myLeader_ = newLeader;93 if (newLeader)94 orxout(internal_error) << "new Leader set" << endl;95 else96 orxout(internal_error) << "0 leader" << endl;97 98 }99 else100 {101 102 }103 }104 /*//collect data for AI behaviour105 Vector3* meanOfEnemiesPtr = new Vector3(0.0,0.0,0.0);106 Vector3* meanOfAlliesPtr = new Vector3(0.0,0.0,0.0);107 Vector3 meanOfAllies = *meanOfAlliesPtr;108 Vector3 meanOfEnemies = *meanOfEnemiesPtr;109 110 111 for (ObjectList<AIController>::iterator it = ObjectList<AIController>::begin(); it; ++it)112 {113 114 Gametype* gt=this->getGametype();115 if (!gt)116 {117 gt=it->getGametype();118 }119 if (!FormationController::sameTeam(this->getControllableEntity(), it->getControllableEntity(),gt))120 {121 enemies_.push_back(*it);122 }123 else {124 allies_.push_back(*it);125 }126 }127 if (enemies_.size() != 0 && allies_.size() != 0){128 for (std::vector<WeakPtr<AIController> >::iterator it = enemies_.begin() ; it != enemies_.end(); ++it)129 meanOfEnemies += (*it)->getControllableEntity()->getWorldPosition();130 131 meanOfEnemies /= enemies_.size();132 133 for (std::vector<WeakPtr<AIController> >::iterator it = allies_.begin() ; it != allies_.end(); ++it)134 meanOfAllies += (*it)->getControllableEntity()->getWorldPosition();135 136 meanOfAllies /= allies_.size();137 138 //orxout(internal_error) << "There are " << enemies_Counter << " enemies_, mean position is " << meanOfEnemies << endl;139 orxout(internal_error) << "Distance is " << (meanOfEnemies-meanOfAllies).length() << endl;140 orxout(internal_error) << "mean of allies_ is " << meanOfAllies << ", with a size " << allies_.size() << endl;141 orxout(internal_error) << "mean of enemies_ is " << meanOfEnemies << ", with a size " << enemies_.size() << endl;142 }*/143 144 145 146 52 void WingmanController::tick(float dt) 147 53 { … … 172 78 SUPER(WingmanController, tick, dt); 173 79 } 80 81 void WingmanController::action() 82 { 83 if (!this->myLeader_) 84 { 85 CommonController* newLeader = findNewLeader(); 86 this->myLeader_ = newLeader; 87 if (newLeader) 88 orxout(internal_error) << "new Leader set" << endl; 89 else 90 orxout(internal_error) << "0 leader" << endl; 91 92 } 93 else 94 { 95 96 } 97 } 98 99 100 101 102 CommonController* WingmanController::findNewLeader() 103 { 104 105 if (!this->getControllableEntity()) 106 return 0; 107 108 CommonController* closestLeader = 0; 109 float minDistance = std::numeric_limits<float>::infinity(); 110 111 for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it) 112 { 113 //0ptr? 114 if (!it || 115 (it->getRank() != SECTIONLEADER && it->getRank() != DIVISIONLEADER) || 116 !(it->getControllableEntity())) 117 continue; 118 //same team? 119 if (this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam()) 120 continue; 121 //is equal to this? 122 if (it->getControllableEntity() == this->getControllableEntity()) 123 continue; 124 125 float distance = (it->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length(); 126 if (distance < minDistance && !(it->hasWingman())) 127 { 128 closestLeader = *it; 129 minDistance = distance; 130 } 131 132 } 133 if (closestLeader) 134 { 135 if (closestLeader->setWingman(this)) 136 return closestLeader; 137 } 138 return 0; 139 } 174 140 175 141 void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
Note: See TracChangeset
for help on using the changeset viewer.