Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed library dependencies

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