Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10882 was 10881, checked in by gania, 10 years ago

finished fixing FlyingController: AI is flying as smooth as possible with minimum loss of performance

File size: 9.4 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:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      Dominik Solenicki
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);
[10719]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);
[10678]61
[10826]62        //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
63    }
64   
65    //----in tick, move (or look) and shoot----
[10731]66    void WingmanController::tick(float dt)
67    {   
68        if (!this->isActive())
69            return;
[10875]70        if (this->myLeader_ && this->myLeader_->getAction() != Action::FIGHT && this->myLeader_->getAction() !=
71            Action::FIGHTALL && this->myLeader_->getAction() != Action::ATTACK)
72        {
73        }   
[10731]74       
75        SUPER(WingmanController, tick, dt);
76    }
77   
[10826]78    //----action for hard calculations----
[10731]79    void WingmanController::action()
80    {
[10851]81
[10826]82        //----If no leader, find one----
[10731]83        if (!this->myLeader_)
84        {
[10877]85            ActionpointController* newLeader = (findNewLeader());
[10731]86            this->myLeader_ = newLeader;
[10879]87            if (this->myLeader_)
88            {
[10881]89                //spread copyOrientation called equally among the division
90                if (this->myLeader_->getIdentifier()->getName() == "SectionController")
91                    this->actionCounter_ = 2;
92                else
93                    this->actionCounter_ = 5;
[10879]94            }
[10731]95        }
[10826]96        //----If have leader, he will deal with logic----
[10731]97        else
98        {
[10851]99
[10731]100        }
[10851]101        if (!this->myLeader_)
102        {
[10864]103           ActionpointController::action();
[10805]104        }
[10854]105        else if (this->myLeader_)
[10805]106        {
[10856]107            switch (this->myLeader_->getAction())
108            {
[10858]109                case Action::FIGHT:
110                {
111                    if (!this->hasTarget())
112                    {
113                        this->setTarget(this->myLeader_->getTarget());
114                    }
115                    break;
116                }
117                case Action::FIGHTALL:
118                {
119                    if (!this->hasTarget())
120                    {
121                        this->setTarget(this->myLeader_->getTarget());
122                    }
123                    break;
124                }
125                case Action::ATTACK:
126                {
127                    if (!this->hasTarget())
128                    {
129                        this->setTarget(this->myLeader_->getTarget());
130                    }
131                    break;
132                }
[10856]133                default:
134                {
[10859]135                   
[10879]136            ControllableEntity* myEntity = this->getControllableEntity();
137            Vector3 myPosition = myEntity->getWorldPosition();
138            if (!this->myLeader_)
139            {
140                return;
141            }
142            ControllableEntity* leaderEntity = this->myLeader_->getControllableEntity();
143            Quaternion orient = leaderEntity->getWorldOrientation();
144            Vector3 leaderPosition = leaderEntity->getWorldPosition();
145            Vector3 targetRelativePosition = getFormationPosition();
146            if (!this->myLeader_)
147            {
148                return;
149            }
150            Vector3 targetAbsolutePosition = 
151                (leaderPosition + (orient*WorldEntity::FRONT) * (leaderEntity->getVelocity().length()/5)
152                 + (orient* (targetRelativePosition)));
[10881]153            //let ship finish rotating. also don't call copyOrientation to often as it is a slow function.
154            if (this->actionCounter_ % 9 == 0 && !this->bHasTargetOrientation_)
155                this->setAction (Action::FLY, targetAbsolutePosition, orient);
156            else
157                this->setAction (Action::FLY, targetAbsolutePosition);
[10879]158            if ((targetAbsolutePosition - myPosition).length() > this->tolerance_ * 1.5f)
159            {
160                this->boostControl();
161            }
162            else
163            {
164               this->getControllableEntity()->boost(false);
165            }
[10856]166                }
167            }
[10858]168            if (this->hasTarget())
169            {
170                //----choose where to go----
171                this->maneuver();
172                //----fire if you can----
173                this->bShooting_ = this->canFire();               
174            }
[10805]175        }
[10881]176        this->actionCounter_ += this->actionCounter_ < 100000 ? 1 : -this->actionCounter_ ;
[10731]177    }
178     
179   
[10856]180    Vector3 WingmanController::getFormationPosition ()
181    {
182        this->setFormationMode( this->myLeader_->getFormationMode() );
183        Vector3* targetRelativePosition;
184
[10869]185        if (this->myLeader_->getIdentifier()->getName() == "DivisionController")
[10856]186        {
187            switch (this->formationMode_){
188                case FormationMode::WALL:
189                {
[10879]190                    targetRelativePosition = new Vector3 (2*this->spread_, 0, 0 - this->tolerance_); 
[10856]191                    break;
192                }
193                case FormationMode::FINGER4: 
194                {
[10879]195                    targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_); 
[10856]196                    break;
197                }
198                case FormationMode::DIAMOND: 
199                {
[10879]200                    targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_);                 
[10856]201                    break;
202                }
203            }
204        }
205        else
206        {
[10859]207
[10856]208            switch (this->formationMode_){
209                case FormationMode::WALL:
210                {
[10879]211                    targetRelativePosition = new Vector3 (-2*this->spread_, 0, 0 - this->tolerance_); 
[10856]212                    break;
213                }
214                case FormationMode::FINGER4: 
215                {
[10879]216                    targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_ - this->tolerance_); 
[10856]217                    break;
218                }
219                case FormationMode::DIAMOND: 
220                {
[10879]221                    targetRelativePosition = new Vector3 (2*this->spread_, -this->spread_, 0 - this->tolerance_);                 
[10856]222                    break;
223                }
224            }
225        }
[10880]226        Vector3 result = *targetRelativePosition;
227        delete targetRelativePosition;
228        return result;
[10856]229    }
[10826]230    //----POST: closest leader that is ready to take a new wingman is returned----
[10877]231    ActionpointController* WingmanController::findNewLeader()
[10717]232    {
233
234        if (!this->getControllableEntity())
[10722]235            return 0;
[10717]236
[10826]237        //----vars for finding the closest leader----
[10877]238        ActionpointController* closestLeader = 0;
[10722]239        float minDistance =  std::numeric_limits<float>::infinity();
[10838]240        Gametype* gt = this->getGametype();
[10877]241
242        for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>::begin(); it; ++it)
[10717]243        {
[10826]244            //----0ptr or not a leader or dead?----
[10731]245            if (!it || 
[10869]246                (it->getIdentifier()->getName() != "SectionController" && it->getIdentifier()->getName() != "DivisionController") || 
[10731]247                !(it->getControllableEntity()))
[10722]248                continue;
[10826]249           
250            //----same team?----
[10838]251            if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) )
[10717]252                continue;
[10826]253           
254            //----check distance----
255            float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity());
[10722]256            if (distance < minDistance && !(it->hasWingman()))
257            {
258                closestLeader = *it;
259                minDistance = distance;
260            }
[10725]261           
[10717]262        }
[10722]263        if (closestLeader)
264        {
[10826]265            //----Racing conditions----
[10877]266            if (closestLeader->setWingman(orxonox_cast<ActionpointController*>(this)))
267            {
[10722]268                return closestLeader;
[10877]269            }
270
[10722]271        }
[10877]272
[10722]273        return 0;
[10717]274    }
[10722]275
[10719]276
277
[10678]278
279}
Note: See TracBrowser for help on using the repository browser.