| [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" |
|---|
| [10849] | 30 | #include "infos/PlayerInfo.h" |
|---|
| [10678] | 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 | { |
|---|
| [10843] | 66 | |
|---|
| [10803] | 67 | if (!this->isActive()) |
|---|
| 68 | return; |
|---|
| 69 | |
|---|
| [10843] | 70 | |
|---|
| [10722] | 71 | SUPER(DivisionController, tick, dt); |
|---|
| [10718] | 72 | |
|---|
| [10678] | 73 | } |
|---|
| [10725] | 74 | void DivisionController::action() |
|---|
| 75 | { |
|---|
| [10847] | 76 | //----Whatever ship is doing, if there are targets close to it and its own target is far away, fight them---- |
|---|
| 77 | //analog to attack move |
|---|
| 78 | if (this->action_ != Action::FIGHT && this->action_ != Action::FIGHTALL) |
|---|
| 79 | { |
|---|
| [10849] | 80 | if ( (this->target_ && CommonController::distance (this->getControllableEntity(), this->target_) > this->attackRange_) |
|---|
| 81 | || !this->target_ ) |
|---|
| [10847] | 82 | { |
|---|
| [10849] | 83 | Pawn* newTarget = this->closestTarget(); |
|---|
| [10848] | 84 | if ( newTarget && |
|---|
| [10849] | 85 | CommonController::distance (this->getControllableEntity(), static_cast<ControllableEntity*>(newTarget)) |
|---|
| 86 | <= this->attackRange_ ) |
|---|
| [10847] | 87 | { |
|---|
| [10848] | 88 | // this->backupAction(); |
|---|
| 89 | // this->setAction (Action::FIGHT, newTarget); |
|---|
| [10849] | 90 | Point p = { Action::FIGHT, CommonController::getName(newTarget), Vector3::ZERO }; |
|---|
| 91 | this->parsedActionpoints_.push_back(p); |
|---|
| 92 | this->executeActionpoint(); |
|---|
| [10847] | 93 | } |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| [10848] | 96 | |
|---|
| [10847] | 97 | //action is NONE whenever ships finishes current action, |
|---|
| 98 | //if it was fighting before because enemies were close, resume what it was doing |
|---|
| 99 | //otherwise try to execute next action |
|---|
| 100 | if (this->action_ == Action::NONE) |
|---|
| 101 | { |
|---|
| [10849] | 102 | this->executeActionpoint(); |
|---|
| [10847] | 103 | } |
|---|
| [10848] | 104 | |
|---|
| 105 | |
|---|
| 106 | //this->action_ is what I am actually executing, this->target_ is what I am |
|---|
| 107 | //actually attacking, etc. |
|---|
| 108 | |
|---|
| 109 | //after action is finished, .pop_back() is to be called. |
|---|
| [10847] | 110 | if (this->action_ == Action::FIGHT || this->action_ == Action::FIGHTALL) |
|---|
| 111 | { |
|---|
| [10832] | 112 | if (!this->hasTarget()) |
|---|
| [10805] | 113 | { |
|---|
| [10832] | 114 | //----find a target---- |
|---|
| [10848] | 115 | ControllableEntity* newTarget = this->closestTarget(); |
|---|
| [10847] | 116 | if (this->action_ == Action::FIGHT) |
|---|
| 117 | { |
|---|
| 118 | if (newTarget && CommonController::distance (this->getControllableEntity(), newTarget) < this->attackRange_) |
|---|
| 119 | { |
|---|
| 120 | this->setAction (Action::FIGHT, newTarget); |
|---|
| 121 | } |
|---|
| 122 | else |
|---|
| 123 | { |
|---|
| [10848] | 124 | this->nextActionpoint(); |
|---|
| 125 | return; |
|---|
| [10847] | 126 | } |
|---|
| 127 | } |
|---|
| 128 | else if (this->action_ == Action::FIGHTALL) |
|---|
| 129 | { |
|---|
| [10849] | 130 | if (newTarget && newTarget->getController()) |
|---|
| [10848] | 131 | { |
|---|
| 132 | this->setAction (Action::FIGHTALL, newTarget); |
|---|
| 133 | } |
|---|
| 134 | else |
|---|
| 135 | { |
|---|
| 136 | this->nextActionpoint(); |
|---|
| 137 | return; |
|---|
| 138 | } |
|---|
| [10847] | 139 | } |
|---|
| 140 | |
|---|
| [10832] | 141 | } |
|---|
| [10847] | 142 | else if (this->hasTarget()) |
|---|
| [10832] | 143 | { |
|---|
| [10826] | 144 | //----fly in formation if far enough---- |
|---|
| [10805] | 145 | Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); |
|---|
| [10849] | 146 | |
|---|
| 147 | |
|---|
| [10847] | 148 | if (diffVector.length() > this->attackRange_) |
|---|
| [10805] | 149 | { |
|---|
| [10848] | 150 | if (this->action_ == Action::FIGHT) |
|---|
| 151 | { |
|---|
| 152 | this->nextActionpoint(); |
|---|
| 153 | return; |
|---|
| 154 | } |
|---|
| 155 | else |
|---|
| 156 | { |
|---|
| 157 | this->setTargetPositionOfWingman(); |
|---|
| 158 | this->setTargetPositionOfFollower(); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| [10832] | 161 | } |
|---|
| 162 | else |
|---|
| 163 | { |
|---|
| 164 | //----wingmans shall support the fire of their leaders---- |
|---|
| 165 | if (this->myWingman_) |
|---|
| 166 | { |
|---|
| [10848] | 167 | this->myWingman_->setAction (this->action_, this->target_); |
|---|
| [10832] | 168 | } |
|---|
| 169 | if (this->myFollower_) |
|---|
| 170 | { |
|---|
| [10848] | 171 | this->myFollower_->setAction (this->action_); |
|---|
| [10832] | 172 | } |
|---|
| 173 | |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| [10805] | 176 | } |
|---|
| [10832] | 177 | if (this->hasTarget()) |
|---|
| 178 | { |
|---|
| 179 | //----choose where to go---- |
|---|
| 180 | this->maneuver(); |
|---|
| 181 | //----fire if you can---- |
|---|
| 182 | this->bShooting_ = this->canFire(); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| [10805] | 185 | } |
|---|
| 186 | else if (this->action_ == Action::FLY) |
|---|
| 187 | { |
|---|
| [10849] | 188 | |
|---|
| 189 | if (this->squaredDistanceToTarget() <= this->squaredaccuracy_) |
|---|
| 190 | { |
|---|
| 191 | orxout(internal_error) << "arrived"; |
|---|
| 192 | this->nextActionpoint(); |
|---|
| 193 | return; |
|---|
| 194 | } |
|---|
| [10805] | 195 | this->setTargetPositionOfWingman(); |
|---|
| 196 | this->setTargetPositionOfFollower(); |
|---|
| 197 | } |
|---|
| 198 | else if (this->action_ == Action::PROTECT) |
|---|
| 199 | { |
|---|
| [10849] | 200 | if (!this->getProtect()) |
|---|
| 201 | { |
|---|
| 202 | this->nextActionpoint(); |
|---|
| 203 | return; |
|---|
| 204 | } |
|---|
| [10848] | 205 | /* if (this->myWingman_) |
|---|
| 206 | this->myWingman_->setAction (Action::PROTECT, this->getProtect()); |
|---|
| 207 | if (this->myFollower_) |
|---|
| 208 | this->myFollower_->setAction (Action::PROTECT, this->getProtect()); |
|---|
| 209 | */ |
|---|
| 210 | Vector3* targetRelativePosition; |
|---|
| 211 | |
|---|
| 212 | targetRelativePosition = new Vector3 (0, 0, 500); |
|---|
| 213 | |
|---|
| 214 | Vector3 targetAbsolutePosition = ((this->getProtect()->getWorldPosition()) + |
|---|
| 215 | (this->getProtect()->getWorldOrientation()* (*targetRelativePosition))); |
|---|
| 216 | this->setTargetPosition(targetAbsolutePosition); |
|---|
| 217 | |
|---|
| 218 | this->setTargetPositionOfWingman(); |
|---|
| 219 | this->setTargetPositionOfFollower(); |
|---|
| [10849] | 220 | |
|---|
| [10848] | 221 | } |
|---|
| 222 | else if (this->action_ == Action::ATTACK) |
|---|
| 223 | { |
|---|
| [10849] | 224 | if (!this->hasTarget()) |
|---|
| [10838] | 225 | { |
|---|
| [10849] | 226 | this->nextActionpoint(); |
|---|
| 227 | return; |
|---|
| 228 | } |
|---|
| 229 | //----fly in formation if far enough---- |
|---|
| 230 | Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); |
|---|
| 231 | if (diffVector.length() > this->attackRange_) |
|---|
| 232 | { |
|---|
| 233 | this->setTargetPositionOfWingman(); |
|---|
| 234 | this->setTargetPositionOfFollower(); |
|---|
| 235 | } |
|---|
| 236 | else |
|---|
| 237 | { |
|---|
| 238 | //----wingmans shall support the fire of their leaders---- |
|---|
| 239 | if (this->myWingman_) |
|---|
| [10840] | 240 | { |
|---|
| [10849] | 241 | this->myWingman_->setAction (this->action_, this->target_); |
|---|
| [10848] | 242 | } |
|---|
| [10849] | 243 | if (this->myFollower_) |
|---|
| [10848] | 244 | { |
|---|
| [10849] | 245 | this->myFollower_->setAction (this->action_); |
|---|
| 246 | } |
|---|
| [10848] | 247 | |
|---|
| [10838] | 248 | } |
|---|
| [10849] | 249 | |
|---|
| 250 | //----choose where to go---- |
|---|
| 251 | this->maneuver(); |
|---|
| 252 | //----fire if you can---- |
|---|
| 253 | this->bShooting_ = this->canFire(); |
|---|
| [10805] | 254 | } |
|---|
| 255 | |
|---|
| [10709] | 256 | } |
|---|
| [10725] | 257 | |
|---|
| [10731] | 258 | |
|---|
| 259 | |
|---|
| [10729] | 260 | void DivisionController::setTargetPositionOfWingman() |
|---|
| [10725] | 261 | { |
|---|
| 262 | if (!this->myWingman_) |
|---|
| 263 | return; |
|---|
| [10729] | 264 | Vector3* targetRelativePositionOfWingman; |
|---|
| [10725] | 265 | switch (this->formationMode_){ |
|---|
| [10759] | 266 | case FormationMode::WALL: |
|---|
| [10725] | 267 | { |
|---|
| [10729] | 268 | targetRelativePositionOfWingman = new Vector3 (400, 0, 0); |
|---|
| [10725] | 269 | break; |
|---|
| 270 | } |
|---|
| [10759] | 271 | case FormationMode::FINGER4: |
|---|
| [10725] | 272 | { |
|---|
| [10832] | 273 | targetRelativePositionOfWingman = new Vector3 (400, 0, 200); |
|---|
| [10725] | 274 | break; |
|---|
| 275 | } |
|---|
| [10826] | 276 | |
|---|
| [10759] | 277 | case FormationMode::DIAMOND: |
|---|
| [10725] | 278 | { |
|---|
| [10832] | 279 | targetRelativePositionOfWingman = new Vector3 (400, 0, 200); |
|---|
| [10725] | 280 | break; |
|---|
| 281 | } |
|---|
| 282 | } |
|---|
| [10729] | 283 | Quaternion orient = this->getControllableEntity()->getWorldOrientation(); |
|---|
| 284 | |
|---|
| 285 | Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + |
|---|
| 286 | (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman))); |
|---|
| 287 | |
|---|
| [10805] | 288 | myWingman_->setAction( Action::FLY, targetAbsolutePositionOfWingman, orient); |
|---|
| [10725] | 289 | |
|---|
| 290 | } |
|---|
| [10729] | 291 | void DivisionController::setTargetPositionOfFollower() |
|---|
| [10725] | 292 | { |
|---|
| 293 | if (!this->myFollower_) |
|---|
| 294 | return; |
|---|
| [10759] | 295 | this->myFollower_->setFormationMode(this->formationMode_); |
|---|
| 296 | |
|---|
| [10729] | 297 | Vector3* targetRelativePositionOfFollower; |
|---|
| [10725] | 298 | switch (this->formationMode_){ |
|---|
| [10759] | 299 | case FormationMode::WALL: |
|---|
| [10725] | 300 | { |
|---|
| [10729] | 301 | targetRelativePositionOfFollower = new Vector3 (-400, 0, 0); |
|---|
| [10725] | 302 | break; |
|---|
| 303 | } |
|---|
| [10759] | 304 | case FormationMode::FINGER4: |
|---|
| [10725] | 305 | { |
|---|
| [10832] | 306 | targetRelativePositionOfFollower = new Vector3 (-400, 0, 200); |
|---|
| [10725] | 307 | break; |
|---|
| 308 | } |
|---|
| [10826] | 309 | |
|---|
| [10759] | 310 | case FormationMode::DIAMOND: |
|---|
| [10725] | 311 | { |
|---|
| [10832] | 312 | targetRelativePositionOfFollower = new Vector3 (-400, 0, 200); |
|---|
| [10725] | 313 | break; |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| [10729] | 316 | Quaternion orient = this->getControllableEntity()->getWorldOrientation(); |
|---|
| 317 | |
|---|
| 318 | Vector3 targetAbsolutePositionOfFollower = ((this->getControllableEntity()->getWorldPosition()) + |
|---|
| 319 | (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfFollower))); |
|---|
| 320 | |
|---|
| [10805] | 321 | myFollower_->setAction ( Action::FLY, targetAbsolutePositionOfFollower, orient ); |
|---|
| [10729] | 322 | } |
|---|
| [10731] | 323 | |
|---|
| 324 | |
|---|
| 325 | bool DivisionController::setWingman(CommonController* cwingman) |
|---|
| 326 | { |
|---|
| 327 | |
|---|
| 328 | WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman); |
|---|
| 329 | if (!this->myWingman_) |
|---|
| 330 | { |
|---|
| 331 | this->myWingman_ = wingman; |
|---|
| 332 | return true; |
|---|
| 333 | } |
|---|
| 334 | else |
|---|
| 335 | { |
|---|
| 336 | return false; |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | } |
|---|
| 340 | bool DivisionController::setFollower(LeaderController* myFollower) |
|---|
| 341 | { |
|---|
| 342 | if (!this->myFollower_) |
|---|
| 343 | { |
|---|
| 344 | this->myFollower_ = myFollower; |
|---|
| 345 | return true; |
|---|
| 346 | } |
|---|
| 347 | else |
|---|
| 348 | { |
|---|
| 349 | return false; |
|---|
| 350 | } |
|---|
| 351 | } |
|---|
| 352 | bool DivisionController::hasWingman() |
|---|
| 353 | { |
|---|
| 354 | if (this->myWingman_) |
|---|
| 355 | return true; |
|---|
| 356 | else |
|---|
| 357 | return false; |
|---|
| 358 | } |
|---|
| 359 | bool DivisionController::hasFollower() |
|---|
| 360 | { |
|---|
| 361 | if (this->myFollower_) |
|---|
| 362 | return true; |
|---|
| 363 | else |
|---|
| 364 | return false; |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | |
|---|
| [10678] | 368 | |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | } |
|---|