Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2428 was 2428, checked in by landauf, 15 years ago
  • Moved some variables from Gametype to GametypeInfo (Gametype exists only on the Server while GametypeInfo is synchronized)
  • Created a new HUD-element, GametypeStatus, based on GametypeInfo (the same effect was formerly achieved by using a hack in Spectator and a TextOverlay)
  • HumanController creates a HUD (additionally to the SpaceShips HUD) which includes GametypeStatus and ChatOverlay and even more in the future
  • Property svn:eol-style set to native
File size: 8.0 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 "objects/infos/PlayerInfo.h"
38#include "objects/worldentities/pawns/Spectator.h"
39#include "objects/worldentities/SpawnPoint.h"
40
41#include "network/Host.h"
42
43namespace orxonox
44{
45    CreateUnloadableFactory(Gametype);
46
47    Gametype::Gametype(BaseObject* creator) : BaseObject(creator), gtinfo_(creator)
48    {
49        RegisterObject(Gametype);
50
51        this->defaultControllableEntity_ = Class(Spectator);
52
53        this->bAutoStart_ = false;
54        this->bForceSpawn_ = false;
55
56        this->initialStartCountdown_ = 3;
57
58        this->setConfigValues();
59    }
60
61    void Gametype::setConfigValues()
62    {
63        SetConfigValue(initialStartCountdown_, 3.0f);
64    }
65
66    void Gametype::tick(float dt)
67    {
68        SUPER(Gametype, tick, dt);
69
70        if (this->gtinfo_.bStartCountdownRunning_ && !this->gtinfo_.bStarted_)
71            this->gtinfo_.startCountdown_ -= dt;
72
73        if (!this->gtinfo_.bStarted_)
74            this->checkStart();
75        else
76            this->spawnDeadPlayersIfRequested();
77
78        this->assignDefaultPawnsIfNeeded();
79    }
80
81    void Gametype::start()
82    {
83        COUT(0) << "game started" << std::endl;
84        this->gtinfo_.bStarted_ = true;
85
86        this->spawnPlayersIfRequested();
87    }
88
89    void Gametype::end()
90    {
91        COUT(0) << "game ended" << std::endl;
92        this->gtinfo_.bEnded_ = true;
93    }
94
95    void Gametype::playerEntered(PlayerInfo* player)
96    {
97        this->players_[player] = PlayerState::Joined;
98
99        std::string message = player->getName() + " entered the game";
100        COUT(0) << message << std::endl;
101        Host::Broadcast(message);
102    }
103
104    void Gametype::playerLeft(PlayerInfo* player)
105    {
106        std::map<PlayerInfo*, PlayerState::Enum>::iterator it = this->players_.find(player);
107        if (it != this->players_.end())
108        {
109            this->players_.erase(it);
110
111            std::string message = player->getName() + " left the game";
112            COUT(0) << message << std::endl;
113            Host::Broadcast(message);
114        }
115    }
116
117    void Gametype::playerSwitched(PlayerInfo* player, Gametype* newgametype)
118    {
119    }
120
121    void Gametype::playerSwitchedBack(PlayerInfo* player, Gametype* oldgametype)
122    {
123    }
124
125    void Gametype::playerChangedName(PlayerInfo* player)
126    {
127        if (this->players_.find(player) != this->players_.end())
128        {
129            if (player->getName() != player->getOldName())
130            {
131                std::string message = player->getOldName() + " changed name to " + player->getName();
132                COUT(0) << message << std::endl;
133                Host::Broadcast(message);
134            }
135        }
136    }
137
138    void Gametype::pawnPreSpawn(Pawn* pawn)
139    {
140    }
141
142    void Gametype::pawnPostSpawn(Pawn* pawn)
143    {
144    }
145
146    void Gametype::pawnKilled(Pawn* victim, Pawn* killer)
147    {
148    }
149
150    void Gametype::playerScored(PlayerInfo* player)
151    {
152    }
153
154    SpawnPoint* Gametype::getBestSpawnPoint(PlayerInfo* player) const
155    {
156        if (this->spawnpoints_.size() > 0)
157        {
158            unsigned int randomspawn = (unsigned int)rnd(this->spawnpoints_.size());
159            unsigned int index = 0;
160            for (std::set<SpawnPoint*>::const_iterator it = this->spawnpoints_.begin(); it != this->spawnpoints_.end(); ++it)
161            {
162                if (index == randomspawn)
163                    return (*it);
164
165                ++index;
166            }
167        }
168        return 0;
169    }
170
171    void Gametype::assignDefaultPawnsIfNeeded()
172    {
173        for (std::map<PlayerInfo*, PlayerState::Enum>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
174        {
175            if (!it->first->getControllableEntity())
176            {
177                it->second = PlayerState::Dead;
178
179                if (!it->first->isReadyToSpawn() || !this->gtinfo_.bStarted_)
180                {
181                    SpawnPoint* spawn = this->getBestSpawnPoint(it->first);
182                    if (spawn)
183                    {
184                        // force spawn at spawnpoint with default pawn
185                        ControllableEntity* entity = this->defaultControllableEntity_.fabricate(spawn);
186                        spawn->spawn(entity);
187                        it->first->startControl(entity);
188                        it->second = PlayerState::Dead;
189                    }
190                    else
191                    {
192                        COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl;
193                        abort();
194                    }
195                }
196            }
197        }
198    }
199
200    void Gametype::checkStart()
201    {
202        if (!this->gtinfo_.bStarted_)
203        {
204            if (this->gtinfo_.bStartCountdownRunning_)
205            {
206                if (this->gtinfo_.startCountdown_ <= 0)
207                {
208                    this->gtinfo_.bStartCountdownRunning_ = false;
209                    this->gtinfo_.startCountdown_ = 0;
210                    this->start();
211                }
212            }
213            else if (this->players_.size() > 0)
214            {
215                if (this->bAutoStart_)
216                {
217                    this->start();
218                }
219                else
220                {
221                    bool allplayersready = true;
222                    bool hashumanplayers = false;
223                    for (std::map<PlayerInfo*, PlayerState::Enum>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
224                    {
225                        if (!it->first->isReadyToSpawn())
226                            allplayersready = false;
227                        if (it->first->isHumanPlayer())
228                            hashumanplayers = true;
229                    }
230                    if (allplayersready && hashumanplayers)
231                    {
232                        this->gtinfo_.startCountdown_ = this->initialStartCountdown_;
233                        this->gtinfo_.bStartCountdownRunning_ = true;
234                    }
235                }
236            }
237        }
238    }
239
240    void Gametype::spawnPlayersIfRequested()
241    {
242        for (std::map<PlayerInfo*, PlayerState::Enum>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
243            if (it->first->isReadyToSpawn() || this->bForceSpawn_)
244                this->spawnPlayer(it->first);
245    }
246
247    void Gametype::spawnDeadPlayersIfRequested()
248    {
249        for (std::map<PlayerInfo*, PlayerState::Enum>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
250            if (it->second == PlayerState::Dead)
251                if (it->first->isReadyToSpawn() || this->bForceSpawn_)
252                    this->spawnPlayer(it->first);
253    }
254
255    void Gametype::spawnPlayer(PlayerInfo* player)
256    {
257        SpawnPoint* spawnpoint = this->getBestSpawnPoint(player);
258        if (spawnpoint)
259        {
260            player->startControl(spawnpoint->spawn());
261            this->players_[player] = PlayerState::Alive;
262        }
263        else
264        {
265            COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl;
266            abort();
267        }
268    }
269}
Note: See TracBrowser for help on using the repository browser.