Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2012merge/src/orxonox/gametypes/Gametype.cc @ 9339

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

Found reason why players are not always detected correctly: numberOfPlayers only count players that have been added before the gametype started.

  • Property svn:eol-style set to native
File size: 16.2 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            this->checkStart();
136        }
137        else if (!this->gtinfo_->hasEnded())
138            this->spawnDeadPlayersIfRequested();
139
140        this->assignDefaultPawnsIfNeeded();
141    }
142
143    void Gametype::start()
144    {
145        this->addBots(this->numberOfBots_);
146
147        this->gtinfo_->start();
148
149        this->spawnPlayersIfRequested();
150    }
151
152    void Gametype::end()
153    {
154        this->gtinfo_->end();
155
156        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
157        {
158            if (it->first->getControllableEntity())
159            {
160                ControllableEntity* oldentity = it->first->getControllableEntity();
161
162                ControllableEntity* entity = this->defaultControllableEntity_.fabricate(oldentity);
163                if (oldentity->getCamera())
164                {
165                    entity->setPosition(oldentity->getCamera()->getWorldPosition());
166                    entity->setOrientation(oldentity->getCamera()->getWorldOrientation());
167                }
168                else
169                {
170                    entity->setPosition(oldentity->getWorldPosition());
171                    entity->setOrientation(oldentity->getWorldOrientation());
172                }
173
174                it->first->startControl(entity);
175            }
176            else
177                this->spawnPlayerAsDefaultPawn(it->first);
178        }
179    }
180
181    void Gametype::playerEntered(PlayerInfo* player)
182    {
183        this->players_[player].state_ = PlayerState::Joined;
184        this->gtinfo_->playerEntered(player);
185        orxout() << "# Players: " << this->getNumberOfPlayers() <<endl;
186    }
187
188    bool Gametype::playerLeft(PlayerInfo* player)
189    {
190        std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player);
191        if (it != this->players_.end())
192        {
193            this->players_.erase(it);
194            orxout() << "# Players: " << this->getNumberOfPlayers() <<endl;
195            return true;
196        }
197        return false;
198    }
199
200    void Gametype::playerSwitched(PlayerInfo* player, Gametype* newgametype)
201    {
202    }
203
204    void Gametype::playerSwitchedBack(PlayerInfo* player, Gametype* oldgametype)
205    {
206    }
207
208    bool Gametype::playerChangedName(PlayerInfo* player)
209    {
210        if (this->players_.find(player) != this->players_.end())
211        {
212            if (player->getName() != player->getOldName())
213            {
214                return true;
215            }
216        }
217        return false;
218    }
219
220    void Gametype::pawnPreSpawn(Pawn* pawn)
221    {
222    }
223
224    void Gametype::pawnPostSpawn(Pawn* pawn)
225    {
226    }
227
228    void Gametype::playerPreSpawn(PlayerInfo* player)
229    {
230    }
231
232    void Gametype::playerPostSpawn(PlayerInfo* player)
233    {
234    }
235
236    void Gametype::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn)
237    {
238    }
239
240    void Gametype::playerStopsControllingPawn(PlayerInfo* player, Pawn* pawn)
241    {
242    }
243
244    bool Gametype::allowPawnHit(Pawn* victim, Pawn* originator)
245    {
246        return true;
247    }
248
249    bool Gametype::allowPawnDamage(Pawn* victim, Pawn* originator)
250    {
251        return true;
252    }
253
254    bool Gametype::allowPawnDeath(Pawn* victim, Pawn* originator)
255    {
256        return true;
257    }
258
259    void Gametype::pawnKilled(Pawn* victim, Pawn* killer)
260    {
261        if (victim && victim->getPlayer())
262        {
263            std::map<PlayerInfo*, Player>::iterator it = this->players_.find(victim->getPlayer());
264            if (it != this->players_.end())
265            {
266                it->second.state_ = PlayerState::Dead;
267                it->second.killed_++;
268
269                // Reward killer
270                if (killer && killer->getPlayer())
271                {
272                    std::map<PlayerInfo*, Player>::iterator it = this->players_.find(killer->getPlayer());
273                    if (it != this->players_.end())
274                    {
275                        it->second.frags_++;
276
277                        if (killer->getPlayer()->getClientID() != NETWORK_PEER_ID_UNKNOWN)
278                            this->gtinfo_->sendKillMessage("You killed " + victim->getPlayer()->getName(), killer->getPlayer()->getClientID());
279                        if (victim->getPlayer()->getClientID() != NETWORK_PEER_ID_UNKNOWN)
280                            this->gtinfo_->sendDeathMessage("You were killed by " + killer->getPlayer()->getName(), victim->getPlayer()->getClientID());
281                    }
282                }
283
284                if(victim->getPlayer()->isHumanPlayer())
285                    this->gtinfo_->pawnKilled(victim->getPlayer());
286
287                ControllableEntity* entity = this->defaultControllableEntity_.fabricate(victim->getCreator());
288                if (victim->getCamera())
289                {
290                    entity->setPosition(victim->getCamera()->getWorldPosition());
291                    entity->setOrientation(victim->getCamera()->getWorldOrientation());
292                }
293                else
294                {
295                    entity->setPosition(victim->getWorldPosition());
296                    entity->setOrientation(victim->getWorldOrientation());
297                }
298                it->first->startControl(entity);
299            }
300            else
301                orxout(internal_warning) << "Killed Pawn was not in the playerlist" << endl;
302        }
303    }
304
305    void Gametype::playerScored(PlayerInfo* player, int score)
306    {
307        std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player);
308        if (it != this->players_.end())
309            it->second.frags_ += score;
310    }
311
312    int Gametype::getScore(PlayerInfo* player) const
313    {
314        std::map<PlayerInfo*, Player>::const_iterator it = this->players_.find(player);
315        if (it != this->players_.end())
316            return it->second.frags_;
317        else
318            return 0;
319    }
320
321    SpawnPoint* Gametype::getBestSpawnPoint(PlayerInfo* player) const
322    {
323        // If there is at least one SpawnPoint.
324        if (this->spawnpoints_.size() > 0)
325        {
326            // Fallback spawn point if there is no active one, choose a random one.
327            SpawnPoint* fallbackSpawnPoint = NULL;
328            unsigned int randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(this->spawnpoints_.size())));
329            unsigned int index = 0;
330            std::vector<SpawnPoint*> activeSpawnPoints;
331            for (std::set<SpawnPoint*>::const_iterator it = this->spawnpoints_.begin(); it != this->spawnpoints_.end(); ++it)
332            {
333                if (index == randomspawn)
334                    fallbackSpawnPoint = (*it);
335
336                if (*it != NULL && (*it)->isActive())
337                    activeSpawnPoints.push_back(*it);
338
339                ++index;
340            }
341
342            if(activeSpawnPoints.size() > 0)
343            {
344                randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(activeSpawnPoints.size())));
345                return activeSpawnPoints[randomspawn];
346            }
347
348            orxout(internal_warning) << "Fallback SpawnPoint was used because there were no active SpawnPoints." << endl;
349            return fallbackSpawnPoint;
350        }
351        return 0;
352    }
353
354    void Gametype::assignDefaultPawnsIfNeeded()
355    {
356        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
357        {
358            if (!it->first->getControllableEntity())
359            {
360                it->second.state_ = PlayerState::Dead;
361
362                if (!it->first->isReadyToSpawn() || !this->gtinfo_->hasStarted())
363                {
364                    this->spawnPlayerAsDefaultPawn(it->first);
365                    it->second.state_ = PlayerState::Dead;
366                }
367            }
368        }
369    }
370
371    void Gametype::checkStart()
372    {
373        if (!this->gtinfo_->hasStarted())
374        {
375            if (this->gtinfo_->isStartCountdownRunning())
376            {
377                if (this->gtinfo_->getStartCountdown() <= 0.0f)
378                {
379                    this->gtinfo_->stopStartCountdown();
380                    this->gtinfo_->setStartCountdown(0.0f);
381                    this->start();
382                }
383            }
384            else if (this->players_.size() > 0)
385            {
386                if (this->bAutoStart_)
387                {
388                    this->start();
389                }
390                else
391                {
392                    bool allplayersready = true;
393                    bool hashumanplayers = false;
394                    for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
395                    {
396                        if (!it->first->isReadyToSpawn())
397                            allplayersready = false;
398                        if (it->first->isHumanPlayer())
399                            hashumanplayers = true;
400                    }
401
402                    if (allplayersready && hashumanplayers)
403                    {
404                        // If in developer's mode, there is no start countdown.
405                        if(Core::getInstance().inDevMode())
406                            this->start();
407                        else
408                        {
409                            this->gtinfo_->setStartCountdown(this->initialStartCountdown_);
410                            this->gtinfo_->startStartCountdown();
411                        }
412                    }
413                }
414            }
415        }
416    }
417
418    void Gametype::spawnPlayersIfRequested()
419    {
420        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
421        {
422            if (it->first->isReadyToSpawn() || this->bForceSpawn_)
423                this->spawnPlayer(it->first);
424        }
425    }
426
427    void Gametype::spawnDeadPlayersIfRequested()
428    {
429        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
430            if (it->second.state_ == PlayerState::Dead)
431                if (it->first->isReadyToSpawn() || this->bForceSpawn_)
432                    this->spawnPlayer(it->first);
433    }
434
435    void Gametype::spawnPlayer(PlayerInfo* player)
436    {
437        SpawnPoint* spawnpoint = this->getBestSpawnPoint(player);
438        if (spawnpoint)
439        {
440            this->playerPreSpawn(player);
441            player->startControl(spawnpoint->spawn());
442            this->players_[player].state_ = PlayerState::Alive;
443
444            if(player->isHumanPlayer())
445                this->gtinfo_->playerSpawned(player);
446
447            this->playerPostSpawn(player);
448        }
449        else
450        {
451            orxout(user_error) << "No SpawnPoints in current Gametype" << endl;
452            abort();
453        }
454    }
455
456    void Gametype::spawnPlayerAsDefaultPawn(PlayerInfo* player)
457    {
458        SpawnPoint* spawn = this->getBestSpawnPoint(player);
459        if (spawn)
460        {
461            // force spawn at spawnpoint with default pawn
462            ControllableEntity* entity = this->defaultControllableEntity_.fabricate(spawn);
463            spawn->spawn(entity);
464            player->startControl(entity);
465        }
466        else
467        {
468            orxout(user_error) << "No SpawnPoints in current Gametype" << endl;
469            abort();
470        }
471    }
472
473    void Gametype::addBots(unsigned int amount)
474    {
475        for (unsigned int i = 0; i < amount; ++i)
476            this->botclass_.fabricate(this);
477    }
478
479    void Gametype::killBots(unsigned int amount)
480    {
481        unsigned int i = 0;
482        for (ObjectList<Bot>::iterator it = ObjectList<Bot>::begin(); (it != ObjectList<Bot>::end()) && ((amount == 0) || (i < amount)); )
483        {
484            if (it->getGametype() == this)
485            {
486                (it++)->destroy();
487                ++i;
488            }
489            else
490                ++it;
491        }
492    }
493
494    void Gametype::addTime(float t)
495    {
496        if (this->timeLimit_ == 0)
497          this->time_ -= t;
498        else
499          this->time_ += t;
500    }
501
502    void Gametype::removeTime(float t)
503    {
504        if (this->timeLimit_ == 0)
505          this->time_ += t;
506        else
507          this->time_ -= t;
508    }
509
510    void Gametype::resetTimer()
511    {
512        this->resetTimer(timeLimit_);
513    }
514
515    void Gametype::resetTimer(float t)
516    {
517        this->timeLimit_ = t;
518        this->time_ = t;
519    }
520}
Note: See TracBrowser for help on using the repository browser.