Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 10, 2010, 4:32:26 PM (15 years ago)
Author:
solex
Message:

formation flight: new formations

Location:
code/branches/ai/src/orxonox/controllers
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/ai/src/orxonox/controllers/AIController.cc

    r6850 r6888  
    127127
    128128            // lose master status (only if less than 4 slaves in formation)
    129             random = rnd(maxrand);
    130             if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 5 )
    131                 this->loseMasterState();
     129//             random = rnd(maxrand);
     130//             if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 )
     131//                 this->loseMasterState();
    132132
    133133            // look out for outher masters if formation is small
     
    167167            {
    168168                this->bShooting_ = true;
    169                 this->forceFreeSlaves();
     169//                 this->forceFreeSlaves();
    170170            }
    171171            // stop shooting
  • code/branches/ai/src/orxonox/controllers/ArtificialController.cc

    r6850 r6888  
    3636#include "gametypes/TeamDeathmatch.h"
    3737#include "controllers/WaypointPatrolController.h"
     38#include "util/Math.h"
    3839
    3940namespace orxonox
    4041{
     42
     43    static const unsigned int MAX_FORMATION_SIZE = 6;
     44    static const int FREEDOM_COUNT = 4; //seconds the slaves in a formation will be set free when master attacks an enemy
     45    static const float SPEED_MASTER = 0.6f;
     46    static const float ROTATEFACTOR_MASTER = 0.2f;
     47    static const float SPEED_FREE = 0.8f;
     48    static const float ROTATEFACTOR_FREE = 0.8f;
     49
    4150    ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator)
    4251    {
     
    6978    void ArtificialController::changedControllableEntity()
    7079    {
    71 COUT(0) << "~changedControllableEntity 0" << std::endl;
    7280        if(!getControllableEntity())
    7381        {
    74 COUT(0) << "~changedControllableEntity 1" << std::endl;
    7582        if (this->state_ == SLAVE) unregisterSlave();
    76 COUT(0) << "~changedControllableEntity 2" << std::endl;
    7783         if (this->state_ == MASTER) setNewMasterWithinFormation();
    78 COUT(0) << "~changedControllableEntity 3" << std::endl;
    7984        this->slaves_.clear();
    80 COUT(0) << "~changedControllableEntity 4" << std::endl;
    8185        this->state_ = FREE;
    82 COUT(0) << "~changedControllableEntity 5" << std::endl;
     86
    8387        }
    8488    }
     
    9296        float distance = (target - this->getControllableEntity()->getPosition()).length();
    9397
    94         if (this->target_ || distance > 10)
    95         {
    96             // Multiply with rotateFactor to make them a bit slower
    97             float rotateFactor;
    98             if(this->state_ == SLAVE) rotateFactor = 1.0f;
    99             if(this->state_ == MASTER) rotateFactor = 0.4f;
    100             else rotateFactor = 0.8f;
    101 
    102             this->getControllableEntity()->rotateYaw(-1.0f * rotateFactor * sgn(coord.x) * coord.x*coord.x);
    103             this->getControllableEntity()->rotatePitch(rotateFactor * sgn(coord.y) * coord.y*coord.y);
    104 
    105 
    106 
    107         }
    108 
    109         if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
    110         {
    111             if(this->state_ == SLAVE) this->getControllableEntity()->moveFrontBack(-0.8f);
    112             else this->getControllableEntity()->moveFrontBack(-0.05f); // They don't brake with full power to give the player a chance
    113         } else {
    114             if(this->state_ == SLAVE) this->getControllableEntity()->moveFrontBack(2.5f);
    115             if(this->state_ == MASTER) this->getControllableEntity()->moveFrontBack(0.4f);
    116             else this->getControllableEntity()->moveFrontBack(0.8f);
     98
     99        if(this->state_ == FREE)
     100        {
     101            if (this->target_ || distance > 10)
     102            {
     103                // Multiply with 0.8 to make them a bit slower
     104                this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_FREE * sgn(coord.x) * coord.x*coord.x);
     105                this->getControllableEntity()->rotatePitch(ROTATEFACTOR_FREE * sgn(coord.y) * coord.y*coord.y);
     106            }
     107
     108            if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
     109            {
     110              this->getControllableEntity()->moveFrontBack(-0.05f); // They don't brake with full power to give the player a chance
     111            } else this->getControllableEntity()->moveFrontBack(SPEED_FREE);
     112        }
     113
     114
     115
     116        if(this->state_ == MASTER)
     117        {
     118            if (this->target_ || distance > 10)
     119            {
     120                this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_MASTER * sgn(coord.x) * coord.x*coord.x);
     121                this->getControllableEntity()->rotatePitch(ROTATEFACTOR_MASTER * sgn(coord.y) * coord.y*coord.y);
     122
     123            }
     124
     125            if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
     126            {
     127                this->getControllableEntity()->moveFrontBack(-0.05f);
     128            } else this->getControllableEntity()->moveFrontBack(SPEED_MASTER);
     129        }
     130
     131
     132
     133        if(this->state_ == SLAVE)
     134        {
     135//             if (this->target_ || distance > 10)
     136//             {
     137                float rotateFactor;
     138                if(this->state_ == SLAVE) rotateFactor = 1.0f;
     139
     140
     141                this->getControllableEntity()->rotateYaw(-1.0f * rotateFactor * sgn(coord.x) * coord.x*coord.x);
     142                this->getControllableEntity()->rotatePitch(rotateFactor * sgn(coord.y) * coord.y*coord.y);
     143
     144
     145
     146//             }
     147
     148            if (this->target_ && distance < 500 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
     149            {
     150                if (this->target_ && distance < 60)
     151                {
     152                    this->getControllableEntity()->setVelocity(0.8f*this->target_->getVelocity());
     153                } else this->getControllableEntity()->moveFrontBack(-1.0f*exp(distance/100.0));
     154
     155            } else {
     156                this->getControllableEntity()->moveFrontBack(1.0f + distance/500.0f);
     157            }
    117158        }
    118159    }
     
    131172        if(myMaster_)
    132173        {
    133             myMaster_->slaves_.remove(this);
    134 COUT(0) << "~unregister slave" << std::endl;
     174            std::vector<ArtificialController*>::iterator it = std::find(myMaster_->slaves_.begin(), myMaster_->slaves_.end(), this);
     175            if( it != myMaster_->slaves_.end() )
     176                myMaster_->slaves_.erase(it);
     177//COUT(0) << "~unregister slave" << std::endl;
    135178        }
    136179    }
     
    162205                continue;
    163206
     207            float distance = (it->getPosition() - this->getControllableEntity()->getPosition()).length();
     208
    164209            //is pawn oneself? && is pawn in range?
    165             if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) //&& it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) < 1000
    166             {
    167                 if(newMaster->slaves_.size() > 6) continue;
    168 
    169                 //this->freeSlaves();
    170 
    171                 for(std::list<ArtificialController*>::iterator itSlave = this->slaves_.begin(); itSlave != this->slaves_.end(); itSlave++)
     210            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity() && distance < 5000)
     211            {
     212                if(newMaster->slaves_.size() > MAX_FORMATION_SIZE) continue;
     213
     214                for(std::vector<ArtificialController*>::iterator itSlave = this->slaves_.begin(); itSlave != this->slaves_.end(); itSlave++)
    172215                {
    173216                    (*itSlave)->myMaster_ = newMaster;
     
    185228
    186229        //hasn't encountered any masters in range? -> become a master
    187         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...
    188 COUT(0) << "~searcheNewMaster" << std::endl;
     230        if (state_!=SLAVE) state_ = MASTER;//master encounters master? ->done
    189231    }
    190232
    191233    void ArtificialController::commandSlaves() {
    192234
    193         for(std::list<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
    194         {
    195             (*it)->setTargetPosition(this->getControllableEntity()->getPosition());
     235        Quaternion orient = this->getControllableEntity()->getOrientation();
     236        Vector3 dest = this->getControllableEntity()->getPosition();
     237
     238        // 1 slave: follow
     239        if (this->slaves_.size() == 1)
     240        {
     241            dest += 4*orient*WorldEntity::BACK;
     242            this->slaves_.front()->setTargetPosition(dest);
     243        }
     244
     245        // 2 slaves: triangle
     246         if (this->slaves_.size() == 2)
     247        {
     248            dest += 10*orient*WorldEntity::BACK;
     249            this->slaves_[0]->setTargetPosition(dest + 10*orient*WorldEntity::LEFT);
     250            this->slaves_[1]->setTargetPosition(dest + 10*orient*WorldEntity::RIGHT);
     251        }
     252
     253        if (this->slaves_.size() > MAX_FORMATION_SIZE)
     254        {
     255            for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
     256            {
     257                (*it)->setTargetPosition(this->getControllableEntity()->getPosition());
     258            }
    196259        }
    197260    }
     
    200263    void ArtificialController::setNewMasterWithinFormation()
    201264    {
    202 COUT(0) << "~setNewMasterWithinFormation 1" << std::endl;
     265
    203266        if (this->slaves_.empty())
    204267            return;
    205 COUT(0) << "~setNewMasterWithinFormation 1b" << std::endl;
    206268
    207269        ArtificialController *newMaster = this->slaves_.back();
    208 COUT(0) << "~setNewMasterWithinFormation 2" << std::endl;
    209270        this->slaves_.pop_back();
    210 COUT(0) << "~setNewMasterWithinFormation 3" << std::endl;
     271
    211272        if(!newMaster) return;
    212 COUT(0) << "~setNewMasterWithinFormation 4" << std::endl;
    213273        newMaster->state_ = MASTER;
    214274        newMaster->slaves_ = this->slaves_;
     275
    215276        this->slaves_.clear();
    216 
    217277        this->state_ = SLAVE;
    218278        this->myMaster_ = newMaster;
    219279
    220 COUT(0) << "~setNewMasterWithinFormation 5" << std::endl;
    221         for(std::list<ArtificialController*>::iterator it = newMaster->slaves_.begin(); it != newMaster->slaves_.end(); it++)
    222         {
    223 COUT(0) << "~setNewMasterWithinFormation 6" << std::endl;
     280        for(std::vector<ArtificialController*>::iterator it = newMaster->slaves_.begin(); it != newMaster->slaves_.end(); it++)
     281        {
    224282            (*it)->myMaster_ = newMaster;
    225283        }
    226 COUT(0) << "~setNewMasterWithinFormation 7" << std::endl;
    227284
    228285    }
     
    230287    void ArtificialController::freeSlaves()
    231288    {
    232         for(std::list<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
     289        for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
    233290        {
    234291            (*it)->state_ = FREE;
     
    239296    void ArtificialController::forceFreeSlaves()
    240297    {
    241         for(std::list<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
     298        for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
    242299        {
    243300            (*it)->state_ = FREE;
     
    245302            (*it)->targetPosition_ = this->targetPosition_;
    246303            (*it)->bShooting_ = true;
    247             (*it)->getControllableEntity()->fire(0);
     304            (*it)->getControllableEntity()->fire(0);// fire once for fun
    248305        }
    249306    }
     
    257314    void ArtificialController::forceFreedom()
    258315    {
    259         this->freedomCount_ = 5;
     316        this->freedomCount_ = FREEDOM_COUNT;
    260317    }
    261318
  • code/branches/ai/src/orxonox/controllers/ArtificialController.h

    r6850 r6888  
    3232#include "OrxonoxPrereqs.h"
    3333
     34#include <vector>
     35
    3436#include "util/Math.h"
    3537#include "Controller.h"
     
    4345            virtual ~ArtificialController();
    4446
    45             virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);//new
     47            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    4648
    4749            void abandonTarget(Pawn* target);
     
    6062            enum State {SLAVE, MASTER, FREE};
    6163            State state_;
    62             std::list<ArtificialController*> slaves_;
     64            std::vector<ArtificialController*> slaves_;
    6365
    6466            void targetDied();
    6567
    6668            void moveToPosition(const Vector3& target);
    67             //void speedToTargetPosition(const Vector3& target);
    6869            void moveToTargetPosition();
    6970
Note: See TracChangeset for help on using the changeset viewer.