diff options
author | kartofen <mladenovnasko0@gmail.com> | 2022-12-21 01:06:18 +0200 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2022-12-21 01:06:18 +0200 |
commit | 85601f555a3a57db44631706a25e0f933afd8551 (patch) | |
tree | 576f7f7518ebd6f7d1b6f3534218c747d2a14a43 /src/main.c | |
parent | 88dd7e5fb0798d441dab81e9173d0e2d63f0b75e (diff) |
works with different connections
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 88 |
1 files changed, 42 insertions, 46 deletions
@@ -2,6 +2,7 @@ #include <stdlib.h> #include <string.h> #include <time.h> +#include <unistd.h> #include "typedef.h" #include "ppm.h" #include "tiles.h" @@ -16,8 +17,8 @@ size_t TILE_HEIGHT; // width and height of the tilemap // to generate -size_t SWIDTH = 30; -size_t SHEIGHT = 30; +size_t SWIDTH = 15; +size_t SHEIGHT = 15; time_t SEED; size_t SCALE = 9; @@ -59,7 +60,7 @@ int get_least_entropy_index() void collapse_this(int i) { if(count_entropy(i) == 0) { - fprintf(stderr, "ERROR: No possible tiles for this position: %d\n", i); + err("No possible tiles for this position: %d\n", i); exit(EXIT_FAILURE); } @@ -92,66 +93,61 @@ void collapse_this(int i) void manage_arguments(int argc, char **argv) { - if(argc == 1) return; - - for(int n = 1; n < argc; n++) + int opt; + while((opt = getopt(argc, argv, "hd:s:m:")) != -1) { - char *arg = argv[n]; - - if((strcmp(arg, "-h") == 0) || (strcmp(argv[1], "--help") == 0)) { + switch(opt) + { + case 'd': + SWIDTH = atoi(strtok(optarg, "x")); + SHEIGHT = atoi(strtok(NULL, "x")); + break; + case 's': + SEED = atoi(optarg); + break; + case 'm': + SCALE = atoi(optarg); + break; + case 'h': + default: puts("Usage: wfc [OPTION] [VALUE(S)]...\n"); - puts("The options are the following: (No option is mandatory)\n"); - puts("-h, --help Get help"); - puts("-d Change width and height of the tilemap, defaut is 30x30"); - puts(" args: 2 numbers; width and height seperated by space"); - puts("-s Change the seed for generating the tilemap, default is time(0)"); - puts(" args: 1 number; the seed"); - puts("-m Change the number, that the tilemap is scaled by"); - puts(" args: 1 number; scaler"); + puts("The options are the following: (No option is mandatory)"); + puts("-h Get help"); + puts("-d Change width and height of the tilemap, defaut is 30x30"); + puts(" args: 2 numbers; width and height seperated by an 'x'"); + puts("-s Change the seed, default is time(0)"); + puts(" args: 1 number; the seed"); + puts("-m Change the number, that the tilemap is scaled by"); + puts(" args: 1 number; scaler"); exit(EXIT_SUCCESS); - } else if(strcmp(arg, "-d") == 0) { - if(!(n+2 < argc)) goto error; - SWIDTH = atoi(argv[++n]); - SHEIGHT = atoi(argv[++n]); - } else if(strcmp(arg, "-s") == 0) { - if(!(n+1 < argc)) goto error; - SEED = atoi(argv[++n]); - } else if(strcmp(arg, "-m") == 0) { - if(!(n+1 < argc)) goto error; - SCALE = atoi(argv[++n]); - } else goto error; + break; + } } - return; - -error: - fputs("ERROR: An error has accured with the given arguments", stderr); - exit(EXIT_FAILURE); } int main(int argc, char **argv) { - // SEED = 4; SEED = time(0); - manage_arguments(argc, argv); - srand(SEED); - printf("The Seed is %ld\n", SEED); + + info("Starting With:\n\tWidth: %ld\n\tHeight: %ld\n\tSeed: %ld\n\tScale: %ld", SWIDTH, SHEIGHT, SEED, SCALE); load_tiles(); + info("Tiles Loaded"); init_tilemap(); + info("Tilemap Initiated"); generate_tile_masks(get_tile_connections()); + info("Tile Masks Generated"); - while(1) { - int lei; - lei = get_least_entropy_index(); + // print_tiles(); + // printf("%ld\n", TILES); - if(has_collapsed(lei)) - break; // every index is set - else - collapse_this(lei); - } + for(int lei = get_least_entropy_index(); !has_collapsed(lei); lei = get_least_entropy_index()) + collapse_this(lei); + + info("Tilemap Successfully Generated"); size_t img_wdt = TILE_WIDTH * SWIDTH; size_t img_hgt = TILE_HEIGHT * SHEIGHT; @@ -178,7 +174,7 @@ int main(int argc, char **argv) sprintf(file_name, "files/file_%ld.ppm", SEED); save_as_ppm(file_name, image, img_wdt, img_hgt, SCALE); - printf("Saved file with name: %s\n", file_name); + info("Saved file with name: %s\n", file_name); free(image); return 0; |