[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 | |
---|
[7583] | 42 | LastManStanding::LastManStanding(BaseObject* creator) : Deathmatch(creator) |
---|
[7480] | 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 | return true; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | bool LastManStanding::allowPawnDeath(Pawn* victim, Pawn* originator) |
---|
| 80 | { |
---|
[7542] | 81 | if (!victim||!victim->getPlayer())// only for safety |
---|
[7480] | 82 | return true; |
---|
[7542] | 83 | playerLives_[victim->getPlayer()]=playerLives_[victim->getPlayer()]-1; |
---|
| 84 | if (playerLives_[victim->getPlayer()]<=0)//if player lost all lives |
---|
[7480] | 85 | { |
---|
| 86 | this->playersAlive--; |
---|
| 87 | const std::string& message = victim->getPlayer()->getName() + " has lost all lives"; |
---|
| 88 | COUT(0) << message << std::endl; |
---|
| 89 | Host::Broadcast(message); |
---|
| 90 | } |
---|
| 91 | return true; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | void LastManStanding::end() |
---|
| 95 | { |
---|
| 96 | Gametype::end(); |
---|
| 97 | |
---|
| 98 | for (std::map<PlayerInfo*, int>::iterator it = this->playerLives_.begin(); it != this->playerLives_.end(); ++it) |
---|
| 99 | { |
---|
| 100 | if (it->first->getClientID() == CLIENTID_UNKNOWN) |
---|
| 101 | continue; |
---|
| 102 | |
---|
| 103 | if (it->second > 0) |
---|
| 104 | this->gtinfo_->sendAnnounceMessage("You have won the match!", it->first->getClientID()); |
---|
| 105 | else |
---|
| 106 | this->gtinfo_->sendAnnounceMessage("You have lost the match!", it->first->getClientID()); |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void LastManStanding::playerEntered(PlayerInfo* player) |
---|
| 111 | { |
---|
| 112 | if (!player)// only for safety |
---|
| 113 | return; |
---|
[7583] | 114 | Deathmatch::playerEntered(player); |
---|
[7480] | 115 | |
---|
| 116 | playerLives_[player]=lives; |
---|
| 117 | this->playersAlive++; |
---|
| 118 | this->timeToAct_[player]=timeRemaining; |
---|
[7579] | 119 | //Update: EachPlayer's "Players in Game"-HUD |
---|
| 120 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
| 121 | { |
---|
| 122 | if (it->first->getClientID() == CLIENTID_UNKNOWN) |
---|
| 123 | continue; |
---|
| 124 | const std::string& message1 = "Remaining Players: "+ multi_cast<std::string>(playersAlive); |
---|
| 125 | this->gtinfo_->sendStaticMessage(message1,it->first->getClientID(),ColourValue(1.0f, 1.0f, 0.5f)); |
---|
| 126 | } |
---|
[7480] | 127 | } |
---|
| 128 | |
---|
| 129 | bool LastManStanding::playerLeft(PlayerInfo* player) |
---|
| 130 | { |
---|
[7583] | 131 | bool valid_player = Deathmatch::playerLeft(player); |
---|
[7480] | 132 | if (valid_player) |
---|
| 133 | { |
---|
| 134 | this->playersAlive--; |
---|
| 135 | //this->playerLives_[player].erase (player); not necessary? |
---|
[7579] | 136 | //Update: EachPlayer's "Players in Game"-HUD |
---|
| 137 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
| 138 | { |
---|
| 139 | if (it->first->getClientID() == CLIENTID_UNKNOWN) |
---|
| 140 | continue; |
---|
| 141 | const std::string& message1 = "Remaining Players: "+ multi_cast<std::string>(playersAlive); |
---|
| 142 | this->gtinfo_->sendStaticMessage(message1,it->first->getClientID(),ColourValue(1.0f, 1.0f, 0.5f)); |
---|
| 143 | } |
---|
[7480] | 144 | } |
---|
| 145 | |
---|
| 146 | return valid_player; |
---|
| 147 | } |
---|
| 148 | |
---|
[7579] | 149 | void LastManStanding::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn) |
---|
| 150 | { |
---|
| 151 | if (!player) |
---|
| 152 | return; |
---|
[7585] | 153 | this->timeToAct_[player]=timeRemaining+3.0f;//reset timer |
---|
[7579] | 154 | //Update: Individual Players "lifes"-HUD |
---|
| 155 | std::map<PlayerInfo*, Player>::iterator it2 = this->players_.find(player); |
---|
| 156 | if (it2 != this->players_.end()) |
---|
| 157 | { |
---|
| 158 | const std::string& message = "Your Lives: " +multi_cast<std::string>(playerLives_[player]); |
---|
| 159 | this->gtinfo_->sendFadingMessage(message,it2->first->getClientID()); |
---|
[7586] | 160 | const std::string& message1 = "Remaining Players: "+ multi_cast<std::string>(playersAlive); |
---|
| 161 | this->gtinfo_->sendStaticMessage(message1,it2->first->getClientID(),ColourValue(1.0f, 1.0f, 0.5f)); |
---|
[7579] | 162 | } |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | void LastManStanding::playerStopsControllingPawn(PlayerInfo* player, Pawn* pawn) |
---|
| 166 | { |
---|
| 167 | //Update: EachPlayer's "Players in Game"-HUD |
---|
| 168 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
| 169 | { |
---|
| 170 | if (it->first->getClientID() == CLIENTID_UNKNOWN) |
---|
| 171 | continue; |
---|
| 172 | const std::string& message1 = "Remaining Players : "+ multi_cast<std::string>(playersAlive); |
---|
| 173 | this->gtinfo_->sendStaticMessage(message1,it->first->getClientID(),ColourValue(1.0f, 1.0f, 0.5f)); |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | |
---|
[7480] | 177 | const int LastManStanding::playerGetLives(PlayerInfo* player) |
---|
| 178 | { |
---|
| 179 | if (player) |
---|
| 180 | return playerLives_[player]; |
---|
| 181 | else |
---|
| 182 | return 0; |
---|
| 183 | } |
---|
[7581] | 184 | |
---|
[7480] | 185 | void LastManStanding::killPlayer(PlayerInfo* player) |
---|
| 186 | { |
---|
| 187 | if(!player) |
---|
| 188 | return; |
---|
[7543] | 189 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player); |
---|
[7480] | 190 | if (it != this->players_.end()) |
---|
| 191 | { |
---|
[7581] | 192 | if(!player->getControllableEntity()) |
---|
| 193 | {return;} |
---|
| 194 | Pawn* pawn = dynamic_cast<Pawn*>(player->getControllableEntity()); |
---|
| 195 | if(!pawn) |
---|
| 196 | {return;} |
---|
| 197 | pawn->kill(); |
---|
| 198 | this->timeToAct_[player]=timeRemaining+3.0f;//reset timer |
---|
[7480] | 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | void LastManStanding::tick(float dt) |
---|
| 203 | { |
---|
| 204 | SUPER(LastManStanding, tick, dt); |
---|
[7581] | 205 | if(this->hasStarted()&&(!this->hasEnded())) |
---|
[7480] | 206 | { |
---|
[7581] | 207 | if ((this->hasStarted()&&(playersAlive<=1)))//last player remaining |
---|
[7480] | 208 | { |
---|
| 209 | this->end(); |
---|
| 210 | } |
---|
| 211 | for (std::map<PlayerInfo*, float>::iterator it = this->timeToAct_.begin(); it != this->timeToAct_.end(); ++it) |
---|
| 212 | { |
---|
| 213 | it->second-=dt; |
---|
[7585] | 214 | if (it->second<timeRemaining/6)//Warning message |
---|
| 215 | { |
---|
| 216 | const std::string& message = "Camper Warning! Don't forget to shoot."; |
---|
| 217 | this->gtinfo_->sendFadingMessage(message,it->first->getClientID()); |
---|
| 218 | } |
---|
[7480] | 219 | if (it->second<0.0f) |
---|
| 220 | { |
---|
[7542] | 221 | it->second=timeRemaining+3.0f;//reset timer |
---|
| 222 | if (playerGetLives(it->first)>0) |
---|
| 223 | this->killPlayer(it->first); |
---|
[7480] | 224 | } |
---|
| 225 | } |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | } |
---|