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