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