[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 | { |
---|
[10946] | 60 | if (!this || !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 | if (!this || !this->getControllableEntity()) |
---|
| 67 | return; |
---|
| 68 | |
---|
[10731] | 69 | this->myLeader_ = newLeader; |
---|
[10879] | 70 | if (this->myLeader_) |
---|
| 71 | { |
---|
[10923] | 72 | |
---|
[10879] | 73 | } |
---|
[10731] | 74 | } |
---|
[10826] | 75 | //----If have leader, he will deal with logic---- |
---|
[10731] | 76 | else |
---|
| 77 | { |
---|
[10851] | 78 | |
---|
[10731] | 79 | } |
---|
[10851] | 80 | if (!this->myLeader_) |
---|
| 81 | { |
---|
[10953] | 82 | ActionpointController::action(); |
---|
[10805] | 83 | } |
---|
[10854] | 84 | else if (this->myLeader_) |
---|
[10805] | 85 | { |
---|
[10974] | 86 | if (this->myLeader_->bKeepFormation_ || !(this->myLeader_->getAction() == Action::FIGHT |
---|
| 87 | || this->myLeader_->getAction() == Action::FIGHTALL |
---|
[10883] | 88 | || this->myLeader_->getAction() == Action::ATTACK)) |
---|
[10856] | 89 | { |
---|
[10886] | 90 | this->keepFormation(); |
---|
[10879] | 91 | } |
---|
[10886] | 92 | else if (!this->myLeader_->bKeepFormation_) |
---|
[10879] | 93 | { |
---|
[10925] | 94 | if (!this || !this->getControllableEntity()) |
---|
| 95 | return; |
---|
| 96 | |
---|
[10886] | 97 | if (!this->hasTarget()) |
---|
[10883] | 98 | { |
---|
[10886] | 99 | this->setTarget(this->myLeader_->getTarget()); |
---|
[10856] | 100 | } |
---|
[10935] | 101 | |
---|
[10856] | 102 | } |
---|
[10805] | 103 | } |
---|
[10731] | 104 | } |
---|
| 105 | |
---|
| 106 | |
---|
[10856] | 107 | Vector3 WingmanController::getFormationPosition () |
---|
| 108 | { |
---|
| 109 | this->setFormationMode( this->myLeader_->getFormationMode() ); |
---|
[10883] | 110 | this->spread_ = this->myLeader_->getSpread(); |
---|
[10869] | 111 | if (this->myLeader_->getIdentifier()->getName() == "DivisionController") |
---|
[10856] | 112 | { |
---|
| 113 | switch (this->formationMode_){ |
---|
| 114 | case FormationMode::WALL: |
---|
[11071] | 115 | return Vector3 (2.0f*this->spread_, 0, 0 - 1.0f*this->tolerance_); |
---|
[10856] | 116 | case FormationMode::FINGER4: |
---|
[11071] | 117 | return Vector3 (2.0f*this->spread_, 0, this->spread_ - 1.0f*this->tolerance_); |
---|
[10856] | 118 | case FormationMode::DIAMOND: |
---|
[11071] | 119 | return Vector3 (2.0f*this->spread_, 0, this->spread_ - 1.0f*this->tolerance_); |
---|
| 120 | default: |
---|
| 121 | return Vector3::ZERO; |
---|
[10856] | 122 | } |
---|
| 123 | } |
---|
| 124 | else |
---|
| 125 | { |
---|
| 126 | switch (this->formationMode_){ |
---|
| 127 | case FormationMode::WALL: |
---|
[11071] | 128 | return Vector3 (-2.0f*this->spread_, 0, 0 - 1.0f*this->tolerance_); |
---|
[10856] | 129 | case FormationMode::FINGER4: |
---|
[11071] | 130 | return Vector3 (-2.0f*this->spread_, 0, this->spread_ - 1.0f*this->tolerance_); |
---|
[10856] | 131 | case FormationMode::DIAMOND: |
---|
[11071] | 132 | return Vector3 (2.0f*this->spread_, -1.0f*this->spread_, 0 - 1.0f*this->tolerance_); |
---|
| 133 | default: |
---|
| 134 | return Vector3::ZERO; |
---|
[10856] | 135 | } |
---|
| 136 | } |
---|
| 137 | } |
---|
[10886] | 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 | } |
---|
[10826] | 147 | //----POST: closest leader that is ready to take a new wingman is returned---- |
---|
[10877] | 148 | ActionpointController* WingmanController::findNewLeader() |
---|
[10717] | 149 | { |
---|
| 150 | |
---|
| 151 | if (!this->getControllableEntity()) |
---|
[11071] | 152 | return nullptr; |
---|
[10717] | 153 | |
---|
[10826] | 154 | //----vars for finding the closest leader---- |
---|
[11071] | 155 | ActionpointController* closestLeader = nullptr; |
---|
[10722] | 156 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
[10838] | 157 | Gametype* gt = this->getGametype(); |
---|
[10877] | 158 | |
---|
[11071] | 159 | for (ActionpointController* controller : ObjectList<ActionpointController>()) |
---|
[10717] | 160 | { |
---|
[10826] | 161 | //----0ptr or not a leader or dead?---- |
---|
[11071] | 162 | if (!controller || |
---|
| 163 | (controller->getIdentifier()->getName() != "SectionController" && controller->getIdentifier()->getName() != "DivisionController") || |
---|
| 164 | !(controller->getControllableEntity())) |
---|
[10722] | 165 | continue; |
---|
[10826] | 166 | |
---|
| 167 | //----same team?---- |
---|
[11071] | 168 | if ( !CommonController::sameTeam (this->getControllableEntity(), controller->getControllableEntity(), gt) ) |
---|
[10717] | 169 | continue; |
---|
[10826] | 170 | |
---|
| 171 | //----check distance---- |
---|
[11071] | 172 | float distance = CommonController::distance (controller->getControllableEntity(), this->getControllableEntity()); |
---|
| 173 | if (distance < minDistance && !(controller->hasWingman())) |
---|
[10722] | 174 | { |
---|
[11071] | 175 | closestLeader = controller; |
---|
[10722] | 176 | minDistance = distance; |
---|
| 177 | } |
---|
[10717] | 178 | } |
---|
[10722] | 179 | if (closestLeader) |
---|
| 180 | { |
---|
[10826] | 181 | //----Racing conditions---- |
---|
[10974] | 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*/ |
---|
[10877] | 184 | if (closestLeader->setWingman(orxonox_cast<ActionpointController*>(this))) |
---|
| 185 | { |
---|
[10722] | 186 | return closestLeader; |
---|
[10877] | 187 | } |
---|
[10722] | 188 | } |
---|
[11071] | 189 | return nullptr; |
---|
[10717] | 190 | } |
---|
[10722] | 191 | |
---|
[10678] | 192 | } |
---|