| [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); | 
|---|
| [10719] | 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); | 
|---|
|  | 70 | } | 
|---|
|  | 71 |  | 
|---|
| [10826] | 72 | //----action for hard calculations---- | 
|---|
| [10731] | 73 | void WingmanController::action() | 
|---|
|  | 74 | { | 
|---|
| [10851] | 75 |  | 
|---|
| [10826] | 76 | //----If no leader, find one---- | 
|---|
| [10731] | 77 | if (!this->myLeader_) | 
|---|
|  | 78 | { | 
|---|
| [10877] | 79 | ActionpointController* newLeader = (findNewLeader()); | 
|---|
| [10731] | 80 | this->myLeader_ = newLeader; | 
|---|
| [10879] | 81 | if (this->myLeader_) | 
|---|
|  | 82 | { | 
|---|
| [10881] | 83 | //spread copyOrientation called equally among the division | 
|---|
|  | 84 | if (this->myLeader_->getIdentifier()->getName() == "SectionController") | 
|---|
| [10885] | 85 | this->actionCounter_ = 1; | 
|---|
| [10881] | 86 | else | 
|---|
| [10885] | 87 | this->actionCounter_ = 4; | 
|---|
| [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 | { | 
|---|
| [10864] | 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 | { | 
|---|
| [10886] | 108 | if (!this->hasTarget()) | 
|---|
| [10883] | 109 | { | 
|---|
| [10886] | 110 | this->setTarget(this->myLeader_->getTarget()); | 
|---|
| [10856] | 111 | } | 
|---|
| [10883] | 112 | if (this->hasTarget()) | 
|---|
|  | 113 | { | 
|---|
|  | 114 | this->maneuver(); | 
|---|
| [10888] | 115 | this->bShooting_ = this->canFire(); | 
|---|
|  | 116 | // Vector3 healthPosition = bestHealthPickup((this->target_->getWorldPosition() - this->getControllableEntity()->getWorldPosition()).length()); | 
|---|
|  | 117 | // if ((this->getControllableEntity()->getWorldPosition() - healthPosition).length() < this->tolerance_) | 
|---|
|  | 118 | // { | 
|---|
|  | 119 | //     //----choose where to go---- | 
|---|
|  | 120 | //     this->maneuver(); | 
|---|
|  | 121 | // } | 
|---|
|  | 122 | // else | 
|---|
|  | 123 | // { | 
|---|
|  | 124 | //     this->dodgeTowards(healthPosition); | 
|---|
|  | 125 | // } | 
|---|
|  | 126 | // //----fire if you can---- | 
|---|
|  | 127 | // this->bShooting_ = this->canFire(); | 
|---|
| [10883] | 128 | } | 
|---|
| [10856] | 129 | } | 
|---|
| [10805] | 130 | } | 
|---|
| [10881] | 131 | this->actionCounter_ += this->actionCounter_ < 100000 ? 1 : -this->actionCounter_ ; | 
|---|
| [10731] | 132 | } | 
|---|
|  | 133 |  | 
|---|
|  | 134 |  | 
|---|
| [10856] | 135 | Vector3 WingmanController::getFormationPosition () | 
|---|
|  | 136 | { | 
|---|
|  | 137 | this->setFormationMode( this->myLeader_->getFormationMode() ); | 
|---|
|  | 138 | Vector3* targetRelativePosition; | 
|---|
| [10883] | 139 | this->spread_ = this->myLeader_->getSpread(); | 
|---|
| [10869] | 140 | if (this->myLeader_->getIdentifier()->getName() == "DivisionController") | 
|---|
| [10856] | 141 | { | 
|---|
|  | 142 | switch (this->formationMode_){ | 
|---|
|  | 143 | case FormationMode::WALL: | 
|---|
|  | 144 | { | 
|---|
| [10879] | 145 | targetRelativePosition = new Vector3 (2*this->spread_, 0, 0 - this->tolerance_); | 
|---|
| [10856] | 146 | break; | 
|---|
|  | 147 | } | 
|---|
|  | 148 | case FormationMode::FINGER4: | 
|---|
|  | 149 | { | 
|---|
| [10879] | 150 | targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_); | 
|---|
| [10856] | 151 | break; | 
|---|
|  | 152 | } | 
|---|
|  | 153 | case FormationMode::DIAMOND: | 
|---|
|  | 154 | { | 
|---|
| [10879] | 155 | targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_); | 
|---|
| [10856] | 156 | break; | 
|---|
|  | 157 | } | 
|---|
|  | 158 | } | 
|---|
|  | 159 | } | 
|---|
|  | 160 | else | 
|---|
|  | 161 | { | 
|---|
| [10859] | 162 |  | 
|---|
| [10856] | 163 | switch (this->formationMode_){ | 
|---|
|  | 164 | case FormationMode::WALL: | 
|---|
|  | 165 | { | 
|---|
| [10879] | 166 | targetRelativePosition = new Vector3 (-2*this->spread_, 0, 0 - this->tolerance_); | 
|---|
| [10856] | 167 | break; | 
|---|
|  | 168 | } | 
|---|
|  | 169 | case FormationMode::FINGER4: | 
|---|
|  | 170 | { | 
|---|
| [10879] | 171 | targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_ - this->tolerance_); | 
|---|
| [10856] | 172 | break; | 
|---|
|  | 173 | } | 
|---|
|  | 174 | case FormationMode::DIAMOND: | 
|---|
|  | 175 | { | 
|---|
| [10879] | 176 | targetRelativePosition = new Vector3 (2*this->spread_, -this->spread_, 0 - this->tolerance_); | 
|---|
| [10856] | 177 | break; | 
|---|
|  | 178 | } | 
|---|
|  | 179 | } | 
|---|
|  | 180 | } | 
|---|
| [10880] | 181 | Vector3 result = *targetRelativePosition; | 
|---|
|  | 182 | delete targetRelativePosition; | 
|---|
|  | 183 | return result; | 
|---|
| [10856] | 184 | } | 
|---|
| [10886] | 185 | void WingmanController::keepFormation() | 
|---|
|  | 186 | { | 
|---|
|  | 187 | this->bKeepFormation_ = true; | 
|---|
|  | 188 | ControllableEntity* leaderEntity = this->myLeader_->getControllableEntity(); | 
|---|
|  | 189 | Vector3 targetRelativePosition = this->getFormationPosition(); | 
|---|
|  | 190 | if (!leaderEntity) | 
|---|
|  | 191 | return; | 
|---|
|  | 192 | FlyingController::keepFormation (leaderEntity, targetRelativePosition); | 
|---|
|  | 193 | } | 
|---|
| [10826] | 194 | //----POST: closest leader that is ready to take a new wingman is returned---- | 
|---|
| [10877] | 195 | ActionpointController* WingmanController::findNewLeader() | 
|---|
| [10717] | 196 | { | 
|---|
|  | 197 |  | 
|---|
|  | 198 | if (!this->getControllableEntity()) | 
|---|
| [10722] | 199 | return 0; | 
|---|
| [10717] | 200 |  | 
|---|
| [10826] | 201 | //----vars for finding the closest leader---- | 
|---|
| [10877] | 202 | ActionpointController* closestLeader = 0; | 
|---|
| [10722] | 203 | float minDistance =  std::numeric_limits<float>::infinity(); | 
|---|
| [10838] | 204 | Gametype* gt = this->getGametype(); | 
|---|
| [10877] | 205 |  | 
|---|
|  | 206 | for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>::begin(); it; ++it) | 
|---|
| [10717] | 207 | { | 
|---|
| [10826] | 208 | //----0ptr or not a leader or dead?---- | 
|---|
| [10731] | 209 | if (!it || | 
|---|
| [10869] | 210 | (it->getIdentifier()->getName() != "SectionController" && it->getIdentifier()->getName() != "DivisionController") || | 
|---|
| [10731] | 211 | !(it->getControllableEntity())) | 
|---|
| [10722] | 212 | continue; | 
|---|
| [10826] | 213 |  | 
|---|
|  | 214 | //----same team?---- | 
|---|
| [10838] | 215 | if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) ) | 
|---|
| [10717] | 216 | continue; | 
|---|
| [10826] | 217 |  | 
|---|
|  | 218 | //----check distance---- | 
|---|
|  | 219 | float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); | 
|---|
| [10722] | 220 | if (distance < minDistance && !(it->hasWingman())) | 
|---|
|  | 221 | { | 
|---|
|  | 222 | closestLeader = *it; | 
|---|
|  | 223 | minDistance = distance; | 
|---|
|  | 224 | } | 
|---|
| [10717] | 225 | } | 
|---|
| [10722] | 226 | if (closestLeader) | 
|---|
|  | 227 | { | 
|---|
| [10826] | 228 | //----Racing conditions---- | 
|---|
| [10877] | 229 | if (closestLeader->setWingman(orxonox_cast<ActionpointController*>(this))) | 
|---|
|  | 230 | { | 
|---|
| [10722] | 231 | return closestLeader; | 
|---|
| [10877] | 232 | } | 
|---|
| [10722] | 233 | } | 
|---|
|  | 234 | return 0; | 
|---|
| [10717] | 235 | } | 
|---|
| [10722] | 236 |  | 
|---|
| [10678] | 237 | } | 
|---|