Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 3, 2010, 6:41:43 PM (14 years ago)
Author:
solex
Message:

master/slave interaction of bots now correct - getting closer to formation flight

File:
1 edited

Legend:

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

    r6795 r6850  
    4545        this->target_ = 0;
    4646        this->myMaster_ = 0;
    47         //this->slaveNumber_ = -1;
    48         this->team_ = -1;//new
    49         this->state_ = FREE;//new
     47        this->freedomCount_ = 0;
     48        this->team_ = -1;
     49        this->state_ = FREE;
    5050        this->bShooting_ = false;
    5151        this->bHasTargetPosition_ = false;
    5252        this->targetPosition_ = Vector3::ZERO;
    53         //this->slaves_ = new std::list<ArtificialController*>;
    5453
    5554        this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this));
     
    6564
    6665        XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(0);
     66    }
     67
     68// gets called when Bot dies
     69    void ArtificialController::changedControllableEntity()
     70    {
     71COUT(0) << "~changedControllableEntity 0" << std::endl;
     72        if(!getControllableEntity())
     73        {
     74COUT(0) << "~changedControllableEntity 1" << std::endl;
     75        if (this->state_ == SLAVE) unregisterSlave();
     76COUT(0) << "~changedControllableEntity 2" << std::endl;
     77         if (this->state_ == MASTER) setNewMasterWithinFormation();
     78COUT(0) << "~changedControllableEntity 3" << std::endl;
     79        this->slaves_.clear();
     80COUT(0) << "~changedControllableEntity 4" << std::endl;
     81        this->state_ = FREE;
     82COUT(0) << "~changedControllableEntity 5" << std::endl;
     83        }
    6784    }
    6885
     
    7794        if (this->target_ || distance > 10)
    7895        {
    79             // Multiply with 0.8 to make them a bit slower
    80             this->getControllableEntity()->rotateYaw(-0.8f * sgn(coord.x) * coord.x*coord.x);
    81             this->getControllableEntity()->rotatePitch(0.8f * sgn(coord.y) * coord.y*coord.y);
     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
    82107        }
    83108
    84109        if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
    85             this->getControllableEntity()->moveFrontBack(-0.5f); // They don't brake with full power to give the player a chance
    86         else
    87             this->getControllableEntity()->moveFrontBack(0.8f);
     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);
     117        }
    88118    }
    89119
     
    102132        {
    103133            myMaster_->slaves_.remove(this);
     134COUT(0) << "~unregister slave" << std::endl;
    104135        }
    105136    }
     
    126157                continue;
    127158
    128             ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
    129             if (!controller || controller->getState() != MASTER)
     159            ArtificialController *newMaster = static_cast<ArtificialController*>(it->getController());
     160
     161            if (!newMaster || newMaster->getState() != MASTER)
    130162                continue;
    131163
     
    133165            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) //&& it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) < 1000
    134166            {
    135                 if(controller->slaves_.size() > 9) continue;
    136 
    137                 this->freeAllSlaves();
     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++)
     172                {
     173                    (*itSlave)->myMaster_ = newMaster;
     174                    newMaster->slaves_.push_back(*itSlave);
     175                }
    138176                this->slaves_.clear();
    139177                this->state_ = SLAVE;
    140178
    141                 this->myMaster_ = controller;
    142                 controller->slaves_.push_back(this);
     179                this->myMaster_ = newMaster;
     180                newMaster->slaves_.push_back(this);
    143181
    144182                break;
     
    148186        //hasn't encountered any masters in range? -> become a master
    149187        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...
    150 
     188COUT(0) << "~searcheNewMaster" << std::endl;
    151189    }
    152190
     
    157195            (*it)->setTargetPosition(this->getControllableEntity()->getPosition());
    158196        }
    159 /*
    160         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
    161         {
    162 
    163             if (!it->getController())
    164                 continue;
    165 
    166             ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
    167             if (!controller || controller->getState() != SLAVE)
    168                 continue;
    169             //controller->setTargetPosition(this->getControllableEntity()->getPosition());
    170             controller->targetPosition_ = this->getControllableEntity()->getPosition();
    171             controller->bHasTargetPosition_ = true;
    172         }
    173 */
    174197    }
    175198
     
    190213        newMaster->state_ = MASTER;
    191214        newMaster->slaves_ = this->slaves_;
    192         //this->slaves_.clear();
     215        this->slaves_.clear();
    193216
    194217        this->state_ = SLAVE;
     
    205228    }
    206229
    207     void ArtificialController::freeAllSlaves()
    208     {
    209 
    210 
     230    void ArtificialController::freeSlaves()
     231    {
    211232        for(std::list<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
    212233        {
    213234            (*it)->state_ = FREE;
    214235        }
    215 /*
    216         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
    217         {
    218             ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
    219             if (controller && controller->getState() != SLAVE)
    220                 continue;
    221 
    222             controller->state_ = FREE;
    223         }
    224 */
     236        this->slaves_.clear();
     237    }
     238
     239    void ArtificialController::forceFreeSlaves()
     240    {
     241        for(std::list<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
     242        {
     243            (*it)->state_ = FREE;
     244            (*it)->forceFreedom();
     245            (*it)->targetPosition_ = this->targetPosition_;
     246            (*it)->bShooting_ = true;
     247            (*it)->getControllableEntity()->fire(0);
     248        }
    225249    }
    226250
    227251    void ArtificialController::loseMasterState()
    228252    {
    229         this->freeAllSlaves();
     253        this->freeSlaves();
    230254        this->state_ = FREE;
     255    }
     256
     257    void ArtificialController::forceFreedom()
     258    {
     259        this->freedomCount_ = 5;
     260    }
     261
     262    bool ArtificialController::forcedFree()
     263    {
     264        if(this->freedomCount_ > 0)
     265        {
     266            this->freedomCount_--;
     267            return true;
     268        } else return false;
    231269    }
    232270
Note: See TracChangeset for help on using the changeset viewer.