Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added a canFire() function

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