[7903] | 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" |
---|
[8858] | 32 | #include "chat/ChatManager.h" |
---|
[7903] | 33 | #include "infos/PlayerInfo.h" |
---|
| 34 | #include "worldentities/pawns/Pawn.h" |
---|
[9667] | 35 | #include "core/config/ConfigValueIncludes.h" |
---|
[7903] | 36 | #include "util/Convert.h" |
---|
| 37 | |
---|
| 38 | namespace orxonox |
---|
| 39 | { |
---|
[9667] | 40 | RegisterUnloadableClass(LastTeamStanding); |
---|
[7903] | 41 | |
---|
[9667] | 42 | LastTeamStanding::LastTeamStanding(Context* context) : TeamDeathmatch(context) |
---|
[7903] | 43 | { |
---|
| 44 | RegisterObject(LastTeamStanding); |
---|
| 45 | this->bForceSpawn_ = true; |
---|
[8212] | 46 | this->lives = 4; |
---|
[7903] | 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 | } |
---|
[8706] | 56 | |
---|
[7903] | 57 | LastTeamStanding::~LastTeamStanding() |
---|
| 58 | { |
---|
[8706] | 59 | } |
---|
[7903] | 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 */ |
---|
[8706] | 70 | |
---|
[7903] | 71 | this->timeToAct_[player] = timeRemaining; |
---|
| 72 | this->playerDelayTime_[player] = respawnDelay; |
---|
| 73 | this->inGame_[player] = true; |
---|
[8170] | 74 | unsigned int team = getTeam(player); |
---|
[8706] | 75 | if(team >= eachTeamsPlayers.size()) // make sure getTeam returns a regular value |
---|
[7903] | 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); |
---|
[8170] | 91 | unsigned int team = getTeam(player); |
---|
[8706] | 92 | if(team >= eachTeamsPlayers.size()) // make sure getTeam returns a regular value |
---|
[7903] | 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;} |
---|
[8706] | 108 | |
---|
[7903] | 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 | { |
---|
[8170] | 113 | unsigned int team = getTeam(victim->getPlayer()); |
---|
[8706] | 114 | if(team >= eachTeamsPlayers.size()) // make sure getTeam returns a regular value |
---|
[7903] | 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"; |
---|
[8858] | 120 | ChatManager::message(message); |
---|
[7903] | 121 | } |
---|
| 122 | return allow; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | bool LastTeamStanding::allowPawnDamage(Pawn* victim, Pawn* originator) |
---|
| 126 | { |
---|
| 127 | bool allow = TeamDeathmatch::allowPawnDamage(victim, originator); |
---|
| 128 | if(!allow) {return allow;} |
---|
| 129 | if (originator && originator->getPlayer())// only for safety |
---|
| 130 | { |
---|
| 131 | this->timeToAct_[originator->getPlayer()] = timeRemaining; |
---|
| 132 | |
---|
| 133 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(originator->getPlayer()); |
---|
| 134 | if (it != this->players_.end()) |
---|
| 135 | { |
---|
[8327] | 136 | if (it->first->getClientID()== NETWORK_PEER_ID_UNKNOWN) |
---|
[7903] | 137 | return true; |
---|
| 138 | const std::string& message = ""; // resets Camper-Warning-message |
---|
| 139 | this->gtinfo_->sendFadingMessage(message,it->first->getClientID()); |
---|
[8706] | 140 | } |
---|
[7903] | 141 | } |
---|
| 142 | return allow; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | void LastTeamStanding::spawnDeadPlayersIfRequested() |
---|
| 146 | { |
---|
| 147 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
| 148 | if (it->second.state_ == PlayerState::Dead) |
---|
| 149 | { |
---|
| 150 | bool alive = (0 < playerLives_[it->first]&&(inGame_[it->first])); |
---|
| 151 | if (alive&&(it->first->isReadyToSpawn() || this->bForceSpawn_)) |
---|
| 152 | { |
---|
| 153 | this->spawnPlayer(it->first); |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | void LastTeamStanding::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn) |
---|
| 159 | { |
---|
| 160 | if (!player) |
---|
| 161 | return; |
---|
| 162 | TeamDeathmatch::playerStartsControllingPawn(player,pawn); |
---|
[8706] | 163 | |
---|
[7903] | 164 | this->timeToAct_[player] = timeRemaining + 3.0f + respawnDelay;//reset timer |
---|
| 165 | this->playerDelayTime_[player] = respawnDelay; |
---|
[8706] | 166 | |
---|
[7903] | 167 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player); |
---|
| 168 | if (it != this->players_.end()) |
---|
| 169 | { |
---|
[8327] | 170 | if (it->first->getClientID()== NETWORK_PEER_ID_UNKNOWN) |
---|
[7903] | 171 | return; |
---|
| 172 | const std::string& message = ""; // resets Camper-Warning-message |
---|
| 173 | this->gtinfo_->sendFadingMessage(message,it->first->getClientID()); |
---|
[8706] | 174 | } |
---|
[7903] | 175 | } |
---|
| 176 | |
---|
| 177 | void LastTeamStanding::tick(float dt) |
---|
| 178 | { |
---|
| 179 | SUPER(LastTeamStanding, tick, dt); |
---|
| 180 | if(this->hasStarted()&&(!this->hasEnded())) |
---|
| 181 | { |
---|
[8170] | 182 | if ( this->hasStarted()&&(teamsAlive<=1) )//last team remaining -> game will end |
---|
[7903] | 183 | { |
---|
| 184 | this->end(); |
---|
| 185 | } |
---|
| 186 | for (std::map<PlayerInfo*, float>::iterator it = this->timeToAct_.begin(); it != this->timeToAct_.end(); ++it) |
---|
[8706] | 187 | { |
---|
[7903] | 188 | if (playerGetLives(it->first) <= 0)//Players without lives shouldn't be affected by time. |
---|
[8706] | 189 | continue; |
---|
[7903] | 190 | it->second -= dt;//Decreases punishment time. |
---|
[8706] | 191 | if (!inGame_[it->first])//Manages respawn delay - player is forced to respawn after the delaytime is used up. |
---|
[7903] | 192 | { |
---|
| 193 | playerDelayTime_[it->first] -= dt; |
---|
| 194 | if (playerDelayTime_[it->first] <= 0) |
---|
| 195 | this->inGame_[it->first] = true; |
---|
| 196 | |
---|
[8327] | 197 | if (it->first->getClientID()== NETWORK_PEER_ID_UNKNOWN) |
---|
[7903] | 198 | continue; |
---|
[8234] | 199 | int output = 1 + (int)playerDelayTime_[it->first]; |
---|
[7903] | 200 | const std::string& message = "Respawn in " +multi_cast<std::string>(output)+ " seconds." ;//Countdown |
---|
| 201 | this->gtinfo_->sendFadingMessage(message,it->first->getClientID()); |
---|
| 202 | } |
---|
| 203 | else if (it->second < 0.0f) |
---|
| 204 | { |
---|
| 205 | it->second = timeRemaining + 3.0f;//reset punishment-timer |
---|
| 206 | if (playerGetLives(it->first) > 0) |
---|
| 207 | { |
---|
| 208 | this->punishPlayer(it->first); |
---|
[8327] | 209 | if (it->first->getClientID() == NETWORK_PEER_ID_UNKNOWN) |
---|
[7903] | 210 | return; |
---|
| 211 | const std::string& message = ""; // resets Camper-Warning-message |
---|
| 212 | this->gtinfo_->sendFadingMessage(message, it->first->getClientID()); |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | else if (it->second < timeRemaining/5)//Warning message |
---|
| 216 | { |
---|
[8327] | 217 | if (it->first->getClientID()== NETWORK_PEER_ID_UNKNOWN) |
---|
[7903] | 218 | continue; |
---|
| 219 | const std::string& message = "Camper Warning! Don't forget to shoot."; |
---|
| 220 | this->gtinfo_->sendFadingMessage(message, it->first->getClientID()); |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | void LastTeamStanding::end()//TODO: Send the message to the whole team |
---|
| 227 | { |
---|
| 228 | Gametype::end(); |
---|
[8170] | 229 | int party = -1; |
---|
| 230 | //find a player who survived |
---|
[7903] | 231 | for (std::map<PlayerInfo*, int>::iterator it = this->playerLives_.begin(); it != this->playerLives_.end(); ++it) |
---|
| 232 | { |
---|
[8327] | 233 | if (it->first->getClientID() == NETWORK_PEER_ID_UNKNOWN) |
---|
[7903] | 234 | continue; |
---|
| 235 | |
---|
[8175] | 236 | if (it->second > 0)//a player that is alive |
---|
[8170] | 237 | { |
---|
[8175] | 238 | //which party has survived? |
---|
[8170] | 239 | std::map<PlayerInfo*, int>::iterator it2 = this->teamnumbers_.find(it->first); |
---|
| 240 | if (it2 != this->teamnumbers_.end()) |
---|
| 241 | { |
---|
| 242 | party = it2->second; |
---|
| 243 | } |
---|
[9941] | 244 | if (party < 0) return; //if search failed |
---|
[8175] | 245 | //victory message to all team members, loose message to everyone else |
---|
[9941] | 246 | this->announceTeamWin(party); |
---|
| 247 | |
---|
[8170] | 248 | return; |
---|
| 249 | } |
---|
[7903] | 250 | } |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | |
---|
| 254 | int LastTeamStanding::getMinLives() |
---|
| 255 | { |
---|
| 256 | int min = lives; |
---|
| 257 | for (std::map<PlayerInfo*, int>::iterator it = this->playerLives_.begin(); it != this->playerLives_.end(); ++it) |
---|
| 258 | { |
---|
| 259 | if (it->second <= 0) |
---|
| 260 | continue; |
---|
| 261 | if (it->second < lives) |
---|
| 262 | min = it->second; |
---|
| 263 | } |
---|
| 264 | return min; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | void LastTeamStanding::punishPlayer(PlayerInfo* player) |
---|
| 268 | { |
---|
| 269 | if(!player) |
---|
| 270 | return; |
---|
| 271 | if(bNoPunishment) |
---|
| 272 | return; |
---|
| 273 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player); |
---|
| 274 | if (it != this->players_.end()) |
---|
| 275 | { |
---|
| 276 | if(!player->getControllableEntity()) |
---|
| 277 | return; |
---|
[9348] | 278 | Pawn* pawn = orxonox_cast<Pawn*>(player->getControllableEntity()); |
---|
[7903] | 279 | if(!pawn) |
---|
| 280 | return; |
---|
| 281 | if(bHardPunishment) |
---|
| 282 | { |
---|
| 283 | pawn->kill(); |
---|
| 284 | this->timeToAct_[player] = timeRemaining + 3.0f + respawnDelay;//reset timer |
---|
| 285 | } |
---|
| 286 | else |
---|
| 287 | { |
---|
[8234] | 288 | float damage = pawn->getMaxHealth()*punishDamageRate*0.5f;//TODO: Factor 0.5 is hard coded. Where is the ratio between MaxHealth actually defined? |
---|
[7903] | 289 | pawn->removeHealth(damage); |
---|
| 290 | this->timeToAct_[player] = timeRemaining;//reset timer |
---|
| 291 | } |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | int LastTeamStanding::playerGetLives(PlayerInfo* player) |
---|
| 296 | { |
---|
| 297 | if (player) |
---|
| 298 | return playerLives_[player]; |
---|
| 299 | else |
---|
| 300 | return 0; |
---|
| 301 | } |
---|
[8706] | 302 | |
---|
[7903] | 303 | void LastTeamStanding::setConfigValues() |
---|
| 304 | { |
---|
| 305 | SetConfigValue(lives, 4); |
---|
| 306 | SetConfigValue(timeRemaining, 15.0f); |
---|
| 307 | SetConfigValue(respawnDelay, 4.0f); |
---|
| 308 | SetConfigValue(bNoPunishment, false); |
---|
| 309 | SetConfigValue(bHardPunishment, false); |
---|
| 310 | } |
---|
| 311 | } |
---|