Mac OS X-specific release changes for Tennix
[tennix.git] / game.c
blob86723d49595ab8db99de6c281fc492f2b7c16d70
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;
203 int p;
205 #ifdef ENABLE_FPS_LIMIT
206 Uint32 ft, frames; /* frame timer and frames */
207 #endif
209 strcpy(s->game_score_str, format_game(s));
210 strcpy(s->sets_score_str, format_sets(s));
211 s->text_changed = true;
213 if (s->rain > 0) {
214 play_sample_background(SOUND_RAIN);
216 if (s->location->max_visitors > 100) {
217 play_sample_loop(SOUND_AUDIENCE);
219 /* Reset the court type, so it is redrawn on first display */
220 s->displayed_court_type = GR_COUNT;
222 for (p=1; p<=MAXPLAYERS; p++) {
223 input_device_join_game(PLAYER(s, p).input, s, p);
226 #ifdef ENABLE_FPS_LIMIT
227 frames = 0;
228 ft = SDL_GetTicks();
229 #endif
230 while( !quit) {
231 nt = SDL_GetTicks();
232 diff = nt-ot;
233 if( diff > 2000) {
234 diff = 0;
237 accumulator += diff;
238 ot = nt;
240 while( accumulator >= dt) {
241 quit = step(s);
242 s->time += dt;
243 s->windtime += s->wind*dt;
244 accumulator -= dt;
246 if( s->was_stopped) {
247 ot = SDL_GetTicks();
248 s->was_stopped = false;
251 if (s->timelimit != 0 && s->time >= s->timelimit) {
252 quit = 1;
255 #ifdef ENABLE_FPS_LIMIT
256 while (frames*1000.0/((float)(SDL_GetTicks()-ft+1))>(float)(DEFAULT_FPS)) {
257 SDL_Delay(10);
259 frames++;
260 #endif
262 render(s);
265 for (p=1; p<=MAXPLAYERS; p++) {
266 input_device_part_game(PLAYER(s, p).input);
269 clear_screen();
270 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
271 store_screen();
273 stop_sample(SOUND_AUDIENCE);
274 stop_sample(SOUND_RAIN);
277 bool step( GameState* s) {
278 Uint8 *keys;
279 SDL_Event e;
280 bool ground_event = false;
281 int p;
283 if (s->ball.z < 0) {
284 /* ground event */
285 ground_event = true;
287 s->referee = REFEREE_NORMAL;
289 /* bounce from the ground */
290 if (fabsf(s->ball.move_z) > 0.3) {
291 s->sound_events |= SOUND_EVENT_GROUND;
292 sample_volume_group(SOUND_GROUND_FIRST, SOUND_GROUND_LAST, fmaxf(0.0, fminf(1.0, fabsf(s->ball.move_z)/2)));
293 pan_sample_group(SOUND_GROUND_FIRST, SOUND_GROUND_LAST, fmaxf(0.0, fminf(1.0, (s->ball.x)/WIDTH)));
294 s->ball.move_z *= -s->ball.restitution;
295 } else {
296 s->ball.move_z = 0;
298 s->ball.z = 0;
301 if (NET_COLLISION_BALL(s->ball)) {
302 /* the net blocks movement of the ball */
303 while (NET_COLLISION_BALL(s->ball)) {
304 /* make sure the ball appears OUTSIDE of the net */
305 if (s->ball.move_x < 0) {
306 s->ball.x += 1;
307 } else {
308 s->ball.x -= 1;
311 s->ball.move_x = 0;
312 s->ball.move_y = 0;
315 /* see if we have something to score */
316 if (s->score_event == SCORE_UNDECIDED) {
317 if (NET_COLLISION_BALL(s->ball)) {
318 /* the ball "fell" into the net */
319 s->score_event = SCORE_EVENT_NET;
320 s->sound_events |= SOUND_EVENT_OUT;
321 s->status = "net!";
322 } else if (IS_OFFSCREEN(s->ball.x, s->ball.y)) {
323 /* ball flew offscreen */
324 s->score_event = SCORE_EVENT_OFFSCREEN;
325 s->sound_events |= SOUND_EVENT_OUT;
326 s->status = "out!";
327 } else if (ground_event) {
328 /* the ball hit the ground on the screen */
329 if (IS_OUT(s->ball.x, s->ball.y)) {
330 /* the ball bounced in the OUT area */
331 s->score_event = SCORE_EVENT_OUT;
332 s->sound_events |= SOUND_EVENT_OUT;
333 s->status = "out!";
334 } else if (GROUND_IS_VALID(s->ball.last_hit_by, s->ball.x, s->ball.y)) {
335 if (s->ball.ground_hit) {
336 s->score_event = SCORE_EVENT_GROUND_VALID;
337 s->sound_events |= SOUND_EVENT_OUT;
338 s->status = "did not catch the ball!";
339 } else {
340 /* first ground hit in valid area */
341 s->ball.ground_hit = true;
343 } else {
344 /* ball hit somewhere invalid */
345 s->score_event = SCORE_EVENT_GROUND_INVALID;
346 s->sound_events |= SOUND_EVENT_OUT;
347 s->status = "fault!";
352 if (s->score_event != SCORE_UNDECIDED) {
353 /* we have some scoring to do */
354 if (s->score_time == 0) {
355 /* schedule scoring in the future */
356 s->score_time = s->time + SCORING_DELAY;
357 s->referee = REFEREE_OUT;
358 } else if (s->time >= s->score_time) {
359 /* time has ran out - score now */
360 switch (score_game(s)) {
361 case WINNER_PLAYER1:
362 s->status = "player 1 scores";
363 s->referee = REFEREE_PLAYER1;
364 break;
365 case WINNER_PLAYER2:
366 s->status = "player 2 scores";
367 s->referee = REFEREE_PLAYER2;
368 break;
369 default:
370 assert(0);
371 break;
373 s->score_time = 0;
375 strcpy(s->game_score_str, format_game(s));
376 strcpy(s->sets_score_str, format_sets(s));
377 s->text_changed = true;
379 game_setup_serve(s);
380 if (s->location->max_visitors > 100) {
381 s->sound_events |= SOUND_EVENT_APPLAUSE;
383 /*FIXME n-gram predictor broken
384 s->history_size = 0;
385 s->history_is_locked = 0;
386 s->ngram_prediction = 0.0;*/
388 } else {
389 /* score is still undecided - do the racket swing thing */
390 for (p=1; p<=2; p++) {
391 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) {
392 /* RACKET HIT */
393 if (!s->ball.ground_hit && s->ball.move_x != 0.0) {
394 s->status = "volley!";
395 } else {
396 s->status = format_status(s);
398 switch (PLAYER(s, p).desire) {
399 case DESIRE_NORMAL:
400 /* normal swing */
401 s->ball.move_x = 2.7 + 2.0*PLAYER(s, p).power/PLAYER_POWER_MAX;
402 s->ball.move_z = 1.2*PLAYER(s, p).power/PLAYER_POWER_MAX;
403 break;
404 case DESIRE_TOPSPIN:
405 /* top spin */
406 s->ball.move_x = 1.1 + 2.2*PLAYER(s, p).power/PLAYER_POWER_MAX;
407 s->ball.move_z = 2.5*PLAYER(s, p).power/PLAYER_POWER_MAX;
408 break;
409 case DESIRE_SMASH:
410 /* smash */
411 s->ball.move_x = 4.0 + 3.0*PLAYER(s, p).power/PLAYER_POWER_MAX;
412 s->ball.move_z = 1.1*PLAYER(s, p).power/PLAYER_POWER_MAX;
413 break;
415 s->ball.move_y = get_move_y( s, p);
416 s->sound_events |= SOUND_EVENT_RACKET;
417 s->ball.ground_hit = false;
418 s->ball.inhibit_gravity = false;
419 s->ball.last_hit_by = p;
420 if (p==1) {
421 pan_sample_group(SOUND_RACKET_FIRST, SOUND_RACKET_LAST, 0.3);
422 } else {
423 pan_sample_group(SOUND_RACKET_FIRST, SOUND_RACKET_LAST, 0.7);
424 s->ball.move_x *= -1;
430 #if 0
431 FIXME n-gram predictor broken
432 if( IS_OUT_X(s->ball.x) || IS_OFFSCREEN_Y(s->ball.y)) {
433 if( IS_OUT_X(s->ball.x)) {
434 /*if( !s->history_is_locked && s->referee != REFEREE_OUT) {
435 s->history[s->history_size] = (int)(NGRAM_STEPS*s->ball.y/HEIGHT);
436 s->history_size++;
437 if( s->history_size == 3) {
438 s->ngram[s->history[0]][s->history[1]][s->history[2]] += 10;
439 s->ngram_prediction = ngram_predictor( s);
440 s->history[0] = s->history[1];
441 s->history[1] = s->history[2];
442 s->history_size--;
444 s->history_is_locked = true;
447 } else {
448 /*s->history_is_locked = false;*/
450 #endif
452 SDL_PumpEvents();
453 keys = SDL_GetKeyState(NULL);
455 if( s->time%50==0) {
456 if (keys['r']) {
457 if (s->rain == 0) {
458 play_sample_background(SOUND_RAIN);
460 s->rain += 10;
462 if (keys['t']) {
463 s->fog++;
465 if (keys['1']) {
466 s->wind++;
468 if (keys['2']) {
469 s->wind--;
473 if(!(SDL_GetTicks() < fading_start+FADE_DURATION) && !s->is_over) {
474 for (p=1; p<=MAXPLAYERS; p++) {
475 if( PLAYER(s, p).type == PLAYER_TYPE_HUMAN) {
476 input_human(s, p);
477 } else {
478 input_ai(s, p);
483 /* Maemo: The "F6" button is the "Fullscreen" button */
484 /*if( keys['f'] || keys[SDLK_F6]) SDL_WM_ToggleFullScreen( screen);*/
485 if( keys['y']) SDL_SaveBMP( screen, "screenshot.bmp");
487 /* Maemo: The "F4" button is the "Open menu" button */
488 if( keys['p'] || keys[SDLK_F4]) {
489 while( keys['p'] || keys[SDLK_F4]) {
490 SDL_PollEvent( &e);
491 keys = SDL_GetKeyState( NULL);
492 SDL_Delay( 10);
494 while( (keys['p'] || keys[SDLK_F4]) == 0) {
495 SDL_PollEvent( &e);
496 keys = SDL_GetKeyState( NULL);
497 SDL_Delay( 10);
499 while( keys['p'] || keys[SDLK_F4]) {
500 SDL_PollEvent( &e);
501 keys = SDL_GetKeyState( NULL);
502 SDL_Delay( 10);
504 s->was_stopped = true;
507 if( keys[SDLK_ESCAPE] || keys['q']) return true;
509 limit_value( &PLAYER(s, 1).y, PLAYER_Y_MIN, PLAYER_Y_MAX);
510 limit_value( &PLAYER(s, 2).y, PLAYER_Y_MIN, PLAYER_Y_MAX);
512 if (s->ball.move_x > 0 && s->wind > 0) {
513 s->ball.x += fabsf(s->wind)/2;
514 } else if (s->ball.move_x < 0 && s->wind < 0) {
515 s->ball.x -= fabsf(s->wind)/2;
518 s->ball.z += s->ball.move_z;
519 if (!s->ball.inhibit_gravity) {
520 s->ball.move_z += GRAVITY;
523 s->ball.x += s->ball.move_x;
524 s->ball.y += s->ball.move_y;
526 for (p=1; p<=MAXPLAYERS; p++) {
527 if (PLAYER(s, p).use_power) {
528 PLAYER(s, p).power = fmaxf(0.0, fminf(PLAYER(s, p).power*PLAYER(s, p).power_down_factor, PLAYER_POWER_MAX));
532 return false;
535 void render( GameState* s) {
536 int x, y, b;
537 unsigned int i;
538 float zoom;
539 float rotate;
540 int t=1000;
542 if (s->sound_events != 0) {
543 if (s->sound_events & SOUND_EVENT_GROUND) {
544 play_sample(SOUND_GROUND);
546 if (s->sound_events & SOUND_EVENT_OUT) {
547 play_sample(SOUND_OUT);
549 if (s->sound_events & SOUND_EVENT_APPLAUSE) {
550 play_sample(SOUND_APPLAUSE);
552 if (s->sound_events & SOUND_EVENT_RACKET) {
553 play_sample(SOUND_RACKET);
555 s->sound_events = 0;
557 if( s->winner != WINNER_NONE) {
558 if( !s->is_over) {
559 start_fade();
560 s->is_over = true;
562 clear_screen();
563 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
564 store_screen();
565 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);
566 sprintf( s->game_score_str, "player %d wins the match with %s", s->winner, format_sets( s));
567 font_draw_string(FONT_LARGE, s->game_score_str, (WIDTH-font_get_string_width(FONT_LARGE, s->game_score_str))/2, HEIGHT/2 + 30);
568 updatescr();
569 return;
571 if (s->displayed_court_type != s->location->court_type || s->text_changed || s->old_referee != s->referee) {
572 clear_screen();
573 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
574 fill_image(s->location->court_type, 120, 120, 400, 250);
575 show_image(GR_COURT, 0, 0, 255);
576 font_draw_string(FONT_XLARGE, s->game_score_str, 14, 14);
577 font_draw_string(FONT_XLARGE, s->sets_score_str, (WIDTH-font_get_string_width(FONT_XLARGE, s->sets_score_str))-14, 14);
578 if (s->location->has_referee) {
579 switch (s->referee) {
580 case REFEREE_NORMAL:
581 t = 1000;
582 break;
583 case REFEREE_OUT:
584 t = 200;
585 break;
586 case REFEREE_PLAYER1:
587 case REFEREE_PLAYER2:
588 t = 400;
589 break;
591 t = (s->time/t)%4;
592 switch (t) {
593 case 0:
594 t=0;
595 break;
596 case 1:
597 t=1;
598 break;
599 case 2:
600 t=0;
601 break;
602 case 3:
603 t=2;
604 break;
606 show_sprite( GR_REFEREE, s->referee*3+t, 12, 250, 10, 255);
607 if (voice_finished_flag == 0) {
608 show_sprite(GR_TALK, (s->time/150)%2, 2, 280, 45, 255);
611 s->displayed_court_type = s->location->court_type;
612 s->text_changed = false;
613 s->old_referee = s->referee;
614 store_screen();
617 /* show_sprite( GR_RACKET, (!PLAYER(s, 1).state), 4, PLAYER(s, 1).x-RACKET_X_MID, PLAYER(s, 1).y-RACKET_Y_MID, 255);
618 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);*/
619 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);
620 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);
622 rectangle(10, HEIGHT-30, PLAYER_POWER_MAX, 10, 50, 50, 50);
623 rectangle(WIDTH-PLAYER_POWER_MAX-10, HEIGHT-30, PLAYER_POWER_MAX, 10, 50, 50, 50);
625 rectangle(10, HEIGHT-30, (int)(PLAYER(s, 1).power), 10, 200, 200, 200);
626 rectangle(WIDTH-PLAYER_POWER_MAX-10, HEIGHT-30, (int)(PLAYER(s, 2).power), 10, 200, 200, 200);
628 if( s->ball.move_x > 0) {
629 b = (s->time/100)%BALL_STATES;
630 } else if( s->ball.move_x < 0) {
631 b = BALL_STATES-1-(s->time/100)%BALL_STATES;
632 } else {
633 b = 0;
636 rotate = 0.0;
637 zoom = fmaxf(0.5, fminf(1.0, (float)(30.-(s->ball.z))/10.));
638 show_image_rotozoom(GR_SHADOW, s->ball.x, s->ball.y+3, rotate, zoom);
640 rotate = (-s->ball.move_x * (float)((float)s->time/10.));
641 zoom = 1.0 + fmaxf(0.0, (s->ball.z-10.)/100.);
642 show_image_rotozoom(GR_BALL, s->ball.x, s->ball.y - s->ball.z, rotate, zoom);
644 /* Player 1's mouse rectangle */
645 if (PLAYER(s, 1).input != NULL && PLAYER(s, 1).input->type == INPUT_TYPE_MOUSE) {
646 rectangle(PLAYER(s, 1).x-2+5, PLAYER(s, 1).input->my-2, 4, 4, 255, 255, 255);
647 rectangle(PLAYER(s, 1).x-1+5, PLAYER(s, 1).input->my-1, 2, 2, 0, 0, 0);
650 /* Player 2's mouse rectangle */
651 if (PLAYER(s, 2).input != NULL && PLAYER(s, 2).input->type == INPUT_TYPE_MOUSE) {
652 rectangle(PLAYER(s, 2).x-2+5, PLAYER(s, 2).input->my-2, 4, 4, 255, 255, 255);
653 rectangle(PLAYER(s, 2).x-1+5, PLAYER(s, 2).input->my-1, 2, 2, 0, 0, 0);
656 font_draw_string(FONT_MEDIUM, s->status, (WIDTH-font_get_string_width(FONT_MEDIUM, s->status))/2, HEIGHT-50);
658 for (i=0; i<s->rain; i++) {
659 x = rand()%WIDTH;
660 y = rand()%HEIGHT;
661 draw_line_faded(x, y, x+10+s->wind*5, y+30, 0, 0, 255, 100, 200, 255);
663 if (s->rain) {
665 * Cheap-ish update of the whole screen. This can
666 * probably be optimized.
668 update_rect(0, 0, WIDTH, HEIGHT);
671 #ifdef DEBUG
672 line_horiz( PLAYER(s, 1).y, 255, 0, 0);
673 line_horiz( PLAYER(s, 2).y, 0, 0, 255);
674 line_horiz( s->ball.y, 0, 255, 0);
676 line_vert( PLAYER(s, 1).x, 255, 0, 0);
677 line_vert( PLAYER(s, 2).x, 0, 0, 255);
678 line_vert( s->ball.x, 0, 255, 0);
680 line_horiz( GAME_Y_MIN, 100, 100, 100);
681 line_horiz( GAME_Y_MAX, 100, 100, 100);
682 #endif
683 switch (s->fog) {
684 default:
685 case 4:
686 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -s->time/150-s->windtime/200, 0);
687 case 3:
688 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, -s->time/100-s->windtime/150, 20);
689 case 2:
690 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -s->time/180-s->windtime/180, 80);
691 case 1:
692 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, s->time/200-s->windtime/100, 0);
693 case 0:
694 break;
696 if (s->night) {
697 show_image(GR_NIGHT, 0, 0, 255);
700 updatescr();
703 void limit_value( float* value, float min, float max) {
704 if( *value < min) {
705 *value = min;
706 } else if( *value > max) {
707 *value = max;
711 float get_move_y( GameState* s, unsigned char player) {
712 float pct, dest, x_len, y_len;
713 float py, by, pa, move_x;
715 py = (player==1)?(PLAYER(s, 1).y):(PLAYER(s, 2).y);
716 by = s->ball.y - s->ball.z;
717 pa = RACKET_Y_MID*2;
718 move_x = s->ball.move_x;
720 /* -1.0 .. 1.0 for racket hit position */
721 pct = fmaxf(-1.0, fminf(1.0, (by-py)/(pa/2)));
723 /* Y destination for ball */
724 dest = GAME_Y_MID + pct*(GAME_Y_MAX-GAME_Y_MIN);
726 /* lengths for the ball's journey */
727 if( player == 1) {
728 x_len = GAME_X_MAX - s->ball.x;
729 } else {
730 x_len = s->ball.x - GAME_X_MIN;
732 y_len = dest - by + MOVE_Y_SEED-rand()%MOVE_Y_SEED*2;
734 /* return the should-be value for move_y */
735 return (y_len*move_x)/(x_len);
738 void input_human(GameState* s, int player) {
739 bool hit, topspin, smash;
740 float move_y;
742 /* For mouse input, hand the player coordinates to the InputDevice */
743 if (PLAYER(s, player).input->type == INPUT_TYPE_MOUSE) {
744 PLAYER(s, player).input->player_x = (int)(PLAYER(s, player).x);
745 PLAYER(s, player).input->player_y = (int)(PLAYER(s, player).y);
748 move_y = PLAYER_MOVE_Y*input_device_get_axis(PLAYER(s, player).input, INPUT_AXIS_Y);
750 hit = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_HIT);
751 topspin = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_TOPSPIN);
752 smash = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_SMASH);
754 if (move_y != 0) {
755 if (fabsf(move_y) > fabsf(move_y*PLAYER(s, player).accelerate)) {
756 move_y *= PLAYER(s, player).accelerate;
758 PLAYER(s, player).y += move_y;
759 PLAYER(s, player).accelerate *= PLAYER_ACCEL_INCREASE;
760 } else {
761 PLAYER(s, player).accelerate = PLAYER_ACCEL_DEFAULT;
764 if(hit || topspin || smash) {
765 PLAYER(s, player).desire = (topspin)?(DESIRE_TOPSPIN):(DESIRE_NORMAL);
766 PLAYER(s, player).desire = (smash)?(DESIRE_SMASH):(PLAYER(s, player).desire);
768 PLAYER(s, player).power = fmaxf(10.0, fminf(PLAYER(s, player).power*PLAYER(s, player).power_up_factor, PLAYER_POWER_MAX));
769 PLAYER(s, player).use_power = false;
770 } else {
771 PLAYER(s, player).use_power = true;
775 void input_ai(GameState* s, int player) {
776 float fact = 1.7;
777 float target;
778 int ball_approaching = 0;
780 if ((PLAYER(s, player).x < GAME_X_MID && s->ball.move_x <= 0) ||
781 (PLAYER(s, player).x > GAME_X_MID && s->ball.move_x >= 0)) {
782 ball_approaching = 1;
785 /* FIXME - this is broken since the new physics model has been introduced */
787 if (fabsf(PLAYER(s, player).y - (s->ball.y-s->ball.z)) > RACKET_Y_MID*5) {
788 fact = 3.5;
791 if(1) {
792 if( PLAYER(s, player).desire == DESIRE_NORMAL && !IS_NEAR_Y_AI(PLAYER(s, player).y, (s->ball.y-s->ball.z)) && ball_approaching) {
793 if( PLAYER(s, player).y < (s->ball.y-s->ball.z)) {
794 PLAYER(s, player).y += fminf(2*fact, (s->ball.y-s->ball.z) - PLAYER(s, player).y);
795 } else if( PLAYER(s, player).y > (s->ball.y-s->ball.z)) {
796 PLAYER(s, player).y -= fminf(2*fact, PLAYER(s, player).y - (s->ball.y-s->ball.z));
800 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.) {
801 PLAYER(s, player).use_power = true;
802 } else if (ball_approaching) {
803 PLAYER(s, player).power = fmaxf(10., fminf(PLAYER(s, player).power*PLAYER(s, player).power_up_factor, PLAYER_POWER_MAX));
804 PLAYER(s, player).use_power = false;
806 } else if( s->ngram_prediction > 0.0) {
807 target = s->ngram_prediction*((float)HEIGHT)/((float)(NGRAM_STEPS));
808 target = GAME_Y_MID + (target-GAME_Y_MID)*1.5;
810 if( PLAYER(s, player).desire == DESIRE_NORMAL && !IS_NEAR_Y_AI( PLAYER(s, player).y, target)) {
811 if( PLAYER(s, player).y < target) {
812 PLAYER(s, player).y += fminf(fact, (target-PLAYER(s, player).y)/40.0);
813 } else if( PLAYER(s, player).y > target) {
814 PLAYER(s, player).y -= fminf(fact, (PLAYER(s, player).y-target)/40.0);
820 void game_setup_serve( GameState* s) {
821 s->ball.z = 10.0;
822 s->ball.y = GAME_Y_MID;
823 s->ball.move_x = 0.0;
824 s->ball.move_y = 0.0;
825 s->ball.move_z = 0.0;
826 s->ball.last_hit_by = -1;
827 s->ball.inhibit_gravity = true;
829 if( s->serving_player == 1) {
830 s->ball.x = GAME_X_MIN-RACKET_X_MID*1.5;
831 } else {
832 s->ball.x = GAME_X_MAX+RACKET_X_MID*1.5;
836 float ngram_predictor( GameState* s) {
837 unsigned int count = 0;
838 unsigned long sum = 0;
839 int x, y, z;
840 float result;
842 if( s->history_size < 3) {
843 return 0.0;
846 x = s->history[1];
847 y = s->history[2];
849 for( z = 0; z<NGRAM_STEPS; z++) {
850 count += s->ngram[x][y][z];
851 sum += z * s->ngram[x][y][z];
854 result = ((float)(sum))/((float)(count));
855 #ifdef DEBUG
856 printf( "predicting next = %.2f\n", result);
857 #endif
859 return result;
862 int score_game(GameState* s) {
863 Player *last_hit_by = NULL;
864 Player *other = NULL;
866 Player* winner = NULL;
867 Player* loser = NULL;
869 /* determine "last hit by" and "other" */
870 if (s->ball.last_hit_by == 1) {
871 last_hit_by = &(PLAYER(s, 1));
872 other = &(PLAYER(s, 2));
873 } else {
874 last_hit_by = &(PLAYER(s, 2));
875 other = &(PLAYER(s, 1));
878 switch (s->score_event) {
879 case SCORE_EVENT_NET:
880 winner = other;
881 break;
882 case SCORE_EVENT_OUT:
883 case SCORE_EVENT_GROUND_INVALID:
884 case SCORE_EVENT_OFFSCREEN:
885 case SCORE_EVENT_GROUND_VALID:
886 if (s->ball.ground_hit) {
887 winner = last_hit_by;
888 } else {
889 winner = other;
891 break;
892 default:
893 break;
896 /* determine loser based on winner */
897 if (winner == last_hit_by) {
898 loser = other;
899 } else {
900 loser = last_hit_by;
903 /* we cannot be in an "impossibly high" set */
904 assert(s->current_set < SETS_TO_WIN*2-1);
906 winner->game++;
907 if( loser->game < winner->game-1) {
908 if( winner->game >= 4) {
909 winner->game = loser->game = 0;
910 winner->sets[s->current_set]++;
912 /* serving is changed when the "game" is over */
913 s->serving_player = (s->serving_player==1)?(2):(1);
915 #ifdef HAVE_VOICE_FILES
916 /* speak the current score */
917 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);
918 #endif
920 /* scoring the set.. */
921 if( (winner->sets[s->current_set] == 6 && loser->sets[s->current_set] < 5) ||
922 winner->sets[s->current_set] == 7) {
923 s->current_set++;
924 s->winner = game_get_winner( s);
929 /* forget this event - we've handled it */
930 s->score_event = SCORE_UNDECIDED;
932 if (winner == &PLAYER(s, 1)) {
933 return WINNER_PLAYER1;
934 } else {
935 return WINNER_PLAYER2;
939 char* format_sets( GameState* s) {
940 static char sets[100];
941 static char tmp[100];
942 int i, max = s->current_set;
944 sets[0] = '\0';
946 if( s->winner != WINNER_NONE) {
947 max--;
949 for( i=0; i<=max; i++) {
950 sprintf( tmp, "%d:%d, ", PLAYER(s, 1).sets[i], PLAYER(s, 2).sets[i]);
951 strcat( sets, tmp);
954 sets[strlen(sets)-2] = '\0';
956 return sets;
959 char* format_game( GameState* s) {
960 static char game[100];
961 static const int game_scoring[] = { 0, 15, 30, 40 };
963 if( PLAYER(s, 1).game < 4 && PLAYER(s, 2).game < 4) {
964 #ifdef HAVE_VOICE_FILES
965 if (PLAYER(s, 1).game > 0 || PLAYER(s, 2).game > 0) {
966 if (PLAYER(s, 1).game == PLAYER(s, 2).game) {
967 voice_say_list(2, VOICE_LOVE_IN + 2*(PLAYER(s, 1).game), VOICE_ALL);
968 } else {
969 voice_say_list(2, VOICE_LOVE_IN + 2*(PLAYER(s, 1).game), VOICE_LOVE_OUT + 2*(PLAYER(s, 2).game));
972 #endif
973 sprintf( game, "%d - %d", game_scoring[PLAYER(s, 1).game], game_scoring[PLAYER(s, 2).game]);
974 } else if( PLAYER(s, 1).game > PLAYER(s, 2).game) {
975 #ifdef HAVE_VOICE_FILES
976 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_ONE);
977 #endif
978 strcpy( game, "advantage player 1");
979 } else if( PLAYER(s, 1).game < PLAYER(s, 2).game) {
980 #ifdef HAVE_VOICE_FILES
981 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_TWO);
982 #endif
983 strcpy( game, "advantage player 2");
984 } else {
985 #ifdef HAVE_VOICE_FILES
986 voice_say_list(1, VOICE_DEUCE);
987 #endif
988 strcpy( game, "deuce");
991 return game;
994 char* format_status( GameState* s) {
995 static char status[100];
996 static const char* set_names[] = { "first", "second", "third", "fourth", "fifth" };
998 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]);
1000 return status;
1003 int game_get_winner( GameState* s) {
1004 unsigned int i;
1005 int sets[2] = {0};
1007 for( i=0; i<s->current_set; i++) {
1008 if( PLAYER(s, 1).sets[i] > PLAYER(s, 2).sets[i]) {
1009 sets[0]++;
1010 } else {
1011 sets[1]++;
1015 if( sets[0] == SETS_TO_WIN) return WINNER_PLAYER1;
1016 if( sets[1] == SETS_TO_WIN) return WINNER_PLAYER2;
1018 return WINNER_NONE;