Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3033 was 3033, checked in by landauf, 15 years ago

merged gametypes branch back to trunk

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