Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/gametypes/Gametype.cc @ 3192

Last change on this file since 3192 was 3192, checked in by rgrieder, 15 years ago

Replaced <OgrePrerequisites.h> with "util/OgreForwardRefs.h": I haven't yet realised that OgrePrerequisites.h includes about every single std header by including the OgreMemoryManager.h file.
And while at it, I took care of some type conversions (partially revealed by the missing OgrePrerequisites.h that disabled warnings)

  • Property svn:eol-style set to native
File size: 13.6 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 "Gametype.h"
30
[3179]31#include "util/Math.h"
[2072]32#include "core/CoreIncludes.h"
[2662]33#include "core/ConfigValueIncludes.h"
[2896]34#include "core/GameMode.h"
[3179]35
[2072]36#include "objects/infos/PlayerInfo.h"
[2662]37#include "objects/infos/Bot.h"
[3179]38#include "objects/worldentities/Camera.h"
39#include "objects/worldentities/ControllableEntity.h"
40#include "objects/worldentities/SpawnPoint.h"
[2072]41#include "objects/worldentities/pawns/Spectator.h"
[3186]42#include "objects/worldentities/pawns/Pawn.h"
[3179]43#include "overlays/OverlayGroup.h"
[2072]44
45namespace orxonox
46{
47    CreateUnloadableFactory(Gametype);
48
[2662]49    Gametype::Gametype(BaseObject* creator) : BaseObject(creator), gtinfo_(creator)
[2072]50    {
51        RegisterObject(Gametype);
52
[2662]53        this->setGametype(this);
54
[2072]55        this->defaultControllableEntity_ = Class(Spectator);
56
57        this->bAutoStart_ = false;
58        this->bForceSpawn_ = false;
[2662]59        this->numberOfBots_ = 0;
[2072]60
[3033]61        this->timeLimit_ = 0;
62        this->time_ = 0;
63        this->timerIsActive_ = false;
64
[2072]65        this->initialStartCountdown_ = 3;
[2662]66
67        this->setConfigValues();
68
69        // load the corresponding score board
[2896]70        if (GameMode::showsGraphics() && this->scoreboardTemplate_ != "")
[2662]71        {
72            this->scoreboard_ = new OverlayGroup(this);
73            this->scoreboard_->addTemplate(this->scoreboardTemplate_);
74            this->scoreboard_->setGametype(this);
75        }
76        else
77            this->scoreboard_ = 0;
[2072]78    }
79
[2662]80    void Gametype::setConfigValues()
81    {
82        SetConfigValue(initialStartCountdown_, 3.0f);
83        SetConfigValue(bAutoStart_, false);
84        SetConfigValue(bForceSpawn_, false);
85        SetConfigValue(numberOfBots_, 0);
86        SetConfigValue(scoreboardTemplate_, "defaultScoreboard");
87    }
88
[2072]89    void Gametype::tick(float dt)
90    {
[2662]91        SUPER(Gametype, tick, dt);
[2072]92
[3033]93        //count timer
94        if (timerIsActive_)
95        {
96            if (this->timeLimit_ == 0)
97                this->time_ += dt;
98            else
99                this->time_ -= dt;
100        }
101
[2662]102        if (this->gtinfo_.bStartCountdownRunning_ && !this->gtinfo_.bStarted_)
103            this->gtinfo_.startCountdown_ -= dt;
104
105        if (!this->gtinfo_.bStarted_)
[2072]106            this->checkStart();
[3033]107        else if (!this->gtinfo_.bEnded_)
[2171]108            this->spawnDeadPlayersIfRequested();
[2072]109
110        this->assignDefaultPawnsIfNeeded();
111    }
112
113    void Gametype::start()
114    {
[2826]115        this->addBots(this->numberOfBots_);
116
[2662]117        this->gtinfo_.bStarted_ = true;
[2072]118
119        this->spawnPlayersIfRequested();
120    }
121
122    void Gametype::end()
123    {
[2662]124        this->gtinfo_.bEnded_ = true;
[3033]125
126        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
127        {
128            if (it->first->getControllableEntity())
129            {
130                ControllableEntity* oldentity = it->first->getControllableEntity();
[3038]131
[3033]132                ControllableEntity* entity = this->defaultControllableEntity_.fabricate(oldentity->getCreator());
133                if (oldentity->getCamera())
134                {
135                    entity->setPosition(oldentity->getCamera()->getWorldPosition());
136                    entity->setOrientation(oldentity->getCamera()->getWorldOrientation());
137                }
138                else
139                {
140                    entity->setPosition(oldentity->getWorldPosition());
141                    entity->setOrientation(oldentity->getWorldOrientation());
142                }
143
144                it->first->startControl(entity);
145            }
146            else
147                this->spawnPlayerAsDefaultPawn(it->first);
148        }
[2072]149    }
150
151    void Gametype::playerEntered(PlayerInfo* player)
152    {
[2662]153        this->players_[player].state_ = PlayerState::Joined;
[2072]154    }
155
[2826]156    bool Gametype::playerLeft(PlayerInfo* player)
[2072]157    {
[2662]158        std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player);
[2072]159        if (it != this->players_.end())
160        {
161            this->players_.erase(it);
[2826]162            return true;
[2072]163        }
[2826]164        return false;
[2072]165    }
166
167    void Gametype::playerSwitched(PlayerInfo* player, Gametype* newgametype)
168    {
169    }
170
171    void Gametype::playerSwitchedBack(PlayerInfo* player, Gametype* oldgametype)
172    {
173    }
174
[2826]175    bool Gametype::playerChangedName(PlayerInfo* player)
[2072]176    {
177        if (this->players_.find(player) != this->players_.end())
178        {
179            if (player->getName() != player->getOldName())
180            {
[2826]181                return true;
[2072]182            }
183        }
[2826]184        return false;
[2072]185    }
186
187    void Gametype::pawnPreSpawn(Pawn* pawn)
188    {
189    }
190
191    void Gametype::pawnPostSpawn(Pawn* pawn)
192    {
193    }
194
[2826]195    void Gametype::playerPreSpawn(PlayerInfo* player)
[2072]196    {
[2826]197    }
[2662]198
[2826]199    void Gametype::playerPostSpawn(PlayerInfo* player)
200    {
201    }
[2662]202
[2826]203    void Gametype::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn)
204    {
205    }
206
207    void Gametype::playerStopsControllingPawn(PlayerInfo* player, Pawn* pawn)
208    {
209    }
210
211    bool Gametype::allowPawnHit(Pawn* victim, Pawn* originator)
212    {
213        return true;
214    }
215
216    bool Gametype::allowPawnDamage(Pawn* victim, Pawn* originator)
217    {
218        return true;
219    }
220
221    bool Gametype::allowPawnDeath(Pawn* victim, Pawn* originator)
222    {
223        return true;
224    }
225
226    void Gametype::pawnKilled(Pawn* victim, Pawn* killer)
227    {
[2662]228        if (victim && victim->getPlayer())
229        {
230            std::map<PlayerInfo*, Player>::iterator it = this->players_.find(victim->getPlayer());
231            if (it != this->players_.end())
232            {
233                it->second.state_ = PlayerState::Dead;
234                it->second.killed_++;
235
236                // Reward killer
[3099]237                if (killer && killer->getPlayer())
[2839]238                {
239                    std::map<PlayerInfo*, Player>::iterator it = this->players_.find(killer->getPlayer());
240                    if (it != this->players_.end())
[3099]241                    {
[2839]242                        it->second.frags_++;
[3099]243
244                        if (killer->getPlayer()->getClientID() != CLIENTID_UNKNOWN)
245                            this->gtinfo_.sendKillMessage("You killed " + victim->getPlayer()->getName(), killer->getPlayer()->getClientID());
246                        if (victim->getPlayer()->getClientID() != CLIENTID_UNKNOWN)
247                            this->gtinfo_.sendDeathMessage("You were killed by " + killer->getPlayer()->getName(), victim->getPlayer()->getClientID());
248                    }
[2839]249                }
[2662]250
251                ControllableEntity* entity = this->defaultControllableEntity_.fabricate(victim->getCreator());
252                if (victim->getCamera())
253                {
254                    entity->setPosition(victim->getCamera()->getWorldPosition());
255                    entity->setOrientation(victim->getCamera()->getWorldOrientation());
256                }
257                else
258                {
259                    entity->setPosition(victim->getWorldPosition());
260                    entity->setOrientation(victim->getWorldOrientation());
261                }
262                it->first->startControl(entity);
263            }
264            else
265                COUT(2) << "Warning: Killed Pawn was not in the playerlist" << std::endl;
266        }
[2072]267    }
268
[2826]269    void Gametype::playerScored(PlayerInfo* player)
[2072]270    {
[2826]271        std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player);
272        if (it != this->players_.end())
273            it->second.frags_++;
[2072]274    }
275
[2890]276    int Gametype::getScore(PlayerInfo* player) const
277    {
278        std::map<PlayerInfo*, Player>::const_iterator it = this->players_.find(player);
279        if (it != this->players_.end())
280            return it->second.frags_;
281        else
282            return 0;
283    }
284
[2072]285    SpawnPoint* Gametype::getBestSpawnPoint(PlayerInfo* player) const
286    {
287        if (this->spawnpoints_.size() > 0)
288        {
[3192]289            unsigned int randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(this->spawnpoints_.size())));
[2072]290            unsigned int index = 0;
291            for (std::set<SpawnPoint*>::const_iterator it = this->spawnpoints_.begin(); it != this->spawnpoints_.end(); ++it)
292            {
293                if (index == randomspawn)
294                    return (*it);
295
296                ++index;
297            }
298        }
299        return 0;
300    }
301
[2171]302    void Gametype::assignDefaultPawnsIfNeeded()
[2072]303    {
[2662]304        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
[2072]305        {
[2662]306            if (!it->first->getControllableEntity())
[2072]307            {
[2662]308                it->second.state_ = PlayerState::Dead;
309
310                if (!it->first->isReadyToSpawn() || !this->gtinfo_.bStarted_)
[2072]311                {
[3033]312                    this->spawnPlayerAsDefaultPawn(it->first);
313                    it->second.state_ = PlayerState::Dead;
[2072]314                }
315            }
316        }
317    }
318
319    void Gametype::checkStart()
320    {
[2662]321        if (!this->gtinfo_.bStarted_)
[2072]322        {
[2662]323            if (this->gtinfo_.bStartCountdownRunning_)
[2072]324            {
[2662]325                if (this->gtinfo_.startCountdown_ <= 0)
[2072]326                {
[2662]327                    this->gtinfo_.bStartCountdownRunning_ = false;
328                    this->gtinfo_.startCountdown_ = 0;
[2072]329                    this->start();
330                }
331            }
332            else if (this->players_.size() > 0)
333            {
334                if (this->bAutoStart_)
335                {
336                    this->start();
337                }
338                else
339                {
340                    bool allplayersready = true;
[2662]341                    bool hashumanplayers = false;
342                    for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
[2072]343                    {
[2171]344                        if (!it->first->isReadyToSpawn())
[2072]345                            allplayersready = false;
[2662]346                        if (it->first->isHumanPlayer())
347                            hashumanplayers = true;
[2072]348                    }
[2662]349                    if (allplayersready && hashumanplayers)
[2072]350                    {
[2662]351                        this->gtinfo_.startCountdown_ = this->initialStartCountdown_;
352                        this->gtinfo_.bStartCountdownRunning_ = true;
[2072]353                    }
354                }
355            }
356        }
357    }
358
359    void Gametype::spawnPlayersIfRequested()
360    {
[2662]361        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
[2171]362            if (it->first->isReadyToSpawn() || this->bForceSpawn_)
363                this->spawnPlayer(it->first);
[2072]364    }
365
366    void Gametype::spawnDeadPlayersIfRequested()
367    {
[2662]368        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
369            if (it->second.state_ == PlayerState::Dead)
[2171]370                if (it->first->isReadyToSpawn() || this->bForceSpawn_)
371                    this->spawnPlayer(it->first);
[2072]372    }
373
374    void Gametype::spawnPlayer(PlayerInfo* player)
375    {
376        SpawnPoint* spawnpoint = this->getBestSpawnPoint(player);
377        if (spawnpoint)
378        {
[2826]379            this->playerPreSpawn(player);
[2072]380            player->startControl(spawnpoint->spawn());
[2662]381            this->players_[player].state_ = PlayerState::Alive;
[2826]382            this->playerPostSpawn(player);
[2072]383        }
384        else
385        {
386            COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl;
387            abort();
388        }
389    }
[2662]390
[3033]391    void Gametype::spawnPlayerAsDefaultPawn(PlayerInfo* player)
392    {
393        SpawnPoint* spawn = this->getBestSpawnPoint(player);
394        if (spawn)
395        {
396            // force spawn at spawnpoint with default pawn
397            ControllableEntity* entity = this->defaultControllableEntity_.fabricate(spawn);
398            spawn->spawn(entity);
399            player->startControl(entity);
400        }
401        else
402        {
403            COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl;
404            abort();
405        }
406    }
407
[2662]408    void Gametype::addBots(unsigned int amount)
409    {
410        for (unsigned int i = 0; i < amount; ++i)
[2839]411            this->botclass_.fabricate(this);
[2662]412    }
413
414    void Gametype::killBots(unsigned int amount)
415    {
416        unsigned int i = 0;
417        for (ObjectList<Bot>::iterator it = ObjectList<Bot>::begin(); (it != ObjectList<Bot>::end()) && ((amount == 0) || (i < amount)); )
418        {
419            if (it->getGametype() == this)
420            {
421                delete (*(it++));
422                ++i;
423            }
424        }
425    }
[3033]426
427    void Gametype::addTime(float t)
[3038]428    {
[3033]429        if (this->timeLimit_ == 0)
430          this->time_ -= t;
431        else
432          this->time_ += t;
433    }
434
435    void Gametype::removeTime(float t)
[3038]436    {
[3033]437        if (this->timeLimit_ == 0)
438          this->time_ += t;
439        else
440          this->time_ -= t;
441    }
442
443    void Gametype::resetTimer()
[3038]444    {
[3033]445        this->resetTimer(timeLimit_);
446    }
447
448    void Gametype::resetTimer(float t)
[3038]449    {
[3033]450        this->timeLimit_ = t;
451        this->time_ = t;
452    }
[2072]453}
Note: See TracBrowser for help on using the repository browser.