From ec57f80b20e2309774b7e3a215640637e1c8e0f7 Mon Sep 17 00:00:00 2001 From: kartofen Date: Tue, 9 Aug 2022 23:07:37 +0300 Subject: better --- src/main.c | 98 ++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 34 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index c169326..d474ae2 100644 --- a/src/main.c +++ b/src/main.c @@ -5,41 +5,60 @@ #include "tiles.h" #include "ppm.h" -#define TILES 6 - -int *(tiles[TILES]) = {0}; +#define TILES_CAP 63 +size_t TILES; size_t TILE_WIDTH; size_t TILE_HEIGHT; -int tile_masks[TILES][4] = { - { 0b000011, 0b000101, 0b001001, 0b010001 }, +small_t *(tiles[TILES_CAP]) = {0}; - { 0b111100, 0b111010, 0b110110, 0b010001 }, - { 0b111100, 0b111010, 0b001001, 0b101110 }, - { 0b111100, 0b000101, 0b110110, 0b101110 }, - { 0b000011, 0b111010, 0b110110, 0b101110 }, +// each bit is if the tile has a connections or not +// from most signifact to least it is up, right, left, down +small_t tile_connections[TILES_CAP]; - { 0b111100, 0b111010, 0b110110, 0b101110 } -}; +// 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 +size_t tile_masks[TILES_CAP][4] = {0}; -#define WIDTH_SCALER 10 -#define HEIGHT_SCALER 10 +#define WIDTH_SCALER 100 +#define HEIGHT_SCALER 100 -int tiles_board[WIDTH_SCALER * HEIGHT_SCALER] = {0}; +size_t tiles_board[WIDTH_SCALER * HEIGHT_SCALER] = {0}; -void init_tiles_board() +void generate_tile_masks() { - for(int i = 0; i < WIDTH_SCALER * HEIGHT_SCALER; i++) - tiles_board[i] = (1 << TILES) - 1; + 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]; + } + } } -int count_1s(int v) +size_t count_1s(int v) { if(((v >> TILES) & 1) == 1) return TILES+1; v &= (1 << TILES) - 1; - int c = 0; + size_t c = 0; for (c = 0; v; v >>= 1) c += v & 1; @@ -81,7 +100,7 @@ int get_least_entropy_index() void set_index(int i) { - int n; + int n = 0; if(count_1s(tiles_board[i]) == 0) { fprintf(stderr, "ERROR: No possible tiles for this position: %d\n", i); @@ -95,8 +114,8 @@ void set_index(int i) continue; tiles_board[i] = 0; - tiles_board[i] |= 1 << n; - tiles_board[i] |= 1 << TILES; + tiles_board[i] |= (1 << n); + tiles_board[i] |= (1 << TILES); } while(((tiles_board[i] >> TILES) & 1) != 1); if(i / WIDTH_SCALER != 0) // up @@ -118,11 +137,22 @@ void set_index(int i) int main(void) { + TILES = calc_tiles(); + if(TILES > TILES_CAP) { + printf("ERROR: Too many tiles: %ld\n", TILES); + exit(EXIT_FAILURE); + } + time_t seed = time(0); srand(seed); printf("The Seed is %ld\n", seed); - init_tiles_board(); + load_connections(); + generate_tile_masks(); + + // init tiles board + for(int i = 0; i < WIDTH_SCALER * HEIGHT_SCALER; i++) + tiles_board[i] = (1 << TILES) - 1; while(1) { int lei; @@ -134,36 +164,36 @@ int main(void) set_index(lei); } - load_tiles(TILES); + load_tiles(); - int img_wdt = TILE_WIDTH * WIDTH_SCALER; - int img_hgt = TILE_HEIGHT * HEIGHT_SCALER; - int *image = malloc(img_wdt * img_hgt * sizeof(int)); - memset(image, 0, img_wdt * img_hgt * sizeof(int)); + size_t img_wdt = TILE_WIDTH * WIDTH_SCALER; + size_t img_hgt = TILE_HEIGHT * HEIGHT_SCALER; + small_t *image = malloc(img_wdt * img_hgt); + memset(image, 0, img_wdt * img_hgt); for(size_t i = 0; i < HEIGHT_SCALER; i++) { for(size_t j = 0; j < WIDTH_SCALER; j++) { - int t; + size_t t; for(t = 0; t < TILES; t++) if(((tiles_board[i * WIDTH_SCALER + j] >> t) & 1) == 1) break; - for(int y = 0; y < TILE_HEIGHT; y++) { - for(int x = 0; x < TILE_WIDTH; x++) { + for(size_t y = 0; y < TILE_HEIGHT; y++) { + for(size_t x = 0; x < TILE_WIDTH; x++) { image[(y+(i*TILE_HEIGHT)) * img_wdt + (x+(j*TILE_WIDTH))] = tiles[t][y * TILE_WIDTH + x]; } } } } - free_tiles(TILES); + free_tiles(); char file_name[64] = {0}; - sprintf(file_name, "files/file_%ld.ppm", seed); - printf("Saved file with name: %s\n", file_name); + sprintf(file_name, "files/file_%ld.ppm", seed); save_as_ppm(file_name, image, img_wdt, img_hgt, 10); + printf("Saved file with name: %s\n", file_name); free(image); return 0; -- cgit v1.2.3