Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/campaignHS15/src/orxonox/controllers/SectionController.cc @ 10864

Last change on this file since 10864 was 10864, checked in by gania, 9 years ago

split up some code

File size: 11.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:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      Dominik Solenicki
26 *
27 */
28
29#include "SectionController.h"
30
31namespace orxonox
32{
33
34    RegisterClass(SectionController);
35
[10826]36    //Leaders share the fact that they have Wingmans
[10719]37    SectionController::SectionController(Context* context) : LeaderController(context)
38    {
39        RegisterObject(SectionController);
[10759]40        this->setFormationMode(FormationMode::FINGER4);
[10725]41
[10719]42        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&SectionController::action, this)));
[10725]43        this->myWingman_ = 0;
44        this->myDivisionLeader_ = 0;
[10759]45        this->rank_ = Rank::SECTIONLEADER;
[10851]46        this->bFirstAction_ = true;
[10826]47        //orxout(internal_error) << this << "Was created" << endl;
[10719]48
49    }
50   
51    SectionController::~SectionController()
52    {
[10859]53        // if (this->myWingman_)
54        // {
55        //     this->myWingman_->takeActionpoints(this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_);
56        // }
[10854]57       for (size_t i = 0; i < this->actionpoints_.size(); ++i)
58        {
59            if(this->actionpoints_[i])
60                this->actionpoints_[i]->destroy();
61        }
62        this->parsedActionpoints_.clear();
63        this->actionpoints_.clear();
[10725]64    }
[10826]65    void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
66    {
67        SUPER(SectionController, XMLPort, xmlelement, mode);
[10731]68
[10826]69        //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
70    }
71
72    //----in tick, move (or look) and shoot----
[10731]73    void SectionController::tick(float dt)
74    {
[10843]75        if (!this->isActive())
[10731]76            return;
[10843]77     
[10731]78       
79        SUPER(SectionController, tick, dt);
80    }
81
82    void SectionController::action()
83    {
[10859]84        if (!this || !this->getControllableEntity())
85            return;
[10851]86
[10826]87        //----If no leader, find one---- 
[10731]88        if (!myDivisionLeader_)
89        {
90            LeaderController* newDivisionLeader = findNewDivisionLeader();
91            this->myDivisionLeader_ = newDivisionLeader;
[10826]92
[10731]93        }
[10826]94        //----If have leader----
[10780]95        else
[10805]96        {
[10826]97        }
[10851]98        if (!myDivisionLeader_)
[10805]99        {
[10861]100           
[10864]101            ActionpointController::action();
[10861]102            if (!this || !this->getControllableEntity())
103                return;
104            if (!(this->parsedActionpoints_.empty() && this->loopActionpoints_.empty()))
105            {
106                if (this->myWingman_)
107                {
108                    this->myWingman_->takeActionpoints(this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_);
109                }   
110            }
[10854]111        }
112        else if (myDivisionLeader_)
113        {
114            switch (myDivisionLeader_->getAction())
[10805]115            {
[10858]116                case Action::FIGHT:
117                {
118                    if (!this->hasTarget())
119                    {
120                        this->chooseTarget();
121                    }
122                    break;
123                }
124                case Action::FIGHTALL:
125                {
126                    if (!this->hasTarget())
127                    {
128                        this->chooseTarget();
129                    }
130                    break;
131                }
132                case Action::ATTACK:
133                {
134                    if (!this->hasTarget())
135                    {
136                        this->chooseTarget();
137                    }
138                    break;
139                }
[10854]140                default:
[10805]141                {
[10854]142                    ControllableEntity* myEntity = this->getControllableEntity();
143                    Vector3 myPosition = myEntity->getWorldPosition();
144                    if (!this->myDivisionLeader_)
[10851]145                    {
[10854]146                        return;
[10851]147                    }
[10854]148                    ControllableEntity* leaderEntity = this->myDivisionLeader_->getControllableEntity();
149                    Quaternion orient = leaderEntity->getWorldOrientation();
150                    Vector3 leaderPosition = leaderEntity->getWorldPosition();
151
152                    Vector3 targetRelativePosition = getFormationPosition();
153                    if (!this->myDivisionLeader_)
[10851]154                    {
[10854]155                        return;
[10851]156                    }
[10854]157                    Vector3 targetAbsolutePosition = 
158                        (leaderPosition + (orient*WorldEntity::FRONT) * (leaderEntity->getVelocity().length()/5)
159                         + (orient* (targetRelativePosition)));
160               
161                    this->setAction (Action::FLY, targetAbsolutePosition, orient);
162                    if ((targetAbsolutePosition - myPosition).length() > this->tolerance_ * 1.5f)
[10851]163                    {
[10854]164                        this->boostControl();
[10851]165                    }
166                    else
167                    {
[10854]168                       this->getControllableEntity()->boost(false);
[10851]169                    }
170                }
[10832]171            }
[10858]172            if (this->hasTarget())
173            {
174                //----choose where to go----
175                this->maneuver();
176                //----fire if you can----
177                this->bShooting_ = this->canFire();               
178            }
[10854]179        }
[10851]180     
[10854]181    }
[10851]182
[10856]183   
[10832]184    //PRE: myDivisionLeader_ != 0 && myDivisionLeader_->action_ == Action::FIGHT
[10826]185    //POST: this->target_ is set unless division leader doesn't have one
186    void SectionController::chooseTarget()
187    {
188        //----If division leader fights, cover him by fighting emenies close to his target----
[10854]189        Action::Value action = this->myDivisionLeader_->getAction();
190       
191        Pawn* target;
192        if (action == Action::FIGHT || action == Action::FIGHTALL || action == Action::ATTACK)
[10826]193        {
194            //----if he has a target----
195            if (this->myDivisionLeader_->hasTarget())
196            {
197                //----try to find a new target if division leader has wingman (doing fine) and no good target already set----
198                if ( this->myDivisionLeader_->hasWingman() && 
199                    !( this->hasTarget() && this->getTarget() != this->myDivisionLeader_->getTarget() ) )
200                {
201
202                    bool foundTarget = false;
203                    //----new target should be close to division's target----
204                    Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition();
[10838]205                    Gametype* gt = this->getGametype();
[10826]206                    for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
207                    {
208                        //----is enemy?----
[10838]209                        if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) )
[10826]210                            continue;           
211                        //----in range?----
212                        if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 && 
213                            (*itP) != this->myDivisionLeader_->getTarget())
214                        {
215                            foundTarget = true;
[10854]216                            target =  (*itP);
[10826]217                            //orxout(internal_error) << "Found target" << endl;
218                            break; 
219                        }
220                    }
221                    //----no target? then attack same target as division leader----
222                    if (!foundTarget)
223                    {
[10854]224                        target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget());
[10826]225                    }
226                }
227                //----if division leader doesn't have a wingman, support his fire----
228                else
229                {
[10854]230                    target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget());
[10826]231                }
232            }
233            //----If he fights but doesn't have a target, wait for him to get one----
234            else
235            {
236
237            }
[10858]238            this->setTarget (orxonox_cast<ControllableEntity*>(target));
[10854]239        }
240        else
241        {
[10826]242        } 
243    }
[10854]244    Vector3 SectionController::getFormationPosition ()
245    {
246        this->setFormationMode( this->myDivisionLeader_->getFormationMode() );
247        Vector3* targetRelativePosition;
248        switch (this->formationMode_){
249            case FormationMode::WALL:
250            {
251                targetRelativePosition = new Vector3 (-400, 0, 0);   
252                break;
253            }
254            case FormationMode::FINGER4: 
255            {
256                targetRelativePosition = new Vector3 (-400, 0, 200);   
257                break;
258            }
259           
260            case FormationMode::DIAMOND: 
261            {
262                targetRelativePosition = new Vector3 (-400, 0, 200);                   
263                break;
264            }
265        }
266        return *targetRelativePosition;
267    }
[10826]268
[10856]269   
[10826]270
[10722]271    LeaderController* SectionController::findNewDivisionLeader()
[10719]272    {
273
274        if (!this->getControllableEntity())
[10722]275            return 0;
[10719]276
[10722]277        LeaderController* closestLeader = 0;
278        float minDistance =  std::numeric_limits<float>::infinity();
[10719]279        //go through all pawns
280        for (ObjectList<LeaderController>::iterator it = ObjectList<LeaderController>::begin(); it; ++it)
281        {
[10722]282            //0ptr or not DivisionController?
[10759]283            if (!(it) || !((it)->getRank() == Rank::DIVISIONLEADER) || !(it->getControllableEntity()))
[10722]284                continue;
[10719]285            //same team?
286            if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam()))
287                continue;
288
289            //is equal to this?
290            if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity())
291                continue;
292
[10826]293            float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity());
[10722]294           
295            if (distance < minDistance && !(it->hasFollower()))
296            {
297                closestLeader = *it;
298                minDistance = distance;
299            }
[10729]300         
[10719]301        }
[10722]302        if (closestLeader)
303        {
304            if (closestLeader->setFollower(this))
305                return closestLeader;
306        }
307        return 0;
[10719]308    }
[10864]309    bool SectionController::setWingman(ActionpointController* cwingman)
[10731]310    {
311        WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman);
[10719]312
[10731]313        if (!this->myWingman_)
[10719]314        {
[10731]315            this->myWingman_ = wingman;
316            return true;
[10719]317        }
[10731]318        else
319        {
320            return false;
321        }
[10729]322    }
323   
[10731]324    bool SectionController::hasWingman()
[10725]325    {
[10731]326        if (this->myWingman_)
327            return true;
328        else
329            return false;
[10719]330    }
331}
Note: See TracBrowser for help on using the repository browser.