Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

small fixes

File size: 4.4 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
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
47    WingmanController::~WingmanController()
48    {
49
50    }
51
52    void WingmanController::tick(float dt)
53    {   
54        //-------------------------------------------------------
55       /* if (this->target_)
56        {
57            this->aimAtTarget();
58            this->doFire();
59            this->bShooting_ = true;
60        }*/
61       
62        if (!this->isActive())
63            return;
64        //--------------------------Stay in formation--------------------------
65        if (this->bHasTargetPosition_)
66        {
67            //targetPosition_ and targetOrientation_ are set by the Leader in its action()
68            this->moveToTargetPosition();
69        } 
70       
71        //--------------------------Attack same target as the Leader--------------------------
72
73        /*if (this->target_)
74        {
75            this->aimAtTarget();
76            this->doFire();
77        }
78        */
79       
80        //orxout(internal_error) << "I am " << this << endl;
81
82       
83        SUPER(WingmanController, tick, dt);
84    }
85   
86    void WingmanController::action()
87    {
88        if (!this->myLeader_)
89        {
90            CommonController* newLeader = findNewLeader();
91            this->myLeader_ = newLeader;
92            if (newLeader)
93                orxout(internal_error) << "new Leader set" << endl;
94            else
95            {
96                //orxout(internal_error) << "0 leader" << endl;
97               
98            }
99
100        }
101        else
102        {
103
104        }
105    }
106     
107   
108   
109
110    CommonController* WingmanController::findNewLeader()
111    {
112
113        if (!this->getControllableEntity())
114            return 0;
115
116        CommonController* closestLeader = 0;
117        float minDistance =  std::numeric_limits<float>::infinity();
118
119        for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it)
120        {
121            //0ptr?
122            if (!it || 
123                (it->getRank() != Rank::SECTIONLEADER && it->getRank() != Rank::DIVISIONLEADER) || 
124                !(it->getControllableEntity()))
125                continue;
126            //same team?
127            if (this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam())
128                continue;
129            //is equal to this?
130            if (it->getControllableEntity() == this->getControllableEntity())
131                continue;
132
133            float distance = (it->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length();
134            if (distance < minDistance && !(it->hasWingman()))
135            {
136                closestLeader = *it;
137                minDistance = distance;
138            }
139           
140        }
141        if (closestLeader)
142        {
143            if (closestLeader->setWingman(this))
144                return closestLeader;
145        }
146        return 0;
147    }
148
149    void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
150    {
151        SUPER(WingmanController, XMLPort, xmlelement, mode);
152
153        //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
154    }
155
156
157
158}
Note: See TracBrowser for help on using the repository browser.