| 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->myLeader_ = nullptr; | 
|---|
| 42 |         this->bFirstAction_ = true; | 
|---|
| 43 |  | 
|---|
| 44 |     } | 
|---|
| 45 |  | 
|---|
| 46 |     WingmanController::~WingmanController() | 
|---|
| 47 |     { | 
|---|
| 48 |         for (WorldEntity* actionpoint : this->actionpoints_) | 
|---|
| 49 |         { | 
|---|
| 50 |             if (actionpoint) | 
|---|
| 51 |                 actionpoint->destroy(); | 
|---|
| 52 |         } | 
|---|
| 53 |         this->parsedActionpoints_.clear(); | 
|---|
| 54 |         this->actionpoints_.clear(); | 
|---|
| 55 |     } | 
|---|
| 56 |      | 
|---|
| 57 |     //----action for hard calculations---- | 
|---|
| 58 |     void WingmanController::action() | 
|---|
| 59 |     { | 
|---|
| 60 |         if (!this->getControllableEntity() || !this->isActive()) | 
|---|
| 61 |             return; | 
|---|
| 62 |         //----If no leader, find one---- | 
|---|
| 63 |         if (!this->myLeader_) | 
|---|
| 64 |         { | 
|---|
| 65 |             ActionpointController* newLeader = (findNewLeader()); | 
|---|
| 66 |  | 
|---|
| 67 |             this->myLeader_ = newLeader; | 
|---|
| 68 |             if (this->myLeader_) | 
|---|
| 69 |             { | 
|---|
| 70 |                 | 
|---|
| 71 |             } | 
|---|
| 72 |         } | 
|---|
| 73 |         //----If have leader, he will deal with logic---- | 
|---|
| 74 |         else | 
|---|
| 75 |         { | 
|---|
| 76 |  | 
|---|
| 77 |         } | 
|---|
| 78 |         if (!this->myLeader_) | 
|---|
| 79 |         { | 
|---|
| 80 |             ActionpointController::action(); | 
|---|
| 81 |         } | 
|---|
| 82 |         else if (this->myLeader_) | 
|---|
| 83 |         { | 
|---|
| 84 |             if (this->myLeader_->bKeepFormation_ || !(this->myLeader_->getAction() == Action::FIGHT  | 
|---|
| 85 |                 || this->myLeader_->getAction() == Action::FIGHTALL | 
|---|
| 86 |                 || this->myLeader_->getAction() == Action::ATTACK)) | 
|---|
| 87 |             { | 
|---|
| 88 |                 this->keepFormation(); | 
|---|
| 89 |             } | 
|---|
| 90 |             else if (!this->myLeader_->bKeepFormation_) | 
|---|
| 91 |             { | 
|---|
| 92 |  | 
|---|
| 93 |                 if (!this->hasTarget()) | 
|---|
| 94 |                 { | 
|---|
| 95 |                     this->setTarget(this->myLeader_->getTarget()); | 
|---|
| 96 |                 } | 
|---|
| 97 |                  | 
|---|
| 98 |             } | 
|---|
| 99 |         } | 
|---|
| 100 |     } | 
|---|
| 101 |       | 
|---|
| 102 |     | 
|---|
| 103 |     Vector3 WingmanController::getFormationPosition () | 
|---|
| 104 |     { | 
|---|
| 105 |         this->setFormationMode( this->myLeader_->getFormationMode() ); | 
|---|
| 106 |         this->spread_ = this->myLeader_->getSpread(); | 
|---|
| 107 |         if (this->myLeader_->getIdentifier()->getName() == "DivisionController") | 
|---|
| 108 |         { | 
|---|
| 109 |             switch (this->formationMode_){ | 
|---|
| 110 |                 case FormationMode::WALL: | 
|---|
| 111 |                     return Vector3 (2.0f*this->spread_, 0, 0 - 1.0f*this->tolerance_); | 
|---|
| 112 |                 case FormationMode::FINGER4:  | 
|---|
| 113 |                     return Vector3 (2.0f*this->spread_, 0, this->spread_ - 1.0f*this->tolerance_); | 
|---|
| 114 |                 case FormationMode::DIAMOND:  | 
|---|
| 115 |                     return Vector3 (2.0f*this->spread_, 0, this->spread_ - 1.0f*this->tolerance_); | 
|---|
| 116 |                 default: | 
|---|
| 117 |                     return Vector3::ZERO; | 
|---|
| 118 |             } | 
|---|
| 119 |         } | 
|---|
| 120 |         else | 
|---|
| 121 |         { | 
|---|
| 122 |             switch (this->formationMode_){ | 
|---|
| 123 |                 case FormationMode::WALL: | 
|---|
| 124 |                     return Vector3 (-2.0f*this->spread_, 0, 0 - 1.0f*this->tolerance_); | 
|---|
| 125 |                 case FormationMode::FINGER4:  | 
|---|
| 126 |                     return Vector3 (-2.0f*this->spread_, 0, this->spread_ - 1.0f*this->tolerance_); | 
|---|
| 127 |                 case FormationMode::DIAMOND:  | 
|---|
| 128 |                     return Vector3 (2.0f*this->spread_, -1.0f*this->spread_, 0 - 1.0f*this->tolerance_); | 
|---|
| 129 |                 default: | 
|---|
| 130 |                     return Vector3::ZERO; | 
|---|
| 131 |             } | 
|---|
| 132 |         } | 
|---|
| 133 |     } | 
|---|
| 134 |     void WingmanController::keepFormation() | 
|---|
| 135 |     { | 
|---|
| 136 |         this->bKeepFormation_ = true; | 
|---|
| 137 |         ControllableEntity* leaderEntity = this->myLeader_->getControllableEntity(); | 
|---|
| 138 |         Vector3 targetRelativePosition = this->getFormationPosition(); | 
|---|
| 139 |         if (!leaderEntity) | 
|---|
| 140 |             return; | 
|---|
| 141 |         FlyingController::keepFormation (leaderEntity, targetRelativePosition); | 
|---|
| 142 |     } | 
|---|
| 143 |     //----POST: closest leader that is ready to take a new wingman is returned---- | 
|---|
| 144 |     ActionpointController* WingmanController::findNewLeader() | 
|---|
| 145 |     { | 
|---|
| 146 |  | 
|---|
| 147 |         if (!this->getControllableEntity()) | 
|---|
| 148 |             return nullptr; | 
|---|
| 149 |  | 
|---|
| 150 |         //----vars for finding the closest leader---- | 
|---|
| 151 |         ActionpointController* closestLeader = nullptr; | 
|---|
| 152 |         float minDistance =  std::numeric_limits<float>::infinity(); | 
|---|
| 153 |         Gametype* gt = this->getGametype(); | 
|---|
| 154 |  | 
|---|
| 155 |         for (ActionpointController* controller : ObjectList<ActionpointController>()) | 
|---|
| 156 |         { | 
|---|
| 157 |             //----0ptr or not a leader or dead?---- | 
|---|
| 158 |             if (!controller || | 
|---|
| 159 |                 (controller->getIdentifier()->getName() != "SectionController" && controller->getIdentifier()->getName() != "DivisionController") || | 
|---|
| 160 |                 !(controller->getControllableEntity())) | 
|---|
| 161 |                 continue; | 
|---|
| 162 |              | 
|---|
| 163 |             //----same team?---- | 
|---|
| 164 |             if ( !CommonController::sameTeam (this->getControllableEntity(), controller->getControllableEntity(), gt) ) | 
|---|
| 165 |                 continue; | 
|---|
| 166 |              | 
|---|
| 167 |             //----check distance---- | 
|---|
| 168 |             float distance = CommonController::distance (controller->getControllableEntity(), this->getControllableEntity()); | 
|---|
| 169 |             if (distance < minDistance && !(controller->hasWingman())) | 
|---|
| 170 |             { | 
|---|
| 171 |                 closestLeader = controller; | 
|---|
| 172 |                 minDistance = distance; | 
|---|
| 173 |             } | 
|---|
| 174 |         } | 
|---|
| 175 |         if (closestLeader) | 
|---|
| 176 |         { | 
|---|
| 177 |             //----Racing conditions---- | 
|---|
| 178 |             /*TODO: racing condition check is wrong and redundant, as there is no multithreading here, ticks get called one after another, | 
|---|
| 179 |             so it can be simplified to a check of whether leader got a wingman*/ | 
|---|
| 180 |             if (closestLeader->setWingman(orxonox_cast<ActionpointController*>(this))) | 
|---|
| 181 |             { | 
|---|
| 182 |                 return closestLeader; | 
|---|
| 183 |             } | 
|---|
| 184 |         } | 
|---|
| 185 |         return nullptr; | 
|---|
| 186 |     } | 
|---|
| 187 |  | 
|---|
| 188 | } | 
|---|