Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7066


Ignore:
Timestamp:
May 31, 2010, 6:18:50 PM (14 years ago)
Author:
solex
Message:

formationflight final commit before presentation (probably…)

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

Legend:

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

    r7034 r7066  
    127127
    128128            if  (this->specificMasterAction_ != NONE)
    129             {
    130129                    this->specificMasterActionHold();
    131 
    132 //                 if (this->specificMasterAction_  == TURN180)
    133 //                     this->turn180Init();
    134 
    135 //                 if (this->specificMasterAction_ == SPIN)
    136 //                     this->spinInit();
    137 
    138 //                 if (this->specificMasterAction_ == FOLLOWHUMAN)
    139 //                     this->followHuman(this->HumanToFollow_, false);
    140             }
    141130
    142131            else {
     
    151140                if (random < 5)
    152141                   this->spinInit();
     142
     143                // follow a randomly chosen human - a specific Master Action
     144                random = rnd(1000.0f);
     145                if (random < 1)
     146                   this->followRandomHumanInit();
    153147
    154148                 // lose master status (only if less than 4 slaves in formation)
     
    230224            if (this->specificMasterAction_ == SPIN)
    231225                    this->spin();
    232             if (this->specificMasterAction_ == FOLLOWHUMAN)
     226            if (this->specificMasterAction_ == FOLLOW)
    233227                    this->follow();
    234228        }
  • code/branches/presentation3/src/orxonox/controllers/ArtificialController.cc

    r7064 r7066  
    5454
    5555    static const unsigned int STANDARD_MAX_FORMATION_SIZE = 7;
     56    static const int RADIUS_TO_SEARCH_FOR_MASTERS = 5000;
    5657    static const int FORMATION_LENGTH =  130;
    5758    static const int FORMATION_WIDTH =  110;
     
    6162    static const float SPEED_FREE = 0.8f;
    6263    static const float ROTATEFACTOR_FREE = 0.8f;
    63     static const int SECONDS_TO_FOLLOW_HUMAN = 100;
     64
    6465
    6566    ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator)
     
    7980        this->bShooting_ = false;
    8081        this->bHasTargetPosition_ = false;
     82        this->speedCounter_ = 0.2f;
    8183        this->targetPosition_ = Vector3::ZERO;
    82         this->humanToFollow_ = NULL;
    8384
    8485        this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this));
     
    9495
    9596        XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(-1);
    96         XMLPortParam(ArtificialController, "formationflight", setFormationFlight, getFormationFlight, xmlelement, mode).defaultValues(true);
    97         XMLPortParam(ArtificialController, "formation_size", setFormationSize, getFormationSize, xmlelement, mode).defaultValues(STANDARD_MAX_FORMATION_SIZE);
     97        XMLPortParam(ArtificialController, "formationFlight", setFormationFlight, getFormationFlight, xmlelement, mode).defaultValues(true);
     98        XMLPortParam(ArtificialController, "formationSize", setFormationSize, getFormationSize, xmlelement, mode).defaultValues(STANDARD_MAX_FORMATION_SIZE);
    9899    }
    99100
     
    104105        @param form activate formflight if form is true
    105106    */
    106     void ArtificialController::formationflight(bool form)
     107    void ArtificialController::formationflight(const bool form)
    107108    {
    108109        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
     
    129130        @param action which action to perform (integer, so it can be called with a console command (tmp solution))
    130131    */
    131     void ArtificialController::masteraction(int action)
     132    void ArtificialController::masteraction(const int action)
    132133    {
    133134        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
     
    149150
    150151    /**
    151         @brief A human player gets followed by its nearest master. Initiated by console command, intended for demonstration puproses. Does not work at the moment.
     152        @brief A human player gets followed by its nearest master. Initiated by console command, so far intended for demonstration puproses (possible future pickup).
    152153    */
    153154    void ArtificialController::followme()
     
    169170            ArtificialController *aiController = orxonox_cast<ArtificialController*>(it->getController());
    170171
    171             if(aiController || aiController->state_ == MASTER)
     172            if(aiController && aiController->state_ == MASTER)
    172173                allMasters.push_back(aiController);
    173174
     
    182183                int i = 0;
    183184
    184                 for(std::vector<ArtificialController*>::iterator it = allMasters.begin(); it != allMasters.end(); it++)
     185                for(std::vector<ArtificialController*>::iterator it = allMasters.begin(); it != allMasters.end(); it++, i++)
    185186                    {
     187                        if (!ArtificialController::sameTeam((*it)->getControllableEntity(), humanPawn, (*it)->getGametype())) continue;
    186188                        distance = posHuman - (*it)->getControllableEntity()->getPosition().length();
    187189                        if(distance < minDistance) index = i;
    188190                    }
    189                 allMasters[index]->humanToFollow_ = humanPawn;
    190 //                allMasters[index]->followHuman(humanPawn, false);
     191                allMasters[index]->followInit(humanPawn);
    191192            }
    192193
     
    197198        @param passive if true, bots won't shoot.
    198199    */
    199     void ArtificialController::passivebehaviour(bool passive)
     200    void ArtificialController::passivebehaviour(const bool passive)
    200201    {
    201202        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
     
    218219        @param size maximal formation size.
    219220    */
    220     void ArtificialController::formationsize(int size)
     221    void ArtificialController::formationsize(const int size)
    221222    {
    222223        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
     
    242243        {
    243244        if (this->state_ == SLAVE) unregisterSlave();
    244          if (this->state_ == MASTER) setNewMasterWithinFormation();
     245        if (this->state_ == MASTER) setNewMasterWithinFormation();
    245246        this->slaves_.clear();
    246247        this->state_ = FREE;
     248        this->specificMasterAction_ = NONE;
    247249
    248250        }
     
    255257            return;
    256258
     259        // Slave uses special movement if its master is in FOLLOW mode
     260        if(this->state_ == SLAVE && this->myMaster_ && this->myMaster_->specificMasterAction_ == FOLLOW)
     261        {
     262//             this->followForSlaves(target);
     263//             return;
     264        }
     265
    257266        Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
    258267        float distance = (target - this->getControllableEntity()->getPosition()).length();
     
    263272            if (this->target_ || distance > 10)
    264273            {
    265                 // Multiply with 0.8 to make them a bit slower
     274                // Multiply with ROTATEFACTOR_FREE to make them a bit slower
    266275                this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_FREE * sgn(coord.x) * coord.x*coord.x);
    267276                this->getControllableEntity()->rotatePitch(ROTATEFACTOR_FREE * sgn(coord.y) * coord.y*coord.y);
     
    316325    }
    317326
    318     int ArtificialController::getState()
    319     {
    320         return this->state_;
    321     }
    322 
    323     /**
    324         @brief Unregisters a slave from its master. Called by a slave.
     327
     328    /**
     329        @brief Unregisters a slave from its master. Initiated by a slave.
    325330    */
    326331    void ArtificialController::unregisterSlave() {
     
    363368
    364369            //is it a master?
    365             if (!newMaster || newMaster->getState() != MASTER)
     370            if (!newMaster || newMaster->state_ != MASTER)
    366371                continue;
    367372
     
    369374
    370375            // is pawn in range?
    371             if (distance < 5000)
     376            if (distance < RADIUS_TO_SEARCH_FOR_MASTERS)
    372377            {
    373378                if(newMaster->slaves_.size() > this->maxFormationSize_) continue;
     
    393398
    394399    /**
    395         @brief Commands the slaves of a master into a formation. Called by a master.
     400        @brief Commands the slaves of a master into a formation. Sufficiently fast not to be called within tick. Initiated by a master.
    396401    */
    397402    void ArtificialController::commandSlaves()
     
    452457
    453458        this->slaves_.clear();
     459        this->specificMasterAction_ = NONE;
    454460        this->state_ = SLAVE;
    455461        this->myMaster_ = newMaster;
     
    463469
    464470    /**
    465         @brief Frees all slaves form a master. Called by a master.
     471        @brief Frees all slaves form a master. Initiated by a master.
    466472    */
    467473    void ArtificialController::freeSlaves()
     
    538544    void ArtificialController::turn180Init()
    539545    {
    540         COUT(0) << "~turnInit" << std::endl;
    541546        if(this->state_ != MASTER) return;
    542547
     
    564569
    565570    /**
    566         @brief Master initializes a spin around its looking direction axis. Leads to a "specific master action". Not yet implemented.
     571        @brief Master initializes a spin around its looking direction axis. Leads to a "specific master action".
    567572    */
    568573    void ArtificialController::spinInit()
    569574    {
    570         COUT(0) << "~spinInit" << std::endl;
    571575        if(this->state_ != MASTER) return;
    572576        this->specificMasterAction_ = SPIN;
     
    584588
    585589    /**
    586         @brief Master begins to follow a human player. Is a "specific master action".
    587         @param humanController human to follow.
    588         @param alaways follows human forever if true, else it follows it for @var SECONDS_TO_FOLLOW_HUMAN seconds.
    589     */
    590     void ArtificialController::followHumanInit(Pawn* human, bool always)
    591     {
    592         COUT(0) << "~followInit" << std::endl;
    593         if (human == NULL || this->state_ != MASTER)
     590        @brief Master begins to follow a pawn. Is a "specific master action".
     591        @param pawn pawn to follow.
     592        @param alaways follows pawn forever if true (false if omitted).
     593        @param secondsToFollow seconds to follow the pawn if always is false. Will follow pawn 100 seconds if omitted (set in header).
     594    */
     595    void ArtificialController::followInit(Pawn* pawn, const bool always, const int secondsToFollow)
     596    {
     597        if (pawn == NULL || this->state_ != MASTER)
    594598            return;
    595 
    596         this->specificMasterAction_  =  FOLLOWHUMAN;
    597 
    598         this->setTarget(human);
     599        this->specificMasterAction_  =  FOLLOW;
     600
     601        this->setTarget(pawn);
    599602        if (!always)
    600             this->specificMasterActionHoldCount_ = SECONDS_TO_FOLLOW_HUMAN;
     603            this->specificMasterActionHoldCount_ = secondsToFollow;
    601604        else
    602605            this->specificMasterActionHoldCount_ = INT_MAX; //for now...
     
    604607    }
    605608
    606     /**
    607         @brief Follows target with adjusted speed. Called within tick.
     609
     610    /**
     611        @brief Master begins to follow a randomly chosen human player of the same team. Is a "specific master action".
     612    */
     613    void ArtificialController::followRandomHumanInit()
     614    {
     615
     616        Pawn *humanPawn = NULL;
     617        NewHumanController *currentHumanController = NULL;
     618
     619        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
     620        {
     621            if (!it->getController())
     622                continue;
     623
     624            currentHumanController = orxonox_cast<NewHumanController*>(it->getController());
     625            if(currentHumanController)
     626            {
     627                if (!ArtificialController::sameTeam(this->getControllableEntity(), *it, this->getGametype())) continue;
     628                humanPawn = *it;
     629                break;
     630            }
     631        }
     632
     633        if((humanPawn != NULL))
     634                this->followInit(humanPawn);
     635    }
     636
     637    /**
     638        @brief Master follows target with adjusted speed. Called within tick.
    608639    */
    609640    void ArtificialController::follow()
    610641    {
    611         this->moveToTargetPosition(); //standard position apprach for now.
    612     }
    613 
     642        this->moveToPosition(this->target_->getPosition());
     643/*
     644        if (!this->getControllableEntity())
     645            return;
     646
     647        float distance = (this->target_->getPosition() - this->getControllableEntity()->getPosition()).length();
     648
     649        Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->target_->getPosition());
     650
     651
     652        this->getControllableEntity()->rotateYaw(-0.8f * sgn(coord.x) * coord.x*coord.x);
     653        this->getControllableEntity()->rotatePitch(0.8f * sgn(coord.y) * coord.y*coord.y);
     654
     655        float speedDiv = this->getControllableEntity()->getVelocity().squaredLength() - this->target_->getVelocity().squaredLength();
     656
     657COUT(0) << "~follow distance: " << distance << "SpeedCounter: " << this->speedCounter_ << "~speedDiv: " << speedDiv << std::endl;
     658        if (distance < 800)
     659        {
     660            if (distance < 200)
     661            {
     662                this->speedCounter_ -= 0.5f;
     663                if(this->speedCounter_ < 0) this->speedCounter_ = 0.0f;
     664                this->getControllableEntity()->moveFrontBack(speedCounter_);
     665            } else {
     666                if(speedDiv < 0)
     667                    this->speedCounter_ +=  0.01f;
     668                else
     669                    this->speedCounter_ -= 0.05f;
     670                this->getControllableEntity()->moveFrontBack(speedCounter_);
     671            }
     672
     673        } else {
     674            this->speedCounter_ += 0.05f;
     675            this->getControllableEntity()->moveFrontBack(speedCounter_ + distance/300.0f);
     676        }
     677//         if (this->getControllableEntity()->getVelocity().squaredLength() > 50.0f) this->speedCounter_ = 0;
     678
     679*/
     680    }
     681
     682
     683    /**
     684        @brief Slave moving behaviour when master is following a pawn, gets redirected from moveToPosition(const Vector3& target)). Called within tick.
     685    */
     686    void ArtificialController::followForSlaves(const Vector3& target)
     687    {
     688
     689/*
     690        if (!this->getControllableEntity() && !this->myMaster_ && this->myMaster_->state_ != FOLLOW && !this->myMaster_->target_)
     691            return;
     692
     693        float distance = (target - this->getControllableEntity()->getPosition()).length();
     694
     695        Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
     696
     697
     698        this->getControllableEntity()->rotateYaw(-0.8f * sgn(coord.x) * coord.x*coord.x);
     699        this->getControllableEntity()->rotatePitch(0.8f * sgn(coord.y) * coord.y*coord.y);
     700
     701
     702        float speedDiv = this->getControllableEntity()->getVelocity().squaredLength() - this->myMaster_->target_->getVelocity().squaredLength();
     703
     704
     705         if (distance < 800)
     706        {
     707            if (distance < 200)
     708            {
     709                this->speedCounter_ -= 5.0f;
     710                if(this->speedCounter_ < 0) this->speedCounter_ = 0.0f;
     711                this->getControllableEntity()->moveFrontBack(speedCounter_);
     712            } else {
     713                if(speedDiv < 0)
     714                    this->speedCounter_ +=  0.01f;
     715                else
     716                    this->speedCounter_ -= 0.05f;
     717                this->getControllableEntity()->moveFrontBack(speedCounter_);
     718            }
     719
     720        } else {
     721            this->speedCounter_ += 0.05f;
     722            this->getControllableEntity()->moveFrontBack(speedCounter_ + distance/300.0f);
     723        }
     724//         if (this->getControllableEntity()->getVelocity().squaredLength() > 50.0f) this->speedCounter_ = 0;
     725*/
     726    }
    614727
    615728
  • code/branches/presentation3/src/orxonox/controllers/ArtificialController.h

    r7034 r7066  
    5454            inline int getTeam() const
    5555                { return this->team_; }
     56
    5657            inline void setFormationFlight(bool formation)
    5758                { this->formationFlight_ = formation; }
    5859            inline bool getFormationFlight() const
    5960                { return this->formationFlight_; }
     61
    6062            inline void setFormationSize(int size)
    6163                { this->maxFormationSize_ = size; }
    6264            inline int getFormationSize() const
    6365                { return this->maxFormationSize_; }
     66
    6467            virtual void changedControllableEntity();
    6568
    66             static void formationflight(bool form);
    67             static void masteraction(int action);
     69            static void formationflight(const bool form);
     70            static void masteraction(const int action);
    6871            static void followme();
    69             static void passivebehaviour(bool passive);
    70             static void formationsize(int size);
     72            static void passivebehaviour(const bool passive);
     73            static void formationsize(const int size);
    7174
    7275        protected:
     
    8184            std::vector<ArtificialController*> slaves_;
    8285            ArtificialController *myMaster_;
    83             enum SpecificMasterAction {NONE, HOLD, SPIN, TURN180, FOLLOWHUMAN};
     86            enum SpecificMasterAction {NONE, HOLD, SPIN, TURN180, FOLLOW};
    8487            SpecificMasterAction specificMasterAction_;
    8588            int specificMasterActionHoldCount_;
    86             Pawn* humanToFollow_;
    87 
    88             void targetDied();
     89            float speedCounter_; //for speed adjustment when following
    8990
    9091            void moveToPosition(const Vector3& target);
    9192            void moveToTargetPosition();
    92 
    93             int getState();
    9493
    9594            void unregisterSlave();
     
    109108            void spinInit();
    110109            void spin();
    111             void followHumanInit(Pawn* human, bool always);
     110            void followInit(Pawn* pawn, const bool always = false, const int secondsToFollow = 100);
     111            void followRandomHumanInit();
    112112            void follow();
     113            void followForSlaves(const Vector3& target);
    113114
    114115            void setTargetPosition(const Vector3& target);
     
    122123            bool isCloseAtTarget(float distance) const;
    123124            bool isLookingAtTarget(float angle) const;
     125
     126            void targetDied();
    124127
    125128            static bool sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype); // hack
Note: See TracChangeset for help on using the changeset viewer.