3 #include "server/mapgen.h"
4 #include "server/voxelctx.h"
8 #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 static VoxelctxState *create_state(VoxelctxState *old)
12 VoxelctxState *state = malloc(sizeof(VoxelctxState));
21 state->scale[0] = 1.0f;
22 state->scale[1] = 1.0f;
23 state->scale[2] = 1.0f;
24 mat4x4_identity(state->mat);
34 Voxelctx *voxelctx_create(List *changed_blocks, MapgenStage mgs, v3s32 pos)
36 Voxelctx *ctx = malloc(sizeof(Voxelctx));
38 ctx->changed_blocks = changed_blocks;
41 ctx->statestack = list_create(NULL);
44 list_put(&ctx->statestack, create_state(NULL), NULL);
49 static void list_delete_state(void *key, unused void *value, unused void *arg)
54 void voxelctx_delete(Voxelctx *ctx)
56 list_clear_func(&ctx->statestack, &list_delete_state, NULL);
60 static void move_value(f32 *x, f32 v, f32 range)
62 f32 dst = v >= 0 ? range : 0;
64 *x = f32_mix(*x, dst, v);
67 void voxelctx_hue(Voxelctx *ctx, f32 value)
69 VOXELCTXSTATE(ctx).h += value;
72 void voxelctx_sat(Voxelctx *ctx, f32 value)
74 move_value(&VOXELCTXSTATE(ctx).s, value, 1.0f);
77 void voxelctx_light(Voxelctx *ctx, f32 value)
79 move_value(&VOXELCTXSTATE(ctx).l, value, 1.0f);
82 void voxelctx_life(Voxelctx *ctx, s32 value)
84 VOXELCTXSTATE(ctx).life += value;
87 static void apply_translation(Voxelctx *ctx, v3f32 translate)
90 mat4x4_mul_vec4(translate_vec, VOXELCTXSTATE(ctx).mat, (vec4) {translate.x, translate.y, translate.z, 0.0f});
91 vec4_add(VOXELCTXSTATE(ctx).pos, VOXELCTXSTATE(ctx).pos, translate_vec);
94 void voxelctx_x(Voxelctx *ctx, f32 value)
96 apply_translation(ctx, (v3f32) {value, 0.0f, 0.0f});
99 void voxelctx_y(Voxelctx *ctx, f32 value)
101 apply_translation(ctx, (v3f32) {0.0f, value, 0.0f});
104 void voxelctx_z(Voxelctx *ctx, f32 value)
106 apply_translation(ctx, (v3f32) {0.0f, 0.0f, value});
109 void voxelctx_rx(Voxelctx *ctx, f32 value)
111 mat4x4_rotate_X(VOXELCTXSTATE(ctx).mat, VOXELCTXSTATE(ctx).mat, value * M_PI / 180.0f);
114 void voxelctx_ry(Voxelctx *ctx, f32 value)
116 mat4x4_rotate_Y(VOXELCTXSTATE(ctx).mat, VOXELCTXSTATE(ctx).mat, value * M_PI / 180.0f);
119 void voxelctx_rz(Voxelctx *ctx, f32 value)
121 mat4x4_rotate_Z(VOXELCTXSTATE(ctx).mat, VOXELCTXSTATE(ctx).mat, value * M_PI / 180.0f);
124 static void apply_scale(Voxelctx *ctx, v3f32 scale)
126 VOXELCTXSTATE(ctx).scale[0] *= scale.x;
127 VOXELCTXSTATE(ctx).scale[1] *= scale.y;
128 VOXELCTXSTATE(ctx).scale[2] *= scale.z;
130 mat4x4_scale_aniso(VOXELCTXSTATE(ctx).mat, VOXELCTXSTATE(ctx).mat, scale.x, scale.y, scale.z);
133 void voxelctx_sx(Voxelctx *ctx, f32 value)
135 apply_scale(ctx, (v3f32) {value, 1.0f, 1.0f});
138 void voxelctx_sy(Voxelctx *ctx, f32 value)
140 apply_scale(ctx, (v3f32) {1.0f, value, 1.0f});
143 void voxelctx_sz(Voxelctx *ctx, f32 value)
145 apply_scale(ctx, (v3f32) {1.0f, 1.0f, value});
148 void voxelctx_s(Voxelctx *ctx, f32 value)
150 apply_scale(ctx, (v3f32) {value, value, value});
153 void voxelctx_pop(Voxelctx *ctx)
155 ListPair *next = ctx->statestack.first->next;
156 free(ctx->statestack.first->key);
157 free(ctx->statestack.first);
158 ctx->statestack.first = next;
161 void voxelctx_push(Voxelctx *ctx)
163 ListPair *pair = malloc(sizeof(ListPair));
164 pair->key = create_state(&VOXELCTXSTATE(ctx));
166 pair->next = ctx->statestack.first;
168 ctx->statestack.first = pair;
171 bool voxelctx_is_alive(Voxelctx *ctx)
173 if (VOXELCTXSTATE(ctx).life > 0) {
174 VOXELCTXSTATE(ctx).life--;
175 if (VOXELCTXSTATE(ctx).life <= 0)
179 return VOXELCTXSTATE(ctx).scale[0] >= 1.0f && VOXELCTXSTATE(ctx).scale[1] >= 1.0f && VOXELCTXSTATE(ctx).scale[2] >= 1.0f;
182 void voxelctx_cube(Voxelctx *ctx, Node node, bool use_color)
184 if (! voxelctx_is_alive(ctx))
187 vec4 base_corners[8] = {
188 {0.0f, 0.0f, 0.0f, 0.0f},
189 {0.0f, 0.0f, 1.0f, 0.0f},
190 {0.0f, 1.0f, 0.0f, 0.0f},
191 {0.0f, 1.0f, 1.0f, 0.0f},
192 {1.0f, 0.0f, 0.0f, 0.0f},
193 {1.0f, 0.0f, 1.0f, 0.0f},
194 {1.0f, 1.0f, 0.0f, 0.0f},
195 {1.0f, 1.0f, 1.0f, 0.0f},
204 mat4x4_mul_vec4(center, VOXELCTXSTATE(ctx).mat, (vec4) {0.5f, 0.5f, 0.5f});
206 for (int i = 0; i < 8; i++) {
207 mat4x4_mul_vec4(corners[i], VOXELCTXSTATE(ctx).mat, base_corners[i]);
210 vec3_sub(from_center, corners[i], center);
212 s32 len = ceil(vec3_len(from_center));
218 for (s32 x = -max_len; x <= +max_len; x++)
219 for (s32 y = -max_len; y <= +max_len; y++)
220 for (s32 z = -max_len; z <= +max_len; z++) {
223 for (int i = 0; i < 3; i++) {
225 + f32_mix(corners[0][i], corners[4][i], (f32) x / (f32) max_len / 2.0f)
226 + f32_mix(corners[0][i], corners[2][i], (f32) y / (f32) max_len / 2.0f)
227 + f32_mix(corners[0][i], corners[1][i], (f32) z / (f32) max_len / 2.0f));
229 v[i] = floor(VOXELCTXSTATE(ctx).pos[i] + f + 0.5f);
232 mapgen_set_node(v3s32_add(ctx->pos, (v3s32) {v[0], v[2], v[1]}), CREATE_NODE, ctx->mgs, ctx->changed_blocks);
237 void voxelctx_cylinder(Voxelctx *ctx, Node node, bool use_color)
239 voxelctx_cube(ctx, node, use_color);
243 void voxelctx_cylinder(Voxelctx *ctx, Node node, bool use_color)
245 if (! voxelctx_is_alive(ctx))
250 f32 xf = VOXELCTXSTATE(ctx).scale[0] / 2.0f;
251 for (s32 x = round(-xf + 0.5f); x <= round(xf); x++) {
252 f32 yf = cos(x / VOXELCTXSTATE(ctx).scale[0] * M_PI) * VOXELCTXSTATE(ctx).scale[1] / 2.0f;
253 for (s32 y = round(-yf); y <= round(yf); y++) {
254 f32 zf = VOXELCTXSTATE(ctx).scale[2] / 2.0f;
255 for (s32 z = round(-zf + 0.5f); z <= round(zf); z++) {
256 mapgen_set_node((v3s32) {
257 ctx->pos.x + round(VOXELCTXSTATE(ctx).pos[0] + x),
258 ctx->pos.y + round(VOXELCTXSTATE(ctx).pos[2] + z),
259 ctx->pos.z + round(VOXELCTXSTATE(ctx).pos[1] + y),
260 }, CREATE_NODE, ctx->mgs, ctx->changed_blocks);
267 f32 voxelctx_random(Voxelctx *ctx, f32 base, f32 vary)
269 return base + noise3d(ctx->pos.x, ctx->pos.y, ctx->pos.z, ctx->random++, seed + SO_VOXELCTX) * vary;