Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Finished groundwork: AI fights enemies like I want it to. TODO implement all the functionality of old AI, like Waypoints, XMLPort

File size: 7.3 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    void DivisionController::tick(float dt)
57    {   
58        if (!this->isActive())
59            return;
60        if (this->bHasTargetPosition_)
61        {
62            this->moveToTargetPosition(dt);
63        }
64        else if (this->bLookAtTarget_)
65        {
66            this->lookAtTarget(dt);
67        }
68        if (bShooting_)
69        {
70            this->doFire();
71        }
72       
73        SUPER(DivisionController, tick, dt);
74
75    }
76    void DivisionController::action()
77    {
78        if (!this->target_)
79        {
80            for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
81            {
82                if (this->getControllableEntity()->getTeam() == static_cast<ControllableEntity*>(*itP)->getTeam())
83                    continue;           
84
85               
86                if (static_cast<ControllableEntity*>(*itP) != (this)->getControllableEntity() && !(this)->hasTarget()
87                    && ((*itP)->getWorldPosition() - (this)->getControllableEntity()->getWorldPosition()).length() < 10000)
88                {
89                    (this)->setAction(Action::FIGHT, *itP);
90                }   
91            }
92        }
93       
94        if (this->action_ == Action::FIGHT)
95        {
96            this->maneuver();
97            this->bShooting_ = this->canFire();
98            if (this->target_)
99            {
100                if (this->myWingman_)
101                {
102                    this->myWingman_->setAction (Action::FIGHT, this->target_);                   
103                }
104               
105                Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();         
106                if (diffVector.length() > 3000)
107                {
108                    this->setTargetPositionOfWingman();
109                    //this->setTargetPositionOfFollower();                   
110                }   
111            }
112        }
113        else if (this->action_ == Action::FLY)
114        {
115            this->setTargetPositionOfWingman();
116            this->setTargetPositionOfFollower();
117        }
118        else if (this->action_ == Action::PROTECT)
119        {
120
121        }
122
123    }
124
125   
126
127    void DivisionController::setTargetPositionOfWingman()
128    {
129        if (!this->myWingman_)
130            return;
131        Vector3* targetRelativePositionOfWingman;
132        switch (this->formationMode_){
133            case FormationMode::WALL:
134            {
135                targetRelativePositionOfWingman = new Vector3 (400, 0, 0); 
136                break;
137            }
138            case FormationMode::FINGER4: 
139            {
140                targetRelativePositionOfWingman = new Vector3 (400, 0, -200); 
141                break;
142            }
143            case FormationMode::VEE: 
144            {
145                break;
146            }
147            case FormationMode::DIAMOND: 
148            {
149                targetRelativePositionOfWingman = new Vector3 (400, 0, -200);                 
150                break;
151            }
152        }
153        Quaternion orient = this->getControllableEntity()->getWorldOrientation();
154       
155        Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + 
156        (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman)));
157       
158        myWingman_->setAction( Action::FLY, targetAbsolutePositionOfWingman, orient);
159       
160    }
161    void DivisionController::setTargetPositionOfFollower()
162    {
163        if (!this->myFollower_)
164            return;
165        this->myFollower_->setFormationMode(this->formationMode_);
166
167        Vector3* targetRelativePositionOfFollower;
168        switch (this->formationMode_){
169            case FormationMode::WALL:
170            {
171                targetRelativePositionOfFollower = new Vector3 (-400, 0, 0);   
172                break;
173            }
174            case FormationMode::FINGER4: 
175            {
176                targetRelativePositionOfFollower = new Vector3 (-400, 0, -200);   
177                break;
178            }
179            case FormationMode::VEE: 
180            {
181                break;
182            }
183            case FormationMode::DIAMOND: 
184            {
185                targetRelativePositionOfFollower = new Vector3 (-400, 0, -200);                   
186                break;
187            }
188        }
189        Quaternion orient = this->getControllableEntity()->getWorldOrientation();
190       
191        Vector3 targetAbsolutePositionOfFollower = ((this->getControllableEntity()->getWorldPosition()) + 
192        (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfFollower)));
193       
194        myFollower_->setAction ( Action::FLY, targetAbsolutePositionOfFollower, orient );       
195    }
196
197
198    bool DivisionController::setWingman(CommonController* cwingman)
199    {
200
201        WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman);
202        if (!this->myWingman_)
203        {
204            this->myWingman_ = wingman;
205            return true;
206        }
207        else
208        {
209            return false;
210        }
211   
212    }
213    bool DivisionController::setFollower(LeaderController* myFollower)
214    {
215         if (!this->myFollower_)
216        {
217            this->myFollower_ = myFollower;
218            return true;
219        }
220        else
221        {
222            return false;
223        }
224    }
225    bool DivisionController::hasWingman()
226    {
227        if (this->myWingman_)
228            return true;
229        else
230            return false;
231    }
232    bool DivisionController::hasFollower()
233    {
234        if (this->myFollower_)
235            return true;
236        else
237            return false;
238    }
239
240
241    void DivisionController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
242    {
243        SUPER(DivisionController, XMLPort, xmlelement, mode);
244
245        //XMLPortParam(DivisionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
246    }
247
248   
249   
250
251}
Note: See TracBrowser for help on using the repository browser.