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
RevLine 
[10719]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:
[10885]23 *      Gani Aliguzhinov
[10719]24 *   Co-authors:
[10885]25 *      ...
[10719]26 *
27 */
28
29#include "SectionController.h"
[10866]30//TODO: formation vectors are wrong, fix it.
31// split classes.
32// weaponsystem.
33//-> Math ?
34 
[10719]35namespace orxonox
36{
37
38    RegisterClass(SectionController);
39
[10826]40    //Leaders share the fact that they have Wingmans
[10886]41    SectionController::SectionController(Context* context) : ActionpointController(context)
[10719]42    {
43        RegisterObject(SectionController);
[10759]44        this->setFormationMode(FormationMode::FINGER4);
[10725]45
[10909]46        //this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&SectionController::action, this)));
[10725]47        this->myWingman_ = 0;
48        this->myDivisionLeader_ = 0;
[10851]49        this->bFirstAction_ = true;
[10826]50        //orxout(internal_error) << this << "Was created" << endl;
[10719]51
52    }
53   
54    SectionController::~SectionController()
55    {
[10854]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();
[10725]63    }
[10826]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----
[10731]70    void SectionController::tick(float dt)
71    {
[10843]72        if (!this->isActive())
[10731]73            return;
[10886]74   
[10731]75        SUPER(SectionController, tick, dt);
[10923]76        /*if (this->timeOffset_ > 0.8f && this->timeOffset_ <= 1.6f && !this->bActionCalled_)
[10909]77        {
78            this->action();
79            this->bActionCalled_ = true;
80        }
[10915]81        if (this->timeOffset_ > 2.0f)
[10909]82        {
83            this->bActionCalled_ = false;
[10923]84        }*/
[10915]85
[10731]86    }
87
88    void SectionController::action()
89    {
[10859]90        if (!this || !this->getControllableEntity())
91            return;
[10851]92
[10826]93        //----If no leader, find one---- 
[10731]94        if (!myDivisionLeader_)
95        {
[10885]96            ActionpointController* newDivisionLeader = findNewDivisionLeader();
[10925]97            if (!this || !this->getControllableEntity())
98                return;
99
[10731]100            this->myDivisionLeader_ = newDivisionLeader;
[10881]101            //spread copyOrientation called equally among the division
[10923]102
[10731]103        }
[10826]104        //----If have leader----
[10780]105        else
[10805]106        {
[10826]107        }
[10851]108        if (!myDivisionLeader_)
[10805]109        {
[10864]110            ActionpointController::action();
[10861]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            }
[10854]120        }
121        else if (myDivisionLeader_)
122        {
[10883]123            if (this->myDivisionLeader_->bKeepFormation_ || !(this->myDivisionLeader_->getAction() == Action::FIGHT
124                || this->myDivisionLeader_->getAction() == Action::FIGHTALL
125                || this->myDivisionLeader_->getAction() == Action::ATTACK))
[10805]126            {
[10886]127                this->keepFormation();
[10912]128                //orxout (internal_error) << "Keeping formation" << endl;
129
[10879]130            }
[10886]131            else if (!this->myDivisionLeader_->bKeepFormation_)
[10879]132            {
[10925]133                if (!this || !this->getControllableEntity())
134                    return;
135
[10886]136                if (!this->hasTarget())
[10883]137                {
[10913]138                    this->chooseTarget(); 
[10912]139                    //orxout (internal_error) << "Section ain't got no target" << endl;
[10851]140                }
[10883]141                if (this->hasTarget())
142                {
[10906]143                    // this->maneuver();
144                    // this->bShooting_ = this->canFire();
[10888]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();               
[10883]157                }
[10832]158            }
[10854]159        }
[10915]160
[10854]161    }
[10851]162
[10856]163   
[10832]164    //PRE: myDivisionLeader_ != 0 && myDivisionLeader_->action_ == Action::FIGHT
[10826]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----
[10854]169        Action::Value action = this->myDivisionLeader_->getAction();
[10923]170
[10854]171        if (action == Action::FIGHT || action == Action::FIGHTALL || action == Action::ATTACK)
[10826]172        {
[10923]173            Pawn* target;
[10826]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();
[10838]184                    Gametype* gt = this->getGametype();
[10826]185                    for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
186                    {
187                        //----is enemy?----
[10838]188                        if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) )
[10826]189                            continue;           
190                        //----in range?----
191                        if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 && 
192                            (*itP) != this->myDivisionLeader_->getTarget())
193                        {
194                            foundTarget = true;
[10854]195                            target =  (*itP);
[10826]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                    {
[10854]203                        target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget());
[10826]204                    }
205                }
206                //----if division leader doesn't have a wingman, support his fire----
207                else
208                {
[10854]209                    target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget());
[10826]210                }
211            }
212            //----If he fights but doesn't have a target, wait for him to get one----
213            else
214            {
215
216            }
[10858]217            this->setTarget (orxonox_cast<ControllableEntity*>(target));
[10854]218        }
219        else
220        {
[10826]221        } 
222    }
[10854]223    Vector3 SectionController::getFormationPosition ()
224    {
225        this->setFormationMode( this->myDivisionLeader_->getFormationMode() );
[10883]226        this->spread_ = this->myDivisionLeader_->getSpread();
[10854]227        Vector3* targetRelativePosition;
228        switch (this->formationMode_){
229            case FormationMode::WALL:
230            {
[10873]231                targetRelativePosition = new Vector3 (-2*this->spread_, 0, 0);   
[10854]232                break;
233            }
234            case FormationMode::FINGER4: 
235            {
[10873]236                targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_);   
[10854]237                break;
238            }
239           
240            case FormationMode::DIAMOND: 
241            {
[10873]242                targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_);                   
[10854]243                break;
244            }
245        }
[10880]246        Vector3 result = *targetRelativePosition;
247        delete targetRelativePosition;
248        return result;
[10854]249    }
[10826]250
[10886]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    }
[10826]260
[10885]261    ActionpointController* SectionController::findNewDivisionLeader()
[10719]262    {
263
264        if (!this->getControllableEntity())
[10722]265            return 0;
[10719]266
[10885]267        ActionpointController* closestLeader = 0;
[10722]268        float minDistance =  std::numeric_limits<float>::infinity();
[10719]269        //go through all pawns
[10885]270        for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>::begin(); it; ++it)
[10719]271        {
[10722]272            //0ptr or not DivisionController?
[10869]273            if (!(it) || !((it)->getIdentifier()->getName() == "DivisionController") || !(it->getControllableEntity()))
[10722]274                continue;
[10719]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
[10826]283            float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity());
[10722]284           
285            if (distance < minDistance && !(it->hasFollower()))
286            {
287                closestLeader = *it;
288                minDistance = distance;
289            }
[10729]290         
[10719]291        }
[10722]292        if (closestLeader)
293        {
294            if (closestLeader->setFollower(this))
295                return closestLeader;
296        }
297        return 0;
[10719]298    }
[10915]299
[10885]300    bool SectionController::setWingman(ActionpointController* newWingman)
[10731]301    {
[10719]302
[10731]303        if (!this->myWingman_)
[10719]304        {
[10877]305            this->myWingman_ = newWingman;
[10885]306            newWingman->takeActionpoints (this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_);
[10731]307            return true;
[10719]308        }
[10731]309        else
310        {
311            return false;
312        }
[10729]313    }
314   
[10731]315    bool SectionController::hasWingman()
[10725]316    {
[10731]317        if (this->myWingman_)
318            return true;
319        else
320            return false;
[10719]321    }
322}
Note: See TracBrowser for help on using the repository browser.