aboutsummaryrefslogtreecommitdiff
path: root/src/tilemap.c
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2022-08-12 14:07:54 +0300
committerkartofen <mladenovnasko0@gmail.com>2022-08-12 14:07:54 +0300
commitf0b443c37daa92d36ea752d837855a28b084dd3c (patch)
tree5c35efb32a07373d6c3d6cc523680d68fd24af02 /src/tilemap.c
parent02a5f64db70b61ad41c59c17d70feefaba1f21a5 (diff)
most likely works with more than 63
Diffstat (limited to 'src/tilemap.c')
-rw-r--r--src/tilemap.c65
1 files changed, 47 insertions, 18 deletions
diff --git a/src/tilemap.c b/src/tilemap.c
index 85d76bd..efd0b4e 100644
--- a/src/tilemap.c
+++ b/src/tilemap.c
@@ -1,39 +1,50 @@
+#include <stdlib.h>
+#include <string.h>
#include "typedef.h"
#include "tilemap.h"
+#define MAX(x, y) (((x) > (y)) ? (x) : (y))
+#define MIN(x, y) (((x) < (y)) ? (x) : (y))
+
extern size_t TILES;
extern size_t SWIDTH;
extern size_t SHEIGHT;
-big_t tilemap[TILEMAP_CAP];
+big_t (*tilemap)[TILEMAP_CAP];
+size_t tsz = 0;
// 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];
+big_t (*tile_masks)[TILES_CAP][4];
void set(int t, int n)
{
- tilemap[t] |= (1 << n);
+ tilemap[(n/64)][t] |= (1 << (n%64));
}
int is_set(int t, int n)
{
- return (tilemap[t] >> n) & 1;
+ return (tilemap[(n/64)][t] >> (n%64)) & 1;
}
void init_tilemap()
{
- // this is for support of more than 63 different tiles in
- // for(size_t i = 0; i < SWIDTH * SHEIGHT; i++)
- // for(size_t n = 0; n < TILES; n++)
- // set(i, n);
+ // 63 max tiles in 64 bit int
+ tsz = (TILES/64) + 1;
+ tilemap = malloc(sizeof(big_t[tsz][TILEMAP_CAP]));
- for(size_t i = 0; i < SWIDTH * SHEIGHT; i++)
- tilemap[i] = ((1 << TILES) - 1);
+ for(size_t i = 0; i < tsz; i++)
+ for(size_t n = 0; n < SWIDTH * SHEIGHT; n++)
+ tilemap[i][n] = ((1 << MIN(63, TILES)) - 1);
+}
+void destroy_tilemap()
+{
+ free(tilemap);
+ free(tile_masks);
}
int has_collapsed(int t)
@@ -51,7 +62,8 @@ int get_collapsed_tile(int t)
void collapse(int t, int n)
{
- tilemap[t] = 0;
+ for(size_t i = 0; i < tsz; i++)
+ tilemap[i][t] = 0;
set(t, n);
set(t, TILES);
}
@@ -69,29 +81,46 @@ size_t count_entropy(int t)
void mask(int t, int m, int r)
{
- tilemap[t] &= tile_masks[m][r];
+ for(size_t i = 0; i < tsz; i++)
+ tilemap[i][t] &= tile_masks[i][m][r];
}
void generate_tile_masks(small_t* tile_connections)
{
- size_t wt_con[4] = {0};
- size_t no_con[4] = {0};
+ size_t (*wt_con)[4];
+ size_t (*no_con)[4];
+ wt_con = malloc(sizeof(size_t[tsz][4]));
+ no_con = malloc(sizeof(size_t[tsz][4]));
for(int n = 0; n < 4; n++)
{
+ for(size_t i = 0; i < tsz; i++) {
+ wt_con[i][n] = 0;
+ no_con[i][n] = 0;
+ }
+
for(size_t i = 0; i < TILES; i++)
- wt_con[n] |= ((tile_connections[i] >> n) & 1) << i;
+ wt_con[(i/64)][n] |= ((tile_connections[i] >> n) & 1) << (i%64);
- no_con[n] = (~wt_con[n]) & ((1 << TILES) - 1);
+ for(size_t i = 0; i < TILES; i++)
+ no_con[(i/64)][n] |= ((wt_con[(i/64)][n] >> (i%64)) & 1) ? 0 : 1 << (i % 64);
}
+ tile_masks = malloc(sizeof(big_t[tsz][TILES_CAP][4]));
+
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];
+ for(size_t t = 0; t < tsz; t++)
+ tile_masks[t][i][n] = wt_con[t][n];
+ else
+ for(size_t t = 0; t < tsz; t++)
+ tile_masks[t][i][n] = no_con[t][n];
}
}
+
+ free(wt_con);
+ free(no_con);
}