Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6695 was 6695, checked in by solex, 15 years ago

master now lists slaves

  • Property svn:eol-style set to native
File size: 12.5 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"
[6601]32#include "core/XMLPort.h"
[5735]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"
[3049]38
[2362]39namespace orxonox
40{
41    ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator)
42    {
43        RegisterObject(ArtificialController);
44
45        this->target_ = 0;
[6695]46        this->myMaster_ = 0;
47        //this->slaveNumber_ = -1;
[6640]48        this->team_ = -1;//new
[6683]49        this->state_ = FREE;//new
[2362]50        this->bShooting_ = false;
51        this->bHasTargetPosition_ = false;
52        this->targetPosition_ = Vector3::ZERO;
[6417]53
[5929]54        this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this));
[2362]55    }
56
57    ArtificialController::~ArtificialController()
58    {
59    }
60
[6601]61    void ArtificialController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
62    {
63        SUPER(ArtificialController, XMLPort, xmlelement, mode);
64
65        XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(0);
66    }
67
[3049]68    void ArtificialController::moveToPosition(const Vector3& target)
[2362]69    {
70        if (!this->getControllableEntity())
71            return;
72
[3049]73        Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
74        float distance = (target - this->getControllableEntity()->getPosition()).length();
[2362]75
[2493]76        if (this->target_ || distance > 10)
[2362]77        {
78            // Multiply with 0.8 to make them a bit slower
[6502]79            this->getControllableEntity()->rotateYaw(-0.8f * sgn(coord.x) * coord.x*coord.x);
80            this->getControllableEntity()->rotatePitch(0.8f * sgn(coord.y) * coord.y*coord.y);
[2362]81        }
82
[2493]83        if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
[6502]84            this->getControllableEntity()->moveFrontBack(-0.5f); // They don't brake with full power to give the player a chance
[2362]85        else
[6502]86            this->getControllableEntity()->moveFrontBack(0.8f);
[2362]87    }
88
[3049]89    void ArtificialController::moveToTargetPosition()
90    {
91        this->moveToPosition(this->targetPosition_);
92    }
93
[6640]94    int ArtificialController::getState()
95    {
96        return this->state_;
97    }
98
[6695]99    void ArtificialController::unregisterSlave() {
100        if(myMaster_)
101        {
102            myMaster_->slaves.remove(this);
103        }
104    }
105
[6640]106    void ArtificialController::searchNewMaster()
107    {
[6695]108
[6640]109        if (!this->getControllableEntity())
110            return;
111
112        this->targetPosition_ = this->getControllableEntity()->getPosition();
113        this->forgetTarget();
114
115        //go through all pawns
116        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
117        {
[6695]118
119            //same team?
[6640]120            if (!ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
121                continue;
122
[6695]123            //has it an ArtificialController and is it a master?
124            if (!it->getController())
125                continue;
[6640]126
127            ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
[6695]128            if (!controller || controller->getState() != MASTER)
[6640]129                continue;
130
131            //is pawn oneself? && is pawn in range?
[6695]132            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) //&& it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) < 1000
[6640]133            {
[6683]134                this->state_ = SLAVE;
[6695]135                this->myMaster_ = controller;
136                controller->slaves.push_back(this);
137                break;
[6640]138            }
139        }//for
140
141        //hasn't encountered any masters in range? -> become a master
[6695]142        if (state_!=SLAVE) state_ = MASTER; // keep in mind: what happens when two masters encounter eache other? -> has to be evaluated in the for loop within master mode in AIcontroller...
143
[6640]144    }
145
[6683]146    void ArtificialController::commandSlaves() {
147
[6695]148        for(std::list<ArtificialController*>::iterator it = slaves.begin(); it != slaves.end(); it++)
149        {
150            (*it)->setTargetPosition(this->getControllableEntity()->getPosition());
151        }
152/*
[6683]153        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
154        {
[6695]155
156            if (!it->getController())
157                continue;
158
[6683]159            ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
[6695]160            if (!controller || controller->getState() != SLAVE)
[6683]161                continue;
[6695]162            //controller->setTargetPosition(this->getControllableEntity()->getPosition());
[6683]163            controller->targetPosition_ = this->getControllableEntity()->getPosition();
[6695]164            controller->bHasTargetPosition_ = true;
[6683]165        }
[6695]166*/
[6683]167    }
168
169    void ArtificialController::freeAllSlaves()
170    {
[6695]171
[6683]172        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
173        {
174            ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
[6695]175            if (controller && controller->getState() != SLAVE)
[6683]176                continue;
177
178            controller->state_ = FREE;
179        }
180
181    }
182
183
[3049]184    void ArtificialController::setTargetPosition(const Vector3& target)
185    {
186        this->targetPosition_ = target;
187        this->bHasTargetPosition_ = true;
188    }
189
[2362]190    void ArtificialController::searchRandomTargetPosition()
191    {
[2493]192        this->targetPosition_ = Vector3(rnd(-2000,2000), rnd(-2000,2000), rnd(-2000,2000));
[2362]193        this->bHasTargetPosition_ = true;
194    }
195
[3049]196    void ArtificialController::setTarget(Pawn* target)
197    {
198        this->target_ = target;
199
200        if (target)
201            this->targetPosition_ = target->getPosition();
202    }
203
[2362]204    void ArtificialController::searchNewTarget()
205    {
206        if (!this->getControllableEntity())
207            return;
208
209        this->targetPosition_ = this->getControllableEntity()->getPosition();
210        this->forgetTarget();
211
212        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
213        {
[3049]214            if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
215                continue;
216
[2506]217            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity())
[2362]218            {
219                float speed = this->getControllableEntity()->getVelocity().length();
220                Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition();
221                Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition();
222                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))
223                        < 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))
224                {
225                    this->target_ = (*it);
226                    this->targetPosition_ = it->getPosition();
227                }
228            }
229        }
230    }
231
232    void ArtificialController::forgetTarget()
233    {
234        this->target_ = 0;
235        this->bShooting_ = false;
236    }
237
238    void ArtificialController::aimAtTarget()
239    {
240        if (!this->target_ || !this->getControllableEntity())
241            return;
242
[2493]243        static const float hardcoded_projectile_speed = 1250;
[2362]244
[2493]245        this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getPosition(), hardcoded_projectile_speed, this->target_->getPosition(), this->target_->getVelocity());
[2362]246        this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO);
[6417]247
248        Pawn* pawn = dynamic_cast<Pawn*>(this->getControllableEntity());
249        if (pawn)
250            pawn->setAimPosition(this->targetPosition_);
[2362]251    }
252
253    bool ArtificialController::isCloseAtTarget(float distance) const
254    {
255        if (!this->getControllableEntity())
256            return false;
257
258        if (!this->target_)
259            return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) < distance*distance);
260        else
261            return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance);
262    }
263
264    bool ArtificialController::isLookingAtTarget(float angle) const
265    {
266        if (!this->getControllableEntity())
267            return false;
268
269        return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle);
270    }
271
[5929]272    void ArtificialController::abandonTarget(Pawn* target)
[2362]273    {
[5929]274        if (target == this->target_)
275            this->targetDied();
[2362]276    }
[3049]277
[5929]278    void ArtificialController::targetDied()
279    {
280        this->forgetTarget();
281        this->searchRandomTargetPosition();
282    }
283
[3049]284    bool ArtificialController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype)
285    {
286        if (entity1 == entity2)
287            return true;
288
289        int team1 = -1;
290        int team2 = -1;
291
[6601]292        Controller* controller = 0;
293        if (entity1->getController())
294            controller = entity1->getController();
295        else
296            controller = entity1->getXMLController();
297        if (controller)
[3049]298        {
[6601]299            ArtificialController* ac = orxonox_cast<ArtificialController*>(controller);
300            if (ac)
301                team1 = ac->getTeam();
[3049]302        }
[6601]303
304        if (entity1->getController())
305            controller = entity1->getController();
306        else
307            controller = entity1->getXMLController();
308        if (controller)
[3049]309        {
[6601]310            ArtificialController* ac = orxonox_cast<ArtificialController*>(controller);
311            if (ac)
312                team2 = ac->getTeam();
[3049]313        }
314
[3325]315        TeamDeathmatch* tdm = orxonox_cast<TeamDeathmatch*>(gametype);
[3049]316        if (tdm)
317        {
318            if (entity1->getPlayer())
319                team1 = tdm->getTeam(entity1->getPlayer());
320
321            if (entity2->getPlayer())
322                team2 = tdm->getTeam(entity2->getPlayer());
323        }
324
[3086]325        TeamBaseMatchBase* base = 0;
[3325]326        base = orxonox_cast<TeamBaseMatchBase*>(entity1);
[3086]327        if (base)
328        {
329            switch (base->getState())
330            {
[3280]331                case BaseState::ControlTeam1:
[3086]332                    team1 = 0;
333                    break;
[3280]334                case BaseState::ControlTeam2:
[3086]335                    team1 = 1;
336                    break;
[3280]337                case BaseState::Uncontrolled:
[3086]338                default:
339                    team1 = -1;
340            }
341        }
[3325]342        base = orxonox_cast<TeamBaseMatchBase*>(entity2);
[3086]343        if (base)
344        {
345            switch (base->getState())
346            {
[3280]347                case BaseState::ControlTeam1:
[3086]348                    team2 = 0;
349                    break;
[3280]350                case BaseState::ControlTeam2:
[3086]351                    team2 = 1;
352                    break;
[3280]353                case BaseState::Uncontrolled:
[3086]354                default:
355                    team2 = -1;
356            }
357        }
358
[3049]359        return (team1 == team2 && team1 != -1);
360    }
[2362]361}
Note: See TracBrowser for help on using the repository browser.