| [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: | 
|---|
 | 25 |  *      ... | 
|---|
 | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
 | 28 |  | 
|---|
 | 29 | #include "Level.h" | 
|---|
 | 30 |  | 
|---|
| [3196] | 31 | #include "util/Math.h" | 
|---|
| [2072] | 32 | #include "core/CoreIncludes.h" | 
|---|
 | 33 | #include "core/Loader.h" | 
|---|
| [3196] | 34 | #include "core/Template.h" | 
|---|
| [2072] | 35 | #include "core/XMLFile.h" | 
|---|
| [3196] | 36 | #include "core/XMLPort.h" | 
|---|
| [10624] | 37 | #include "core/module/PluginReference.h" | 
|---|
| [2072] | 38 |  | 
|---|
| [5735] | 39 | #include "infos/PlayerInfo.h" | 
|---|
 | 40 | #include "gametypes/Gametype.h" | 
|---|
| [2168] | 41 | #include "overlays/OverlayGroup.h" | 
|---|
| [3196] | 42 | #include "LevelManager.h" | 
|---|
| [2072] | 43 |  | 
|---|
 | 44 | namespace orxonox | 
|---|
 | 45 | { | 
|---|
| [9667] | 46 |     RegisterClass(Level); | 
|---|
| [2072] | 47 |  | 
|---|
| [9667] | 48 |     Level::Level(Context* context) : BaseObject(context), Synchronisable(context), Context(context) | 
|---|
| [2072] | 49 |     { | 
|---|
 | 50 |         RegisterObject(Level); | 
|---|
 | 51 |  | 
|---|
| [10624] | 52 |         this->setLevel(WeakPtr<Level>(this)); // store a weak-pointer to itself (a strong-pointer would create a recursive dependency) | 
|---|
| [7163] | 53 |  | 
|---|
| [2072] | 54 |         this->registerVariables(); | 
|---|
 | 55 |         this->xmlfilename_ = this->getFilename(); | 
|---|
| [5929] | 56 |         this->xmlfile_ = 0; | 
|---|
| [2072] | 57 |     } | 
|---|
 | 58 |  | 
|---|
 | 59 |     Level::~Level() | 
|---|
 | 60 |     { | 
|---|
 | 61 |         if (this->isInitialized()) | 
|---|
 | 62 |         { | 
|---|
| [6746] | 63 |             if (LevelManager::exists()) | 
|---|
| [2168] | 64 |                 LevelManager::getInstance().releaseActivity(this); | 
|---|
| [2072] | 65 |  | 
|---|
 | 66 |             if (this->xmlfile_) | 
|---|
| [10624] | 67 |                 Loader::getInstance().unload(this->xmlfile_); | 
|---|
 | 68 |  | 
|---|
 | 69 |             this->unloadPlugins(); | 
|---|
| [2072] | 70 |         } | 
|---|
 | 71 |     } | 
|---|
 | 72 |  | 
|---|
 | 73 |     void Level::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
 | 74 |     { | 
|---|
 | 75 |         SUPER(Level, XMLPort, xmlelement, mode); | 
|---|
 | 76 |  | 
|---|
| [10624] | 77 |         XMLPortParam(Level, "plugins",  setPluginsString,  getPluginsString,  xmlelement, mode); | 
|---|
| [2072] | 78 |         XMLPortParam(Level, "gametype", setGametypeString, getGametypeString, xmlelement, mode).defaultValues("Gametype"); | 
|---|
 | 79 |  | 
|---|
| [7163] | 80 |         XMLPortObject(Level, MeshLodInformation, "lodinformation", addLodInfo, getLodInfo, xmlelement, mode); | 
|---|
| [2072] | 81 |         XMLPortObjectExtended(Level, BaseObject, "", addObject, getObject, xmlelement, mode, true, false); | 
|---|
| [7648] | 82 |     } | 
|---|
| [2072] | 83 |  | 
|---|
 | 84 |     void Level::registerVariables() | 
|---|
 | 85 |     { | 
|---|
| [7163] | 86 |         registerVariable(this->xmlfilename_,            VariableDirection::ToClient, new NetworkCallback<Level>(this, &Level::networkcallback_applyXMLFile)); | 
|---|
 | 87 |         registerVariable(this->name_,                   VariableDirection::ToClient, new NetworkCallback<Level>(this, &Level::changedName)); | 
|---|
 | 88 |         registerVariable(this->networkTemplateNames_,   VariableDirection::ToClient, new NetworkCallback<Level>(this, &Level::networkCallbackTemplatesChanged)); | 
|---|
| [2072] | 89 |     } | 
|---|
 | 90 |  | 
|---|
 | 91 |     void Level::networkcallback_applyXMLFile() | 
|---|
 | 92 |     { | 
|---|
| [8858] | 93 |         orxout(user_status) << "Loading level \"" << this->xmlfilename_ << "\"..." << endl; | 
|---|
| [2072] | 94 |  | 
|---|
 | 95 |         ClassTreeMask mask; | 
|---|
 | 96 |         mask.exclude(Class(BaseObject)); | 
|---|
 | 97 |         mask.include(Class(Template)); | 
|---|
| [2168] | 98 |         mask.include(Class(OverlayGroup)); // HACK to include the ChatOverlay | 
|---|
| [2072] | 99 |  | 
|---|
| [5695] | 100 |         this->xmlfile_ = new XMLFile(mask, this->xmlfilename_); | 
|---|
| [2072] | 101 |  | 
|---|
| [10624] | 102 |         Loader::getInstance().load(this->xmlfile_); | 
|---|
| [2072] | 103 |     } | 
|---|
 | 104 |  | 
|---|
| [7163] | 105 |     void Level::networkCallbackTemplatesChanged() | 
|---|
 | 106 |     { | 
|---|
 | 107 |         for( std::set<std::string>::iterator it = this->networkTemplateNames_.begin(); it!=this->networkTemplateNames_.end(); ++it ) | 
|---|
 | 108 |         { | 
|---|
 | 109 |             assert(Template::getTemplate(*it)); | 
|---|
 | 110 |             Template::getTemplate(*it)->applyOn(this); | 
|---|
 | 111 |         } | 
|---|
 | 112 |     } | 
|---|
 | 113 |  | 
|---|
| [10624] | 114 |     void Level::setPluginsString(const std::string& pluginsString) | 
|---|
 | 115 |     { | 
|---|
 | 116 |         // unload old plugins | 
|---|
 | 117 |         this->unloadPlugins(); | 
|---|
 | 118 |  | 
|---|
 | 119 |         // load new plugins | 
|---|
 | 120 |         this->pluginsString_ = pluginsString; | 
|---|
 | 121 |         SubString tokens(pluginsString, ","); | 
|---|
 | 122 |         for (size_t i = 0; i < tokens.size(); ++i) | 
|---|
 | 123 |             this->plugins_.push_back(new PluginReference(tokens[i])); | 
|---|
 | 124 |     } | 
|---|
 | 125 |  | 
|---|
 | 126 |     void Level::unloadPlugins() | 
|---|
 | 127 |     { | 
|---|
 | 128 |         // use destroyLater() - this ensures that plugins are not unloaded too early. | 
|---|
 | 129 |         // Note: When a level gets unloaded, the Level object is usually the last object that gets destroyed. This is because all other | 
|---|
 | 130 |         //       objects inside a level have a StrongPtr (in BaseObject) that references the Level object. This means that the Level | 
|---|
 | 131 |         //       object is only destroyed, when all StrongPtrs that pointed to it were destroyed. But at the time when the last StrongPtr | 
|---|
 | 132 |         //       is destroyed, the other object is not yet fully destroyed because the StrongPtr is destroyed in ~BaseObject (and this | 
|---|
 | 133 |         //       means that e.g. ~Identifiable was not yet called for this object). This means that technically there are still other | 
|---|
 | 134 |         //       objects alive when ~Level is called. This is the reason why we cannot directly destroy() the Plugins - instead we need | 
|---|
 | 135 |         //       to call destroyLater() to ensure that no instances from this plugin exist anymore. | 
|---|
 | 136 |         for (std::list<PluginReference*>::iterator it = this->plugins_.begin(); it != this->plugins_.end(); ++it) | 
|---|
 | 137 |             (*it)->destroyLater(); | 
|---|
 | 138 |         this->plugins_.clear(); | 
|---|
 | 139 |     } | 
|---|
 | 140 |  | 
|---|
| [2072] | 141 |     void Level::setGametypeString(const std::string& gametype) | 
|---|
 | 142 |     { | 
|---|
 | 143 |         Identifier* identifier = ClassByString(gametype); | 
|---|
| [2826] | 144 |  | 
|---|
 | 145 |         if (!identifier || !identifier->isA(Class(Gametype))) | 
|---|
| [2072] | 146 |         { | 
|---|
| [8858] | 147 |             orxout(internal_error) << "\"" << gametype << "\" is not a valid gametype." << endl; | 
|---|
| [2826] | 148 |             identifier = Class(Gametype); | 
|---|
 | 149 |             this->gametype_ = "Gametype"; | 
|---|
 | 150 |         } | 
|---|
 | 151 |         else | 
|---|
| [2072] | 152 |             this->gametype_ = gametype; | 
|---|
 | 153 |  | 
|---|
| [3325] | 154 |         Gametype* rootgametype = orxonox_cast<Gametype*>(identifier->fabricate(this)); | 
|---|
| [2072] | 155 |  | 
|---|
| [10624] | 156 |         // store a weak-pointer to the gametype to avoid a circular dependency between this level and the gametype (which has a strong-reference on this level) | 
|---|
 | 157 |         this->setGametype(WeakPtr<Gametype>(rootgametype)); | 
|---|
| [2826] | 158 |  | 
|---|
| [10624] | 159 |         rootgametype->init(); // call init() AFTER the gametype was set | 
|---|
 | 160 |  | 
|---|
| [6746] | 161 |         if (LevelManager::exists()) | 
|---|
| [2826] | 162 |             LevelManager::getInstance().requestActivity(this); | 
|---|
| [2072] | 163 |     } | 
|---|
 | 164 |  | 
|---|
 | 165 |  | 
|---|
 | 166 |     void Level::addObject(BaseObject* object) | 
|---|
 | 167 |     { | 
|---|
 | 168 |         this->objects_.push_back(object); | 
|---|
 | 169 |     } | 
|---|
 | 170 |  | 
|---|
 | 171 |     BaseObject* Level::getObject(unsigned int index) const | 
|---|
 | 172 |     { | 
|---|
 | 173 |         unsigned int i = 0; | 
|---|
 | 174 |         for (std::list<BaseObject*>::const_iterator it = this->objects_.begin(); it != this->objects_.end(); ++it) | 
|---|
 | 175 |         { | 
|---|
 | 176 |             if (i == index) | 
|---|
 | 177 |                 return (*it); | 
|---|
 | 178 |             ++i; | 
|---|
 | 179 |         } | 
|---|
 | 180 |         return 0; | 
|---|
 | 181 |     } | 
|---|
 | 182 |  | 
|---|
| [7163] | 183 |     void Level::addLodInfo(MeshLodInformation* lodInformation) | 
|---|
 | 184 |     { | 
|---|
 | 185 |         std::string meshName = lodInformation->getMeshName(); | 
|---|
 | 186 | //         this->lodInformation_.insert(std::make_pair(meshName,lodInformation)); | 
|---|
 | 187 |         if( this->lodInformation_.find(meshName) != this->lodInformation_.end()) | 
|---|
| [8858] | 188 |             orxout(verbose, context::lod) << "replacing lod information for " << meshName << endl; | 
|---|
| [7163] | 189 |         this->lodInformation_[meshName] = lodInformation; | 
|---|
 | 190 |     } | 
|---|
 | 191 |  | 
|---|
 | 192 |     MeshLodInformation* Level::getLodInfo(std::string meshName) const | 
|---|
 | 193 |     { | 
|---|
 | 194 |         if(this->lodInformation_.find(meshName)!=this->lodInformation_.end()) | 
|---|
 | 195 |             return this->lodInformation_.find(meshName)->second; | 
|---|
 | 196 |  | 
|---|
 | 197 |         return 0; | 
|---|
 | 198 |     } | 
|---|
 | 199 |  | 
|---|
| [2072] | 200 |     void Level::playerEntered(PlayerInfo* player) | 
|---|
 | 201 |     { | 
|---|
| [8858] | 202 |         orxout(internal_info) << "player entered level (id: " << player->getClientID() << ", name: " << player->getName() << ')' << endl; | 
|---|
| [10624] | 203 |         player->switchGametype(this->getGametype()); | 
|---|
| [2072] | 204 |     } | 
|---|
 | 205 |  | 
|---|
 | 206 |     void Level::playerLeft(PlayerInfo* player) | 
|---|
 | 207 |     { | 
|---|
| [8858] | 208 |         orxout(internal_info) << "player left level (id: " << player->getClientID() << ", name: " << player->getName() << ')' << endl; | 
|---|
| [10624] | 209 |         player->switchGametype(0); | 
|---|
| [2072] | 210 |     } | 
|---|
 | 211 | } | 
|---|