Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10883 was 10883, checked in by gania, 9 years ago

ships spread before fight

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