[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" |
---|
[2428] | 36 | #include "core/ConfigValueIncludes.h" |
---|
[2072] | 37 | #include "objects/infos/PlayerInfo.h" |
---|
| 38 | #include "objects/worldentities/pawns/Spectator.h" |
---|
| 39 | #include "objects/worldentities/SpawnPoint.h" |
---|
[2438] | 40 | #include "objects/worldentities/Camera.h" |
---|
[2072] | 41 | |
---|
| 42 | #include "network/Host.h" |
---|
| 43 | |
---|
| 44 | namespace orxonox |
---|
| 45 | { |
---|
| 46 | CreateUnloadableFactory(Gametype); |
---|
| 47 | |
---|
[2428] | 48 | Gametype::Gametype(BaseObject* creator) : BaseObject(creator), gtinfo_(creator) |
---|
[2072] | 49 | { |
---|
| 50 | RegisterObject(Gametype); |
---|
| 51 | |
---|
| 52 | this->defaultControllableEntity_ = Class(Spectator); |
---|
| 53 | |
---|
| 54 | this->bAutoStart_ = false; |
---|
| 55 | this->bForceSpawn_ = false; |
---|
| 56 | |
---|
| 57 | this->initialStartCountdown_ = 3; |
---|
[2428] | 58 | |
---|
| 59 | this->setConfigValues(); |
---|
[2072] | 60 | } |
---|
| 61 | |
---|
[2428] | 62 | void Gametype::setConfigValues() |
---|
| 63 | { |
---|
| 64 | SetConfigValue(initialStartCountdown_, 3.0f); |
---|
[2438] | 65 | SetConfigValue(bAutoStart_, false); |
---|
| 66 | SetConfigValue(bForceSpawn_, false); |
---|
[2428] | 67 | } |
---|
| 68 | |
---|
[2072] | 69 | void Gametype::tick(float dt) |
---|
| 70 | { |
---|
[2361] | 71 | SUPER(Gametype, tick, dt); |
---|
| 72 | |
---|
[2428] | 73 | if (this->gtinfo_.bStartCountdownRunning_ && !this->gtinfo_.bStarted_) |
---|
| 74 | this->gtinfo_.startCountdown_ -= dt; |
---|
[2072] | 75 | |
---|
[2428] | 76 | if (!this->gtinfo_.bStarted_) |
---|
[2072] | 77 | this->checkStart(); |
---|
[2171] | 78 | else |
---|
| 79 | this->spawnDeadPlayersIfRequested(); |
---|
[2072] | 80 | |
---|
| 81 | this->assignDefaultPawnsIfNeeded(); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | void Gametype::start() |
---|
| 85 | { |
---|
| 86 | COUT(0) << "game started" << std::endl; |
---|
[2428] | 87 | this->gtinfo_.bStarted_ = true; |
---|
[2072] | 88 | |
---|
| 89 | this->spawnPlayersIfRequested(); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | void Gametype::end() |
---|
| 93 | { |
---|
| 94 | COUT(0) << "game ended" << std::endl; |
---|
[2428] | 95 | this->gtinfo_.bEnded_ = true; |
---|
[2072] | 96 | } |
---|
| 97 | |
---|
| 98 | void Gametype::playerEntered(PlayerInfo* player) |
---|
| 99 | { |
---|
[2171] | 100 | this->players_[player] = PlayerState::Joined; |
---|
[2072] | 101 | |
---|
| 102 | std::string message = player->getName() + " entered the game"; |
---|
| 103 | COUT(0) << message << std::endl; |
---|
[2171] | 104 | Host::Broadcast(message); |
---|
[2072] | 105 | } |
---|
| 106 | |
---|
| 107 | void Gametype::playerLeft(PlayerInfo* player) |
---|
| 108 | { |
---|
[2171] | 109 | std::map<PlayerInfo*, PlayerState::Enum>::iterator it = this->players_.find(player); |
---|
[2072] | 110 | if (it != this->players_.end()) |
---|
| 111 | { |
---|
| 112 | this->players_.erase(it); |
---|
| 113 | |
---|
| 114 | std::string message = player->getName() + " left the game"; |
---|
| 115 | COUT(0) << message << std::endl; |
---|
[2171] | 116 | Host::Broadcast(message); |
---|
[2072] | 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | void Gametype::playerSwitched(PlayerInfo* player, Gametype* newgametype) |
---|
| 121 | { |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | void Gametype::playerSwitchedBack(PlayerInfo* player, Gametype* oldgametype) |
---|
| 125 | { |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | void Gametype::playerChangedName(PlayerInfo* player) |
---|
| 129 | { |
---|
| 130 | if (this->players_.find(player) != this->players_.end()) |
---|
| 131 | { |
---|
| 132 | if (player->getName() != player->getOldName()) |
---|
| 133 | { |
---|
| 134 | std::string message = player->getOldName() + " changed name to " + player->getName(); |
---|
| 135 | COUT(0) << message << std::endl; |
---|
[2171] | 136 | Host::Broadcast(message); |
---|
[2072] | 137 | } |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | void Gametype::pawnPreSpawn(Pawn* pawn) |
---|
| 142 | { |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | void Gametype::pawnPostSpawn(Pawn* pawn) |
---|
| 146 | { |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | void Gametype::pawnKilled(Pawn* victim, Pawn* killer) |
---|
| 150 | { |
---|
[2447] | 151 | if (victim && victim->getPlayer()) |
---|
[2438] | 152 | { |
---|
| 153 | std::map<PlayerInfo*, PlayerState::Enum>::iterator it = this->players_.find(victim->getPlayer()); |
---|
[2447] | 154 | if (it != this->players_.end()) |
---|
| 155 | { |
---|
| 156 | it->second = PlayerState::Dead; |
---|
[2438] | 157 | |
---|
[2447] | 158 | ControllableEntity* entity = this->defaultControllableEntity_.fabricate(victim->getCreator()); |
---|
| 159 | if (victim->getCamera()) |
---|
| 160 | { |
---|
| 161 | entity->setPosition(victim->getCamera()->getWorldPosition()); |
---|
| 162 | entity->setOrientation(victim->getCamera()->getWorldOrientation()); |
---|
| 163 | } |
---|
| 164 | else |
---|
| 165 | { |
---|
| 166 | entity->setPosition(victim->getWorldPosition()); |
---|
| 167 | entity->setOrientation(victim->getWorldOrientation()); |
---|
| 168 | } |
---|
| 169 | it->first->startControl(entity); |
---|
[2438] | 170 | } |
---|
| 171 | else |
---|
[2447] | 172 | COUT(2) << "Warning: Killed Pawn was not in the playerlist" << std::endl; |
---|
[2438] | 173 | } |
---|
[2072] | 174 | } |
---|
| 175 | |
---|
| 176 | void Gametype::playerScored(PlayerInfo* player) |
---|
| 177 | { |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | SpawnPoint* Gametype::getBestSpawnPoint(PlayerInfo* player) const |
---|
| 181 | { |
---|
| 182 | if (this->spawnpoints_.size() > 0) |
---|
| 183 | { |
---|
| 184 | unsigned int randomspawn = (unsigned int)rnd(this->spawnpoints_.size()); |
---|
| 185 | unsigned int index = 0; |
---|
| 186 | for (std::set<SpawnPoint*>::const_iterator it = this->spawnpoints_.begin(); it != this->spawnpoints_.end(); ++it) |
---|
| 187 | { |
---|
| 188 | if (index == randomspawn) |
---|
| 189 | return (*it); |
---|
| 190 | |
---|
| 191 | ++index; |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | return 0; |
---|
| 195 | } |
---|
| 196 | |
---|
[2171] | 197 | void Gametype::assignDefaultPawnsIfNeeded() |
---|
[2072] | 198 | { |
---|
[2171] | 199 | for (std::map<PlayerInfo*, PlayerState::Enum>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
[2072] | 200 | { |
---|
[2369] | 201 | if (!it->first->getControllableEntity()) |
---|
[2072] | 202 | { |
---|
[2369] | 203 | it->second = PlayerState::Dead; |
---|
| 204 | |
---|
[2428] | 205 | if (!it->first->isReadyToSpawn() || !this->gtinfo_.bStarted_) |
---|
[2072] | 206 | { |
---|
[2369] | 207 | SpawnPoint* spawn = this->getBestSpawnPoint(it->first); |
---|
| 208 | if (spawn) |
---|
| 209 | { |
---|
| 210 | // force spawn at spawnpoint with default pawn |
---|
| 211 | ControllableEntity* entity = this->defaultControllableEntity_.fabricate(spawn); |
---|
| 212 | spawn->spawn(entity); |
---|
| 213 | it->first->startControl(entity); |
---|
| 214 | it->second = PlayerState::Dead; |
---|
| 215 | } |
---|
| 216 | else |
---|
| 217 | { |
---|
| 218 | COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl; |
---|
| 219 | abort(); |
---|
| 220 | } |
---|
[2072] | 221 | } |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | void Gametype::checkStart() |
---|
| 227 | { |
---|
[2428] | 228 | if (!this->gtinfo_.bStarted_) |
---|
[2072] | 229 | { |
---|
[2428] | 230 | if (this->gtinfo_.bStartCountdownRunning_) |
---|
[2072] | 231 | { |
---|
[2428] | 232 | if (this->gtinfo_.startCountdown_ <= 0) |
---|
[2072] | 233 | { |
---|
[2428] | 234 | this->gtinfo_.bStartCountdownRunning_ = false; |
---|
| 235 | this->gtinfo_.startCountdown_ = 0; |
---|
[2072] | 236 | this->start(); |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | else if (this->players_.size() > 0) |
---|
| 240 | { |
---|
| 241 | if (this->bAutoStart_) |
---|
| 242 | { |
---|
| 243 | this->start(); |
---|
| 244 | } |
---|
| 245 | else |
---|
| 246 | { |
---|
| 247 | bool allplayersready = true; |
---|
[2369] | 248 | bool hashumanplayers = false; |
---|
[2171] | 249 | for (std::map<PlayerInfo*, PlayerState::Enum>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
[2072] | 250 | { |
---|
[2171] | 251 | if (!it->first->isReadyToSpawn()) |
---|
[2072] | 252 | allplayersready = false; |
---|
[2369] | 253 | if (it->first->isHumanPlayer()) |
---|
| 254 | hashumanplayers = true; |
---|
[2072] | 255 | } |
---|
[2369] | 256 | if (allplayersready && hashumanplayers) |
---|
[2072] | 257 | { |
---|
[2428] | 258 | this->gtinfo_.startCountdown_ = this->initialStartCountdown_; |
---|
| 259 | this->gtinfo_.bStartCountdownRunning_ = true; |
---|
[2072] | 260 | } |
---|
| 261 | } |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | void Gametype::spawnPlayersIfRequested() |
---|
| 267 | { |
---|
[2171] | 268 | for (std::map<PlayerInfo*, PlayerState::Enum>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
| 269 | if (it->first->isReadyToSpawn() || this->bForceSpawn_) |
---|
| 270 | this->spawnPlayer(it->first); |
---|
[2072] | 271 | } |
---|
| 272 | |
---|
| 273 | void Gametype::spawnDeadPlayersIfRequested() |
---|
| 274 | { |
---|
[2171] | 275 | for (std::map<PlayerInfo*, PlayerState::Enum>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
| 276 | if (it->second == PlayerState::Dead) |
---|
| 277 | if (it->first->isReadyToSpawn() || this->bForceSpawn_) |
---|
| 278 | this->spawnPlayer(it->first); |
---|
[2072] | 279 | } |
---|
| 280 | |
---|
| 281 | void Gametype::spawnPlayer(PlayerInfo* player) |
---|
| 282 | { |
---|
| 283 | SpawnPoint* spawnpoint = this->getBestSpawnPoint(player); |
---|
| 284 | if (spawnpoint) |
---|
| 285 | { |
---|
| 286 | player->startControl(spawnpoint->spawn()); |
---|
[2171] | 287 | this->players_[player] = PlayerState::Alive; |
---|
[2072] | 288 | } |
---|
| 289 | else |
---|
| 290 | { |
---|
| 291 | COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl; |
---|
| 292 | abort(); |
---|
| 293 | } |
---|
| 294 | } |
---|
| 295 | } |
---|