Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

decided to get rid of action timer and call action in tick instead: action is being called once in 2 sec in a tick, 2 sec were split in 4 * 0.25 sec, with 0.25 sec gap for each member of division to call its action, resulted in significant speed up.

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