summaryrefslogtreecommitdiff
path: root/src/field.c
blob: 9be29549336b5bf4adc5da32e4e494bee0c01df8 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <stdlib.h>
#include <string.h>
#include <SFML/Graphics/RectangleShape.h>

#include "vector.h"
#include "field.h"
#include "tet_conf.h"

extern char arrShapeL[4][4];
extern char arrShapeRL[4][4];
extern char arrShapeZ[4][4];
extern char arrShapeS[4][4];
extern char arrShapeB[4][4];
extern char arrShapeI[4][4];
extern char arrShapeT[4][4];

static void rotate_shape_left(struct shape *shape);
static void rotate_shape_right(struct shape *shape);
static int out_of_bounds(struct field *fld, struct shape *active);

void field_init(struct field *fld)
{
    fld->c = calloc(fld->bound.y, sizeof(struct cell *));
    for (size_t j = 0; j < fld->bound.y; j++) {
        fld->c[j] = calloc(fld->bound.x, sizeof(struct cell));
        for (size_t i = 0; i < fld->size.x; i++)
            fld->c[j][i].a = 0;
    }
    fld->shape = calloc(fld->shape_cnt, sizeof(struct shape));
}

void field_deinit(struct field *fld) {
    for (int j = 0; j < (int)fld->bound.y; j++)
        free(fld->c[j]);
    free(fld->c);
    free(fld->shape);
}

void field_clear(struct field *fld)
{
    for (size_t j = 0; j < fld->bound.y; j++)
        for (size_t i = 0; i < fld->bound.x; i++) {
            fld->c[j][i].a = 0;
            fld->c[j][i].color = 0;
        }
}

void field_fill_random(struct field *fld)
{
    for (size_t j = 0; j < fld->size.y; j++)
        for (size_t i = 0; i < fld->size.x; i++) {
            fld->c[j][i].a = 1;
            fld->c[j][i].color = rand() % 7 + 1;
        }
}

void field_put_shape(struct field *fld, struct shape *active)
{
    for (int j = 0; j < 4; j++)
        for (int i = 0; i < 4; i++)
            if (active->c[j][i]) {
                fld->c[j+active->y][i+active->x].a = active->c[j][i];
                fld->c[j+active->y][i+active->x].color = active->t;
            }
}

int field_shape_out_of_bounds(struct field *fld, struct shape *active)
{
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            if (active->c[i][j] && active->y + i >= (int)fld->size.y)
                return 1;
    return 0;
}

void shape_load(struct shape *shape)
{
    switch (shape->t) { // Copy cell active/inactive state
        case 1 :
            memcpy(&shape->c[0][0], &arrShapeL[0][0], sizeof(char)*4*4);
            break;
        case 2 :
            memcpy(&shape->c[0][0], &arrShapeRL[0][0], sizeof(char)*4*4);
            break;
        case 3 :
            memcpy(&shape->c[0][0], &arrShapeZ[0][0], sizeof(char)*4*4);
            break;
        case 4 :
            memcpy(&shape->c[0][0], &arrShapeS[0][0], sizeof(char)*4*4);
            break;
        case 5 :
            memcpy(&shape->c[0][0], &arrShapeB[0][0], sizeof(char)*4*4);
            break;
        case 6 :
            memcpy(&shape->c[0][0], &arrShapeI[0][0], sizeof(char)*4*4);
            break;
        case 7 :
            memcpy(&shape->c[0][0], &arrShapeT[0][0], sizeof(char)*4*4);
            break;
    }
}

void field_reset_walking_shape(struct field *fld, size_t index)
{
    struct shape *shape = &fld->shape[index];
    shape_load(shape);
    shape->x = 3;
    if (shape->t == 6)
        shape->y = 19;
    else
        shape->y = 18;
    while (field_shape_collision(fld, shape))
        shape->y++;
}

void shape_gen_random(struct shape *shape)
{
    shape->t = (rand() % 7) + 1; // Insert new random shape of 7 variants
    shape_load(shape);
    if (shape->t == 5)
        for (int j = 0; j < 3; j++)
            for (int i = 0; i < 4; i++)
                shape->c[i][j] = shape->c[i][j+1];
}

