Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/controllers/HumanController.cc @ 9016

Last change on this file since 9016 was 9016, checked in by jo, 12 years ago

Merging presentation2011 branch to trunk. Please check for possible bugs.

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