implement bfs multi-target distance calculation to nearest winning state

This commit is contained in:
2026-02-25 01:15:47 +01:00
parent b9e3ab8d2d
commit fd58f217c6
11 changed files with 208 additions and 37 deletions

32
include/distance.hpp Normal file
View File

@ -0,0 +1,32 @@
#ifndef __DISTANCE_HPP_
#define __DISTANCE_HPP_
#include "config.hpp"
#include <cstddef>
#include <vector>
struct DistanceResult {
// distances[n] = distance from n to target
std::vector<int> distances;
// parents[n] = next node on the path from n to target
std::vector<std::size_t> parents;
// nearest_target[n] = closest target node to n
std::vector<std::size_t> nearest_targets;
auto Clear() -> void;
auto Empty() -> bool;
};
auto CalculateDistances(
std::size_t node_count,
const std::vector<std::pair<std::size_t, std::size_t>> &edges,
const std::vector<std::size_t> &targets) -> DistanceResult;
auto GetPath(const DistanceResult &result, std::size_t source)
-> std::vector<std::size_t>;
#endif

View File

@ -141,11 +141,9 @@ class ThreadedPhysics {
std::condition_variable_any data_ready_cnd;
std::condition_variable_any data_consumed_cnd;
unsigned int ups = 0;
std::vector<Vector3> masses; // Read by renderer
bool data_ready = false;
bool data_consumed = true;
std::vector<Vector3> masses; // Read by renderer
std::vector<std::pair<std::size_t, std::size_t>>
springs; // Read by renderer
std::atomic<bool> running{true};
};

View File

@ -67,15 +67,11 @@ private:
public:
auto UpdateTextureSizes() -> void;
auto DrawMassSprings(
const std::vector<Vector3> &masses,
const std::vector<std::pair<std::size_t, std::size_t>> &springs) -> void;
auto DrawMassSprings(const std::vector<Vector3> &masses) -> void;
auto DrawKlotski() -> void;
auto DrawMenu(const std::vector<Vector3> &masses,
const std::vector<std::pair<std::size_t, std::size_t>> &springs)
-> void;
auto DrawMenu(const std::vector<Vector3> &masses) -> void;
auto DrawTextures(float ups) -> void;
};

View File

@ -2,6 +2,7 @@
#define __STATE_HPP_
#include "config.hpp"
#include "distance.hpp"
#include "physics.hpp"
#include "puzzle.hpp"
@ -15,10 +16,23 @@ public:
std::vector<State> presets;
// Some stuff is faster to map from state to mass (e.g. in the renderer)
std::unordered_map<State, std::size_t> states;
std::unordered_set<State> winning_states;
std::unordered_set<State> visited_states;
// Other stuff maps from mass to state :/
std::unordered_map<std::size_t, State> masses;
std::vector<std::size_t> winning_path;
// Fuck it, duplicate the springs too, we don't even need to copy them from
// the physics thread then...
std::vector<std::pair<std::size_t, std::size_t>> springs;
// Distance calculation result can be buffered and reused to calculate a new
// path on the same graph
DistanceResult target_distances;
int current_preset;
State starting_state;
State current_state;
@ -62,6 +76,10 @@ public:
auto FindWinningStates() -> void;
auto FindTargetDistances() -> void;
auto FindTargetPath() -> void;
auto CurrentMassIndex() const -> std::size_t;
};