[8901] | 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 "TeamGametype.h" |
---|
| 30 | |
---|
| 31 | #include "core/CoreIncludes.h" |
---|
[9667] | 32 | #include "core/config/ConfigValueIncludes.h" |
---|
[8930] | 33 | #include "infos/PlayerInfo.h" |
---|
[8901] | 34 | #include "interfaces/TeamColourable.h" |
---|
| 35 | #include "worldentities/TeamSpawnPoint.h" |
---|
| 36 | #include "worldentities/pawns/Pawn.h" |
---|
[8942] | 37 | #include "worldentities/ControllableEntity.h" |
---|
[8941] | 38 | #include "controllers/ArtificialController.h" |
---|
[8901] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
[9667] | 42 | RegisterUnloadableClass(TeamGametype); |
---|
[8901] | 43 | |
---|
[9667] | 44 | TeamGametype::TeamGametype(Context* context) : Gametype(context) |
---|
[8901] | 45 | { |
---|
| 46 | RegisterObject(TeamGametype); |
---|
| 47 | |
---|
[8956] | 48 | this->teams_ = 2; //most team gametypes use two different teams |
---|
[8923] | 49 | this->allowFriendlyFire_ = false; |
---|
[8930] | 50 | //this->playersPerTeam_ = 0; |
---|
[8996] | 51 | this->maxPlayers_ = 0; //Value "0": no limit is set. |
---|
[8901] | 52 | this->setConfigValues(); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | void TeamGametype::setConfigValues() |
---|
| 56 | { |
---|
| 57 | SetConfigValue(teams_, 2); |
---|
| 58 | |
---|
| 59 | static ColourValue colours[] = |
---|
| 60 | { |
---|
[8951] | 61 | ColourValue(0.2f, 0.2f, 1.0f), |
---|
| 62 | ColourValue(1.0f, 0.1f, 0.1f), |
---|
[8901] | 63 | ColourValue(0.3f, 1.0f, 0.3f), |
---|
[8951] | 64 | ColourValue(1.0f, 1.0f, 0.0f), |
---|
| 65 | ColourValue(0.0f, 1.0f, 1.0f), |
---|
| 66 | ColourValue(1.0f, 0.0f, 1.0f), |
---|
| 67 | ColourValue(7.0f, 7.0f, 7.0f), |
---|
| 68 | ColourValue(2.0f, 2.0f, 2.0f) |
---|
[8901] | 69 | }; |
---|
| 70 | static std::vector<ColourValue> defaultcolours(colours, colours + sizeof(colours) / sizeof(ColourValue)); |
---|
| 71 | |
---|
| 72 | SetConfigValue(teamcolours_, defaultcolours); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | void TeamGametype::playerEntered(PlayerInfo* player) |
---|
| 76 | { |
---|
[8996] | 77 | if(player == NULL) return; // catch null pointers |
---|
[8901] | 78 | Gametype::playerEntered(player); |
---|
[8904] | 79 | this->findAndSetTeam(player); |
---|
[8956] | 80 | if( this->players_.size() <= maxPlayers_ || maxPlayers_ == 0) |
---|
| 81 | { |
---|
[8941] | 82 | this->allowedInGame_[player]= true; |
---|
[8956] | 83 | } |
---|
[8941] | 84 | else |
---|
[8956] | 85 | { |
---|
[8941] | 86 | this->allowedInGame_[player]= false; |
---|
[8956] | 87 | orxout() << "not allowed in game: players = " << this->players_.size() << " > maximum: " << maxPlayers_ << endl; |
---|
| 88 | } |
---|
[8904] | 89 | } |
---|
[8901] | 90 | |
---|
[9348] | 91 | /** |
---|
| 92 | @brief |
---|
| 93 | Function that determines the player's team number when entering the game for the first time. |
---|
| 94 | Override this function for other team structures. |
---|
| 95 | */ |
---|
[8904] | 96 | void TeamGametype::findAndSetTeam(PlayerInfo* player) |
---|
| 97 | { |
---|
[8996] | 98 | if(player == NULL) return; // catch null pointers |
---|
| 99 | std::vector<unsigned int> playersperteam(this->teams_, 0); |
---|
[8901] | 100 | |
---|
| 101 | for (std::map<PlayerInfo*, int>::iterator it = this->teamnumbers_.begin(); it != this->teamnumbers_.end(); ++it) |
---|
| 102 | if (it->second < static_cast<int>(this->teams_) && it->second >= 0) |
---|
| 103 | playersperteam[it->second]++; |
---|
| 104 | |
---|
| 105 | unsigned int minplayers = static_cast<unsigned int>(-1); |
---|
| 106 | size_t minplayersteam = 0; |
---|
| 107 | for (size_t i = 0; i < this->teams_; ++i) |
---|
| 108 | { |
---|
| 109 | if (playersperteam[i] < minplayers) |
---|
| 110 | { |
---|
| 111 | minplayers = playersperteam[i]; |
---|
| 112 | minplayersteam = i; |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | this->teamnumbers_[player] = minplayersteam; |
---|
[8904] | 117 | |
---|
[8901] | 118 | } |
---|
| 119 | |
---|
| 120 | bool TeamGametype::playerLeft(PlayerInfo* player) |
---|
| 121 | { |
---|
| 122 | bool valid_player = Gametype::playerLeft(player); |
---|
[8956] | 123 | if( (this->players_.size() >= maxPlayers_) && (allowedInGame_[player] == true) ) // if there's a "waiting list" |
---|
| 124 | { |
---|
| 125 | for (std::map<PlayerInfo*, bool>::iterator it = this->allowedInGame_.begin(); it != this->allowedInGame_.end(); ++it) |
---|
| 126 | { |
---|
| 127 | if(it->second == false) // waiting player found |
---|
| 128 | {it->second = true; break;} // allow player to enter |
---|
[8996] | 129 | } |
---|
| 130 | } |
---|
[8901] | 131 | |
---|
| 132 | if (valid_player) |
---|
[8956] | 133 | { // clean up the maps |
---|
[8901] | 134 | this->teamnumbers_.erase(player); |
---|
[8956] | 135 | this->allowedInGame_.erase(player); |
---|
| 136 | } |
---|
[8901] | 137 | |
---|
| 138 | return valid_player; |
---|
| 139 | } |
---|
| 140 | |
---|
[8930] | 141 | void TeamGametype::spawnDeadPlayersIfRequested() |
---|
| 142 | { |
---|
[8941] | 143 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)\ |
---|
[8930] | 144 | { |
---|
[8956] | 145 | if(allowedInGame_[it->first] == false)//check if dead player is allowed to enter |
---|
| 146 | { |
---|
[8996] | 147 | continue; |
---|
[8956] | 148 | } |
---|
[8941] | 149 | if (it->second.state_ == PlayerState::Dead) |
---|
[8930] | 150 | { |
---|
[8941] | 151 | if ((it->first->isReadyToSpawn() || this->bForceSpawn_)) |
---|
| 152 | { |
---|
| 153 | this->spawnPlayer(it->first); |
---|
| 154 | } |
---|
[8930] | 155 | } |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | |
---|
[8901] | 160 | bool TeamGametype::allowPawnHit(Pawn* victim, Pawn* originator) |
---|
[8956] | 161 | {// hit allowed: if victim & originator are foes or if originator doesnot exist or if friendlyfire is allowed |
---|
[8923] | 162 | return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator || this->allowFriendlyFire_); |
---|
[8901] | 163 | } |
---|
| 164 | |
---|
| 165 | bool TeamGametype::allowPawnDamage(Pawn* victim, Pawn* originator) |
---|
| 166 | { |
---|
[8923] | 167 | return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator || this->allowFriendlyFire_); |
---|
[8901] | 168 | } |
---|
| 169 | |
---|
| 170 | bool TeamGametype::allowPawnDeath(Pawn* victim, Pawn* originator) |
---|
| 171 | { |
---|
[8923] | 172 | return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator || this->allowFriendlyFire_); |
---|
[8901] | 173 | } |
---|
| 174 | |
---|
[9941] | 175 | int TeamGametype::getTeamScore(PlayerInfo* player) |
---|
| 176 | { |
---|
| 177 | int teamscore = 0; |
---|
| 178 | if(!player || this->getTeam(player) == -1) |
---|
| 179 | return 0; |
---|
| 180 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
| 181 | { |
---|
| 182 | if ( this->getTeam(it->first) == this->getTeam(player) ) |
---|
| 183 | { |
---|
| 184 | teamscore += it->second.frags_; |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | return teamscore; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | int TeamGametype::getTeamSize(int team) |
---|
| 191 | { |
---|
| 192 | int teamSize = 0; |
---|
| 193 | for (std::map<PlayerInfo*, int>::iterator it = this->teamnumbers_.begin(); it != this->teamnumbers_.end(); ++it) |
---|
| 194 | { |
---|
| 195 | if (it->second == team) |
---|
| 196 | teamSize++; |
---|
| 197 | } |
---|
| 198 | return teamSize; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | int TeamGametype::getHumansInTeam(int team) |
---|
| 202 | { |
---|
| 203 | int teamSize = 0; |
---|
| 204 | for (std::map<PlayerInfo*, int>::iterator it = this->teamnumbers_.begin(); it != this->teamnumbers_.end(); ++it) |
---|
| 205 | { |
---|
| 206 | if (it->second == team && it->first->isHumanPlayer()) |
---|
| 207 | teamSize++; |
---|
| 208 | } |
---|
| 209 | return teamSize; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | |
---|
[8901] | 213 | SpawnPoint* TeamGametype::getBestSpawnPoint(PlayerInfo* player) const |
---|
| 214 | { |
---|
| 215 | int desiredTeamNr = -1; |
---|
| 216 | std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player); |
---|
| 217 | if (it_player != this->teamnumbers_.end()) |
---|
| 218 | desiredTeamNr = it_player->second; |
---|
| 219 | |
---|
| 220 | // Only use spawnpoints of the own team (or non-team-spawnpoints) |
---|
| 221 | std::set<SpawnPoint*> teamSpawnPoints = this->spawnpoints_; |
---|
| 222 | for (std::set<SpawnPoint*>::iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ) |
---|
| 223 | { |
---|
| 224 | if ((*it)->isA(Class(TeamSpawnPoint))) |
---|
| 225 | { |
---|
| 226 | TeamSpawnPoint* tsp = orxonox_cast<TeamSpawnPoint*>(*it); |
---|
| 227 | if (tsp && static_cast<int>(tsp->getTeamNumber()) != desiredTeamNr) |
---|
| 228 | { |
---|
| 229 | teamSpawnPoints.erase(it++); |
---|
| 230 | continue; |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | ++it; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | SpawnPoint* fallbackSpawnPoint = NULL; |
---|
| 238 | if (teamSpawnPoints.size() > 0) |
---|
| 239 | { |
---|
| 240 | unsigned int randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(teamSpawnPoints.size()))); |
---|
| 241 | unsigned int index = 0; |
---|
| 242 | // Get random fallback spawnpoint in case there is no active SpawnPoint. |
---|
| 243 | for (std::set<SpawnPoint*>::const_iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ++it) |
---|
| 244 | { |
---|
| 245 | if (index == randomspawn) |
---|
| 246 | { |
---|
| 247 | fallbackSpawnPoint = (*it); |
---|
| 248 | break; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | ++index; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | // Remove all inactive SpawnPoints from the list. |
---|
| 255 | for (std::set<SpawnPoint*>::iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ) |
---|
| 256 | { |
---|
| 257 | if(!(*it)->isActive()) |
---|
| 258 | { |
---|
| 259 | teamSpawnPoints.erase(it++); |
---|
| 260 | continue; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | ++it; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(teamSpawnPoints.size()))); |
---|
| 267 | index = 0; |
---|
| 268 | for (std::set<SpawnPoint*>::const_iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ++it) |
---|
| 269 | { |
---|
| 270 | if (index == randomspawn) |
---|
| 271 | return (*it); |
---|
| 272 | |
---|
| 273 | ++index; |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | return fallbackSpawnPoint; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | return 0; |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | void TeamGametype::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn) |
---|
| 283 | { |
---|
| 284 | if (!player) |
---|
| 285 | return; |
---|
| 286 | |
---|
[8941] | 287 | this->setTeamColour(player,pawn); |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | bool TeamGametype::pawnsAreInTheSameTeam(Pawn* pawn1, Pawn* pawn2) |
---|
| 291 | { |
---|
| 292 | if (pawn1 && pawn2) |
---|
| 293 | { |
---|
| 294 | std::map<PlayerInfo*, int>::const_iterator it1 = this->teamnumbers_.find(pawn1->getPlayer()); |
---|
| 295 | std::map<PlayerInfo*, int>::const_iterator it2 = this->teamnumbers_.find(pawn2->getPlayer()); |
---|
| 296 | |
---|
| 297 | if (it1 != this->teamnumbers_.end() && it2 != this->teamnumbers_.end()) |
---|
| 298 | return (it1->second == it2->second); |
---|
| 299 | } |
---|
| 300 | return false; |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | int TeamGametype::getTeam(PlayerInfo* player) |
---|
| 304 | { |
---|
| 305 | std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player); |
---|
| 306 | if (it_player != this->teamnumbers_.end()) |
---|
| 307 | return it_player->second; |
---|
| 308 | else |
---|
| 309 | return -1; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | void TeamGametype::setTeamColour(PlayerInfo* player, Pawn* pawn) |
---|
| 313 | { |
---|
[8901] | 314 | // Set the team colour |
---|
| 315 | std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player); |
---|
| 316 | if (it_player != this->teamnumbers_.end() && it_player->second >= 0 && it_player->second < static_cast<int>(this->teamcolours_.size())) |
---|
| 317 | { |
---|
[8942] | 318 | this->colourPawn(pawn, it_player->second); |
---|
[8901] | 319 | } |
---|
| 320 | } |
---|
| 321 | |
---|
[8997] | 322 | /** |
---|
| 323 | @brief |
---|
| 324 | Colours a pawn depending on the team values set in XML. |
---|
| 325 | A pawn is coloured depending on it's team set via XML. |
---|
| 326 | If there is a controller the pawn is coloured depending on its team which also can be set via XML. |
---|
| 327 | */ |
---|
[8942] | 328 | void TeamGametype::setDefaultObjectColour(Pawn* pawn) |
---|
[8901] | 329 | { |
---|
[8941] | 330 | if(pawn == NULL) |
---|
| 331 | return; |
---|
[8956] | 332 | |
---|
[8942] | 333 | int teamnumber = pawn->getTeam(); |
---|
| 334 | |
---|
| 335 | if(teamnumber >= 0) |
---|
| 336 | { |
---|
| 337 | this->colourPawn(pawn, teamnumber); return; |
---|
| 338 | } |
---|
[8941] | 339 | //get Pawn's controller |
---|
[8942] | 340 | ControllableEntity* entity = orxonox_cast<ControllableEntity*>(pawn); |
---|
| 341 | |
---|
| 342 | Controller* controller = 0; |
---|
| 343 | if (entity->getController()) |
---|
| 344 | controller = entity->getController(); |
---|
| 345 | else if (entity->getXMLController()) |
---|
| 346 | controller = entity->getXMLController(); |
---|
| 347 | else |
---|
[8941] | 348 | return; |
---|
[8942] | 349 | |
---|
| 350 | ArtificialController* artificial = orxonox_cast<ArtificialController*>(controller); |
---|
[8941] | 351 | //get Teamnumber - get the data |
---|
[8942] | 352 | if(artificial == NULL) |
---|
[8941] | 353 | return; |
---|
[8942] | 354 | teamnumber= artificial->getTeam(); |
---|
| 355 | |
---|
[8941] | 356 | //set ObjectColour |
---|
[8942] | 357 | this->colourPawn(pawn, teamnumber); |
---|
| 358 | } |
---|
[8941] | 359 | |
---|
[8942] | 360 | void TeamGametype::colourPawn(Pawn* pawn, int teamNr) |
---|
[8997] | 361 | {// catch: no-colouring-case and wrong input |
---|
[9048] | 362 | if(teamNr < 0 || teamNr+1 > int(this->teamcolours_.size()) ||pawn == NULL) return; |
---|
[8942] | 363 | pawn->setRadarObjectColour(this->teamcolours_[teamNr]); |
---|
| 364 | |
---|
[8941] | 365 | std::set<WorldEntity*> pawnAttachments = pawn->getAttachedObjects(); |
---|
| 366 | for (std::set<WorldEntity*>::iterator it = pawnAttachments.begin(); it != pawnAttachments.end(); ++it) |
---|
[8901] | 367 | { |
---|
[8941] | 368 | if ((*it)->isA(Class(TeamColourable))) |
---|
| 369 | { |
---|
| 370 | TeamColourable* tc = orxonox_cast<TeamColourable*>(*it); |
---|
[8942] | 371 | tc->setTeamColour(this->teamcolours_[teamNr]); |
---|
[8941] | 372 | } |
---|
| 373 | } |
---|
[8901] | 374 | } |
---|
| 375 | |
---|
[9941] | 376 | void TeamGametype::announceTeamWin(int winnerTeam) |
---|
| 377 | { |
---|
| 378 | for (std::map<PlayerInfo*, int>::iterator it3 = this->teamnumbers_.begin(); it3 != this->teamnumbers_.end(); ++it3) |
---|
| 379 | { |
---|
| 380 | if (it3->first->getClientID() == NETWORK_PEER_ID_UNKNOWN) |
---|
| 381 | continue; |
---|
| 382 | if (it3->second == winnerTeam) |
---|
| 383 | { |
---|
| 384 | this->gtinfo_->sendAnnounceMessage("Your team has won the match!", it3->first->getClientID()); |
---|
| 385 | } |
---|
| 386 | else |
---|
| 387 | { |
---|
| 388 | this->gtinfo_->sendAnnounceMessage("Your team has lost the match!", it3->first->getClientID()); |
---|
| 389 | } |
---|
| 390 | } |
---|
| 391 | } |
---|
| 392 | |
---|
[8901] | 393 | } |
---|