5 * Copyright (C) 2003, 2007, 2008, 2009 Thomas Perl <thp@thpinfo.com>
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,
38 GameState
*gamestate_new() {
42 GameState
template = {
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
,
82 s
= (GameState
*)malloc(sizeof(GameState
));
83 if (s
== NULL
) abort();
85 memcpy(s
, &template, sizeof(GameState
));
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;
102 int gamestate_save(GameState
* s
, const char* filename
)
106 InputDevice
* input_devices
[MAXPLAYERS
];
110 char tmp
[MAXPATHLEN
];
112 assert(getcwd(tmp
, MAXPATHLEN
) == tmp
);
113 assert(chdir(getenv("HOME")) == 0);
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");
129 /* Save data for later recovery + clear */
130 location
= s
->location
;
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) {
143 /* Restore process-specific data */
144 s
->location
= location
;
146 for (i
=1; i
<=MAXPLAYERS
; i
++) {
147 PLAYER(s
, i
).input
= input_devices
[i
-1];
153 assert(chdir(tmp
) == 0);
160 GameState
* gamestate_load(const char* filename
)
165 char tmp
[MAXPATHLEN
];
167 assert(getcwd(tmp
, MAXPATHLEN
) == tmp
);
168 assert(chdir(getenv("HOME")) == 0);
171 fp
= fopen(filename
, "r");
174 s
= (GameState
*)malloc(sizeof(GameState
));
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" */
189 assert(chdir(tmp
) == 0);
196 void gameloop(GameState
*s
) {
197 Uint32 ot
= SDL_GetTicks();
199 Uint32 dt
= GAME_TICKS
;
201 Uint32 accumulator
= 0;
205 #ifdef ENABLE_FPS_LIMIT
206 Uint32 ft
, frames
; /* frame timer and frames */
209 strcpy(s
->game_score_str
, format_game(s
));
210 strcpy(s
->sets_score_str
, format_sets(s
));
211 s
->text_changed
= true;
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
240 while( accumulator
>= dt
) {
243 s
->windtime
+= s
->wind
*dt
;
246 if( s
->was_stopped
) {
248 s
->was_stopped
= false;
251 if (s
->timelimit
!= 0 && s
->time
>= s
->timelimit
) {
255 #ifdef ENABLE_FPS_LIMIT
256 while (frames
*1000.0/((float)(SDL_GetTicks()-ft
+1))>(float)(DEFAULT_FPS
)) {
265 for (p
=1; p
<=MAXPLAYERS
; p
++) {
266 input_device_part_game(PLAYER(s
, p
).input
);
270 rectangle(0, 0, WIDTH
, HEIGHT
, 80, 80, 80);
273 stop_sample(SOUND_AUDIENCE
);
274 stop_sample(SOUND_RAIN
);
277 bool step( GameState
* s
) {
280 bool ground_event
= false;
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
;
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) {
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
;
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
;
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
;
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!";
340 /* first ground hit in valid area */
341 s
->ball
.ground_hit
= true;
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
)) {
362 s
->status
= "player 1 scores";
363 s
->referee
= REFEREE_PLAYER1
;
366 s
->status
= "player 2 scores";
367 s
->referee
= REFEREE_PLAYER2
;
375 strcpy(s
->game_score_str
, format_game(s
));
376 strcpy(s
->sets_score_str
, format_sets(s
));
377 s
->text_changed
= true;
380 if (s
->location
->max_visitors
> 100) {
381 s
->sound_events
|= SOUND_EVENT_APPLAUSE
;
383 /*FIXME n-gram predictor broken
385 s->history_is_locked = 0;
386 s->ngram_prediction = 0.0;*/
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
) {
393 if (!s
->ball
.ground_hit
&& s
->ball
.move_x
!= 0.0) {
394 s
->status
= "volley!";
396 s
->status
= format_status(s
);
398 switch (PLAYER(s
, p
).desire
) {
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
;
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
;
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
;
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
;
421 pan_sample_group(SOUND_RACKET_FIRST
, SOUND_RACKET_LAST
, 0.3);
423 pan_sample_group(SOUND_RACKET_FIRST
, SOUND_RACKET_LAST
, 0.7);
424 s
->ball
.move_x
*= -1;
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);
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];
444 s->history_is_locked = true;
448 /*s->history_is_locked = false;*/
453 keys
= SDL_GetKeyState(NULL
);
458 play_sample_background(SOUND_RAIN
);
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
) {
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
]) {
491 keys
= SDL_GetKeyState( NULL
);
494 while( (keys
['p'] || keys
[SDLK_F4
]) == 0) {
496 keys
= SDL_GetKeyState( NULL
);
499 while( keys
['p'] || keys
[SDLK_F4
]) {
501 keys
= SDL_GetKeyState( NULL
);
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
));
535 void render( GameState
* s
) {
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
);
557 if( s
->winner
!= WINNER_NONE
) {
563 rectangle(0, 0, WIDTH
, HEIGHT
, 80, 80, 80);
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);
571 if (s
->displayed_court_type
!= s
->location
->court_type
|| s
->text_changed
|| s
->old_referee
!= s
->referee
) {
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
) {
586 case REFEREE_PLAYER1
:
587 case REFEREE_PLAYER2
:
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
;
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
;
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
++) {
661 draw_line_faded(x
, y
, x
+10+s
->wind
*5, y
+30, 0, 0, 255, 100, 200, 255);
665 * Cheap-ish update of the whole screen. This can
666 * probably be optimized.
668 update_rect(0, 0, WIDTH
, HEIGHT
);
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);
686 fill_image_offset(GR_FOG2
, 0, 0, WIDTH
, HEIGHT
, -s
->time
/150-s
->windtime
/200, 0);
688 fill_image_offset(GR_FOG
, 0, 0, WIDTH
, HEIGHT
, -s
->time
/100-s
->windtime
/150, 20);
690 fill_image_offset(GR_FOG2
, 0, 0, WIDTH
, HEIGHT
, -s
->time
/180-s
->windtime
/180, 80);
692 fill_image_offset(GR_FOG
, 0, 0, WIDTH
, HEIGHT
, s
->time
/200-s
->windtime
/100, 0);
697 show_image(GR_NIGHT
, 0, 0, 255);
703 void limit_value( float* value
, float min
, float max
) {
706 } else if( *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
;
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 */
728 x_len
= GAME_X_MAX
- s
->ball
.x
;
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
;
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
);
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
;
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;
771 PLAYER(s
, player
).use_power
= true;
775 void input_ai(GameState
* s
, int player
) {
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) {
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
) {
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;
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;
842 if( s
->history_size
< 3) {
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
));
856 printf( "predicting next = %.2f\n", 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));
874 last_hit_by
= &(PLAYER(s
, 2));
875 other
= &(PLAYER(s
, 1));
878 switch (s
->score_event
) {
879 case SCORE_EVENT_NET
:
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
;
896 /* determine loser based on winner */
897 if (winner
== last_hit_by
) {
903 /* we cannot be in an "impossibly high" set */
904 assert(s
->current_set
< SETS_TO_WIN
*2-1);
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
);
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) {
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
;
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
;
946 if( s
->winner
!= WINNER_NONE
) {
949 for( i
=0; i
<=max
; i
++) {
950 sprintf( tmp
, "%d:%d, ", PLAYER(s
, 1).sets
[i
], PLAYER(s
, 2).sets
[i
]);
954 sets
[strlen(sets
)-2] = '\0';
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
);
969 voice_say_list(2, VOICE_LOVE_IN
+ 2*(PLAYER(s
, 1).game
), VOICE_LOVE_OUT
+ 2*(PLAYER(s
, 2).game
));
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
);
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
);
983 strcpy( game
, "advantage player 2");
985 #ifdef HAVE_VOICE_FILES
986 voice_say_list(1, VOICE_DEUCE
);
988 strcpy( game
, "deuce");
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
]);
1003 int game_get_winner( GameState
* s
) {
1007 for( i
=0; i
<s
->current_set
; i
++) {
1008 if( PLAYER(s
, 1).sets
[i
] > PLAYER(s
, 2).sets
[i
]) {
1015 if( sets
[0] == SETS_TO_WIN
) return WINNER_PLAYER1
;
1016 if( sets
[1] == SETS_TO_WIN
) return WINNER_PLAYER2
;