First support for in-game weather (rainfall)
[tennix.git] / game.c
blob292f67e2fae15a91a185d25893ed390b426c00a5
2 /**
4 * Tennix! SDL Port
5 * Copyright (C) 2003, 2007, 2008 Thomas Perl <thp@perli.net>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
22 **/
24 #include <stdio.h>
25 #include <math.h>
27 #include "tennix.h"
28 #include "game.h"
29 #include "graphics.h"
30 #include "input.h"
31 #include "sound.h"
34 GameState *gamestate_new() {
35 int x, y, z;
36 GameState *s;
38 GameState template = {
39 { 0, 0, 0.0, 0.0, 0.0 },
40 { 0, 0, 0, 0, 0 },
41 { GAME_X_MIN-RACKET_X_MID*2, GAME_Y_MID, 0, 0, 1, DESIRE_NORMAL, PLAYER_TYPE_HUMAN, GAME_Y_MID, false, 0, {0}, 0, 0, PLAYER_ACCEL_DEFAULT, true },
42 { GAME_X_MAX+RACKET_X_MID*2, GAME_Y_MID, 0, 0, 0, DESIRE_NORMAL, PLAYER_TYPE_AI, GAME_Y_MID, false, 0, {0}, 0, 0, PLAYER_ACCEL_DEFAULT, true },
46 true,
47 "welcome to tennix " VERSION,
48 { 0 },
49 { 0 },
50 REFEREE_NORMAL,
52 WINNER_NONE,
53 false,
54 GR_CTT_GRASS,
55 -1,
56 { 0 },
58 false,
59 { { { 0 } } },
60 0.0,
61 SOUND_MAX,
62 0.0,
63 0.0,
68 s = (GameState*)malloc(sizeof(GameState));
69 if (s == NULL) abort();
71 memcpy(s, &template, sizeof(GameState));
73 game_setup_serve(s);
75 /* smoothen n-gram */
76 for( x = 0; x<NGRAM_STEPS; x++) {
77 for( y = 0; y<NGRAM_STEPS; y++) {
78 for( z = 0; z<NGRAM_STEPS; z++) {
79 s->ngram[x][y][z] = 1;
84 return s;
88 void gameloop(GameState *s) {
89 strcpy(s->game_score_str, format_game(s));
90 strcpy(s->sets_score_str, format_sets(s));
92 Uint32 ot = SDL_GetTicks();
93 Uint32 nt;
94 Uint32 dt = GAME_TICKS;
95 Uint32 diff;
96 Uint32 accumulator = 0;
97 bool quit = false;
99 #ifdef ENABLE_FPS_LIMIT
100 Uint32 ft, frames; /* frame timer and frames */
101 #endif
103 play_sample_loop(SOUND_AUDIENCE);
104 /* Reset the court type, so it is redrawn on first display */
105 s->old_court_type = -1;
107 #ifdef ENABLE_FPS_LIMIT
108 frames = 0;
109 ft = SDL_GetTicks();
110 #endif
111 while( !quit) {
112 nt = SDL_GetTicks();
113 diff = nt-ot;
114 if( diff > 2000) {
115 diff = 0;
118 accumulator += diff;
119 ot = nt;
121 while( accumulator >= dt) {
122 quit = step(s);
123 s->time += dt;
124 accumulator -= dt;
126 if( s->was_stopped) {
127 ot = SDL_GetTicks();
128 s->was_stopped = false;
132 #ifdef ENABLE_FPS_LIMIT
133 while (frames*1000.0/((float)(SDL_GetTicks()-ft+1))>(float)(DEFAULT_FPS)) {
134 SDL_Delay(10);
136 frames++;
137 #endif
139 render(s);
142 clear_screen();
143 store_screen();
145 stop_sample(SOUND_AUDIENCE);
148 bool step( GameState* s) {
149 Uint8 *keys;
150 SDL_Event e;
152 if( get_phase( s) < 1.0) {
153 if( !s->ground.jump) {
154 s->play_sound = SOUND_GROUND;
156 if( IS_OUT_Y( s->ball.y)) {
157 /* out - responsibilities stay the same */
158 s->status = "out!";
159 s->play_sound = SOUND_OUT;
160 s->referee = REFEREE_OUT;
161 } else {
162 /* not out - responsibilities change */
163 s->player1.responsible = !(s->player2.responsible = !s->player2.responsible);
164 s->status = format_status( s);
165 s->referee = REFEREE_NORMAL;
168 s->ground.jump = 3;
169 s->ground.x = s->ball.x;
170 s->ground.y = s->ball.y;
171 } else {
172 if( s->ground.jump && !(s->time%5)) s->ground.jump--;
175 if( IS_OUT_X(s->ball.x) || IS_OFFSCREEN_Y(s->ball.y)) {
176 if( IS_OFFSCREEN( s->ball.x, s->ball.y)) {
177 s->player1_serves = s->player1.responsible;
179 score_game( s, s->player2.responsible);
180 strcpy( s->game_score_str, format_game( s));
181 strcpy( s->sets_score_str, format_sets( s));
183 if( s->player1.responsible) {
184 if( s->player1.type == PLAYER_TYPE_HUMAN && s->player2.type == PLAYER_TYPE_AI) {
185 s->status = "computer scores";
186 } else {
187 s->status = "player 2 scores";
189 s->referee = REFEREE_PLAYER2;
190 } else {
191 if( s->player1.type == PLAYER_TYPE_HUMAN && s->player2.type == PLAYER_TYPE_AI) {
192 s->status = "player scores";
193 } else {
194 s->status = "player 1 scores";
196 s->referee = REFEREE_PLAYER1;
199 game_setup_serve( s);
200 s->play_sound = SOUND_APPLAUSE;
201 SDL_Delay( 500);
202 s->was_stopped = true;
203 s->history_size = 0;
204 s->history_is_locked = 0;
205 s->ngram_prediction = 0.0;
206 #ifdef DEBUG
207 printf( "-- game reset --\n");
208 #endif
211 if( IS_OUT_X(s->ball.x)) {
212 if( !s->history_is_locked && s->referee != REFEREE_OUT) {
213 s->history[s->history_size] = (int)(NGRAM_STEPS*s->ball.y/HEIGHT);
214 s->history_size++;
215 if( s->history_size == 3) {
216 s->ngram[s->history[0]][s->history[1]][s->history[2]] += 10;
217 #ifdef DEBUG
218 printf( "history: %d, %d, %d\n", s->history[0], s->history[1], s->history[2]);
219 #endif
220 s->ngram_prediction = ngram_predictor( s);
221 s->history[0] = s->history[1];
222 s->history[1] = s->history[2];
223 s->history_size--;
225 s->history_is_locked = true;
227 if( s->ball.move_x <= 0 && IS_NEAR_X( s->player1.x, s->ball.x) && IS_NEAR_Y( s->player1.y, s->ball.y) && s->player1.state && s->referee != REFEREE_OUT) {
228 s->ball.x = GAME_X_MIN;
229 if( s->player1.state == PLAYER_STATE_MAX) {
230 s->ball.move_x = PLAYER_POWERSHOT;
231 } else {
232 s->ball.move_x = 2.5 + 2.0*s->player1.state/PLAYER_STATE_MAX;
234 s->ball.move_y = get_move_y( s, 1);
235 s->player2.responsible = !(s->player1.responsible = 1);
236 s->ball.jump += 1.0-2.0*(s->player1.state<5);
237 s->play_sound = SOUND_RACKET;
238 pan_sample(SOUND_RACKET, 0.4);
239 } else if( s->ball.move_x >= 0 && IS_NEAR_X( s->player2.x, s->ball.x) && IS_NEAR_Y( s->player2.y, s->ball.y) && s->player2.state && s->referee != REFEREE_OUT) {
240 s->ball.x = GAME_X_MAX;
241 if( s->player2.state == PLAYER_STATE_MAX) {
242 s->ball.move_x = -PLAYER_POWERSHOT;
243 } else {
244 s->ball.move_x = -(2.5 + 2.0*s->player2.state/PLAYER_STATE_MAX);
246 s->ball.move_y = get_move_y( s, 2);
247 s->player1.responsible = !(s->player2.responsible = 1);
248 s->ball.jump += 1.0-2.0*(s->player2.state<5);
249 s->play_sound = SOUND_RACKET;
250 pan_sample(SOUND_RACKET, 0.6);
253 } else {
254 s->history_is_locked = false;
257 SDL_PollEvent( &e);
258 keys = SDL_GetKeyState( NULL);
259 switch(e.type) {
260 case SDL_JOYAXISMOTION:
261 if (e.jaxis.axis == JOYSTICK_Y_AXIS) {
262 s->joystick_y = JOYSTICK_PERCENTIZE(e.jaxis.value);
263 } else if (e.jaxis.axis == JOYSTICK_X_AXIS) {
264 s->joystick_x = JOYSTICK_PERCENTIZE(e.jaxis.value);
266 break;
267 case SDL_JOYBUTTONUP: case SDL_JOYBUTTONDOWN:
268 if (e.jbutton.button == JOYSTICK_BUTTON_A) {
269 s->joystick_a = (e.jbutton.state == SDL_PRESSED);
271 break;
274 if( s->time%50==0) {
276 * Maemo keys:
277 * F7 = "Decrease" key
278 * F8 = "Increase" key
280 if (keys['c'] || keys[SDLK_F8]) {
281 s->court_type++;
282 } else if (keys[SDLK_F7]) {
283 s->court_type--;
285 if (keys['r']) {
286 s->rain += 10;
288 if( s->court_type > GR_CTT_LAST) {
289 s->court_type = GR_CTT_FIRST;
290 } else if (s->court_type < GR_CTT_FIRST) {
291 s->court_type = GR_CTT_LAST;
295 if( !is_fading() && !s->is_over) {
296 if( s->player1.type == PLAYER_TYPE_HUMAN) {
297 input_human( &s->player1,
298 keys['w'] || keys[SDLK_UP] || s->joystick_y < -JOYSTICK_TRESHOLD,
299 keys['s'] || keys[SDLK_DOWN] || s->joystick_y > JOYSTICK_TRESHOLD,
300 keys['d'] || keys[SDLK_SPACE] || keys[SDLK_LCTRL] || keys[SDLK_RETURN] || s->joystick_a,
301 #ifdef ENABLE_MOUSE
302 true,
303 #else
304 false,
305 #endif
307 } else {
308 input_ai( &s->player1, &s->ball, &s->player2, s);
311 if( s->player2.type == PLAYER_TYPE_HUMAN) {
312 input_human( &s->player2, keys['o'], keys['l'], keys['k'], false, s);
313 } else {
314 input_ai( &s->player2, &s->ball, &s->player1, s);
318 /* Maemo: The "F6" button is the "Fullscreen" button */
319 if( keys['f'] || keys[SDLK_F6]) SDL_WM_ToggleFullScreen( screen);
320 if( keys['y']) SDL_SaveBMP( screen, "screenshot.bmp");
322 /* Maemo: The "F4" button is the "Open menu" button */
323 if( keys['p'] || keys[SDLK_F4]) {
324 while( keys['p'] || keys[SDLK_F4]) {
325 SDL_PollEvent( &e);
326 keys = SDL_GetKeyState( NULL);
327 SDL_Delay( 10);
329 while( (keys['p'] || keys[SDLK_F4]) == 0) {
330 SDL_PollEvent( &e);
331 keys = SDL_GetKeyState( NULL);
332 SDL_Delay( 10);
334 while( keys['p'] || keys[SDLK_F4]) {
335 SDL_PollEvent( &e);
336 keys = SDL_GetKeyState( NULL);
337 SDL_Delay( 10);
339 s->was_stopped = true;
342 if( keys[SDLK_ESCAPE] || keys['q']) return true;
344 limit_value( &s->player1.y, PLAYER_Y_MIN, PLAYER_Y_MAX);
345 limit_value( &s->player2.y, PLAYER_Y_MIN, PLAYER_Y_MAX);
346 limit_value( &s->ball.jump, BALL_JUMP_MIN, BALL_JUMP_MAX);
348 /* Update ball_dest for debugging purposes */
349 get_move_y(s, 1);
350 get_move_y(s, 2);
352 s->ball.x += s->ball.move_x;
353 s->ball.y += s->ball.move_y;
355 if(s->player1.state) s->player1.state--;
356 if(s->player2.state) s->player2.state--;
358 return false;
361 void render( GameState* s) {
362 int i, x, y;
363 if (s->play_sound != SOUND_MAX) {
364 play_sample(s->play_sound);
365 s->play_sound = SOUND_MAX;
367 if( s->winner != WINNER_NONE) {
368 if( !s->is_over) {
369 start_fade();
370 s->is_over = true;
372 clear_screen();
373 store_screen();
374 show_sprite( GR_RACKET, 2*(s->winner-1), 4, WIDTH/2 - get_image_width( GR_RACKET)/8, HEIGHT/2 - get_image_height( GR_RACKET), 255);
375 sprintf( s->game_score_str, "player %d wins the match with %s", s->winner, format_sets( s));
376 font_draw_string( GR_DKC2_FONT, s->game_score_str, (WIDTH-font_get_string_width( GR_DKC2_FONT, s->game_score_str))/2, HEIGHT/2 + 30, s->time/20, ANIMATION_WAVE | ANIMATION_BUNGEE);
377 updatescr();
378 return;
380 if (s->old_court_type != s->court_type) {
381 clear_screen();
382 fill_image(s->court_type, 120, 120, 400, 250);
383 show_image(GR_COURT, 0, 0, 255);
384 s->old_court_type = s->court_type;
385 store_screen();
387 show_sprite( GR_REFEREE, s->referee, 4, 250, 10, 255);
388 show_image( GR_SHADOW, s->ball.x-BALL_X_MID, s->ball.y + get_phase( s) - BALL_Y_MID, 255);
390 show_sprite( GR_RACKET, (!s->player1.state), 4, s->player1.x-RACKET_X_MID, s->player1.y-RACKET_Y_MID, 255);
391 show_sprite( GR_RACKET, (!s->player2.state)+2, 4, s->player2.x-RACKET_X_MID, s->player2.y-RACKET_Y_MID, 255);
393 if( s->ball.move_x > 0) {
394 show_sprite( GR_BALL, (s->time/500)%4, 4, s->ball.x-BALL_X_MID, s->ball.y-BALL_Y_MID, 255);
395 } else if( s->ball.move_x < 0) {
396 show_sprite( GR_BALL, 3-(s->time/500)%4, 4, s->ball.x-BALL_X_MID, s->ball.y-BALL_Y_MID, 255);
397 } else {
398 show_sprite( GR_BALL, 0, 4, s->ball.x-BALL_X_MID, s->ball.y-BALL_Y_MID, 255);
401 /* Player 1's mouse rectangle */
402 if (!(s->player1.mouse_locked)) {
403 rectangle(s->player1.x-2+5, s->player1.mouse_y-2, 4, 4, 255, 255, 255);
404 rectangle(s->player1.x-1+5, s->player1.mouse_y-1, 2, 2, 0, 0, 0);
407 font_draw_string( GR_DKC2_FONT, s->game_score_str, 14, 14, 0, ANIMATION_NONE);
408 font_draw_string( GR_DKC2_FONT, s->sets_score_str, (WIDTH-font_get_string_width( GR_DKC2_FONT, s->sets_score_str))-14, 14, 0, ANIMATION_NONE);
410 font_draw_string( GR_DKC2_FONT, s->status, (WIDTH-font_get_string_width( GR_DKC2_FONT, s->status))/2, HEIGHT-50, s->time/30, ANIMATION_WAVE);
412 for (i=0; i<s->rain; i++) {
413 x = rand()%WIDTH;
414 y = rand()%HEIGHT;
415 draw_line_faded(x, y, x+10, y+30, 0, 0, 255, 100, 200, 255);
417 if (s->rain) {
419 * Cheap-ish update of the whole screen. This can
420 * probably be optimized.
422 update_rect(0, 0, WIDTH, HEIGHT);
425 #ifdef DEBUG
426 line_horiz( s->player1.y, 255, 0, 0);
427 line_horiz( s->player2.y, 0, 0, 255);
428 line_horiz( s->ball.y, 0, 255, 0);
430 line_vert( s->player1.x, 255, 0, 0);
431 line_vert( s->player2.x, 0, 0, 255);
432 line_vert( s->ball.x, 0, 255, 0);
434 line_horiz( s->player1.ball_dest, 255, 0, 255);
435 line_horiz( s->player2.ball_dest, 0, 255, 255);
437 line_horiz( GAME_Y_MIN, 100, 100, 100);
438 line_horiz( GAME_Y_MAX, 100, 100, 100);
439 #endif
441 updatescr();
444 void limit_value( float* value, float min, float max) {
445 if( *value < min) {
446 *value = min;
447 } else if( *value > max) {
448 *value = max;
452 float get_phase( GameState* s) {
453 float pos, fract;
454 float x, min, max, direction;
456 x = s->ball.x;
457 min = GAME_X_MIN;
458 max = GAME_X_MAX;
459 direction = s->ball.move_x;
461 pos = (direction>0)?(1-GROUND_PHASE):(GROUND_PHASE);
463 fract = (x-min)/(max-min);
465 if( fract < pos) {
466 fract = fract/pos;
467 return fabsf( cosf(PI*fract/2))*PHASE_AMP*s->ball.jump;
468 } else {
469 fract = (pos-fract)/(1-pos);
470 return fabsf( sinf(PI*fract/2))*PHASE_AMP*s->ball.jump;
474 float get_move_y( GameState* s, unsigned char player) {
475 float pct, dest, x_len, y_len;
476 float py, by, pa, move_x;
478 py = (player==1)?(s->player1.y):(s->player2.y);
479 by = s->ball.y;
480 pa = RACKET_Y_MID*2;
481 move_x = s->ball.move_x;
483 /* -1.0 .. 1.0 for racket hit position */
484 pct = (by-py)/(pa/2);
485 limit_value( &pct, -1.0, 1.0);
487 /* Y destination for ball */
488 dest = GAME_Y_MID + pct*(GAME_Y_MAX-GAME_Y_MIN);
489 if( player == 1) {
490 s->player1.ball_dest = dest;
491 } else {
492 s->player2.ball_dest = dest;
495 /* lengths for the ball's journey */
496 if( player == 1) {
497 x_len = fabsf(GAME_X_MAX - s->ball.x);
498 y_len = dest - by + MOVE_Y_SEED-rand()%MOVE_Y_SEED*2;
499 } else {
500 x_len = s->ball.x - GAME_X_MIN;
501 y_len = by - dest + MOVE_Y_SEED-rand()%MOVE_Y_SEED*2;
504 /* return the should-be value for move_y */
505 return (y_len*move_x)/(x_len);
508 void input_human( Player* player, bool up, bool down, bool hit, bool use_mouse, GameState* s) {
509 int diff = PLAYER_MOVE_Y;
510 int mb;
513 * Only use mouse control if the user isn't pressing any buttons
514 * this way, keyboard control still works when mouse control is
515 * enabled.
517 if (use_mouse && (down || up || hit)) {
519 * this is here so if the user decides to play
520 * with keyboard controls, we will lock the
521 * mouse and disable displaying the mouse cursor
523 player->mouse_locked = true;
525 if (use_mouse && !down && !up && !hit) {
526 mb = SDL_GetMouseState(&(player->mouse_x), &(player->mouse_y));
527 if (mb&SDL_BUTTON(SDL_BUTTON_LEFT)) {
528 if (player->mouse_y < player->y) {
529 down = false;
530 up = true;
531 diff = (player->y-player->mouse_y<diff)?(player->y-player->mouse_y):(diff);
532 } else if (player->mouse_y > player->y) {
533 up = false;
534 down = true;
535 diff = (player->mouse_y-player->y<diff)?(player->mouse_y-player->y):(diff);
537 player->mouse_locked = false;
538 } else if (!player->mouse_locked) {
539 hit = true;
543 if (fabsf(s->joystick_y) > JOYSTICK_TRESHOLD) {
544 diff = PLAYER_MOVE_Y*fabsf(s->joystick_y)/40.0;
545 if (diff > PLAYER_MOVE_Y) {
546 diff = PLAYER_MOVE_Y;
550 if (up) {
551 player->y -= fminf(diff, diff*player->accelerate);
552 player->accelerate *= PLAYER_ACCEL_INCREASE;
553 } else if (down) {
554 player->y += fminf(diff, diff*player->accelerate);
555 player->accelerate *= PLAYER_ACCEL_INCREASE;
556 } else {
557 player->accelerate = PLAYER_ACCEL_DEFAULT;
560 if( hit) {
561 if( !player->state && !player->state_locked) {
562 player->state = PLAYER_STATE_MAX;
563 player->state_locked = true;
565 } else {
566 player->state_locked = false;
570 void input_ai( Player* player, Ball* ball, Player* opponent, GameState* s) {
571 float fact = 1.7;
572 float target;
574 if( fabsf( player->y - ball->y) > RACKET_Y_MID*5) {
575 fact = 3.5;
578 target = GAME_Y_MID + (opponent->ball_dest - GAME_Y_MID)/5;
580 if( player->responsible) {
581 if( player->desire == DESIRE_NORMAL && !IS_NEAR_Y_AI( player->y, ball->y)) {
582 if( player->y < ball->y) {
583 player->y += fmin( 2*fact, ball->y - player->y);
584 } else if( player->y > ball->y) {
585 player->y -= fmin( 2*fact, player->y - ball->y);
589 if( (ball->move_x != 0 || IS_NEAR_Y_AI( player->y, ball->y)) && IS_NEAR_X_AI( player->x, ball->x) && !player->state && rand()%4) {
590 player->state = PLAYER_STATE_MAX;
592 } else if( ball->move_x == 0) {
593 if( player->desire == DESIRE_NORMAL && !IS_NEAR_Y_AI( player->y, target)) {
594 if( player->y < target) {
595 player->y += fmin( fact, (target-player->y)/40.0);
596 } else if( player->y > target) {
597 player->y -= fmin( fact, (player->y-target)/40.0);
600 } else if( s->ngram_prediction > 0.0) {
601 target = s->ngram_prediction*((float)HEIGHT)/((float)(NGRAM_STEPS));
602 target = GAME_Y_MID + (target-GAME_Y_MID)*1.5;
604 if( player->desire == DESIRE_NORMAL && !IS_NEAR_Y_AI( player->y, target)) {
605 if( player->y < target) {
606 player->y += fmin( fact, (target-player->y)/40.0);
607 } else if( player->y > target) {
608 player->y -= fmin( fact, (player->y-target)/40.0);
611 } else {/*
612 if( player->desire == DESIRE_NORMAL) {
613 if( !IS_NEAR_Y_AI( player->y, target)) {
614 player->y += (target - player->y)/40.0;
620 void game_setup_serve( GameState* s) {
621 s->ball.jump = 7.5;
622 s->ball.y = GAME_Y_MID;
623 s->ball.move_x = 0.0;
624 s->ball.move_y = 0.0;
626 if( s->player1_serves) {
627 s->player1.responsible = true;
628 s->player1.ball_dest = 0.0;
629 s->ball.x = GAME_X_MIN-RACKET_X_MID*1.5;
630 } else {
631 s->player1.responsible = false;
632 s->player2.ball_dest = 0.0;
633 s->ball.x = GAME_X_MAX+RACKET_X_MID*1.5;
636 s->player2.responsible = !(s->player1.responsible);
639 float ngram_predictor( GameState* s) {
640 unsigned int count = 0;
641 unsigned long sum = 0;
642 int x, y, z;
643 float result;
645 if( s->history_size < 3) {
646 return 0.0;
649 x = s->history[1];
650 y = s->history[2];
652 for( z = 0; z<NGRAM_STEPS; z++) {
653 count += s->ngram[x][y][z];
654 sum += z * s->ngram[x][y][z];
657 result = ((float)(sum))/((float)(count));
658 #ifdef DEBUG
659 printf( "predicting next = %.2f\n", result);
660 #endif
662 return result;
665 void score_game( GameState* s, bool player1_scored) {
666 Player* winner = (player1_scored)?(&(s->player1)):(&(s->player2));
667 Player* loser = (player1_scored)?(&(s->player2)):(&(s->player1));
669 if( s->current_set >= SETS_TO_WIN*2-1) {
670 return;
673 winner->game++;
674 if( loser->game < winner->game-1) {
675 if( winner->game >= 4) {
676 winner->game = loser->game = 0;
677 winner->sets[s->current_set]++;
678 /* scoring the set.. */
679 if( (winner->sets[s->current_set] == 6 && loser->sets[s->current_set] < 5) ||
680 winner->sets[s->current_set] == 7) {
681 s->current_set++;
682 s->winner = game_get_winner( s);
688 char* format_sets( GameState* s) {
689 static char sets[100];
690 static char tmp[100];
691 int i, max = s->current_set;
693 sets[0] = '\0';
695 if( s->winner != WINNER_NONE) {
696 max--;
698 for( i=0; i<=max; i++) {
699 sprintf( tmp, "%d:%d, ", s->player1.sets[i], s->player2.sets[i]);
700 strcat( sets, tmp);
703 sets[strlen(sets)-2] = '\0';
705 return sets;
708 char* format_game( GameState* s) {
709 static char game[100];
710 static const int game_scoring[] = { 0, 15, 30, 40 };
712 if( s->player1.game < 4 && s->player2.game < 4) {
713 sprintf( game, "%d - %d", game_scoring[s->player1.game], game_scoring[s->player2.game]);
714 } else if( s->player1.game > s->player2.game) {
715 strcpy( game, "advantage player 1");
716 } else if( s->player1.game < s->player2.game) {
717 strcpy( game, "advantage player 2");
718 } else {
719 strcpy( game, "deuce");
722 return game;
725 char* format_status( GameState* s) {
726 static char status[100];
727 static const char* set_names[] = { "first", "second", "third", "fourth", "fifth" };
729 sprintf( status, "%d:%d in %s set", s->player1.sets[s->current_set], s->player2.sets[s->current_set], set_names[s->current_set]);
731 return status;
734 int game_get_winner( GameState* s) {
735 int i;
736 int sets[2] = {0};
738 for( i=0; i<s->current_set; i++) {
739 if( s->player1.sets[i] > s->player2.sets[i]) {
740 sets[0]++;
741 } else {
742 sets[1]++;
746 if( sets[0] == SETS_TO_WIN) return WINNER_PLAYER1;
747 if( sets[1] == SETS_TO_WIN) return WINNER_PLAYER2;
749 return WINNER_NONE;