[2072] | 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 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "Gametype.h" |
---|
| 30 | |
---|
[3196] | 31 | #include "util/Math.h" |
---|
[2072] | 32 | #include "core/CoreIncludes.h" |
---|
[2662] | 33 | #include "core/ConfigValueIncludes.h" |
---|
[2896] | 34 | #include "core/GameMode.h" |
---|
[3196] | 35 | |
---|
[5735] | 36 | #include "infos/PlayerInfo.h" |
---|
| 37 | #include "infos/Bot.h" |
---|
[5737] | 38 | #include "graphics/Camera.h" |
---|
[5735] | 39 | #include "worldentities/ControllableEntity.h" |
---|
| 40 | #include "worldentities/SpawnPoint.h" |
---|
| 41 | #include "worldentities/pawns/Spectator.h" |
---|
| 42 | #include "worldentities/pawns/Pawn.h" |
---|
[3196] | 43 | #include "overlays/OverlayGroup.h" |
---|
[2072] | 44 | |
---|
| 45 | namespace orxonox |
---|
| 46 | { |
---|
| 47 | CreateUnloadableFactory(Gametype); |
---|
| 48 | |
---|
[5806] | 49 | Gametype::Gametype(BaseObject* creator) : BaseObject(creator) |
---|
[2072] | 50 | { |
---|
| 51 | RegisterObject(Gametype); |
---|
[5806] | 52 | |
---|
| 53 | this->gtinfo_ = new GametypeInfo(creator); |
---|
[2072] | 54 | |
---|
[5806] | 55 | this->setGametype(SmartPtr<Gametype>(this, false)); |
---|
[2662] | 56 | |
---|
[2072] | 57 | this->defaultControllableEntity_ = Class(Spectator); |
---|
| 58 | |
---|
| 59 | this->bAutoStart_ = false; |
---|
| 60 | this->bForceSpawn_ = false; |
---|
[2662] | 61 | this->numberOfBots_ = 0; |
---|
[2072] | 62 | |
---|
[3033] | 63 | this->timeLimit_ = 0; |
---|
| 64 | this->time_ = 0; |
---|
| 65 | this->timerIsActive_ = false; |
---|
| 66 | |
---|
[2072] | 67 | this->initialStartCountdown_ = 3; |
---|
[2662] | 68 | |
---|
| 69 | this->setConfigValues(); |
---|
| 70 | |
---|
| 71 | // load the corresponding score board |
---|
[2896] | 72 | if (GameMode::showsGraphics() && this->scoreboardTemplate_ != "") |
---|
[2662] | 73 | { |
---|
| 74 | this->scoreboard_ = new OverlayGroup(this); |
---|
| 75 | this->scoreboard_->addTemplate(this->scoreboardTemplate_); |
---|
| 76 | this->scoreboard_->setGametype(this); |
---|
| 77 | } |
---|
| 78 | else |
---|
| 79 | this->scoreboard_ = 0; |
---|
[2072] | 80 | } |
---|
[5806] | 81 | |
---|
| 82 | Gametype::~Gametype() |
---|
| 83 | { |
---|
| 84 | if (this->isInitialized()) |
---|
| 85 | this->gtinfo_->destroy(); |
---|
| 86 | } |
---|
[2072] | 87 | |
---|
[2662] | 88 | void Gametype::setConfigValues() |
---|
| 89 | { |
---|
| 90 | SetConfigValue(initialStartCountdown_, 3.0f); |
---|
| 91 | SetConfigValue(bAutoStart_, false); |
---|
| 92 | SetConfigValue(bForceSpawn_, false); |
---|
| 93 | SetConfigValue(numberOfBots_, 0); |
---|
| 94 | SetConfigValue(scoreboardTemplate_, "defaultScoreboard"); |
---|
| 95 | } |
---|
| 96 | |
---|
[2072] | 97 | void Gametype::tick(float dt) |
---|
| 98 | { |
---|
[2662] | 99 | SUPER(Gametype, tick, dt); |
---|
[2072] | 100 | |
---|
[3033] | 101 | //count timer |
---|
| 102 | if (timerIsActive_) |
---|
| 103 | { |
---|
| 104 | if (this->timeLimit_ == 0) |
---|
| 105 | this->time_ += dt; |
---|
| 106 | else |
---|
| 107 | this->time_ -= dt; |
---|
| 108 | } |
---|
| 109 | |
---|
[5806] | 110 | if (this->gtinfo_->bStartCountdownRunning_ && !this->gtinfo_->bStarted_) |
---|
| 111 | this->gtinfo_->startCountdown_ -= dt; |
---|
[2662] | 112 | |
---|
[5806] | 113 | if (!this->gtinfo_->bStarted_) |
---|
[2072] | 114 | this->checkStart(); |
---|
[5806] | 115 | else if (!this->gtinfo_->bEnded_) |
---|
[2171] | 116 | this->spawnDeadPlayersIfRequested(); |
---|
[2072] | 117 | |
---|
| 118 | this->assignDefaultPawnsIfNeeded(); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | void Gametype::start() |
---|
| 122 | { |
---|
[2826] | 123 | this->addBots(this->numberOfBots_); |
---|
| 124 | |
---|
[5806] | 125 | this->gtinfo_->bStarted_ = true; |
---|
[2072] | 126 | |
---|
| 127 | this->spawnPlayersIfRequested(); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | void Gametype::end() |
---|
| 131 | { |
---|
[5806] | 132 | this->gtinfo_->bEnded_ = true; |
---|
[3033] | 133 | |
---|
| 134 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
| 135 | { |
---|
| 136 | if (it->first->getControllableEntity()) |
---|
| 137 | { |
---|
| 138 | ControllableEntity* oldentity = it->first->getControllableEntity(); |
---|
[3038] | 139 | |
---|
[5827] | 140 | ControllableEntity* entity = this->defaultControllableEntity_.fabricate(oldentity); |
---|
[3033] | 141 | if (oldentity->getCamera()) |
---|
| 142 | { |
---|
| 143 | entity->setPosition(oldentity->getCamera()->getWorldPosition()); |
---|
| 144 | entity->setOrientation(oldentity->getCamera()->getWorldOrientation()); |
---|
| 145 | } |
---|
| 146 | else |
---|
| 147 | { |
---|
| 148 | entity->setPosition(oldentity->getWorldPosition()); |
---|
| 149 | entity->setOrientation(oldentity->getWorldOrientation()); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | it->first->startControl(entity); |
---|
| 153 | } |
---|
| 154 | else |
---|
| 155 | this->spawnPlayerAsDefaultPawn(it->first); |
---|
| 156 | } |
---|
[2072] | 157 | } |
---|
| 158 | |
---|
| 159 | void Gametype::playerEntered(PlayerInfo* player) |
---|
| 160 | { |
---|
[2662] | 161 | this->players_[player].state_ = PlayerState::Joined; |
---|
[2072] | 162 | } |
---|
| 163 | |
---|
[2826] | 164 | bool Gametype::playerLeft(PlayerInfo* player) |
---|
[2072] | 165 | { |
---|
[2662] | 166 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player); |
---|
[2072] | 167 | if (it != this->players_.end()) |
---|
| 168 | { |
---|
| 169 | this->players_.erase(it); |
---|
[2826] | 170 | return true; |
---|
[2072] | 171 | } |
---|
[2826] | 172 | return false; |
---|
[2072] | 173 | } |
---|
| 174 | |
---|
| 175 | void Gametype::playerSwitched(PlayerInfo* player, Gametype* newgametype) |
---|
| 176 | { |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | void Gametype::playerSwitchedBack(PlayerInfo* player, Gametype* oldgametype) |
---|
| 180 | { |
---|
| 181 | } |
---|
| 182 | |
---|
[2826] | 183 | bool Gametype::playerChangedName(PlayerInfo* player) |
---|
[2072] | 184 | { |
---|
| 185 | if (this->players_.find(player) != this->players_.end()) |
---|
| 186 | { |
---|
| 187 | if (player->getName() != player->getOldName()) |
---|
| 188 | { |
---|
[2826] | 189 | return true; |
---|
[2072] | 190 | } |
---|
| 191 | } |
---|
[2826] | 192 | return false; |
---|
[2072] | 193 | } |
---|
| 194 | |
---|
| 195 | void Gametype::pawnPreSpawn(Pawn* pawn) |
---|
| 196 | { |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | void Gametype::pawnPostSpawn(Pawn* pawn) |
---|
| 200 | { |
---|
| 201 | } |
---|
| 202 | |
---|
[2826] | 203 | void Gametype::playerPreSpawn(PlayerInfo* player) |
---|
[2072] | 204 | { |
---|
[2826] | 205 | } |
---|
[2662] | 206 | |
---|
[2826] | 207 | void Gametype::playerPostSpawn(PlayerInfo* player) |
---|
| 208 | { |
---|
| 209 | } |
---|
[2662] | 210 | |
---|
[2826] | 211 | void Gametype::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn) |
---|
| 212 | { |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | void Gametype::playerStopsControllingPawn(PlayerInfo* player, Pawn* pawn) |
---|
| 216 | { |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | bool Gametype::allowPawnHit(Pawn* victim, Pawn* originator) |
---|
| 220 | { |
---|
| 221 | return true; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | bool Gametype::allowPawnDamage(Pawn* victim, Pawn* originator) |
---|
| 225 | { |
---|
| 226 | return true; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | bool Gametype::allowPawnDeath(Pawn* victim, Pawn* originator) |
---|
| 230 | { |
---|
| 231 | return true; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | void Gametype::pawnKilled(Pawn* victim, Pawn* killer) |
---|
| 235 | { |
---|
[2662] | 236 | if (victim && victim->getPlayer()) |
---|
| 237 | { |
---|
| 238 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(victim->getPlayer()); |
---|
| 239 | if (it != this->players_.end()) |
---|
| 240 | { |
---|
| 241 | it->second.state_ = PlayerState::Dead; |
---|
| 242 | it->second.killed_++; |
---|
| 243 | |
---|
| 244 | // Reward killer |
---|
[3099] | 245 | if (killer && killer->getPlayer()) |
---|
[2839] | 246 | { |
---|
| 247 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(killer->getPlayer()); |
---|
| 248 | if (it != this->players_.end()) |
---|
[3099] | 249 | { |
---|
[2839] | 250 | it->second.frags_++; |
---|
[3099] | 251 | |
---|
| 252 | if (killer->getPlayer()->getClientID() != CLIENTID_UNKNOWN) |
---|
[5806] | 253 | this->gtinfo_->sendKillMessage("You killed " + victim->getPlayer()->getName(), killer->getPlayer()->getClientID()); |
---|
[3099] | 254 | if (victim->getPlayer()->getClientID() != CLIENTID_UNKNOWN) |
---|
[5806] | 255 | this->gtinfo_->sendDeathMessage("You were killed by " + killer->getPlayer()->getName(), victim->getPlayer()->getClientID()); |
---|
[3099] | 256 | } |
---|
[2839] | 257 | } |
---|
[2662] | 258 | |
---|
| 259 | ControllableEntity* entity = this->defaultControllableEntity_.fabricate(victim->getCreator()); |
---|
| 260 | if (victim->getCamera()) |
---|
| 261 | { |
---|
| 262 | entity->setPosition(victim->getCamera()->getWorldPosition()); |
---|
| 263 | entity->setOrientation(victim->getCamera()->getWorldOrientation()); |
---|
| 264 | } |
---|
| 265 | else |
---|
| 266 | { |
---|
| 267 | entity->setPosition(victim->getWorldPosition()); |
---|
| 268 | entity->setOrientation(victim->getWorldOrientation()); |
---|
| 269 | } |
---|
| 270 | it->first->startControl(entity); |
---|
| 271 | } |
---|
| 272 | else |
---|
| 273 | COUT(2) << "Warning: Killed Pawn was not in the playerlist" << std::endl; |
---|
| 274 | } |
---|
[2072] | 275 | } |
---|
| 276 | |
---|
[2826] | 277 | void Gametype::playerScored(PlayerInfo* player) |
---|
[2072] | 278 | { |
---|
[2826] | 279 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player); |
---|
| 280 | if (it != this->players_.end()) |
---|
| 281 | it->second.frags_++; |
---|
[2072] | 282 | } |
---|
| 283 | |
---|
[2890] | 284 | int Gametype::getScore(PlayerInfo* player) const |
---|
| 285 | { |
---|
| 286 | std::map<PlayerInfo*, Player>::const_iterator it = this->players_.find(player); |
---|
| 287 | if (it != this->players_.end()) |
---|
| 288 | return it->second.frags_; |
---|
| 289 | else |
---|
| 290 | return 0; |
---|
| 291 | } |
---|
| 292 | |
---|
[2072] | 293 | SpawnPoint* Gametype::getBestSpawnPoint(PlayerInfo* player) const |
---|
| 294 | { |
---|
| 295 | if (this->spawnpoints_.size() > 0) |
---|
| 296 | { |
---|
[3196] | 297 | unsigned int randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(this->spawnpoints_.size()))); |
---|
[2072] | 298 | unsigned int index = 0; |
---|
| 299 | for (std::set<SpawnPoint*>::const_iterator it = this->spawnpoints_.begin(); it != this->spawnpoints_.end(); ++it) |
---|
| 300 | { |
---|
| 301 | if (index == randomspawn) |
---|
| 302 | return (*it); |
---|
| 303 | |
---|
| 304 | ++index; |
---|
| 305 | } |
---|
| 306 | } |
---|
| 307 | return 0; |
---|
| 308 | } |
---|
| 309 | |
---|
[2171] | 310 | void Gametype::assignDefaultPawnsIfNeeded() |
---|
[2072] | 311 | { |
---|
[2662] | 312 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
[2072] | 313 | { |
---|
[2662] | 314 | if (!it->first->getControllableEntity()) |
---|
[2072] | 315 | { |
---|
[2662] | 316 | it->second.state_ = PlayerState::Dead; |
---|
| 317 | |
---|
[5806] | 318 | if (!it->first->isReadyToSpawn() || !this->gtinfo_->bStarted_) |
---|
[2072] | 319 | { |
---|
[3033] | 320 | this->spawnPlayerAsDefaultPawn(it->first); |
---|
| 321 | it->second.state_ = PlayerState::Dead; |
---|
[2072] | 322 | } |
---|
| 323 | } |
---|
| 324 | } |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | void Gametype::checkStart() |
---|
| 328 | { |
---|
[5806] | 329 | if (!this->gtinfo_->bStarted_) |
---|
[2072] | 330 | { |
---|
[5806] | 331 | if (this->gtinfo_->bStartCountdownRunning_) |
---|
[2072] | 332 | { |
---|
[5806] | 333 | if (this->gtinfo_->startCountdown_ <= 0) |
---|
[2072] | 334 | { |
---|
[5806] | 335 | this->gtinfo_->bStartCountdownRunning_ = false; |
---|
| 336 | this->gtinfo_->startCountdown_ = 0; |
---|
[2072] | 337 | this->start(); |
---|
| 338 | } |
---|
| 339 | } |
---|
| 340 | else if (this->players_.size() > 0) |
---|
| 341 | { |
---|
| 342 | if (this->bAutoStart_) |
---|
| 343 | { |
---|
| 344 | this->start(); |
---|
| 345 | } |
---|
| 346 | else |
---|
| 347 | { |
---|
| 348 | bool allplayersready = true; |
---|
[2662] | 349 | bool hashumanplayers = false; |
---|
| 350 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
[2072] | 351 | { |
---|
[2171] | 352 | if (!it->first->isReadyToSpawn()) |
---|
[2072] | 353 | allplayersready = false; |
---|
[2662] | 354 | if (it->first->isHumanPlayer()) |
---|
| 355 | hashumanplayers = true; |
---|
[2072] | 356 | } |
---|
[2662] | 357 | if (allplayersready && hashumanplayers) |
---|
[2072] | 358 | { |
---|
[5806] | 359 | this->gtinfo_->startCountdown_ = this->initialStartCountdown_; |
---|
| 360 | this->gtinfo_->bStartCountdownRunning_ = true; |
---|
[2072] | 361 | } |
---|
| 362 | } |
---|
| 363 | } |
---|
| 364 | } |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | void Gametype::spawnPlayersIfRequested() |
---|
| 368 | { |
---|
[2662] | 369 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
[2171] | 370 | if (it->first->isReadyToSpawn() || this->bForceSpawn_) |
---|
| 371 | this->spawnPlayer(it->first); |
---|
[2072] | 372 | } |
---|
| 373 | |
---|
| 374 | void Gametype::spawnDeadPlayersIfRequested() |
---|
| 375 | { |
---|
[2662] | 376 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
| 377 | if (it->second.state_ == PlayerState::Dead) |
---|
[2171] | 378 | if (it->first->isReadyToSpawn() || this->bForceSpawn_) |
---|
| 379 | this->spawnPlayer(it->first); |
---|
[2072] | 380 | } |
---|
| 381 | |
---|
| 382 | void Gametype::spawnPlayer(PlayerInfo* player) |
---|
| 383 | { |
---|
| 384 | SpawnPoint* spawnpoint = this->getBestSpawnPoint(player); |
---|
| 385 | if (spawnpoint) |
---|
| 386 | { |
---|
[2826] | 387 | this->playerPreSpawn(player); |
---|
[2072] | 388 | player->startControl(spawnpoint->spawn()); |
---|
[2662] | 389 | this->players_[player].state_ = PlayerState::Alive; |
---|
[2826] | 390 | this->playerPostSpawn(player); |
---|
[2072] | 391 | } |
---|
| 392 | else |
---|
| 393 | { |
---|
| 394 | COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl; |
---|
| 395 | abort(); |
---|
| 396 | } |
---|
| 397 | } |
---|
[2662] | 398 | |
---|
[3033] | 399 | void Gametype::spawnPlayerAsDefaultPawn(PlayerInfo* player) |
---|
| 400 | { |
---|
| 401 | SpawnPoint* spawn = this->getBestSpawnPoint(player); |
---|
| 402 | if (spawn) |
---|
| 403 | { |
---|
| 404 | // force spawn at spawnpoint with default pawn |
---|
| 405 | ControllableEntity* entity = this->defaultControllableEntity_.fabricate(spawn); |
---|
| 406 | spawn->spawn(entity); |
---|
| 407 | player->startControl(entity); |
---|
| 408 | } |
---|
| 409 | else |
---|
| 410 | { |
---|
| 411 | COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl; |
---|
| 412 | abort(); |
---|
| 413 | } |
---|
| 414 | } |
---|
| 415 | |
---|
[2662] | 416 | void Gametype::addBots(unsigned int amount) |
---|
| 417 | { |
---|
| 418 | for (unsigned int i = 0; i < amount; ++i) |
---|
[2839] | 419 | this->botclass_.fabricate(this); |
---|
[2662] | 420 | } |
---|
| 421 | |
---|
| 422 | void Gametype::killBots(unsigned int amount) |
---|
| 423 | { |
---|
| 424 | unsigned int i = 0; |
---|
| 425 | for (ObjectList<Bot>::iterator it = ObjectList<Bot>::begin(); (it != ObjectList<Bot>::end()) && ((amount == 0) || (i < amount)); ) |
---|
| 426 | { |
---|
| 427 | if (it->getGametype() == this) |
---|
| 428 | { |
---|
[5801] | 429 | (it++)->destroy(); |
---|
[2662] | 430 | ++i; |
---|
| 431 | } |
---|
[5795] | 432 | else |
---|
| 433 | ++it; |
---|
[2662] | 434 | } |
---|
| 435 | } |
---|
[3033] | 436 | |
---|
| 437 | void Gametype::addTime(float t) |
---|
[3038] | 438 | { |
---|
[3033] | 439 | if (this->timeLimit_ == 0) |
---|
| 440 | this->time_ -= t; |
---|
| 441 | else |
---|
| 442 | this->time_ += t; |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | void Gametype::removeTime(float t) |
---|
[3038] | 446 | { |
---|
[3033] | 447 | if (this->timeLimit_ == 0) |
---|
| 448 | this->time_ += t; |
---|
| 449 | else |
---|
| 450 | this->time_ -= t; |
---|
| 451 | } |
---|
| 452 | |
---|
| 453 | void Gametype::resetTimer() |
---|
[3038] | 454 | { |
---|
[3033] | 455 | this->resetTimer(timeLimit_); |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | void Gametype::resetTimer(float t) |
---|
[3038] | 459 | { |
---|
[3033] | 460 | this->timeLimit_ = t; |
---|
| 461 | this->time_ = t; |
---|
| 462 | } |
---|
[2072] | 463 | } |
---|