Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Finished groundwork: AI fights enemies like I want it to. TODO implement all the functionality of old AI, like Waypoints, XMLPort

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