Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp6/src/orxonox/objects/controllers/ArtificialController.cc @ 3530

Last change on this file since 3530 was 3196, checked in by rgrieder, 16 years ago

Merged pch branch back to trunk.

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