<commit_before>/**
 *  For conditions of distribution and use, see copyright notice in license.txt
 *
 *  @file   Environment.cpp
 *  @brief  Manages environment-related reX-logic, e.g. world time and lighting.
 */

#include "StableHeaders.h"

#include "Environment.h"
#include "EnvironmentModule.h"
#include "EC_WaterPlane.h"
#include "EC_Fog.h"

#include "EC_OgreEnvironment.h"
#include "SceneManager.h"
#include "NetworkMessages/NetInMessage.h"
#include "OgreRenderingModule.h"

#ifdef CAELUM
#include <Caelum.h>
#endif

namespace Environment
{

/// Utility tool for clamping fog distance
void ClampFog(float& start, float& end, float farclip)
{
    if (farclip < 10.0) 
        farclip = 10.0;
    if (end > farclip - 10.0)
        end = farclip - 10.0;
    if (start > farclip/3.0)
        start = farclip/3.0;
}

Environment::Environment(EnvironmentModule *owner) :
    owner_(owner),
    activeEnvEntity_(Scene::EntityWeakPtr()),
    time_override_(false),
    usecSinceStart_(0),
    secPerDay_(0),
    secPerYear_(0),
    sunPhase_(0.0)
{

#ifdef CAELUM
    caelumComponents_ = Caelum::CaelumSystem::CAELUM_COMPONENTS_NONE;
    caelumComponents_ = caelumComponents_ |
        Caelum::CaelumSystem::CAELUM_COMPONENT_SKY_DOME |
        Caelum::CaelumSystem::CAELUM_COMPONENT_MOON |
        Caelum::CaelumSystem::CAELUM_COMPONENT_SUN |
        Caelum::CaelumSystem::CAELUM_COMPONENT_POINT_STARFIELD |
        Caelum::CaelumSystem::CAELUM_COMPONENT_SCREEN_SPACE_FOG |
        Caelum::CaelumSystem::CAELUM_COMPONENT_GROUND_FOG;
#endif
}

Environment::~Environment()
{
    if ( activeEnvEntity_.lock().get() != 0)
    {
        Scene::EntityPtr entity = activeEnvEntity_.lock();
        if ( entity != 0)
        {
            // Remove entity from scene.
            Scene::SceneManager* manager = entity->GetScene();
            if ( manager != 0)
                manager->RemoveEntity(entity->GetId());
        }

        entity.reset();
    }

    activeEnvEntity_.reset();
    owner_ = 0;
}

EC_OgreEnvironment* Environment::GetEnvironmentComponent()
{
    if (activeEnvEntity_.expired())
        return 0;

    return activeEnvEntity_.lock()->GetComponent<EC_OgreEnvironment>().get();  
}

void Environment::CreateEnvironment()
{
    Scene::ScenePtr active_scene = owner_->GetFramework()->GetDefaultWorldScene();
    Scene::EntityPtr entity = active_scene->CreateEntity(active_scene->GetNextFreeIdLocal());
    active_scene->EmitEntityCreated(entity);
    activeEnvEntity_ = entity;

    // Creates Caelum component!!
    entity->AddComponent(owner_->GetFramework()->GetComponentManager()->CreateComponent("EC_OgreEnvironment"));<commit_msg>Create the environment entity as a temporary entity.<commit_after>/**
 *  For conditions of distribution and use, see copyright notice in license.txt
 *
 *  @file   Environment.cpp
 *  @brief  Manages environment-related reX-logic, e.g. world time and lighting.
 */

#include "StableHeaders.h"

#include "Environment.h"
#include "EnvironmentModule.h"
#include "EC_WaterPlane.h"
#include "EC_Fog.h"

#include "EC_OgreEnvironment.h"
#include "SceneManager.h"
#include "NetworkMessages/NetInMessage.h"
#include "OgreRenderingModule.h"

#ifdef CAELUM
#include <Caelum.h>
#endif

namespace Environment
{

/// Utility tool for clamping fog distance
void ClampFog(float& start, float& end, float farclip)
{
    if (farclip < 10.0) 
        farclip = 10.0;
    if (end > farclip - 10.0)
        end = farclip - 10.0;
    if (start > farclip/3.0)
        start = farclip/3.0;
}

Environment::Environment(EnvironmentModule *owner) :
    owner_(owner),
    activeEnvEntity_(Scene::EntityWeakPtr()),
    time_override_(false),
    usecSinceStart_(0),
    secPerDay_(0),
    secPerYear_(0),
    sunPhase_(0.0)
{

#ifdef CAELUM
    caelumComponents_ = Caelum::CaelumSystem::CAELUM_COMPONENTS_NONE;
    caelumComponents_ = caelumComponents_ |
        Caelum::CaelumSystem::CAELUM_COMPONENT_SKY_DOME |
        Caelum::CaelumSystem::CAELUM_COMPONENT_MOON |
        Caelum::CaelumSystem::CAELUM_COMPONENT_SUN |
        Caelum::CaelumSystem::CAELUM_COMPONENT_POINT_STARFIELD |
        Caelum::CaelumSystem::CAELUM_COMPONENT_SCREEN_SPACE_FOG |
        Caelum::CaelumSystem::CAELUM_COMPONENT_GROUND_FOG;
#endif
}

Environment::~Environment()
{
    if ( activeEnvEntity_.lock().get() != 0)
    {
        Scene::EntityPtr entity = activeEnvEntity_.lock();
        if ( entity != 0)
        {
            // Remove entity from scene.
            Scene::SceneManager* manager = entity->GetScene();
            if ( manager != 0)
                manager->RemoveEntity(entity->GetId());
        }

        entity.reset();
    }

    activeEnvEntity_.reset();
    owner_ = 0;
}

EC_OgreEnvironment* Environment::GetEnvironmentComponent()
{
    if (activeEnvEntity_.expired())
        return 0;

    return activeEnvEntity_.lock()->GetComponent<EC_OgreEnvironment>().get();  
}

void Environment::CreateEnvironment()
{
    Scene::ScenePtr active_scene = owner_->GetFramework()->GetDefaultWorldScene();
    Scene::EntityPtr entity = active_scene->CreateEntity(active_scene->GetNextFreeIdLocal());

    // The environment entity exists due to legacy from Taiga. It is bad design to create the entity here.
    entity->SetTemporary(true);
    active_scene->EmitEntityCreated(entity);
    activeEnvEntity_ = entity;

    // Creates Caelum component!!
    entity->AddComponent(owner_->GetFramework()->GetComponentManager()->CreateComponent("EC_OgreEnvironment"));
{'fraction_non_alphanumeric': 0.12155502828983392, 'fraction_numerical': 0.009490782989596642, 'mean_word_length': 3.7200689061154177, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '105517', 'n_tokens_mistral': 1799, 'n_tokens_neox': 1745, 'n_words': 321}