2024-10-15 02:59:26 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "connection.h"
|
2024-10-26 02:14:41 +02:00
|
|
|
#include "node-manager.h"
|
2024-10-20 23:42:38 +02:00
|
|
|
#include "raylib.h"
|
2024-10-30 02:00:40 +01:00
|
|
|
#include "raymath.h"
|
2024-10-15 02:59:26 +02:00
|
|
|
#include <map>
|
2024-10-16 02:26:02 +02:00
|
|
|
#include <vector>
|
2024-10-15 02:59:26 +02:00
|
|
|
|
|
|
|
|
class ConnectionManager{
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2024-10-26 02:14:41 +02:00
|
|
|
ConnectionManager(NodeManager *nm) : nm(nm) {}
|
|
|
|
|
|
|
|
|
|
NodeManager *nm;
|
2024-10-15 02:59:26 +02:00
|
|
|
|
|
|
|
|
std::vector<Connection> connections;
|
|
|
|
|
|
2024-10-26 02:14:41 +02:00
|
|
|
std::map<int, int> counter;
|
2024-10-15 02:59:26 +02:00
|
|
|
|
2024-10-25 02:43:41 +02:00
|
|
|
void reset(){
|
|
|
|
|
counter.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-26 02:14:41 +02:00
|
|
|
int next(int current, int last){
|
2024-10-30 02:00:40 +01:00
|
|
|
std::vector<Connection*> possible_connections;
|
|
|
|
|
std::vector<int> next_node_ids;
|
2024-10-15 02:59:26 +02:00
|
|
|
|
2024-10-30 02:00:40 +01:00
|
|
|
for(auto& c : connections){
|
|
|
|
|
if((c.v1 == current && c.v1_v2) || (c.v2 == current && c.v2_v1)) {
|
|
|
|
|
c.active = false;
|
|
|
|
|
possible_connections.push_back(&c);
|
|
|
|
|
next_node_ids.push_back( c.v1 == current ? c.v2 : c.v1 );
|
2024-10-15 02:59:26 +02:00
|
|
|
}
|
2024-10-19 17:41:31 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-30 02:00:40 +01:00
|
|
|
/*if(possible_connections.size() == 2) {*/
|
|
|
|
|
/* int next = next_node_ids[0] == last ? next_node_ids[1] : next_node_ids[0];*/
|
|
|
|
|
/* if(possible_connections[0]->v1 == next || possible_connections[0]->v2 == next) possible_connections[0]->active = true;*/
|
|
|
|
|
/* if(possible_connections[1]->v1 == next || possible_connections[1]->v2 == next) possible_connections[1]->active = true;*/
|
|
|
|
|
/* return next;*/
|
|
|
|
|
/*}*/
|
2024-10-15 02:59:26 +02:00
|
|
|
|
2024-10-30 02:00:40 +01:00
|
|
|
if(possible_connections.empty()) return -1;
|
2024-10-15 02:59:26 +02:00
|
|
|
|
|
|
|
|
int return_path = 0;
|
2024-10-30 02:00:40 +01:00
|
|
|
float angle = -1;
|
|
|
|
|
if(nm->nodes.at(current).distributionMode == Distribution::GEOMETRIC){
|
|
|
|
|
Vec2D<float> coming_from = (nm->nodes.at(current).position - nm->nodes.at(last).position).norm();
|
|
|
|
|
int i = 0;
|
|
|
|
|
for(auto n : possible_connections){
|
|
|
|
|
int next_position = n->v1 == current ? n->v2 : n->v1;
|
|
|
|
|
float angle_v1_v2 = coming_from.norm().dot((nm->nodes.at(next_position).position - nm->nodes.at(current).position).norm());
|
|
|
|
|
if(angle_v1_v2 > angle) {
|
|
|
|
|
return_path = i;
|
|
|
|
|
angle = angle_v1_v2;
|
|
|
|
|
}
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(nm->nodes.at(current).distributionMode == Distribution::ROUNDROBIN){
|
2024-10-16 02:26:02 +02:00
|
|
|
if(counter.find(current) == counter.end()) {
|
|
|
|
|
counter[current] = 0;
|
|
|
|
|
} else {
|
2024-10-30 02:00:40 +01:00
|
|
|
counter[current] = (counter[current] + 1) % possible_connections.size();
|
2024-10-16 02:26:02 +02:00
|
|
|
return_path = counter[current];
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-30 02:00:40 +01:00
|
|
|
|
|
|
|
|
if(nm->nodes.at(current).distributionMode == Distribution::RANDOM){
|
|
|
|
|
return_path = rand() % possible_connections.size();
|
2024-10-15 02:59:26 +02:00
|
|
|
}
|
2024-10-30 02:00:40 +01:00
|
|
|
|
|
|
|
|
possible_connections.at(return_path)->active = true;
|
|
|
|
|
return possible_connections.at(return_path)->v1 == current ? possible_connections.at(return_path)->v2 : possible_connections.at(return_path)->v1;
|
2024-10-15 02:59:26 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-26 02:14:41 +02:00
|
|
|
void addConnection(int from, int to){
|
|
|
|
|
connections.emplace_back(&nm->nodes, from, to);
|
2024-10-23 00:23:24 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-22 01:55:31 +02:00
|
|
|
void drawConnections(Vec2D<float> *mouse_pos, Camera2D *cam){
|
2024-10-16 02:26:02 +02:00
|
|
|
std::erase_if(connections, [](Connection c){ return c.remove == true;});
|
2024-10-15 02:59:26 +02:00
|
|
|
for(auto& c : connections){
|
2024-10-20 23:42:38 +02:00
|
|
|
c.draw(mouse_pos, cam);
|
2024-10-15 02:59:26 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|