aboutsummaryrefslogtreecommitdiff
path: root/src/tilemap.c
blob: 85d76bd5fe405f39c8d741574c846a31d3465601 (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
#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 t, int n)
{
    tilemap[t] |= (1 << n);
}

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

void init_tilemap()
{
    // this is for support of more than 63 different tiles in
    // for(size_t i = 0; i < SWIDTH * SHEIGHT; i++)
    //     for(size_t n = 0; n < TILES; n++)
    //         set(i, n);

    for(size_t i = 0; i < SWIDTH * SHEIGHT; i++)
        tilemap[i] = ((1 << TILES) - 1);

}

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

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

    return TILES;
}

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

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

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

    return c;
}

void mask(int t, int m, int r)
{
    tilemap[t] &= 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];
        }
    }
}