Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 29, 2015, 11:53:45 AM (9 years ago)
Author:
gania
Message:

Wingmen and Leaders look for their leaders

File:
1 edited

Legend:

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

    r10678 r10717  
    4444    {
    4545        RegisterObject(AIController);
     46
    4647        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&AIController::action, this)));
    4748    }
     
    6162            if (this->formationFlight_)
    6263            {
    63                 //When this is a master and was destroyed, destructor might complain that there are slaves of this, although this was removed from formation
    64                 //race conditions?
    65                 //destructor takes care of slaves anyway, so no need to worry about internal_error
    66 
    6764
    6865                //changed order -> searchNewMaster MUSTN'T be called in SLAVE-state (bugfix for internal-error messages at quit)
     
    7269
    7370                // return to Master after being forced free
    74                 if (this->freedomCount_ == ACTION_INTERVAL)
     71                if (this->freedomCount_ == 1)
    7572                {
    7673                    this->state_ = SLAVE;
    77                     this->freedomCount_ = 0; // ACTION_INTERVAL is 1 sec, freedomCount is a remaining time of temp. freedom
    78                 }
    79             } else{
    80                 //form a formation
    81                 if (!this->forcedFree())
    82                     this->searchNewMaster();
    83             }
     74                    this->freedomCount_ = 0;
     75                }
     76            }
     77
    8478            this->defaultBehaviour(maxrand);
    8579
    8680        }
    8781
    88 
    8982        if (this->state_ == SLAVE && this->formationMode_ == ATTACK)
    9083        {
    9184            // search enemy
    92             if ((!this->target_))
     85            random = rnd(maxrand);
     86            if (random < (botlevel_*100) && (!this->target_))
    9387                this->searchNewTarget();
    9488
    95            
     89            // next enemy
     90            random = rnd(maxrand);
     91            if (random < (botlevel_*30) && (this->target_))
     92                this->searchNewTarget();
     93
    9694            // shoot
    97             if ((this->target_ && !this->bShooting_))
     95            random = rnd(maxrand);
     96            if (!(this->passive_) && random < (botlevel_*100) && (this->target_ && !this->bShooting_))
    9897                this->bShooting_ = true;
    9998
    10099            // stop shooting
    101             if (this->bShooting_ && !this->target_)
     100            random = rnd(maxrand);
     101            if (random < (1-botlevel_)*50 && (this->bShooting_))
    102102                this->bShooting_ = false;
    103103
     
    106106        if (this->state_ == MASTER)
    107107        {
    108            
    109             //-------------------------------------------------------
    110             //collect data for AI behaviour
    111             Vector3* meanOfEnemiesPtr = new Vector3(0.0,0.0,0.0);
    112             Vector3* meanOfAlliesPtr  = new Vector3(0.0,0.0,0.0);
    113             Vector3 meanOfAllies = *meanOfAlliesPtr;
    114             Vector3 meanOfEnemies = *meanOfEnemiesPtr;
    115 
    116 
    117             for (ObjectList<AIController>::iterator it = ObjectList<AIController>::begin(); it; ++it)
    118             {
    119 
    120                 Gametype* gt=this->getGametype();
    121                 if (!gt)
    122                 {
    123                     gt=it->getGametype();
    124                 }
    125                 if (!FormationController::sameTeam(this->getControllableEntity(), it->getControllableEntity(),gt))
    126                 {
    127                     enemies_.push_back(*it);
    128                 }
    129                 else {
    130                     allies_.push_back(*it);
    131                 }
    132             }
    133             if (enemies_.size() != 0 && allies_.size() != 0){
    134                 for (std::vector<WeakPtr<AIController> >::iterator it = enemies_.begin() ; it != enemies_.end(); ++it)
    135                     meanOfEnemies += (*it)->getControllableEntity()->getWorldPosition();
    136 
    137                 meanOfEnemies /= enemies_.size();
    138 
    139                 for (std::vector<WeakPtr<AIController> >::iterator it = allies_.begin() ; it != allies_.end(); ++it)
    140                     meanOfAllies += (*it)->getControllableEntity()->getWorldPosition();
    141 
    142                 meanOfAllies /= allies_.size();
    143 
    144                 //orxout(internal_error) << "There are " << enemies_Counter << " enemies_, mean position is " << meanOfEnemies << endl;
    145                 orxout(internal_error) << "Distance is " << (meanOfEnemies-meanOfAllies).length() << endl;
    146                 orxout(internal_error) << "mean of allies_ is " << meanOfAllies << ", with a size " << allies_.size() << endl;
    147                 orxout(internal_error) << "mean of enemies_ is " << meanOfEnemies << ", with a size " << enemies_.size() << endl;
    148             }
    149             //-------------------------------------------------------
    150            
    151             //Decide which formationMode to choose
    152             this->setFormationMode(ATTACK);
    153             this->setDesiredPositionOfSlaves();
    154 
    155             //this->commandSlaves();
     108            this->commandSlaves();
    156109
    157110            if  (this->specificMasterAction_ != NONE)
     
    161114
    162115                 // make 180 degree turn - a specific Master Action
    163                 /*
    164116                random = rnd(1000.0f);
    165117                if (random < 5)
     
    171123                   this->spinInit();
    172124
    173                 */
    174125                /*// follow a randomly chosen human - a specific Master Action
    175126                random = rnd(1000.0f);
     
    177128                   this->followRandomHumanInit();
    178129*/
    179                /*
    180130                 // lose master status (only if less than 4 slaves in formation)
    181131                random = rnd(maxrand);
    182132                if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 )
    183133                   this->loseMasterState();
    184                 */
    185                
     134
    186135                // look out for outher masters if formation is small
    187136                random = rnd(maxrand);
     
    193142            }
    194143        }
    195         allies_.clear();
    196         enemies_.clear();
     144
    197145    }
    198146
    199147    void AIController::tick(float dt)
    200148    {
    201 
    202149        if (!this->isActive())
    203150            return;
     151
    204152        float random;
    205153        float maxrand = 100.0f / ACTION_INTERVAL;
    206154        ControllableEntity* controllable = this->getControllableEntity();
    207         if (this->state_ == SLAVE && controllable && this->mode_ == DEFAULT)
    208         {
    209            
    210             if (this->myMaster_ && this->myMaster_->getControllableEntity() && desiredRelativePosition_){
    211                 Vector3 desiredAbsolutePosition = this->myMaster_->getControllableEntity()->getWorldPosition() + this->myMaster_->getControllableEntity()->getWorldOrientation()* (*desiredRelativePosition_);
    212                 this->moveToPosition (desiredAbsolutePosition);
    213                 //WorldEntity* waypoint = new WorldEntity(this->getContext());
    214                 //waypoint->setPosition(desiredAbsolutePosition);
    215            
    216                 //this->addWaypoint(waypoint);
    217            
    218             }
    219          
    220         }
    221155        //DOES: Either move to the waypoint or search for a Point of interest
    222156        if (controllable && this->mode_ == DEFAULT)// bot is ready to move to a target
     
    255189                        {
    256190                            this->aimAtTarget();
    257                             this->follow();  //If a bot is shooting a player, it shouldn't let him go away easily.
     191                            random = rnd(maxrand);
     192                            if(this->botlevel_*70 > random && !this->isCloseAtTarget(100))
     193                                this->follow();  //If a bot is shooting a player, it shouldn't let him go away easily.
    258194                        }
    259195                    }
     
    316252        }//END_OF ROCKET MODE
    317253
    318 
    319254        SUPER(AIController, tick, dt);
    320255    }
    321256//**********************************************NEW
    322257    void AIController::defaultBehaviour(float maxrand)
    323     { 
    324         if (!this->target_)
    325             this->searchNewTarget();
    326         if (!(this->passive_) && (this->target_ && !this->bShooting_))
    327             this->bShooting_ = true;
    328            
     258    {       float random;
     259            // search enemy
     260            random = rnd(maxrand);
     261            if (random < (botlevel_* 100) && (!this->target_))
     262                this->searchNewTarget();
     263
     264            // forget enemy
     265            random = rnd(maxrand);
     266            if (random < ((1-botlevel_)*20) && (this->target_))
     267                this->forgetTarget();
     268
     269            // next enemy
     270            random = rnd(maxrand);
     271            if (random < (botlevel_*30) && (this->target_))
     272                this->searchNewTarget();
     273
     274            // fly somewhere
     275            random = rnd(maxrand);
     276            if (random < 50 && (!this->bHasTargetPosition_ && !this->target_))
     277                this->searchRandomTargetPosition();
     278
     279            // stop flying
     280            random = rnd(maxrand);
     281            if (random < 10 && (this->bHasTargetPosition_ && !this->target_))
     282                this->bHasTargetPosition_ = false;
     283
     284            // fly somewhere else
     285            random = rnd(maxrand);
     286            if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
     287                this->searchRandomTargetPosition();
     288
     289            if (this->state_ == MASTER) // master: shoot
     290            {
     291                random = rnd(maxrand);
     292                if (!(this->passive_) && random < (100*botlevel_) && (this->target_ && !this->bShooting_))
     293                {
     294                    this->bShooting_ = true;
     295                    this->forceFreeSlaves();
     296                }
     297            }
     298            else
     299            {
     300                // shoot
     301                random = rnd(maxrand);
     302                if (!(this->passive_) && random < (botlevel_*100) && (this->target_ && !this->bShooting_))
     303                    this->bShooting_ = true;
     304            }
     305
     306            // stop shooting
     307            random = rnd(maxrand);
     308            if (random < ((1 - botlevel_)*50) && (this->bShooting_))
     309                this->bShooting_ = false;
     310
     311            // boost
     312            random = rnd(maxrand);
     313            if (random < botlevel_*50 )
     314                this->boostControl();
     315
     316            // update Checkpoints
     317            /*random = rnd(maxrand);
     318            if (this->defaultWaypoint_ && random > (maxrand-10))
     319                this->manageWaypoints();
     320            else //if(random > maxrand-10) //CHECK USABILITY!!*/
     321            if (this->waypoints_.size() == 0 )
     322                this->manageWaypoints();
    329323    }
    330324
Note: See TracChangeset for help on using the changeset viewer.