Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/cpp11_v3/src/orxonox/controllers/SectionController.cc @ 11065

Last change on this file since 11065 was 11065, checked in by landauf, 8 years ago

added c++11 features to code that was added in presentationHS15

  • Property svn:eol-style set to native
File size: 9.3 KB
RevLine 
[10719]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
[10885]23 *      Gani Aliguzhinov
[10719]24 *   Co-authors:
[10885]25 *      ...
[10719]26 *
27 */
28
29#include "SectionController.h"
[10935]30
[10719]31namespace orxonox
32{
33
34    RegisterClass(SectionController);
35
[10826]36    //Leaders share the fact that they have Wingmans
[10886]37    SectionController::SectionController(Context* context) : ActionpointController(context)
[10719]38    {
39        RegisterObject(SectionController);
[10759]40        this->setFormationMode(FormationMode::FINGER4);
[10725]41
[11065]42        this->myWingman_ = nullptr;
43        this->myDivisionLeader_ = nullptr;
[10851]44        this->bFirstAction_ = true;
[10719]45
46    }
47   
48    SectionController::~SectionController()
49    {
[11065]50        for (WorldEntity* actionpoint : this->actionpoints_)
[10854]51        {
[11065]52            if (actionpoint)
53                actionpoint->destroy();
[10854]54        }
55        this->parsedActionpoints_.clear();
56        this->actionpoints_.clear();
[10725]57    }
[10826]58
[10731]59    void SectionController::action()
60    {
[10946]61        if (!this || !this->getControllableEntity() || !this->isActive())
[10859]62            return;
[10851]63
[10826]64        //----If no leader, find one---- 
[10731]65        if (!myDivisionLeader_)
66        {
[10885]67            ActionpointController* newDivisionLeader = findNewDivisionLeader();
[10925]68            if (!this || !this->getControllableEntity())
69                return;
70
[10731]71            this->myDivisionLeader_ = newDivisionLeader;
72        }
[10826]73        //----If have leader----
[10780]74        else
[10805]75        {
[10826]76        }
[10851]77        if (!myDivisionLeader_)
[10805]78        {
[10864]79            ActionpointController::action();
[10861]80            if (!this || !this->getControllableEntity())
81                return;
82            if (!(this->parsedActionpoints_.empty() && this->loopActionpoints_.empty()))
83            {
84                if (this->myWingman_)
85                {
86                    this->myWingman_->takeActionpoints(this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_);
87                }   
88            }
[10854]89        }
90        else if (myDivisionLeader_)
91        {
[10883]92            if (this->myDivisionLeader_->bKeepFormation_ || !(this->myDivisionLeader_->getAction() == Action::FIGHT
93                || this->myDivisionLeader_->getAction() == Action::FIGHTALL
94                || this->myDivisionLeader_->getAction() == Action::ATTACK))
[10805]95            {
[10886]96                this->keepFormation();
[10879]97            }
[10886]98            else if (!this->myDivisionLeader_->bKeepFormation_)
[10879]99            {
[10925]100                if (!this || !this->getControllableEntity())
101                    return;
102
[10886]103                if (!this->hasTarget())
[10883]104                {
[10913]105                    this->chooseTarget(); 
[10851]106                }
[10935]107
[10832]108            }
[10854]109        }
[10915]110
[10854]111    }
[10851]112
[10856]113   
[10832]114    //PRE: myDivisionLeader_ != 0 && myDivisionLeader_->action_ == Action::FIGHT
[10826]115    //POST: this->target_ is set unless division leader doesn't have one
116    void SectionController::chooseTarget()
117    {
118        //----If division leader fights, cover him by fighting emenies close to his target----
[11065]119        Action action = this->myDivisionLeader_->getAction();
[10923]120
[10854]121        if (action == Action::FIGHT || action == Action::FIGHTALL || action == Action::ATTACK)
[10826]122        {
[10923]123            Pawn* target;
[10826]124            //----if he has a target----
125            if (this->myDivisionLeader_->hasTarget())
126            {
127                //----try to find a new target if division leader has wingman (doing fine) and no good target already set----
128                if ( this->myDivisionLeader_->hasWingman() && 
129                    !( this->hasTarget() && this->getTarget() != this->myDivisionLeader_->getTarget() ) )
130                {
131                    bool foundTarget = false;
132                    //----new target should be close to division's target----
133                    Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition();
[10838]134                    Gametype* gt = this->getGametype();
[11065]135                    for (Pawn* pawn : ObjectList<Pawn>())
[10826]136                    {
137                        //----is enemy?----
[11065]138                        if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(pawn), gt) )
[10826]139                            continue;           
140                        //----in range?----
[11065]141                        if ((pawn->getWorldPosition() - divisionTargetPosition).length() < 3000 &&
142                            pawn != this->myDivisionLeader_->getTarget())
[10826]143                        {
144                            foundTarget = true;
[11065]145                            target = pawn;
[10826]146                            break; 
147                        }
148                    }
149                    //----no target? then attack same target as division leader----
150                    if (!foundTarget)
151                    {
[10854]152                        target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget());
[10826]153                    }
154                }
155                //----if division leader doesn't have a wingman, support his fire----
156                else
157                {
[10854]158                    target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget());
[10826]159                }
160            }
161            //----If he fights but doesn't have a target, wait for him to get one----
162            else
163            {
164
165            }
[10858]166            this->setTarget (orxonox_cast<ControllableEntity*>(target));
[10854]167        }
168        else
169        {
[10826]170        } 
171    }
[10854]172    Vector3 SectionController::getFormationPosition ()
173    {
174        this->setFormationMode( this->myDivisionLeader_->getFormationMode() );
[10883]175        this->spread_ = this->myDivisionLeader_->getSpread();
[10854]176        Vector3* targetRelativePosition;
177        switch (this->formationMode_){
178            case FormationMode::WALL:
179            {
[11030]180                targetRelativePosition = new Vector3 (-2.0f*this->spread_, 0, 0);   
[10854]181                break;
182            }
183            case FormationMode::FINGER4: 
184            {
[11030]185                targetRelativePosition = new Vector3 (-2.0f*this->spread_, 0, 1.0f*this->spread_);   
[10854]186                break;
187            }
188           
189            case FormationMode::DIAMOND: 
190            {
[11030]191                targetRelativePosition = new Vector3 (-2.0f*this->spread_, 0, 1.0f*this->spread_);
[10854]192                break;
193            }
194        }
[10880]195        Vector3 result = *targetRelativePosition;
196        delete targetRelativePosition;
197        return result;
[10854]198    }
[10826]199
[10886]200    void SectionController::keepFormation()
201    {
202        this->bKeepFormation_ = true;
203        ControllableEntity* leaderEntity = this->myDivisionLeader_->getControllableEntity();
204        Vector3 targetRelativePosition = this->getFormationPosition();
205        if (!leaderEntity)
206            return;
207        FlyingController::keepFormation(leaderEntity, targetRelativePosition);
208    }
[10826]209
[10885]210    ActionpointController* SectionController::findNewDivisionLeader()
[10719]211    {
212
213        if (!this->getControllableEntity())
[11065]214            return nullptr;
[10719]215
[11065]216        ActionpointController* closestLeader = nullptr;
[10722]217        float minDistance =  std::numeric_limits<float>::infinity();
[10719]218        //go through all pawns
[11065]219        for (ActionpointController* controller : ObjectList<ActionpointController>())
[10719]220        {
[10722]221            //0ptr or not DivisionController?
[11065]222            if (!controller || !(controller->getIdentifier()->getName() == "DivisionController") || !(controller->getControllableEntity()))
[10722]223                continue;
[10719]224            //same team?
[11065]225            if ((this->getControllableEntity()->getTeam() != controller->getControllableEntity()->getTeam()))
[10719]226                continue;
227
228            //is equal to this?
[11065]229            if (orxonox_cast<ControllableEntity*>(controller) == this->getControllableEntity())
[10719]230                continue;
231
[11065]232            float distance = CommonController::distance (controller->getControllableEntity(), this->getControllableEntity());
[10722]233           
[11065]234            if (distance < minDistance && !(controller->hasFollower()))
[10722]235            {
[11065]236                closestLeader = controller;
[10722]237                minDistance = distance;
238            }
[10729]239         
[10719]240        }
[10722]241        if (closestLeader)
242        {
243            if (closestLeader->setFollower(this))
244                return closestLeader;
245        }
[11065]246        return nullptr;
[10719]247    }
[10915]248
[10885]249    bool SectionController::setWingman(ActionpointController* newWingman)
[10731]250    {
[10719]251
[10731]252        if (!this->myWingman_)
[10719]253        {
[10877]254            this->myWingman_ = newWingman;
[10885]255            newWingman->takeActionpoints (this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_);
[10731]256            return true;
[10719]257        }
[10731]258        else
259        {
260            return false;
261        }
[10729]262    }
263   
[10731]264    bool SectionController::hasWingman()
[10725]265    {
[10731]266        if (this->myWingman_)
267            return true;
268        else
269            return false;
[10719]270    }
271}
Note: See TracBrowser for help on using the repository browser.