Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

some comments added

File size: 10.2 KB
RevLine 
[10719]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 "SectionController.h"
30
31namespace orxonox
32{
33
34    RegisterClass(SectionController);
35
[10826]36    //Leaders share the fact that they have Wingmans
[10719]37    SectionController::SectionController(Context* context) : LeaderController(context)
38    {
39        RegisterObject(SectionController);
[10759]40        this->setFormationMode(FormationMode::FINGER4);
[10725]41
[10719]42        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&SectionController::action, this)));
[10725]43        this->myWingman_ = 0;
44        this->myDivisionLeader_ = 0;
[10759]45        this->rank_ = Rank::SECTIONLEADER;
[10719]46
[10826]47        //orxout(internal_error) << this << "Was created" << endl;
[10719]48
49    }
50   
51    SectionController::~SectionController()
52    {
[10725]53       
54    }
[10826]55    void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
56    {
57        SUPER(SectionController, XMLPort, xmlelement, mode);
[10731]58
[10826]59        //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
60    }
61
62    //----in tick, move (or look) and shoot----
[10731]63    void SectionController::tick(float dt)
64    {
[10805]65         if (!this->isActive())
[10731]66            return;
67        if (this->bHasTargetPosition_)
68        {
[10789]69            this->moveToTargetPosition(dt);
[10731]70        }
[10805]71        else if (this->bLookAtTarget_)
72        {
73            this->lookAtTarget(dt);
74        }
75        if (bShooting_)
76        {
77            this->doFire();
78        }
[10731]79       
80        SUPER(SectionController, tick, dt);
81    }
82
83    void SectionController::action()
84    {
[10826]85        //----If no leader, find one---- 
[10731]86        if (!myDivisionLeader_)
87        {
88            LeaderController* newDivisionLeader = findNewDivisionLeader();
89            this->myDivisionLeader_ = newDivisionLeader;
[10826]90
[10731]91            if (newDivisionLeader)
[10826]92            {
93                //orxout(internal_error) << "new DivisionLeader set" << endl;
94            }
95            //----If no leader found, attack someone----
96            //----TODO: find closest enemy----
[10759]97            else
98            {
[10826]99                if ( !this->hasTarget() || this->action_ != Action::FIGHT )
[10805]100                {
[10826]101                    for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
102                    {
103                        if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) )
104                            continue;
[10805]105
106                        this->setAction(Action::FIGHT, (*itP));
[10826]107                        break;   
108                    }   
[10805]109                }
[10826]110               
[10759]111            }
[10731]112
113        }
[10826]114        //----If have leader----
[10780]115        else
[10805]116        {
[10826]117            this->chooseTarget();
118        }
[10805]119
[10826]120        //----action was set to fight----
[10805]121        if (this->action_ == Action::FIGHT)
122        {
[10826]123            //----choose where to go----
[10805]124            this->maneuver();
[10826]125            //----fire if you can----
[10805]126            this->bShooting_ = this->canFire();
[10826]127
[10805]128            if (this->target_)
129            {
[10826]130                //----wingmans shall support the fire of their leaders----
[10805]131                if (this->myWingman_)
132                {
133                    this->myWingman_->setAction (Action::FIGHT, this->target_);                   
134                }
[10826]135                //----fly in formation if far enough----
[10805]136                Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();         
137                if (diffVector.length() > 3000)
138                {
139                    this->setTargetPositionOfWingman();
140                }   
141            }
142        }
[10826]143
144        //----action was set to fly----
[10805]145        else if (this->action_ == Action::FLY)
146        {
147            this->setTargetPositionOfWingman();
148        }
[10826]149
150        //----action was set to protect----
[10805]151        else if (this->action_ == Action::PROTECT)
152        {
153
154        }
[10759]155               
[10731]156
157    }
[10826]158    //PRE: myDivisionLeader_ != 0
159    //POST: this->target_ is set unless division leader doesn't have one
160    void SectionController::chooseTarget()
161    {
162        //----If division leader fights, cover him by fighting emenies close to his target----
163        if (this->myDivisionLeader_->getAction() == Action::FIGHT)
164        {
165            //----if he has a target----
166            if (this->myDivisionLeader_->hasTarget())
167            {
168                //----try to find a new target if division leader has wingman (doing fine) and no good target already set----
169                if ( this->myDivisionLeader_->hasWingman() && 
170                    !( this->hasTarget() && this->getTarget() != this->myDivisionLeader_->getTarget() ) )
171                {
172
173                    bool foundTarget = false;
174                    //----new target should be close to division's target----
175                    Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition();
176                   
177                    for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
178                    {
179                        //----is enemy?----
180                        if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) )
181                            continue;           
182                        //----in range?----
183                        if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 && 
184                            (*itP) != this->myDivisionLeader_->getTarget())
185                        {
186                            foundTarget = true;
187                            this->setAction(Action::FIGHT, (*itP));
188                            //orxout(internal_error) << "Found target" << endl;
189                            break; 
190                        }
191                    }
192                    //----no target? then attack same target as division leader----
193                    if (!foundTarget)
194                    {
195                        this->setAction(Action::FIGHT, this->myDivisionLeader_->getTarget());
196                    }
197                }
198                //----if division leader doesn't have a wingman, support his fire----
199                else
200                {
201                    this->setAction(Action::FIGHT, this->myDivisionLeader_->getTarget());
202                }
203            }
204            //----If he fights but doesn't have a target, wait for him to get one----
205            else
206            {
207
208            }
209        } 
210    }
211
212    //----stay in formation----
[10729]213    void SectionController::setTargetPositionOfWingman()
[10725]214    {
215        if (!this->myWingman_)
216            return;
[10729]217        Vector3* targetRelativePositionOfWingman;
[10725]218        switch (this->formationMode_){
[10759]219            case FormationMode::WALL:
[10725]220            {
[10729]221                targetRelativePositionOfWingman = new Vector3 (-400, 0, 0); 
[10725]222                break;
223            }
[10759]224            case FormationMode::FINGER4: 
[10725]225            {
[10759]226                targetRelativePositionOfWingman = new Vector3 (-400, 0, -200); 
[10725]227                break;
228            }
[10759]229            case FormationMode::DIAMOND: 
[10725]230            {
[10759]231                targetRelativePositionOfWingman = new Vector3 (400, -200, 0);                 
[10725]232                break;
233            }
[10719]234        }
[10729]235        Quaternion orient = this->getControllableEntity()->getWorldOrientation();
236       
237        Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + 
238        (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman)));
239       
[10805]240        myWingman_->setAction (Action::FLY, targetAbsolutePositionOfWingman, orient);
[10725]241       
[10719]242    }
[10826]243
[10722]244    LeaderController* SectionController::findNewDivisionLeader()
[10719]245    {
246
247        if (!this->getControllableEntity())
[10722]248            return 0;
[10719]249
[10722]250        LeaderController* closestLeader = 0;
251        float minDistance =  std::numeric_limits<float>::infinity();
[10719]252        //go through all pawns
253        for (ObjectList<LeaderController>::iterator it = ObjectList<LeaderController>::begin(); it; ++it)
254        {
[10722]255            //0ptr or not DivisionController?
[10759]256            if (!(it) || !((it)->getRank() == Rank::DIVISIONLEADER) || !(it->getControllableEntity()))
[10722]257                continue;
[10719]258            //same team?
259            if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam()))
260                continue;
261
262            //is equal to this?
263            if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity())
264                continue;
265
[10826]266            float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity());
[10722]267           
268            if (distance < minDistance && !(it->hasFollower()))
269            {
270                closestLeader = *it;
271                minDistance = distance;
272            }
[10729]273         
[10719]274        }
[10722]275        if (closestLeader)
276        {
277            if (closestLeader->setFollower(this))
278                return closestLeader;
279        }
280        return 0;
[10719]281
282    }
[10731]283    bool SectionController::setWingman(CommonController* cwingman)
284    {
285        WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman);
[10719]286
[10731]287        if (!this->myWingman_)
[10719]288        {
[10731]289            this->myWingman_ = wingman;
290            return true;
[10719]291        }
[10731]292        else
293        {
294            return false;
295        }
[10729]296    }
297   
[10731]298    bool SectionController::hasWingman()
[10725]299    {
[10731]300        if (this->myWingman_)
301            return true;
302        else
303            return false;
[10719]304    }
305
306   
[10826]307   
[10719]308   
309
310}
Note: See TracBrowser for help on using the repository browser.