Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7284 was 7284, checked in by landauf, 14 years ago

merged consolecommands3 branch back to trunk.

note: the console command interface has changed completely, but the documentation is not yet up to date. just copy an existing command and change it to your needs, it's pretty self-explanatory. also the include files related to console commands are now located in core/command/. in the game it should work exactly like before, except for some changes in the auto-completion.

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