| [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" | 
|---|
 | 32 | #include "core/Core.h" | 
|---|
| [2072] | 33 | #include "core/CoreIncludes.h" | 
|---|
 | 34 | #include "core/Loader.h" | 
|---|
| [3196] | 35 | #include "core/Template.h" | 
|---|
| [2072] | 36 | #include "core/XMLFile.h" | 
|---|
| [3196] | 37 | #include "core/XMLPort.h" | 
|---|
| [2072] | 38 |  | 
|---|
| [2169] | 39 | #include "objects/infos/PlayerInfo.h" | 
|---|
| [2072] | 40 | #include "objects/gametypes/Gametype.h" | 
|---|
| [2168] | 41 | #include "overlays/OverlayGroup.h" | 
|---|
| [3060] | 42 | #include "sound/SoundBase.h" | 
|---|
| [3196] | 43 | #include "LevelManager.h" | 
|---|
| [2072] | 44 |  | 
|---|
 | 45 | namespace orxonox | 
|---|
 | 46 | { | 
|---|
 | 47 |     CreateFactory(Level); | 
|---|
 | 48 |  | 
|---|
| [2169] | 49 |     Level::Level(BaseObject* creator) : BaseObject(creator), Synchronisable(creator) | 
|---|
| [2072] | 50 |     { | 
|---|
 | 51 |         RegisterObject(Level); | 
|---|
 | 52 |  | 
|---|
 | 53 |         this->registerVariables(); | 
|---|
 | 54 |         this->xmlfilename_ = this->getFilename(); | 
|---|
 | 55 |  | 
|---|
| [2710] | 56 |         if (this->xmlfilename_.length() >= Core::getMediaPathString().length()) | 
|---|
 | 57 |             this->xmlfilename_ = this->xmlfilename_.substr(Core::getMediaPathString().length()); | 
|---|
| [2072] | 58 |     } | 
|---|
 | 59 |  | 
|---|
 | 60 |     Level::~Level() | 
|---|
 | 61 |     { | 
|---|
 | 62 |         if (this->isInitialized()) | 
|---|
 | 63 |         { | 
|---|
| [2168] | 64 |             if (LevelManager::getInstancePtr()) | 
|---|
 | 65 |                 LevelManager::getInstance().releaseActivity(this); | 
|---|
| [2072] | 66 |  | 
|---|
 | 67 |             if (this->xmlfile_) | 
|---|
 | 68 |                 Loader::unload(this->xmlfile_); | 
|---|
| [3060] | 69 |  | 
|---|
 | 70 |             if(this->ambientsound_ != NULL) | 
|---|
 | 71 |                 delete this->ambientsound_; | 
|---|
| [2072] | 72 |         } | 
|---|
 | 73 |     } | 
|---|
 | 74 |  | 
|---|
 | 75 |     void Level::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
 | 76 |     { | 
|---|
 | 77 |         SUPER(Level, XMLPort, xmlelement, mode); | 
|---|
 | 78 |  | 
|---|
 | 79 |         XMLPortParam(Level, "description", setDescription, getDescription, xmlelement, mode); | 
|---|
 | 80 |         XMLPortParam(Level, "gametype", setGametypeString, getGametypeString, xmlelement, mode).defaultValues("Gametype"); | 
|---|
 | 81 |  | 
|---|
| [3060] | 82 |         XMLPortParamLoadOnly(Level, "ambientsound", loadAmbientSound, xmlelement, mode); | 
|---|
 | 83 |  | 
|---|
| [2072] | 84 |         XMLPortObjectExtended(Level, BaseObject, "", addObject, getObject, xmlelement, mode, true, false); | 
|---|
 | 85 |     } | 
|---|
 | 86 |  | 
|---|
 | 87 |     void Level::registerVariables() | 
|---|
 | 88 |     { | 
|---|
| [3280] | 89 |         registerVariable(this->xmlfilename_, VariableDirection::ToClient, new NetworkCallback<Level>(this, &Level::networkcallback_applyXMLFile)); | 
|---|
 | 90 |         registerVariable(this->name_,        VariableDirection::ToClient, new NetworkCallback<Level>(this, &Level::changedName)); | 
|---|
 | 91 |         registerVariable(this->description_, VariableDirection::ToClient); | 
|---|
| [2072] | 92 |     } | 
|---|
 | 93 |  | 
|---|
 | 94 |     void Level::networkcallback_applyXMLFile() | 
|---|
 | 95 |     { | 
|---|
 | 96 |         COUT(0) << "Loading level \"" << this->xmlfilename_ << "\"..." << std::endl; | 
|---|
 | 97 |  | 
|---|
 | 98 |         ClassTreeMask mask; | 
|---|
 | 99 |         mask.exclude(Class(BaseObject)); | 
|---|
 | 100 |         mask.include(Class(Template)); | 
|---|
| [2168] | 101 |         mask.include(Class(OverlayGroup)); // HACK to include the ChatOverlay | 
|---|
| [2072] | 102 |  | 
|---|
| [2710] | 103 |         this->xmlfile_ = new XMLFile(Core::getMediaPathString() + this->xmlfilename_, mask); | 
|---|
| [2072] | 104 |  | 
|---|
 | 105 |         Loader::open(this->xmlfile_); | 
|---|
 | 106 |     } | 
|---|
 | 107 |  | 
|---|
 | 108 |     void Level::setGametypeString(const std::string& gametype) | 
|---|
 | 109 |     { | 
|---|
 | 110 |         Identifier* identifier = ClassByString(gametype); | 
|---|
| [2826] | 111 |  | 
|---|
 | 112 |         if (!identifier || !identifier->isA(Class(Gametype))) | 
|---|
| [2072] | 113 |         { | 
|---|
| [2826] | 114 |             COUT(0) << "Error: \"" << gametype << "\" is not a valid gametype." << std::endl; | 
|---|
 | 115 |             identifier = Class(Gametype); | 
|---|
 | 116 |             this->gametype_ = "Gametype"; | 
|---|
 | 117 |         } | 
|---|
 | 118 |         else | 
|---|
| [2072] | 119 |             this->gametype_ = gametype; | 
|---|
 | 120 |  | 
|---|
| [2826] | 121 | std::cout << "Load Gametype: " << this->gametype_ << std::endl; | 
|---|
| [2072] | 122 |  | 
|---|
| [3325] | 123 |         Gametype* rootgametype = orxonox_cast<Gametype*>(identifier->fabricate(this)); | 
|---|
| [2826] | 124 |         this->setGametype(rootgametype); | 
|---|
| [2072] | 125 |  | 
|---|
| [2826] | 126 | std::cout << "root gametype: " << rootgametype->getIdentifier()->getName() << std::endl; | 
|---|
 | 127 |  | 
|---|
 | 128 |         for (std::list<BaseObject*>::iterator it = this->objects_.begin(); it != this->objects_.end(); ++it) | 
|---|
 | 129 |             (*it)->setGametype(rootgametype); | 
|---|
 | 130 |  | 
|---|
 | 131 |         if (LevelManager::getInstancePtr()) | 
|---|
 | 132 |             LevelManager::getInstance().requestActivity(this); | 
|---|
| [2072] | 133 |     } | 
|---|
 | 134 |  | 
|---|
 | 135 |  | 
|---|
 | 136 |     void Level::addObject(BaseObject* object) | 
|---|
 | 137 |     { | 
|---|
 | 138 |         this->objects_.push_back(object); | 
|---|
 | 139 |         object->setGametype(this->getGametype()); | 
|---|
 | 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 |  | 
|---|
| [3060] | 154 |     void Level::loadAmbientSound(const std::string& filename) | 
|---|
 | 155 |     { | 
|---|
 | 156 |         if(filename == "") return; | 
|---|
 | 157 |         else | 
|---|
 | 158 |         { | 
|---|
 | 159 |             if(this->ambientsound_ == NULL) | 
|---|
 | 160 |             { | 
|---|
 | 161 |                 this->ambientsound_ = new SoundBase(); | 
|---|
 | 162 |             } | 
|---|
 | 163 |  | 
|---|
 | 164 |             this->ambientsound_->loadFile(filename); | 
|---|
 | 165 |             this->ambientsound_->play(true); | 
|---|
 | 166 |         } | 
|---|
 | 167 |     } | 
|---|
 | 168 |  | 
|---|
| [2072] | 169 |     void Level::playerEntered(PlayerInfo* player) | 
|---|
 | 170 |     { | 
|---|
| [2662] | 171 |         COUT(3) << "player entered level (id: " << player->getClientID() << ", name: " << player->getName() << ")" << std::endl; | 
|---|
| [2072] | 172 |         player->setGametype(this->getGametype()); | 
|---|
 | 173 |     } | 
|---|
 | 174 |  | 
|---|
 | 175 |     void Level::playerLeft(PlayerInfo* player) | 
|---|
 | 176 |     { | 
|---|
| [2662] | 177 |         COUT(3) << "player left level (id: " << player->getClientID() << ", name: " << player->getName() << ")" << std::endl; | 
|---|
| [2072] | 178 |         player->setGametype(0); | 
|---|
 | 179 |     } | 
|---|
 | 180 | } | 
|---|