Add state machine for main menu + options menu
[tennix.git] / game.c
blob6f861c1567e775604bbdd3a0be366a2995c438c4
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,
67 s = (GameState*)malloc(sizeof(GameState));
68 if (s == NULL) abort();
70 memcpy(s, &template, sizeof(GameState));
72 game_setup_serve(s);
74 /* smoothen n-gram */
75 for( x = 0; x<NGRAM_STEPS; x++) {
76 for( y = 0; y<NGRAM_STEPS; y++) {
77 for( z = 0; z<NGRAM_STEPS; z++) {
78 s->ngram[x][y][z] = 1;
83 return s;
87 void gameloop(GameState *s) {
88 strcpy(s->game_score_str, format_game(s));
89 strcpy(s->sets_score_str, format_sets(s));
91 Uint32 ot = SDL_GetTicks();
92 Uint32 nt;
93 Uint32 dt = GAME_TICKS;
94 Uint32 diff;
95 Uint32 accumulator = 0;
96 bool quit = false;
98 #ifdef ENABLE_FPS_LIMIT
99 Uint32 ft, frames; /* frame timer and frames */
100 #endif
102 play_sample_loop(SOUND_AUDIENCE);
103 /* Reset the court type, so it is redrawn on first display */
104 s->old_court_type = -1;
106 #ifdef ENABLE_FPS_LIMIT
107 frames = 0;
108 ft = SDL_GetTicks();
109 #endif
110 while( !quit) {
111 nt = SDL_GetTicks();
112 diff = nt-ot;
113 if( diff > 2000) {
114 diff = 0;
117 accumulator += diff;
118 ot = nt;
120 while( accumulator >= dt) {
121 quit = step(s);
122 s->time += dt;
123 accumulator -= dt;
125 if( s->was_stopped) {
126 ot = SDL_GetTicks();
127 s->was_stopped = false;
131 #ifdef ENABLE_FPS_LIMIT
132 while (frames*1000.0/((float)(SDL_GetTicks()-ft+1))>(float)(DEFAULT_FPS)) {
133 SDL_Delay(10);
135 frames++;
136 #endif
138 render(s);
141 clear_screen();
142 store_screen();
144 stop_sample(SOUND_AUDIENCE);
147 bool step( GameState* s) {
148 Uint8 *keys;
149 SDL_Event e;
151 if( get_phase( s) < 1.0) {
152 if( !s->ground.jump) {
153 s->play_sound = SOUND_GROUND;
155 if( IS_OUT_Y( s->ball.y)) {
156 /* out - responsibilities stay the same */
157 s->status = "out!";
158 s->play_sound = SOUND_OUT;
159 s->referee = REFEREE_OUT;
160 } else {
161 /* not out - responsibilities change */
162 s->player1.responsible = !(s->player2.responsible = !s->player2.responsible);
163 s->status = format_status( s);
164 s->referee = REFEREE_NORMAL;
167 s->ground.jump = 3;
168 s->ground.x = s->ball.x;
169 s->ground.y = s->ball.y;
170 } else {
171 if( s->ground.jump && !(s->time%5)) s->ground.jump--;
174 if( IS_OUT_X(s->ball.x) || IS_OFFSCREEN_Y(s->ball.y)) {
175 if( IS_OFFSCREEN( s->ball.x, s->ball.y)) {
176 s->player1_serves = s->player1.responsible;
178 score_game( s, s->player2.responsible);
179 strcpy( s->game_score_str, format_game( s));
180 strcpy( s->sets_score_str, format_sets( s));
182 if( s->player1.responsible) {
183 if( s->player1.type == PLAYER_TYPE_HUMAN && s->player2.type == PLAYER_TYPE_AI) {
184 s->status = "computer scores";
185 } else {
186 s->status = "player 2 scores";
188 s->referee = REFEREE_PLAYER2;
189 } else {
190 if( s->player1.type == PLAYER_TYPE_HUMAN && s->player2.type == PLAYER_TYPE_AI) {
191 s->status = "player scores";
192 } else {
193 s->status = "player 1 scores";
195 s->referee = REFEREE_PLAYER1;
198 game_setup_serve( s);
199 s->play_sound = SOUND_APPLAUSE;
200 SDL_Delay( 500);
201 s->was_stopped = true;
202 s->history_size = 0;
203 s->history_is_locked = 0;
204 s->ngram_prediction = 0.0;
205 #ifdef DEBUG
206 printf( "-- game reset --\n");
207 #endif
210 if( IS_OUT_X(s->ball.x)) {
211 if( !s->history_is_locked && s->referee != REFEREE_OUT) {
212 s->history[s->history_size] = (int)(NGRAM_STEPS*s->ball.y/HEIGHT);
213 s->history_size++;
214 if( s->history_size == 3) {
215 s->ngram[s->history[0]][s->history[1]][s->history[2]] += 10;
216 #ifdef DEBUG
217 printf( "history: %d, %d, %d\n", s->history[0], s->history[1], s->history[2]);
218 #endif
219 s->ngram_prediction = ngram_predictor( s);
220 s->history[0] = s->history[1];
221 s->history[1] = s->history[2];
222 s->history_size--;
224 s->history_is_locked = true;
226 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) {
227 s->ball.x = GAME_X_MIN;
228 if( s->player1.state == PLAYER_STATE_MAX) {
229 s->ball.move_x = PLAYER_POWERSHOT;
230 } else {
231 s->ball.move_x = 2.5 + 2.0*s->player1.state/PLAYER_STATE_MAX;
233 s->ball.move_y = get_move_y( s, 1);
234 s->player2.responsible = !(s->player1.responsible = 1);
235 s->ball.jump += 1.0-2.0*(s->player1.state<5);
236 s->play_sound = SOUND_RACKET;
237 pan_sample(SOUND_RACKET, 0.4);
238 } 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) {
239 s->ball.x = GAME_X_MAX;
240 if( s->player2.state == PLAYER_STATE_MAX) {
241 s->ball.move_x = -PLAYER_POWERSHOT;
242 } else {
243 s->ball.move_x = -(2.5 + 2.0*s->player2.state/PLAYER_STATE_MAX);
245 s->ball.move_y = get_move_y( s, 2);
246 s->player1.responsible = !(s->player2.responsible = 1);
247 s->ball.jump += 1.0-2.0*(s->player2.state<5);
248 s->play_sound = SOUND_RACKET;
249 pan_sample(SOUND_RACKET, 0.6);
252 } else {
253 s->history_is_locked = false;
256 SDL_PollEvent( &e);
257 keys = SDL_GetKeyState( NULL);
258 switch(e.type) {
259 case SDL_JOYAXISMOTION:
260 if (e.jaxis.axis == JOYSTICK_Y_AXIS) {
261 s->joystick_y = JOYSTICK_PERCENTIZE(e.jaxis.value);
262 } else if (e.jaxis.axis == JOYSTICK_X_AXIS) {
263 s->joystick_x = JOYSTICK_PERCENTIZE(e.jaxis.value);
265 break;
266 case SDL_JOYBUTTONUP: case SDL_JOYBUTTONDOWN:
267 if (e.jbutton.button == JOYSTICK_BUTTON_A) {
268 s->joystick_a = (e.jbutton.state == SDL_PRESSED);
270 break;
273 if( s->time%50==0) {
275 * Maemo keys:
276 * F7 = "Decrease" key
277 * F8 = "Increase" key
279 if (keys['c'] || keys[SDLK_F8]) {
280 s->court_type++;
281 } else if (keys[SDLK_F7]) {
282 s->court_type--;
284 if( s->court_type > GR_CTT_LAST) {
285 s->court_type = GR_CTT_FIRST;
286 } else if (s->court_type < GR_CTT_FIRST) {
287 s->court_type = GR_CTT_LAST;
291 if( !is_fading() && !s->is_over) {
292 if( s->player1.type == PLAYER_TYPE_HUMAN) {
293 input_human( &s->player1,
294 keys['w'] || keys[SDLK_UP] || s->joystick_y < -JOYSTICK_TRESHOLD,
295 keys['s'] || keys[SDLK_DOWN] || s->joystick_y > JOYSTICK_TRESHOLD,
296 keys['d'] || keys[SDLK_SPACE] || keys[SDLK_LCTRL] || keys[SDLK_RETURN] || s->joystick_a,
297 #ifdef ENABLE_MOUSE
298 true,
299 #else
300 false,
301 #endif
303 } else {
304 input_ai( &s->player1, &s->ball, &s->player2, s);
307 if( s->player2.type == PLAYER_TYPE_HUMAN) {
308 input_human( &s->player2, keys['o'], keys['l'], keys['k'], false, s);
309 } else {
310 input_ai( &s->player2, &s->ball, &s->player1, s);
314 /* Maemo: The "F6" button is the "Fullscreen" button */
315 if( keys['f'] || keys[SDLK_F6]) SDL_WM_ToggleFullScreen( screen);
316 if( keys['y']) SDL_SaveBMP( screen, "screenshot.bmp");
318 /* Maemo: The "F4" button is the "Open menu" button */
319 if( keys['p'] || keys[SDLK_F4]) {
320 while( keys['p'] || keys[SDLK_F4]) {
321 SDL_PollEvent( &e);
322 keys = SDL_GetKeyState( NULL);
323 SDL_Delay( 10);
325 while( (keys['p'] || keys[SDLK_F4]) == 0) {
326 SDL_PollEvent( &e);
327 keys = SDL_GetKeyState( NULL);
328 SDL_Delay( 10);
330 while( keys['p'] || keys[SDLK_F4]) {
331 SDL_PollEvent( &e);
332 keys = SDL_GetKeyState( NULL);
333 SDL_Delay( 10);
335 s->was_stopped = true;
338 if( keys[SDLK_ESCAPE] || keys['q']) return true;
340 limit_value( &s->player1.y, PLAYER_Y_MIN, PLAYER_Y_MAX);
341 limit_value( &s->player2.y, PLAYER_Y_MIN, PLAYER_Y_MAX);
342 limit_value( &s->ball.jump, BALL_JUMP_MIN, BALL_JUMP_MAX);
344 /* Update ball_dest for debugging purposes */
345 get_move_y(s, 1);
346 get_move_y(s, 2);
348 s->ball.x += s->ball.move_x;
349 s->ball.y += s->ball.move_y;
351 if(s->player1.state) s->player1.state--;
352 if(s->player2.state) s->player2.state--;
354 return false;
357 void render( GameState* s) {
358 if (s->play_sound != SOUND_MAX) {
359 play_sample(s->play_sound);
360 s->play_sound = SOUND_MAX;
362 if( s->winner != WINNER_NONE) {
363 if( !s->is_over) {
364 start_fade();
365 s->is_over = true;
367 clear_screen();
368 store_screen();
369 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);
370 sprintf( s->game_score_str, "player %d wins the match with %s", s->winner, format_sets( s));
371 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);
372 updatescr();
373 return;
375 if (s->old_court_type != s->court_type) {
376 clear_screen();
377 fill_image(s->court_type, 120, 120, 400, 250);
378 show_image(GR_COURT, 0, 0, 255);
379 s->old_court_type = s->court_type;
380 store_screen();
382 show_sprite( GR_REFEREE, s->referee, 4, 250, 10, 255);
383 show_image( GR_SHADOW, s->ball.x-BALL_X_MID, s->ball.y + get_phase( s) - BALL_Y_MID, 255);
385 show_sprite( GR_RACKET, (!s->player1.state), 4, s->player1.x-RACKET_X_MID, s->player1.y-RACKET_Y_MID, 255);
386 show_sprite( GR_RACKET, (!s->player2.state)+2, 4, s->player2.x-RACKET_X_MID, s->player2.y-RACKET_Y_MID, 255);
388 if( s->ball.move_x > 0) {
389 show_sprite( GR_BALL, (s->time/500)%4, 4, s->ball.x-BALL_X_MID, s->ball.y-BALL_Y_MID, 255);
390 } else if( s->ball.move_x < 0) {
391 show_sprite( GR_BALL, 3-(s->time/500)%4, 4, s->ball.x-BALL_X_MID, s->ball.y-BALL_Y_MID, 255);
392 } else {
393 show_sprite( GR_BALL, 0, 4, s->ball.x-BALL_X_MID, s->ball.y-BALL_Y_MID, 255);
396 /* Player 1's mouse rectangle */
397 if (!(s->player1.mouse_locked)) {
398 rectangle(s->player1.x-2+5, s->player1.mouse_y-2, 4, 4, 255, 255, 255);
399 rectangle(s->player1.x-1+5, s->player1.mouse_y-1, 2, 2, 0, 0, 0);
402 font_draw_string( GR_DKC2_FONT, s->game_score_str, 14, 14, 0, ANIMATION_NONE);
403 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);
405 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);
407 #ifdef DEBUG
408 line_horiz( s->player1.y, 255, 0, 0);
409 line_horiz( s->player2.y, 0, 0, 255);
410 line_horiz( s->ball.y, 0, 255, 0);
412 line_vert( s->player1.x, 255, 0, 0);
413 line_vert( s->player2.x, 0, 0, 255);
414 line_vert( s->ball.x, 0, 255, 0);
416 line_horiz( s->player1.ball_dest, 255, 0, 255);
417 line_horiz( s->player2.ball_dest, 0, 255, 255);
419 line_horiz( GAME_Y_MIN, 100, 100, 100);
420 line_horiz( GAME_Y_MAX, 100, 100, 100);
421 #endif
423 updatescr();
426 void limit_value( float* value, float min, float max) {
427 if( *value < min) {
428 *value = min;
429 } else if( *value > max) {
430 *value = max;
434 float get_phase( GameState* s) {
435 float pos, fract;
436 float x, min, max, direction;
438 x = s->ball.x;
439 min = GAME_X_MIN;
440 max = GAME_X_MAX;
441 direction = s->ball.move_x;
443 pos = (direction>0)?(1-GROUND_PHASE):(GROUND_PHASE);
445 fract = (x-min)/(max-min);
447 if( fract < pos) {
448 fract = fract/pos;
449 return fabsf( cosf(PI*fract/2))*PHASE_AMP*s->ball.jump;
450 } else {
451 fract = (pos-fract)/(1-pos);
452 return fabsf( sinf(PI*fract/2))*PHASE_AMP*s->ball.jump;
456 float get_move_y( GameState* s, unsigned char player) {
457 float pct, dest, x_len, y_len;
458 float py, by, pa, move_x;
460 py = (player==1)?(s->player1.y):(s->player2.y);
461 by = s->ball.y;
462 pa = RACKET_Y_MID*2;
463 move_x = s->ball.move_x;
465 /* -1.0 .. 1.0 for racket hit position */
466 pct = (by-py)/(pa/2);
467 limit_value( &pct, -1.0, 1.0);
469 /* Y destination for ball */
470 dest = GAME_Y_MID + pct*(GAME_Y_MAX-GAME_Y_MIN);
471 if( player == 1) {
472 s->player1.ball_dest = dest;
473 } else {
474 s->player2.ball_dest = dest;
477 /* lengths for the ball's journey */
478 if( player == 1) {
479 x_len = fabsf(GAME_X_MAX - s->ball.x);
480 y_len = dest - by + MOVE_Y_SEED-rand()%MOVE_Y_SEED*2;
481 } else {
482 x_len = s->ball.x - GAME_X_MIN;
483 y_len = by - dest + MOVE_Y_SEED-rand()%MOVE_Y_SEED*2;
486 /* return the should-be value for move_y */
487 return (y_len*move_x)/(x_len);
490 void input_human( Player* player, bool up, bool down, bool hit, bool use_mouse, GameState* s) {
491 int diff = PLAYER_MOVE_Y;
492 int mb;
495 * Only use mouse control if the user isn't pressing any buttons
496 * this way, keyboard control still works when mouse control is
497 * enabled.
499 if (use_mouse && (down || up || hit)) {
501 * this is here so if the user decides to play
502 * with keyboard controls, we will lock the
503 * mouse and disable displaying the mouse cursor
505 player->mouse_locked = true;
507 if (use_mouse && !down && !up && !hit) {
508 mb = SDL_GetMouseState(&(player->mouse_x), &(player->mouse_y));
509 if (mb&SDL_BUTTON(SDL_BUTTON_LEFT)) {
510 if (player->mouse_y < player->y) {
511 down = false;
512 up = true;
513 diff = (player->y-player->mouse_y<diff)?(player->y-player->mouse_y):(diff);
514 } else if (player->mouse_y > player->y) {
515 up = false;
516 down = true;
517 diff = (player->mouse_y-player->y<diff)?(player->mouse_y-player->y):(diff);
519 player->mouse_locked = false;
520 } else if (!player->mouse_locked) {
521 hit = true;
525 if (fabsf(s->joystick_y) > JOYSTICK_TRESHOLD) {
526 diff = PLAYER_MOVE_Y*fabsf(s->joystick_y)/40.0;
527 if (diff > PLAYER_MOVE_Y) {
528 diff = PLAYER_MOVE_Y;
532 if (up) {
533 player->y -= fminf(diff, diff*player->accelerate);
534 player->accelerate *= PLAYER_ACCEL_INCREASE;
535 } else if (down) {
536 player->y += fminf(diff, diff*player->accelerate);
537 player->accelerate *= PLAYER_ACCEL_INCREASE;
538 } else {
539 player->accelerate = PLAYER_ACCEL_DEFAULT;
542 if( hit) {
543 if( !player->state && !player->state_locked) {
544 player->state = PLAYER_STATE_MAX;
545 player->state_locked = true;
547 } else {
548 player->state_locked = false;
552 void input_ai( Player* player, Ball* ball, Player* opponent, GameState* s) {
553 float fact = 1.7;
554 float target;
556 if( fabsf( player->y - ball->y) > RACKET_Y_MID*5) {
557 fact = 3.5;
560 target = GAME_Y_MID + (opponent->ball_dest - GAME_Y_MID)/5;
562 if( player->responsible) {
563 if( player->desire == DESIRE_NORMAL && !IS_NEAR_Y_AI( player->y, ball->y)) {
564 if( player->y < ball->y) {
565 player->y += fmin( 2*fact, ball->y - player->y);
566 } else if( player->y > ball->y) {
567 player->y -= fmin( 2*fact, player->y - ball->y);
571 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) {
572 player->state = PLAYER_STATE_MAX;
574 } else if( ball->move_x == 0) {
575 if( player->desire == DESIRE_NORMAL && !IS_NEAR_Y_AI( player->y, target)) {
576 if( player->y < target) {
577 player->y += fmin( fact, (target-player->y)/40.0);
578 } else if( player->y > target) {
579 player->y -= fmin( fact, (player->y-target)/40.0);
582 } else if( s->ngram_prediction > 0.0) {
583 target = s->ngram_prediction*((float)HEIGHT)/((float)(NGRAM_STEPS));
584 target = GAME_Y_MID + (target-GAME_Y_MID)*1.5;
586 if( player->desire == DESIRE_NORMAL && !IS_NEAR_Y_AI( player->y, target)) {
587 if( player->y < target) {
588 player->y += fmin( fact, (target-player->y)/40.0);
589 } else if( player->y > target) {
590 player->y -= fmin( fact, (player->y-target)/40.0);
593 } else {/*
594 if( player->desire == DESIRE_NORMAL) {
595 if( !IS_NEAR_Y_AI( player->y, target)) {
596 player->y += (target - player->y)/40.0;
602 void game_setup_serve( GameState* s) {
603 s->ball.jump = 7.5;
604 s->ball.y = GAME_Y_MID;
605 s->ball.move_x = 0.0;
606 s->ball.move_y = 0.0;
608 if( s->player1_serves) {
609 s->player1.responsible = true;
610 s->player1.ball_dest = 0.0;
611 s->ball.x = GAME_X_MIN-RACKET_X_MID*1.5;
612 } else {
613 s->player1.responsible = false;
614 s->player2.ball_dest = 0.0;
615 s->ball.x = GAME_X_MAX+RACKET_X_MID*1.5;
618 s->player2.responsible = !(s->player1.responsible);
621 float ngram_predictor( GameState* s) {
622 unsigned int count = 0;
623 unsigned long sum = 0;
624 int x, y, z;
625 float result;
627 if( s->history_size < 3) {
628 return 0.0;
631 x = s->history[1];
632 y = s->history[2];
634 for( z = 0; z<NGRAM_STEPS; z++) {
635 count += s->ngram[x][y][z];
636 sum += z * s->ngram[x][y][z];
639 result = ((float)(sum))/((float)(count));
640 #ifdef DEBUG
641 printf( "predicting next = %.2f\n", result);
642 #endif
644 return result;
647 void score_game( GameState* s, bool player1_scored) {
648 Player* winner = (player1_scored)?(&(s->player1)):(&(s->player2));
649 Player* loser = (player1_scored)?(&(s->player2)):(&(s->player1));
651 if( s->current_set >= SETS_TO_WIN*2-1) {
652 return;
655 winner->game++;
656 if( loser->game < winner->game-1) {
657 if( winner->game >= 4) {
658 winner->game = loser->game = 0;
659 winner->sets[s->current_set]++;
660 /* scoring the set.. */
661 if( (winner->sets[s->current_set] == 6 && loser->sets[s->current_set] < 5) ||
662 winner->sets[s->current_set] == 7) {
663 s->current_set++;
664 s->winner = game_get_winner( s);
670 char* format_sets( GameState* s) {
671 static char sets[100];
672 static char tmp[100];
673 int i, max = s->current_set;
675 sets[0] = '\0';
677 if( s->winner != WINNER_NONE) {
678 max--;
680 for( i=0; i<=max; i++) {
681 sprintf( tmp, "%d:%d, ", s->player1.sets[i], s->player2.sets[i]);
682 strcat( sets, tmp);
685 sets[strlen(sets)-2] = '\0';
687 return sets;
690 char* format_game( GameState* s) {
691 static char game[100];
692 static const int game_scoring[] = { 0, 15, 30, 40 };
694 if( s->player1.game < 4 && s->player2.game < 4) {
695 sprintf( game, "%d - %d", game_scoring[s->player1.game], game_scoring[s->player2.game]);
696 } else if( s->player1.game > s->player2.game) {
697 strcpy( game, "advantage player 1");
698 } else if( s->player1.game < s->player2.game) {
699 strcpy( game, "advantage player 2");
700 } else {
701 strcpy( game, "deuce");
704 return game;
707 char* format_status( GameState* s) {
708 static char status[100];
709 static const char* set_names[] = { "first", "second", "third", "fourth", "fifth" };
711 sprintf( status, "%d:%d in %s set", s->player1.sets[s->current_set], s->player2.sets[s->current_set], set_names[s->current_set]);
713 return status;
716 int game_get_winner( GameState* s) {
717 int i;
718 int sets[2] = {0};
720 for( i=0; i<s->current_set; i++) {
721 if( s->player1.sets[i] > s->player2.sets[i]) {
722 sets[0]++;
723 } else {
724 sets[1]++;
728 if( sets[0] == SETS_TO_WIN) return WINNER_PLAYER1;
729 if( sets[1] == SETS_TO_WIN) return WINNER_PLAYER2;
731 return WINNER_NONE;