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/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
Note: See TracChangeset for help on using the changeset viewer.