Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10717


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

Wingmen and Leaders look for their leaders

Location:
code/branches/AI_HS15
Files:
2 deleted
14 edited

Legend:

Unmodified
Added
Removed
  • code/branches/AI_HS15/data/levels/AITest.oxw

    r10709 r10717  
    7878      </templates>
    7979      <controller>
    80         <WingmanController team=1>
    81         </WingmanController>
     80        <DivisionController team=1>
     81        </DivisionController>
    8282      </controller>
    8383    </SpaceShip>
     
    9191      </controller>
    9292    </SpaceShip>
     93    <SpaceShip position="1000, 1000, -1500 ?>" lookat="0,0,0">
     94      <templates>
     95        <Template link=spaceshipassff />
     96      </templates>
     97      <controller>
     98        <LeaderController team=1>
     99        </LeaderController>
     100      </controller>
     101    </SpaceShip>
     102    <SpaceShip position="1000, 1000, -1900 ?>" lookat="0,0,0">
     103      <templates>
     104        <Template link=spaceshipassff />
     105      </templates>
     106      <controller>
     107        <WingmanController team=1>
     108        </WingmanController>
     109      </controller>
     110    </SpaceShip>
     111    <!-->   
    93112    <!-->
    94113    <?lua
  • 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
  • code/branches/AI_HS15/src/orxonox/controllers/AIController.h

    r10678 r10717  
    3030#define _AIController_H__
    3131
    32 #include "OrxonoxPrereqs.h" 
     32#include "OrxonoxPrereqs.h"
    3333
    34 #include "util/Math.h"
    3534#include "tools/Timer.h"
    3635#include "tools/interfaces/Tickable.h"
    3736#include "ArtificialController.h"
    38 
    3937
    4038namespace orxonox
     
    5654
    5755            Timer actionTimer_; //<! Regularly calls action().
    58             std::vector<WeakPtr<AIController> > enemies_, allies_;
    59            
    6056    };
    6157}
  • code/branches/AI_HS15/src/orxonox/controllers/ArtificialController.cc

    r10678 r10717  
    8282    void ArtificialController::changedControllableEntity()
    8383    {
    84         FormationController::changedControllableEntity(); // super
    85 
    8684        if (!this->getControllableEntity())
    8785            this->removeFromFormation();
  • code/branches/AI_HS15/src/orxonox/controllers/ArtificialController.h

    r10678 r10717  
    5353                { return this->botlevel_; }
    5454            static void setAllBotLevel(float level);
    55             //WAYPOINT FUNCTIONS`
     55            //WAYPOINT FUNCTIONS
    5656            void addWaypoint(WorldEntity* waypoint);
    5757            WorldEntity* getWaypoint(unsigned int index) const;
     
    7474
    7575            float botlevel_; //<! Makes the level of a bot configurable.
    76             //botLevel_ is never used in XML (so far), is it redundant?
    7776            enum Mode {DEFAULT, ROCKET, DEFENCE, MOVING};//TODO; implement DEFENCE, MOVING modes
    7877            Mode mode_; //TODO: replace single value with stack-like implementation: std::vector<Mode> mode_;
  • code/branches/AI_HS15/src/orxonox/controllers/DivisionController.cc

    r10709 r10717  
    3838    {
    3939        RegisterObject(DivisionController);
     40        bIsDivisionLeader_ = true;
    4041    }
    4142
     
    4748        }
    4849    }
    49     void DivisionController::setLeader(LeaderController* leader)
     50    bool DivisionController::setFollower(LeaderController* myFollower)
    5051    {
    51         this->leader_ = leader;
     52         if (!this->myFollower_)
     53        {
     54            this->myFollower_ = myFollower;
     55            return true;
     56        }
     57        else
     58        {
     59            return false;
     60        }
    5261    }
    5362    /*void DivisionController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
  • code/branches/AI_HS15/src/orxonox/controllers/DivisionController.h

    r10709 r10717  
    3333
    3434
     35#include "util/Math.h"
     36#include "tools/Timer.h"
     37#include "tools/interfaces/Tickable.h"
    3538
    3639namespace orxonox
     
    4952                inline FormationMode getFormationMode() const
    5053                        { return this->formationMode_; }
    51                         virtual void setLeader(LeaderController* leader);
     54                        virtual bool setFollower(LeaderController* myFollower);
    5255
    5356                       
     
    6366                        FormationMode formationMode_;
    6467                       
    65                         /*void setTargetPosition(const Vector3& target);
    66                         void searchRandomTargetPosition();
    67 
    68                         void setTargetOrientation(const Quaternion& orient);
    69                         void setTargetOrientation(Pawn* target);
    70 
    71                         virtual void positionReached() {}
    72 
    73 
    74 
    75                         void setTarget(Pawn* target);
    76 
    77                         void searchNewTarget();
    78                     void forgetTarget();
    79 
    80                         void targetDied();
    81                         bool bShooting_;
    82                         void aimAtTarget();
    83 
    84             bool isCloseAtTarget(float distance) const;
    85             bool isLookingAtTarget(float angle) const;
    86 
    87                 //Has nothing to do with desiredRelativePosition_,
    88                 //is set by fleet controller.
    89                 Vector3* desiredAbsolutePosition_;
    90 
    91                 enum Maneuver {NONE, SPIN, TURN180};
    92                 Maneuver maneuver_;
    93 
    94                 void moveToPosition(const Vector3& target);
    95                     void moveToTargetPosition();
    96                     void absoluteMoveToPosition(const Vector3& target);
    97                     void copyOrientation(const Quaternion& orient);
    98                     void copyTargetOrientation();
    99 
    100                         void turn180Init();
    101                     void spinInit();
    102                     void spin();
    103                 void turn180();*/
    104 
    105                 LeaderController* leader_;
     68                       
     69                LeaderController* myFollower_;
    10670               
    10771
  • code/branches/AI_HS15/src/orxonox/controllers/FleetController.cc

    r10709 r10717  
    6363    {
    6464
    65          if (nTicks_ == 30)
     65         /*if (nTicks_ == 30)
    6666         {
    6767            std::vector<WeakPtr<WingmanController> > wingmen;
     
    143143            nTicks_ += 1;
    144144
    145          }
     145         }*/
    146146        SUPER(FleetController, tick, dt);
    147147
  • code/branches/AI_HS15/src/orxonox/controllers/FormationController.cc

    r10678 r10717  
    105105                        orxout(internal_error) << this << " is still master in " << (*it) << endl;
    106106                        it->myMaster_ = 0;
    107                         it->state_ = FREE;
    108107                    }
    109108
     
    278277        }
    279278
    280         Vector2 coord = get2DViewCoordinates(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
     279        Vector2 coord = get2DViewcoordinates(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
    281280        float distance = (target - this->getControllableEntity()->getPosition()).length();
    282281        float rotateX = clamp(coord.x * 10, -1.0f, 1.0f);
     
    473472        Vector3 dest = this->getControllableEntity()->getPosition();
    474473
    475         // only 1 slave: follow
     474        // 1 slave: follow
    476475        if (this->slaves_.size() == 1)
    477476        {
     
    703702        }
    704703    }
    705      /**
    706       @brief If master, set the desired relative position of slaves (Vector3) in respect to a master for all the slaves.
    707     */
    708     void FormationController::setDesiredPositionOfSlaves()
    709     {
    710         if (this->state_ != MASTER)
    711             return;
    712         switch (this->formationMode_){
    713             case ATTACK:
    714             {
    715                 float i = 0;
    716                 for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
    717                 {
    718                     (*it)->desiredRelativePosition_ = new Vector3 ((i-slaves_.size()/2)*200, 0, 0);
    719                     i++;
    720                 }
    721                 break;
    722             }
    723             case NORMAL:
    724             {
    725                 break;
    726             }
    727             case DEFEND:
    728             {
    729                 break;
    730             }
    731         }
    732        
    733     }
    734704
    735705    /**
     
    10991069            return;
    11001070
    1101         Vector2 coord = get2DViewCoordinates(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
     1071        Vector2 coord = get2DViewcoordinates(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
    11021072        float distance = (target - this->getControllableEntity()->getPosition()).length();
    11031073
     
    11171087    }
    11181088
    1119     void FormationController::changedControllableEntity()
    1120     {
    1121         Controller::changedControllableEntity(); // super
    1122 
    1123         // when changing the controllable entity, ensure that the controller does not use the new entity as target
    1124         if (this->target_ && this->getControllableEntity() == static_cast<ControllableEntity*>(this->target_))
    1125             this->forgetTarget();
    1126     }
    11271089}
  • code/branches/AI_HS15/src/orxonox/controllers/FormationController.h

    r10678 r10717  
    7070           { return this->maxFormationSize_; }
    7171
    72       /**
    73       @brief If master, set the desired position of slaves (Vector2) for all the slaves.
    74       */
    75       void setDesiredPositionOfSlaves();
    7672
    7773      inline void setPassive(bool passive)
     
    9692      inline FormationMode getFormationMode() const
    9793           { return this->formationMode_; }
    98      
     94
    9995      virtual void hit(Pawn* originator, btManifoldPoint& contactpoint, float damage);
    10096
     
    10399      FormationController* getSlave( void ) { return this->slaves_.back(); }
    104100
    105       virtual void changedControllableEntity();
    106 
    107101  protected:
    108       Vector3* desiredRelativePosition_;
    109102      bool formationFlight_;
    110103      bool passive_;
     
    185178#endif /* _FormationController_h__ */
    186179
     180
  • code/branches/AI_HS15/src/orxonox/controllers/LeaderController.cc

    r10709 r10717  
    3030
    3131
    32 
    3332namespace orxonox
    3433{
     
    3635    RegisterClass(LeaderController);
    3736
    38     LeaderController::LeaderController(Context* context) : WingmanController(context)
     37    static const int RADIUS_TO_SEARCH_FOR_LEADER = 3000;
     38
     39    LeaderController::LeaderController(Context* context) : CommonController(context)
    3940    {
    4041
    4142        RegisterObject(LeaderController);
     43        bIsDivisionLeader_ = false;
     44
    4245        //this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&LeaderController::action, this)));
    4346    }
     
    4851    }
    4952
     53    LeaderController* LeaderController::findNewDivisionLeader()
     54    {
     55
     56        if (!this->getControllableEntity())
     57            return NULL;
     58
     59       
     60        //go through all pawns
     61        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
     62        {
     63
     64            //same team?
     65            if (!(this->getControllableEntity()->getTeam() != static_cast<ControllableEntity*>(*it)->getTeam()))
     66                continue;
     67
     68            //Does it have a Controller?
     69            Controller* controller = 0;
     70
     71            if (it->getController())
     72                controller = it->getController();
     73            else if (it->getXMLController())
     74                controller = it->getXMLController();
     75
     76            if (!controller)
     77                continue;
     78
     79            //is equal to this?
     80            if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity())
     81                continue;
     82
     83
     84            LeaderController* newLeader = orxonox_cast<LeaderController*>(controller);
     85
     86            //nullptr or not DivisionController?
     87            if (!newLeader || !newLeader->bIsDivisionLeader_)
     88                continue;
     89
     90            float distance = (it->getPosition() - this->getControllableEntity()->getPosition()).length();
     91
     92            // is pawn in range?
     93            if (distance < RADIUS_TO_SEARCH_FOR_LEADER)
     94            {
     95
     96                if (newLeader->setFollower(this))
     97                    return newLeader;
     98            }
     99        }
     100                return NULL;
     101
     102    }
    50103    void LeaderController::action()
    51104    {
    52105        //this->target_ = this->sectionTarget_;       
     106        if (!myDivisionLeader_)
     107        {
     108            LeaderController* newDivisionLeader = findNewDivisionLeader();
     109            myDivisionLeader_ = newDivisionLeader;
     110            orxout(internal_error) << "new DivisionLeader set" << endl;
     111        }
    53112    }
    54113    /*
     
    94153        }*/
    95154
    96         orxout(internal_error) << "my Wingman is " << this->wingman_ << endl;
     155        orxout(internal_error) << "my Wingman is " << this->myWingman_ << endl;
    97156       
    98157        SUPER(LeaderController, tick, dt);
    99158    }
    100     void LeaderController::setWingman(WingmanController* wingman)
     159    bool LeaderController::setWingman(WingmanController* wingman)
    101160    {
    102         this->wingman_ = wingman;
     161        if (!this->myWingman_)
     162        {
     163            this->myWingman_ = wingman;
     164            return true;
     165        }
     166        else
     167        {
     168            return false;
     169        }
     170    }
     171    bool LeaderController::isLeader()
     172    {
     173        return true;
    103174    }
    104175//**********************************************NEW
  • code/branches/AI_HS15/src/orxonox/controllers/LeaderController.h

    r10709 r10717  
    3030#define _LeaderController_H__
    3131
     32#include "controllers/CommonController.h"
    3233#include "controllers/WingmanController.h"
    3334
    3435
    35 
     36#include "util/Math.h"
     37#include "tools/Timer.h"
     38#include "tools/interfaces/Tickable.h"
    3639
    3740namespace orxonox
    3841{
    39     class _OrxonoxExport LeaderController : public WingmanController
     42    class _OrxonoxExport LeaderController : public CommonController, public Tickable
    4043    {
    4144        public:
     
    4346            LeaderController(Context* context);
    4447            virtual ~LeaderController();
    45             virtual void setWingman(WingmanController* wingman);
     48            virtual bool isLeader();
     49
     50            virtual bool setWingman(WingmanController* wingman);
    4651            virtual void tick(float dt); //<! Carrying out the targets set in action().
    4752
    4853        protected:
    4954           
    50            
     55            LeaderController* findNewDivisionLeader();
     56
     57            bool bIsDivisionLeader_;
    5158
    5259            virtual void action(); //<! action() is called in regular intervals managing the bot's behaviour ~ setting targets.
     
    5764            WeakPtr<Pawn> target_;
    5865           
    59             WingmanController* wingman_;
    60 
     66            WingmanController* myWingman_;
     67            LeaderController* myDivisionLeader_;
    6168            //Timer actionTimer_; //<! Regularly calls action().
    6269               
  • code/branches/AI_HS15/src/orxonox/controllers/WingmanController.cc

    r10709 r10717  
    2929#include "WingmanController.h"
    3030
     31#include "core/CoreIncludes.h"
     32
     33#include "core/XMLPort.h"
     34#include "core/command/ConsoleCommandIncludes.h"
     35
     36#include "worldentities/ControllableEntity.h"
     37#include "worldentities/pawns/Pawn.h"
    3138
    3239namespace orxonox
     
    3441
    3542    RegisterClass(WingmanController);
    36 
    37     WingmanController::WingmanController(Context* context) : Controller(context)
     43    static const int RADIUS_TO_SEARCH_FOR_LEADER = 3000;
     44
     45    WingmanController::WingmanController(Context* context) : CommonController(context)
    3846    {
    3947        RegisterObject(WingmanController);
     
    5260        XMLPortObject(WingmanController, WorldEntity, "waypoints", addWaypoint, getWaypoint,  xmlelement, mode);
    5361    }*/
     62    CommonController* WingmanController::findNewLeader()
     63    {
     64
     65        if (!this->getControllableEntity())
     66            return NULL;
     67
     68       
     69        //go through all pawns
     70        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
     71        {
     72
     73            //same team?
     74            if (!(this->getControllableEntity()->getTeam() != static_cast<ControllableEntity*>(*it)->getTeam()))
     75                continue;
     76
     77            //Does it have a Controller?
     78            Controller* controller = 0;
     79
     80            if (it->getController())
     81                controller = it->getController();
     82            else if (it->getXMLController())
     83                controller = it->getXMLController();
     84
     85            if (!controller)
     86                continue;
     87
     88            //is equal to this?
     89            if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity())
     90                continue;
     91
     92
     93            CommonController* newLeader = orxonox_cast<CommonController*>(controller);
     94
     95            //nullptr?
     96            if (!newLeader || !newLeader->isLeader())
     97                continue;
     98
     99            float distance = (it->getPosition() - this->getControllableEntity()->getPosition()).length();
     100
     101            // is pawn in range?
     102            if (distance < RADIUS_TO_SEARCH_FOR_LEADER)
     103            {
     104
     105                if (newLeader->setWingman(this))
     106                    return newLeader;
     107            }
     108        }
     109        return NULL;
     110    }
     111    bool WingmanController::isLeader()
     112    {
     113        return false;
     114    }
    54115    void WingmanController::action()
    55116    {
    56117        //this->target_ = this->sectionTarget_;
     118        if (!myLeader_)
     119        {
     120            CommonController* newLeader = findNewLeader();
     121            myLeader_ = newLeader;
     122            orxout(internal_error) << "new Leader set" << endl;
     123        }
    57124    }
    58125
    59126    void WingmanController::tick(float dt)
    60     {/*
     127    {   
     128        //-------------------------------------------------------
     129            /*//collect data for AI behaviour
     130            Vector3* meanOfEnemiesPtr = new Vector3(0.0,0.0,0.0);
     131            Vector3* meanOfAlliesPtr  = new Vector3(0.0,0.0,0.0);
     132            Vector3 meanOfAllies = *meanOfAlliesPtr;
     133            Vector3 meanOfEnemies = *meanOfEnemiesPtr;
     134
     135
     136            for (ObjectList<AIController>::iterator it = ObjectList<AIController>::begin(); it; ++it)
     137            {
     138
     139                Gametype* gt=this->getGametype();
     140                if (!gt)
     141                {
     142                    gt=it->getGametype();
     143                }
     144                if (!FormationController::sameTeam(this->getControllableEntity(), it->getControllableEntity(),gt))
     145                {
     146                    enemies_.push_back(*it);
     147                }
     148                else {
     149                    allies_.push_back(*it);
     150                }
     151            }
     152            if (enemies_.size() != 0 && allies_.size() != 0){
     153                for (std::vector<WeakPtr<AIController> >::iterator it = enemies_.begin() ; it != enemies_.end(); ++it)
     154                    meanOfEnemies += (*it)->getControllableEntity()->getWorldPosition();
     155
     156                meanOfEnemies /= enemies_.size();
     157
     158                for (std::vector<WeakPtr<AIController> >::iterator it = allies_.begin() ; it != allies_.end(); ++it)
     159                    meanOfAllies += (*it)->getControllableEntity()->getWorldPosition();
     160
     161                meanOfAllies /= allies_.size();
     162
     163                //orxout(internal_error) << "There are " << enemies_Counter << " enemies_, mean position is " << meanOfEnemies << endl;
     164                orxout(internal_error) << "Distance is " << (meanOfEnemies-meanOfAllies).length() << endl;
     165                orxout(internal_error) << "mean of allies_ is " << meanOfAllies << ", with a size " << allies_.size() << endl;
     166                orxout(internal_error) << "mean of enemies_ is " << meanOfEnemies << ", with a size " << enemies_.size() << endl;
     167            }*/
     168    /*
    61169        if (!this->isActive())
    62170            return;
     
    83191        }*/
    84192         //orxout(internal_error) << "I am " << this << endl;
     193
     194       /* void FormationController::setDesiredPositionOfSlaves()
     195    {
     196        if (this->state_ != MASTER)
     197            return;
     198        switch (this->formationMode_){
     199            case ATTACK:
     200            {
     201                float i = 0;
     202                for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
     203                {
     204                    (*it)->desiredRelativePosition_ = new Vector3 ((i-slaves_.size()/2)*200, 0, 0);
     205                    i++;
     206                }
     207                break;
     208            }
     209            case NORMAL:
     210            {
     211                break;
     212            }
     213            case DEFEND:
     214            {
     215                break;
     216            }
     217        }
     218       
     219    }*/
    85220       
    86221        SUPER(WingmanController, tick, dt);
  • code/branches/AI_HS15/src/orxonox/controllers/WingmanController.h

    r10709 r10717  
    3131
    3232
    33 #include "controllers/Controller.h"
     33#include "controllers/CommonController.h"
    3434
    3535
    36 #include "OrxonoxPrereqs.h"
    37 #include "core/class/Super.h"
    38 #include "core/CoreIncludes.h"
    39 #include "core/XMLPort.h"
    40 #include "core/command/ConsoleCommandIncludes.h"
    41 #include "core/command/Executor.h"
    42 
    43 #include <vector>
    4436#include "util/Math.h"
    45 #include <climits>
    46 
    4737#include "tools/Timer.h"
    4838#include "tools/interfaces/Tickable.h"
    49 
    50 #include "worldentities/ControllableEntity.h"
    51 #include "worldentities/pawns/SpaceShip.h"
    52 #include "worldentities/pawns/Pawn.h"
    53 #include "worldentities/pawns/TeamBaseMatchBase.h"
    54 
    55 #include "gametypes/TeamDeathmatch.h"
    56 #include "gametypes/Dynamicmatch.h"
    57 #include "gametypes/Mission.h"
    58 #include "gametypes/Gametype.h"
    59 
    6039
    6140
    6241namespace orxonox
    6342{
    64     class _OrxonoxExport WingmanController : public Controller, public Tickable
     43    class _OrxonoxExport WingmanController : public CommonController, public Tickable
    6544    {
    6645        public:
    6746            WingmanController(Context* context);
    6847            virtual ~WingmanController();
    69             //virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
     48            virtual bool isLeader();
     49          //virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    7050            virtual void tick(float dt); //<! Carrying out the targets set in action().
    7151
    7252        protected:
     53            CommonController* findNewLeader();
     54
    7355            virtual void action(); //<! action() is called in regular intervals managing the bot's behaviour ~ setting targets.
    7456            //void defaultBehaviour(float maxrand); //<! Helper function for code reusage. Some concrete commands for a bot.
     
    8062           
    8163            WeakPtr<Pawn> target_;
    82            
     64            CommonController* myLeader_;
    8365            //LeaderController* leader_;
    8466
Note: See TracChangeset for help on using the changeset viewer.