aboutsummaryrefslogtreecommitdiff
path: root/src/tilemap.c
blob: 70d534fe900b9fa200bcef4604008a2eeed253fe (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
#include "typedef.h"
#include "tilemap.h"

extern size_t TILES;
extern size_t SWIDTH;
extern size_t SHEIGHT;

big_t tilemap[TILEMAP_CAP];

// 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
big_t tile_masks[TILES_CAP][4];

void set(int i, int n)
{
    tilemap[i] |= (1 << n);
}

int is_set(int i, int n)
{
    return (tilemap[i] >> n) & 1;
}

void init_tilemap()
{
    for(size_t i = 0; i < SWIDTH * SHEIGHT; i++)
        for(size_t n = 0; n < TILES; n++)
            set(i, n);
}

size_t count_entropy(int i)
{
    if(has_collapsed(i)) return TILES+1;

    size_t c = 0;
    for (size_t j = 0; j < TILES; j++)
        c += is_set(i, j);

    return c;
}

void collapse(int i, int n)
{
    tilemap[i] = 0;
    set(i, n);
    set(i, TILES);
}

int has_collapsed(int i)
{
    return is_set(i, TILES);
}

int get_collapsed_tile(int i)
{
    for(size_t t = 0; t < TILES; t++)
        if(is_set(i, t)) return t;

    return TILES;
}


void mask(int i, int m, int r)
{
    tilemap[i] &= tile_masks[m][r];
}

void generate_tile_masks(small_t* tile_connections)
{
    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];
        }
    }
}