[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 | |
---|
| 29 | #include "LevelManager.h" |
---|
| 30 | |
---|
[3196] | 31 | #include <map> |
---|
[3280] | 32 | |
---|
[7284] | 33 | #include "util/ScopedSingletonManager.h" |
---|
[7648] | 34 | #include "core/ClassTreeMask.h" |
---|
[6021] | 35 | #include "core/CommandLineParser.h" |
---|
[3280] | 36 | #include "core/ConfigValueIncludes.h" |
---|
| 37 | #include "core/CoreIncludes.h" |
---|
[3370] | 38 | #include "core/Loader.h" |
---|
[6417] | 39 | #include "core/Resource.h" |
---|
[7648] | 40 | #include "core/XMLFile.h" |
---|
[7802] | 41 | #include "Level.h" |
---|
[2171] | 42 | #include "PlayerManager.h" |
---|
[2072] | 43 | |
---|
| 44 | namespace orxonox |
---|
| 45 | { |
---|
[3280] | 46 | SetCommandLineArgument(level, "").shortcut("l").information("Default level file (overrides LevelManager::defaultLevelName_ configValue)"); |
---|
| 47 | |
---|
[5929] | 48 | ManageScopedSingleton(LevelManager, ScopeID::Root, false); |
---|
[2072] | 49 | |
---|
[7802] | 50 | /** |
---|
| 51 | @brief |
---|
| 52 | Constructor. Registers the object, sets config values and initializes variables. |
---|
| 53 | */ |
---|
[2072] | 54 | LevelManager::LevelManager() |
---|
| 55 | { |
---|
[3280] | 56 | RegisterRootObject(LevelManager); |
---|
| 57 | this->setConfigValues(); |
---|
| 58 | |
---|
| 59 | // check override |
---|
[6021] | 60 | if (!CommandLineParser::getArgument("level")->hasDefaultValue()) |
---|
[3280] | 61 | { |
---|
[6021] | 62 | ModifyConfigValue(defaultLevelName_, tset, CommandLineParser::getValue("level").getString()); |
---|
[3280] | 63 | } |
---|
[7648] | 64 | |
---|
| 65 | this->compileAvailableLevelList(); |
---|
[7802] | 66 | this->nextIndex_ = 0; |
---|
| 67 | this->nextLevel_ = this->availableLevels_.begin(); |
---|
[2072] | 68 | } |
---|
| 69 | |
---|
| 70 | LevelManager::~LevelManager() |
---|
| 71 | { |
---|
| 72 | } |
---|
| 73 | |
---|
[7802] | 74 | /** |
---|
| 75 | @brief |
---|
| 76 | Set the config values for this object. |
---|
| 77 | */ |
---|
[3280] | 78 | void LevelManager::setConfigValues() |
---|
| 79 | { |
---|
[7724] | 80 | SetConfigValue(defaultLevelName_, "presentationDM.oxw") |
---|
[6417] | 81 | .description("Sets the pre selection of the level in the main menu."); |
---|
[3280] | 82 | } |
---|
| 83 | |
---|
[7802] | 84 | /** |
---|
| 85 | @brief |
---|
| 86 | Request activity for the input Level. |
---|
| 87 | The Level will be added to the list of Levels whose activity is requested. The list is accessed in a FIFO manner. |
---|
| 88 | 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. |
---|
| 89 | @param level |
---|
| 90 | A pointer to the Level whose activity is requested. |
---|
| 91 | */ |
---|
[2072] | 92 | void LevelManager::requestActivity(Level* level) |
---|
| 93 | { |
---|
[7802] | 94 | assert( std::find(this->levels_.begin(), this->levels_.end(), level)==this->levels_.end() ); |
---|
| 95 | // If the level is already in list. |
---|
| 96 | if( std::find(this->levels_.begin(), this->levels_.end(), level)!=this->levels_.end() ) |
---|
| 97 | return; |
---|
| 98 | // If it isn't insert it at the back. |
---|
| 99 | this->levels_.push_back(level); |
---|
| 100 | // If it is the only level in the list activate it. |
---|
| 101 | if (this->levels_.size() == 1) |
---|
[2072] | 102 | this->activateNextLevel(); |
---|
| 103 | } |
---|
| 104 | |
---|
[7802] | 105 | /** |
---|
| 106 | @brief |
---|
| 107 | Release activity for the input Level. |
---|
| 108 | 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. |
---|
| 109 | @param level |
---|
| 110 | A pointer to the Level whose activity is to be released. |
---|
| 111 | */ |
---|
[2072] | 112 | void LevelManager::releaseActivity(Level* level) |
---|
| 113 | { |
---|
[7802] | 114 | if (this->levels_.size() > 0) |
---|
[2072] | 115 | { |
---|
[7802] | 116 | // If the level is the active level in the front of the list. |
---|
| 117 | if (this->levels_.front() == level) |
---|
[2072] | 118 | { |
---|
[7802] | 119 | // Deactivate it, remove it from the list and activate the next level in line. |
---|
[2072] | 120 | level->setActive(false); |
---|
[7802] | 121 | this->levels_.pop_front(); |
---|
[2072] | 122 | this->activateNextLevel(); |
---|
| 123 | } |
---|
[7802] | 124 | else // Else just remove it from the list. |
---|
| 125 | this->levels_.erase(std::find(this->levels_.begin(), this->levels_.end(), level)); |
---|
[2072] | 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
[7802] | 129 | /** |
---|
| 130 | @brief |
---|
| 131 | Get the currently active Level. |
---|
| 132 | @return |
---|
| 133 | Returns a pointer to the currently active level or NULL if there currently are no active Levels. |
---|
| 134 | */ |
---|
[2072] | 135 | Level* LevelManager::getActiveLevel() |
---|
| 136 | { |
---|
[7802] | 137 | if (this->levels_.size() > 0) |
---|
| 138 | return this->levels_.front(); |
---|
[2072] | 139 | else |
---|
| 140 | return 0; |
---|
| 141 | } |
---|
| 142 | |
---|
[7802] | 143 | /** |
---|
| 144 | @brief |
---|
| 145 | Activate the next Level. |
---|
| 146 | */ |
---|
[2072] | 147 | void LevelManager::activateNextLevel() |
---|
| 148 | { |
---|
[7802] | 149 | if (this->levels_.size() > 0) |
---|
[2072] | 150 | { |
---|
[7802] | 151 | // Activate the level that is the first in the list of levels whose activity has been requested. |
---|
| 152 | this->levels_.front()->setActive(true); |
---|
| 153 | // Make every player enter the newly activated level. |
---|
[2171] | 154 | for (std::map<unsigned int, PlayerInfo*>::const_iterator it = PlayerManager::getInstance().getClients().begin(); it != PlayerManager::getInstance().getClients().end(); ++it) |
---|
[7802] | 155 | this->levels_.front()->playerEntered(it->second); |
---|
[2072] | 156 | } |
---|
| 157 | } |
---|
[3280] | 158 | |
---|
[7802] | 159 | /** |
---|
| 160 | @brief |
---|
| 161 | Set the default Level. |
---|
| 162 | @param levelName |
---|
| 163 | The filename of the default Level. |
---|
| 164 | */ |
---|
[3280] | 165 | void LevelManager::setDefaultLevel(const std::string& levelName) |
---|
| 166 | { |
---|
| 167 | ModifyConfigValue(defaultLevelName_, set, levelName); |
---|
| 168 | } |
---|
| 169 | |
---|
[7802] | 170 | /** |
---|
| 171 | @brief |
---|
| 172 | Get the number of available Levels. |
---|
| 173 | Also updates the list of available Levels. |
---|
| 174 | @return |
---|
| 175 | Returns the number of available Levels. |
---|
| 176 | */ |
---|
[7648] | 177 | unsigned int LevelManager::getNumberOfLevels() |
---|
[3370] | 178 | { |
---|
[7648] | 179 | this->updateAvailableLevelList(); |
---|
| 180 | |
---|
| 181 | return this->availableLevels_.size(); |
---|
| 182 | } |
---|
| 183 | |
---|
[7802] | 184 | /** |
---|
| 185 | @brief |
---|
| 186 | Get the LevelInfoItem at the given index in the list of available Levels. |
---|
| 187 | The LevelInfoItems are sorted in alphabetical order accoridng to the name of the Level. |
---|
| 188 | This method is most efficiently called with consecutive indices (or at least ascending indices). |
---|
| 189 | @return |
---|
| 190 | Returns a pointer to the LevelInfoItem at the given index. |
---|
| 191 | */ |
---|
| 192 | LevelInfoItem* LevelManager::getAvailableLevelListItem(unsigned int index) |
---|
[7648] | 193 | { |
---|
| 194 | if (index >= this->availableLevels_.size()) |
---|
| 195 | return NULL; |
---|
[7802] | 196 | |
---|
| 197 | // If this index directly follows the last we can optimize a lot. |
---|
| 198 | if(index == this->nextIndex_) |
---|
| 199 | { |
---|
| 200 | this->nextIndex_++; |
---|
| 201 | std::set<LevelInfoItem*, LevelInfoCompare>::iterator it = this->nextLevel_; |
---|
| 202 | this->nextLevel_++; |
---|
| 203 | return *it; |
---|
| 204 | } |
---|
[3370] | 205 | else |
---|
[7648] | 206 | { |
---|
[7802] | 207 | // If this index is bigger than the last, we can optimize a little. |
---|
| 208 | if(index > this->nextIndex_) |
---|
| 209 | { |
---|
| 210 | this->nextIndex_ = 0; |
---|
| 211 | this->nextLevel_ = this->availableLevels_.begin(); |
---|
| 212 | } |
---|
| 213 | while(this->nextIndex_ != index) |
---|
| 214 | { |
---|
| 215 | this->nextIndex_++; |
---|
| 216 | this->nextLevel_++; |
---|
| 217 | } |
---|
| 218 | this->nextIndex_++; |
---|
| 219 | std::set<LevelInfoItem*, LevelInfoCompare>::iterator it = this->nextLevel_; |
---|
| 220 | this->nextLevel_++; |
---|
| 221 | return *it; |
---|
[7648] | 222 | } |
---|
[3370] | 223 | } |
---|
| 224 | |
---|
[7802] | 225 | /** |
---|
| 226 | @brief |
---|
| 227 | Compile the list of available Levels. |
---|
| 228 | Iterates over all *.oxw files, loads the LevelInfo objects in them and from that it creates the LevelInfoItems which are inserted in a list. |
---|
| 229 | */ |
---|
[3370] | 230 | void LevelManager::compileAvailableLevelList() |
---|
| 231 | { |
---|
[6501] | 232 | Ogre::StringVectorPtr levels = Resource::findResourceNames("*.oxw"); |
---|
[7648] | 233 | // Iterate over all *.oxw level files. |
---|
[7801] | 234 | COUT(3) << "Loading LevelInfos..." << std::endl; |
---|
[6501] | 235 | for (Ogre::StringVector::const_iterator it = levels->begin(); it != levels->end(); ++it) |
---|
| 236 | { |
---|
[7802] | 237 | //TODO: Replace with tag? |
---|
[6501] | 238 | if (it->find("old/") != 0) |
---|
[3370] | 239 | { |
---|
[5695] | 240 | size_t pos = it->find(".oxw"); |
---|
[7648] | 241 | |
---|
[7802] | 242 | // Load the LevelInfo object from the level file. |
---|
[7648] | 243 | bool infoExists = false; |
---|
| 244 | XMLFile file = XMLFile(*it); |
---|
| 245 | ClassTreeMask mask = ClassTreeMask(); |
---|
| 246 | mask.exclude(ClassIdentifier<BaseObject>::getIdentifier()); |
---|
| 247 | mask.include(ClassIdentifier<LevelInfo>::getIdentifier()); |
---|
| 248 | Loader::load(&file, mask, false); |
---|
[7802] | 249 | // Iterate over all LevelInfos. |
---|
[7648] | 250 | for(ObjectList<LevelInfo>::iterator item = ObjectList<LevelInfo>::begin(); item != ObjectList<LevelInfo>::end(); ++item) |
---|
| 251 | { |
---|
| 252 | LevelInfoItem* info = item->copy(); |
---|
[7802] | 253 | if(info->getXMLFilename() == *it) // If the LevelInfo for this level exists we insert it into the list of available levels. |
---|
[7648] | 254 | { |
---|
[7802] | 255 | this->availableLevels_.insert(info); |
---|
[7648] | 256 | infoExists = true; |
---|
| 257 | } |
---|
| 258 | } |
---|
| 259 | Loader::unload(&file, mask); |
---|
[7802] | 260 | if(!infoExists) // If the LevelInfo for this level doesn't exist, we create a new one and insert it into the list of available levels. |
---|
| 261 | this->availableLevels_.insert(new LevelInfoItem(it->substr(0, pos), *it)); |
---|
[3370] | 262 | } |
---|
[6501] | 263 | } |
---|
[3370] | 264 | } |
---|
[7648] | 265 | |
---|
[7802] | 266 | /** |
---|
| 267 | @brief |
---|
| 268 | Update the list of available Levels. |
---|
| 269 | */ |
---|
[7648] | 270 | void LevelManager::updateAvailableLevelList(void) |
---|
| 271 | { |
---|
| 272 | //TODO: Implement some kind of update? |
---|
| 273 | } |
---|
[2072] | 274 | } |
---|