<commit_before>package de.mycrobase.ssim.ed.app;


import org.apache.log4j.Logger;

import ssim.util.MathExt;

import com.jme3.app.Application;
import com.jme3.app.state.AppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.shadow.DirectionalLightShadowRenderer;
import com.jme3.shadow.EdgeFilteringMode;

import de.mycrobase.ssim.ed.util.TempVars;
import de.mycrobase.ssim.ed.util.Util;
import de.mycrobase.ssim.ed.weather.Weather;

/**
 * <b>Higher layer</b> {@link AppState} responsible for setting up scene
 * lighting and shadows.
 * 
 * @author cn
 */
public class LightingAppState extends BasicAppState {
    
    private static final Logger logger = Logger.getLogger(LightingAppState.class);
    
    private static final float UpdateInterval = 30f; // in seconds
    
    // exists only while AppState is attached
    private DirectionalLight sunLight;
    private AmbientLight envLight;
    private DirectionalLightShadowRenderer shadowRenderer;
    
    private int texSize;
    private int numSplits;
    
    public LightingAppState() {
        super(UpdateInterval);
    }
    
    @Override
    public void initialize(AppStateManager stateManager, Application baseApp) {
        super.initialize(stateManager, baseApp);
        
        sunLight = new DirectionalLight();
        updateSunLight();
        getApp().getRootNode().addLight(sunLight);
        
        envLight = new AmbientLight();
        updateEnvLight();
        getApp().getRootNode().addLight(envLight);
        
        evalSettings();
        logger.info(String.format("Shadow map texture size: %d", texSize));
        logger.info(String.format("Number of shadow maps: %d", numSplits));
        
        shadowRenderer = new DirectionalLightShadowRenderer(
            getApp().getAssetManager(), texSize, numSplits);
        shadowRenderer.setLight(sunLight);
        shadowRenderer.setEdgeFilteringMode(EdgeFilteringMode.Dither);
        updateShadows();
        getApp().getViewPort().addProcessor(shadowRenderer);
    }
    
    @Override
    protected void intervalUpdate(float dt) {
        updateSunLight();
        updateEnvLight();
        updateShadows();
    }
    
    @Override
    public void cleanup() {
        super.cleanup();
        
        getApp().getViewPort().removeProcessor(shadowRenderer);
        getApp().getRootNode().removeLight(envLight);
        getApp().getRootNode().removeLight(sunLight);
        
        sunLight = null;
        envLight = null;
    }
    
    private void updateSunLight() {
        TempVars vars = TempVars.get();
        
        ColorRGBA sunLightColor = getSkyAppState().getSkyGradient().getSunLightColor(vars.color1);
        sunLight.setColor(sunLightColor);
        
        Vector3f sunPosition = getSkyAppState().getSun().getSunPosition(vars.vect1);
        sunPosition.negateLocal();
        sunLight.setDirection(sunPosition);
        
        vars.release();
    }
    
    private void updateEnvLight() {
        TempVars vars = TempVars.get();
        
        Vector3f v = getState(WeatherAppState.class).getWeather().getVec3("sky.light");
        ColorRGBA envColor = Util.setTo(vars.color1, v, 1f);
        envLight.setColor(envColor);
        
        vars.release();
    }
    
    private void updateShadows() {
        // range 0 to 1
        float baseIntensity = getSkyAppState().getSkyGradient().getShadowBaseIntensity();
        // range -1 (totally overcast) to +1 (completely clear) 
        float cloudInfluence = (getWeather().getFloat("cloud.cover")/255f)*2f - 1f;
        // combine
        baseIntensity += cloudInfluence * 0.3f;
        shadowRenderer.setShadowIntensity(MathExt.clamp01(baseIntensity));
    }

    private void evalSettings() {
        int detailLevel = getApp().getSettingsManager().getInteger("engine.detail.level");
        switch(detailLevel) {
            case 0: {
                texSize = 256; numSplits = 1;
                break;
            }
            case 1: {
                texSize = 512; numSplits = 2;
                break;
            }
            case 2: {
                texSize = 1024; numSplits = 3;
                break;
            }
        }
    }
    
    private Weather getWeather() {
        return getState(WeatherAppState.class).getWeather();
    }
    
    private SkyAppState getSkyAppState() {
        return getState(SkyAppState.class);
    }
}<commit_msg>Refactoring<commit_after>package de.mycrobase.ssim.ed.app;


import org.apache.log4j.Logger;

import ssim.util.MathExt;

