[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" |
---|
[5735] | 34 | #include "worldentities/pawns/Pawn.h" |
---|
[3078] | 35 | |
---|
| 36 | namespace orxonox |
---|
| 37 | { |
---|
[9667] | 38 | RegisterClass(WaypointPatrolController); |
---|
[3078] | 39 | |
---|
[9667] | 40 | WaypointPatrolController::WaypointPatrolController(Context* context) : WaypointController(context) |
---|
[3078] | 41 | { |
---|
| 42 | RegisterObject(WaypointPatrolController); |
---|
| 43 | |
---|
[9716] | 44 | this->alertnessradius_ = 500.0f; |
---|
| 45 | this->attackradius_ = 1000.0f; |
---|
[3078] | 46 | |
---|
[5929] | 47 | this->patrolTimer_.setTimer(rnd(), true, createExecutor(createFunctor(&WaypointPatrolController::searchEnemy, this))); |
---|
[3078] | 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); |
---|
[9716] | 55 | XMLPortParam(WaypointPatrolController, "attackradius", setAttackRadius, getAttackRadius, xmlelement, mode).defaultValues(1000.0f); |
---|
[3078] | 56 | } |
---|
| 57 | |
---|
| 58 | void WaypointPatrolController::tick(float dt) |
---|
| 59 | { |
---|
| 60 | if (!this->isActive()) |
---|
| 61 | return; |
---|
| 62 | |
---|
[9716] | 63 | if (this->target_) //if there is a target, follow it and shoot it, if it is close enough |
---|
[3078] | 64 | { |
---|
| 65 | this->aimAtTarget(); |
---|
| 66 | |
---|
| 67 | if (this->bHasTargetPosition_) |
---|
| 68 | this->moveToTargetPosition(); |
---|
| 69 | |
---|
[9716] | 70 | if (this->getControllableEntity() && this->isCloseAtTarget(this->attackradius_) && this->isLookingAtTarget(math::pi / 20.0f)) |
---|
[11175] | 71 | this->getControllableEntity()->push(0); |
---|
[3078] | 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(); |
---|
[6502] | 87 | float shortestsqdistance = (float)static_cast<unsigned int>(-1); |
---|
[3078] | 88 | |
---|
[11071] | 89 | for (Pawn* pawn : ObjectList<Pawn>()) |
---|
[3078] | 90 | { |
---|
[11071] | 91 | if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(pawn), this->getGametype())) |
---|
[3078] | 92 | continue; |
---|
| 93 | |
---|
[11071] | 94 | float sqdistance = pawn->getPosition().squaredDistance(myposition); |
---|
[3078] | 95 | if (sqdistance < shortestsqdistance) |
---|
| 96 | { |
---|
| 97 | shortestsqdistance = sqdistance; |
---|
[11071] | 98 | this->target_ = pawn; |
---|
[3078] | 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | if (shortestsqdistance > (this->alertnessradius_ * this->alertnessradius_)) |
---|
[11071] | 103 | this->target_ = nullptr; |
---|
[3078] | 104 | } |
---|
| 105 | } |
---|