Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/AI_HS15/src/orxonox/controllers/DivisionController.cc @ 10796

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

Sometimes there are too many rotations

File size: 9.6 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    DivisionController::DivisionController(Context* context) : LeaderController(context)
38    {
39        RegisterObject(DivisionController);
40       
41        this->setFormationMode(FormationMode::DIAMOND);
42        this->target_ = 0;
43        this->myFollower_ = 0;
44        this->myWingman_ = 0;
45        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&DivisionController::action, this)));
46        this->rank_ = Rank::DIVISIONLEADER;
47
48
49    }
50
51    DivisionController::~DivisionController()
52    {
53     
54    } 
55
56   
57    void DivisionController::tick(float dt)
58    {
59        if (this->target_ )
60        {
61            this->setPositionOfTarget(target_->getWorldPosition());
62
63        }
64        /*if (this->target_)
65        {
66            this->aimAtTarget();
67            this->doFire();
68            this->bShooting_ = true;
69        }*/
70       
71       /* if (this->bHasTargetPosition_)
72        {
73            this->moveToTargetPosition();
74        }*/
75        if (executingMoveToPoint_)
76        {
77            executingMoveToPoint_ = !this->moveAndRoll(dt);
78        }
79        else
80        {
81            if (this->target_)
82            {
83                this->positionOfTarget_ = getPredictedPosition( 
84                    this->getControllableEntity()->getWorldPosition(), 
85                    hardcoded_projectile_speed, 
86                    this->target_->getWorldPosition(), 
87                    this->target_->getVelocity() 
88                    );
89                Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();
90                float diffLength = diffVector.length();
91               /* Quaternion rotationToTarget = (this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).getRotationTo(diffVector);
92                Vector3* targetPosition = new Vector3 ( 0, 0, -200 );
93                Vector3 target = rotationToTarget * (*targetPosition);*/
94                if (diffLength > 200)
95                    this->setTargetPosition( this->positionOfTarget_ - 0.6f*diffVector);
96               
97                else
98                    this->setTargetPosition( this->getControllableEntity()->getWorldPosition() );
99                executingMoveToPoint_ = true; 
100            }
101           
102           
103            if (this->maneuverType_ == ManeuverType::OFFENSIVE)
104            {
105                this->attack();
106                this->moveAndRoll(dt);
107            }
108        }
109        if (this->bShooting_)
110            doFire();
111        SUPER(DivisionController, tick, dt);
112
113    }
114    void DivisionController::action()
115    {
116/*
117        Vector3* pos = new Vector3(4000,1000,2000);
118        this->setTargetPosition(*pos);*/
119       
120/*        if (!this->target_)
121        {
122
123            for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
124            {
125                if (this->getControllableEntity()->getTeam() == static_cast<ControllableEntity*>(*it)->getTeam())
126                    continue;
127
128               
129
130                if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity())
131                {
132                    this->setTarget(*it);
133                }
134            }
135        }*/
136        //this->chooseManeuverType();
137        switch (this->maneuverType_)
138        {
139            case ManeuverType::NONE:
140            {
141                setTargetPositionOfFollower();
142                setTargetPositionOfWingman();
143                break;
144            }
145            case ManeuverType::DEFENCIVE:
146            {
147                this->gunsD();
148                break;
149            }
150            case ManeuverType::OFFENSIVE:
151            {
152                this->attack();
153                break;
154            }
155            case ManeuverType::NEUTRAL:
156            {
157                this->scissors();
158            }
159            default:
160            {
161                this->gunsD();
162                break;
163            }
164        }
165       
166        /*if (!executingMoveToPoint_)
167        {
168            Vector3* targetPosition = new Vector3 (0, 0, -2000);
169            moveToPoint(*targetPosition, 180);
170            executingMoveToPoint_ = true;
171        }
172        */
173        if (this->myFollower_ && this->target_)
174            this->myFollower_->setTarget(this->target_);
175        if (this->target_ && this->myWingman_)
176            this->myWingman_->setTarget(this->target_);
177    /*           
178        for (ObjectList<Controller>::iterator it = ObjectList<Controller>::begin(); it; ++it)
179        {
180            if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam()) && (it)->getControllableEntity()->getTeam() != 0)
181            {
182                this->setTargetPosition(it->getControllableEntity()->getWorldPosition());
183                             
184                break;
185            }
186        }
187           */
188        if (canFire())
189           this->bShooting_ = true;
190        else
191            this->bShooting_ = false;
192    }
193
194   
195
196    void DivisionController::setTargetPositionOfWingman()
197    {
198        if (!this->myWingman_)
199            return;
200        Vector3* targetRelativePositionOfWingman;
201        switch (this->formationMode_){
202            case FormationMode::WALL:
203            {
204                targetRelativePositionOfWingman = new Vector3 (400, 0, 0); 
205                break;
206            }
207            case FormationMode::FINGER4: 
208            {
209                targetRelativePositionOfWingman = new Vector3 (400, 0, -200); 
210                break;
211            }
212            case FormationMode::VEE: 
213            {
214                break;
215            }
216            case FormationMode::DIAMOND: 
217            {
218                targetRelativePositionOfWingman = new Vector3 (400, 0, -200);                 
219                break;
220            }
221        }
222        Quaternion orient = this->getControllableEntity()->getWorldOrientation();
223       
224        Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + 
225        (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman)));
226       
227        myWingman_->setTargetOrientation(orient);
228        myWingman_->setTargetPosition(targetAbsolutePositionOfWingman);
229       
230    }
231    void DivisionController::setTargetPositionOfFollower()
232    {
233        if (!this->myFollower_)
234            return;
235        this->myFollower_->setFormationMode(this->formationMode_);
236
237        Vector3* targetRelativePositionOfFollower;
238        switch (this->formationMode_){
239            case FormationMode::WALL:
240            {
241                targetRelativePositionOfFollower = new Vector3 (-400, 0, 0);   
242                break;
243            }
244            case FormationMode::FINGER4: 
245            {
246                targetRelativePositionOfFollower = new Vector3 (-400, 0, -200);   
247                break;
248            }
249            case FormationMode::VEE: 
250            {
251                break;
252            }
253            case FormationMode::DIAMOND: 
254            {
255                targetRelativePositionOfFollower = new Vector3 (-400, 0, -200);                   
256                break;
257            }
258        }
259        Quaternion orient = this->getControllableEntity()->getWorldOrientation();
260       
261        Vector3 targetAbsolutePositionOfFollower = ((this->getControllableEntity()->getWorldPosition()) + 
262        (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfFollower)));
263       
264        myFollower_->setTargetOrientation(orient);
265        myFollower_->setTargetPosition(targetAbsolutePositionOfFollower);
266       
267    }
268
269
270    bool DivisionController::setWingman(CommonController* cwingman)
271    {
272
273        WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman);
274        if (!this->myWingman_)
275        {
276            this->myWingman_ = wingman;
277            return true;
278        }
279        else
280        {
281            return false;
282        }
283   
284    }
285    bool DivisionController::setFollower(LeaderController* myFollower)
286    {
287         if (!this->myFollower_)
288        {
289            this->myFollower_ = myFollower;
290            return true;
291        }
292        else
293        {
294            return false;
295        }
296    }
297    bool DivisionController::hasWingman()
298    {
299        if (this->myWingman_)
300            return true;
301        else
302            return false;
303    }
304    bool DivisionController::hasFollower()
305    {
306        if (this->myFollower_)
307            return true;
308        else
309            return false;
310    }
311
312
313    void DivisionController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
314    {
315        SUPER(DivisionController, XMLPort, xmlelement, mode);
316
317        //XMLPortParam(DivisionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
318    }
319
320   
321   
322
323}
Note: See TracBrowser for help on using the repository browser.