Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

almost all works now, check AITest.oxw

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