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,
33 void game( bool singleplayer
) {
35 { 0, 0, 0.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 },
43 "welcome to tennix " VERSION
,
63 strcpy( s
.game_score_str
, format_game( &s
));
64 strcpy( s
.sets_score_str
, format_sets( &s
));
66 Uint32 ot
= SDL_GetTicks();
68 Uint32 dt
= GAME_TICKS
;
70 Uint32 accumulator
= 0;
74 #ifdef ENABLE_FPS_LIMIT
75 Uint32 ft
, frames
; /* frame timer and frames */
80 s
.player1
.type
= PLAYER_TYPE_AI
;
82 s
.player2
.type
= PLAYER_TYPE_AI
;
85 game_setup_serve( &s
);
88 for( x
= 0; x
<NGRAM_STEPS
; x
++) {
89 for( y
= 0; y
<NGRAM_STEPS
; y
++) {
90 for( z
= 0; z
<NGRAM_STEPS
; z
++) {
96 #ifdef ENABLE_FPS_LIMIT
110 while( accumulator
>= dt
) {
117 s
.was_stopped
= false;
121 #ifdef ENABLE_FPS_LIMIT
122 while (frames
*1000.0/((float)(SDL_GetTicks()-ft
+1))>(float)(DEFAULT_FPS
)) {
133 bool step( GameState
* s
) {
137 if( get_phase( s
) < 1.0) {
138 if( !s
->ground
.jump
) {
139 s
->play_sound
= SOUND_GROUND
;
141 if( IS_OUT_Y( s
->ball
.y
)) {
142 /* out - responsibilities stay the same */
144 s
->play_sound
= SOUND_OUT
;
145 s
->referee
= REFEREE_OUT
;
147 /* not out - responsibilities change */
148 s
->player1
.responsible
= !(s
->player2
.responsible
= !s
->player2
.responsible
);
149 s
->status
= format_status( s
);
150 s
->referee
= REFEREE_NORMAL
;
154 s
->ground
.x
= s
->ball
.x
;
155 s
->ground
.y
= s
->ball
.y
;
157 if( s
->ground
.jump
&& !(s
->time
%5)) s
->ground
.jump
--;
160 if( IS_OUT_X(s
->ball
.x
) || IS_OFFSCREEN_Y(s
->ball
.y
)) {
161 if( IS_OFFSCREEN( s
->ball
.x
, s
->ball
.y
)) {
162 s
->player1_serves
= s
->player1
.responsible
;
164 score_game( s
, s
->player2
.responsible
);
165 strcpy( s
->game_score_str
, format_game( s
));
166 strcpy( s
->sets_score_str
, format_sets( s
));
168 if( s
->player1
.responsible
) {
169 if( s
->player1
.type
== PLAYER_TYPE_HUMAN
&& s
->player2
.type
== PLAYER_TYPE_AI
) {
170 s
->status
= "computer scores";
172 s
->status
= "player 2 scores";
174 s
->referee
= REFEREE_PLAYER2
;
176 if( s
->player1
.type
== PLAYER_TYPE_HUMAN
&& s
->player2
.type
== PLAYER_TYPE_AI
) {
177 s
->status
= "player scores";
179 s
->status
= "player 1 scores";
181 s
->referee
= REFEREE_PLAYER1
;
184 game_setup_serve( s
);
185 s
->play_sound
= SOUND_APPLAUSE
;
188 s
->was_stopped
= true;
190 s
->history_is_locked
= 0;
191 s
->ngram_prediction
= 0.0;
193 printf( "-- game reset --\n");
197 if( IS_OUT_X(s
->ball
.x
)) {
198 if( !s
->history_is_locked
&& s
->referee
!= REFEREE_OUT
) {
199 s
->history
[s
->history_size
] = (int)(NGRAM_STEPS
*s
->ball
.y
/HEIGHT
);
201 if( s
->history_size
== 3) {
202 s
->ngram
[s
->history
[0]][s
->history
[1]][s
->history
[2]] += 10;
204 printf( "history: %d, %d, %d\n", s
->history
[0], s
->history
[1], s
->history
[2]);
206 s
->ngram_prediction
= ngram_predictor( s
);
207 s
->history
[0] = s
->history
[1];
208 s
->history
[1] = s
->history
[2];
211 s
->history_is_locked
= true;
213 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
) {
214 s
->ball
.x
= GAME_X_MIN
;
215 if( s
->player1
.state
== PLAYER_STATE_MAX
) {
216 s
->ball
.move_x
= PLAYER_POWERSHOT
;
218 s
->ball
.move_x
= 2.5 + 2.0*s
->player1
.state
/PLAYER_STATE_MAX
;
220 s
->ball
.move_y
= get_move_y( s
, 1);
221 s
->player2
.responsible
= !(s
->player1
.responsible
= 1);
222 s
->ball
.jump
+= 1.0-2.0*(s
->player1
.state
<5);
223 s
->play_sound
= SOUND_RACKET
;
224 pan_sample(SOUND_RACKET
, 0.4);
225 } 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
) {
226 s
->ball
.x
= GAME_X_MAX
;
227 if( s
->player2
.state
== PLAYER_STATE_MAX
) {
228 s
->ball
.move_x
= -PLAYER_POWERSHOT
;
230 s
->ball
.move_x
= -(2.5 + 2.0*s
->player2
.state
/PLAYER_STATE_MAX
);
232 s
->ball
.move_y
= get_move_y( s
, 2);
233 s
->player1
.responsible
= !(s
->player2
.responsible
= 1);
234 s
->ball
.jump
+= 1.0-2.0*(s
->player2
.state
<5);
235 s
->play_sound
= SOUND_RACKET
;
236 pan_sample(SOUND_RACKET
, 0.6);
240 s
->history_is_locked
= false;
244 keys
= SDL_GetKeyState( NULL
);
246 case SDL_JOYAXISMOTION
:
247 if (e
.jaxis
.axis
== JOYSTICK_Y_AXIS
) {
248 s
->joystick_y
= JOYSTICK_PERCENTIZE(e
.jaxis
.value
);
249 } else if (e
.jaxis
.axis
== JOYSTICK_X_AXIS
) {
250 s
->joystick_x
= JOYSTICK_PERCENTIZE(e
.jaxis
.value
);
253 case SDL_JOYBUTTONUP
: case SDL_JOYBUTTONDOWN
:
254 if (e
.jbutton
.button
== JOYSTICK_BUTTON_A
) {
255 s
->joystick_a
= (e
.jbutton
.state
== SDL_PRESSED
);
260 if( keys
['c'] && s
->time
%50==0) {
262 if( s
->court_type
> GR_CTT_LAST
) {
263 s
->court_type
= GR_CTT_FIRST
;
267 if( !is_fading() && !s
->is_over
) {
268 if( s
->player1
.type
== PLAYER_TYPE_HUMAN
) {
269 input_human( &s
->player1
,
270 keys
['w'] || keys
[SDLK_UP
] || s
->joystick_y
< -JOYSTICK_TRESHOLD
,
271 keys
['s'] || keys
[SDLK_DOWN
] || s
->joystick_y
> JOYSTICK_TRESHOLD
,
272 keys
['d'] || keys
[SDLK_SPACE
] || keys
[SDLK_LCTRL
] || s
->joystick_a
,
280 input_ai( &s
->player1
, &s
->ball
, &s
->player2
, s
);
283 if( s
->player2
.type
== PLAYER_TYPE_HUMAN
) {
284 input_human( &s
->player2
, keys
['o'], keys
['l'], keys
['k'], false, s
);
286 input_ai( &s
->player2
, &s
->ball
, &s
->player1
, s
);
290 if( keys
['f']) SDL_WM_ToggleFullScreen( screen
);
291 if( keys
['y']) SDL_SaveBMP( screen
, "screenshot.bmp");
296 keys
= SDL_GetKeyState( NULL
);
299 while( keys
['p'] == 0) {
301 keys
= SDL_GetKeyState( NULL
);
306 keys
= SDL_GetKeyState( NULL
);
309 s
->was_stopped
= true;
312 if( keys
[SDLK_ESCAPE
] || keys
['q']) return true;
314 limit_value( &s
->player1
.y
, PLAYER_Y_MIN
, PLAYER_Y_MAX
);
315 limit_value( &s
->player2
.y
, PLAYER_Y_MIN
, PLAYER_Y_MAX
);
316 limit_value( &s
->ball
.jump
, BALL_JUMP_MIN
, BALL_JUMP_MAX
);
318 /* Update ball_dest for debugging purposes */
322 s
->ball
.x
+= s
->ball
.move_x
;
323 s
->ball
.y
+= s
->ball
.move_y
;
325 if(s
->player1
.state
) s
->player1
.state
--;
326 if(s
->player2
.state
) s
->player2
.state
--;
331 void render( GameState
* s
) {
332 if (s
->play_sound
!= SOUND_MAX
) {
333 play_sample(s
->play_sound
);
334 s
->play_sound
= SOUND_MAX
;
336 if( s
->winner
!= WINNER_NONE
) {
343 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);
344 sprintf( s
->game_score_str
, "player %d wins the match with %s", s
->winner
, format_sets( s
));
345 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
);
349 if (s
->old_court_type
!= s
->court_type
) {
351 fill_image(s
->court_type
, 120, 120, 400, 250);
352 show_image(GR_COURT
, 0, 0, 255);
353 s
->old_court_type
= s
->court_type
;
356 show_sprite( GR_REFEREE
, s
->referee
, 4, 250, 10, 255);
357 show_image( GR_SHADOW
, s
->ball
.x
-BALL_X_MID
, s
->ball
.y
+ get_phase( s
) - BALL_Y_MID
, 255);
359 show_sprite( GR_RACKET
, (!s
->player1
.state
), 4, s
->player1
.x
-RACKET_X_MID
, s
->player1
.y
-RACKET_Y_MID
, 255);
360 show_sprite( GR_RACKET
, (!s
->player2
.state
)+2, 4, s
->player2
.x
-RACKET_X_MID
, s
->player2
.y
-RACKET_Y_MID
, 255);
362 if( s
->ground
.jump
) {
363 show_sprite( GR_GROUND
, s
->ground
.jump
-1, 3, s
->ground
.x
- BALL_X_MID
, s
->ground
.y
- BALL_Y_MID
, 128);
366 if( s
->ball
.move_x
> 0) {
367 show_sprite( GR_BALL
, (s
->time
/500)%4, 4, s
->ball
.x
-BALL_X_MID
, s
->ball
.y
-BALL_Y_MID
, 255);
368 } else if( s
->ball
.move_x
< 0) {
369 show_sprite( GR_BALL
, 3-(s
->time
/500)%4, 4, s
->ball
.x
-BALL_X_MID
, s
->ball
.y
-BALL_Y_MID
, 255);
371 show_sprite( GR_BALL
, 0, 4, s
->ball
.x
-BALL_X_MID
, s
->ball
.y
-BALL_Y_MID
, 255);
374 /* Player 1's mouse rectangle */
375 if (!(s
->player1
.mouse_locked
)) {
376 rectangle(s
->player1
.x
-2+5, s
->player1
.mouse_y
-2, 4, 4, 255, 255, 255);
377 rectangle(s
->player1
.x
-1+5, s
->player1
.mouse_y
-1, 2, 2, 0, 0, 0);
380 font_draw_string( GR_DKC2_FONT
, s
->game_score_str
, 14, 14, 0, ANIMATION_NONE
);
381 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
);
383 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
);
386 line_horiz( s
->player1
.y
, 255, 0, 0);
387 line_horiz( s
->player2
.y
, 0, 0, 255);
388 line_horiz( s
->ball
.y
, 0, 255, 0);
390 line_vert( s
->player1
.x
, 255, 0, 0);
391 line_vert( s
->player2
.x
, 0, 0, 255);
392 line_vert( s
->ball
.x
, 0, 255, 0);
394 line_horiz( s
->player1
.ball_dest
, 255, 0, 255);
395 line_horiz( s
->player2
.ball_dest
, 0, 255, 255);
397 line_horiz( GAME_Y_MIN
, 100, 100, 100);
398 line_horiz( GAME_Y_MAX
, 100, 100, 100);
404 void limit_value( float* value
, float min
, float max
) {
407 } else if( *value
> max
) {
412 float get_phase( GameState
* s
) {
414 float x
, min
, max
, direction
;
419 direction
= s
->ball
.move_x
;
421 pos
= (direction
>0)?(1-GROUND_PHASE
):(GROUND_PHASE
);
423 fract
= (x
-min
)/(max
-min
);
427 return fabsf( cosf(PI
*fract
/2))*PHASE_AMP
*s
->ball
.jump
;
429 fract
= (pos
-fract
)/(1-pos
);
430 return fabsf( sinf(PI
*fract
/2))*PHASE_AMP
*s
->ball
.jump
;
434 float get_move_y( GameState
* s
, unsigned char player
) {
435 float pct
, dest
, x_len
, y_len
;
436 float py
, by
, pa
, move_x
;
438 py
= (player
==1)?(s
->player1
.y
):(s
->player2
.y
);
441 move_x
= s
->ball
.move_x
;
443 /* -1.0 .. 1.0 for racket hit position */
444 pct
= (by
-py
)/(pa
/2);
445 limit_value( &pct
, -1.0, 1.0);
447 /* Y destination for ball */
448 dest
= GAME_Y_MID
+ pct
*(GAME_Y_MAX
-GAME_Y_MIN
);
450 s
->player1
.ball_dest
= dest
;
452 s
->player2
.ball_dest
= dest
;
455 /* lengths for the ball's journey */
457 x_len
= fabsf(GAME_X_MAX
- s
->ball
.x
);
458 y_len
= dest
- by
+ MOVE_Y_SEED
-rand()%MOVE_Y_SEED
*2;
460 x_len
= s
->ball
.x
- GAME_X_MIN
;
461 y_len
= by
- dest
+ MOVE_Y_SEED
-rand()%MOVE_Y_SEED
*2;
464 /* return the should-be value for move_y */
465 return (y_len
*move_x
)/(x_len
);
468 void input_human( Player
* player
, bool up
, bool down
, bool hit
, bool use_mouse
, GameState
* s
) {
469 int diff
= PLAYER_MOVE_Y
;
473 * Only use mouse control if the user isn't pressing any buttons
474 * this way, keyboard control still works when mouse control is
477 if (use_mouse
&& (down
|| up
|| hit
)) {
479 * this is here so if the user decides to play
480 * with keyboard controls, we will lock the
481 * mouse and disable displaying the mouse cursor
483 player
->mouse_locked
= true;
485 if (use_mouse
&& !down
&& !up
&& !hit
) {
486 mb
= SDL_GetMouseState(&(player
->mouse_x
), &(player
->mouse_y
));
487 if (mb
&SDL_BUTTON(SDL_BUTTON_LEFT
)) {
488 if (player
->mouse_y
< player
->y
) {
491 diff
= (player
->y
-player
->mouse_y
<diff
)?(player
->y
-player
->mouse_y
):(diff
);
492 } else if (player
->mouse_y
> player
->y
) {
495 diff
= (player
->mouse_y
-player
->y
<diff
)?(player
->mouse_y
-player
->y
):(diff
);
497 player
->mouse_locked
= false;
498 } else if (!player
->mouse_locked
) {
503 if (fabsf(s
->joystick_y
) > JOYSTICK_TRESHOLD
) {
504 diff
= PLAYER_MOVE_Y
*fabsf(s
->joystick_y
)/40.0;
505 if (diff
> PLAYER_MOVE_Y
) {
506 diff
= PLAYER_MOVE_Y
;
511 player
->y
-= fminf(diff
, diff
*player
->accelerate
);
512 player
->accelerate
*= PLAYER_ACCEL_INCREASE
;
514 player
->y
+= fminf(diff
, diff
*player
->accelerate
);
515 player
->accelerate
*= PLAYER_ACCEL_INCREASE
;
517 player
->accelerate
= PLAYER_ACCEL_DEFAULT
;
521 if( !player
->state
&& !player
->state_locked
) {
522 player
->state
= PLAYER_STATE_MAX
;
523 player
->state_locked
= true;
526 player
->state_locked
= false;
530 void input_ai( Player
* player
, Ball
* ball
, Player
* opponent
, GameState
* s
) {
534 if( fabsf( player
->y
- ball
->y
) > RACKET_Y_MID
*5) {
538 target
= GAME_Y_MID
+ (opponent
->ball_dest
- GAME_Y_MID
)/5;
540 if( player
->responsible
) {
541 if( player
->desire
== DESIRE_NORMAL
&& !IS_NEAR_Y_AI( player
->y
, ball
->y
)) {
542 if( player
->y
< ball
->y
) {
543 player
->y
+= fmin( 2*fact
, ball
->y
- player
->y
);
544 } else if( player
->y
> ball
->y
) {
545 player
->y
-= fmin( 2*fact
, player
->y
- ball
->y
);
549 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) {
550 player
->state
= PLAYER_STATE_MAX
;
552 } else if( ball
->move_x
== 0) {
553 if( player
->desire
== DESIRE_NORMAL
&& !IS_NEAR_Y_AI( player
->y
, target
)) {
554 if( player
->y
< target
) {
555 player
->y
+= fmin( fact
, (target
-player
->y
)/40.0);
556 } else if( player
->y
> target
) {
557 player
->y
-= fmin( fact
, (player
->y
-target
)/40.0);
560 } else if( s
->ngram_prediction
> 0.0) {
561 target
= s
->ngram_prediction
*((float)HEIGHT
)/((float)(NGRAM_STEPS
));
562 target
= GAME_Y_MID
+ (target
-GAME_Y_MID
)*1.5;
564 if( player
->desire
== DESIRE_NORMAL
&& !IS_NEAR_Y_AI( player
->y
, target
)) {
565 if( player
->y
< target
) {
566 player
->y
+= fmin( fact
, (target
-player
->y
)/40.0);
567 } else if( player
->y
> target
) {
568 player
->y
-= fmin( fact
, (player
->y
-target
)/40.0);
572 if( player->desire == DESIRE_NORMAL) {
573 if( !IS_NEAR_Y_AI( player->y, target)) {
574 player->y += (target - player->y)/40.0;
580 void game_setup_serve( GameState
* s
) {
582 s
->ball
.y
= GAME_Y_MID
;
583 s
->ball
.move_x
= 0.0;
584 s
->ball
.move_y
= 0.0;
586 if( s
->player1_serves
) {
587 s
->player1
.responsible
= true;
588 s
->player1
.ball_dest
= 0.0;
589 s
->ball
.x
= GAME_X_MIN
-RACKET_X_MID
*1.5;
591 s
->player1
.responsible
= false;
592 s
->player2
.ball_dest
= 0.0;
593 s
->ball
.x
= GAME_X_MAX
+RACKET_X_MID
*1.5;
596 s
->player2
.responsible
= !(s
->player1
.responsible
);
599 float ngram_predictor( GameState
* s
) {
600 unsigned int count
= 0;
601 unsigned long sum
= 0;
605 if( s
->history_size
< 3) {
612 for( z
= 0; z
<NGRAM_STEPS
; z
++) {
613 count
+= s
->ngram
[x
][y
][z
];
614 sum
+= z
* s
->ngram
[x
][y
][z
];
617 result
= ((float)(sum
))/((float)(count
));
619 printf( "predicting next = %.2f\n", result
);
625 void score_game( GameState
* s
, bool player1_scored
) {
626 Player
* winner
= (player1_scored
)?(&(s
->player1
)):(&(s
->player2
));
627 Player
* loser
= (player1_scored
)?(&(s
->player2
)):(&(s
->player1
));
629 if( s
->current_set
>= SETS_TO_WIN
*2-1) {
634 if( loser
->game
< winner
->game
-1) {
635 if( winner
->game
>= 4) {
636 winner
->game
= loser
->game
= 0;
637 winner
->sets
[s
->current_set
]++;
638 /* scoring the set.. */
639 if( (winner
->sets
[s
->current_set
] == 6 && loser
->sets
[s
->current_set
] < 5) ||
640 winner
->sets
[s
->current_set
] == 7) {
642 s
->winner
= game_get_winner( s
);
648 char* format_sets( GameState
* s
) {
649 static char sets
[100];
650 static char tmp
[100];
651 int i
, max
= s
->current_set
;
655 if( s
->winner
!= WINNER_NONE
) {
658 for( i
=0; i
<=max
; i
++) {
659 sprintf( tmp
, "%d:%d, ", s
->player1
.sets
[i
], s
->player2
.sets
[i
]);
663 sets
[strlen(sets
)-2] = '\0';
668 char* format_game( GameState
* s
) {
669 static char game
[100];
670 static const int game_scoring
[] = { 0, 15, 30, 40 };
672 if( s
->player1
.game
< 4 && s
->player2
.game
< 4) {
673 sprintf( game
, "%d - %d", game_scoring
[s
->player1
.game
], game_scoring
[s
->player2
.game
]);
674 } else if( s
->player1
.game
> s
->player2
.game
) {
675 strcpy( game
, "advantage player 1");
676 } else if( s
->player1
.game
< s
->player2
.game
) {
677 strcpy( game
, "advantage player 2");
679 strcpy( game
, "deuce");
685 char* format_status( GameState
* s
) {
686 static char status
[100];
687 static const char* set_names
[] = { "first", "second", "third", "fourth", "fifth" };
689 sprintf( status
, "%d:%d in %s set", s
->player1
.sets
[s
->current_set
], s
->player2
.sets
[s
->current_set
], set_names
[s
->current_set
]);
694 int game_get_winner( GameState
* s
) {
698 for( i
=0; i
<s
->current_set
; i
++) {
699 if( s
->player1
.sets
[i
] > s
->player2
.sets
[i
]) {
706 if( sets
[0] == SETS_TO_WIN
) return WINNER_PLAYER1
;
707 if( sets
[1] == SETS_TO_WIN
) return WINNER_PLAYER2
;