3 #include "server/mapgen.h"
4 #include "server/voxelctx.h"
8 static VoxelctxState *create_state(VoxelctxState *old)
10 VoxelctxState *state = malloc(sizeof(VoxelctxState));
19 state->scale[0] = 1.0f;
20 state->scale[1] = 1.0f;
21 state->scale[2] = 1.0f;
22 mat4x4_identity(state->mat);
32 Voxelctx *voxelctx_create(List *changed_blocks, MapgenStage mgs, v3s32 pos)
34 Voxelctx *ctx = malloc(sizeof(Voxelctx));
36 ctx->changed_blocks = changed_blocks;
39 ctx->statestack = list_create(NULL);
42 list_put(&ctx->statestack, create_state(NULL), NULL);
47 static void list_delete_state(void *key, unused void *value, unused void *arg)
52 void voxelctx_delete(Voxelctx *ctx)
54 list_clear_func(&ctx->statestack, &list_delete_state, NULL);
58 static void move_value(f32 *x, f32 v, f32 range)
60 f32 dst = v >= 0 ? range : 0;
62 *x = f32_mix(*x, dst, v);
65 void voxelctx_hue(Voxelctx *ctx, f32 value)
67 VOXELCTXSTATE(ctx).h += value;
70 void voxelctx_sat(Voxelctx *ctx, f32 value)
72 move_value(&VOXELCTXSTATE(ctx).s, value, 1.0f);
75 void voxelctx_light(Voxelctx *ctx, f32 value)
77 move_value(&VOXELCTXSTATE(ctx).l, value, 1.0f);
80 void voxelctx_life(Voxelctx *ctx, s32 value)
82 VOXELCTXSTATE(ctx).life += value;
85 static void apply_translation(Voxelctx *ctx, v3f32 translate)
88 mat4x4_mul_vec4(translate_vec, VOXELCTXSTATE(ctx).mat, (vec4) {translate.x, translate.y, translate.z, 0.0f});
89 vec4_add(VOXELCTXSTATE(ctx).pos, VOXELCTXSTATE(ctx).pos, translate_vec);
92 void voxelctx_x(Voxelctx *ctx, f32 value)
94 apply_translation(ctx, (v3f32) {value, 0.0f, 0.0f});
97 void voxelctx_y(Voxelctx *ctx, f32 value)
99 apply_translation(ctx, (v3f32) {0.0f, value, 0.0f});
102 void voxelctx_z(Voxelctx *ctx, f32 value)
104 apply_translation(ctx, (v3f32) {0.0f, 0.0f, value});
107 void voxelctx_rx(Voxelctx *ctx, f32 value)
109 mat4x4_rotate_X(VOXELCTXSTATE(ctx).mat, VOXELCTXSTATE(ctx).mat, value * M_PI / 180.0f);
112 void voxelctx_ry(Voxelctx *ctx, f32 value)
114 mat4x4_rotate_Y(VOXELCTXSTATE(ctx).mat, VOXELCTXSTATE(ctx).mat, value * M_PI / 180.0f);
117 void voxelctx_rz(Voxelctx *ctx, f32 value)
119 mat4x4_rotate_Z(VOXELCTXSTATE(ctx).mat, VOXELCTXSTATE(ctx).mat, value * M_PI / 180.0f);
122 static void apply_scale(Voxelctx *ctx, v3f32 scale)
124 VOXELCTXSTATE(ctx).scale[0] *= scale.x;
125 VOXELCTXSTATE(ctx).scale[1] *= scale.y;
126 VOXELCTXSTATE(ctx).scale[2] *= scale.z;
128 mat4x4_scale_aniso(VOXELCTXSTATE(ctx).mat, VOXELCTXSTATE(ctx).mat, scale.x, scale.y, scale.z);
131 void voxelctx_sx(Voxelctx *ctx, f32 value)
133 apply_scale(ctx, (v3f32) {value, 1.0f, 1.0f});
136 void voxelctx_sy(Voxelctx *ctx, f32 value)
138 apply_scale(ctx, (v3f32) {1.0f, value, 1.0f});
141 void voxelctx_sz(Voxelctx *ctx, f32 value)
143 apply_scale(ctx, (v3f32) {1.0f, 1.0f, value});
146 void voxelctx_s(Voxelctx *ctx, f32 value)
148 apply_scale(ctx, (v3f32) {value, value, value});
151 void voxelctx_pop(Voxelctx *ctx)
153 ListPair *next = ctx->statestack.first->next;
154 free(ctx->statestack.first->key);
155 free(ctx->statestack.first);
156 ctx->statestack.first = next;
159 void voxelctx_push(Voxelctx *ctx)
161 ListPair *pair = malloc(sizeof(ListPair));
162 pair->key = create_state(&VOXELCTXSTATE(ctx));
164 pair->next = ctx->statestack.first;
166 ctx->statestack.first = pair;
169 bool voxelctx_is_alive(Voxelctx *ctx)
171 if (VOXELCTXSTATE(ctx).life > 0) {
172 VOXELCTXSTATE(ctx).life--;
173 if (VOXELCTXSTATE(ctx).life <= 0)
177 return VOXELCTXSTATE(ctx).scale[0] >= 1.0f && VOXELCTXSTATE(ctx).scale[1] >= 1.0f && VOXELCTXSTATE(ctx).scale[2] >= 1.0f;
180 void voxelctx_cube(Voxelctx *ctx, Node node, bool use_color)
182 if (! voxelctx_is_alive(ctx))
185 vec4 base_corners[8] = {
186 {0.0f, 0.0f, 0.0f, 0.0f},
187 {0.0f, 0.0f, 1.0f, 0.0f},
188 {0.0f, 1.0f, 0.0f, 0.0f},
189 {0.0f, 1.0f, 1.0f, 0.0f},
190 {1.0f, 0.0f, 0.0f, 0.0f},
191 {1.0f, 0.0f, 1.0f, 0.0f},
192 {1.0f, 1.0f, 0.0f, 0.0f},
193 {1.0f, 1.0f, 1.0f, 0.0f},
202 mat4x4_mul_vec4(center, VOXELCTXSTATE(ctx).mat, (vec4) {0.5f, 0.5f, 0.5f});
204 for (int i = 0; i < 8; i++) {
205 mat4x4_mul_vec4(corners[i], VOXELCTXSTATE(ctx).mat, base_corners[i]);
208 vec3_sub(from_center, corners[i], center);
210 s32 len = ceil(vec3_len(from_center));
216 for (s32 x = -max_len; x <= +max_len; x++)
217 for (s32 y = -max_len; y <= +max_len; y++)
218 for (s32 z = -max_len; z <= +max_len; z++) {
221 for (int i = 0; i < 3; i++) {
223 + f32_mix(corners[0][i], corners[4][i], (f32) x / (f32) max_len / 2.0f)
224 + f32_mix(corners[0][i], corners[2][i], (f32) y / (f32) max_len / 2.0f)
225 + f32_mix(corners[0][i], corners[1][i], (f32) z / (f32) max_len / 2.0f));
227 v[i] = floor(VOXELCTXSTATE(ctx).pos[i] + f + 0.5f);
230 Blob buffer = {0, NULL};
233 HSLData_write(&buffer, &(HSLData) {{
234 VOXELCTXSTATE(ctx).h / 360.0,
235 VOXELCTXSTATE(ctx).s,
236 VOXELCTXSTATE(ctx).l,
240 v3s32_add(ctx->pos, (v3s32) {v[0], v[2], v[1]}),
241 map_node_create(node, buffer),
249 void voxelctx_cylinder(Voxelctx *ctx, Node node, bool use_color)
251 voxelctx_cube(ctx, node, use_color);
255 void voxelctx_cylinder(Voxelctx *ctx, Node node, bool use_color)
257 if (! voxelctx_is_alive(ctx))
262 f32 xf = VOXELCTXSTATE(ctx).scale[0] / 2.0f;
263 for (s32 x = round(-xf + 0.5f); x <= round(xf); x++) {
264 f32 yf = cos(x / VOXELCTXSTATE(ctx).scale[0] * M_PI) * VOXELCTXSTATE(ctx).scale[1] / 2.0f;
265 for (s32 y = round(-yf); y <= round(yf); y++) {
266 f32 zf = VOXELCTXSTATE(ctx).scale[2] / 2.0f;
267 for (s32 z = round(-zf + 0.5f); z <= round(zf); z++) {
268 mapgen_set_node((v3s32) {
269 ctx->pos.x + round(VOXELCTXSTATE(ctx).pos[0] + x),
270 ctx->pos.y + round(VOXELCTXSTATE(ctx).pos[2] + z),
271 ctx->pos.z + round(VOXELCTXSTATE(ctx).pos[1] + y),
272 }, CREATE_NODE, ctx->mgs, ctx->changed_blocks);
279 f32 voxelctx_random(Voxelctx *ctx, f32 base, f32 vary)
281 return base + noise3d(ctx->pos.x, ctx->pos.y, ctx->pos.z, ctx->random++, seed + SO_VOXELCTX) * vary;