wip: integrating threaded decoupled physics
Current Issues: - HUGE memory leak - HUGE amount of needles copying - FillGraph() does thousands of lock_guards instead of one - Can no longer rely on new states appearing immediately - have to check each access - Physics run as fast as possible, no constant sim speed` - Irregular long freezes
This commit is contained in:
@ -1,8 +1,8 @@
|
||||
#include "state.hpp"
|
||||
#include "config.hpp"
|
||||
#include "presets.hpp"
|
||||
#include "tracy.hpp"
|
||||
|
||||
#include <mutex>
|
||||
#include <raymath.h>
|
||||
|
||||
auto StateManager::LoadPreset(int preset) -> void {
|
||||
@ -38,30 +38,18 @@ auto StateManager::FillGraph() -> void {
|
||||
std::pair<std::unordered_set<State>, std::vector<std::pair<State, State>>>
|
||||
closure = current_state.Closure();
|
||||
for (const auto &state : closure.first) {
|
||||
mass_springs.AddMass(MASS, false, state);
|
||||
physics.AddMassCmd(state);
|
||||
}
|
||||
for (const auto &[from, to] : closure.second) {
|
||||
mass_springs.AddSpring(from, to, SPRING_CONSTANT, DAMPENING_CONSTANT,
|
||||
REST_LENGTH);
|
||||
physics.AddSpringCmd(from, to);
|
||||
}
|
||||
std::cout << "Inserted " << mass_springs.masses.size() << " masses and "
|
||||
<< mass_springs.springs.size() << " springs." << std::endl;
|
||||
FindWinningStates();
|
||||
std::cout << "Consuming "
|
||||
<< sizeof(decltype(*mass_springs.masses.begin())) *
|
||||
mass_springs.masses.size()
|
||||
<< " Bytes for masses." << std::endl;
|
||||
std::cout << "Consuming "
|
||||
<< sizeof(decltype(*mass_springs.springs.begin())) *
|
||||
mass_springs.springs.size()
|
||||
<< " Bytes for springs." << std::endl;
|
||||
}
|
||||
|
||||
auto StateManager::UpdateGraph() -> void {
|
||||
if (previous_state != current_state) {
|
||||
mass_springs.AddMass(MASS, false, current_state);
|
||||
mass_springs.AddSpring(current_state, previous_state, SPRING_CONSTANT,
|
||||
DAMPENING_CONSTANT, REST_LENGTH);
|
||||
physics.AddMassCmd(current_state);
|
||||
physics.AddSpringCmd(current_state, previous_state);
|
||||
if (win_conditions[current_preset](current_state)) {
|
||||
winning_states.insert(current_state);
|
||||
}
|
||||
@ -72,8 +60,8 @@ auto StateManager::UpdateGraph() -> void {
|
||||
auto StateManager::ClearGraph() -> void {
|
||||
winning_states.clear();
|
||||
visited_states.clear();
|
||||
mass_springs.Clear();
|
||||
mass_springs.AddMass(MASS, false, current_state);
|
||||
physics.ClearCmd();
|
||||
physics.AddMassCmd(current_state);
|
||||
|
||||
// The previous_state is no longer in the graph
|
||||
previous_state = current_state;
|
||||
@ -84,14 +72,16 @@ auto StateManager::ClearGraph() -> void {
|
||||
|
||||
auto StateManager::FindWinningStates() -> void {
|
||||
winning_states.clear();
|
||||
for (const auto &[state, mass] : mass_springs.state_masses) {
|
||||
std::unordered_map<State, int> state_masses;
|
||||
{
|
||||
std::lock_guard<LockableBase(std::mutex)> lock(physics.state.pos_mtx);
|
||||
state_masses = physics.state.state_masses;
|
||||
}
|
||||
for (const auto &[state, mass] : state_masses) {
|
||||
if (win_conditions[current_preset](state)) {
|
||||
winning_states.insert(state);
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Found " << winning_states.size() << " winning states."
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
auto StateManager::CurrentGenerator() -> StateGenerator {
|
||||
|
||||
Reference in New Issue
Block a user