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, 8 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
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    SectionController::SectionController(Context* context) : LeaderController(context)
37    {
38        RegisterObject(SectionController);
39        this->setFormationMode(FormationMode::FINGER4);
40
41        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&SectionController::action, this)));
42        this->myWingman_ = 0;
43        this->myDivisionLeader_ = 0;
44        this->rank_ = Rank::SECTIONLEADER;
45
46        orxout(internal_error) << this << "Was created" << endl;
47
48    }
49   
50    SectionController::~SectionController()
51    {
52       
53    }
54
55    void SectionController::tick(float dt)
56    {
57         if (!this->isActive())
58            return;
59        if (this->bHasTargetPosition_)
60        {
61            this->moveToTargetPosition(dt);
62        }
63        else if (this->bLookAtTarget_)
64        {
65            this->lookAtTarget(dt);
66        }
67        if (bShooting_)
68        {
69            this->doFire();
70        }
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;
84            else
85            {
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                }
98            }
99
100        }
101        else
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        }
161               
162
163    }
164   
165    void SectionController::setTargetPositionOfWingman()
166    {
167        if (!this->myWingman_)
168            return;
169        Vector3* targetRelativePositionOfWingman;
170        switch (this->formationMode_){
171            case FormationMode::WALL:
172            {
173                targetRelativePositionOfWingman = new Vector3 (-400, 0, 0); 
174                break;
175            }
176            case FormationMode::FINGER4: 
177            {
178                targetRelativePositionOfWingman = new Vector3 (-400, 0, -200); 
179                break;
180            }
181            case FormationMode::VEE: 
182            {
183                break;
184            }
185            case FormationMode::DIAMOND: 
186            {
187                targetRelativePositionOfWingman = new Vector3 (400, -200, 0);                 
188                break;
189            }
190        }
191        Quaternion orient = this->getControllableEntity()->getWorldOrientation();
192       
193        Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + 
194        (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman)));
195       
196        myWingman_->setAction (Action::FLY, targetAbsolutePositionOfWingman, orient);
197       
198    }
199    LeaderController* SectionController::findNewDivisionLeader()
200    {
201
202        if (!this->getControllableEntity())
203            return 0;
204
205        LeaderController* closestLeader = 0;
206        float minDistance =  std::numeric_limits<float>::infinity();
207        //go through all pawns
208        for (ObjectList<LeaderController>::iterator it = ObjectList<LeaderController>::begin(); it; ++it)
209        {
210            //0ptr or not DivisionController?
211            if (!(it) || !((it)->getRank() == Rank::DIVISIONLEADER) || !(it->getControllableEntity()))
212                continue;
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();
223           
224            if (distance < minDistance && !(it->hasFollower()))
225            {
226                closestLeader = *it;
227                minDistance = distance;
228            }
229         
230        }
231        if (closestLeader)
232        {
233            if (closestLeader->setFollower(this))
234                return closestLeader;
235        }
236        return 0;
237
238    }
239    bool SectionController::setWingman(CommonController* cwingman)
240    {
241        WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman);
242
243        if (!this->myWingman_)
244        {
245            this->myWingman_ = wingman;
246            return true;
247        }
248        else
249        {
250            return false;
251        }
252    }
253   
254    bool SectionController::hasWingman()
255    {
256        if (this->myWingman_)
257            return true;
258        else
259            return false;
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.