int field_shape_collision(struct field *fld, struct shape *shape)
{
    if (out_of_bounds(fld, shape))
        return 1;
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            if (shape->c[i][j] && fld->c[i + shape->y][j + shape->x].a)
                return 1;
    return 0;
}

static int out_of_bounds(struct field *fld, struct shape *active)
{
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            if (active->c[i][j])
                if (active->x + j >= (int)fld->bound.x || active->x + j < 0
                    || active->y + i >= (int)fld->bound.y || active->y + i < 0)
                    return 1;
    return 0;
}

static void rotate_shape_left(struct shape *shape)
{
    char arr[4][4];
    memcpy(&arr[0][0], &shape->c[0][0], sizeof(char)*4*4);
    if (shape->t == 5)
        return;
    if (shape->t == 6) {
        for (int j = 3; j >= 0; j--)
            for (int i = 0; i < 4; i++)
                shape->c[j][i] = arr[3-i][3-j];
        return;
    }
    for (int j = 3; j > 0; j--)
        for (int i = 0; i < 3; i++)
            shape->c[j][i] = arr[3-i][j-1];
}

static void rotate_shape_right(struct shape *shape)
{
    char arr[4][4];
    memcpy(&arr[0][0], &shape->c[0][0], sizeof(char)*4*4);
    if (shape->t == 5)
        return;
    if (shape->t == 6) {
        for (int j = 3; j >= 0; j--)
            for (int i = 0; i < 4; i++)
                shape->c[j][i] = arr[3-i][3-j];
        return;
    }
    for (int j = 3; j > 0; j--)
        for (int i = 0; i < 3; i++)
            shape->c[j][i] = arr[i+1][3-j];
}

static int wall_kick(struct field *fld, struct shape *shape)
{
    // try kick the left wall
    shape->x++;
    if (field_shape_collision(fld, shape))
        shape->x--;
    else
        return 1;

    // try kick the right wall
    shape->x--;
    if (field_shape_collision(fld, shape)) {
        if (shape->t == 6) {
            shape->x--;
            if (field_shape_collision(fld, shape))
                shape->x++;
            else
                return 1;
        }
        shape->x++;
    } else {
        return 1;
    }

    // try kick the floor
    shape->y++;
    if (field_shape_collision(fld, shape)) {
        if (shape->t == 6) {
            shape->y++;
            if (field_shape_collision(fld, shape))
                shape->y--;
            else
                return 1;
        }
        shape->y--;
    } else {
        return 1;
    }

    return 0;
}

void field_rotate_shape_clockwise(struct field *fld, size_t index)
{
    struct shape *shape = &fld->shape[index];
    rotate_shape_right(shape);
    if (field_shape_collision(fld, shape))
        if (!wall_kick(fld, shape))
            rotate_shape_left(shape);
}

int field_move_shape_down(struct field *fld, size_t index)
{
    fld->shape[index].y--;
    if (field_shape_collision(fld, &fld->shape[index])) {
        fld->shape[index].y++;
        return 0;
    }
    return -1;
}

int field_move_shape_left(struct field *fld, size_t index)
{
    fld->shape[index].x--;
    if (field_shape_collision(fld, &fld->shape[index])) {
        fld->shape[index].x++;
        return 0;
    }
    return -1;
}

int field_move_shape_right(struct field *fld, size_t index)
{
    fld->shape[index].x++;
    if (field_shape_collision(fld, &fld->shape[index])) {
        fld->shape[index].x--;
        return 0;
    }
    return -1;
}

int field_rm_lines(struct field *fld)
{
    size_t lines = 0;
    for (size_t j = 0; j < fld->bound.y; j++) {
        size_t cells = 0;
        for (size_t i = 0; i < fld->bound.x; i++)
            if (fld->c[j][i].a)
                ++cells;
        if (cells == fld->bound.x) {
            ++lines;
            for (size_t n = j; n < fld->bound.y - 1; n++)
                for (size_t m = 0; m < fld->bound.x; m++) {
                    fld->c[n][m].a = fld->c[n + 1][m].a;
                    fld->c[n][m].color = fld->c[n + 1][m].color;
                }
            --j;
        }
    }
    return lines;
}