Support for BINARY_INSTALL_DIR, Makefile updates
[tennix.git] / game.c
blobdfddfb80e0d71cf6acbb1ae99948355775c2974e
2 /**
4 * Tennix! SDL Port
5 * Copyright (C) 2003, 2007 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, false },
38 { GAME_X_MAX+RACKET_X_MID*2, GAME_Y_MID, 0, 0, 0, DESIRE_NORMAL, PLAYER_TYPE_HUMAN, false },
42 true,
43 "welcome to tennix " VERSION,
44 { 0 },
45 { 0 },
46 REFEREE_NORMAL,
48 WINNER_NONE,
49 false,
50 GR_CTT_GRASS,
51 { 0 },
53 false,
54 { { { 0 } } },
55 0.0,
56 SOUND_MAX,
57 0.0,
58 0.0,
62 strcpy( s.game_score_str, format_game( &s));
63 strcpy( s.sets_score_str, format_sets( &s));
65 Uint32 ot = SDL_GetTicks();
66 Uint32 nt;
67 Uint32 dt = GAME_TICKS;
68 Uint32 diff;
69 Uint32 accumulator = 0;
70 bool quit = false;
71 int x, y, z;
73 if( singleplayer) {
74 #ifdef DEBUG
75 s.player1.type = PLAYER_TYPE_AI;
76 #endif
77 s.player2.type = PLAYER_TYPE_AI;
80 game_setup_serve( &s);
82 /* smoothen n-gram */
83 for( x = 0; x<NGRAM_STEPS; x++) {
84 for( y = 0; y<NGRAM_STEPS; y++) {
85 for( z = 0; z<NGRAM_STEPS; z++) {
86 s.ngram[x][y][z] = 1;
91 while( !quit) {
92 nt = SDL_GetTicks();
93 diff = nt-ot;
94 if( diff > 2000) {
95 diff = 0;
98 accumulator += diff;
99 ot = nt;
101 while( accumulator >= dt) {
102 quit = step( &s);
103 s.time += dt;
104 accumulator -= dt;
106 if( s.was_stopped) {
107 ot = SDL_GetTicks();
108 s.was_stopped = false;
112 render( &s);
117 bool step( GameState* s) {
118 Uint8 *keys;
119 SDL_Event e;
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 */
127 s->status = "out!";
128 s->play_sound = SOUND_OUT;
129 s->referee = REFEREE_OUT;
130 } else {
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;
137 s->ground.jump = 3;
138 s->ground.x = s->ball.x;
139 s->ground.y = s->ball.y;
140 } else {
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";
155 } else {
156 s->status = "player 2 scores";
158 s->referee = REFEREE_PLAYER2;
159 } else {
160 if( s->player1.type == PLAYER_TYPE_HUMAN && s->player2.type == PLAYER_TYPE_AI) {
161 s->status = "player scores";
162 } else {
163 s->status = "player 1 scores";
165 s->referee = REFEREE_PLAYER1;
168 game_setup_serve( s);
169 s->play_sound = SOUND_APPLAUSE;
170 SDL_Delay( 500);
171 start_fade();
172 s->was_stopped = true;
173 s->history_size = 0;
174 s->history_is_locked = 0;
175 s->ngram_prediction = 0.0;
176 #ifdef DEBUG
177 printf( "-- game reset --\n");
178 #endif
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);
184 s->history_size++;
185 if( s->history_size == 3) {
186 s->ngram[s->history[0]][s->history[1]][s->history[2]] += 10;
187 #ifdef DEBUG
188 printf( "history: %d, %d, %d\n", s->history[0], s->history[1], s->history[2]);
189 #endif
190 s->ngram_prediction = ngram_predictor( s);
191 s->history[0] = s->history[1];
192 s->history[1] = s->history[2];
193 s->history_size--;
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;
201 } else {
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;
213 } else {
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);
223 } else {
224 s->history_is_locked = false;
227 /* Update ball_dest for debugging purposes */
228 get_move_y( s, 1);
229 get_move_y( s, 2);
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--;
237 SDL_PollEvent( &e);
238 keys = SDL_GetKeyState( NULL);
239 switch(e.type) {
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);
246 break;
247 case SDL_JOYBUTTONUP: case SDL_JOYBUTTONDOWN:
248 if (e.jbutton.button == JOYSTICK_BUTTON_A) {
249 s->joystick_a = (e.jbutton.state == SDL_PRESSED);
251 break;
254 if( keys['c'] && s->time%50==0) {
255 s->court_type++;
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,
268 } else {
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);
274 } else {
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");
282 if( keys['p']) {
283 while( keys['p']) {
284 SDL_PollEvent( &e);
285 keys = SDL_GetKeyState( NULL);
286 SDL_Delay( 10);
288 while( keys['p'] == 0) {
289 SDL_PollEvent( &e);
290 keys = SDL_GetKeyState( NULL);
291 SDL_Delay( 10);
293 while( keys['p']) {
294 SDL_PollEvent( &e);
295 keys = SDL_GetKeyState( NULL);
296 SDL_Delay( 10);
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);
307 return false;
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) {
316 if( !s->is_over) {
317 start_fade();
318 s->is_over = true;
320 clearscr();
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);
324 updatescr();
325 return;
327 clearscr();
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);
344 } else {
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);
354 #ifdef DEBUG
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);
368 #endif
370 updatescr();
373 void limit_value( float* value, float min, float max) {
374 if( *value < min) {
375 *value = min;
376 } else if( *value > max) {
377 *value = max;
381 float get_phase( GameState* s) {
382 float pos, fract;
383 float x, min, max, direction;
385 x = s->ball.x;
386 min = GAME_X_MIN;
387 max = GAME_X_MAX;
388 direction = s->ball.move_x;
390 pos = (direction>0)?(1-GROUND_PHASE):(GROUND_PHASE);
392 fract = (x-min)/(max-min);
394 if( fract < pos) {
395 fract = fract/pos;
396 return fabsf( cosf(PI*fract/2))*PHASE_AMP*s->ball.jump;
397 } else {
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);
408 by = s->ball.y;
409 pa = RACKET_Y_MID*2;
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);
418 if( player == 1) {
419 s->player1.ball_dest = dest;
420 } else {
421 s->player2.ball_dest = dest;
424 /* lengths for the ball's journey */
425 if( player == 1) {
426 x_len = fabsf(GAME_X_MAX - s->ball.x);
427 y_len = dest - by + MOVE_Y_SEED-rand()%MOVE_Y_SEED*2;
428 } else {
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;
447 if( up) {
448 player->y -= diff;
451 if( down) {
452 player->y += diff;
455 if( hit) {
456 if( !player->state && !player->state_locked) {
457 player->state = PLAYER_STATE_MAX;
458 player->state_locked = true;
460 } else {
461 player->state_locked = false;
465 void input_ai( Player* player, Ball* ball, Player* opponent, GameState* s) {
466 float fact = 1.7;
467 float target;
469 if( fabsf( player->y - ball->y) > RACKET_Y_MID*5) {
470 fact = 3.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);
506 } else {/*
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) {
516 s->ball.jump = 7.5;
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;
525 } else {
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;
537 int x, y, z;
538 float result;
540 if( s->history_size < 3) {
541 return 0.0;
544 x = s->history[1];
545 y = s->history[2];
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));
553 #ifdef DEBUG
554 printf( "predicting next = %.2f\n", result);
555 #endif
557 return 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) {
565 return;
568 winner->game++;
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) {
576 s->current_set++;
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;
588 sets[0] = '\0';
590 if( s->winner != WINNER_NONE) {
591 max--;
593 for( i=0; i<=max; i++) {
594 sprintf( tmp, "%d:%d, ", s->player1.sets[i], s->player2.sets[i]);
595 strcat( sets, tmp);
598 sets[strlen(sets)-2] = '\0';
600 return sets;
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");
613 } else {
614 strcpy( game, "deuce");
617 return game;
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]);
626 return status;
629 int game_get_winner( GameState* s) {
630 int i;
631 int sets[2] = {0};
633 for( i=0; i<s->current_set; i++) {
634 if( s->player1.sets[i] > s->player2.sets[i]) {
635 sets[0]++;
636 } else {
637 sets[1]++;
641 if( sets[0] == SETS_TO_WIN) return WINNER_PLAYER1;
642 if( sets[1] == SETS_TO_WIN) return WINNER_PLAYER2;
644 return WINNER_NONE;