aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2022-08-11 21:45:03 +0300
committerkartofen <mladenovnasko0@gmail.com>2022-08-11 21:45:03 +0300
commit30d1c9b30b5642efcba66bdc7956f4d7e321fc97 (patch)
treeae3cf3aeb4b01f1cb70ae84cc84d453e08da35a5 /src/main.c
parent57315da56fa3b036f8e3e2d32a5f90a11ae7c3de (diff)
more used friendly
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c70
1 files changed, 59 insertions, 11 deletions
diff --git a/src/main.c b/src/main.c
index 6bec32e..5c32c13 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,19 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <time.h>
#include "typedef.h"
#include "ppm.h"
#include "tiles.h"
#include "tilemap.h"
+// amount of tiles
size_t TILES;
+// width and height of a single tile
size_t TILE_WIDTH;
size_t TILE_HEIGHT;
+// width and height of the tilemap
+// to generate
size_t SWIDTH = 30;
size_t SHEIGHT = 30;
+time_t SEED;
+size_t SCALE = 9;
+
int get_least_entropy_index()
{
// array of indexes with the least entropy
@@ -49,14 +57,13 @@ int get_least_entropy_index()
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
+ int n = 0;
do {
n = rand() % TILES;
if(!(is_set(i, n)))
@@ -65,6 +72,7 @@ void collapse_this(int i)
collapse(i, n);
} while(!(has_collapsed(i)));
+ // apply a bitmask, based on the direction
if(i / SWIDTH != 0) // up
if(!(has_collapsed(i-SWIDTH)))
mask(i-SWIDTH, n, 0);
@@ -82,15 +90,56 @@ void collapse_this(int i)
mask(i+SWIDTH, n, 3);
}
-int main(void)
+void manage_arguments(int argc, char **argv)
{
- load_tiles();
- print_tiles();
+ if(argc == 1) return;
+
+ for(int n = 1; n < argc; n++)
+ {
+ char *arg = argv[n];
+
+ if((strcmp(arg, "-h") == 0) || (strcmp(argv[1], "--help") == 0)) {
+ 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");
+ 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;
- time_t seed = 69;
- srand(seed);
- printf("The Seed is %ld\n", seed);
+ }
+ return;
+error:
+ fputs("ERROR: An error has accured with the given arguments", stderr);
+ exit(EXIT_FAILURE);
+}
+
+int main(int argc, char **argv)
+{
+ // SEED = 69;
+ SEED = time(0);
+
+ manage_arguments(argc, argv);
+
+ srand(SEED);
+ printf("The Seed is %ld\n", SEED);
+
+ load_tiles();
generate_tile_masks(get_tile_connections());
init_tilemap();
@@ -113,7 +162,6 @@ int main(void)
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++)
@@ -127,8 +175,8 @@ int main(void)
char file_name[64] = {0};
- sprintf(file_name, "files/file_%ld.ppm", seed);
- save_as_ppm(file_name, image, img_wdt, img_hgt, 9);
+ 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);
free(image);