Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2970 was 2970, checked in by Aurelian, 15 years ago

Added timer in class gametype, timer working in asteroids, modified checkpoint with firstCheckPoint… working! yeepa ;-)

  • Property svn:eol-style set to native
File size: 12.6 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/Core.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 (Core::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                    this->playerScored(killer->getPlayer());
241
242                ControllableEntity* entity = this->defaultControllableEntity_.fabricate(victim->getCreator());
243                if (victim->getCamera())
244                {
245                    entity->setPosition(victim->getCamera()->getWorldPosition());
246                    entity->setOrientation(victim->getCamera()->getWorldOrientation());
247                }
248                else
249                {
250                    entity->setPosition(victim->getWorldPosition());
251                    entity->setOrientation(victim->getWorldOrientation());
252                }
253                it->first->startControl(entity);
254            }
255            else
256                COUT(2) << "Warning: Killed Pawn was not in the playerlist" << std::endl;
257        }
258    }
259
260    void Gametype::playerScored(PlayerInfo* player)
261    {
262        std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player);
263        if (it != this->players_.end())
264            it->second.frags_++;
265    }
266
267    SpawnPoint* Gametype::getBestSpawnPoint(PlayerInfo* player) const
268    {
269        if (this->spawnpoints_.size() > 0)
270        {
271            unsigned int randomspawn = (unsigned int)rnd(this->spawnpoints_.size());
272            unsigned int index = 0;
273            for (std::set<SpawnPoint*>::const_iterator it = this->spawnpoints_.begin(); it != this->spawnpoints_.end(); ++it)
274            {
275                if (index == randomspawn)
276                    return (*it);
277
278                ++index;
279            }
280        }
281        return 0;
282    }
283
284    void Gametype::assignDefaultPawnsIfNeeded()
285    {
286        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
287        {
288            if (!it->first->getControllableEntity())
289            {
290                it->second.state_ = PlayerState::Dead;
291
292                if (!it->first->isReadyToSpawn() || !this->gtinfo_.bStarted_)
293                {
294                    this->spawnPlayerAsDefaultPawn(it->first);
295                    it->second.state_ = PlayerState::Dead;
296                }
297            }
298        }
299    }
300
301    void Gametype::checkStart()
302    {
303        if (!this->gtinfo_.bStarted_)
304        {
305            if (this->gtinfo_.bStartCountdownRunning_)
306            {
307                if (this->gtinfo_.startCountdown_ <= 0)
308                {
309                    this->gtinfo_.bStartCountdownRunning_ = false;
310                    this->gtinfo_.startCountdown_ = 0;
311                    this->start();
312                }
313            }
314            else if (this->players_.size() > 0)
315            {
316                if (this->bAutoStart_)
317                {
318                    this->start();
319                }
320                else
321                {
322                    bool allplayersready = true;
323                    bool hashumanplayers = false;
324                    for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
325                    {
326                        if (!it->first->isReadyToSpawn())
327                            allplayersready = false;
328                        if (it->first->isHumanPlayer())
329                            hashumanplayers = true;
330                    }
331                    if (allplayersready && hashumanplayers)
332                    {
333                        this->gtinfo_.startCountdown_ = this->initialStartCountdown_;
334                        this->gtinfo_.bStartCountdownRunning_ = true;
335                    }
336                }
337            }
338        }
339    }
340
341    void Gametype::spawnPlayersIfRequested()
342    {
343        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
344            if (it->first->isReadyToSpawn() || this->bForceSpawn_)
345                this->spawnPlayer(it->first);
346    }
347
348    void Gametype::spawnDeadPlayersIfRequested()
349    {
350        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
351            if (it->second.state_ == PlayerState::Dead)
352                if (it->first->isReadyToSpawn() || this->bForceSpawn_)
353                    this->spawnPlayer(it->first);
354    }
355
356    void Gametype::spawnPlayer(PlayerInfo* player)
357    {
358        SpawnPoint* spawnpoint = this->getBestSpawnPoint(player);
359        if (spawnpoint)
360        {
361            this->playerPreSpawn(player);
362            player->startControl(spawnpoint->spawn());
363            this->players_[player].state_ = PlayerState::Alive;
364            this->playerPostSpawn(player);
365        }
366        else
367        {
368            COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl;
369            abort();
370        }
371    }
372
373    void Gametype::spawnPlayerAsDefaultPawn(PlayerInfo* player)
374    {
375        SpawnPoint* spawn = this->getBestSpawnPoint(player);
376        if (spawn)
377        {
378            // force spawn at spawnpoint with default pawn
379            ControllableEntity* entity = this->defaultControllableEntity_.fabricate(spawn);
380            spawn->spawn(entity);
381            player->startControl(entity);
382        }
383        else
384        {
385            COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl;
386            abort();
387        }
388    }
389
390    void Gametype::addBots(unsigned int amount)
391    {
392        for (unsigned int i = 0; i < amount; ++i)
393            new Bot(this);
394    }
395
396    void Gametype::killBots(unsigned int amount)
397    {
398        unsigned int i = 0;
399        for (ObjectList<Bot>::iterator it = ObjectList<Bot>::begin(); (it != ObjectList<Bot>::end()) && ((amount == 0) || (i < amount)); )
400        {
401            if (it->getGametype() == this)
402            {
403                delete (*(it++));
404                ++i;
405            }
406        }
407    }
408
409    void Gametype::addTime(float t)
410    { 
411        if (this->timeLimit_ == 0)
412          this->time_ -= t;
413        else
414          this->time_ += t;
415    }
416
417    void Gametype::removeTime(float t)
418    { 
419        if (this->timeLimit_ == 0)
420          this->time_ += t;
421        else
422          this->time_ -= t;
423    }
424
425    void Gametype::resetTimer()
426    { 
427        this->resetTimer(timeLimit_);
428    }
429
430    void Gametype::resetTimer(float t)
431    { 
432        this->timeLimit_ = t;
433        this->time_ = t;
434    }
435}
Note: See TracBrowser for help on using the repository browser.