Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

created destructor functions and improved findNewLeader and findNewDivisionLeader

File size: 7.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    static const int RADIUS_TO_SEARCH_FOR_LEADER = 7000;
37    static const float ACTION_INTERVAL = 1.0f;
38    WingmanController::WingmanController(Context* context) : CommonController(context)
39    {
40        RegisterObject(WingmanController);
41        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&WingmanController::action, this)));
42    }
43
44    WingmanController::~WingmanController()
45    {
46        if (this->myLeader_)
47            this->myLeader_->myWingman_ = 0;
48    }
49
50    CommonController* WingmanController::findNewLeader()
51    {
52
53        if (!this->getControllableEntity())
54            return 0;
55
56        CommonController* closestLeader = 0;
57        float minDistance =  std::numeric_limits<float>::infinity();
58
59        for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it)
60        {
61            //0ptr?
62            if (!it || !it->isLeader() || !(it->getControllableEntity()))
63                continue;
64            //same team?
65            if (this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam())
66                continue;
67            //is equal to this?
68            if (it->getControllableEntity() == this->getControllableEntity())
69                continue;
70
71
72
73         
74
75            float distance = (it->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length();
76            if (distance < minDistance && !(it->hasWingman()))
77            {
78                closestLeader = *it;
79                minDistance = distance;
80            }
81            /*// is pawn in range?
82            if (distance < RADIUS_TO_SEARCH_FOR_LEADER)
83            {
84
85                if (it->setWingman(this))
86                    return *it;
87            }*/
88        }
89        if (closestLeader)
90        {
91            if (closestLeader->setWingman(this))
92                return closestLeader;
93        }
94        return 0;
95    }
96   
97    void WingmanController::action()
98    {
99        //this->target_ = this->sectionTarget_;
100        if (!this->myLeader_)
101        {
102            CommonController* newLeader = findNewLeader();
103            this->myLeader_ = newLeader;
104            if (newLeader)
105                orxout(internal_error) << "new Leader set" << endl;
106            else
107                orxout(internal_error) << "0 leader" << endl;
108
109        }
110        else
111        {
112
113            //orxout(internal_error) << "already have a Leader" << endl;
114
115        }
116    }
117 /*//collect data for AI behaviour
118            Vector3* meanOfEnemiesPtr = new Vector3(0.0,0.0,0.0);
119            Vector3* meanOfAlliesPtr  = new Vector3(0.0,0.0,0.0);
120            Vector3 meanOfAllies = *meanOfAlliesPtr;
121            Vector3 meanOfEnemies = *meanOfEnemiesPtr;
122
123
124            for (ObjectList<AIController>::iterator it = ObjectList<AIController>::begin(); it; ++it)
125            {
126
127                Gametype* gt=this->getGametype();
128                if (!gt)
129                {
130                    gt=it->getGametype();
131                }
132                if (!FormationController::sameTeam(this->getControllableEntity(), it->getControllableEntity(),gt))
133                {
134                    enemies_.push_back(*it);
135                }
136                else {
137                    allies_.push_back(*it);
138                }
139            }
140            if (enemies_.size() != 0 && allies_.size() != 0){
141                for (std::vector<WeakPtr<AIController> >::iterator it = enemies_.begin() ; it != enemies_.end(); ++it)
142                    meanOfEnemies += (*it)->getControllableEntity()->getWorldPosition();
143
144                meanOfEnemies /= enemies_.size();
145
146                for (std::vector<WeakPtr<AIController> >::iterator it = allies_.begin() ; it != allies_.end(); ++it)
147                    meanOfAllies += (*it)->getControllableEntity()->getWorldPosition();
148
149                meanOfAllies /= allies_.size();
150
151                //orxout(internal_error) << "There are " << enemies_Counter << " enemies_, mean position is " << meanOfEnemies << endl;
152                orxout(internal_error) << "Distance is " << (meanOfEnemies-meanOfAllies).length() << endl;
153                orxout(internal_error) << "mean of allies_ is " << meanOfAllies << ", with a size " << allies_.size() << endl;
154                orxout(internal_error) << "mean of enemies_ is " << meanOfEnemies << ", with a size " << enemies_.size() << endl;
155            }*/
156
157      /*  void FormationController::setDesiredPositionOfSlaves()
158    {
159        if (this->state_ != MASTER)
160            return;
161        switch (this->formationMode_){
162            case ATTACK:
163            {
164                float i = 0;
165                for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
166                {
167                    (*it)->desiredRelativePosition_ = new Vector3 ((i-slaves_.size()/2)*200, 0, 0);
168                    i++;
169                }
170                break;
171            }
172            case NORMAL:
173            {
174                break;
175            }
176            case DEFEND:
177            {
178                break;
179            }
180        }
181       
182    }*/
183    void WingmanController::tick(float dt)
184    {   
185        //-------------------------------------------------------
186           
187        /*
188        if (!this->isActive())
189            return;
190        //--------------------------Stay in formation--------------------------
191        if (bFollowLeader_)
192        {
193            this->keepSectionTick();
194           
195            keepSectionTick(){
196                if (this->sectionLeader_ && this->sectionLeader_->getControllableEntity() && desiredRelativePosition_){
197                    Vector3 desiredAbsolutePosition = ((this->sectionLeader_->getControllableEntity()->getWorldPosition()) +
198                        (this->sectionLeader_->getControllableEntity()->getWorldOrientation()* (*desiredRelativePosition_)));
199                    this->moveToPosition (desiredAbsolutePosition);
200                }
201            }
202           
203           
204            //--------------------------Attack same target as the Leader--------------------------
205
206            if (this->target_)
207            {
208                this->aimAtTarget();
209                this->doFire();
210            }
211        }
212        */
213         //orxout(internal_error) << "I am " << this << endl;
214
215       
216        SUPER(WingmanController, tick, dt);
217    }
218
219    void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
220    {
221        SUPER(WingmanController, XMLPort, xmlelement, mode);
222
223        //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
224    }
225
226//**********************************************NEW
227    /*void WingmanController::defaultBehaviour(float maxrand)
228    { 
229       
230    }*/
231
232}
Note: See TracBrowser for help on using the repository browser.