Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/cpp11_v3/src/orxonox/controllers/SectionController.cc @ 11057

Last change on this file since 11057 was 11057, checked in by landauf, 8 years ago

removed empty XMLPort functions

  • Property svn:eol-style set to native
File size: 9.5 KB
Line 
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 *      Gani Aliguzhinov
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "SectionController.h"
30
31namespace orxonox
32{
33
34    RegisterClass(SectionController);
35
36    //Leaders share the fact that they have Wingmans
37    SectionController::SectionController(Context* context) : ActionpointController(context)
38    {
39        RegisterObject(SectionController);
40        this->setFormationMode(FormationMode::FINGER4);
41
42        this->myWingman_ = 0;
43        this->myDivisionLeader_ = 0;
44        this->bFirstAction_ = true;
45
46    }
47   
48    SectionController::~SectionController()
49    {
50       for (size_t i = 0; i < this->actionpoints_.size(); ++i)
51        {
52            if(this->actionpoints_[i])
53                this->actionpoints_[i]->destroy();
54        }
55        this->parsedActionpoints_.clear();
56        this->actionpoints_.clear();
57    }
58
59    //----in tick, move (or look) and shoot----
60    void SectionController::tick(float dt)
61    {
62        if (!this->isActive())
63            return;
64   
65        SUPER(SectionController, tick, dt);
66       
67    }
68
69    void SectionController::action()
70    {
71        if (!this || !this->getControllableEntity() || !this->isActive())
72            return;
73
74        //----If no leader, find one---- 
75        if (!myDivisionLeader_)
76        {
77            ActionpointController* newDivisionLeader = findNewDivisionLeader();
78            if (!this || !this->getControllableEntity())
79                return;
80
81            this->myDivisionLeader_ = newDivisionLeader;
82        }
83        //----If have leader----
84        else
85        {
86        }
87        if (!myDivisionLeader_)
88        {
89            ActionpointController::action();
90            if (!this || !this->getControllableEntity())
91                return;
92            if (!(this->parsedActionpoints_.empty() && this->loopActionpoints_.empty()))
93            {
94                if (this->myWingman_)
95                {
96                    this->myWingman_->takeActionpoints(this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_);
97                }   
98            }
99        }
100        else if (myDivisionLeader_)
101        {
102            if (this->myDivisionLeader_->bKeepFormation_ || !(this->myDivisionLeader_->getAction() == Action::FIGHT
103                || this->myDivisionLeader_->getAction() == Action::FIGHTALL
104                || this->myDivisionLeader_->getAction() == Action::ATTACK))
105            {
106                this->keepFormation();
107            }
108            else if (!this->myDivisionLeader_->bKeepFormation_)
109            {
110                if (!this || !this->getControllableEntity())
111                    return;
112
113                if (!this->hasTarget())
114                {
115                    this->chooseTarget(); 
116                }
117
118            }
119        }
120
121    }
122
123   
124    //PRE: myDivisionLeader_ != 0 && myDivisionLeader_->action_ == Action::FIGHT
125    //POST: this->target_ is set unless division leader doesn't have one
126    void SectionController::chooseTarget()
127    {
128        //----If division leader fights, cover him by fighting emenies close to his target----
129        Action::Value action = this->myDivisionLeader_->getAction();
130
131        if (action == Action::FIGHT || action == Action::FIGHTALL || action == Action::ATTACK)
132        {
133            Pawn* target;
134            //----if he has a target----
135            if (this->myDivisionLeader_->hasTarget())
136            {
137                //----try to find a new target if division leader has wingman (doing fine) and no good target already set----
138                if ( this->myDivisionLeader_->hasWingman() && 
139                    !( this->hasTarget() && this->getTarget() != this->myDivisionLeader_->getTarget() ) )
140                {
141                    bool foundTarget = false;
142                    //----new target should be close to division's target----
143                    Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition();
144                    Gametype* gt = this->getGametype();
145                    for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>().begin(); itP; ++itP)
146                    {
147                        //----is enemy?----
148                        if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) )
149                            continue;           
150                        //----in range?----
151                        if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 && 
152                            (*itP) != this->myDivisionLeader_->getTarget())
153                        {
154                            foundTarget = true;
155                            target =  (*itP);
156                            break; 
157                        }
158                    }
159                    //----no target? then attack same target as division leader----
160                    if (!foundTarget)
161                    {
162                        target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget());
163                    }
164                }
165                //----if division leader doesn't have a wingman, support his fire----
166                else
167                {
168                    target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget());
169                }
170            }
171            //----If he fights but doesn't have a target, wait for him to get one----
172            else
173            {
174
175            }
176            this->setTarget (orxonox_cast<ControllableEntity*>(target));
177        }
178        else
179        {
180        } 
181    }
182    Vector3 SectionController::getFormationPosition ()
183    {
184        this->setFormationMode( this->myDivisionLeader_->getFormationMode() );
185        this->spread_ = this->myDivisionLeader_->getSpread();
186        Vector3* targetRelativePosition;
187        switch (this->formationMode_){
188            case FormationMode::WALL:
189            {
190                targetRelativePosition = new Vector3 (-2.0f*this->spread_, 0, 0);   
191                break;
192            }
193            case FormationMode::FINGER4: 
194            {
195                targetRelativePosition = new Vector3 (-2.0f*this->spread_, 0, 1.0f*this->spread_);   
196                break;
197            }
198           
199            case FormationMode::DIAMOND: 
200            {
201                targetRelativePosition = new Vector3 (-2.0f*this->spread_, 0, 1.0f*this->spread_);
202                break;
203            }
204        }
205        Vector3 result = *targetRelativePosition;
206        delete targetRelativePosition;
207        return result;
208    }
209
210    void SectionController::keepFormation()
211    {
212        this->bKeepFormation_ = true;
213        ControllableEntity* leaderEntity = this->myDivisionLeader_->getControllableEntity();
214        Vector3 targetRelativePosition = this->getFormationPosition();
215        if (!leaderEntity)
216            return;
217        FlyingController::keepFormation(leaderEntity, targetRelativePosition);
218    }
219
220    ActionpointController* SectionController::findNewDivisionLeader()
221    {
222
223        if (!this->getControllableEntity())
224            return 0;
225
226        ActionpointController* closestLeader = 0;
227        float minDistance =  std::numeric_limits<float>::infinity();
228        //go through all pawns
229        for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>().begin(); it; ++it)
230        {
231            //0ptr or not DivisionController?
232            if (!(it) || !((it)->getIdentifier()->getName() == "DivisionController") || !(it->getControllableEntity()))
233                continue;
234            //same team?
235            if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam()))
236                continue;
237
238            //is equal to this?
239            if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity())
240                continue;
241
242            float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity());
243           
244            if (distance < minDistance && !(it->hasFollower()))
245            {
246                closestLeader = *it;
247                minDistance = distance;
248            }
249         
250        }
251        if (closestLeader)
252        {
253            if (closestLeader->setFollower(this))
254                return closestLeader;
255        }
256        return 0;
257    }
258
259    bool SectionController::setWingman(ActionpointController* newWingman)
260    {
261
262        if (!this->myWingman_)
263        {
264            this->myWingman_ = newWingman;
265            newWingman->takeActionpoints (this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_);
266            return true;
267        }
268        else
269        {
270            return false;
271        }
272    }
273   
274    bool SectionController::hasWingman()
275    {
276        if (this->myWingman_)
277            return true;
278        else
279            return false;
280    }
281}
Note: See TracBrowser for help on using the repository browser.