Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed library dependencies

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