Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

check in

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