Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/AI_HS15/src/orxonox/controllers/WingmanController.cc @ 10826

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

some comments added

File size: 5.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    }
45
46    WingmanController::~WingmanController()
47    {
48
49    }
50 
51    void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
52    {
53        SUPER(WingmanController, XMLPort, xmlelement, mode);
54
55        //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
56    }
57   
58    //----in tick, move (or look) and shoot----
59    void WingmanController::tick(float dt)
60    {   
61        if (!this->isActive())
62            return;
63        if (this->bHasTargetPosition_)
64        {
65            this->moveToTargetPosition(dt);
66        }
67        else if (this->bLookAtTarget_)
68        {
69            this->lookAtTarget(dt);
70        }
71        if (bShooting_)
72        {
73            this->doFire();
74        }
75       
76        SUPER(WingmanController, tick, dt);
77    }
78   
79    //----action for hard calculations----
80    void WingmanController::action()
81    {
82        //----If no leader, find one----
83        if (!this->myLeader_)
84        {
85            CommonController* newLeader = findNewLeader();
86            this->myLeader_ = newLeader;
87            if (newLeader)
88            {
89                //orxout(internal_error) << "new Leader set" << endl;
90            }
91            //----If no leader found, attack someone----
92            //----TODO: find closest enemy----
93            else
94            {
95                if ( !this->hasTarget() || this->action_ != Action::FIGHT )
96                {
97                    for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
98                    {
99                        if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) )
100                            continue;   
101                        this->setAction(Action::FIGHT, (*itP));
102                        break;
103                    }
104                }
105            }
106        }
107        //----If have leader, he will deal with logic----
108        else
109        {
110
111        }
112
113
114        //----action was set to fight----
115        if (this->action_ == Action::FIGHT)
116        {
117            //----choose where to go----
118            this->maneuver();
119            //----fire if you can----
120            this->bShooting_ = this->canFire();
121        }
122        //----action was set to fly, leader handles the logic----
123        else if (this->action_ == Action::FLY)
124        {
125
126        }
127        //----TODO: implement protect----
128        else if (this->action_ == Action::PROTECT)
129        {
130
131        }
132         
133    }
134     
135   
136   
137    //----POST: closest leader that is ready to take a new wingman is returned----
138    CommonController* WingmanController::findNewLeader()
139    {
140
141        if (!this->getControllableEntity())
142            return 0;
143
144        //----vars for finding the closest leader----
145        CommonController* closestLeader = 0;
146        float minDistance =  std::numeric_limits<float>::infinity();
147
148        for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it)
149        {
150            //----0ptr or not a leader or dead?----
151            if (!it || 
152                (it->getRank() != Rank::SECTIONLEADER && it->getRank() != Rank::DIVISIONLEADER) || 
153                !(it->getControllableEntity()))
154                continue;
155           
156            //----same team?----
157            if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity()) )
158                continue;
159           
160            //----check distance----
161            float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity());
162            if (distance < minDistance && !(it->hasWingman()))
163            {
164                closestLeader = *it;
165                minDistance = distance;
166            }
167           
168        }
169        if (closestLeader)
170        {
171            //----Racing conditions----
172            if (closestLeader->setWingman(this))
173                return closestLeader;
174        }
175        return 0;
176    }
177
178
179
180
181}
Note: See TracBrowser for help on using the repository browser.