Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/AI_HS15/src/orxonox/controllers/SectionController.cc @ 10789

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

did nothing today

File size: 6.0 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 "SectionController.h"
30
31namespace orxonox
32{
33
34    RegisterClass(SectionController);
35
36    SectionController::SectionController(Context* context) : LeaderController(context)
37    {
38        RegisterObject(SectionController);
39        this->setFormationMode(FormationMode::FINGER4);
40
41        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&SectionController::action, this)));
42        this->myWingman_ = 0;
43        this->myDivisionLeader_ = 0;
44        this->rank_ = Rank::SECTIONLEADER;
45
46        orxout(internal_error) << this << "Was created" << endl;
47
48    }
49   
50    SectionController::~SectionController()
51    {
52       
53    }
54
55    void SectionController::tick(float dt)
56    {
57        if (!this->isActive())
58            return;
59       
60       
61        if (this->bHasTargetPosition_)
62        {
63            this->moveToTargetPosition(dt);
64        }
65        if (this->bShooting_)
66            doFire();
67       
68        SUPER(SectionController, tick, dt);
69    }
70
71    void SectionController::action()
72    {
73        //this->target_ = this->sectionTarget_;       
74        if (!myDivisionLeader_)
75        {
76            LeaderController* newDivisionLeader = findNewDivisionLeader();
77            this->myDivisionLeader_ = newDivisionLeader;
78            if (newDivisionLeader)
79                orxout(internal_error) << "new DivisionLeader set" << endl;
80            else
81            {
82               
83            }
84
85        }
86        setTargetPositionOfWingman();
87        if (this->target_ && this->myWingman_)
88            this->myWingman_->setTarget(this->target_);
89        if (canFire())
90           this->bShooting_ = true;
91        else
92            this->bShooting_ = false;
93               
94
95    }
96   
97    void SectionController::setTargetPositionOfWingman()
98    {
99        if (!this->myWingman_)
100            return;
101        Vector3* targetRelativePositionOfWingman;
102        switch (this->formationMode_){
103            case FormationMode::WALL:
104            {
105                targetRelativePositionOfWingman = new Vector3 (-400, 0, 0); 
106                break;
107            }
108            case FormationMode::FINGER4: 
109            {
110                targetRelativePositionOfWingman = new Vector3 (-400, 0, -200); 
111                break;
112            }
113            case FormationMode::VEE: 
114            {
115                break;
116            }
117            case FormationMode::DIAMOND: 
118            {
119                targetRelativePositionOfWingman = new Vector3 (400, -200, 0);                 
120                break;
121            }
122        }
123        Quaternion orient = this->getControllableEntity()->getWorldOrientation();
124       
125        Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + 
126        (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman)));
127       
128        myWingman_->setTargetOrientation(orient);
129        myWingman_->setTargetPosition(targetAbsolutePositionOfWingman);
130       
131    }
132    LeaderController* SectionController::findNewDivisionLeader()
133    {
134
135        if (!this->getControllableEntity())
136            return 0;
137
138        LeaderController* closestLeader = 0;
139        float minDistance =  std::numeric_limits<float>::infinity();
140        //go through all pawns
141        for (ObjectList<LeaderController>::iterator it = ObjectList<LeaderController>::begin(); it; ++it)
142        {
143            //0ptr or not DivisionController?
144            if (!(it) || !((it)->getRank() == Rank::DIVISIONLEADER) || !(it->getControllableEntity()))
145                continue;
146            //same team?
147            if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam()))
148                continue;
149
150            //is equal to this?
151            if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity())
152                continue;
153
154
155            float distance = ((it)->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length();
156           
157            if (distance < minDistance && !(it->hasFollower()))
158            {
159                closestLeader = *it;
160                minDistance = distance;
161            }
162         
163        }
164        if (closestLeader)
165        {
166            if (closestLeader->setFollower(this))
167                return closestLeader;
168        }
169        return 0;
170
171    }
172    bool SectionController::setWingman(CommonController* cwingman)
173    {
174        WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman);
175
176        if (!this->myWingman_)
177        {
178            this->myWingman_ = wingman;
179            return true;
180        }
181        else
182        {
183            return false;
184        }
185    }
186   
187    bool SectionController::hasWingman()
188    {
189        if (this->myWingman_)
190            return true;
191        else
192            return false;
193    }
194
195    void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
196    {
197        SUPER(SectionController, XMLPort, xmlelement, mode);
198
199        //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
200    }
201
202   
203   
204
205}
Note: See TracBrowser for help on using the repository browser.