[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 | } |
---|
[10826] | 56 | |
---|
| 57 | //----in tick, move (or look) and shoot---- |
---|
[10731] | 58 | void WingmanController::tick(float dt) |
---|
| 59 | { |
---|
| 60 | if (!this->isActive()) |
---|
[10886] | 61 | return; |
---|
[10731] | 62 | |
---|
| 63 | SUPER(WingmanController, tick, dt); |
---|
[10915] | 64 | |
---|
[10731] | 65 | } |
---|
| 66 | |
---|
[10826] | 67 | //----action for hard calculations---- |
---|
[10731] | 68 | void WingmanController::action() |
---|
| 69 | { |
---|
[10946] | 70 | if (!this || !this->getControllableEntity() || !this->isActive()) |
---|
[10923] | 71 | return; |
---|
[10826] | 72 | //----If no leader, find one---- |
---|
[10731] | 73 | if (!this->myLeader_) |
---|
| 74 | { |
---|
[10877] | 75 | ActionpointController* newLeader = (findNewLeader()); |
---|
[10925] | 76 | if (!this || !this->getControllableEntity()) |
---|
| 77 | return; |
---|
| 78 | |
---|
[10731] | 79 | this->myLeader_ = newLeader; |
---|
[10879] | 80 | if (this->myLeader_) |
---|
| 81 | { |
---|
[10923] | 82 | |
---|
[10879] | 83 | } |
---|
[10731] | 84 | } |
---|
[10826] | 85 | //----If have leader, he will deal with logic---- |
---|
[10731] | 86 | else |
---|
| 87 | { |
---|
[10851] | 88 | |
---|
[10731] | 89 | } |
---|
[10851] | 90 | if (!this->myLeader_) |
---|
| 91 | { |
---|
[10953] | 92 | ActionpointController::action(); |
---|
[10805] | 93 | } |
---|
[10854] | 94 | else if (this->myLeader_) |
---|
[10805] | 95 | { |
---|
[10974] | 96 | if (this->myLeader_->bKeepFormation_ || !(this->myLeader_->getAction() == Action::FIGHT |
---|
| 97 | || this->myLeader_->getAction() == Action::FIGHTALL |
---|
[10883] | 98 | || this->myLeader_->getAction() == Action::ATTACK)) |
---|
[10856] | 99 | { |
---|
[10886] | 100 | this->keepFormation(); |
---|
[10879] | 101 | } |
---|
[10886] | 102 | else if (!this->myLeader_->bKeepFormation_) |
---|
[10879] | 103 | { |
---|
[10925] | 104 | if (!this || !this->getControllableEntity()) |
---|
| 105 | return; |
---|
| 106 | |
---|
[10886] | 107 | if (!this->hasTarget()) |
---|
[10883] | 108 | { |
---|
[10886] | 109 | this->setTarget(this->myLeader_->getTarget()); |
---|
[10856] | 110 | } |
---|
[10935] | 111 | |
---|
[10856] | 112 | } |
---|
[10805] | 113 | } |
---|
[10731] | 114 | } |
---|
| 115 | |
---|
| 116 | |
---|
[10856] | 117 | Vector3 WingmanController::getFormationPosition () |
---|
| 118 | { |
---|
[10925] | 119 | |
---|
| 120 | |
---|
[10856] | 121 | this->setFormationMode( this->myLeader_->getFormationMode() ); |
---|
| 122 | Vector3* targetRelativePosition; |
---|
[10883] | 123 | this->spread_ = this->myLeader_->getSpread(); |
---|
[10869] | 124 | if (this->myLeader_->getIdentifier()->getName() == "DivisionController") |
---|
[10856] | 125 | { |
---|
| 126 | switch (this->formationMode_){ |
---|
| 127 | case FormationMode::WALL: |
---|
| 128 | { |
---|
[11030] | 129 | targetRelativePosition = new Vector3 (2.0f*this->spread_, 0, 0 - 1.0f*this->tolerance_); |
---|
[10856] | 130 | break; |
---|
| 131 | } |
---|
| 132 | case FormationMode::FINGER4: |
---|
| 133 | { |
---|
[11030] | 134 | targetRelativePosition = new Vector3 (2.0f*this->spread_, 0, this->spread_ - 1.0f*this->tolerance_); |
---|
[10856] | 135 | break; |
---|
| 136 | } |
---|
| 137 | case FormationMode::DIAMOND: |
---|
| 138 | { |
---|
[11030] | 139 | targetRelativePosition = new Vector3 (2.0f*this->spread_, 0, this->spread_ - 1.0f*this->tolerance_); |
---|
[10856] | 140 | break; |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | else |
---|
| 145 | { |
---|
| 146 | switch (this->formationMode_){ |
---|
| 147 | case FormationMode::WALL: |
---|
| 148 | { |
---|
[11030] | 149 | targetRelativePosition = new Vector3 (-2.0f*this->spread_, 0, 0 - 1.0f*this->tolerance_); |
---|
[10856] | 150 | break; |
---|
| 151 | } |
---|
| 152 | case FormationMode::FINGER4: |
---|
| 153 | { |
---|
[11030] | 154 | targetRelativePosition = new Vector3 (-2.0f*this->spread_, 0, this->spread_ - 1.0f*this->tolerance_); |
---|
[10856] | 155 | break; |
---|
| 156 | } |
---|
| 157 | case FormationMode::DIAMOND: |
---|
| 158 | { |
---|
[11030] | 159 | targetRelativePosition = new Vector3 (2.0f*this->spread_, -1.0f*this->spread_, 0 - 1.0f*this->tolerance_); |
---|
[10856] | 160 | break; |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | } |
---|
[10880] | 164 | Vector3 result = *targetRelativePosition; |
---|
| 165 | delete targetRelativePosition; |
---|
| 166 | return result; |
---|
[10856] | 167 | } |
---|
[10886] | 168 | void WingmanController::keepFormation() |
---|
| 169 | { |
---|
| 170 | this->bKeepFormation_ = true; |
---|
| 171 | ControllableEntity* leaderEntity = this->myLeader_->getControllableEntity(); |
---|
| 172 | Vector3 targetRelativePosition = this->getFormationPosition(); |
---|
| 173 | if (!leaderEntity) |
---|
| 174 | return; |
---|
| 175 | FlyingController::keepFormation (leaderEntity, targetRelativePosition); |
---|
| 176 | } |
---|
[10826] | 177 | //----POST: closest leader that is ready to take a new wingman is returned---- |
---|
[10877] | 178 | ActionpointController* WingmanController::findNewLeader() |
---|
[10717] | 179 | { |
---|
| 180 | |
---|
| 181 | if (!this->getControllableEntity()) |
---|
[10722] | 182 | return 0; |
---|
[10717] | 183 | |
---|
[10826] | 184 | //----vars for finding the closest leader---- |
---|
[10877] | 185 | ActionpointController* closestLeader = 0; |
---|
[10722] | 186 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
[10838] | 187 | Gametype* gt = this->getGametype(); |
---|
[10877] | 188 | |
---|
[11054] | 189 | for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>().begin(); it; ++it) |
---|
[10717] | 190 | { |
---|
[10826] | 191 | //----0ptr or not a leader or dead?---- |
---|
[10731] | 192 | if (!it || |
---|
[10869] | 193 | (it->getIdentifier()->getName() != "SectionController" && it->getIdentifier()->getName() != "DivisionController") || |
---|
[10731] | 194 | !(it->getControllableEntity())) |
---|
[10722] | 195 | continue; |
---|
[10826] | 196 | |
---|
| 197 | //----same team?---- |
---|
[10838] | 198 | if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) ) |
---|
[10717] | 199 | continue; |
---|
[10826] | 200 | |
---|
| 201 | //----check distance---- |
---|
| 202 | float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); |
---|
[10722] | 203 | if (distance < minDistance && !(it->hasWingman())) |
---|
| 204 | { |
---|
| 205 | closestLeader = *it; |
---|
| 206 | minDistance = distance; |
---|
| 207 | } |
---|
[10717] | 208 | } |
---|
[10722] | 209 | if (closestLeader) |
---|
| 210 | { |
---|
[10826] | 211 | //----Racing conditions---- |
---|
[10974] | 212 | /*TODO: racing condition check is wrong and redundant, as there is no multithreading here, ticks get called one after another, |
---|
| 213 | so it can be simplified to a check of whether leader got a wingman*/ |
---|
[10877] | 214 | if (closestLeader->setWingman(orxonox_cast<ActionpointController*>(this))) |
---|
| 215 | { |
---|
[10722] | 216 | return closestLeader; |
---|
[10877] | 217 | } |
---|
[10722] | 218 | } |
---|
| 219 | return 0; |
---|
[10717] | 220 | } |
---|
[10722] | 221 | |
---|
[10678] | 222 | } |
---|