Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ScriptableController_HS17/src/orxonox/Level.cc @ 11535

Last change on this file since 11535 was 11518, checked in by kohlia, 7 years ago

Nothing to see yet, really.

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