Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/campaignHS15/src/orxonox/controllers/WingmanController.cc @ 10851

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

works for the most part, still need to fix Section and Wingman

File size: 8.4 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 "WingmanController.h"
30
31
32namespace orxonox
33{
34
35    RegisterClass(WingmanController);
36   
37    //CommonController contains all common functionality of AI Controllers
38    WingmanController::WingmanController(Context* context) : CommonController(context)
39    {
40        RegisterObject(WingmanController);
41        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&WingmanController::action, this)));
42        this->myLeader_ = 0;
43        this->rank_ = Rank::WINGMAN;
44        this->bFirstAction_ = true;
45
46    }
47
48    WingmanController::~WingmanController()
49    {
50
51    }
52 
53    void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
54    {
55        SUPER(WingmanController, XMLPort, xmlelement, mode);
56
57        //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
58    }
59   
60    //----in tick, move (or look) and shoot----
61    void WingmanController::tick(float dt)
62    {   
63        if (!this->isActive())
64            return;
65       
66       
67        SUPER(WingmanController, tick, dt);
68    }
69   
70    //----action for hard calculations----
71    void WingmanController::action()
72    {
73
74        //----If no leader, find one----
75        if (!this->myLeader_)
76        {
77            CommonController* newLeader = findNewLeader();
78            this->myLeader_ = newLeader;
79           
80        }
81        //----If have leader, he will deal with logic----
82        else
83        {
84
85        }
86        if (!this->myLeader_)
87        {
88            bool b = this->startAttackingEnemiesThatAreClose();
89
90            if (this->action_ == Action::NONE)
91            {
92                this->executeActionpoint();
93            }
94            if (this->action_ == Action::FIGHT || this->action_ == Action::FIGHTALL)
95            {
96                if (!this->hasTarget())
97                {
98                    //----find a target----
99                    ControllableEntity* newTarget = this->closestTarget();
100                    if (this->action_ == Action::FIGHT)
101                    {
102                        if (newTarget && 
103                                CommonController::distance (this->getControllableEntity(), newTarget) < this->attackRange_)
104                        {
105                            this->setAction (Action::FIGHT, newTarget);
106                        }
107                        else
108                        {
109                            this->nextActionpoint();
110                            return;
111                        }
112                    }
113                    else if (this->action_ == Action::FIGHTALL)
114                    {
115                        if (newTarget && newTarget->getController())
116                        {
117                            this->setAction (Action::FIGHTALL, newTarget);
118                        }
119                        else
120                        {
121                            this->nextActionpoint();
122                            return;
123                        }
124                    }
125
126                }
127                else if (this->hasTarget())
128                {
129                    //----fly in formation if far enough----
130                    Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();         
131                       
132                    if (diffVector.length() > this->attackRange_)
133                    {
134                        if (this->action_ == Action::FIGHT)
135                        {
136                            this->nextActionpoint();
137                            return;
138                        }
139                    }
140                }
141            }
142            else if (this->action_ == Action::FLY)
143            {
144                if (this->squaredDistanceToTarget() <= this->squaredaccuracy_)
145                {
146                    this->nextActionpoint();
147                    return;
148                }
149            }
150            else if (this->action_ == Action::PROTECT)
151            {
152                if (!this->getProtect())
153                {
154                    this->nextActionpoint();
155                    return;
156                }
157
158                Vector3* targetRelativePosition;
159                   
160                targetRelativePosition = new Vector3 (0, 300, 300); 
161     
162                Vector3 targetAbsolutePosition = ((this->getProtect()->getWorldPosition()) + 
163                    (this->getProtect()->getWorldOrientation()* (*targetRelativePosition)));
164                this->setTargetPosition(targetAbsolutePosition);
165               
166
167            }
168            else if (this->action_ == Action::ATTACK)
169            {   
170                if (!this->hasTarget())
171                {
172                    this->nextActionpoint();
173                    return;
174                }
175                       
176            }
177            if (this->hasTarget())
178            {
179                //----choose where to go----
180                this->maneuver();
181                //----fire if you can----
182                this->bShooting_ = this->canFire();               
183            }
184        }
185        else
186        {
187            //----action was set to fight----
188            if (this->action_ == Action::FIGHT)
189            {
190                //----If no leader found, attack someone----
191                if (!this->hasTarget())
192                {
193                    this->setClosestTarget(); 
194                }
195                if (this->hasTarget())
196                {
197                    //----choose where to go----
198                    this->maneuver();
199                    //----fire if you can----
200                    this->bShooting_ = this->canFire();               
201                }
202            }
203            //----action was set to fly, leader handles the logic----
204            else if (this->action_ == Action::FLY)
205            {
206
207            }
208        } 
209        if (this->bFirstAction_ && this->myLeader_)
210        {
211            this->parsedActionpoints_ = this->myLeader_->parsedActionpoints_;
212            this->bFirstAction_ = false;
213        }
214    }
215     
216   
217   
218    //----POST: closest leader that is ready to take a new wingman is returned----
219    CommonController* WingmanController::findNewLeader()
220    {
221
222        if (!this->getControllableEntity())
223            return 0;
224
225        //----vars for finding the closest leader----
226        CommonController* closestLeader = 0;
227        float minDistance =  std::numeric_limits<float>::infinity();
228        Gametype* gt = this->getGametype();
229        for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it)
230        {
231            //----0ptr or not a leader or dead?----
232            if (!it || 
233                (it->getRank() != Rank::SECTIONLEADER && it->getRank() != Rank::DIVISIONLEADER) || 
234                !(it->getControllableEntity()))
235                continue;
236           
237            //----same team?----
238            if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) )
239                continue;
240           
241            //----check distance----
242            float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity());
243            if (distance < minDistance && !(it->hasWingman()))
244            {
245                closestLeader = *it;
246                minDistance = distance;
247            }
248           
249        }
250        if (closestLeader)
251        {
252            //----Racing conditions----
253            if (closestLeader->setWingman(this))
254                return closestLeader;
255        }
256        return 0;
257    }
258
259
260
261
262}
Note: See TracBrowser for help on using the repository browser.