<commit_before>package com.shade.lighting;

import java.util.Arrays;
import java.util.LinkedList;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;

import org.newdawn.slick.state.StateBasedGame;

import com.shade.controls.DayPhaseTimer;

/**
 * A view which renders a set of entities, lights, and background images in such
 * a way as to generate dynamic lighting.
 *
 * It's safe to draw things to the screen after calling LightMask.render if you
 * want them to appear above the gameplay, for instance user controls.
 *
 * <em>Note that calling render will update your entities' luminosity. Please
 * direct any hate mail to JJ Jou.</em>
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
public class LightMask {


    protected final static Color SHADE = new Color(0, 0, 0, .3f);
    public static final float MAX_DARKNESS = 0.4f;
    private DayPhaseTimer timer;
    
    /**======================END CONSTANTS=======================*/

    private int threshold;
    private LinkedList<LightSource> lights;

    public LightMask(int threshold, DayPhaseTimer time) {
        this.threshold = threshold;
        lights = new LinkedList<LightSource>();
        timer = time;
    }

    public void add(LightSource light) {
        lights.add(light);
    }

    public void render(StateBasedGame game, Graphics g,
                       LuminousEntity[] entities, Image... backgrounds) {
        renderLights(game, g, entities);
        renderBackgrounds(game, g, backgrounds);
        renderEntities(game, g, entities);
        //RENDER NIGHT! WHEEE
        renderTimeOfDay(game, g);
    }
    
    public void renderTimeOfDay(StateBasedGame game, Graphics g){
    	Color c = g.getColor();
    	if(timer.getDaylightStatus()==DayPhaseTimer.DayLightStatus.DUSK){
    		g.setColor(new Color(1-timer.timeLeft(),1-timer.timeLeft(),0f,MAX_DARKNESS*timer.timeLeft()));
    		g.fillRect(0, 0, game.getContainer().getWidth(), game.getContainer().getHeight());
    	}
    	else if(timer.getDaylightStatus()==DayPhaseTimer.DayLightStatus.NIGHT){
    		g.setColor(new Color(0,0,0,MAX_DARKNESS));
    		g.fillRect(0, 0, game.getContainer().getWidth(), game.getContainer().getHeight());
    	}
    	else if(timer.getDaylightStatus()==DayPhaseTimer.DayLightStatus.DAWN){
    		g.setColor(new Color(timer.timeLeft(),timer.timeLeft(),0,MAX_DARKNESS*(1-timer.timeLeft())));
    		g.fillRect(0, 0, game.getContainer().getWidth(), game.getContainer().getHeight());
    	}
    	g.setColor(c);
    
    }

    private void renderLights(StateBasedGame game, Graphics g,
                              LuminousEntity... entities) {
    	enableStencil();
        for (LightSource light : lights) {
            light.render(game, g, entities);
        }
        disableStencil();

        GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);
        g.setColor(SHADE);
        GameContainer c = game.getContainer();
        g.fillRect(0, 0, c.getWidth(), c.getHeight());
        g.setColor(Color.white);
    }

    private void renderBackgrounds(StateBasedGame game, Graphics g,
                                   Image... backgrounds) {
        GL11.glBlendFunc(GL11.GL_DST_ALPHA, GL11.GL_ONE);
        for (Image background : backgrounds) {
            background.draw();
        }
    }

    private void renderEntities(StateBasedGame game, Graphics g,
                                LuminousEntity... entities) {
        Arrays.sort(entities);
        int i = 0;
        GL11.glEnable(GL11.GL_ALPHA_TEST);
        GL11.glAlphaFunc(GL11.GL_GREATER, 0);
        GL11.glBlendFunc(GL11.GL_DST_ALPHA, GL11.GL_ZERO);
        while (i < entities.length && entities[i].getZIndex() < threshold) {
            entities[i].render(game, g);
            entities[i].setLuminosity(getLuminosityFor(entities[i], g));
            i++;
        }

        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        while (i < entities.length) {
            entities[i].render(game, g);
            entities[i].setLuminosity(getLuminosityFor(entities[i], g));
            i++;
        }
        GL11.glDisable(GL11.GL_ALPHA_TEST);
    }

    private float getLuminosityFor(LuminousEntity entity, Graphics g) {
        return g.getPixel((int) entity.getXCenter(), (int) entity.getYCenter()).a;
    }

    /**
     * Called before drawing the shadows cast by a light.
     */
    protected static void enableStencil() {
        // write only to the stencil buffer
        GL11.glEnable(GL11.GL_STENCIL_TEST);
        GL11.glColorMask(false, false, false, false);
        GL11.glDepthMask(false);
    }
    
    protected static void resetStencil(){
    	GL11.glClearStencil(0);
        // write a one to the stencil buffer everywhere we are about to draw
        GL11.glStencilFunc(GL11.GL_ALWAYS, 1, 1);
        // this is to always pass a one to the stencil buffer where we draw
        GL11.glStencilOp(GL11.GL_REPLACE, GL11.GL_REPLACE, GL11.GL_REPLACE);
    }
    
    /**
     * Called after drawing the shadows cast by a light.
     */
    protected static void keepStencil() {
        // resume drawing to everything<commit_msg>Bleeding colors fixed. More to come...<commit_after>package com.shade.lighting;

import java.util.Arrays;
import java.util.LinkedList;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;

import org.newdawn.slick.state.StateBasedGame;

import com.shade.controls.DayPhaseTimer;

/**
 * A view which renders a set of entities, lights, and background images in such
 * a way as to generate dynamic lighting.
 *
 * It's safe to draw things to the screen after calling LightMask.render if you
 * want them to appear above the gameplay, for instance user controls.
 *
 * <em>Note that calling render will update your entities' luminosity. Please
 * direct any hate mail to JJ Jou.</em>
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
public class LightMask {


    protected final static Color SHADE = new Color(0, 0, 0, .3f);
    public static final float MAX_DARKNESS = 0.4f;
    private DayPhaseTimer timer;
    
    /**======================END CONSTANTS=======================*/

    private int threshold;
    private LinkedList<LightSource> lights;

    public LightMask(int threshold, DayPhaseTimer time) {
        this.threshold = threshold;
        lights = new LinkedList<LightSource>();
        timer = time;
    }

    public void add(LightSource light) {
        lights.add(light);
    }

    public void render(StateBasedGame game, Graphics g,
                       LuminousEntity[] entities, Image... backgrounds) {
        renderLights(game, g, entities);
        renderBackgrounds(game, g, backgrounds);
        renderEntities(game, g, entities);
        //RENDER NIGHT! WHEEE
        renderTimeOfDay(game, g);
    }
    
    public void renderTimeOfDay(StateBasedGame game, Graphics g){
    	Color c = g.getColor();
    	if(timer.getDaylightStatus()==DayPhaseTimer.DayLightStatus.DUSK){
    		g.setColor(new Color(1-timer.timeLeft(),1-timer.timeLeft(),0f,MAX_DARKNESS*timer.timeLeft()));
    		g.fillRect(0, 0, game.getContainer().getWidth(), game.getContainer().getHeight());
    	}
    	else if(timer.getDaylightStatus()==DayPhaseTimer.DayLightStatus.NIGHT){
    		g.setColor(new Color(0,0,0,MAX_DARKNESS));
    		g.fillRect(0, 0, game.getContainer().getWidth(), game.getContainer().getHeight());
    	}
    	else if(timer.getDaylightStatus()==DayPhaseTimer.DayLightStatus.DAWN){
    		g.setColor(new Color(timer.timeLeft(),timer.timeLeft(),0,MAX_DARKNESS*(1-timer.timeLeft())));
    		g.fillRect(0, 0, game.getContainer().getWidth(), game.getContainer().getHeight());
    	}
    	g.setColor(c);
    
    }

    private void renderLights(StateBasedGame game, Graphics g,
                              LuminousEntity... entities) {
    	enableStencil();
        for (LightSource light : lights) {
            light.render(game, g, entities);
        }
        disableStencil();

        GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);
        g.setColor(SHADE);
        GameContainer c = game.getContainer();
        g.fillRect(0, 0, c.getWidth(), c.getHeight());
        g.setColor(Color.white);
    }

    private void renderBackgrounds(StateBasedGame game, Graphics g,
                                   Image... backgrounds) {
        GL11.glBlendFunc(GL11.GL_DST_ALPHA, GL11.GL_ONE);
        for (Image background : backgrounds) {
            background.draw();
        }
    }

    private void renderEntities(StateBasedGame game, Graphics g,
                                LuminousEntity... entities) {
        Arrays.sort(entities);
        int i = 0;
       // GL11.glEnable(GL11.GL_ALPHA_TEST);
        GL11.glEnable(GL11.GL_STENCIL_TEST);
        GL11.glStencilFunc(GL11.GL_ALWAYS, 1, 1);
        GL11.glStencilOp(GL11.GL_KEEP, GL11.GL_KEEP, GL11.GL_REPLACE);
        GL11.glAlphaFunc(GL11.GL_GREATER, 0);
        GL11.glBlendFunc(GL11.GL_DST_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        while (i < entities.length && entities[i].getZIndex() < threshold) {
            entities[i].render(game, g);
            entities[i].setLuminosity(getLuminosityFor(entities[i], g));
            i++;
        }

        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        while (i < entities.length) {
            entities[i].render(game, g);
            entities[i].setLuminosity(getLuminosityFor(entities[i], g));
            i++;
        }
        GL11.glDisable(GL11.GL_STENCIL_TEST);
        GL11.glDisable(GL11.GL_ALPHA_TEST);
    }

    private float getLuminosityFor(LuminousEntity entity, Graphics g) {
        return g.getPixel((int) entity.getXCenter(), (int) entity.getYCenter()).a;
    }

    /**
     * Called before drawing the shadows cast by a light.
     */
    protected static void enableStencil() {
        // write only to the stencil buffer
        GL11.glEnable(GL11.GL_STENCIL_TEST);
        GL11.glColorMask(false, false, false, false);
        GL11.glDepthMask(false);
    }
    
    protected static void resetStencil(){
    	GL11.glClearStencil(0);
        // write a one to the stencil buffer everywhere we are about to draw
        GL11.glStencilFunc(GL11.GL_ALWAYS, 1, 1);
        // this is to always pass a one to the stencil buffer where we draw
        GL11.glStencilOp(GL11.GL_REPLACE, GL11.GL_REPLACE, GL11.GL_REPLACE);
    }
    
    /**
     * Called after drawing the shadows cast by a light.
     */
    protected static void keepStencil() {
        // resume drawing to everything
{'fraction_non_alphanumeric': 0.12404402163775415, 'fraction_numerical': 0.018280171609774295, 'mean_word_length': 2.720680083275503, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6597531', 'n_tokens_mistral': 3546, 'n_tokens_neox': 3360, 'n_words': 740}