Use Tennix Archive Format instead in-executable data
[tennix.git] / game.c
blob2ffbd71162568b34168f77e3515e8a572665ca6d
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"
34 GameState *gamestate_new() {
35 int x, y, z;
36 GameState *s;
38 GameState template = {
39 { 0, 0, 0.0, 0.0, 0.0 },
40 { 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 },
46 true,
47 "welcome to tennix " VERSION,
48 { 0 },
49 { 0 },
50 REFEREE_NORMAL,
52 WINNER_NONE,
53 false,
54 GR_CTT_GRASS,
55 -1,
56 { 0 },
58 false,
59 { { { 0 } } },
60 0.0,
61 SOUND_MAX,
62 0.0,
63 0.0,
67 false,
72 s = (GameState*)malloc(sizeof(GameState));
73 if (s == NULL) abort();
75 memcpy(s, &template, sizeof(GameState));
77 game_setup_serve(s);
79 /* smoothen n-gram */
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;
88 return s;
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();
97 Uint32 nt;
98 Uint32 dt = GAME_TICKS;
99 Uint32 diff;
100 Uint32 accumulator = 0;
101 bool quit = false;
103 #ifdef ENABLE_FPS_LIMIT
104 Uint32 ft, frames; /* frame timer and frames */
105 #endif
107 if (s->rain > 0) {
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
115 frames = 0;
116 ft = SDL_GetTicks();
117 #endif
118 while( !quit) {
119 nt = SDL_GetTicks();
120 diff = nt-ot;
121 if( diff > 2000) {
122 diff = 0;
125 accumulator += diff;
126 ot = nt;
128 while( accumulator >= dt) {
129 quit = step(s);
130 s->time += dt;
131 s->windtime += s->wind*dt;
132 accumulator -= dt;
134 if( s->was_stopped) {
135 ot = SDL_GetTicks();
136 s->was_stopped = false;
140 #ifdef ENABLE_FPS_LIMIT
141 while (frames*1000.0/((float)(SDL_GetTicks()-ft+1))>(float)(DEFAULT_FPS)) {
142 SDL_Delay(10);
144 frames++;
145 #endif
147 render(s);
150 clear_screen();
151 store_screen();
153 stop_sample(SOUND_AUDIENCE);
154 stop_sample(SOUND_RAIN);
157 bool step( GameState* s) {
158 Uint8 *keys;
159 SDL_Event e;
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 */
167 s->status = "out!";
168 s->play_sound = SOUND_OUT;
169 s->referee = REFEREE_OUT;
170 } else {
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;
177 s->ground.jump = 3;
178 s->ground.x = s->ball.x;
179 s->ground.y = s->ball.y;
180 } else {
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";
195 } else {
196 s->status = "player 2 scores";
198 s->referee = REFEREE_PLAYER2;
199 } else {
200 if( s->player1.type == PLAYER_TYPE_HUMAN && s->player2.type == PLAYER_TYPE_AI) {
201 s->status = "player scores";
202 } else {
203 s->status = "player 1 scores";
205 s->referee = REFEREE_PLAYER1;
208 game_setup_serve( s);
209 s->play_sound = SOUND_APPLAUSE;
210 SDL_Delay( 500);
211 s->was_stopped = true;
212 s->history_size = 0;
213 s->history_is_locked = 0;
214 s->ngram_prediction = 0.0;
215 #ifdef DEBUG
216 printf( "-- game reset --\n");
217 #endif
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);
223 s->history_size++;
224 if( s->history_size == 3) {
225 s->ngram[s->history[0]][s->history[1]][s->history[2]] += 10;
226 #ifdef DEBUG
227 printf( "history: %d, %d, %d\n", s->history[0], s->history[1], s->history[2]);
228 #endif
229 s->ngram_prediction = ngram_predictor( s);
230 s->history[0] = s->history[1];
231 s->history[1] = s->history[2];
232 s->history_size--;
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;
240 } else {
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;
252 } else {
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);
262 } else {
263 s->history_is_locked = false;
266 SDL_PollEvent( &e);
267 keys = SDL_GetKeyState( NULL);
268 switch(e.type) {
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);
275 break;
276 case SDL_JOYBUTTONUP: case SDL_JOYBUTTONDOWN:
277 if (e.jbutton.button == JOYSTICK_BUTTON_A) {
278 s->joystick_a = (e.jbutton.state == SDL_PRESSED);
280 break;
283 if( s->time%50==0) {
285 * Maemo keys:
286 * F7 = "Decrease" key
287 * F8 = "Increase" key
289 if (keys['c'] || keys[SDLK_F8]) {
290 s->court_type++;
291 } else if (keys[SDLK_F7]) {
292 s->court_type--;
294 if (keys['r']) {
295 if (s->rain == 0) {
296 play_sample_background(SOUND_RAIN);
298 s->rain += 10;
300 if (keys['t']) {
301 s->fog++;
303 if (keys['1']) {
304 s->wind++;
306 if (keys['2']) {
307 s->wind--;
309 if (keys['n']) {
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,
325 #ifdef ENABLE_MOUSE
326 true,
327 #else
328 false,
329 #endif
331 } else {
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);
337 } else {
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]) {
349 SDL_PollEvent( &e);
350 keys = SDL_GetKeyState( NULL);
351 SDL_Delay( 10);
353 while( (keys['p'] || keys[SDLK_F4]) == 0) {
354 SDL_PollEvent( &e);
355 keys = SDL_GetKeyState( NULL);
356 SDL_Delay( 10);
358 while( keys['p'] || keys[SDLK_F4]) {
359 SDL_PollEvent( &e);
360 keys = SDL_GetKeyState( NULL);
361 SDL_Delay( 10);
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 */
373 get_move_y(s, 1);
374 get_move_y(s, 2);
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--;
388 return false;
391 void render( GameState* s) {
392 int i, x, y;
393 #ifdef EXTENDED_REFEREE
394 int t=1000;
395 #endif
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) {
401 if( !s->is_over) {
402 start_fade();
403 s->is_over = true;
405 clear_screen();
406 store_screen();
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);
410 updatescr();
411 return;
413 if (s->old_court_type != s->court_type) {
414 clear_screen();
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;
418 store_screen();
420 #ifdef EXTENDED_REFEREE
421 switch (s->referee) {
422 case REFEREE_NORMAL:
423 t = 1000;
424 break;
425 case REFEREE_OUT:
426 t = 200;
427 break;
428 case REFEREE_PLAYER1:
429 case REFEREE_PLAYER2:
430 t = 400;
431 break;
433 t = (s->time/t)%4;
434 switch (t) {
435 case 0:
436 t=0;
437 break;
438 case 1:
439 t=1;
440 break;
441 case 2:
442 t=0;
443 break;
444 case 3:
445 t=2;
446 break;
448 show_sprite( GR_REFEREE, s->referee*3+t, 12, 250, 10, 255);
449 #else
450 show_sprite( GR_REFEREE, s->referee, 4, 250, 10, 255);
451 #endif
452 if (voice_finished_flag == 0) {
453 show_sprite(GR_TALK, (s->time/150)%2, 2, 280, 45, 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/500)%4, 4, 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, 3-(s->time/500)%4, 4, s->ball.x-BALL_X_MID, s->ball.y-BALL_Y_MID, 255);
464 } else {
465 show_sprite( GR_BALL, 0, 4, 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++) {
480 x = rand()%WIDTH;
481 y = rand()%HEIGHT;
482 draw_line_faded(x, y, x+10+s->wind*5, y+30, 0, 0, 255, 100, 200, 255);
484 if (s->rain) {
486 * Cheap-ish update of the whole screen. This can
487 * probably be optimized.
489 update_rect(0, 0, WIDTH, HEIGHT);
492 #ifdef DEBUG
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);
506 #endif
507 switch (s->fog) {
508 default:
509 case 4:
510 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -s->time/150-s->windtime/200, 0);
511 case 3:
512 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, -s->time/100-s->windtime/150, 20);
513 case 2:
514 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -s->time/180-s->windtime/180, 80);
515 case 1:
516 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, s->time/200-s->windtime/100, 0);
517 case 0:
518 break;
520 if (s->night) {
521 show_image(GR_NIGHT, 0, 0, 255);
524 updatescr();
527 void limit_value( float* value, float min, float max) {
528 if( *value < min) {
529 *value = min;
530 } else if( *value > max) {
531 *value = max;
535 float get_phase( GameState* s) {
536 float pos, fract;
537 float x, min, max, direction;
539 x = s->ball.x;
540 min = GAME_X_MIN;
541 max = GAME_X_MAX;
542 direction = s->ball.move_x;
544 pos = (direction>0)?(1-GROUND_PHASE):(GROUND_PHASE);
546 fract = (x-min)/(max-min);
548 if( fract < pos) {
549 fract = fract/pos;
550 return fabsf( cosf(PI*fract/2))*PHASE_AMP*s->ball.jump;
551 } else {
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);
562 by = s->ball.y;
563 pa = RACKET_Y_MID*2;
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);
572 if( player == 1) {
573 s->player1.ball_dest = dest;
574 } else {
575 s->player2.ball_dest = dest;
578 /* lengths for the ball's journey */
579 if( player == 1) {
580 x_len = fabsf(GAME_X_MAX - s->ball.x);
581 y_len = dest - by + MOVE_Y_SEED-rand()%MOVE_Y_SEED*2;
582 } else {
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;
593 int mb;
596 * Only use mouse control if the user isn't pressing any buttons
597 * this way, keyboard control still works when mouse control is
598 * enabled.
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) {
612 down = false;
613 up = true;
614 diff = (player->y-player->mouse_y<diff)?(player->y-player->mouse_y):(diff);
615 } else if (player->mouse_y > player->y) {
616 up = false;
617 down = true;
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) {
622 hit = true;
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;
633 if (up) {
634 player->y -= fminf(diff, diff*player->accelerate);
635 player->accelerate *= PLAYER_ACCEL_INCREASE;
636 } else if (down) {
637 player->y += fminf(diff, diff*player->accelerate);
638 player->accelerate *= PLAYER_ACCEL_INCREASE;
639 } else {
640 player->accelerate = PLAYER_ACCEL_DEFAULT;
643 if( hit) {
644 if( !player->state && !player->state_locked) {
645 player->state = PLAYER_STATE_MAX;
646 player->state_locked = true;
648 } else {
649 player->state_locked = false;
653 void input_ai( Player* player, Ball* ball, Player* opponent, GameState* s) {
654 float fact = 1.7;
655 float target;
657 if( fabsf( player->y - ball->y) > RACKET_Y_MID*5) {
658 fact = 3.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);
694 } else {/*
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) {
704 s->ball.jump = 7.5;
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;
713 } else {
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;
725 int x, y, z;
726 float result;
728 if( s->history_size < 3) {
729 return 0.0;
732 x = s->history[1];
733 y = s->history[2];
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));
741 #ifdef DEBUG
742 printf( "predicting next = %.2f\n", result);
743 #endif
745 return 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) {
753 return;
756 winner->game++;
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);
765 #endif
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) {
770 s->current_set++;
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;
782 sets[0] = '\0';
784 if( s->winner != WINNER_NONE) {
785 max--;
787 for( i=0; i<=max; i++) {
788 sprintf( tmp, "%d:%d, ", s->player1.sets[i], s->player2.sets[i]);
789 strcat( sets, tmp);
792 sets[strlen(sets)-2] = '\0';
794 return sets;
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);
806 } else {
807 voice_say_list(2, VOICE_LOVE_IN + 2*(s->player1.game), VOICE_LOVE_OUT + 2*(s->player2.game));
810 #endif
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);
815 #endif
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);
820 #endif
821 strcpy( game, "advantage player 2");
822 } else {
823 #ifdef HAVE_VOICE_FILES
824 voice_say_list(1, VOICE_DEUCE);
825 #endif
826 strcpy( game, "deuce");
829 return game;
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]);
838 return status;
841 int game_get_winner( GameState* s) {
842 int i;
843 int sets[2] = {0};
845 for( i=0; i<s->current_set; i++) {
846 if( s->player1.sets[i] > s->player2.sets[i]) {
847 sets[0]++;
848 } else {
849 sets[1]++;
853 if( sets[0] == SETS_TO_WIN) return WINNER_PLAYER1;
854 if( sets[1] == SETS_TO_WIN) return WINNER_PLAYER2;
856 return WINNER_NONE;