<commit_before>#include "augs/math/camera_cone.h"
#include "augs/math/matrix.h"

#include "augs/drawing/drawing.h"

#include "augs/graphics/renderer.h"
#include "augs/graphics/shader.h"

#include "game/cosmos/entity_handle.h"
#include "game/cosmos/cosmos.h"
#include "game/cosmos/for_each_entity.h"
#include "game/enums/filters.h"

#include "view/frame_profiler.h"
#include "view/rendering_scripts/draw_entity.h"

#include "game/components/item_slot_transfers_component.h"
#include "game/debug_drawing_settings.h"
#include "game/detail/visible_entities.h"

#include "game/components/interpolation_component.h"
#include "game/components/fixtures_component.h"

#include "game/detail/inventory/inventory_slot_handle.h"

#include "view/rendering_scripts/rendering_scripts.h"
#include "view/rendering_scripts/illuminated_rendering.h"
#include "view/rendering_scripts/draw_wandering_pixels_as_sprites.h"
#include "view/rendering_scripts/helper_drawer.h"
#include "view/rendering_scripts/draw_area_indicator.h"

#include "view/viewables/all_viewables_declaration.h"
#include "view/viewables/image_in_atlas.h"
#include "view/viewables/images_in_atlas_map.h"
#include "game/stateless_systems/visibility_system.h"

#include "view/audiovisual_state/audiovisual_state.h"

#include "view/viewables/atlas_distributions.h"

#include "view/rendering_scripts/illuminated_rendering.h"
#include "game/detail/crosshair_math.hpp"

