Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/Level.cc @ 11222

Last change on this file since 11222 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

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