#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]; } } }