Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/AI_HS15/src/orxonox/controllers/LeaderController.cc @ 10717

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

Wingmen and Leaders look for their leaders

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