Add new, sliding main menu system
[tennix.git] / game.c
blobffa4793efc238f019355c4deb35d29f55247e5f4
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"
33 void game( bool singleplayer) {
34 GameState s = {
35 { 0, 0, 0.0, 0.0, 0.0 },
36 { 0, 0, 0, 0, 0 },
37 { 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 },
38 { GAME_X_MAX+RACKET_X_MID*2, GAME_Y_MID, 0, 0, 0, DESIRE_NORMAL, PLAYER_TYPE_HUMAN, GAME_Y_MID, false, 0, {0}, 0, 0, PLAYER_ACCEL_DEFAULT, true },
42 true,
43 "welcome to tennix " VERSION,
44 { 0 },
45 { 0 },
46 REFEREE_NORMAL,
48 WINNER_NONE,
49 false,
50 GR_CTT_GRASS,
51 -1,
52 { 0 },
54 false,
55 { { { 0 } } },
56 0.0,
57 SOUND_MAX,
58 0.0,
59 0.0,
63 strcpy( s.game_score_str, format_game( &s));
64 strcpy( s.sets_score_str, format_sets( &s));
66 Uint32 ot = SDL_GetTicks();
67 Uint32 nt;
68 Uint32 dt = GAME_TICKS;
69 Uint32 diff;
70 Uint32 accumulator = 0;
71 bool quit = false;
72 int x, y, z;
74 #ifdef ENABLE_FPS_LIMIT
75 Uint32 ft, frames; /* frame timer and frames */
76 #endif
78 if( singleplayer) {
79 #ifdef DEBUG
80 s.player1.type = PLAYER_TYPE_AI;
81 #endif
82 s.player2.type = PLAYER_TYPE_AI;
85 game_setup_serve( &s);
87 /* smoothen n-gram */
88 for( x = 0; x<NGRAM_STEPS; x++) {
89 for( y = 0; y<NGRAM_STEPS; y++) {
90 for( z = 0; z<NGRAM_STEPS; z++) {
91 s.ngram[x][y][z] = 1;
96 play_sample_loop(SOUND_AUDIENCE);
98 #ifdef ENABLE_FPS_LIMIT
99 frames = 0;
100 ft = SDL_GetTicks();
101 #endif
102 while( !quit) {
103 nt = SDL_GetTicks();
104 diff = nt-ot;
105 if( diff > 2000) {
106 diff = 0;
109 accumulator += diff;
110 ot = nt;
112 while( accumulator >= dt) {
113 quit = step( &s);
114 s.time += dt;
115 accumulator -= dt;
117 if( s.was_stopped) {
118 ot = SDL_GetTicks();
119 s.was_stopped = false;
123 #ifdef ENABLE_FPS_LIMIT
124 while (frames*1000.0/((float)(SDL_GetTicks()-ft+1))>(float)(DEFAULT_FPS)) {
125 SDL_Delay(10);
127 frames++;
128 #endif
130 render( &s);
133 stop_sample(SOUND_AUDIENCE);
136 bool step( GameState* s) {
137 Uint8 *keys;
138 SDL_Event e;
140 if( get_phase( s) < 1.0) {
141 if( !s->ground.jump) {
142 s->play_sound = SOUND_GROUND;
144 if( IS_OUT_Y( s->ball.y)) {
145 /* out - responsibilities stay the same */
146 s->status = "out!";
147 s->play_sound = SOUND_OUT;
148 s->referee = REFEREE_OUT;
149 } else {
150 /* not out - responsibilities change */
151 s->player1.responsible = !(s->player2.responsible = !s->player2.responsible);
152 s->status = format_status( s);
153 s->referee = REFEREE_NORMAL;
156 s->ground.jump = 3;
157 s->ground.x = s->ball.x;
158 s->ground.y = s->ball.y;
159 } else {
160 if( s->ground.jump && !(s->time%5)) s->ground.jump--;
163 if( IS_OUT_X(s->ball.x) || IS_OFFSCREEN_Y(s->ball.y)) {
164 if( IS_OFFSCREEN( s->ball.x, s->ball.y)) {
165 s->player1_serves = s->player1.responsible;
167 score_game( s, s->player2.responsible);
168 strcpy( s->game_score_str, format_game( s));
169 strcpy( s->sets_score_str, format_sets( s));
171 if( s->player1.responsible) {
172 if( s->player1.type == PLAYER_TYPE_HUMAN && s->player2.type == PLAYER_TYPE_AI) {
173 s->status = "computer scores";
174 } else {
175 s->status = "player 2 scores";
177 s->referee = REFEREE_PLAYER2;
178 } else {
179 if( s->player1.type == PLAYER_TYPE_HUMAN && s->player2.type == PLAYER_TYPE_AI) {
180 s->status = "player scores";
181 } else {
182 s->status = "player 1 scores";
184 s->referee = REFEREE_PLAYER1;
187 game_setup_serve( s);
188 s->play_sound = SOUND_APPLAUSE;
189 SDL_Delay( 500);
190 start_fade();
191 s->was_stopped = true;
192 s->history_size = 0;
193 s->history_is_locked = 0;
194 s->ngram_prediction = 0.0;
195 #ifdef DEBUG
196 printf( "-- game reset --\n");
197 #endif
200 if( IS_OUT_X(s->ball.x)) {
201 if( !s->history_is_locked && s->referee != REFEREE_OUT) {
202 s->history[s->history_size] = (int)(NGRAM_STEPS*s->ball.y/HEIGHT);
203 s->history_size++;
204 if( s->history_size == 3) {
205 s->ngram[s->history[0]][s->history[1]][s->history[2]] += 10;
206 #ifdef DEBUG
207 printf( "history: %d, %d, %d\n", s->history[0], s->history[1], s->history[2]);
208 #endif
209 s->ngram_prediction = ngram_predictor( s);
210 s->history[0] = s->history[1];
211 s->history[1] = s->history[2];
212 s->history_size--;
214 s->history_is_locked = true;
216 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) {
217 s->ball.x = GAME_X_MIN;
218 if( s->player1.state == PLAYER_STATE_MAX) {
219 s->ball.move_x = PLAYER_POWERSHOT;
220 } else {
221 s->ball.move_x = 2.5 + 2.0*s->player1.state/PLAYER_STATE_MAX;
223 s->ball.move_y = get_move_y( s, 1);
224 s->player2.responsible = !(s->player1.responsible = 1);
225 s->ball.jump += 1.0-2.0*(s->player1.state<5);
226 s->play_sound = SOUND_RACKET;
227 pan_sample(SOUND_RACKET, 0.4);
228 } 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) {
229 s->ball.x = GAME_X_MAX;
230 if( s->player2.state == PLAYER_STATE_MAX) {
231 s->ball.move_x = -PLAYER_POWERSHOT;
232 } else {
233 s->ball.move_x = -(2.5 + 2.0*s->player2.state/PLAYER_STATE_MAX);
235 s->ball.move_y = get_move_y( s, 2);
236 s->player1.responsible = !(s->player2.responsible = 1);
237 s->ball.jump += 1.0-2.0*(s->player2.state<5);
238 s->play_sound = SOUND_RACKET;
239 pan_sample(SOUND_RACKET, 0.6);
242 } else {
243 s->history_is_locked = false;
246 SDL_PollEvent( &e);
247 keys = SDL_GetKeyState( NULL);
248 switch(e.type) {
249 case SDL_JOYAXISMOTION:
250 if (e.jaxis.axis == JOYSTICK_Y_AXIS) {
251 s->joystick_y = JOYSTICK_PERCENTIZE(e.jaxis.value);
252 } else if (e.jaxis.axis == JOYSTICK_X_AXIS) {
253 s->joystick_x = JOYSTICK_PERCENTIZE(e.jaxis.value);
255 break;
256 case SDL_JOYBUTTONUP: case SDL_JOYBUTTONDOWN:
257 if (e.jbutton.button == JOYSTICK_BUTTON_A) {
258 s->joystick_a = (e.jbutton.state == SDL_PRESSED);
260 break;
263 if( s->time%50==0) {
265 * Maemo keys:
266 * F7 = "Decrease" key
267 * F8 = "Increase" key
269 if (keys['c'] || keys[SDLK_F8]) {
270 s->court_type++;
271 } else if (keys[SDLK_F7]) {
272 s->court_type--;
274 if( s->court_type > GR_CTT_LAST) {
275 s->court_type = GR_CTT_FIRST;
276 } else if (s->court_type < GR_CTT_FIRST) {
277 s->court_type = GR_CTT_LAST;
281 if( !is_fading() && !s->is_over) {
282 if( s->player1.type == PLAYER_TYPE_HUMAN) {
283 input_human( &s->player1,
284 keys['w'] || keys[SDLK_UP] || s->joystick_y < -JOYSTICK_TRESHOLD,
285 keys['s'] || keys[SDLK_DOWN] || s->joystick_y > JOYSTICK_TRESHOLD,
286 keys['d'] || keys[SDLK_SPACE] || keys[SDLK_LCTRL] || keys[SDLK_RETURN] || s->joystick_a,
287 #ifdef ENABLE_MOUSE
288 true,
289 #else
290 false,
291 #endif
293 } else {
294 input_ai( &s->player1, &s->ball, &s->player2, s);
297 if( s->player2.type == PLAYER_TYPE_HUMAN) {
298 input_human( &s->player2, keys['o'], keys['l'], keys['k'], false, s);
299 } else {
300 input_ai( &s->player2, &s->ball, &s->player1, s);
304 /* Maemo: The "F6" button is the "Fullscreen" button */
305 if( keys['f'] || keys[SDLK_F6]) SDL_WM_ToggleFullScreen( screen);
306 if( keys['y']) SDL_SaveBMP( screen, "screenshot.bmp");
308 /* Maemo: The "F4" button is the "Open menu" button */
309 if( keys['p'] || keys[SDLK_F4]) {
310 while( keys['p'] || keys[SDLK_F4]) {
311 SDL_PollEvent( &e);
312 keys = SDL_GetKeyState( NULL);
313 SDL_Delay( 10);
315 while( (keys['p'] || keys[SDLK_F4]) == 0) {
316 SDL_PollEvent( &e);
317 keys = SDL_GetKeyState( NULL);
318 SDL_Delay( 10);
320 while( keys['p'] || keys[SDLK_F4]) {
321 SDL_PollEvent( &e);
322 keys = SDL_GetKeyState( NULL);
323 SDL_Delay( 10);
325 s->was_stopped = true;
328 if( keys[SDLK_ESCAPE] || keys['q']) return true;
330 limit_value( &s->player1.y, PLAYER_Y_MIN, PLAYER_Y_MAX);
331 limit_value( &s->player2.y, PLAYER_Y_MIN, PLAYER_Y_MAX);
332 limit_value( &s->ball.jump, BALL_JUMP_MIN, BALL_JUMP_MAX);
334 /* Update ball_dest for debugging purposes */
335 get_move_y(s, 1);
336 get_move_y(s, 2);
338 s->ball.x += s->ball.move_x;
339 s->ball.y += s->ball.move_y;
341 if(s->player1.state) s->player1.state--;
342 if(s->player2.state) s->player2.state--;
344 return false;
347 void render( GameState* s) {
348 if (s->play_sound != SOUND_MAX) {
349 play_sample(s->play_sound);
350 s->play_sound = SOUND_MAX;
352 if( s->winner != WINNER_NONE) {
353 if( !s->is_over) {
354 start_fade();
355 s->is_over = true;
357 clear_screen();
358 store_screen();
359 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);
360 sprintf( s->game_score_str, "player %d wins the match with %s", s->winner, format_sets( s));
361 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);
362 updatescr();
363 return;
365 if (s->old_court_type != s->court_type) {
366 clear_screen();
367 fill_image(s->court_type, 120, 120, 400, 250);
368 show_image(GR_COURT, 0, 0, 255);
369 s->old_court_type = s->court_type;
370 store_screen();
372 show_sprite( GR_REFEREE, s->referee, 4, 250, 10, 255);
373 show_image( GR_SHADOW, s->ball.x-BALL_X_MID, s->ball.y + get_phase( s) - BALL_Y_MID, 255);
375 show_sprite( GR_RACKET, (!s->player1.state), 4, s->player1.x-RACKET_X_MID, s->player1.y-RACKET_Y_MID, 255);
376 show_sprite( GR_RACKET, (!s->player2.state)+2, 4, s->player2.x-RACKET_X_MID, s->player2.y-RACKET_Y_MID, 255);
378 if( s->ground.jump) {
379 show_sprite( GR_GROUND, s->ground.jump-1, 3, s->ground.x - BALL_X_MID, s->ground.y - BALL_Y_MID, 128);
382 if( s->ball.move_x > 0) {
383 show_sprite( GR_BALL, (s->time/500)%4, 4, s->ball.x-BALL_X_MID, s->ball.y-BALL_Y_MID, 255);
384 } else if( s->ball.move_x < 0) {
385 show_sprite( GR_BALL, 3-(s->time/500)%4, 4, s->ball.x-BALL_X_MID, s->ball.y-BALL_Y_MID, 255);
386 } else {
387 show_sprite( GR_BALL, 0, 4, s->ball.x-BALL_X_MID, s->ball.y-BALL_Y_MID, 255);
390 /* Player 1's mouse rectangle */
391 if (!(s->player1.mouse_locked)) {
392 rectangle(s->player1.x-2+5, s->player1.mouse_y-2, 4, 4, 255, 255, 255);
393 rectangle(s->player1.x-1+5, s->player1.mouse_y-1, 2, 2, 0, 0, 0);
396 font_draw_string( GR_DKC2_FONT, s->game_score_str, 14, 14, 0, ANIMATION_NONE);
397 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);
399 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);
401 #ifdef DEBUG
402 line_horiz( s->player1.y, 255, 0, 0);
403 line_horiz( s->player2.y, 0, 0, 255);
404 line_horiz( s->ball.y, 0, 255, 0);
406 line_vert( s->player1.x, 255, 0, 0);
407 line_vert( s->player2.x, 0, 0, 255);
408 line_vert( s->ball.x, 0, 255, 0);
410 line_horiz( s->player1.ball_dest, 255, 0, 255);
411 line_horiz( s->player2.ball_dest, 0, 255, 255);
413 line_horiz( GAME_Y_MIN, 100, 100, 100);
414 line_horiz( GAME_Y_MAX, 100, 100, 100);
415 #endif
417 updatescr();
420 void limit_value( float* value, float min, float max) {
421 if( *value < min) {
422 *value = min;
423 } else if( *value > max) {
424 *value = max;
428 float get_phase( GameState* s) {
429 float pos, fract;
430 float x, min, max, direction;
432 x = s->ball.x;
433 min = GAME_X_MIN;
434 max = GAME_X_MAX;
435 direction = s->ball.move_x;
437 pos = (direction>0)?(1-GROUND_PHASE):(GROUND_PHASE);
439 fract = (x-min)/(max-min);
441 if( fract < pos) {
442 fract = fract/pos;
443 return fabsf( cosf(PI*fract/2))*PHASE_AMP*s->ball.jump;
444 } else {
445 fract = (pos-fract)/(1-pos);
446 return fabsf( sinf(PI*fract/2))*PHASE_AMP*s->ball.jump;
450 float get_move_y( GameState* s, unsigned char player) {
451 float pct, dest, x_len, y_len;
452 float py, by, pa, move_x;
454 py = (player==1)?(s->player1.y):(s->player2.y);
455 by = s->ball.y;
456 pa = RACKET_Y_MID*2;
457 move_x = s->ball.move_x;
459 /* -1.0 .. 1.0 for racket hit position */
460 pct = (by-py)/(pa/2);
461 limit_value( &pct, -1.0, 1.0);
463 /* Y destination for ball */
464 dest = GAME_Y_MID + pct*(GAME_Y_MAX-GAME_Y_MIN);
465 if( player == 1) {
466 s->player1.ball_dest = dest;
467 } else {
468 s->player2.ball_dest = dest;
471 /* lengths for the ball's journey */
472 if( player == 1) {
473 x_len = fabsf(GAME_X_MAX - s->ball.x);
474 y_len = dest - by + MOVE_Y_SEED-rand()%MOVE_Y_SEED*2;
475 } else {
476 x_len = s->ball.x - GAME_X_MIN;
477 y_len = by - dest + MOVE_Y_SEED-rand()%MOVE_Y_SEED*2;
480 /* return the should-be value for move_y */
481 return (y_len*move_x)/(x_len);
484 void input_human( Player* player, bool up, bool down, bool hit, bool use_mouse, GameState* s) {
485 int diff = PLAYER_MOVE_Y;
486 int mb;
489 * Only use mouse control if the user isn't pressing any buttons
490 * this way, keyboard control still works when mouse control is
491 * enabled.
493 if (use_mouse && (down || up || hit)) {
495 * this is here so if the user decides to play
496 * with keyboard controls, we will lock the
497 * mouse and disable displaying the mouse cursor
499 player->mouse_locked = true;
501 if (use_mouse && !down && !up && !hit) {
502 mb = SDL_GetMouseState(&(player->mouse_x), &(player->mouse_y));
503 if (mb&SDL_BUTTON(SDL_BUTTON_LEFT)) {
504 if (player->mouse_y < player->y) {
505 down = false;
506 up = true;
507 diff = (player->y-player->mouse_y<diff)?(player->y-player->mouse_y):(diff);
508 } else if (player->mouse_y > player->y) {
509 up = false;
510 down = true;
511 diff = (player->mouse_y-player->y<diff)?(player->mouse_y-player->y):(diff);
513 player->mouse_locked = false;
514 } else if (!player->mouse_locked) {
515 hit = true;
519 if (fabsf(s->joystick_y) > JOYSTICK_TRESHOLD) {
520 diff = PLAYER_MOVE_Y*fabsf(s->joystick_y)/40.0;
521 if (diff > PLAYER_MOVE_Y) {
522 diff = PLAYER_MOVE_Y;
526 if (up) {
527 player->y -= fminf(diff, diff*player->accelerate);
528 player->accelerate *= PLAYER_ACCEL_INCREASE;
529 } else if (down) {
530 player->y += fminf(diff, diff*player->accelerate);
531 player->accelerate *= PLAYER_ACCEL_INCREASE;
532 } else {
533 player->accelerate = PLAYER_ACCEL_DEFAULT;
536 if( hit) {
537 if( !player->state && !player->state_locked) {
538 player->state = PLAYER_STATE_MAX;
539 player->state_locked = true;
541 } else {
542 player->state_locked = false;
546 void input_ai( Player* player, Ball* ball, Player* opponent, GameState* s) {
547 float fact = 1.7;
548 float target;
550 if( fabsf( player->y - ball->y) > RACKET_Y_MID*5) {
551 fact = 3.5;
554 target = GAME_Y_MID + (opponent->ball_dest - GAME_Y_MID)/5;
556 if( player->responsible) {
557 if( player->desire == DESIRE_NORMAL && !IS_NEAR_Y_AI( player->y, ball->y)) {
558 if( player->y < ball->y) {
559 player->y += fmin( 2*fact, ball->y - player->y);
560 } else if( player->y > ball->y) {
561 player->y -= fmin( 2*fact, player->y - ball->y);
565 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) {
566 player->state = PLAYER_STATE_MAX;
568 } else if( ball->move_x == 0) {
569 if( player->desire == DESIRE_NORMAL && !IS_NEAR_Y_AI( player->y, target)) {
570 if( player->y < target) {
571 player->y += fmin( fact, (target-player->y)/40.0);
572 } else if( player->y > target) {
573 player->y -= fmin( fact, (player->y-target)/40.0);
576 } else if( s->ngram_prediction > 0.0) {
577 target = s->ngram_prediction*((float)HEIGHT)/((float)(NGRAM_STEPS));
578 target = GAME_Y_MID + (target-GAME_Y_MID)*1.5;
580 if( player->desire == DESIRE_NORMAL && !IS_NEAR_Y_AI( player->y, target)) {
581 if( player->y < target) {
582 player->y += fmin( fact, (target-player->y)/40.0);
583 } else if( player->y > target) {
584 player->y -= fmin( fact, (player->y-target)/40.0);
587 } else {/*
588 if( player->desire == DESIRE_NORMAL) {
589 if( !IS_NEAR_Y_AI( player->y, target)) {
590 player->y += (target - player->y)/40.0;
596 void game_setup_serve( GameState* s) {
597 s->ball.jump = 7.5;
598 s->ball.y = GAME_Y_MID;
599 s->ball.move_x = 0.0;
600 s->ball.move_y = 0.0;
602 if( s->player1_serves) {
603 s->player1.responsible = true;
604 s->player1.ball_dest = 0.0;
605 s->ball.x = GAME_X_MIN-RACKET_X_MID*1.5;
606 } else {
607 s->player1.responsible = false;
608 s->player2.ball_dest = 0.0;
609 s->ball.x = GAME_X_MAX+RACKET_X_MID*1.5;
612 s->player2.responsible = !(s->player1.responsible);
615 float ngram_predictor( GameState* s) {
616 unsigned int count = 0;
617 unsigned long sum = 0;
618 int x, y, z;
619 float result;
621 if( s->history_size < 3) {
622 return 0.0;
625 x = s->history[1];
626 y = s->history[2];
628 for( z = 0; z<NGRAM_STEPS; z++) {
629 count += s->ngram[x][y][z];
630 sum += z * s->ngram[x][y][z];
633 result = ((float)(sum))/((float)(count));
634 #ifdef DEBUG
635 printf( "predicting next = %.2f\n", result);
636 #endif
638 return result;
641 void score_game( GameState* s, bool player1_scored) {
642 Player* winner = (player1_scored)?(&(s->player1)):(&(s->player2));
643 Player* loser = (player1_scored)?(&(s->player2)):(&(s->player1));
645 if( s->current_set >= SETS_TO_WIN*2-1) {
646 return;
649 winner->game++;
650 if( loser->game < winner->game-1) {
651 if( winner->game >= 4) {
652 winner->game = loser->game = 0;
653 winner->sets[s->current_set]++;
654 /* scoring the set.. */
655 if( (winner->sets[s->current_set] == 6 && loser->sets[s->current_set] < 5) ||
656 winner->sets[s->current_set] == 7) {
657 s->current_set++;
658 s->winner = game_get_winner( s);
664 char* format_sets( GameState* s) {
665 static char sets[100];
666 static char tmp[100];
667 int i, max = s->current_set;
669 sets[0] = '\0';
671 if( s->winner != WINNER_NONE) {
672 max--;
674 for( i=0; i<=max; i++) {
675 sprintf( tmp, "%d:%d, ", s->player1.sets[i], s->player2.sets[i]);
676 strcat( sets, tmp);
679 sets[strlen(sets)-2] = '\0';
681 return sets;
684 char* format_game( GameState* s) {
685 static char game[100];
686 static const int game_scoring[] = { 0, 15, 30, 40 };
688 if( s->player1.game < 4 && s->player2.game < 4) {
689 sprintf( game, "%d - %d", game_scoring[s->player1.game], game_scoring[s->player2.game]);
690 } else if( s->player1.game > s->player2.game) {
691 strcpy( game, "advantage player 1");
692 } else if( s->player1.game < s->player2.game) {
693 strcpy( game, "advantage player 2");
694 } else {
695 strcpy( game, "deuce");
698 return game;
701 char* format_status( GameState* s) {
702 static char status[100];
703 static const char* set_names[] = { "first", "second", "third", "fourth", "fifth" };
705 sprintf( status, "%d:%d in %s set", s->player1.sets[s->current_set], s->player2.sets[s->current_set], set_names[s->current_set]);
707 return status;
710 int game_get_winner( GameState* s) {
711 int i;
712 int sets[2] = {0};
714 for( i=0; i<s->current_set; i++) {
715 if( s->player1.sets[i] > s->player2.sets[i]) {
716 sets[0]++;
717 } else {
718 sets[1]++;
722 if( sets[0] == SETS_TO_WIN) return WINNER_PLAYER1;
723 if( sets[1] == SETS_TO_WIN) return WINNER_PLAYER2;
725 return WINNER_NONE;