Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

check in

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 *      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        if (!this || !this->getControllableEntity())
117            return;
118        //----If no leader, find one----
119        if (!this->myLeader_)
120        {
121            ActionpointController* newLeader = (findNewLeader());
122            this->myLeader_ = newLeader;
123            if (this->myLeader_)
124            {
125               
126            }
127        }
128        //----If have leader, he will deal with logic----
129        else
130        {
131
132        }
133        if (!this->myLeader_)
134        {
135           ActionpointController::action();
136            if (!this || !this->getControllableEntity())
137                return;
138
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    }
173     
174   
175    Vector3 WingmanController::getFormationPosition ()
176    {
177        this->setFormationMode( this->myLeader_->getFormationMode() );
178        Vector3* targetRelativePosition;
179        this->spread_ = this->myLeader_->getSpread();
180        if (this->myLeader_->getIdentifier()->getName() == "DivisionController")
181        {
182            switch (this->formationMode_){
183                case FormationMode::WALL:
184                {
185                    targetRelativePosition = new Vector3 (2*this->spread_, 0, 0 - this->tolerance_); 
186                    break;
187                }
188                case FormationMode::FINGER4: 
189                {
190                    targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_); 
191                    break;
192                }
193                case FormationMode::DIAMOND: 
194                {
195                    targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_);                 
196                    break;
197                }
198            }
199        }
200        else
201        {
202
203            switch (this->formationMode_){
204                case FormationMode::WALL:
205                {
206                    targetRelativePosition = new Vector3 (-2*this->spread_, 0, 0 - this->tolerance_); 
207                    break;
208                }
209                case FormationMode::FINGER4: 
210                {
211                    targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_ - this->tolerance_); 
212                    break;
213                }
214                case FormationMode::DIAMOND: 
215                {
216                    targetRelativePosition = new Vector3 (2*this->spread_, -this->spread_, 0 - this->tolerance_);                 
217                    break;
218                }
219            }
220        }
221        Vector3 result = *targetRelativePosition;
222        delete targetRelativePosition;
223        return result;
224    }
225    void WingmanController::keepFormation()
226    {
227        this->bKeepFormation_ = true;
228        ControllableEntity* leaderEntity = this->myLeader_->getControllableEntity();
229        Vector3 targetRelativePosition = this->getFormationPosition();
230        if (!leaderEntity)
231            return;
232        FlyingController::keepFormation (leaderEntity, targetRelativePosition);
233    }
234    //----POST: closest leader that is ready to take a new wingman is returned----
235    ActionpointController* WingmanController::findNewLeader()
236    {
237
238        if (!this->getControllableEntity())
239            return 0;
240
241        //----vars for finding the closest leader----
242        ActionpointController* closestLeader = 0;
243        float minDistance =  std::numeric_limits<float>::infinity();
244        Gametype* gt = this->getGametype();
245
246        for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>::begin(); it; ++it)
247        {
248            //----0ptr or not a leader or dead?----
249            if (!it || 
250                (it->getIdentifier()->getName() != "SectionController" && it->getIdentifier()->getName() != "DivisionController") || 
251                !(it->getControllableEntity()))
252                continue;
253           
254            //----same team?----
255            if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) )
256                continue;
257           
258            //----check distance----
259            float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity());
260            if (distance < minDistance && !(it->hasWingman()))
261            {
262                closestLeader = *it;
263                minDistance = distance;
264            }
265        }
266        if (closestLeader)
267        {
268            //----Racing conditions----
269            if (closestLeader->setWingman(orxonox_cast<ActionpointController*>(this)))
270            {
271                if (closestLeader->getIdentifier()->getName() == "SectionController")
272                {
273                    this->actionTime_ = 1.6f;
274                }
275                else
276                {
277                    this->actionTime_ = 2.0f;
278                }
279                return closestLeader;
280            }
281        }
282        return 0;
283    }
284
285}
Note: See TracBrowser for help on using the repository browser.