Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ggz/src/orxonox/objects/controllers/ArtificialController.cc @ 4317

Last change on this file since 4317 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

  • Property svn:eol-style set to native
File size: 5.9 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 "OrxonoxStableHeaders.h"
30#include "ArtificialController.h"
31
32#include "core/CoreIncludes.h"
33#include "objects/worldentities/ControllableEntity.h"
34#include "objects/worldentities/pawns/Pawn.h"
35
36namespace orxonox
37{
38    ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator)
39    {
40        RegisterObject(ArtificialController);
41
42        this->target_ = 0;
43        this->bShooting_ = false;
44        this->bHasTargetPosition_ = false;
45        this->targetPosition_ = Vector3::ZERO;
46    }
47
48    ArtificialController::~ArtificialController()
49    {
50    }
51
52    void ArtificialController::moveToTargetPosition(float dt)
53    {
54        if (!this->getControllableEntity())
55            return;
56
57        Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->targetPosition_);
58        float distance = (this->targetPosition_ - this->getControllableEntity()->getPosition()).length();
59
60        if (this->target_ || distance > 10)
61        {
62            // Multiply with 0.8 to make them a bit slower
63            this->getControllableEntity()->rotateYaw(-0.8 * sgn(coord.x) * coord.x*coord.x);
64            this->getControllableEntity()->rotatePitch(0.8 * sgn(coord.y) * coord.y*coord.y);
65        }
66
67        if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
68            this->getControllableEntity()->moveFrontBack(-0.5); // They don't brake with full power to give the player a chance
69        else
70            this->getControllableEntity()->moveFrontBack(0.8);
71    }
72
73    void ArtificialController::searchRandomTargetPosition()
74    {
75        this->targetPosition_ = Vector3(rnd(-2000,2000), rnd(-2000,2000), rnd(-2000,2000));
76        this->bHasTargetPosition_ = true;
77    }
78
79    void ArtificialController::searchNewTarget()
80    {
81        if (!this->getControllableEntity())
82            return;
83
84        this->targetPosition_ = this->getControllableEntity()->getPosition();
85        this->forgetTarget();
86
87        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
88        {
89//            if (it->getTeamNr() != this->getTeamNr())
90            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity())
91            {
92                float speed = this->getControllableEntity()->getVelocity().length();
93                Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition();
94                Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition();
95                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))
96                        < 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))
97                {
98                    this->target_ = (*it);
99                    this->targetPosition_ = it->getPosition();
100                }
101            }
102        }
103    }
104
105    void ArtificialController::forgetTarget()
106    {
107        this->target_ = 0;
108        this->bShooting_ = false;
109    }
110
111    void ArtificialController::aimAtTarget()
112    {
113        if (!this->target_ || !this->getControllableEntity())
114            return;
115
116        static const float hardcoded_projectile_speed = 1250;
117
118        this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getPosition(), hardcoded_projectile_speed, this->target_->getPosition(), this->target_->getVelocity());
119        this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO);
120    }
121
122    bool ArtificialController::isCloseAtTarget(float distance) const
123    {
124        if (!this->getControllableEntity())
125            return false;
126
127        if (!this->target_)
128            return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) < distance*distance);
129        else
130            return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance);
131    }
132
133    bool ArtificialController::isLookingAtTarget(float angle) const
134    {
135        if (!this->getControllableEntity())
136            return false;
137
138        return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle);
139    }
140
141    void ArtificialController::destroyedPawn(Pawn* ship)
142    {
143        if (ship == this->target_)
144        {
145            this->forgetTarget();
146            this->searchRandomTargetPosition();
147        }
148    }
149}
Note: See TracBrowser for help on using the repository browser.