[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: |
---|
[7802] | 25 | * Damian 'Mozork' Frick |
---|
[11206] | 26 | * Matthias Binder |
---|
[2072] | 27 | * |
---|
| 28 | */ |
---|
| 29 | |
---|
[7804] | 30 | /** |
---|
| 31 | @file LevelManager.cc |
---|
| 32 | @brief Implementation of the LevelManager singleton. |
---|
| 33 | */ |
---|
| 34 | |
---|
[2072] | 35 | #include "LevelManager.h" |
---|
[11206] | 36 | #include "LevelStatus.h" |
---|
[2072] | 37 | |
---|
[3196] | 38 | #include <map> |
---|
[3280] | 39 | |
---|
[10624] | 40 | #include "core/singleton/ScopedSingletonIncludes.h" |
---|
| 41 | #include "core/commandline/CommandLineIncludes.h" |
---|
[9667] | 42 | #include "core/config/ConfigValueIncludes.h" |
---|
| 43 | #include "core/CoreIncludes.h" |
---|
[7648] | 44 | #include "core/ClassTreeMask.h" |
---|
[3370] | 45 | #include "core/Loader.h" |
---|
[6417] | 46 | #include "core/Resource.h" |
---|
[7648] | 47 | #include "core/XMLFile.h" |
---|
[7802] | 48 | #include "Level.h" |
---|
[2171] | 49 | #include "PlayerManager.h" |
---|
[2072] | 50 | |
---|
[11206] | 51 | |
---|
[2072] | 52 | namespace orxonox |
---|
| 53 | { |
---|
[11206] | 54 | |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | |
---|
[3280] | 58 | SetCommandLineArgument(level, "").shortcut("l").information("Default level file (overrides LevelManager::defaultLevelName_ configValue)"); |
---|
| 59 | |
---|
[10624] | 60 | ManageScopedSingleton(LevelManager, ScopeID::ROOT, false); |
---|
[2072] | 61 | |
---|
[10624] | 62 | RegisterAbstractClass(LevelManager).inheritsFrom<Configurable>(); |
---|
| 63 | |
---|
[7802] | 64 | /** |
---|
| 65 | @brief |
---|
| 66 | Constructor. Registers the object, sets config values and initializes variables. |
---|
| 67 | */ |
---|
[2072] | 68 | LevelManager::LevelManager() |
---|
| 69 | { |
---|
[9667] | 70 | RegisterObject(LevelManager); |
---|
[3280] | 71 | this->setConfigValues(); |
---|
| 72 | |
---|
[11206] | 73 | |
---|
| 74 | |
---|
[3280] | 75 | // check override |
---|
[6021] | 76 | if (!CommandLineParser::getArgument("level")->hasDefaultValue()) |
---|
[3280] | 77 | { |
---|
[9550] | 78 | ModifyConfigValue(defaultLevelName_, tset, CommandLineParser::getValue("level").get<std::string>()); |
---|
[3280] | 79 | } |
---|
[7648] | 80 | |
---|
| 81 | this->compileAvailableLevelList(); |
---|
[7802] | 82 | this->nextIndex_ = 0; |
---|
| 83 | this->nextLevel_ = this->availableLevels_.begin(); |
---|
[11206] | 84 | |
---|
| 85 | buildallLevelStatus(); |
---|
[2072] | 86 | } |
---|
| 87 | |
---|
[11206] | 88 | |
---|
[2072] | 89 | LevelManager::~LevelManager() |
---|
| 90 | { |
---|
[8079] | 91 | // Delete all the LevelInfoItem objects because the LevelManager created them |
---|
[11071] | 92 | for (LevelInfoItem* info : availableLevels_) |
---|
| 93 | info->destroy(); |
---|
[11206] | 94 | for(unsigned int i = 0; i<allLevelStatus_.size();++i) |
---|
| 95 | { |
---|
| 96 | allLevelStatus_[i]->destroy(); |
---|
| 97 | } |
---|
[2072] | 98 | } |
---|
| 99 | |
---|
[7802] | 100 | /** |
---|
| 101 | @brief |
---|
| 102 | Set the config values for this object. |
---|
| 103 | */ |
---|
[3280] | 104 | void LevelManager::setConfigValues() |
---|
| 105 | { |
---|
[11206] | 106 | |
---|
[8891] | 107 | SetConfigValue(defaultLevelName_, "missionOne.oxw") |
---|
[6417] | 108 | .description("Sets the pre selection of the level in the main menu."); |
---|
[11206] | 109 | SetConfigValue(lastWonMission_, "") |
---|
[10258] | 110 | .description("The last finished mission of a campaign"); |
---|
| 111 | SetConfigValue(campaignMissions_, std::vector<std::string>()) |
---|
| 112 | .description("The list of missions in the campaign"); |
---|
[3280] | 113 | } |
---|
| 114 | |
---|
[10258] | 115 | |
---|
| 116 | /** |
---|
[7802] | 117 | @brief |
---|
| 118 | Request activity for the input Level. |
---|
| 119 | The Level will be added to the list of Levels whose activity is requested. The list is accessed in a FIFO manner. |
---|
| 120 | If the Level is the only Level in the list it will be immediately activated. If not it will be activated as soon as it reaches the front of the list. |
---|
| 121 | @param level |
---|
| 122 | A pointer to the Level whose activity is requested. |
---|
| 123 | */ |
---|
[2072] | 124 | void LevelManager::requestActivity(Level* level) |
---|
| 125 | { |
---|
[7802] | 126 | assert( std::find(this->levels_.begin(), this->levels_.end(), level)==this->levels_.end() ); |
---|
| 127 | // If the level is already in list. |
---|
| 128 | if( std::find(this->levels_.begin(), this->levels_.end(), level)!=this->levels_.end() ) |
---|
| 129 | return; |
---|
| 130 | // If it isn't insert it at the back. |
---|
| 131 | this->levels_.push_back(level); |
---|
| 132 | // If it is the only level in the list activate it. |
---|
| 133 | if (this->levels_.size() == 1) |
---|
[2072] | 134 | this->activateNextLevel(); |
---|
| 135 | } |
---|
| 136 | |
---|
[7802] | 137 | /** |
---|
| 138 | @brief |
---|
| 139 | Release activity for the input Level. |
---|
| 140 | Removes the Level from the list. If the Level was the one currently active, it is deactivated and the next Level in line is activated. |
---|
| 141 | @param level |
---|
| 142 | A pointer to the Level whose activity is to be released. |
---|
| 143 | */ |
---|
[2072] | 144 | void LevelManager::releaseActivity(Level* level) |
---|
| 145 | { |
---|
[7802] | 146 | if (this->levels_.size() > 0) |
---|
[2072] | 147 | { |
---|
[7802] | 148 | // If the level is the active level in the front of the list. |
---|
| 149 | if (this->levels_.front() == level) |
---|
[2072] | 150 | { |
---|
[7802] | 151 | // Deactivate it, remove it from the list and activate the next level in line. |
---|
[2072] | 152 | level->setActive(false); |
---|
[7802] | 153 | this->levels_.pop_front(); |
---|
[2072] | 154 | this->activateNextLevel(); |
---|
| 155 | } |
---|
[7802] | 156 | else // Else just remove it from the list. |
---|
| 157 | this->levels_.erase(std::find(this->levels_.begin(), this->levels_.end(), level)); |
---|
[2072] | 158 | } |
---|
| 159 | } |
---|
| 160 | |
---|
[7802] | 161 | /** |
---|
| 162 | @brief |
---|
| 163 | Get the currently active Level. |
---|
| 164 | @return |
---|
[11071] | 165 | Returns a pointer to the currently active level or nullptr if there currently are no active Levels. |
---|
[7802] | 166 | */ |
---|
[2072] | 167 | Level* LevelManager::getActiveLevel() |
---|
| 168 | { |
---|
[7802] | 169 | if (this->levels_.size() > 0) |
---|
| 170 | return this->levels_.front(); |
---|
[2072] | 171 | else |
---|
[11071] | 172 | return nullptr; |
---|
[2072] | 173 | } |
---|
| 174 | |
---|
[7802] | 175 | /** |
---|
| 176 | @brief |
---|
| 177 | Activate the next Level. |
---|
| 178 | */ |
---|
[2072] | 179 | void LevelManager::activateNextLevel() |
---|
| 180 | { |
---|
[11206] | 181 | |
---|
[7802] | 182 | if (this->levels_.size() > 0) |
---|
[2072] | 183 | { |
---|
[8891] | 184 | // Activate the level that is the first in the list of levels whose activity has been requested. |
---|
[7802] | 185 | this->levels_.front()->setActive(true); |
---|
| 186 | // Make every player enter the newly activated level. |
---|
[11071] | 187 | for (const auto& mapEntry : PlayerManager::getInstance().getClients()) |
---|
| 188 | this->levels_.front()->playerEntered(mapEntry.second); |
---|
[2072] | 189 | } |
---|
| 190 | } |
---|
[3280] | 191 | |
---|
[7802] | 192 | /** |
---|
| 193 | @brief |
---|
| 194 | Set the default Level. |
---|
| 195 | @param levelName |
---|
| 196 | The filename of the default Level. |
---|
| 197 | */ |
---|
[3280] | 198 | void LevelManager::setDefaultLevel(const std::string& levelName) |
---|
| 199 | { |
---|
| 200 | ModifyConfigValue(defaultLevelName_, set, levelName); |
---|
| 201 | } |
---|
| 202 | |
---|
[7802] | 203 | /** |
---|
| 204 | @brief |
---|
| 205 | Get the number of available Levels. |
---|
| 206 | Also updates the list of available Levels. |
---|
| 207 | @return |
---|
| 208 | Returns the number of available Levels. |
---|
| 209 | */ |
---|
[7648] | 210 | unsigned int LevelManager::getNumberOfLevels() |
---|
[3370] | 211 | { |
---|
[7648] | 212 | this->updateAvailableLevelList(); |
---|
| 213 | |
---|
| 214 | return this->availableLevels_.size(); |
---|
| 215 | } |
---|
| 216 | |
---|
[7802] | 217 | /** |
---|
| 218 | @brief |
---|
| 219 | Get the LevelInfoItem at the given index in the list of available Levels. |
---|
| 220 | The LevelInfoItems are sorted in alphabetical order accoridng to the name of the Level. |
---|
| 221 | This method is most efficiently called with consecutive indices (or at least ascending indices). |
---|
[7804] | 222 | @param index |
---|
| 223 | The index of the item that should be returned. |
---|
[7802] | 224 | @return |
---|
| 225 | Returns a pointer to the LevelInfoItem at the given index. |
---|
| 226 | */ |
---|
| 227 | LevelInfoItem* LevelManager::getAvailableLevelListItem(unsigned int index) |
---|
[7648] | 228 | { |
---|
[7804] | 229 | if(index >= this->availableLevels_.size()) |
---|
[11071] | 230 | return nullptr; |
---|
[7802] | 231 | |
---|
| 232 | // If this index directly follows the last we can optimize a lot. |
---|
| 233 | if(index == this->nextIndex_) |
---|
| 234 | { |
---|
| 235 | this->nextIndex_++; |
---|
| 236 | std::set<LevelInfoItem*, LevelInfoCompare>::iterator it = this->nextLevel_; |
---|
| 237 | this->nextLevel_++; |
---|
| 238 | return *it; |
---|
| 239 | } |
---|
[3370] | 240 | else |
---|
[7648] | 241 | { |
---|
[7802] | 242 | // If this index is bigger than the last, we can optimize a little. |
---|
[7839] | 243 | if(index < this->nextIndex_) |
---|
[7802] | 244 | { |
---|
| 245 | this->nextIndex_ = 0; |
---|
| 246 | this->nextLevel_ = this->availableLevels_.begin(); |
---|
| 247 | } |
---|
[8706] | 248 | |
---|
[7802] | 249 | while(this->nextIndex_ != index) |
---|
| 250 | { |
---|
| 251 | this->nextIndex_++; |
---|
| 252 | this->nextLevel_++; |
---|
| 253 | } |
---|
| 254 | this->nextIndex_++; |
---|
| 255 | std::set<LevelInfoItem*, LevelInfoCompare>::iterator it = this->nextLevel_; |
---|
| 256 | this->nextLevel_++; |
---|
| 257 | return *it; |
---|
[7648] | 258 | } |
---|
[3370] | 259 | } |
---|
| 260 | |
---|
[7802] | 261 | /** |
---|
| 262 | @brief |
---|
| 263 | Compile the list of available Levels. |
---|
| 264 | Iterates over all *.oxw files, loads the LevelInfo objects in them and from that it creates the LevelInfoItems which are inserted in a list. |
---|
| 265 | */ |
---|
[3370] | 266 | void LevelManager::compileAvailableLevelList() |
---|
| 267 | { |
---|
[8079] | 268 | // Get all files matching the level criteria |
---|
[6501] | 269 | Ogre::StringVectorPtr levels = Resource::findResourceNames("*.oxw"); |
---|
[8079] | 270 | |
---|
| 271 | // We only want to load as little as possible |
---|
| 272 | ClassTreeMask mask; |
---|
| 273 | mask.exclude(Class(BaseObject)); |
---|
| 274 | mask.include(Class(LevelInfo)); |
---|
| 275 | |
---|
| 276 | // Iterate over all the found *.oxw files |
---|
[8858] | 277 | orxout(internal_info) << "Loading LevelInfos..." << endl; |
---|
[8079] | 278 | std::set<std::string> names; |
---|
[6501] | 279 | for (Ogre::StringVector::const_iterator it = levels->begin(); it != levels->end(); ++it) |
---|
| 280 | { |
---|
[8079] | 281 | // TODO: Replace with tag? |
---|
[6501] | 282 | if (it->find("old/") != 0) |
---|
[3370] | 283 | { |
---|
[11071] | 284 | LevelInfoItem* info = nullptr; |
---|
[7648] | 285 | |
---|
[7802] | 286 | // Load the LevelInfo object from the level file. |
---|
[7648] | 287 | XMLFile file = XMLFile(*it); |
---|
[10624] | 288 | Loader::getInstance().load(&file, mask, false, true); |
---|
[8079] | 289 | |
---|
| 290 | // Find the LevelInfo object we've just loaded (if there was one) |
---|
[11071] | 291 | for(LevelInfo* levelInfo : ObjectList<LevelInfo>()) |
---|
| 292 | if(levelInfo->getXMLFilename() == *it) |
---|
| 293 | info = levelInfo->copy(); |
---|
[8079] | 294 | |
---|
| 295 | // We don't need the loaded stuff anymore |
---|
[10624] | 296 | Loader::getInstance().unload(&file); |
---|
[8079] | 297 | |
---|
[11071] | 298 | if(info == nullptr) |
---|
[7648] | 299 | { |
---|
[8079] | 300 | // Create a default LevelInfoItem object that merely contains the name |
---|
| 301 | std::string filenameWOExtension = it->substr(0, it->find(".oxw")); |
---|
| 302 | info = new LevelInfoItem(filenameWOExtension, *it); |
---|
[7648] | 303 | } |
---|
[8079] | 304 | |
---|
| 305 | // Warn about levels with the same name. |
---|
| 306 | if(!names.insert(info->getName()).second) |
---|
[8858] | 307 | orxout(internal_warning) << "Multiple levels (" << info->getXMLFilename() << ") with name '" << info->getName() << "' found!" << endl; |
---|
[8079] | 308 | |
---|
| 309 | // Warn about multiple items so that it gets fixed quickly |
---|
| 310 | if(availableLevels_.find(info) != availableLevels_.end()) |
---|
| 311 | { |
---|
[8858] | 312 | orxout(internal_warning) << "Multiple levels (" << info->getXMLFilename() << ") with same name '" << info->getName() << "' and filename found! Exluding..." << endl; |
---|
[8079] | 313 | // Delete LevelInfoItem to avoid a dangling pointer |
---|
| 314 | delete info; |
---|
| 315 | } |
---|
| 316 | else |
---|
| 317 | this->availableLevels_.insert(info); |
---|
[3370] | 318 | } |
---|
[6501] | 319 | } |
---|
[3370] | 320 | } |
---|
[7648] | 321 | |
---|
[7802] | 322 | /** |
---|
| 323 | @brief |
---|
| 324 | Update the list of available Levels. |
---|
| 325 | */ |
---|
[7648] | 326 | void LevelManager::updateAvailableLevelList(void) |
---|
| 327 | { |
---|
| 328 | //TODO: Implement some kind of update? |
---|
| 329 | } |
---|
[11206] | 330 | |
---|
| 331 | |
---|
| 332 | |
---|
| 333 | /** |
---|
| 334 | @brief |
---|
| 335 | first updates allLevelStatus and then check if the level with index i is activated |
---|
| 336 | */ |
---|
| 337 | int LevelManager::missionactivate(int index) |
---|
| 338 | { |
---|
| 339 | updateAllLevelStatus(); |
---|
| 340 | int activated = allLevelStatus_[index]->activated; |
---|
| 341 | return activated; |
---|
| 342 | } |
---|
| 343 | /** |
---|
| 344 | @brief |
---|
| 345 | update the last won mission to won=true |
---|
| 346 | */ |
---|
| 347 | |
---|
| 348 | void LevelManager::updatewon(int lastwon) |
---|
| 349 | { |
---|
| 350 | allLevelStatus_[lastwon]->won=true; |
---|
| 351 | |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | /** |
---|
| 355 | @brief |
---|
| 356 | checks if a level is won |
---|
| 357 | if a level is won, the other levels should be shown as saved in 'nextLevels' of the won level |
---|
| 358 | therefore 'activated' of all other levels will be updated to the correct value |
---|
| 359 | if more than one level is won, the level with the highes index decides which other levels will be shown, activated or not activated |
---|
| 360 | */ |
---|
| 361 | |
---|
| 362 | void LevelManager::updateAllLevelStatus() |
---|
| 363 | { |
---|
| 364 | for(unsigned int i =0;i<allLevelStatus_.size();i++) |
---|
| 365 | { |
---|
| 366 | if(allLevelStatus_[i]->won) |
---|
| 367 | { |
---|
| 368 | allLevelStatus_[i]->activated=1; |
---|
| 369 | std::vector<int> nextLevels=allLevelStatus_[i]->nextLevels; |
---|
| 370 | for(unsigned int j=0;j<allLevelStatus_.size();j++) |
---|
| 371 | { |
---|
| 372 | allLevelStatus_[j]->activated=nextLevels[j]; |
---|
| 373 | } |
---|
| 374 | } |
---|
| 375 | } |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | /** |
---|
| 379 | @brief |
---|
| 380 | the name of the last won mission is saved in the config file |
---|
| 381 | */ |
---|
| 382 | |
---|
| 383 | void LevelManager::setLevelStatus(const std::string& LevelWon) |
---|
| 384 | { |
---|
| 385 | |
---|
| 386 | |
---|
| 387 | ModifyConfigValue(lastWonMission_, set, LevelWon); |
---|
| 388 | |
---|
| 389 | /** |
---|
| 390 | TODO: save allLevelWon_ into the config file |
---|
| 391 | */ |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | |
---|
| 395 | /** |
---|
| 396 | @brief |
---|
| 397 | build up allLevelStatus_ |
---|
| 398 | has to be done once per game (not per level) |
---|
| 399 | all connections between the levels are saved in the allLevelStatus_ |
---|
| 400 | also the won variable of the LevelStatus have to be set to the corresponding allLevelWon_ values |
---|
| 401 | */ |
---|
| 402 | void LevelManager::buildallLevelStatus() |
---|
| 403 | { |
---|
| 404 | for(unsigned int i =0;i<campaignMissions_.size();++i) |
---|
| 405 | { |
---|
| 406 | LevelStatus* level= new LevelStatus(this->getContext()); |
---|
| 407 | allLevelStatus_.push_back(level); |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | /** |
---|
| 411 | TODO: allLevelWon_ should be loaded into the allLevelSatus_ such that the progress of the campaign can be saved |
---|
| 412 | */ |
---|
| 413 | |
---|
| 414 | allLevelStatus_[0]->activated=1; |
---|
| 415 | |
---|
| 416 | |
---|
| 417 | std::vector<int> v={1,1,1,0,0,0,0,0,0}; |
---|
| 418 | allLevelStatus_[0]->nextLevels=v; |
---|
| 419 | |
---|
| 420 | v={1,1,2,1,0,0,0,0,0}; |
---|
| 421 | allLevelStatus_[1]->nextLevels=v; |
---|
| 422 | |
---|
| 423 | v={1,2,1,0,1,0,0,0,0}; |
---|
| 424 | allLevelStatus_[2]->nextLevels=v; |
---|
| 425 | |
---|
| 426 | v={1,1,2,1,0,1,1,0,0}; |
---|
| 427 | allLevelStatus_[3]->nextLevels=v; |
---|
| 428 | |
---|
| 429 | v={1,2,1,0,1,0,0,1,1}; |
---|
| 430 | allLevelStatus_[4]->nextLevels=v; |
---|
| 431 | |
---|
| 432 | v={1,1,2,1,0,1,2,0,0}; |
---|
| 433 | allLevelStatus_[5]->nextLevels=v; |
---|
| 434 | |
---|
| 435 | v={1,1,2,1,0,2,1,0,0}; |
---|
| 436 | allLevelStatus_[6]->nextLevels=v; |
---|
| 437 | |
---|
| 438 | v={1,2,1,0,1,0,0,1,2}; |
---|
| 439 | allLevelStatus_[7]->nextLevels=v; |
---|
| 440 | |
---|
| 441 | v={1,2,1,0,1,0,0,2,1}; |
---|
| 442 | allLevelStatus_[8]->nextLevels=v; |
---|
| 443 | |
---|
| 444 | } |
---|
[2072] | 445 | } |
---|