Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 3, 2012, 6:05:24 PM (13 years ago)
Author:
landauf
Message:

replaced tabs with spaces (no code changed)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation2012merge/src/orxonox/controllers/FormationController.cc

    r9271 r9272  
    5050{
    5151
    52   SetConsoleCommand("FormationController", "formationflight",  &FormationController::formationflight);
    53   SetConsoleCommand("FormationController", "masteraction",     &FormationController::masteraction);
    54   SetConsoleCommand("FormationController", "followme",         &FormationController::followme);
    55   SetConsoleCommand("FormationController", "passivebehaviour", &FormationController::passivebehaviour);
    56   SetConsoleCommand("FormationController", "formationsize",    &FormationController::formationsize);
    57 
    58 
    59 
    60 
    61   static const unsigned int STANDARD_MAX_FORMATION_SIZE = 9;
    62   static const int RADIUS_TO_SEARCH_FOR_MASTERS = 5000;
    63   static const float FORMATION_LENGTH =  110;
    64   static const float FORMATION_WIDTH =  110;
    65   static const int FREEDOM_COUNT = 4; //seconds the slaves in a formation will be set free when master attacks an enemy
    66   static const float SPEED_MASTER = 0.6f;
    67   static const float ROTATEFACTOR_MASTER = 0.2f;
    68   static const float SPEED_FREE = 0.8f;
    69   static const float ROTATEFACTOR_FREE = 0.8f;
    70 
    71   FormationController::FormationController(BaseObject* creator) : Controller(creator)
    72   {
     52    SetConsoleCommand("FormationController", "formationflight",  &FormationController::formationflight);
     53    SetConsoleCommand("FormationController", "masteraction",     &FormationController::masteraction);
     54    SetConsoleCommand("FormationController", "followme",         &FormationController::followme);
     55    SetConsoleCommand("FormationController", "passivebehaviour", &FormationController::passivebehaviour);
     56    SetConsoleCommand("FormationController", "formationsize",    &FormationController::formationsize);
     57
     58
     59
     60
     61    static const unsigned int STANDARD_MAX_FORMATION_SIZE = 9;
     62    static const int RADIUS_TO_SEARCH_FOR_MASTERS = 5000;
     63    static const float FORMATION_LENGTH =  110;
     64    static const float FORMATION_WIDTH =  110;
     65    static const int FREEDOM_COUNT = 4; //seconds the slaves in a formation will be set free when master attacks an enemy
     66    static const float SPEED_MASTER = 0.6f;
     67    static const float ROTATEFACTOR_MASTER = 0.2f;
     68    static const float SPEED_FREE = 0.8f;
     69    static const float ROTATEFACTOR_FREE = 0.8f;
     70
     71    FormationController::FormationController(BaseObject* creator) : Controller(creator)
     72    {
    7373        RegisterObject(FormationController);
    7474
     
    9191        this->team_=-1;
    9292        this->target_.setCallback(createFunctor(&FormationController::targetDied, this));
    93   }
    94 
    95   FormationController::~FormationController()
    96   {
    97     if (this->isInitialized())
     93    }
     94
     95    FormationController::~FormationController()
     96    {
     97        if (this->isInitialized())
    9898        {
    9999            this->removeFromFormation();
     
    123123            }
    124124        }
    125   }
    126 
    127   void FormationController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
     125    }
     126
     127    void FormationController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
    128128    {
    129129        SUPER(FormationController, XMLPort, xmlelement, mode);
     
    137137
    138138
    139   /**
     139    /**
    140140        @brief Activates / deactivates formationflight behaviour
    141141        @param form activate formflight if form is true
    142142    */
    143   void FormationController::formationflight(const bool form)
     143    void FormationController::formationflight(const bool form)
    144144    {
    145145        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
     
    168168    }
    169169
    170   /**
     170    /**
    171171        @brief Get all masters to do a "specific master action"
    172172        @param action which action to perform (integer, so it can be called with a console command (tmp solution))
     
    198198    }
    199199
    200   /**
     200    /**
    201201        @brief Sets shooting behaviour of pawns.
    202202        @param passive if true, bots won't shoot.
     
    225225    }
    226226
    227   /**
     227    /**
    228228        @brief Sets maximal formation size
    229229        @param size maximal formation size.
     
    282282        Vector2 coord = get2DViewcoordinates(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
    283283        float distance = (target - this->getControllableEntity()->getPosition()).length();
    284                 if(coord.x < 0.0001 && coord.y < 0.0001)
    285                 {
    286                         // if the ship reaches a direction very close to the direct one, set it to the direct one
    287                         Vector3 v_temp = this->getControllableEntity()->getPosition();
    288                         Quaternion quat = v_temp.getRotationTo(target);
    289                         this->getControllableEntity()->rotate(quat);
    290                 }
     284        if(coord.x < 0.0001 && coord.y < 0.0001)
     285        {
     286            // if the ship reaches a direction very close to the direct one, set it to the direct one
     287            Vector3 v_temp = this->getControllableEntity()->getPosition();
     288            Quaternion quat = v_temp.getRotationTo(target);
     289            this->getControllableEntity()->rotate(quat);
     290        }
    291291
    292292        if(this->state_ == FREE)
     
    324324
    325325        if(this->state_ == SLAVE)
    326                 {
    327 
    328                         this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR_MASTER * coord.x * 2);
    329                         this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR_MASTER * coord.y * 2);
    330 
    331                         if (distance < 300)
    332                         {
    333                                 if (bHasTargetOrientation_)
    334                                 {
    335                                         copyTargetOrientation();
    336                                 }
    337                                 if (distance < 100)
    338                                 { //linear speed reduction
    339                                         this->getControllableEntity()->moveFrontBack(distance/100.0f*0.4f*SPEED_MASTER);
    340 
    341                                 } else this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER);
    342 
    343                         } else {
    344                                 this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER + distance/300.0f);
    345                         }
    346                 }
     326        {
     327
     328            this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR_MASTER * coord.x * 2);
     329            this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR_MASTER * coord.y * 2);
     330
     331            if (distance < 300)
     332            {
     333                if (bHasTargetOrientation_)
     334                {
     335                    copyTargetOrientation();
     336                }
     337                if (distance < 100)
     338                { //linear speed reduction
     339                    this->getControllableEntity()->moveFrontBack(distance/100.0f*0.4f*SPEED_MASTER);
     340                }
     341                else
     342                    this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER);
     343            }
     344            else
     345                this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER + distance/300.0f);
     346        }
    347347
    348348        if (distance < 10)
    349349        {
    350350            this->positionReached();
    351             bHasTargetOrientation_=false;
    352         }
    353     }
    354 
    355 
    356 
    357   void FormationController::moveToTargetPosition()
     351            bHasTargetOrientation_=false;
     352        }
     353    }
     354
     355
     356
     357    void FormationController::moveToTargetPosition()
    358358    {
    359359        this->moveToPosition(this->targetPosition_);
    360360    }
    361361
    362   //copy the Roll orientation of given Quaternion.
    363   void FormationController::copyOrientation(const Quaternion& orient)
     362    //copy the Roll orientation of given Quaternion.
     363    void FormationController::copyOrientation(const Quaternion& orient)
    364364    {
    365365        //roll angle difference in radian
     
    379379
    380380
    381    /**
     381    /**
    382382        @brief Unregisters a slave from its master. Initiated by a slave.
    383383    */
     
    469469        }
    470470    }
    471  /**
     471
     472    /**
    472473        @brief Commands the slaves of a master into a formation. Sufficiently fast not to be called within tick. Initiated by a master.
    473474    */
    474 
    475 void FormationController::commandSlaves()
     475    void FormationController::commandSlaves()
    476476    {
    477477        if(this->state_ != MASTER) return;
     
    487487        }
    488488        else
    489         // formation:
     489        // formation:
    490490        {
    491491            dest += 1.0f*orient*WorldEntity::BACK;
    492492            Vector3 pos = Vector3::ZERO;
    493                  bool left=true;
     493            bool left=true;
    494494            int i = 1;
    495495
     
    689689
    690690        if (specificMasterActionHoldCount_ == 0)
    691          {
     691        {
    692692            this->specificMasterAction_ = NONE;
    693693            this->searchNewTarget();
    694          }
    695         else specificMasterActionHoldCount_--;
     694        }
     695        else
     696            specificMasterActionHoldCount_--;
    696697    }
    697698
     
    717718    void FormationController::turn180()
    718719    {
    719             Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->targetPosition_);
    720 
    721             this->getControllableEntity()->rotateYaw(-2.0f * sgn(coord.x) * coord.x*coord.x);
    722             this->getControllableEntity()->rotatePitch(2.0f * sgn(coord.y) * coord.y*coord.y);
    723 
    724             this->getControllableEntity()->moveFrontBack(SPEED_MASTER);
     720        Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->targetPosition_);
     721
     722        this->getControllableEntity()->rotateYaw(-2.0f * sgn(coord.x) * coord.x*coord.x);
     723        this->getControllableEntity()->rotatePitch(2.0f * sgn(coord.y) * coord.y*coord.y);
     724
     725        this->getControllableEntity()->moveFrontBack(SPEED_MASTER);
    725726    }
    726727
     
    740741    void FormationController::spin()
    741742    {
    742             this->moveToTargetPosition();
    743             this->getControllableEntity()->rotateRoll(0.8f);
     743        this->moveToTargetPosition();
     744        this->getControllableEntity()->rotateRoll(0.8f);
    744745    }
    745746
     
    779780        if((humanPawn != NULL) && (allMasters.size() != 0))
    780781        {
    781                 float posHuman = humanPawn->getPosition().length();
    782                 float distance = 0.0f;
    783                 float minDistance = FLT_MAX;
    784                 int index = 0;
    785                 int i = 0;
    786 
    787                 for(std::vector<FormationController*>::iterator it = allMasters.begin(); it != allMasters.end(); it++, i++)
    788                     {
    789                         if (!FormationController::sameTeam((*it)->getControllableEntity(), humanPawn, (*it)->getGametype())) continue;
    790                         distance = posHuman - (*it)->getControllableEntity()->getPosition().length();
    791                         if(distance < minDistance) index = i;
    792                     }
    793                 allMasters[index]->followInit(humanPawn);
    794             }
    795 
    796     }
    797 
    798 
    799 
    800 
     782            float posHuman = humanPawn->getPosition().length();
     783            float distance = 0.0f;
     784            float minDistance = FLT_MAX;
     785            int index = 0;
     786            int i = 0;
     787
     788            for(std::vector<FormationController*>::iterator it = allMasters.begin(); it != allMasters.end(); it++, i++)
     789            {
     790                if (!FormationController::sameTeam((*it)->getControllableEntity(), humanPawn, (*it)->getGametype())) continue;
     791                distance = posHuman - (*it)->getControllableEntity()->getPosition().length();
     792                if(distance < minDistance) index = i;
     793            }
     794            allMasters[index]->followInit(humanPawn);
     795        }
     796    }
    801797
    802798    /**
     
    820816    }
    821817
    822    /**
     818    /**
    823819        @brief Master begins to follow a randomly chosen human player of the same team. Is a "specific master action".
    824820    */
     
    848844
    849845
    850   /**
     846    /**
    851847        @brief Master follows target with adjusted speed. Called within tick.
    852848    */
     
    860856
    861857
    862   void FormationController::setTargetPosition(const Vector3& target)
     858    void FormationController::setTargetPosition(const Vector3& target)
    863859    {
    864860        this->targetPosition_ = target;
     
    924920    }
    925921
    926   void FormationController::forgetTarget()
     922    void FormationController::forgetTarget()
    927923    {
    928924        this->target_ = 0;
     
    930926    }
    931927
    932    void FormationController::targetDied()
     928    void FormationController::targetDied()
    933929    {
    934930        this->forgetTarget();
     
    936932    }
    937933
    938   bool FormationController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype)
     934    bool FormationController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype)
    939935    {
    940936        if (entity1 == entity2)
Note: See TracChangeset for help on using the changeset viewer.