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