Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6919


Ignore:
Timestamp:
May 17, 2010, 4:45:50 PM (14 years ago)
Author:
solex
Message:

finally some decent formation flying!

Location:
code/branches/ai/src/orxonox
Files:
4 edited

Legend:

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

    r6891 r6919  
    5959        {
    6060
    61 
     61            if (this->formationFlight_)
     62            {
    6263            // return to Master after being forced free
    63             if (this->freedomCount_ == 1)   
    64             {
     64                if (this->freedomCount_ == 1)
     65                {
    6566                this->state_ = SLAVE;
    6667                this->freedomCount_ = 0;
    67             }
    68 
    69             random = rnd(maxrand);
    70             if (random < 90 && (((!this->target_) || (random < 50 && this->target_)) && !this->forcedFree()))
    71                 this->searchNewMaster();
    72 
     68                }
     69
     70                random = rnd(maxrand);
     71                if (random < 90 && (((!this->target_) || (random < 50 && this->target_)) && !this->forcedFree()))
     72                    this->searchNewMaster();
     73            }
    7374
    7475            // search enemy
     
    116117        if (this->state_ == SLAVE)
    117118        {
    118                // this->bShooting_ = true;
     119
    119120        }
    120121
     
    125126            this->commandSlaves();
    126127
    127 
    128             // 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() < 4 )
    131 //                 this->loseMasterState();
    132 
    133             // look out for outher masters if formation is small
    134             random = rnd(maxrand);
    135             if(this->slaves_.size() < 3 && random < 20)
    136                 this->searchNewMaster();
    137 
    138             // search enemy
    139             random = rnd(maxrand);
    140             if (random < 15 && (!this->target_))
    141                 this->searchNewTarget();
    142 
    143             // forget enemy
    144             random = rnd(maxrand);
    145             if (random < 5 && (this->target_))
    146                 this->forgetTarget();
    147 
    148             // next enemy
    149             random = rnd(maxrand);
    150             if (random < 10 && (this->target_))
    151                 this->searchNewTarget();
    152 
    153             // fly somewhere
    154             random = rnd(maxrand);
    155             if (random < 50 && (!this->bHasTargetPosition_ && !this->target_))
    156                 this->searchRandomTargetPosition();
    157 
    158 
    159             // fly somewhere else
    160             random = rnd(maxrand);
    161             if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
    162                 this->searchRandomTargetPosition();
    163 
    164             // shoot
    165             random = rnd(maxrand);
    166             if (random < 5 && (this->target_ && !this->bShooting_))
     128            if  (this->specificMasterAction_ != NONE)
    167129            {
     130                if (this->specificMasterAction_  == HOLD)
     131                    this->specificMasterActionHold();
     132
     133                if (this->specificMasterAction_  == TURN180)
     134                    this->turn180();
     135
     136                if (this->specificMasterAction_ == SPIN)
     137                    this->spin();
     138            }
     139
     140            else {
     141
     142                 // make 180 degree turn - a specific Master Action
     143                random = rnd(maxrand);
     144                if (random < 5)
     145                   this->specificMasterAction_ = TURN180;
     146
     147                // spin around - a specific Master Action
     148                random = rnd(maxrand);
     149                if (random < 5)
     150                   this->specificMasterAction_ = SPIN;
     151
     152                 // lose master status (only if less than 4 slaves in formation)
     153                random = rnd(maxrand);
     154                if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 )
     155                   this->loseMasterState();
     156
     157                // look out for outher masters if formation is small
     158                random = rnd(maxrand);
     159                if(this->slaves_.size() < 3 && random < 20)
     160                    this->searchNewMaster();
     161
     162                // search enemy
     163                random = rnd(maxrand);
     164                if (random < 15 && (!this->target_))
     165                    this->searchNewTarget();
     166
     167                // forget enemy
     168                random = rnd(maxrand);
     169                if (random < 5 && (this->target_))
     170                    this->forgetTarget();
     171
     172                // next enemy
     173                random = rnd(maxrand);
     174                if (random < 10 && (this->target_))
     175                    this->searchNewTarget();
     176
     177                // fly somewhere
     178                random = rnd(maxrand);
     179                if (random < 50 && (!this->bHasTargetPosition_ && !this->target_))
     180                    this->searchRandomTargetPosition();
     181
     182
     183                // fly somewhere else
     184                random = rnd(maxrand);
     185                if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
     186                    this->searchRandomTargetPosition();
     187
     188                // shoot
     189                random = rnd(maxrand);
     190                if (random < 5 && (this->target_ && !this->bShooting_))
     191                {
    168192                this->bShooting_ = true;
    169 //                 this->forceFreeSlaves();
    170             }
    171             // stop shooting
    172             random = rnd(maxrand);
    173             if (random < 25 && (this->bShooting_))
    174                 this->bShooting_ = false;
     193                this->forceFreeSlaves();
     194                }
     195
     196                // stop shooting
     197                random = rnd(maxrand);
     198                if (random < 25 && (this->bShooting_))
     199                    this->bShooting_ = false;
     200
     201            }
    175202        }
    176203
     
    189216            if (this->bHasTargetPosition_)
    190217                this->moveToTargetPosition();
     218            if (this->specificMasterAction_ ==  NONE)
     219            {
     220                if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(Ogre::Math::PI / 20.0f))
     221                    this->getControllableEntity()->fire(0);
     222            }
     223        }
     224
     225        if (this->state_ == SLAVE)
     226        {
     227
     228            if (this->bHasTargetPosition_)
     229                this->moveToTargetPosition();
     230
     231        }
     232
     233         if (this->state_ == FREE)
     234        {
     235            if (this->target_)
     236                this->aimAtTarget();
     237
     238            if (this->bHasTargetPosition_)
     239                this->moveToTargetPosition();
    191240
    192241            if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(Ogre::Math::PI / 20.0f))
     
    194243        }
    195244
    196         if (this->state_ == SLAVE)
    197         {
    198 
    199             if (this->bHasTargetPosition_)
    200                 this->moveToTargetPosition();
    201 
    202         }
    203 
    204          if (this->state_ == FREE)
    205         {
    206             if (this->target_)
    207                 this->aimAtTarget();
    208 
    209             if (this->bHasTargetPosition_)
    210                 this->moveToTargetPosition();
    211 
    212             if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(Ogre::Math::PI / 20.0f))
    213                 this->getControllableEntity()->fire(0);
    214         }
    215 
    216245        SUPER(AIController, tick, dt);
    217246    }
  • code/branches/ai/src/orxonox/controllers/ArtificialController.cc

    r6918 r6919  
    4242{
    4343
    44     static const unsigned int MAX_FORMATION_SIZE = 6;
     44    static const unsigned int MAX_FORMATION_SIZE = 7;
     45    static const int FORMATION_LENGTH =  10;
     46    static const int FORMATION_WIDTH =  110;
    4547    static const int FREEDOM_COUNT = 4; //seconds the slaves in a formation will be set free when master attacks an enemy
    4648    static const float SPEED_MASTER = 0.6f;
     
    5456
    5557        this->target_ = 0;
     58        this->formationFlight_  =  true;
    5659        this->myMaster_ = 0;
    5760        this->freedomCount_ = 0;
    5861        this->team_ = -1;
    5962        this->state_ = FREE;
     63        this->specificMasterAction_ = NONE;
     64        this->specificMasterActionHoldCount_  = 0;
    6065        this->bShooting_ = false;
    6166        this->bHasTargetPosition_ = false;
     
    7378        SUPER(ArtificialController, XMLPort, xmlelement, mode);
    7479
    75         XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(0);
     80        XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(-1);
     81//         XMLPortParam(ArtificialController, "formation", setFormationFlight, getFormationFlight, xmlelement, mode).defaultValues(true);
    7682    }
    7783
     
    134140        if(this->state_ == SLAVE)
    135141        {
    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)
     142
     143           this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR_MASTER * sgn(coord.x) * coord.x*coord.x);
     144           this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR_MASTER * sgn(coord.y) * coord.y*coord.y);
     145
     146
     147
     148
     149
     150            if (distance < 300)
     151            {
     152                if (distance < 40)
    152153                {
    153                     this->getControllableEntity()->setVelocity(0.8f*this->target_->getVelocity());
    154                 } else this->getControllableEntity()->moveFrontBack(-1.0f*exp(distance/100.0));
     154                    this->getControllableEntity()->moveFrontBack(0.8f*SPEED_MASTER);
     155                } else this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER);
    155156
    156157            } else {
    157                 this->getControllableEntity()->moveFrontBack(1.0f + distance/500.0f);
     158                this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER + distance/300.0f);
    158159            }
    159160        }
     
    176177            if( it != myMaster_->slaves_.end() )
    177178                myMaster_->slaves_.erase(it);
    178 //COUT(0) << "~unregister slave" << std::endl;
     179// COUT(0) << "~unregister slave" << std::endl;
    179180        }
    180181    }
     
    188189        this->targetPosition_ = this->getControllableEntity()->getPosition();
    189190        this->forgetTarget();
    190 
     191        int teamSize = 0;
    191192        //go through all pawns
    192193        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
     
    197198                continue;
    198199
    199             //has it an ArtificialController and is it a master?
     200            //has it an ArtificialController?
    200201            if (!it->getController())
    201202                continue;
    202203
     204            //is pawn oneself?
     205            if (static_cast<ControllableEntity*>(*it) == this->getControllableEntity())
     206                continue;
     207
     208            teamSize++;
     209
    203210            ArtificialController *newMaster = static_cast<ArtificialController*>(it->getController());
    204211
     212            //is it a master?
    205213            if (!newMaster || newMaster->getState() != MASTER)
    206214                continue;
     
    208216            float distance = (it->getPosition() - this->getControllableEntity()->getPosition()).length();
    209217
    210             //is pawn oneself? && is pawn in range?
    211             if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity() && distance < 5000)
     218            // is pawn in range?
     219            if (distance < 5000)
    212220            {
    213221                if(newMaster->slaves_.size() > MAX_FORMATION_SIZE) continue;
     
    227235            }
    228236        }//for
    229 
    230237        //hasn't encountered any masters in range? -> become a master
    231         if (state_!=SLAVE) state_ = MASTER;//master encounters master? ->done
     238        if (state_ != SLAVE  && teamSize != 0) state_ = MASTER;//master encounters master? ->done
     239
    232240    }
    233241
     
    243251            this->slaves_.front()->setTargetPosition(dest);
    244252        }
    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         {
     253        else
     254        {
     255            dest += 1.0f*orient*WorldEntity::BACK;
     256            Vector3 pos = Vector3::ZERO;
     257            int i = 1;
     258
    256259            for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
    257260            {
    258                 (*it)->setTargetPosition(this->getControllableEntity()->getPosition());
     261                pos = Vector3::ZERO;
     262                if (i <= 1) pos += dest  + FORMATION_WIDTH*WorldEntity::LEFT;
     263                if (i == 2) pos += dest  + FORMATION_WIDTH*WorldEntity::RIGHT;
     264                if (i == 3) pos += dest  + FORMATION_WIDTH*WorldEntity::UP;
     265                if (i >= 4)
     266                {
     267                    pos += dest  + FORMATION_WIDTH*WorldEntity::DOWN;
     268                    i = 1;
     269                    dest += FORMATION_LENGTH*orient*WorldEntity::BACK;
     270                    (*it)->setTargetPosition(pos);
     271                    continue;
     272                }
     273                i++;
     274                (*it)->setTargetPosition(pos);
    259275            }
    260276        }
     
    326342        } else return false;
    327343    }
     344
     345    void ArtificialController::specificMasterActionHold()
     346    {
     347        if (specificMasterActionHoldCount_ == 0) this->specificMasterAction_ = NONE;
     348        else specificMasterActionHoldCount_--;
     349    }
     350
     351    void ArtificialController::turn180()
     352    {
     353        this->specificMasterAction_  =  NONE;
     354    }
     355
     356    void ArtificialController::spin()
     357    {
     358        this->specificMasterAction_  =  NONE;
     359    }
     360
    328361
    329362    void ArtificialController::setTargetPosition(const Vector3& target)
  • code/branches/ai/src/orxonox/controllers/ArtificialController.h

    r6888 r6919  
    5353            inline int getTeam() const
    5454                { return this->team_; }
     55            inline void setFormationFlight(bool formation)
     56                { this->formationFlight_ = formation; }
     57            inline bool getFormationFlight() const
     58                { return this->formationFlight_; }
    5559            virtual void changedControllableEntity();
    5660
     
    5963
    6064            int team_;
     65            bool formationFlight_;
    6166            int freedomCount_;
    6267            enum State {SLAVE, MASTER, FREE};
    6368            State state_;
    6469            std::vector<ArtificialController*> slaves_;
     70            ArtificialController *myMaster_;
     71            enum SpecificMasterAction {NONE, HOLD, SPIN, TURN180};
     72            SpecificMasterAction specificMasterAction_;
     73            int specificMasterActionHoldCount_;
    6574
    6675            void targetDied();
     
    8089            void forceFreedom();
    8190            bool forcedFree();
    82 
    83             ArtificialController *myMaster_;
     91            void specificMasterActionHold();
     92            void turn180();
     93            void spin();
    8494
    8595            void setTargetPosition(const Vector3& target);
  • code/branches/ai/src/orxonox/worldentities/ControllableEntity.cc

    r6417 r6919  
    577577        {
    578578            MobileEntity::setAngularVelocity(velocity);
    579             this->client_angular_velocity_ = this->getAngularVelocity();
     579// //             this->client_angular_velocity_ = this->getAngularVelocity();
    580580        }
    581581    }
Note: See TracChangeset for help on using the changeset viewer.