Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added c++11 features to code that was added in presentationHS15

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