void illuminated_rendering(
	const illuminated_rendering_input in,
	const std::vector<additional_highlight>& additional_highlights
) {
	auto& profiler = in.frame_performance;

	auto& renderer = in.renderer;
	
	const auto viewed_character = in.camera.viewed_character;

	const auto cone = [&in](){ 
		auto result = in.camera.cone;
		result.eye.transform.pos.discard_fract();
		return result;
	}();

	const auto screen_size = cone.screen_size;

	const auto& cosm = viewed_character.get_cosmos();
	
	const auto& anims = cosm.get_logical_assets().plain_animations;

	const auto& av = in.audiovisuals;
	const auto& interp = av.get<interpolation_system>();
	const auto& particles = av.get<particles_simulation_system>();
	const auto& wandering_pixels = av.get<wandering_pixels_system>();
	const auto& exploding_rings = av.get<exploding_ring_system>();
	const auto& flying_numbers = av.get<flying_number_indicator_system>();
	const auto& highlights = av.get<pure_color_highlight_system>();
	const auto& thunders = av.get<thunder_system>();
	const auto global_time_seconds = cosm.get_total_seconds_passed(in.interpolation_ratio);
	const auto settings = in.drawing;
	const auto matrix = cone.get_projection_matrix();
	const auto& visible = in.all_visible;
	const auto& shaders = in.shaders;
	const auto& fbos = in.fbos;
	const auto& necessarys = in.necessary_images;
	const auto& game_images = in.game_images;
	const auto blank = necessarys.at(assets::necessary_image_id::BLANK);
	const auto& gui_font = in.gui_font;

	const auto output = augs::drawer_with_default{ renderer.get_triangle_buffer(), blank };
	const auto line_output = augs::line_drawer_with_default{ renderer.get_line_buffer(), blank };
	const auto viewed_character_transform = viewed_character ? viewed_character.find_viewing_transform(interp) : std::optional<transformr>();

	if (in.general_atlas) {
		in.general_atlas->bind();
	}

	auto set_shader_with_matrix = [&](auto& shader) {
		shader->set_as_current();
		shader->set_projection(matrix);
	};

	set_shader_with_matrix(shaders.standard);

	fbos.smoke->set_as_current();

	renderer.clear_current_fbo();
	renderer.set_additive_blending();
	
	const auto draw_particles_in = draw_particles_input{ output, false };

	auto total_particles_scope = measure_scope_additive(profiler.particles_rendering);
	auto total_layer_scope = measure_scope_additive(profiler.drawing_layers);

	auto draw_particles = [&](const particle_layer layer) {
		auto scope = measure_scope(total_particles_scope);

		particles.draw_particles(
			game_images,
			anims,
			draw_particles_in,
			layer
		);
	};

	draw_particles(particle_layer::DIM_SMOKES);
	draw_particles(particle_layer::ILLUMINATING_SMOKES);

	renderer.call_and_clear_triangles();

	fbos.illuminating_smoke->set_as_current();
	renderer.clear_current_fbo();

	draw_particles(particle_layer::ILLUMINATING_SMOKES);

	renderer.call_and_clear_triangles();
	
	renderer.set_standard_blending();

	augs::graphics::fbo::set_current_to_none();

	const auto& light = av.get<light_system>();
	
	const auto laser_glow = necessarys.at(assets::necessary_image_id::LASER_GLOW);
	const auto glow_edge_tex = necessarys.at(assets::necessary_image_id::LASER_GLOW_EDGE);

	const auto cast_highlight = necessarys.at(assets::necessary_image_id::CAST_HIGHLIGHT);

	const auto queried_cone = [&]() {
		auto c = cone;
		c.eye.zoom /= in.camera_query_mult;
		return c;
	}();

	const auto drawing_input = draw_renderable_input { 
		{
			output, 
			game_images, 
			global_time_seconds,
			flip_flags(),
			av.randomizing,
			queried_cone
		},
		interp
	};

	const bool fog_of_war_effective = 
		viewed_character_transform != std::nullopt 
		&& settings.fog_of_war.enabled
	;

#if BUILD_STENCIL_BUFFER
	if (fog_of_war_effective) {
		auto fow_raycasts_scope = cosm.measure_raycasts(profiler.fog_of_war_raycasts);

		messages::visibility_information_request request;
		request.eye_transform = *viewed_character_transform;
		request.filter = predefined_queries::pathfinding();
		request.square_side = static_cast<float>(std::max(screen_size.x, screen_size.y) * 2);
		request.subject = viewed_character;

		visibility_system(DEBUG_LOGIC_STEP_LINES).calc_visibility(cosm, request);
	}

	auto fill_stencil = [&]() {
		auto& viewed_visibility = thread_local_visibility_responses();

		if (viewed_visibility.size() > 0 && !viewed_visibility[0].empty()) {
			renderer.enable_stencil();

			renderer.start_writing_stencil();
			renderer.set_clear_color(rgba(0, 0, 0, 0));
			renderer.clear_stencil();

			const auto eye_pos = viewed_character_transform->pos;
			const auto& r = viewed_visibility[0];

			for (std::size_t t = 0; t < r.get_num_triangles(); ++t) {
				const auto world_light_tri = r.get_world_triangle(t, eye_pos);
				augs::vertex_triangle renderable_light_tri;

				renderable_light_tri.vertices[0].pos = world_light_tri[0];
				renderable_light_tri.vertices[1].pos = world_light_tri[1];
				renderable_light_tri.vertices[2].pos = world_light_tri[2];
<commit_msg>Bugfix: neon maps were visible behind the fog of war<commit_after>#include "augs/math/camera_cone.h"
#include "augs/math/matrix.h"

#include "augs/drawing/drawing.h"

#include "augs/graphics/renderer.h"
#include "augs/graphics/shader.h"

#include "game/cosmos/entity_handle.h"
#include "game/cosmos/cosmos.h"
#include "game/cosmos/for_each_entity.h"
#include "game/enums/filters.h"

#include "view/frame_profiler.h"
#include "view/rendering_scripts/draw_entity.h"

#include "game/components/item_slot_transfers_component.h"
#include "game/debug_drawing_settings.h"
#include "game/detail/visible_entities.h"

#include "game/components/interpolation_component.h"
#include "game/components/fixtures_component.h"

#include "game/detail/inventory/inventory_slot_handle.h"

#include "view/rendering_scripts/rendering_scripts.h"
#include "view/rendering_scripts/illuminated_rendering.h"
#include "view/rendering_scripts/draw_wandering_pixels_as_sprites.h"
#include "view/rendering_scripts/helper_drawer.h"
#include "view/rendering_scripts/draw_area_indicator.h"

#include "view/viewables/all_viewables_declaration.h"
#include "view/viewables/image_in_atlas.h"
#include "view/viewables/images_in_atlas_map.h"
#include "game/stateless_systems/visibility_system.h"

#include "view/audiovisual_state/audiovisual_state.h"

#include "view/viewables/atlas_distributions.h"

#include "view/rendering_scripts/illuminated_rendering.h"
#include "game/detail/crosshair_math.hpp"

void illuminated_rendering(
	const illuminated_rendering_input in,
	const std::vector<additional_highlight>& additional_highlights
) {
	auto& profiler = in.frame_performance;

	auto& renderer = in.renderer;
	
	const auto viewed_character = in.camera.viewed_character;

	const auto cone = [&in](){ 
		auto result = in.camera.cone;
		result.eye.transform.pos.discard_fract();
		return result;
	}();

	const auto screen_size = cone.screen_size;

	const auto& cosm = viewed_character.get_cosmos();
	
	const auto& anims = cosm.get_logical_assets().plain_animations;

	const auto& av = in.audiovisuals;
	const auto& interp = av.get<interpolation_system>();
	const auto& particles = av.get<particles_simulation_system>();
	const auto& wandering_pixels = av.get<wandering_pixels_system>();
	const auto& exploding_rings = av.get<exploding_ring_system>();
	const auto& flying_numbers = av.get<flying_number_indicator_system>();
	const auto& highlights = av.get<pure_color_highlight_system>();
	const auto& thunders = av.get<thunder_system>();
	const auto global_time_seconds = cosm.get_total_seconds_passed(in.interpolation_ratio);
	const auto settings = in.drawing;
	const auto matrix = cone.get_projection_matrix();
	const auto& visible = in.all_visible;
	const auto& shaders = in.shaders;
	const auto& fbos = in.fbos;
	const auto& necessarys = in.necessary_images;
	const auto& game_images = in.game_images;
	const auto blank = necessarys.at(assets::necessary_image_id::BLANK);
	const auto& gui_font = in.gui_font;

	const auto output = augs::drawer_with_default{ renderer.get_triangle_buffer(), blank };
	const auto line_output = augs::line_drawer_with_default{ renderer.get_line_buffer(), blank };
	const auto viewed_character_transform = viewed_character ? viewed_character.find_viewing_transform(interp) : std::optional<transformr>();

	if (in.general_atlas) {
		in.general_atlas->bind();
	}

	auto set_shader_with_matrix = [&](auto& shader) {
		shader->set_as_current();
		shader->set_projection(matrix);
	};

	set_shader_with_matrix(shaders.standard);

	fbos.smoke->set_as_current();

	renderer.clear_current_fbo();
	renderer.set_additive_blending();
	
	const auto draw_particles_in = draw_particles_input{ output, false };

	auto total_particles_scope = measure_scope_additive(profiler.particles_rendering);
	auto total_layer_scope = measure_scope_additive(profiler.drawing_layers);

	auto draw_particles = [&](const particle_layer layer) {
		auto scope = measure_scope(total_particles_scope);

		particles.draw_particles(
			game_images,
			anims,
			draw_particles_in,
			layer
		);
	};

	draw_particles(particle_layer::DIM_SMOKES);
	draw_particles(particle_layer::ILLUMINATING_SMOKES);

	renderer.call_and_clear_triangles();

	fbos.illuminating_smoke->set_as_current();
	renderer.clear_current_fbo();

	draw_particles(particle_layer::ILLUMINATING_SMOKES);

	renderer.call_and_clear_triangles();
	
	renderer.set_standard_blending();

	augs::graphics::fbo::set_current_to_none();

	const auto& light = av.get<light_system>();
	
	const auto laser_glow = necessarys.at(assets::necessary_image_id::LASER_GLOW);
	const auto glow_edge_tex = necessarys.at(assets::necessary_image_id::LASER_GLOW_EDGE);

	const auto cast_highlight = necessarys.at(assets::necessary_image_id::CAST_HIGHLIGHT);

	const auto queried_cone = [&]() {
		auto c = cone;
		c.eye.zoom /= in.camera_query_mult;
		return c;
	}();

	const auto drawing_input = draw_renderable_input { 
		{
			output, 
			game_images, 
			global_time_seconds,
			flip_flags(),
			av.randomizing,
			queried_cone
		},
		interp
	};

	const bool fog_of_war_effective = 
		viewed_character_transform != std::nullopt 
		&& settings.fog_of_war.enabled
	;

#if BUILD_STENCIL_BUFFER
	/* 
		We need a separate TLS because the one returned by thread_local_visibility_responses
		would get overwritten by light calculations before the call to fill_stencil.
	*/

	thread_local visibility_responses viewed_visibility;

	if (fog_of_war_effective) {
		auto fow_raycasts_scope = cosm.measure_raycasts(profiler.fog_of_war_raycasts);

		messages::visibility_information_request request;
		request.eye_transform = *viewed_character_transform;
		request.filter = predefined_queries::pathfinding();
		request.square_side = static_cast<float>(std::max(screen_size.x, screen_size.y) * 2);
		request.subject = viewed_character;

		auto& requests = thread_local_visibility_requests();
		requests.clear();
		requests.push_back(request);

		visibility_system(DEBUG_LOGIC_STEP_LINES).calc_visibility(cosm, requests, viewed_visibility);
	}

	auto fill_stencil = [&]() {
		if (viewed_visibility.size() > 0 && !viewed_visibility[0].empty()) {
			renderer.enable_stencil();

			renderer.start_writing_stencil();
			renderer.set_clear_color(rgba(0, 0, 0, 0));
			renderer.clear_stencil();

			const auto eye_pos = viewed_character_transform->pos;
			const auto& r = viewed_visibility[0];

			for (std::size_t t = 0; t < r.get_num_triangles(); ++t) {
				const auto world_light_tri = r.get_world_triangle(t, eye_pos);
				augs::vertex_triangle renderable_light_tri;

				renderable_light_tri.vertices[0].pos = world_light_tri[0];
				renderable_light_tri.vertices[1].pos = world_light_tri[1];
				renderable_light_tri.vertices[2].pos = world_light_tri[2];

{'fraction_non_alphanumeric': 0.11487773291688885, 'fraction_numerical': 0.0022853660394606535, 'mean_word_length': 6.30550918196995, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 39, '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': '1212020', 'n_tokens_mistral': 4897, 'n_tokens_neox': 4571, 'n_words': 727}