| [3078] | 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 |  *      ... | 
|---|
 | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
 | 28 |  | 
|---|
 | 29 | #include "WaypointPatrolController.h" | 
|---|
 | 30 |  | 
|---|
| [3196] | 31 | #include "util/Math.h" | 
|---|
| [3078] | 32 | #include "core/CoreIncludes.h" | 
|---|
 | 33 | #include "core/XMLPort.h" | 
|---|
| [3196] | 34 | #include "objects/worldentities/pawns/Pawn.h" | 
|---|
| [3078] | 35 |  | 
|---|
 | 36 | namespace orxonox | 
|---|
 | 37 | { | 
|---|
 | 38 |     CreateFactory(WaypointPatrolController); | 
|---|
 | 39 |  | 
|---|
 | 40 |     WaypointPatrolController::WaypointPatrolController(BaseObject* creator) : WaypointController(creator) | 
|---|
 | 41 |     { | 
|---|
 | 42 |         RegisterObject(WaypointPatrolController); | 
|---|
 | 43 |  | 
|---|
 | 44 |         this->team_ = 0; | 
|---|
 | 45 |         this->alertnessradius_ = 500; | 
|---|
 | 46 |  | 
|---|
 | 47 |         this->patrolTimer_.setTimer(rnd(), true, this, createExecutor(createFunctor(&WaypointPatrolController::searchEnemy))); | 
|---|
 | 48 |     } | 
|---|
 | 49 |  | 
|---|
 | 50 |     void WaypointPatrolController::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
 | 51 |     { | 
|---|
 | 52 |         SUPER(WaypointPatrolController, XMLPort, xmlelement, mode); | 
|---|
 | 53 |  | 
|---|
 | 54 |         XMLPortParam(WaypointPatrolController, "alertnessradius", setAlertnessRadius, getAlertnessRadius, xmlelement, mode).defaultValues(500.0f); | 
|---|
 | 55 |         XMLPortParam(WaypointPatrolController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(0); | 
|---|
 | 56 |     } | 
|---|
 | 57 |  | 
|---|
 | 58 |     void WaypointPatrolController::tick(float dt) | 
|---|
 | 59 |     { | 
|---|
 | 60 |         if (!this->isActive()) | 
|---|
 | 61 |             return; | 
|---|
 | 62 |  | 
|---|
 | 63 |         if (this->target_) | 
|---|
 | 64 |         { | 
|---|
 | 65 |             this->aimAtTarget(); | 
|---|
 | 66 |  | 
|---|
 | 67 |             if (this->bHasTargetPosition_) | 
|---|
 | 68 |                 this->moveToTargetPosition(); | 
|---|
 | 69 |  | 
|---|
 | 70 |             if (this->getControllableEntity() && this->isCloseAtTarget(1000) && this->isLookingAtTarget(Ogre::Math::PI / 20.0)) | 
|---|
 | 71 |                 this->getControllableEntity()->fire(0); | 
|---|
 | 72 |         } | 
|---|
 | 73 |         else | 
|---|
 | 74 |         { | 
|---|
 | 75 |             SUPER(WaypointPatrolController, tick, dt); | 
|---|
 | 76 |         } | 
|---|
 | 77 |     } | 
|---|
 | 78 |  | 
|---|
 | 79 |     void WaypointPatrolController::searchEnemy() | 
|---|
 | 80 |     { | 
|---|
 | 81 |         this->patrolTimer_.setInterval(rnd()); | 
|---|
 | 82 |  | 
|---|
 | 83 |         if (!this->getControllableEntity()) | 
|---|
 | 84 |             return; | 
|---|
 | 85 |  | 
|---|
 | 86 |         Vector3 myposition = this->getControllableEntity()->getPosition(); | 
|---|
| [3301] | 87 |         float shortestsqdistance = static_cast<unsigned int>(-1); | 
|---|
| [3078] | 88 |  | 
|---|
 | 89 |         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it) | 
|---|
 | 90 |         { | 
|---|
 | 91 |             if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype())) | 
|---|
 | 92 |                 continue; | 
|---|
 | 93 |  | 
|---|
 | 94 |             float sqdistance = it->getPosition().squaredDistance(myposition); | 
|---|
 | 95 |             if (sqdistance < shortestsqdistance) | 
|---|
 | 96 |             { | 
|---|
 | 97 |                 shortestsqdistance = sqdistance; | 
|---|
 | 98 |                 this->target_ = (*it); | 
|---|
 | 99 |             } | 
|---|
 | 100 |         } | 
|---|
 | 101 |  | 
|---|
 | 102 |         if (shortestsqdistance > (this->alertnessradius_ * this->alertnessradius_)) | 
|---|
 | 103 |             this->target_ = 0; | 
|---|
 | 104 |     } | 
|---|
 | 105 | } | 
|---|