Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/objects/controllers/ArtificialController.cc @ 2664

Last change on this file since 2664 was 2362, checked in by landauf, 16 years ago
  • Added Bot (the artifical counterpart to HumanPlayer)
  • Added ArtificialController, the baseclass of all non-human-controllers
  • Added AIController, a simple reimplementation of the old AISpaceShip (but of course without the SpaceShip, just the AI)
  • Added currently empty class ScriptController
  • Some tweaks in PlayerInfo, ControllableEntity and Controller
  • Made Spacetator speed configurable

I don't know where, but there's still an issue in the new code, maybe even caused by the previous commit. The Server seems to crash when a Client disconnects, but only if the Server runs within the debugger.

  • Property svn:eol-style set to native
File size: 5.8 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
59        float distance = (this->targetPosition_ - this->getControllableEntity()->getPosition()).length();
60        if (this->target_ || distance > 50)
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 < 1000 && 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(-5000,5000), rnd(-5000,5000), rnd(-5000,5000));
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            {
91                float speed = this->getControllableEntity()->getVelocity().length();
92                Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition();
93                Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition();
94                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))
95                        < 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))
96                {
97                    this->target_ = (*it);
98                    this->targetPosition_ = it->getPosition();
99                }
100            }
101        }
102    }
103
104    void ArtificialController::forgetTarget()
105    {
106        this->target_ = 0;
107        this->bShooting_ = false;
108    }
109
110    void ArtificialController::aimAtTarget()
111    {
112        if (!this->target_ || !this->getControllableEntity())
113            return;
114
115        static const float hardcoded_projectile_speed = 500;
116
117        this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getPosition(), hardcoded_projectile_speed, this->target_->getPosition(), this->target_->getOrientation() * this->target_->getVelocity());
118        this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO);
119    }
120
121    bool ArtificialController::isCloseAtTarget(float distance) const
122    {
123        if (!this->getControllableEntity())
124            return false;
125
126        if (!this->target_)
127            return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) < distance*distance);
128        else
129            return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance);
130    }
131
132    bool ArtificialController::isLookingAtTarget(float angle) const
133    {
134        if (!this->getControllableEntity())
135            return false;
136
137        return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle);
138    }
139
140    void ArtificialController::shipDied(Pawn* ship)
141    {
142        if (ship == this->target_)
143        {
144            this->forgetTarget();
145            this->searchRandomTargetPosition();
146        }
147    }
148}
Note: See TracBrowser for help on using the repository browser.