Enable more compiler warnings, fix most warnings
[tennix.git] / game.c
blobaa5a50839b8c3ca2af75f0b6fff81a19e6442538
2 /**
4 * Tennix! SDL Port
5 * Copyright (C) 2003, 2007, 2008, 2009 Thomas Perl <thp@thpinfo.com>
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, 0.0, 0.0, false, -1, false },
41 { GAME_X_MIN-RACKET_X_MID*2, GAME_Y_MID, 0, 0, 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, DESIRE_NORMAL, PLAYER_TYPE_AI, GAME_Y_MID, false, 0, {0}, 0, 0, PLAYER_ACCEL_DEFAULT, 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,
71 false,
72 REFEREE_COUNT,
73 SCORE_UNDECIDED,
77 s = (GameState*)malloc(sizeof(GameState));
78 if (s == NULL) abort();
80 memcpy(s, &template, sizeof(GameState));
82 game_setup_serve(s);
84 /* smoothen n-gram */
85 for( x = 0; x<NGRAM_STEPS; x++) {
86 for( y = 0; y<NGRAM_STEPS; y++) {
87 for( z = 0; z<NGRAM_STEPS; z++) {
88 s->ngram[x][y][z] = 1;
93 return s;
97 void gameloop(GameState *s) {
98 Uint32 ot = SDL_GetTicks();
99 Uint32 nt;
100 Uint32 dt = GAME_TICKS;
101 Uint32 diff;
102 Uint32 accumulator = 0;
103 bool quit = false;
105 #ifdef ENABLE_FPS_LIMIT
106 Uint32 ft, frames; /* frame timer and frames */
107 #endif
109 strcpy(s->game_score_str, format_game(s));
110 strcpy(s->sets_score_str, format_sets(s));
111 s->text_changed = true;
113 if (s->rain > 0) {
114 play_sample_background(SOUND_RAIN);
116 play_sample_loop(SOUND_AUDIENCE);
117 /* Reset the court type, so it is redrawn on first display */
118 s->old_court_type = -1;
120 #ifdef ENABLE_FPS_LIMIT
121 frames = 0;
122 ft = SDL_GetTicks();
123 #endif
124 while( !quit) {
125 nt = SDL_GetTicks();
126 diff = nt-ot;
127 if( diff > 2000) {
128 diff = 0;
131 accumulator += diff;
132 ot = nt;
134 while( accumulator >= dt) {
135 quit = step(s);
136 s->time += dt;
137 s->windtime += s->wind*dt;
138 accumulator -= dt;
140 if( s->was_stopped) {
141 ot = SDL_GetTicks();
142 s->was_stopped = false;
145 if (s->timelimit != 0 && s->time >= s->timelimit) {
146 quit = 1;
149 #ifdef ENABLE_FPS_LIMIT
150 while (frames*1000.0/((float)(SDL_GetTicks()-ft+1))>(float)(DEFAULT_FPS)) {
151 SDL_Delay(10);
153 frames++;
154 #endif
156 render(s);
159 clear_screen();
160 store_screen();
162 stop_sample(SOUND_AUDIENCE);
163 stop_sample(SOUND_RAIN);
166 bool step( GameState* s) {
167 Uint8 *keys;
168 SDL_Event e;
169 bool ground_event = false;
170 int p;
172 if (s->ball.z < 0) {
173 /* ground event */
174 ground_event = true;
176 s->referee = REFEREE_NORMAL;
177 /* FIXME: set referee to correct "position" */
179 /* bounce from the ground */
180 if (fabsf(s->ball.move_z) > 0.5) {
181 s->ball.move_z *= -RESTITUTION;
182 } else {
183 s->ball.move_z = 0;
185 s->ball.z = 0;
187 /* only make the sound if it's visibly bouncing hard */
188 if (!IS_OFFSCREEN(s->ball.x, s->ball.y) && fabsf(s->ball.move_z) > .5) {
189 s->play_sound = SOUND_GROUND;
193 if (NET_COLLISION_BALL(s->ball)) {
194 /* the net blocks movement of the ball */
195 s->ball.move_x = 0;
196 s->ball.move_y = 0;
197 /* FIXME: make sure the ball appears OUTSIDE of the net */
200 /* see if we have something to score */
201 if (s->score_event == SCORE_UNDECIDED) {
202 if (NET_COLLISION_BALL(s->ball)) {
203 /* the ball "fell" into the net */
204 s->score_event = SCORE_EVENT_NET;
205 s->play_sound = SOUND_OUT;
206 s->status = "net!";
207 } else if (IS_OFFSCREEN(s->ball.x, s->ball.y)) {
208 /* ball flew offscreen */
209 s->score_event = SCORE_EVENT_OFFSCREEN;
210 s->play_sound = SOUND_OUT;
211 s->status = "out!";
212 } else if (ground_event) {
213 /* the ball hit the ground on the screen */
214 if (IS_OUT(s->ball.x, s->ball.y)) {
215 /* the ball bounced in the OUT area */
216 s->score_event = SCORE_EVENT_OUT;
217 s->play_sound = SOUND_OUT;
218 s->status = "out!";
219 } else if (GROUND_IS_VALID(s->ball.last_hit_by, s->ball.x, s->ball.y)) {
220 if (s->ball.ground_hit) {
221 s->score_event = SCORE_EVENT_GROUND_VALID;
222 s->play_sound = SOUND_OUT;
223 s->status = "did not catch the ball!";
224 } else {
225 /* first ground hit in valid area */
226 s->ball.ground_hit = true;
228 } else {
229 /* ball hit somewhere invalid */
230 s->score_event = SCORE_EVENT_GROUND_INVALID;
231 s->play_sound = SOUND_OUT;
232 s->status = "fault!";
237 if (s->score_event != SCORE_UNDECIDED) {
238 /* we have some scoring to do */
239 if (s->score_time == 0) {
240 /* schedule scoring in the future */
241 s->score_time = s->time + SCORING_DELAY;
242 } else if (s->time >= s->score_time) {
243 /* time has ran out - score now */
244 score_game(s);
245 s->score_time = 0;
247 strcpy(s->game_score_str, format_game(s));
248 strcpy(s->sets_score_str, format_sets(s));
249 s->text_changed = true;
251 /*if (FIXME - get winner) {
252 if( PLAYER(s, 1).type == PLAYER_TYPE_HUMAN && PLAYER(s, 2).type == PLAYER_TYPE_AI) {
253 s->status = "computer scores";
254 } else {
255 s->status = "player 2 scores";
257 s->referee = REFEREE_PLAYER2;
258 } else {
259 if( PLAYER(s, 1).type == PLAYER_TYPE_HUMAN && PLAYER(s, 2).type == PLAYER_TYPE_AI) {
260 s->status = "player scores";
261 } else {
262 s->status = "player 1 scores";
264 s->referee = REFEREE_PLAYER1;
267 game_setup_serve(s);
268 s->play_sound = SOUND_APPLAUSE;
269 /*FIXME n-gram predictor broken
270 s->history_size = 0;
271 s->history_is_locked = 0;
272 s->ngram_prediction = 0.0;*/
274 } else {
275 /* score is still undecided - do the racket swing thing */
276 for (p=1; p<=2; p++) {
277 if (IS_NEAR_X(PLAYER(s, p).x, s->ball.x) && IS_NEAR_Y(PLAYER(s, p).y, s->ball.y-s->ball.z) && PLAYER(s, p).state && s->ball.last_hit_by != p) {
278 /* RACKET HIT */
279 if (!s->ball.ground_hit && s->ball.move_x != 0.0) {
280 s->status = "volley!";
281 } else {
282 s->status = format_status(s);
284 switch (PLAYER(s, p).desire) {
285 case DESIRE_NORMAL:
286 /* normal swing */
287 s->ball.move_x = 2.7 + 2.0*PLAYER(s, p).state/PLAYER_STATE_MAX;
288 s->ball.move_z = 1.2*PLAYER(s, p).state/PLAYER_STATE_MAX;
289 break;
290 case DESIRE_TOPSPIN:
291 /* top spin */
292 s->ball.move_x = 1.1 + 2.2*PLAYER(s, p).state/PLAYER_STATE_MAX;
293 s->ball.move_z = 2.5*PLAYER(s, p).state/PLAYER_STATE_MAX;
294 break;
295 case DESIRE_SMASH:
296 /* smash */
297 s->ball.move_x = 4.0 + 3.0*PLAYER(s, p).state/PLAYER_STATE_MAX;
298 s->ball.move_z = 1.1*PLAYER(s, p).state/PLAYER_STATE_MAX;
299 break;
301 s->ball.move_y = get_move_y( s, p);
302 s->play_sound = SOUND_RACKET;
303 s->ball.ground_hit = false;
304 s->ball.inhibit_gravity = false;
305 s->ball.last_hit_by = p;
306 if (p==1) {
307 pan_sample(SOUND_RACKET, 0.4);
308 } else {
309 pan_sample(SOUND_RACKET, 0.6);
310 s->ball.move_x *= -1;
316 #if 0
317 FIXME n-gram predictor broken
318 if( IS_OUT_X(s->ball.x) || IS_OFFSCREEN_Y(s->ball.y)) {
319 if( IS_OUT_X(s->ball.x)) {
320 /*if( !s->history_is_locked && s->referee != REFEREE_OUT) {
321 s->history[s->history_size] = (int)(NGRAM_STEPS*s->ball.y/HEIGHT);
322 s->history_size++;
323 if( s->history_size == 3) {
324 s->ngram[s->history[0]][s->history[1]][s->history[2]] += 10;
325 s->ngram_prediction = ngram_predictor( s);
326 s->history[0] = s->history[1];
327 s->history[1] = s->history[2];
328 s->history_size--;
330 s->history_is_locked = true;
333 } else {
334 /*s->history_is_locked = false;*/
336 #endif
338 SDL_PollEvent(&e);
339 keys = SDL_GetKeyState(NULL);
340 switch(e.type) {
341 case SDL_JOYAXISMOTION:
342 if (e.jaxis.axis == JOYSTICK_Y_AXIS) {
343 s->joystick_y = JOYSTICK_PERCENTIZE(e.jaxis.value);
344 } else if (e.jaxis.axis == JOYSTICK_X_AXIS) {
345 s->joystick_x = JOYSTICK_PERCENTIZE(e.jaxis.value);
347 break;
348 case SDL_JOYBUTTONUP: case SDL_JOYBUTTONDOWN:
349 if (e.jbutton.button == JOYSTICK_BUTTON_A) {
350 s->joystick_a = (e.jbutton.state == SDL_PRESSED);
352 break;
355 if( s->time%50==0) {
357 * Maemo keys:
358 * F7 = "Decrease" key
359 * F8 = "Increase" key
361 if (keys['c'] || keys[SDLK_F8]) {
362 s->court_type++;
363 } else if (keys[SDLK_F7]) {
364 s->court_type--;
366 if (keys['r']) {
367 if (s->rain == 0) {
368 play_sample_background(SOUND_RAIN);
370 s->rain += 10;
372 if (keys['t']) {
373 s->fog++;
375 if (keys['1']) {
376 s->wind++;
378 if (keys['2']) {
379 s->wind--;
381 if (keys['n']) {
382 s->night = 1 - s->night;
384 if( s->court_type > GR_CTT_LAST) {
385 s->court_type = GR_CTT_FIRST;
386 } else if (s->court_type < GR_CTT_FIRST) {
387 s->court_type = GR_CTT_LAST;
391 if(!(SDL_GetTicks() < fading_start+FADE_DURATION) && !s->is_over) {
392 if( PLAYER(s, 1).type == PLAYER_TYPE_HUMAN) {
393 input_human( &PLAYER(s, 1),
394 keys['w'] || keys[SDLK_UP] || s->joystick_y < -JOYSTICK_TRESHOLD,
395 keys['s'] || keys[SDLK_DOWN] || s->joystick_y > JOYSTICK_TRESHOLD,
396 keys['d'] || keys[SDLK_SPACE] || keys[SDLK_LCTRL] || keys[SDLK_RETURN] || s->joystick_a,
397 keys['e'],
398 keys['f'],
399 #ifdef ENABLE_MOUSE
400 true,
401 #else
402 false,
403 #endif
405 } else {
406 input_ai( &PLAYER(s, 1), &s->ball, &PLAYER(s, 2), s);
409 if( PLAYER(s, 2).type == PLAYER_TYPE_HUMAN) {
410 input_human( &PLAYER(s, 2), keys['o'], keys['l'], keys['k'], keys['i'], keys['j'], false, s);
411 } else {
412 input_ai( &PLAYER(s, 2), &s->ball, &PLAYER(s, 1), s);
416 /* Maemo: The "F6" button is the "Fullscreen" button */
417 /*if( keys['f'] || keys[SDLK_F6]) SDL_WM_ToggleFullScreen( screen);*/
418 if( keys['y']) SDL_SaveBMP( screen, "screenshot.bmp");
420 /* Maemo: The "F4" button is the "Open menu" button */
421 if( keys['p'] || keys[SDLK_F4]) {
422 while( keys['p'] || keys[SDLK_F4]) {
423 SDL_PollEvent( &e);
424 keys = SDL_GetKeyState( NULL);
425 SDL_Delay( 10);
427 while( (keys['p'] || keys[SDLK_F4]) == 0) {
428 SDL_PollEvent( &e);
429 keys = SDL_GetKeyState( NULL);
430 SDL_Delay( 10);
432 while( keys['p'] || keys[SDLK_F4]) {
433 SDL_PollEvent( &e);
434 keys = SDL_GetKeyState( NULL);
435 SDL_Delay( 10);
437 s->was_stopped = true;
440 if( keys[SDLK_ESCAPE] || keys['q']) return true;
442 limit_value( &PLAYER(s, 1).y, PLAYER_Y_MIN, PLAYER_Y_MAX);
443 limit_value( &PLAYER(s, 2).y, PLAYER_Y_MIN, PLAYER_Y_MAX);
445 if (s->ball.move_x > 0 && s->wind > 0) {
446 s->ball.x += fabsf(s->wind)/2;
447 } else if (s->ball.move_x < 0 && s->wind < 0) {
448 s->ball.x -= fabsf(s->wind)/2;
451 s->ball.z += s->ball.move_z;
452 if (!s->ball.inhibit_gravity) {
453 s->ball.move_z += GRAVITY;
456 s->ball.x += s->ball.move_x;
457 s->ball.y += s->ball.move_y;
459 if(PLAYER(s, 1).state) PLAYER(s, 1).state--;
460 if(PLAYER(s, 2).state) PLAYER(s, 2).state--;
462 return false;
465 void render( GameState* s) {
466 int x, y, b;
467 unsigned int i;
468 #ifdef EXTENDED_REFEREE
469 int t=1000;
470 #endif
471 if (s->play_sound != SOUND_MAX) {
472 play_sample(s->play_sound);
473 s->play_sound = SOUND_MAX;
475 if( s->winner != WINNER_NONE) {
476 if( !s->is_over) {
477 start_fade();
478 s->is_over = true;
480 clear_screen();
481 store_screen();
482 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);
483 sprintf( s->game_score_str, "player %d wins the match with %s", s->winner, format_sets( s));
484 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);
485 updatescr();
486 return;
488 if (s->old_court_type != s->court_type || s->text_changed || s->old_referee != s->referee) {
489 clear_screen();
490 fill_image(s->court_type, 120, 120, 400, 250);
491 show_image(GR_COURT, 0, 0, 255);
492 font_draw_string( GR_DKC2_FONT, s->game_score_str, 14, 14, 0, ANIMATION_NONE);
493 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);
494 #ifdef EXTENDED_REFEREE
495 switch (s->referee) {
496 case REFEREE_NORMAL:
497 t = 1000;
498 break;
499 case REFEREE_OUT:
500 t = 200;
501 break;
502 case REFEREE_PLAYER1:
503 case REFEREE_PLAYER2:
504 t = 400;
505 break;
507 t = (s->time/t)%4;
508 switch (t) {
509 case 0:
510 t=0;
511 break;
512 case 1:
513 t=1;
514 break;
515 case 2:
516 t=0;
517 break;
518 case 3:
519 t=2;
520 break;
522 show_sprite( GR_REFEREE, s->referee*3+t, 12, 250, 10, 255);
523 if (voice_finished_flag == 0) {
524 show_sprite(GR_TALK, (s->time/150)%2, 2, 280, 45, 255);
526 #else
527 show_sprite( GR_REFEREE, s->referee, 4, 250, 10, 255);
528 #endif
529 s->old_court_type = s->court_type;
530 s->text_changed = false;
531 s->old_referee = s->referee;
532 store_screen();
535 show_sprite( GR_RACKET, (!PLAYER(s, 1).state), 4, PLAYER(s, 1).x-RACKET_X_MID, PLAYER(s, 1).y-RACKET_Y_MID, 255);
536 show_sprite( GR_RACKET, (!PLAYER(s, 2).state)+2, 4, PLAYER(s, 2).x-RACKET_X_MID, PLAYER(s, 2).y-RACKET_Y_MID, 255);
538 if( s->ball.move_x > 0) {
539 b = (s->time/100)%BALL_STATES;
540 } else if( s->ball.move_x < 0) {
541 b = BALL_STATES-1-(s->time/100)%BALL_STATES;
542 } else {
543 b = 0;
545 show_image(GR_SHADOW, s->ball.x-BALL_X_MID, s->ball.y-4, 255);
546 show_sprite(GR_BALL, b, BALL_STATES, s->ball.x-BALL_X_MID, s->ball.y-BALL_Y_MID-s->ball.z, 255);
548 /* Player 1's mouse rectangle */
549 if (!(PLAYER(s, 1).mouse_locked)) {
550 rectangle(PLAYER(s, 1).x-2+5, PLAYER(s, 1).mouse_y-2, 4, 4, 255, 255, 255);
551 rectangle(PLAYER(s, 1).x-1+5, PLAYER(s, 1).mouse_y-1, 2, 2, 0, 0, 0);
555 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);
557 for (i=0; i<s->rain; i++) {
558 x = rand()%WIDTH;
559 y = rand()%HEIGHT;
560 draw_line_faded(x, y, x+10+s->wind*5, y+30, 0, 0, 255, 100, 200, 255);
562 if (s->rain) {
564 * Cheap-ish update of the whole screen. This can
565 * probably be optimized.
567 update_rect(0, 0, WIDTH, HEIGHT);
570 #ifdef DEBUG
571 line_horiz( PLAYER(s, 1).y, 255, 0, 0);
572 line_horiz( PLAYER(s, 2).y, 0, 0, 255);
573 line_horiz( s->ball.y, 0, 255, 0);
575 line_vert( PLAYER(s, 1).x, 255, 0, 0);
576 line_vert( PLAYER(s, 2).x, 0, 0, 255);
577 line_vert( s->ball.x, 0, 255, 0);
579 line_horiz( GAME_Y_MIN, 100, 100, 100);
580 line_horiz( GAME_Y_MAX, 100, 100, 100);
581 #endif
582 switch (s->fog) {
583 default:
584 case 4:
585 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -s->time/150-s->windtime/200, 0);
586 case 3:
587 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, -s->time/100-s->windtime/150, 20);
588 case 2:
589 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -s->time/180-s->windtime/180, 80);
590 case 1:
591 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, s->time/200-s->windtime/100, 0);
592 case 0:
593 break;
595 if (s->night) {
596 show_image(GR_NIGHT, 0, 0, 255);
599 updatescr();
602 void limit_value( float* value, float min, float max) {
603 if( *value < min) {
604 *value = min;
605 } else if( *value > max) {
606 *value = max;
610 float get_move_y( GameState* s, unsigned char player) {
611 float pct, dest, x_len, y_len;
612 float py, by, pa, move_x;
614 py = (player==1)?(PLAYER(s, 1).y):(PLAYER(s, 2).y);
615 by = s->ball.y - s->ball.z;
616 pa = RACKET_Y_MID*2;
617 move_x = s->ball.move_x;
619 /* -1.0 .. 1.0 for racket hit position */
620 pct = (by-py)/(pa/2);
621 limit_value( &pct, -1.0, 1.0);
623 /* Y destination for ball */
624 dest = GAME_Y_MID + pct*(GAME_Y_MAX-GAME_Y_MIN);
626 /* lengths for the ball's journey */
627 if( player == 1) {
628 x_len = fabsf(GAME_X_MAX - s->ball.x);
629 y_len = dest - by + MOVE_Y_SEED-rand()%MOVE_Y_SEED*2;
630 } else {
631 x_len = s->ball.x - GAME_X_MIN;
632 y_len = by - dest + MOVE_Y_SEED-rand()%MOVE_Y_SEED*2;
635 /* return the should-be value for move_y */
636 return (y_len*move_x)/(x_len);
639 void input_human( Player* player, bool up, bool down, bool hit, bool topspin, bool smash, bool use_mouse, GameState* s) {
640 int diff = PLAYER_MOVE_Y;
641 int mb;
644 * Only use mouse control if the user isn't pressing any buttons
645 * this way, keyboard control still works when mouse control is
646 * enabled.
648 if (use_mouse && (down || up || hit)) {
650 * this is here so if the user decides to play
651 * with keyboard controls, we will lock the
652 * mouse and disable displaying the mouse cursor
654 player->mouse_locked = true;
656 if (use_mouse && !down && !up && !hit) {
657 mb = SDL_GetMouseState(&(player->mouse_x), &(player->mouse_y));
658 if (mb&SDL_BUTTON(SDL_BUTTON_LEFT)) {
659 if (player->mouse_y < player->y) {
660 down = false;
661 up = true;
662 diff = (player->y-player->mouse_y<diff)?(player->y-player->mouse_y):(diff);
663 } else if (player->mouse_y > player->y) {
664 up = false;
665 down = true;
666 diff = (player->mouse_y-player->y<diff)?(player->mouse_y-player->y):(diff);
668 player->mouse_locked = false;
669 } else if (!player->mouse_locked) {
670 hit = true;
674 if (fabsf(s->joystick_y) > JOYSTICK_TRESHOLD) {
675 diff = PLAYER_MOVE_Y*fabsf(s->joystick_y)/40.0;
676 if (diff > PLAYER_MOVE_Y) {
677 diff = PLAYER_MOVE_Y;
681 if (up) {
682 player->y -= fminf(diff, diff*player->accelerate);
683 player->accelerate *= PLAYER_ACCEL_INCREASE;
684 } else if (down) {
685 player->y += fminf(diff, diff*player->accelerate);
686 player->accelerate *= PLAYER_ACCEL_INCREASE;
687 } else {
688 player->accelerate = PLAYER_ACCEL_DEFAULT;
691 if(hit || topspin || smash) {
692 player->desire = (topspin)?(DESIRE_TOPSPIN):(DESIRE_NORMAL);
693 player->desire = (smash)?(DESIRE_SMASH):(player->desire);
695 if( !player->state && !player->state_locked) {
696 player->state = PLAYER_STATE_MAX;
697 player->state_locked = true;
699 } else {
700 player->state_locked = false;
704 void input_ai( Player* player, Ball* ball, Player* opponent, GameState* s) {
705 float fact = 1.7;
706 float target;
708 /* FIXME - this is broken since the new physics model has been introduced */
710 if( fabsf( player->y - (ball->y-ball->z)) > RACKET_Y_MID*5) {
711 fact = 3.5;
714 if(1) {
715 if( player->desire == DESIRE_NORMAL && !IS_NEAR_Y_AI( player->y, (ball->y-ball->z))) {
716 if( player->y < (ball->y-ball->z)) {
717 player->y += fmin( 2*fact, (ball->y-ball->z) - player->y);
718 } else if( player->y > (ball->y-ball->z)) {
719 player->y -= fmin( 2*fact, player->y - (ball->y-ball->z));
723 if( (ball->move_x != 0 || IS_NEAR_Y_AI( player->y, (ball->y-ball->z))) && IS_NEAR_X_AI( player->x, ball->x)/* && !player->state*/) {
724 player->state = PLAYER_STATE_MAX;
726 } else if( s->ngram_prediction > 0.0) {
727 target = s->ngram_prediction*((float)HEIGHT)/((float)(NGRAM_STEPS));
728 target = GAME_Y_MID + (target-GAME_Y_MID)*1.5;
730 if( player->desire == DESIRE_NORMAL && !IS_NEAR_Y_AI( player->y, target)) {
731 if( player->y < target) {
732 player->y += fmin( fact, (target-player->y)/40.0);
733 } else if( player->y > target) {
734 player->y -= fmin( fact, (player->y-target)/40.0);
740 void game_setup_serve( GameState* s) {
741 s->ball.z = 10.0;
742 s->ball.y = GAME_Y_MID;
743 s->ball.move_x = 0.0;
744 s->ball.move_y = 0.0;
745 s->ball.move_z = 0.0;
746 s->ball.last_hit_by = -1;
747 s->ball.inhibit_gravity = true;
749 if( s->serving_player == 1) {
750 s->ball.x = GAME_X_MIN-RACKET_X_MID*1.5;
751 } else {
752 s->ball.x = GAME_X_MAX+RACKET_X_MID*1.5;
756 float ngram_predictor( GameState* s) {
757 unsigned int count = 0;
758 unsigned long sum = 0;
759 int x, y, z;
760 float result;
762 if( s->history_size < 3) {
763 return 0.0;
766 x = s->history[1];
767 y = s->history[2];
769 for( z = 0; z<NGRAM_STEPS; z++) {
770 count += s->ngram[x][y][z];
771 sum += z * s->ngram[x][y][z];
774 result = ((float)(sum))/((float)(count));
775 #ifdef DEBUG
776 printf( "predicting next = %.2f\n", result);
777 #endif
779 return result;
782 void score_game(GameState* s) {
783 Player *last_hit_by = NULL;
784 Player *other = NULL;
786 Player* winner = NULL;
787 Player* loser = NULL;
789 /* determine "last hit by" and "other" */
790 if (s->ball.last_hit_by == 1) {
791 last_hit_by = &(PLAYER(s, 1));
792 other = &(PLAYER(s, 2));
793 } else {
794 last_hit_by = &(PLAYER(s, 2));
795 other = &(PLAYER(s, 1));
798 switch (s->score_event) {
799 case SCORE_EVENT_NET:
800 winner = other;
801 break;
802 case SCORE_EVENT_OUT:
803 case SCORE_EVENT_GROUND_INVALID:
804 case SCORE_EVENT_OFFSCREEN:
805 case SCORE_EVENT_GROUND_VALID:
806 if (s->ball.ground_hit) {
807 winner = last_hit_by;
808 } else {
809 winner = other;
811 break;
812 default:
813 break;
816 /* determine loser based on winner */
817 if (winner == last_hit_by) {
818 loser = other;
819 } else {
820 loser = last_hit_by;
823 if( s->current_set >= SETS_TO_WIN*2-1) {
824 return;
827 winner->game++;
828 if( loser->game < winner->game-1) {
829 if( winner->game >= 4) {
830 winner->game = loser->game = 0;
831 winner->sets[s->current_set]++;
833 /* serving is changed when the "game" is over */
834 s->serving_player = (s->serving_player==1)?(2):(1);
836 #ifdef HAVE_VOICE_FILES
837 /* speak the current score */
838 voice_say_list(4, VOICE_ZERO_IN + (PLAYER(s, 1).sets[s->current_set])*2, VOICE_TO, VOICE_ZERO_OUT + (PLAYER(s, 2).sets[s->current_set])*2, VOICE_IN_THE_FIRST_SET+s->current_set);
839 #endif
841 /* scoring the set.. */
842 if( (winner->sets[s->current_set] == 6 && loser->sets[s->current_set] < 5) ||
843 winner->sets[s->current_set] == 7) {
844 s->current_set++;
845 s->winner = game_get_winner( s);
850 /* forget this event - we've handled it */
851 s->score_event = SCORE_UNDECIDED;
854 char* format_sets( GameState* s) {
855 static char sets[100];
856 static char tmp[100];
857 int i, max = s->current_set;
859 sets[0] = '\0';
861 if( s->winner != WINNER_NONE) {
862 max--;
864 for( i=0; i<=max; i++) {
865 sprintf( tmp, "%d:%d, ", PLAYER(s, 1).sets[i], PLAYER(s, 2).sets[i]);
866 strcat( sets, tmp);
869 sets[strlen(sets)-2] = '\0';
871 return sets;
874 char* format_game( GameState* s) {
875 static char game[100];
876 static const int game_scoring[] = { 0, 15, 30, 40 };
878 if( PLAYER(s, 1).game < 4 && PLAYER(s, 2).game < 4) {
879 #ifdef HAVE_VOICE_FILES
880 if (PLAYER(s, 1).game > 0 || PLAYER(s, 2).game > 0) {
881 if (PLAYER(s, 1).game == PLAYER(s, 2).game) {
882 voice_say_list(2, VOICE_LOVE_IN + 2*(PLAYER(s, 1).game), VOICE_ALL);
883 } else {
884 voice_say_list(2, VOICE_LOVE_IN + 2*(PLAYER(s, 1).game), VOICE_LOVE_OUT + 2*(PLAYER(s, 2).game));
887 #endif
888 sprintf( game, "%d - %d", game_scoring[PLAYER(s, 1).game], game_scoring[PLAYER(s, 2).game]);
889 } else if( PLAYER(s, 1).game > PLAYER(s, 2).game) {
890 #ifdef HAVE_VOICE_FILES
891 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_ONE);
892 #endif
893 strcpy( game, "advantage player 1");
894 } else if( PLAYER(s, 1).game < PLAYER(s, 2).game) {
895 #ifdef HAVE_VOICE_FILES
896 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_TWO);
897 #endif
898 strcpy( game, "advantage player 2");
899 } else {
900 #ifdef HAVE_VOICE_FILES
901 voice_say_list(1, VOICE_DEUCE);
902 #endif
903 strcpy( game, "deuce");
906 return game;
909 char* format_status( GameState* s) {
910 static char status[100];
911 static const char* set_names[] = { "first", "second", "third", "fourth", "fifth" };
913 sprintf( status, "%d:%d in %s set", PLAYER(s, 1).sets[s->current_set], PLAYER(s, 2).sets[s->current_set], set_names[s->current_set]);
915 return status;
918 int game_get_winner( GameState* s) {
919 unsigned int i;
920 int sets[2] = {0};
922 for( i=0; i<s->current_set; i++) {
923 if( PLAYER(s, 1).sets[i] > PLAYER(s, 2).sets[i]) {
924 sets[0]++;
925 } else {
926 sets[1]++;
930 if( sets[0] == SETS_TO_WIN) return WINNER_PLAYER1;
931 if( sets[1] == SETS_TO_WIN) return WINNER_PLAYER2;
933 return WINNER_NONE;