import com.jme3.app.Application;
import com.jme3.app.state.AppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.shadow.DirectionalLightShadowRenderer;
import com.jme3.shadow.EdgeFilteringMode;

import de.mycrobase.ssim.ed.util.TempVars;
import de.mycrobase.ssim.ed.util.Util;
import de.mycrobase.ssim.ed.weather.Weather;

/**
 * <b>Higher layer</b> {@link AppState} responsible for setting up scene
 * lighting and shadows.
 * 
 * @author cn
 */
public class LightingAppState extends BasicAppState {
    
    private static final Logger logger = Logger.getLogger(LightingAppState.class);
    
    private static final float UpdateInterval = 30f; // in seconds
    
    // exists only while AppState is attached
    private DirectionalLight sunLight;
    private AmbientLight envLight;
    private DirectionalLightShadowRenderer shadowRenderer;
    
    private int shadowTexSize;
    private int shadowNumSplits;
    
    public LightingAppState() {
        super(UpdateInterval);
    }
    
    @Override
    public void initialize(AppStateManager stateManager, Application baseApp) {
        super.initialize(stateManager, baseApp);
        
        sunLight = new DirectionalLight();
        updateSunLight();
        getApp().getRootNode().addLight(sunLight);
        
        envLight = new AmbientLight();
        updateEnvLight();
        getApp().getRootNode().addLight(envLight);
        
        evalSettings();
        logger.info(String.format("Shadow map texture size: %d", shadowTexSize));
        logger.info(String.format("Number of shadow maps: %d", shadowNumSplits));
        
        shadowRenderer = new DirectionalLightShadowRenderer(
            getApp().getAssetManager(), shadowTexSize, shadowNumSplits);
        shadowRenderer.setLight(sunLight);
        shadowRenderer.setEdgeFilteringMode(EdgeFilteringMode.Dither);
        updateShadows();
        getApp().getViewPort().addProcessor(shadowRenderer);
    }
    
    @Override
    protected void intervalUpdate(float dt) {
        updateSunLight();
        updateEnvLight();
        updateShadows();
    }
    
    @Override
    public void cleanup() {
        super.cleanup();
        
        getApp().getViewPort().removeProcessor(shadowRenderer);
        getApp().getRootNode().removeLight(envLight);
        getApp().getRootNode().removeLight(sunLight);
        
        sunLight = null;
        envLight = null;
    }
    
    private void updateSunLight() {
        TempVars vars = TempVars.get();
        
        ColorRGBA sunLightColor = getSkyAppState().getSkyGradient().getSunLightColor(vars.color1);
        sunLight.setColor(sunLightColor);
        
        Vector3f sunPosition = getSkyAppState().getSun().getSunPosition(vars.vect1);
        sunPosition.negateLocal();
        sunLight.setDirection(sunPosition);
        
        vars.release();
    }
    
    private void updateEnvLight() {
        TempVars vars = TempVars.get();
        
        Vector3f v = getState(WeatherAppState.class).getWeather().getVec3("sky.light");
        ColorRGBA envColor = Util.setTo(vars.color1, v, 1f);
        envLight.setColor(envColor);
        
        vars.release();
    }
    
    private void updateShadows() {
        // range 0 to 1
        float baseIntensity = getSkyAppState().getSkyGradient().getShadowBaseIntensity();
        // range -1 (totally overcast) to +1 (completely clear) 
        float cloudInfluence = (getWeather().getFloat("cloud.cover")/255f)*2f - 1f;
        // combine
        baseIntensity += cloudInfluence * 0.3f;
        shadowRenderer.setShadowIntensity(MathExt.clamp01(baseIntensity));
    }

    private void evalSettings() {
        int detailLevel = getApp().getSettingsManager().getInteger("engine.detail.level");
        switch(detailLevel) {
            case 0: {
                shadowTexSize = 256; shadowNumSplits = 1;
                break;
            }
            case 1: {
                shadowTexSize = 512; shadowNumSplits = 2;
                break;
            }
            case 2: {
                shadowTexSize = 1024; shadowNumSplits = 3;
                break;
            }
        }
    }
    
    private Weather getWeather() {
        return getState(WeatherAppState.class).getWeather();
    }
    
    private SkyAppState getSkyAppState() {
        return getState(SkyAppState.class);
    }
}
{'fraction_non_alphanumeric': 0.1039074437895656, 'fraction_numerical': 0.0106963545077494, 'mean_word_length': 2.48800913589646, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6074691', 'n_tokens_mistral': 2731, 'n_tokens_neox': 2613, 'n_words': 492}