]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/server/voxelctx.c
refactoring
[dragonblocks_alpha.git] / src / server / voxelctx.c
1 #include <stdlib.h>
2 #include <pthread.h>
3 #include "color.h"
4 #include "perlin.h"
5 #include "server/terrain_gen.h"
6 #include "server/voxelctx.h"
7
8 static VoxelctxState *create_state(VoxelctxState *old)
9 {
10         VoxelctxState *state = malloc(sizeof *state);
11
12         if (old) {
13                 *state = *old;
14         } else {
15                 state->pos[0] = 0.0f;
16                 state->pos[1] = 0.0f;
17                 state->pos[2] = 0.0f;
18                 state->pos[3] = 0.0f;
19                 state->scale[0] = 1.0f;
20                 state->scale[1] = 1.0f;
21                 state->scale[2] = 1.0f;
22                 mat4x4_identity(state->mat);
23                 state->h = 0.0f;
24                 state->s = 0.0f;
25                 state->l = 1.0f;
26                 state->life = 0;
27         }
28
29         return state;
30 }
31
32 Voxelctx *voxelctx_create(List *changed_chunks, TerrainGenStage tgs, v3s32 pos)
33 {
34         Voxelctx *ctx = malloc(sizeof(Voxelctx));
35
36         ctx->changed_chunks = changed_chunks;
37         ctx->tgs = tgs;
38         ctx->pos = pos;
39         list_ini(&ctx->statestack);
40         ctx->random = 0;
41
42         list_apd(&ctx->statestack, create_state(NULL));
43
44         return ctx;
45 }
46
47 void voxelctx_delete(Voxelctx *ctx)
48 {
49         list_clr(&ctx->statestack, (void *) &free, NULL, NULL);
50         free(ctx);
51 }
52
53 static void move_value(f32 *x, f32 v, f32 range)
54 {
55     f32 dst = v >= 0 ? range : 0;
56     v = fabs(v);
57     *x = f32_mix(*x, dst, v);
58 }
59
60 void voxelctx_hue(Voxelctx *ctx, f32 value)
61 {
62         VOXELCTXSTATE(ctx).h += value;
63 }
64
65 void voxelctx_sat(Voxelctx *ctx, f32 value)
66 {
67         move_value(&VOXELCTXSTATE(ctx).s, value, 1.0f);
68 }
69
70 void voxelctx_light(Voxelctx *ctx, f32 value)
71 {
72         move_value(&VOXELCTXSTATE(ctx).l, value, 1.0f);
73 }
74
75 void voxelctx_life(Voxelctx *ctx, s32 value)
76 {
77         VOXELCTXSTATE(ctx).life += value;
78 }
79
80 static void apply_translation(Voxelctx *ctx, v3f32 translate)
81 {
82         vec4 translate_vec;
83         mat4x4_mul_vec4(translate_vec, VOXELCTXSTATE(ctx).mat, (vec4) {translate.x, translate.y, translate.z, 0.0f});
84         vec4_add(VOXELCTXSTATE(ctx).pos, VOXELCTXSTATE(ctx).pos, translate_vec);
85 }
86
87 void voxelctx_x(Voxelctx *ctx, f32 value)
88 {
89         apply_translation(ctx, (v3f32) {value, 0.0f, 0.0f});
90 }
91
92 void voxelctx_y(Voxelctx *ctx, f32 value)
93 {
94         apply_translation(ctx, (v3f32) {0.0f, value, 0.0f});
95 }
96
97 void voxelctx_z(Voxelctx *ctx, f32 value)
98 {
99         apply_translation(ctx, (v3f32) {0.0f, 0.0f, value});
100 }
101
102 void voxelctx_rx(Voxelctx *ctx, f32 value)
103 {
104         mat4x4_rotate_X(VOXELCTXSTATE(ctx).mat, VOXELCTXSTATE(ctx).mat, value * M_PI / 180.0f);
105 }
106
107 void voxelctx_ry(Voxelctx *ctx, f32 value)
108 {
109         mat4x4_rotate_Y(VOXELCTXSTATE(ctx).mat, VOXELCTXSTATE(ctx).mat, value * M_PI / 180.0f);
110 }
111
112 void voxelctx_rz(Voxelctx *ctx, f32 value)
113 {
114         mat4x4_rotate_Z(VOXELCTXSTATE(ctx).mat, VOXELCTXSTATE(ctx).mat, value * M_PI / 180.0f);
115 }
116
117 static void apply_scale(Voxelctx *ctx, v3f32 scale)
118 {
119         VOXELCTXSTATE(ctx).scale[0] *= scale.x;
120         VOXELCTXSTATE(ctx).scale[1] *= scale.y;
121         VOXELCTXSTATE(ctx).scale[2] *= scale.z;
122
123         mat4x4_scale_aniso(VOXELCTXSTATE(ctx).mat, VOXELCTXSTATE(ctx).mat, scale.x, scale.y, scale.z);
124 }
125
126 void voxelctx_sx(Voxelctx *ctx, f32 value)
127 {
128         apply_scale(ctx, (v3f32) {value, 1.0f, 1.0f});
129 }
130
131 void voxelctx_sy(Voxelctx *ctx, f32 value)
132 {
133         apply_scale(ctx, (v3f32) {1.0f, value, 1.0f});
134 }
135
136 void voxelctx_sz(Voxelctx *ctx, f32 value)
137 {
138         apply_scale(ctx, (v3f32) {1.0f, 1.0f, value});
139 }
140
141 void voxelctx_s(Voxelctx *ctx, f32 value)
142 {
143         apply_scale(ctx, (v3f32) {value, value, value});
144 }
145
146 void voxelctx_pop(Voxelctx *ctx)
147 {
148         free(ctx->statestack.fst->dat);
149         list_nrm(&ctx->statestack, &ctx->statestack.fst);
150 }
151
152 void voxelctx_push(Voxelctx *ctx)
153 {
154         list_ppd(&ctx->statestack, create_state(&VOXELCTXSTATE(ctx)));
155 }
156
157 bool voxelctx_is_alive(Voxelctx *ctx)
158 {
159         if (VOXELCTXSTATE(ctx).life > 0) {
160                 VOXELCTXSTATE(ctx).life--;
161                 if (VOXELCTXSTATE(ctx).life <= 0)
162                         return false;
163         }
164
165         return VOXELCTXSTATE(ctx).scale[0] >= 1.0f && VOXELCTXSTATE(ctx).scale[1] >= 1.0f && VOXELCTXSTATE(ctx).scale[2] >= 1.0f;
166 }
167
168 void voxelctx_cube(Voxelctx *ctx, NodeType node, bool use_color)
169 {
170         if (!voxelctx_is_alive(ctx))
171                 return;
172
173         vec4 base_corners[8] = {
174                 {0.0f, 0.0f, 0.0f, 0.0f},
175                 {0.0f, 0.0f, 1.0f, 0.0f},
176                 {0.0f, 1.0f, 0.0f, 0.0f},
177                 {0.0f, 1.0f, 1.0f, 0.0f},
178                 {1.0f, 0.0f, 0.0f, 0.0f},
179                 {1.0f, 0.0f, 1.0f, 0.0f},
180                 {1.0f, 1.0f, 0.0f, 0.0f},
181                 {1.0f, 1.0f, 1.0f, 0.0f},
182         };
183
184         vec4 corners[8];
185
186         s32 max_len = 0;
187
188         vec4 center;
189
190         mat4x4_mul_vec4(center, VOXELCTXSTATE(ctx).mat, (vec4) {0.5f, 0.5f, 0.5f});
191
192         for (int i = 0; i < 8; i++) {
193                 mat4x4_mul_vec4(corners[i], VOXELCTXSTATE(ctx).mat, base_corners[i]);
194
195                 vec3 from_center;
196                 vec3_sub(from_center, corners[i], center);
197
198                 s32 len = ceil(vec3_len(from_center));
199
200                 if (max_len < len)
201                         max_len = len;
202         }
203
204         for (s32 x = -max_len; x <= +max_len; x++)
205         for (s32 y = -max_len; y <= +max_len; y++)
206         for (s32 z = -max_len; z <= +max_len; z++) {
207                 s32 v[3];
208
209                 for (int i = 0; i < 3; i++) {
210                         f32 f = trunc(
211                                 + f32_mix(corners[0][i], corners[4][i], (f32) x / (f32) max_len / 2.0f)
212                                 + f32_mix(corners[0][i], corners[2][i], (f32) y / (f32) max_len / 2.0f)
213                                 + f32_mix(corners[0][i], corners[1][i], (f32) z / (f32) max_len / 2.0f));
214
215                         v[i] = floor(VOXELCTXSTATE(ctx).pos[i] + f + 0.5f);
216                 }
217
218                 Blob buffer = {0, NULL};
219
220                 if (use_color)
221                         ColorData_write(&buffer, &(ColorData) {hsl_to_rgb((v3f32) {
222                                 VOXELCTXSTATE(ctx).h / 360.0,
223                                 VOXELCTXSTATE(ctx).s,
224                                 VOXELCTXSTATE(ctx).l,
225                         })});
226
227                 server_terrain_gen_node(
228                         v3s32_add(ctx->pos, (v3s32) {v[0], v[2], v[1]}),
229                         terrain_node_create(node, buffer),
230                         ctx->tgs,
231                         ctx->changed_chunks
232                 );
233
234                 Blob_free(&buffer);
235         }
236 }
237
238
239 void voxelctx_cylinder(Voxelctx *ctx, NodeType node, bool use_color)
240 {
241         voxelctx_cube(ctx, node, use_color);
242 }
243
244 /*
245 void voxelctx_cylinder(Voxelctx *ctx, Node node, bool use_color)
246 {
247         if (!voxelctx_is_alive(ctx))
248                 return;
249
250         return;
251
252         f32 xf = VOXELCTXSTATE(ctx).scale[0] / 2.0f;
253         for (s32 x = round(-xf + 0.5f); x <= round(xf); x++) {
254                 f32 yf = cos(x / VOXELCTXSTATE(ctx).scale[0] * M_PI) * VOXELCTXSTATE(ctx).scale[1] / 2.0f;
255                 for (s32 y = round(-yf); y <= round(yf); y++) {
256                         f32 zf = VOXELCTXSTATE(ctx).scale[2] / 2.0f;
257                         for (s32 z = round(-zf + 0.5f); z <= round(zf); z++) {
258                                 mapgen_set_node((v3s32) {
259                                         ctx->pos.x + round(VOXELCTXSTATE(ctx).pos[0] + x),
260                                         ctx->pos.y + round(VOXELCTXSTATE(ctx).pos[2] + z),
261                                         ctx->pos.z + round(VOXELCTXSTATE(ctx).pos[1] + y),
262                                 }, CREATE_NODE, ctx->tgs, ctx->changed_chunks);
263                         }
264                 }
265         }
266 }
267 */
268
269 f32 voxelctx_random(Voxelctx *ctx, f32 base, f32 vary)
270 {
271         return base + noise3d(ctx->pos.x, ctx->pos.y, ctx->pos.z, ctx->random++, seed + SO_VOXELCTX) * vary;
272 }