| [10678] | 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: | 
|---|
| [10885] | 23 |  *      Gani Aliguzhinov | 
|---|
| [10678] | 24 |  *   Co-authors: | 
|---|
| [10885] | 25 |  *      ... | 
|---|
| [10678] | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
 | 28 |  | 
|---|
 | 29 | #include "WingmanController.h" | 
|---|
 | 30 |  | 
|---|
 | 31 |  | 
|---|
 | 32 | namespace orxonox | 
|---|
 | 33 | { | 
|---|
 | 34 |  | 
|---|
 | 35 |     RegisterClass(WingmanController); | 
|---|
| [10729] | 36 |      | 
|---|
| [10864] | 37 |     //ActionpointController contains all common functionality of AI Controllers | 
|---|
 | 38 |     WingmanController::WingmanController(Context* context) : ActionpointController(context) | 
|---|
| [10678] | 39 |     { | 
|---|
 | 40 |         RegisterObject(WingmanController); | 
|---|
| [11071] | 41 |         this->myLeader_ = nullptr; | 
|---|
| [10851] | 42 |         this->bFirstAction_ = true; | 
|---|
 | 43 |  | 
|---|
| [10678] | 44 |     } | 
|---|
 | 45 |  | 
|---|
 | 46 |     WingmanController::~WingmanController() | 
|---|
 | 47 |     { | 
|---|
| [11071] | 48 |         for (WorldEntity* actionpoint : this->actionpoints_) | 
|---|
| [10854] | 49 |         { | 
|---|
| [11071] | 50 |             if (actionpoint) | 
|---|
 | 51 |                 actionpoint->destroy(); | 
|---|
| [10854] | 52 |         } | 
|---|
 | 53 |         this->parsedActionpoints_.clear(); | 
|---|
 | 54 |         this->actionpoints_.clear(); | 
|---|
| [10678] | 55 |     } | 
|---|
| [10826] | 56 |      | 
|---|
 | 57 |     //----action for hard calculations---- | 
|---|
| [10731] | 58 |     void WingmanController::action() | 
|---|
 | 59 |     { | 
|---|
| [11083] | 60 |         if (!this->getControllableEntity() || !this->isActive()) | 
|---|
| [10923] | 61 |             return; | 
|---|
| [10826] | 62 |         //----If no leader, find one---- | 
|---|
| [10731] | 63 |         if (!this->myLeader_) | 
|---|
 | 64 |         { | 
|---|
| [10877] | 65 |             ActionpointController* newLeader = (findNewLeader()); | 
|---|
| [10925] | 66 |  | 
|---|
| [10731] | 67 |             this->myLeader_ = newLeader; | 
|---|
| [10879] | 68 |             if (this->myLeader_) | 
|---|
 | 69 |             { | 
|---|
| [10923] | 70 |                 | 
|---|
| [10879] | 71 |             } | 
|---|
| [10731] | 72 |         } | 
|---|
| [10826] | 73 |         //----If have leader, he will deal with logic---- | 
|---|
| [10731] | 74 |         else | 
|---|
 | 75 |         { | 
|---|
| [10851] | 76 |  | 
|---|
| [10731] | 77 |         } | 
|---|
| [10851] | 78 |         if (!this->myLeader_) | 
|---|
 | 79 |         { | 
|---|
| [10953] | 80 |             ActionpointController::action(); | 
|---|
| [10805] | 81 |         } | 
|---|
| [10854] | 82 |         else if (this->myLeader_) | 
|---|
| [10805] | 83 |         { | 
|---|
| [10974] | 84 |             if (this->myLeader_->bKeepFormation_ || !(this->myLeader_->getAction() == Action::FIGHT  | 
|---|
 | 85 |                 || this->myLeader_->getAction() == Action::FIGHTALL | 
|---|
| [10883] | 86 |                 || this->myLeader_->getAction() == Action::ATTACK)) | 
|---|
| [10856] | 87 |             { | 
|---|
| [10886] | 88 |                 this->keepFormation(); | 
|---|
| [10879] | 89 |             } | 
|---|
| [10886] | 90 |             else if (!this->myLeader_->bKeepFormation_) | 
|---|
| [10879] | 91 |             { | 
|---|
| [10925] | 92 |  | 
|---|
| [10886] | 93 |                 if (!this->hasTarget()) | 
|---|
| [10883] | 94 |                 { | 
|---|
| [10886] | 95 |                     this->setTarget(this->myLeader_->getTarget()); | 
|---|
| [10856] | 96 |                 } | 
|---|
| [10935] | 97 |                  | 
|---|
| [10856] | 98 |             } | 
|---|
| [10805] | 99 |         } | 
|---|
| [10731] | 100 |     } | 
|---|
 | 101 |       | 
|---|
 | 102 |     | 
|---|
| [10856] | 103 |     Vector3 WingmanController::getFormationPosition () | 
|---|
 | 104 |     { | 
|---|
 | 105 |         this->setFormationMode( this->myLeader_->getFormationMode() ); | 
|---|
| [10883] | 106 |         this->spread_ = this->myLeader_->getSpread(); | 
|---|
| [10869] | 107 |         if (this->myLeader_->getIdentifier()->getName() == "DivisionController") | 
|---|
| [10856] | 108 |         { | 
|---|
 | 109 |             switch (this->formationMode_){ | 
|---|
 | 110 |                 case FormationMode::WALL: | 
|---|
| [11071] | 111 |                     return Vector3 (2.0f*this->spread_, 0, 0 - 1.0f*this->tolerance_); | 
|---|
| [10856] | 112 |                 case FormationMode::FINGER4:  | 
|---|
| [11071] | 113 |                     return Vector3 (2.0f*this->spread_, 0, this->spread_ - 1.0f*this->tolerance_); | 
|---|
| [10856] | 114 |                 case FormationMode::DIAMOND:  | 
|---|
| [11071] | 115 |                     return Vector3 (2.0f*this->spread_, 0, this->spread_ - 1.0f*this->tolerance_); | 
|---|
 | 116 |                 default: | 
|---|
 | 117 |                     return Vector3::ZERO; | 
|---|
| [10856] | 118 |             } | 
|---|
 | 119 |         } | 
|---|
 | 120 |         else | 
|---|
 | 121 |         { | 
|---|
 | 122 |             switch (this->formationMode_){ | 
|---|
 | 123 |                 case FormationMode::WALL: | 
|---|
| [11071] | 124 |                     return Vector3 (-2.0f*this->spread_, 0, 0 - 1.0f*this->tolerance_); | 
|---|
| [10856] | 125 |                 case FormationMode::FINGER4:  | 
|---|
| [11071] | 126 |                     return Vector3 (-2.0f*this->spread_, 0, this->spread_ - 1.0f*this->tolerance_); | 
|---|
| [10856] | 127 |                 case FormationMode::DIAMOND:  | 
|---|
| [11071] | 128 |                     return Vector3 (2.0f*this->spread_, -1.0f*this->spread_, 0 - 1.0f*this->tolerance_); | 
|---|
 | 129 |                 default: | 
|---|
 | 130 |                     return Vector3::ZERO; | 
|---|
| [10856] | 131 |             } | 
|---|
 | 132 |         } | 
|---|
 | 133 |     } | 
|---|
| [10886] | 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 |     } | 
|---|
| [10826] | 143 |     //----POST: closest leader that is ready to take a new wingman is returned---- | 
|---|
| [10877] | 144 |     ActionpointController* WingmanController::findNewLeader() | 
|---|
| [10717] | 145 |     { | 
|---|
 | 146 |  | 
|---|
 | 147 |         if (!this->getControllableEntity()) | 
|---|
| [11071] | 148 |             return nullptr; | 
|---|
| [10717] | 149 |  | 
|---|
| [10826] | 150 |         //----vars for finding the closest leader---- | 
|---|
| [11071] | 151 |         ActionpointController* closestLeader = nullptr; | 
|---|
| [10722] | 152 |         float minDistance =  std::numeric_limits<float>::infinity(); | 
|---|
| [10838] | 153 |         Gametype* gt = this->getGametype(); | 
|---|
| [10877] | 154 |  | 
|---|
| [11071] | 155 |         for (ActionpointController* controller : ObjectList<ActionpointController>()) | 
|---|
| [10717] | 156 |         { | 
|---|
| [10826] | 157 |             //----0ptr or not a leader or dead?---- | 
|---|
| [11071] | 158 |             if (!controller || | 
|---|
 | 159 |                 (controller->getIdentifier()->getName() != "SectionController" && controller->getIdentifier()->getName() != "DivisionController") || | 
|---|
 | 160 |                 !(controller->getControllableEntity())) | 
|---|
| [10722] | 161 |                 continue; | 
|---|
| [10826] | 162 |              | 
|---|
 | 163 |             //----same team?---- | 
|---|
| [11071] | 164 |             if ( !CommonController::sameTeam (this->getControllableEntity(), controller->getControllableEntity(), gt) ) | 
|---|
| [10717] | 165 |                 continue; | 
|---|
| [10826] | 166 |              | 
|---|
 | 167 |             //----check distance---- | 
|---|
| [11071] | 168 |             float distance = CommonController::distance (controller->getControllableEntity(), this->getControllableEntity()); | 
|---|
 | 169 |             if (distance < minDistance && !(controller->hasWingman())) | 
|---|
| [10722] | 170 |             { | 
|---|
| [11071] | 171 |                 closestLeader = controller; | 
|---|
| [10722] | 172 |                 minDistance = distance; | 
|---|
 | 173 |             } | 
|---|
| [10717] | 174 |         } | 
|---|
| [10722] | 175 |         if (closestLeader) | 
|---|
 | 176 |         { | 
|---|
| [10826] | 177 |             //----Racing conditions---- | 
|---|
| [10974] | 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*/ | 
|---|
| [10877] | 180 |             if (closestLeader->setWingman(orxonox_cast<ActionpointController*>(this))) | 
|---|
 | 181 |             { | 
|---|
| [10722] | 182 |                 return closestLeader; | 
|---|
| [10877] | 183 |             } | 
|---|
| [10722] | 184 |         } | 
|---|
| [11071] | 185 |         return nullptr; | 
|---|
| [10717] | 186 |     } | 
|---|
| [10722] | 187 |  | 
|---|
| [10678] | 188 | } | 
|---|