2020-07-25 16:45:03 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-07-31 17:09:44 +00:00
|
|
|
#include <string>
|
2020-07-25 16:45:03 +00:00
|
|
|
#include <thread>
|
2020-07-31 17:09:44 +00:00
|
|
|
#include "../data/math.hpp"
|
2020-07-25 16:45:03 +00:00
|
|
|
#include "../data/safe_queue.hpp"
|
|
|
|
#include "../data/safe_priority_queue.hpp"
|
|
|
|
#include "../data/circular_buffer.hpp"
|
|
|
|
#include "../data/geometry/Ray.hpp"
|
2020-07-31 17:09:44 +00:00
|
|
|
#include "forward.h"
|
2020-08-02 20:15:53 +00:00
|
|
|
#include "Area.hpp"
|
2020-07-31 17:09:44 +00:00
|
|
|
#include "Voxel.hpp"
|
2020-07-25 16:45:03 +00:00
|
|
|
|
|
|
|
#define REPORT_BUFFER_SIZE 128
|
|
|
|
|
|
|
|
namespace contouring {
|
|
|
|
class Abstract;
|
|
|
|
};
|
|
|
|
|
|
|
|
using namespace data;
|
|
|
|
/// Universe data
|
|
|
|
namespace world {
|
|
|
|
/// Whole universe container
|
|
|
|
class Universe {
|
|
|
|
public:
|
|
|
|
/// Distance management
|
|
|
|
struct options {
|
|
|
|
/// Radius in chunks to load if missing
|
|
|
|
int loadDistance = 5;
|
|
|
|
/// Radius in chunks to keep in memory
|
|
|
|
int keepDistance = 6;
|
2020-07-26 20:53:14 +00:00
|
|
|
/// Storage path
|
|
|
|
std::string folderPath = "world";
|
2020-07-25 16:45:03 +00:00
|
|
|
};
|
|
|
|
/// Reports to UI
|
|
|
|
struct report {
|
|
|
|
/// Chunks in memory
|
|
|
|
circular_buffer<float> chunk_count = circular_buffer<float>(REPORT_BUFFER_SIZE, 0); // MAYBE: store int
|
|
|
|
/// Loaded chunks
|
|
|
|
circular_buffer<float> chunk_load = circular_buffer<float>(REPORT_BUFFER_SIZE, 0);
|
|
|
|
/// Saved chunks
|
|
|
|
circular_buffer<float> chunk_unload = circular_buffer<float>(REPORT_BUFFER_SIZE, 0);
|
2020-07-31 17:09:44 +00:00
|
|
|
/// Regions in memory
|
|
|
|
circular_buffer<float> region_count = circular_buffer<float>(REPORT_BUFFER_SIZE, 0);
|
2020-07-25 16:45:03 +00:00
|
|
|
};
|
|
|
|
|
2020-07-31 17:09:44 +00:00
|
|
|
Universe(const options &);
|
2020-07-25 16:45:03 +00:00
|
|
|
~Universe();
|
|
|
|
|
|
|
|
/// Update physics and contouring
|
2020-08-01 21:31:01 +00:00
|
|
|
void update(const voxel_pos &pos, report &rep);
|
2020-07-25 16:45:03 +00:00
|
|
|
/// Apply new options
|
|
|
|
void setOptions(const options &);
|
|
|
|
|
|
|
|
/// Get nearest voxel colliding ray
|
|
|
|
/// @note ray in world scale
|
2020-08-02 20:15:53 +00:00
|
|
|
std::optional<std::pair<area_<voxel_pos>, Voxel>> raycast(const geometry::Ray &ray) const;
|
2020-07-25 16:45:03 +00:00
|
|
|
/// Set voxel at pos
|
2020-08-02 20:15:53 +00:00
|
|
|
std::optional<Item> set(const area_<voxel_pos> &pos, const Voxel &val);
|
2020-07-25 16:45:03 +00:00
|
|
|
/// Set cube of voxel with pos as center
|
2020-08-02 20:15:53 +00:00
|
|
|
/// MAYBE: allow set multi area
|
|
|
|
ItemList setCube(const area_<voxel_pos> &pos, const Voxel &val, int radius);
|
2020-07-25 16:45:03 +00:00
|
|
|
|
|
|
|
/// Change contouring worker
|
2020-07-31 23:17:09 +00:00
|
|
|
void setContouring(const std::shared_ptr<contouring::Abstract>& ct);
|
2020-07-25 16:45:03 +00:00
|
|
|
/// Get current contouring worker
|
|
|
|
std::shared_ptr<contouring::Abstract> getContouring() const {
|
|
|
|
return contouring;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
chunk_pos last_pos = chunk_pos(INT_MAX);
|
|
|
|
|
2020-08-02 20:15:53 +00:00
|
|
|
/// Alive areas containing chunks
|
|
|
|
area_map areas;
|
|
|
|
using area_it_t = robin_hood::pair<area_id, std::shared_ptr<Area>>;
|
2020-07-25 16:45:03 +00:00
|
|
|
|
2020-08-02 20:15:53 +00:00
|
|
|
//TODO: far_areas: <pos, {id, size, seed}>
|
2020-07-25 16:45:03 +00:00
|
|
|
|
2020-07-30 16:35:13 +00:00
|
|
|
bool running = true;
|
|
|
|
std::vector<std::thread> loadWorkers;
|
2020-08-02 20:15:53 +00:00
|
|
|
safe_priority_queue_map<area_<chunk_pos>, std::shared_ptr<Area>, int, area_hash> loadQueue;
|
|
|
|
safe_queue<robin_hood::pair<area_<chunk_pos>, std::shared_ptr<Chunk>>> loadedQueue;
|
2020-07-25 16:45:03 +00:00
|
|
|
|
2020-07-30 16:35:13 +00:00
|
|
|
std::vector<std::thread> saveWorkers;
|
2020-08-02 20:15:53 +00:00
|
|
|
using save_task_t = std::pair<area_it_t, robin_hood::pair<chunk_pos, std::shared_ptr<Chunk>>>;
|
|
|
|
data::safe_queue<save_task_t> saveQueue; //NOTE: consider const Area and Chunk
|
2020-07-25 16:45:03 +00:00
|
|
|
|
|
|
|
int loadDistance;
|
|
|
|
int keepDistance;
|
2020-07-26 20:53:14 +00:00
|
|
|
std::string folderPath;
|
2020-07-25 16:45:03 +00:00
|
|
|
|
2020-07-31 22:11:08 +00:00
|
|
|
dict_set dicts;
|
2020-07-30 16:35:13 +00:00
|
|
|
|
2020-07-25 16:45:03 +00:00
|
|
|
/// Contouring worker
|
|
|
|
std::shared_ptr<contouring::Abstract> contouring;
|
|
|
|
};
|
|
|
|
}
|