[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()); |
---|
[10925] | 122 | if (!this || !this->getControllableEntity()) |
---|
| 123 | return; |
---|
| 124 | |
---|
[10731] | 125 | this->myLeader_ = newLeader; |
---|
[10879] | 126 | if (this->myLeader_) |
---|
| 127 | { |
---|
[10923] | 128 | |
---|
[10879] | 129 | } |
---|
[10731] | 130 | } |
---|
[10826] | 131 | //----If have leader, he will deal with logic---- |
---|
[10731] | 132 | else |
---|
| 133 | { |
---|
[10851] | 134 | |
---|
[10731] | 135 | } |
---|
[10851] | 136 | if (!this->myLeader_) |
---|
| 137 | { |
---|
[10864] | 138 | ActionpointController::action(); |
---|
[10923] | 139 | if (!this || !this->getControllableEntity()) |
---|
| 140 | return; |
---|
| 141 | |
---|
[10805] | 142 | } |
---|
[10854] | 143 | else if (this->myLeader_) |
---|
[10805] | 144 | { |
---|
[10883] | 145 | if (this->myLeader_->bKeepFormation_ || !(this->myLeader_->getAction() == Action::FIGHT || this->myLeader_->getAction() == Action::FIGHTALL |
---|
| 146 | || this->myLeader_->getAction() == Action::ATTACK)) |
---|
[10856] | 147 | { |
---|
[10886] | 148 | this->keepFormation(); |
---|
[10879] | 149 | } |
---|
[10886] | 150 | else if (!this->myLeader_->bKeepFormation_) |
---|
[10879] | 151 | { |
---|
[10925] | 152 | if (!this || !this->getControllableEntity()) |
---|
| 153 | return; |
---|
| 154 | |
---|
[10886] | 155 | if (!this->hasTarget()) |
---|
[10883] | 156 | { |
---|
[10886] | 157 | this->setTarget(this->myLeader_->getTarget()); |
---|
[10856] | 158 | } |
---|
[10883] | 159 | if (this->hasTarget()) |
---|
| 160 | { |
---|
[10906] | 161 | // this->maneuver(); |
---|
| 162 | // this->bShooting_ = this->canFire(); |
---|
[10888] | 163 | // Vector3 healthPosition = bestHealthPickup((this->target_->getWorldPosition() - this->getControllableEntity()->getWorldPosition()).length()); |
---|
| 164 | // if ((this->getControllableEntity()->getWorldPosition() - healthPosition).length() < this->tolerance_) |
---|
| 165 | // { |
---|
| 166 | // //----choose where to go---- |
---|
| 167 | // this->maneuver(); |
---|
| 168 | // } |
---|
| 169 | // else |
---|
| 170 | // { |
---|
| 171 | // this->dodgeTowards(healthPosition); |
---|
| 172 | // } |
---|
| 173 | // //----fire if you can---- |
---|
| 174 | // this->bShooting_ = this->canFire(); |
---|
[10883] | 175 | } |
---|
[10856] | 176 | } |
---|
[10805] | 177 | } |
---|
[10731] | 178 | } |
---|
| 179 | |
---|
| 180 | |
---|
[10856] | 181 | Vector3 WingmanController::getFormationPosition () |
---|
| 182 | { |
---|
[10925] | 183 | |
---|
| 184 | |
---|
[10856] | 185 | this->setFormationMode( this->myLeader_->getFormationMode() ); |
---|
| 186 | Vector3* targetRelativePosition; |
---|
[10883] | 187 | this->spread_ = this->myLeader_->getSpread(); |
---|
[10869] | 188 | if (this->myLeader_->getIdentifier()->getName() == "DivisionController") |
---|
[10856] | 189 | { |
---|
| 190 | switch (this->formationMode_){ |
---|
| 191 | case FormationMode::WALL: |
---|
| 192 | { |
---|
[10879] | 193 | targetRelativePosition = new Vector3 (2*this->spread_, 0, 0 - this->tolerance_); |
---|
[10856] | 194 | break; |
---|
| 195 | } |
---|
| 196 | case FormationMode::FINGER4: |
---|
| 197 | { |
---|
[10879] | 198 | targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_); |
---|
[10856] | 199 | break; |
---|
| 200 | } |
---|
| 201 | case FormationMode::DIAMOND: |
---|
| 202 | { |
---|
[10879] | 203 | targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_); |
---|
[10856] | 204 | break; |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | else |
---|
| 209 | { |
---|
[10859] | 210 | |
---|
[10856] | 211 | switch (this->formationMode_){ |
---|
| 212 | case FormationMode::WALL: |
---|
| 213 | { |
---|
[10879] | 214 | targetRelativePosition = new Vector3 (-2*this->spread_, 0, 0 - this->tolerance_); |
---|
[10856] | 215 | break; |
---|
| 216 | } |
---|
| 217 | case FormationMode::FINGER4: |
---|
| 218 | { |
---|
[10879] | 219 | targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_ - this->tolerance_); |
---|
[10856] | 220 | break; |
---|
| 221 | } |
---|
| 222 | case FormationMode::DIAMOND: |
---|
| 223 | { |
---|
[10879] | 224 | targetRelativePosition = new Vector3 (2*this->spread_, -this->spread_, 0 - this->tolerance_); |
---|
[10856] | 225 | break; |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | } |
---|
[10880] | 229 | Vector3 result = *targetRelativePosition; |
---|
| 230 | delete targetRelativePosition; |
---|
| 231 | return result; |
---|
[10856] | 232 | } |
---|
[10886] | 233 | void WingmanController::keepFormation() |
---|
| 234 | { |
---|
| 235 | this->bKeepFormation_ = true; |
---|
| 236 | ControllableEntity* leaderEntity = this->myLeader_->getControllableEntity(); |
---|
| 237 | Vector3 targetRelativePosition = this->getFormationPosition(); |
---|
| 238 | if (!leaderEntity) |
---|
| 239 | return; |
---|
| 240 | FlyingController::keepFormation (leaderEntity, targetRelativePosition); |
---|
| 241 | } |
---|
[10826] | 242 | //----POST: closest leader that is ready to take a new wingman is returned---- |
---|
[10877] | 243 | ActionpointController* WingmanController::findNewLeader() |
---|
[10717] | 244 | { |
---|
| 245 | |
---|
| 246 | if (!this->getControllableEntity()) |
---|
[10722] | 247 | return 0; |
---|
[10717] | 248 | |
---|
[10826] | 249 | //----vars for finding the closest leader---- |
---|
[10877] | 250 | ActionpointController* closestLeader = 0; |
---|
[10722] | 251 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
[10838] | 252 | Gametype* gt = this->getGametype(); |
---|
[10877] | 253 | |
---|
| 254 | for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>::begin(); it; ++it) |
---|
[10717] | 255 | { |
---|
[10826] | 256 | //----0ptr or not a leader or dead?---- |
---|
[10731] | 257 | if (!it || |
---|
[10869] | 258 | (it->getIdentifier()->getName() != "SectionController" && it->getIdentifier()->getName() != "DivisionController") || |
---|
[10731] | 259 | !(it->getControllableEntity())) |
---|
[10722] | 260 | continue; |
---|
[10826] | 261 | |
---|
| 262 | //----same team?---- |
---|
[10838] | 263 | if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) ) |
---|
[10717] | 264 | continue; |
---|
[10826] | 265 | |
---|
| 266 | //----check distance---- |
---|
| 267 | float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); |
---|
[10722] | 268 | if (distance < minDistance && !(it->hasWingman())) |
---|
| 269 | { |
---|
| 270 | closestLeader = *it; |
---|
| 271 | minDistance = distance; |
---|
| 272 | } |
---|
[10717] | 273 | } |
---|
[10722] | 274 | if (closestLeader) |
---|
| 275 | { |
---|
[10826] | 276 | //----Racing conditions---- |
---|
[10877] | 277 | if (closestLeader->setWingman(orxonox_cast<ActionpointController*>(this))) |
---|
| 278 | { |
---|
[10909] | 279 | if (closestLeader->getIdentifier()->getName() == "SectionController") |
---|
| 280 | { |
---|
[10915] | 281 | this->actionTime_ = 1.6f; |
---|
[10909] | 282 | } |
---|
| 283 | else |
---|
| 284 | { |
---|
[10915] | 285 | this->actionTime_ = 2.0f; |
---|
[10909] | 286 | } |
---|
[10722] | 287 | return closestLeader; |
---|
[10877] | 288 | } |
---|
[10722] | 289 | } |
---|
| 290 | return 0; |
---|
[10717] | 291 | } |
---|
[10722] | 292 | |
---|
[10678] | 293 | } |
---|