Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/lastmanstanding2/src/orxonox/gametypes/LastTeamStanding.cc @ 7899

Last change on this file since 7899 was 7899, checked in by jo, 13 years ago

Level polishing.

File size: 11.3 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 *      Johannes Ritz
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "LastTeamStanding.h"
30
31#include "core/CoreIncludes.h"
32#include "network/Host.h"
33#include "infos/PlayerInfo.h"
34#include "worldentities/pawns/Pawn.h"
35#include "core/ConfigValueIncludes.h"
36#include "util/Convert.h"
37
38namespace orxonox
39{
40    CreateUnloadableFactory(LastTeamStanding);
41
42    LastTeamStanding::LastTeamStanding(BaseObject* creator) : TeamDeathmatch(creator)
43    {
44        RegisterObject(LastTeamStanding);
45        this->bForceSpawn_ = true;
46        this->lives = 1;//4
47        this->eachTeamsPlayers.resize(teams_,0);
48        this->teamsAlive = 0;
49        this->bMinTeamsReached = false;
50        this->bNoPunishment = false;
51        this->bHardPunishment = false;
52        this->punishDamageRate = 0.4f;
53        this->timeRemaining = 15.0f;
54        this->respawnDelay = 4.0f;
55        this->setHUDTemplate("lastTeamStandingHUD");
56    }
57   
58    LastTeamStanding::~LastTeamStanding()
59    {
60        this->playerLives_.clear();
61        this->eachTeamsPlayers.clear();
62        this->timeToAct_.clear();
63        this->inGame_.clear();
64        this->playerDelayTime_.clear();
65    }   
66
67    void LastTeamStanding::playerEntered(PlayerInfo* player)
68    {
69        if (!player)// only for safety
70            return;
71        TeamDeathmatch::playerEntered(player);
72        if (teamsAlive<=1)
73            playerLives_[player]=lives;
74        else
75            playerLives_[player]=getMinLives();//new players only get minimum of lives */
76       
77        if(this->eachTeamsPlayers[getTeam(player)]==0) //if a player is the first in his group, a new team is alive
78            this->teamsAlive++;
79        this->eachTeamsPlayers[getTeam(player)]++; //the number of player in this team is increased
80
81        if (teamsAlive>1) // Now the game is allowed to end, since there are at least two teams.
82            bMinTeamsReached = true; // since there are at least two teams, the game is allowed to end
83        this->timeToAct_[player] = timeRemaining;
84        this->playerDelayTime_[player] = respawnDelay;
85        this->inGame_[player] = true;
86    }
87
88    bool LastTeamStanding::playerLeft(PlayerInfo* player)
89    {
90        bool valid_player = TeamDeathmatch::playerLeft(player);
91        if (valid_player)
92        {
93            this->eachTeamsPlayers[getTeam(player)]--;       // a player left the team
94            if(this->eachTeamsPlayers[getTeam(player)] == 0) // if it was the last player a team died
95                 this->teamsAlive--;
96            this->playerLives_.erase(player);
97            this->timeToAct_.erase(player);
98            this->playerDelayTime_.erase(player);
99            this->inGame_.erase(player);
100        }
101
102        return valid_player;
103    }
104
105    bool LastTeamStanding::allowPawnDeath(Pawn* victim, Pawn* originator)
106    {
107        if (!victim||!victim->getPlayer())// only for safety
108            return true;
109        bool allow = TeamDeathmatch::allowPawnDeath(victim, originator);
110        if(!allow) {return allow;}
111       
112        playerLives_[victim->getPlayer()] = playerLives_[victim->getPlayer()] - 1; //player lost a live
113        this->inGame_[victim->getPlayer()] = false; //if player dies, he isn't allowed to respawn immediately
114        if (playerLives_[victim->getPlayer()]<=0) //if player lost all lives
115        {
116            this->eachTeamsPlayers[getTeam(victim->getPlayer())]--;
117            if(eachTeamsPlayers[getTeam(victim->getPlayer())] == 0) //last team member died
118                this->teamsAlive--;
119            const std::string& message = victim->getPlayer()->getName() + " has lost all lives";
120            COUT(0) << message << std::endl;
121            Host::Broadcast(message);
122        }
123
124        return allow;
125    }
126
127    bool LastTeamStanding::allowPawnDamage(Pawn* victim, Pawn* originator)
128    {
129        bool allow = TeamDeathmatch::allowPawnDamage(victim, originator);
130        if(!allow) {return allow;}
131        if (originator && originator->getPlayer())// only for safety
132        {
133            this->timeToAct_[originator->getPlayer()] = timeRemaining;
134
135            std::map<PlayerInfo*, Player>::iterator it = this->players_.find(originator->getPlayer());
136            if (it != this->players_.end())
137            {
138                if (it->first->getClientID()== CLIENTID_UNKNOWN)
139                    return true;
140                const std::string& message = ""; // resets Camper-Warning-message
141                this->gtinfo_->sendFadingMessage(message,it->first->getClientID());
142            }   
143        }
144        return allow;
145    }
146
147    void LastTeamStanding::spawnDeadPlayersIfRequested()
148    {
149        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
150            if (it->second.state_ == PlayerState::Dead)
151            {
152                bool alive = (0 < playerLives_[it->first]&&(inGame_[it->first]));
153                if (alive&&(it->first->isReadyToSpawn() || this->bForceSpawn_))
154                {
155                    this->spawnPlayer(it->first);
156                }
157            }
158    }
159
160    void LastTeamStanding::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn)
161    {
162        if (!player)
163            return;
164        TeamDeathmatch::playerStartsControllingPawn(player,pawn);
165       
166        this->timeToAct_[player] = timeRemaining + 3.0f + respawnDelay;//reset timer
167        this->playerDelayTime_[player] = respawnDelay;
168       
169        std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player);
170        if (it != this->players_.end())
171        {
172            if (it->first->getClientID()== CLIENTID_UNKNOWN)
173                return;
174            const std::string& message = ""; // resets Camper-Warning-message
175            this->gtinfo_->sendFadingMessage(message,it->first->getClientID());
176        } 
177    }
178
179    void LastTeamStanding::tick(float dt)
180    {
181        SUPER(LastTeamStanding, tick, dt);
182        if(this->hasStarted()&&(!this->hasEnded()))
183        {
184            if (bMinTeamsReached &&(this->hasStarted()&&(teamsAlive<=1)))//last team remaining -> game will end
185            {
186                this->end();
187            }
188            for (std::map<PlayerInfo*, float>::iterator it = this->timeToAct_.begin(); it != this->timeToAct_.end(); ++it)
189            {   
190                if (playerGetLives(it->first) <= 0)//Players without lives shouldn't be affected by time.
191                    continue;     
192                it->second -= dt;//Decreases punishment time.
193                if (!inGame_[it->first])//Manages respawn delay - player is forced to respawn after the delaytime is used up.
194                {
195                    playerDelayTime_[it->first] -= dt;
196                    if (playerDelayTime_[it->first] <= 0)
197                    this->inGame_[it->first] = true;
198
199                    if (it->first->getClientID()== CLIENTID_UNKNOWN)
200                        continue;
201                    int output = 1 + playerDelayTime_[it->first];
202                    const std::string& message = "Respawn in " +multi_cast<std::string>(output)+ " seconds." ;//Countdown
203                    this->gtinfo_->sendFadingMessage(message,it->first->getClientID());
204                }
205                else if (it->second < 0.0f)
206                {
207                    it->second = timeRemaining + 3.0f;//reset punishment-timer
208                    if (playerGetLives(it->first) > 0)
209                    {
210                        this->punishPlayer(it->first);
211                        if (it->first->getClientID() == CLIENTID_UNKNOWN)
212                            return;
213                        const std::string& message = ""; // resets Camper-Warning-message
214                        this->gtinfo_->sendFadingMessage(message, it->first->getClientID());
215                    }
216                }
217                else if (it->second < timeRemaining/5)//Warning message
218                {
219                    if (it->first->getClientID()== CLIENTID_UNKNOWN)
220                        continue;
221                    const std::string& message = "Camper Warning! Don't forget to shoot.";
222                    this->gtinfo_->sendFadingMessage(message, it->first->getClientID());
223                }
224            }
225        }
226    }
227
228    void LastTeamStanding::end()//TODO: Send the message to the whole team
229    {
230        Gametype::end();
231       
232        for (std::map<PlayerInfo*, int>::iterator it = this->playerLives_.begin(); it != this->playerLives_.end(); ++it)
233        {
234            if (it->first->getClientID() == CLIENTID_UNKNOWN)
235                continue;
236
237            if (it->second > 0)
238                this->gtinfo_->sendAnnounceMessage("You have won the match!", it->first->getClientID());
239            else
240                this->gtinfo_->sendAnnounceMessage("You have lost the match!", it->first->getClientID());
241        }
242
243    }
244
245
246    int LastTeamStanding::getMinLives()
247    {
248        int min = lives;
249        for (std::map<PlayerInfo*, int>::iterator it = this->playerLives_.begin(); it != this->playerLives_.end(); ++it)
250        {
251            if (it->second <= 0)
252                continue;
253            if (it->second < lives)
254                min = it->second;
255        }
256        return min;
257    }
258
259    void LastTeamStanding::punishPlayer(PlayerInfo* player)
260    {
261        if(!player)
262            return;
263        if(bNoPunishment)
264            return;
265        std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player);
266        if (it != this->players_.end())
267        {
268            if(!player->getControllableEntity())
269                return;
270            Pawn* pawn = dynamic_cast<Pawn*>(player->getControllableEntity());
271            if(!pawn)
272                return;
273            if(bHardPunishment)
274            {
275                pawn->kill();
276                this->timeToAct_[player] = timeRemaining + 3.0f + respawnDelay;//reset timer
277            }
278            else
279            {
280                float damage = pawn->getMaxHealth()*punishDamageRate*0.5;//TODO: Factor 0.5 is hard coded. Where is the ratio between MaxHealth actually defined?
281                pawn->removeHealth(damage);
282                this->timeToAct_[player] = timeRemaining;//reset timer
283            }
284        }
285    }
286
287    int LastTeamStanding::playerGetLives(PlayerInfo* player)
288    {
289        if (player)
290            return  playerLives_[player];
291        else
292            return 0;
293    }
294   
295    void LastTeamStanding::setConfigValues()
296    {
297        SetConfigValue(lives, 4);
298        SetConfigValue(timeRemaining, 15.0f);
299        SetConfigValue(respawnDelay, 4.0f);
300        SetConfigValue(bNoPunishment, false);
301        SetConfigValue(bHardPunishment, false);
302    }
303}
Note: See TracBrowser for help on using the repository browser.