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