squash merge efficient-puzzle into main

This commit is contained in:
2026-03-02 05:26:57 +01:00
parent 846ff72d1f
commit 2ef2a29601
16 changed files with 1915 additions and 859 deletions

View File

@ -162,11 +162,12 @@ auto input_handler::select_block() -> void
auto input_handler::start_add_block() -> void
{
const puzzle& current = state.get_current_state();
if (!editing || current.try_get_block(hov_x, hov_y) || has_block_add_xy) {
if (!editing || current.try_get_block(hov_x, hov_y) || has_block_add_xy || current.block_count() >=
puzzle::MAX_BLOCKS) {
return;
}
if (hov_x >= 0 && hov_x < current.width && hov_y >= 0 && hov_y < current.height) {
if (hov_x >= 0 && hov_x < current.get_width() && hov_y >= 0 && hov_y < current.get_height()) {
block_add_x = hov_x;
block_add_y = hov_y;
has_block_add_xy = true;
@ -187,7 +188,8 @@ auto input_handler::clear_add_block() -> void
auto input_handler::add_block() -> void
{
const puzzle& current = state.get_current_state();
if (!editing || current.try_get_block(hov_x, hov_y) || !has_block_add_xy) {
if (!editing || current.try_get_block(hov_x, hov_y) || !has_block_add_xy || current.block_count() >=
puzzle::MAX_BLOCKS) {
return;
}
@ -276,7 +278,7 @@ auto input_handler::toggle_camera_projection() const -> void
auto input_handler::move_block_nor() -> void
{
const puzzle& current = state.get_current_state();
const std::optional<puzzle>& next = current.try_move_block_at(sel_x, sel_y, nor);
const std::optional<puzzle>& next = current.try_move_block_at_fast(sel_x, sel_y, nor);
if (!next) {
return;
}
@ -288,7 +290,7 @@ auto input_handler::move_block_nor() -> void
auto input_handler::move_block_wes() -> void
{
const puzzle& current = state.get_current_state();
const std::optional<puzzle>& next = current.try_move_block_at(sel_x, sel_y, wes);
const std::optional<puzzle>& next = current.try_move_block_at_fast(sel_x, sel_y, wes);
if (!next) {
return;
}
@ -300,7 +302,7 @@ auto input_handler::move_block_wes() -> void
auto input_handler::move_block_sou() -> void
{
const puzzle& current = state.get_current_state();
const std::optional<puzzle>& next = current.try_move_block_at(sel_x, sel_y, sou);
const std::optional<puzzle>& next = current.try_move_block_at_fast(sel_x, sel_y, sou);
if (!next) {
return;
}
@ -312,7 +314,7 @@ auto input_handler::move_block_sou() -> void
auto input_handler::move_block_eas() -> void
{
const puzzle& current = state.get_current_state();
const std::optional<puzzle>& next = current.try_move_block_at(sel_x, sel_y, eas);
const std::optional<puzzle>& next = current.try_move_block_at_fast(sel_x, sel_y, eas);
if (!next) {
return;
}
@ -323,7 +325,7 @@ auto input_handler::move_block_eas() -> void
auto input_handler::print_state() const -> void
{
infoln("State: \"{}\"", state.get_current_state().state);
infoln("State: \"{}\"", state.get_current_state().string_repr());
}
auto input_handler::load_previous_preset() -> void
@ -332,7 +334,7 @@ auto input_handler::load_previous_preset() -> void
return;
}
const auto handler = [&]()
const auto handler = [&]
{
block_add_x = -1;
block_add_y = -1;
@ -353,7 +355,7 @@ auto input_handler::load_next_preset() -> void
return;
}
const auto handler = [&]()
const auto handler = [&]
{
block_add_x = -1;
block_add_y = -1;
@ -370,7 +372,7 @@ auto input_handler::load_next_preset() -> void
auto input_handler::goto_starting_state() -> void
{
const auto handler = [&]()
const auto handler = [&]
{
state.goto_starting_state();
sel_x = 0;
@ -387,7 +389,7 @@ auto input_handler::populate_graph() const -> void
auto input_handler::clear_graph() -> void
{
const auto handler = [&]()
const auto handler = [&]
{
state.clear_graph_and_add_current();
};
@ -433,7 +435,7 @@ auto input_handler::goto_previous_state() const -> void
auto input_handler::toggle_restricted_movement() const -> void
{
const puzzle& current = state.get_current_state();
const std::optional<puzzle>& next = current.try_toggle_restricted();
const std::optional<puzzle>& next = current.toggle_restricted();
if (!editing || !next) {
return;
@ -471,7 +473,7 @@ auto input_handler::remove_board_column() const -> void
const puzzle& current = state.get_current_state();
const std::optional<puzzle>& next = current.try_remove_column();
if (!editing || current.width <= puzzle::MIN_WIDTH || !next) {
if (!editing || current.get_width() <= puzzle::MIN_WIDTH || !next) {
return;
}
@ -483,7 +485,7 @@ auto input_handler::add_board_column() const -> void
const puzzle& current = state.get_current_state();
const std::optional<puzzle>& next = current.try_add_column();
if (!editing || current.width >= puzzle::MAX_WIDTH || !next) {
if (!editing || current.get_width() >= puzzle::MAX_WIDTH || !next) {
return;
}
@ -495,7 +497,7 @@ auto input_handler::remove_board_row() const -> void
const puzzle& current = state.get_current_state();
const std::optional<puzzle>& next = current.try_remove_row();
if (!editing || current.height <= puzzle::MIN_HEIGHT || !next) {
if (!editing || current.get_height() <= puzzle::MIN_HEIGHT || !next) {
return;
}
@ -507,7 +509,7 @@ auto input_handler::add_board_row() const -> void
const puzzle& current = state.get_current_state();
const std::optional<puzzle>& next = current.try_add_row();
if (!editing || current.height >= puzzle::MAX_HEIGHT || !next) {
if (!editing || current.get_height() >= puzzle::MAX_HEIGHT || !next) {
return;
}
@ -528,7 +530,7 @@ auto input_handler::toggle_editing() -> void
auto input_handler::clear_goal() const -> void
{
const puzzle& current = state.get_current_state();
const std::optional<puzzle>& next = current.try_clear_goal();
const std::optional<puzzle>& next = current.clear_goal();
if (!editing || !next) {
return;

View File

@ -11,21 +11,14 @@
#include "user_interface.hpp"
#ifdef TRACY
#include <tracy/Tracy.hpp>
#include <tracy/Tracy.hpp>
#endif
// TODO: Add some popups (my split between input.cpp/gui.cpp makes this ugly)
// - Clear graph: Notify that this will clear the visited states and move
// history
// - Reset state: Notify that this will reset the move count
// TODO: Manual movement is now completely broken
// TODO: Reduce memory usage
// - The memory model of the puzzle board is terrible (bitboards?)
// TODO: Add state space generation time to debug overlay
// TODO: Improve solver
// - Move discovery is terrible
// - Instead of trying each direction for each block, determine the
// possible moves more efficiently (requires a different memory model)
// - Implement state discovery/enumeration
// - Find all possible initial board states (single one for each
// possible statespace). Currently wer're just finding all states
@ -37,7 +30,18 @@
// TODO: Click states in the graph to display them in the board
// NOTE: Tracy uses a huge amount of memory. For longer testing disable Tracy.
// For profiling explore_state_space
auto main2(int argc, char* argv[]) -> int
{
const puzzle p = puzzle(
"S:[4x5] G:[1,3] M:[F] B:[{_ 2X2 _ _} {1x1 _ _ 1x1} {1x2 2x1 _ 1x2} {_ 2x1 _ _} {1x1 2x1 _ 1x1}]");
for (int i = 0; i < 50; ++i) {
auto space = p.explore_state_space();
}
return 0;
}
auto main(int argc, char* argv[]) -> int
{
@ -48,17 +52,17 @@ auto main(int argc, char* argv[]) -> int
preset_file = argv[1];
}
#ifdef BACKWARD
#ifdef BACKWARD
infoln("Backward stack-traces enabled.");
#else
#else
infoln("Backward stack-traces disabled.");
#endif
#endif
#ifdef TRACY
#ifdef TRACY
infoln("Tracy adapter enabled.");
#else
#else
infoln("Tracy adapter disabled.");
#endif
#endif
// RayLib window setup
SetTraceLogLevel(LOG_ERROR);
@ -89,9 +93,9 @@ auto main(int argc, char* argv[]) -> int
// Game loop
while (!WindowShouldClose()) {
#ifdef TRACY
#ifdef TRACY
FrameMarkStart("MainThread");
#endif
#endif
// Time tracking
std::chrono::time_point now = std::chrono::high_resolution_clock::now();
@ -102,16 +106,16 @@ auto main(int argc, char* argv[]) -> int
// Input update
input.handle_input();
// Read positions from physics thread
#ifdef TRACY
// Read positions from physics thread
#ifdef TRACY
FrameMarkStart("MainThreadConsumeLock");
#endif
#endif
{
#ifdef TRACY
#ifdef TRACY
std::unique_lock<LockableBase(std::mutex)> lock(physics.state.data_mtx);
#else
#else
std::unique_lock<std::mutex> lock(physics.state.data_mtx);
#endif
#endif
ups = physics.state.ups;
mass_center = physics.state.mass_center;
@ -130,16 +134,15 @@ auto main(int argc, char* argv[]) -> int
physics.state.data_consumed_cnd.notify_all();
}
}
#ifdef TRACY
#ifdef TRACY
FrameMarkEnd("MainThreadConsumeLock");
#endif
#endif
// Update the camera after the physics, so target lock is smooth
size_t current_index = state.get_current_index();
if (masses.size() > current_index) {
const mass_spring_system::mass& current_mass = mass_spring_system::mass(masses.at(current_index));
camera.update(current_mass.position, mass_center, input.camera_lock,
input.camera_mass_center_lock);
camera.update(current_mass.position, mass_center, input.camera_lock, input.camera_mass_center_lock);
}
// Rendering
@ -153,13 +156,12 @@ auto main(int argc, char* argv[]) -> int
}
++loop_iterations;
#ifdef TRACY
FrameMark;
FrameMarkEnd("MainThread");
#endif
#ifdef TRACY
FrameMark; FrameMarkEnd("MainThread");
#endif
}
CloseWindow();
return 0;
}
}

View File

@ -5,7 +5,7 @@
#include <raymath.h>
#ifdef TRACY
#include <tracy/Tracy.hpp>
#include <tracy/Tracy.hpp>
#endif
auto octree::node::child_count() const -> int
@ -32,9 +32,8 @@ auto octree::create_empty_leaf(const Vector3& box_min, const Vector3& box_max) -
auto octree::get_octant(const int node_idx, const Vector3& pos) const -> int
{
const node& n = nodes[node_idx];
auto [cx, cy, cz] =
Vector3((n.box_min.x + n.box_max.x) / 2.0f, (n.box_min.y + n.box_max.y) / 2.0f,
(n.box_min.z + n.box_max.z) / 2.0f);
auto [cx, cy, cz] = Vector3((n.box_min.x + n.box_max.x) / 2.0f, (n.box_min.y + n.box_max.y) / 2.0f,
(n.box_min.z + n.box_max.z) / 2.0f);
// The octant is encoded as a 3-bit integer "zyx". The node area is split
// along all 3 axes, if a position is right of an axis, this bit is set to 1.
@ -54,13 +53,11 @@ auto octree::get_octant(const int node_idx, const Vector3& pos) const -> int
return octant;
}
auto octree::get_child_bounds(const int node_idx, const int octant) const
-> std::pair<Vector3, Vector3>
auto octree::get_child_bounds(const int node_idx, const int octant) const -> std::pair<Vector3, Vector3>
{
const node& n = nodes[node_idx];
auto [cx, cy, cz] =
Vector3((n.box_min.x + n.box_max.x) / 2.0f, (n.box_min.y + n.box_max.y) / 2.0f,
(n.box_min.z + n.box_max.z) / 2.0f);
auto [cx, cy, cz] = Vector3((n.box_min.x + n.box_max.x) / 2.0f, (n.box_min.y + n.box_max.y) / 2.0f,
(n.box_min.z + n.box_max.z) / 2.0f);
Vector3 min = Vector3Zero();
Vector3 max = Vector3Zero();
@ -84,13 +81,11 @@ auto octree::insert(const int node_idx, const int mass_id, const Vector3& pos, c
// infoln("Inserting position ({}, {}, {}) into octree at node {} (depth {})", pos.x, pos.y,
// pos.z, node_idx, depth);
if (depth > MAX_DEPTH) {
errln("MAX_DEPTH! node={} box_min=({},{},{}) box_max=({},{},{}) pos=({},{},{})", node_idx,
nodes[node_idx].box_min.x, nodes[node_idx].box_min.y, nodes[node_idx].box_min.z,
nodes[node_idx].box_max.x, nodes[node_idx].box_max.y, nodes[node_idx].box_max.z,
pos.x, pos.y, pos.z);
// This runs from inside the physics thread so it won't exit cleanly
exit(1);
throw std::runtime_error(std::format("MAX_DEPTH! node={} box_min=({},{},{}) box_max=({},{},{}) pos=({},{},{})",
node_idx, nodes[node_idx].box_min.x, nodes[node_idx].box_min.y,
nodes[node_idx].box_min.z, nodes[node_idx].box_max.x,
nodes[node_idx].box_max.y, nodes[node_idx].box_max.z, pos.x, pos.y,
pos.z));
}
// NOTE: Do not store a nodes[node_idx] reference as the nodes vector might reallocate during
@ -152,12 +147,9 @@ auto octree::insert(const int node_idx, const int mass_id, const Vector3& pos, c
// Update the center of mass
const float new_mass = nodes[node_idx].mass_total + mass;
nodes[node_idx].mass_center.x =
(nodes[node_idx].mass_center.x * nodes[node_idx].mass_total + pos.x) / new_mass;
nodes[node_idx].mass_center.y =
(nodes[node_idx].mass_center.y * nodes[node_idx].mass_total + pos.y) / new_mass;
nodes[node_idx].mass_center.z =
(nodes[node_idx].mass_center.z * nodes[node_idx].mass_total + pos.z) / new_mass;
nodes[node_idx].mass_center.x = (nodes[node_idx].mass_center.x * nodes[node_idx].mass_total + pos.x) / new_mass;
nodes[node_idx].mass_center.y = (nodes[node_idx].mass_center.y * nodes[node_idx].mass_total + pos.y) / new_mass;
nodes[node_idx].mass_center.z = (nodes[node_idx].mass_center.z * nodes[node_idx].mass_total + pos.z) / new_mass;
nodes[node_idx].mass_total = new_mass;
}
@ -198,4 +190,4 @@ auto octree::calculate_force(const int node_idx, const Vector3& pos) const -> Ve
}
return force;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -91,7 +91,7 @@ auto state_manager::parse_preset_file(const std::string& _preset_file) -> bool
std::vector<std::string> comment_lines;
std::vector<std::string> preset_lines;
while (std::getline(file, line)) {
if (line.starts_with("F") || line.starts_with("R")) {
if (line.starts_with("S")) {
preset_lines.push_back(line);
} else if (line.starts_with("#")) {
comment_lines.push_back(line);
@ -105,10 +105,11 @@ auto state_manager::parse_preset_file(const std::string& _preset_file) -> bool
preset_states.clear();
for (const auto& preset : preset_lines) {
// Each char is a bit
const puzzle& p = puzzle(preset);
if (const std::optional<std::string>& reason = p.try_get_invalid_reason()) {
preset_states = {puzzle(4, 5, 9, 9, false)};
preset_states = {puzzle(4, 5, 0, 0, true, false)};
infoln("Preset file \"{}\" contained invalid presets: {}", preset_file, *reason);
return false;
}
@ -135,7 +136,7 @@ auto state_manager::append_preset_file(const std::string& preset_name) -> bool
return false;
}
file << "\n# " << preset_name << "\n" << get_current_state().state << std::flush;
file << "\n# " << preset_name << "\n" << get_current_state().string_repr() << std::flush;
infoln("Refreshing presets...");
if (parse_preset_file(preset_file)) {
@ -189,7 +190,7 @@ auto state_manager::update_current_state(const puzzle& p) -> void
move_history.emplace_back(previous_state_index);
}
if (p.won()) {
if (p.goal_reached()) {
winning_indices.insert(current_state_index);
}
@ -297,6 +298,7 @@ auto state_manager::populate_graph() -> void
const puzzle s = get_starting_state();
const puzzle p = get_current_state();
// Clear the graph first so we don't add duplicates somehow
synced_clear_statespace();
@ -340,7 +342,7 @@ auto state_manager::populate_winning_indices() -> void
{
winning_indices.clear();
for (const auto& [state, index] : state_indices) {
if (state.won()) {
if (state.goal_reached()) {
winning_indices.insert(index);
}
}
@ -429,12 +431,12 @@ auto state_manager::get_links() const -> const std::vector<std::pair<size_t, siz
return links;
}
auto state_manager::get_winning_indices() const -> const std::unordered_set<size_t>&
auto state_manager::get_winning_indices() const -> const boost::unordered_flat_set<size_t>&
{
return winning_indices;
}
auto state_manager::get_visit_counts() const -> const std::unordered_map<size_t, int>&
auto state_manager::get_visit_counts() const -> const boost::unordered_flat_map<size_t, int>&
{
return visit_counts;
}
@ -444,7 +446,7 @@ auto state_manager::get_winning_path() const -> const std::vector<size_t>&
return winning_path;
}
auto state_manager::get_path_indices() const -> const std::unordered_set<size_t>&
auto state_manager::get_path_indices() const -> const boost::unordered_flat_set<size_t>&
{
return path_indices;
}

View File

@ -8,12 +8,11 @@
#include <raygui.h>
#ifdef TRACY
#include <tracy/Tracy.hpp>
#include <tracy/Tracy.hpp>
#endif
auto user_interface::grid::update_bounds(const int _x, const int _y, const int _width,
const int _height, const int _columns, const int _rows)
-> void
auto user_interface::grid::update_bounds(const int _x, const int _y, const int _width, const int _height,
const int _columns, const int _rows) -> void
{
x = _x;
y = _y;
@ -23,8 +22,7 @@ auto user_interface::grid::update_bounds(const int _x, const int _y, const int _
rows = _rows;
}
auto user_interface::grid::update_bounds(const int _x, const int _y, const int _width,
const int _height) -> void
auto user_interface::grid::update_bounds(const int _x, const int _y, const int _width, const int _height) -> void
{
x = _x;
y = _y;
@ -48,19 +46,17 @@ auto user_interface::grid::bounds() const -> Rectangle
return bounds;
}
auto user_interface::grid::bounds(const int _x, const int _y, const int _width,
const int _height) const -> Rectangle
auto user_interface::grid::bounds(const int _x, const int _y, const int _width, const int _height) const -> Rectangle
{
if (_x < 0 || _x + _width > columns || _y < 0 || _y + _height > rows) {
errln("Grid bounds are outside range.");
exit(1);
throw std::invalid_argument("Grid bounds out of range");
}
const int cell_width = (width - padding) / columns;
const int cell_height = (height - padding) / rows;
return Rectangle(x + _x * cell_width + padding, y + _y * cell_height + padding,
_width * cell_width - padding, _height * cell_height - padding);
return Rectangle(x + _x * cell_width + padding, y + _y * cell_height + padding, _width * cell_width - padding,
_height * cell_height - padding);
}
auto user_interface::grid::square_bounds() const -> Rectangle
@ -80,8 +76,7 @@ auto user_interface::grid::square_bounds(const int _x, const int _y, const int _
// filled
if (_x < 0 || _x + _width > columns || _y < 0 || _y + _height > rows) {
errln("Grid bounds are outside range.");
exit(1);
throw std::invalid_argument("Grid bounds out of range");
}
const int available_width = width - padding * (columns + 1);
@ -93,10 +88,8 @@ auto user_interface::grid::square_bounds(const int _x, const int _y, const int _
const int x_offset = (width - grid_width) / 2;
const int y_offset = (height - grid_height) / 2;
return Rectangle(x_offset + _x * (cell_size + padding) + padding,
y_offset + _y * (cell_size + padding) + padding,
_width * cell_size + padding * (_width - 1),
_height * cell_size + padding * (_height - 1));
return Rectangle(x_offset + _x * (cell_size + padding) + padding, y_offset + _y * (cell_size + padding) + padding,
_width * cell_size + padding * (_width - 1), _height * cell_size + padding * (_height - 1));
}
auto user_interface::init() -> void
@ -155,19 +148,19 @@ auto user_interface::get_default_style() -> default_style
{
// Could've iterated over the values, but then it wouldn't be as nice to
// access...
return {{GuiGetStyle(DEFAULT, BORDER_COLOR_NORMAL), GuiGetStyle(DEFAULT, BASE_COLOR_NORMAL),
GuiGetStyle(DEFAULT, TEXT_COLOR_NORMAL), GuiGetStyle(DEFAULT, BORDER_COLOR_FOCUSED),
GuiGetStyle(DEFAULT, BASE_COLOR_FOCUSED), GuiGetStyle(DEFAULT, TEXT_COLOR_FOCUSED),
GuiGetStyle(DEFAULT, BORDER_COLOR_PRESSED), GuiGetStyle(DEFAULT, BASE_COLOR_PRESSED),
GuiGetStyle(DEFAULT, TEXT_COLOR_PRESSED), GuiGetStyle(DEFAULT, BORDER_COLOR_DISABLED),
GuiGetStyle(DEFAULT, BASE_COLOR_DISABLED), GuiGetStyle(DEFAULT, TEXT_COLOR_DISABLED)},
GuiGetStyle(DEFAULT, BACKGROUND_COLOR),
GuiGetStyle(DEFAULT, LINE_COLOR),
GuiGetStyle(DEFAULT, TEXT_SIZE),
GuiGetStyle(DEFAULT, TEXT_SPACING),
GuiGetStyle(DEFAULT, TEXT_LINE_SPACING),
GuiGetStyle(DEFAULT, TEXT_ALIGNMENT_VERTICAL),
GuiGetStyle(DEFAULT, TEXT_WRAP_MODE)};
return {
{
GuiGetStyle(DEFAULT, BORDER_COLOR_NORMAL), GuiGetStyle(DEFAULT, BASE_COLOR_NORMAL),
GuiGetStyle(DEFAULT, TEXT_COLOR_NORMAL), GuiGetStyle(DEFAULT, BORDER_COLOR_FOCUSED),
GuiGetStyle(DEFAULT, BASE_COLOR_FOCUSED), GuiGetStyle(DEFAULT, TEXT_COLOR_FOCUSED),
GuiGetStyle(DEFAULT, BORDER_COLOR_PRESSED), GuiGetStyle(DEFAULT, BASE_COLOR_PRESSED),
GuiGetStyle(DEFAULT, TEXT_COLOR_PRESSED), GuiGetStyle(DEFAULT, BORDER_COLOR_DISABLED),
GuiGetStyle(DEFAULT, BASE_COLOR_DISABLED), GuiGetStyle(DEFAULT, TEXT_COLOR_DISABLED)
},
GuiGetStyle(DEFAULT, BACKGROUND_COLOR), GuiGetStyle(DEFAULT, LINE_COLOR), GuiGetStyle(DEFAULT, TEXT_SIZE),
GuiGetStyle(DEFAULT, TEXT_SPACING), GuiGetStyle(DEFAULT, TEXT_LINE_SPACING),
GuiGetStyle(DEFAULT, TEXT_ALIGNMENT_VERTICAL), GuiGetStyle(DEFAULT, TEXT_WRAP_MODE)
};
}
auto user_interface::set_default_style(const default_style& style) -> void
@ -201,15 +194,17 @@ auto user_interface::set_default_style(const default_style& style) -> void
auto user_interface::get_component_style(const int component) -> component_style
{
return {
{GuiGetStyle(component, BORDER_COLOR_NORMAL), GuiGetStyle(component, BASE_COLOR_NORMAL),
GuiGetStyle(component, TEXT_COLOR_NORMAL), GuiGetStyle(component, BORDER_COLOR_FOCUSED),
GuiGetStyle(component, BASE_COLOR_FOCUSED), GuiGetStyle(component, TEXT_COLOR_FOCUSED),
GuiGetStyle(component, BORDER_COLOR_PRESSED), GuiGetStyle(component, BASE_COLOR_PRESSED),
GuiGetStyle(component, TEXT_COLOR_PRESSED), GuiGetStyle(component, BORDER_COLOR_DISABLED),
GuiGetStyle(component, BASE_COLOR_DISABLED), GuiGetStyle(component, TEXT_COLOR_DISABLED)},
GuiGetStyle(component, BORDER_WIDTH),
GuiGetStyle(component, TEXT_PADDING),
GuiGetStyle(component, TEXT_ALIGNMENT)};
{
GuiGetStyle(component, BORDER_COLOR_NORMAL), GuiGetStyle(component, BASE_COLOR_NORMAL),
GuiGetStyle(component, TEXT_COLOR_NORMAL), GuiGetStyle(component, BORDER_COLOR_FOCUSED),
GuiGetStyle(component, BASE_COLOR_FOCUSED), GuiGetStyle(component, TEXT_COLOR_FOCUSED),
GuiGetStyle(component, BORDER_COLOR_PRESSED), GuiGetStyle(component, BASE_COLOR_PRESSED),
GuiGetStyle(component, TEXT_COLOR_PRESSED), GuiGetStyle(component, BORDER_COLOR_DISABLED),
GuiGetStyle(component, BASE_COLOR_DISABLED), GuiGetStyle(component, TEXT_COLOR_DISABLED)
},
GuiGetStyle(component, BORDER_WIDTH), GuiGetStyle(component, TEXT_PADDING),
GuiGetStyle(component, TEXT_ALIGNMENT)
};
}
auto user_interface::set_component_style(const int component, const component_style& style) -> void
@ -238,13 +233,11 @@ auto user_interface::set_component_style(const int component, const component_st
auto user_interface::popup_bounds() -> Rectangle
{
return Rectangle(static_cast<float>(GetScreenWidth()) / 2.0f - POPUP_WIDTH / 2.0f,
static_cast<float>(GetScreenHeight()) / 2.0f - POPUP_HEIGHT / 2.0f,
POPUP_WIDTH, POPUP_HEIGHT);
static_cast<float>(GetScreenHeight()) / 2.0f - POPUP_HEIGHT / 2.0f, POPUP_WIDTH, POPUP_HEIGHT);
}
auto user_interface::draw_button(const Rectangle bounds, const std::string& label,
const Color color, const bool enabled, const int font_size) const
-> int
auto user_interface::draw_button(const Rectangle bounds, const std::string& label, const Color color,
const bool enabled, const int font_size) const -> int
{
// Save original styling
const default_style original_default = get_default_style();
@ -275,16 +268,16 @@ auto user_interface::draw_button(const Rectangle bounds, const std::string& labe
}
auto user_interface::draw_menu_button(const int x, const int y, const int width, const int height,
const std::string& label, const Color color,
const bool enabled, const int font_size) const -> int
const std::string& label, const Color color, const bool enabled,
const int font_size) const -> int
{
const Rectangle bounds = menu_grid.bounds(x, y, width, height);
return draw_button(bounds, label, color, enabled, font_size);
}
auto user_interface::draw_toggle_slider(const Rectangle bounds, const std::string& off_label,
const std::string& on_label, int* active, Color color,
bool enabled, int font_size) const -> int
const std::string& on_label, int* active, Color color, bool enabled,
int font_size) const -> int
{
// Save original styling
const default_style original_default = get_default_style();
@ -319,18 +312,16 @@ auto user_interface::draw_toggle_slider(const Rectangle bounds, const std::strin
return pressed;
}
auto user_interface::draw_menu_toggle_slider(const int x, const int y, const int width,
const int height, const std::string& off_label,
const std::string& on_label, int* active,
const Color color, const bool enabled,
const int font_size) const -> int
auto user_interface::draw_menu_toggle_slider(const int x, const int y, const int width, const int height,
const std::string& off_label, const std::string& on_label, int* active,
const Color color, const bool enabled, const int font_size) const -> int
{
const Rectangle bounds = menu_grid.bounds(x, y, width, height);
return draw_toggle_slider(bounds, off_label, on_label, active, color, enabled, font_size);
}
auto user_interface::draw_spinner(Rectangle bounds, const std::string& label, int* value, int min,
int max, Color color, bool enabled, int font_size) const -> int
auto user_interface::draw_spinner(Rectangle bounds, const std::string& label, int* value, int min, int max, Color color,
bool enabled, int font_size) const -> int
{
// Save original styling
const default_style original_default = get_default_style();
@ -366,16 +357,15 @@ auto user_interface::draw_spinner(Rectangle bounds, const std::string& label, in
}
auto user_interface::draw_menu_spinner(const int x, const int y, const int width, const int height,
const std::string& label, int* value, const int min,
const int max, const Color color, const bool enabled,
const int font_size) const -> int
const std::string& label, int* value, const int min, const int max,
const Color color, const bool enabled, const int font_size) const -> int
{
const Rectangle bounds = menu_grid.bounds(x, y, width, height);
return draw_spinner(bounds, label, value, min, max, color, enabled, font_size);
}
auto user_interface::draw_label(const Rectangle bounds, const std::string& text, const Color color,
const bool enabled, const int font_size) const -> int
auto user_interface::draw_label(const Rectangle bounds, const std::string& text, const Color color, const bool enabled,
const int font_size) const -> int
{
// Save original styling
const default_style original_default = get_default_style();
@ -405,8 +395,8 @@ auto user_interface::draw_label(const Rectangle bounds, const std::string& text,
return pressed;
}
auto user_interface::draw_board_block(const int x, const int y, const int width, const int height,
const Color color, const bool enabled) const -> bool
auto user_interface::draw_board_block(const int x, const int y, const int width, const int height, const Color color,
const bool enabled) const -> bool
{
component_style s = get_component_style(BUTTON);
apply_block_color(s, color);
@ -452,18 +442,19 @@ auto user_interface::window_open() const -> bool
auto user_interface::draw_menu_header(const Color color) const -> void
{
int preset = static_cast<int>(state.get_current_preset());
draw_menu_spinner(0, 0, 1, 1, "Preset: ", &preset, -1,
static_cast<int>(state.get_preset_count()), color, !input.editing);
draw_menu_spinner(0, 0, 1, 1, "Preset: ", &preset, -1, static_cast<int>(state.get_preset_count()), color,
!input.editing);
if (preset > static_cast<int>(state.get_current_preset())) {
input.load_next_preset();
} else if (preset < static_cast<int>(state.get_current_preset())) {
input.load_previous_preset();
}
draw_menu_button(1, 0, 1, 1,
std::format("Puzzle: \"{}\"{}", state.get_current_preset_comment().substr(2),
state.was_edited() ? " (Modified)" : ""),
color);
draw_menu_button(1, 0, 1, 1, std::format("{}: {}/{} Blocks",
state.was_edited()
? "Modified"
: std::format("\"{}\"", state.get_current_preset_comment().substr(2)),
state.get_current_state().block_count(), puzzle::MAX_BLOCKS), color);
int editing = input.editing;
draw_menu_toggle_slider(2, 0, 1, 1, "Puzzle Mode (Tab)", "Edit Mode (Tab)", &editing, color);
@ -474,18 +465,13 @@ auto user_interface::draw_menu_header(const Color color) const -> void
auto user_interface::draw_graph_info(const Color color) const -> void
{
draw_menu_button(0, 1, 1, 1,
std::format("Found {} States ({} Winning)", state.get_state_count(),
state.get_target_count()),
color);
draw_menu_button(0, 1, 1, 1, std::format("Found {} States ({} Winning)", state.get_state_count(),
state.get_target_count()), color);
draw_menu_button(1, 1, 1, 1, std::format("Found {} Transitions", state.get_link_count()),
color);
draw_menu_button(1, 1, 1, 1, std::format("Found {} Transitions", state.get_link_count()), color);
draw_menu_button(2, 1, 1, 1,
std::format("{} Moves to Nearest Solution",
state.get_path_length() > 0 ? state.get_path_length() - 1 : 0),
color);
draw_menu_button(2, 1, 1, 1, std::format("{} Moves to Nearest Solution",
state.get_path_length() > 0 ? state.get_path_length() - 1 : 0), color);
}
auto user_interface::draw_graph_controls(const Color color) const -> void
@ -506,8 +492,7 @@ auto user_interface::draw_graph_controls(const Color color) const -> void
}
int mark_solutions = input.mark_solutions;
draw_menu_toggle_slider(2, 2, 1, 1, "Solution Hidden (I)", "Solution Shown (I)",
&mark_solutions, color);
draw_menu_toggle_slider(2, 2, 1, 1, "Solution Hidden (I)", "Solution Shown (I)", &mark_solutions, color);
if (mark_solutions != input.mark_solutions) {
input.toggle_mark_solutions();
}
@ -517,22 +502,20 @@ auto user_interface::draw_graph_controls(const Color color) const -> void
auto user_interface::draw_camera_controls(const Color color) const -> void
{
int lock_camera = input.camera_lock;
draw_menu_toggle_slider(0, 3, 1, 1, "Free Camera (L)", "Locked Camera (L)", &lock_camera,
color);
draw_menu_toggle_slider(0, 3, 1, 1, "Free Camera (L)", "Locked Camera (L)", &lock_camera, color);
if (lock_camera != input.camera_lock) {
input.toggle_camera_lock();
}
int lock_camera_mass_center = input.camera_mass_center_lock;
draw_menu_toggle_slider(1, 3, 1, 1, "Current Block (U)", "Graph Center (U)",
&lock_camera_mass_center, color, input.camera_lock);
draw_menu_toggle_slider(1, 3, 1, 1, "Current Block (U)", "Graph Center (U)", &lock_camera_mass_center, color,
input.camera_lock);
if (lock_camera_mass_center != input.camera_mass_center_lock) {
input.toggle_camera_mass_center_lock();
}
int projection = camera.projection == CAMERA_ORTHOGRAPHIC;
draw_menu_toggle_slider(2, 3, 1, 1, "Perspective (Alt)", "Orthographic (Alt)", &projection,
color);
draw_menu_toggle_slider(2, 3, 1, 1, "Perspective (Alt)", "Orthographic (Alt)", &projection, color);
if (projection != (camera.projection == CAMERA_ORTHOGRAPHIC)) {
input.toggle_camera_projection();
}
@ -558,10 +541,8 @@ auto user_interface::draw_puzzle_controls(const Color color) const -> void
};
const int visits = state.get_current_visits();
draw_menu_button(0, 4, 1, 1,
std::format("{} Moves ({}{} Time at this State)", state.get_total_moves(),
visits, nth(visits)),
color);
draw_menu_button(0, 4, 1, 1, std::format("{} Moves ({}{} Time at this State)", state.get_total_moves(), visits,
nth(visits)), color);
if (draw_menu_button(1, 4, 1, 1, "Make Optimal Move (Space)", color, state.has_distances())) {
input.goto_optimal_next_state();
@ -600,43 +581,40 @@ auto user_interface::draw_edit_controls(const Color color) const -> void
}
// Toggle Restricted/Free Block Movement
int free = !current.restricted;
int free = !current.get_restricted();
draw_menu_toggle_slider(1, 4, 1, 1, "Restricted (F)", "Free (F)", &free, color);
if (free != !current.restricted) {
if (free != !current.get_restricted()) {
input.toggle_restricted_movement();
}
// Clear Goal
if (draw_menu_button(1, 5, 1, 1, "Clear Goal (X)", color)) {
}
if (draw_menu_button(1, 5, 1, 1, "Clear Goal (X)", color)) {}
// Column Count Spinner
int columns = current.width;
int columns = current.get_width();
draw_menu_spinner(2, 4, 1, 1, "Cols: ", &columns, puzzle::MIN_WIDTH, puzzle::MAX_WIDTH, color);
if (columns > current.width) {
if (columns > current.get_width()) {
input.add_board_column();
} else if (columns < current.width) {
} else if (columns < current.get_width()) {
input.remove_board_column();
}
// Row Count Spinner
int rows = current.height;
int rows = current.get_height();
draw_menu_spinner(2, 5, 1, 1, "Rows: ", &rows, puzzle::MIN_WIDTH, puzzle::MAX_WIDTH, color);
if (rows > current.height) {
if (rows > current.get_height()) {
input.add_board_row();
} else if (rows < current.height) {
} else if (rows < current.get_height()) {
input.remove_board_row();
}
}
auto user_interface::draw_menu_footer(const Color color) -> void
{
draw_menu_button(0, 6, 2, 1, std::format("State: \"{}\"", state.get_current_state().state),
color);
draw_menu_button(0, 6, 2, 1, state.get_current_state().string_repr().data(), color);
if (draw_menu_button(2, 6, 1, 1, "Save as Preset", color)) {
if (const std::optional<std::string>& reason =
state.get_current_state().try_get_invalid_reason()) {
if (const std::optional<std::string>& reason = state.get_current_state().try_get_invalid_reason()) {
message_title = "Can't Save Preset";
message_message = std::format("Invalid Board: {}.", *reason);
ok_message = true;
@ -651,8 +629,7 @@ auto user_interface::get_background_color() -> Color
return GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR));
}
auto user_interface::help_popup() -> void
{}
auto user_interface::help_popup() -> void {}
auto user_interface::draw_save_preset_popup() -> void
{
@ -661,8 +638,8 @@ auto user_interface::draw_save_preset_popup() -> void
}
// Returns the pressed button index
const int button = GuiTextInputBox(popup_bounds(), "Save as Preset", "Enter Preset Name",
"Ok;Cancel", preset_name.data(), 255, nullptr);
const int button = GuiTextInputBox(popup_bounds(), "Save as Preset", "Enter Preset Name", "Ok;Cancel",
preset_name.data(), 255, nullptr);
if (button == 1) {
state.append_preset_file(preset_name.data());
}
@ -678,8 +655,7 @@ auto user_interface::draw_ok_message_box() -> void
return;
}
const int button =
GuiMessageBox(popup_bounds(), message_title.data(), message_message.data(), "Ok");
const int button = GuiMessageBox(popup_bounds(), message_title.data(), message_message.data(), "Ok");
if (button == 0 || button == 1) {
message_title = "";
message_message = "";
@ -693,8 +669,7 @@ auto user_interface::draw_yes_no_message_box() -> void
return;
}
const int button =
GuiMessageBox(popup_bounds(), message_title.data(), message_message.data(), "Yes;No");
const int button = GuiMessageBox(popup_bounds(), message_title.data(), message_message.data(), "Yes;No");
if (button == 1) {
yes_no_handler();
}
@ -727,37 +702,36 @@ auto user_interface::draw_puzzle_board() -> void
{
const puzzle& current = state.get_current_state();
board_grid.update_bounds(0, MENU_HEIGHT, GetScreenWidth() / 2, GetScreenHeight() - MENU_HEIGHT,
current.width, current.height);
board_grid.update_bounds(0, MENU_HEIGHT, GetScreenWidth() / 2, GetScreenHeight() - MENU_HEIGHT, current.get_width(),
current.get_height());
// Draw outer border
const Rectangle bounds = board_grid.square_bounds();
DrawRectangleRec(bounds, current.won() ? BOARD_COLOR_WON : BOARD_COLOR_RESTRICTED);
DrawRectangleRec(bounds, current.goal_reached() ? BOARD_COLOR_WON : BOARD_COLOR_RESTRICTED);
// Draw inner borders
DrawRectangle(bounds.x + BOARD_PADDING, bounds.y + BOARD_PADDING,
bounds.width - 2 * BOARD_PADDING, bounds.height - 2 * BOARD_PADDING,
current.restricted ? BOARD_COLOR_RESTRICTED : BOARD_COLOR_FREE);
DrawRectangle(bounds.x + BOARD_PADDING, bounds.y + BOARD_PADDING, bounds.width - 2 * BOARD_PADDING,
bounds.height - 2 * BOARD_PADDING,
current.get_restricted() ? BOARD_COLOR_RESTRICTED : BOARD_COLOR_FREE);
// Draw target opening
// TODO: Only draw single direction (in corner) if restricted (use target block principal
// direction)
const std::optional<puzzle::block> target_block = current.try_get_target_block();
const int target_x = current.target_x;
const int target_y = current.target_y;
if (current.has_win_condition() && target_block) {
auto [x, y, width, height] = board_grid.square_bounds(
target_x, target_y, target_block.value().width, target_block.value().height);
const int target_x = current.get_goal_x();
const int target_y = current.get_goal_y();
if (current.get_goal() && target_block) {
auto [x, y, width, height] = board_grid.square_bounds(target_x, target_y, target_block->get_width(),
target_block->get_height());
const Color opening_color =
Fade(current.won() ? BOARD_COLOR_WON : BOARD_COLOR_RESTRICTED, 0.3);
const Color opening_color = Fade(current.goal_reached() ? BOARD_COLOR_WON : BOARD_COLOR_RESTRICTED, 0.3);
if (target_x == 0) {
// Left opening
DrawRectangle(x - BOARD_PADDING, y, BOARD_PADDING, height, RAYWHITE);
DrawRectangle(x - BOARD_PADDING, y, BOARD_PADDING, height, opening_color);
}
if (target_x + target_block.value().width == current.width) {
if (target_x + target_block->get_width() == current.get_width()) {
// Right opening
DrawRectangle(x + width, y, BOARD_PADDING, height, RAYWHITE);
DrawRectangle(x + width, y, BOARD_PADDING, height, opening_color);
@ -767,7 +741,7 @@ auto user_interface::draw_puzzle_board() -> void
DrawRectangle(x, y - BOARD_PADDING, width, BOARD_PADDING, RAYWHITE);
DrawRectangle(x, y - BOARD_PADDING, width, BOARD_PADDING, opening_color);
}
if (target_y + target_block.value().height == current.height) {
if (target_y + target_block->get_height() == current.get_height()) {
// Bottom opening
DrawRectangle(x, y + height, width, BOARD_PADDING, RAYWHITE);
DrawRectangle(x, y + height, width, BOARD_PADDING, opening_color);
@ -794,87 +768,87 @@ auto user_interface::draw_puzzle_board() -> void
}
// Draw blocks
for (const puzzle::block& b : current) {
for (const puzzle::block b : current.block_view()) {
Color c = BLOCK_COLOR;
if (b.target) {
if (b.get_target()) {
c = TARGET_BLOCK_COLOR;
} else if (b.immovable) {
} else if (b.get_immovable()) {
c = WALL_COLOR;
}
if (!b.valid() || b.x < 0 || b.y < 0 || b.width <= 0 || b.height <= 0) {
warnln("Iterator returned invalid block for state \"{}\".", current.state);
continue;
}
const auto [x, y, w, h, t, i] = b.unpack_repr();
draw_board_block(b.x, b.y, b.width, b.height, c, !b.immovable);
draw_board_block(x, y, w, h, c, !i);
}
// Draw block placing
if (input.editing && input.has_block_add_xy) {
if (current.covers(input.block_add_x, input.block_add_y) &&
input.hov_x >= input.block_add_x && input.hov_y >= input.block_add_y) {
if (current.covers(input.block_add_x, input.block_add_y) && input.hov_x >= input.block_add_x && input.hov_y >=
input.block_add_y) {
bool collides = false;
for (const puzzle::block& b : current) {
if (b.collides(puzzle::block(input.block_add_x, input.block_add_y,
input.hov_x - input.block_add_x + 1,
for (const puzzle::block b : current.block_view()) {
if (b.collides(puzzle::block(input.block_add_x, input.block_add_y, input.hov_x - input.block_add_x + 1,
input.hov_y - input.block_add_y + 1, false))) {
collides = true;
break;
}
}
if (!collides) {
draw_board_block(input.block_add_x, input.block_add_y,
input.hov_x - input.block_add_x + 1,
draw_board_block(input.block_add_x, input.block_add_y, input.hov_x - input.block_add_x + 1,
input.hov_y - input.block_add_y + 1, PURPLE);
}
}
}
// TODO: In edit mode
// - Clear Goal button doesn't work
// - Toggle Target Block button throws "Grid bounds out of range"
// - Clicking the goal to remove it throws "Grid bounds out of range"
// Draw goal boundaries when editing
if (input.editing) {
DrawRectangleLinesEx(board_grid.square_bounds(target_x, target_y, target_block->width, target_block->height), 2.0, TARGET_BLOCK_COLOR);
if (input.editing && current.get_goal() && target_block) {
DrawRectangleLinesEx(
board_grid.square_bounds(target_x, target_y, target_block->get_width(), target_block->get_height()), 2.0,
TARGET_BLOCK_COLOR);
}
}
auto user_interface::draw_graph_overlay(int fps, int ups, size_t mass_count, size_t spring_count)
-> void
auto user_interface::draw_graph_overlay(int fps, int ups, size_t mass_count, size_t spring_count) -> void
{
graph_overlay_grid.update_bounds(GetScreenWidth() / 2, MENU_HEIGHT);
debug_overlay_grid.update_bounds(GetScreenWidth() / 2, GetScreenHeight() - 75);
draw_label(graph_overlay_grid.bounds(0, 0, 1, 1),
std::format("Dist: {:0>7.2f}", camera.distance), BLACK);
draw_label(graph_overlay_grid.bounds(0, 1, 1, 1), std::format("FoV: {:0>6.2f}", camera.fov),
BLACK);
draw_label(graph_overlay_grid.bounds(0, 0, 1, 1), std::format("Dist: {:0>7.2f}", camera.distance), BLACK);
draw_label(graph_overlay_grid.bounds(0, 1, 1, 1), std::format("FoV: {:0>6.2f}", camera.fov), BLACK);
draw_label(graph_overlay_grid.bounds(0, 2, 1, 1), std::format("FPS: {:0>3}", fps), LIME);
draw_label(graph_overlay_grid.bounds(0, 3, 1, 1), std::format("UPS: {:0>3}", ups), ORANGE);
// Debug
draw_label(debug_overlay_grid.bounds(0, 0, 1, 1), std::format("Physics Debug:"), BLACK);
draw_label(debug_overlay_grid.bounds(0, 1, 1, 1), std::format("Masses: {}", mass_count),
BLACK);
draw_label(debug_overlay_grid.bounds(0, 2, 1, 1), std::format("Springs: {}", spring_count),
BLACK);
draw_label(debug_overlay_grid.bounds(0, 1, 1, 1), std::format("Masses: {}", mass_count), BLACK);
draw_label(debug_overlay_grid.bounds(0, 2, 1, 1), std::format("Springs: {}", spring_count), BLACK);
}
auto user_interface::draw(const int fps, const int ups, const size_t mass_count,
const size_t spring_count) -> void
auto user_interface::draw(const int fps, const int ups, const size_t mass_count, const size_t spring_count) -> void
{
const auto visitor = overloads{[&](const show_ok_message& msg)
{
message_title = msg.title;
message_message = msg.message;
ok_message = true;
},
[&](const show_yes_no_message& msg)
{
message_title = msg.title;
message_message = msg.message;
yes_no_handler = msg.on_yes;
yes_no_message = true;
},
[&](const show_save_preset_window& msg) { save_window = true; }};
const auto visitor = overloads{
[&](const show_ok_message& msg)
{
message_title = msg.title;
message_message = msg.message;
ok_message = true;
},
[&](const show_yes_no_message& msg)
{
message_title = msg.title;
message_message = msg.message;
yes_no_handler = msg.on_yes;
yes_no_message = true;
},
[&](const show_save_preset_window& msg)
{
save_window = true;
}
};
while (!input.ui_commands.empty()) {
const ui_command& cmd = input.ui_commands.front();