]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/server/voxel_depth_search.c
Rework structure
[dragonblocks_alpha.git] / src / server / voxel_depth_search.c
1 #include <stdlib.h>
2 #include "server/voxel_depth_search.h"
3
4 v3s32 dirs[6] = {
5         {+0, -1, +0}, // this is commonly used to find ground, search downwards first
6         {-1, +0, +0},
7         {+0, +0, -1},
8         {+1, +0, +0},
9         {+0, +0, +1},
10         {+0, +1, +0},
11 };
12
13 static int cmp_depth_search_node(const DepthSearchNode *node, const v3s32 *pos)
14 {
15         return v3s32_cmp(&node->pos, pos);
16 }
17
18 bool voxel_depth_search(v3s32 pos, void (*callback)(DepthSearchNode *node, void *arg), void *arg, bool *success, Tree *visit)
19 {
20         TreeNode **tree_node = tree_nfd(visit, &pos, &cmp_depth_search_node);
21         if (*tree_node)
22                 return *success = *((DepthSearchNode *) (*tree_node)->dat)->success;
23
24         DepthSearchNode *node = malloc(sizeof *node);
25         tree_nmk(visit, tree_node, node);
26         node->pos = pos;
27         node->extra = NULL;
28         callback(node, arg);
29         if ((*(node->success = success) = (node->type == DEPTH_SEARCH_TARGET)))
30                 return true;
31
32         if (node->type == DEPTH_SEARCH_PATH)
33                 for (int i = 0; i < 6; i++)
34                         if (voxel_depth_search(v3s32_add(pos, dirs[i]), callback, arg, success, visit))
35                                 return true;
36
37         return false;
38 }