Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/campaignHS15/src/orxonox/controllers/SectionController.cc @ 10915

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

little fix

File size: 11.4 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 "SectionController.h"
30//TODO: formation vectors are wrong, fix it.
31// split classes.
32// weaponsystem.
33//-> Math ?
34 
35namespace orxonox
36{
37
38    RegisterClass(SectionController);
39
40    //Leaders share the fact that they have Wingmans
41    SectionController::SectionController(Context* context) : ActionpointController(context)
42    {
43        RegisterObject(SectionController);
44        this->setFormationMode(FormationMode::FINGER4);
45
46        //this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&SectionController::action, this)));
47        this->myWingman_ = 0;
48        this->myDivisionLeader_ = 0;
49        this->bFirstAction_ = true;
50        //orxout(internal_error) << this << "Was created" << endl;
51
52    }
53   
54    SectionController::~SectionController()
55    {
56       for (size_t i = 0; i < this->actionpoints_.size(); ++i)
57        {
58            if(this->actionpoints_[i])
59                this->actionpoints_[i]->destroy();
60        }
61        this->parsedActionpoints_.clear();
62        this->actionpoints_.clear();
63    }
64    void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
65    {
66        SUPER(SectionController, XMLPort, xmlelement, mode);
67    }
68
69    //----in tick, move (or look) and shoot----
70    void SectionController::tick(float dt)
71    {
72        if (!this->isActive())
73            return;
74   
75        SUPER(SectionController, tick, dt);
76        if (this->timeOffset_ > 0.8f && this->timeOffset_ <= 1.6f && !this->bActionCalled_)
77        {
78            this->action();
79            this->bActionCalled_ = true;
80        }
81        if (this->timeOffset_ > 2.0f)
82        {
83            this->bActionCalled_ = false;
84        }
85
86    }
87
88    void SectionController::action()
89    {
90        if (!this || !this->getControllableEntity())
91            return;
92
93        //----If no leader, find one---- 
94        if (!myDivisionLeader_)
95        {
96            ActionpointController* newDivisionLeader = findNewDivisionLeader();
97            this->myDivisionLeader_ = newDivisionLeader;
98            //spread copyOrientation called equally among the division
99            if (this->myDivisionLeader_)
100            {
101                this->actionCounter_ = 5;
102            }
103        }
104        //----If have leader----
105        else
106        {
107        }
108        if (!myDivisionLeader_)
109        {
110            ActionpointController::action();
111            if (!this || !this->getControllableEntity())
112                return;
113            if (!(this->parsedActionpoints_.empty() && this->loopActionpoints_.empty()))
114            {
115                if (this->myWingman_)
116                {
117                    this->myWingman_->takeActionpoints(this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_);
118                }   
119            }
120        }
121        else if (myDivisionLeader_)
122        {
123            if (this->myDivisionLeader_->bKeepFormation_ || !(this->myDivisionLeader_->getAction() == Action::FIGHT
124                || this->myDivisionLeader_->getAction() == Action::FIGHTALL
125                || this->myDivisionLeader_->getAction() == Action::ATTACK))
126            {
127                this->keepFormation();
128                //orxout (internal_error) << "Keeping formation" << endl;
129
130            }
131            else if (!this->myDivisionLeader_->bKeepFormation_)
132            {
133                if (!this->hasTarget())
134                {
135                    this->chooseTarget(); 
136                    //orxout (internal_error) << "Section ain't got no target" << endl;
137                }
138                if (this->hasTarget())
139                {
140                    // this->maneuver();
141                    // this->bShooting_ = this->canFire();
142                    // Vector3 healthPosition = bestHealthPickup((this->target_->getWorldPosition() - this->getControllableEntity()->getWorldPosition()).length());
143                    // if ((this->getControllableEntity()->getWorldPosition() - healthPosition).length() < this->tolerance_)
144                    // {
145                    //     //----choose where to go----
146                    //     this->maneuver();
147                    // }
148                    // else
149                    // {
150                    //     this->dodgeTowards(healthPosition);
151                    // }
152                    // //----fire if you can----
153                    // this->bShooting_ = this->canFire();               
154                }
155            }
156        }
157
158        this->actionCounter_ += this->actionCounter_ < 100000 ? 1 : -this->actionCounter_ ;
159    }
160
161   
162    //PRE: myDivisionLeader_ != 0 && myDivisionLeader_->action_ == Action::FIGHT
163    //POST: this->target_ is set unless division leader doesn't have one
164    void SectionController::chooseTarget()
165    {
166        //----If division leader fights, cover him by fighting emenies close to his target----
167        Action::Value action = this->myDivisionLeader_->getAction();
168       
169        Pawn* target;
170        if (action == Action::FIGHT || action == Action::FIGHTALL || action == Action::ATTACK)
171        {
172            //----if he has a target----
173            if (this->myDivisionLeader_->hasTarget())
174            {
175                //----try to find a new target if division leader has wingman (doing fine) and no good target already set----
176                if ( this->myDivisionLeader_->hasWingman() && 
177                    !( this->hasTarget() && this->getTarget() != this->myDivisionLeader_->getTarget() ) )
178                {
179
180                    bool foundTarget = false;
181                    //----new target should be close to division's target----
182                    Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition();
183                    Gametype* gt = this->getGametype();
184                    for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
185                    {
186                        //----is enemy?----
187                        if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) )
188                            continue;           
189                        //----in range?----
190                        if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 && 
191                            (*itP) != this->myDivisionLeader_->getTarget())
192                        {
193                            foundTarget = true;
194                            target =  (*itP);
195                            //orxout(internal_error) << "Found target" << endl;
196                            break; 
197                        }
198                    }
199                    //----no target? then attack same target as division leader----
200                    if (!foundTarget)
201                    {
202                        target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget());
203                    }
204                }
205                //----if division leader doesn't have a wingman, support his fire----
206                else
207                {
208                    target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget());
209                }
210            }
211            //----If he fights but doesn't have a target, wait for him to get one----
212            else
213            {
214
215            }
216            this->setTarget (orxonox_cast<ControllableEntity*>(target));
217        }
218        else
219        {
220        } 
221    }
222    Vector3 SectionController::getFormationPosition ()
223    {
224        this->setFormationMode( this->myDivisionLeader_->getFormationMode() );
225        this->spread_ = this->myDivisionLeader_->getSpread();
226        Vector3* targetRelativePosition;
227        switch (this->formationMode_){
228            case FormationMode::WALL:
229            {
230                targetRelativePosition = new Vector3 (-2*this->spread_, 0, 0);   
231                break;
232            }
233            case FormationMode::FINGER4: 
234            {
235                targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_);   
236                break;
237            }
238           
239            case FormationMode::DIAMOND: 
240            {
241                targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_);                   
242                break;
243            }
244        }
245        Vector3 result = *targetRelativePosition;
246        delete targetRelativePosition;
247        return result;
248    }
249
250    void SectionController::keepFormation()
251    {
252        this->bKeepFormation_ = true;
253        ControllableEntity* leaderEntity = this->myDivisionLeader_->getControllableEntity();
254        Vector3 targetRelativePosition = this->getFormationPosition();
255        if (!leaderEntity)
256            return;
257        FlyingController::keepFormation(leaderEntity, targetRelativePosition);
258    }
259
260    ActionpointController* SectionController::findNewDivisionLeader()
261    {
262
263        if (!this->getControllableEntity())
264            return 0;
265
266        ActionpointController* closestLeader = 0;
267        float minDistance =  std::numeric_limits<float>::infinity();
268        //go through all pawns
269        for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>::begin(); it; ++it)
270        {
271            //0ptr or not DivisionController?
272            if (!(it) || !((it)->getIdentifier()->getName() == "DivisionController") || !(it->getControllableEntity()))
273                continue;
274            //same team?
275            if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam()))
276                continue;
277
278            //is equal to this?
279            if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity())
280                continue;
281
282            float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity());
283           
284            if (distance < minDistance && !(it->hasFollower()))
285            {
286                closestLeader = *it;
287                minDistance = distance;
288            }
289         
290        }
291        if (closestLeader)
292        {
293            if (closestLeader->setFollower(this))
294                return closestLeader;
295        }
296        return 0;
297    }
298
299    bool SectionController::setWingman(ActionpointController* newWingman)
300    {
301
302        if (!this->myWingman_)
303        {
304            this->myWingman_ = newWingman;
305            newWingman->takeActionpoints (this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_);
306            return true;
307        }
308        else
309        {
310            return false;
311        }
312    }
313   
314    bool SectionController::hasWingman()
315    {
316        if (this->myWingman_)
317            return true;
318        else
319            return false;
320    }
321}
Note: See TracBrowser for help on using the repository browser.