Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/AI_HS15/src/orxonox/controllers/DivisionController.cc @ 10826

Last change on this file since 10826 was 10826, checked in by gania, 8 years ago

some comments added

File size: 7.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 "DivisionController.h"
30
31
32namespace orxonox
33{
34
35    RegisterClass(DivisionController);
36
[10826]37    //Leaders share the fact that they have Wingmans
[10709]38    DivisionController::DivisionController(Context* context) : LeaderController(context)
[10678]39    {
40        RegisterObject(DivisionController);
[10759]41       
42        this->setFormationMode(FormationMode::DIAMOND);
[10763]43        this->target_ = 0;
[10725]44        this->myFollower_ = 0;
45        this->myWingman_ = 0;
46        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&DivisionController::action, this)));
[10759]47        this->rank_ = Rank::DIVISIONLEADER;
[10725]48
[10731]49
[10678]50    }
51
52    DivisionController::~DivisionController()
53    {
[10725]54    } 
[10731]55
[10826]56    void DivisionController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
57    {
58        SUPER(DivisionController, XMLPort, xmlelement, mode);
59
60        //XMLPortParam(DivisionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
61    }
62
[10731]63   
[10725]64    void DivisionController::tick(float dt)
[10803]65    {   
66        if (!this->isActive())
67            return;
[10800]68        if (this->bHasTargetPosition_)
[10729]69        {
[10800]70            this->moveToTargetPosition(dt);
71        }
[10803]72        else if (this->bLookAtTarget_)
[10789]73        {
[10803]74            this->lookAtTarget(dt);
[10729]75        }
[10803]76        if (bShooting_)
[10796]77        {
[10803]78            this->doFire();
79        }
80       
[10722]81        SUPER(DivisionController, tick, dt);
[10718]82
[10678]83    }
[10725]84    void DivisionController::action()
85    {
[10826]86        //----find a target----
87        if ( !this->hasTarget() )
[10805]88        {
89            for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
90            {
[10826]91                if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) )
[10805]92                    continue;           
93
94               
[10826]95                if (static_cast<ControllableEntity*>(*itP) != (this)->getControllableEntity()
96                    && CommonController::distance (*itP, this->getControllableEntity())  < 10000)
[10805]97                {
98                    (this)->setAction(Action::FIGHT, *itP);
99                }   
100            }
101        }
[10803]102       
[10805]103        if (this->action_ == Action::FIGHT)
104        {
[10826]105            //----choose where to go----
[10805]106            this->maneuver();
[10826]107            //----fire if you can----
[10805]108            this->bShooting_ = this->canFire();
[10826]109
[10805]110            if (this->target_)
111            {
112                if (this->myWingman_)
113                {
[10826]114                    //----wingmans shall support the fire of their leaders----
[10805]115                    this->myWingman_->setAction (Action::FIGHT, this->target_);                   
116                }
117               
[10826]118                //----fly in formation if far enough----
[10805]119                Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();         
120                if (diffVector.length() > 3000)
121                {
122                    this->setTargetPositionOfWingman();
123                    //this->setTargetPositionOfFollower();                   
124                }   
125            }
126        }
127        else if (this->action_ == Action::FLY)
128        {
129            this->setTargetPositionOfWingman();
130            this->setTargetPositionOfFollower();
131        }
132        else if (this->action_ == Action::PROTECT)
133        {
[10763]134
[10805]135        }
136
[10709]137    }
[10725]138
[10731]139   
140
[10729]141    void DivisionController::setTargetPositionOfWingman()
[10725]142    {
143        if (!this->myWingman_)
144            return;
[10729]145        Vector3* targetRelativePositionOfWingman;
[10725]146        switch (this->formationMode_){
[10759]147            case FormationMode::WALL:
[10725]148            {
[10729]149                targetRelativePositionOfWingman = new Vector3 (400, 0, 0); 
[10725]150                break;
151            }
[10759]152            case FormationMode::FINGER4: 
[10725]153            {
[10759]154                targetRelativePositionOfWingman = new Vector3 (400, 0, -200); 
[10725]155                break;
156            }
[10826]157         
[10759]158            case FormationMode::DIAMOND: 
[10725]159            {
[10759]160                targetRelativePositionOfWingman = new Vector3 (400, 0, -200);                 
[10725]161                break;
162            }
163        }
[10729]164        Quaternion orient = this->getControllableEntity()->getWorldOrientation();
165       
166        Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + 
167        (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman)));
168       
[10805]169        myWingman_->setAction( Action::FLY, targetAbsolutePositionOfWingman, orient);
[10725]170       
171    }
[10729]172    void DivisionController::setTargetPositionOfFollower()
[10725]173    {
174        if (!this->myFollower_)
175            return;
[10759]176        this->myFollower_->setFormationMode(this->formationMode_);
177
[10729]178        Vector3* targetRelativePositionOfFollower;
[10725]179        switch (this->formationMode_){
[10759]180            case FormationMode::WALL:
[10725]181            {
[10729]182                targetRelativePositionOfFollower = new Vector3 (-400, 0, 0);   
[10725]183                break;
184            }
[10759]185            case FormationMode::FINGER4: 
[10725]186            {
[10759]187                targetRelativePositionOfFollower = new Vector3 (-400, 0, -200);   
[10725]188                break;
189            }
[10826]190           
[10759]191            case FormationMode::DIAMOND: 
[10725]192            {
[10759]193                targetRelativePositionOfFollower = new Vector3 (-400, 0, -200);                   
[10725]194                break;
195            }
196        }
[10729]197        Quaternion orient = this->getControllableEntity()->getWorldOrientation();
198       
199        Vector3 targetAbsolutePositionOfFollower = ((this->getControllableEntity()->getWorldPosition()) + 
200        (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfFollower)));
201       
[10805]202        myFollower_->setAction ( Action::FLY, targetAbsolutePositionOfFollower, orient );       
[10729]203    }
[10731]204
205
206    bool DivisionController::setWingman(CommonController* cwingman)
207    {
208
209        WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman);
210        if (!this->myWingman_)
211        {
212            this->myWingman_ = wingman;
213            return true;
214        }
215        else
216        {
217            return false;
218        }
219   
220    }
221    bool DivisionController::setFollower(LeaderController* myFollower)
222    {
223         if (!this->myFollower_)
224        {
225            this->myFollower_ = myFollower;
226            return true;
227        }
228        else
229        {
230            return false;
231        }
232    }
233    bool DivisionController::hasWingman()
234    {
235        if (this->myWingman_)
236            return true;
237        else
238            return false;
239    }
240    bool DivisionController::hasFollower()
241    {
242        if (this->myFollower_)
243            return true;
244        else
245            return false;
246    }
247
248
[10678]249   
250   
251
252}
Note: See TracBrowser for help on using the repository browser.