implement very slow puzzle space exploration
This commit is contained in:
@ -5,6 +5,17 @@
|
||||
|
||||
#define THREADPOOL // Enable physics threadpool
|
||||
|
||||
// TODO: Using the octree from the last frame completely breaks the physics :/
|
||||
// #define ASYNC_OCTREE
|
||||
|
||||
// Gets set by CMake
|
||||
// #define BACKWARD // Enable pretty stack traces
|
||||
// #define TRACY // Enable tracy profiling support
|
||||
|
||||
#ifdef TRACY
|
||||
#include <tracy/Tracy.hpp>
|
||||
#endif
|
||||
|
||||
#ifdef THREADPOOL
|
||||
#if defined(_WIN32)
|
||||
#define NOGDI // All GDI defines and routines
|
||||
@ -19,13 +30,6 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// TODO: Using the octree from the last frame completely breaks the physics :/
|
||||
// #define ASYNC_OCTREE
|
||||
|
||||
// Gets set by CMake
|
||||
// #define BACKWARD // Enable pretty stack traces
|
||||
// #define TRACY // Enable tracy profiling support
|
||||
|
||||
// Window
|
||||
constexpr int INITIAL_WIDTH = 600;
|
||||
constexpr int INITIAL_HEIGHT = 600;
|
||||
|
||||
@ -21,4 +21,4 @@ public:
|
||||
[[nodiscard]] auto get_shortest_path(size_t source) const -> std::vector<size_t>;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
@ -1,5 +1,5 @@
|
||||
#ifndef INPUT_HPP_
|
||||
#define INPUT_HPP_
|
||||
#ifndef INPUT_HANDLER_HPP_
|
||||
#define INPUT_HANDLER_HPP_
|
||||
|
||||
#include "orbit_camera.hpp"
|
||||
#include "state_manager.hpp"
|
||||
|
||||
15
include/load_save.hpp
Normal file
15
include/load_save.hpp
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef LOAD_SAVE_HPP_
|
||||
#define LOAD_SAVE_HPP_
|
||||
|
||||
#include "puzzle.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
auto parse_preset_file(const std::string& preset_file) -> std::pair<std::vector<puzzle>, std::vector<std::string>>;
|
||||
auto append_preset_file(const std::string& preset_file, const std::string& preset_name, const puzzle& p) -> bool;
|
||||
auto append_preset_file_quiet(const std::string& preset_file,
|
||||
const std::string& preset_name,
|
||||
const puzzle& p,
|
||||
bool validate) -> bool;
|
||||
|
||||
#endif
|
||||
@ -4,6 +4,7 @@
|
||||
#include "octree.hpp"
|
||||
#include "config.hpp"
|
||||
|
||||
#include <optional>
|
||||
#include <raylib.h>
|
||||
|
||||
class mass_spring_system
|
||||
@ -20,11 +21,6 @@ public:
|
||||
: a(_a), b(_b) {}
|
||||
};
|
||||
|
||||
private:
|
||||
#ifdef THREADPOOL
|
||||
BS::thread_pool<> threads;
|
||||
#endif
|
||||
|
||||
public:
|
||||
static constexpr int SMALL_TASK_BLOCK_SIZE = 256;
|
||||
static constexpr int LARGE_TASK_BLOCK_SIZE = 256;
|
||||
@ -40,22 +36,13 @@ public:
|
||||
std::vector<spring> springs;
|
||||
|
||||
public:
|
||||
mass_spring_system()
|
||||
#ifdef THREADPOOL
|
||||
: threads(std::thread::hardware_concurrency() - 2, set_mass_springs_pool_thread_name)
|
||||
#endif
|
||||
{}
|
||||
mass_spring_system() {}
|
||||
|
||||
mass_spring_system(const mass_spring_system& copy) = delete;
|
||||
auto operator=(const mass_spring_system& copy) -> mass_spring_system& = delete;
|
||||
mass_spring_system(mass_spring_system& move) = delete;
|
||||
auto operator=(mass_spring_system&& move) -> mass_spring_system& = delete;
|
||||
|
||||
private:
|
||||
#ifdef THREADPOOL
|
||||
static auto set_mass_springs_pool_thread_name(size_t idx) -> void;
|
||||
#endif
|
||||
|
||||
public:
|
||||
auto clear() -> void;
|
||||
auto add_mass() -> void;
|
||||
@ -63,14 +50,14 @@ public:
|
||||
|
||||
auto clear_forces() -> void;
|
||||
auto calculate_spring_force(size_t s) -> void;
|
||||
auto calculate_spring_forces() -> void;
|
||||
auto calculate_repulsion_forces() -> void;
|
||||
auto calculate_spring_forces(std::optional<BS::thread_pool<>* const> thread_pool = std::nullopt) -> void;
|
||||
auto calculate_repulsion_forces(std::optional<BS::thread_pool<>* const> thread_pool = std::nullopt) -> void;
|
||||
auto integrate_velocity(size_t m, float dt) -> void;
|
||||
auto integrate_position(size_t m, float dt) -> void;
|
||||
auto verlet_update(size_t m, float dt) -> void;
|
||||
auto update(float dt) -> void;
|
||||
auto update(float dt, std::optional<BS::thread_pool<>* const> thread_pool = std::nullopt) -> void;
|
||||
|
||||
auto center_masses() -> void;
|
||||
auto center_masses(std::optional<BS::thread_pool<>* const> thread_pool = std::nullopt) -> void;
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -2,9 +2,10 @@
|
||||
#define OCTREE_HPP_
|
||||
|
||||
#include <array>
|
||||
#include <vector>
|
||||
|
||||
#include <raylib.h>
|
||||
#include <raymath.h>
|
||||
#include <vector>
|
||||
|
||||
class octree
|
||||
{
|
||||
|
||||
@ -28,4 +28,4 @@ public:
|
||||
bool mass_center_lock) -> void;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
@ -1,16 +1,24 @@
|
||||
#ifndef PUZZLE_HPP_
|
||||
#define PUZZLE_HPP_
|
||||
|
||||
#include "config.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <format>
|
||||
#include <functional>
|
||||
#include <ranges>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <bits/fs_fwd.h>
|
||||
#include <boost/unordered/unordered_flat_set.hpp>
|
||||
|
||||
// #define RUNTIME_CHECKS
|
||||
|
||||
// Forward declare to use in puzzle member functions
|
||||
struct block_hasher;
|
||||
struct block_hasher2;
|
||||
struct block_equal2;
|
||||
struct puzzle_hasher;
|
||||
|
||||
/*
|
||||
* 8x8 board
|
||||
@ -125,29 +133,33 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
[[nodiscard]] static auto create_repr(uint8_t x, uint8_t y, uint8_t w, uint8_t h, bool t = false,
|
||||
bool i = false) -> uint16_t;
|
||||
|
||||
// Repr setters
|
||||
[[nodiscard]] auto set_x(uint8_t x) const -> block;
|
||||
[[nodiscard]] auto set_y(uint8_t y) const -> block;
|
||||
[[nodiscard]] auto set_width(uint8_t width) const -> block;
|
||||
[[nodiscard]] auto set_height(uint8_t height) const -> block;
|
||||
[[nodiscard]] auto set_target(bool target) const -> block;
|
||||
[[nodiscard]] auto set_immovable(bool immovable) const -> block;
|
||||
[[nodiscard]] static auto create_repr(uint8_t x,
|
||||
uint8_t y,
|
||||
uint8_t w,
|
||||
uint8_t h,
|
||||
bool t = false,
|
||||
bool i = false) -> uint16_t;
|
||||
[[nodiscard]] inline auto set_x(uint8_t x) const -> block;
|
||||
[[nodiscard]] inline auto set_y(uint8_t y) const -> block;
|
||||
[[nodiscard]] inline auto set_width(uint8_t width) const -> block;
|
||||
[[nodiscard]] inline auto set_height(uint8_t height) const -> block;
|
||||
[[nodiscard]] inline auto set_target(bool target) const -> block;
|
||||
[[nodiscard]] inline auto set_immovable(bool immovable) const -> block;
|
||||
|
||||
public:
|
||||
[[nodiscard]] auto unpack_repr() const -> std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, bool, bool>;
|
||||
|
||||
// Repr getters
|
||||
[[nodiscard]] auto get_x() const -> uint8_t;
|
||||
[[nodiscard]] auto get_y() const -> uint8_t;
|
||||
[[nodiscard]] auto get_width() const -> uint8_t;
|
||||
[[nodiscard]] auto get_height() const -> uint8_t;
|
||||
[[nodiscard]] auto get_target() const -> bool;
|
||||
[[nodiscard]] auto get_immovable() const -> bool;
|
||||
[[nodiscard]] auto unpack_repr() const -> std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, bool, bool>;
|
||||
[[nodiscard]] inline auto get_x() const -> uint8_t;
|
||||
[[nodiscard]] inline auto get_y() const -> uint8_t;
|
||||
[[nodiscard]] inline auto get_width() const -> uint8_t;
|
||||
[[nodiscard]] inline auto get_height() const -> uint8_t;
|
||||
[[nodiscard]] inline auto get_target() const -> bool;
|
||||
[[nodiscard]] inline auto get_immovable() const -> bool;
|
||||
|
||||
// Util
|
||||
[[nodiscard]] auto hash() const -> size_t;
|
||||
[[nodiscard]] auto position_independent_hash() const -> size_t;
|
||||
[[nodiscard]] auto valid() const -> bool;
|
||||
[[nodiscard]] auto principal_dirs() const -> uint8_t;
|
||||
[[nodiscard]] auto covers(int _x, int _y) const -> bool;
|
||||
@ -199,7 +211,8 @@ private:
|
||||
// repr_cooked() = delete;
|
||||
// repr_cooked(const repr_cooked& copy) = delete;
|
||||
// repr_cooked(repr_cooked&& move) = delete;
|
||||
} __attribute__((packed));
|
||||
}
|
||||
PACKED;
|
||||
|
||||
/**
|
||||
* With gcc, were allowed to acces the members arbitrarily, even if they're not active (not the ones last written):
|
||||
@ -247,7 +260,12 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
puzzle(const uint8_t w, const uint8_t h, const uint8_t tx, const uint8_t ty, const bool r, const bool g,
|
||||
puzzle(const uint8_t w,
|
||||
const uint8_t h,
|
||||
const uint8_t tx,
|
||||
const uint8_t ty,
|
||||
const bool r,
|
||||
const bool g,
|
||||
const std::array<uint16_t, MAX_BLOCKS>& b)
|
||||
: repr(create_repr(w, h, tx, ty, r, g, b))
|
||||
{
|
||||
@ -259,6 +277,14 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
puzzle(const uint8_t w, const uint8_t h)
|
||||
: repr(create_repr(w, h, 0, 0, false, false, invalid_blocks()))
|
||||
{
|
||||
if (w < MIN_WIDTH || w > MAX_WIDTH || h < MIN_HEIGHT || h > MAX_HEIGHT) {
|
||||
throw std::invalid_argument("Board size out of bounds");
|
||||
}
|
||||
}
|
||||
|
||||
explicit puzzle(const std::string& string_repr)
|
||||
: repr(create_repr(string_repr)) {}
|
||||
|
||||
@ -331,33 +357,38 @@ private:
|
||||
return blocks;
|
||||
}
|
||||
|
||||
[[nodiscard]] static auto create_meta(const std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, bool, bool>& meta) -> uint16_t;
|
||||
[[nodiscard]] static auto create_repr(uint8_t w, uint8_t h, uint8_t tx, uint8_t ty, bool r, bool g,
|
||||
const std::array<uint16_t, MAX_BLOCKS>& b) -> repr_cooked;
|
||||
|
||||
[[nodiscard]] static auto create_repr(uint64_t byte_0, uint64_t byte_1, uint64_t byte_2,
|
||||
uint64_t byte_3) -> repr_cooked;
|
||||
|
||||
[[nodiscard]] static auto create_repr(const std::string& string_repr) -> repr_cooked;
|
||||
|
||||
// Repr setters
|
||||
[[nodiscard]] auto set_restricted(bool restricted) const -> puzzle;
|
||||
[[nodiscard]] auto set_width(uint8_t width) const -> puzzle;
|
||||
[[nodiscard]] auto set_height(uint8_t height) const -> puzzle;
|
||||
[[nodiscard]] auto set_goal(bool goal) const -> puzzle;
|
||||
[[nodiscard]] auto set_goal_x(uint8_t target_x) const -> puzzle;
|
||||
[[nodiscard]] auto set_goal_y(uint8_t target_y) const -> puzzle;
|
||||
[[nodiscard]] static auto create_meta(
|
||||
const std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, bool, bool>& meta) -> uint16_t;
|
||||
[[nodiscard]] static auto create_repr(uint8_t w,
|
||||
uint8_t h,
|
||||
uint8_t tx,
|
||||
uint8_t ty,
|
||||
bool r,
|
||||
bool g,
|
||||
const std::array<uint16_t, MAX_BLOCKS>& b) -> repr_cooked;
|
||||
[[nodiscard]] static auto create_repr(uint64_t byte_0,
|
||||
uint64_t byte_1,
|
||||
uint64_t byte_2,
|
||||
uint64_t byte_3) -> repr_cooked;
|
||||
[[nodiscard]] static auto create_repr(const std::string& string_repr) -> repr_cooked;
|
||||
[[nodiscard]] inline auto set_restricted(bool restricted) const -> puzzle;
|
||||
[[nodiscard]] inline auto set_width(uint8_t width) const -> puzzle;
|
||||
[[nodiscard]] inline auto set_height(uint8_t height) const -> puzzle;
|
||||
[[nodiscard]] inline auto set_goal(bool goal) const -> puzzle;
|
||||
[[nodiscard]] inline auto set_goal_x(uint8_t target_x) const -> puzzle;
|
||||
[[nodiscard]] inline auto set_goal_y(uint8_t target_y) const -> puzzle;
|
||||
[[nodiscard]] auto set_blocks(std::array<uint16_t, MAX_BLOCKS> blocks) const -> puzzle;
|
||||
|
||||
public:
|
||||
// Repr getters
|
||||
[[nodiscard]] auto unpack_meta() const -> std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, bool, bool>;
|
||||
[[nodiscard]] auto get_restricted() const -> bool;
|
||||
[[nodiscard]] auto get_width() const -> uint8_t;
|
||||
[[nodiscard]] auto get_height() const -> uint8_t;
|
||||
[[nodiscard]] auto get_goal() const -> bool;
|
||||
[[nodiscard]] auto get_goal_x() const -> uint8_t;
|
||||
[[nodiscard]] auto get_goal_y() const -> uint8_t;
|
||||
[[nodiscard]] inline auto get_restricted() const -> bool;
|
||||
[[nodiscard]] inline auto get_width() const -> uint8_t;
|
||||
[[nodiscard]] inline auto get_height() const -> uint8_t;
|
||||
[[nodiscard]] inline auto get_goal() const -> bool;
|
||||
[[nodiscard]] inline auto get_goal_x() const -> uint8_t;
|
||||
[[nodiscard]] inline auto get_goal_y() const -> uint8_t;
|
||||
|
||||
// Util
|
||||
[[nodiscard]] auto hash() const -> size_t;
|
||||
@ -390,19 +421,46 @@ public:
|
||||
[[nodiscard]] auto try_move_block_at(uint8_t x, uint8_t y, direction dir) const -> std::optional<puzzle>;
|
||||
|
||||
// Statespace
|
||||
[[nodiscard]] auto try_move_block_at_fast(uint64_t bitmap, uint8_t block_idx,
|
||||
direction dir) const -> std::optional<puzzle>;
|
||||
static auto sorted_replace(std::array<uint16_t, MAX_BLOCKS> blocks, uint8_t idx,
|
||||
uint16_t new_val) -> std::array<uint16_t, MAX_BLOCKS>;
|
||||
auto blocks_bitmap() const -> uint64_t;
|
||||
static inline auto bitmap_set_bit(uint64_t bitmap, uint8_t x, uint8_t y) -> uint64_t;
|
||||
static inline auto bitmap_get_bit(uint64_t bitmap, uint8_t x, uint8_t y) -> bool;
|
||||
static auto bitmap_clear_block(uint64_t bitmap, block b) -> uint64_t;
|
||||
static auto bitmap_check_collision(uint64_t bitmap, block b) -> bool;
|
||||
static auto bitmap_check_collision(uint64_t bitmap, block b, direction dir) -> bool;
|
||||
[[nodiscard]] INLINE inline auto try_move_block_at_fast(uint64_t bitmap,
|
||||
uint8_t block_idx,
|
||||
direction dir,
|
||||
bool check_collision = true) const -> std::optional<puzzle>;
|
||||
[[nodiscard]] static auto sorted_replace(std::array<uint16_t, MAX_BLOCKS> blocks,
|
||||
uint8_t idx,
|
||||
uint16_t new_val) -> std::array<uint16_t, MAX_BLOCKS>;
|
||||
[[nodiscard]] auto blocks_bitmap() const -> uint64_t;
|
||||
[[nodiscard]] auto blocks_bitmap_h() const -> uint64_t;
|
||||
[[nodiscard]] auto blocks_bitmap_v() const -> uint64_t;
|
||||
static INLINE inline auto bitmap_clear_bit(uint64_t& bitmap, uint8_t w, uint8_t x, uint8_t y) -> void;
|
||||
static INLINE inline auto bitmap_set_bit(uint64_t& bitmap, uint8_t w, uint8_t x, uint8_t y) -> void;
|
||||
[[nodiscard]] static INLINE inline auto bitmap_get_bit(uint64_t bitmap, uint8_t w, uint8_t x, uint8_t y) -> bool;
|
||||
INLINE inline auto bitmap_clear_block(uint64_t& bitmap, block b) const -> void;
|
||||
INLINE inline auto bitmap_set_block(uint64_t& bitmap, block b) const -> void;
|
||||
[[nodiscard]] INLINE inline auto bitmap_is_empty(uint64_t bitmap) const -> bool;
|
||||
[[nodiscard]] INLINE inline auto bitmap_is_full(uint64_t bitmap) const -> bool;
|
||||
|
||||
/**
|
||||
* Checks if b would collide with any block on the board.
|
||||
*
|
||||
* @param bitmap Board occupancy map
|
||||
* @param b Hypothetical block to check collision with
|
||||
* @return True if b would collide with any other block on the board
|
||||
*/
|
||||
[[nodiscard]] INLINE inline auto bitmap_check_collision(uint64_t bitmap, block b) const -> bool;
|
||||
|
||||
/**
|
||||
* Checks if b would collide with any block on the board after moving in direction dir.
|
||||
*
|
||||
* @param bitmap Board occupancy map
|
||||
* @param b Existing block to check collision with
|
||||
* @param dir Direction in which the block should be moved
|
||||
* @return True if b would collide with any other block on the board after moving in direction dir
|
||||
*/
|
||||
[[nodiscard]] INLINE inline auto bitmap_check_collision(uint64_t bitmap, block b, direction dir) const -> bool;
|
||||
|
||||
template <typename F>
|
||||
auto for_each_adjacent(F&& callback) const -> void
|
||||
// ReSharper disable once CppRedundantInlineSpecifier
|
||||
INLINE inline auto for_each_adjacent(F&& callback) const -> void
|
||||
{
|
||||
const uint64_t bitmap = blocks_bitmap();
|
||||
const bool r = get_restricted();
|
||||
@ -427,9 +485,444 @@ public:
|
||||
|
||||
[[nodiscard]] auto explore_state_space() const
|
||||
-> std::pair<std::vector<puzzle>, std::vector<std::pair<size_t, size_t>>>;
|
||||
|
||||
// Determines to which cluster a puzzle belongs. Clusters are identified by the
|
||||
// state with the numerically smallest binary representation.
|
||||
[[nodiscard]] auto get_cluster_id_and_solution() const -> std::pair<puzzle, bool>;
|
||||
|
||||
[[nodiscard]] auto bitmap_find_first_empty(uint64_t bitmap, int& x, int& y) const -> bool;
|
||||
|
||||
static auto generate_block_sequences(
|
||||
const boost::unordered_flat_set<block, block_hasher2, block_equal2>& permitted_blocks,
|
||||
block target_block,
|
||||
size_t max_blocks,
|
||||
std::vector<block>& current_sequence,
|
||||
int current_area,
|
||||
int board_area,
|
||||
const std::function<void(const std::vector<block>&)>& callback) -> void;
|
||||
|
||||
static auto place_block_sequence(const puzzle& p,
|
||||
const uint64_t& bitmap,
|
||||
const std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, bool, bool>& p_repr,
|
||||
const std::vector<block>& sequence,
|
||||
block target_block,
|
||||
const std::tuple<uint8_t, uint8_t, uint8_t, uint8_t>& target_block_pos_range,
|
||||
bool has_target,
|
||||
size_t index,
|
||||
const std::function<void(const puzzle&)>& callback) -> void;
|
||||
|
||||
[[nodiscard]] auto explore_puzzle_space(
|
||||
const boost::unordered_flat_set<block, block_hasher2, block_equal2>& permitted_blocks,
|
||||
block target_block,
|
||||
const std::tuple<uint8_t, uint8_t, uint8_t, uint8_t>& target_block_pos_range,
|
||||
size_t max_blocks,
|
||||
std::optional<BS::thread_pool<>* const> thread_pool = std::nullopt) const -> boost::unordered_flat_set<
|
||||
puzzle, puzzle_hasher>;
|
||||
};
|
||||
|
||||
// Hash functions for sets and maps
|
||||
// Inline functions definitions
|
||||
#ifndef REGION_INLINE_DEFS
|
||||
|
||||
inline auto puzzle::block::set_x(const uint8_t x) const -> block
|
||||
{
|
||||
#ifdef RUNTIME_CHECKS
|
||||
if (x > 7) {
|
||||
throw std::invalid_argument("Block x-position out of bounds");
|
||||
}
|
||||
#endif
|
||||
|
||||
block b = *this;
|
||||
set_bits(b.repr, X_S, X_E, x);
|
||||
return b;
|
||||
}
|
||||
|
||||
inline auto puzzle::block::set_y(const uint8_t y) const -> block
|
||||
{
|
||||
#ifdef RUNTIME_CHECKS
|
||||
if (y > 7) {
|
||||
throw std::invalid_argument("Block y-position out of bounds");
|
||||
}
|
||||
#endif
|
||||
|
||||
block b = *this;
|
||||
set_bits(b.repr, Y_S, Y_E, y);
|
||||
return b;
|
||||
}
|
||||
|
||||
inline auto puzzle::block::set_width(const uint8_t width) const -> block
|
||||
{
|
||||
#ifdef RUNTIME_CHECKS
|
||||
if (width - 1 > 7) {
|
||||
throw std::invalid_argument("Block width out of bounds");
|
||||
}
|
||||
#endif
|
||||
|
||||
block b = *this;
|
||||
set_bits(b.repr, WIDTH_S, WIDTH_E, width - 1u);
|
||||
return b;
|
||||
}
|
||||
|
||||
inline auto puzzle::block::set_height(const uint8_t height) const -> block
|
||||
{
|
||||
#ifdef RUNTIME_CHECKS
|
||||
if (height - 1 > 7) {
|
||||
throw std::invalid_argument("Block height out of bounds");
|
||||
}
|
||||
#endif
|
||||
|
||||
block b = *this;
|
||||
set_bits(b.repr, HEIGHT_S, HEIGHT_E, height - 1u);
|
||||
return b;
|
||||
}
|
||||
|
||||
inline auto puzzle::block::set_target(const bool target) const -> block
|
||||
{
|
||||
block b = *this;
|
||||
set_bits(b.repr, TARGET_S, TARGET_E, target);
|
||||
return b;
|
||||
}
|
||||
|
||||
inline auto puzzle::block::set_immovable(const bool immovable) const -> block
|
||||
{
|
||||
block b = *this;
|
||||
set_bits(b.repr, IMMOVABLE_S, IMMOVABLE_E, immovable);
|
||||
return b;
|
||||
}
|
||||
|
||||
inline auto puzzle::block::get_x() const -> uint8_t
|
||||
{
|
||||
return get_bits(repr, X_S, X_E);
|
||||
}
|
||||
|
||||
inline auto puzzle::block::get_y() const -> uint8_t
|
||||
{
|
||||
return get_bits(repr, Y_S, Y_E);
|
||||
}
|
||||
|
||||
inline auto puzzle::block::get_width() const -> uint8_t
|
||||
{
|
||||
return get_bits(repr, WIDTH_S, WIDTH_E) + 1u;
|
||||
}
|
||||
|
||||
inline auto puzzle::block::get_height() const -> uint8_t
|
||||
{
|
||||
return get_bits(repr, HEIGHT_S, HEIGHT_E) + 1u;
|
||||
}
|
||||
|
||||
inline auto puzzle::block::get_target() const -> bool
|
||||
{
|
||||
return get_bits(repr, TARGET_S, TARGET_E);
|
||||
}
|
||||
|
||||
inline auto puzzle::block::get_immovable() const -> bool
|
||||
{
|
||||
return get_bits(repr, IMMOVABLE_S, IMMOVABLE_E);
|
||||
}
|
||||
|
||||
inline auto puzzle::set_restricted(const bool restricted) const -> puzzle
|
||||
{
|
||||
uint16_t meta = repr.cooked.meta;
|
||||
set_bits(meta, RESTRICTED_S, RESTRICTED_E, restricted);
|
||||
return puzzle(meta, repr.cooked.blocks);
|
||||
}
|
||||
|
||||
inline auto puzzle::set_width(const uint8_t width) const -> puzzle
|
||||
{
|
||||
#ifdef RUNTIME_CHECKS
|
||||
if (width - 1 > MAX_WIDTH) {
|
||||
throw "Board width out of bounds";
|
||||
}
|
||||
#endif
|
||||
|
||||
uint16_t meta = repr.cooked.meta;
|
||||
set_bits(meta, WIDTH_S, WIDTH_E, width - 1u);
|
||||
return puzzle(meta, repr.cooked.blocks);
|
||||
}
|
||||
|
||||
inline auto puzzle::set_height(const uint8_t height) const -> puzzle
|
||||
{
|
||||
#ifdef RUNTIME_CHECKS
|
||||
if (height - 1 > MAX_HEIGHT) {
|
||||
throw "Board height out of bounds";
|
||||
}
|
||||
#endif
|
||||
|
||||
uint16_t meta = repr.cooked.meta;
|
||||
set_bits(meta, HEIGHT_S, HEIGHT_E, height - 1u);
|
||||
return puzzle(meta, repr.cooked.blocks);
|
||||
}
|
||||
|
||||
inline auto puzzle::set_goal(const bool goal) const -> puzzle
|
||||
{
|
||||
uint16_t meta = repr.cooked.meta;
|
||||
set_bits(meta, GOAL_S, GOAL_E, goal);
|
||||
return puzzle(meta, repr.cooked.blocks);
|
||||
}
|
||||
|
||||
inline auto puzzle::set_goal_x(const uint8_t target_x) const -> puzzle
|
||||
{
|
||||
#ifdef RUNTIME_CHECKS
|
||||
if (target_x >= MAX_WIDTH) {
|
||||
throw "Board target x out of bounds";
|
||||
}
|
||||
#endif
|
||||
|
||||
uint16_t meta = repr.cooked.meta;
|
||||
set_bits(meta, GOAL_X_S, GOAL_X_E, target_x);
|
||||
return puzzle(meta, repr.cooked.blocks);
|
||||
}
|
||||
|
||||
inline auto puzzle::set_goal_y(const uint8_t target_y) const -> puzzle
|
||||
{
|
||||
#ifdef RUNTIME_CHECKS
|
||||
if (target_y >= MAX_HEIGHT) {
|
||||
throw "Board target y out of bounds";
|
||||
}
|
||||
#endif
|
||||
|
||||
uint16_t meta = repr.cooked.meta;
|
||||
set_bits(meta, GOAL_Y_S, GOAL_Y_E, target_y);
|
||||
return puzzle(meta, repr.cooked.blocks);
|
||||
}
|
||||
|
||||
inline auto puzzle::get_restricted() const -> bool
|
||||
{
|
||||
return get_bits(repr.cooked.meta, RESTRICTED_S, RESTRICTED_E);
|
||||
}
|
||||
|
||||
inline auto puzzle::get_width() const -> uint8_t
|
||||
{
|
||||
return get_bits(repr.cooked.meta, WIDTH_S, WIDTH_E) + 1u;
|
||||
}
|
||||
|
||||
inline auto puzzle::get_height() const -> uint8_t
|
||||
{
|
||||
return get_bits(repr.cooked.meta, HEIGHT_S, HEIGHT_E) + 1u;
|
||||
}
|
||||
|
||||
inline auto puzzle::get_goal() const -> bool
|
||||
{
|
||||
return get_bits(repr.cooked.meta, GOAL_S, GOAL_E);
|
||||
}
|
||||
|
||||
inline auto puzzle::get_goal_x() const -> uint8_t
|
||||
{
|
||||
return get_bits(repr.cooked.meta, GOAL_X_S, GOAL_X_E);
|
||||
}
|
||||
|
||||
inline auto puzzle::get_goal_y() const -> uint8_t
|
||||
{
|
||||
return get_bits(repr.cooked.meta, GOAL_Y_S, GOAL_Y_E);
|
||||
}
|
||||
|
||||
INLINE inline auto puzzle::try_move_block_at_fast(uint64_t bitmap,
|
||||
const uint8_t block_idx,
|
||||
const direction dir,
|
||||
const bool check_collision) const -> std::optional<puzzle>
|
||||
{
|
||||
const block b = block(repr.cooked.blocks[block_idx]);
|
||||
const auto [bx, by, bw, bh, bt, bi] = b.unpack_repr();
|
||||
if (bi) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const auto [w, h, gx, gy, r, g] = unpack_meta();
|
||||
const int dirs = r ? b.principal_dirs() : nor | eas | sou | wes;
|
||||
|
||||
// Get target block
|
||||
int _target_x = bx;
|
||||
int _target_y = by;
|
||||
switch (dir) {
|
||||
case nor:
|
||||
if (!(dirs & nor) || _target_y < 1) {
|
||||
return std::nullopt;
|
||||
}
|
||||
--_target_y;
|
||||
break;
|
||||
case eas:
|
||||
if (!(dirs & eas) || _target_x + bw >= w) {
|
||||
return std::nullopt;
|
||||
}
|
||||
++_target_x;
|
||||
break;
|
||||
case sou:
|
||||
if (!(dirs & sou) || _target_y + bh >= h) {
|
||||
return std::nullopt;
|
||||
}
|
||||
++_target_y;
|
||||
break;
|
||||
case wes:
|
||||
if (!(dirs & wes) || _target_x < 1) {
|
||||
return std::nullopt;
|
||||
}
|
||||
--_target_x;
|
||||
break;
|
||||
}
|
||||
|
||||
// Check collisions
|
||||
if (check_collision) {
|
||||
bitmap_clear_block(bitmap, b);
|
||||
if (bitmap_check_collision(bitmap, b, dir)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
// Replace block
|
||||
const std::array<uint16_t, MAX_BLOCKS> blocks = sorted_replace(repr.cooked.blocks,
|
||||
block_idx,
|
||||
block::create_repr(
|
||||
_target_x,
|
||||
_target_y,
|
||||
bw,
|
||||
bh,
|
||||
bt));
|
||||
|
||||
// This constructor doesn't sort
|
||||
return puzzle(std::make_tuple(w, h, gx, gy, r, g), blocks);
|
||||
}
|
||||
|
||||
INLINE inline auto puzzle::bitmap_clear_bit(uint64_t& bitmap, const uint8_t w, const uint8_t x, const uint8_t y) -> void
|
||||
{
|
||||
set_bits(bitmap, y * w + x, y * w + x, 0u);
|
||||
}
|
||||
|
||||
INLINE inline auto puzzle::bitmap_set_bit(uint64_t& bitmap, const uint8_t w, const uint8_t x, const uint8_t y) -> void
|
||||
{
|
||||
set_bits(bitmap, y * w + x, y * w + x, 1u);
|
||||
}
|
||||
|
||||
INLINE inline auto puzzle::bitmap_get_bit(const uint64_t bitmap,
|
||||
const uint8_t w,
|
||||
const uint8_t x,
|
||||
const uint8_t y) -> bool
|
||||
{
|
||||
return get_bits(bitmap, y * w + x, y * w + x);
|
||||
}
|
||||
|
||||
INLINE inline auto puzzle::bitmap_clear_block(uint64_t& bitmap, const block b) const -> void
|
||||
{
|
||||
const auto [x, y, w, h, t, i] = b.unpack_repr();
|
||||
const uint8_t width = get_width();
|
||||
|
||||
for (int dy = 0; dy < h; ++dy) {
|
||||
for (int dx = 0; dx < w; ++dx) {
|
||||
bitmap_clear_bit(bitmap, width, x + dx, y + dy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
INLINE inline auto puzzle::bitmap_set_block(uint64_t& bitmap, const block b) const -> void
|
||||
{
|
||||
const auto [x, y, w, h, t, i] = b.unpack_repr();
|
||||
const uint8_t width = get_width();
|
||||
|
||||
for (int dy = 0; dy < h; ++dy) {
|
||||
for (int dx = 0; dx < w; ++dx) {
|
||||
bitmap_set_bit(bitmap, width, x + dx, y + dy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
INLINE inline auto puzzle::bitmap_is_empty(const uint64_t bitmap) const -> bool
|
||||
{
|
||||
const uint8_t shift = 64 - get_width() * get_height();
|
||||
return bitmap << shift == 0;
|
||||
}
|
||||
|
||||
INLINE inline auto puzzle::bitmap_is_full(const uint64_t bitmap) const -> bool
|
||||
{
|
||||
const uint8_t shift = 64 - get_width() * get_height();
|
||||
return ((bitmap << shift) >> shift) == ((static_cast<uint64_t>(-1) << shift) >> shift);
|
||||
}
|
||||
|
||||
INLINE inline auto puzzle::bitmap_check_collision(const uint64_t bitmap, const block b) const -> bool
|
||||
{
|
||||
const auto [x, y, w, h, t, i] = b.unpack_repr();
|
||||
const uint8_t width = get_width();
|
||||
|
||||
for (int dy = 0; dy < h; ++dy) {
|
||||
for (int dx = 0; dx < w; ++dx) {
|
||||
if (bitmap_get_bit(bitmap, width, x + dx, y + dy)) {
|
||||
return true; // collision
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
INLINE inline auto puzzle::bitmap_check_collision(const uint64_t bitmap,
|
||||
const block b,
|
||||
const direction dir) const -> bool
|
||||
{
|
||||
const auto [x, y, w, h, t, i] = b.unpack_repr();
|
||||
const uint8_t width = get_width();
|
||||
|
||||
switch (dir) {
|
||||
case nor: // Check the row above: (x...x+w-1, y-1)
|
||||
for (int dx = 0; dx < w; ++dx) {
|
||||
if (bitmap_get_bit(bitmap, width, x + dx, y - 1)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case sou: // Check the row below: (x...x+w-1, y+h)
|
||||
for (int dx = 0; dx < w; ++dx) {
|
||||
if (bitmap_get_bit(bitmap, width, x + dx, y + h)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case wes: // Check the column left: (x-1, y...y+h-1)
|
||||
for (int dy = 0; dy < h; ++dy) {
|
||||
if (bitmap_get_bit(bitmap, width, x - 1, y + dy)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case eas: // Check the column right: (x+w, y...y+h-1)
|
||||
for (int dy = 0; dy < h; ++dy) {
|
||||
if (bitmap_get_bit(bitmap, width, x + w, y + dy)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// Hash functions for sets and maps.
|
||||
// Declared after puzzle class to use puzzle::hash_combine
|
||||
#ifndef REGION_HASHERS
|
||||
|
||||
struct block_hasher
|
||||
{
|
||||
auto operator()(const puzzle::block& b) const noexcept -> size_t
|
||||
{
|
||||
return b.hash();
|
||||
}
|
||||
};
|
||||
|
||||
struct block_hasher2
|
||||
{
|
||||
auto operator()(const puzzle::block& b) const noexcept -> size_t
|
||||
{
|
||||
return b.position_independent_hash();
|
||||
}
|
||||
};
|
||||
|
||||
struct block_equal2
|
||||
{
|
||||
auto operator()(const puzzle::block& a, const puzzle::block& b) const noexcept -> bool
|
||||
{
|
||||
const auto [ax, ay, aw, ah, at, ai] = a.unpack_repr();
|
||||
const auto [bx, by, bw, bh, bt, bi] = b.unpack_repr();
|
||||
|
||||
return aw == bw && ah == bh && at == bt && ai == bi;
|
||||
}
|
||||
};
|
||||
|
||||
struct puzzle_hasher
|
||||
{
|
||||
@ -453,7 +946,7 @@ struct link_hasher
|
||||
}
|
||||
};
|
||||
|
||||
struct link_equal_to
|
||||
struct link_equal
|
||||
{
|
||||
auto operator()(const std::pair<puzzle, puzzle>& a, const std::pair<puzzle, puzzle>& b) const noexcept -> bool
|
||||
{
|
||||
@ -462,9 +955,9 @@ struct link_equal_to
|
||||
};
|
||||
|
||||
template <typename T, typename... Rest>
|
||||
auto puzzle::hash_combine(std::size_t& seed, const T& v, const Rest&... rest) -> void
|
||||
auto puzzle::hash_combine(size_t& seed, const T& v, const Rest&... rest) -> void
|
||||
{
|
||||
auto h = []<typename HashedType>(const HashedType& val) -> std::size_t
|
||||
auto hasher = []<typename HashedType>(const HashedType& val) -> std::size_t
|
||||
{
|
||||
if constexpr (std::is_same_v<std::decay_t<HashedType>, puzzle>) {
|
||||
return puzzle_hasher{}(val);
|
||||
@ -475,8 +968,10 @@ auto puzzle::hash_combine(std::size_t& seed, const T& v, const Rest&... rest) ->
|
||||
}
|
||||
};
|
||||
|
||||
seed ^= h(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
|
||||
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
|
||||
(hash_combine(seed, rest), ...);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@ -1,8 +1,8 @@
|
||||
#ifndef RENDERER_HPP_
|
||||
#define RENDERER_HPP_
|
||||
|
||||
#include "orbit_camera.hpp"
|
||||
#include "config.hpp"
|
||||
#include "orbit_camera.hpp"
|
||||
#include "input_handler.hpp"
|
||||
#include "state_manager.hpp"
|
||||
#include "user_interface.hpp"
|
||||
@ -18,7 +18,7 @@ private:
|
||||
user_interface& gui;
|
||||
|
||||
const orbit_camera& camera;
|
||||
RenderTexture render_target =
|
||||
RenderTexture graph_target =
|
||||
LoadRenderTexture(GetScreenWidth() / 2, GetScreenHeight() - MENU_HEIGHT);
|
||||
|
||||
// TODO: Those should be moved to the user_interface.h
|
||||
@ -81,7 +81,7 @@ public:
|
||||
|
||||
~renderer()
|
||||
{
|
||||
UnloadRenderTexture(render_target);
|
||||
UnloadRenderTexture(graph_target);
|
||||
UnloadRenderTexture(klotski_target);
|
||||
UnloadRenderTexture(menu_target);
|
||||
|
||||
|
||||
@ -2,10 +2,10 @@
|
||||
#define STATE_MANAGER_HPP_
|
||||
|
||||
#include "graph_distances.hpp"
|
||||
#include "load_save.hpp"
|
||||
#include "threaded_physics.hpp"
|
||||
#include "puzzle.hpp"
|
||||
|
||||
#include <stack>
|
||||
#include <boost/unordered/unordered_flat_map.hpp>
|
||||
#include <boost/unordered/unordered_flat_set.hpp>
|
||||
|
||||
@ -43,10 +43,9 @@ private:
|
||||
|
||||
public:
|
||||
state_manager(threaded_physics& _physics, const std::string& _preset_file)
|
||||
: physics(_physics)
|
||||
: physics(_physics), preset_file(_preset_file)
|
||||
{
|
||||
parse_preset_file(_preset_file);
|
||||
load_preset(0);
|
||||
reload_preset_file();
|
||||
}
|
||||
|
||||
state_manager(const state_manager& copy) = delete;
|
||||
@ -95,15 +94,13 @@ private:
|
||||
|
||||
public:
|
||||
// Presets
|
||||
|
||||
auto parse_preset_file(const std::string& _preset_file) -> bool;
|
||||
auto append_preset_file(const std::string& preset_name) -> bool;
|
||||
auto save_current_to_preset_file(const std::string& preset_comment) -> void;
|
||||
auto reload_preset_file() -> void;
|
||||
auto load_preset(size_t preset) -> void;
|
||||
auto load_previous_preset() -> void;
|
||||
auto load_next_preset() -> void;
|
||||
|
||||
// Update current_state
|
||||
|
||||
auto update_current_state(const puzzle& p) -> void;
|
||||
auto edit_starting_state(const puzzle& p) -> void;
|
||||
auto goto_starting_state() -> void;
|
||||
@ -113,7 +110,6 @@ public:
|
||||
auto goto_closest_target_state() -> void;
|
||||
|
||||
// Update graph
|
||||
|
||||
auto populate_graph() -> void;
|
||||
auto clear_graph_and_add_current(const puzzle& p) -> void;
|
||||
auto clear_graph_and_add_current() -> void;
|
||||
@ -122,7 +118,6 @@ public:
|
||||
auto populate_winning_path() -> void;
|
||||
|
||||
// Index mapping
|
||||
|
||||
[[nodiscard]] auto get_index(const puzzle& state) const -> size_t;
|
||||
[[nodiscard]] auto get_current_index() const -> size_t;
|
||||
[[nodiscard]] auto get_starting_index() const -> size_t;
|
||||
|
||||
@ -13,10 +13,6 @@
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#ifdef TRACY
|
||||
#include <tracy/Tracy.hpp>
|
||||
#endif
|
||||
|
||||
class threaded_physics
|
||||
{
|
||||
struct add_mass {};
|
||||
@ -65,8 +61,8 @@ public:
|
||||
physics_state state;
|
||||
|
||||
public:
|
||||
threaded_physics()
|
||||
: physics(physics_thread, std::ref(state)) {}
|
||||
explicit threaded_physics(const std::optional<BS::thread_pool<>* const> thread_pool = std::nullopt)
|
||||
: physics(physics_thread, std::ref(state), std::ref(thread_pool)) {}
|
||||
|
||||
threaded_physics(const threaded_physics& copy) = delete;
|
||||
auto operator=(const threaded_physics& copy) -> threaded_physics& = delete;
|
||||
@ -86,7 +82,7 @@ private:
|
||||
static auto set_octree_pool_thread_name(size_t idx) -> void;
|
||||
#endif
|
||||
|
||||
static auto physics_thread(physics_state& state) -> void;
|
||||
static auto physics_thread(physics_state& state, std::optional<BS::thread_pool<>* const> thread_pool) -> void;
|
||||
|
||||
public:
|
||||
auto clear_cmd() -> void;
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
#ifndef GUI_HPP_
|
||||
#define GUI_HPP_
|
||||
#ifndef USER_INTERFACE_HPP_
|
||||
#define USER_INTERFACE_HPP_
|
||||
|
||||
#include "orbit_camera.hpp"
|
||||
#include "config.hpp"
|
||||
@ -104,7 +104,7 @@ private:
|
||||
bool ok_message = false;
|
||||
bool yes_no_message = false;
|
||||
bool save_window = false;
|
||||
std::array<char, 256> preset_name = {};
|
||||
std::array<char, 256> preset_comment = {};
|
||||
bool help_window = false;
|
||||
|
||||
public:
|
||||
|
||||
192
include/util.hpp
192
include/util.hpp
@ -4,81 +4,8 @@
|
||||
#include <iostream>
|
||||
#include <raylib.h>
|
||||
|
||||
// Bit shifting + masking
|
||||
|
||||
template <class T>
|
||||
requires std::unsigned_integral<T>
|
||||
auto create_mask(const uint8_t first, const uint8_t last) -> T
|
||||
{
|
||||
// If the mask width is equal the type width return all 1s instead of shifting
|
||||
// as shifting by type-width is undefined behavior.
|
||||
if (static_cast<size_t>(last - first + 1) >= sizeof(T) * 8) {
|
||||
return ~T{0};
|
||||
}
|
||||
|
||||
// Example: first=4, last=7, 7-4+1=4
|
||||
// 1 << 4 = 0b00010000
|
||||
// 32 - 1 = 0b00001111
|
||||
// 31 << 4 = 0b11110000
|
||||
// Subtracting 1 generates a consecutive mask.
|
||||
return ((T{1} << (last - first + 1)) - 1) << first;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
requires std::unsigned_integral<T>
|
||||
auto clear_bits(T& bits, const uint8_t first, const uint8_t last) -> void
|
||||
{
|
||||
const T mask = create_mask<T>(first, last);
|
||||
|
||||
bits = bits & ~mask;
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
requires std::unsigned_integral<T> && std::unsigned_integral<U>
|
||||
auto set_bits(T& bits, const uint8_t first, const uint8_t last, const U value) -> void
|
||||
{
|
||||
const T mask = create_mask<T>(first, last);
|
||||
|
||||
// Example: first=4, last=6, value=0b1110, bits = 0b 01111110
|
||||
// mask = 0b 01110000
|
||||
// bits & ~mask = 0b 00001110
|
||||
// value << 4 = 0b 11100000
|
||||
// (value << 4) & mask = 0b 01100000
|
||||
// (bits & ~mask) | (value << 4) & mask = 0b 01101110
|
||||
// Insert position: ^^^
|
||||
// First clear the bits, then | with the value positioned at the insertion point.
|
||||
// The value may be larger than [first, last], extra bits are ignored.
|
||||
bits = (bits & ~mask) | ((static_cast<T>(value) << first) & mask);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
requires std::unsigned_integral<T>
|
||||
auto get_bits(const T bits, const uint8_t first, const uint8_t last) -> T
|
||||
{
|
||||
const T mask = create_mask<T>(first, last);
|
||||
|
||||
// We can >> without sign extension because T is unsigned_integral
|
||||
return (bits & mask) >> first;
|
||||
}
|
||||
|
||||
// std::variant visitor
|
||||
|
||||
// https://en.cppreference.com/w/cpp/utility/variant/visit
|
||||
template <class... Ts>
|
||||
struct overloads : Ts...
|
||||
{
|
||||
using Ts::operator()...;
|
||||
};
|
||||
|
||||
// Enums
|
||||
|
||||
enum direction
|
||||
{
|
||||
nor = 1 << 0,
|
||||
eas = 1 << 1,
|
||||
sou = 1 << 2,
|
||||
wes = 1 << 3,
|
||||
};
|
||||
#define INLINE __attribute__((always_inline))
|
||||
#define PACKED __attribute__((packed))
|
||||
|
||||
enum ctrl
|
||||
{
|
||||
@ -115,6 +42,96 @@ enum bg
|
||||
bg_white = 47
|
||||
};
|
||||
|
||||
inline auto ansi_bold_fg(const fg color) -> std::string
|
||||
{
|
||||
return std::format("\033[{};{}m", static_cast<int>(bold_bright), static_cast<int>(color));
|
||||
}
|
||||
|
||||
inline auto ansi_reset() -> std::string
|
||||
{
|
||||
return std::format("\033[{}m", static_cast<int>(reset));
|
||||
}
|
||||
|
||||
// Bit shifting + masking
|
||||
|
||||
template <class T>
|
||||
requires std::unsigned_integral<T>
|
||||
// ReSharper disable once CppRedundantInlineSpecifier
|
||||
INLINE inline auto create_mask(const uint8_t first, const uint8_t last) -> T
|
||||
{
|
||||
// If the mask width is equal the type width return all 1s instead of shifting
|
||||
// as shifting by type-width is undefined behavior.
|
||||
if (static_cast<size_t>(last - first + 1) >= sizeof(T) * 8) {
|
||||
return ~T{0};
|
||||
}
|
||||
|
||||
// Example: first=4, last=7, 7-4+1=4
|
||||
// 1 << 4 = 0b00010000
|
||||
// 32 - 1 = 0b00001111
|
||||
// 31 << 4 = 0b11110000
|
||||
// Subtracting 1 generates a consecutive mask.
|
||||
return ((T{1} << (last - first + 1)) - 1) << first;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
requires std::unsigned_integral<T>
|
||||
// ReSharper disable once CppRedundantInlineSpecifier
|
||||
INLINE inline auto clear_bits(T& bits, const uint8_t first, const uint8_t last) -> void
|
||||
{
|
||||
const T mask = create_mask<T>(first, last);
|
||||
|
||||
bits = bits & ~mask;
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
requires std::unsigned_integral<T> && std::unsigned_integral<U>
|
||||
// ReSharper disable once CppRedundantInlineSpecifier
|
||||
INLINE inline auto set_bits(T& bits, const uint8_t first, const uint8_t last, const U value) -> void
|
||||
{
|
||||
const T mask = create_mask<T>(first, last);
|
||||
|
||||
// Example: first=4, last=6, value=0b1110, bits = 0b 01111110
|
||||
// mask = 0b 01110000
|
||||
// bits & ~mask = 0b 00001110
|
||||
// value << 4 = 0b 11100000
|
||||
// (value << 4) & mask = 0b 01100000
|
||||
// (bits & ~mask) | (value << 4) & mask = 0b 01101110
|
||||
// Insert position: ^^^
|
||||
// First clear the bits, then | with the value positioned at the insertion point.
|
||||
// The value may be larger than [first, last], extra bits are ignored.
|
||||
bits = (bits & ~mask) | ((static_cast<T>(value) << first) & mask);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
requires std::unsigned_integral<T>
|
||||
// ReSharper disable once CppRedundantInlineSpecifier
|
||||
INLINE inline auto get_bits(const T bits, const uint8_t first, const uint8_t last) -> T
|
||||
{
|
||||
const T mask = create_mask<T>(first, last);
|
||||
|
||||
// We can >> without sign extension because T is unsigned_integral
|
||||
return (bits & mask) >> first;
|
||||
}
|
||||
|
||||
// std::variant visitor
|
||||
|
||||
// https://en.cppreference.com/w/cpp/utility/variant/visit
|
||||
template <class... Ts>
|
||||
struct overloads : Ts...
|
||||
{
|
||||
using Ts::operator()...;
|
||||
};
|
||||
|
||||
// Enums
|
||||
|
||||
enum direction
|
||||
{
|
||||
nor = 1 << 0,
|
||||
eas = 1 << 1,
|
||||
sou = 1 << 2,
|
||||
wes = 1 << 3,
|
||||
};
|
||||
|
||||
// Output
|
||||
|
||||
inline auto operator<<(std::ostream& os, const Vector2& v) -> std::ostream&
|
||||
@ -129,16 +146,6 @@ inline auto operator<<(std::ostream& os, const Vector3& v) -> std::ostream&
|
||||
return os;
|
||||
}
|
||||
|
||||
inline auto ansi_bold_fg(const fg color) -> std::string
|
||||
{
|
||||
return std::format("\033[1;{}m", static_cast<int>(color));
|
||||
}
|
||||
|
||||
inline auto ansi_reset() -> std::string
|
||||
{
|
||||
return "\033[0m";
|
||||
}
|
||||
|
||||
// std::println doesn't work with mingw
|
||||
template <typename... Args>
|
||||
auto traceln(std::format_string<Args...> fmt, Args&&... args) -> void
|
||||
@ -168,4 +175,19 @@ auto errln(std::format_string<Args...> fmt, Args&&... args) -> void
|
||||
fmt, std::forward<Args>(args)...) << std::endl;
|
||||
}
|
||||
|
||||
inline auto print_bitmap(const uint64_t bitmap, const uint8_t w, const uint8_t h, const std::string& title) -> void
|
||||
{
|
||||
traceln("{}:", title);
|
||||
traceln("{}", std::string(2 * w - 1, '='));
|
||||
for (size_t y = 0; y < w; ++y) {
|
||||
std::cout << " ";
|
||||
for (size_t x = 0; x < h; ++x) {
|
||||
std::cout << static_cast<int>(get_bits(bitmap, y * w + x, y * h + x)) << " ";
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
std::cout << std::flush;
|
||||
traceln("{}", std::string(2 * w - 1, '='));
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user