[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" |
---|
| 45 | #include "Settings.h" |
---|
[2072] | 46 | |
---|
| 47 | #include "network/Host.h" |
---|
| 48 | |
---|
| 49 | namespace orxonox |
---|
| 50 | { |
---|
| 51 | CreateUnloadableFactory(Gametype); |
---|
| 52 | |
---|
[2662] | 53 | Gametype::Gametype(BaseObject* creator) : BaseObject(creator), gtinfo_(creator) |
---|
[2072] | 54 | { |
---|
| 55 | RegisterObject(Gametype); |
---|
| 56 | |
---|
[2662] | 57 | this->setGametype(this); |
---|
| 58 | |
---|
[2072] | 59 | this->defaultControllableEntity_ = Class(Spectator); |
---|
| 60 | |
---|
| 61 | this->bAutoStart_ = false; |
---|
| 62 | this->bForceSpawn_ = false; |
---|
[2662] | 63 | this->numberOfBots_ = 0; |
---|
[2072] | 64 | |
---|
| 65 | this->initialStartCountdown_ = 3; |
---|
[2662] | 66 | |
---|
| 67 | this->setConfigValues(); |
---|
| 68 | |
---|
| 69 | this->addBots(this->numberOfBots_); |
---|
| 70 | |
---|
| 71 | // load the corresponding score board |
---|
| 72 | if (Core::showsGraphics() && this->scoreboardTemplate_ != "") |
---|
| 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 | } |
---|
| 81 | |
---|
[2662] | 82 | void Gametype::setConfigValues() |
---|
| 83 | { |
---|
| 84 | SetConfigValue(initialStartCountdown_, 3.0f); |
---|
| 85 | SetConfigValue(bAutoStart_, false); |
---|
| 86 | SetConfigValue(bForceSpawn_, false); |
---|
| 87 | SetConfigValue(numberOfBots_, 0); |
---|
| 88 | SetConfigValue(scoreboardTemplate_, "defaultScoreboard"); |
---|
| 89 | } |
---|
| 90 | |
---|
[2072] | 91 | void Gametype::tick(float dt) |
---|
| 92 | { |
---|
[2662] | 93 | SUPER(Gametype, tick, dt); |
---|
[2072] | 94 | |
---|
[2662] | 95 | if (this->gtinfo_.bStartCountdownRunning_ && !this->gtinfo_.bStarted_) |
---|
| 96 | this->gtinfo_.startCountdown_ -= dt; |
---|
| 97 | |
---|
| 98 | if (!this->gtinfo_.bStarted_) |
---|
[2072] | 99 | this->checkStart(); |
---|
[2171] | 100 | else |
---|
| 101 | this->spawnDeadPlayersIfRequested(); |
---|
[2072] | 102 | |
---|
| 103 | this->assignDefaultPawnsIfNeeded(); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | void Gametype::start() |
---|
| 107 | { |
---|
| 108 | COUT(0) << "game started" << std::endl; |
---|
[2662] | 109 | this->gtinfo_.bStarted_ = true; |
---|
[2072] | 110 | |
---|
| 111 | this->spawnPlayersIfRequested(); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | void Gametype::end() |
---|
| 115 | { |
---|
| 116 | COUT(0) << "game ended" << std::endl; |
---|
[2662] | 117 | this->gtinfo_.bEnded_ = true; |
---|
[2072] | 118 | } |
---|
| 119 | |
---|
| 120 | void Gametype::playerEntered(PlayerInfo* player) |
---|
| 121 | { |
---|
[2662] | 122 | this->players_[player].state_ = PlayerState::Joined; |
---|
[2072] | 123 | |
---|
| 124 | std::string message = player->getName() + " entered the game"; |
---|
| 125 | COUT(0) << message << std::endl; |
---|
[2171] | 126 | Host::Broadcast(message); |
---|
[2072] | 127 | } |
---|
| 128 | |
---|
| 129 | void Gametype::playerLeft(PlayerInfo* player) |
---|
| 130 | { |
---|
[2662] | 131 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player); |
---|
[2072] | 132 | if (it != this->players_.end()) |
---|
| 133 | { |
---|
| 134 | this->players_.erase(it); |
---|
| 135 | |
---|
| 136 | std::string message = player->getName() + " left the game"; |
---|
| 137 | COUT(0) << message << std::endl; |
---|
[2171] | 138 | Host::Broadcast(message); |
---|
[2072] | 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | void Gametype::playerSwitched(PlayerInfo* player, Gametype* newgametype) |
---|
| 143 | { |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | void Gametype::playerSwitchedBack(PlayerInfo* player, Gametype* oldgametype) |
---|
| 147 | { |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | void Gametype::playerChangedName(PlayerInfo* player) |
---|
| 151 | { |
---|
| 152 | if (this->players_.find(player) != this->players_.end()) |
---|
| 153 | { |
---|
| 154 | if (player->getName() != player->getOldName()) |
---|
| 155 | { |
---|
| 156 | std::string message = player->getOldName() + " changed name to " + player->getName(); |
---|
| 157 | COUT(0) << message << std::endl; |
---|
[2171] | 158 | Host::Broadcast(message); |
---|
[2072] | 159 | } |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | void Gametype::pawnPreSpawn(Pawn* pawn) |
---|
| 164 | { |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | void Gametype::pawnPostSpawn(Pawn* pawn) |
---|
| 168 | { |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | void Gametype::pawnKilled(Pawn* victim, Pawn* killer) |
---|
| 172 | { |
---|
[2662] | 173 | if (victim && victim->getPlayer()) |
---|
| 174 | { |
---|
| 175 | std::string message; |
---|
| 176 | if (killer) |
---|
| 177 | { |
---|
| 178 | if (killer->getPlayer()) |
---|
| 179 | message = victim->getPlayer()->getName() + " was killed by " + killer->getPlayer()->getName(); |
---|
| 180 | else |
---|
| 181 | message = victim->getPlayer()->getName() + " was killed"; |
---|
| 182 | } |
---|
| 183 | else |
---|
| 184 | message = victim->getPlayer()->getName() + " died"; |
---|
| 185 | |
---|
| 186 | COUT(0) << message << std::endl; |
---|
| 187 | Host::Broadcast(message); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | if (victim && victim->getPlayer()) |
---|
| 191 | { |
---|
| 192 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(victim->getPlayer()); |
---|
| 193 | if (it != this->players_.end()) |
---|
| 194 | { |
---|
| 195 | it->second.state_ = PlayerState::Dead; |
---|
| 196 | it->second.killed_++; |
---|
| 197 | |
---|
| 198 | // Reward killer |
---|
| 199 | if (killer) |
---|
| 200 | { |
---|
| 201 | std::map<PlayerInfo*, Player>::iterator itKiller = this->players_.find(killer->getPlayer()); |
---|
| 202 | if (itKiller != this->players_.end()) |
---|
| 203 | { |
---|
| 204 | this->playerScored(itKiller->second); |
---|
| 205 | } |
---|
| 206 | else |
---|
| 207 | COUT(2) << "Warning: Killing Pawn was not in the playerlist" << std::endl; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | ControllableEntity* entity = this->defaultControllableEntity_.fabricate(victim->getCreator()); |
---|
| 211 | if (victim->getCamera()) |
---|
| 212 | { |
---|
| 213 | entity->setPosition(victim->getCamera()->getWorldPosition()); |
---|
| 214 | entity->setOrientation(victim->getCamera()->getWorldOrientation()); |
---|
| 215 | } |
---|
| 216 | else |
---|
| 217 | { |
---|
| 218 | entity->setPosition(victim->getWorldPosition()); |
---|
| 219 | entity->setOrientation(victim->getWorldOrientation()); |
---|
| 220 | } |
---|
| 221 | it->first->startControl(entity); |
---|
| 222 | } |
---|
| 223 | else |
---|
| 224 | COUT(2) << "Warning: Killed Pawn was not in the playerlist" << std::endl; |
---|
| 225 | } |
---|
[2072] | 226 | } |
---|
| 227 | |
---|
[2662] | 228 | void Gametype::playerScored(Player& player) |
---|
[2072] | 229 | { |
---|
[2662] | 230 | player.frags_++; |
---|
[2072] | 231 | } |
---|
| 232 | |
---|
| 233 | SpawnPoint* Gametype::getBestSpawnPoint(PlayerInfo* player) const |
---|
| 234 | { |
---|
| 235 | if (this->spawnpoints_.size() > 0) |
---|
| 236 | { |
---|
| 237 | unsigned int randomspawn = (unsigned int)rnd(this->spawnpoints_.size()); |
---|
| 238 | unsigned int index = 0; |
---|
| 239 | for (std::set<SpawnPoint*>::const_iterator it = this->spawnpoints_.begin(); it != this->spawnpoints_.end(); ++it) |
---|
| 240 | { |
---|
| 241 | if (index == randomspawn) |
---|
| 242 | return (*it); |
---|
| 243 | |
---|
| 244 | ++index; |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | return 0; |
---|
| 248 | } |
---|
| 249 | |
---|
[2171] | 250 | void Gametype::assignDefaultPawnsIfNeeded() |
---|
[2072] | 251 | { |
---|
[2662] | 252 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
[2072] | 253 | { |
---|
[2662] | 254 | if (!it->first->getControllableEntity()) |
---|
[2072] | 255 | { |
---|
[2662] | 256 | it->second.state_ = PlayerState::Dead; |
---|
| 257 | |
---|
| 258 | if (!it->first->isReadyToSpawn() || !this->gtinfo_.bStarted_) |
---|
[2072] | 259 | { |
---|
[2662] | 260 | SpawnPoint* spawn = this->getBestSpawnPoint(it->first); |
---|
| 261 | if (spawn) |
---|
| 262 | { |
---|
| 263 | // force spawn at spawnpoint with default pawn |
---|
| 264 | ControllableEntity* entity = this->defaultControllableEntity_.fabricate(spawn); |
---|
| 265 | spawn->spawn(entity); |
---|
| 266 | it->first->startControl(entity); |
---|
| 267 | it->second.state_ = PlayerState::Dead; |
---|
| 268 | } |
---|
| 269 | else |
---|
| 270 | { |
---|
| 271 | COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl; |
---|
| 272 | abort(); |
---|
| 273 | } |
---|
[2072] | 274 | } |
---|
| 275 | } |
---|
| 276 | } |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | void Gametype::checkStart() |
---|
| 280 | { |
---|
[2662] | 281 | if (!this->gtinfo_.bStarted_) |
---|
[2072] | 282 | { |
---|
[2662] | 283 | if (this->gtinfo_.bStartCountdownRunning_) |
---|
[2072] | 284 | { |
---|
[2662] | 285 | if (this->gtinfo_.startCountdown_ <= 0) |
---|
[2072] | 286 | { |
---|
[2662] | 287 | this->gtinfo_.bStartCountdownRunning_ = false; |
---|
| 288 | this->gtinfo_.startCountdown_ = 0; |
---|
[2072] | 289 | this->start(); |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | else if (this->players_.size() > 0) |
---|
| 293 | { |
---|
| 294 | if (this->bAutoStart_) |
---|
| 295 | { |
---|
| 296 | this->start(); |
---|
| 297 | } |
---|
| 298 | else |
---|
| 299 | { |
---|
| 300 | bool allplayersready = true; |
---|
[2662] | 301 | bool hashumanplayers = false; |
---|
| 302 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
[2072] | 303 | { |
---|
[2171] | 304 | if (!it->first->isReadyToSpawn()) |
---|
[2072] | 305 | allplayersready = false; |
---|
[2662] | 306 | if (it->first->isHumanPlayer()) |
---|
| 307 | hashumanplayers = true; |
---|
[2072] | 308 | } |
---|
[2662] | 309 | if (allplayersready && hashumanplayers) |
---|
[2072] | 310 | { |
---|
[2662] | 311 | this->gtinfo_.startCountdown_ = this->initialStartCountdown_; |
---|
| 312 | this->gtinfo_.bStartCountdownRunning_ = true; |
---|
[2072] | 313 | } |
---|
| 314 | } |
---|
| 315 | } |
---|
| 316 | } |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | void Gametype::spawnPlayersIfRequested() |
---|
| 320 | { |
---|
[2662] | 321 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
[2171] | 322 | if (it->first->isReadyToSpawn() || this->bForceSpawn_) |
---|
| 323 | this->spawnPlayer(it->first); |
---|
[2072] | 324 | } |
---|
| 325 | |
---|
| 326 | void Gametype::spawnDeadPlayersIfRequested() |
---|
| 327 | { |
---|
[2662] | 328 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
| 329 | if (it->second.state_ == PlayerState::Dead) |
---|
[2171] | 330 | if (it->first->isReadyToSpawn() || this->bForceSpawn_) |
---|
| 331 | this->spawnPlayer(it->first); |
---|
[2072] | 332 | } |
---|
| 333 | |
---|
| 334 | void Gametype::spawnPlayer(PlayerInfo* player) |
---|
| 335 | { |
---|
| 336 | SpawnPoint* spawnpoint = this->getBestSpawnPoint(player); |
---|
| 337 | if (spawnpoint) |
---|
| 338 | { |
---|
| 339 | player->startControl(spawnpoint->spawn()); |
---|
[2662] | 340 | this->players_[player].state_ = PlayerState::Alive; |
---|
[2072] | 341 | } |
---|
| 342 | else |
---|
| 343 | { |
---|
| 344 | COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl; |
---|
| 345 | abort(); |
---|
| 346 | } |
---|
| 347 | } |
---|
[2662] | 348 | |
---|
| 349 | void Gametype::addBots(unsigned int amount) |
---|
| 350 | { |
---|
| 351 | for (unsigned int i = 0; i < amount; ++i) |
---|
| 352 | new Bot(this); |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | void Gametype::killBots(unsigned int amount) |
---|
| 356 | { |
---|
| 357 | unsigned int i = 0; |
---|
| 358 | for (ObjectList<Bot>::iterator it = ObjectList<Bot>::begin(); (it != ObjectList<Bot>::end()) && ((amount == 0) || (i < amount)); ) |
---|
| 359 | { |
---|
| 360 | if (it->getGametype() == this) |
---|
| 361 | { |
---|
| 362 | delete (*(it++)); |
---|
| 363 | ++i; |
---|
| 364 | } |
---|
| 365 | } |
---|
| 366 | } |
---|
[2072] | 367 | } |
---|