Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/gametypes/LastTeamStanding.cc @ 8212

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

Set lives value to 4 in last team standing gametype. 1 live is only needed for testing purposes.

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