[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); |
---|
[10725] | 41 | this->myLeader_ = 0; |
---|
[10851] | 42 | this->bFirstAction_ = true; |
---|
| 43 | |
---|
[10678] | 44 | } |
---|
| 45 | |
---|
| 46 | WingmanController::~WingmanController() |
---|
| 47 | { |
---|
[10854] | 48 | for (size_t i = 0; i < this->actionpoints_.size(); ++i) |
---|
| 49 | { |
---|
| 50 | if(this->actionpoints_[i]) |
---|
| 51 | this->actionpoints_[i]->destroy(); |
---|
| 52 | } |
---|
| 53 | this->parsedActionpoints_.clear(); |
---|
| 54 | this->actionpoints_.clear(); |
---|
[10678] | 55 | } |
---|
[10731] | 56 | |
---|
[10826] | 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 | { |
---|
[10925] | 109 | |
---|
| 110 | |
---|
[10856] | 111 | this->setFormationMode( this->myLeader_->getFormationMode() ); |
---|
| 112 | Vector3* targetRelativePosition; |
---|
[10883] | 113 | this->spread_ = this->myLeader_->getSpread(); |
---|
[10869] | 114 | if (this->myLeader_->getIdentifier()->getName() == "DivisionController") |
---|
[10856] | 115 | { |
---|
| 116 | switch (this->formationMode_){ |
---|
| 117 | case FormationMode::WALL: |
---|
| 118 | { |
---|
[11030] | 119 | targetRelativePosition = new Vector3 (2.0f*this->spread_, 0, 0 - 1.0f*this->tolerance_); |
---|
[10856] | 120 | break; |
---|
| 121 | } |
---|
| 122 | case FormationMode::FINGER4: |
---|
| 123 | { |
---|
[11030] | 124 | targetRelativePosition = new Vector3 (2.0f*this->spread_, 0, this->spread_ - 1.0f*this->tolerance_); |
---|
[10856] | 125 | break; |
---|
| 126 | } |
---|
| 127 | case FormationMode::DIAMOND: |
---|
| 128 | { |
---|
[11030] | 129 | targetRelativePosition = new Vector3 (2.0f*this->spread_, 0, this->spread_ - 1.0f*this->tolerance_); |
---|
[10856] | 130 | break; |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | else |
---|
| 135 | { |
---|
| 136 | switch (this->formationMode_){ |
---|
| 137 | case FormationMode::WALL: |
---|
| 138 | { |
---|
[11030] | 139 | targetRelativePosition = new Vector3 (-2.0f*this->spread_, 0, 0 - 1.0f*this->tolerance_); |
---|
[10856] | 140 | break; |
---|
| 141 | } |
---|
| 142 | case FormationMode::FINGER4: |
---|
| 143 | { |
---|
[11030] | 144 | targetRelativePosition = new Vector3 (-2.0f*this->spread_, 0, this->spread_ - 1.0f*this->tolerance_); |
---|
[10856] | 145 | break; |
---|
| 146 | } |
---|
| 147 | case FormationMode::DIAMOND: |
---|
| 148 | { |
---|
[11030] | 149 | targetRelativePosition = new Vector3 (2.0f*this->spread_, -1.0f*this->spread_, 0 - 1.0f*this->tolerance_); |
---|
[10856] | 150 | break; |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | } |
---|
[10880] | 154 | Vector3 result = *targetRelativePosition; |
---|
| 155 | delete targetRelativePosition; |
---|
| 156 | return result; |
---|
[10856] | 157 | } |
---|
[10886] | 158 | void WingmanController::keepFormation() |
---|
| 159 | { |
---|
| 160 | this->bKeepFormation_ = true; |
---|
| 161 | ControllableEntity* leaderEntity = this->myLeader_->getControllableEntity(); |
---|
| 162 | Vector3 targetRelativePosition = this->getFormationPosition(); |
---|
| 163 | if (!leaderEntity) |
---|
| 164 | return; |
---|
| 165 | FlyingController::keepFormation (leaderEntity, targetRelativePosition); |
---|
| 166 | } |
---|
[10826] | 167 | //----POST: closest leader that is ready to take a new wingman is returned---- |
---|
[10877] | 168 | ActionpointController* WingmanController::findNewLeader() |
---|
[10717] | 169 | { |
---|
| 170 | |
---|
| 171 | if (!this->getControllableEntity()) |
---|
[10722] | 172 | return 0; |
---|
[10717] | 173 | |
---|
[10826] | 174 | //----vars for finding the closest leader---- |
---|
[10877] | 175 | ActionpointController* closestLeader = 0; |
---|
[10722] | 176 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
[10838] | 177 | Gametype* gt = this->getGametype(); |
---|
[10877] | 178 | |
---|
[11054] | 179 | for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>().begin(); it; ++it) |
---|
[10717] | 180 | { |
---|
[10826] | 181 | //----0ptr or not a leader or dead?---- |
---|
[10731] | 182 | if (!it || |
---|
[10869] | 183 | (it->getIdentifier()->getName() != "SectionController" && it->getIdentifier()->getName() != "DivisionController") || |
---|
[10731] | 184 | !(it->getControllableEntity())) |
---|
[10722] | 185 | continue; |
---|
[10826] | 186 | |
---|
| 187 | //----same team?---- |
---|
[10838] | 188 | if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) ) |
---|
[10717] | 189 | continue; |
---|
[10826] | 190 | |
---|
| 191 | //----check distance---- |
---|
| 192 | float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); |
---|
[10722] | 193 | if (distance < minDistance && !(it->hasWingman())) |
---|
| 194 | { |
---|
| 195 | closestLeader = *it; |
---|
| 196 | minDistance = distance; |
---|
| 197 | } |
---|
[10717] | 198 | } |
---|
[10722] | 199 | if (closestLeader) |
---|
| 200 | { |
---|
[10826] | 201 | //----Racing conditions---- |
---|
[10974] | 202 | /*TODO: racing condition check is wrong and redundant, as there is no multithreading here, ticks get called one after another, |
---|
| 203 | so it can be simplified to a check of whether leader got a wingman*/ |
---|
[10877] | 204 | if (closestLeader->setWingman(orxonox_cast<ActionpointController*>(this))) |
---|
| 205 | { |
---|
[10722] | 206 | return closestLeader; |
---|
[10877] | 207 | } |
---|
[10722] | 208 | } |
---|
| 209 | return 0; |
---|
[10717] | 210 | } |
---|
[10722] | 211 | |
---|
[10678] | 212 | } |
---|