#include <core/object.h>
#include <core/reference.h>
#include <core/math/random_number_generator.h>
#include <modules/opensimplex/open_simplex_noise.h>

class RoadMap: public Object {
	GDCLASS(RoadMap, Object)
public:
	RoadMap();
	~RoadMap();
	static RoadMap *get_singleton();
	static void create_singleton();
	static void destroy_singleton();

protected:
	Ref<OpenSimplexNoise> noise;
	Ref<RandomNumberGenerator> rnd;
	struct Segment {
		Vector3 p1, p2;
	};
	Vector<Segment> segments;
	struct Pq {
		struct PqItem {
			struct Segment data;
			int priority;
		};
		List<PqItem> queue;
		void push(int priority, const struct Segment &seg)
		{
			List<PqItem>::Element *e;
			PqItem it;
			it.data = seg;
			it.priority = priority;

			for (e = queue.front(); e; e = e->next()) {
				PqItem item = e->get();
				if (item.priority > priority) {
					queue.insert_before(e, it);
					break;
				}
				if (!e->next())
					queue.push_back(it);

			}
		}
		Segment peek()
		{
			return queue.front()->get().data;
		}
		Segment pop()
		{
			Segment seg = queue.front()->get().data;
			queue.pop_front();
			return seg;
		}
	};
	struct Pq queue;
	void gen_primary_network();
};
