From f0b443c37daa92d36ea752d837855a28b084dd3c Mon Sep 17 00:00:00 2001 From: kartofen Date: Fri, 12 Aug 2022 14:07:54 +0300 Subject: most likely works with more than 63 --- src/tilemap.c | 65 ++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 18 deletions(-) (limited to 'src/tilemap.c') 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 +#include #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); } -- cgit v1.2.3