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