4 #include "server/mapgen.h"
5 #include "server/voxelctx.h"
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)
11 static VoxelctxState *create_state(VoxelctxState *old)
13 VoxelctxState *state = malloc(sizeof(VoxelctxState));
22 state->scale[0] = 1.0f;
23 state->scale[1] = 1.0f;
24 state->scale[2] = 1.0f;
25 mat4x4_identity(state->mat);
35 Voxelctx *voxelctx_create(List *changed_blocks, MapgenStage mgs, v3s32 pos)
37 Voxelctx *ctx = malloc(sizeof(Voxelctx));
39 ctx->changed_blocks = changed_blocks;
42 ctx->statestack = list_create(NULL);
45 list_put(&ctx->statestack, create_state(NULL), NULL);
50 static void list_delete_state(void *key, unused void *value, unused void *arg)
55 void voxelctx_delete(Voxelctx *ctx)
57 list_clear_func(&ctx->statestack, &list_delete_state, NULL);
61 static inline f32 mix(f32 x, f32 y, f32 t)
63 return (1.0 - t) * x + t * y;
66 static void move_value(f32 *x, f32 v, f32 range)
68 f32 dst = v >= 0 ? range : 0;
73 void voxelctx_hue(Voxelctx *ctx, f32 value)
75 VOXELCTXSTATE(ctx).h += value;
78 void voxelctx_sat(Voxelctx *ctx, f32 value)
80 move_value(&VOXELCTXSTATE(ctx).s, value, 1.0f);
83 void voxelctx_light(Voxelctx *ctx, f32 value)
85 move_value(&VOXELCTXSTATE(ctx).l, value, 1.0f);
88 void voxelctx_life(Voxelctx *ctx, s32 value)
90 VOXELCTXSTATE(ctx).life += value;
93 static void apply_translation(Voxelctx *ctx, v3f32 translate)
96 mat4x4_mul_vec4(translate_vec, VOXELCTXSTATE(ctx).mat, (vec4) {translate.x, translate.y, translate.z, 0.0f});
97 vec4_add(VOXELCTXSTATE(ctx).pos, VOXELCTXSTATE(ctx).pos, translate_vec);
100 void voxelctx_x(Voxelctx *ctx, f32 value)
102 apply_translation(ctx, (v3f32) {value, 0.0f, 0.0f});
105 void voxelctx_y(Voxelctx *ctx, f32 value)
107 apply_translation(ctx, (v3f32) {0.0f, value, 0.0f});
110 void voxelctx_z(Voxelctx *ctx, f32 value)
112 apply_translation(ctx, (v3f32) {0.0f, 0.0f, value});
115 void voxelctx_rx(Voxelctx *ctx, f32 value)
117 mat4x4_rotate_X(VOXELCTXSTATE(ctx).mat, VOXELCTXSTATE(ctx).mat, value * M_PI / 180.0f);
120 void voxelctx_ry(Voxelctx *ctx, f32 value)
122 mat4x4_rotate_Y(VOXELCTXSTATE(ctx).mat, VOXELCTXSTATE(ctx).mat, value * M_PI / 180.0f);
125 void voxelctx_rz(Voxelctx *ctx, f32 value)
127 mat4x4_rotate_Z(VOXELCTXSTATE(ctx).mat, VOXELCTXSTATE(ctx).mat, value * M_PI / 180.0f);
130 static void apply_scale(Voxelctx *ctx, v3f32 scale)
132 VOXELCTXSTATE(ctx).scale[0] *= scale.x;
133 VOXELCTXSTATE(ctx).scale[1] *= scale.y;
134 VOXELCTXSTATE(ctx).scale[2] *= scale.z;
136 mat4x4_scale_aniso(VOXELCTXSTATE(ctx).mat, VOXELCTXSTATE(ctx).mat, scale.x, scale.y, scale.z);
139 void voxelctx_sx(Voxelctx *ctx, f32 value)
141 apply_scale(ctx, (v3f32) {value, 1.0f, 1.0f});
144 void voxelctx_sy(Voxelctx *ctx, f32 value)
146 apply_scale(ctx, (v3f32) {1.0f, value, 1.0f});
149 void voxelctx_sz(Voxelctx *ctx, f32 value)
151 apply_scale(ctx, (v3f32) {1.0f, 1.0f, value});
154 void voxelctx_s(Voxelctx *ctx, f32 value)
156 apply_scale(ctx, (v3f32) {value, value, value});
159 void voxelctx_pop(Voxelctx *ctx)
161 ListPair *next = ctx->statestack.first->next;
162 free(ctx->statestack.first->key);
163 free(ctx->statestack.first);
164 ctx->statestack.first = next;
167 void voxelctx_push(Voxelctx *ctx)
169 ListPair *pair = malloc(sizeof(ListPair));
170 pair->key = create_state(&VOXELCTXSTATE(ctx));
172 pair->next = ctx->statestack.first;
174 ctx->statestack.first = pair;
177 bool voxelctx_is_alive(Voxelctx *ctx)
179 if (VOXELCTXSTATE(ctx).life > 0) {
180 VOXELCTXSTATE(ctx).life--;
181 if (VOXELCTXSTATE(ctx).life <= 0)
185 return VOXELCTXSTATE(ctx).scale[0] >= 1.0f && VOXELCTXSTATE(ctx).scale[1] >= 1.0f && VOXELCTXSTATE(ctx).scale[2] >= 1.0f;
188 void voxelctx_cube(Voxelctx *ctx, Node node, bool use_color)
190 if (! voxelctx_is_alive(ctx))
193 vec4 base_corners[8] = {
194 {0.0f, 0.0f, 0.0f, 0.0f},
195 {0.0f, 0.0f, 1.0f, 0.0f},
196 {0.0f, 1.0f, 0.0f, 0.0f},
197 {0.0f, 1.0f, 1.0f, 0.0f},
198 {1.0f, 0.0f, 0.0f, 0.0f},
199 {1.0f, 0.0f, 1.0f, 0.0f},
200 {1.0f, 1.0f, 0.0f, 0.0f},
201 {1.0f, 1.0f, 1.0f, 0.0f},
210 mat4x4_mul_vec4(center, VOXELCTXSTATE(ctx).mat, (vec4) {0.5f, 0.5f, 0.5f});
212 for (int i = 0; i < 8; i++) {
213 mat4x4_mul_vec4(corners[i], VOXELCTXSTATE(ctx).mat, base_corners[i]);
216 vec3_sub(from_center, corners[i], center);
218 s32 len = ceil(vec3_len(from_center));
224 for (s32 x = -max_len; x <= +max_len; x++)
225 for (s32 y = -max_len; y <= +max_len; y++)
226 for (s32 z = -max_len; z <= +max_len; z++) {
229 for (int i = 0; i < 3; i++)
230 v[i] = floor(VOXELCTXSTATE(ctx).pos[i]
231 + mix(corners[0][i], corners[4][i], (f32) x / (f32) max_len / 2.0f)
232 + mix(corners[0][i], corners[2][i], (f32) y / (f32) max_len / 2.0f)
233 + mix(corners[0][i], corners[1][i], (f32) z / (f32) max_len / 2.0f));
235 mapgen_set_node(v3s32_add(ctx->pos, (v3s32) {v[0], v[2], v[1]}), CREATE_NODE, ctx->mgs, ctx->changed_blocks);
240 void voxelctx_cylinder(Voxelctx *ctx, Node node, bool use_color)
242 voxelctx_cube(ctx, node, use_color);
246 void voxelctx_cylinder(Voxelctx *ctx, Node node, bool use_color)
248 if (! voxelctx_is_alive(ctx))
253 f32 xf = VOXELCTXSTATE(ctx).scale[0] / 2.0f;
254 for (s32 x = round(-xf + 0.5f); x <= round(xf); x++) {
255 f32 yf = cos(x / VOXELCTXSTATE(ctx).scale[0] * M_PI) * VOXELCTXSTATE(ctx).scale[1] / 2.0f;
256 for (s32 y = round(-yf); y <= round(yf); y++) {
257 f32 zf = VOXELCTXSTATE(ctx).scale[2] / 2.0f;
258 for (s32 z = round(-zf + 0.5f); z <= round(zf); z++) {
259 mapgen_set_node((v3s32) {
260 ctx->pos.x + round(VOXELCTXSTATE(ctx).pos[0] + x),
261 ctx->pos.y + round(VOXELCTXSTATE(ctx).pos[2] + z),
262 ctx->pos.z + round(VOXELCTXSTATE(ctx).pos[1] + y),
263 }, CREATE_NODE, ctx->mgs, ctx->changed_blocks);
270 f32 voxelctx_random(Voxelctx *ctx, f32 base, f32 vary)
272 return base + noise3d(ctx->pos.x, ctx->pos.y, ctx->pos.z, ctx->random++, seed + SO_VOXELCTX) * vary;