Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed pointers

File size: 6.8 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        this->myLeader_ = 0;
43        this->desiredRelativePosition_ = 0;
44    }
45
46    WingmanController::~WingmanController()
47    {
48
49    }
50
51    CommonController* WingmanController::findNewLeader()
52    {
53
54        if (!this->getControllableEntity())
55            return 0;
56
57        CommonController* closestLeader = 0;
58        float minDistance =  std::numeric_limits<float>::infinity();
59
60        for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it)
61        {
62            //0ptr?
63            if (!it || !it->isLeader() || !(it->getControllableEntity()))
64                continue;
65            //same team?
66            if (this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam())
67                continue;
68            //is equal to this?
69            if (it->getControllableEntity() == this->getControllableEntity())
70                continue;
71
72            float distance = (it->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length();
73            if (distance < minDistance && !(it->hasWingman()))
74            {
75                closestLeader = *it;
76                minDistance = distance;
77            }
78           
79        }
80        if (closestLeader)
81        {
82            if (closestLeader->setWingman(this))
83                return closestLeader;
84        }
85        return 0;
86    }
87   
88    void WingmanController::action()
89    {
90        //this->target_ = this->sectionTarget_;
91        if (!this->myLeader_)
92        {
93            CommonController* newLeader = findNewLeader();
94            this->myLeader_ = newLeader;
95            if (newLeader)
96                orxout(internal_error) << "new Leader set" << endl;
97            else
98                orxout(internal_error) << "0 leader" << endl;
99
100        }
101        else
102        {
103
104        }
105    }
106 /*//collect data for AI behaviour
107            Vector3* meanOfEnemiesPtr = new Vector3(0.0,0.0,0.0);
108            Vector3* meanOfAlliesPtr  = new Vector3(0.0,0.0,0.0);
109            Vector3 meanOfAllies = *meanOfAlliesPtr;
110            Vector3 meanOfEnemies = *meanOfEnemiesPtr;
111
112
113            for (ObjectList<AIController>::iterator it = ObjectList<AIController>::begin(); it; ++it)
114            {
115
116                Gametype* gt=this->getGametype();
117                if (!gt)
118                {
119                    gt=it->getGametype();
120                }
121                if (!FormationController::sameTeam(this->getControllableEntity(), it->getControllableEntity(),gt))
122                {
123                    enemies_.push_back(*it);
124                }
125                else {
126                    allies_.push_back(*it);
127                }
128            }
129            if (enemies_.size() != 0 && allies_.size() != 0){
130                for (std::vector<WeakPtr<AIController> >::iterator it = enemies_.begin() ; it != enemies_.end(); ++it)
131                    meanOfEnemies += (*it)->getControllableEntity()->getWorldPosition();
132
133                meanOfEnemies /= enemies_.size();
134
135                for (std::vector<WeakPtr<AIController> >::iterator it = allies_.begin() ; it != allies_.end(); ++it)
136                    meanOfAllies += (*it)->getControllableEntity()->getWorldPosition();
137
138                meanOfAllies /= allies_.size();
139
140                //orxout(internal_error) << "There are " << enemies_Counter << " enemies_, mean position is " << meanOfEnemies << endl;
141                orxout(internal_error) << "Distance is " << (meanOfEnemies-meanOfAllies).length() << endl;
142                orxout(internal_error) << "mean of allies_ is " << meanOfAllies << ", with a size " << allies_.size() << endl;
143                orxout(internal_error) << "mean of enemies_ is " << meanOfEnemies << ", with a size " << enemies_.size() << endl;
144            }*/
145
146     
147    /*void WingmanController::keepSectionTick()
148    {
149        if (this->myLeader_ && this->myLeader_->getControllableEntity())
150                            //orxout(internal_error) << "MOVING" << endl;
151
152        if (this->myLeader_ && this->myLeader_->getControllableEntity() && desiredRelativePosition_)
153        {
154            Vector3 desiredAbsolutePosition = ((this->myLeader_->getControllableEntity()->getWorldPosition()) +
155                (this->myLeader_->getControllableEntity()->getWorldOrientation()* (*desiredRelativePosition_)));
156            this->moveToPosition (desiredAbsolutePosition);
157        }
158    }*/
159    void WingmanController::tick(float dt)
160    {   
161       /* //-------------------------------------------------------
162           
163       
164        if (!this->isActive())
165            return;
166        //--------------------------Stay in formation--------------------------
167        this->keepSectionTick();*/
168           
169       
170        //--------------------------Attack same target as the Leader--------------------------
171
172        /*if (this->target_)
173        {
174            this->aimAtTarget();
175            this->doFire();
176        }
177*/
178       
179        //orxout(internal_error) << "I am " << this << endl;
180
181       
182        SUPER(WingmanController, tick, dt);
183    }
184
185    void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
186    {
187        SUPER(WingmanController, XMLPort, xmlelement, mode);
188
189        //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
190    }
191
192//**********************************************NEW
193    /*void WingmanController::defaultBehaviour(float maxrand)
194    { 
195       
196    }*/
197
198}
Note: See TracBrowser for help on using the repository browser.