Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2492 was 2492, checked in by rgrieder, 15 years ago

Merged overlay branch into presentation branch.

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