[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" |
---|
| 32 | #include "core/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 | { |
---|
| 42 | CreateUnloadableFactory(TeamGametype); |
---|
| 43 | |
---|
| 44 | TeamGametype::TeamGametype(BaseObject* creator) : Gametype(creator) |
---|
| 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 | |
---|
[8904] | 91 | void TeamGametype::findAndSetTeam(PlayerInfo* player) |
---|
| 92 | { |
---|
[8996] | 93 | if(player == NULL) return; // catch null pointers |
---|
| 94 | std::vector<unsigned int> playersperteam(this->teams_, 0); |
---|
[8901] | 95 | |
---|
| 96 | for (std::map<PlayerInfo*, int>::iterator it = this->teamnumbers_.begin(); it != this->teamnumbers_.end(); ++it) |
---|
| 97 | if (it->second < static_cast<int>(this->teams_) && it->second >= 0) |
---|
| 98 | playersperteam[it->second]++; |
---|
| 99 | |
---|
| 100 | unsigned int minplayers = static_cast<unsigned int>(-1); |
---|
| 101 | size_t minplayersteam = 0; |
---|
| 102 | for (size_t i = 0; i < this->teams_; ++i) |
---|
| 103 | { |
---|
| 104 | if (playersperteam[i] < minplayers) |
---|
| 105 | { |
---|
| 106 | minplayers = playersperteam[i]; |
---|
| 107 | minplayersteam = i; |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | this->teamnumbers_[player] = minplayersteam; |
---|
[8904] | 112 | |
---|
[8901] | 113 | } |
---|
| 114 | |
---|
| 115 | bool TeamGametype::playerLeft(PlayerInfo* player) |
---|
| 116 | { |
---|
| 117 | bool valid_player = Gametype::playerLeft(player); |
---|
[8956] | 118 | if( (this->players_.size() >= maxPlayers_) && (allowedInGame_[player] == true) ) // if there's a "waiting list" |
---|
| 119 | { |
---|
| 120 | for (std::map<PlayerInfo*, bool>::iterator it = this->allowedInGame_.begin(); it != this->allowedInGame_.end(); ++it) |
---|
| 121 | { |
---|
| 122 | if(it->second == false) // waiting player found |
---|
| 123 | {it->second = true; break;} // allow player to enter |
---|
[8996] | 124 | } |
---|
| 125 | } |
---|
[8901] | 126 | |
---|
| 127 | if (valid_player) |
---|
[8956] | 128 | { // clean up the maps |
---|
[8901] | 129 | this->teamnumbers_.erase(player); |
---|
[8956] | 130 | this->allowedInGame_.erase(player); |
---|
| 131 | } |
---|
[8901] | 132 | |
---|
[8956] | 133 | |
---|
[8901] | 134 | return valid_player; |
---|
| 135 | } |
---|
| 136 | |
---|
[8930] | 137 | void TeamGametype::spawnDeadPlayersIfRequested() |
---|
| 138 | { |
---|
[8941] | 139 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)\ |
---|
[8930] | 140 | { |
---|
[8956] | 141 | if(allowedInGame_[it->first] == false)//check if dead player is allowed to enter |
---|
| 142 | { |
---|
[8996] | 143 | continue; |
---|
[8956] | 144 | } |
---|
[8941] | 145 | if (it->second.state_ == PlayerState::Dead) |
---|
[8930] | 146 | { |
---|
[8941] | 147 | if ((it->first->isReadyToSpawn() || this->bForceSpawn_)) |
---|
| 148 | { |
---|
| 149 | this->spawnPlayer(it->first); |
---|
| 150 | } |
---|
[8930] | 151 | } |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | |
---|
[8901] | 156 | bool TeamGametype::allowPawnHit(Pawn* victim, Pawn* originator) |
---|
[8956] | 157 | {// hit allowed: if victim & originator are foes or if originator doesnot exist or if friendlyfire is allowed |
---|
[8923] | 158 | return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator || this->allowFriendlyFire_); |
---|
[8901] | 159 | } |
---|
| 160 | |
---|
| 161 | bool TeamGametype::allowPawnDamage(Pawn* victim, Pawn* originator) |
---|
| 162 | { |
---|
[8923] | 163 | return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator || this->allowFriendlyFire_); |
---|
[8901] | 164 | } |
---|
| 165 | |
---|
| 166 | bool TeamGametype::allowPawnDeath(Pawn* victim, Pawn* originator) |
---|
| 167 | { |
---|
[8923] | 168 | return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator || this->allowFriendlyFire_); |
---|
[8901] | 169 | } |
---|
| 170 | |
---|
| 171 | SpawnPoint* TeamGametype::getBestSpawnPoint(PlayerInfo* player) const |
---|
| 172 | { |
---|
| 173 | int desiredTeamNr = -1; |
---|
| 174 | std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player); |
---|
| 175 | if (it_player != this->teamnumbers_.end()) |
---|
| 176 | desiredTeamNr = it_player->second; |
---|
| 177 | |
---|
| 178 | // Only use spawnpoints of the own team (or non-team-spawnpoints) |
---|
| 179 | std::set<SpawnPoint*> teamSpawnPoints = this->spawnpoints_; |
---|
| 180 | for (std::set<SpawnPoint*>::iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ) |
---|
| 181 | { |
---|
| 182 | if ((*it)->isA(Class(TeamSpawnPoint))) |
---|
| 183 | { |
---|
| 184 | TeamSpawnPoint* tsp = orxonox_cast<TeamSpawnPoint*>(*it); |
---|
| 185 | if (tsp && static_cast<int>(tsp->getTeamNumber()) != desiredTeamNr) |
---|
| 186 | { |
---|
| 187 | teamSpawnPoints.erase(it++); |
---|
| 188 | continue; |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | ++it; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | SpawnPoint* fallbackSpawnPoint = NULL; |
---|
| 196 | if (teamSpawnPoints.size() > 0) |
---|
| 197 | { |
---|
| 198 | unsigned int randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(teamSpawnPoints.size()))); |
---|
| 199 | unsigned int index = 0; |
---|
| 200 | // Get random fallback spawnpoint in case there is no active SpawnPoint. |
---|
| 201 | for (std::set<SpawnPoint*>::const_iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ++it) |
---|
| 202 | { |
---|
| 203 | if (index == randomspawn) |
---|
| 204 | { |
---|
| 205 | fallbackSpawnPoint = (*it); |
---|
| 206 | break; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | ++index; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | // Remove all inactive SpawnPoints from the list. |
---|
| 213 | for (std::set<SpawnPoint*>::iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ) |
---|
| 214 | { |
---|
| 215 | if(!(*it)->isActive()) |
---|
| 216 | { |
---|
| 217 | teamSpawnPoints.erase(it++); |
---|
| 218 | continue; |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | ++it; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(teamSpawnPoints.size()))); |
---|
| 225 | index = 0; |
---|
| 226 | for (std::set<SpawnPoint*>::const_iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ++it) |
---|
| 227 | { |
---|
| 228 | if (index == randomspawn) |
---|
| 229 | return (*it); |
---|
| 230 | |
---|
| 231 | ++index; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | return fallbackSpawnPoint; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | return 0; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | void TeamGametype::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn) |
---|
| 241 | { |
---|
| 242 | if (!player) |
---|
| 243 | return; |
---|
| 244 | |
---|
[8941] | 245 | this->setTeamColour(player,pawn); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | bool TeamGametype::pawnsAreInTheSameTeam(Pawn* pawn1, Pawn* pawn2) |
---|
| 249 | { |
---|
| 250 | if (pawn1 && pawn2) |
---|
| 251 | { |
---|
| 252 | std::map<PlayerInfo*, int>::const_iterator it1 = this->teamnumbers_.find(pawn1->getPlayer()); |
---|
| 253 | std::map<PlayerInfo*, int>::const_iterator it2 = this->teamnumbers_.find(pawn2->getPlayer()); |
---|
| 254 | |
---|
| 255 | if (it1 != this->teamnumbers_.end() && it2 != this->teamnumbers_.end()) |
---|
| 256 | return (it1->second == it2->second); |
---|
| 257 | } |
---|
| 258 | return false; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | int TeamGametype::getTeam(PlayerInfo* player) |
---|
| 262 | { |
---|
| 263 | std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player); |
---|
| 264 | if (it_player != this->teamnumbers_.end()) |
---|
| 265 | return it_player->second; |
---|
| 266 | else |
---|
| 267 | return -1; |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | void TeamGametype::setTeamColour(PlayerInfo* player, Pawn* pawn) |
---|
| 271 | { |
---|
[8901] | 272 | // Set the team colour |
---|
| 273 | std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player); |
---|
| 274 | if (it_player != this->teamnumbers_.end() && it_player->second >= 0 && it_player->second < static_cast<int>(this->teamcolours_.size())) |
---|
| 275 | { |
---|
[8942] | 276 | this->colourPawn(pawn, it_player->second); |
---|
[8901] | 277 | } |
---|
| 278 | } |
---|
| 279 | |
---|
[8997] | 280 | /** |
---|
| 281 | @brief |
---|
| 282 | Colours a pawn depending on the team values set in XML. |
---|
| 283 | A pawn is coloured depending on it's team set via XML. |
---|
| 284 | If there is a controller the pawn is coloured depending on its team which also can be set via XML. |
---|
| 285 | */ |
---|
[8942] | 286 | void TeamGametype::setDefaultObjectColour(Pawn* pawn) |
---|
[8901] | 287 | { |
---|
[8941] | 288 | if(pawn == NULL) |
---|
| 289 | return; |
---|
[8956] | 290 | |
---|
[8942] | 291 | int teamnumber = pawn->getTeam(); |
---|
| 292 | |
---|
| 293 | if(teamnumber >= 0) |
---|
| 294 | { |
---|
| 295 | this->colourPawn(pawn, teamnumber); return; |
---|
| 296 | } |
---|
[8941] | 297 | //get Pawn's controller |
---|
[8942] | 298 | ControllableEntity* entity = orxonox_cast<ControllableEntity*>(pawn); |
---|
| 299 | |
---|
| 300 | Controller* controller = 0; |
---|
| 301 | if (entity->getController()) |
---|
| 302 | controller = entity->getController(); |
---|
| 303 | else if (entity->getXMLController()) |
---|
| 304 | controller = entity->getXMLController(); |
---|
| 305 | else |
---|
[8941] | 306 | return; |
---|
[8942] | 307 | |
---|
| 308 | ArtificialController* artificial = orxonox_cast<ArtificialController*>(controller); |
---|
[8941] | 309 | //get Teamnumber - get the data |
---|
[8942] | 310 | if(artificial == NULL) |
---|
[8941] | 311 | return; |
---|
[8942] | 312 | teamnumber= artificial->getTeam(); |
---|
| 313 | |
---|
[8941] | 314 | //set ObjectColour |
---|
[8942] | 315 | this->colourPawn(pawn, teamnumber); |
---|
| 316 | } |
---|
[8941] | 317 | |
---|
[8942] | 318 | void TeamGametype::colourPawn(Pawn* pawn, int teamNr) |
---|
[8997] | 319 | {// catch: no-colouring-case and wrong input |
---|
[8995] | 320 | if(teamNr < 0 || teamNr+1 > this->teamcolours_.size() ||pawn == NULL) return; |
---|
[8942] | 321 | pawn->setRadarObjectColour(this->teamcolours_[teamNr]); |
---|
| 322 | |
---|
[8941] | 323 | std::set<WorldEntity*> pawnAttachments = pawn->getAttachedObjects(); |
---|
| 324 | for (std::set<WorldEntity*>::iterator it = pawnAttachments.begin(); it != pawnAttachments.end(); ++it) |
---|
[8901] | 325 | { |
---|
[8941] | 326 | if ((*it)->isA(Class(TeamColourable))) |
---|
| 327 | { |
---|
| 328 | TeamColourable* tc = orxonox_cast<TeamColourable*>(*it); |
---|
[8942] | 329 | tc->setTeamColour(this->teamcolours_[teamNr]); |
---|
[8941] | 330 | } |
---|
| 331 | } |
---|
[8901] | 332 | } |
---|
| 333 | |
---|
| 334 | } |
---|