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