Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/controllers/SectionController.cc @ 11075

Last change on this file since 11075 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 8.9 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
[11071]42        this->myWingman_ = nullptr;
43        this->myDivisionLeader_ = nullptr;
[10851]44        this->bFirstAction_ = true;
[10719]45
46    }
47   
48    SectionController::~SectionController()
49    {
[11071]50        for (WorldEntity* actionpoint : this->actionpoints_)
[10854]51        {
[11071]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----
[11071]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();
[11071]135                    for (Pawn* pawn : ObjectList<Pawn>())
[10826]136                    {
137                        //----is enemy?----
[11071]138                        if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(pawn), gt) )
[10826]139                            continue;           
140                        //----in range?----
[11071]141                        if ((pawn->getWorldPosition() - divisionTargetPosition).length() < 3000 &&
142                            pawn != this->myDivisionLeader_->getTarget())
[10826]143                        {
144                            foundTarget = true;
[11071]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        switch (this->formationMode_){
177            case FormationMode::WALL:
[11071]178                return Vector3 (-2.0f*this->spread_, 0, 0);
179
[10854]180            case FormationMode::FINGER4: 
[11071]181                return Vector3 (-2.0f*this->spread_, 0, 1.0f*this->spread_);
[10854]182           
183            case FormationMode::DIAMOND: 
[11071]184                return Vector3 (-2.0f*this->spread_, 0, 1.0f*this->spread_);
185
186            default:
187                return Vector3::ZERO;
[10854]188        }
189    }
[10826]190
[10886]191    void SectionController::keepFormation()
192    {
193        this->bKeepFormation_ = true;
194        ControllableEntity* leaderEntity = this->myDivisionLeader_->getControllableEntity();
195        Vector3 targetRelativePosition = this->getFormationPosition();
196        if (!leaderEntity)
197            return;
198        FlyingController::keepFormation(leaderEntity, targetRelativePosition);
199    }
[10826]200
[10885]201    ActionpointController* SectionController::findNewDivisionLeader()
[10719]202    {
203
204        if (!this->getControllableEntity())
[11071]205            return nullptr;
[10719]206
[11071]207        ActionpointController* closestLeader = nullptr;
[10722]208        float minDistance =  std::numeric_limits<float>::infinity();
[10719]209        //go through all pawns
[11071]210        for (ActionpointController* controller : ObjectList<ActionpointController>())
[10719]211        {
[10722]212            //0ptr or not DivisionController?
[11071]213            if (!controller || !(controller->getIdentifier()->getName() == "DivisionController") || !(controller->getControllableEntity()))
[10722]214                continue;
[10719]215            //same team?
[11071]216            if ((this->getControllableEntity()->getTeam() != controller->getControllableEntity()->getTeam()))
[10719]217                continue;
218
219            //is equal to this?
[11071]220            if (orxonox_cast<ControllableEntity*>(controller) == this->getControllableEntity())
[10719]221                continue;
222
[11071]223            float distance = CommonController::distance (controller->getControllableEntity(), this->getControllableEntity());
[10722]224           
[11071]225            if (distance < minDistance && !(controller->hasFollower()))
[10722]226            {
[11071]227                closestLeader = controller;
[10722]228                minDistance = distance;
229            }
[10729]230         
[10719]231        }
[10722]232        if (closestLeader)
233        {
234            if (closestLeader->setFollower(this))
235                return closestLeader;
236        }
[11071]237        return nullptr;
[10719]238    }
[10915]239
[10885]240    bool SectionController::setWingman(ActionpointController* newWingman)
[10731]241    {
[10719]242
[10731]243        if (!this->myWingman_)
[10719]244        {
[10877]245            this->myWingman_ = newWingman;
[10885]246            newWingman->takeActionpoints (this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_);
[10731]247            return true;
[10719]248        }
[10731]249        else
250        {
251            return false;
252        }
[10729]253    }
254   
[10731]255    bool SectionController::hasWingman()
[10725]256    {
[10731]257        if (this->myWingman_)
258            return true;
259        else
260            return false;
[10719]261    }
262}
Note: See TracBrowser for help on using the repository browser.