Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

cleaned source up a bit

File size: 7.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->myLeader_ = 0;
42        this->bFirstAction_ = true;
43
44    }
45
46    WingmanController::~WingmanController()
47    {
48        for (size_t i = 0; i < this->actionpoints_.size(); ++i)
49        {
50            if(this->actionpoints_[i])
51                this->actionpoints_[i]->destroy();
52        }
53        this->parsedActionpoints_.clear();
54        this->actionpoints_.clear();
55    }
56 
57    void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
58    {
59        SUPER(WingmanController, XMLPort, xmlelement, mode);
60    }
61   
62    //----in tick, move (or look) and shoot----
63    void WingmanController::tick(float dt)
64    {   
65        if (!this->isActive())
66            return; 
67       
68        SUPER(WingmanController, tick, dt);
69
70    }
71   
72    //----action for hard calculations----
73    void WingmanController::action()
74    {
75        if (!this || !this->getControllableEntity())
76            return;
77        //----If no leader, find one----
78        if (!this->myLeader_)
79        {
80            ActionpointController* newLeader = (findNewLeader());
81            if (!this || !this->getControllableEntity())
82                return;
83
84            this->myLeader_ = newLeader;
85            if (this->myLeader_)
86            {
87               
88            }
89        }
90        //----If have leader, he will deal with logic----
91        else
92        {
93
94        }
95        if (!this->myLeader_)
96        {
97           ActionpointController::action();
98            if (!this || !this->getControllableEntity())
99                return;
100
101        }
102        else if (this->myLeader_)
103        {
104            if (this->myLeader_->bKeepFormation_ || !(this->myLeader_->getAction() == Action::FIGHT || this->myLeader_->getAction() == Action::FIGHTALL
105                || this->myLeader_->getAction() == Action::ATTACK))
106            {
107                this->keepFormation();
108            }
109            else if (!this->myLeader_->bKeepFormation_)
110            {
111                if (!this || !this->getControllableEntity())
112                    return;
113
114                if (!this->hasTarget())
115                {
116                    this->setTarget(this->myLeader_->getTarget());
117                }
118               
119            }
120        }
121    }
122     
123   
124    Vector3 WingmanController::getFormationPosition ()
125    {
126
127
128        this->setFormationMode( this->myLeader_->getFormationMode() );
129        Vector3* targetRelativePosition;
130        this->spread_ = this->myLeader_->getSpread();
131        if (this->myLeader_->getIdentifier()->getName() == "DivisionController")
132        {
133            switch (this->formationMode_){
134                case FormationMode::WALL:
135                {
136                    targetRelativePosition = new Vector3 (2*this->spread_, 0, 0 - this->tolerance_); 
137                    break;
138                }
139                case FormationMode::FINGER4: 
140                {
141                    targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_); 
142                    break;
143                }
144                case FormationMode::DIAMOND: 
145                {
146                    targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_);                 
147                    break;
148                }
149            }
150        }
151        else
152        {
153
154            switch (this->formationMode_){
155                case FormationMode::WALL:
156                {
157                    targetRelativePosition = new Vector3 (-2*this->spread_, 0, 0 - this->tolerance_); 
158                    break;
159                }
160                case FormationMode::FINGER4: 
161                {
162                    targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_ - this->tolerance_); 
163                    break;
164                }
165                case FormationMode::DIAMOND: 
166                {
167                    targetRelativePosition = new Vector3 (2*this->spread_, -this->spread_, 0 - this->tolerance_);                 
168                    break;
169                }
170            }
171        }
172        Vector3 result = *targetRelativePosition;
173        delete targetRelativePosition;
174        return result;
175    }
176    void WingmanController::keepFormation()
177    {
178        this->bKeepFormation_ = true;
179        ControllableEntity* leaderEntity = this->myLeader_->getControllableEntity();
180        Vector3 targetRelativePosition = this->getFormationPosition();
181        if (!leaderEntity)
182            return;
183        FlyingController::keepFormation (leaderEntity, targetRelativePosition);
184    }
185    //----POST: closest leader that is ready to take a new wingman is returned----
186    ActionpointController* WingmanController::findNewLeader()
187    {
188
189        if (!this->getControllableEntity())
190            return 0;
191
192        //----vars for finding the closest leader----
193        ActionpointController* closestLeader = 0;
194        float minDistance =  std::numeric_limits<float>::infinity();
195        Gametype* gt = this->getGametype();
196
197        for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>::begin(); it; ++it)
198        {
199            //----0ptr or not a leader or dead?----
200            if (!it || 
201                (it->getIdentifier()->getName() != "SectionController" && it->getIdentifier()->getName() != "DivisionController") || 
202                !(it->getControllableEntity()))
203                continue;
204           
205            //----same team?----
206            if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) )
207                continue;
208           
209            //----check distance----
210            float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity());
211            if (distance < minDistance && !(it->hasWingman()))
212            {
213                closestLeader = *it;
214                minDistance = distance;
215            }
216        }
217        if (closestLeader)
218        {
219            //----Racing conditions----
220            if (closestLeader->setWingman(orxonox_cast<ActionpointController*>(this)))
221            {
222                if (closestLeader->getIdentifier()->getName() == "SectionController")
223                {
224                    this->actionTime_ = 1.6f;
225                }
226                else
227                {
228                    this->actionTime_ = 2.0f;
229                }
230                return closestLeader;
231            }
232        }
233        return 0;
234    }
235
236}
Note: See TracBrowser for help on using the repository browser.