Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

synchronized AI with a static tick counter

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