]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/client_node.c
Trees break if they have no connection to ground
[dragonblocks_alpha.git] / src / client / client_node.c
1 #include <stdlib.h>
2 #include "client/client.h"
3 #include "client/client_node.h"
4 #include "color.h"
5 #include "environment.h"
6 #include "node.h"
7 #include "perlin.h"
8
9 #define TILES_SIMPLE(path) {.paths = {path, NULL, NULL, NULL, NULL, NULL}, .indices = {0, 0, 0, 0, 0, 0}, .textures = {NULL}}
10 #define TILES_NONE {.paths = {NULL}, .indices = {0}, .textures = {NULL}}
11
12 static void render_grass(NodeArgsRender *args)
13 {
14         args->vertex.color = hsl_to_rgb((v3f32) {f32_mix(
15                 // hue values between .13 and .33 depending on humidity
16                 f32_mix(
17                         0.13f,
18                         0.33f,
19                         get_humidity(args->pos)
20                 ),
21                 // move towards .45 while temperature is between .3 and .0
22                 0.45f,
23                 f32_clamp(
24                         0.3f - get_temperature(args->pos),
25                         0.0f,
26                         0.3f
27                 ) / 0.3f
28         ), 1.0f, 0.5f});
29 }
30
31 static void render_stone(NodeArgsRender *args)
32 {
33         args->vertex.cube.textureCoordinates.x += noise2d(args->pos.x, args->pos.z, 0, seed + OFFSET_TEXTURE_OFFSET_S);
34         args->vertex.cube.textureCoordinates.y += noise2d(args->pos.x, args->pos.z, 0, seed + OFFSET_TEXTURE_OFFSET_T);
35 }
36
37 static void render_color(NodeArgsRender *args)
38 {
39         args->vertex.color = ((ColorData *) args->node->data)->color;
40 }
41
42 ClientNodeDef client_node_def[COUNT_NODE] = {
43         // unknown
44         {
45                 .tiles = TILES_SIMPLE(RESSOURCE_PATH "textures/unknown.png"),
46                 .visibility = VISIBILITY_SOLID,
47                 .mipmap = true,
48                 .render = NULL,
49                 .pointable = true,
50                 .selection_color = {1.0f, 1.0f, 1.0f},
51                 .name = "Unknown",
52         },
53         // air
54         {
55                 .tiles = TILES_NONE,
56                 .visibility = VISIBILITY_NONE,
57                 .mipmap = true,
58                 .render = NULL,
59                 .pointable = false,
60                 .selection_color = {1.0f, 1.0f, 1.0f},
61                 .name = "Air",
62         },
63         // grass
64         {
65                 .tiles = TILES_SIMPLE(RESSOURCE_PATH "textures/grass.png"),
66                 .visibility = VISIBILITY_SOLID,
67                 .mipmap = true,
68                 .render = &render_grass,
69                 .pointable = true,
70                 .selection_color = {1.0f, 1.0f, 1.0f},
71                 .name = "Grass",
72         },
73         // dirt
74         {
75                 .tiles = TILES_SIMPLE(RESSOURCE_PATH "textures/dirt.png"),
76                 .visibility = VISIBILITY_SOLID,
77                 .mipmap = true,
78                 .render = NULL,
79                 .pointable = true,
80                 .selection_color = {1.0f, 1.0f, 1.0f},
81                 .name = "Dirt",
82         },
83         // stone
84         {
85                 .tiles = TILES_SIMPLE(RESSOURCE_PATH "textures/stone.png"),
86                 .visibility = VISIBILITY_SOLID,
87                 .mipmap = true,
88                 .render = &render_stone,
89                 .pointable = true,
90                 .selection_color = {1.0f, 1.0f, 1.0f},
91                 .name = "Stone",
92         },
93         // snow
94         {
95                 .tiles = TILES_SIMPLE(RESSOURCE_PATH "textures/snow.png"),
96                 .visibility = VISIBILITY_SOLID,
97                 .mipmap = true,
98                 .render = NULL,
99                 .pointable = true,
100                 .selection_color = {0.1f, 0.5f, 1.0f},
101                 .name = "Snow",
102         },
103         // oak wood
104         {
105                 .tiles = {
106                         .paths = {RESSOURCE_PATH "textures/oak_wood.png", RESSOURCE_PATH "textures/oak_wood_top.png", NULL, NULL, NULL, NULL},
107                         .indices = {0, 0, 0, 0, 1, 1},
108                         .textures = {NULL},
109                 },
110                 .visibility = VISIBILITY_SOLID,
111                 .mipmap = true,
112                 .render = &render_color,
113                 .pointable = true,
114                 .selection_color = {1.0f, 1.0f, 1.0f},
115                 .name = "Oak Wood",
116         },
117         // oak leaves
118         {
119                 .tiles = TILES_SIMPLE(RESSOURCE_PATH "textures/oak_leaves.png"),
120                 .visibility = VISIBILITY_SOLID,
121                 .mipmap = true,
122                 .render = &render_color,
123                 .pointable = true,
124                 .selection_color = {1.0f, 1.0f, 1.0f},
125                 .name = "Oak Leaves",
126         },
127         // pine wood
128         {
129                 .tiles = {
130                         .paths = {RESSOURCE_PATH "textures/pine_wood.png", RESSOURCE_PATH "textures/pine_wood_top.png", NULL, NULL, NULL, NULL},
131                         .indices = {0, 0, 0, 0, 1, 1},
132                         .textures = {NULL},
133                 },
134                 .visibility = VISIBILITY_SOLID,
135                 .mipmap = true,
136                 .render = &render_color,
137                 .pointable = true,
138                 .selection_color = {1.0f, 1.0f, 1.0f},
139                 .name = "Pine Wood",
140         },
141         // pine leaves
142         {
143                 .tiles = TILES_SIMPLE(RESSOURCE_PATH "textures/pine_leaves.png"),
144                 .visibility = VISIBILITY_CLIP,
145                 .mipmap = true,
146                 .render = &render_color,
147                 .pointable = true,
148                 .selection_color = {1.0f, 1.0f, 1.0f},
149                 .name = "Pine Leaves",
150         },
151         // palm wood
152         {
153                 .tiles = {
154                         .paths = {RESSOURCE_PATH "textures/palm_wood.png", RESSOURCE_PATH "textures/palm_wood_top.png", NULL, NULL, NULL, NULL},
155                         .indices = {0, 0, 0, 0, 1, 1},
156                         .textures = {NULL},
157                 },
158                 .visibility = VISIBILITY_SOLID,
159                 .mipmap = true,
160                 .render = &render_color,
161                 .pointable = true,
162                 .selection_color = {1.0f, 1.0f, 1.0f},
163                 .name = "Palm Wood",
164         },
165         // palm leaves
166         {
167                 .tiles = TILES_SIMPLE(RESSOURCE_PATH "textures/palm_leaves.png"),
168                 .visibility = VISIBILITY_SOLID,
169                 .mipmap = true,
170                 .render = &render_color,
171                 .pointable = true,
172                 .selection_color = {1.0f, 1.0f, 1.0f},
173                 .name = "Palm Leaves",
174         },
175         // sand
176         {
177                 .tiles = TILES_SIMPLE(RESSOURCE_PATH "textures/sand.png"),
178                 .visibility = VISIBILITY_SOLID,
179                 .mipmap = true,
180                 .render = NULL,
181                 .pointable = true,
182                 .selection_color = {1.0f, 1.0f, 1.0f},
183                 .name = "Sand",
184         },
185         // water
186         {
187                 .tiles = TILES_SIMPLE(RESSOURCE_PATH "textures/water.png"),
188                 .visibility = VISIBILITY_BLEND,
189                 .mipmap = true,
190                 .render = NULL,
191                 .pointable = false,
192                 .selection_color = {1.0f, 1.0f, 1.0f},
193                 .name = "Water",
194         },
195         // lava
196         {
197                 .tiles = TILES_SIMPLE(RESSOURCE_PATH "textures/lava.png"),
198                 .visibility = VISIBILITY_BLEND,
199                 .mipmap = true,
200                 .render = NULL,
201                 .pointable = false,
202                 .selection_color = {1.0f, 1.0f, 1.0f},
203                 .name = "Lava",
204         },
205         // vulcano_stone
206         {
207                 .tiles = TILES_SIMPLE(RESSOURCE_PATH "textures/vulcano_stone.png"),
208                 .visibility = VISIBILITY_SOLID,
209                 .mipmap = true,
210                 .render = NULL,
211                 .pointable = true,
212                 .selection_color = {1.0f, 1.0f, 1.0f},
213                 .name = "Vulcano Stone",
214         },
215 };
216
217 void client_node_init()
218 {
219         for (NodeType node = 0; node < COUNT_NODE; node++) {
220                 ClientNodeDef *def = &client_node_def[node];
221
222                 if (def->visibility != VISIBILITY_NONE) {
223                         Texture *textures[6];
224
225                         for (int i = 0; i < 6; i++) {
226                                 char *path = def->tiles.paths[i];
227
228                                 if (path)
229                                         textures[i] = texture_load(path, def->mipmap);
230                                 else
231                                         break;
232                         }
233
234                         for (int i = 0; i < 6; i++)
235                                 def->tiles.textures[i] = textures[def->tiles.indices[i]];
236                 }
237         }
238 }
239
240 void client_node_delete(TerrainNode *node)
241 {
242         switch (node->type) {
243                 NODES_TREE
244                         free(node->data);
245                         break;
246
247                 default:
248                         break;
249         }
250 }
251
252 void client_node_deserialize(TerrainNode *node, Blob buffer)
253 {
254         switch (node->type) {
255                 NODES_TREE
256                         ColorData_read(&buffer, node->data = malloc(sizeof(ColorData)));
257                         break;
258
259                 default:
260                         break;
261         }
262 }