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