Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

action PROTECT works. Look FUCK

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