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