-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
1826 lines (1579 loc) · 36.3 KB
/
main.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/soundcard.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <inttypes.h>
#ifndef NULL
#define NULL ((void*)0)
#endif
/* grid dimension : 10x18 */
/**
* \def WIDTH
* \brief Total width of the board
*/
#define WIDTH 19
/**
* \def MIN
* \brief Minimum of two values
*/
#define MIN(a, b) ((a) < (b) ? (a) : (b))
/**
* \def MAX
* \brief Maximum of two values
*/
#define MAX(a, b) ((a) > (b) ? (a) : (b))
/**
* \def HEIGHT
* \brief Total height of the board including player's information column
*/
#define HEIGHT 19
/**
* \def INITIAL_PERIOD
* \brief Number of cycles it takes a piece to drop of 1
*/
#define INITIAL_PERIOD 100
/**
* \def INTER_FRAME
* \brief Approximate inter-frame duration
*/
#define INTER_FRAME 11500
/**
* \def BUF_SIZE
* \brief Size of the audio buffers
*/
#define BUF_SIZE 1024
/**
* \def SFX
* \brief Directory which contains sfx raw 8 bits unsigned audio sfx
*/
#define SFX "sound/sfx/"
/**
* \def GAME_B_LINES
* \brief Number of lines to complete in game b
*/
#define GAME_B_LINES 25
/* Network modes */
/**
* \def NET_NONE
* \brief Single player mode
*/
#define NET_NONE 0
/**
* \def NET_SERVER
* \brief Two player mode, server
*/
#define NET_SERVER 1
/**
* \def NET_CLIENT
* \brief Two player mode, client
*/
#define NET_CLIENT 2
/**
* \def NET_DEFAULT_PORT
* \brief Default port used for two player mode
*/
#define NET_DEFAULT_PORT "37280"
/* Message codes are on the first (big) 3 bytes, value on the last five */
/**
* \def MSG_CODE
* \brief Extracts the code part of a network message
*/
#define MSG_CODE(msg) (0xE0 & (msg))
/**
* \def MSG_VALUE
* \brief Extracts the value part of a network message
*/
#define MSG_VALUE(msg) (0x1F & (msg))
/**
* \def MSG_HEIGHT
* \brief Network message indicating the current height the player is at
*/
#define MSG_HEIGHT 0x00
/**
* \def MSG_LINES
* \brief Network message to send lines to the other player
*/
#define MSG_LINES 0x20
/**
* \def MSG_LOST
* \brief Network message to indicate that the player has lost
*/
#define MSG_LOST 0x40
/**
* \def MSG_QUIT
* \brief Network message to indicate that the player has quit
*/
#define MSG_QUIT 0x60
/**
* \def MSG_PAUSE
* \brief Network message to indicate that the player has paused the game
*/
#define MSG_PAUSE 0x80
/**
* \def MSG_BUILD
* \brief Builds a message by packing together the message code an the
* associated value
*/
#define MSG_BUILD(code, value) ((char)((code) | (value)))
/**
* \var period
* \brief Array of the periods which define the speed of the game
* TODO tune that, and make it last up to lvl... hum 30 ?
*/
static const int period[] = {100, 87, 75, 64, 54, 45, 37, 30, 24, 19,
15, 12, 10, 9, 8, 7, 6, 5, 4, 3};
/**
* \var clear
* \brief Console escape sequence for clearing the terminal
*/
static const char clear[] = {0x1b, 0x5b, 0x48, 0x1b, 0x5b, 0x4a, 0};
/**
* \var civis
* \brief Console escape sequence to make the cursor invisible
*/
static const char civis[] = {0x1b, 0x5b, 0x3f, 0x32, 0x35, 0x6c, 0};
/**
* \var cnorm
* \brief Console escape sequence to make the cursor visible
*/
static const char cnorm[] = {
0x1b, 0x5b, 0x33, 0x34, 0x68, 0x1b, 0x5b, 0x3f, 0x32, 0x35, 0x68, 0
};
/**
* \var sgr0
* \brief Console escape sequence to put back the font fore and back ground
* colors to normal
*/
static const char sgr0[] = {0x1b, 0x5b, 0x6d, 0x0f, 0};
/**
* \var board
* \brief Complete board containing the playing grid and the user informations
*/
static char board[19][20] = {
"* ********",
"* *score**",
"* ********",
"* * 0*",
"* ********",
"* *level**",
"* ********",
"* * 0*",
"* ********",
"* *lines**",
"* ********",
"* * 0*",
"* ********",
"* *** *",
"* *** *",
"* *** *",
"* *** *",
"* ********",
"*******************",
};
/**
* \enum end_status
* \brief status when the game ends
*/
enum end_status {
END_NONE, /**< The game is still on going */
END_WON, /**< The player won the game, in mode 2 or b */
END_LOST, /**< The player has lost */
END_QUIT, /**< The player has requested to quit the game */
END_PEER_LEFT, /**< The remote has quit in a 2 player game */
};
/**
* \var game
* \brief Main structure representing the game's current state
*/
struct {
char mode; /**< Game mode, 'a', 'b' or '2' (for two players) */
int high; /**< Height of the handicap */
int lvl; /**< Speed of pieces dropping */
int lines; /**< Number of lines achieved so far */
/**< Indexes of the lines that have been completed */
int comp_lines[4];
int score; /**< Current score of the player */
int period; /**< Number of loop passes between two automatic drops */
int pause; /**< If true, the game is suspended */
int height; /**< Current height of the game, for network mode only */
int void_col; /**< Index of the void column for penalties */
int loop; /**< Non-zero while in main loop */
int freeze; /**< Number of frame when a down can't occur (key delay) */
int music; /**< Non zero if music is enabled */
int dsp; /**< File descriptor of sound card */
int bgm; /**< File descriptor of background music */
int sfx; /**< Sfx currently playing, -1 if none */
int suspended;/**< Number of frame during when the game is suspended */
/** status when the game is finished */
enum end_status status;
/** audio buffer */
char snd_buf[BUF_SIZE];
/** length of the data to put into the audio buffer */
size_t chunk_len;
} game = {
.mode = 'a',
.high = 0,
.lvl = 0,
.lines = 0,
.comp_lines = {-1, -1, -1, -1},
.score = 0,
.period = INITIAL_PERIOD,
.pause = 0,
.height = 0,
.void_col = 0,
.loop = 1,
.freeze = 0,
.music = 0,
.dsp = -1,
.bgm = -1,
.sfx = -1,
.status = END_NONE,
.suspended = 0,
.snd_buf = {0},
.chunk_len = 0,
};
/**
* \var net
* \brief Network related informations. Not used if mode if NET_NONE (single
* player)
*/
struct {
int mode; /**< One of NET_NONE, NET_SERVER and NET_CLIENT */
char *addr; /**< Dotted numerical address of the server */
char *port; /**< Port number of the connection */
int sfd; /**< Socket of the server, only used by the server */
int fd; /**< socket of the remote host */
int pending_lines; /**< Number of lines to send to the remote */
} net = {
.mode = NET_NONE,
.addr = "localhost",
.port = NET_DEFAULT_PORT,
.sfd = -1,
.fd = -1,
.pending_lines = 0,
};
/**
* \typedef image
* \brief type of the image a given rotation of a piece. One image is a rotation
* of the basic piece, it is stored as a binary bitmap, in 2 bytes. For example,
* the first rotation of the T piece will give :
* 0100 4
* 1100 C
* 0100 4
* 0000 0
* hence 0x4C40
*/
typedef const uint16_t image;
/**
* \def PIXEL_LIT
* \brief Returns true if the pixel at the coordinates (x, y) is lit. im is a
* pointer on an image
*/
#define PIXEL_LIT(im, x, y) ((1 << ((3 - x) + 4 * (3 - y))) & ((im)))
/**
* \def GET_IMG
* \brief Returns the image corresponding to a given orientation for a given
* piece
*/
#define GET_IMG(scl, piece, ori) ((*((scl)[(piece)]))[(ori)])
/**
* \def VALID_IMG
* \brief Decides if the image pointed is valid or not
*/
#define VALID_IMG(im) ((im) != 0X0000)
typedef const image sprite[5];
/**
* \var A
* \brief Line shaped piece
*/
static const sprite A = {0x00F0, 0x4444, 0x0000, 0x0000, 0x0000};
/**
* \var B
* \brief Block shaped piece
*/
static const sprite B = {0x0660, 0x0000, 0x0000, 0x0000, 0x0000};
/**
* \var C
* \brief Tee shaped piece
*/
static const sprite C = {0x0E40, 0x4C40, 0x4E00, 0x4640, 0x0000};
/**
* \var D
* \brief S shaped piece
*/
static const sprite D = {0x06C0, 0x8C40, 0x0000, 0x0000, 0x0000};
/**
* \var E
* \brief Z shaped piece
*/
static const sprite E = {0x0C60, 0x4C80, 0x0000, 0x0000, 0x0000};
/**
* \var F
* \brief L shaped piece
*/
static const sprite F = {0x0E80, 0xC440, 0x2E00, 0x4460, 0x0000};
/**
* \var G
* \brief J shaped piece
*/
static const sprite G = {0x0E20, 0x44C0, 0x8E00, 0x6440, 0x0000};
sprite const *scale[7] = {&A, &B, &C, &D, &E, &F, &G};
struct {
int piece;
int next_piece;
int ori;
int next_ori;
int x;
int next_x;
int y;
int next_y;
int hit;
} current = {
.y = 0,
.next_y = 0,
.x = 3,
.next_x = 3,
.ori = 0,
.next_ori = 0,
.hit = 0,
};
/**
* \def WRITE
* \brief Writes a single char to standard output
*/
#define WRITE(c) do { \
char _buf = (char)(c); \
if (write(1, &_buf, 1)); \
} while (0)
/**
* @brief Writes a 0 terminated set of char to standard output
* @param s Set of char ending with a 0
*/
#define WRITES(s) do { \
/* size_t _i = 0; */\
unsigned _i = 0; \
while (s[_i]) \
_i++; \
if (write(1, s, _i)); \
} while(0)
enum sfx {
SFX_DROP, /**< */
SFX_GRID_DROP, /**< */
SFX_LINE, /**< */
SFX_LOST, /**< */
SFX_MOVE, /**< */
SFX_PAUSE, /**< */
SFX_ROTATION, /**< */
SFX_TETRIS, /**< */
SFX_WIN, /**< */
SFX_NB, /**< */
};
static struct {
const char *path;
int fd;
} sfx_file[] = {
{SFX"Drop.raw", -1},
{SFX"Grid_drop.raw", -1},
{SFX"Line.raw", -1},
{SFX"Lost.raw", -1},
{SFX"Move.raw", -1},
{SFX"Pause.raw", -1},
{SFX"Rotation.raw", -1},
{SFX"Tetris.raw", -1},
{SFX"Win.raw", -1},
};
/**
* Sets an sfx wating for playing, resetting any non fully played previous sfx
*/
void play_sfx(enum sfx fx) {
if (-1 != game.sfx)
lseek(game.sfx, 0, SEEK_SET);
game.sfx = sfx_file[fx].fd;
}
/**
* Place the cursor at a given position
* @param x Abscissa in [0,98]
* @param y Ordinate in [0,98]
*/
inline void put_cur(int x, int y) {
x++;
y++;
WRITE(0x1b);
WRITE(0x5b);
if (y / 10) {
WRITE(y / 10 + '0');
}
WRITE((y % 10) + '0');
WRITE(0x3b);
if (x / 10) {
WRITE(x / 10 + '0');
}
WRITE((x % 10) + '0');
WRITE(0x48);
}
/**
* Cleanups the terminal state, re-displays the cursor, restores the color.
*/
void cleanup() {
WRITES(cnorm);
WRITE('\n');
WRITES(sgr0);
WRITES(clear);
}
/**
* Writes a whitespace of a given background color
* @param color Color to put at the current position
*/
void put_color(int color) {
char block[] = "\033[22;30m\033[22;40m \033[m\x0f";
char black[] = "\033[m\x0f ";
char *seq = color ? block : black;
if (color) {
seq[6] = (char)('0' + color);
seq[14] = seq[6];
}
WRITES(seq);
}
/**
* Hides the next piece indicator
*/
void hide_next() {
int x = 0;
int y = 0;
for (x = 14; x < 18; x++) {
for (y = 13; y < 17; y++) {
put_cur(x, y);
put_color(0);
}
}
}
/**
* Re-draws or hides all the board.
* @param hide If true, the board content is masked, otherwise it is refreshed
*/
void refresh_board(int hide) {
int x = 0;
int y = 0;
for (x = 1; x < 11; x++) {
for (y = 0; y < 18; y++) {
put_cur(x, y);
if (hide)
put_color(5);
else {
if (' ' == board[y][x])
put_color(0);
else
put_color(board[y][x] - '0');
}
}
}
}
/**
* Writes a number at a givent position, starting from it's right
* @param x Abscissa where the number will be written to, starting from the
* right
* @param y Ordinate where the number will be written to
* @param number Number to write
*/
void print_number(int x, int y, int number) {
if (0 == number) {
put_cur(x, y);
WRITE('0');
} else {
while (number) {
put_cur(x--, y);
WRITE('0' + (number % 10));
number /= 10;
}
}
}
/**
* Writes all the board, except the playground
*/
void print_board() {
int x = 0;
int y = 0;
for (x = 0; x < WIDTH; x++) {
for (y = 0; y < HEIGHT; y++) {
put_cur(x, y);
if ('*' == board[y][x])
put_color(7);
else
WRITE(board[y][x]);
}
}
if ('b' == game.mode) {
put_cur(12, 1);
WRITES("high ");
print_number(17, 11, game.lines);
print_number(17, 3, game.high);
}
print_number(17, 7, game.lvl);
refresh_board(0);
}
/**
* Custom implementation of the libc time()
* @param t If non-NULL, store the returned value
* @return Number of elapsed seconds elapsed since the epoch
*/
time_t time(time_t *t) {
struct timeval tv;
int ret;
ret = gettimeofday(&tv, NULL);
if (-1 == ret)
return (time_t)-1;
if (NULL != t)
*t = tv.tv_sec;
return tv.tv_sec;
}
/**
* Pseudo-random generator
* @param seed if 0, ask for a number, else, set the new seed
* @return Next number generated, in [0, 2^30)
*/
int my_random(int seed) {
static int prev = 0;
if (seed)
prev = seed;
prev = 1103515245 * prev + 12345;
return prev & ((1 << 30) - 1);
}
/**
* Draws/erases a piece
* @param piece Piece to draw
* @param ori Orientation of the piece
* @param x Abscissa where to place the piece
* @param y Ordinate where to place the piece
* @param draw if 0, erases the piece, otherwise, draws it
*/
void draw_piece(int piece, int ori, int x, int y, int draw) {
int i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (PIXEL_LIT(GET_IMG(scale, piece, ori), i, j)) {
put_cur(x + i, y + j);
put_color(draw ? piece + 1 : 0);
}
}
/**
* Draws / erase the current piece in the play ground
* @param draw 0 to erase, non-zero to draw it
*/
void draw_current_piece(int draw) {
draw_piece(current.piece, current.ori, 1 + current.x, current.y, draw);
put_cur(80, 80);
}
/**
* Draws / erase the next piece in the next piece indicator
* @param draw 0 to erase, non-zero to draw it
*/
void draw_next_piece(int draw) {
draw_piece(current.next_piece, 0, 14, 13, draw);
}
/**
* Adds the piece to the play ground, once it has hit
*/
void fix_piece() {
int i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (PIXEL_LIT(GET_IMG(scale, current.piece, current.ori), i, j))
board[current.next_y + j][1 + current.next_x + i] = (char)('1' + current.piece);
}
/**
* Check wether the piece at the next position, collides with fixed block or not
* @return 1 if the next position is possible, otherwise 0
*/
int can_move() {
int i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (PIXEL_LIT(GET_IMG(scale, current.piece, current.next_ori), i, j) &&
' ' != board[current.next_y + j][1 + current.next_x + i])
if (current.next_y + j <= 18 &&
current.next_y + j >= 0 &&
current.next_x + i <= 11)
return 0;
return 1;
}
/**
* Effectively make the piece move, by setting the next position as the current
* one
*/
void move() {
if (game.music) {
if (current.ori != current.next_ori)
play_sfx(SFX_ROTATION);
else if (current.x != current.next_x)
play_sfx(SFX_MOVE);
}
draw_current_piece(0);
current.x = current.next_x;
current.y = current.next_y;
current.ori = current.next_ori;
draw_current_piece(1);
}
/**
* Cancels the move if it would collide fixed blocks
*/
void cancel_move() {
if (current.next_y != current.y) {
current.hit = 1;
current.next_y = current.y;
} else {
current.next_x = current.x;
current.next_ori = current.ori;
}
}
/**
* Performs a move if it is possible, othewise, cancel it
* @return 1 if the move is valid, 0 otherwise
*/
int try_move() {
int moved = can_move();
if (moved)
move();
else
cancel_move();
return moved;
}
/**
* Makes the current piece try to go down of one step
* @return 1 if the move is valid, 0 otherwise
*/
int down() {
current.next_y++;
return try_move();
}
/**
* Chooses the next piece and updates the next piece indicator
*/
void get_next() {
draw_next_piece(0);
current.piece = current.next_piece;
current.next_piece = my_random(0) % 7;
current.x = 3;
current.next_x = 3;
current.y = 0;
current.next_y = 0;
current.ori = 0;
current.next_ori = 0;
if (0 == current.piece) {
current.y--;
current.next_y--;
}
draw_next_piece(1);
}
/**
* For two player (lan) mode, when a player has cleared more than one
* line at once, adds the other penality lines.
* @param pending_lines Number of lines to add at the bottom of the board
*/
void add_lines(int pending_lines) {
int i, j;
for (j = 0; j < 18 - pending_lines; j++)
for (i = 1; i < 11; i++)
board[j][i] = board[j + pending_lines][i];
for (j = 18 - pending_lines; j < 18; j++)
for (i = 1; i < 11; i++)
board[j][i] = i != game.void_col ? '1' : ' ';
refresh_board(0);
}
/**
* In 2 player mode, updates the gauge showing the height of the other player
* @param value Height reached by the other player
*/
void update_gauge(int value) {
int j;
for (j = 0; j < value; j++) {
put_cur(18, j);
put_color(7);
}
for (; j < 18; j++) {
put_cur(18, j);
put_color(3);
}
}
/**
* Sends a 2 player network message to the remote peer
* @param code Code of the tetris message to send
* @param value Value associated, used only by MSG_LINES and
* MSG_HEIGHT, ignored otherwise
* @return -1 in case of error, otherwise a positive value
*/
int send_msg(int code, int value) {
if (net.mode) {
char msg = MSG_BUILD(code, value);
return write(net.fd, &msg, sizeof(char));
}
return -1;
}
/**
* Hides the board and prints a message of 10 char max, with given for and
* background colors
* @param msg Message of 10 chars max
* @param fore Foreground color
* @param back Background color
*/
void print_msg(char *msg, int fore, int back) {
char f = (char)(MAX(0, MIN(7, fore)) + '0');
char b = (char)(MAX(0, MIN(7, back)) + '0');
refresh_board(1);
put_cur(1, 5);
WRITES("\033[30;3");
WRITE(f);
WRITES("m\033[22;4");
WRITE(b);
WRITES("m ");
put_cur(1, 6);
WRITES(msg);
put_cur(1, 7);
WRITES(" \033[m\x0f");
}
/**
* Suspends/restarts the game, and prints a pause message
*/
void in_pause() {
game.pause = !game.pause;
if (game.pause) {
play_sfx(SFX_PAUSE);
print_msg("* pause! *", 5, 3);
hide_next();
} else {
refresh_board(0);
draw_current_piece(1);
draw_next_piece(1);
}
}
/**
* Checks if the remote sent a message and acts accordingly
* @param pending_lines Number of penalty lines, updated in output if the remote
* did multiples lines
* @param loop in output, set to 0 if a message which stops the game has been
* received, i.e. QUIT or LOST
* @return -1 in case of error, otherwise 0
*/
int read_msg(int *pending_lines, int *loop, char *msg) {
int ret = -1;
ret = read(net.fd, msg, 1);
switch (ret) {
case -1:
if (errno != EAGAIN) {
WRITES("error : read");
return -1;
}
break;
case 1:
switch (MSG_CODE(*msg)) {
case MSG_HEIGHT:
put_cur(25, 25);
update_gauge(MSG_VALUE(*msg));
break;
case MSG_LINES:
*pending_lines += MSG_VALUE(*msg);
break;
case MSG_LOST:
*loop = 0;
game.status = END_WON;
play_sfx(SFX_WIN);
break;
case MSG_QUIT:
*loop = 0;
game.status = END_PEER_LEFT;
break;
case MSG_PAUSE:
in_pause();
break;
default:
WRITES("error : Unknown message\n");
return -1;
break;
}
break;
default:
break;
}
return 0;
}
/**
* Computes the current game's height and sends it to the remote
*/
void update_height() {
int i, j;
int old_height = game.height;
game.height = 0;
for (j = 17; j >= 0; j--)
for (i = 1; i < 11; i++)
if (' ' != board[j][i])
game.height = j;
if (game.height != old_height)
send_msg(MSG_HEIGHT, game.height);
}
/**
* Deletes a given line which is known to have been completed. Updates and
* prints the lines counter and the level and updates the game speed accordingly
* @param line Index of the line to clear from top to bottom starting by 0
*/
void complete_line(int line) {
int i;
/* clear the line */
while (line--)
for (i = 1; i < 11; i++) {
board[line + 1][i] = board[line][i];
put_cur(i, line + 1);
if (' ' == board[line + 1][i])
put_color(0);
else
put_color(board[line + 1][i] - '0');
}
/* update lines, level and period (game speed) */
if (game.mode == 'b') {
game.lines--;
if (0 > game.lines)
game.lines = 0;
} else {
game.lines++;
if (game.lines % 10 == 0) {
int real_lvl = game.lines / 10;
game.lvl = real_lvl > game.lvl ? real_lvl : game.lvl;
print_number(17, 7, game.lvl);
}
}
print_number(17, 11, game.lines);
if (9 == game.lines) {/* quirk for erasing leading 1, when < 10 */
put_cur(16, 11);
WRITE(' ');
}
game.period = period[game.lvl];
}
/**
* Check if one or more lines have been completed. If there are some, cleans
* them, updates the score and send a message to the remote in case of multiple
* lines.
*/
int check_lines() {
int i, j;
int line_complete = 1;
int total = 0;
for (j = 0; j < 18; j++) {
for (i = 1; i < 11; i++) {
if (board[j][i] == ' ') {
line_complete = 0;
break;
}