Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10843 was 10843, checked in by gania, 9 years ago

gani check in before a major change

File size: 8.2 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->action_ == Action::FIGHT)
79        {
80            if (!this->hasTarget())
81            {
82                //----find a target----
83                this->setClosestTarget(); 
84            }
85            else
86            {
87                //----fly in formation if far enough----
88                Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();         
89                if (diffVector.length() > 3000)
90                {
91                    this->setTargetPositionOfWingman();
92                    this->setTargetPositionOfFollower();                   
93                }
94                else
95                {
96                    //----wingmans shall support the fire of their leaders----
97                    if (this->myWingman_)
98                    {
99                        this->myWingman_->setAction (Action::FIGHT, this->target_);     
100                    }
101                    if (this->myFollower_)
102                    {
103                        this->myFollower_->setAction (Action::FIGHT);                                   
104                    }
105
106                }
107               
108            }
109            if (this->hasTarget())
110            {
111                //----choose where to go----
112                this->maneuver();
113                //----fire if you can----
114                this->bShooting_ = this->canFire();               
115            }
116
117        }
118        else if (this->action_ == Action::FLY)
119        {
120            this->setTargetPositionOfWingman();
121            this->setTargetPositionOfFollower();
122        }
123        else if (this->action_ == Action::PROTECT)
124        {
125            if (!this->getProtect())
126            {
127                for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
128                {
129                    if ((*itP)->getName() == this->protectName_)
130                    {
131                        this->setProtect (static_cast<ControllableEntity*>(*itP));
132                    }
133                }
134            }
135            else
136            {
137               /* if (this->myWingman_)
138                    this->myWingman_->setAction (Action::PROTECT, this->getProtect());
139                if (this->myFollower_)
140                    this->myFollower_->setAction (Action::PROTECT, this->getProtect());
141                */
142                Vector3* targetRelativePosition;
143                   
144                targetRelativePosition = new Vector3 (0, 0, 500); 
145     
146                Vector3 targetAbsolutePosition = ((this->getProtect()->getWorldPosition()) + 
147                    (this->getProtect()->getWorldOrientation()* (*targetRelativePosition)));
148                this->setTargetPosition(targetAbsolutePosition);
149               
150                this->setTargetPositionOfWingman();
151                this->setTargetPositionOfFollower();
152            }           
153
154        }
155
156    }
157
158   
159
160    void DivisionController::setTargetPositionOfWingman()
161    {
162        if (!this->myWingman_)
163            return;
164        Vector3* targetRelativePositionOfWingman;
165        switch (this->formationMode_){
166            case FormationMode::WALL:
167            {
168                targetRelativePositionOfWingman = new Vector3 (400, 0, 0); 
169                break;
170            }
171            case FormationMode::FINGER4: 
172            {
173                targetRelativePositionOfWingman = new Vector3 (400, 0, 200); 
174                break;
175            }
176         
177            case FormationMode::DIAMOND: 
178            {
179                targetRelativePositionOfWingman = new Vector3 (400, 0, 200);                 
180                break;
181            }
182        }
183        Quaternion orient = this->getControllableEntity()->getWorldOrientation();
184       
185        Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + 
186        (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman)));
187       
188        myWingman_->setAction( Action::FLY, targetAbsolutePositionOfWingman, orient);
189       
190    }
191    void DivisionController::setTargetPositionOfFollower()
192    {
193        if (!this->myFollower_)
194            return;
195        this->myFollower_->setFormationMode(this->formationMode_);
196
197        Vector3* targetRelativePositionOfFollower;
198        switch (this->formationMode_){
199            case FormationMode::WALL:
200            {
201                targetRelativePositionOfFollower = new Vector3 (-400, 0, 0);   
202                break;
203            }
204            case FormationMode::FINGER4: 
205            {
206                targetRelativePositionOfFollower = new Vector3 (-400, 0, 200);   
207                break;
208            }
209           
210            case FormationMode::DIAMOND: 
211            {
212                targetRelativePositionOfFollower = new Vector3 (-400, 0, 200);                   
213                break;
214            }
215        }
216        Quaternion orient = this->getControllableEntity()->getWorldOrientation();
217       
218        Vector3 targetAbsolutePositionOfFollower = ((this->getControllableEntity()->getWorldPosition()) + 
219        (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfFollower)));
220       
221        myFollower_->setAction ( Action::FLY, targetAbsolutePositionOfFollower, orient );       
222    }
223
224
225    bool DivisionController::setWingman(CommonController* cwingman)
226    {
227
228        WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman);
229        if (!this->myWingman_)
230        {
231            this->myWingman_ = wingman;
232            return true;
233        }
234        else
235        {
236            return false;
237        }
238   
239    }
240    bool DivisionController::setFollower(LeaderController* myFollower)
241    {
242         if (!this->myFollower_)
243        {
244            this->myFollower_ = myFollower;
245            return true;
246        }
247        else
248        {
249            return false;
250        }
251    }
252    bool DivisionController::hasWingman()
253    {
254        if (this->myWingman_)
255            return true;
256        else
257            return false;
258    }
259    bool DivisionController::hasFollower()
260    {
261        if (this->myFollower_)
262            return true;
263        else
264            return false;
265    }
266
267
268   
269   
270
271}
Note: See TracBrowser for help on using the repository browser.