Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/campaignHS15/src/orxonox/controllers/DivisionController.cc @ 10847

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

I hope that you don't code today, that version is not compilable just yet

File size: 10.8 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 "DivisionController.h"
30
31
32namespace orxonox
33{
34
35    RegisterClass(DivisionController);
36
37    //Leaders share the fact that they have Wingmans
38    DivisionController::DivisionController(Context* context) : LeaderController(context)
39    {
40        RegisterObject(DivisionController);
41       
42        this->setFormationMode(FormationMode::DIAMOND);
43        this->target_ = 0;
44        this->myFollower_ = 0;
45        this->myWingman_ = 0;
46        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&DivisionController::action, this)));
47        this->rank_ = Rank::DIVISIONLEADER;
48
49
50    }
51
52    DivisionController::~DivisionController()
53    {
54    } 
55
56    void DivisionController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
57    {
58        SUPER(DivisionController, XMLPort, xmlelement, mode);
59
60        //XMLPortParam(DivisionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
61    }
62
63   
64    void DivisionController::tick(float dt)
65    {   
66
67        if (!this->isActive())
68            return;
69       
70       
71        SUPER(DivisionController, tick, dt);
72
73    }
74    void DivisionController::action()
75    {
76
77       
78  /*      if (this->target_)
79        {
80            if (CommonController::distance (this->getControllableEntity(), newTarget) <
81                CommonController::distance (this->getControllableEntity(), target_))
82            {
83                Actionpoint* ap = new Actionpoint(this->getContext());
84                ap->setPosition (0, 0, 0);
85                ap->setActionXML ("FIGHT");
86                //ap->setEnemyXML(CommonController::getName(newTarget));
87                this->addActionpoint (ap);
88            }
89
90        }*/
91        //----Whatever ship is doing, if there are targets close to it and its own target is far away, fight them----
92        //analog to attack move
93        if (this->action_ != Action::FIGHT && this->action_ != Action::FIGHTALL)
94        {
95            if ((this->target_ && CommonController::distance (this->getControllableEntity(), this->target_) > this->attackRange_) ||
96             !this->target_)
97            {
98                ControllableEntity* newTarget = this->closestTarget();
99                if (newTarget && CommonController::distance (this->getControllableEntity(), newTarget) < this->attackRange_)
100                {
101                    this->backupAction();
102                    this->setAction (Action::FIGHT, newTarget);
103                }
104            }
105        }
106        //action is NONE whenever ships finishes current action,
107        //if it was fighting before because enemies were close, resume what it was doing
108        //otherwise try to execute next action
109        if (this->action_ == Action::NONE)
110        {
111            if (this->bBackuped_)
112            {
113                this->restoreAction();
114            } else if (!this->actionpoints_.empty())
115            {
116                this->popAction();
117            }
118        }
119        if (this->action_ == Action::FIGHT || this->action_ == Action::FIGHTALL)
120        {
121            if (!this->hasTarget())
122            {
123                //----find a target----
124                if (this->action_ == Action::FIGHT)
125                {
126                    ControllableEntity* newTarget = this->closestTarget();
127                    if (newTarget && CommonController::distance (this->getControllableEntity(), newTarget) < this->attackRange_)
128                    {
129                        this->setAction (Action::FIGHT, newTarget);
130                    }
131                    else
132                    {
133                        this->setAction (Action::NONE);
134                    }
135                }
136                else if (this->action_ == Action::FIGHTALL)
137                {
138                    this->setClosestTarget();                     
139                }
140
141            }
142            else if (this->hasTarget())
143            {
144                //----fly in formation if far enough----
145                Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();         
146                if (diffVector.length() > this->attackRange_)
147                {
148                    this->setTargetPositionOfWingman();
149                    this->setTargetPositionOfFollower();                   
150                }
151                else
152                {
153                    //----wingmans shall support the fire of their leaders----
154                    if (this->myWingman_)
155                    {
156                        this->myWingman_->setAction (Action::FIGHT, this->target_);     
157                    }
158                    if (this->myFollower_)
159                    {
160                        this->myFollower_->setAction (Action::FIGHT);                                   
161                    }
162
163                }
164               
165            }
166            if (this->hasTarget())
167            {
168                //----choose where to go----
169                this->maneuver();
170                //----fire if you can----
171                this->bShooting_ = this->canFire();               
172            }
173
174        }
175        else if (this->action_ == Action::FLY)
176        {
177            this->setTargetPositionOfWingman();
178            this->setTargetPositionOfFollower();
179        }
180        else if (this->action_ == Action::PROTECT)
181        {
182            if (!this->getProtect())
183            {
184                for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
185                {
186                    if ((*itP)->getName() == this->protectName_)
187                    {
188                        this->setProtect (static_cast<ControllableEntity*>(*itP));
189                    }
190                }
191                if (!this->getProtect())
192                {
193                    this->setAction (Action::NONE);
194                }
195            }
196            else
197            {
198               /* if (this->myWingman_)
199                    this->myWingman_->setAction (Action::PROTECT, this->getProtect());
200                if (this->myFollower_)
201                    this->myFollower_->setAction (Action::PROTECT, this->getProtect());
202                */
203                Vector3* targetRelativePosition;
204                   
205                targetRelativePosition = new Vector3 (0, 0, 500); 
206     
207                Vector3 targetAbsolutePosition = ((this->getProtect()->getWorldPosition()) + 
208                    (this->getProtect()->getWorldOrientation()* (*targetRelativePosition)));
209                this->setTargetPosition(targetAbsolutePosition);
210               
211                this->setTargetPositionOfWingman();
212                this->setTargetPositionOfFollower();
213            }           
214
215        }
216
217    }
218
219   
220
221    void DivisionController::setTargetPositionOfWingman()
222    {
223        if (!this->myWingman_)
224            return;
225        Vector3* targetRelativePositionOfWingman;
226        switch (this->formationMode_){
227            case FormationMode::WALL:
228            {
229                targetRelativePositionOfWingman = new Vector3 (400, 0, 0); 
230                break;
231            }
232            case FormationMode::FINGER4: 
233            {
234                targetRelativePositionOfWingman = new Vector3 (400, 0, 200); 
235                break;
236            }
237         
238            case FormationMode::DIAMOND: 
239            {
240                targetRelativePositionOfWingman = new Vector3 (400, 0, 200);                 
241                break;
242            }
243        }
244        Quaternion orient = this->getControllableEntity()->getWorldOrientation();
245       
246        Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + 
247        (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman)));
248       
249        myWingman_->setAction( Action::FLY, targetAbsolutePositionOfWingman, orient);
250       
251    }
252    void DivisionController::setTargetPositionOfFollower()
253    {
254        if (!this->myFollower_)
255            return;
256        this->myFollower_->setFormationMode(this->formationMode_);
257
258        Vector3* targetRelativePositionOfFollower;
259        switch (this->formationMode_){
260            case FormationMode::WALL:
261            {
262                targetRelativePositionOfFollower = new Vector3 (-400, 0, 0);   
263                break;
264            }
265            case FormationMode::FINGER4: 
266            {
267                targetRelativePositionOfFollower = new Vector3 (-400, 0, 200);   
268                break;
269            }
270           
271            case FormationMode::DIAMOND: 
272            {
273                targetRelativePositionOfFollower = new Vector3 (-400, 0, 200);                   
274                break;
275            }
276        }
277        Quaternion orient = this->getControllableEntity()->getWorldOrientation();
278       
279        Vector3 targetAbsolutePositionOfFollower = ((this->getControllableEntity()->getWorldPosition()) + 
280        (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfFollower)));
281       
282        myFollower_->setAction ( Action::FLY, targetAbsolutePositionOfFollower, orient );       
283    }
284
285
286    bool DivisionController::setWingman(CommonController* cwingman)
287    {
288
289        WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman);
290        if (!this->myWingman_)
291        {
292            this->myWingman_ = wingman;
293            return true;
294        }
295        else
296        {
297            return false;
298        }
299   
300    }
301    bool DivisionController::setFollower(LeaderController* myFollower)
302    {
303         if (!this->myFollower_)
304        {
305            this->myFollower_ = myFollower;
306            return true;
307        }
308        else
309        {
310            return false;
311        }
312    }
313    bool DivisionController::hasWingman()
314    {
315        if (this->myWingman_)
316            return true;
317        else
318            return false;
319    }
320    bool DivisionController::hasFollower()
321    {
322        if (this->myFollower_)
323            return true;
324        else
325            return false;
326    }
327
328
329   
330   
331
332}
Note: See TracBrowser for help on using the repository browser.