Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Wingmen and Leaders look for their leaders

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