[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 | { |
---|
[9667] | 45 | RegisterClass(Level); |
---|
[2072] | 46 | |
---|
[9667] | 47 | Level::Level(Context* context) : BaseObject(context), Synchronisable(context), Context(context) |
---|
[2072] | 48 | { |
---|
| 49 | RegisterObject(Level); |
---|
| 50 | |
---|
[10578] | 51 | this->setLevel(WeakPtr<Level>(this)); // store a weak-pointer to itself (a strong-pointer would create a recursive dependency) |
---|
[7163] | 52 | |
---|
[2072] | 53 | this->registerVariables(); |
---|
| 54 | this->xmlfilename_ = this->getFilename(); |
---|
[5929] | 55 | this->xmlfile_ = 0; |
---|
[2072] | 56 | } |
---|
| 57 | |
---|
| 58 | Level::~Level() |
---|
| 59 | { |
---|
| 60 | if (this->isInitialized()) |
---|
| 61 | { |
---|
[6746] | 62 | if (LevelManager::exists()) |
---|
[2168] | 63 | LevelManager::getInstance().releaseActivity(this); |
---|
[2072] | 64 | |
---|
| 65 | if (this->xmlfile_) |
---|
[10392] | 66 | Loader::getInstance().unload(this->xmlfile_); |
---|
[2072] | 67 | } |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | void Level::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 71 | { |
---|
| 72 | SUPER(Level, XMLPort, xmlelement, mode); |
---|
| 73 | |
---|
| 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); |
---|
[7648] | 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->networkTemplateNames_, VariableDirection::ToClient, new NetworkCallback<Level>(this, &Level::networkCallbackTemplatesChanged)); |
---|
[2072] | 85 | } |
---|
| 86 | |
---|
| 87 | void Level::networkcallback_applyXMLFile() |
---|
| 88 | { |
---|
[8858] | 89 | orxout(user_status) << "Loading level \"" << this->xmlfilename_ << "\"..." << endl; |
---|
[2072] | 90 | |
---|
| 91 | ClassTreeMask mask; |
---|
| 92 | mask.exclude(Class(BaseObject)); |
---|
| 93 | mask.include(Class(Template)); |
---|
[2168] | 94 | mask.include(Class(OverlayGroup)); // HACK to include the ChatOverlay |
---|
[2072] | 95 | |
---|
[5695] | 96 | this->xmlfile_ = new XMLFile(mask, this->xmlfilename_); |
---|
[2072] | 97 | |
---|
[10508] | 98 | Loader::getInstance().load(this->xmlfile_); |
---|
[2072] | 99 | } |
---|
| 100 | |
---|
[7163] | 101 | void Level::networkCallbackTemplatesChanged() |
---|
| 102 | { |
---|
| 103 | for( std::set<std::string>::iterator it = this->networkTemplateNames_.begin(); it!=this->networkTemplateNames_.end(); ++it ) |
---|
| 104 | { |
---|
| 105 | assert(Template::getTemplate(*it)); |
---|
| 106 | Template::getTemplate(*it)->applyOn(this); |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
[2072] | 110 | void Level::setGametypeString(const std::string& gametype) |
---|
| 111 | { |
---|
| 112 | Identifier* identifier = ClassByString(gametype); |
---|
[2826] | 113 | |
---|
| 114 | if (!identifier || !identifier->isA(Class(Gametype))) |
---|
[2072] | 115 | { |
---|
[8858] | 116 | orxout(internal_error) << "\"" << gametype << "\" is not a valid gametype." << endl; |
---|
[2826] | 117 | identifier = Class(Gametype); |
---|
| 118 | this->gametype_ = "Gametype"; |
---|
| 119 | } |
---|
| 120 | else |
---|
[2072] | 121 | this->gametype_ = gametype; |
---|
| 122 | |
---|
[3325] | 123 | Gametype* rootgametype = orxonox_cast<Gametype*>(identifier->fabricate(this)); |
---|
[10575] | 124 | |
---|
[10578] | 125 | rootgametype->setLevel(StrongPtr<Level>(NULL)); // avoid circular references |
---|
| 126 | this->setGametype(StrongPtr<Gametype>(rootgametype)); |
---|
[10575] | 127 | rootgametype->init(); // call init() AFTER the gametype was set |
---|
[2072] | 128 | |
---|
[6746] | 129 | if (LevelManager::exists()) |
---|
[2826] | 130 | LevelManager::getInstance().requestActivity(this); |
---|
[2072] | 131 | } |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | void Level::addObject(BaseObject* object) |
---|
| 135 | { |
---|
| 136 | this->objects_.push_back(object); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | BaseObject* Level::getObject(unsigned int index) const |
---|
| 140 | { |
---|
| 141 | unsigned int i = 0; |
---|
| 142 | for (std::list<BaseObject*>::const_iterator it = this->objects_.begin(); it != this->objects_.end(); ++it) |
---|
| 143 | { |
---|
| 144 | if (i == index) |
---|
| 145 | return (*it); |
---|
| 146 | ++i; |
---|
| 147 | } |
---|
| 148 | return 0; |
---|
| 149 | } |
---|
| 150 | |
---|
[7163] | 151 | void Level::addLodInfo(MeshLodInformation* lodInformation) |
---|
| 152 | { |
---|
| 153 | std::string meshName = lodInformation->getMeshName(); |
---|
| 154 | // this->lodInformation_.insert(std::make_pair(meshName,lodInformation)); |
---|
| 155 | if( this->lodInformation_.find(meshName) != this->lodInformation_.end()) |
---|
[8858] | 156 | orxout(verbose, context::lod) << "replacing lod information for " << meshName << endl; |
---|
[7163] | 157 | this->lodInformation_[meshName] = lodInformation; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | MeshLodInformation* Level::getLodInfo(std::string meshName) const |
---|
| 161 | { |
---|
| 162 | if(this->lodInformation_.find(meshName)!=this->lodInformation_.end()) |
---|
| 163 | return this->lodInformation_.find(meshName)->second; |
---|
| 164 | |
---|
| 165 | return 0; |
---|
| 166 | } |
---|
| 167 | |
---|
[2072] | 168 | void Level::playerEntered(PlayerInfo* player) |
---|
| 169 | { |
---|
[8858] | 170 | orxout(internal_info) << "player entered level (id: " << player->getClientID() << ", name: " << player->getName() << ')' << endl; |
---|
[10576] | 171 | player->switchGametype(this->getGametype()); |
---|
[2072] | 172 | } |
---|
| 173 | |
---|
| 174 | void Level::playerLeft(PlayerInfo* player) |
---|
| 175 | { |
---|
[8858] | 176 | orxout(internal_info) << "player left level (id: " << player->getClientID() << ", name: " << player->getName() << ')' << endl; |
---|
[10576] | 177 | player->switchGametype(0); |
---|
[2072] | 178 | } |
---|
| 179 | } |
---|