- Timestamp:
- Nov 22, 2015, 5:06:38 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/AI_HS15/src/orxonox/controllers/SectionController.cc
r10805 r10826 34 34 RegisterClass(SectionController); 35 35 36 //Leaders share the fact that they have Wingmans 36 37 SectionController::SectionController(Context* context) : LeaderController(context) 37 38 { … … 44 45 this->rank_ = Rank::SECTIONLEADER; 45 46 46 orxout(internal_error) << this << "Was created" << endl;47 //orxout(internal_error) << this << "Was created" << endl; 47 48 48 49 } … … 52 53 53 54 } 54 55 void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode) 56 { 57 SUPER(SectionController, XMLPort, xmlelement, mode); 58 59 //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); 60 } 61 62 //----in tick, move (or look) and shoot---- 55 63 void SectionController::tick(float dt) 56 64 { … … 75 83 void SectionController::action() 76 84 { 77 // this->target_ = this->sectionTarget_;85 //----If no leader, find one---- 78 86 if (!myDivisionLeader_) 79 87 { 80 88 LeaderController* newDivisionLeader = findNewDivisionLeader(); 81 89 this->myDivisionLeader_ = newDivisionLeader; 90 82 91 if (newDivisionLeader) 83 orxout(internal_error) << "new DivisionLeader set" << endl; 92 { 93 //orxout(internal_error) << "new DivisionLeader set" << endl; 94 } 95 //----If no leader found, attack someone---- 96 //----TODO: find closest enemy---- 84 97 else 85 98 { 86 for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) 87 { 88 if (this->getControllableEntity()->getTeam() == static_cast<ControllableEntity*>(*itP)->getTeam()) 89 continue; 90 91 if (!this->myDivisionLeader_) 99 if ( !this->hasTarget() || this->action_ != Action::FIGHT ) 100 { 101 for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) 92 102 { 103 if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) ) 104 continue; 105 93 106 this->setAction(Action::FIGHT, (*itP)); 94 break; 107 break; 108 } 109 } 110 111 } 112 113 } 114 //----If have leader---- 115 else 116 { 117 this->chooseTarget(); 118 } 119 120 //----action was set to fight---- 121 if (this->action_ == Action::FIGHT) 122 { 123 //----choose where to go---- 124 this->maneuver(); 125 //----fire if you can---- 126 this->bShooting_ = this->canFire(); 127 128 if (this->target_) 129 { 130 //----wingmans shall support the fire of their leaders---- 131 if (this->myWingman_) 132 { 133 this->myWingman_->setAction (Action::FIGHT, this->target_); 134 } 135 //----fly in formation if far enough---- 136 Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); 137 if (diffVector.length() > 3000) 138 { 139 this->setTargetPositionOfWingman(); 140 } 141 } 142 } 143 144 //----action was set to fly---- 145 else if (this->action_ == Action::FLY) 146 { 147 this->setTargetPositionOfWingman(); 148 } 149 150 //----action was set to protect---- 151 else if (this->action_ == Action::PROTECT) 152 { 153 154 } 155 156 157 } 158 //PRE: myDivisionLeader_ != 0 159 //POST: this->target_ is set unless division leader doesn't have one 160 void SectionController::chooseTarget() 161 { 162 //----If division leader fights, cover him by fighting emenies close to his target---- 163 if (this->myDivisionLeader_->getAction() == Action::FIGHT) 164 { 165 //----if he has a target---- 166 if (this->myDivisionLeader_->hasTarget()) 167 { 168 //----try to find a new target if division leader has wingman (doing fine) and no good target already set---- 169 if ( this->myDivisionLeader_->hasWingman() && 170 !( this->hasTarget() && this->getTarget() != this->myDivisionLeader_->getTarget() ) ) 171 { 172 173 bool foundTarget = false; 174 //----new target should be close to division's target---- 175 Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition(); 176 177 for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) 178 { 179 //----is enemy?---- 180 if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) ) 181 continue; 182 //----in range?---- 183 if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 && 184 (*itP) != this->myDivisionLeader_->getTarget()) 185 { 186 foundTarget = true; 187 this->setAction(Action::FIGHT, (*itP)); 188 //orxout(internal_error) << "Found target" << endl; 189 break; 190 } 95 191 } 96 97 } 98 } 99 100 } 101 else 102 { 103 if (this->myDivisionLeader_->getAction() == Action::FIGHT) 104 { 105 if (this->myDivisionLeader_->hasTarget()) 106 { 107 if (this->myDivisionLeader_->hasWingman() && (!this->hasTarget() || this->getTarget() == this->myDivisionLeader_->getTarget())) 192 //----no target? then attack same target as division leader---- 193 if (!foundTarget) 108 194 { 109 bool foundTarget = false; 110 Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition(); 111 for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) 112 { 113 if (this->getControllableEntity()->getTeam() == static_cast<ControllableEntity*>(*itP)->getTeam()) 114 continue; 115 116 if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 && 117 ((*itP)->getWorldPosition() - divisionTargetPosition).length() > 1000 && 118 (*itP) != this->myDivisionLeader_->getTarget()) 119 { 120 foundTarget = true; 121 this->setAction(Action::FIGHT, (*itP)); 122 orxout(internal_error) << "Found target" << endl; 123 break; 124 } 125 } 126 if (!foundTarget) 127 { 128 this->setAction(Action::FIGHT, this->myDivisionLeader_->getTarget()); 129 } 130 195 this->setAction(Action::FIGHT, this->myDivisionLeader_->getTarget()); 131 196 } 132 197 } 133 } 134 } 135 136 if (this->action_ == Action::FIGHT) 137 { 138 this->maneuver(); 139 this->bShooting_ = this->canFire(); 140 if (this->target_) 141 { 142 if (this->myWingman_) 143 { 144 this->myWingman_->setAction (Action::FIGHT, this->target_); 198 //----if division leader doesn't have a wingman, support his fire---- 199 else 200 { 201 this->setAction(Action::FIGHT, this->myDivisionLeader_->getTarget()); 145 202 } 146 Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); 147 if (diffVector.length() > 3000) 148 { 149 this->setTargetPositionOfWingman(); 150 } 151 } 152 } 153 else if (this->action_ == Action::FLY) 154 { 155 this->setTargetPositionOfWingman(); 156 } 157 else if (this->action_ == Action::PROTECT) 158 { 159 160 } 161 162 163 } 164 203 } 204 //----If he fights but doesn't have a target, wait for him to get one---- 205 else 206 { 207 208 } 209 } 210 } 211 212 //----stay in formation---- 165 213 void SectionController::setTargetPositionOfWingman() 166 214 { … … 177 225 { 178 226 targetRelativePositionOfWingman = new Vector3 (-400, 0, -200); 179 break;180 }181 case FormationMode::VEE:182 {183 227 break; 184 228 } … … 197 241 198 242 } 243 199 244 LeaderController* SectionController::findNewDivisionLeader() 200 245 { … … 219 264 continue; 220 265 221 222 float distance = ((it)->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length(); 266 float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); 223 267 224 268 if (distance < minDistance && !(it->hasFollower())) … … 260 304 } 261 305 262 void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode) 263 { 264 SUPER(SectionController, XMLPort, xmlelement, mode); 265 266 //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); 267 } 268 306 269 307 270 308
Note: See TracChangeset
for help on using the changeset viewer.