Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/AI_HS15/src/orxonox/controllers/WingmanController.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: 6.7 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
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
45    }
46
47    WingmanController::~WingmanController()
48    {
49
50    }
51    // void WingmanController::chooseManeuver()
52    // {
53    //     if (this->maneuverType_ == ManeuverType::NONE)
54    //     switch (this->maneuverType_ )
55    //     {
56    //         case ManeuverType::NONE:
57    //         {
58
59    //             break;
60    //         }
61    //         case ManeuverType::NEUTRAL:
62    //         {
63
64    //             break;
65    //         }
66    //         case ManeuverType::OFFENSIVE:
67    //         {
68
69    //             break;
70    //         }
71    //         case ManeuverType::DEFENSIVE:
72    //         {
73
74    //             break;
75    //         }
76    //     }
77    //     if (!this->myWingman_)
78    //         return;
79    //     Vector3* targetRelativePositionOfWingman;
80    //     switch (this->formationMode_){
81    //         case FormationMode::WALL:
82    //         {
83    //             targetRelativePositionOfWingman = new Vector3 (-400, 0, 0); 
84    //             break;
85    //         }
86    //         case FormationMode::FINGER4:
87    //         {
88    //             targetRelativePositionOfWingman = new Vector3 (-400, 0, -200); 
89    //             break;
90    //         }
91    //         case FormationMode::VEE:
92    //         {
93    //             break;
94    //         }
95    //         case FormationMode::DIAMOND:
96    //         {
97    //             targetRelativePositionOfWingman = new Vector3 (400, -200, 0);                 
98    //             break;
99    //         }
100    //     }
101    //     Quaternion orient = this->getControllableEntity()->getWorldOrientation();
102       
103    //     Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) +
104    //     (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman)));
105       
106    //     myWingman_->setTargetOrientation(orient);
107    //     myWingman_->setTargetPosition(targetAbsolutePositionOfWingman);
108       
109    // }
110
111    void WingmanController::tick(float dt)
112    {   
113        if (!this->isActive())
114            return;
115        if (this->bHasTargetPosition_)
116        {
117            this->moveToTargetPosition(dt);
118        }
119        else if (this->bLookAtTarget_)
120        {
121            this->lookAtTarget(dt);
122        }
123        if (bShooting_)
124        {
125            this->doFire();
126        }
127       
128        SUPER(WingmanController, tick, dt);
129    }
130   
131    void WingmanController::action()
132    {
133        if (!this->myLeader_)
134        {
135            CommonController* newLeader = findNewLeader();
136            this->myLeader_ = newLeader;
137            if (newLeader)
138                orxout(internal_error) << "new Leader set" << endl;
139            else
140            {
141                if (!this->target_){
142                    for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
143                    {
144                        if (this->getControllableEntity()->getTeam() == static_cast<ControllableEntity*>(*itP)->getTeam())
145                            continue;   
146                        this->setAction(Action::FIGHT, (*itP));
147                        break;
148                    }
149                }
150            }
151
152        }
153        else
154        {
155
156        }
157       
158        if (this->action_ == Action::FIGHT)
159        {
160            this->maneuver();
161            this->bShooting_ = this->canFire();
162            if (this->target_)
163            {
164                Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();         
165                if (diffVector.length() > 5000)
166                {
167                 
168                }   
169            }
170        }
171        else if (this->action_ == Action::FLY)
172        {
173
174        }
175        else if (this->action_ == Action::PROTECT)
176        {
177
178        }
179         
180    }
181     
182   
183   
184
185    CommonController* WingmanController::findNewLeader()
186    {
187
188        if (!this->getControllableEntity())
189            return 0;
190
191        CommonController* closestLeader = 0;
192        float minDistance =  std::numeric_limits<float>::infinity();
193
194        for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it)
195        {
196            //0ptr?
197            if (!it || 
198                (it->getRank() != Rank::SECTIONLEADER && it->getRank() != Rank::DIVISIONLEADER) || 
199                !(it->getControllableEntity()))
200                continue;
201            //same team?
202            if (this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam())
203                continue;
204            //is equal to this?
205            if (it->getControllableEntity() == this->getControllableEntity())
206                continue;
207
208            float distance = (it->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length();
209            if (distance < minDistance && !(it->hasWingman()))
210            {
211                closestLeader = *it;
212                minDistance = distance;
213            }
214           
215        }
216        if (closestLeader)
217        {
218            if (closestLeader->setWingman(this))
219                return closestLeader;
220        }
221        return 0;
222    }
223
224    void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
225    {
226        SUPER(WingmanController, XMLPort, xmlelement, mode);
227
228        //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
229    }
230
231
232
233}
Note: See TracBrowser for help on using the repository browser.