<commit_before>#pragma once
/* RLTK (RogueLike Tool Kit) 1.00
 * Copyright (c) 2016-Present, <NAME>.
 * Licensed under the LGPL - see LICENSE file.
 *
 * Provides a wrapper for bitmap fonts.
 */

#include <functional>
#include "geometry.hpp"

namespace rltk {

namespace visibility_private {

template<class location_t_>
void internal_2d_sweep(const location_t_ &position, const location_t_ &destination, const int &range, std::function<void(location_t_)> set_visible, 
	std::function<bool(location_t_)> is_opaque)
{
	bool blocked = false;
	line_func(position.x, position.y, destination.x, destination.y, [&blocked, &is_opaque, &set_visible, &range, &position] (int X, int Y) {
		float distance = distance2d(position.x, position.y, X, Y);
		if (distance <= range) {
			location_t_ pos;
			pos.x = X;
			pos.y = Y;
			if (!blocked) set_visible(pos);
			if (!is_opaque(pos)) blocked = true;
		}
	});	
}

}

/* Simple all-direction visibility sweep in 2 dimensions. This requires that your location_t utilize an x and y
 * component. Parameters:
 * position - where you are sweeping from.
 * range - the number of tiles you can traverse.
 * set_visible - a callback (such as bool set_visible(location_t & loc)) to say "this is visible"
 * is_opaque - a callback to ask your map if you can see through a tile.
 */
template<class location_t_>
void visibility_sweep_2d(const location_t_ &position, const int &range, std::function<void(location_t_)> set_visible, 
	std::function<bool(location_t_)> is_opaque)
{
	// You can always see yourself
	set_visible(position);

	// Box-sweep
	for (int i=0-range; i<range; ++i) {

		// Going up
		location_t_ destination;
		destination.x = position.x + i;
		destination.y = position.y - range;
		visibility_private::internal_2d_sweep(position, destination, range, set_visible, is_opaque);
			
		// Going down
		destination.x = position.x + i;
		destination.y = position.y + range;
		visibility_private::internal_2d_sweep(position, destination, range, set_visible, is_opaque);

		// Going left
		destination.x = position.x - range;
		destination.y = position.y + i;
		visibility_private::internal_2d_sweep(position, destination, range, set_visible, is_opaque);

		// Going right
		destination.x = position.x + range;
		destination.y = position.y + i;
		visibility_private::internal_2d_sweep(position, destination, range, set_visible, is_opaque);
	}
}<commit_msg>Improve box-sweep readability by exposing an offset rather than repeatedly calculating a destination.<commit_after>#pragma once
/* RLTK (RogueLike Tool Kit) 1.00
 * Copyright (c) 2016-Present, <NAME>.
 * Licensed under the LGPL - see LICENSE file.
 *
 * Provides a wrapper for bitmap fonts.
 */

#include <functional>
#include "geometry.hpp"

namespace rltk {

namespace visibility_private {

template<class location_t_>
void internal_2d_sweep(const location_t_ &position, const int &range, std::function<void(location_t_)> set_visible, 
	std::function<bool(location_t_)> is_opaque, const std::pair<int,int> offset)
{
	bool blocked = false;
	line_func(position.x, position.y, position.x + offset.first, position.y + offset.second, [&blocked, &is_opaque, &set_visible, &range, &position] (int X, int Y) {
		float distance = distance2d(position.x, position.y, X, Y);
		if (distance <= range) {
			location_t_ pos;
			pos.x = X;
			pos.y = Y;
			if (!blocked) set_visible(pos);
			if (!is_opaque(pos)) blocked = true;
		}
	});	
}

}

/* Simple all-direction visibility sweep in 2 dimensions. This requires that your location_t utilize an x and y
 * component. Parameters:
 * position - where you are sweeping from.
 * range - the number of tiles you can traverse.
 * set_visible - a callback (such as bool set_visible(location_t & loc)) to say "this is visible"
 * is_opaque - a callback to ask your map if you can see through a tile.
 */
template<class location_t_>
void visibility_sweep_2d(const location_t_ &position, const int &range, std::function<void(location_t_)> set_visible, 
	std::function<bool(location_t_)> is_opaque)
{
	// You can always see yourself
	set_visible(position);

	// Box-sweep
	for (int i=0-range; i<range; ++i) {
		visibility_private::internal_2d_sweep(position, range, set_visible, is_opaque, std::make_pair(i, 0-range));
		visibility_private::internal_2d_sweep(position, range, set_visible, is_opaque, std::make_pair(i, range));
		visibility_private::internal_2d_sweep(position, range, set_visible, is_opaque, std::make_pair(0-range, i));
		visibility_private::internal_2d_sweep(position, range, set_visible, is_opaque, std::make_pair(range, i));
	}
}
{'fraction_non_alphanumeric': 0.11791730474732007, 'fraction_numerical': 0.007438197330999781, 'mean_word_length': 4.7873417721518985, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 20, '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': '2805768', 'n_tokens_mistral': 1614, 'n_tokens_neox': 1500, 'n_words': 434}