Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

finished everything, demos are commented out in AITest.oxw

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