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