#include #include #include #include #include "typedef.h" #include "ppm.h" #include "tiles.h" #include "tilemap.h" size_t TILES; size_t TILE_WIDTH; size_t TILE_HEIGHT; size_t SWIDTH = 10; size_t SHEIGHT = 10; int get_least_entropy_index() { // array of indexes with the least entropy int least_entpy[TILEMAP_CAP]; size_t least_entpy_sz = 0; for(size_t i = 0; i < SHEIGHT; i++) { for (size_t j = 0; j < SWIDTH; j++) { size_t index = i * SWIDTH + j; if(least_entpy_sz == 0) { least_entpy[least_entpy_sz++] = index; continue; } size_t b1s = count_entropy(index); size_t l1s = count_entropy(least_entpy[least_entpy_sz-1]); if(b1s == l1s) { least_entpy[least_entpy_sz++] = index; } else if(b1s < l1s) { least_entpy[0] = index; least_entpy_sz = 1; } } } return least_entpy[rand() % least_entpy_sz]; } void collapse_this(int i) { int n = 0; if(count_entropy(i) == 0) { fprintf(stderr, "ERROR: No possible tiles for this position: %d\n", i); exit(EXIT_FAILURE); } // this bad do { n = rand() % TILES; if(!(is_set(i, n))) continue; collapse(i, n); } while(!(has_collapsed(i))); if(i / SWIDTH != 0) // up if(!(has_collapsed(i-SWIDTH))) mask(i-SWIDTH, n, 0); if(i % SWIDTH != (SWIDTH - 1)) // right if(!(has_collapsed(i+1))) mask(i+1, n, 1); if(i % SWIDTH != 0) // left if(!(has_collapsed(i-1))) mask(i-1, n, 2); if(i / SWIDTH != (SHEIGHT - 1)) // down if(!(has_collapsed(i+SWIDTH))) mask(i+SWIDTH, n, 3); } int main(void) { calc_tiles(); if(TILES > TILES_CAP) { printf("ERROR: Too many tiles: %ld\n", TILES); exit(EXIT_FAILURE); } time_t seed = 420; srand(seed); printf("The Seed is %ld\n", seed); generate_tile_masks(load_tile_connections()); init_tilemap(); while(1) { int lei; lei = get_least_entropy_index(); if(has_collapsed(lei)) break; // every index is set else collapse_this(lei); } load_tiles(); size_t img_wdt = TILE_WIDTH * SWIDTH; size_t img_hgt = TILE_HEIGHT * SHEIGHT; small_t *image = malloc(img_wdt * img_hgt * 3); memset(image, 0, img_wdt * img_hgt); for(size_t i = 0; i < SHEIGHT; i++) { for(size_t j = 0; j < SWIDTH; j++) { size_t t = get_collapsed_tile(i * SWIDTH + j); if(t == TILES) exit(-124); for(size_t y = 0; y < TILE_HEIGHT; y++) for(size_t x = 0; x < TILE_WIDTH; x++) for(int k = 0; k < 3; k++) image[((y+(i*TILE_HEIGHT))*img_wdt+(x+(j*TILE_WIDTH)))*3 + k] = get_tile_pixel(t, x, y, k); } } free_tiles(); char file_name[64] = {0}; 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; }