Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/gametypes/Gametype.cc @ 9939

Last change on this file since 9939 was 9939, checked in by jo, 10 years ago

presentationHS13 branch merged into trunk

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