Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

respawn of late joined clients works now

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