Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/campaignHS15/src/orxonox/controllers/SectionController.cc @ 10869

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

fixed a bug and got rid of unnecessary Rank enumeration, used getIdentifire instead

File size: 11.4 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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      Dominik Solenicki
26 *
27 */
28
29#include "SectionController.h"
30//TODO: formation vectors are wrong, fix it.
31// split classes.
32// weaponsystem.
33//-> Math ?
34 
35namespace orxonox
36{
37
38    RegisterClass(SectionController);
39
40    //Leaders share the fact that they have Wingmans
41    SectionController::SectionController(Context* context) : LeaderController(context)
42    {
43        RegisterObject(SectionController);
44        this->setFormationMode(FormationMode::FINGER4);
45
46        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&SectionController::action, this)));
47        this->myWingman_ = 0;
48        this->myDivisionLeader_ = 0;
49        this->bFirstAction_ = true;
50        //orxout(internal_error) << this << "Was created" << endl;
51
52    }
53   
54    SectionController::~SectionController()
55    {
56        // if (this->myWingman_)
57        // {
58        //     this->myWingman_->takeActionpoints(this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_);
59        // }
60       for (size_t i = 0; i < this->actionpoints_.size(); ++i)
61        {
62            if(this->actionpoints_[i])
63                this->actionpoints_[i]->destroy();
64        }
65        this->parsedActionpoints_.clear();
66        this->actionpoints_.clear();
67    }
68    void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
69    {
70        SUPER(SectionController, XMLPort, xmlelement, mode);
71
72        //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
73    }
74
75    //----in tick, move (or look) and shoot----
76    void SectionController::tick(float dt)
77    {
78        if (!this->isActive())
79            return;
80     
81       
82        SUPER(SectionController, tick, dt);
83    }
84
85    void SectionController::action()
86    {
87        if (!this || !this->getControllableEntity())
88            return;
89
90        //----If no leader, find one---- 
91        if (!myDivisionLeader_)
92        {
93            LeaderController* newDivisionLeader = findNewDivisionLeader();
94            this->myDivisionLeader_ = newDivisionLeader;
95
96        }
97        //----If have leader----
98        else
99        {
100        }
101        if (!myDivisionLeader_)
102        {
103           
104            ActionpointController::action();
105            if (!this || !this->getControllableEntity())
106                return;
107            if (!(this->parsedActionpoints_.empty() && this->loopActionpoints_.empty()))
108            {
109                if (this->myWingman_)
110                {
111                    this->myWingman_->takeActionpoints(this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_);
112                }   
113            }
114        }
115        else if (myDivisionLeader_)
116        {
117            switch (myDivisionLeader_->getAction())
118            {
119                case Action::FIGHT:
120                {
121                    if (!this->hasTarget())
122                    {
123                        this->chooseTarget();
124                    }
125                    break;
126                }
127                case Action::FIGHTALL:
128                {
129                    if (!this->hasTarget())
130                    {
131                        this->chooseTarget();
132                    }
133                    break;
134                }
135                case Action::ATTACK:
136                {
137                    if (!this->hasTarget())
138                    {
139                        this->chooseTarget();
140                    }
141                    break;
142                }
143                default:
144                {
145                    ControllableEntity* myEntity = this->getControllableEntity();
146                    Vector3 myPosition = myEntity->getWorldPosition();
147                    if (!this->myDivisionLeader_)
148                    {
149                        return;
150                    }
151                    ControllableEntity* leaderEntity = this->myDivisionLeader_->getControllableEntity();
152                    Quaternion orient = leaderEntity->getWorldOrientation();
153                    Vector3 leaderPosition = leaderEntity->getWorldPosition();
154
155                    Vector3 targetRelativePosition = getFormationPosition();
156                    if (!this->myDivisionLeader_)
157                    {
158                        return;
159                    }
160                    Vector3 targetAbsolutePosition = 
161                        (leaderPosition + (orient*WorldEntity::FRONT) * (leaderEntity->getVelocity().length()/5)
162                         + (orient* (targetRelativePosition)));
163               
164                    this->setAction (Action::FLY, targetAbsolutePosition, orient);
165                    if ((targetAbsolutePosition - myPosition).length() > this->tolerance_ * 1.5f)
166                    {
167                        this->boostControl();
168                    }
169                    else
170                    {
171                       this->getControllableEntity()->boost(false);
172                    }
173                }
174            }
175            if (this->hasTarget())
176            {
177                //----choose where to go----
178                this->maneuver();
179                //----fire if you can----
180                this->bShooting_ = this->canFire();               
181            }
182        }
183     
184    }
185
186   
187    //PRE: myDivisionLeader_ != 0 && myDivisionLeader_->action_ == Action::FIGHT
188    //POST: this->target_ is set unless division leader doesn't have one
189    void SectionController::chooseTarget()
190    {
191        //----If division leader fights, cover him by fighting emenies close to his target----
192        Action::Value action = this->myDivisionLeader_->getAction();
193       
194        Pawn* target;
195        if (action == Action::FIGHT || action == Action::FIGHTALL || action == Action::ATTACK)
196        {
197            //----if he has a target----
198            if (this->myDivisionLeader_->hasTarget())
199            {
200                //----try to find a new target if division leader has wingman (doing fine) and no good target already set----
201                if ( this->myDivisionLeader_->hasWingman() && 
202                    !( this->hasTarget() && this->getTarget() != this->myDivisionLeader_->getTarget() ) )
203                {
204
205                    bool foundTarget = false;
206                    //----new target should be close to division's target----
207                    Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition();
208                    Gametype* gt = this->getGametype();
209                    for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
210                    {
211                        //----is enemy?----
212                        if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) )
213                            continue;           
214                        //----in range?----
215                        if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 && 
216                            (*itP) != this->myDivisionLeader_->getTarget())
217                        {
218                            foundTarget = true;
219                            target =  (*itP);
220                            //orxout(internal_error) << "Found target" << endl;
221                            break; 
222                        }
223                    }
224                    //----no target? then attack same target as division leader----
225                    if (!foundTarget)
226                    {
227                        target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget());
228                    }
229                }
230                //----if division leader doesn't have a wingman, support his fire----
231                else
232                {
233                    target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget());
234                }
235            }
236            //----If he fights but doesn't have a target, wait for him to get one----
237            else
238            {
239
240            }
241            this->setTarget (orxonox_cast<ControllableEntity*>(target));
242        }
243        else
244        {
245        } 
246    }
247    Vector3 SectionController::getFormationPosition ()
248    {
249        this->setFormationMode( this->myDivisionLeader_->getFormationMode() );
250        Vector3* targetRelativePosition;
251        switch (this->formationMode_){
252            case FormationMode::WALL:
253            {
254                targetRelativePosition = new Vector3 (-400, 0, 0);   
255                break;
256            }
257            case FormationMode::FINGER4: 
258            {
259                targetRelativePosition = new Vector3 (-400, 0, 200);   
260                break;
261            }
262           
263            case FormationMode::DIAMOND: 
264            {
265                targetRelativePosition = new Vector3 (-400, 0, 200);                   
266                break;
267            }
268        }
269        return *targetRelativePosition;
270    }
271
272   
273
274    LeaderController* SectionController::findNewDivisionLeader()
275    {
276
277        if (!this->getControllableEntity())
278            return 0;
279
280        LeaderController* closestLeader = 0;
281        float minDistance =  std::numeric_limits<float>::infinity();
282        //go through all pawns
283        for (ObjectList<LeaderController>::iterator it = ObjectList<LeaderController>::begin(); it; ++it)
284        {
285            //0ptr or not DivisionController?
286            if (!(it) || !((it)->getIdentifier()->getName() == "DivisionController") || !(it->getControllableEntity()))
287                continue;
288            //same team?
289            if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam()))
290                continue;
291
292            //is equal to this?
293            if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity())
294                continue;
295
296            float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity());
297           
298            if (distance < minDistance && !(it->hasFollower()))
299            {
300                closestLeader = *it;
301                minDistance = distance;
302            }
303         
304        }
305        if (closestLeader)
306        {
307            if (closestLeader->setFollower(this))
308                return closestLeader;
309        }
310        return 0;
311    }
312    bool SectionController::setWingman(CommonController* cwingman)
313    {
314        WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman);
315
316        if (!this->myWingman_)
317        {
318            this->myWingman_ = wingman;
319            return true;
320        }
321        else
322        {
323            return false;
324        }
325    }
326   
327    bool SectionController::hasWingman()
328    {
329        if (this->myWingman_)
330            return true;
331        else
332            return false;
333    }
334}
Note: See TracBrowser for help on using the repository browser.