]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/terrain.h
refactoring
[dragonblocks_alpha.git] / src / terrain.h
1 #ifndef _TERRAIN_H_
2 #define _TERRAIN_H_
3
4 #include <dragonstd/list.h>
5 #include <dragonstd/tree.h>
6 #include <stdbool.h>
7 #include <pthread.h>
8 #include "node.h"
9 #include "types.h"
10
11 #define CHUNK_ITERATE \
12         for (u8 x = 0; x < CHUNK_SIZE; x++) \
13         for (u8 y = 0; y < CHUNK_SIZE; y++) \
14         for (u8 z = 0; z < CHUNK_SIZE; z++)
15
16 typedef struct TerrainNode {
17         NodeType type;
18         void *data;
19 } TerrainNode;
20
21 typedef TerrainNode TerrainChunkData[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE];
22
23 typedef struct {
24         s32 level;
25         v3s32 pos;
26         TerrainChunkData data;
27         void *extra;
28         pthread_mutex_t mtx;
29 } TerrainChunk;
30
31 typedef struct
32 {
33         v2s32 pos;
34         Tree chunks;
35         pthread_rwlock_t lock;
36 } TerrainSector;
37
38 typedef struct {
39         Tree sectors;
40         pthread_rwlock_t lock;
41         TerrainChunk *cache;
42         pthread_rwlock_t cache_lock;
43         struct {
44                 void (*create_chunk)(TerrainChunk *chunk);
45                 void (*delete_chunk)(TerrainChunk *chunk);
46                 bool (*get_chunk)(TerrainChunk *chunk, bool create);
47                 bool (*set_node) (TerrainChunk *chunk, v3u8 offset, TerrainNode *node, void *arg);
48                 void (*after_set_node)(TerrainChunk *chunk, v3u8 offset, void *arg);
49         } callbacks;
50 } Terrain;
51
52 Terrain *terrain_create();
53 void terrain_delete(Terrain *terrain);
54
55 TerrainSector *terrain_get_sector(Terrain *terrain, v2s32 pos, bool create);
56 TerrainChunk *terrain_get_chunk(Terrain *terrain, v3s32 pos, bool create);
57
58 TerrainChunk *terrain_allocate_chunk(v3s32 pos);
59 void terrain_free_chunk(TerrainChunk *chunk);
60
61 Blob terrain_serialize_chunk(TerrainChunk *chunk);
62 bool terrain_deserialize_chunk(TerrainChunk *chunk, Blob buffer);
63
64 v3s32 terrain_node_to_chunk_pos(v3s32 pos, v3u8 *offset);
65
66 TerrainNode terrain_get_node(Terrain *terrain, v3s32 pos);
67 void terrain_set_node(Terrain *terrain, v3s32 pos, TerrainNode node, bool create, void *arg);
68
69 TerrainNode terrain_node_create(NodeType type, Blob buffer);
70 void terrain_node_delete(TerrainNode node);
71
72 #endif