| 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 |  *      Gani Aliguzhinov | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      ... | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | #include "ActionpointController.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include "core/XMLPort.h" | 
|---|
| 32 | #include <algorithm> | 
|---|
| 33 | #include "worldentities/Actionpoint.h" | 
|---|
| 34 | namespace orxonox | 
|---|
| 35 | { | 
|---|
| 36 |  | 
|---|
| 37 |     RegisterClass(ActionpointController); | 
|---|
| 38 |  | 
|---|
| 39 |     ActionpointController::ActionpointController(Context* context) : FightingController(context) | 
|---|
| 40 |     { | 
|---|
| 41 |         this->actionCounter_ = 0; | 
|---|
| 42 |         this->bInLoop_ = false; | 
|---|
| 43 |         this->bLoop_ = false; | 
|---|
| 44 |         this->bEndLoop_ = false; | 
|---|
| 45 |         loopActionpoints_.clear(); | 
|---|
| 46 |         parsedActionpoints_.clear(); | 
|---|
| 47 |         actionpoints_.clear(); | 
|---|
| 48 |         healthSpawners_.clear(); | 
|---|
| 49 |         damageSpawners_.clear(); | 
|---|
| 50 |         speedSpawners_.clear(); | 
|---|
| 51 |         this->bTakenOver_ = false; | 
|---|
| 52 |         this->action_ = Action::NONE; | 
|---|
| 53 |         this->squaredaccuracy_ = 2500; | 
|---|
| 54 |         this->bFirstTick_ = true; | 
|---|
| 55 |  | 
|---|
| 56 |         RegisterObject(ActionpointController); | 
|---|
| 57 |  | 
|---|
| 58 |     } | 
|---|
| 59 |     void ActionpointController::XMLPort( Element& xmlelement, XMLPort::Mode mode ) | 
|---|
| 60 |     { | 
|---|
| 61 |         SUPER( ActionpointController, XMLPort, xmlelement, mode ); | 
|---|
| 62 |         XMLPortObject(ActionpointController, WorldEntity, "actionpoints", addActionpoint, getActionpoint,  xmlelement, mode); | 
|---|
| 63 |     } | 
|---|
| 64 |     ActionpointController::~ActionpointController() | 
|---|
| 65 |     { | 
|---|
| 66 |         loopActionpoints_.clear(); | 
|---|
| 67 |         parsedActionpoints_.clear(); | 
|---|
| 68 |         actionpoints_.clear(); | 
|---|
| 69 |         healthSpawners_.clear(); | 
|---|
| 70 |         damageSpawners_.clear(); | 
|---|
| 71 |         speedSpawners_.clear(); | 
|---|
| 72 |     } | 
|---|
| 73 |     void ActionpointController::tick(float dt) | 
|---|
| 74 |     { | 
|---|
| 75 |  | 
|---|
| 76 |         if (this->timeout_ > 0 && this->bFiredRocket_) | 
|---|
| 77 |         { | 
|---|
| 78 |             this->timeout_ -= dt; | 
|---|
| 79 |         } | 
|---|
| 80 |         if (timeout_ <= 0) | 
|---|
| 81 |             this->bFiredRocket_ = false; | 
|---|
| 82 |  | 
|---|
| 83 |         if (this->bHasTargetPosition_) | 
|---|
| 84 |         { | 
|---|
| 85 |             this->moveToTargetPosition(dt); | 
|---|
| 86 |         } | 
|---|
| 87 |         else if (this->bLookAtTarget_) | 
|---|
| 88 |         { | 
|---|
| 89 |             this->lookAtTarget(dt); | 
|---|
| 90 |         } | 
|---|
| 91 |         if (this->bShooting_) | 
|---|
| 92 |         { | 
|---|
| 93 |             this->doFire(); | 
|---|
| 94 |         } | 
|---|
| 95 |         if (this->bFirstTick_) | 
|---|
| 96 |         { | 
|---|
| 97 |             std::reverse(parsedActionpoints_.begin(), parsedActionpoints_.end()); | 
|---|
| 98 |             std::reverse(actionpoints_.begin(), actionpoints_.end()); | 
|---|
| 99 |             if (this->parsedActionpoints_.empty()) | 
|---|
| 100 |             { | 
|---|
| 101 |                 this->action_ = Action::FIGHTALL; | 
|---|
| 102 |             } | 
|---|
| 103 |         } | 
|---|
| 104 |         if (this->bFirstTick_) | 
|---|
| 105 |         {     | 
|---|
| 106 |             setSpawners(); | 
|---|
| 107 |             // orxout(internal_error) << "health spawners size = " << this->healthSpawners_.size() << | 
|---|
| 108 |             // ", damage spawners size = " << this->damageSpawners_.size() << | 
|---|
| 109 |             // ", speed spawners size = " << this->speedSpawners_.size() << endl;         | 
|---|
| 110 |             this->bFirstTick_ = false; | 
|---|
| 111 |         } | 
|---|
| 112 |  | 
|---|
| 113 |         SUPER(ActionpointController, tick, dt); | 
|---|
| 114 |     } | 
|---|
| 115 |     void ActionpointController::action() | 
|---|
| 116 |     { | 
|---|
| 117 |         if (!this || !this->getControllableEntity()) | 
|---|
| 118 |             return; | 
|---|
| 119 |         if (!this->getControllableEntity() || !orxonox_cast<Pawn*> (this->getControllableEntity())) | 
|---|
| 120 |             return; | 
|---|
| 121 |             this->startAttackingEnemiesThatAreClose(); | 
|---|
| 122 |  | 
|---|
| 123 |         this->deltaHp = orxonox_cast<Pawn*> (this->getControllableEntity())->getHealth() - this->previousHp; | 
|---|
| 124 |         this->previousHp = orxonox_cast<Pawn*> (this->getControllableEntity())->getHealth(); | 
|---|
| 125 |         // if (this->actionCounter_ % 2 == 0) | 
|---|
| 126 |         //No action -> pop one from stack | 
|---|
| 127 |         if (this->action_ == Action::NONE || this->bTakenOver_) | 
|---|
| 128 |         { | 
|---|
| 129 |             // Point point = closestPickup(PickupType::HEALTH); | 
|---|
| 130 |             // if (point.action != Action::NONE) | 
|---|
| 131 |             // { | 
|---|
| 132 |             //     orxout(internal_error) << "found health" << endl; | 
|---|
| 133 |             //     this->parsedActionpoints_.push_back(point); | 
|---|
| 134 |             // } | 
|---|
| 135 |  | 
|---|
| 136 |             if (this->parsedActionpoints_.empty() && this->loopActionpoints_.empty()) | 
|---|
| 137 |             { | 
|---|
| 138 |                 Point p = { Action::FIGHTALL, "", Vector3::ZERO, false }; | 
|---|
| 139 |                 this->parsedActionpoints_.push_back (p); | 
|---|
| 140 |             } | 
|---|
| 141 |             this->executeActionpoint(); | 
|---|
| 142 |             this->bTakenOver_ = false; | 
|---|
| 143 |             this->action(); | 
|---|
| 144 |         } | 
|---|
| 145 |         //Action fightall -> fight till nobody alive | 
|---|
| 146 |         if (this->action_ == Action::FIGHTALL) | 
|---|
| 147 |         { | 
|---|
| 148 |  | 
|---|
| 149 |             if (!this->hasTarget()) | 
|---|
| 150 |             { | 
|---|
| 151 |                 ControllableEntity* newTarget = this->closestTarget();     | 
|---|
| 152 |                 if (newTarget) | 
|---|
| 153 |                 { | 
|---|
| 154 |                     this->setAction (Action::FIGHTALL, newTarget); | 
|---|
| 155 |                     this->action(); | 
|---|
| 156 |                 } | 
|---|
| 157 |                 else | 
|---|
| 158 |                 { | 
|---|
| 159 |                     this->nextActionpoint(); | 
|---|
| 160 |                     if (!(this->parsedActionpoints_.empty() && this->loopActionpoints_.empty())) | 
|---|
| 161 |                     { | 
|---|
| 162 |                         this->action(); | 
|---|
| 163 |                     } | 
|---|
| 164 |                     else | 
|---|
| 165 |                     { | 
|---|
| 166 |                     }       | 
|---|
| 167 |                 } | 
|---|
| 168 |             } | 
|---|
| 169 |         } | 
|---|
| 170 |         //Action fight -> fight as long as enemies in range | 
|---|
| 171 |         else if (this->action_ == Action::FIGHT) | 
|---|
| 172 |         { | 
|---|
| 173 |  | 
|---|
| 174 |             if (!this->hasTarget() ) | 
|---|
| 175 |             { | 
|---|
| 176 |                 //----find a target---- | 
|---|
| 177 |                 ControllableEntity* newTarget = this->closestTarget();     | 
|---|
| 178 |                 if (newTarget &&  | 
|---|
| 179 |                         CommonController::distance (this->getControllableEntity(), newTarget) < this->attackRange_) | 
|---|
| 180 |                 { | 
|---|
| 181 |                     this->setAction (Action::FIGHT, newTarget); | 
|---|
| 182 |                     this->action(); | 
|---|
| 183 |                 } | 
|---|
| 184 |                 else | 
|---|
| 185 |                 { | 
|---|
| 186 |                     this->nextActionpoint(); | 
|---|
| 187 |                     this->action(); | 
|---|
| 188 |                 } | 
|---|
| 189 |             } | 
|---|
| 190 |             else if (this->hasTarget()) | 
|---|
| 191 |             { | 
|---|
| 192 |                 //----fly in formation if far enough---- | 
|---|
| 193 |                 Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();          | 
|---|
| 194 |                      | 
|---|
| 195 |                 if (diffVector.length() > this->attackRange_) | 
|---|
| 196 |                 { | 
|---|
| 197 |                     ControllableEntity* newTarget = this->closestTarget(); | 
|---|
| 198 |                      | 
|---|
| 199 |                     if (newTarget &&  | 
|---|
| 200 |                         CommonController::distance (this->getControllableEntity(), newTarget) < this->attackRange_) | 
|---|
| 201 |                     { | 
|---|
| 202 |                         this->setAction (Action::FIGHT, newTarget); | 
|---|
| 203 |                     } | 
|---|
| 204 |                     else | 
|---|
| 205 |                     { | 
|---|
| 206 |                         this->nextActionpoint(); | 
|---|
| 207 |                         this->action(); | 
|---|
| 208 |                     } | 
|---|
| 209 |                 } | 
|---|
| 210 |             } | 
|---|
| 211 |         } | 
|---|
| 212 |         else if (this->action_ == Action::FLY) | 
|---|
| 213 |         { | 
|---|
| 214 |             if (this->squaredDistanceToTarget() <= this->squaredaccuracy_) | 
|---|
| 215 |             { | 
|---|
| 216 |                 this->nextActionpoint();    | 
|---|
| 217 |                 this->action(); | 
|---|
| 218 |             } | 
|---|
| 219 |         } | 
|---|
| 220 |         else if (this->action_ == Action::PROTECT) | 
|---|
| 221 |         { | 
|---|
| 222 |             if (!this->getProtect()) | 
|---|
| 223 |             { | 
|---|
| 224 |                 this->nextActionpoint(); | 
|---|
| 225 |                 this->action();  | 
|---|
| 226 |             } | 
|---|
| 227 |             this->stayNearProtect(); | 
|---|
| 228 |         } | 
|---|
| 229 |         else if (this->action_ == Action::ATTACK) | 
|---|
| 230 |         {    | 
|---|
| 231 |  | 
|---|
| 232 |             if (!this->hasTarget()) | 
|---|
| 233 |             { | 
|---|
| 234 |                 this->nextActionpoint(); | 
|---|
| 235 |                 this->action(); | 
|---|
| 236 |             } | 
|---|
| 237 |         } | 
|---|
| 238 |         if (this->hasTarget()) | 
|---|
| 239 |         { | 
|---|
| 240 |             this->maneuver(); | 
|---|
| 241 |             this->bShooting_ = this->canFire(); | 
|---|
| 242 |             // Vector3 healthPosition = bestHealthPickup((this->target_->getWorldPosition() - this->getControllableEntity()->getWorldPosition()).length()); | 
|---|
| 243 |             // if ((this->getControllableEntity()->getWorldPosition() - healthPosition).length() < this->tolerance_) | 
|---|
| 244 |             // { | 
|---|
| 245 |             //     //----choose where to go---- | 
|---|
| 246 |             //     this->maneuver(); | 
|---|
| 247 |             // } | 
|---|
| 248 |             // else | 
|---|
| 249 |             // { | 
|---|
| 250 |             //     this->dodgeTowards(healthPosition); | 
|---|
| 251 |             // } | 
|---|
| 252 |             // //----fire if you can---- | 
|---|
| 253 |             // this->bShooting_ = this->canFire();   | 
|---|
| 254 |         } | 
|---|
| 255 |         this->actionCounter_ += this->actionCounter_ < 100000 ? 1 : -this->actionCounter_ ; | 
|---|
| 256 |  | 
|---|
| 257 |     } | 
|---|
| 258 |     void ActionpointController::setProtect (ControllableEntity* protect) | 
|---|
| 259 |     { | 
|---|
| 260 |         this->protect_ = protect; | 
|---|
| 261 |     } | 
|---|
| 262 |     ControllableEntity* ActionpointController::getProtect () | 
|---|
| 263 |     { | 
|---|
| 264 |         return this->protect_; | 
|---|
| 265 |     } | 
|---|
| 266 |     void ActionpointController::addActionpoint(WorldEntity* actionpoint) | 
|---|
| 267 |     { | 
|---|
| 268 |         std::string actionName; | 
|---|
| 269 |         Vector3 position; | 
|---|
| 270 |         std::string targetName; | 
|---|
| 271 |         bool inLoop = false; | 
|---|
| 272 |         Point p; | 
|---|
| 273 |         if (actionpoint->getIdentifier()->getName() == "Actionpoint") | 
|---|
| 274 |         { | 
|---|
| 275 |             Actionpoint* ap = orxonox_cast<Actionpoint*> (actionpoint); | 
|---|
| 276 |             actionName = ap->getActionXML(); | 
|---|
| 277 |             targetName = ap->getName(); | 
|---|
| 278 |             position = ap->getWorldPosition(); | 
|---|
| 279 |  | 
|---|
| 280 |             if (this->bEndLoop_) | 
|---|
| 281 |             { | 
|---|
| 282 |                 this->bInLoop_ = false; | 
|---|
| 283 |             } | 
|---|
| 284 |             if (!this->bInLoop_ && ap->getLoopStart()) | 
|---|
| 285 |             { | 
|---|
| 286 |                 this->bInLoop_ = true; | 
|---|
| 287 |             } | 
|---|
| 288 |             if (this->bInLoop_ && ap->getLoopEnd()) | 
|---|
| 289 |             { | 
|---|
| 290 |                 this->bEndLoop_ = true; | 
|---|
| 291 |             } | 
|---|
| 292 |             inLoop = this->bInLoop_; | 
|---|
| 293 |  | 
|---|
| 294 |             Action::Value value; | 
|---|
| 295 |              | 
|---|
| 296 |             if ( actionName == "FIGHT" ) | 
|---|
| 297 |             { value = Action::FIGHT; } | 
|---|
| 298 |             else if ( actionName == "FLY" ) | 
|---|
| 299 |             { value = Action::FLY; } | 
|---|
| 300 |             else if ( actionName == "PROTECT" ) | 
|---|
| 301 |             { value = Action::PROTECT; } | 
|---|
| 302 |             else if ( actionName == "NONE" ) | 
|---|
| 303 |             { value = Action::NONE; } | 
|---|
| 304 |             else if ( actionName == "FIGHTALL" ) | 
|---|
| 305 |             { value = Action::FIGHTALL; } | 
|---|
| 306 |             else if ( actionName == "ATTACK" ) | 
|---|
| 307 |             { value = Action::ATTACK; } | 
|---|
| 308 |             else | 
|---|
| 309 |                 ThrowException( ParseError, std::string( "Attempting to set an unknown Action: '" )+ actionName + "'." ); | 
|---|
| 310 |             p.action = value; p.name = targetName; p.position = position; p.inLoop = inLoop; | 
|---|
| 311 |         } | 
|---|
| 312 |         else | 
|---|
| 313 |         { | 
|---|
| 314 |             inLoop = true; | 
|---|
| 315 |             p.action = Action::FLY; p.name = ""; p.position = actionpoint->getWorldPosition(); p.inLoop = inLoop; | 
|---|
| 316 |         } | 
|---|
| 317 |             parsedActionpoints_.push_back(p); | 
|---|
| 318 |             this->actionpoints_.push_back(actionpoint); | 
|---|
| 319 |     } | 
|---|
| 320 |     WorldEntity* ActionpointController::getActionpoint(unsigned int index) const | 
|---|
| 321 |     { | 
|---|
| 322 |         if (index < this->actionpoints_.size()) | 
|---|
| 323 |             return this->actionpoints_[index]; | 
|---|
| 324 |         else | 
|---|
| 325 |             return 0; | 
|---|
| 326 |     } | 
|---|
| 327 |  | 
|---|
| 328 |     Action::Value ActionpointController::getAction () | 
|---|
| 329 |     { | 
|---|
| 330 |         return this->action_; | 
|---|
| 331 |     } | 
|---|
| 332 |     std::string ActionpointController::getActionName() | 
|---|
| 333 |     { | 
|---|
| 334 |         switch ( this->action_ ) | 
|---|
| 335 |         { | 
|---|
| 336 |             case Action::FIGHT: | 
|---|
| 337 |             { return "FIGHT"; break; } | 
|---|
| 338 |             case Action::FLY: | 
|---|
| 339 |             { return "FLY"; break; } | 
|---|
| 340 |             case Action::PROTECT: | 
|---|
| 341 |             { return "PROTECT"; break; } | 
|---|
| 342 |             case Action::NONE: | 
|---|
| 343 |             { return "NONE"; break; } | 
|---|
| 344 |             case Action::FIGHTALL: | 
|---|
| 345 |             { return "FIGHTALL"; break; } | 
|---|
| 346 |             case Action::ATTACK: | 
|---|
| 347 |             { return "ATTACK"; break; } | 
|---|
| 348 |             default: | 
|---|
| 349 |                 return "NONE"; | 
|---|
| 350 |                 break; | 
|---|
| 351 |         } | 
|---|
| 352 |     } | 
|---|
| 353 |     void ActionpointController::setAction (Action::Value action) | 
|---|
| 354 |     { | 
|---|
| 355 |         this->action_ = action; | 
|---|
| 356 |     } | 
|---|
| 357 |     void ActionpointController::setAction (Action::Value action, ControllableEntity* target) | 
|---|
| 358 |     { | 
|---|
| 359 |         this->action_ = action; | 
|---|
| 360 |         if (action == Action::FIGHT || action == Action::FIGHTALL || action == Action::ATTACK) | 
|---|
| 361 |         {    | 
|---|
| 362 |             if (target) | 
|---|
| 363 |                 this->setTarget (target); | 
|---|
| 364 |         } | 
|---|
| 365 |         else if (action == Action::PROTECT) | 
|---|
| 366 |         { | 
|---|
| 367 |             if (target) | 
|---|
| 368 |                 this->setProtect (target); | 
|---|
| 369 |         } | 
|---|
| 370 |     } | 
|---|
| 371 |     void ActionpointController::setAction (Action::Value action, const Vector3& target) | 
|---|
| 372 |     { | 
|---|
| 373 |         this->action_ = action; | 
|---|
| 374 |         if (action == Action::FLY) | 
|---|
| 375 |         { | 
|---|
| 376 |             this->setTargetPosition (target); | 
|---|
| 377 |         } | 
|---|
| 378 |     } | 
|---|
| 379 |     void ActionpointController::setAction (Action::Value action, const Vector3& target,  const Quaternion& orient ) | 
|---|
| 380 |     { | 
|---|
| 381 |         this->action_ = action; | 
|---|
| 382 |         if (action == Action::FLY) | 
|---|
| 383 |         { | 
|---|
| 384 |             this->setTargetPosition (target); | 
|---|
| 385 |             this->setTargetOrientation (orient); | 
|---|
| 386 |         }   | 
|---|
| 387 |     } | 
|---|
| 388 |      | 
|---|
| 389 |     //------------------------------------------------------------------------------ | 
|---|
| 390 |     //------------------------------Actionpoint methods----------------------------- | 
|---|
| 391 |     //------------------------------------------------------------------------------ | 
|---|
| 392 |  | 
|---|
| 393 |     //POST: this starts doing what was asked by the last element of parsedActionpoints_, | 
|---|
| 394 |     //if last element was failed to be parsed, next element will be executed. | 
|---|
| 395 |     void ActionpointController::executeActionpoint() | 
|---|
| 396 |     { | 
|---|
| 397 |         Point p; | 
|---|
| 398 |         if (this->bLoop_ && !loopActionpoints_.empty()) | 
|---|
| 399 |         { | 
|---|
| 400 |             p = loopActionpoints_.back(); | 
|---|
| 401 |         } | 
|---|
| 402 |         else if (this->bLoop_) | 
|---|
| 403 |         { | 
|---|
| 404 |             this->bLoop_ = false; | 
|---|
| 405 |             return; | 
|---|
| 406 |         } | 
|---|
| 407 |         else if (!this->bLoop_ && !parsedActionpoints_.empty()) | 
|---|
| 408 |         { | 
|---|
| 409 |             p = parsedActionpoints_.back(); | 
|---|
| 410 |         } | 
|---|
| 411 |         else | 
|---|
| 412 |         { | 
|---|
| 413 |             this->setTarget(0); | 
|---|
| 414 |             this->setTargetPosition(this->getControllableEntity()->getWorldPosition()); | 
|---|
| 415 |             this->action_ = Action::NONE; | 
|---|
| 416 |             return; | 
|---|
| 417 |         } | 
|---|
| 418 |         if (!this->bLoop_ && this->parsedActionpoints_.back().inLoop) | 
|---|
| 419 |         { | 
|---|
| 420 |             //MOVES all points that are in loop to a loop vector | 
|---|
| 421 |             this->fillLoop(); | 
|---|
| 422 |             this->bLoop_ = true; | 
|---|
| 423 |             executeActionpoint(); | 
|---|
| 424 |             return; | 
|---|
| 425 |         } | 
|---|
| 426 |         this->setAction (p.action); | 
|---|
| 427 |         switch (this->action_) | 
|---|
| 428 |         { | 
|---|
| 429 |             case Action::FIGHT: | 
|---|
| 430 |             { | 
|---|
| 431 |                 std::string targetName = p.name; | 
|---|
| 432 |                 if (targetName == "") | 
|---|
| 433 |                     break; | 
|---|
| 434 |                 for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) | 
|---|
| 435 |                 { | 
|---|
| 436 |                     if (CommonController::getName(*itP) == targetName) | 
|---|
| 437 |                     { | 
|---|
| 438 |                         this->setTarget (static_cast<ControllableEntity*>(*itP)); | 
|---|
| 439 |                     } | 
|---|
| 440 |                 } | 
|---|
| 441 |                 break; | 
|---|
| 442 |             } | 
|---|
| 443 |             case Action::FLY: | 
|---|
| 444 |             { | 
|---|
| 445 |                 this->setTargetPosition( p.position ); | 
|---|
| 446 |                 if (this->squaredDistanceToTarget() <= this->squaredaccuracy_) | 
|---|
| 447 |                 { | 
|---|
| 448 |                     this->nextActionpoint(); | 
|---|
| 449 |                     this->executeActionpoint(); | 
|---|
| 450 |                 } | 
|---|
| 451 |                 break; | 
|---|
| 452 |             } | 
|---|
| 453 |             case Action::PROTECT: | 
|---|
| 454 |             { | 
|---|
| 455 |                  | 
|---|
| 456 |                 std::string protectName = p.name; | 
|---|
| 457 |                 if (protectName == "reservedKeyword:human") | 
|---|
| 458 |                 { | 
|---|
| 459 |                     for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) | 
|---|
| 460 |                     { | 
|---|
| 461 |                         if (orxonox_cast<ControllableEntity*>(*itP) && ((*itP)->getController()) && ((*itP)->getController()->getIdentifier()->getName() == "NewHumanController")) | 
|---|
| 462 |                         { | 
|---|
| 463 |                             this->setProtect (static_cast<ControllableEntity*>(*itP)); | 
|---|
| 464 |                         } | 
|---|
| 465 |                     } | 
|---|
| 466 |                 } | 
|---|
| 467 |                 else | 
|---|
| 468 |                 { | 
|---|
| 469 |                     for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) | 
|---|
| 470 |                     { | 
|---|
| 471 |                         if (CommonController::getName(*itP) == protectName) | 
|---|
| 472 |                         { | 
|---|
| 473 |                             this->setProtect (static_cast<ControllableEntity*>(*itP)); | 
|---|
| 474 |                         } | 
|---|
| 475 |                     }                             | 
|---|
| 476 |                 } | 
|---|
| 477 |                 if (!this->getProtect()) | 
|---|
| 478 |                 { | 
|---|
| 479 |                     this->nextActionpoint(); | 
|---|
| 480 |                     this->executeActionpoint(); | 
|---|
| 481 |                 } | 
|---|
| 482 |                 break; | 
|---|
| 483 |             } | 
|---|
| 484 |             case Action::NONE: | 
|---|
| 485 |             { | 
|---|
| 486 |                 break; | 
|---|
| 487 |             } | 
|---|
| 488 |             case Action::FIGHTALL: | 
|---|
| 489 |             { | 
|---|
| 490 |                 break; | 
|---|
| 491 |             } | 
|---|
| 492 |             case Action::ATTACK: | 
|---|
| 493 |             { | 
|---|
| 494 |                 std::string targetName = p.name; | 
|---|
| 495 |  | 
|---|
| 496 |                 for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) | 
|---|
| 497 |                 { | 
|---|
| 498 |                     if (CommonController::getName(*itP) == targetName) | 
|---|
| 499 |                     { | 
|---|
| 500 |                         this->setTarget (static_cast<ControllableEntity*>(*itP)); | 
|---|
| 501 |                     } | 
|---|
| 502 |                 } | 
|---|
| 503 |                 if (!this->hasTarget()) | 
|---|
| 504 |                 { | 
|---|
| 505 |                     this->nextActionpoint(); | 
|---|
| 506 |                     this->executeActionpoint(); | 
|---|
| 507 |                 } | 
|---|
| 508 |                 break; | 
|---|
| 509 |             } | 
|---|
| 510 |             default: | 
|---|
| 511 |                 break; | 
|---|
| 512 |         }    | 
|---|
| 513 |     } | 
|---|
| 514 |  | 
|---|
| 515 |      | 
|---|
| 516 |     void ActionpointController::stayNearProtect() | 
|---|
| 517 |     { | 
|---|
| 518 |         Vector3 targetRelativePosition(0, 300, 300); | 
|---|
| 519 |         if (!this->getProtect()) | 
|---|
| 520 |         { | 
|---|
| 521 |             this->nextActionpoint(); | 
|---|
| 522 |             return; | 
|---|
| 523 |         }   | 
|---|
| 524 |         Vector3 targetAbsolutePosition = ((this->getProtect()->getWorldPosition()) +  | 
|---|
| 525 |             (this->getProtect()->getWorldOrientation()* (targetRelativePosition))); | 
|---|
| 526 |         this->setTargetPosition(targetAbsolutePosition); | 
|---|
| 527 |         if (!this->getProtect()) | 
|---|
| 528 |         { | 
|---|
| 529 |             this->nextActionpoint(); | 
|---|
| 530 |             return; | 
|---|
| 531 |         }   | 
|---|
| 532 |         if (this->actionCounter_ % 3 == 0) | 
|---|
| 533 |             this->setTargetOrientation(this->getProtect()->getWorldOrientation()); | 
|---|
| 534 |     } | 
|---|
| 535 |     void ActionpointController::nextActionpoint() | 
|---|
| 536 |     { | 
|---|
| 537 |         if (!this || !this->getControllableEntity()) | 
|---|
| 538 |             return; | 
|---|
| 539 |         if (this->bLoop_) | 
|---|
| 540 |         { | 
|---|
| 541 |             if (!this->loopActionpoints_.empty()) | 
|---|
| 542 |             { | 
|---|
| 543 |                 this->moveBackToTop(); | 
|---|
| 544 |             } | 
|---|
| 545 |         } | 
|---|
| 546 |         else | 
|---|
| 547 |         { | 
|---|
| 548 |             if (!this->parsedActionpoints_.empty()) | 
|---|
| 549 |             { | 
|---|
| 550 |                 this->parsedActionpoints_.pop_back(); | 
|---|
| 551 |             }             | 
|---|
| 552 |         } | 
|---|
| 553 |         this->setAction(Action::NONE); | 
|---|
| 554 |         this->bHasTargetPosition_ = false; | 
|---|
| 555 |     } | 
|---|
| 556 |     void ActionpointController::moveBackToTop() | 
|---|
| 557 |     { | 
|---|
| 558 |         Point temp = loopActionpoints_.back(); | 
|---|
| 559 |         loopActionpoints_.pop_back(); | 
|---|
| 560 |         std::reverse (loopActionpoints_.begin(), loopActionpoints_.end()); | 
|---|
| 561 |         loopActionpoints_.push_back(temp); | 
|---|
| 562 |         std::reverse (loopActionpoints_.begin(), loopActionpoints_.end()); | 
|---|
| 563 |     } | 
|---|
| 564 |     void ActionpointController::fillLoop() | 
|---|
| 565 |     { | 
|---|
| 566 |         loopActionpoints_.clear(); | 
|---|
| 567 |         fillLoopReversed(); | 
|---|
| 568 |         std::reverse (loopActionpoints_.begin(), loopActionpoints_.end()); | 
|---|
| 569 |     } | 
|---|
| 570 |     void ActionpointController::fillLoopReversed() | 
|---|
| 571 |     { | 
|---|
| 572 |         if (parsedActionpoints_.back().inLoop) | 
|---|
| 573 |         { | 
|---|
| 574 |             loopActionpoints_.push_back(parsedActionpoints_.back()); | 
|---|
| 575 |             parsedActionpoints_.pop_back(); | 
|---|
| 576 |         } | 
|---|
| 577 |         if (parsedActionpoints_.back().inLoop) | 
|---|
| 578 |         { | 
|---|
| 579 |             fillLoopReversed(); | 
|---|
| 580 |         } | 
|---|
| 581 |     } | 
|---|
| 582 |      | 
|---|
| 583 |     void ActionpointController::takeActionpoints (const std::vector<Point>& vector, const std::vector<Point>& loop, bool b) | 
|---|
| 584 |     { | 
|---|
| 585 |       this->parsedActionpoints_ = vector; | 
|---|
| 586 |       this->loopActionpoints_ = loop; | 
|---|
| 587 |       this->bLoop_ = b; | 
|---|
| 588 |       this->bTakenOver_ = true; | 
|---|
| 589 |     } | 
|---|
| 590 |     void ActionpointController::setClosestTarget() | 
|---|
| 591 |     { | 
|---|
| 592 |         this->setTarget (static_cast<ControllableEntity*>( closestTarget() ) );  | 
|---|
| 593 |     } | 
|---|
| 594 |     Pawn* ActionpointController::closestTarget() | 
|---|
| 595 |     { | 
|---|
| 596 |         if (!this->getControllableEntity()) | 
|---|
| 597 |             return 0; | 
|---|
| 598 |  | 
|---|
| 599 |         Pawn* closestTarget = 0; | 
|---|
| 600 |         float minDistance =  std::numeric_limits<float>::infinity(); | 
|---|
| 601 |         Gametype* gt = this->getGametype(); | 
|---|
| 602 |         for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) | 
|---|
| 603 |         { | 
|---|
| 604 |             if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) ) | 
|---|
| 605 |                 continue; | 
|---|
| 606 |  | 
|---|
| 607 |             float distance = CommonController::distance (*itP, this->getControllableEntity()); | 
|---|
| 608 |             if (distance < minDistance) | 
|---|
| 609 |             { | 
|---|
| 610 |                 closestTarget = *itP; | 
|---|
| 611 |                 minDistance = distance; | 
|---|
| 612 |             } | 
|---|
| 613 |         } | 
|---|
| 614 |         if (closestTarget) | 
|---|
| 615 |         { | 
|---|
| 616 |            return closestTarget; | 
|---|
| 617 |         }  | 
|---|
| 618 |         return 0;   | 
|---|
| 619 |     } | 
|---|
| 620 |     void ActionpointController::startAttackingEnemiesThatAreClose() | 
|---|
| 621 |     { | 
|---|
| 622 |         //if (this->action_ != Action::FIGHT && this->action_ != Action::FIGHTALL) | 
|---|
| 623 |         { | 
|---|
| 624 |             if (!this->target_ || (this->target_ && CommonController::distance (this->getControllableEntity(), this->target_) > this->attackRange_)) | 
|---|
| 625 |             { | 
|---|
| 626 |                 Pawn* newTarget = this->closestTarget(); | 
|---|
| 627 |                 if ( newTarget &&  | 
|---|
| 628 |                     CommonController::distance (this->getControllableEntity(), static_cast<ControllableEntity*>(newTarget)) | 
|---|
| 629 |                         <= this->attackRange_ ) | 
|---|
| 630 |                 { | 
|---|
| 631 |                     if (this->bLoop_) | 
|---|
| 632 |                     { | 
|---|
| 633 |                         Point p = { Action::FIGHT, CommonController::getName(newTarget), Vector3::ZERO, true }; | 
|---|
| 634 |                         this->loopActionpoints_.push_back(p); | 
|---|
| 635 |                     } | 
|---|
| 636 |                     else | 
|---|
| 637 |                     { | 
|---|
| 638 |                         //orxout (internal_error) << "found new target " << CommonController::getName(newTarget) <<endl; | 
|---|
| 639 |                         Point p = { Action::FIGHT, CommonController::getName(newTarget), Vector3::ZERO, false }; | 
|---|
| 640 |                         this->parsedActionpoints_.push_back(p); | 
|---|
| 641 |                     } | 
|---|
| 642 |                     this->executeActionpoint(); | 
|---|
| 643 |                 } | 
|---|
| 644 |             } | 
|---|
| 645 |         } | 
|---|
| 646 |     } | 
|---|
| 647 |     Vector3 ActionpointController::bestHealthPickup(float searchRadius) | 
|---|
| 648 |     { | 
|---|
| 649 |  | 
|---|
| 650 |         //1000000/distance^2 * 1/index+1 | 
|---|
| 651 |         float maxUse = 0; | 
|---|
| 652 |         float tempUse = -1; | 
|---|
| 653 |         int index = 0; | 
|---|
| 654 |         float distance = 0; | 
|---|
| 655 |         Vector3 bestPosition = this->getControllableEntity()->getWorldPosition(); | 
|---|
| 656 |  | 
|---|
| 657 |         for (std::multimap<int, std::pair<PickupSpawner*, bool> >::iterator it=healthSpawners_.begin(); it!=healthSpawners_.end(); ++it) | 
|---|
| 658 |         { | 
|---|
| 659 |             distance = (this->getControllableEntity()->getWorldPosition() - (*it).second.first->getWorldPosition()).length(); | 
|---|
| 660 |             if (distance < this->tolerance_ || !(*it).second.second) | 
|---|
| 661 |             { | 
|---|
| 662 |                 (*it).second.second = false; | 
|---|
| 663 |                 continue; | 
|---|
| 664 |             } | 
|---|
| 665 |             if (distance > searchRadius) | 
|---|
| 666 |                 continue; | 
|---|
| 667 |             index = (*it).first; | 
|---|
| 668 |             tempUse = 1000000*1/(1+distance*distance) * 1/((index/2.0f)+1); | 
|---|
| 669 |             if (tempUse > maxUse) | 
|---|
| 670 |             { | 
|---|
| 671 |                 bestPosition = (*it).second.first->getWorldPosition(); | 
|---|
| 672 |                 maxUse = tempUse; | 
|---|
| 673 |             } | 
|---|
| 674 |         } | 
|---|
| 675 |         //std::multimap<int, std::pair<PickupSpawner*, bool> >::iterator it = this->healthSpawners_.find(index); | 
|---|
| 676 |         //P.S. check if it == ... .end() | 
|---|
| 677 |         //orxout (internal_error) << "best position = " << bestPosition << endl; | 
|---|
| 678 |         return  bestPosition; | 
|---|
| 679 |     } | 
|---|
| 680 |     void ActionpointController::setSpawners() | 
|---|
| 681 |     { | 
|---|
| 682 |         std::vector<std::string> damagePickups; | 
|---|
| 683 |         std::vector<std::string> healthPickups; | 
|---|
| 684 |         std::vector<std::string> speedPickups;        | 
|---|
| 685 |         int index = 0; | 
|---|
| 686 |  | 
|---|
| 687 |  | 
|---|
| 688 |         damagePickups.push_back("largedamageboostpickup"); | 
|---|
| 689 |         damagePickups.push_back("dronepickup");             | 
|---|
| 690 |         damagePickups.push_back("mediumdamageboostpickup"); | 
|---|
| 691 |         damagePickups.push_back("smalldamageboostpickup"); | 
|---|
| 692 |  | 
|---|
| 693 |         healthPickups.push_back("triplehealthspeedinvisibilitypickup");             | 
|---|
| 694 |         healthPickups.push_back("crazyhealthpickup"); | 
|---|
| 695 |         healthPickups.push_back("hugehealthpickup"); | 
|---|
| 696 |         healthPickups.push_back("hugeshieldpickup"); | 
|---|
| 697 |         healthPickups.push_back("hugeinvisiblepickup"); | 
|---|
| 698 |         healthPickups.push_back("hugeshrinkpickup"); | 
|---|
| 699 |         healthPickups.push_back("mediumhealthpickup"); | 
|---|
| 700 |         healthPickups.push_back("mediumshieldpickup"); | 
|---|
| 701 |         healthPickups.push_back("mediuminvisiblepickup"); | 
|---|
| 702 |         healthPickups.push_back("mediumshrinkpickup");   | 
|---|
| 703 |         healthPickups.push_back("smallhealthpickup");                    | 
|---|
| 704 |         healthPickups.push_back("smallshieldpickup"); | 
|---|
| 705 |         healthPickups.push_back("smallinvisiblepickup"); | 
|---|
| 706 |         healthPickups.push_back("smallshrinkpickup"); | 
|---|
| 707 |  | 
|---|
| 708 |         speedPickups.push_back("triplehealthspeedinvisibilitypickup"); | 
|---|
| 709 |         speedPickups.push_back("hugespeedpickup"); | 
|---|
| 710 |         speedPickups.push_back("smalljumppickup"); | 
|---|
| 711 |         speedPickups.push_back("mediumspeedpickup"); | 
|---|
| 712 |         speedPickups.push_back("smallspeedpickup"); | 
|---|
| 713 |          | 
|---|
| 714 |  | 
|---|
| 715 |  | 
|---|
| 716 |         for (ObjectList<WorldEntity>::iterator it = ObjectList<WorldEntity>::begin(); it; ++it) | 
|---|
| 717 |         { | 
|---|
| 718 |             if ((*it)->getIdentifier()->getName() != "PickupSpawner") | 
|---|
| 719 |                 continue; | 
|---|
| 720 |             PickupSpawner* spawner = static_cast<PickupSpawner*>(*it); | 
|---|
| 721 |             std::string name = spawner->getPickupTemplateName(); | 
|---|
| 722 |             //float distance = ((*it)->getWorldPosition() - this->getControllableEntity()->getWorldPosition()).length();  | 
|---|
| 723 |             // if (distance < 50.0f) | 
|---|
| 724 |             //     continue;            | 
|---|
| 725 |             for (unsigned int i = 0; i < healthPickups.size(); ++i) | 
|---|
| 726 |             { | 
|---|
| 727 |                 if (name == healthPickups.at(i)) | 
|---|
| 728 |                 { | 
|---|
| 729 |                     index = i; | 
|---|
| 730 |                     this->healthSpawners_.insert(std::make_pair(index, std::make_pair(spawner, true))); | 
|---|
| 731 |                     break; | 
|---|
| 732 |                 } | 
|---|
| 733 |             } | 
|---|
| 734 |             for (unsigned int i = 0; i < damagePickups.size(); ++i) | 
|---|
| 735 |             { | 
|---|
| 736 |                 if (name == damagePickups.at(i)) | 
|---|
| 737 |                 { | 
|---|
| 738 |                     //tempUse = 1000000*1/(1+distance*distance) * 1/((i/2.0f)+1); | 
|---|
| 739 |                     index = i; | 
|---|
| 740 |                     this->damageSpawners_.insert(std::make_pair(index, std::make_pair(spawner, true))); | 
|---|
| 741 |                     break; | 
|---|
| 742 |                 } | 
|---|
| 743 |             } | 
|---|
| 744 |             for (unsigned int i = 0; i < speedPickups.size(); ++i) | 
|---|
| 745 |             { | 
|---|
| 746 |                 if (name == speedPickups.at(i)) | 
|---|
| 747 |                 { | 
|---|
| 748 |                     //tempUse = 1000000*1/(1+distance*distance) * 1/((i/2.0f)+1); | 
|---|
| 749 |                     index = i; | 
|---|
| 750 |                     this->speedSpawners_.insert(std::make_pair(index, std::make_pair(spawner, true))); | 
|---|
| 751 |                     break; | 
|---|
| 752 |                 } | 
|---|
| 753 |             } | 
|---|
| 754 |             // if (tempUse > maxUse) | 
|---|
| 755 |             // { | 
|---|
| 756 |             //     Point p = { Action::FLY, "", (*it)->getWorldPosition(), false }; | 
|---|
| 757 |             //     maxUse = tempUse; | 
|---|
| 758 |             //     maxPoint = p; | 
|---|
| 759 |             // } | 
|---|
| 760 |             // else | 
|---|
| 761 |             // { | 
|---|
| 762 |             //     tempUse = -1; | 
|---|
| 763 |             //     continue; | 
|---|
| 764 |             // } | 
|---|
| 765 |             // orxout(internal_error) << "resp time  = " << static_cast<PickupSpawner*>(*it)->getRespawnTime() << endl; | 
|---|
| 766 |         }   | 
|---|
| 767 |     } | 
|---|
| 768 | } | 
|---|