| 1 | /* | 
|---|
| 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 |  *                    > www.orxonox.net < | 
|---|
| 4 |  * | 
|---|
| 5 |  * | 
|---|
| 6 |  *   License notice: | 
|---|
| 7 |  * | 
|---|
| 8 |  *   This program is free software; you can redistribute it and/or | 
|---|
| 9 |  *   modify it under the terms of the GNU General Public License | 
|---|
| 10 |  *   as published by the Free Software Foundation; either version 2 | 
|---|
| 11 |  *   of the License, or (at your option) any later version. | 
|---|
| 12 |  * | 
|---|
| 13 |  *   This program is distributed in the hope that it will be useful, | 
|---|
| 14 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 |  *   GNU General Public License for more details. | 
|---|
| 17 |  * | 
|---|
| 18 |  *   You should have received a copy of the GNU General Public License | 
|---|
| 19 |  *   along with this program; if not, write to the Free Software | 
|---|
| 20 |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
| 21 |  * | 
|---|
| 22 |  *   Author: | 
|---|
| 23 |  *      Fabian 'x3n' Landau | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      Dominik Solenicki | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | #include "SectionController.h" | 
|---|
| 30 |  | 
|---|
| 31 | namespace orxonox | 
|---|
| 32 | { | 
|---|
| 33 |  | 
|---|
| 34 |     RegisterClass(SectionController); | 
|---|
| 35 |  | 
|---|
| 36 |     //Leaders share the fact that they have Wingmans | 
|---|
| 37 |     SectionController::SectionController(Context* context) : LeaderController(context) | 
|---|
| 38 |     { | 
|---|
| 39 |         RegisterObject(SectionController); | 
|---|
| 40 |         this->setFormationMode(FormationMode::FINGER4); | 
|---|
| 41 |  | 
|---|
| 42 |         this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&SectionController::action, this))); | 
|---|
| 43 |         this->myWingman_ = 0; | 
|---|
| 44 |         this->myDivisionLeader_ = 0; | 
|---|
| 45 |         this->rank_ = Rank::SECTIONLEADER; | 
|---|
| 46 |  | 
|---|
| 47 |         //orxout(internal_error) << this << "Was created" << endl; | 
|---|
| 48 |  | 
|---|
| 49 |     } | 
|---|
| 50 |     | 
|---|
| 51 |     SectionController::~SectionController() | 
|---|
| 52 |     { | 
|---|
| 53 |         | 
|---|
| 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---- | 
|---|
| 63 |     void SectionController::tick(float dt) | 
|---|
| 64 |     { | 
|---|
| 65 |          if (!this->isActive()) | 
|---|
| 66 |             return; | 
|---|
| 67 |         if (this->bHasTargetPosition_) | 
|---|
| 68 |         { | 
|---|
| 69 |             this->moveToTargetPosition(dt); | 
|---|
| 70 |         } | 
|---|
| 71 |         else if (this->bLookAtTarget_) | 
|---|
| 72 |         { | 
|---|
| 73 |             this->lookAtTarget(dt); | 
|---|
| 74 |         } | 
|---|
| 75 |         if (bShooting_) | 
|---|
| 76 |         { | 
|---|
| 77 |             this->doFire(); | 
|---|
| 78 |         } | 
|---|
| 79 |          | 
|---|
| 80 |         SUPER(SectionController, tick, dt); | 
|---|
| 81 |     } | 
|---|
| 82 |  | 
|---|
| 83 |     void SectionController::action() | 
|---|
| 84 |     { | 
|---|
| 85 |         //----If no leader, find one----   | 
|---|
| 86 |         if (!myDivisionLeader_) | 
|---|
| 87 |         { | 
|---|
| 88 |             LeaderController* newDivisionLeader = findNewDivisionLeader(); | 
|---|
| 89 |             this->myDivisionLeader_ = newDivisionLeader; | 
|---|
| 90 |  | 
|---|
| 91 |         } | 
|---|
| 92 |         //----If have leader---- | 
|---|
| 93 |         else | 
|---|
| 94 |         { | 
|---|
| 95 |         } | 
|---|
| 96 |  | 
|---|
| 97 |         //----action was set to fight---- | 
|---|
| 98 |         if (this->action_ == Action::FIGHT) | 
|---|
| 99 |         { | 
|---|
| 100 |             if (!this->hasTarget()) | 
|---|
| 101 |             { | 
|---|
| 102 |                 if (this->myDivisionLeader_) | 
|---|
| 103 |                 { | 
|---|
| 104 |                     this->chooseTarget();                 | 
|---|
| 105 |                 } | 
|---|
| 106 |                 else | 
|---|
| 107 |                 { | 
|---|
| 108 |                     this->setClosestTarget();  | 
|---|
| 109 |                 } | 
|---|
| 110 |             } | 
|---|
| 111 |             else | 
|---|
| 112 |             { | 
|---|
| 113 |                  | 
|---|
| 114 |                 //----fly in formation if far enough---- | 
|---|
| 115 |                 Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();          | 
|---|
| 116 |                 if (diffVector.length() > 3000) | 
|---|
| 117 |                 { | 
|---|
| 118 |                     this->setTargetPositionOfWingman(); | 
|---|
| 119 |                 }    | 
|---|
| 120 |                 else | 
|---|
| 121 |                 { | 
|---|
| 122 |                     //----wingmans shall support the fire of their leaders---- | 
|---|
| 123 |                     if (this->myWingman_) | 
|---|
| 124 |                     { | 
|---|
| 125 |                         this->myWingman_->setAction (Action::FIGHT, this->target_);                     | 
|---|
| 126 |                     } | 
|---|
| 127 |                 } | 
|---|
| 128 |             } | 
|---|
| 129 |             if (this->hasTarget()) | 
|---|
| 130 |             { | 
|---|
| 131 |                 //----choose where to go---- | 
|---|
| 132 |                 this->maneuver(); | 
|---|
| 133 |                 //----fire if you can---- | 
|---|
| 134 |                 this->bShooting_ = this->canFire();                 | 
|---|
| 135 |             } | 
|---|
| 136 |         } | 
|---|
| 137 |  | 
|---|
| 138 |         //----action was set to fly---- | 
|---|
| 139 |         else if (this->action_ == Action::FLY) | 
|---|
| 140 |         { | 
|---|
| 141 |             this->setTargetPositionOfWingman(); | 
|---|
| 142 |         } | 
|---|
| 143 |  | 
|---|
| 144 |         //----action was set to protect---- | 
|---|
| 145 |         else if (this->action_ == Action::PROTECT) | 
|---|
| 146 |         { | 
|---|
| 147 |  | 
|---|
| 148 |         } | 
|---|
| 149 |                  | 
|---|
| 150 |  | 
|---|
| 151 |     } | 
|---|
| 152 |     //PRE: myDivisionLeader_ != 0 && myDivisionLeader_->action_ == Action::FIGHT | 
|---|
| 153 |     //POST: this->target_ is set unless division leader doesn't have one | 
|---|
| 154 |     void SectionController::chooseTarget() | 
|---|
| 155 |     { | 
|---|
| 156 |         //----If division leader fights, cover him by fighting emenies close to his target---- | 
|---|
| 157 |         if (this->myDivisionLeader_->getAction() == Action::FIGHT) | 
|---|
| 158 |         { | 
|---|
| 159 |             //----if he has a target---- | 
|---|
| 160 |             if (this->myDivisionLeader_->hasTarget()) | 
|---|
| 161 |             { | 
|---|
| 162 |                 //----try to find a new target if division leader has wingman (doing fine) and no good target already set---- | 
|---|
| 163 |                 if ( this->myDivisionLeader_->hasWingman() &&  | 
|---|
| 164 |                     !( this->hasTarget() && this->getTarget() != this->myDivisionLeader_->getTarget() ) ) | 
|---|
| 165 |                 { | 
|---|
| 166 |  | 
|---|
| 167 |                     bool foundTarget = false; | 
|---|
| 168 |                     //----new target should be close to division's target---- | 
|---|
| 169 |                     Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition(); | 
|---|
| 170 |                      | 
|---|
| 171 |                     for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) | 
|---|
| 172 |                     { | 
|---|
| 173 |                         //----is enemy?---- | 
|---|
| 174 |                         if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) ) | 
|---|
| 175 |                             continue;            | 
|---|
| 176 |                         //----in range?---- | 
|---|
| 177 |                         if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 &&  | 
|---|
| 178 |                             (*itP) != this->myDivisionLeader_->getTarget()) | 
|---|
| 179 |                         { | 
|---|
| 180 |                             foundTarget = true; | 
|---|
| 181 |                             this->setAction(Action::FIGHT, (*itP)); | 
|---|
| 182 |                             //orxout(internal_error) << "Found target" << endl; | 
|---|
| 183 |                             break;  | 
|---|
| 184 |                         } | 
|---|
| 185 |                     } | 
|---|
| 186 |                     //----no target? then attack same target as division leader---- | 
|---|
| 187 |                     if (!foundTarget) | 
|---|
| 188 |                     { | 
|---|
| 189 |                         this->setAction(Action::FIGHT, this->myDivisionLeader_->getTarget()); | 
|---|
| 190 |                     } | 
|---|
| 191 |                 } | 
|---|
| 192 |                 //----if division leader doesn't have a wingman, support his fire---- | 
|---|
| 193 |                 else | 
|---|
| 194 |                 { | 
|---|
| 195 |                     this->setAction(Action::FIGHT, this->myDivisionLeader_->getTarget()); | 
|---|
| 196 |                 } | 
|---|
| 197 |             } | 
|---|
| 198 |             //----If he fights but doesn't have a target, wait for him to get one---- | 
|---|
| 199 |             else | 
|---|
| 200 |             { | 
|---|
| 201 |  | 
|---|
| 202 |             } | 
|---|
| 203 |         }  | 
|---|
| 204 |     } | 
|---|
| 205 |  | 
|---|
| 206 |     //----stay in formation---- | 
|---|
| 207 |     //gani-TODO: sum targetAbso... and this->predicted position  | 
|---|
| 208 |     void SectionController::setTargetPositionOfWingman() | 
|---|
| 209 |     { | 
|---|
| 210 |         if (!this->myWingman_) | 
|---|
| 211 |             return; | 
|---|
| 212 |         Vector3* targetRelativePositionOfWingman; | 
|---|
| 213 |         switch (this->formationMode_){ | 
|---|
| 214 |             case FormationMode::WALL: | 
|---|
| 215 |             { | 
|---|
| 216 |                 targetRelativePositionOfWingman = new Vector3 (-400, 0, 0);   | 
|---|
| 217 |                 break; | 
|---|
| 218 |             } | 
|---|
| 219 |             case FormationMode::FINGER4:  | 
|---|
| 220 |             { | 
|---|
| 221 |                 targetRelativePositionOfWingman = new Vector3 (-400, 0, 200);   | 
|---|
| 222 |                 break; | 
|---|
| 223 |             } | 
|---|
| 224 |             case FormationMode::DIAMOND:  | 
|---|
| 225 |             { | 
|---|
| 226 |                 targetRelativePositionOfWingman = new Vector3 (400, -200, 0);                   | 
|---|
| 227 |                 break; | 
|---|
| 228 |             } | 
|---|
| 229 |         } | 
|---|
| 230 |         Quaternion orient = this->getControllableEntity()->getWorldOrientation(); | 
|---|
| 231 |          | 
|---|
| 232 |         Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) +  | 
|---|
| 233 |         (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman))); | 
|---|
| 234 |          | 
|---|
| 235 |         myWingman_->setAction (Action::FLY, targetAbsolutePositionOfWingman, orient); | 
|---|
| 236 |         | 
|---|
| 237 |     } | 
|---|
| 238 |  | 
|---|
| 239 |     LeaderController* SectionController::findNewDivisionLeader() | 
|---|
| 240 |     { | 
|---|
| 241 |  | 
|---|
| 242 |         if (!this->getControllableEntity()) | 
|---|
| 243 |             return 0; | 
|---|
| 244 |  | 
|---|
| 245 |         LeaderController* closestLeader = 0; | 
|---|
| 246 |         float minDistance =  std::numeric_limits<float>::infinity(); | 
|---|
| 247 |         //go through all pawns | 
|---|
| 248 |         for (ObjectList<LeaderController>::iterator it = ObjectList<LeaderController>::begin(); it; ++it) | 
|---|
| 249 |         { | 
|---|
| 250 |             //0ptr or not DivisionController? | 
|---|
| 251 |             if (!(it) || !((it)->getRank() == Rank::DIVISIONLEADER) || !(it->getControllableEntity())) | 
|---|
| 252 |                 continue; | 
|---|
| 253 |             //same team? | 
|---|
| 254 |             if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam())) | 
|---|
| 255 |                 continue; | 
|---|
| 256 |  | 
|---|
| 257 |             //is equal to this? | 
|---|
| 258 |             if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity()) | 
|---|
| 259 |                 continue; | 
|---|
| 260 |  | 
|---|
| 261 |             float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); | 
|---|
| 262 |              | 
|---|
| 263 |             if (distance < minDistance && !(it->hasFollower())) | 
|---|
| 264 |             { | 
|---|
| 265 |                 closestLeader = *it; | 
|---|
| 266 |                 minDistance = distance; | 
|---|
| 267 |             } | 
|---|
| 268 |            | 
|---|
| 269 |         } | 
|---|
| 270 |         if (closestLeader) | 
|---|
| 271 |         { | 
|---|
| 272 |             if (closestLeader->setFollower(this)) | 
|---|
| 273 |                 return closestLeader; | 
|---|
| 274 |         } | 
|---|
| 275 |         return 0; | 
|---|
| 276 |  | 
|---|
| 277 |     } | 
|---|
| 278 |     bool SectionController::setWingman(CommonController* cwingman) | 
|---|
| 279 |     { | 
|---|
| 280 |         WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman); | 
|---|
| 281 |  | 
|---|
| 282 |         if (!this->myWingman_) | 
|---|
| 283 |         { | 
|---|
| 284 |             this->myWingman_ = wingman; | 
|---|
| 285 |             return true; | 
|---|
| 286 |         } | 
|---|
| 287 |         else | 
|---|
| 288 |         { | 
|---|
| 289 |             return false; | 
|---|
| 290 |         } | 
|---|
| 291 |     } | 
|---|
| 292 |      | 
|---|
| 293 |     bool SectionController::hasWingman() | 
|---|
| 294 |     { | 
|---|
| 295 |         if (this->myWingman_) | 
|---|
| 296 |             return true; | 
|---|
| 297 |         else | 
|---|
| 298 |             return false; | 
|---|
| 299 |     } | 
|---|
| 300 |  | 
|---|
| 301 |      | 
|---|
| 302 |      | 
|---|
| 303 |     | 
|---|
| 304 |  | 
|---|
| 305 | } | 
|---|