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