Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6918 was 6918, checked in by gasserlu, 15 years ago

drone follows in realistic matter

  • Property svn:eol-style set to native
File size: 17.6 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"
[6918]38#include "controllers/DroneController.h"
[6888]39#include "util/Math.h"
[3049]40
[2362]41namespace orxonox
42{
[6888]43
44    static const unsigned int MAX_FORMATION_SIZE = 6;
45    static const int FREEDOM_COUNT = 4; //seconds the slaves in a formation will be set free when master attacks an enemy
46    static const float SPEED_MASTER = 0.6f;
47    static const float ROTATEFACTOR_MASTER = 0.2f;
48    static const float SPEED_FREE = 0.8f;
49    static const float ROTATEFACTOR_FREE = 0.8f;
50
[2362]51    ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator)
52    {
53        RegisterObject(ArtificialController);
54
55        this->target_ = 0;
[6695]56        this->myMaster_ = 0;
[6850]57        this->freedomCount_ = 0;
58        this->team_ = -1;
59        this->state_ = FREE;
[2362]60        this->bShooting_ = false;
61        this->bHasTargetPosition_ = false;
62        this->targetPosition_ = Vector3::ZERO;
[6417]63
[5929]64        this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this));
[2362]65    }
66
67    ArtificialController::~ArtificialController()
68    {
69    }
70
[6601]71    void ArtificialController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
72    {
73        SUPER(ArtificialController, XMLPort, xmlelement, mode);
74
75        XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(0);
76    }
77
[6850]78// gets called when Bot dies
79    void ArtificialController::changedControllableEntity()
80    {
81        if(!getControllableEntity()) 
82        {
83        if (this->state_ == SLAVE) unregisterSlave();
84         if (this->state_ == MASTER) setNewMasterWithinFormation();
85        this->slaves_.clear();
86        this->state_ = FREE;
[6888]87
[6850]88        }
89    }
90
[3049]91    void ArtificialController::moveToPosition(const Vector3& target)
[2362]92    {
93        if (!this->getControllableEntity())
94            return;
95
[3049]96        Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
97        float distance = (target - this->getControllableEntity()->getPosition()).length();
[2362]98
[6888]99
100        if(this->state_ == FREE)
[2362]101        {
[6888]102            if (this->target_ || distance > 10)
103            {
104                // Multiply with 0.8 to make them a bit slower
105                this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_FREE * sgn(coord.x) * coord.x*coord.x);
106                this->getControllableEntity()->rotatePitch(ROTATEFACTOR_FREE * sgn(coord.y) * coord.y*coord.y);
107            }
[6850]108
[6888]109            if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
110            {
111              this->getControllableEntity()->moveFrontBack(-0.05f); // They don't brake with full power to give the player a chance
112            } else this->getControllableEntity()->moveFrontBack(SPEED_FREE);
113        }
[6850]114
115
116
[6888]117        if(this->state_ == MASTER)
118        {
119            if (this->target_ || distance > 10)
120            {
121                this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_MASTER * sgn(coord.x) * coord.x*coord.x);
122                this->getControllableEntity()->rotatePitch(ROTATEFACTOR_MASTER * sgn(coord.y) * coord.y*coord.y);
123
124            }
125
126            if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
127            {
128                this->getControllableEntity()->moveFrontBack(-0.05f);
129            } else this->getControllableEntity()->moveFrontBack(SPEED_MASTER);
[2362]130        }
131
[6888]132
133
134        if(this->state_ == SLAVE)
[6850]135        {
[6888]136//             if (this->target_ || distance > 10)
137//             {
138                float rotateFactor;
139                if(this->state_ == SLAVE) rotateFactor = 1.0f;
140
141
142                this->getControllableEntity()->rotateYaw(-1.0f * rotateFactor * sgn(coord.x) * coord.x*coord.x);
143                this->getControllableEntity()->rotatePitch(rotateFactor * sgn(coord.y) * coord.y*coord.y);
144
145
146
147//             }
148
149            if (this->target_ && distance < 500 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
150            {
151                if (this->target_ && distance < 60)
152                {
153                    this->getControllableEntity()->setVelocity(0.8f*this->target_->getVelocity());
154                } else this->getControllableEntity()->moveFrontBack(-1.0f*exp(distance/100.0));
155
156            } else {
157                this->getControllableEntity()->moveFrontBack(1.0f + distance/500.0f);
158            }
[6850]159        }
[2362]160    }
161
[3049]162    void ArtificialController::moveToTargetPosition()
163    {
164        this->moveToPosition(this->targetPosition_);
165    }
166
[6640]167    int ArtificialController::getState()
168    {
169        return this->state_;
170    }
171
[6695]172    void ArtificialController::unregisterSlave() {
173        if(myMaster_)
174        {
[6888]175            std::vector<ArtificialController*>::iterator it = std::find(myMaster_->slaves_.begin(), myMaster_->slaves_.end(), this);
176            if( it != myMaster_->slaves_.end() )
177                myMaster_->slaves_.erase(it);
178//COUT(0) << "~unregister slave" << std::endl;
[6695]179        }
180    }
181
[6640]182    void ArtificialController::searchNewMaster()
183    {
[6695]184
[6640]185        if (!this->getControllableEntity())
186            return;
187
188        this->targetPosition_ = this->getControllableEntity()->getPosition();
189        this->forgetTarget();
190
191        //go through all pawns
192        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
193        {
[6695]194
195            //same team?
[6640]196            if (!ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
197                continue;
198
[6695]199            //has it an ArtificialController and is it a master?
200            if (!it->getController())
201                continue;
[6640]202
[6850]203            ArtificialController *newMaster = static_cast<ArtificialController*>(it->getController());
204
205            if (!newMaster || newMaster->getState() != MASTER)
[6640]206                continue;
207
[6888]208            float distance = (it->getPosition() - this->getControllableEntity()->getPosition()).length();
209
[6640]210            //is pawn oneself? && is pawn in range?
[6888]211            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity() && distance < 5000) 
[6640]212            {
[6888]213                if(newMaster->slaves_.size() > MAX_FORMATION_SIZE) continue;
[6696]214
[6888]215                for(std::vector<ArtificialController*>::iterator itSlave = this->slaves_.begin(); itSlave != this->slaves_.end(); itSlave++)
[6850]216                {
217                    (*itSlave)->myMaster_ = newMaster;
218                    newMaster->slaves_.push_back(*itSlave);
219                }
[6795]220                this->slaves_.clear();
[6683]221                this->state_ = SLAVE;
[6696]222
[6850]223                this->myMaster_ = newMaster;
224                newMaster->slaves_.push_back(this);
[6696]225
[6695]226                break;
[6640]227            }
228        }//for
229
230        //hasn't encountered any masters in range? -> become a master
[6888]231        if (state_!=SLAVE) state_ = MASTER;//master encounters master? ->done
[6640]232    }
233
[6683]234    void ArtificialController::commandSlaves() {
235
[6888]236        Quaternion orient = this->getControllableEntity()->getOrientation();
237        Vector3 dest = this->getControllableEntity()->getPosition();
238
239        // 1 slave: follow
240        if (this->slaves_.size() == 1)
[6695]241        {
[6888]242            dest += 4*orient*WorldEntity::BACK;
243            this->slaves_.front()->setTargetPosition(dest);
[6695]244        }
[6888]245
246        // 2 slaves: triangle
247         if (this->slaves_.size() == 2) 
248        {
249            dest += 10*orient*WorldEntity::BACK;
250            this->slaves_[0]->setTargetPosition(dest + 10*orient*WorldEntity::LEFT);
251            this->slaves_[1]->setTargetPosition(dest + 10*orient*WorldEntity::RIGHT);
252        }
253
254        if (this->slaves_.size() > MAX_FORMATION_SIZE)
255        {
256            for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
257            {
258                (*it)->setTargetPosition(this->getControllableEntity()->getPosition());
259            }
260        }
[6683]261    }
262
[6795]263    // binds slaves to new Master within formation
264    void ArtificialController::setNewMasterWithinFormation()
265    {
[6888]266
[6795]267        if (this->slaves_.empty())
268            return;
269
270        ArtificialController *newMaster = this->slaves_.back();
271        this->slaves_.pop_back();
[6888]272
[6795]273        if(!newMaster) return;
274        newMaster->state_ = MASTER;
275        newMaster->slaves_ = this->slaves_;
[6888]276
[6850]277        this->slaves_.clear();
[6795]278        this->state_ = SLAVE;
279        this->myMaster_ = newMaster;
280
[6888]281        for(std::vector<ArtificialController*>::iterator it = newMaster->slaves_.begin(); it != newMaster->slaves_.end(); it++)
[6795]282        {
283            (*it)->myMaster_ = newMaster;
284        }
285
286    }
287
[6850]288    void ArtificialController::freeSlaves()
[6683]289    {
[6888]290        for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
[6696]291        {
292            (*it)->state_ = FREE;
293        }
[6850]294        this->slaves_.clear();
295    }
296
297    void ArtificialController::forceFreeSlaves()
298    {
[6888]299        for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
[6683]300        {
[6850]301            (*it)->state_ = FREE;
302            (*it)->forceFreedom();
303            (*it)->targetPosition_ = this->targetPosition_;
304            (*it)->bShooting_ = true;
[6888]305            (*it)->getControllableEntity()->fire(0);// fire once for fun
[6683]306        }
[6696]307    }
[6683]308
[6696]309    void ArtificialController::loseMasterState()
310    {
[6850]311        this->freeSlaves();
[6696]312        this->state_ = FREE;
[6683]313    }
314
[6850]315    void ArtificialController::forceFreedom()
316    {
[6888]317        this->freedomCount_ = FREEDOM_COUNT;
[6850]318    }
319
320    bool ArtificialController::forcedFree()
321    {
322        if(this->freedomCount_ > 0) 
323        {
324            this->freedomCount_--;
325            return true;
326        } else return false;
327    }
328
[3049]329    void ArtificialController::setTargetPosition(const Vector3& target)
330    {
331        this->targetPosition_ = target;
332        this->bHasTargetPosition_ = true;
333    }
334
[2362]335    void ArtificialController::searchRandomTargetPosition()
336    {
[2493]337        this->targetPosition_ = Vector3(rnd(-2000,2000), rnd(-2000,2000), rnd(-2000,2000));
[2362]338        this->bHasTargetPosition_ = true;
339    }
340
[3049]341    void ArtificialController::setTarget(Pawn* target)
342    {
343        this->target_ = target;
344
345        if (target)
346            this->targetPosition_ = target->getPosition();
347    }
348
[2362]349    void ArtificialController::searchNewTarget()
350    {
[6918]351COUT(0) << "search new target - start" << std::endl;
[2362]352        if (!this->getControllableEntity())
353            return;
354
355        this->targetPosition_ = this->getControllableEntity()->getPosition();
356        this->forgetTarget();
357
358        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
359        {
[3049]360            if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
361                continue;
362
[2506]363            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity())
[2362]364            {
365                float speed = this->getControllableEntity()->getVelocity().length();
366                Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition();
367                Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition();
368                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))
369                        < 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))
370                {
371                    this->target_ = (*it);
372                    this->targetPosition_ = it->getPosition();
373                }
374            }
375        }
[6918]376COUT(0) << "search new target - end: " << this->target_ << std::endl;
[2362]377    }
378
379    void ArtificialController::forgetTarget()
380    {
381        this->target_ = 0;
382        this->bShooting_ = false;
383    }
384
385    void ArtificialController::aimAtTarget()
386    {
387        if (!this->target_ || !this->getControllableEntity())
388            return;
389
[2493]390        static const float hardcoded_projectile_speed = 1250;
[2362]391
[2493]392        this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getPosition(), hardcoded_projectile_speed, this->target_->getPosition(), this->target_->getVelocity());
[2362]393        this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO);
[6417]394
395        Pawn* pawn = dynamic_cast<Pawn*>(this->getControllableEntity());
396        if (pawn)
397            pawn->setAimPosition(this->targetPosition_);
[2362]398    }
399
400    bool ArtificialController::isCloseAtTarget(float distance) const
401    {
402        if (!this->getControllableEntity())
403            return false;
404
405        if (!this->target_)
406            return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) < distance*distance);
407        else
408            return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance);
409    }
410
411    bool ArtificialController::isLookingAtTarget(float angle) const
412    {
413        if (!this->getControllableEntity())
414            return false;
415
416        return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle);
417    }
418
[5929]419    void ArtificialController::abandonTarget(Pawn* target)
[2362]420    {
[5929]421        if (target == this->target_)
422            this->targetDied();
[2362]423    }
[3049]424
[5929]425    void ArtificialController::targetDied()
426    {
427        this->forgetTarget();
428        this->searchRandomTargetPosition();
429    }
430
[3049]431    bool ArtificialController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype)
432    {
433        if (entity1 == entity2)
434            return true;
435
436        int team1 = -1;
437        int team2 = -1;
438
[6601]439        Controller* controller = 0;
440        if (entity1->getController())
441            controller = entity1->getController();
442        else
443            controller = entity1->getXMLController();
444        if (controller)
[3049]445        {
[6601]446            ArtificialController* ac = orxonox_cast<ArtificialController*>(controller);
447            if (ac)
448                team1 = ac->getTeam();
[3049]449        }
[6601]450
451        if (entity1->getController())
452            controller = entity1->getController();
453        else
454            controller = entity1->getXMLController();
455        if (controller)
[3049]456        {
[6601]457            ArtificialController* ac = orxonox_cast<ArtificialController*>(controller);
458            if (ac)
459                team2 = ac->getTeam();
[3049]460        }
461
[3325]462        TeamDeathmatch* tdm = orxonox_cast<TeamDeathmatch*>(gametype);
[3049]463        if (tdm)
464        {
465            if (entity1->getPlayer())
466                team1 = tdm->getTeam(entity1->getPlayer());
467
468            if (entity2->getPlayer())
469                team2 = tdm->getTeam(entity2->getPlayer());
470        }
471
[3086]472        TeamBaseMatchBase* base = 0;
[3325]473        base = orxonox_cast<TeamBaseMatchBase*>(entity1);
[3086]474        if (base)
475        {
476            switch (base->getState())
477            {
[3280]478                case BaseState::ControlTeam1:
[3086]479                    team1 = 0;
480                    break;
[3280]481                case BaseState::ControlTeam2:
[3086]482                    team1 = 1;
483                    break;
[3280]484                case BaseState::Uncontrolled:
[3086]485                default:
486                    team1 = -1;
487            }
488        }
[3325]489        base = orxonox_cast<TeamBaseMatchBase*>(entity2);
[3086]490        if (base)
491        {
492            switch (base->getState())
493            {
[3280]494                case BaseState::ControlTeam1:
[3086]495                    team2 = 0;
496                    break;
[3280]497                case BaseState::ControlTeam2:
[3086]498                    team2 = 1;
499                    break;
[3280]500                case BaseState::Uncontrolled:
[3086]501                default:
502                    team2 = -1;
503            }
504        }
505
[6891]506        DroneController* droneController = 0;
507        droneController = orxonox_cast<DroneController*>(entity1->getController());
508        if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity2)
509            return true;
510        droneController = orxonox_cast<DroneController*>(entity2->getController());
511        if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity1)
512            return true;
513           
[3049]514        return (team1 == team2 && team1 != -1);
515    }
[2362]516}
Note: See TracBrowser for help on using the repository browser.