Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10695 was 10624, checked in by landauf, 9 years ago

merged branch core7 back to trunk

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