Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

synchronized AI with a static tick counter

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        if (!this || !this->getControllableEntity())
117            return;
118        //----If no leader, find one----
119        if (!this->myLeader_)
120        {
121            ActionpointController* newLeader = (findNewLeader());
122            if (!this || !this->getControllableEntity())
123                return;
124
125            this->myLeader_ = newLeader;
126            if (this->myLeader_)
127            {
128               
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            if (!this || !this->getControllableEntity())
140                return;
141
142        }
143        else if (this->myLeader_)
144        {
145            if (this->myLeader_->bKeepFormation_ || !(this->myLeader_->getAction() == Action::FIGHT || this->myLeader_->getAction() == Action::FIGHTALL
146                || this->myLeader_->getAction() == Action::ATTACK))
147            {
148                this->keepFormation();
149            }
150            else if (!this->myLeader_->bKeepFormation_)
151            {
152                if (!this || !this->getControllableEntity())
153                    return;
154
155                if (!this->hasTarget())
156                {
157                    this->setTarget(this->myLeader_->getTarget());
158                }
159                if (this->hasTarget())
160                {
161                    // this->maneuver();
162                    // this->bShooting_ = this->canFire();
163                    // Vector3 healthPosition = bestHealthPickup((this->target_->getWorldPosition() - this->getControllableEntity()->getWorldPosition()).length());
164                    // if ((this->getControllableEntity()->getWorldPosition() - healthPosition).length() < this->tolerance_)
165                    // {
166                    //     //----choose where to go----
167                    //     this->maneuver();
168                    // }
169                    // else
170                    // {
171                    //     this->dodgeTowards(healthPosition);
172                    // }
173                    // //----fire if you can----
174                    // this->bShooting_ = this->canFire();               
175                }
176            }
177        }
178    }
179     
180   
181    Vector3 WingmanController::getFormationPosition ()
182    {
183
184
185        this->setFormationMode( this->myLeader_->getFormationMode() );
186        Vector3* targetRelativePosition;
187        this->spread_ = this->myLeader_->getSpread();
188        if (this->myLeader_->getIdentifier()->getName() == "DivisionController")
189        {
190            switch (this->formationMode_){
191                case FormationMode::WALL:
192                {
193                    targetRelativePosition = new Vector3 (2*this->spread_, 0, 0 - this->tolerance_); 
194                    break;
195                }
196                case FormationMode::FINGER4: 
197                {
198                    targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_); 
199                    break;
200                }
201                case FormationMode::DIAMOND: 
202                {
203                    targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_);                 
204                    break;
205                }
206            }
207        }
208        else
209        {
210
211            switch (this->formationMode_){
212                case FormationMode::WALL:
213                {
214                    targetRelativePosition = new Vector3 (-2*this->spread_, 0, 0 - this->tolerance_); 
215                    break;
216                }
217                case FormationMode::FINGER4: 
218                {
219                    targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_ - this->tolerance_); 
220                    break;
221                }
222                case FormationMode::DIAMOND: 
223                {
224                    targetRelativePosition = new Vector3 (2*this->spread_, -this->spread_, 0 - this->tolerance_);                 
225                    break;
226                }
227            }
228        }
229        Vector3 result = *targetRelativePosition;
230        delete targetRelativePosition;
231        return result;
232    }
233    void WingmanController::keepFormation()
234    {
235        this->bKeepFormation_ = true;
236        ControllableEntity* leaderEntity = this->myLeader_->getControllableEntity();
237        Vector3 targetRelativePosition = this->getFormationPosition();
238        if (!leaderEntity)
239            return;
240        FlyingController::keepFormation (leaderEntity, targetRelativePosition);
241    }
242    //----POST: closest leader that is ready to take a new wingman is returned----
243    ActionpointController* WingmanController::findNewLeader()
244    {
245
246        if (!this->getControllableEntity())
247            return 0;
248
249        //----vars for finding the closest leader----
250        ActionpointController* closestLeader = 0;
251        float minDistance =  std::numeric_limits<float>::infinity();
252        Gametype* gt = this->getGametype();
253
254        for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>::begin(); it; ++it)
255        {
256            //----0ptr or not a leader or dead?----
257            if (!it || 
258                (it->getIdentifier()->getName() != "SectionController" && it->getIdentifier()->getName() != "DivisionController") || 
259                !(it->getControllableEntity()))
260                continue;
261           
262            //----same team?----
263            if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) )
264                continue;
265           
266            //----check distance----
267            float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity());
268            if (distance < minDistance && !(it->hasWingman()))
269            {
270                closestLeader = *it;
271                minDistance = distance;
272            }
273        }
274        if (closestLeader)
275        {
276            //----Racing conditions----
277            if (closestLeader->setWingman(orxonox_cast<ActionpointController*>(this)))
278            {
279                if (closestLeader->getIdentifier()->getName() == "SectionController")
280                {
281                    this->actionTime_ = 1.6f;
282                }
283                else
284                {
285                    this->actionTime_ = 2.0f;
286                }
287                return closestLeader;
288            }
289        }
290        return 0;
291    }
292
293}
Note: See TracBrowser for help on using the repository browser.