Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

move functions were added, everyone stays in formations

File size: 5.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 "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(WALL);
40
41        bIsDivisionLeader_ = false;
42        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&SectionController::action, this)));
43        this->myWingman_ = 0;
44        this->myDivisionLeader_ = 0;
45
46        orxout(internal_error) << this << "Was created" << endl;
47
48    }
49   
50    SectionController::~SectionController()
51    {
52       
53    }
54    void SectionController::setTargetPositionOfWingman()
55    {
56        if (!this->myWingman_)
57            return;
58        Vector3* targetRelativePositionOfWingman;
59        switch (this->formationMode_){
60            case WALL:
61            {
62                targetRelativePositionOfWingman = new Vector3 (-400, 0, 0); 
63                break;
64            }
65            case FINGER4: 
66            {
67                break;
68            }
69            case VEE: 
70            {
71                break;
72            }
73            case DIAMOND: 
74            {
75                break;
76            }
77        }
78        Quaternion orient = this->getControllableEntity()->getWorldOrientation();
79       
80        Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + 
81        (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman)));
82       
83        myWingman_->setTargetOrientation(orient);
84        myWingman_->setTargetPosition(targetAbsolutePositionOfWingman);
85       
86    }
87
88    LeaderController* SectionController::findNewDivisionLeader()
89    {
90
91        if (!this->getControllableEntity())
92            return 0;
93
94        LeaderController* closestLeader = 0;
95        float minDistance =  std::numeric_limits<float>::infinity();
96        //go through all pawns
97        for (ObjectList<LeaderController>::iterator it = ObjectList<LeaderController>::begin(); it; ++it)
98        {
99            //0ptr or not DivisionController?
100            if (!(it) || !(it)->bIsDivisionLeader_ || !(it->getControllableEntity()))
101                continue;
102            //same team?
103            if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam()))
104                continue;
105
106            //is equal to this?
107            if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity())
108                continue;
109
110
111            float distance = ((it)->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length();
112           
113            if (distance < minDistance && !(it->hasFollower()))
114            {
115                closestLeader = *it;
116                minDistance = distance;
117            }
118         
119        }
120        if (closestLeader)
121        {
122            if (closestLeader->setFollower(this))
123                return closestLeader;
124        }
125        return 0;
126
127    }
128
129    void SectionController::action()
130    {
131        //this->target_ = this->sectionTarget_;       
132        if (!myDivisionLeader_)
133        {
134            LeaderController* newDivisionLeader = findNewDivisionLeader();
135            this->myDivisionLeader_ = newDivisionLeader;
136            if (newDivisionLeader)
137                orxout(internal_error) << "new DivisionLeader set" << endl;
138
139        }
140        setTargetPositionOfWingman();
141
142    }
143    /*
144    Wingmen and Leaders attack target_, which is a member variable of their classes.
145    Wingmen's target_ is set to sectionTarget_, which is a member variable of SectionController class, unless
146    Wingman covers Leader's rear.
147    Leader's target_ must always equal sectionTarget_.
148    if section has a target, its Leader shoots at it, but doesn't follow.
149    Every section is a part of division. Division consisting of one Section is still a division.
150    Division's leader's target_ must always equal divisionTarget_, which is a member variable of DivisionController.
151    Division leader ONLY can follow target_ while in formation flight.
152    If Division doesn't have a target, Division Leader stays in place, unless it has a waypoint.
153    Division Leader's sectionTarget_ must equal divisionTarget_,
154    but the other section, that is not a leading section, can attack any target that is near divisonTarget_
155
156    */
157   
158    void SectionController::tick(float dt)
159    {
160        if (!this->isActive())
161            return;
162       
163        if (this->bHasTargetPosition_)
164        {
165            this->moveToTargetPosition();
166        }
167
168       
169        SUPER(SectionController, tick, dt);
170    }
171   
172
173    void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
174    {
175        SUPER(SectionController, XMLPort, xmlelement, mode);
176
177        //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
178    }
179
180   
181   
182
183}
Note: See TracBrowser for help on using the repository browser.