Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/turretFS14/src/orxonox/controllers/WaypointPatrolController.cc @ 10060

Last change on this file since 10060 was 9716, checked in by jo, 11 years ago

Making the WaypointPatrolController configurable such that it can follow/stalk/hunt an enemy without shooting at the first place.

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
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
31#include "util/Math.h"
32#include "core/CoreIncludes.h"
33#include "core/XMLPort.h"
34#include "worldentities/pawns/Pawn.h"
35
36namespace orxonox
37{
38    RegisterClass(WaypointPatrolController);
39
40    WaypointPatrolController::WaypointPatrolController(Context* context) : WaypointController(context)
41    {
42        RegisterObject(WaypointPatrolController);
43
44        this->alertnessradius_ = 500.0f;
45        this->attackradius_ = 1000.0f;
46
47        this->patrolTimer_.setTimer(rnd(), true, createExecutor(createFunctor(&WaypointPatrolController::searchEnemy, this)));
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, "attackradius", setAttackRadius, getAttackRadius, xmlelement, mode).defaultValues(1000.0f);
56    }
57
58    void WaypointPatrolController::tick(float dt)
59    {
60        if (!this->isActive())
61            return;
62
63        if (this->target_) //if there is a target, follow it and shoot it, if it is close enough
64        {
65            this->aimAtTarget();
66
67            if (this->bHasTargetPosition_)
68                this->moveToTargetPosition();
69
70            if (this->getControllableEntity() && this->isCloseAtTarget(this->attackradius_) && this->isLookingAtTarget(math::pi / 20.0f))
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();
87        float shortestsqdistance = (float)static_cast<unsigned int>(-1);
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}
Note: See TracBrowser for help on using the repository browser.