Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2447 was 2447, checked in by landauf, 15 years ago
  • Removed directional-light-hack from Scene
  • Many changes in Light, works in all game-modes (standalone, dedicated, server and client)
  • Fixed a bug which caused clients to not having shadows

There's still a big problem, bug I can't explain it.

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