forked from DavidGriffith/tint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.c
468 lines (435 loc) · 15.4 KB
/
engine.c
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* Copyright (c) Abraham vd Merwe <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the author nor the names of other contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <string.h>
#include "typedefs.h"
#include "utils.h"
#include "io.h"
#include "engine.h"
static void new_block(engine_t *engine) ;
/*
* Global variables
*/
const shapes_t SHAPES =
{
{ COLOR_CYAN, 0, FALSE, { { 1, 0 }, { 0, 0 }, { 0, -1 }, { -1, -1 } } },
{ COLOR_GREEN, 1, FALSE, { { 1, -1 }, { 0, -1 }, { 0, 0 }, { -1, 0 } } },
{ COLOR_YELLOW, 2, FALSE, { { -1, 0 }, { 0, 0 }, { 1, 0 }, { 0, 1 } } },
{ COLOR_BLUE, 3, FALSE, { { -1, -1 }, { 0, -1 }, { -1, 0 }, { 0, 0 } } },
{ COLOR_MAGENTA, 4, FALSE, { { -1, 1 }, { -1, 0 }, { 0, 0 }, { 1, 0 } } },
{ COLOR_WHITE, 5, FALSE, { { 1, 1 }, { 1, 0 }, { 0, 0 }, { -1, 0 } } },
{ COLOR_RED, 6, FALSE, { { -1, 0 }, { 0, 0 }, { 1, 0 }, { 2, 0 } } }
};
static engine_t engine_save[2]; // for undo
/*
* Functions
*/
/* This rotates a shape */
static void real_rotate (shape_t *shape,bool clockwise)
{
int i,tmp;
if (clockwise)
{
for (i = 0; i < NUMBLOCKS; i++)
{
tmp = shape->block[i].x;
shape->block[i].x = -shape->block[i].y;
shape->block[i].y = tmp;
}
}
else
{
for (i = 0; i < NUMBLOCKS; i++)
{
tmp = shape->block[i].x;
shape->block[i].x = shape->block[i].y;
shape->block[i].y = -tmp;
}
}
}
/* Rotate shapes the way tetris likes it (= not mathematically correct) */
static void fake_rotate (shape_t *shape)
{
switch (shape->type)
{
case 0: /* Just rotate this one anti-clockwise and clockwise */
if (shape->flipped) real_rotate (shape,TRUE); else real_rotate (shape,FALSE);
shape->flipped = !shape->flipped;
break;
case 1: /* Just rotate these two clockwise and anti-clockwise */
case 6:
if (shape->flipped) real_rotate (shape,FALSE); else real_rotate (shape,TRUE);
shape->flipped = !shape->flipped;
break;
case 2: /* Rotate these three anti-clockwise */
case 4:
case 5:
real_rotate (shape,FALSE);
break;
case 3: /* This one is not rotated at all */
break;
}
}
/* Draw a shape on the board */
static void drawshape (board_t board,shape_t *shape,int x,int y)
{
int i;
for (i = 0; i < NUMBLOCKS; i++) board[x + shape->block[i].x][y + shape->block[i].y] = shape->color;
}
static void drawshadowshape (board_t board,shape_t *shape,int x,int y)
{
int i;
for (i = 0; i < NUMBLOCKS; i++) board[x + shape->block[i].x][y + shape->block[i].y] = -shape->color;
}
/* Erase a shape from the board */
static void eraseshape (board_t board,shape_t *shape,int x,int y)
{
int i;
for (i = 0; i < NUMBLOCKS; i++) board[x + shape->block[i].x][y + shape->block[i].y] = COLOR_BLACK;
}
/* Check if shape is allowed to be in this position */
static bool allowed (board_t board,shape_t *shape,int x,int y)
{
int i,occupied = FALSE;
for (i = 0; i < NUMBLOCKS; i++) if (board[x + shape->block[i].x][y + shape->block[i].y]) occupied = TRUE;
return (!occupied);
}
/* Set y coordinate of shadow */
static void place_shadow_to_bottom (board_t board,shape_t *shape,int x_shadow,int *y_shadow,int y) {
while (allowed(board,shape,x_shadow,y+1)) y++;
*y_shadow = y;
}
/* Move the shape left if possible */
static bool shape_left (engine_t *engine)
{
board_t *board = &engine->board;
shape_t *shape = &engine->shapes[engine->curshape];
bool result = FALSE;
eraseshape (*board,shape,engine->curx,engine->cury);
if (engine->shadow) eraseshape (*board,shape,engine->curx_shadow,engine->cury_shadow);
if (allowed (*board,shape,engine->curx - 1,engine->cury))
{
engine->curx--;
result = TRUE;
if (engine->shadow)
{
engine->curx_shadow--;
place_shadow_to_bottom(*board,shape,engine->curx_shadow,&engine->cury_shadow,engine->cury);
}
}
if (engine->shadow) drawshadowshape (*board,shape,engine->curx_shadow,engine->cury_shadow);
drawshape (*board,shape,engine->curx,engine->cury);
return result;
}
/* Move the shape right if possible */
static bool shape_right (engine_t *engine)
{
board_t *board = &engine->board;
shape_t *shape = &engine->shapes[engine->curshape];
bool result = FALSE;
eraseshape (*board,shape,engine->curx,engine->cury);
if (engine->shadow) eraseshape (*board,shape,engine->curx_shadow,engine->cury_shadow);
if (allowed (*board,shape,engine->curx + 1,engine->cury))
{
engine->curx++;
result = TRUE;
if (engine->shadow)
{
engine->curx_shadow++;
place_shadow_to_bottom(*board,shape,engine->curx_shadow,&engine->cury_shadow,engine->cury);
}
}
if (engine->shadow) drawshadowshape (*board,shape,engine->curx_shadow,engine->cury_shadow);
drawshape (*board,shape,engine->curx,engine->cury);
return result;
}
/* Rotate the shape if possible */
static bool shape_rotate (engine_t *engine)
{
board_t *board = &engine->board;
shape_t *shape = &engine->shapes[engine->curshape];
bool result = FALSE;
shape_t test;
eraseshape (*board,shape,engine->curx,engine->cury);
if (engine->shadow) eraseshape (*board,shape,engine->curx_shadow,engine->cury_shadow);
memcpy (&test,shape,sizeof (shape_t));
fake_rotate (&test);
if (allowed (*board,&test,engine->curx,engine->cury))
{
memcpy (shape,&test,sizeof (shape_t));
result = TRUE;
if (engine->shadow) place_shadow_to_bottom(*board,shape,engine->curx_shadow,&engine->cury_shadow,engine->cury);
}
if (engine->shadow) drawshadowshape (*board,shape,engine->curx_shadow,engine->cury_shadow);
drawshape (*board,shape,engine->curx,engine->cury);
return result;
}
/* Move the shape one row down if possible */
static bool shape_down (engine_t *engine)
{
board_t *board = &engine->board;
shape_t *shape = &engine->shapes[engine->curshape];
bool result = FALSE;
eraseshape (*board,shape,engine->curx,engine->cury);
if (engine->shadow) eraseshape (*board,shape,engine->curx_shadow,engine->cury_shadow);
if (allowed (*board,shape,engine->curx,engine->cury + 1))
{
engine->cury++;
result = TRUE;
if (engine->shadow) place_shadow_to_bottom(*board,shape,engine->curx_shadow,&engine->cury_shadow,engine->cury);
}
if (engine->shadow) drawshadowshape (*board,shape,engine->curx_shadow,engine->cury_shadow);
drawshape (*board,shape,engine->curx,engine->cury);
return result;
}
/* Check if shape can move down (= in the air) or not (= at the bottom */
/* of the board or on top of one of the resting shapes) */
static bool shape_bottom (engine_t *engine)
{
board_t *board = &engine->board;
shape_t *shape = &engine->shapes[engine->curshape];
bool result = FALSE;
eraseshape (*board,shape,engine->curx,engine->cury);
if (engine->shadow) eraseshape (*board,shape,engine->curx_shadow,engine->cury_shadow);
result = !allowed (*board,shape,engine->curx,engine->cury + 1);
if (engine->shadow) drawshadowshape (*board,shape,engine->curx_shadow,engine->cury_shadow);
drawshape (*board,shape,engine->curx,engine->cury);
return result;
}
/* Drop the shape until it comes to rest on the bottom of the board or */
/* on top of a resting shape */
static int shape_drop (engine_t *engine)
{
board_t *board = &engine->board;
shape_t *shape = &engine->shapes[engine->curshape];
eraseshape (*board,shape,engine->curx,engine->cury);
int droppedlines = 0;
if (engine->shadow) {
drawshadowshape (*board,shape,engine->curx_shadow,engine->cury_shadow);
droppedlines = engine->cury_shadow - engine->cury;
engine->cury = engine->cury_shadow;
return droppedlines;
}
while (allowed (*board,shape,engine->curx,engine->cury + 1))
{
engine->cury++;
droppedlines++;
}
drawshape (*board,shape,engine->curx,engine->cury);
return droppedlines;
}
static int shape_undo(engine_t *engine)
{
board_t *board = &engine->board;
shape_t *shape = &engine->shapes[engine->curshape];
bool result = FALSE;
eraseshape (*board,shape,engine->curx,engine->cury);
if (engine->shadow) eraseshape (*board,shape,engine->curx_shadow,engine->cury_shadow);
memcpy(engine, &engine_save[0], sizeof(engine_t));
board = &engine->board;
shape = &engine->shapes[engine->curshape];
result = TRUE;
drawshape (*board,shape,engine->curx,engine->cury);
return result;
}
static int shape_discard(engine_t *engine)
{
board_t *board = &engine->board;
shape_t *shape = &engine->shapes[engine->curshape];
bool result = FALSE;
eraseshape (*board,shape,engine->curx,engine->cury);
if (engine->shadow) eraseshape (*board,shape,engine->curx_shadow,engine->cury_shadow);
result = TRUE;
new_block(engine);
return result;
}
/* This removes all the rows on the board that is completely filled with blocks */
static int droplines (board_t board)
{
int i,x,y,ny,status,droppedlines;
board_t newboard;
/* initialize new board */
memset (newboard,0,sizeof (board_t));
for (i = 0; i < NUMCOLS; i++) newboard[i][NUMROWS - 1] = newboard[i][NUMROWS - 2] = WALL;
for (i = 0; i < NUMROWS; i++) newboard[0][i] = newboard[NUMCOLS - 1][i] = newboard[NUMCOLS - 2][i] = WALL;
/* ... */
ny = NUMROWS - 3;
droppedlines = 0;
for (y = NUMROWS - 3; y > 0; y--)
{
status = 0;
for (x = 1; x < NUMCOLS - 2; x++) if (board[x][y]) status++;
if (status < NUMCOLS - 3)
{
for (x = 1; x < NUMCOLS - 2; x++) newboard[x][ny] = board[x][y];
ny--;
}
else droppedlines++;
}
memcpy (board,newboard,sizeof (board_t));
return droppedlines;
}
/* shuffle int array */
void shuffle (int *array, size_t n)
{
size_t i;
for (i = 0; i < n - 1; i++)
{
int range = (int)(n - i);
size_t j = i + rand_value(range);
int t = array[j];
array[j] = array[i];
array[i] = t;
}
}
/*
* Initialize specified tetris engine
*/
void engine_init (engine_t *engine,void (*score_function)(engine_t *))
{
int i;
engine->shadow = FALSE;
engine->score_function = score_function;
/* intialize values */
engine->curx = 5;
engine->cury = 1;
engine->curx_shadow = 5;
engine->cury_shadow = 1;
engine->bag_iterator = 0;
/* create and randomize bag */
for (int j = 0; j < NUMSHAPES; j++) engine->bag[j] = j;
shuffle (engine->bag,NUMSHAPES);
engine->curshape = engine->bag[engine->bag_iterator%NUMSHAPES];
engine->nextshape = engine->bag[(engine->bag_iterator+1)%NUMSHAPES];
engine->bag_iterator++;
engine->score = 0;
engine->status.moves = engine->status.rotations = engine->status.dropcount = engine->status.efficiency = engine->status.droppedlines = 0;
/* initialize shapes */
memcpy (engine->shapes,SHAPES,sizeof (shapes_t));
/* initialize board */
memset (engine->board,0,sizeof (board_t));
for (i = 0; i < NUMCOLS; i++) engine->board[i][NUMROWS - 1] = engine->board[i][NUMROWS - 2] = WALL;
for (i = 0; i < NUMROWS; i++) engine->board[0][i] = engine->board[NUMCOLS - 1][i] = engine->board[NUMCOLS - 2][i] = WALL;
memcpy(&engine_save[0], engine, sizeof(engine_t));
memcpy(&engine_save[1], engine, sizeof(engine_t));
}
/*
* Perform the given action on the specified tetris engine
*/
void engine_move (engine_t *engine,action_t action)
{
switch (action)
{
/* move shape to the left if possible */
case ACTION_LEFT:
if (shape_left (engine)) engine->status.moves++;
break;
/* rotate shape if possible */
case ACTION_ROTATE:
if (shape_rotate (engine)) engine->status.rotations++;
break;
/* move shape to the right if possible */
case ACTION_RIGHT:
if (shape_right (engine)) engine->status.moves++;
break;
/* move shape to the down if possible */
case ACTION_DOWN:
if (shape_down (engine)) engine->status.moves++;
break;
/* drop shape to the bottom */
case ACTION_DROP:
engine->status.dropcount += shape_drop (engine);
break;
case ACTION_UNDO:
shape_undo(engine);
break;
case ACTION_DISCARD:
shape_discard(engine);
}
}
static void new_block(engine_t *engine)
{
engine->curx -= 5;
engine->curx = abs (engine->curx);
engine->curx_shadow -= 5;
engine->curx_shadow = abs (engine->curx_shadow);
engine->status.rotations = 4 - engine->status.rotations;
engine->status.rotations = engine->status.rotations > 0 ? 0 : engine->status.rotations;
engine->status.efficiency += engine->status.dropcount + engine->status.rotations + (engine->curx - engine->status.moves);
engine->status.efficiency >>= 1;
engine->status.dropcount = engine->status.rotations = engine->status.moves = 0;
/* intialize values */
engine->curx = 5;
engine->cury = 1;
engine->curx_shadow = 5;
engine->cury_shadow = 1;
engine->curshape = engine->bag[engine->bag_iterator%NUMSHAPES];
/* shuffle bag before first item in bag would be reused */
if ((engine->bag_iterator+1) % NUMSHAPES == 0) shuffle(engine->bag, NUMSHAPES);
engine->nextshape = engine->bag[(engine->bag_iterator+1)%NUMSHAPES];
engine->bag_iterator++;
/* initialize shapes */
memcpy (engine->shapes,SHAPES,sizeof (shapes_t));
memcpy(&engine_save[0], &engine_save[1], sizeof(engine_t));
memcpy(&engine_save[1], engine, sizeof(engine_t));
}
/*
* Evaluate the status of the specified tetris engine
*
* OUTPUT:
* 1 = shape moved down one line
* 0 = shape at bottom, next one released
* -1 = game over (board full)
*/
int engine_evaluate (engine_t *engine)
{
if (shape_bottom (engine))
{
/* update status information */
int dropped_lines = droplines(engine->board);
engine->status.droppedlines += dropped_lines;
engine->status.currentdroppedlines = dropped_lines;
/* increase score */
engine->score_function (engine);
new_block(engine);
/* return games status */
return allowed (engine->board,&engine->shapes[engine->curshape],engine->curx,engine->cury) ? 0 : -1;
}
shape_down (engine);
return 1;
}
int engine_setshadow(engine_t *engine, bool shadow)
{
if (engine->shadow && shadow == FALSE) {
board_t *board = &engine->board;
shape_t *shape = &engine->shapes[engine->curshape];
eraseshape (*board,shape,engine->curx_shadow,engine->cury_shadow);
}
engine->shadow = shadow;
return 1;
}