Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/campaignHS15/src/orxonox/controllers/WingmanController.cc @ 10858

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

should work, didn't compile

File size: 8.4 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 "WingmanController.h"
30
31
32namespace orxonox
33{
34
35    RegisterClass(WingmanController);
36   
37    //CommonController contains all common functionality of AI Controllers
38    WingmanController::WingmanController(Context* context) : CommonController(context)
39    {
40        RegisterObject(WingmanController);
41        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&WingmanController::action, this)));
42        this->myLeader_ = 0;
43        this->rank_ = Rank::WINGMAN;
44        this->bFirstAction_ = true;
45
46    }
47
48    WingmanController::~WingmanController()
49    {
50        for (size_t i = 0; i < this->actionpoints_.size(); ++i)
51        {
52            if(this->actionpoints_[i])
53                this->actionpoints_[i]->destroy();
54        }
55        this->parsedActionpoints_.clear();
56        this->actionpoints_.clear();
57    }
58 
59    void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
60    {
61        SUPER(WingmanController, XMLPort, xmlelement, mode);
62
63        //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
64    }
65   
66    //----in tick, move (or look) and shoot----
67    void WingmanController::tick(float dt)
68    {   
69        if (!this->isActive())
70            return;
71       
72       
73        SUPER(WingmanController, tick, dt);
74    }
75   
76    //----action for hard calculations----
77    void WingmanController::action()
78    {
79
80        //----If no leader, find one----
81        if (!this->myLeader_)
82        {
83            CommonController* newLeader = findNewLeader();
84            this->myLeader_ = newLeader;
85           
86        }
87        //----If have leader, he will deal with logic----
88        else
89        {
90
91        }
92        if (!this->myLeader_)
93        {
94           CommonController::action();
95        }
96        else if (this->myLeader_)
97        {
98            switch (this->myLeader_->getAction())
99            {
100                case Action::FIGHT:
101                {
102                    if (!this->hasTarget())
103                    {
104                        this->setTarget(this->myLeader_->getTarget());
105                    }
106                    break;
107                }
108                case Action::FIGHTALL:
109                {
110                    if (!this->hasTarget())
111                    {
112                        this->setTarget(this->myLeader_->getTarget());
113                    }
114                    break;
115                }
116                case Action::ATTACK:
117                {
118                    if (!this->hasTarget())
119                    {
120                        this->setTarget(this->myLeader_->getTarget());
121                    }
122                    break;
123                }
124                default:
125                {
126                    ControllableEntity* myEntity = this->getControllableEntity();
127                    Vector3 myPosition = myEntity->getWorldPosition();
128                    if (!this->myLeader_)
129                    {
130                        return;
131                    }
132                    ControllableEntity* leaderEntity = this->myLeader_->getControllableEntity();
133                    Quaternion orient = leaderEntity->getWorldOrientation();
134                    Vector3 leaderPosition = leaderEntity->getWorldPosition();
135
136                    Vector3 targetRelativePosition = getFormationPosition();
137                    if (!this->myLeader_)
138                    {
139                        return;
140                    }
141                    Vector3 targetAbsolutePosition = 
142                        (leaderPosition + (orient*WorldEntity::FRONT) * (leaderEntity->getVelocity().length()/5)
143                         + (orient* (targetRelativePosition)));
144               
145                    this->setAction (Action::FLY, targetAbsolutePosition, orient);
146                    if ((targetAbsolutePosition - myPosition).length() > this->tolerance_ * 1.5f)
147                    {
148                        this->boostControl();
149                    }
150                    else
151                    {
152                       this->getControllableEntity()->boost(false);
153                    }
154                }
155            }
156            if (this->hasTarget())
157            {
158                //----choose where to go----
159                this->maneuver();
160                //----fire if you can----
161                this->bShooting_ = this->canFire();               
162            }
163        }
164       
165    }
166     
167   
168    Vector3 WingmanController::getFormationPosition ()
169    {
170        this->setFormationMode( this->myLeader_->getFormationMode() );
171        Vector3* targetRelativePosition;
172
173        if (this->myLeader_->getRank() == Rank::DIVISIONLEADER)
174        {
175            switch (this->formationMode_){
176                case FormationMode::WALL:
177                {
178                    targetRelativePositionOfWingman = new Vector3 (400, 0, 0); 
179                    break;
180                }
181                case FormationMode::FINGER4: 
182                {
183                    targetRelativePositionOfWingman = new Vector3 (400, 0, 200); 
184                    break;
185                }
186                case FormationMode::DIAMOND: 
187                {
188                    targetRelativePositionOfWingman = new Vector3 (400, 0, 200);                 
189                    break;
190                }
191            }
192        }
193        else
194        {
195            switch (this->formationMode_){
196                case FormationMode::WALL:
197                {
198                    targetRelativePositionOfWingman = new Vector3 (-400, 0, 0); 
199                    break;
200                }
201                case FormationMode::FINGER4: 
202                {
203                    targetRelativePositionOfWingman = new Vector3 (-400, 0, 200); 
204                    break;
205                }
206                case FormationMode::DIAMOND: 
207                {
208                    targetRelativePositionOfWingman = new Vector3 (400, -200, 0);                 
209                    break;
210                }
211            }
212        }
213       
214        return *targetRelativePosition;
215    }
216    //----POST: closest leader that is ready to take a new wingman is returned----
217    CommonController* WingmanController::findNewLeader()
218    {
219
220        if (!this->getControllableEntity())
221            return 0;
222
223        //----vars for finding the closest leader----
224        CommonController* closestLeader = 0;
225        float minDistance =  std::numeric_limits<float>::infinity();
226        Gametype* gt = this->getGametype();
227        for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it)
228        {
229            //----0ptr or not a leader or dead?----
230            if (!it || 
231                (it->getRank() != Rank::SECTIONLEADER && it->getRank() != Rank::DIVISIONLEADER) || 
232                !(it->getControllableEntity()))
233                continue;
234           
235            //----same team?----
236            if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) )
237                continue;
238           
239            //----check distance----
240            float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity());
241            if (distance < minDistance && !(it->hasWingman()))
242            {
243                closestLeader = *it;
244                minDistance = distance;
245            }
246           
247        }
248        if (closestLeader)
249        {
250            //----Racing conditions----
251            if (closestLeader->setWingman(this))
252                return closestLeader;
253        }
254        return 0;
255    }
256
257
258
259
260}
Note: See TracBrowser for help on using the repository browser.