]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/terrain.h
c6aea4e6487ea03d9f673e5c0d7434cf99055401
[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 (s32 x = 0; x < CHUNK_SIZE; x++) \
13         for (s32 y = 0; y < CHUNK_SIZE; y++) \
14         for (s32 z = 0; z < CHUNK_SIZE; z++)
15
16 #define CHUNK_MODE_PASSIVE 0
17 #define CHUNK_MODE_CREATE 1
18
19 typedef struct TerrainNode {
20         NodeType type;
21         void *data;
22 } TerrainNode;
23
24 typedef TerrainNode TerrainChunkData[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE];
25
26 typedef struct {
27         s32 level;
28         v3s32 pos;
29         TerrainChunkData data;
30         void *extra;
31         pthread_rwlock_t lock;
32 } TerrainChunk;
33
34 typedef struct {
35         Tree sectors;
36         pthread_rwlock_t lock;
37         TerrainChunk *cache;
38         pthread_rwlock_t cache_lock;
39         struct {
40                 void (*create_chunk)(TerrainChunk *chunk);
41                 void (*delete_chunk)(TerrainChunk *chunk);
42                 bool (*get_chunk)(TerrainChunk *chunk, int mode);
43                 void (*delete_node)(TerrainNode *node);
44         } callbacks;
45 } Terrain;
46
47 Terrain *terrain_create();
48 void terrain_delete(Terrain *terrain);
49
50 TerrainChunk *terrain_get_chunk(Terrain *terrain, v3s32 pos, int mode);
51 TerrainChunk *terrain_get_chunk_nodep(Terrain *terrain, v3s32 node_pos, v3s32 *offset, int mode);
52
53 Blob terrain_serialize_chunk(Terrain *terrain, TerrainChunk *chunk, void (*callback)(TerrainNode *node, Blob *buffer));
54 bool terrain_deserialize_chunk(Terrain *terrain, TerrainChunk *chunk, Blob buffer, void (*callback)(TerrainNode *node, Blob buffer));
55
56 TerrainNode terrain_get_node(Terrain *terrain, v3s32 pos);
57
58 v3s32 terrain_chunkp(v3s32 pos);
59 v3s32 terrain_offset(v3s32 pos);
60
61 #endif