5 * Copyright (C) 2003, 2007, 2008 Thomas Perl <thp@perli.net>
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,
34 GameState
*gamestate_new() {
38 GameState
template = {
39 { 0, 0, 0.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 },
47 "welcome to tennix " VERSION
,
72 s
= (GameState
*)malloc(sizeof(GameState
));
73 if (s
== NULL
) abort();
75 memcpy(s
, &template, sizeof(GameState
));
80 for( x
= 0; x
<NGRAM_STEPS
; x
++) {
81 for( y
= 0; y
<NGRAM_STEPS
; y
++) {
82 for( z
= 0; z
<NGRAM_STEPS
; z
++) {
83 s
->ngram
[x
][y
][z
] = 1;
92 void gameloop(GameState
*s
) {
93 strcpy(s
->game_score_str
, format_game(s
));
94 strcpy(s
->sets_score_str
, format_sets(s
));
96 Uint32 ot
= SDL_GetTicks();
98 Uint32 dt
= GAME_TICKS
;
100 Uint32 accumulator
= 0;
103 #ifdef ENABLE_FPS_LIMIT
104 Uint32 ft
, frames
; /* frame timer and frames */
108 play_sample_background(SOUND_RAIN
);
110 play_sample_loop(SOUND_AUDIENCE
);
111 /* Reset the court type, so it is redrawn on first display */
112 s
->old_court_type
= -1;
114 #ifdef ENABLE_FPS_LIMIT
128 while( accumulator
>= dt
) {
131 s
->windtime
+= s
->wind
*dt
;
134 if( s
->was_stopped
) {
136 s
->was_stopped
= false;
140 #ifdef ENABLE_FPS_LIMIT
141 while (frames
*1000.0/((float)(SDL_GetTicks()-ft
+1))>(float)(DEFAULT_FPS
)) {
153 stop_sample(SOUND_AUDIENCE
);
154 stop_sample(SOUND_RAIN
);
157 bool step( GameState
* s
) {
161 if( get_phase( s
) < 1.0) {
162 if( !s
->ground
.jump
) {
163 s
->play_sound
= SOUND_GROUND
;
165 if( IS_OUT_Y( s
->ball
.y
)) {
166 /* out - responsibilities stay the same */
168 s
->play_sound
= SOUND_OUT
;
169 s
->referee
= REFEREE_OUT
;
171 /* not out - responsibilities change */
172 s
->player1
.responsible
= !(s
->player2
.responsible
= !s
->player2
.responsible
);
173 s
->status
= format_status( s
);
174 s
->referee
= REFEREE_NORMAL
;
178 s
->ground
.x
= s
->ball
.x
;
179 s
->ground
.y
= s
->ball
.y
;
181 if( s
->ground
.jump
&& !(s
->time
%5)) s
->ground
.jump
--;
184 if( IS_OUT_X(s
->ball
.x
) || IS_OFFSCREEN_Y(s
->ball
.y
)) {
185 if( IS_OFFSCREEN( s
->ball
.x
, s
->ball
.y
)) {
186 s
->player1_serves
= s
->player1
.responsible
;
188 score_game( s
, s
->player2
.responsible
);
189 strcpy( s
->game_score_str
, format_game( s
));
190 strcpy( s
->sets_score_str
, format_sets( s
));
192 if( s
->player1
.responsible
) {
193 if( s
->player1
.type
== PLAYER_TYPE_HUMAN
&& s
->player2
.type
== PLAYER_TYPE_AI
) {
194 s
->status
= "computer scores";
196 s
->status
= "player 2 scores";
198 s
->referee
= REFEREE_PLAYER2
;
200 if( s
->player1
.type
== PLAYER_TYPE_HUMAN
&& s
->player2
.type
== PLAYER_TYPE_AI
) {
201 s
->status
= "player scores";
203 s
->status
= "player 1 scores";
205 s
->referee
= REFEREE_PLAYER1
;
208 game_setup_serve( s
);
209 s
->play_sound
= SOUND_APPLAUSE
;
211 s
->was_stopped
= true;
213 s
->history_is_locked
= 0;
214 s
->ngram_prediction
= 0.0;
216 printf( "-- game reset --\n");
220 if( IS_OUT_X(s
->ball
.x
)) {
221 if( !s
->history_is_locked
&& s
->referee
!= REFEREE_OUT
) {
222 s
->history
[s
->history_size
] = (int)(NGRAM_STEPS
*s
->ball
.y
/HEIGHT
);
224 if( s
->history_size
== 3) {
225 s
->ngram
[s
->history
[0]][s
->history
[1]][s
->history
[2]] += 10;
227 printf( "history: %d, %d, %d\n", s
->history
[0], s
->history
[1], s
->history
[2]);
229 s
->ngram_prediction
= ngram_predictor( s
);
230 s
->history
[0] = s
->history
[1];
231 s
->history
[1] = s
->history
[2];
234 s
->history_is_locked
= true;
236 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
) {
237 s
->ball
.x
= GAME_X_MIN
;
238 if( s
->player1
.state
== PLAYER_STATE_MAX
) {
239 s
->ball
.move_x
= PLAYER_POWERSHOT
;
241 s
->ball
.move_x
= 2.5 + 2.0*s
->player1
.state
/PLAYER_STATE_MAX
;
243 s
->ball
.move_y
= get_move_y( s
, 1);
244 s
->player2
.responsible
= !(s
->player1
.responsible
= 1);
245 s
->ball
.jump
+= 1.0-2.0*(s
->player1
.state
<5);
246 s
->play_sound
= SOUND_RACKET
;
247 pan_sample(SOUND_RACKET
, 0.4);
248 } 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
) {
249 s
->ball
.x
= GAME_X_MAX
;
250 if( s
->player2
.state
== PLAYER_STATE_MAX
) {
251 s
->ball
.move_x
= -PLAYER_POWERSHOT
;
253 s
->ball
.move_x
= -(2.5 + 2.0*s
->player2
.state
/PLAYER_STATE_MAX
);
255 s
->ball
.move_y
= get_move_y( s
, 2);
256 s
->player1
.responsible
= !(s
->player2
.responsible
= 1);
257 s
->ball
.jump
+= 1.0-2.0*(s
->player2
.state
<5);
258 s
->play_sound
= SOUND_RACKET
;
259 pan_sample(SOUND_RACKET
, 0.6);
263 s
->history_is_locked
= false;
267 keys
= SDL_GetKeyState( NULL
);
269 case SDL_JOYAXISMOTION
:
270 if (e
.jaxis
.axis
== JOYSTICK_Y_AXIS
) {
271 s
->joystick_y
= JOYSTICK_PERCENTIZE(e
.jaxis
.value
);
272 } else if (e
.jaxis
.axis
== JOYSTICK_X_AXIS
) {
273 s
->joystick_x
= JOYSTICK_PERCENTIZE(e
.jaxis
.value
);
276 case SDL_JOYBUTTONUP
: case SDL_JOYBUTTONDOWN
:
277 if (e
.jbutton
.button
== JOYSTICK_BUTTON_A
) {
278 s
->joystick_a
= (e
.jbutton
.state
== SDL_PRESSED
);
286 * F7 = "Decrease" key
287 * F8 = "Increase" key
289 if (keys
['c'] || keys
[SDLK_F8
]) {
291 } else if (keys
[SDLK_F7
]) {
296 play_sample_background(SOUND_RAIN
);
310 s
->night
= 1 - s
->night
;
312 if( s
->court_type
> GR_CTT_LAST
) {
313 s
->court_type
= GR_CTT_FIRST
;
314 } else if (s
->court_type
< GR_CTT_FIRST
) {
315 s
->court_type
= GR_CTT_LAST
;
319 if( !is_fading() && !s
->is_over
) {
320 if( s
->player1
.type
== PLAYER_TYPE_HUMAN
) {
321 input_human( &s
->player1
,
322 keys
['w'] || keys
[SDLK_UP
] || s
->joystick_y
< -JOYSTICK_TRESHOLD
,
323 keys
['s'] || keys
[SDLK_DOWN
] || s
->joystick_y
> JOYSTICK_TRESHOLD
,
324 keys
['d'] || keys
[SDLK_SPACE
] || keys
[SDLK_LCTRL
] || keys
[SDLK_RETURN
] || s
->joystick_a
,
332 input_ai( &s
->player1
, &s
->ball
, &s
->player2
, s
);
335 if( s
->player2
.type
== PLAYER_TYPE_HUMAN
) {
336 input_human( &s
->player2
, keys
['o'], keys
['l'], keys
['k'], false, s
);
338 input_ai( &s
->player2
, &s
->ball
, &s
->player1
, s
);
342 /* Maemo: The "F6" button is the "Fullscreen" button */
343 if( keys
['f'] || keys
[SDLK_F6
]) SDL_WM_ToggleFullScreen( screen
);
344 if( keys
['y']) SDL_SaveBMP( screen
, "screenshot.bmp");
346 /* Maemo: The "F4" button is the "Open menu" button */
347 if( keys
['p'] || keys
[SDLK_F4
]) {
348 while( keys
['p'] || keys
[SDLK_F4
]) {
350 keys
= SDL_GetKeyState( NULL
);
353 while( (keys
['p'] || keys
[SDLK_F4
]) == 0) {
355 keys
= SDL_GetKeyState( NULL
);
358 while( keys
['p'] || keys
[SDLK_F4
]) {
360 keys
= SDL_GetKeyState( NULL
);
363 s
->was_stopped
= true;
366 if( keys
[SDLK_ESCAPE
] || keys
['q']) return true;
368 limit_value( &s
->player1
.y
, PLAYER_Y_MIN
, PLAYER_Y_MAX
);
369 limit_value( &s
->player2
.y
, PLAYER_Y_MIN
, PLAYER_Y_MAX
);
370 limit_value( &s
->ball
.jump
, BALL_JUMP_MIN
, BALL_JUMP_MAX
);
372 /* Update ball_dest for debugging purposes */
376 if (s
->ball
.move_x
> 0 && s
->wind
> 0) {
377 s
->ball
.x
+= fabsf(s
->wind
)/2;
378 } else if (s
->ball
.move_x
< 0 && s
->wind
< 0) {
379 s
->ball
.x
-= fabsf(s
->wind
)/2;
382 s
->ball
.x
+= s
->ball
.move_x
;
383 s
->ball
.y
+= s
->ball
.move_y
;
385 if(s
->player1
.state
) s
->player1
.state
--;
386 if(s
->player2
.state
) s
->player2
.state
--;
391 void render( GameState
* s
) {
393 #ifdef EXTENDED_REFEREE
396 if (s
->play_sound
!= SOUND_MAX
) {
397 play_sample(s
->play_sound
);
398 s
->play_sound
= SOUND_MAX
;
400 if( s
->winner
!= WINNER_NONE
) {
407 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);
408 sprintf( s
->game_score_str
, "player %d wins the match with %s", s
->winner
, format_sets( s
));
409 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
);
413 if (s
->old_court_type
!= s
->court_type
) {
415 fill_image(s
->court_type
, 120, 120, 400, 250);
416 show_image(GR_COURT
, 0, 0, 255);
417 s
->old_court_type
= s
->court_type
;
420 #ifdef EXTENDED_REFEREE
421 switch (s
->referee
) {
428 case REFEREE_PLAYER1
:
429 case REFEREE_PLAYER2
:
448 show_sprite( GR_REFEREE
, s
->referee
*3+t
, 12, 250, 10, 255);
449 if (voice_finished_flag
== 0) {
450 show_sprite(GR_TALK
, (s
->time
/150)%2, 2, 280, 45, 255);
453 show_sprite( GR_REFEREE
, s
->referee
, 4, 250, 10, 255);
455 show_image( GR_SHADOW
, s
->ball
.x
-BALL_X_MID
, s
->ball
.y
+ get_phase( s
) - BALL_Y_MID
, 255);
457 show_sprite( GR_RACKET
, (!s
->player1
.state
), 4, s
->player1
.x
-RACKET_X_MID
, s
->player1
.y
-RACKET_Y_MID
, 255);
458 show_sprite( GR_RACKET
, (!s
->player2
.state
)+2, 4, s
->player2
.x
-RACKET_X_MID
, s
->player2
.y
-RACKET_Y_MID
, 255);
460 if( s
->ball
.move_x
> 0) {
461 show_sprite( GR_BALL
, (s
->time
/100)%BALL_STATES
, BALL_STATES
, s
->ball
.x
-BALL_X_MID
, s
->ball
.y
-BALL_Y_MID
, 255);
462 } else if( s
->ball
.move_x
< 0) {
463 show_sprite( GR_BALL
, BALL_STATES
-1-(s
->time
/100)%BALL_STATES
, BALL_STATES
, s
->ball
.x
-BALL_X_MID
, s
->ball
.y
-BALL_Y_MID
, 255);
465 show_sprite( GR_BALL
, 0, BALL_STATES
, s
->ball
.x
-BALL_X_MID
, s
->ball
.y
-BALL_Y_MID
, 255);
468 /* Player 1's mouse rectangle */
469 if (!(s
->player1
.mouse_locked
)) {
470 rectangle(s
->player1
.x
-2+5, s
->player1
.mouse_y
-2, 4, 4, 255, 255, 255);
471 rectangle(s
->player1
.x
-1+5, s
->player1
.mouse_y
-1, 2, 2, 0, 0, 0);
474 font_draw_string( GR_DKC2_FONT
, s
->game_score_str
, 14, 14, 0, ANIMATION_NONE
);
475 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
);
477 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
);
479 for (i
=0; i
<s
->rain
; i
++) {
482 draw_line_faded(x
, y
, x
+10+s
->wind
*5, y
+30, 0, 0, 255, 100, 200, 255);
486 * Cheap-ish update of the whole screen. This can
487 * probably be optimized.
489 update_rect(0, 0, WIDTH
, HEIGHT
);
493 line_horiz( s
->player1
.y
, 255, 0, 0);
494 line_horiz( s
->player2
.y
, 0, 0, 255);
495 line_horiz( s
->ball
.y
, 0, 255, 0);
497 line_vert( s
->player1
.x
, 255, 0, 0);
498 line_vert( s
->player2
.x
, 0, 0, 255);
499 line_vert( s
->ball
.x
, 0, 255, 0);
501 line_horiz( s
->player1
.ball_dest
, 255, 0, 255);
502 line_horiz( s
->player2
.ball_dest
, 0, 255, 255);
504 line_horiz( GAME_Y_MIN
, 100, 100, 100);
505 line_horiz( GAME_Y_MAX
, 100, 100, 100);
510 fill_image_offset(GR_FOG2
, 0, 0, WIDTH
, HEIGHT
, -s
->time
/150-s
->windtime
/200, 0);
512 fill_image_offset(GR_FOG
, 0, 0, WIDTH
, HEIGHT
, -s
->time
/100-s
->windtime
/150, 20);
514 fill_image_offset(GR_FOG2
, 0, 0, WIDTH
, HEIGHT
, -s
->time
/180-s
->windtime
/180, 80);
516 fill_image_offset(GR_FOG
, 0, 0, WIDTH
, HEIGHT
, s
->time
/200-s
->windtime
/100, 0);
521 show_image(GR_NIGHT
, 0, 0, 255);
527 void limit_value( float* value
, float min
, float max
) {
530 } else if( *value
> max
) {
535 float get_phase( GameState
* s
) {
537 float x
, min
, max
, direction
;
542 direction
= s
->ball
.move_x
;
544 pos
= (direction
>0)?(1-GROUND_PHASE
):(GROUND_PHASE
);
546 fract
= (x
-min
)/(max
-min
);
550 return fabsf( cosf(PI
*fract
/2))*PHASE_AMP
*s
->ball
.jump
;
552 fract
= (pos
-fract
)/(1-pos
);
553 return fabsf( sinf(PI
*fract
/2))*PHASE_AMP
*s
->ball
.jump
;
557 float get_move_y( GameState
* s
, unsigned char player
) {
558 float pct
, dest
, x_len
, y_len
;
559 float py
, by
, pa
, move_x
;
561 py
= (player
==1)?(s
->player1
.y
):(s
->player2
.y
);
564 move_x
= s
->ball
.move_x
;
566 /* -1.0 .. 1.0 for racket hit position */
567 pct
= (by
-py
)/(pa
/2);
568 limit_value( &pct
, -1.0, 1.0);
570 /* Y destination for ball */
571 dest
= GAME_Y_MID
+ pct
*(GAME_Y_MAX
-GAME_Y_MIN
);
573 s
->player1
.ball_dest
= dest
;
575 s
->player2
.ball_dest
= dest
;
578 /* lengths for the ball's journey */
580 x_len
= fabsf(GAME_X_MAX
- s
->ball
.x
);
581 y_len
= dest
- by
+ MOVE_Y_SEED
-rand()%MOVE_Y_SEED
*2;
583 x_len
= s
->ball
.x
- GAME_X_MIN
;
584 y_len
= by
- dest
+ MOVE_Y_SEED
-rand()%MOVE_Y_SEED
*2;
587 /* return the should-be value for move_y */
588 return (y_len
*move_x
)/(x_len
);
591 void input_human( Player
* player
, bool up
, bool down
, bool hit
, bool use_mouse
, GameState
* s
) {
592 int diff
= PLAYER_MOVE_Y
;
596 * Only use mouse control if the user isn't pressing any buttons
597 * this way, keyboard control still works when mouse control is
600 if (use_mouse
&& (down
|| up
|| hit
)) {
602 * this is here so if the user decides to play
603 * with keyboard controls, we will lock the
604 * mouse and disable displaying the mouse cursor
606 player
->mouse_locked
= true;
608 if (use_mouse
&& !down
&& !up
&& !hit
) {
609 mb
= SDL_GetMouseState(&(player
->mouse_x
), &(player
->mouse_y
));
610 if (mb
&SDL_BUTTON(SDL_BUTTON_LEFT
)) {
611 if (player
->mouse_y
< player
->y
) {
614 diff
= (player
->y
-player
->mouse_y
<diff
)?(player
->y
-player
->mouse_y
):(diff
);
615 } else if (player
->mouse_y
> player
->y
) {
618 diff
= (player
->mouse_y
-player
->y
<diff
)?(player
->mouse_y
-player
->y
):(diff
);
620 player
->mouse_locked
= false;
621 } else if (!player
->mouse_locked
) {
626 if (fabsf(s
->joystick_y
) > JOYSTICK_TRESHOLD
) {
627 diff
= PLAYER_MOVE_Y
*fabsf(s
->joystick_y
)/40.0;
628 if (diff
> PLAYER_MOVE_Y
) {
629 diff
= PLAYER_MOVE_Y
;
634 player
->y
-= fminf(diff
, diff
*player
->accelerate
);
635 player
->accelerate
*= PLAYER_ACCEL_INCREASE
;
637 player
->y
+= fminf(diff
, diff
*player
->accelerate
);
638 player
->accelerate
*= PLAYER_ACCEL_INCREASE
;
640 player
->accelerate
= PLAYER_ACCEL_DEFAULT
;
644 if( !player
->state
&& !player
->state_locked
) {
645 player
->state
= PLAYER_STATE_MAX
;
646 player
->state_locked
= true;
649 player
->state_locked
= false;
653 void input_ai( Player
* player
, Ball
* ball
, Player
* opponent
, GameState
* s
) {
657 if( fabsf( player
->y
- ball
->y
) > RACKET_Y_MID
*5) {
661 target
= GAME_Y_MID
+ (opponent
->ball_dest
- GAME_Y_MID
)/5;
663 if( player
->responsible
) {
664 if( player
->desire
== DESIRE_NORMAL
&& !IS_NEAR_Y_AI( player
->y
, ball
->y
)) {
665 if( player
->y
< ball
->y
) {
666 player
->y
+= fmin( 2*fact
, ball
->y
- player
->y
);
667 } else if( player
->y
> ball
->y
) {
668 player
->y
-= fmin( 2*fact
, player
->y
- ball
->y
);
672 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==0) {
673 player
->state
= PLAYER_STATE_MAX
;
675 } else if( ball
->move_x
== 0) {
676 if( player
->desire
== DESIRE_NORMAL
&& !IS_NEAR_Y_AI( player
->y
, target
)) {
677 if( player
->y
< target
) {
678 player
->y
+= fmin( fact
, (target
-player
->y
)/40.0);
679 } else if( player
->y
> target
) {
680 player
->y
-= fmin( fact
, (player
->y
-target
)/40.0);
683 } else if( s
->ngram_prediction
> 0.0) {
684 target
= s
->ngram_prediction
*((float)HEIGHT
)/((float)(NGRAM_STEPS
));
685 target
= GAME_Y_MID
+ (target
-GAME_Y_MID
)*1.5;
687 if( player
->desire
== DESIRE_NORMAL
&& !IS_NEAR_Y_AI( player
->y
, target
)) {
688 if( player
->y
< target
) {
689 player
->y
+= fmin( fact
, (target
-player
->y
)/40.0);
690 } else if( player
->y
> target
) {
691 player
->y
-= fmin( fact
, (player
->y
-target
)/40.0);
695 if( player->desire == DESIRE_NORMAL) {
696 if( !IS_NEAR_Y_AI( player->y, target)) {
697 player->y += (target - player->y)/40.0;
703 void game_setup_serve( GameState
* s
) {
705 s
->ball
.y
= GAME_Y_MID
;
706 s
->ball
.move_x
= 0.0;
707 s
->ball
.move_y
= 0.0;
709 if( s
->player1_serves
) {
710 s
->player1
.responsible
= true;
711 s
->player1
.ball_dest
= 0.0;
712 s
->ball
.x
= GAME_X_MIN
-RACKET_X_MID
*1.5;
714 s
->player1
.responsible
= false;
715 s
->player2
.ball_dest
= 0.0;
716 s
->ball
.x
= GAME_X_MAX
+RACKET_X_MID
*1.5;
719 s
->player2
.responsible
= !(s
->player1
.responsible
);
722 float ngram_predictor( GameState
* s
) {
723 unsigned int count
= 0;
724 unsigned long sum
= 0;
728 if( s
->history_size
< 3) {
735 for( z
= 0; z
<NGRAM_STEPS
; z
++) {
736 count
+= s
->ngram
[x
][y
][z
];
737 sum
+= z
* s
->ngram
[x
][y
][z
];
740 result
= ((float)(sum
))/((float)(count
));
742 printf( "predicting next = %.2f\n", result
);
748 void score_game( GameState
* s
, bool player1_scored
) {
749 Player
* winner
= (player1_scored
)?(&(s
->player1
)):(&(s
->player2
));
750 Player
* loser
= (player1_scored
)?(&(s
->player2
)):(&(s
->player1
));
752 if( s
->current_set
>= SETS_TO_WIN
*2-1) {
757 if( loser
->game
< winner
->game
-1) {
758 if( winner
->game
>= 4) {
759 winner
->game
= loser
->game
= 0;
760 winner
->sets
[s
->current_set
]++;
762 #ifdef HAVE_VOICE_FILES
763 /* speak the current score */
764 voice_say_list(4, VOICE_ZERO_IN
+ (s
->player1
.sets
[s
->current_set
])*2, VOICE_TO
, VOICE_ZERO_OUT
+ (s
->player2
.sets
[s
->current_set
])*2, VOICE_IN_THE_FIRST_SET
+s
->current_set
);
767 /* scoring the set.. */
768 if( (winner
->sets
[s
->current_set
] == 6 && loser
->sets
[s
->current_set
] < 5) ||
769 winner
->sets
[s
->current_set
] == 7) {
771 s
->winner
= game_get_winner( s
);
777 char* format_sets( GameState
* s
) {
778 static char sets
[100];
779 static char tmp
[100];
780 int i
, max
= s
->current_set
;
784 if( s
->winner
!= WINNER_NONE
) {
787 for( i
=0; i
<=max
; i
++) {
788 sprintf( tmp
, "%d:%d, ", s
->player1
.sets
[i
], s
->player2
.sets
[i
]);
792 sets
[strlen(sets
)-2] = '\0';
797 char* format_game( GameState
* s
) {
798 static char game
[100];
799 static const int game_scoring
[] = { 0, 15, 30, 40 };
801 if( s
->player1
.game
< 4 && s
->player2
.game
< 4) {
802 #ifdef HAVE_VOICE_FILES
803 if (s
->player1
.game
> 0 || s
->player2
.game
> 0) {
804 if (s
->player1
.game
== s
->player2
.game
) {
805 voice_say_list(2, VOICE_LOVE_IN
+ 2*(s
->player1
.game
), VOICE_ALL
);
807 voice_say_list(2, VOICE_LOVE_IN
+ 2*(s
->player1
.game
), VOICE_LOVE_OUT
+ 2*(s
->player2
.game
));
811 sprintf( game
, "%d - %d", game_scoring
[s
->player1
.game
], game_scoring
[s
->player2
.game
]);
812 } else if( s
->player1
.game
> s
->player2
.game
) {
813 #ifdef HAVE_VOICE_FILES
814 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_ONE
);
816 strcpy( game
, "advantage player 1");
817 } else if( s
->player1
.game
< s
->player2
.game
) {
818 #ifdef HAVE_VOICE_FILES
819 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_TWO
);
821 strcpy( game
, "advantage player 2");
823 #ifdef HAVE_VOICE_FILES
824 voice_say_list(1, VOICE_DEUCE
);
826 strcpy( game
, "deuce");
832 char* format_status( GameState
* s
) {
833 static char status
[100];
834 static const char* set_names
[] = { "first", "second", "third", "fourth", "fifth" };
836 sprintf( status
, "%d:%d in %s set", s
->player1
.sets
[s
->current_set
], s
->player2
.sets
[s
->current_set
], set_names
[s
->current_set
]);
841 int game_get_winner( GameState
* s
) {
845 for( i
=0; i
<s
->current_set
; i
++) {
846 if( s
->player1
.sets
[i
] > s
->player2
.sets
[i
]) {
853 if( sets
[0] == SETS_TO_WIN
) return WINNER_PLAYER1
;
854 if( sets
[1] == SETS_TO_WIN
) return WINNER_PLAYER2
;