[7480] | 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 | */ |
---|
[7485] | 28 | |
---|
[7480] | 29 | #include "LastManStanding.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" |
---|
[7579] | 36 | #include "util/Convert.h" |
---|
[7480] | 37 | |
---|
| 38 | namespace orxonox |
---|
| 39 | { |
---|
| 40 | CreateUnloadableFactory(LastManStanding); |
---|
| 41 | |
---|
| 42 | LastManStanding::LastManStanding(BaseObject* creator) : Gametype(creator) |
---|
| 43 | { |
---|
| 44 | RegisterObject(LastManStanding); |
---|
| 45 | this->bForceSpawn_=true; |
---|
[7485] | 46 | this->lives=4; |
---|
[7480] | 47 | this->playersAlive=0; |
---|
| 48 | this->timeRemaining=20.0f; |
---|
[7579] | 49 | this->setHUDTemplate("LastmanstandingHUD"); |
---|
[7480] | 50 | } |
---|
| 51 | |
---|
[7542] | 52 | void LastManStanding::spawnDeadPlayersIfRequested() |
---|
| 53 | { |
---|
| 54 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
| 55 | if (it->second.state_ == PlayerState::Dead) |
---|
| 56 | { |
---|
| 57 | bool alive = (0<playerLives_[it->first]); |
---|
| 58 | if (alive&&(it->first->isReadyToSpawn() || this->bForceSpawn_)) |
---|
| 59 | this->spawnPlayer(it->first); |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | |
---|
[7480] | 64 | void LastManStanding::setConfigValues() |
---|
| 65 | { |
---|
[7485] | 66 | SetConfigValue(lives, 4); |
---|
| 67 | SetConfigValue(timeRemaining, 20.0f); |
---|
[7480] | 68 | } |
---|
| 69 | |
---|
| 70 | bool LastManStanding::allowPawnDamage(Pawn* victim, Pawn* originator) |
---|
| 71 | { |
---|
| 72 | if (originator && originator->getPlayer())// only for safety |
---|
| 73 | { |
---|
| 74 | this->timeToAct_[originator->getPlayer()]=timeRemaining; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | return true; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | bool LastManStanding::allowPawnDeath(Pawn* victim, Pawn* originator) |
---|
| 81 | { |
---|
[7542] | 82 | if (!victim||!victim->getPlayer())// only for safety |
---|
[7480] | 83 | return true; |
---|
[7542] | 84 | playerLives_[victim->getPlayer()]=playerLives_[victim->getPlayer()]-1; |
---|
| 85 | if (playerLives_[victim->getPlayer()]<=0)//if player lost all lives |
---|
[7480] | 86 | { |
---|
| 87 | this->playersAlive--; |
---|
| 88 | const std::string& message = victim->getPlayer()->getName() + " has lost all lives"; |
---|
| 89 | COUT(0) << message << std::endl; |
---|
| 90 | Host::Broadcast(message); |
---|
| 91 | } |
---|
| 92 | return true; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | void LastManStanding::start() |
---|
| 96 | { |
---|
| 97 | Gametype::start(); |
---|
| 98 | |
---|
| 99 | std::string message("Try to survive!"); |
---|
| 100 | COUT(0) << message << std::endl; |
---|
| 101 | Host::Broadcast(message); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | void LastManStanding::end() |
---|
| 105 | { |
---|
| 106 | Gametype::end(); |
---|
| 107 | |
---|
| 108 | for (std::map<PlayerInfo*, int>::iterator it = this->playerLives_.begin(); it != this->playerLives_.end(); ++it) |
---|
| 109 | { |
---|
| 110 | if (it->first->getClientID() == CLIENTID_UNKNOWN) |
---|
| 111 | continue; |
---|
| 112 | |
---|
| 113 | if (it->second > 0) |
---|
| 114 | this->gtinfo_->sendAnnounceMessage("You have won the match!", it->first->getClientID()); |
---|
| 115 | else |
---|
| 116 | this->gtinfo_->sendAnnounceMessage("You have lost the match!", it->first->getClientID()); |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | void LastManStanding::playerEntered(PlayerInfo* player) |
---|
| 121 | { |
---|
| 122 | if (!player)// only for safety |
---|
| 123 | return; |
---|
| 124 | Gametype::playerEntered(player); |
---|
| 125 | |
---|
| 126 | playerLives_[player]=lives; |
---|
| 127 | this->playersAlive++; |
---|
| 128 | this->timeToAct_[player]=timeRemaining; |
---|
| 129 | const std::string& message = player->getName() + " entered the game"; |
---|
| 130 | COUT(0) << message << std::endl; |
---|
| 131 | Host::Broadcast(message); |
---|
[7579] | 132 | //Update: EachPlayer's "Players in Game"-HUD |
---|
| 133 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
| 134 | { |
---|
| 135 | if (it->first->getClientID() == CLIENTID_UNKNOWN) |
---|
| 136 | continue; |
---|
| 137 | const std::string& message1 = "Remaining Players: "+ multi_cast<std::string>(playersAlive); |
---|
| 138 | this->gtinfo_->sendStaticMessage(message1,it->first->getClientID(),ColourValue(1.0f, 1.0f, 0.5f)); |
---|
| 139 | } |
---|
| 140 | |
---|
[7480] | 141 | } |
---|
| 142 | |
---|
| 143 | bool LastManStanding::playerLeft(PlayerInfo* player) |
---|
| 144 | { |
---|
| 145 | bool valid_player = Gametype::playerLeft(player); |
---|
| 146 | |
---|
| 147 | if (valid_player) |
---|
| 148 | { |
---|
| 149 | this->playersAlive--; |
---|
| 150 | //this->playerLives_[player].erase (player); not necessary? |
---|
| 151 | // |
---|
| 152 | const std::string& message = player->getName() + " left the game"; |
---|
| 153 | COUT(0) << message << std::endl; |
---|
| 154 | Host::Broadcast(message); |
---|
[7579] | 155 | //Update: EachPlayer's "Players in Game"-HUD |
---|
| 156 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
| 157 | { |
---|
| 158 | if (it->first->getClientID() == CLIENTID_UNKNOWN) |
---|
| 159 | continue; |
---|
| 160 | const std::string& message1 = "Remaining Players: "+ multi_cast<std::string>(playersAlive); |
---|
| 161 | this->gtinfo_->sendStaticMessage(message1,it->first->getClientID(),ColourValue(1.0f, 1.0f, 0.5f)); |
---|
| 162 | } |
---|
[7480] | 163 | } |
---|
| 164 | |
---|
| 165 | return valid_player; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | bool LastManStanding::playerChangedName(PlayerInfo* player) |
---|
| 169 | { |
---|
| 170 | bool valid_player = Gametype::playerChangedName(player); |
---|
| 171 | |
---|
| 172 | if (valid_player) |
---|
| 173 | { |
---|
| 174 | const std::string& message = player->getOldName() + " changed name to " + player->getName(); |
---|
| 175 | COUT(0) << message << std::endl; |
---|
| 176 | Host::Broadcast(message); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | return valid_player; |
---|
| 180 | } |
---|
| 181 | |
---|
[7579] | 182 | void LastManStanding::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn) |
---|
| 183 | { |
---|
| 184 | if (!player) |
---|
| 185 | return; |
---|
| 186 | //Update: Individual Players "lifes"-HUD |
---|
| 187 | std::map<PlayerInfo*, Player>::iterator it2 = this->players_.find(player); |
---|
| 188 | if (it2 != this->players_.end()) |
---|
| 189 | { |
---|
| 190 | const std::string& message = "Your Lives: " +multi_cast<std::string>(playerLives_[player]); |
---|
| 191 | this->gtinfo_->sendFadingMessage(message,it2->first->getClientID()); |
---|
| 192 | |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | void LastManStanding::playerStopsControllingPawn(PlayerInfo* player, Pawn* pawn) |
---|
| 197 | { |
---|
| 198 | //Update: EachPlayer's "Players in Game"-HUD |
---|
| 199 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
| 200 | { |
---|
| 201 | if (it->first->getClientID() == CLIENTID_UNKNOWN) |
---|
| 202 | continue; |
---|
| 203 | const std::string& message1 = "Remaining Players : "+ multi_cast<std::string>(playersAlive); |
---|
| 204 | this->gtinfo_->sendStaticMessage(message1,it->first->getClientID(),ColourValue(1.0f, 1.0f, 0.5f)); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | } |
---|
| 208 | |
---|
[7480] | 209 | void LastManStanding::pawnKilled(Pawn* victim, Pawn* killer) |
---|
| 210 | { |
---|
| 211 | if (victim && victim->getPlayer()) |
---|
| 212 | { |
---|
| 213 | std::string message; |
---|
| 214 | if (killer) |
---|
| 215 | { |
---|
| 216 | if (killer->getPlayer()) |
---|
| 217 | message = victim->getPlayer()->getName() + " was killed by " + killer->getPlayer()->getName(); |
---|
| 218 | else |
---|
| 219 | message = victim->getPlayer()->getName() + " was killed"; |
---|
| 220 | } |
---|
| 221 | else |
---|
| 222 | message = victim->getPlayer()->getName() + " died"; |
---|
| 223 | |
---|
| 224 | COUT(0) << message << std::endl; |
---|
| 225 | Host::Broadcast(message); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | Gametype::pawnKilled(victim, killer); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | const int LastManStanding::playerGetLives(PlayerInfo* player) |
---|
| 232 | { |
---|
| 233 | if (player) |
---|
| 234 | return playerLives_[player]; |
---|
| 235 | else |
---|
| 236 | return 0; |
---|
| 237 | } |
---|
[7543] | 238 | /*BUG-Description: |
---|
| 239 | *There are two ways for a player to be killed: Either receiving damage, or through the following function. |
---|
| 240 | *The function works fine - until the last call, when a player looses his last live. Than the kill part |
---|
| 241 | *(it->second.state_ = PlayerState::Dead;) somehow isn't executed: |
---|
| 242 | *The player isn't killed (he doesn't leave play). Although the corresponding code is reached. |
---|
| 243 | * |
---|
| 244 | *How to reproduce this bug: Start a new Lastmanstanding-Match. Immdiately add 8 bots(before actually entering the level). |
---|
| 245 | *Just fly around and wait. Don't shoot, since only passive behaviour triggers the killPlayer-Function. |
---|
| 246 | */ |
---|
[7480] | 247 | void LastManStanding::killPlayer(PlayerInfo* player) |
---|
| 248 | { |
---|
| 249 | if(!player) |
---|
| 250 | return; |
---|
[7543] | 251 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player); |
---|
[7480] | 252 | if (it != this->players_.end()) |
---|
| 253 | { |
---|
[7543] | 254 | if (playerLives_[player]<=1)//if player lost all lives |
---|
[7480] | 255 | { |
---|
| 256 | this->playersAlive--; |
---|
[7542] | 257 | const std::string& message = player->getName() + " is out"; |
---|
[7480] | 258 | COUT(0) << message << std::endl; |
---|
| 259 | Host::Broadcast(message); |
---|
[7543] | 260 | playerLives_[player]=playerLives_[player]-1;//-----------datapart |
---|
| 261 | it->second.killed_++; |
---|
| 262 | it->second.state_ = PlayerState::Dead;//-------------killpart |
---|
[7480] | 263 | } |
---|
[7543] | 264 | else |
---|
| 265 | { |
---|
| 266 | playerLives_[player]=playerLives_[player]-1;//-----------datapart |
---|
| 267 | it->second.killed_++; |
---|
| 268 | it->second.state_ = PlayerState::Dead;//-------------killpart |
---|
| 269 | |
---|
| 270 | this->timeToAct_[player]=timeRemaining+3.0f;//reset timer |
---|
| 271 | } |
---|
[7579] | 272 | |
---|
[7480] | 273 | } |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | void LastManStanding::tick(float dt) |
---|
| 277 | { |
---|
| 278 | SUPER(LastManStanding, tick, dt); |
---|
| 279 | if(!this->hasEnded()) |
---|
| 280 | { |
---|
| 281 | if ((this->hasStarted()&&(playersAlive==1)))//last player remaining |
---|
| 282 | { |
---|
| 283 | this->end(); |
---|
| 284 | } |
---|
| 285 | for (std::map<PlayerInfo*, float>::iterator it = this->timeToAct_.begin(); it != this->timeToAct_.end(); ++it) |
---|
| 286 | { |
---|
| 287 | it->second-=dt; |
---|
| 288 | if (it->second<0.0f) |
---|
| 289 | { |
---|
[7542] | 290 | it->second=timeRemaining+3.0f;//reset timer |
---|
| 291 | if (playerGetLives(it->first)>0) |
---|
| 292 | this->killPlayer(it->first); |
---|
[7480] | 293 | } |
---|
| 294 | } |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | } |
---|