| [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" | 
|---|
| [2072] | 37 |  | 
|---|
| [5735] | 38 | #include "infos/PlayerInfo.h" | 
|---|
 | 39 | #include "gametypes/Gametype.h" | 
|---|
| [2168] | 40 | #include "overlays/OverlayGroup.h" | 
|---|
| [3196] | 41 | #include "LevelManager.h" | 
|---|
| [2072] | 42 |  | 
|---|
 | 43 | namespace orxonox | 
|---|
 | 44 | { | 
|---|
 | 45 |     CreateFactory(Level); | 
|---|
 | 46 |  | 
|---|
| [2169] | 47 |     Level::Level(BaseObject* creator) : BaseObject(creator), Synchronisable(creator) | 
|---|
| [2072] | 48 |     { | 
|---|
 | 49 |         RegisterObject(Level); | 
|---|
 | 50 |  | 
|---|
| [7163] | 51 |  | 
|---|
| [2072] | 52 |         this->registerVariables(); | 
|---|
 | 53 |         this->xmlfilename_ = this->getFilename(); | 
|---|
| [5929] | 54 |         this->xmlfile_ = 0; | 
|---|
| [2072] | 55 |     } | 
|---|
 | 56 |  | 
|---|
 | 57 |     Level::~Level() | 
|---|
 | 58 |     { | 
|---|
 | 59 |         if (this->isInitialized()) | 
|---|
 | 60 |         { | 
|---|
| [6746] | 61 |             if (LevelManager::exists()) | 
|---|
| [2168] | 62 |                 LevelManager::getInstance().releaseActivity(this); | 
|---|
| [2072] | 63 |  | 
|---|
 | 64 |             if (this->xmlfile_) | 
|---|
 | 65 |                 Loader::unload(this->xmlfile_); | 
|---|
 | 66 |         } | 
|---|
 | 67 |     } | 
|---|
 | 68 |  | 
|---|
 | 69 |     void Level::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
 | 70 |     { | 
|---|
 | 71 |         SUPER(Level, XMLPort, xmlelement, mode); | 
|---|
 | 72 |  | 
|---|
 | 73 |         XMLPortParam(Level, "description", setDescription, getDescription, xmlelement, mode); | 
|---|
 | 74 |         XMLPortParam(Level, "gametype", setGametypeString, getGametypeString, xmlelement, mode).defaultValues("Gametype"); | 
|---|
 | 75 |  | 
|---|
| [7163] | 76 |         XMLPortObject(Level, MeshLodInformation, "lodinformation", addLodInfo, getLodInfo, xmlelement, mode); | 
|---|
| [2072] | 77 |         XMLPortObjectExtended(Level, BaseObject, "", addObject, getObject, xmlelement, mode, true, false); | 
|---|
| [7163] | 78 | } | 
|---|
| [2072] | 79 |  | 
|---|
 | 80 |     void Level::registerVariables() | 
|---|
 | 81 |     { | 
|---|
| [7163] | 82 |         registerVariable(this->xmlfilename_,            VariableDirection::ToClient, new NetworkCallback<Level>(this, &Level::networkcallback_applyXMLFile)); | 
|---|
 | 83 |         registerVariable(this->name_,                   VariableDirection::ToClient, new NetworkCallback<Level>(this, &Level::changedName)); | 
|---|
 | 84 |         registerVariable(this->description_,            VariableDirection::ToClient); | 
|---|
 | 85 |         registerVariable(this->networkTemplateNames_,   VariableDirection::ToClient, new NetworkCallback<Level>(this, &Level::networkCallbackTemplatesChanged)); | 
|---|
| [2072] | 86 |     } | 
|---|
 | 87 |  | 
|---|
 | 88 |     void Level::networkcallback_applyXMLFile() | 
|---|
 | 89 |     { | 
|---|
 | 90 |         COUT(0) << "Loading level \"" << this->xmlfilename_ << "\"..." << std::endl; | 
|---|
 | 91 |  | 
|---|
 | 92 |         ClassTreeMask mask; | 
|---|
 | 93 |         mask.exclude(Class(BaseObject)); | 
|---|
 | 94 |         mask.include(Class(Template)); | 
|---|
| [2168] | 95 |         mask.include(Class(OverlayGroup)); // HACK to include the ChatOverlay | 
|---|
| [2072] | 96 |  | 
|---|
| [5695] | 97 |         this->xmlfile_ = new XMLFile(mask, this->xmlfilename_); | 
|---|
| [2072] | 98 |  | 
|---|
 | 99 |         Loader::open(this->xmlfile_); | 
|---|
 | 100 |     } | 
|---|
 | 101 |  | 
|---|
| [7163] | 102 |     void Level::networkCallbackTemplatesChanged() | 
|---|
 | 103 |     { | 
|---|
 | 104 |         for( std::set<std::string>::iterator it = this->networkTemplateNames_.begin(); it!=this->networkTemplateNames_.end(); ++it ) | 
|---|
 | 105 |         { | 
|---|
 | 106 |             assert(Template::getTemplate(*it)); | 
|---|
 | 107 |             Template::getTemplate(*it)->applyOn(this); | 
|---|
 | 108 |         } | 
|---|
 | 109 |     } | 
|---|
 | 110 |  | 
|---|
| [2072] | 111 |     void Level::setGametypeString(const std::string& gametype) | 
|---|
 | 112 |     { | 
|---|
 | 113 |         Identifier* identifier = ClassByString(gametype); | 
|---|
| [2826] | 114 |  | 
|---|
 | 115 |         if (!identifier || !identifier->isA(Class(Gametype))) | 
|---|
| [2072] | 116 |         { | 
|---|
| [2826] | 117 |             COUT(0) << "Error: \"" << gametype << "\" is not a valid gametype." << std::endl; | 
|---|
 | 118 |             identifier = Class(Gametype); | 
|---|
 | 119 |             this->gametype_ = "Gametype"; | 
|---|
 | 120 |         } | 
|---|
 | 121 |         else | 
|---|
| [2072] | 122 |             this->gametype_ = gametype; | 
|---|
 | 123 |  | 
|---|
| [3325] | 124 |         Gametype* rootgametype = orxonox_cast<Gametype*>(identifier->fabricate(this)); | 
|---|
| [2826] | 125 |         this->setGametype(rootgametype); | 
|---|
| [2072] | 126 |  | 
|---|
| [2826] | 127 |         for (std::list<BaseObject*>::iterator it = this->objects_.begin(); it != this->objects_.end(); ++it) | 
|---|
 | 128 |             (*it)->setGametype(rootgametype); | 
|---|
 | 129 |  | 
|---|
| [6746] | 130 |         if (LevelManager::exists()) | 
|---|
| [2826] | 131 |             LevelManager::getInstance().requestActivity(this); | 
|---|
| [2072] | 132 |     } | 
|---|
 | 133 |  | 
|---|
 | 134 |  | 
|---|
 | 135 |     void Level::addObject(BaseObject* object) | 
|---|
 | 136 |     { | 
|---|
 | 137 |         this->objects_.push_back(object); | 
|---|
 | 138 |         object->setGametype(this->getGametype()); | 
|---|
| [7163] | 139 |         object->setLevel(this); | 
|---|
| [2072] | 140 |     } | 
|---|
 | 141 |  | 
|---|
 | 142 |     BaseObject* Level::getObject(unsigned int index) const | 
|---|
 | 143 |     { | 
|---|
 | 144 |         unsigned int i = 0; | 
|---|
 | 145 |         for (std::list<BaseObject*>::const_iterator it = this->objects_.begin(); it != this->objects_.end(); ++it) | 
|---|
 | 146 |         { | 
|---|
 | 147 |             if (i == index) | 
|---|
 | 148 |                 return (*it); | 
|---|
 | 149 |             ++i; | 
|---|
 | 150 |         } | 
|---|
 | 151 |         return 0; | 
|---|
 | 152 |     } | 
|---|
 | 153 |  | 
|---|
| [7163] | 154 |     void Level::addLodInfo(MeshLodInformation* lodInformation) | 
|---|
 | 155 |     { | 
|---|
 | 156 |         std::string meshName = lodInformation->getMeshName(); | 
|---|
 | 157 | //         this->lodInformation_.insert(std::make_pair(meshName,lodInformation)); | 
|---|
 | 158 |         if( this->lodInformation_.find(meshName) != this->lodInformation_.end()) | 
|---|
 | 159 |           CCOUT(4) << "replacing lod information for " << meshName << endl; | 
|---|
 | 160 |         this->lodInformation_[meshName] = lodInformation; | 
|---|
 | 161 |     } | 
|---|
 | 162 |  | 
|---|
 | 163 |     MeshLodInformation* Level::getLodInfo(std::string meshName) const | 
|---|
 | 164 |     { | 
|---|
 | 165 |         if(this->lodInformation_.find(meshName)!=this->lodInformation_.end()) | 
|---|
 | 166 |             return this->lodInformation_.find(meshName)->second; | 
|---|
 | 167 |  | 
|---|
 | 168 |         return 0; | 
|---|
 | 169 |     } | 
|---|
 | 170 |  | 
|---|
| [2072] | 171 |     void Level::playerEntered(PlayerInfo* player) | 
|---|
 | 172 |     { | 
|---|
| [6417] | 173 |         COUT(3) << "player entered level (id: " << player->getClientID() << ", name: " << player->getName() << ')' << std::endl; | 
|---|
| [2072] | 174 |         player->setGametype(this->getGametype()); | 
|---|
 | 175 |     } | 
|---|
 | 176 |  | 
|---|
 | 177 |     void Level::playerLeft(PlayerInfo* player) | 
|---|
 | 178 |     { | 
|---|
| [6417] | 179 |         COUT(3) << "player left level (id: " << player->getClientID() << ", name: " << player->getName() << ')' << std::endl; | 
|---|
| [2072] | 180 |         player->setGametype(0); | 
|---|
 | 181 |     } | 
|---|
 | 182 | } | 
|---|