Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/AI_HS15/src/orxonox/controllers/SectionController.cc @ 10805

Last change on this file since 10805 was 10805, checked in by gania, 9 years ago

Finished groundwork: AI fights enemies like I want it to. TODO implement all the functionality of old AI, like Waypoints, XMLPort

File size: 8.8 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:
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    SectionController::SectionController(Context* context) : LeaderController(context)
37    {
38        RegisterObject(SectionController);
[10759]39        this->setFormationMode(FormationMode::FINGER4);
[10725]40
[10719]41        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&SectionController::action, this)));
[10725]42        this->myWingman_ = 0;
43        this->myDivisionLeader_ = 0;
[10759]44        this->rank_ = Rank::SECTIONLEADER;
[10719]45
[10725]46        orxout(internal_error) << this << "Was created" << endl;
[10719]47
48    }
49   
50    SectionController::~SectionController()
51    {
[10725]52       
53    }
[10731]54
55    void SectionController::tick(float dt)
56    {
[10805]57         if (!this->isActive())
[10731]58            return;
59        if (this->bHasTargetPosition_)
60        {
[10789]61            this->moveToTargetPosition(dt);
[10731]62        }
[10805]63        else if (this->bLookAtTarget_)
64        {
65            this->lookAtTarget(dt);
66        }
67        if (bShooting_)
68        {
69            this->doFire();
70        }
[10731]71       
72        SUPER(SectionController, tick, dt);
73    }
74
75    void SectionController::action()
76    {
77        //this->target_ = this->sectionTarget_;       
78        if (!myDivisionLeader_)
79        {
80            LeaderController* newDivisionLeader = findNewDivisionLeader();
81            this->myDivisionLeader_ = newDivisionLeader;
82            if (newDivisionLeader)
83                orxout(internal_error) << "new DivisionLeader set" << endl;
[10759]84            else
85            {
[10805]86                for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
87                {
88                    if (this->getControllableEntity()->getTeam() == static_cast<ControllableEntity*>(*itP)->getTeam())
89                        continue;           
90
91                    if (!this->myDivisionLeader_)
92                    {
93                        this->setAction(Action::FIGHT, (*itP));
94                        break;
95                    }
96                   
97                }
[10759]98            }
[10731]99
100        }
[10780]101        else
[10805]102        {
103            if (this->myDivisionLeader_->getAction() == Action::FIGHT)
104            {
105                if (this->myDivisionLeader_->hasTarget())
106                {
107                    if (this->myDivisionLeader_->hasWingman() && (!this->hasTarget() || this->getTarget() == this->myDivisionLeader_->getTarget()))
108                    {
109                        bool foundTarget = false;
110                        Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition();
111                        for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
112                        {
113                            if (this->getControllableEntity()->getTeam() == static_cast<ControllableEntity*>(*itP)->getTeam())
114                                continue;           
115
116                            if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 &&
117                                ((*itP)->getWorldPosition() - divisionTargetPosition).length() > 1000 && 
118                                (*itP) != this->myDivisionLeader_->getTarget())
119                            {
120                                foundTarget = true;
121                                this->setAction(Action::FIGHT, (*itP));
122                                orxout(internal_error) << "Found target" << endl;
123                                break; 
124                            }
125                        }
126                        if (!foundTarget)
127                        {
128                            this->setAction(Action::FIGHT, this->myDivisionLeader_->getTarget());
129                        }
130
131                    }
132                }
133            }   
134        }
135       
136        if (this->action_ == Action::FIGHT)
137        {
138            this->maneuver();
139            this->bShooting_ = this->canFire();
140            if (this->target_)
141            {
142                if (this->myWingman_)
143                {
144                    this->myWingman_->setAction (Action::FIGHT, this->target_);                   
145                }
146                Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();         
147                if (diffVector.length() > 3000)
148                {
149                    this->setTargetPositionOfWingman();
150                }   
151            }
152        }
153        else if (this->action_ == Action::FLY)
154        {
155            this->setTargetPositionOfWingman();
156        }
157        else if (this->action_ == Action::PROTECT)
158        {
159
160        }
[10759]161               
[10731]162
163    }
164   
[10729]165    void SectionController::setTargetPositionOfWingman()
[10725]166    {
167        if (!this->myWingman_)
168            return;
[10729]169        Vector3* targetRelativePositionOfWingman;
[10725]170        switch (this->formationMode_){
[10759]171            case FormationMode::WALL:
[10725]172            {
[10729]173                targetRelativePositionOfWingman = new Vector3 (-400, 0, 0); 
[10725]174                break;
175            }
[10759]176            case FormationMode::FINGER4: 
[10725]177            {
[10759]178                targetRelativePositionOfWingman = new Vector3 (-400, 0, -200); 
[10725]179                break;
180            }
[10759]181            case FormationMode::VEE: 
[10725]182            {
183                break;
184            }
[10759]185            case FormationMode::DIAMOND: 
[10725]186            {
[10759]187                targetRelativePositionOfWingman = new Vector3 (400, -200, 0);                 
[10725]188                break;
189            }
[10719]190        }
[10729]191        Quaternion orient = this->getControllableEntity()->getWorldOrientation();
192       
193        Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + 
194        (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman)));
195       
[10805]196        myWingman_->setAction (Action::FLY, targetAbsolutePositionOfWingman, orient);
[10725]197       
[10719]198    }
[10722]199    LeaderController* SectionController::findNewDivisionLeader()
[10719]200    {
201
202        if (!this->getControllableEntity())
[10722]203            return 0;
[10719]204
[10722]205        LeaderController* closestLeader = 0;
206        float minDistance =  std::numeric_limits<float>::infinity();
[10719]207        //go through all pawns
208        for (ObjectList<LeaderController>::iterator it = ObjectList<LeaderController>::begin(); it; ++it)
209        {
[10722]210            //0ptr or not DivisionController?
[10759]211            if (!(it) || !((it)->getRank() == Rank::DIVISIONLEADER) || !(it->getControllableEntity()))
[10722]212                continue;
[10719]213            //same team?
214            if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam()))
215                continue;
216
217            //is equal to this?
218            if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity())
219                continue;
220
221
222            float distance = ((it)->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length();
[10722]223           
224            if (distance < minDistance && !(it->hasFollower()))
225            {
226                closestLeader = *it;
227                minDistance = distance;
228            }
[10729]229         
[10719]230        }
[10722]231        if (closestLeader)
232        {
233            if (closestLeader->setFollower(this))
234                return closestLeader;
235        }
236        return 0;
[10719]237
238    }
[10731]239    bool SectionController::setWingman(CommonController* cwingman)
240    {
241        WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman);
[10719]242
[10731]243        if (!this->myWingman_)
[10719]244        {
[10731]245            this->myWingman_ = wingman;
246            return true;
[10719]247        }
[10731]248        else
249        {
250            return false;
251        }
[10729]252    }
253   
[10731]254    bool SectionController::hasWingman()
[10725]255    {
[10731]256        if (this->myWingman_)
257            return true;
258        else
259            return false;
[10719]260    }
261
262    void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
263    {
264        SUPER(SectionController, XMLPort, xmlelement, mode);
265
266        //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
267    }
268
269   
270   
271
272}
Note: See TracBrowser for help on using the repository browser.