aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 59a1fc034ee1ce0fa557399fdca45c827d6b209d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#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
    size_t 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) {
                if(least_entpy_sz >= TILEMAP_CAP) exit(69);
                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)
{
    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)))
            continue;

        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);

    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);
}

void manage_arguments(int argc, char **argv)
{
    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;

    }
    return;

error:
    fputs("ERROR: An error has accured with the given arguments", stderr);
    exit(EXIT_FAILURE);
}

int main(int argc, char **argv)
{
    SEED = time(0);

    manage_arguments(argc, argv);

    srand(SEED);
    printf("The Seed is %ld\n", SEED);

    load_tiles();
    init_tilemap();
    generate_tile_masks(get_tile_connections());

    while(1) {
        int lei;
        lei = get_least_entropy_index();

        if(has_collapsed(lei))
            break; // every index is set
        else
            collapse_this(lei);
    }

    size_t  img_wdt = TILE_WIDTH  * SWIDTH;
    size_t  img_hgt = TILE_HEIGHT * SHEIGHT;
    small_t *image = malloc(img_wdt * img_hgt * 3);

    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);

            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();
    destroy_tilemap();

    char file_name[64] = {0};

    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);
    return 0;
}