2021-11-15 20:58:13 -05:00
# include "world.h"
2021-12-03 11:49:59 -05:00
World : : World ( ) { }
2021-11-15 20:58:13 -05:00
2021-12-03 11:49:59 -05:00
World : : ~ World ( ) { }
2021-11-15 20:58:13 -05:00
Array2d < Chunk * > & World : : GetChunks ( ) { return m_chunks ; }
2021-11-15 21:39:50 -05:00
Chunk * World : : ChunkAt ( float x , float y , float z ) const {
2021-12-03 11:49:59 -05:00
int cx = ( int ) x / CHUNK_SIZE_X ;
int cz = ( int ) z / CHUNK_SIZE_Z ;
2021-11-15 21:39:50 -05:00
2021-12-03 11:49:59 -05:00
if ( x < 0 | | y < 0 | | z < 0 | |
x > = WORLD_SIZE_X * CHUNK_SIZE_X | |
z > = CHUNK_SIZE_Z * WORLD_SIZE_Y | |
y > CHUNK_SIZE_Y )
return 0 ;
2021-11-15 21:39:50 -05:00
2021-12-03 11:49:59 -05:00
return m_chunks . Get ( cx , cz ) ;
2021-11-15 21:39:50 -05:00
}
Chunk * World : : ChunkAt ( const Vector3f & pos ) const { return ChunkAt ( pos . x , pos . y , pos . z ) ; }
BlockType World : : BlockAt ( float x , float y , float z , BlockType defaultBlockType ) const {
2021-12-03 11:49:59 -05:00
Chunk * c = ChunkAt ( x , y , z ) ;
2021-11-15 21:39:50 -05:00
2021-12-03 11:49:59 -05:00
if ( ! c )
return defaultBlockType ;
2021-11-15 21:39:50 -05:00
2021-12-03 11:49:59 -05:00
int bx = ( int ) x % CHUNK_SIZE_X ;
int by = ( int ) y % CHUNK_SIZE_Y ;
int bz = ( int ) z % CHUNK_SIZE_Z ;
2021-11-15 21:39:50 -05:00
2021-12-03 11:49:59 -05:00
return c - > GetBlock ( bx , by , bz ) ;
2021-11-15 21:39:50 -05:00
}
BlockType World : : BlockAt ( const Vector3f & pos , BlockType defaultBlockType ) const {
2021-12-03 11:49:59 -05:00
return BlockAt ( pos . x , pos . y , pos . z , defaultBlockType ) ;
2021-11-15 21:39:50 -05:00
}
2021-11-30 19:55:11 -05:00
2021-12-07 18:59:50 -05:00
void World : : TransposeWorld ( Player & player , Bullet * bullets [ MAX_BULLETS ] ) {
2021-12-08 16:29:36 -05:00
int x = 0 , y = 0 ;
2021-12-03 11:49:59 -05:00
2021-12-13 23:56:20 -05:00
if ( player . GetPosition ( ) . x > ( WORLD_SIZE_X * CHUNK_SIZE_X ) * .6f ) + + x ;
else if ( player . GetPosition ( ) . x < ( WORLD_SIZE_X * CHUNK_SIZE_X ) * .4f ) - - x ;
if ( player . GetPosition ( ) . z > ( WORLD_SIZE_Y * CHUNK_SIZE_Z ) * .6f ) + + y ;
else if ( player . GetPosition ( ) . z < ( WORLD_SIZE_Y * CHUNK_SIZE_Z ) * .4f ) - - y ;
2021-12-03 11:49:59 -05:00
if ( ! x & & ! y ) return ;
if ( x > 0 ) {
for ( int ax = 0 ; ax < WORLD_SIZE_X ; + + ax )
for ( int ay = 0 ; ay < WORLD_SIZE_Y ; + + ay )
2021-12-13 23:24:12 -05:00
if ( ax - x > = 0 ) {
2021-12-03 11:49:59 -05:00
m_chunks . Set ( ax - x , ay ,
m_chunks . Remove ( ax , ay ) ) ;
2021-12-15 21:00:06 -05:00
if ( ax = = WORLD_SIZE_X - 1 & & m_chunks . Get ( ax - x , ay ) )
2021-12-14 17:16:14 -05:00
m_chunks . Get ( ax - x , ay ) - > MakeDirty ( ) ;
2021-12-13 23:24:12 -05:00
}
2021-12-03 11:49:59 -05:00
else if ( m_chunks . Get ( ax , ay ) ) m_tbDeleted . emplace_back ( m_chunks . Remove ( ax , ay ) ) ;
}
else if ( x < 0 ) {
for ( int ax = WORLD_SIZE_X - 1 ; ax > = 0 ; - - ax )
for ( int ay = WORLD_SIZE_Y - 1 ; ay > = 0 ; - - ay )
2021-12-13 23:24:12 -05:00
if ( ax - x < WORLD_SIZE_X ) {
2021-12-03 11:49:59 -05:00
m_chunks . Set ( ax - x , ay ,
m_chunks . Remove ( ax , ay ) ) ;
2021-12-15 21:00:06 -05:00
if ( ax = = 0 & & m_chunks . Get ( ax - x , ay ) )
2021-12-14 17:16:14 -05:00
m_chunks . Get ( ax - x , ay ) - > MakeDirty ( ) ;
2021-12-13 23:24:12 -05:00
}
2021-12-03 11:49:59 -05:00
else if ( m_chunks . Get ( ax , ay ) ) m_tbDeleted . emplace_back ( m_chunks . Remove ( ax , ay ) ) ;
}
2021-12-01 22:06:47 -05:00
2021-12-03 11:49:59 -05:00
if ( y > 0 ) {
for ( int ax = 0 ; ax < WORLD_SIZE_X ; + + ax )
for ( int ay = 0 ; ay < WORLD_SIZE_Y ; + + ay )
2021-12-13 23:24:12 -05:00
if ( ay - y > = 0 ) {
2021-12-03 11:49:59 -05:00
m_chunks . Set ( ax , ay - y ,
m_chunks . Remove ( ax , ay ) ) ;
2021-12-15 21:00:06 -05:00
if ( ay = = WORLD_SIZE_Y - 1 & & m_chunks . Get ( ax , ay - y ) )
2021-12-14 17:16:14 -05:00
m_chunks . Get ( ax , ay - y ) - > MakeDirty ( ) ;
2021-12-13 23:24:12 -05:00
}
2021-12-03 11:49:59 -05:00
else if ( m_chunks . Get ( ax , ay ) ) m_tbDeleted . emplace_back ( m_chunks . Remove ( ax , ay ) ) ;
}
else if ( y < 0 ) {
for ( int ax = WORLD_SIZE_X - 1 ; ax > = 0 ; - - ax )
for ( int ay = WORLD_SIZE_Y - 1 ; ay > = 0 ; - - ay )
2021-12-13 23:24:12 -05:00
if ( ay - y < WORLD_SIZE_Y ) {
2021-12-03 11:49:59 -05:00
m_chunks . Set ( ax , ay - y ,
m_chunks . Remove ( ax , ay ) ) ;
2021-12-15 21:00:06 -05:00
if ( ay = = 0 & & m_chunks . Get ( ax , ay - y ) )
2021-12-14 17:16:14 -05:00
m_chunks . Get ( ax , ay - y ) - > MakeDirty ( ) ;
2021-12-13 23:24:12 -05:00
}
2021-12-03 11:49:59 -05:00
else if ( m_chunks . Get ( ax , ay ) ) m_tbDeleted . emplace_back ( m_chunks . Remove ( ax , ay ) ) ;
}
2021-12-01 22:06:47 -05:00
2021-12-03 11:49:59 -05:00
m_center [ 0 ] + = x ; m_center [ 1 ] + = y ;
player . Teleport ( x , y ) ;
2021-12-07 18:59:50 -05:00
for ( int index = 0 ; index < MAX_BULLETS ; + + index )
if ( bullets [ index ] ) bullets [ index ] - > Transpose ( x , y ) ;
2021-12-03 11:49:59 -05:00
}
2021-12-01 22:06:47 -05:00
2021-12-03 11:49:59 -05:00
void World : : CleanUpWorld ( int & deleteframes , bool clear = false ) {
if ( clear ) {
while ( m_tbDeleted . size ( ) > 0 ) {
delete m_tbDeleted . back ( ) ;
m_tbDeleted . pop_back ( ) ;
}
}
if ( ! m_tbDeleted . empty ( ) & & ! deleteframes ) {
2021-12-15 21:00:06 -05:00
int deleted = 0 ;
while ( deleted < THREADS_DELETE_CHUNKS ) {
}
2021-12-03 11:49:59 -05:00
delete m_tbDeleted . back ( ) ;
m_tbDeleted . pop_back ( ) ;
deleteframes = FRAMES_DELETE_CHUNKS ;
}
2021-12-01 22:06:47 -05:00
}
2021-12-02 18:12:35 -05:00
2021-12-18 15:24:57 -05:00
void World : : GetScope ( unsigned int & x , unsigned int & y ) {
2021-12-02 18:12:35 -05:00
x = m_center [ 0 ] ;
y = m_center [ 1 ] ;
}
2022-04-02 15:26:55 -04:00
void World : : Update ( int & rendercount , Bullet * bullets [ MAX_BULLETS ] , Player & player , Transformation & world , Shader & shader , TextureAtlas & atlas , BlockInfo * blockinfo [ BTYPE_LAST ] ) {
2021-12-06 11:08:34 -05:00
glStencilFunc ( GL_EQUAL , 1 , 0x00 ) ;
glStencilOp ( GL_KEEP , GL_KEEP , GL_REPLACE ) ;
2021-12-02 18:12:35 -05:00
atlas . Bind ( ) ;
2021-12-07 18:59:50 -05:00
RenderWorld ( rendercount , player , world , shader ) ;
2022-04-02 15:26:55 -04:00
UpdateWorld ( player , blockinfo ) ;
2023-08-28 17:39:55 -04:00
//TransposeWorld(player, bullets);
2021-12-02 18:12:35 -05:00
shader . Disable ( ) ;
2021-12-06 11:08:34 -05:00
glStencilFunc ( GL_GREATER , 1 , 0xFF ) ;
2021-12-02 18:12:35 -05:00
}
2021-12-18 15:24:57 -05:00
void World : : UpdateChunk ( int & updates , unsigned int chx , unsigned int chy , BlockInfo * blockinfo [ BTYPE_LAST ] ) {
2021-12-02 18:12:35 -05:00
if ( updates = = 0 & & ChunkAt ( chx , 1 , chy ) & &
ChunkAt ( chx , 1 , chy ) - > IsDirty ( ) ) {
ChunkAt ( chx , 1 , chy ) - > Update ( blockinfo , this ) ;
updates = FRAMES_UPDATE_CHUNKS ;
}
2021-12-14 17:16:14 -05:00
2021-12-02 18:12:35 -05:00
}
void World : : ChangeBlockAtCursor ( BlockType blockType , Player & player , bool & block ) {
Vector3f currentPos = player . GetPosition ( ) ;
Vector3f currentBlock = currentPos ;
Vector3f ray = player . GetDirection ( ) ;
bool found = false ;
if ( block ) return ;
while ( ( currentPos - currentBlock ) . Length ( ) < = MAX_SELECTION_DISTANCE & & ! found ) {
currentBlock + = ray / 10.f ;
BlockType bt = BlockAt ( currentBlock ) ;
if ( bt ! = BTYPE_AIR )
found = true ;
}
if ( found )
if ( blockType ! = BTYPE_AIR ) {
found = false ;
while ( ( currentPos - currentBlock ) . Length ( ) > = 1.7f & & ! found ) {
currentBlock - = ray / 10.f ;
BlockType bt = BlockAt ( currentBlock ) ;
if ( bt = = BTYPE_AIR ) { // V<> rification pour <20> tre s<> r que le bloc <20> changer n'est pas dans le joueur.
int Bx = ( int ) currentBlock . x ;
int By = ( int ) currentBlock . y ;
int Bz = ( int ) currentBlock . z ;
int Px = ( int ) currentPos . x ;
int PyA = ( int ) currentPos . y ;
int PyB = ( int ) ( currentPos . y - .9f ) ;
int PyC = ( int ) ( currentPos . y - 1.7f ) ;
int Pz = ( int ) currentPos . z ;
if ( ! ( Bx = = Px & &
( By = = PyA | |
By = = PyB | |
By = = PyC ) & &
Bz = = Pz ) )
found = true ;
}
}
}
if ( found & & ( int ) currentBlock . y < CHUNK_SIZE_Y ) {
int bx = ( int ) currentBlock . x % CHUNK_SIZE_X ;
int by = ( int ) currentBlock . y % CHUNK_SIZE_Y ;
int bz = ( int ) currentBlock . z % CHUNK_SIZE_Z ;
ChunkAt ( currentBlock ) - > SetBlock ( bx , by , bz , blockType , this ) ;
ChunkAt ( currentBlock ) - > MakeModified ( ) ;
block = true ;
}
}
2021-12-07 18:59:50 -05:00
void World : : ChangeBlockAtPosition ( BlockType blockType , Vector3f pos ) {
2021-12-14 17:16:14 -05:00
int bx = ( int ) pos . x % CHUNK_SIZE_X ;
int by = ( int ) pos . y % CHUNK_SIZE_Y ;
int bz = ( int ) pos . z % CHUNK_SIZE_Z ;
2021-12-07 18:59:50 -05:00
2021-12-14 17:16:14 -05:00
ChunkAt ( pos ) - > SetBlock ( bx , by , bz , blockType , this ) ;
ChunkAt ( pos ) - > MakeModified ( ) ;
2021-12-07 18:59:50 -05:00
}
void World : : RenderWorld ( int & rendercount , Player & player , Transformation & world , Shader & shader ) {
2021-12-02 18:12:35 -05:00
shader . Use ( ) ;
rendercount = 0 ;
Vector3f angle ;
Vector3f cursor ;
Vector3f direct = player . GetDirection ( ) ;
Vector3f pos = player . GetPosition ( ) - direct ;
direct . y = 0 ;
direct . Normalize ( ) ;
pos . y = 1 ;
2021-12-03 11:49:59 -05:00
2023-09-18 15:56:17 -04:00
static Vector3 < unsigned int > renderManifest [ VIEW_DISTANCE * 8 ] ; // Nombre de Chunks maximal <20> <20> tre rendus.
2021-12-02 18:12:35 -05:00
2021-12-06 09:45:51 -05:00
//for (int dist = VIEW_DISTANCE; dist >= 0; dist -= CHUNK_SIZE_X) {
for ( int dist = 0 ; dist < = VIEW_DISTANCE ; dist + = CHUNK_SIZE_X ) {
2021-12-02 18:12:35 -05:00
// Configuration du radar.
2021-12-03 11:49:59 -05:00
float sinus , cosinus ;
int echantillons ;
2023-09-18 15:56:17 -04:00
if ( dist > VIEW_DISTANCE * .1f ) {
2021-12-06 11:53:18 -05:00
sinus = .00872653549f ; // sin(1/2 degr<67> )
cosinus = .99996192306 ; // cos(1/2 degr<67> )
2021-12-06 12:21:05 -05:00
echantillons = 180 ;
2021-12-06 11:53:18 -05:00
}
2023-09-18 15:56:17 -04:00
//else if (dist > VIEW_DISTANCE * .3f) {
// sinus = .01151891831f; // sin(2/3 degr<67> )
// cosinus = .99993365506; // cos(2/3 degr<67> )
// echantillons = 120;
//}
//else if (dist > VIEW_DISTANCE * .2f) {
// sinus = .01745240643; // sin(1 degr<67> )
// cosinus = .99984769515; // cos(1 degr<67> )
// echantillons = 90;
//}
//else if (dist > VIEW_DISTANCE * .1f) {
// sinus = .0261769483;
// cosinus = .99965732497;
// echantillons = 60;
//}
2021-12-06 09:45:51 -05:00
else {
2021-12-03 11:49:59 -05:00
sinus = .0348994967 ;
cosinus = .99939082701 ;
echantillons = 45 ;
}
2021-12-03 11:53:53 -05:00
angle . x = direct . z + direct . x ;
angle . z = direct . z - direct . x ;
2021-12-02 18:12:35 -05:00
angle . y = 0 ;
angle . Normalize ( ) ;
for ( int radar = 0 ; radar < echantillons ; + + radar ) {
float x = angle . x ;
2021-12-03 11:51:59 -05:00
angle . x = angle . x * cosinus - angle . z * sinus ;
angle . z = angle . z * cosinus + x * sinus ;
2021-12-02 18:12:35 -05:00
angle . Normalize ( ) ;
cursor = pos - direct * CHUNK_SIZE_X * 2 + angle * dist ;
2021-12-04 09:55:43 -05:00
if ( cursor . y > = 128.f | | cursor . y > = 0.f ) cursor . y = CHUNK_SIZE_Y / 2.f ;
2021-12-02 18:12:35 -05:00
if ( ChunkAt ( cursor ) ) {
2021-12-18 15:24:57 -05:00
bool valide = true ;
unsigned int chx , chy ;
2021-12-02 18:12:35 -05:00
ChunkAt ( cursor ) - > GetPosition ( chx , chy ) ;
2021-12-03 11:53:05 -05:00
for ( int index = 0 ; index < rendercount ; + + index ) // Permet de v<> rifier seulement contre celles ajout<75> es dans la frame, et ne pas avoir <20> refaire l'array <20> chaque frame.
2021-12-14 17:16:14 -05:00
if ( renderManifest [ index ] . x = = chx & & renderManifest [ index ] . z = = chy )
2021-12-02 18:12:35 -05:00
valide = false ;
2021-12-18 15:24:57 -05:00
if ( valide ) renderManifest [ rendercount + + ] = Vector3 < unsigned int > ( chx ,
2021-12-18 15:34:47 -05:00
( VIEW_DISTANCE - ( pos - cursor ) . Length ( ) * 3.f + 256.f ) < 0.f ? 0 :
( VIEW_DISTANCE - ( pos - cursor ) . Length ( ) * 3.f + 256.f ) * 1000 ,
2021-12-18 15:24:57 -05:00
chy ) ;
2021-12-02 18:12:35 -05:00
}
}
}
2021-12-04 09:55:43 -05:00
for ( int index = 0 ; index < rendercount ; + + index ) {
int chx = ( renderManifest [ index ] . x - m_center [ 0 ] ) * CHUNK_SIZE_X , chy = ( renderManifest [ index ] . z - m_center [ 1 ] ) * CHUNK_SIZE_Z ;
world . ApplyTranslation ( chx , 0 , chy ) ;
world . Use ( ) ;
2023-09-18 15:56:17 -04:00
float blcolor = renderManifest [ index ] . y / ( VIEW_DISTANCE / 50.f ) ;
2021-12-18 15:24:57 -05:00
glBlendColor ( blcolor , blcolor , blcolor , 1.f ) ;
2021-12-04 09:55:43 -05:00
ChunkAt ( chx , 1 , chy ) - > Render ( ) ;
world . ApplyTranslation ( - chx , 0 , - chy ) ;
}
2021-12-02 18:12:35 -05:00
shader . Disable ( ) ;
} ;
2022-04-02 15:26:55 -04:00
void World : : UpdateWorld ( Player & player , BlockInfo * blockinfo [ BTYPE_LAST ] ) {
2021-12-02 18:12:35 -05:00
int cx = player . GetPosition ( ) . x ;
int cy = player . GetPosition ( ) . z ;
2022-04-06 23:07:24 -04:00
static int frameGenerate = 1 ;
static int frameUpdate = 2 ;
static int frameDelete = 3 ;
2021-12-02 18:12:35 -05:00
int side = 0 ;
2021-12-12 23:31:38 -05:00
int threads = 0 ;
std : : future < Chunk * > genThList [ THREADS_GENERATE_CHUNKS ] ;
2021-12-13 23:24:12 -05:00
std : : future < Chunk * > updateThList [ THREADS_UPDATE_CHUNKS ] ;
2021-12-15 21:00:06 -05:00
std : : future < void > delThList [ THREADS_DELETE_CHUNKS ] ;
2021-12-02 18:12:35 -05:00
if ( frameGenerate > 0 ) - - frameGenerate ;
if ( frameUpdate > 0 ) - - frameUpdate ;
if ( frameDelete > 0 ) - - frameDelete ;
2022-04-06 23:07:24 -04:00
2021-12-12 23:31:38 -05:00
if ( ! frameGenerate )
2021-12-13 23:24:12 -05:00
while ( side * CHUNK_SIZE_X < = VIEW_DISTANCE * 2 + CHUNK_SIZE_X ) {
2021-12-02 18:12:35 -05:00
int tx = - side , ty = - side ;
2021-12-12 23:31:38 -05:00
int chx = 0 ;
int chy = 0 ;
for ( ; tx < = side ; + + tx ) {
if ( frameGenerate )
break ;
chx = cx + tx * CHUNK_SIZE_X ;
chy = cy + ty * CHUNK_SIZE_Z ;
if ( chx < WORLD_SIZE_X * CHUNK_SIZE_X & & chy < WORLD_SIZE_Y * CHUNK_SIZE_Z & &
chx > = 0 & & chy > = 0 & & ! ChunkAt ( chx , 1 , chy ) )
2021-12-18 15:24:57 -05:00
genThList [ threads + + ] = std : : async ( std : : launch : : async , [ ] ( unsigned int x , unsigned int y ) { return new Chunk ( x , y ) ; } , chx / CHUNK_SIZE_X + m_center [ 0 ] , chy / CHUNK_SIZE_Z + m_center [ 1 ] ) ;
2021-12-12 23:31:38 -05:00
if ( threads = = THREADS_GENERATE_CHUNKS ) frameGenerate = FRAMES_RENDER_CHUNKS ;
}
for ( ; ty < = side ; + + ty ) {
if ( frameGenerate )
break ;
chx = cx + tx * CHUNK_SIZE_X ;
chy = cy + ty * CHUNK_SIZE_Z ;
if ( chx < WORLD_SIZE_X * CHUNK_SIZE_X & & chy < WORLD_SIZE_Y * CHUNK_SIZE_Z & &
chx > = 0 & & chy > = 0 & & ! ChunkAt ( chx , 1 , chy ) )
2021-12-18 15:24:57 -05:00
genThList [ threads + + ] = std : : async ( std : : launch : : async , [ ] ( unsigned int x , unsigned int y ) { return new Chunk ( x , y ) ; } , chx / CHUNK_SIZE_X + m_center [ 0 ] , chy / CHUNK_SIZE_Z + m_center [ 1 ] ) ;
2021-12-12 23:31:38 -05:00
if ( threads = = THREADS_GENERATE_CHUNKS ) frameGenerate = FRAMES_RENDER_CHUNKS ;
}
for ( ; tx > = - side ; - - tx ) {
if ( frameGenerate )
break ;
chx = cx + tx * CHUNK_SIZE_X ;
chy = cy + ty * CHUNK_SIZE_Z ;
if ( chx < WORLD_SIZE_X * CHUNK_SIZE_X & & chy < WORLD_SIZE_Y * CHUNK_SIZE_Z & &
chx > = 0 & & chy > = 0 & & ! ChunkAt ( chx , 1 , chy ) )
2021-12-18 15:24:57 -05:00
genThList [ threads + + ] = std : : async ( std : : launch : : async , [ ] ( unsigned int x , unsigned int y ) { return new Chunk ( x , y ) ; } , chx / CHUNK_SIZE_X + m_center [ 0 ] , chy / CHUNK_SIZE_Z + m_center [ 1 ] ) ;
2021-12-12 23:31:38 -05:00
if ( threads = = THREADS_GENERATE_CHUNKS ) frameGenerate = FRAMES_RENDER_CHUNKS ;
}
for ( ; ty > = - side ; - - ty ) {
if ( frameGenerate )
break ;
chx = cx + tx * CHUNK_SIZE_X ;
chy = cy + ty * CHUNK_SIZE_Z ;
if ( chx < WORLD_SIZE_X * CHUNK_SIZE_X & & chy < WORLD_SIZE_Y * CHUNK_SIZE_Z & &
chx > = 0 & & chy > = 0 & & ! ChunkAt ( chx , 1 , chy ) )
2021-12-18 15:24:57 -05:00
genThList [ threads + + ] = std : : async ( std : : launch : : async , [ ] ( unsigned int x , unsigned int y ) { return new Chunk ( x , y ) ; } , chx / CHUNK_SIZE_X + m_center [ 0 ] , chy / CHUNK_SIZE_Z + m_center [ 1 ] ) ;
2021-12-12 23:31:38 -05:00
if ( threads = = THREADS_GENERATE_CHUNKS ) frameGenerate = FRAMES_RENDER_CHUNKS ;
}
if ( frameGenerate )
break ;
+ + side ;
}
if ( threads > 0 ) {
for ( int i = 0 ; i < threads ; + + i )
genThList [ i ] . wait ( ) ;
2021-12-14 17:16:14 -05:00
2021-12-12 23:31:38 -05:00
for ( int i = 0 ; i < threads ; + + i ) {
2021-12-18 15:24:57 -05:00
unsigned int x , y ;
2021-12-12 23:31:38 -05:00
Chunk * chunk = genThList [ i ] . get ( ) ;
2021-12-14 17:16:14 -05:00
chunk - > GetPosition ( x , y ) ;
2021-12-12 23:31:38 -05:00
m_chunks . Set ( x - m_center [ 0 ] , y - m_center [ 1 ] , chunk ) ;
}
}
2021-12-02 18:12:35 -05:00
2021-12-12 23:31:38 -05:00
side = 0 ;
threads = 0 ;
if ( ! frameUpdate )
while ( side * CHUNK_SIZE_X < = VIEW_DISTANCE * 2 ) {
int tx = - side , ty = - side ;
2021-12-02 18:12:35 -05:00
2021-12-12 23:31:38 -05:00
for ( ; tx < = side ; + + tx ) {
if ( frameUpdate )
break ;
2021-12-18 15:24:57 -05:00
unsigned int chx = cx + tx * CHUNK_SIZE_X , chy = cy + ty * CHUNK_SIZE_Z ;
2021-12-14 17:16:14 -05:00
if ( ChunkAt ( chx , 1 , chy ) & &
2021-12-12 23:31:38 -05:00
ChunkAt ( chx , 1 , chy ) - > IsDirty ( ) ) {
2021-12-13 23:24:12 -05:00
updateThList [ threads + + ] =
std : : async ( std : : launch : : async ,
[ ] ( Chunk * chunk , BlockInfo * blockinfo [ BTYPE_LAST ] , World * world ) {
chunk - > Update ( blockinfo , world ) ; return chunk ; } , ChunkAt ( chx , 1 , chy ) , blockinfo , this ) ;
if ( threads = = THREADS_UPDATE_CHUNKS ) frameUpdate = FRAMES_UPDATE_CHUNKS ;
2021-12-12 23:31:38 -05:00
}
2021-12-14 17:16:14 -05:00
}
2021-12-12 23:31:38 -05:00
for ( ; ty < = side ; + + ty ) {
if ( frameUpdate )
break ;
2021-12-18 15:24:57 -05:00
unsigned int chx = cx + tx * CHUNK_SIZE_X , chy = cy + ty * CHUNK_SIZE_Z ;
2021-12-14 17:16:14 -05:00
if ( ChunkAt ( chx , 1 , chy ) & &
2021-12-12 23:31:38 -05:00
ChunkAt ( chx , 1 , chy ) - > IsDirty ( ) ) {
2021-12-13 23:24:12 -05:00
updateThList [ threads + + ] =
std : : async ( std : : launch : : async ,
[ ] ( Chunk * chunk , BlockInfo * blockinfo [ BTYPE_LAST ] , World * world ) {
chunk - > Update ( blockinfo , world ) ; return chunk ; } , ChunkAt ( chx , 1 , chy ) , blockinfo , this ) ;
if ( threads = = THREADS_UPDATE_CHUNKS ) frameUpdate = FRAMES_UPDATE_CHUNKS ;
2021-12-12 23:31:38 -05:00
}
2021-12-14 17:16:14 -05:00
}
2021-12-12 23:31:38 -05:00
for ( ; tx > = - side ; - - tx ) {
if ( frameUpdate )
break ;
2021-12-18 15:24:57 -05:00
unsigned int chx = cx + tx * CHUNK_SIZE_X , chy = cy + ty * CHUNK_SIZE_Z ;
2021-12-14 17:16:14 -05:00
if ( ChunkAt ( chx , 1 , chy ) & &
2021-12-12 23:31:38 -05:00
ChunkAt ( chx , 1 , chy ) - > IsDirty ( ) ) {
2021-12-13 23:24:12 -05:00
updateThList [ threads + + ] =
std : : async ( std : : launch : : async ,
[ ] ( Chunk * chunk , BlockInfo * blockinfo [ BTYPE_LAST ] , World * world ) {
chunk - > Update ( blockinfo , world ) ; return chunk ; } , ChunkAt ( chx , 1 , chy ) , blockinfo , this ) ;
if ( threads = = THREADS_UPDATE_CHUNKS ) frameUpdate = FRAMES_UPDATE_CHUNKS ;
2021-12-12 23:31:38 -05:00
}
2021-12-14 17:16:14 -05:00
}
2021-12-12 23:31:38 -05:00
for ( ; ty > = - side ; - - ty ) {
if ( frameUpdate )
break ;
2021-12-18 15:24:57 -05:00
unsigned int chx = cx + tx * CHUNK_SIZE_X , chy = cy + ty * CHUNK_SIZE_Z ;
2021-12-14 17:16:14 -05:00
if ( ChunkAt ( chx , 1 , chy ) & &
2021-12-12 23:31:38 -05:00
ChunkAt ( chx , 1 , chy ) - > IsDirty ( ) ) {
updateThList [ threads + + ] =
std : : async ( std : : launch : : async ,
[ ] ( Chunk * chunk , BlockInfo * blockinfo [ BTYPE_LAST ] , World * world ) {
chunk - > Update ( blockinfo , world ) ; return chunk ; } , ChunkAt ( chx , 1 , chy ) , blockinfo , this ) ;
if ( threads = = THREADS_UPDATE_CHUNKS ) frameUpdate = FRAMES_UPDATE_CHUNKS ;
2021-12-13 23:24:12 -05:00
}
2021-12-14 17:16:14 -05:00
}
2021-12-12 23:31:38 -05:00
if ( frameUpdate )
break ;
2021-12-02 18:12:35 -05:00
+ + side ;
}
2021-12-13 23:24:12 -05:00
if ( threads > 0 ) {
2021-12-12 23:31:38 -05:00
for ( int i = 0 ; i < threads ; + + i ) {
2021-12-13 23:24:12 -05:00
updateThList [ i ] . wait ( ) ;
2021-12-12 23:31:38 -05:00
Chunk * chunk = updateThList [ i ] . get ( ) ;
2021-12-13 23:24:12 -05:00
chunk - > FlushMeshToVBO ( ) ;
2021-12-12 23:31:38 -05:00
}
2021-12-13 23:24:12 -05:00
}
2021-12-12 23:31:38 -05:00
2021-12-15 21:00:06 -05:00
threads = 0 ;
2023-08-28 16:23:34 -04:00
int del = THREADS_DELETE_CHUNKS ;
while ( ! m_tbDeleted . empty ( ) & & del - - ) { // Moins rapide que le bout en dessous, mais -beaucoup- plus stable.
m_tbDeleted . back ( ) - > FlushVBO ( ) ;
m_tbDeleted . back ( ) - > ~ Chunk ( ) ;
m_tbDeleted . pop_back ( ) ;
}
/*while (!m_tbDeleted.empty() && !frameDelete) {
2021-12-15 21:00:06 -05:00
if ( m_tbDeleted . back ( ) ) {
m_tbDeleted . back ( ) - > FlushVBO ( ) ;
delThList [ threads ] =
std : : async ( std : : launch : : async ,
[ ] ( Chunk * chunk ) { delete chunk ; } , m_tbDeleted . back ( ) ) ;
m_tbDeleted . pop_back ( ) ;
if ( + + threads > THREADS_DELETE_CHUNKS ) frameDelete = FRAMES_DELETE_CHUNKS ;
}
else m_tbDeleted . pop_back ( ) ;
2023-08-28 16:23:34 -04:00
} */
2021-12-15 21:00:06 -05:00
2022-04-02 15:26:55 -04:00
for ( int x = 0 ; x < threads ; + + x ) {
2021-12-15 21:00:06 -05:00
delThList [ x ] . wait ( ) ;
2022-04-02 15:26:55 -04:00
delThList [ x ] . get ( ) ;
}
2021-12-15 21:00:06 -05:00
}
int World : : GettbDeleted ( ) const { return m_tbDeleted . size ( ) ; }