Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

ok, now you can use attack and protect just like in my level, but I only updated DivsionController, will finish others some time soon. Anyways, Actionpoints are to be inserted in DivisionController only.

File size: 12.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 "DivisionController.h"
30#include "infos/PlayerInfo.h"
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        //----Whatever ship is doing, if there are targets close to it and its own target is far away, fight them----
77        //analog to attack move
78        if (this->action_ != Action::FIGHT && this->action_ != Action::FIGHTALL)
79        {
80            if ( (this->target_ && CommonController::distance (this->getControllableEntity(), this->target_) > this->attackRange_) 
81                || !this->target_ )
82            {
83                Pawn* newTarget = this->closestTarget();
84                if ( newTarget && 
85                    CommonController::distance (this->getControllableEntity(), static_cast<ControllableEntity*>(newTarget))
86                        <= this->attackRange_ )
87                {
88                    // this->backupAction();
89                    // this->setAction (Action::FIGHT, newTarget);
90                    Point p = { Action::FIGHT, CommonController::getName(newTarget), Vector3::ZERO };
91                    this->parsedActionpoints_.push_back(p);
92                    this->executeActionpoint();
93                }
94            }
95        }
96
97        //action is NONE whenever ships finishes current action,
98        //if it was fighting before because enemies were close, resume what it was doing
99        //otherwise try to execute next action
100        if (this->action_ == Action::NONE)
101        {
102            this->executeActionpoint();
103        }
104
105
106        //this->action_ is what I am actually executing, this->target_ is what I am
107        //actually attacking, etc.
108
109        //after action is finished, .pop_back() is to be called.
110        if (this->action_ == Action::FIGHT || this->action_ == Action::FIGHTALL)
111        {
112            if (!this->hasTarget())
113            {
114                //----find a target----
115                ControllableEntity* newTarget = this->closestTarget();
116                if (this->action_ == Action::FIGHT)
117                {
118                    if (newTarget && CommonController::distance (this->getControllableEntity(), newTarget) < this->attackRange_)
119                    {
120                        this->setAction (Action::FIGHT, newTarget);
121                    }
122                    else
123                    {
124                        this->nextActionpoint();
125                        return;
126                    }
127                }
128                else if (this->action_ == Action::FIGHTALL)
129                {
130                    if (newTarget && newTarget->getController())
131                    {
132                        this->setAction (Action::FIGHTALL, newTarget);
133                    }
134                    else
135                    {
136                        this->nextActionpoint();
137                        return;
138                    }
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               
147                   
148                if (diffVector.length() > this->attackRange_)
149                {
150                    if (this->action_ == Action::FIGHT)
151                    {
152                        this->nextActionpoint();
153                        return;
154                    }
155                    else
156                    {
157                        this->setTargetPositionOfWingman();
158                        this->setTargetPositionOfFollower();                   
159                    }
160
161                }
162                else
163                {
164                    //----wingmans shall support the fire of their leaders----
165                    if (this->myWingman_)
166                    {
167                        this->myWingman_->setAction (this->action_, this->target_);     
168                    }
169                    if (this->myFollower_)
170                    {
171                        this->myFollower_->setAction (this->action_);                                   
172                    }
173
174                }
175               
176            }
177            if (this->hasTarget())
178            {
179                //----choose where to go----
180                this->maneuver();
181                //----fire if you can----
182                this->bShooting_ = this->canFire();               
183            }
184
185        }
186        else if (this->action_ == Action::FLY)
187        {
188
189            if (this->squaredDistanceToTarget() <= this->squaredaccuracy_)
190            {
191                orxout(internal_error) << "arrived";
192                this->nextActionpoint();
193                return;
194            }
195            this->setTargetPositionOfWingman();
196            this->setTargetPositionOfFollower();
197        }
198        else if (this->action_ == Action::PROTECT)
199        {
200            if (!this->getProtect())
201            {
202                this->nextActionpoint();
203                return;
204            }
205           /* if (this->myWingman_)
206                this->myWingman_->setAction (Action::PROTECT, this->getProtect());
207            if (this->myFollower_)
208                this->myFollower_->setAction (Action::PROTECT, this->getProtect());
209            */
210            Vector3* targetRelativePosition;
211               
212            targetRelativePosition = new Vector3 (0, 0, 500); 
213 
214            Vector3 targetAbsolutePosition = ((this->getProtect()->getWorldPosition()) + 
215                (this->getProtect()->getWorldOrientation()* (*targetRelativePosition)));
216            this->setTargetPosition(targetAbsolutePosition);
217           
218            this->setTargetPositionOfWingman();
219            this->setTargetPositionOfFollower();
220
221        }
222        else if (this->action_ == Action::ATTACK)
223        {   
224            if (!this->hasTarget())
225            {
226                this->nextActionpoint();
227                return;
228            }
229            //----fly in formation if far enough----
230            Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();         
231            if (diffVector.length() > this->attackRange_)
232            {
233                this->setTargetPositionOfWingman();
234                this->setTargetPositionOfFollower();                   
235            }
236            else
237            {
238                //----wingmans shall support the fire of their leaders----
239                if (this->myWingman_)
240                {
241                    this->myWingman_->setAction (this->action_, this->target_);     
242                }
243                if (this->myFollower_)
244                {
245                    this->myFollower_->setAction (this->action_);                                   
246                }
247
248            }
249           
250            //----choose where to go----
251            this->maneuver();
252            //----fire if you can----
253            this->bShooting_ = this->canFire();               
254        }
255
256    }
257
258   
259
260    void DivisionController::setTargetPositionOfWingman()
261    {
262        if (!this->myWingman_)
263            return;
264        Vector3* targetRelativePositionOfWingman;
265        switch (this->formationMode_){
266            case FormationMode::WALL:
267            {
268                targetRelativePositionOfWingman = new Vector3 (400, 0, 0); 
269                break;
270            }
271            case FormationMode::FINGER4: 
272            {
273                targetRelativePositionOfWingman = new Vector3 (400, 0, 200); 
274                break;
275            }
276         
277            case FormationMode::DIAMOND: 
278            {
279                targetRelativePositionOfWingman = new Vector3 (400, 0, 200);                 
280                break;
281            }
282        }
283        Quaternion orient = this->getControllableEntity()->getWorldOrientation();
284       
285        Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + 
286        (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman)));
287       
288        myWingman_->setAction( Action::FLY, targetAbsolutePositionOfWingman, orient);
289       
290    }
291    void DivisionController::setTargetPositionOfFollower()
292    {
293        if (!this->myFollower_)
294            return;
295        this->myFollower_->setFormationMode(this->formationMode_);
296
297        Vector3* targetRelativePositionOfFollower;
298        switch (this->formationMode_){
299            case FormationMode::WALL:
300            {
301                targetRelativePositionOfFollower = new Vector3 (-400, 0, 0);   
302                break;
303            }
304            case FormationMode::FINGER4: 
305            {
306                targetRelativePositionOfFollower = new Vector3 (-400, 0, 200);   
307                break;
308            }
309           
310            case FormationMode::DIAMOND: 
311            {
312                targetRelativePositionOfFollower = new Vector3 (-400, 0, 200);                   
313                break;
314            }
315        }
316        Quaternion orient = this->getControllableEntity()->getWorldOrientation();
317       
318        Vector3 targetAbsolutePositionOfFollower = ((this->getControllableEntity()->getWorldPosition()) + 
319        (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfFollower)));
320       
321        myFollower_->setAction ( Action::FLY, targetAbsolutePositionOfFollower, orient );       
322    }
323
324
325    bool DivisionController::setWingman(CommonController* cwingman)
326    {
327
328        WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman);
329        if (!this->myWingman_)
330        {
331            this->myWingman_ = wingman;
332            return true;
333        }
334        else
335        {
336            return false;
337        }
338   
339    }
340    bool DivisionController::setFollower(LeaderController* myFollower)
341    {
342         if (!this->myFollower_)
343        {
344            this->myFollower_ = myFollower;
345            return true;
346        }
347        else
348        {
349            return false;
350        }
351    }
352    bool DivisionController::hasWingman()
353    {
354        if (this->myWingman_)
355            return true;
356        else
357            return false;
358    }
359    bool DivisionController::hasFollower()
360    {
361        if (this->myFollower_)
362            return true;
363        else
364            return false;
365    }
366
367
368   
369   
370
371}
Note: See TracBrowser for help on using the repository browser.