Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed a bug and got rid of unnecessary Rank enumeration, used getIdentifire instead

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