Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ai/src/orxonox/controllers/ArtificialController.cc @ 6601

Last change on this file since 6601 was 6601, checked in by solex, 14 years ago

ArtificialController: sameTeam hacking

  • Property svn:eol-style set to native
File size: 9.4 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 "ArtificialController.h"
30
31#include "core/CoreIncludes.h"
32#include "core/XMLPort.h"
33#include "worldentities/ControllableEntity.h"
34#include "worldentities/pawns/Pawn.h"
35#include "worldentities/pawns/TeamBaseMatchBase.h"
36#include "gametypes/TeamDeathmatch.h"
37#include "controllers/WaypointPatrolController.h"
38
39namespace orxonox
40{
41    ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator)
42    {
43        RegisterObject(ArtificialController);
44
45        this->target_ = 0;
46        this->team_ = 0;//new
47        this->isMaster_ = false;//new
48        this->bShooting_ = false;
49        this->bHasTargetPosition_ = false;
50        this->targetPosition_ = Vector3::ZERO;
51
52        this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this));
53    }
54
55    ArtificialController::~ArtificialController()
56    {
57    }
58
59    void ArtificialController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
60    {
61        SUPER(ArtificialController, XMLPort, xmlelement, mode);
62
63        XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(0);
64    }
65
66    void ArtificialController::moveToPosition(const Vector3& target)
67    {
68        if (!this->getControllableEntity())
69            return;
70
71        Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
72        float distance = (target - this->getControllableEntity()->getPosition()).length();
73
74        if (this->target_ || distance > 10)
75        {
76            // Multiply with 0.8 to make them a bit slower
77            this->getControllableEntity()->rotateYaw(-0.8f * sgn(coord.x) * coord.x*coord.x);
78            this->getControllableEntity()->rotatePitch(0.8f * sgn(coord.y) * coord.y*coord.y);
79        }
80
81        if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
82            this->getControllableEntity()->moveFrontBack(-0.5f); // They don't brake with full power to give the player a chance
83        else
84            this->getControllableEntity()->moveFrontBack(0.8f);
85    }
86
87    void ArtificialController::moveToTargetPosition()
88    {
89        this->moveToPosition(this->targetPosition_);
90    }
91
92    void ArtificialController::setTargetPosition(const Vector3& target)
93    {
94        this->targetPosition_ = target;
95        this->bHasTargetPosition_ = true;
96    }
97
98    void ArtificialController::searchRandomTargetPosition()
99    {
100        this->targetPosition_ = Vector3(rnd(-2000,2000), rnd(-2000,2000), rnd(-2000,2000));
101        this->bHasTargetPosition_ = true;
102    }
103
104    void ArtificialController::setTarget(Pawn* target)
105    {
106        this->target_ = target;
107
108        if (target)
109            this->targetPosition_ = target->getPosition();
110    }
111
112    void ArtificialController::searchNewTarget()
113    {
114        if (!this->getControllableEntity())
115            return;
116
117        this->targetPosition_ = this->getControllableEntity()->getPosition();
118        this->forgetTarget();
119
120        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
121        {
122            if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
123                continue;
124
125            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity())
126            {
127                float speed = this->getControllableEntity()->getVelocity().length();
128                Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition();
129                Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition();
130                if (!this->target_ || it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceNew) / speed / distanceNew.length()) / (2 * Ogre::Math::PI))
131                        < this->targetPosition_.squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceCurrent) / speed / distanceCurrent.length()) / (2 * Ogre::Math::PI)) + rnd(-250, 250))
132                {
133                    this->target_ = (*it);
134                    this->targetPosition_ = it->getPosition();
135                }
136            }
137        }
138    }
139
140    void ArtificialController::forgetTarget()
141    {
142        this->target_ = 0;
143        this->bShooting_ = false;
144    }
145
146    void ArtificialController::aimAtTarget()
147    {
148        if (!this->target_ || !this->getControllableEntity())
149            return;
150
151        static const float hardcoded_projectile_speed = 1250;
152
153        this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getPosition(), hardcoded_projectile_speed, this->target_->getPosition(), this->target_->getVelocity());
154        this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO);
155
156        Pawn* pawn = dynamic_cast<Pawn*>(this->getControllableEntity());
157        if (pawn)
158            pawn->setAimPosition(this->targetPosition_);
159    }
160
161    bool ArtificialController::isCloseAtTarget(float distance) const
162    {
163        if (!this->getControllableEntity())
164            return false;
165
166        if (!this->target_)
167            return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) < distance*distance);
168        else
169            return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance);
170    }
171
172    bool ArtificialController::isLookingAtTarget(float angle) const
173    {
174        if (!this->getControllableEntity())
175            return false;
176
177        return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle);
178    }
179
180    void ArtificialController::abandonTarget(Pawn* target)
181    {
182        if (target == this->target_)
183            this->targetDied();
184    }
185
186    void ArtificialController::targetDied()
187    {
188        this->forgetTarget();
189        this->searchRandomTargetPosition();
190    }
191
192    bool ArtificialController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype)
193    {
194        if (entity1 == entity2)
195            return true;
196
197        int team1 = -1;
198        int team2 = -1;
199
200        Controller* controller = 0;
201        if (entity1->getController())
202            controller = entity1->getController();
203        else
204            controller = entity1->getXMLController();
205        if (controller)
206        {
207            ArtificialController* ac = orxonox_cast<ArtificialController*>(controller);
208            if (ac)
209                team1 = ac->getTeam();
210        }
211
212        if (entity1->getController())
213            controller = entity1->getController();
214        else
215            controller = entity1->getXMLController();
216        if (controller)
217        {
218            ArtificialController* ac = orxonox_cast<ArtificialController*>(controller);
219            if (ac)
220                team2 = ac->getTeam();
221        }
222
223        TeamDeathmatch* tdm = orxonox_cast<TeamDeathmatch*>(gametype);
224        if (tdm)
225        {
226            if (entity1->getPlayer())
227                team1 = tdm->getTeam(entity1->getPlayer());
228
229            if (entity2->getPlayer())
230                team2 = tdm->getTeam(entity2->getPlayer());
231        }
232
233        TeamBaseMatchBase* base = 0;
234        base = orxonox_cast<TeamBaseMatchBase*>(entity1);
235        if (base)
236        {
237            switch (base->getState())
238            {
239                case BaseState::ControlTeam1:
240                    team1 = 0;
241                    break;
242                case BaseState::ControlTeam2:
243                    team1 = 1;
244                    break;
245                case BaseState::Uncontrolled:
246                default:
247                    team1 = -1;
248            }
249        }
250        base = orxonox_cast<TeamBaseMatchBase*>(entity2);
251        if (base)
252        {
253            switch (base->getState())
254            {
255                case BaseState::ControlTeam1:
256                    team2 = 0;
257                    break;
258                case BaseState::ControlTeam2:
259                    team2 = 1;
260                    break;
261                case BaseState::Uncontrolled:
262                default:
263                    team2 = -1;
264            }
265        }
266
267        return (team1 == team2 && team1 != -1);
268    }
269}
Note: See TracBrowser for help on using the repository browser.