Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10910 was 10909, checked in by gania, 10 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: 8.9 KB
RevLine 
[10678]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
[10678]24 *   Co-authors:
[10885]25 *      ...
[10678]26 *
27 */
28
29#include "WingmanController.h"
30
31
32namespace orxonox
33{
34
35    RegisterClass(WingmanController);
[10729]36   
[10864]37    //ActionpointController contains all common functionality of AI Controllers
38    WingmanController::WingmanController(Context* context) : ActionpointController(context)
[10678]39    {
40        RegisterObject(WingmanController);
[10909]41        //this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&WingmanController::action, this)));
[10725]42        this->myLeader_ = 0;
[10851]43        this->bFirstAction_ = true;
44
[10678]45    }
46
47    WingmanController::~WingmanController()
48    {
[10854]49        for (size_t i = 0; i < this->actionpoints_.size(); ++i)
50        {
51            if(this->actionpoints_[i])
52                this->actionpoints_[i]->destroy();
53        }
54        this->parsedActionpoints_.clear();
55        this->actionpoints_.clear();
[10678]56    }
[10826]57 
58    void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
59    {
60        SUPER(WingmanController, XMLPort, xmlelement, mode);
61    }
62   
63    //----in tick, move (or look) and shoot----
[10731]64    void WingmanController::tick(float dt)
65    {   
66        if (!this->isActive())
[10886]67            return; 
[10731]68       
69        SUPER(WingmanController, tick, dt);
[10909]70        if (this->timeOffset_ >= this->actionTime_ && this->timeOffset_ <= this->actionTime_ + 0.5f && !this->bActionCalled_)
71        {
72            this->action();
73            this->bActionCalled_ = true;
74        }
75        if (this->timeOffset_ <= 0.5f)
76        {
77            this->bActionCalled_ = false;
78        }
[10731]79    }
80   
[10826]81    //----action for hard calculations----
[10731]82    void WingmanController::action()
83    {
[10851]84
[10826]85        //----If no leader, find one----
[10731]86        if (!this->myLeader_)
87        {
[10877]88            ActionpointController* newLeader = (findNewLeader());
[10731]89            this->myLeader_ = newLeader;
[10879]90            if (this->myLeader_)
91            {
[10881]92                //spread copyOrientation called equally among the division
93                if (this->myLeader_->getIdentifier()->getName() == "SectionController")
[10885]94                    this->actionCounter_ = 1;
[10881]95                else
[10885]96                    this->actionCounter_ = 4;
[10879]97            }
[10731]98        }
[10826]99        //----If have leader, he will deal with logic----
[10731]100        else
101        {
[10851]102
[10731]103        }
[10851]104        if (!this->myLeader_)
105        {
[10864]106           ActionpointController::action();
[10805]107        }
[10854]108        else if (this->myLeader_)
[10805]109        {
[10883]110            if (this->myLeader_->bKeepFormation_ || !(this->myLeader_->getAction() == Action::FIGHT || this->myLeader_->getAction() == Action::FIGHTALL
111                || this->myLeader_->getAction() == Action::ATTACK))
[10856]112            {
[10886]113                this->keepFormation();
[10879]114            }
[10886]115            else if (!this->myLeader_->bKeepFormation_)
[10879]116            {
[10886]117                if (!this->hasTarget())
[10883]118                {
[10886]119                    this->setTarget(this->myLeader_->getTarget());
[10856]120                }
[10883]121                if (this->hasTarget())
122                {
[10906]123                    // this->maneuver();
124                    // this->bShooting_ = this->canFire();
[10888]125                    // Vector3 healthPosition = bestHealthPickup((this->target_->getWorldPosition() - this->getControllableEntity()->getWorldPosition()).length());
126                    // if ((this->getControllableEntity()->getWorldPosition() - healthPosition).length() < this->tolerance_)
127                    // {
128                    //     //----choose where to go----
129                    //     this->maneuver();
130                    // }
131                    // else
132                    // {
133                    //     this->dodgeTowards(healthPosition);
134                    // }
135                    // //----fire if you can----
136                    // this->bShooting_ = this->canFire();               
[10883]137                }
[10856]138            }
[10805]139        }
[10881]140        this->actionCounter_ += this->actionCounter_ < 100000 ? 1 : -this->actionCounter_ ;
[10731]141    }
142     
143   
[10856]144    Vector3 WingmanController::getFormationPosition ()
145    {
146        this->setFormationMode( this->myLeader_->getFormationMode() );
147        Vector3* targetRelativePosition;
[10883]148        this->spread_ = this->myLeader_->getSpread();
[10869]149        if (this->myLeader_->getIdentifier()->getName() == "DivisionController")
[10856]150        {
151            switch (this->formationMode_){
152                case FormationMode::WALL:
153                {
[10879]154                    targetRelativePosition = new Vector3 (2*this->spread_, 0, 0 - this->tolerance_); 
[10856]155                    break;
156                }
157                case FormationMode::FINGER4: 
158                {
[10879]159                    targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_); 
[10856]160                    break;
161                }
162                case FormationMode::DIAMOND: 
163                {
[10879]164                    targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_);                 
[10856]165                    break;
166                }
167            }
168        }
169        else
170        {
[10859]171
[10856]172            switch (this->formationMode_){
173                case FormationMode::WALL:
174                {
[10879]175                    targetRelativePosition = new Vector3 (-2*this->spread_, 0, 0 - this->tolerance_); 
[10856]176                    break;
177                }
178                case FormationMode::FINGER4: 
179                {
[10879]180                    targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_ - this->tolerance_); 
[10856]181                    break;
182                }
183                case FormationMode::DIAMOND: 
184                {
[10879]185                    targetRelativePosition = new Vector3 (2*this->spread_, -this->spread_, 0 - this->tolerance_);                 
[10856]186                    break;
187                }
188            }
189        }
[10880]190        Vector3 result = *targetRelativePosition;
191        delete targetRelativePosition;
192        return result;
[10856]193    }
[10886]194    void WingmanController::keepFormation()
195    {
196        this->bKeepFormation_ = true;
197        ControllableEntity* leaderEntity = this->myLeader_->getControllableEntity();
198        Vector3 targetRelativePosition = this->getFormationPosition();
199        if (!leaderEntity)
200            return;
201        FlyingController::keepFormation (leaderEntity, targetRelativePosition);
202    }
[10826]203    //----POST: closest leader that is ready to take a new wingman is returned----
[10877]204    ActionpointController* WingmanController::findNewLeader()
[10717]205    {
206
207        if (!this->getControllableEntity())
[10722]208            return 0;
[10717]209
[10826]210        //----vars for finding the closest leader----
[10877]211        ActionpointController* closestLeader = 0;
[10722]212        float minDistance =  std::numeric_limits<float>::infinity();
[10838]213        Gametype* gt = this->getGametype();
[10877]214
215        for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>::begin(); it; ++it)
[10717]216        {
[10826]217            //----0ptr or not a leader or dead?----
[10731]218            if (!it || 
[10869]219                (it->getIdentifier()->getName() != "SectionController" && it->getIdentifier()->getName() != "DivisionController") || 
[10731]220                !(it->getControllableEntity()))
[10722]221                continue;
[10826]222           
223            //----same team?----
[10838]224            if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) )
[10717]225                continue;
[10826]226           
227            //----check distance----
228            float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity());
[10722]229            if (distance < minDistance && !(it->hasWingman()))
230            {
231                closestLeader = *it;
232                minDistance = distance;
233            }
[10717]234        }
[10722]235        if (closestLeader)
236        {
[10826]237            //----Racing conditions----
[10877]238            if (closestLeader->setWingman(orxonox_cast<ActionpointController*>(this)))
239            {
[10909]240                if (closestLeader->getIdentifier()->getName() == "SectionController")
241                {
242                    this->actionTime_ = 1.0f;
243                }
244                else
245                {
246                    this->actionTime_ = 1.5f;
247                }
[10722]248                return closestLeader;
[10877]249            }
[10722]250        }
251        return 0;
[10717]252    }
[10722]253
[10678]254}
Note: See TracBrowser for help on using the repository browser.