aboutsummaryrefslogtreecommitdiff
path: root/src/tilemap.c
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2022-08-10 17:18:45 +0300
committerkartofen <mladenovnasko0@gmail.com>2022-08-10 17:18:45 +0300
commit851a3f34efd456295b21849199c3eaf4e70f9fb9 (patch)
tree7eff274bf1ea6b144254a0ebb063257ab2b33af0 /src/tilemap.c
parentbb820d973e9f53d33a32c46659e2a163e68be4ba (diff)
big refactor
Diffstat (limited to 'src/tilemap.c')
-rw-r--r--src/tilemap.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/tilemap.c b/src/tilemap.c
new file mode 100644
index 0000000..70d534f
--- /dev/null
+++ b/src/tilemap.c
@@ -0,0 +1,93 @@
+#include "typedef.h"
+#include "tilemap.h"
+
+extern size_t TILES;
+extern size_t SWIDTH;
+extern size_t SHEIGHT;
+
+big_t tilemap[TILEMAP_CAP];
+
+// 0 up 1 right
+// 2 left 3 down
+// each bit of the numbers is for every tiles
+// if the tile is a possibility
+// least significant bit is tile index 0
+big_t tile_masks[TILES_CAP][4];
+
+void set(int i, int n)
+{
+ tilemap[i] |= (1 << n);
+}
+
+int is_set(int i, int n)
+{
+ return (tilemap[i] >> n) & 1;
+}
+
+void init_tilemap()
+{
+ for(size_t i = 0; i < SWIDTH * SHEIGHT; i++)
+ for(size_t n = 0; n < TILES; n++)
+ set(i, n);
+}
+
+size_t count_entropy(int i)
+{
+ if(has_collapsed(i)) return TILES+1;
+
+ size_t c = 0;
+ for (size_t j = 0; j < TILES; j++)
+ c += is_set(i, j);
+
+ return c;
+}
+
+void collapse(int i, int n)
+{
+ tilemap[i] = 0;
+ set(i, n);
+ set(i, TILES);
+}
+
+int has_collapsed(int i)
+{
+ return is_set(i, TILES);
+}
+
+int get_collapsed_tile(int i)
+{
+ for(size_t t = 0; t < TILES; t++)
+ if(is_set(i, t)) return t;
+
+ return TILES;
+}
+
+
+void mask(int i, int m, int r)
+{
+ tilemap[i] &= tile_masks[m][r];
+}
+
+void generate_tile_masks(small_t* tile_connections)
+{
+ size_t wt_con[4] = {0};
+ size_t no_con[4] = {0};
+
+ for(int n = 0; n < 4; n++)
+ {
+ for(size_t i = 0; i < TILES; i++)
+ wt_con[n] |= ((tile_connections[i] >> n) & 1) << i;
+
+ no_con[n] = (~wt_con[n]) & ((1 << TILES) - 1);
+ }
+
+ for(size_t i = 0; i < TILES; i++)
+ {
+ for(int n = 0; n < 4; n++)
+ {
+ if((tile_connections[i] >> (3-n)) & 1)
+ tile_masks[i][n] = wt_con[n];
+ else tile_masks[i][n] = no_con[n];
+ }
+ }
+}