| [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: |
|---|
| 23 | * Fabian 'x3n' Landau |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * Dominik Solenicki |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | #include "DivisionController.h" |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | namespace orxonox |
|---|
| 33 | { |
|---|
| 34 | |
|---|
| 35 | RegisterClass(DivisionController); |
|---|
| 36 | |
|---|
| [10826] | 37 | //Leaders share the fact that they have Wingmans |
|---|
| [10709] | 38 | DivisionController::DivisionController(Context* context) : LeaderController(context) |
|---|
| [10678] | 39 | { |
|---|
| 40 | RegisterObject(DivisionController); |
|---|
| [10759] | 41 | |
|---|
| 42 | this->setFormationMode(FormationMode::DIAMOND); |
|---|
| [10763] | 43 | this->target_ = 0; |
|---|
| [10725] | 44 | this->myFollower_ = 0; |
|---|
| 45 | this->myWingman_ = 0; |
|---|
| 46 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&DivisionController::action, this))); |
|---|
| [10759] | 47 | this->rank_ = Rank::DIVISIONLEADER; |
|---|
| [10725] | 48 | |
|---|
| [10731] | 49 | |
|---|
| [10678] | 50 | } |
|---|
| 51 | |
|---|
| 52 | DivisionController::~DivisionController() |
|---|
| 53 | { |
|---|
| [10725] | 54 | } |
|---|
| [10731] | 55 | |
|---|
| [10826] | 56 | void DivisionController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
|---|
| 57 | { |
|---|
| 58 | SUPER(DivisionController, XMLPort, xmlelement, mode); |
|---|
| 59 | |
|---|
| 60 | //XMLPortParam(DivisionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| [10731] | 63 | |
|---|
| [10725] | 64 | void DivisionController::tick(float dt) |
|---|
| [10803] | 65 | { |
|---|
| 66 | if (!this->isActive()) |
|---|
| 67 | return; |
|---|
| [10800] | 68 | if (this->bHasTargetPosition_) |
|---|
| [10729] | 69 | { |
|---|
| [10800] | 70 | this->moveToTargetPosition(dt); |
|---|
| 71 | } |
|---|
| [10803] | 72 | else if (this->bLookAtTarget_) |
|---|
| [10789] | 73 | { |
|---|
| [10803] | 74 | this->lookAtTarget(dt); |
|---|
| [10729] | 75 | } |
|---|
| [10803] | 76 | if (bShooting_) |
|---|
| [10796] | 77 | { |
|---|
| [10803] | 78 | this->doFire(); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| [10722] | 81 | SUPER(DivisionController, tick, dt); |
|---|
| [10718] | 82 | |
|---|
| [10678] | 83 | } |
|---|
| [10725] | 84 | void DivisionController::action() |
|---|
| 85 | { |
|---|
| [10805] | 86 | |
|---|
| [10803] | 87 | |
|---|
| [10805] | 88 | if (this->action_ == Action::FIGHT) |
|---|
| 89 | { |
|---|
| [10832] | 90 | if (!this->hasTarget()) |
|---|
| [10805] | 91 | { |
|---|
| [10832] | 92 | //----find a target---- |
|---|
| 93 | this->setClosestTarget(); |
|---|
| 94 | } |
|---|
| 95 | else |
|---|
| 96 | { |
|---|
| [10826] | 97 | //----fly in formation if far enough---- |
|---|
| [10805] | 98 | Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); |
|---|
| 99 | if (diffVector.length() > 3000) |
|---|
| 100 | { |
|---|
| 101 | this->setTargetPositionOfWingman(); |
|---|
| [10832] | 102 | this->setTargetPositionOfFollower(); |
|---|
| 103 | } |
|---|
| 104 | else |
|---|
| 105 | { |
|---|
| 106 | //----wingmans shall support the fire of their leaders---- |
|---|
| 107 | if (this->myWingman_) |
|---|
| 108 | { |
|---|
| 109 | this->myWingman_->setAction (Action::FIGHT, this->target_); |
|---|
| 110 | } |
|---|
| 111 | if (this->myFollower_) |
|---|
| 112 | { |
|---|
| 113 | this->myFollower_->setAction (Action::FIGHT); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| [10805] | 118 | } |
|---|
| [10832] | 119 | if (this->hasTarget()) |
|---|
| 120 | { |
|---|
| 121 | //----choose where to go---- |
|---|
| 122 | this->maneuver(); |
|---|
| 123 | //----fire if you can---- |
|---|
| 124 | this->bShooting_ = this->canFire(); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| [10805] | 127 | } |
|---|
| 128 | else if (this->action_ == Action::FLY) |
|---|
| 129 | { |
|---|
| 130 | this->setTargetPositionOfWingman(); |
|---|
| 131 | this->setTargetPositionOfFollower(); |
|---|
| 132 | } |
|---|
| 133 | else if (this->action_ == Action::PROTECT) |
|---|
| 134 | { |
|---|
| [10763] | 135 | |
|---|
| [10805] | 136 | } |
|---|
| 137 | |
|---|
| [10709] | 138 | } |
|---|
| [10725] | 139 | |
|---|
| [10731] | 140 | |
|---|
| 141 | |
|---|
| [10729] | 142 | void DivisionController::setTargetPositionOfWingman() |
|---|
| [10725] | 143 | { |
|---|
| 144 | if (!this->myWingman_) |
|---|
| 145 | return; |
|---|
| [10729] | 146 | Vector3* targetRelativePositionOfWingman; |
|---|
| [10725] | 147 | switch (this->formationMode_){ |
|---|
| [10759] | 148 | case FormationMode::WALL: |
|---|
| [10725] | 149 | { |
|---|
| [10729] | 150 | targetRelativePositionOfWingman = new Vector3 (400, 0, 0); |
|---|
| [10725] | 151 | break; |
|---|
| 152 | } |
|---|
| [10759] | 153 | case FormationMode::FINGER4: |
|---|
| [10725] | 154 | { |
|---|
| [10832] | 155 | targetRelativePositionOfWingman = new Vector3 (400, 0, 200); |
|---|
| [10725] | 156 | break; |
|---|
| 157 | } |
|---|
| [10826] | 158 | |
|---|
| [10759] | 159 | case FormationMode::DIAMOND: |
|---|
| [10725] | 160 | { |
|---|
| [10832] | 161 | targetRelativePositionOfWingman = new Vector3 (400, 0, 200); |
|---|
| [10725] | 162 | break; |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| [10729] | 165 | Quaternion orient = this->getControllableEntity()->getWorldOrientation(); |
|---|
| 166 | |
|---|
| 167 | Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + |
|---|
| 168 | (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman))); |
|---|
| 169 | |
|---|
| [10805] | 170 | myWingman_->setAction( Action::FLY, targetAbsolutePositionOfWingman, orient); |
|---|
| [10725] | 171 | |
|---|
| 172 | } |
|---|
| [10729] | 173 | void DivisionController::setTargetPositionOfFollower() |
|---|
| [10725] | 174 | { |
|---|
| 175 | if (!this->myFollower_) |
|---|
| 176 | return; |
|---|
| [10759] | 177 | this->myFollower_->setFormationMode(this->formationMode_); |
|---|
| 178 | |
|---|
| [10729] | 179 | Vector3* targetRelativePositionOfFollower; |
|---|
| [10725] | 180 | switch (this->formationMode_){ |
|---|
| [10759] | 181 | case FormationMode::WALL: |
|---|
| [10725] | 182 | { |
|---|
| [10729] | 183 | targetRelativePositionOfFollower = new Vector3 (-400, 0, 0); |
|---|
| [10725] | 184 | break; |
|---|
| 185 | } |
|---|
| [10759] | 186 | case FormationMode::FINGER4: |
|---|
| [10725] | 187 | { |
|---|
| [10832] | 188 | targetRelativePositionOfFollower = new Vector3 (-400, 0, 200); |
|---|
| [10725] | 189 | break; |
|---|
| 190 | } |
|---|
| [10826] | 191 | |
|---|
| [10759] | 192 | case FormationMode::DIAMOND: |
|---|
| [10725] | 193 | { |
|---|
| [10832] | 194 | targetRelativePositionOfFollower = new Vector3 (-400, 0, 200); |
|---|
| [10725] | 195 | break; |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| [10729] | 198 | Quaternion orient = this->getControllableEntity()->getWorldOrientation(); |
|---|
| 199 | |
|---|
| 200 | Vector3 targetAbsolutePositionOfFollower = ((this->getControllableEntity()->getWorldPosition()) + |
|---|
| 201 | (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfFollower))); |
|---|
| 202 | |
|---|
| [10805] | 203 | myFollower_->setAction ( Action::FLY, targetAbsolutePositionOfFollower, orient ); |
|---|
| [10729] | 204 | } |
|---|
| [10731] | 205 | |
|---|
| 206 | |
|---|
| 207 | bool DivisionController::setWingman(CommonController* cwingman) |
|---|
| 208 | { |
|---|
| 209 | |
|---|
| 210 | WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman); |
|---|
| 211 | if (!this->myWingman_) |
|---|
| 212 | { |
|---|
| 213 | this->myWingman_ = wingman; |
|---|
| 214 | return true; |
|---|
| 215 | } |
|---|
| 216 | else |
|---|
| 217 | { |
|---|
| 218 | return false; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | } |
|---|
| 222 | bool DivisionController::setFollower(LeaderController* myFollower) |
|---|
| 223 | { |
|---|
| 224 | if (!this->myFollower_) |
|---|
| 225 | { |
|---|
| 226 | this->myFollower_ = myFollower; |
|---|
| 227 | return true; |
|---|
| 228 | } |
|---|
| 229 | else |
|---|
| 230 | { |
|---|
| 231 | return false; |
|---|
| 232 | } |
|---|
| 233 | } |
|---|
| 234 | bool DivisionController::hasWingman() |
|---|
| 235 | { |
|---|
| 236 | if (this->myWingman_) |
|---|
| 237 | return true; |
|---|
| 238 | else |
|---|
| 239 | return false; |
|---|
| 240 | } |
|---|
| 241 | bool DivisionController::hasFollower() |
|---|
| 242 | { |
|---|
| 243 | if (this->myFollower_) |
|---|
| 244 | return true; |
|---|
| 245 | else |
|---|
| 246 | return false; |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | |
|---|
| [10678] | 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | } |
|---|