]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/client_node.c
Add oceans and toggle keys for flight & collision
[dragonblocks_alpha.git] / src / client / client_node.c
1 #include "client/client.h"
2 #include "client/client_node.h"
3 #include "environment.h"
4 #include "node.h"
5 #include "perlin.h"
6 #include "util.h"
7 #define TILES_SIMPLE(path) {.paths = {path, NULL, NULL, NULL, NULL, NULL}, .indices = {0, 0, 0, 0, 0, 0}, .textures = {NULL}}
8 #define TILES_NONE {.paths = {NULL}, .indices = {0}, .textures = {NULL}}
9
10 static f64 clamp(f64 v, f64 min, f64 max)
11 {
12         return v < min ? min : v > max ? max : v;
13 }
14
15 static void render_grass(v3s32 pos, unused MapNode *node, Vertex3D *vertex, unused int f, unused int v)
16 {
17         f32 wet_min, wet_max, temp_max;
18         wet_min = 0.13f;
19         wet_max = 0.33f;
20         temp_max = 0.45f;
21
22         f32 temp_f = clamp(0.3f - get_temperature(pos), 0.0f, 0.3f) / 0.3f;
23
24         vertex->color.h = (get_wetness(pos) * (wet_max - wet_min) + wet_min) * (1.0f - temp_f) + temp_max * temp_f;
25         vertex->color.s = 1.0f;
26         vertex->color.v = 1.0f;
27 }
28
29 static void render_stone(v3s32 pos, unused MapNode *node, Vertex3D *vertex, unused int f, unused int v)
30 {
31         vertex->textureCoordinates.s += smooth2d(U32(pos.x), U32(pos.z), 0, seed + SO_TEXTURE_OFFSET_S);
32         vertex->textureCoordinates.t += smooth2d(U32(pos.x), U32(pos.z), 0, seed + SO_TEXTURE_OFFSET_T);
33 }
34
35 static void render_wood(unused v3s32 pos, unused MapNode *node, Vertex3D *vertex, int f, unused int v)
36 {
37         vertex->color.h = f < 4 ? 0.1f : 0.11f;
38         vertex->color.s = 1.0f;
39         vertex->color.v = 1.0f;
40 }
41
42 ClientNodeDefintion client_node_definitions[NODE_UNLOADED] = {
43         // invalid
44         {
45                 .tiles = TILES_SIMPLE(RESSOURCEPATH "textures/invalid.png"),
46                 .visibility = NV_SOLID,
47                 .render = NULL,
48         },
49         // air
50         {
51                 .tiles = TILES_NONE,
52                 .visibility = NV_NONE,
53                 .render = NULL,
54         },
55         // grass
56         {
57                 .tiles = TILES_SIMPLE(RESSOURCEPATH "textures/grass.png"),
58                 .visibility = NV_SOLID,
59                 .render = &render_grass,
60         },
61         // dirt
62         {
63                 .tiles = TILES_SIMPLE(RESSOURCEPATH "textures/dirt.png"),
64                 .visibility = NV_SOLID,
65                 .render = NULL,
66         },
67         // stone
68         {
69                 .tiles = TILES_SIMPLE(RESSOURCEPATH "textures/stone.png"),
70                 .visibility = NV_SOLID,
71                 .render = &render_stone,
72         },
73         // snow
74         {
75                 .tiles = TILES_SIMPLE(RESSOURCEPATH "textures/snow.png"),
76                 .visibility = NV_SOLID,
77                 .render = NULL,
78         },
79         // wood
80         {
81                 .tiles = {
82                         .paths = {RESSOURCEPATH "textures/wood.png", RESSOURCEPATH "textures/wood_top.png", NULL, NULL, NULL, NULL},
83                         .indices = {0, 0, 0, 0, 1, 1},
84                         .textures = {NULL},
85                 },
86                 .visibility = NV_SOLID,
87                 .render = &render_wood,
88         },
89         // sand
90         {
91                 .tiles = TILES_SIMPLE(RESSOURCEPATH "textures/sand.png"),
92                 .visibility = NV_SOLID,
93                 .render = NULL,
94         },
95         // water
96         {
97                 .tiles = TILES_SIMPLE(RESSOURCEPATH "textures/water.png"),
98                 .visibility = NV_TRANSPARENT,
99                 .render = NULL,
100         },
101 };
102
103 void client_node_init()
104 {
105         for (Node node = NODE_INVALID; node < NODE_UNLOADED; node++) {
106                 ClientNodeDefintion *def = &client_node_definitions[node];
107
108                 if (client_node_definitions[node].visibility != NV_NONE) {
109                         Texture *textures[6];
110
111                         for (int i = 0; i < 6; i++) {
112                                 char *path = def->tiles.paths[i];
113
114                                 if (path)
115                                         textures[i] = texture_get(path);
116                                 else
117                                         break;
118                         }
119
120                         for (int i = 0; i < 6; i++)
121                                 def->tiles.textures[i] = textures[def->tiles.indices[i]];
122                 }
123         }
124 }