1
- use crate :: map:: procgen:: heightmap;
2
- use crate :: map:: procgen:: heightmap:: tree_density;
1
+ use std:: ops:: Mul ;
2
+
3
+ use flat_spatial:: storage:: CellIdx ;
3
4
use flat_spatial:: Grid ;
4
- use geom:: { lerp, pack_height, vec2, Intersect , Radians , Ray3 , Vec2 , Vec3 , AABB } ;
5
- use prototypes:: { Tick , DELTA } ;
6
5
use rayon:: iter:: { IntoParallelIterator , ParallelIterator } ;
7
6
use serde:: { Deserialize , Serialize } ;
8
- use std:: collections:: HashSet ;
9
- use std:: ops:: Mul ;
7
+
8
+ use common:: FastSet ;
9
+ use egui_inspect:: egui:: ahash:: HashSetExt ;
10
+ use geom:: { lerp, pack_height, vec2, Intersect , Radians , Ray3 , Vec2 , Vec3 , AABB } ;
11
+ use prototypes:: { Tick , DELTA } ;
12
+
13
+ use crate :: map:: procgen:: heightmap;
14
+ use crate :: map:: procgen:: heightmap:: tree_density;
10
15
11
16
pub type TerrainChunkID = common:: ChunkID_512 ;
12
17
@@ -97,17 +102,16 @@ impl Environment {
97
102
}
98
103
} ) ;
99
104
100
- let mut seen = HashSet :: new ( ) ;
105
+ let mut seen = FastSet :: new ( ) ;
101
106
for h in to_remove {
102
- let Some ( tree) = self . trees . remove ( h) else {
107
+ let Some ( tree) = self . trees . remove_maintain ( h) else {
103
108
continue ;
104
109
} ;
105
110
let id = TerrainChunkID :: new ( tree. pos ) ;
106
111
if seen. insert ( id) {
107
112
f ( id) ;
108
113
}
109
114
}
110
- self . trees . maintain ( ) ;
111
115
}
112
116
113
117
pub fn get_chunk ( & self , id : TerrainChunkID ) -> Option < & Chunk > {
@@ -279,6 +283,8 @@ impl Environment {
279
283
280
284
let mut trees = Vec :: with_capacity ( 128 ) ;
281
285
286
+ let tree_storage = self . trees . storage ( ) ;
287
+
282
288
for offx in 0 ..RES_TREES {
283
289
for offy in 0 ..RES_TREES {
284
290
let cellpos = vec2 ( offx as f32 , offy as f32 ) * TCELLW ;
@@ -293,7 +299,11 @@ impl Environment {
293
299
let tdens = tree_density ( pchunk + sample) ;
294
300
295
301
if dens_test < tdens && chunk. height_unchecked ( sample) >= 0.0 {
296
- trees. push ( Tree :: new ( pchunk + sample) ) ;
302
+ let pos = pchunk + sample;
303
+ // normalize pos
304
+ let cell = tree_storage. cell_id ( pos) ;
305
+ let pos = decode_pos ( encode_pos ( pos, cell) , cell) ;
306
+ trees. push ( Tree :: new ( pos) ) ;
297
307
}
298
308
}
299
309
}
@@ -323,15 +333,15 @@ impl Tree {
323
333
324
334
type SmolTree = u16 ;
325
335
326
- pub fn new_smoltree ( pos : Vec2 , chunk : ( u32 , u32 ) ) -> SmolTree {
327
- let diffx = pos. x - ( chunk. 0 * TREE_GRID_SIZE as u32 ) as f32 ;
328
- let diffy = pos. y - ( chunk. 1 * TREE_GRID_SIZE as u32 ) as f32 ;
336
+ pub fn encode_pos ( pos : Vec2 , chunk : CellIdx ) -> SmolTree {
337
+ let diffx = pos. x - ( chunk. 0 * TREE_GRID_SIZE as i32 ) as f32 ;
338
+ let diffy = pos. y - ( chunk. 1 * TREE_GRID_SIZE as i32 ) as f32 ;
329
339
330
340
( ( ( ( diffx / TREE_GRID_SIZE as f32 ) * 256.0 ) as u8 as u16 ) << 8 )
331
341
+ ( ( diffy / TREE_GRID_SIZE as f32 ) * 256.0 ) as u8 as u16
332
342
}
333
343
334
- pub fn to_pos ( encoded : SmolTree , chunk : ( u32 , u32 ) ) -> Vec2 {
344
+ pub fn decode_pos ( encoded : SmolTree , chunk : CellIdx ) -> Vec2 {
335
345
let diffx = ( encoded >> 8 ) as u8 ;
336
346
let diffy = ( encoded & 0xFF ) as u8 ;
337
347
Vec2 {
@@ -343,7 +353,7 @@ pub fn to_pos(encoded: SmolTree, chunk: (u32, u32)) -> Vec2 {
343
353
#[ derive( Serialize , Deserialize ) ]
344
354
struct SerializedEnvironment {
345
355
h : Heightmap ,
346
- trees : Vec < ( ( u32 , u32 ) , Vec < SmolTree > ) > ,
356
+ trees : Vec < ( CellIdx , Vec < SmolTree > ) > ,
347
357
}
348
358
349
359
impl From < SerializedEnvironment > for Environment {
@@ -355,7 +365,7 @@ impl From<SerializedEnvironment> for Environment {
355
365
356
366
for ( chunk_id, trees) in ser. trees {
357
367
for tree in trees {
358
- let tree = Tree :: new ( to_pos ( tree, chunk_id) ) ;
368
+ let tree = Tree :: new ( decode_pos ( tree, chunk_id) ) ;
359
369
terrain. trees . insert ( tree. pos , tree) ;
360
370
}
361
371
}
@@ -370,11 +380,16 @@ impl From<&Environment> for SerializedEnvironment {
370
380
trees : Vec :: new ( ) ,
371
381
} ;
372
382
373
- for ( cell_id, chunk) in ter. trees . storage ( ) . cells . iter ( ) {
374
- let cell_id = ( cell_id. 0 as u32 , cell_id. 1 as u32 ) ;
383
+ let tree_cells = & ter. trees . storage ( ) . cells ;
384
+
385
+ let mut keys = tree_cells. keys ( ) . copied ( ) . collect :: < Vec < _ > > ( ) ;
386
+ keys. sort_unstable ( ) ;
387
+
388
+ for cell_id in keys {
389
+ let chunk = & tree_cells[ & cell_id] ;
375
390
let mut smoltrees = Vec :: with_capacity ( chunk. objs . len ( ) ) ;
376
391
for ( _, tree_pos) in chunk. objs . iter ( ) {
377
- let smol = new_smoltree ( * tree_pos, cell_id) ;
392
+ let smol = encode_pos ( * tree_pos, cell_id) ;
378
393
smoltrees. push ( smol) ;
379
394
}
380
395
t. trees . push ( ( cell_id, smoltrees) ) ;
0 commit comments