Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

defaultPatrol and defaultFightAll XML commands

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