From 846ff72d1fd81f3dbb53249324031ba8445df6ac Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Sat, 28 Feb 2026 22:52:05 +0100 Subject: [PATCH] fix bug where starting index was set incorrectly when populating graph --- default.puzzle | 2 +- include/renderer.hpp | 10 +++++----- include/state_manager.hpp | 3 ++- src/puzzle.cpp | 2 +- src/state_manager.cpp | 39 +++++++++++++++++++++------------------ 5 files changed, 30 insertions(+), 26 deletions(-) diff --git a/default.puzzle b/default.puzzle index 2501232..1796baa 100644 --- a/default.puzzle +++ b/default.puzzle @@ -41,4 +41,4 @@ F451311bb..1112....12..12....11....1121..21.. F451312111111..12bb..12........21..11....21.. # Supercompo -F4513..bb....11....111221..12..21....1121..11 +F4513..bb....11....111221..12..21....1121..11 \ No newline at end of file diff --git a/include/renderer.hpp b/include/renderer.hpp index 0e365b2..e444b73 100644 --- a/include/renderer.hpp +++ b/include/renderer.hpp @@ -51,11 +51,11 @@ public: instancing_shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(instancing_shader, "viewPos"); - infoln("LOC vertexPosition: {}", - rlGetLocationAttrib(instancing_shader.id, "vertexPosition")); - infoln("LOC instanceTransform: {}", - rlGetLocationAttrib(instancing_shader.id, "instanceTransform")); - infoln("LOC instanceColor: {}", rlGetLocationAttrib(instancing_shader.id, "instanceColor")); + // infoln("LOC vertexPosition: {}", + // rlGetLocationAttrib(instancing_shader.id, "vertexPosition")); + // infoln("LOC instanceTransform: {}", + // rlGetLocationAttrib(instancing_shader.id, "instanceTransform")); + // infoln("LOC instanceColor: {}", rlGetLocationAttrib(instancing_shader.id, "instanceColor")); // vertex_mat.maps[MATERIAL_MAP_DIFFUSE].color = VERTEX_COLOR; vertex_mat.shader = instancing_shader; diff --git a/include/state_manager.hpp b/include/state_manager.hpp index f95c010..a0b02ff 100644 --- a/include/state_manager.hpp +++ b/include/state_manager.hpp @@ -34,7 +34,7 @@ private: std::unordered_set path_indices; // For faster lookup if a vertex is part of the path in renderer - std::stack move_history; // Moves between the starting state and the current state + std::vector move_history; // Moves between the starting state and the current state std::unordered_map visit_counts; // How often each state was visited size_t starting_state_index = 0; @@ -42,6 +42,7 @@ private: size_t previous_state_index = 0; int total_moves = 0; + bool edited = false; public: state_manager(threaded_physics& _physics, const std::string& _preset_file) : physics(_physics) diff --git a/src/puzzle.cpp b/src/puzzle.cpp index 40f97be..69c41e9 100644 --- a/src/puzzle.cpp +++ b/src/puzzle.cpp @@ -498,7 +498,7 @@ auto puzzle::explore_state_space() const ZoneScoped; #endif - infoln("Exploring state space, this might take a while..."); + // infoln("Exploring state space, this might take a while..."); std::vector state_pool; std::unordered_map state_indices; // Helper to construct the links vector diff --git a/src/state_manager.cpp b/src/state_manager.cpp index b34ab84..a387537 100644 --- a/src/state_manager.cpp +++ b/src/state_manager.cpp @@ -6,7 +6,7 @@ #include #ifdef TRACY - #include +#include #endif auto state_manager::synced_try_insert_state(const puzzle& state) -> size_t @@ -35,8 +35,7 @@ auto state_manager::synced_insert_link(size_t first_index, size_t second_index) } auto state_manager::synced_insert_statespace(const std::vector& states, - const std::vector>& _links) - -> void + const std::vector>& _links) -> void { if (!state_pool.empty() || !state_indices.empty() || !links.empty()) { warnln("Inserting statespace but collections haven't been cleared"); @@ -71,7 +70,7 @@ auto state_manager::synced_clear_statespace() -> void winning_path.clear(); path_indices.clear(); - move_history = std::stack(); + move_history.clear(); visit_counts.clear(); // Queue an update to the physics engine state to keep in sync @@ -150,6 +149,7 @@ auto state_manager::load_preset(const size_t preset) -> void { clear_graph_and_add_current(preset_states.at(preset)); current_preset = preset; + edited = false; } auto state_manager::load_previous_preset() -> void @@ -186,7 +186,7 @@ auto state_manager::update_current_state(const puzzle& p) -> void current_state_index = index; if (current_state_index != previous_state_index) { - move_history.push(previous_state_index); + move_history.emplace_back(previous_state_index); } if (p.won()) { @@ -208,12 +208,14 @@ auto state_manager::edit_starting_state(const puzzle& p) -> void { clear_graph_and_add_current(p); - move_history = std::stack(); + move_history.clear(); total_moves = 0; for (int& visits : visit_counts | std::views::values) { visits = 0; } visit_counts[current_state_index]++; + + edited = true; } auto state_manager::goto_starting_state() -> void @@ -226,7 +228,7 @@ auto state_manager::goto_starting_state() -> void visits = 0; } visit_counts[current_state_index]++; - move_history = std::stack(); + move_history.clear(); total_moves = 0; } @@ -251,11 +253,11 @@ auto state_manager::goto_previous_state() -> void return; } - update_current_state(get_state(move_history.top())); + update_current_state(get_state(move_history.back())); // Pop twice because update_current_state adds the state again... - move_history.pop(); - move_history.pop(); + move_history.pop_back(); + move_history.pop_back(); } auto state_manager::goto_most_distant_state() -> void @@ -287,23 +289,24 @@ auto state_manager::goto_closest_target_state() -> void auto state_manager::populate_graph() -> void { -#ifdef TRACY + #ifdef TRACY ZoneScoped; -#endif + #endif // Need to make a copy before clearing the state_pool + 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(); // Explore the entire statespace starting from the current state - const auto& [states, _links] = p.explore_state_space(); + const auto& [states, _links] = s.explore_state_space(); synced_insert_statespace(states, _links); current_state_index = state_indices.at(p); previous_state_index = current_state_index; - starting_state_index = current_state_index; + starting_state_index = state_indices.at(s); // Search for cool stuff populate_winning_indices(); @@ -313,7 +316,7 @@ auto state_manager::populate_graph() -> void auto state_manager::clear_graph_and_add_current(const puzzle& p) -> void { - // Do we need to make a copy before clearing the state_pool? + // We need to make a copy before clearing the state_pool const puzzle _p = p; // NOLINT(*-unnecessary-copy-initialization) synced_clear_statespace(); @@ -345,9 +348,9 @@ auto state_manager::populate_winning_indices() -> void auto state_manager::populate_node_target_distances() -> void { -#ifdef TRACY + #ifdef TRACY ZoneScoped; -#endif + #endif if (links.empty() || winning_indices.empty()) { return; @@ -483,5 +486,5 @@ auto state_manager::get_total_moves() const -> size_t auto state_manager::was_edited() const -> bool { - return preset_states.at(current_preset) != get_state(starting_state_index); + return edited; } \ No newline at end of file