Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

little fix

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