1
0
Fork 0
Univerxel/src/world/Universe.hpp

118 lines
4.1 KiB
C++

#pragma once
#include <string>
#include <thread>
#include "../data/math.hpp"
#include "../data/safe_queue.hpp"
#include "../data/safe_priority_queue.hpp"
#include "../data/circular_buffer.hpp"
#include "../data/geometry/Ray.hpp"
#include "../data/geometry/Frustum.hpp"
#include "forward.h"
#include "Area.hpp"
#include "Voxel.hpp"
namespace contouring {
class Abstract;
};
namespace buffer {
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;
/// Storage path
std::string folderPath = "world";
};
Universe(const options &);
~Universe();
/// Update physics and contouring
void update(const voxel_pos &pos, float deltaTime);
/// Apply new options
void setOptions(const options &);
struct ray_target {
area_<voxel_pos> pos;
Voxel value;
voxel_pos offset;
};
/// Get nearest voxel colliding ray
/// @note ray in world scale
std::optional<ray_target> raycast(const geometry::Ray &ray) const;
/// Set voxel at pos
std::optional<Item> set(const area_<voxel_pos> &pos, const Voxel &val);
/// Set cube of voxel with pos as center
/// MAYBE: allow set multi area
ItemList setCube(const area_<voxel_pos> &pos, const Voxel &val, int radius);
/// Check for collision on movement
bool collide(const glm::ifvec3 &pos, const glm::vec3 &vel, int density, float radius = 0) const;
/// Move with collision check
/// @note must remove velocity after colision
bool move(glm::ifvec3 &pos, const glm::vec3 &vel, int density, float radius = 0) const;
/// Entities commun properties
struct Entity {
Entity(buffer::Abstract* buffer, const glm::vec3& size = glm::vec3(1), const glm::vec3& scale = glm::vec3(1)):
buffer(buffer), size(size), scale(scale) { };
buffer::Abstract* buffer;
glm::vec3 size;
glm::vec3 scale;
struct Instance {
glm::ifvec3 pos;
glm::vec3 velocity;
};
data::generational::vector<Instance> instances;
};
/// Instante entity
entity_instance_id addEntity(entity_id type, const Entity::Instance &instance);
void getEntitiesModels(std::vector<std::pair<std::vector<glm::mat4>, buffer::Abstract *const>> &buffers, const std::optional<geometry::Frustum> &frustum, const glm::llvec3 &offset, int density);
/// Change contouring worker
void setContouring(const std::shared_ptr<contouring::Abstract>& ct);
/// Get current contouring worker
std::shared_ptr<contouring::Abstract> getContouring() const {
return contouring;
}
private:
chunk_pos last_pos = chunk_pos(INT_MAX);
/// Alive areas containing chunks
area_map areas;
using area_it_t = robin_hood::pair<area_id, std::shared_ptr<Area>>;
/// Dead areas
data::generational::vector<Area::params> far_areas;
void saveAreas() const;
data::generational::vector<Entity> entities;
bool running = true;
std::vector<std::thread> workers;
safe_priority_queue_map<area_<chunk_pos>, std::shared_ptr<Area>, int, area_hash> loadQueue; //NOTE: consider Area const (getRegion uses mutex)
safe_queue<robin_hood::pair<area_<chunk_pos>, std::shared_ptr<Chunk>>> loadedQueue;
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 Area and Chunk const
int loadDistance;
int keepDistance;
std::string folderPath;
dict_set dicts;
/// Contouring worker
std::shared_ptr<contouring::Abstract> contouring;
};
}