[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); |
---|
[10909] | 41 | //this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&WingmanController::action, this))); |
---|
[10725] | 42 | this->myLeader_ = 0; |
---|
[10851] | 43 | this->bFirstAction_ = true; |
---|
| 44 | |
---|
[10678] | 45 | } |
---|
| 46 | |
---|
| 47 | WingmanController::~WingmanController() |
---|
| 48 | { |
---|
[10854] | 49 | for (size_t i = 0; i < this->actionpoints_.size(); ++i) |
---|
| 50 | { |
---|
| 51 | if(this->actionpoints_[i]) |
---|
| 52 | this->actionpoints_[i]->destroy(); |
---|
| 53 | } |
---|
| 54 | this->parsedActionpoints_.clear(); |
---|
| 55 | this->actionpoints_.clear(); |
---|
[10678] | 56 | } |
---|
[10826] | 57 | |
---|
| 58 | void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 59 | { |
---|
| 60 | SUPER(WingmanController, XMLPort, xmlelement, mode); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | //----in tick, move (or look) and shoot---- |
---|
[10731] | 64 | void WingmanController::tick(float dt) |
---|
| 65 | { |
---|
| 66 | if (!this->isActive()) |
---|
[10886] | 67 | return; |
---|
[10731] | 68 | |
---|
| 69 | SUPER(WingmanController, tick, dt); |
---|
[10915] | 70 | if (!this->myLeader_) |
---|
[10909] | 71 | { |
---|
[10923] | 72 | /*if (this->actionTime_ == 2.0f) |
---|
[10915] | 73 | { |
---|
| 74 | if (this->timeOffset_ >= 0.0f && this->timeOffset_ <= 0.8f && !this->bActionCalled_) |
---|
| 75 | { |
---|
| 76 | this->action(); |
---|
| 77 | this->bActionCalled_ = true; |
---|
| 78 | } |
---|
| 79 | if (this->timeOffset_ > 1.6f) |
---|
| 80 | { |
---|
| 81 | this->bActionCalled_ = false; |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | else |
---|
| 85 | { |
---|
| 86 | if (this->timeOffset_ > 0.8f && this->timeOffset_ <= 1.6f && !this->bActionCalled_) |
---|
| 87 | { |
---|
| 88 | this->action(); |
---|
| 89 | this->bActionCalled_ = true; |
---|
| 90 | } |
---|
| 91 | if (this->timeOffset_ > 2.0f) |
---|
| 92 | { |
---|
| 93 | this->bActionCalled_ = false; |
---|
| 94 | } |
---|
[10923] | 95 | }*/ |
---|
[10909] | 96 | } |
---|
[10915] | 97 | else |
---|
[10909] | 98 | { |
---|
[10923] | 99 | /* if (this->timeOffset_ >= this->actionTime_ && this->timeOffset_ <= this->actionTime_ + 0.4f && !this->bActionCalled_) |
---|
[10915] | 100 | { |
---|
| 101 | this->action(); |
---|
| 102 | this->bActionCalled_ = true; |
---|
| 103 | } |
---|
| 104 | if (this->timeOffset_ <= 0.5f) |
---|
| 105 | { |
---|
| 106 | this->bActionCalled_ = false; |
---|
[10923] | 107 | }*/ |
---|
[10909] | 108 | } |
---|
[10915] | 109 | |
---|
| 110 | |
---|
[10731] | 111 | } |
---|
| 112 | |
---|
[10826] | 113 | //----action for hard calculations---- |
---|
[10731] | 114 | void WingmanController::action() |
---|
| 115 | { |
---|
[10923] | 116 | if (!this || !this->getControllableEntity()) |
---|
| 117 | return; |
---|
[10826] | 118 | //----If no leader, find one---- |
---|
[10731] | 119 | if (!this->myLeader_) |
---|
| 120 | { |
---|
[10877] | 121 | ActionpointController* newLeader = (findNewLeader()); |
---|
[10731] | 122 | this->myLeader_ = newLeader; |
---|
[10879] | 123 | if (this->myLeader_) |
---|
| 124 | { |
---|
[10923] | 125 | |
---|
[10879] | 126 | } |
---|
[10731] | 127 | } |
---|
[10826] | 128 | //----If have leader, he will deal with logic---- |
---|
[10731] | 129 | else |
---|
| 130 | { |
---|
[10851] | 131 | |
---|
[10731] | 132 | } |
---|
[10851] | 133 | if (!this->myLeader_) |
---|
| 134 | { |
---|
[10864] | 135 | ActionpointController::action(); |
---|
[10923] | 136 | if (!this || !this->getControllableEntity()) |
---|
| 137 | return; |
---|
| 138 | |
---|
[10805] | 139 | } |
---|
[10854] | 140 | else if (this->myLeader_) |
---|
[10805] | 141 | { |
---|
[10883] | 142 | if (this->myLeader_->bKeepFormation_ || !(this->myLeader_->getAction() == Action::FIGHT || this->myLeader_->getAction() == Action::FIGHTALL |
---|
| 143 | || this->myLeader_->getAction() == Action::ATTACK)) |
---|
[10856] | 144 | { |
---|
[10886] | 145 | this->keepFormation(); |
---|
[10879] | 146 | } |
---|
[10886] | 147 | else if (!this->myLeader_->bKeepFormation_) |
---|
[10879] | 148 | { |
---|
[10886] | 149 | if (!this->hasTarget()) |
---|
[10883] | 150 | { |
---|
[10886] | 151 | this->setTarget(this->myLeader_->getTarget()); |
---|
[10856] | 152 | } |
---|
[10883] | 153 | if (this->hasTarget()) |
---|
| 154 | { |
---|
[10906] | 155 | // this->maneuver(); |
---|
| 156 | // this->bShooting_ = this->canFire(); |
---|
[10888] | 157 | // Vector3 healthPosition = bestHealthPickup((this->target_->getWorldPosition() - this->getControllableEntity()->getWorldPosition()).length()); |
---|
| 158 | // if ((this->getControllableEntity()->getWorldPosition() - healthPosition).length() < this->tolerance_) |
---|
| 159 | // { |
---|
| 160 | // //----choose where to go---- |
---|
| 161 | // this->maneuver(); |
---|
| 162 | // } |
---|
| 163 | // else |
---|
| 164 | // { |
---|
| 165 | // this->dodgeTowards(healthPosition); |
---|
| 166 | // } |
---|
| 167 | // //----fire if you can---- |
---|
| 168 | // this->bShooting_ = this->canFire(); |
---|
[10883] | 169 | } |
---|
[10856] | 170 | } |
---|
[10805] | 171 | } |
---|
[10731] | 172 | } |
---|
| 173 | |
---|
| 174 | |
---|
[10856] | 175 | Vector3 WingmanController::getFormationPosition () |
---|
| 176 | { |
---|
| 177 | this->setFormationMode( this->myLeader_->getFormationMode() ); |
---|
| 178 | Vector3* targetRelativePosition; |
---|
[10883] | 179 | this->spread_ = this->myLeader_->getSpread(); |
---|
[10869] | 180 | if (this->myLeader_->getIdentifier()->getName() == "DivisionController") |
---|
[10856] | 181 | { |
---|
| 182 | switch (this->formationMode_){ |
---|
| 183 | case FormationMode::WALL: |
---|
| 184 | { |
---|
[10879] | 185 | targetRelativePosition = new Vector3 (2*this->spread_, 0, 0 - this->tolerance_); |
---|
[10856] | 186 | break; |
---|
| 187 | } |
---|
| 188 | case FormationMode::FINGER4: |
---|
| 189 | { |
---|
[10879] | 190 | targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_); |
---|
[10856] | 191 | break; |
---|
| 192 | } |
---|
| 193 | case FormationMode::DIAMOND: |
---|
| 194 | { |
---|
[10879] | 195 | targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_); |
---|
[10856] | 196 | break; |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | else |
---|
| 201 | { |
---|
[10859] | 202 | |
---|
[10856] | 203 | switch (this->formationMode_){ |
---|
| 204 | case FormationMode::WALL: |
---|
| 205 | { |
---|
[10879] | 206 | targetRelativePosition = new Vector3 (-2*this->spread_, 0, 0 - this->tolerance_); |
---|
[10856] | 207 | break; |
---|
| 208 | } |
---|
| 209 | case FormationMode::FINGER4: |
---|
| 210 | { |
---|
[10879] | 211 | targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_ - this->tolerance_); |
---|
[10856] | 212 | break; |
---|
| 213 | } |
---|
| 214 | case FormationMode::DIAMOND: |
---|
| 215 | { |
---|
[10879] | 216 | targetRelativePosition = new Vector3 (2*this->spread_, -this->spread_, 0 - this->tolerance_); |
---|
[10856] | 217 | break; |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | } |
---|
[10880] | 221 | Vector3 result = *targetRelativePosition; |
---|
| 222 | delete targetRelativePosition; |
---|
| 223 | return result; |
---|
[10856] | 224 | } |
---|
[10886] | 225 | void WingmanController::keepFormation() |
---|
| 226 | { |
---|
| 227 | this->bKeepFormation_ = true; |
---|
| 228 | ControllableEntity* leaderEntity = this->myLeader_->getControllableEntity(); |
---|
| 229 | Vector3 targetRelativePosition = this->getFormationPosition(); |
---|
| 230 | if (!leaderEntity) |
---|
| 231 | return; |
---|
| 232 | FlyingController::keepFormation (leaderEntity, targetRelativePosition); |
---|
| 233 | } |
---|
[10826] | 234 | //----POST: closest leader that is ready to take a new wingman is returned---- |
---|
[10877] | 235 | ActionpointController* WingmanController::findNewLeader() |
---|
[10717] | 236 | { |
---|
| 237 | |
---|
| 238 | if (!this->getControllableEntity()) |
---|
[10722] | 239 | return 0; |
---|
[10717] | 240 | |
---|
[10826] | 241 | //----vars for finding the closest leader---- |
---|
[10877] | 242 | ActionpointController* closestLeader = 0; |
---|
[10722] | 243 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
[10838] | 244 | Gametype* gt = this->getGametype(); |
---|
[10877] | 245 | |
---|
| 246 | for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>::begin(); it; ++it) |
---|
[10717] | 247 | { |
---|
[10826] | 248 | //----0ptr or not a leader or dead?---- |
---|
[10731] | 249 | if (!it || |
---|
[10869] | 250 | (it->getIdentifier()->getName() != "SectionController" && it->getIdentifier()->getName() != "DivisionController") || |
---|
[10731] | 251 | !(it->getControllableEntity())) |
---|
[10722] | 252 | continue; |
---|
[10826] | 253 | |
---|
| 254 | //----same team?---- |
---|
[10838] | 255 | if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) ) |
---|
[10717] | 256 | continue; |
---|
[10826] | 257 | |
---|
| 258 | //----check distance---- |
---|
| 259 | float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); |
---|
[10722] | 260 | if (distance < minDistance && !(it->hasWingman())) |
---|
| 261 | { |
---|
| 262 | closestLeader = *it; |
---|
| 263 | minDistance = distance; |
---|
| 264 | } |
---|
[10717] | 265 | } |
---|
[10722] | 266 | if (closestLeader) |
---|
| 267 | { |
---|
[10826] | 268 | //----Racing conditions---- |
---|
[10877] | 269 | if (closestLeader->setWingman(orxonox_cast<ActionpointController*>(this))) |
---|
| 270 | { |
---|
[10909] | 271 | if (closestLeader->getIdentifier()->getName() == "SectionController") |
---|
| 272 | { |
---|
[10915] | 273 | this->actionTime_ = 1.6f; |
---|
[10909] | 274 | } |
---|
| 275 | else |
---|
| 276 | { |
---|
[10915] | 277 | this->actionTime_ = 2.0f; |
---|
[10909] | 278 | } |
---|
[10722] | 279 | return closestLeader; |
---|
[10877] | 280 | } |
---|
[10722] | 281 | } |
---|
| 282 | return 0; |
---|
[10717] | 283 | } |
---|
[10722] | 284 | |
---|
[10678] | 285 | } |
---|