| 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 |  *      Gani Aliguzhinov | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      ... | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | #include "WingmanController.h" | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 | namespace orxonox | 
|---|
| 33 | { | 
|---|
| 34 |  | 
|---|
| 35 |     RegisterClass(WingmanController); | 
|---|
| 36 |      | 
|---|
| 37 |     //ActionpointController contains all common functionality of AI Controllers | 
|---|
| 38 |     WingmanController::WingmanController(Context* context) : ActionpointController(context) | 
|---|
| 39 |     { | 
|---|
| 40 |         RegisterObject(WingmanController); | 
|---|
| 41 |         //this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&WingmanController::action, this))); | 
|---|
| 42 |         this->myLeader_ = 0; | 
|---|
| 43 |         this->bFirstAction_ = true; | 
|---|
| 44 |  | 
|---|
| 45 |     } | 
|---|
| 46 |  | 
|---|
| 47 |     WingmanController::~WingmanController() | 
|---|
| 48 |     { | 
|---|
| 49 |         for (size_t i = 0; i < this->actionpoints_.size(); ++i) | 
|---|
| 50 |         { | 
|---|
| 51 |             if(this->actionpoints_[i]) | 
|---|
| 52 |                 this->actionpoints_[i]->destroy(); | 
|---|
| 53 |         } | 
|---|
| 54 |         this->parsedActionpoints_.clear(); | 
|---|
| 55 |         this->actionpoints_.clear(); | 
|---|
| 56 |     } | 
|---|
| 57 |    | 
|---|
| 58 |     void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 59 |     { | 
|---|
| 60 |         SUPER(WingmanController, XMLPort, xmlelement, mode); | 
|---|
| 61 |     } | 
|---|
| 62 |      | 
|---|
| 63 |     //----in tick, move (or look) and shoot---- | 
|---|
| 64 |     void WingmanController::tick(float dt) | 
|---|
| 65 |     {    | 
|---|
| 66 |         if (!this->isActive()) | 
|---|
| 67 |             return;  | 
|---|
| 68 |          | 
|---|
| 69 |         SUPER(WingmanController, tick, dt); | 
|---|
| 70 |         if (this->timeOffset_ >= this->actionTime_ && this->timeOffset_ <= this->actionTime_ + 0.5f && !this->bActionCalled_) | 
|---|
| 71 |         { | 
|---|
| 72 |             this->action(); | 
|---|
| 73 |             this->bActionCalled_ = true; | 
|---|
| 74 |         } | 
|---|
| 75 |         if (this->timeOffset_ <= 0.5f) | 
|---|
| 76 |         { | 
|---|
| 77 |             this->bActionCalled_ = false; | 
|---|
| 78 |         } | 
|---|
| 79 |     } | 
|---|
| 80 |      | 
|---|
| 81 |     //----action for hard calculations---- | 
|---|
| 82 |     void WingmanController::action() | 
|---|
| 83 |     { | 
|---|
| 84 |  | 
|---|
| 85 |         //----If no leader, find one---- | 
|---|
| 86 |         if (!this->myLeader_) | 
|---|
| 87 |         { | 
|---|
| 88 |             ActionpointController* newLeader = (findNewLeader()); | 
|---|
| 89 |             this->myLeader_ = newLeader; | 
|---|
| 90 |             if (this->myLeader_) | 
|---|
| 91 |             { | 
|---|
| 92 |                 //spread copyOrientation called equally among the division | 
|---|
| 93 |                 if (this->myLeader_->getIdentifier()->getName() == "SectionController") | 
|---|
| 94 |                     this->actionCounter_ = 1; | 
|---|
| 95 |                 else | 
|---|
| 96 |                     this->actionCounter_ = 4; | 
|---|
| 97 |             } | 
|---|
| 98 |         } | 
|---|
| 99 |         //----If have leader, he will deal with logic---- | 
|---|
| 100 |         else | 
|---|
| 101 |         { | 
|---|
| 102 |  | 
|---|
| 103 |         } | 
|---|
| 104 |         if (!this->myLeader_) | 
|---|
| 105 |         { | 
|---|
| 106 |            ActionpointController::action(); | 
|---|
| 107 |         } | 
|---|
| 108 |         else if (this->myLeader_) | 
|---|
| 109 |         { | 
|---|
| 110 |             if (this->myLeader_->bKeepFormation_ || !(this->myLeader_->getAction() == Action::FIGHT || this->myLeader_->getAction() == Action::FIGHTALL | 
|---|
| 111 |                 || this->myLeader_->getAction() == Action::ATTACK)) | 
|---|
| 112 |             { | 
|---|
| 113 |                 this->keepFormation(); | 
|---|
| 114 |             } | 
|---|
| 115 |             else if (!this->myLeader_->bKeepFormation_) | 
|---|
| 116 |             { | 
|---|
| 117 |                 if (!this->hasTarget()) | 
|---|
| 118 |                 { | 
|---|
| 119 |                     this->setTarget(this->myLeader_->getTarget()); | 
|---|
| 120 |                 } | 
|---|
| 121 |                 if (this->hasTarget()) | 
|---|
| 122 |                 { | 
|---|
| 123 |                     // this->maneuver(); | 
|---|
| 124 |                     // this->bShooting_ = this->canFire(); | 
|---|
| 125 |                     // Vector3 healthPosition = bestHealthPickup((this->target_->getWorldPosition() - this->getControllableEntity()->getWorldPosition()).length()); | 
|---|
| 126 |                     // if ((this->getControllableEntity()->getWorldPosition() - healthPosition).length() < this->tolerance_) | 
|---|
| 127 |                     // { | 
|---|
| 128 |                     //     //----choose where to go---- | 
|---|
| 129 |                     //     this->maneuver(); | 
|---|
| 130 |                     // } | 
|---|
| 131 |                     // else | 
|---|
| 132 |                     // { | 
|---|
| 133 |                     //     this->dodgeTowards(healthPosition); | 
|---|
| 134 |                     // } | 
|---|
| 135 |                     // //----fire if you can---- | 
|---|
| 136 |                     // this->bShooting_ = this->canFire();                | 
|---|
| 137 |                 } | 
|---|
| 138 |             } | 
|---|
| 139 |         } | 
|---|
| 140 |         this->actionCounter_ += this->actionCounter_ < 100000 ? 1 : -this->actionCounter_ ; | 
|---|
| 141 |     } | 
|---|
| 142 |       | 
|---|
| 143 |     | 
|---|
| 144 |     Vector3 WingmanController::getFormationPosition () | 
|---|
| 145 |     { | 
|---|
| 146 |         this->setFormationMode( this->myLeader_->getFormationMode() ); | 
|---|
| 147 |         Vector3* targetRelativePosition; | 
|---|
| 148 |         this->spread_ = this->myLeader_->getSpread(); | 
|---|
| 149 |         if (this->myLeader_->getIdentifier()->getName() == "DivisionController") | 
|---|
| 150 |         { | 
|---|
| 151 |             switch (this->formationMode_){ | 
|---|
| 152 |                 case FormationMode::WALL: | 
|---|
| 153 |                 { | 
|---|
| 154 |                     targetRelativePosition = new Vector3 (2*this->spread_, 0, 0 - this->tolerance_);   | 
|---|
| 155 |                     break; | 
|---|
| 156 |                 } | 
|---|
| 157 |                 case FormationMode::FINGER4:  | 
|---|
| 158 |                 { | 
|---|
| 159 |                     targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_);   | 
|---|
| 160 |                     break; | 
|---|
| 161 |                 } | 
|---|
| 162 |                 case FormationMode::DIAMOND:  | 
|---|
| 163 |                 { | 
|---|
| 164 |                     targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_);                   | 
|---|
| 165 |                     break; | 
|---|
| 166 |                 } | 
|---|
| 167 |             } | 
|---|
| 168 |         } | 
|---|
| 169 |         else | 
|---|
| 170 |         { | 
|---|
| 171 |  | 
|---|
| 172 |             switch (this->formationMode_){ | 
|---|
| 173 |                 case FormationMode::WALL: | 
|---|
| 174 |                 { | 
|---|
| 175 |                     targetRelativePosition = new Vector3 (-2*this->spread_, 0, 0 - this->tolerance_);   | 
|---|
| 176 |                     break; | 
|---|
| 177 |                 } | 
|---|
| 178 |                 case FormationMode::FINGER4:  | 
|---|
| 179 |                 { | 
|---|
| 180 |                     targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_ - this->tolerance_);   | 
|---|
| 181 |                     break; | 
|---|
| 182 |                 } | 
|---|
| 183 |                 case FormationMode::DIAMOND:  | 
|---|
| 184 |                 { | 
|---|
| 185 |                     targetRelativePosition = new Vector3 (2*this->spread_, -this->spread_, 0 - this->tolerance_);                   | 
|---|
| 186 |                     break; | 
|---|
| 187 |                 } | 
|---|
| 188 |             } | 
|---|
| 189 |         } | 
|---|
| 190 |         Vector3 result = *targetRelativePosition; | 
|---|
| 191 |         delete targetRelativePosition; | 
|---|
| 192 |         return result; | 
|---|
| 193 |     } | 
|---|
| 194 |     void WingmanController::keepFormation() | 
|---|
| 195 |     { | 
|---|
| 196 |         this->bKeepFormation_ = true; | 
|---|
| 197 |         ControllableEntity* leaderEntity = this->myLeader_->getControllableEntity(); | 
|---|
| 198 |         Vector3 targetRelativePosition = this->getFormationPosition(); | 
|---|
| 199 |         if (!leaderEntity) | 
|---|
| 200 |             return; | 
|---|
| 201 |         FlyingController::keepFormation (leaderEntity, targetRelativePosition); | 
|---|
| 202 |     } | 
|---|
| 203 |     //----POST: closest leader that is ready to take a new wingman is returned---- | 
|---|
| 204 |     ActionpointController* WingmanController::findNewLeader() | 
|---|
| 205 |     { | 
|---|
| 206 |  | 
|---|
| 207 |         if (!this->getControllableEntity()) | 
|---|
| 208 |             return 0; | 
|---|
| 209 |  | 
|---|
| 210 |         //----vars for finding the closest leader---- | 
|---|
| 211 |         ActionpointController* closestLeader = 0; | 
|---|
| 212 |         float minDistance =  std::numeric_limits<float>::infinity(); | 
|---|
| 213 |         Gametype* gt = this->getGametype(); | 
|---|
| 214 |  | 
|---|
| 215 |         for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>::begin(); it; ++it) | 
|---|
| 216 |         { | 
|---|
| 217 |             //----0ptr or not a leader or dead?---- | 
|---|
| 218 |             if (!it ||  | 
|---|
| 219 |                 (it->getIdentifier()->getName() != "SectionController" && it->getIdentifier()->getName() != "DivisionController") ||  | 
|---|
| 220 |                 !(it->getControllableEntity())) | 
|---|
| 221 |                 continue; | 
|---|
| 222 |              | 
|---|
| 223 |             //----same team?---- | 
|---|
| 224 |             if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) ) | 
|---|
| 225 |                 continue; | 
|---|
| 226 |              | 
|---|
| 227 |             //----check distance---- | 
|---|
| 228 |             float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); | 
|---|
| 229 |             if (distance < minDistance && !(it->hasWingman())) | 
|---|
| 230 |             { | 
|---|
| 231 |                 closestLeader = *it; | 
|---|
| 232 |                 minDistance = distance; | 
|---|
| 233 |             } | 
|---|
| 234 |         } | 
|---|
| 235 |         if (closestLeader) | 
|---|
| 236 |         { | 
|---|
| 237 |             //----Racing conditions---- | 
|---|
| 238 |             if (closestLeader->setWingman(orxonox_cast<ActionpointController*>(this))) | 
|---|
| 239 |             { | 
|---|
| 240 |                 if (closestLeader->getIdentifier()->getName() == "SectionController") | 
|---|
| 241 |                 { | 
|---|
| 242 |                     this->actionTime_ = 1.0f; | 
|---|
| 243 |                 } | 
|---|
| 244 |                 else | 
|---|
| 245 |                 { | 
|---|
| 246 |                     this->actionTime_ = 1.5f; | 
|---|
| 247 |                 } | 
|---|
| 248 |                 return closestLeader; | 
|---|
| 249 |             } | 
|---|
| 250 |         } | 
|---|
| 251 |         return 0; | 
|---|
| 252 |     } | 
|---|
| 253 |  | 
|---|
| 254 | } | 
|---|