summaryrefslogtreecommitdiff
path: root/src/utils/include/engine
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/include/engine')
-rw-r--r--src/utils/include/engine/utils/btree.h54
-rw-r--r--src/utils/include/engine/utils/fov.h21
-rw-r--r--src/utils/include/engine/utils/hashmap.h54
-rw-r--r--src/utils/include/engine/utils/list.h11
-rw-r--r--src/utils/include/engine/utils/stack.h50
-rw-r--r--src/utils/include/engine/utils/utils.h36
-rw-r--r--src/utils/include/engine/utils/vector.h31
7 files changed, 257 insertions, 0 deletions
diff --git a/src/utils/include/engine/utils/btree.h b/src/utils/include/engine/utils/btree.h
new file mode 100644
index 0000000..04f9ff8
--- /dev/null
+++ b/src/utils/include/engine/utils/btree.h
@@ -0,0 +1,54 @@
+#ifndef BTREE_H
+#define BTREE_H
+
+#include <stddef.h>
+
+#define BTREE_DEGREE_DEFAULT 4
+
+#define BTREE_SIZE_MIN 8
+#define BTREE_SIZE_MAX 4096
+
+#define BTREE_CMP_LT (-1)
+#define BTREE_CMP_EQ (0)
+#define BTREE_CMP_GT (1)
+
+struct btree;
+struct btree_iter_t;
+
+/* elem_size: the size of the elements, typically `sizeof(struct <your struct>)`
+ * t: degree of the btree, if you're in doubt, use `BTREE_SIZE_DEFAULT`
+ * cmp: comparison function, in order to support any operations on the tree.
+ *
+ * This function just calls `btree_new_with_allocator` with `free` and `malloc`
+ * as initializers.
+ */
+struct btree* btree_new(size_t elem_size, size_t t,
+ int (*cmp)(const void* a, const void* b));
+
+/* Same as `btree_new`, except that it actually initializes a btree, but with
+ * the given allocators.
+ */
+struct btree* btree_new_with_allocator(size_t elem_size, size_t t,
+ int (*cmp)(const void* a, const void* b),
+ void* (*alloc)(size_t),
+ void (*dealloc)(void*));
+
+void btree_free(struct btree** btree);
+
+void* btree_search(struct btree* btree, void* elem);
+void btree_insert(struct btree* btree, void* elem);
+int btree_delete(struct btree* btree, void* elem);
+
+void btree_print(struct btree* btree, void (*print_elem)(const void*));
+
+void* btree_first(struct btree* btree);
+void* btree_last(struct btree* btree);
+
+size_t btree_size(struct btree* btree);
+
+struct btree_iter_t* btree_iter_t_new(struct btree* tree);
+void btree_iter_t_reset(struct btree* tree, struct btree_iter_t** it);
+
+void* btree_iter(struct btree* tree, struct btree_iter_t* iter);
+
+#endif
diff --git a/src/utils/include/engine/utils/fov.h b/src/utils/include/engine/utils/fov.h
new file mode 100644
index 0000000..15ef38b
--- /dev/null
+++ b/src/utils/include/engine/utils/fov.h
@@ -0,0 +1,21 @@
+#ifndef ENGINE_FOV_H
+#define ENGINE_FOV_H
+
+#include "types.h"
+#include "vector.h"
+#include <stdbool.h>
+
+/* `fov_shadowcast`: */
+/* map: your 2D enum tile grid
+ * mapsize: x: width, y: height of the map
+ * visblocking: pointer to a function that returns `true` when receiving a
+ * pointer to a LOS blocking tile
+ * lightmap: [out] 2D lightmap, this is simply overwritten with the
+ * distance to the source. range: visibility range/radius. src: 2D
+ * point to calculate FOV from
+ * */
+void fov_shadowcast(const void* map, const v2_i32 mapsize,
+ bool (*visblocking)(const void*), i32* lightmap,
+ const i32 range, v2_i32 src);
+
+#endif
diff --git a/src/utils/include/engine/utils/hashmap.h b/src/utils/include/engine/utils/hashmap.h
new file mode 100644
index 0000000..bf1d87c
--- /dev/null
+++ b/src/utils/include/engine/utils/hashmap.h
@@ -0,0 +1,54 @@
+#ifndef ENGINE_HASHMAP_H
+#define ENGINE_HASHMAP_H
+
+#include "types.h"
+
+#include "list.h"
+#include "memory.h"
+#include <stdlib.h>
+
+i32 lolhash(const usize s, i32 v);
+
+/* Define a linked list before using this */
+/* Example: DEFINE_LLIST(i32) */
+#define DEFINE_HASHMAP(type, lsize, cmp, type_to_int) \
+ typedef DEFINE_LLIST(type); \
+ typedef struct hashmap_##type { \
+ usize size; \
+ List_##type elems[64]; \
+ } hashmap_##type; \
+ \
+ type* hashmap_##type##_lookup(hashmap_##type* hmap, const type* val) { \
+ const i32 idx = lolhash(64, type_to_int(val)); \
+ List_##type* head = &hmap->elems[idx]; \
+ while (head != NULL) { \
+ if (!cmp(&(head->value), val)) return &(head->value); \
+ head = head->next; \
+ } \
+ return NULL; \
+ } \
+ \
+ void hashmap_##type##_insert(memory* m, hashmap_##type* hmap, \
+ const type* val) { \
+ const i32 idx = lolhash(64, type_to_int(val)); \
+ List_##type* head = &(hmap->elems[idx]); \
+ \
+ /* This is highly dependant on whether the memory is zero-initialized */ \
+ if (!type_to_int(&(head->value))) { \
+ memcpy(&(head->value), val, sizeof(type)); \
+ return; \
+ } \
+ \
+ while (head->next != NULL && cmp(&head->value, val)) { \
+ head = head->next; \
+ } \
+ \
+ if (!cmp(&head->value, val)) \
+ memcpy(&(head->value), val, sizeof(type)); \
+ else { \
+ head->next = memory_allocate(m, sizeof(List_##type)); \
+ memcpy(&(head->next->value), val, sizeof(type)); \
+ } \
+ }
+
+#endif
diff --git a/src/utils/include/engine/utils/list.h b/src/utils/include/engine/utils/list.h
new file mode 100644
index 0000000..37857f0
--- /dev/null
+++ b/src/utils/include/engine/utils/list.h
@@ -0,0 +1,11 @@
+#ifndef ENGINE_LIST_H
+#define ENGINE_LIST_H
+
+#define DEFINE_LLIST(type) \
+ struct List_##type { \
+ type value; \
+ struct List_##type* next; \
+ /* Force the user to add `;` for style consistency */ \
+ } List_##type
+
+#endif
diff --git a/src/utils/include/engine/utils/stack.h b/src/utils/include/engine/utils/stack.h
new file mode 100644
index 0000000..69975df
--- /dev/null
+++ b/src/utils/include/engine/utils/stack.h
@@ -0,0 +1,50 @@
+/* Copyright © 2021 Upqwerk
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the “Software”), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ * */
+
+/* Stack implementation
+ * Author: Upqwerk
+ */
+#ifndef STACK_H
+#define STACK_H
+
+#include "types.h"
+
+typedef struct {
+ isize head; /* current number of elements */
+ const usize elem_size; /* size in bytes of each element */
+ usize size; /* current memory size used by the stack */
+ const usize chunk_size; /* size of which the stack increases when running out
+ of mem */
+ void* data; /* memory buffer */
+} Stack;
+
+Stack stack_new_ex(const usize element_size, const usize size);
+
+Stack stack_new(const usize element_size);
+
+void stack_free(Stack* s);
+void* stack_pop(Stack* s);
+void stack_push(Stack* s, void* elem);
+void* stack_peek(Stack* s);
+isize stack_size(const Stack* s);
+void stack_swap(Stack* s, Stack* t);
+
+#endif
diff --git a/src/utils/include/engine/utils/utils.h b/src/utils/include/engine/utils/utils.h
new file mode 100644
index 0000000..e537f52
--- /dev/null
+++ b/src/utils/include/engine/utils/utils.h
@@ -0,0 +1,36 @@
+#ifndef ENGINE_UTILS_H
+#define ENGINE_UTILS_H
+
+#include "types.h"
+#include "vector.h"
+
+#define MIN(a, b) ((a < b) ? (a) : (b))
+#define MAX(a, b) ((a > b) ? (a) : (b))
+
+#define MASK_TL (1 << 0)
+#define MASK_T (1 << 1)
+#define MASK_TR (1 << 2)
+#define MASK_L (1 << 3)
+#define MASK_C (1 << 4)
+#define MASK_R (1 << 5)
+#define MASK_BL (1 << 6)
+#define MASK_B (1 << 7)
+#define MASK_BR (1 << 8)
+
+/* Linear interpolate */
+f32 lerp(f32 dt, f32 a, f32 b);
+i32 int_lerp(f32 dt, i32 a, i32 b);
+
+/* Hashes */
+u32 hash(char* str);
+
+/* Masks surrounding tiles of a kernel size of 3x3 */
+/* In reality we only need 9 bits for this, but I think I had a reason for using
+ * i32 */
+i32* kernmap(const void* map, i32* dstmap, const v2_i32 mapsize,
+ predicate_t* predicate);
+
+/* Returns an index from the given weights. */
+i32 pick_from_sample(const i32* weights, i32 len);
+
+#endif
diff --git a/src/utils/include/engine/utils/vector.h b/src/utils/include/engine/utils/vector.h
new file mode 100644
index 0000000..58bb0a2
--- /dev/null
+++ b/src/utils/include/engine/utils/vector.h
@@ -0,0 +1,31 @@
+#ifndef VECTOR_H
+#define VECTOR_H
+
+#include "types.h"
+
+#include <stdbool.h>
+#include <stdio.h>
+
+typedef struct {
+ i32 x;
+ i32 y;
+} v2_i32;
+
+bool v2_i32_eq(const v2_i32 a, const v2_i32 b);
+
+v2_i32 v2_i32_add(v2_i32 a, v2_i32 b);
+v2_i32 v2_i32_add_i(v2_i32 a, i32 b);
+v2_i32 v2_i32_sub(v2_i32 a, v2_i32 b);
+v2_i32 v2_i32_sub_i(v2_i32 a, i32 b);
+v2_i32 v2_i32_div(v2_i32 a, v2_i32 b);
+v2_i32 v2_i32_div_i(v2_i32 a, i32 b);
+v2_i32 v2_i32_mul(v2_i32 a, v2_i32 b);
+v2_i32 v2_i32_mul_i(v2_i32 a, i32 b);
+v2_i32 v2_i32_mod(v2_i32 a, v2_i32 b);
+v2_i32 v2_i32_mod_i(v2_i32 a, i32 b);
+v2_i32 v2_i32_max(v2_i32 a, v2_i32 b);
+v2_i32 v2_i32_min(v2_i32 a, v2_i32 b);
+v2_i32 v2_i32_lerp(f32 dt, v2_i32 a, v2_i32 b);
+
+void v2_i32_fprintf(FILE* stream, v2_i32 a);
+#endif