2022-11-10 19:47:39 +01:00
# ifndef CHUNK_H
# define CHUNK_H
# include <glm/glm.hpp>
# include <glm/gtc/matrix_transform.hpp>
# include <glm/gtc/type_ptr.hpp>
2023-04-23 16:28:36 +02:00
# include <atomic>
2022-11-10 19:47:39 +01:00
# include <array>
# include <bitset>
2023-03-03 21:33:11 +01:00
# include <mutex>
# include <vector>
2022-11-10 19:47:39 +01:00
# include "block.hpp"
2022-11-20 19:14:11 +01:00
# include "spacefilling.hpp"
# include "intervalmap.hpp"
2023-02-12 12:18:30 +01:00
# include "shader.hpp"
2022-11-10 19:47:39 +01:00
2023-05-20 21:52:33 +02:00
# define CHUNK_SIZE 32
2022-11-10 19:47:39 +01:00
# define CHUNK_VOLUME (CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE)
# define CHUNK_MAX_INDEX (CHUNK_VOLUME - 1)
2023-10-03 22:30:42 +02:00
// int32_t is fine, since i'm limiting the coordinate to only use up to ten bits (1023). There's actually two spare bits
typedef int32_t chunk_index_t ;
typedef int16_t chunk_intcoord_t ;
2023-10-04 11:14:18 +02:00
typedef uint16_t chunk_state_t ;
2023-10-03 22:30:42 +02:00
2022-11-10 19:47:39 +01:00
namespace Chunk
{
2023-10-03 22:30:42 +02:00
chunk_index_t calculateIndex ( chunk_intcoord_t i , chunk_intcoord_t j , chunk_intcoord_t k ) ;
chunk_index_t calculateIndex ( glm : : vec3 pos ) ;
2023-10-04 11:14:18 +02:00
constexpr chunk_state_t CHUNK_STATE_GENERATED = 1 ;
constexpr chunk_state_t CHUNK_STATE_MESHED = 2 ;
constexpr chunk_state_t CHUNK_STATE_MESH_LOADED = 4 ;
constexpr chunk_state_t CHUNK_STATE_LOADED = 8 ;
constexpr chunk_state_t CHUNK_STATE_OUTOFVISION = 16 ;
constexpr chunk_state_t CHUNK_STATE_UNLOADED = 32 ;
constexpr chunk_state_t CHUNK_STATE_EMPTY = 64 ;
2023-10-04 11:23:38 +02:00
constexpr chunk_state_t CHUNK_STATE_IN_GENERATION_QUEUE = 128 ;
constexpr chunk_state_t CHUNK_STATE_IN_MESHING_QUEUE = 256 ;
constexpr chunk_state_t CHUNK_STATE_IN_DELETING_QUEUE = 512 ;
2022-11-10 19:47:39 +01:00
int coord3DTo1D ( int x , int y , int z ) ;
class Chunk
{
public :
Chunk ( glm : : vec3 pos = glm : : vec3 ( 0.0f ) ) ; // a default value for the argument satisfies the need for a default constructor when using the type in an unordered_map (i.e. in chunkmanager)
2022-11-11 20:36:39 +01:00
~ Chunk ( ) ;
2022-11-10 19:47:39 +01:00
public :
glm : : vec3 getPosition ( ) { return this - > position ; }
2023-10-04 11:14:18 +02:00
void setState ( chunk_state_t nstate , bool value ) ;
2023-10-04 11:23:38 +02:00
bool getState ( chunk_state_t n ) { return ( this - > state & n ) = = n ; }
bool isFree ( ) { return ! (
this - > getState ( CHUNK_STATE_IN_GENERATION_QUEUE ) | |
this - > getState ( CHUNK_STATE_IN_MESHING_QUEUE ) | |
this - > getState ( CHUNK_STATE_IN_DELETING_QUEUE )
) ; }
chunk_state_t getTotalState ( ) { return this - > state ; }
2022-11-10 19:47:39 +01:00
void setBlock ( Block b , int x , int y , int z ) ;
2022-11-20 19:47:04 +01:00
void setBlocks ( int start , int end , Block b ) ;
2022-11-10 19:47:39 +01:00
Block getBlock ( int x , int y , int z ) ;
2022-11-20 19:14:11 +01:00
IntervalMap < Block > & getBlocks ( ) { return ( this - > blocks ) ; }
2023-03-12 18:31:22 +01:00
std : : unique_ptr < Block [ ] > getBlocksArray ( int * len ) { return ( this - > blocks . toArray ( len ) ) ; }
2022-11-10 19:47:39 +01:00
2023-02-12 14:36:59 +01:00
public :
2023-04-29 14:54:43 +02:00
std : : atomic < float > unload_timer { 0 } ;
2023-10-03 22:32:16 +02:00
chunk_index_t getIndex ( ) { return this - > index ; }
2023-02-12 14:36:59 +01:00
2022-11-10 19:47:39 +01:00
private :
glm : : vec3 position { } ;
2022-11-20 19:14:11 +01:00
IntervalMap < Block > blocks { } ;
2023-03-03 21:33:11 +01:00
2023-10-04 11:14:18 +02:00
std : : atomic < chunk_state_t > state { 0 } ;
2023-10-03 22:32:16 +02:00
chunk_index_t index ;
2022-11-10 19:47:39 +01:00
} ;
} ;
2023-03-12 18:31:22 +01:00
# endif