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