Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

improved dodging

File size: 10.2 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    }
77
78    void SectionController::action()
79    {
80        if (!this || !this->getControllableEntity())
81            return;
82
83        //----If no leader, find one---- 
84        if (!myDivisionLeader_)
85        {
86            ActionpointController* newDivisionLeader = findNewDivisionLeader();
87            this->myDivisionLeader_ = newDivisionLeader;
88            //spread copyOrientation called equally among the division
89            if (this->myDivisionLeader_)
90            {
91                this->actionCounter_ = 5;
92            }
93        }
94        //----If have leader----
95        else
96        {
97        }
98        if (!myDivisionLeader_)
99        {
100            ActionpointController::action();
101            if (!this || !this->getControllableEntity())
102                return;
103            if (!(this->parsedActionpoints_.empty() && this->loopActionpoints_.empty()))
104            {
105                if (this->myWingman_)
106                {
107                    this->myWingman_->takeActionpoints(this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_);
108                }   
109            }
110        }
111        else if (myDivisionLeader_)
112        {
113            if (this->myDivisionLeader_->bKeepFormation_ || !(this->myDivisionLeader_->getAction() == Action::FIGHT
114                || this->myDivisionLeader_->getAction() == Action::FIGHTALL
115                || this->myDivisionLeader_->getAction() == Action::ATTACK))
116            {
117                this->keepFormation();
118            }
119            else if (!this->myDivisionLeader_->bKeepFormation_)
120            {
121                if (!this->hasTarget())
122                {
123                    this->chooseTarget(); 
124                }
125                if (this->hasTarget())
126                {
127                    this->maneuver();
128                    this->bShooting_ = this->canFire();               
129                }
130            }
131        }
132        this->actionCounter_ += this->actionCounter_ < 100000 ? 1 : -this->actionCounter_ ;
133    }
134
135   
136    //PRE: myDivisionLeader_ != 0 && myDivisionLeader_->action_ == Action::FIGHT
137    //POST: this->target_ is set unless division leader doesn't have one
138    void SectionController::chooseTarget()
139    {
140        //----If division leader fights, cover him by fighting emenies close to his target----
141        Action::Value action = this->myDivisionLeader_->getAction();
142       
143        Pawn* target;
144        if (action == Action::FIGHT || action == Action::FIGHTALL || action == Action::ATTACK)
145        {
146            //----if he has a target----
147            if (this->myDivisionLeader_->hasTarget())
148            {
149                //----try to find a new target if division leader has wingman (doing fine) and no good target already set----
150                if ( this->myDivisionLeader_->hasWingman() && 
151                    !( this->hasTarget() && this->getTarget() != this->myDivisionLeader_->getTarget() ) )
152                {
153
154                    bool foundTarget = false;
155                    //----new target should be close to division's target----
156                    Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition();
157                    Gametype* gt = this->getGametype();
158                    for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
159                    {
160                        //----is enemy?----
161                        if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) )
162                            continue;           
163                        //----in range?----
164                        if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 && 
165                            (*itP) != this->myDivisionLeader_->getTarget())
166                        {
167                            foundTarget = true;
168                            target =  (*itP);
169                            //orxout(internal_error) << "Found target" << endl;
170                            break; 
171                        }
172                    }
173                    //----no target? then attack same target as division leader----
174                    if (!foundTarget)
175                    {
176                        target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget());
177                    }
178                }
179                //----if division leader doesn't have a wingman, support his fire----
180                else
181                {
182                    target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget());
183                }
184            }
185            //----If he fights but doesn't have a target, wait for him to get one----
186            else
187            {
188
189            }
190            this->setTarget (orxonox_cast<ControllableEntity*>(target));
191        }
192        else
193        {
194        } 
195    }
196    Vector3 SectionController::getFormationPosition ()
197    {
198        this->setFormationMode( this->myDivisionLeader_->getFormationMode() );
199        this->spread_ = this->myDivisionLeader_->getSpread();
200        Vector3* targetRelativePosition;
201        switch (this->formationMode_){
202            case FormationMode::WALL:
203            {
204                targetRelativePosition = new Vector3 (-2*this->spread_, 0, 0);   
205                break;
206            }
207            case FormationMode::FINGER4: 
208            {
209                targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_);   
210                break;
211            }
212           
213            case FormationMode::DIAMOND: 
214            {
215                targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_);                   
216                break;
217            }
218        }
219        Vector3 result = *targetRelativePosition;
220        delete targetRelativePosition;
221        return result;
222    }
223
224    void SectionController::keepFormation()
225    {
226        this->bKeepFormation_ = true;
227        ControllableEntity* leaderEntity = this->myDivisionLeader_->getControllableEntity();
228        Vector3 targetRelativePosition = this->getFormationPosition();
229        if (!leaderEntity)
230            return;
231        FlyingController::keepFormation(leaderEntity, targetRelativePosition);
232    }
233
234    ActionpointController* SectionController::findNewDivisionLeader()
235    {
236
237        if (!this->getControllableEntity())
238            return 0;
239
240        ActionpointController* closestLeader = 0;
241        float minDistance =  std::numeric_limits<float>::infinity();
242        //go through all pawns
243        for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>::begin(); it; ++it)
244        {
245            //0ptr or not DivisionController?
246            if (!(it) || !((it)->getIdentifier()->getName() == "DivisionController") || !(it->getControllableEntity()))
247                continue;
248            //same team?
249            if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam()))
250                continue;
251
252            //is equal to this?
253            if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity())
254                continue;
255
256            float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity());
257           
258            if (distance < minDistance && !(it->hasFollower()))
259            {
260                closestLeader = *it;
261                minDistance = distance;
262            }
263         
264        }
265        if (closestLeader)
266        {
267            if (closestLeader->setFollower(this))
268                return closestLeader;
269        }
270        return 0;
271    }
272    bool SectionController::setWingman(ActionpointController* newWingman)
273    {
274
275        if (!this->myWingman_)
276        {
277            this->myWingman_ = newWingman;
278            newWingman->takeActionpoints (this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_);
279            return true;
280        }
281        else
282        {
283            return false;
284        }
285    }
286   
287    bool SectionController::hasWingman()
288    {
289        if (this->myWingman_)
290            return true;
291        else
292            return false;
293    }
294}
Note: See TracBrowser for help on using the repository browser.