Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

minor bugfixes and code style improvement

File size: 4.9 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           
88        }
89        //----If have leader, he will deal with logic----
90        else
91        {
92            this->action_ = this->myLeader_->getAction();
93        }
94
95
96        //----action was set to fight----
97        if (this->action_ == Action::FIGHT)
98        {
99            //----If no leader found, attack someone----
100            if (!this->hasTarget() && !this->myLeader_)
101            {
102                this->setClosestTarget(); 
103            }
104            if (this->hasTarget())
105            {
106                //----choose where to go----
107                this->maneuver();
108                //----fire if you can----
109                this->bShooting_ = this->canFire();               
110            }
111        }
112        //----action was set to fly, leader handles the logic----
113        else if (this->action_ == Action::FLY)
114        {
115
116        }
117        //----gani-TODO: implement protect----
118        else if (this->action_ == Action::PROTECT)
119        {
120
121        }
122         
123    }
124     
125   
126   
127    //----POST: closest leader that is ready to take a new wingman is returned----
128    CommonController* WingmanController::findNewLeader()
129    {
130
131        if (!this->getControllableEntity())
132            return 0;
133
134        //----vars for finding the closest leader----
135        CommonController* closestLeader = 0;
136        float minDistance =  std::numeric_limits<float>::infinity();
137
138        for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it)
139        {
140            //----0ptr or not a leader or dead?----
141            if (!it || 
142                (it->getRank() != Rank::SECTIONLEADER && it->getRank() != Rank::DIVISIONLEADER) || 
143                !(it->getControllableEntity()))
144                continue;
145           
146            //----same team?----
147            if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity()) )
148                continue;
149           
150            //----check distance----
151            float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity());
152            if (distance < minDistance && !(it->hasWingman()))
153            {
154                closestLeader = *it;
155                minDistance = distance;
156            }
157           
158        }
159        if (closestLeader)
160        {
161            //----Racing conditions----
162            if (closestLeader->setWingman(this))
163                return closestLeader;
164        }
165        return 0;
166    }
167
168
169
170
171}
Note: See TracBrowser for help on using the repository browser.