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-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-15 02:59:26 +02:00
|
|
|
std::vector<Connection*> paths;
|
2024-10-26 02:14:41 +02:00
|
|
|
std::vector<int> node_ids;
|
2024-10-15 02:59:26 +02:00
|
|
|
|
|
|
|
|
for(auto& p : connections){
|
2024-10-20 23:42:38 +02:00
|
|
|
if((p.v1 == current && p.v1_v2) || (p.v2 == current && p.v2_v1)) {
|
2024-10-15 02:59:26 +02:00
|
|
|
p.active = false;
|
|
|
|
|
paths.push_back(&p);
|
2024-10-26 02:14:41 +02:00
|
|
|
node_ids.push_back( p.v1 == current ? p.v2 : p.v1 );
|
2024-10-15 02:59:26 +02:00
|
|
|
}
|
2024-10-19 17:41:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(paths.size() == 2) {
|
2024-10-26 02:14:41 +02:00
|
|
|
int next = node_ids[0] == last ? node_ids[1] : node_ids[0];
|
2024-10-19 17:41:31 +02:00
|
|
|
if(paths[0]->v1 == next || paths[0]->v2 == next) paths[0]->active = true;
|
|
|
|
|
if(paths[1]->v1 == next || paths[1]->v2 == next) paths[1]->active = true;
|
|
|
|
|
return next;
|
2024-10-15 02:59:26 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-26 02:14:41 +02:00
|
|
|
if(paths.empty()) return -1;
|
2024-10-15 02:59:26 +02:00
|
|
|
|
|
|
|
|
int return_path = 0;
|
2024-10-16 02:26:02 +02:00
|
|
|
|
2024-10-26 02:14:41 +02:00
|
|
|
if(nm->nodes.at(current).mode == Distribution::ROUNDROBIN){
|
2024-10-16 02:26:02 +02:00
|
|
|
if(counter.find(current) == counter.end()) {
|
|
|
|
|
counter[current] = 0;
|
|
|
|
|
} else {
|
|
|
|
|
counter[current] = (counter[current] + 1) % paths.size();
|
|
|
|
|
return_path = counter[current];
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-26 02:14:41 +02:00
|
|
|
if(nm->nodes.at(current).mode == Distribution::RANDOM){
|
2024-10-16 02:26:02 +02:00
|
|
|
return_path = rand() % paths.size();
|
2024-10-15 02:59:26 +02:00
|
|
|
}
|
2024-10-16 02:26:02 +02:00
|
|
|
paths.at(return_path)->active = true;
|
|
|
|
|
return paths.at(return_path)->v1 == current ? paths.at(return_path)->v2 : paths.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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|