Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10729 was 10729, checked in by gania, 10 years ago

move functions were added, everyone stays in formations

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