Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

little fix

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