Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/formation/src/orxonox/controllers/HumanController.cc @ 8965

Last change on this file since 8965 was 8965, checked in by willis, 12 years ago

added some Mode features, fixed a speed issue

  • Property svn:eol-style set to native
File size: 15.9 KB
RevLine 
[2072]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 *      ...
26 *
27 */
28
29#include "HumanController.h"
30
31#include "core/CoreIncludes.h"
[7284]32#include "core/command/ConsoleCommand.h"
[5735]33#include "worldentities/ControllableEntity.h"
34#include "worldentities/pawns/Pawn.h"
35#include "gametypes/Gametype.h"
36#include "infos/PlayerInfo.h"
[5929]37#include "Radar.h"
38#include "Scene.h"
[2072]39
40namespace orxonox
41{
[7862]42    extern const std::string __CC_fire_name = "fire";
[7863]43    extern const std::string __CC_suicide_name = "suicide";
[8706]44    const std::string __CC_boost_name = "boost";
[7862]45
[7284]46    SetConsoleCommand("HumanController", "moveFrontBack",          &HumanController::moveFrontBack ).addShortcut().setAsInputCommand();
47    SetConsoleCommand("HumanController", "moveRightLeft",          &HumanController::moveRightLeft ).addShortcut().setAsInputCommand();
48    SetConsoleCommand("HumanController", "moveUpDown",             &HumanController::moveUpDown    ).addShortcut().setAsInputCommand();
49    SetConsoleCommand("HumanController", "rotateYaw",              &HumanController::rotateYaw     ).addShortcut().setAsInputCommand();
50    SetConsoleCommand("HumanController", "rotatePitch",            &HumanController::rotatePitch   ).addShortcut().setAsInputCommand();
51    SetConsoleCommand("HumanController", "rotateRoll",             &HumanController::rotateRoll    ).addShortcut().setAsInputCommand();
[8943]52    SetConsoleCommand("HumanController", "toggleFormationFlight",  &HumanController::toggleFormationFlight).addShortcut().keybindMode(KeybindMode::OnPress);
[8953]53    SetConsoleCommand("HumanController", "FFChangeMode",  &HumanController::FFChangeMode).addShortcut().keybindMode(KeybindMode::OnPress);
[7862]54    SetConsoleCommand("HumanController", __CC_fire_name,           &HumanController::fire          ).addShortcut().keybindMode(KeybindMode::OnHold);
[7284]55    SetConsoleCommand("HumanController", "reload",                 &HumanController::reload        ).addShortcut();
[8706]56    SetConsoleCommand("HumanController", __CC_boost_name,          &HumanController::keepBoost     ).addShortcut().keybindMode(KeybindMode::OnHold);
[7284]57    SetConsoleCommand("HumanController", "greet",                  &HumanController::greet         ).addShortcut();
58    SetConsoleCommand("HumanController", "switchCamera",           &HumanController::switchCamera  ).addShortcut();
59    SetConsoleCommand("HumanController", "mouseLook",              &HumanController::mouseLook     ).addShortcut();
[7863]60    SetConsoleCommand("HumanController", __CC_suicide_name,        &HumanController::suicide       ).addShortcut();
[7284]61    SetConsoleCommand("HumanController", "toggleGodMode",          &HumanController::toggleGodMode ).addShortcut();
62    SetConsoleCommand("HumanController", "addBots",                &HumanController::addBots       ).addShortcut().defaultValues(1);
63    SetConsoleCommand("HumanController", "killBots",               &HumanController::killBots      ).addShortcut().defaultValues(0);
64    SetConsoleCommand("HumanController", "cycleNavigationFocus",   &HumanController::cycleNavigationFocus).addShortcut();
65    SetConsoleCommand("HumanController", "releaseNavigationFocus", &HumanController::releaseNavigationFocus).addShortcut();
[8079]66    SetConsoleCommand("HumanController", "myposition",             &HumanController::myposition    ).addShortcut();
[2072]67
68    CreateUnloadableFactory(HumanController);
69
70    HumanController* HumanController::localController_s = 0;
[8706]71    /*static*/ const float HumanController::BOOSTING_TIME = 0.1f;
[2072]72
[8939]73    HumanController::HumanController(BaseObject* creator) : Masterable(creator)
[2072]74    {
75        RegisterObject(HumanController);
76
[8706]77        this->controlPaused_ = false;
78        this->boosting_ = false;
79        this->boosting_ = false;
[8948]80        this->tempMaster=NULL;
[2072]81        HumanController::localController_s = this;
[8706]82        this->boostingTimeout_.setTimer(HumanController::BOOSTING_TIME, false, createExecutor(createFunctor(&HumanController::terminateBoosting, this)));
83        this->boostingTimeout_.stopTimer();
[8948]84        this->state_=FREE;
[2072]85    }
86
87    HumanController::~HumanController()
88    {
[8948]89        if (HumanController::localController_s) 
90        {
91            HumanController::localController_s->removeFromFormation();
92        }
[2072]93        HumanController::localController_s = 0;
94    }
95
[5929]96    void HumanController::tick(float dt)
97    {
98        if (GameMode::playsSound() && HumanController::localController_s && HumanController::localController_s->controllableEntity_)
99        {
100            Camera* camera = HumanController::localController_s->controllableEntity_->getCamera();
[6417]101            if (!camera)
[8858]102                orxout(internal_warning) << "HumanController, Warning: Using a ControllableEntity without Camera" << endl;
[5929]103        }
[8939]104
105        // commandslaves when Master of a formation
[8948]106        if (HumanController::localController_s && HumanController::localController_s->state_==MASTER)
[8939]107        {
[8953]108            if (HumanController::localController_s->mode_!=ATTACK)
109                HumanController::localController_s->commandSlaves();
[8939]110        }
[5929]111    }
112
[2072]113    void HumanController::moveFrontBack(const Vector2& value)
114    {
[6417]115        if (HumanController::localController_s)
116            HumanController::localController_s->frontback(value);
117    }
118
119    void HumanController::frontback(const Vector2& value)
120    {
[2072]121        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
122            HumanController::localController_s->controllableEntity_->moveFrontBack(value);
123    }
124
125    void HumanController::moveRightLeft(const Vector2& value)
126    {
127        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
128            HumanController::localController_s->controllableEntity_->moveRightLeft(value);
129    }
130
131    void HumanController::moveUpDown(const Vector2& value)
132    {
133        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
134            HumanController::localController_s->controllableEntity_->moveUpDown(value);
135    }
136
[6417]137    void HumanController::yaw(const Vector2& value)
[2072]138    {
139        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
140            HumanController::localController_s->controllableEntity_->rotateYaw(value);
141    }
142
[6417]143    void HumanController::pitch(const Vector2& value)
[2072]144    {
145        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
146            HumanController::localController_s->controllableEntity_->rotatePitch(value);
147    }
148
[6417]149    void HumanController::rotateYaw(const Vector2& value)
150    {
151        if (HumanController::localController_s)
152            HumanController::localController_s->yaw(value);
153    }
154
155    void HumanController::rotatePitch(const Vector2& value)
156    {
157        if (HumanController::localController_s)
158            HumanController::localController_s->pitch(value);
159    }
160
[2072]161    void HumanController::rotateRoll(const Vector2& value)
162    {
163        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
164            HumanController::localController_s->controllableEntity_->rotateRoll(value);
165    }
166
[3053]167    void HumanController::fire(unsigned int firemode)
[2072]168    {
[6417]169        if (HumanController::localController_s)
170            HumanController::localController_s->doFire(firemode);
171    }
172
173    void HumanController::doFire(unsigned int firemode)
174    {
[2072]175        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
[8965]176        {
[3053]177            HumanController::localController_s->controllableEntity_->fire(firemode);
[8965]178            //if human fires, set slaves free. See Masterable::forceFreeSlaves()
179            if (HumanController::localController_s->state_==MASTER && HumanController::localController_s->mode_==NORMAL)
180            {
181                HumanController::localController_s->forceFreeSlaves();
182            }
183        }
[2072]184    }
185
[3053]186    void HumanController::reload()
[2072]187    {
188        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
[3053]189            HumanController::localController_s->controllableEntity_->reload();
[2072]190    }
191
[8706]192    /**
193    @brief
194        Static method,keeps boosting.
195    */
196    /*static*/ void HumanController::keepBoost()
[2662]197    {
198        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
[8706]199            HumanController::localController_s->keepBoosting();
[2662]200    }
[8706]201   
202    /**
203    @brief
204        Starts, or keeps the boosting mode.
205        Resets the boosting timeout and ells the ControllableEntity to boost (or not boost anymore).
206    */
207    void HumanController::keepBoosting(void)
208    {
209        if(this->boostingTimeout_.isActive())
210        {
211            this->boostingTimeout_.stopTimer();
212            this->boostingTimeout_.startTimer();
213        }
214        else
215        {
216            this->boosting_ = true;
217            this->boostingTimeout_.startTimer();
218           
219            this->controllableEntity_->boost(this->boosting_);
[8858]220//            orxout() << "Start boosting" << endl;
[8706]221        }
222    }
[2662]223
[8706]224    /**
225    @brief
226        Terminates the boosting mode.
227    */
228    void HumanController::terminateBoosting(void)
229    {
230        this->boosting_ = false;
231        this->boostingTimeout_.stopTimer();
232
233        this->controllableEntity_->boost(this->boosting_);
[8858]234//        orxout() << "Stop boosting" << endl;
[8706]235    }
236
[2072]237    void HumanController::greet()
238    {
239        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
240            HumanController::localController_s->controllableEntity_->greet();
241    }
242
243    void HumanController::switchCamera()
244    {
245        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
246            HumanController::localController_s->controllableEntity_->switchCamera();
247    }
[2662]248
249    void HumanController::mouseLook()
250    {
251        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
252            HumanController::localController_s->controllableEntity_->mouseLook();
253    }
254
255    void HumanController::suicide()
256    {
257        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
258        {
[3325]259            Pawn* pawn = orxonox_cast<Pawn*>(HumanController::localController_s->controllableEntity_);
[2662]260            if (pawn)
261                pawn->kill();
[2872]262            else if (HumanController::localController_s->player_)
[3038]263                HumanController::localController_s->player_->stopControl();
[2662]264        }
265    }
266
[6417]267    void HumanController::toggleGodMode()
268    {
[8079]269        if (HumanController::localController_s)
270            HumanController::localController_s->setGodMode(!HumanController::localController_s->getGodMode());
[6417]271    }
272
[8079]273    void HumanController::myposition()
274    {
275        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
276        {
277            const Vector3& position = HumanController::localController_s->controllableEntity_->getPosition();
278            const Quaternion& orientation = HumanController::localController_s->controllableEntity_->getOrientation();
279
[8858]280            orxout(message) << "position=\"" << position.x << ", " << position.y << ", " << position.z << "\" "
281                            << "orientation=\"" << orientation.w << ", " << orientation.x << ", " << orientation.y << ", " << orientation.z << "\"" << endl;
[8079]282        }
283    }
284
[8953]285    /**
286    @brief
287       toggle the formation. Not usable, if formationflight is disabled generally (formationFlight_)
288    */
[8939]289    void HumanController::toggleFormationFlight()
290    {
291        if (HumanController::localController_s)
292        {
[8953]293            if (!HumanController::localController_s->formationFlight_)
294            {
295                return; //dont use when formationFlight is disabled
296            }
[8939]297            if (HumanController::localController_s->state_==MASTER)
298            {
[8953]299                HumanController::localController_s->loseMasterState();
[8948]300                orxout(message) <<"FormationFlight disabled "<< endl;
[8939]301            } else //SLAVE or FREE
302            {
303                HumanController::localController_s->takeLeadOfFormation();
[8948]304                orxout(message) <<"FormationFlight enabled "<< endl;
[8939]305            }
[8948]306           
[8939]307        }
308
309    }
310
[8953]311    /**
312    @brief
313       Switch through the different Modes of formationflight. You must be a master of a formation to use.
314    */
315    void HumanController::FFChangeMode()
316    {
317        if (HumanController::localController_s && HumanController::localController_s->state_==MASTER)
318        {
319            switch (HumanController::localController_s->getMode()) {
320              case NORMAL:
321                HumanController::localController_s->setMode(DEFEND);
322                orxout(message) <<"Mode: DEFEND "<< endl;
323                break;
324              case DEFEND:
325                HumanController::localController_s->setMode(ATTACK);
326                orxout(message) <<"Mode: ATTACK "<< endl;
327                break;
328              case ATTACK:
329                HumanController::localController_s->setMode(NORMAL);
330                orxout(message) <<"Mode: NORMAL "<< endl;
331                break;
332            }
333        }
334    }
335
[8965]336
337    //used, when slaves are in DEFEND mode.
338    void HumanController::hit(Pawn* originator, btManifoldPoint& contactpoint, float damage)
[8953]339    {
[8965]340        if (!this->formationFlight_ || this->state_!=MASTER || this->mode_!=DEFEND) return;
341            this->masterAttacked(originator);
342    }
[8953]343
[2662]344    void HumanController::addBots(unsigned int amount)
345    {
346        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_ && HumanController::localController_s->controllableEntity_->getGametype())
347            HumanController::localController_s->controllableEntity_->getGametype()->addBots(amount);
348    }
349
350    void HumanController::killBots(unsigned int amount)
351    {
352        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_ && HumanController::localController_s->controllableEntity_->getGametype())
353            HumanController::localController_s->controllableEntity_->getGametype()->killBots(amount);
354    }
355
[3196]356    Pawn* HumanController::getLocalControllerEntityAsPawn()
357    {
358        if (HumanController::localController_s)
[3325]359            return orxonox_cast<Pawn*>(HumanController::localController_s->getControllableEntity());
[3196]360        else
361            return NULL;
362    }
[5929]363
364    void HumanController::cycleNavigationFocus()
365    {
366        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
367            HumanController::localController_s->controllableEntity_->getScene()->getRadar()->cycleFocus();
368    }
369
370    void HumanController::releaseNavigationFocus()
371    {
372        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
373            HumanController::localController_s->controllableEntity_->getScene()->getRadar()->releaseFocus();
374    }
[6417]375
376    void HumanController::pauseControl()
377    {
378        if (HumanController::localController_s)
379            HumanController::localController_s->doPauseControl();
380    }
381
382    void HumanController::resumeControl()
383    {
384        if (HumanController::localController_s)
385            HumanController::localController_s->doResumeControl();
386    }
[2072]387}
Note: See TracBrowser for help on using the repository browser.