Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/gametypes/Gametype.cc @ 8631

Last change on this file since 8631 was 8631, checked in by dafrick, 13 years ago

Merging spacerace branch into presentation branch.

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