Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

created destructor functions and improved findNewLeader and findNewDivisionLeader

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