5 * Copyright (C) 2003, 2007 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
, false },
38 { GAME_X_MAX
+RACKET_X_MID
*2, GAME_Y_MID
, 0, 0, 0, DESIRE_NORMAL
, PLAYER_TYPE_HUMAN
, false },
43 "welcome to tennix " VERSION
,
62 strcpy( s
.game_score_str
, format_game( &s
));
63 strcpy( s
.sets_score_str
, format_sets( &s
));
65 Uint32 ot
= SDL_GetTicks();
67 Uint32 dt
= GAME_TICKS
;
69 Uint32 accumulator
= 0;
75 s
.player1
.type
= PLAYER_TYPE_AI
;
77 s
.player2
.type
= PLAYER_TYPE_AI
;
80 game_setup_serve( &s
);
83 for( x
= 0; x
<NGRAM_STEPS
; x
++) {
84 for( y
= 0; y
<NGRAM_STEPS
; y
++) {
85 for( z
= 0; z
<NGRAM_STEPS
; z
++) {
101 while( accumulator
>= dt
) {
108 s
.was_stopped
= false;
117 bool step( GameState
* s
) {
121 if( get_phase( s
) < 1.0) {
122 if( !s
->ground
.jump
) {
123 s
->play_sound
= SOUND_GROUND
;
125 if( IS_OUT_Y( s
->ball
.y
)) {
126 /* out - responsibilities stay the same */
128 s
->play_sound
= SOUND_OUT
;
129 s
->referee
= REFEREE_OUT
;
131 /* not out - responsibilities change */
132 s
->player1
.responsible
= !(s
->player2
.responsible
= !s
->player2
.responsible
);
133 s
->status
= format_status( s
);
134 s
->referee
= REFEREE_NORMAL
;
138 s
->ground
.x
= s
->ball
.x
;
139 s
->ground
.y
= s
->ball
.y
;
141 if( s
->ground
.jump
&& !(s
->time
%5)) s
->ground
.jump
--;
144 if( IS_OUT_X(s
->ball
.x
) || IS_OFFSCREEN_Y(s
->ball
.y
)) {
145 if( IS_OFFSCREEN( s
->ball
.x
, s
->ball
.y
)) {
146 s
->player1_serves
= s
->player1
.responsible
;
148 score_game( s
, s
->player2
.responsible
);
149 strcpy( s
->game_score_str
, format_game( s
));
150 strcpy( s
->sets_score_str
, format_sets( s
));
152 if( s
->player1
.responsible
) {
153 if( s
->player1
.type
== PLAYER_TYPE_HUMAN
&& s
->player2
.type
== PLAYER_TYPE_AI
) {
154 s
->status
= "computer scores";
156 s
->status
= "player 2 scores";
158 s
->referee
= REFEREE_PLAYER2
;
160 if( s
->player1
.type
== PLAYER_TYPE_HUMAN
&& s
->player2
.type
== PLAYER_TYPE_AI
) {
161 s
->status
= "player scores";
163 s
->status
= "player 1 scores";
165 s
->referee
= REFEREE_PLAYER1
;
168 game_setup_serve( s
);
169 s
->play_sound
= SOUND_APPLAUSE
;
172 s
->was_stopped
= true;
174 s
->history_is_locked
= 0;
175 s
->ngram_prediction
= 0.0;
177 printf( "-- game reset --\n");
181 if( IS_OUT_X(s
->ball
.x
)) {
182 if( !s
->history_is_locked
&& s
->referee
!= REFEREE_OUT
) {
183 s
->history
[s
->history_size
] = (int)(NGRAM_STEPS
*s
->ball
.y
/HEIGHT
);
185 if( s
->history_size
== 3) {
186 s
->ngram
[s
->history
[0]][s
->history
[1]][s
->history
[2]] += 10;
188 printf( "history: %d, %d, %d\n", s
->history
[0], s
->history
[1], s
->history
[2]);
190 s
->ngram_prediction
= ngram_predictor( s
);
191 s
->history
[0] = s
->history
[1];
192 s
->history
[1] = s
->history
[2];
195 s
->history_is_locked
= true;
197 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
) {
198 s
->ball
.x
= GAME_X_MIN
;
199 if( s
->player1
.state
== PLAYER_STATE_MAX
) {
200 s
->ball
.move_x
= PLAYER_POWERSHOT
;
202 s
->ball
.move_x
= 2.5 + 2.0*s
->player1
.state
/PLAYER_STATE_MAX
;
204 s
->ball
.move_y
= get_move_y( s
, 1);
205 s
->player2
.responsible
= !(s
->player1
.responsible
= 1);
206 s
->ball
.jump
+= 1.0-2.0*(s
->player1
.state
<5);
207 s
->play_sound
= SOUND_RACKET
;
208 pan_sample(SOUND_RACKET
, 0.4);
209 } 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
) {
210 s
->ball
.x
= GAME_X_MAX
;
211 if( s
->player2
.state
== PLAYER_STATE_MAX
) {
212 s
->ball
.move_x
= -PLAYER_POWERSHOT
;
214 s
->ball
.move_x
= -(2.5 + 2.0*s
->player2
.state
/PLAYER_STATE_MAX
);
216 s
->ball
.move_y
= get_move_y( s
, 2);
217 s
->player1
.responsible
= !(s
->player2
.responsible
= 1);
218 s
->ball
.jump
+= 1.0-2.0*(s
->player2
.state
<5);
219 s
->play_sound
= SOUND_RACKET
;
220 pan_sample(SOUND_RACKET
, 0.6);
224 s
->history_is_locked
= false;
227 /* Update ball_dest for debugging purposes */
231 s
->ball
.x
+= s
->ball
.move_x
;
232 s
->ball
.y
+= s
->ball
.move_y
;
234 if( s
->player1
.state
) s
->player1
.state
--;
235 if( s
->player2
.state
) s
->player2
.state
--;
238 keys
= SDL_GetKeyState( NULL
);
240 case SDL_JOYAXISMOTION
:
241 if (e
.jaxis
.axis
== JOYSTICK_Y_AXIS
) {
242 s
->joystick_y
= JOYSTICK_PERCENTIZE(e
.jaxis
.value
);
243 } else if (e
.jaxis
.axis
== JOYSTICK_X_AXIS
) {
244 s
->joystick_x
= JOYSTICK_PERCENTIZE(e
.jaxis
.value
);
247 case SDL_JOYBUTTONUP
: case SDL_JOYBUTTONDOWN
:
248 if (e
.jbutton
.button
== JOYSTICK_BUTTON_A
) {
249 s
->joystick_a
= (e
.jbutton
.state
== SDL_PRESSED
);
254 if( keys
['c'] && s
->time
%50==0) {
256 if( s
->court_type
> GR_CTT_LAST
) {
257 s
->court_type
= GR_CTT_FIRST
;
261 if( !is_fading() && !s
->is_over
) {
262 if( s
->player1
.type
== PLAYER_TYPE_HUMAN
) {
263 input_human( &s
->player1
,
264 keys
['w'] || keys
[SDLK_UP
] || s
->joystick_y
< -JOYSTICK_TRESHOLD
,
265 keys
['s'] || keys
[SDLK_DOWN
] || s
->joystick_y
> JOYSTICK_TRESHOLD
,
266 keys
['d'] || keys
[SDLK_SPACE
] || keys
[SDLK_LCTRL
] || s
->joystick_a
,
269 input_ai( &s
->player1
, &s
->ball
, &s
->player2
, s
);
272 if( s
->player2
.type
== PLAYER_TYPE_HUMAN
) {
273 input_human( &s
->player2
, keys
['o'], keys
['l'], keys
['k'], s
);
275 input_ai( &s
->player2
, &s
->ball
, &s
->player1
, s
);
279 if( keys
['f']) SDL_WM_ToggleFullScreen( screen
);
280 if( keys
['y']) SDL_SaveBMP( screen
, "screenshot.bmp");
285 keys
= SDL_GetKeyState( NULL
);
288 while( keys
['p'] == 0) {
290 keys
= SDL_GetKeyState( NULL
);
295 keys
= SDL_GetKeyState( NULL
);
298 s
->was_stopped
= true;
301 if( keys
[SDLK_ESCAPE
] || keys
['q']) return true;
303 limit_value( &s
->player1
.y
, PLAYER_Y_MIN
, PLAYER_Y_MAX
);
304 limit_value( &s
->player2
.y
, PLAYER_Y_MIN
, PLAYER_Y_MAX
);
305 limit_value( &s
->ball
.jump
, BALL_JUMP_MIN
, BALL_JUMP_MAX
);
310 void render( GameState
* s
) {
311 if (s
->play_sound
!= SOUND_MAX
) {
312 play_sample(s
->play_sound
);
313 s
->play_sound
= SOUND_MAX
;
315 if( s
->winner
!= WINNER_NONE
) {
321 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);
322 sprintf( s
->game_score_str
, "player %d wins the match with %s", s
->winner
, format_sets( s
));
323 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
);
328 fill_image( s
->court_type
, 120, 120, 400, 250);
329 show_image( GR_COURT
, 0, 0, 255);
330 show_sprite( GR_REFEREE
, s
->referee
, 4, 250, 10, 255);
331 show_image( GR_SHADOW
, s
->ball
.x
-BALL_X_MID
, s
->ball
.y
+ get_phase( s
) - BALL_Y_MID
, 255);
333 show_sprite( GR_RACKET
, (!s
->player1
.state
), 4, s
->player1
.x
-RACKET_X_MID
, s
->player1
.y
-RACKET_Y_MID
, 255);
334 show_sprite( GR_RACKET
, (!s
->player2
.state
)+2, 4, s
->player2
.x
-RACKET_X_MID
, s
->player2
.y
-RACKET_Y_MID
, 255);
336 if( s
->ground
.jump
) {
337 show_sprite( GR_GROUND
, s
->ground
.jump
-1, 3, s
->ground
.x
- BALL_X_MID
, s
->ground
.y
- BALL_Y_MID
, 128);
340 if( s
->ball
.move_x
> 0) {
341 show_sprite( GR_BALL
, (s
->time
/500)%4, 4, s
->ball
.x
-BALL_X_MID
, s
->ball
.y
-BALL_Y_MID
, 255);
342 } else if( s
->ball
.move_x
< 0) {
343 show_sprite( GR_BALL
, 3-(s
->time
/500)%4, 4, s
->ball
.x
-BALL_X_MID
, s
->ball
.y
-BALL_Y_MID
, 255);
345 show_sprite( GR_BALL
, 0, 4, s
->ball
.x
-BALL_X_MID
, s
->ball
.y
-BALL_Y_MID
, 255);
349 font_draw_string( GR_DKC2_FONT
, s
->game_score_str
, 14, 14, 0, ANIMATION_NONE
);
350 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
);
352 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
);
355 line_horiz( s
->player1
.y
, 255, 0, 0);
356 line_horiz( s
->player2
.y
, 0, 0, 255);
357 line_horiz( s
->ball
.y
, 0, 255, 0);
359 line_vert( s
->player1
.x
, 255, 0, 0);
360 line_vert( s
->player2
.x
, 0, 0, 255);
361 line_vert( s
->ball
.x
, 0, 255, 0);
363 line_horiz( s
->player1
.ball_dest
, 255, 0, 255);
364 line_horiz( s
->player2
.ball_dest
, 0, 255, 255);
366 line_horiz( GAME_Y_MIN
, 100, 100, 100);
367 line_horiz( GAME_Y_MAX
, 100, 100, 100);
373 void limit_value( float* value
, float min
, float max
) {
376 } else if( *value
> max
) {
381 float get_phase( GameState
* s
) {
383 float x
, min
, max
, direction
;
388 direction
= s
->ball
.move_x
;
390 pos
= (direction
>0)?(1-GROUND_PHASE
):(GROUND_PHASE
);
392 fract
= (x
-min
)/(max
-min
);
396 return fabsf( cosf(PI
*fract
/2))*PHASE_AMP
*s
->ball
.jump
;
398 fract
= (pos
-fract
)/(1-pos
);
399 return fabsf( sinf(PI
*fract
/2))*PHASE_AMP
*s
->ball
.jump
;
403 float get_move_y( GameState
* s
, unsigned char player
) {
404 float pct
, dest
, x_len
, y_len
;
405 float py
, by
, pa
, move_x
;
407 py
= (player
==1)?(s
->player1
.y
):(s
->player2
.y
);
410 move_x
= s
->ball
.move_x
;
412 /* -1.0 .. 1.0 for racket hit position */
413 pct
= (by
-py
)/(pa
/2);
414 limit_value( &pct
, -1.0, 1.0);
416 /* Y destination for ball */
417 dest
= GAME_Y_MID
+ pct
*(GAME_Y_MAX
-GAME_Y_MIN
);
419 s
->player1
.ball_dest
= dest
;
421 s
->player2
.ball_dest
= dest
;
424 /* lengths for the ball's journey */
426 x_len
= fabsf(GAME_X_MAX
- s
->ball
.x
);
427 y_len
= dest
- by
+ MOVE_Y_SEED
-rand()%MOVE_Y_SEED
*2;
429 x_len
= s
->ball
.x
- GAME_X_MIN
;
430 y_len
= by
- dest
+ MOVE_Y_SEED
-rand()%MOVE_Y_SEED
*2;
433 /* return the should-be value for move_y */
434 return (y_len
*move_x
)/(x_len
);
437 void input_human( Player
* player
, bool up
, bool down
, bool hit
, GameState
* s
) {
438 int diff
= PLAYER_MOVE_Y
;
440 if (fabsf(s
->joystick_y
) > JOYSTICK_TRESHOLD
) {
441 diff
= PLAYER_MOVE_Y
*fabsf(s
->joystick_y
)/40.0;
442 if (diff
> PLAYER_MOVE_Y
) {
443 diff
= PLAYER_MOVE_Y
;
456 if( !player
->state
&& !player
->state_locked
) {
457 player
->state
= PLAYER_STATE_MAX
;
458 player
->state_locked
= true;
461 player
->state_locked
= false;
465 void input_ai( Player
* player
, Ball
* ball
, Player
* opponent
, GameState
* s
) {
469 if( fabsf( player
->y
- ball
->y
) > RACKET_Y_MID
*5) {
473 target
= GAME_Y_MID
+ (opponent
->ball_dest
- GAME_Y_MID
)/5;
475 if( player
->responsible
) {
476 if( player
->desire
== DESIRE_NORMAL
&& !IS_NEAR_Y_AI( player
->y
, ball
->y
)) {
477 if( player
->y
< ball
->y
) {
478 player
->y
+= fmin( 2*fact
, ball
->y
- player
->y
);
479 } else if( player
->y
> ball
->y
) {
480 player
->y
-= fmin( 2*fact
, player
->y
- ball
->y
);
484 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) {
485 player
->state
= PLAYER_STATE_MAX
;
487 } else if( ball
->move_x
== 0) {
488 if( player
->desire
== DESIRE_NORMAL
&& !IS_NEAR_Y_AI( player
->y
, target
)) {
489 if( player
->y
< target
) {
490 player
->y
+= fmin( fact
, (target
-player
->y
)/40.0);
491 } else if( player
->y
> target
) {
492 player
->y
-= fmin( fact
, (player
->y
-target
)/40.0);
495 } else if( s
->ngram_prediction
> 0.0) {
496 target
= s
->ngram_prediction
*((float)HEIGHT
)/((float)(NGRAM_STEPS
));
497 target
= GAME_Y_MID
+ (target
-GAME_Y_MID
)*1.5;
499 if( player
->desire
== DESIRE_NORMAL
&& !IS_NEAR_Y_AI( player
->y
, target
)) {
500 if( player
->y
< target
) {
501 player
->y
+= fmin( fact
, (target
-player
->y
)/40.0);
502 } else if( player
->y
> target
) {
503 player
->y
-= fmin( fact
, (player
->y
-target
)/40.0);
507 if( player->desire == DESIRE_NORMAL) {
508 if( !IS_NEAR_Y_AI( player->y, target)) {
509 player->y += (target - player->y)/40.0;
515 void game_setup_serve( GameState
* s
) {
517 s
->ball
.y
= GAME_Y_MID
;
518 s
->ball
.move_x
= 0.0;
519 s
->ball
.move_y
= 0.0;
521 if( s
->player1_serves
) {
522 s
->player1
.responsible
= true;
523 s
->player1
.ball_dest
= 0.0;
524 s
->ball
.x
= GAME_X_MIN
-RACKET_X_MID
*1.5;
526 s
->player1
.responsible
= false;
527 s
->player2
.ball_dest
= 0.0;
528 s
->ball
.x
= GAME_X_MAX
+RACKET_X_MID
*1.5;
531 s
->player2
.responsible
= !(s
->player1
.responsible
);
534 float ngram_predictor( GameState
* s
) {
535 unsigned int count
= 0;
536 unsigned long sum
= 0;
540 if( s
->history_size
< 3) {
547 for( z
= 0; z
<NGRAM_STEPS
; z
++) {
548 count
+= s
->ngram
[x
][y
][z
];
549 sum
+= z
* s
->ngram
[x
][y
][z
];
552 result
= ((float)(sum
))/((float)(count
));
554 printf( "predicting next = %.2f\n", result
);
560 void score_game( GameState
* s
, bool player1_scored
) {
561 Player
* winner
= (player1_scored
)?(&(s
->player1
)):(&(s
->player2
));
562 Player
* loser
= (player1_scored
)?(&(s
->player2
)):(&(s
->player1
));
564 if( s
->current_set
>= SETS_TO_WIN
*2-1) {
569 if( loser
->game
< winner
->game
-1) {
570 if( winner
->game
>= 4) {
571 winner
->game
= loser
->game
= 0;
572 winner
->sets
[s
->current_set
]++;
573 /* scoring the set.. */
574 if( (winner
->sets
[s
->current_set
] == 6 && loser
->sets
[s
->current_set
] < 5) ||
575 winner
->sets
[s
->current_set
] == 7) {
577 s
->winner
= game_get_winner( s
);
583 char* format_sets( GameState
* s
) {
584 static char sets
[100];
585 static char tmp
[100];
586 int i
, max
= s
->current_set
;
590 if( s
->winner
!= WINNER_NONE
) {
593 for( i
=0; i
<=max
; i
++) {
594 sprintf( tmp
, "%d:%d, ", s
->player1
.sets
[i
], s
->player2
.sets
[i
]);
598 sets
[strlen(sets
)-2] = '\0';
603 char* format_game( GameState
* s
) {
604 static char game
[100];
605 static const int game_scoring
[] = { 0, 15, 30, 40 };
607 if( s
->player1
.game
< 4 && s
->player2
.game
< 4) {
608 sprintf( game
, "%d - %d", game_scoring
[s
->player1
.game
], game_scoring
[s
->player2
.game
]);
609 } else if( s
->player1
.game
> s
->player2
.game
) {
610 strcpy( game
, "advantage player 1");
611 } else if( s
->player1
.game
< s
->player2
.game
) {
612 strcpy( game
, "advantage player 2");
614 strcpy( game
, "deuce");
620 char* format_status( GameState
* s
) {
621 static char status
[100];
622 static const char* set_names
[] = { "first", "second", "third", "fourth", "fifth" };
624 sprintf( status
, "%d:%d in %s set", s
->player1
.sets
[s
->current_set
], s
->player2
.sets
[s
->current_set
], set_names
[s
->current_set
]);
629 int game_get_winner( GameState
* s
) {
633 for( i
=0; i
<s
->current_set
; i
++) {
634 if( s
->player1
.sets
[i
] > s
->player2
.sets
[i
]) {
641 if( sets
[0] == SETS_TO_WIN
) return WINNER_PLAYER1
;
642 if( sets
[1] == SETS_TO_WIN
) return WINNER_PLAYER2
;