Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/dynamicmatch/src/orxonox/controllers/ArtificialController.cc @ 6686

Last change on this file since 6686 was 6686, checked in by jo, 14 years ago

first version- not playable yet

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