Save and load the current game on quit/start
[tennix.git] / game.c
blob0e6416ae842230b0d75db7a51ff0ad309a305be7
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 <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <unistd.h>
30 #include "tennix.h"
31 #include "game.h"
32 #include "graphics.h"
33 #include "input.h"
34 #include "sound.h"
35 #include "util.h"
38 GameState *gamestate_new() {
39 int x, y, z;
40 GameState *s;
42 GameState template = {
43 NULL,
44 -1,
45 { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.6, false, -1, false },
47 { NULL, -1, GAME_X_MIN-RACKET_X_MID*2, GAME_Y_MID, 0.0, POWER_UP_FACTOR, POWER_DOWN_FACTOR, true, 0, DESIRE_NORMAL, PLAYER_TYPE_AI, GAME_Y_MID, false, 0, {0}, 0, 0, PLAYER_ACCEL_DEFAULT, true },
48 { NULL, -1, GAME_X_MAX+RACKET_X_MID*2, GAME_Y_MID, 0.0, POWER_UP_FACTOR, POWER_DOWN_FACTOR, true, 0, DESIRE_NORMAL, PLAYER_TYPE_AI, GAME_Y_MID, false, 0, {0}, 0, 0, PLAYER_ACCEL_DEFAULT, true },
53 "welcome to tennix " VERSION,
54 { 0 },
55 { 0 },
56 REFEREE_NORMAL,
58 WINNER_NONE,
59 false,
60 GR_COUNT,
61 { 0 },
63 false,
64 { { { 0 } } },
65 0.0,
66 0, /* sound events */
67 0.0,
68 0.0,
72 false,
76 false,
77 REFEREE_COUNT,
78 SCORE_UNDECIDED,
82 s = (GameState*)malloc(sizeof(GameState));
83 if (s == NULL) abort();
85 memcpy(s, &template, sizeof(GameState));
87 game_setup_serve(s);
89 /* smoothen n-gram */
90 for( x = 0; x<NGRAM_STEPS; x++) {
91 for( y = 0; y<NGRAM_STEPS; y++) {
92 for( z = 0; z<NGRAM_STEPS; z++) {
93 s->ngram[x][y][z] = 1;
98 return s;
102 int gamestate_save(GameState* s, const char* filename)
104 Location *location;
105 const char* status;
106 InputDevice* input_devices[MAXPLAYERS];
107 int i, result = 0;
108 FILE* fp = NULL;
109 #ifndef WIN32
110 char tmp[MAXPATHLEN];
112 assert(getcwd(tmp, MAXPATHLEN) == tmp);
113 assert(chdir(getenv("HOME")) == 0);
114 #endif
117 * Process-specific data (pointers to data
118 * structures only of interest to this process)
119 * has to be "removed" before saving.
121 * It can later be restored (in gamestate_load).
124 fp = fopen(filename, "w");
125 if (fp == NULL) {
126 return -1;
129 /* Save data for later recovery + clear */
130 location = s->location;
131 s->location = NULL;
132 status = s->status;
133 s->status = NULL;
134 for (i=1; i<=MAXPLAYERS; i++) {
135 input_devices[i-1] = PLAYER(s, i).input;
136 PLAYER(s, i).input = NULL;
139 if (fwrite(s, sizeof(GameState), 1, fp) != 1) {
140 result = -2;
143 /* Restore process-specific data */
144 s->location = location;
145 s->status = status;
146 for (i=1; i<=MAXPLAYERS; i++) {
147 PLAYER(s, i).input = input_devices[i-1];
150 fclose(fp);
152 #ifndef WIN32
153 assert(chdir(tmp) == 0);
154 #endif
156 return result;
160 GameState* gamestate_load(const char* filename)
162 FILE* fp;
163 GameState* s = NULL;
164 #ifndef WIN32
165 char tmp[MAXPATHLEN];
167 assert(getcwd(tmp, MAXPATHLEN) == tmp);
168 assert(chdir(getenv("HOME")) == 0);
169 #endif
171 fp = fopen(filename, "r");
173 if (fp != NULL) {
174 s = (GameState*)malloc(sizeof(GameState));
175 if (s != NULL) {
176 if (fread(s, sizeof(GameState), 1, fp) == 1) {
177 /* Restore/create process-specific data */
178 s->status = format_status(s);
179 /* FIXME: s->location, players' "input" */
180 } else {
181 free(s);
182 s = NULL;
185 fclose(fp);
188 #ifndef WIN32
189 assert(chdir(tmp) == 0);
190 #endif
192 return s;
196 void gameloop(GameState *s) {
197 Uint32 ot = SDL_GetTicks();
198 Uint32 nt;
199 Uint32 dt = GAME_TICKS;
200 Uint32 diff;
201 Uint32 accumulator = 0;
202 bool quit = false;
204 #ifdef ENABLE_FPS_LIMIT
205 Uint32 ft, frames; /* frame timer and frames */
206 #endif
208 strcpy(s->game_score_str, format_game(s));
209 strcpy(s->sets_score_str, format_sets(s));
210 s->text_changed = true;
212 if (s->rain > 0) {
213 play_sample_background(SOUND_RAIN);
215 if (s->location->max_visitors > 100) {
216 play_sample_loop(SOUND_AUDIENCE);
218 /* Reset the court type, so it is redrawn on first display */
219 s->displayed_court_type = GR_COUNT;
221 #ifdef ENABLE_FPS_LIMIT
222 frames = 0;
223 ft = SDL_GetTicks();
224 #endif
225 while( !quit) {
226 nt = SDL_GetTicks();
227 diff = nt-ot;
228 if( diff > 2000) {
229 diff = 0;
232 accumulator += diff;
233 ot = nt;
235 while( accumulator >= dt) {
236 quit = step(s);
237 s->time += dt;
238 s->windtime += s->wind*dt;
239 accumulator -= dt;
241 if( s->was_stopped) {
242 ot = SDL_GetTicks();
243 s->was_stopped = false;
246 if (s->timelimit != 0 && s->time >= s->timelimit) {
247 quit = 1;
250 #ifdef ENABLE_FPS_LIMIT
251 while (frames*1000.0/((float)(SDL_GetTicks()-ft+1))>(float)(DEFAULT_FPS)) {
252 SDL_Delay(10);
254 frames++;
255 #endif
257 render(s);
260 clear_screen();
261 store_screen();
263 stop_sample(SOUND_AUDIENCE);
264 stop_sample(SOUND_RAIN);
267 bool step( GameState* s) {
268 Uint8 *keys;
269 SDL_Event e;
270 bool ground_event = false;
271 int p;
273 if (s->ball.z < 0) {
274 /* ground event */
275 ground_event = true;
277 s->referee = REFEREE_NORMAL;
279 /* bounce from the ground */
280 if (absf(s->ball.move_z) > 0.3) {
281 s->sound_events |= SOUND_EVENT_GROUND;
282 sample_volume_group(SOUND_GROUND_FIRST, SOUND_GROUND_LAST, maxf(0.0, minf(1.0, absf(s->ball.move_z)/2)));
283 pan_sample_group(SOUND_GROUND_FIRST, SOUND_GROUND_LAST, maxf(0.0, minf(1.0, (s->ball.x)/WIDTH)));
284 s->ball.move_z *= -s->ball.restitution;
285 } else {
286 s->ball.move_z = 0;
288 s->ball.z = 0;
291 if (NET_COLLISION_BALL(s->ball)) {
292 /* the net blocks movement of the ball */
293 while (NET_COLLISION_BALL(s->ball)) {
294 /* make sure the ball appears OUTSIDE of the net */
295 if (s->ball.move_x < 0) {
296 s->ball.x += 1;
297 } else {
298 s->ball.x -= 1;
301 s->ball.move_x = 0;
302 s->ball.move_y = 0;
305 /* see if we have something to score */
306 if (s->score_event == SCORE_UNDECIDED) {
307 if (NET_COLLISION_BALL(s->ball)) {
308 /* the ball "fell" into the net */
309 s->score_event = SCORE_EVENT_NET;
310 s->sound_events |= SOUND_EVENT_OUT;
311 s->status = "net!";
312 } else if (IS_OFFSCREEN(s->ball.x, s->ball.y)) {
313 /* ball flew offscreen */
314 s->score_event = SCORE_EVENT_OFFSCREEN;
315 s->sound_events |= SOUND_EVENT_OUT;
316 s->status = "out!";
317 } else if (ground_event) {
318 /* the ball hit the ground on the screen */
319 if (IS_OUT(s->ball.x, s->ball.y)) {
320 /* the ball bounced in the OUT area */
321 s->score_event = SCORE_EVENT_OUT;
322 s->sound_events |= SOUND_EVENT_OUT;
323 s->status = "out!";
324 } else if (GROUND_IS_VALID(s->ball.last_hit_by, s->ball.x, s->ball.y)) {
325 if (s->ball.ground_hit) {
326 s->score_event = SCORE_EVENT_GROUND_VALID;
327 s->sound_events |= SOUND_EVENT_OUT;
328 s->status = "did not catch the ball!";
329 } else {
330 /* first ground hit in valid area */
331 s->ball.ground_hit = true;
333 } else {
334 /* ball hit somewhere invalid */
335 s->score_event = SCORE_EVENT_GROUND_INVALID;
336 s->sound_events |= SOUND_EVENT_OUT;
337 s->status = "fault!";
342 if (s->score_event != SCORE_UNDECIDED) {
343 /* we have some scoring to do */
344 if (s->score_time == 0) {
345 /* schedule scoring in the future */
346 s->score_time = s->time + SCORING_DELAY;
347 s->referee = REFEREE_OUT;
348 } else if (s->time >= s->score_time) {
349 /* time has ran out - score now */
350 switch (score_game(s)) {
351 case WINNER_PLAYER1:
352 s->status = "player 1 scores";
353 s->referee = REFEREE_PLAYER1;
354 break;
355 case WINNER_PLAYER2:
356 s->status = "player 2 scores";
357 s->referee = REFEREE_PLAYER2;
358 break;
359 default:
360 assert(0);
361 break;
363 s->score_time = 0;
365 strcpy(s->game_score_str, format_game(s));
366 strcpy(s->sets_score_str, format_sets(s));
367 s->text_changed = true;
369 game_setup_serve(s);
370 if (s->location->max_visitors > 100) {
371 s->sound_events |= SOUND_EVENT_APPLAUSE;
373 /*FIXME n-gram predictor broken
374 s->history_size = 0;
375 s->history_is_locked = 0;
376 s->ngram_prediction = 0.0;*/
378 } else {
379 /* score is still undecided - do the racket swing thing */
380 for (p=1; p<=2; p++) {
381 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).use_power && PLAYER(s, p).power > 30.0 && s->ball.last_hit_by != p) {
382 /* RACKET HIT */
383 if (!s->ball.ground_hit && s->ball.move_x != 0.0) {
384 s->status = "volley!";
385 } else {
386 s->status = format_status(s);
388 switch (PLAYER(s, p).desire) {
389 case DESIRE_NORMAL:
390 /* normal swing */
391 s->ball.move_x = 2.7 + 2.0*PLAYER(s, p).power/PLAYER_POWER_MAX;
392 s->ball.move_z = 1.2*PLAYER(s, p).power/PLAYER_POWER_MAX;
393 break;
394 case DESIRE_TOPSPIN:
395 /* top spin */
396 s->ball.move_x = 1.1 + 2.2*PLAYER(s, p).power/PLAYER_POWER_MAX;
397 s->ball.move_z = 2.5*PLAYER(s, p).power/PLAYER_POWER_MAX;
398 break;
399 case DESIRE_SMASH:
400 /* smash */
401 s->ball.move_x = 4.0 + 3.0*PLAYER(s, p).power/PLAYER_POWER_MAX;
402 s->ball.move_z = 1.1*PLAYER(s, p).power/PLAYER_POWER_MAX;
403 break;
405 s->ball.move_y = get_move_y( s, p);
406 s->sound_events |= SOUND_EVENT_RACKET;
407 s->ball.ground_hit = false;
408 s->ball.inhibit_gravity = false;
409 s->ball.last_hit_by = p;
410 if (p==1) {
411 pan_sample_group(SOUND_RACKET_FIRST, SOUND_RACKET_LAST, 0.3);
412 } else {
413 pan_sample_group(SOUND_RACKET_FIRST, SOUND_RACKET_LAST, 0.7);
414 s->ball.move_x *= -1;
420 #if 0
421 FIXME n-gram predictor broken
422 if( IS_OUT_X(s->ball.x) || IS_OFFSCREEN_Y(s->ball.y)) {
423 if( IS_OUT_X(s->ball.x)) {
424 /*if( !s->history_is_locked && s->referee != REFEREE_OUT) {
425 s->history[s->history_size] = (int)(NGRAM_STEPS*s->ball.y/HEIGHT);
426 s->history_size++;
427 if( s->history_size == 3) {
428 s->ngram[s->history[0]][s->history[1]][s->history[2]] += 10;
429 s->ngram_prediction = ngram_predictor( s);
430 s->history[0] = s->history[1];
431 s->history[1] = s->history[2];
432 s->history_size--;
434 s->history_is_locked = true;
437 } else {
438 /*s->history_is_locked = false;*/
440 #endif
442 SDL_PumpEvents();
443 keys = SDL_GetKeyState(NULL);
445 if( s->time%50==0) {
446 if (keys['r']) {
447 if (s->rain == 0) {
448 play_sample_background(SOUND_RAIN);
450 s->rain += 10;
452 if (keys['t']) {
453 s->fog++;
455 if (keys['1']) {
456 s->wind++;
458 if (keys['2']) {
459 s->wind--;
463 if(!(SDL_GetTicks() < fading_start+FADE_DURATION) && !s->is_over) {
464 for (p=1; p<=MAXPLAYERS; p++) {
465 if( PLAYER(s, p).type == PLAYER_TYPE_HUMAN) {
466 input_human(s, p);
467 } else {
468 input_ai(s, p);
473 /* Maemo: The "F6" button is the "Fullscreen" button */
474 /*if( keys['f'] || keys[SDLK_F6]) SDL_WM_ToggleFullScreen( screen);*/
475 if( keys['y']) SDL_SaveBMP( screen, "screenshot.bmp");
477 /* Maemo: The "F4" button is the "Open menu" button */
478 if( keys['p'] || keys[SDLK_F4]) {
479 while( keys['p'] || keys[SDLK_F4]) {
480 SDL_PollEvent( &e);
481 keys = SDL_GetKeyState( NULL);
482 SDL_Delay( 10);
484 while( (keys['p'] || keys[SDLK_F4]) == 0) {
485 SDL_PollEvent( &e);
486 keys = SDL_GetKeyState( NULL);
487 SDL_Delay( 10);
489 while( keys['p'] || keys[SDLK_F4]) {
490 SDL_PollEvent( &e);
491 keys = SDL_GetKeyState( NULL);
492 SDL_Delay( 10);
494 s->was_stopped = true;
497 if( keys[SDLK_ESCAPE] || keys['q']) return true;
499 limit_value( &PLAYER(s, 1).y, PLAYER_Y_MIN, PLAYER_Y_MAX);
500 limit_value( &PLAYER(s, 2).y, PLAYER_Y_MIN, PLAYER_Y_MAX);
502 if (s->ball.move_x > 0 && s->wind > 0) {
503 s->ball.x += absf(s->wind)/2;
504 } else if (s->ball.move_x < 0 && s->wind < 0) {
505 s->ball.x -= absf(s->wind)/2;
508 s->ball.z += s->ball.move_z;
509 if (!s->ball.inhibit_gravity) {
510 s->ball.move_z += GRAVITY;
513 s->ball.x += s->ball.move_x;
514 s->ball.y += s->ball.move_y;
516 for (p=1; p<=MAXPLAYERS; p++) {
517 if (PLAYER(s, p).use_power) {
518 PLAYER(s, p).power = maxf(0.0, minf(PLAYER(s, p).power*PLAYER(s, p).power_down_factor, PLAYER_POWER_MAX));
522 return false;
525 void render( GameState* s) {
526 int x, y, b;
527 unsigned int i;
528 float zoom;
529 float rotate;
530 #ifdef EXTENDED_REFEREE
531 int t=1000;
532 #endif
533 if (s->sound_events != 0) {
534 if (s->sound_events & SOUND_EVENT_GROUND) {
535 play_sample(SOUND_GROUND);
537 if (s->sound_events & SOUND_EVENT_OUT) {
538 play_sample(SOUND_OUT);
540 if (s->sound_events & SOUND_EVENT_APPLAUSE) {
541 play_sample(SOUND_APPLAUSE);
543 if (s->sound_events & SOUND_EVENT_RACKET) {
544 play_sample(SOUND_RACKET);
546 s->sound_events = 0;
548 if( s->winner != WINNER_NONE) {
549 if( !s->is_over) {
550 start_fade();
551 s->is_over = true;
553 clear_screen();
554 store_screen();
555 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);
556 sprintf( s->game_score_str, "player %d wins the match with %s", s->winner, format_sets( s));
557 font_draw_string(FONT_LARGE, s->game_score_str, (WIDTH-font_get_string_width(FONT_LARGE, s->game_score_str))/2, HEIGHT/2 + 30);
558 updatescr();
559 return;
561 if (s->displayed_court_type != s->location->court_type || s->text_changed || s->old_referee != s->referee) {
562 clear_screen();
563 fill_image(s->location->court_type, 120, 120, 400, 250);
564 show_image(GR_COURT, 0, 0, 255);
565 font_draw_string(FONT_XLARGE, s->game_score_str, 14, 14);
566 font_draw_string(FONT_XLARGE, s->sets_score_str, (WIDTH-font_get_string_width(FONT_XLARGE, s->sets_score_str))-14, 14);
567 if (s->location->has_referee) {
568 #ifdef EXTENDED_REFEREE
569 switch (s->referee) {
570 case REFEREE_NORMAL:
571 t = 1000;
572 break;
573 case REFEREE_OUT:
574 t = 200;
575 break;
576 case REFEREE_PLAYER1:
577 case REFEREE_PLAYER2:
578 t = 400;
579 break;
581 t = (s->time/t)%4;
582 switch (t) {
583 case 0:
584 t=0;
585 break;
586 case 1:
587 t=1;
588 break;
589 case 2:
590 t=0;
591 break;
592 case 3:
593 t=2;
594 break;
596 show_sprite( GR_REFEREE, s->referee*3+t, 12, 250, 10, 255);
597 if (voice_finished_flag == 0) {
598 show_sprite(GR_TALK, (s->time/150)%2, 2, 280, 45, 255);
600 #else
601 show_sprite( GR_REFEREE, s->referee, 4, 250, 10, 255);
602 #endif
604 s->displayed_court_type = s->location->court_type;
605 s->text_changed = false;
606 s->old_referee = s->referee;
607 store_screen();
610 /* show_sprite( GR_RACKET, (!PLAYER(s, 1).state), 4, PLAYER(s, 1).x-RACKET_X_MID, PLAYER(s, 1).y-RACKET_Y_MID, 255);
611 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);*/
612 show_sprite( GR_RACKET, !PLAYER(s, 1).use_power, 4, PLAYER(s, 1).x-RACKET_X_MID, PLAYER(s, 1).y-RACKET_Y_MID, 255);
613 show_sprite( GR_RACKET, !PLAYER(s, 2).use_power + 2, 4, PLAYER(s, 2).x-RACKET_X_MID, PLAYER(s, 2).y-RACKET_Y_MID, 255);
615 rectangle(10, HEIGHT-30, PLAYER_POWER_MAX, 10, 50, 50, 50);
616 rectangle(WIDTH-PLAYER_POWER_MAX-10, HEIGHT-30, PLAYER_POWER_MAX, 10, 50, 50, 50);
618 rectangle(10, HEIGHT-30, (int)(PLAYER(s, 1).power), 10, 200, 200, 200);
619 rectangle(WIDTH-PLAYER_POWER_MAX-10, HEIGHT-30, (int)(PLAYER(s, 2).power), 10, 200, 200, 200);
621 if( s->ball.move_x > 0) {
622 b = (s->time/100)%BALL_STATES;
623 } else if( s->ball.move_x < 0) {
624 b = BALL_STATES-1-(s->time/100)%BALL_STATES;
625 } else {
626 b = 0;
629 rotate = 0.0;
630 zoom = maxf(0.5, minf(1.0, (float)(30.-(s->ball.z))/10.));
631 show_image_rotozoom(GR_SHADOW, s->ball.x, s->ball.y+3, rotate, zoom);
633 rotate = (-s->ball.move_x * (float)((float)s->time/10.));
634 zoom = 1.0 + maxf(0.0, (s->ball.z-10.)/100.);
635 show_image_rotozoom(GR_BALL, s->ball.x, s->ball.y - s->ball.z, rotate, zoom);
637 /* Player 1's mouse rectangle */
638 if (PLAYER(s, 1).input != NULL && PLAYER(s, 1).input->type == INPUT_TYPE_MOUSE) {
639 rectangle(PLAYER(s, 1).x-2+5, PLAYER(s, 1).input->my-2, 4, 4, 255, 255, 255);
640 rectangle(PLAYER(s, 1).x-1+5, PLAYER(s, 1).input->my-1, 2, 2, 0, 0, 0);
643 /* Player 2's mouse rectangle */
644 if (PLAYER(s, 2).input != NULL && PLAYER(s, 2).input->type == INPUT_TYPE_MOUSE) {
645 rectangle(PLAYER(s, 2).x-2+5, PLAYER(s, 2).input->my-2, 4, 4, 255, 255, 255);
646 rectangle(PLAYER(s, 2).x-1+5, PLAYER(s, 2).input->my-1, 2, 2, 0, 0, 0);
649 font_draw_string(FONT_MEDIUM, s->status, (WIDTH-font_get_string_width(FONT_MEDIUM, s->status))/2, HEIGHT-50);
651 for (i=0; i<s->rain; i++) {
652 x = rand()%WIDTH;
653 y = rand()%HEIGHT;
654 draw_line_faded(x, y, x+10+s->wind*5, y+30, 0, 0, 255, 100, 200, 255);
656 if (s->rain) {
658 * Cheap-ish update of the whole screen. This can
659 * probably be optimized.
661 update_rect(0, 0, WIDTH, HEIGHT);
664 #ifdef DEBUG
665 line_horiz( PLAYER(s, 1).y, 255, 0, 0);
666 line_horiz( PLAYER(s, 2).y, 0, 0, 255);
667 line_horiz( s->ball.y, 0, 255, 0);
669 line_vert( PLAYER(s, 1).x, 255, 0, 0);
670 line_vert( PLAYER(s, 2).x, 0, 0, 255);
671 line_vert( s->ball.x, 0, 255, 0);
673 line_horiz( GAME_Y_MIN, 100, 100, 100);
674 line_horiz( GAME_Y_MAX, 100, 100, 100);
675 #endif
676 switch (s->fog) {
677 default:
678 case 4:
679 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -s->time/150-s->windtime/200, 0);
680 case 3:
681 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, -s->time/100-s->windtime/150, 20);
682 case 2:
683 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -s->time/180-s->windtime/180, 80);
684 case 1:
685 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, s->time/200-s->windtime/100, 0);
686 case 0:
687 break;
689 if (s->night) {
690 show_image(GR_NIGHT, 0, 0, 255);
693 updatescr();
696 void limit_value( float* value, float min, float max) {
697 if( *value < min) {
698 *value = min;
699 } else if( *value > max) {
700 *value = max;
704 float get_move_y( GameState* s, unsigned char player) {
705 float pct, dest, x_len, y_len;
706 float py, by, pa, move_x;
708 py = (player==1)?(PLAYER(s, 1).y):(PLAYER(s, 2).y);
709 by = s->ball.y - s->ball.z;
710 pa = RACKET_Y_MID*2;
711 move_x = s->ball.move_x;
713 /* -1.0 .. 1.0 for racket hit position */
714 pct = maxf(-1.0, minf(1.0, (by-py)/(pa/2)));
716 /* Y destination for ball */
717 dest = GAME_Y_MID + pct*(GAME_Y_MAX-GAME_Y_MIN);
719 /* lengths for the ball's journey */
720 if( player == 1) {
721 x_len = GAME_X_MAX - s->ball.x;
722 } else {
723 x_len = s->ball.x - GAME_X_MIN;
725 y_len = dest - by + MOVE_Y_SEED-rand()%MOVE_Y_SEED*2;
727 /* return the should-be value for move_y */
728 return (y_len*move_x)/(x_len);
731 void input_human(GameState* s, int player) {
732 bool hit, topspin, smash;
733 float move_y;
735 /* For mouse input, hand the player coordinates to the InputDevice */
736 if (PLAYER(s, player).input->type == INPUT_TYPE_MOUSE) {
737 PLAYER(s, player).input->player_x = (int)(PLAYER(s, player).x);
738 PLAYER(s, player).input->player_y = (int)(PLAYER(s, player).y);
741 move_y = PLAYER_MOVE_Y*input_device_get_axis(PLAYER(s, player).input, INPUT_AXIS_Y);
743 hit = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_HIT);
744 topspin = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_TOPSPIN);
745 smash = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_SMASH);
747 if (move_y != 0) {
748 if (absf(move_y) > absf(move_y*PLAYER(s, player).accelerate)) {
749 move_y *= PLAYER(s, player).accelerate;
751 PLAYER(s, player).y += move_y;
752 PLAYER(s, player).accelerate *= PLAYER_ACCEL_INCREASE;
753 } else {
754 PLAYER(s, player).accelerate = PLAYER_ACCEL_DEFAULT;
757 if(hit || topspin || smash) {
758 PLAYER(s, player).desire = (topspin)?(DESIRE_TOPSPIN):(DESIRE_NORMAL);
759 PLAYER(s, player).desire = (smash)?(DESIRE_SMASH):(PLAYER(s, player).desire);
761 PLAYER(s, player).power = maxf(10.0, minf(PLAYER(s, player).power*PLAYER(s, player).power_up_factor, PLAYER_POWER_MAX));
762 PLAYER(s, player).use_power = false;
763 } else {
764 PLAYER(s, player).use_power = true;
768 void input_ai(GameState* s, int player) {
769 float fact = 1.7;
770 float target;
771 int ball_approaching = 0;
773 if ((PLAYER(s, player).x < GAME_X_MID && s->ball.move_x <= 0) ||
774 (PLAYER(s, player).x > GAME_X_MID && s->ball.move_x >= 0)) {
775 ball_approaching = 1;
778 /* FIXME - this is broken since the new physics model has been introduced */
780 if (absf(PLAYER(s, player).y - (s->ball.y-s->ball.z)) > RACKET_Y_MID*5) {
781 fact = 3.5;
784 if(1) {
785 if( PLAYER(s, player).desire == DESIRE_NORMAL && !IS_NEAR_Y_AI(PLAYER(s, player).y, (s->ball.y-s->ball.z)) && ball_approaching) {
786 if( PLAYER(s, player).y < (s->ball.y-s->ball.z)) {
787 PLAYER(s, player).y += minf(2*fact, (s->ball.y-s->ball.z) - PLAYER(s, player).y);
788 } else if( PLAYER(s, player).y > (s->ball.y-s->ball.z)) {
789 PLAYER(s, player).y -= minf(2*fact, PLAYER(s, player).y - (s->ball.y-s->ball.z));
793 if (IS_NEAR_Y(PLAYER(s, player).y, (s->ball.y-s->ball.z)) && IS_NEAR_X_AI(PLAYER(s, player).x, s->ball.x) && PLAYER(s, player).power > 90.) {
794 PLAYER(s, player).use_power = true;
795 } else if (ball_approaching) {
796 PLAYER(s, player).power = maxf(10., minf(PLAYER(s, player).power*PLAYER(s, player).power_up_factor, PLAYER_POWER_MAX));
797 PLAYER(s, player).use_power = false;
799 } else if( s->ngram_prediction > 0.0) {
800 target = s->ngram_prediction*((float)HEIGHT)/((float)(NGRAM_STEPS));
801 target = GAME_Y_MID + (target-GAME_Y_MID)*1.5;
803 if( PLAYER(s, player).desire == DESIRE_NORMAL && !IS_NEAR_Y_AI( PLAYER(s, player).y, target)) {
804 if( PLAYER(s, player).y < target) {
805 PLAYER(s, player).y += minf(fact, (target-PLAYER(s, player).y)/40.0);
806 } else if( PLAYER(s, player).y > target) {
807 PLAYER(s, player).y -= minf(fact, (PLAYER(s, player).y-target)/40.0);
813 void game_setup_serve( GameState* s) {
814 s->ball.z = 10.0;
815 s->ball.y = GAME_Y_MID;
816 s->ball.move_x = 0.0;
817 s->ball.move_y = 0.0;
818 s->ball.move_z = 0.0;
819 s->ball.last_hit_by = -1;
820 s->ball.inhibit_gravity = true;
822 if( s->serving_player == 1) {
823 s->ball.x = GAME_X_MIN-RACKET_X_MID*1.5;
824 } else {
825 s->ball.x = GAME_X_MAX+RACKET_X_MID*1.5;
829 float ngram_predictor( GameState* s) {
830 unsigned int count = 0;
831 unsigned long sum = 0;
832 int x, y, z;
833 float result;
835 if( s->history_size < 3) {
836 return 0.0;
839 x = s->history[1];
840 y = s->history[2];
842 for( z = 0; z<NGRAM_STEPS; z++) {
843 count += s->ngram[x][y][z];
844 sum += z * s->ngram[x][y][z];
847 result = ((float)(sum))/((float)(count));
848 #ifdef DEBUG
849 printf( "predicting next = %.2f\n", result);
850 #endif
852 return result;
855 int score_game(GameState* s) {
856 Player *last_hit_by = NULL;
857 Player *other = NULL;
859 Player* winner = NULL;
860 Player* loser = NULL;
862 /* determine "last hit by" and "other" */
863 if (s->ball.last_hit_by == 1) {
864 last_hit_by = &(PLAYER(s, 1));
865 other = &(PLAYER(s, 2));
866 } else {
867 last_hit_by = &(PLAYER(s, 2));
868 other = &(PLAYER(s, 1));
871 switch (s->score_event) {
872 case SCORE_EVENT_NET:
873 winner = other;
874 break;
875 case SCORE_EVENT_OUT:
876 case SCORE_EVENT_GROUND_INVALID:
877 case SCORE_EVENT_OFFSCREEN:
878 case SCORE_EVENT_GROUND_VALID:
879 if (s->ball.ground_hit) {
880 winner = last_hit_by;
881 } else {
882 winner = other;
884 break;
885 default:
886 break;
889 /* determine loser based on winner */
890 if (winner == last_hit_by) {
891 loser = other;
892 } else {
893 loser = last_hit_by;
896 /* we cannot be in an "impossibly high" set */
897 assert(s->current_set < SETS_TO_WIN*2-1);
899 winner->game++;
900 if( loser->game < winner->game-1) {
901 if( winner->game >= 4) {
902 winner->game = loser->game = 0;
903 winner->sets[s->current_set]++;
905 /* serving is changed when the "game" is over */
906 s->serving_player = (s->serving_player==1)?(2):(1);
908 #ifdef HAVE_VOICE_FILES
909 /* speak the current score */
910 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);
911 #endif
913 /* scoring the set.. */
914 if( (winner->sets[s->current_set] == 6 && loser->sets[s->current_set] < 5) ||
915 winner->sets[s->current_set] == 7) {
916 s->current_set++;
917 s->winner = game_get_winner( s);
922 /* forget this event - we've handled it */
923 s->score_event = SCORE_UNDECIDED;
925 if (winner == &PLAYER(s, 1)) {
926 return WINNER_PLAYER1;
927 } else {
928 return WINNER_PLAYER2;
932 char* format_sets( GameState* s) {
933 static char sets[100];
934 static char tmp[100];
935 int i, max = s->current_set;
937 sets[0] = '\0';
939 if( s->winner != WINNER_NONE) {
940 max--;
942 for( i=0; i<=max; i++) {
943 sprintf( tmp, "%d:%d, ", PLAYER(s, 1).sets[i], PLAYER(s, 2).sets[i]);
944 strcat( sets, tmp);
947 sets[strlen(sets)-2] = '\0';
949 return sets;
952 char* format_game( GameState* s) {
953 static char game[100];
954 static const int game_scoring[] = { 0, 15, 30, 40 };
956 if( PLAYER(s, 1).game < 4 && PLAYER(s, 2).game < 4) {
957 #ifdef HAVE_VOICE_FILES
958 if (PLAYER(s, 1).game > 0 || PLAYER(s, 2).game > 0) {
959 if (PLAYER(s, 1).game == PLAYER(s, 2).game) {
960 voice_say_list(2, VOICE_LOVE_IN + 2*(PLAYER(s, 1).game), VOICE_ALL);
961 } else {
962 voice_say_list(2, VOICE_LOVE_IN + 2*(PLAYER(s, 1).game), VOICE_LOVE_OUT + 2*(PLAYER(s, 2).game));
965 #endif
966 sprintf( game, "%d - %d", game_scoring[PLAYER(s, 1).game], game_scoring[PLAYER(s, 2).game]);
967 } else if( PLAYER(s, 1).game > PLAYER(s, 2).game) {
968 #ifdef HAVE_VOICE_FILES
969 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_ONE);
970 #endif
971 strcpy( game, "advantage player 1");
972 } else if( PLAYER(s, 1).game < PLAYER(s, 2).game) {
973 #ifdef HAVE_VOICE_FILES
974 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_TWO);
975 #endif
976 strcpy( game, "advantage player 2");
977 } else {
978 #ifdef HAVE_VOICE_FILES
979 voice_say_list(1, VOICE_DEUCE);
980 #endif
981 strcpy( game, "deuce");
984 return game;
987 char* format_status( GameState* s) {
988 static char status[100];
989 static const char* set_names[] = { "first", "second", "third", "fourth", "fifth" };
991 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]);
993 return status;
996 int game_get_winner( GameState* s) {
997 unsigned int i;
998 int sets[2] = {0};
1000 for( i=0; i<s->current_set; i++) {
1001 if( PLAYER(s, 1).sets[i] > PLAYER(s, 2).sets[i]) {
1002 sets[0]++;
1003 } else {
1004 sets[1]++;
1008 if( sets[0] == SETS_TO_WIN) return WINNER_PLAYER1;
1009 if( sets[1] == SETS_TO_WIN) return WINNER_PLAYER2;
1011 return WINNER_NONE;