Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

started working on pickups

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