Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/gametypes/Gametype.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.0 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 "Gametype.h"
30
31#include "util/Math.h"
32#include "core/Core.h"
33#include "core/CoreIncludes.h"
34#include "core/ConfigValueIncludes.h"
35#include "core/GameMode.h"
36#include "core/command/ConsoleCommand.h"
37
38#include "infos/PlayerInfo.h"
39#include "infos/Bot.h"
40#include "graphics/Camera.h"
41#include "worldentities/ControllableEntity.h"
42#include "worldentities/SpawnPoint.h"
43#include "worldentities/pawns/Spectator.h"
44#include "worldentities/pawns/Pawn.h"
45#include "overlays/OverlayGroup.h"
46
47namespace orxonox
48{
49    CreateUnloadableFactory(Gametype);
50
51    Gametype::Gametype(BaseObject* creator) : BaseObject(creator)
52    {
53        RegisterObject(Gametype);
54
55        this->gtinfo_ = new GametypeInfo(creator);
56
57        this->setGametype(SmartPtr<Gametype>(this, false));
58
59        this->defaultControllableEntity_ = Class(Spectator);
60
61        this->bAutoStart_ = false;
62        this->bForceSpawn_ = false;
63        this->numberOfBots_ = 0;
64
65        this->timeLimit_ = 0;
66        this->time_ = 0;
67        this->timerIsActive_ = false;
68
69        this->initialStartCountdown_ = 3;
70
71        this->setConfigValues();
72
73        // load the corresponding score board
74        if (GameMode::showsGraphics() && !this->scoreboardTemplate_.empty())
75        {
76            this->scoreboard_ = new OverlayGroup(this);
77            this->scoreboard_->addTemplate(this->scoreboardTemplate_);
78            this->scoreboard_->setGametype(this);
79        }
80        else
81            this->scoreboard_ = 0;
82
83        /* HACK HACK HACK */
84        this->dedicatedAddBots_ = createConsoleCommand( "dedicatedAddBots", createExecutor( createFunctor(&Gametype::addBots, this) ) );
85        this->dedicatedKillBots_ = createConsoleCommand( "dedicatedKillBots", createExecutor( createFunctor(&Gametype::killBots, this) ) );
86        /* HACK HACK HACK */
87    }
88
89    Gametype::~Gametype()
90    {
91        if (this->isInitialized())
92        {
93            this->gtinfo_->destroy();
94            if( this->dedicatedAddBots_ )
95                delete this->dedicatedAddBots_;
96            if( this->dedicatedKillBots_ )
97                delete this->dedicatedKillBots_;
98        }
99    }
100
101    void Gametype::setConfigValues()
102    {
103        SetConfigValue(initialStartCountdown_, 3.0f);
104        SetConfigValue(bAutoStart_, false);
105        SetConfigValue(bForceSpawn_, false);
106        SetConfigValue(numberOfBots_, 0);
107        SetConfigValue(scoreboardTemplate_, "defaultScoreboard");
108    }
109
110    void Gametype::tick(float dt)
111    {
112        SUPER(Gametype, tick, dt);
113
114        //count timer
115        if (timerIsActive_)
116        {
117            if (this->timeLimit_ == 0)
118                this->time_ += dt;
119            else
120                this->time_ -= dt;
121        }
122
123        if (this->gtinfo_->isStartCountdownRunning() && !this->gtinfo_->hasStarted())
124            this->gtinfo_->countdownStartCountdown(dt);
125                       
126        if (!this->gtinfo_->hasStarted())
127        {
128            for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
129            {
130                // Inform the GametypeInfo that the player is ready to spawn.
131                if(it->first->isHumanPlayer() && it->first->isReadyToSpawn())
132                    this->gtinfo_->playerReadyToSpawn(it->first);
133                   
134               
135            }
136            this->checkStart();
137        }
138        else if (!this->gtinfo_->hasEnded())
139            this->spawnDeadPlayersIfRequested();
140
141        this->assignDefaultPawnsIfNeeded();
142    }
143
144    void Gametype::start()
145    {
146         
147        this->addBots(this->numberOfBots_);
148
149        this->gtinfo_->start();
150
151        this->spawnPlayersIfRequested();
152       
153       
154    }
155
156    void Gametype::end()
157    {
158        this->gtinfo_->end();
159
160        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
161        {
162            if (it->first->getControllableEntity())
163            {
164                ControllableEntity* oldentity = it->first->getControllableEntity();
165
166                ControllableEntity* entity = this->defaultControllableEntity_.fabricate(oldentity);
167                if (oldentity->getCamera())
168                {
169                    entity->setPosition(oldentity->getCamera()->getWorldPosition());
170                    entity->setOrientation(oldentity->getCamera()->getWorldOrientation());
171                }
172                else
173                {
174                    entity->setPosition(oldentity->getWorldPosition());
175                    entity->setOrientation(oldentity->getWorldOrientation());
176                }
177
178                it->first->startControl(entity);
179            }
180            else
181                this->spawnPlayerAsDefaultPawn(it->first);
182        }
183    }
184
185    void Gametype::playerEntered(PlayerInfo* player)
186    {
187        this->players_[player].state_ = PlayerState::Joined;
188        this->gtinfo_->playerEntered(player);
189    }
190
191    bool Gametype::playerLeft(PlayerInfo* player)
192    {
193        std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player);
194        if (it != this->players_.end())
195        {
196            this->players_.erase(it);
197            return true;
198        }
199        return false;
200    }
201
202    void Gametype::playerSwitched(PlayerInfo* player, Gametype* newgametype)
203    {
204    }
205
206    void Gametype::playerSwitchedBack(PlayerInfo* player, Gametype* oldgametype)
207    {
208    }
209
210    bool Gametype::playerChangedName(PlayerInfo* player)
211    {
212        if (this->players_.find(player) != this->players_.end())
213        {
214            if (player->getName() != player->getOldName())
215            {
216                return true;
217            }
218        }
219        return false;
220    }
221
222    void Gametype::pawnPreSpawn(Pawn* pawn)
223    {
224    }
225
226    void Gametype::pawnPostSpawn(Pawn* pawn)
227    {
228    }
229
230    void Gametype::playerPreSpawn(PlayerInfo* player)
231    {
232    }
233
234    void Gametype::playerPostSpawn(PlayerInfo* player)
235    {
236    }
237
238    void Gametype::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn)
239    {
240    }
241
242    void Gametype::playerStopsControllingPawn(PlayerInfo* player, Pawn* pawn)
243    {
244    }
245
246    bool Gametype::allowPawnHit(Pawn* victim, Pawn* originator)
247    {
248        return true;
249    }
250
251    bool Gametype::allowPawnDamage(Pawn* victim, Pawn* originator)
252    {
253        return true;
254    }
255
256    bool Gametype::allowPawnDeath(Pawn* victim, Pawn* originator)
257    {
258        return true;
259    }
260
261    void Gametype::pawnKilled(Pawn* victim, Pawn* killer)
262    {
263        if (victim && victim->getPlayer())
264        {
265            std::map<PlayerInfo*, Player>::iterator it = this->players_.find(victim->getPlayer());
266            if (it != this->players_.end())
267            {
268                it->second.state_ = PlayerState::Dead;
269                it->second.killed_++;
270
271                // Reward killer
272                if (killer && killer->getPlayer())
273                {
274                    std::map<PlayerInfo*, Player>::iterator it = this->players_.find(killer->getPlayer());
275                    if (it != this->players_.end())
276                    {
277                        it->second.frags_++;
278
279                        if (killer->getPlayer()->getClientID() != NETWORK_PEER_ID_UNKNOWN)
280                            this->gtinfo_->sendKillMessage("You killed " + victim->getPlayer()->getName(), killer->getPlayer()->getClientID());
281                        if (victim->getPlayer()->getClientID() != NETWORK_PEER_ID_UNKNOWN)
282                            this->gtinfo_->sendDeathMessage("You were killed by " + killer->getPlayer()->getName(), victim->getPlayer()->getClientID());
283                    }
284                }
285
286                if(victim->getPlayer()->isHumanPlayer())
287                    this->gtinfo_->pawnKilled(victim->getPlayer());
288
289                ControllableEntity* entity = this->defaultControllableEntity_.fabricate(victim->getCreator());
290                if (victim->getCamera())
291                {
292                    entity->setPosition(victim->getCamera()->getWorldPosition());
293                    entity->setOrientation(victim->getCamera()->getWorldOrientation());
294                }
295                else
296                {
297                    entity->setPosition(victim->getWorldPosition());
298                    entity->setOrientation(victim->getWorldOrientation());
299                }
300                it->first->startControl(entity);
301            }
302            else
303                orxout(internal_warning) << "Killed Pawn was not in the playerlist" << endl;
304        }
305    }
306
307    void Gametype::playerScored(PlayerInfo* player)
308    {
309        std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player);
310        if (it != this->players_.end())
311            it->second.frags_++;
312    }
313
314    int Gametype::getScore(PlayerInfo* player) const
315    {
316        std::map<PlayerInfo*, Player>::const_iterator it = this->players_.find(player);
317        if (it != this->players_.end())
318            return it->second.frags_;
319        else
320            return 0;
321    }
322
323    SpawnPoint* Gametype::getBestSpawnPoint(PlayerInfo* player) const
324    {
325        // If there is at least one SpawnPoint.
326        if (this->spawnpoints_.size() > 0)
327        {
328            // Fallback spawn point if there is no active one, choose a random one.
329            SpawnPoint* fallbackSpawnPoint = NULL;
330            unsigned int randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(this->spawnpoints_.size())));
331            unsigned int index = 0;
332            std::vector<SpawnPoint*> activeSpawnPoints;
333            for (std::set<SpawnPoint*>::const_iterator it = this->spawnpoints_.begin(); it != this->spawnpoints_.end(); ++it)
334            {
335                if (index == randomspawn)
336                    fallbackSpawnPoint = (*it);
337
338                if (*it != NULL && (*it)->isActive())
339                    activeSpawnPoints.push_back(*it);
340
341                ++index;
342            }
343
344            if(activeSpawnPoints.size() > 0)
345            {
346                randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(activeSpawnPoints.size())));
347                return activeSpawnPoints[randomspawn];
348            }
349
350            orxout(internal_warning) << "Fallback SpawnPoint was used because there were no active SpawnPoints." << endl;
351            return fallbackSpawnPoint;
352        }
353        return 0;
354    }
355
356    void Gametype::assignDefaultPawnsIfNeeded()
357    {
358        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
359        {
360            if (!it->first->getControllableEntity())
361            {
362                it->second.state_ = PlayerState::Dead;
363
364                if (!it->first->isReadyToSpawn() || !this->gtinfo_->hasStarted())
365                {
366                    this->spawnPlayerAsDefaultPawn(it->first);
367                    it->second.state_ = PlayerState::Dead;
368                }
369            }
370        }
371    }
372
373    void Gametype::checkStart()
374    {
375        if (!this->gtinfo_->hasStarted())
376        {
377            if (this->gtinfo_->isStartCountdownRunning())
378            {
379                if (this->gtinfo_->getStartCountdown() <= 0.0f)
380                {
381                    this->gtinfo_->stopStartCountdown();
382                    this->gtinfo_->setStartCountdown(0.0f);
383                    this->start();
384                }
385            }
386            else if (this->players_.size() > 0)
387            {
388                if (this->bAutoStart_)
389                {
390                    this->start();
391                }
392                else
393                {
394                    bool allplayersready = true;
395                    bool hashumanplayers = false;
396                    for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
397                    {
398                        if (!it->first->isReadyToSpawn())
399                            allplayersready = false;
400                        if (it->first->isHumanPlayer())
401                            hashumanplayers = true;
402                    }
403                         
404                    if (allplayersready && hashumanplayers)
405                    {
406                        // If in developer's mode, there is no start countdown.
407                        if(Core::getInstance().inDevMode())
408                            this->start();
409                        else
410                            this->gtinfo_->setStartCountdown(this->initialStartCountdown_);
411                        this->gtinfo_->startStartCountdown();
412                    }
413                }
414            }
415           
416        }
417    }
418
419    void Gametype::spawnPlayersIfRequested()
420    {
421        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
422        {
423            if (it->first->isReadyToSpawn() || this->bForceSpawn_)
424                this->spawnPlayer(it->first);
425        }
426    }
427
428    void Gametype::spawnDeadPlayersIfRequested()
429    {
430        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
431            if (it->second.state_ == PlayerState::Dead)
432                if (it->first->isReadyToSpawn() || this->bForceSpawn_)
433                    this->spawnPlayer(it->first);
434    }
435
436    void Gametype::spawnPlayer(PlayerInfo* player)
437    {
438        SpawnPoint* spawnpoint = this->getBestSpawnPoint(player);
439        if (spawnpoint)
440        {
441            this->playerPreSpawn(player);
442            player->startControl(spawnpoint->spawn());
443            this->players_[player].state_ = PlayerState::Alive;
444
445            if(player->isHumanPlayer())
446                this->gtinfo_->playerSpawned(player);
447
448            this->playerPostSpawn(player);
449        }
450        else
451        {
452            orxout(user_error) << "No SpawnPoints in current Gametype" << endl;
453            abort();
454        }
455    }
456
457    void Gametype::spawnPlayerAsDefaultPawn(PlayerInfo* player)
458    {
459        SpawnPoint* spawn = this->getBestSpawnPoint(player);
460        if (spawn)
461        {
462            // force spawn at spawnpoint with default pawn
463            ControllableEntity* entity = this->defaultControllableEntity_.fabricate(spawn);
464            spawn->spawn(entity);
465            player->startControl(entity);
466        }
467        else
468        {
469            orxout(user_error) << "No SpawnPoints in current Gametype" << endl;
470            abort();
471        }
472    }
473
474    void Gametype::addBots(unsigned int amount)
475    {
476        for (unsigned int i = 0; i < amount; ++i)
477            this->botclass_.fabricate(this);
478    }
479
480    void Gametype::killBots(unsigned int amount)
481    {
482        unsigned int i = 0;
483        for (ObjectList<Bot>::iterator it = ObjectList<Bot>::begin(); (it != ObjectList<Bot>::end()) && ((amount == 0) || (i < amount)); )
484        {
485            if (it->getGametype() == this)
486            {
487                (it++)->destroy();
488                ++i;
489            }
490            else
491                ++it;
492        }
493    }
494
495    void Gametype::addTime(float t)
496    {
497        if (this->timeLimit_ == 0)
498          this->time_ -= t;
499        else
500          this->time_ += t;
501    }
502
503    void Gametype::removeTime(float t)
504    {
505        if (this->timeLimit_ == 0)
506          this->time_ += t;
507        else
508          this->time_ -= t;
509    }
510
511    void Gametype::resetTimer()
512    {
513        this->resetTimer(timeLimit_);
514    }
515
516    void Gametype::resetTimer(float t)
517    {
518        this->timeLimit_ = t;
519        this->time_ = t;
520    }
521}
Note: See TracBrowser for help on using the repository browser.