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() {
41 GameState
template = {
44 { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.6, false, -1, false },
46 { 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
, 0, {0}, PLAYER_ACCEL_DEFAULT
},
47 { 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
, 0, {0}, PLAYER_ACCEL_DEFAULT
},
54 SOUND_EVENT_NONE
, /* sound events */
57 EVENTCOUNTER_GAMESTATE_START
,
58 EVENTCOUNTER_GAMESTATE_START
,
62 s
= (GameState
*)malloc(sizeof(GameState
));
63 if (s
== NULL
) abort();
65 memcpy(s
, &template, sizeof(GameState
));
73 int gamestate_save(GameState
* s
, const char* filename
)
76 InputDevice
* input_devices
[MAXPLAYERS
];
82 assert(getcwd(tmp
, MAXPATHLEN
) == tmp
);
83 assert(chdir(getenv("HOME")) == 0);
87 * Process-specific data (pointers to data
88 * structures only of interest to this process)
89 * has to be "removed" before saving.
91 * It can later be restored (in gamestate_load).
94 fp
= fopen(filename
, "w");
99 /* Save data for later recovery + clear */
100 location
= s
->location
;
102 for (i
=1; i
<=MAXPLAYERS
; i
++) {
103 input_devices
[i
-1] = PLAYER(s
, i
).input
;
104 PLAYER(s
, i
).input
= NULL
;
107 if (fwrite(s
, sizeof(GameState
), 1, fp
) != 1) {
111 /* Restore process-specific data */
112 s
->location
= location
;
113 for (i
=1; i
<=MAXPLAYERS
; i
++) {
114 PLAYER(s
, i
).input
= input_devices
[i
-1];
120 assert(chdir(tmp
) == 0);
127 GameState
* gamestate_load(const char* filename
)
132 char tmp
[MAXPATHLEN
];
134 assert(getcwd(tmp
, MAXPATHLEN
) == tmp
);
135 assert(chdir(getenv("HOME")) == 0);
138 fp
= fopen(filename
, "r");
141 s
= (GameState
*)malloc(sizeof(GameState
));
143 if (fread(s
, sizeof(GameState
), 1, fp
) == 1) {
144 /* Restore/create process-specific data */
145 /* FIXME: s->location, players' "input" */
155 assert(chdir(tmp
) == 0);
162 void gameloop(GameState
*s
) {
163 Uint32 ot
= SDL_GetTicks();
165 Uint32 dt
= GAME_TICKS
;
167 Uint32 accumulator
= 0;
173 EVENTCOUNTER_RENDERSTATE_START
,
174 EVENTCOUNTER_RENDERSTATE_START
,
181 /* Catch-up with existing sound events */
182 r
.sound_events
= s
->sound_events
;
184 #ifdef ENABLE_FPS_LIMIT
185 Uint32 ft
, frames
; /* frame timer and frames */
188 if (s
->location
->rain
> 0) {
189 play_sample_background(SOUND_RAIN
);
191 if (s
->location
->max_visitors
> 100) {
192 play_sample_loop(SOUND_AUDIENCE
);
195 for (p
=1; p
<=MAXPLAYERS
; p
++) {
196 input_device_join_game(PLAYER(s
, p
).input
, s
, p
);
199 #ifdef ENABLE_FPS_LIMIT
213 while( accumulator
>= dt
) {
219 #ifdef ENABLE_FPS_LIMIT
220 while (frames
*1000.0/((float)(SDL_GetTicks()-ft
+1))>(float)(DEFAULT_FPS
)) {
229 for (p
=1; p
<=MAXPLAYERS
; p
++) {
230 input_device_part_game(PLAYER(s
, p
).input
);
234 rectangle(0, 0, WIDTH
, HEIGHT
, 80, 80, 80);
237 stop_sample(SOUND_AUDIENCE
);
238 stop_sample(SOUND_RAIN
);
241 bool step( GameState
* s
) {
243 bool ground_event
= false;
250 s
->referee
= REFEREE_NORMAL
;
252 /* bounce from the ground */
253 if (fabsf(s
->ball
.move_z
) > 0.3) {
254 s
->sound_events
^= SOUND_EVENT_GROUND
;
255 sample_volume_group(SOUND_GROUND_FIRST
, SOUND_GROUND_LAST
, fmaxf(0.0, fminf(1.0, fabsf(s
->ball
.move_z
)/2)));
256 pan_sample_group(SOUND_GROUND_FIRST
, SOUND_GROUND_LAST
, fmaxf(0.0, fminf(1.0, (s
->ball
.x
)/WIDTH
)));
257 s
->ball
.move_z
*= -s
->ball
.restitution
;
264 if (NET_COLLISION_BALL(s
->ball
)) {
265 /* the net blocks movement of the ball */
266 while (NET_COLLISION_BALL(s
->ball
)) {
267 /* make sure the ball appears OUTSIDE of the net */
268 if (s
->ball
.move_x
< 0) {
278 /* see if we have something to score */
279 if (s
->score_event
== SCORE_UNDECIDED
) {
280 if (NET_COLLISION_BALL(s
->ball
)) {
281 /* the ball "fell" into the net */
282 s
->score_event
= SCORE_EVENT_NET
;
283 s
->sound_events
^= SOUND_EVENT_OUT
;
284 s
->status_message
= STATUSMSG_NET
;
285 } else if (IS_OFFSCREEN(s
->ball
.x
, s
->ball
.y
)) {
286 /* ball flew offscreen */
287 s
->score_event
= SCORE_EVENT_OFFSCREEN
;
288 s
->sound_events
^= SOUND_EVENT_OUT
;
289 s
->status_message
= STATUSMSG_OUT
;
290 } else if (ground_event
) {
291 /* the ball hit the ground on the screen */
292 if (IS_OUT(s
->ball
.x
, s
->ball
.y
)) {
293 /* the ball bounced in the OUT area */
294 s
->score_event
= SCORE_EVENT_OUT
;
295 s
->sound_events
^= SOUND_EVENT_OUT
;
296 s
->status_message
= STATUSMSG_OUT
;
297 } else if (GROUND_IS_VALID(s
->ball
.last_hit_by
, s
->ball
.x
, s
->ball
.y
)) {
298 if (s
->ball
.ground_hit
) {
299 s
->score_event
= SCORE_EVENT_GROUND_VALID
;
300 s
->sound_events
^= SOUND_EVENT_OUT
;
301 s
->status_message
= STATUSMSG_DIDNTCATCH
;
303 /* first ground hit in valid area */
304 s
->ball
.ground_hit
= true;
307 /* ball hit somewhere invalid */
308 s
->score_event
= SCORE_EVENT_GROUND_INVALID
;
309 s
->sound_events
^= SOUND_EVENT_OUT
;
310 s
->status_message
= STATUSMSG_FAULT
;
315 if (s
->score_event
!= SCORE_UNDECIDED
) {
316 /* we have some scoring to do */
317 if (s
->score_time
== 0) {
318 /* schedule scoring in the future */
319 s
->score_time
= s
->time
+ SCORING_DELAY
;
320 s
->referee
= REFEREE_OUT
;
321 } else if (s
->time
>= s
->score_time
) {
322 /* time has ran out - score now */
323 switch (score_game(s
)) {
325 s
->status_message
= STATUSMSG_P1SCORES
;
326 s
->referee
= REFEREE_PLAYER1
;
329 s
->status_message
= STATUSMSG_P2SCORES
;
330 s
->referee
= REFEREE_PLAYER2
;
339 if (s
->location
->max_visitors
> 100) {
340 s
->sound_events
^= SOUND_EVENT_APPLAUSE
;
344 /* score is still undecided - do the racket swing thing */
345 for (p
=1; p
<=2; p
++) {
346 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
) {
348 if (!s
->ball
.ground_hit
&& s
->ball
.move_x
!= 0.0) {
349 s
->status_message
= STATUSMSG_VOLLEY
;
351 s
->status_message
= STATUSMSG_DEFAULT
;
353 switch (PLAYER(s
, p
).desire
) {
356 s
->ball
.move_x
= 2.7 + 2.0*PLAYER(s
, p
).power
/PLAYER_POWER_MAX
;
357 s
->ball
.move_z
= 1.2*PLAYER(s
, p
).power
/PLAYER_POWER_MAX
;
361 s
->ball
.move_x
= 1.1 + 2.2*PLAYER(s
, p
).power
/PLAYER_POWER_MAX
;
362 s
->ball
.move_z
= 2.5*PLAYER(s
, p
).power
/PLAYER_POWER_MAX
;
366 s
->ball
.move_x
= 4.0 + 3.0*PLAYER(s
, p
).power
/PLAYER_POWER_MAX
;
367 s
->ball
.move_z
= 1.1*PLAYER(s
, p
).power
/PLAYER_POWER_MAX
;
370 s
->ball
.move_y
= get_move_y( s
, p
);
371 s
->sound_events
^= SOUND_EVENT_RACKET
;
372 s
->ball
.ground_hit
= false;
373 s
->ball
.inhibit_gravity
= false;
374 s
->ball
.last_hit_by
= p
;
376 pan_sample_group(SOUND_RACKET_FIRST
, SOUND_RACKET_LAST
, 0.3);
378 pan_sample_group(SOUND_RACKET_FIRST
, SOUND_RACKET_LAST
, 0.7);
379 s
->ball
.move_x
*= -1;
386 keys
= SDL_GetKeyState(NULL
);
388 if(!(SDL_GetTicks() < fading_start
+FADE_DURATION
) && s
->winner
== WINNER_NONE
) {
389 for (p
=1; p
<=MAXPLAYERS
; p
++) {
390 if( PLAYER(s
, p
).type
== PLAYER_TYPE_HUMAN
) {
398 /* Maemo: The "F6" button is the "Fullscreen" button */
399 /*if( keys['f'] || keys[SDLK_F6]) SDL_WM_ToggleFullScreen( screen);*/
400 if( keys
['y']) SDL_SaveBMP( screen
, "screenshot.bmp");
402 /* Maemo: The "F4" button is the "Open menu" button */
403 if(keys
[SDLK_ESCAPE
] || keys
['q']
404 || keys
['p'] || keys
[SDLK_F4
]) {
408 limit_value( &PLAYER(s
, 1).y
, PLAYER_Y_MIN
, PLAYER_Y_MAX
);
409 limit_value( &PLAYER(s
, 2).y
, PLAYER_Y_MIN
, PLAYER_Y_MAX
);
411 s
->ball
.z
+= s
->ball
.move_z
;
412 if (!s
->ball
.inhibit_gravity
) {
413 s
->ball
.move_z
+= GRAVITY
;
416 s
->ball
.x
+= s
->ball
.move_x
;
417 s
->ball
.y
+= s
->ball
.move_y
;
419 for (p
=1; p
<=MAXPLAYERS
; p
++) {
420 if (PLAYER(s
, p
).use_power
) {
421 PLAYER(s
, p
).power
= fmaxf(0.0, fminf(PLAYER(s
, p
).power
*PLAYER(s
, p
).power_down_factor
, PLAYER_POWER_MAX
));
428 void render(const GameState
* s
, RenderState
* r
) {
436 /* The bits in sound_events flip when the sound should play */
437 if ((sounds
= (r
->sound_events
^ s
->sound_events
)) != 0) {
438 if (sounds
& SOUND_EVENT_GROUND
) {
439 play_sample(SOUND_GROUND
);
441 if (sounds
& SOUND_EVENT_OUT
) {
442 play_sample(SOUND_OUT
);
444 if (sounds
& SOUND_EVENT_APPLAUSE
) {
445 play_sample(SOUND_APPLAUSE
);
447 if (sounds
& SOUND_EVENT_RACKET
) {
448 play_sample(SOUND_RACKET
);
450 r
->sound_events
= s
->sound_events
;
453 if( s
->winner
!= WINNER_NONE
) {
455 rectangle(0, 0, WIDTH
, HEIGHT
, 80, 80, 80);
457 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);
458 /*sprintf( s->game_score_str, "player %d wins the match with %s", s->winner, format_sets( s));
459 font_draw_string(FONT_LARGE, s->game_score_str, (WIDTH-font_get_string_width(FONT_LARGE, s->game_score_str))/2, HEIGHT/2 + 30);*/
463 if ((r
->referee
!= s
->referee
) ||
464 (r
->ec_game
!= s
->ec_game
) ||
465 (r
->ec_sets
!= s
->ec_sets
) ||
466 (r
->status_message
!= s
->status_message
)) {
467 /* Update status message text */
468 if (r
->status_message
!= s
->status_message
) {
469 r
->text_status
= format_status(s
);
470 r
->status_message
= s
->status_message
;
472 /* Update game status text */
473 if (r
->ec_game
!= s
->ec_game
) {
474 r
->text_game
= format_game(s
);
475 r
->ec_game
= s
->ec_game
;
477 /* Update set status text */
478 if (r
->ec_sets
!= s
->ec_sets
) {
479 r
->text_sets
= format_sets(s
);
480 if (r
->status_message
== STATUSMSG_DEFAULT
) {
481 r
->text_status
= format_status(s
);
483 r
->ec_sets
= s
->ec_sets
;
486 rectangle(0, 0, WIDTH
, HEIGHT
, 80, 80, 80);
487 fill_image(s
->location
->court_type
, 120, 120, 400, 250);
488 show_image(GR_COURT
, 0, 0, 255);
489 font_draw_string(FONT_XLARGE
, r
->text_game
, 14, 14);
490 font_draw_string(FONT_XLARGE
, r
->text_sets
,
491 (WIDTH
-font_get_string_width(FONT_XLARGE
, r
->text_sets
))-14, 14);
492 if (s
->location
->has_referee
) {
493 switch (s
->referee
) {
500 case REFEREE_PLAYER1
:
501 case REFEREE_PLAYER2
:
520 show_sprite( GR_REFEREE
, s
->referee
*3+t
, 12, 250, 10, 255);
521 if (voice_finished_flag
== 0) {
522 show_sprite(GR_TALK
, (s
->time
/150)%2, 2, 280, 45, 255);
525 r
->referee
= s
->referee
;
529 /* show_sprite( GR_RACKET, (!PLAYER(s, 1).state), 4, PLAYER(s, 1).x-RACKET_X_MID, PLAYER(s, 1).y-RACKET_Y_MID, 255);
530 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);*/
531 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);
532 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);
534 rectangle(10, HEIGHT
-30, PLAYER_POWER_MAX
, 10, 50, 50, 50);
535 rectangle(WIDTH
-PLAYER_POWER_MAX
-10, HEIGHT
-30, PLAYER_POWER_MAX
, 10, 50, 50, 50);
537 rectangle(10, HEIGHT
-30, (int)(PLAYER(s
, 1).power
), 10, 200, 200, 200);
538 rectangle(WIDTH
-PLAYER_POWER_MAX
-10, HEIGHT
-30, (int)(PLAYER(s
, 2).power
), 10, 200, 200, 200);
540 if( s
->ball
.move_x
> 0) {
541 b
= (s
->time
/100)%BALL_STATES
;
542 } else if( s
->ball
.move_x
< 0) {
543 b
= BALL_STATES
-1-(s
->time
/100)%BALL_STATES
;
549 zoom
= fmaxf(0.5, fminf(1.0, (float)(30.-(s
->ball
.z
))/10.));
550 show_image_rotozoom(GR_SHADOW
, s
->ball
.x
, s
->ball
.y
+3, rotate
, zoom
);
552 rotate
= (-s
->ball
.move_x
* (float)((float)s
->time
/10.));
553 zoom
= 1.0 + fmaxf(0.0, (s
->ball
.z
-10.)/100.);
554 show_image_rotozoom(GR_BALL
, s
->ball
.x
, s
->ball
.y
- s
->ball
.z
, rotate
, zoom
);
556 /* Player 1's mouse rectangle */
557 if (PLAYER(s
, 1).input
!= NULL
&& PLAYER(s
, 1).input
->type
== INPUT_TYPE_MOUSE
) {
558 rectangle(PLAYER(s
, 1).x
-2+5, PLAYER(s
, 1).input
->my
-2, 4, 4, 255, 255, 255);
559 rectangle(PLAYER(s
, 1).x
-1+5, PLAYER(s
, 1).input
->my
-1, 2, 2, 0, 0, 0);
562 /* Player 2's mouse rectangle */
563 if (PLAYER(s
, 2).input
!= NULL
&& PLAYER(s
, 2).input
->type
== INPUT_TYPE_MOUSE
) {
564 rectangle(PLAYER(s
, 2).x
-2+5, PLAYER(s
, 2).input
->my
-2, 4, 4, 255, 255, 255);
565 rectangle(PLAYER(s
, 2).x
-1+5, PLAYER(s
, 2).input
->my
-1, 2, 2, 0, 0, 0);
568 font_draw_string(FONT_MEDIUM
, r
->text_status
,
569 (WIDTH
-font_get_string_width(FONT_MEDIUM
, r
->text_status
))/2, HEIGHT
-50);
571 for (i
=0; i
<s
->location
->rain
; i
++) {
574 draw_line_faded(x
, y
, x
+10, y
+30, 0, 0, 255, 100, 200, 255);
576 if (s
->location
->rain
) {
578 * Cheap-ish update of the whole screen. This can
579 * probably be optimized.
581 update_rect(0, 0, WIDTH
, HEIGHT
);
585 line_horiz( PLAYER(s
, 1).y
, 255, 0, 0);
586 line_horiz( PLAYER(s
, 2).y
, 0, 0, 255);
587 line_horiz( s
->ball
.y
, 0, 255, 0);
589 line_vert( PLAYER(s
, 1).x
, 255, 0, 0);
590 line_vert( PLAYER(s
, 2).x
, 0, 0, 255);
591 line_vert( s
->ball
.x
, 0, 255, 0);
593 line_horiz( GAME_Y_MIN
, 100, 100, 100);
594 line_horiz( GAME_Y_MAX
, 100, 100, 100);
596 switch (s
->location
->fog
) {
599 fill_image_offset(GR_FOG2
, 0, 0, WIDTH
, HEIGHT
, -s
->time
/150, 0);
601 fill_image_offset(GR_FOG
, 0, 0, WIDTH
, HEIGHT
, -s
->time
/100, 20);
603 fill_image_offset(GR_FOG2
, 0, 0, WIDTH
, HEIGHT
, -s
->time
/180, 80);
605 fill_image_offset(GR_FOG
, 0, 0, WIDTH
, HEIGHT
, s
->time
/200, 0);
609 if (s
->location
->night
) {
610 show_image(GR_NIGHT
, 0, 0, 255);
616 void limit_value( float* value
, float min
, float max
) {
619 } else if( *value
> max
) {
624 float get_move_y( GameState
* s
, unsigned char player
) {
625 float pct
, dest
, x_len
, y_len
;
626 float py
, by
, pa
, move_x
;
628 py
= (player
==1)?(PLAYER(s
, 1).y
):(PLAYER(s
, 2).y
);
629 by
= s
->ball
.y
- s
->ball
.z
;
631 move_x
= s
->ball
.move_x
;
633 /* -1.0 .. 1.0 for racket hit position */
634 pct
= fmaxf(-1.0, fminf(1.0, (by
-py
)/(pa
/2)));
636 /* Y destination for ball */
637 dest
= GAME_Y_MID
+ pct
*(GAME_Y_MAX
-GAME_Y_MIN
);
639 /* lengths for the ball's journey */
641 x_len
= GAME_X_MAX
- s
->ball
.x
;
643 x_len
= s
->ball
.x
- GAME_X_MIN
;
645 y_len
= dest
- by
+ MOVE_Y_SEED
-rand()%MOVE_Y_SEED
*2;
647 /* return the should-be value for move_y */
648 return (y_len
*move_x
)/(x_len
);
651 void input_human(GameState
* s
, int player
) {
652 bool hit
, topspin
, smash
;
655 /* For mouse input, hand the player coordinates to the InputDevice */
656 if (PLAYER(s
, player
).input
->type
== INPUT_TYPE_MOUSE
) {
657 PLAYER(s
, player
).input
->player_x
= (int)(PLAYER(s
, player
).x
);
658 PLAYER(s
, player
).input
->player_y
= (int)(PLAYER(s
, player
).y
);
661 move_y
= PLAYER_MOVE_Y
*input_device_get_axis(PLAYER(s
, player
).input
, INPUT_AXIS_Y
);
663 hit
= input_device_get_key(PLAYER(s
, player
).input
, INPUT_KEY_HIT
);
664 topspin
= input_device_get_key(PLAYER(s
, player
).input
, INPUT_KEY_TOPSPIN
);
665 smash
= input_device_get_key(PLAYER(s
, player
).input
, INPUT_KEY_SMASH
);
668 if (fabsf(move_y
) > fabsf(move_y
*PLAYER(s
, player
).accelerate
)) {
669 move_y
*= PLAYER(s
, player
).accelerate
;
671 PLAYER(s
, player
).y
+= move_y
;
672 PLAYER(s
, player
).accelerate
*= PLAYER_ACCEL_INCREASE
;
674 PLAYER(s
, player
).accelerate
= PLAYER_ACCEL_DEFAULT
;
677 if(hit
|| topspin
|| smash
) {
678 PLAYER(s
, player
).desire
= (topspin
)?(DESIRE_TOPSPIN
):(DESIRE_NORMAL
);
679 PLAYER(s
, player
).desire
= (smash
)?(DESIRE_SMASH
):(PLAYER(s
, player
).desire
);
681 PLAYER(s
, player
).power
= fmaxf(10.0, fminf(PLAYER(s
, player
).power
*PLAYER(s
, player
).power_up_factor
, PLAYER_POWER_MAX
));
682 PLAYER(s
, player
).use_power
= false;
684 PLAYER(s
, player
).use_power
= true;
688 void input_ai(GameState
* s
, int player
) {
690 int ball_approaching
= 0;
692 if ((PLAYER(s
, player
).x
< GAME_X_MID
&& s
->ball
.move_x
<= 0) ||
693 (PLAYER(s
, player
).x
> GAME_X_MID
&& s
->ball
.move_x
>= 0)) {
694 ball_approaching
= 1;
697 /* FIXME - this is broken since the new physics model has been introduced */
699 if (fabsf(PLAYER(s
, player
).y
- (s
->ball
.y
-s
->ball
.z
)) > RACKET_Y_MID
*5) {
704 if( PLAYER(s
, player
).desire
== DESIRE_NORMAL
&& !IS_NEAR_Y_AI(PLAYER(s
, player
).y
, (s
->ball
.y
-s
->ball
.z
)) && ball_approaching
) {
705 if( PLAYER(s
, player
).y
< (s
->ball
.y
-s
->ball
.z
)) {
706 PLAYER(s
, player
).y
+= fminf(2*fact
, (s
->ball
.y
-s
->ball
.z
) - PLAYER(s
, player
).y
);
707 } else if( PLAYER(s
, player
).y
> (s
->ball
.y
-s
->ball
.z
)) {
708 PLAYER(s
, player
).y
-= fminf(2*fact
, PLAYER(s
, player
).y
- (s
->ball
.y
-s
->ball
.z
));
712 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.) {
713 PLAYER(s
, player
).use_power
= true;
714 } else if (ball_approaching
) {
715 PLAYER(s
, player
).power
= fmaxf(10., fminf(PLAYER(s
, player
).power
*PLAYER(s
, player
).power_up_factor
, PLAYER_POWER_MAX
));
716 PLAYER(s
, player
).use_power
= false;
721 void game_setup_serve( GameState
* s
) {
723 s
->ball
.y
= GAME_Y_MID
;
724 s
->ball
.move_x
= 0.0;
725 s
->ball
.move_y
= 0.0;
726 s
->ball
.move_z
= 0.0;
727 s
->ball
.last_hit_by
= -1;
728 s
->ball
.inhibit_gravity
= true;
730 if( s
->serving_player
== 1) {
731 s
->ball
.x
= GAME_X_MIN
-RACKET_X_MID
*1.5;
733 s
->ball
.x
= GAME_X_MAX
+RACKET_X_MID
*1.5;
737 int score_game(GameState
* s
) {
738 Player
*last_hit_by
= NULL
;
739 Player
*other
= NULL
;
741 Player
* winner
= NULL
;
742 Player
* loser
= NULL
;
744 /* determine "last hit by" and "other" */
745 if (s
->ball
.last_hit_by
== 1) {
746 last_hit_by
= &(PLAYER(s
, 1));
747 other
= &(PLAYER(s
, 2));
749 last_hit_by
= &(PLAYER(s
, 2));
750 other
= &(PLAYER(s
, 1));
753 switch (s
->score_event
) {
754 case SCORE_EVENT_NET
:
757 case SCORE_EVENT_OUT
:
758 case SCORE_EVENT_GROUND_INVALID
:
759 case SCORE_EVENT_OFFSCREEN
:
760 case SCORE_EVENT_GROUND_VALID
:
761 if (s
->ball
.ground_hit
) {
762 winner
= last_hit_by
;
771 /* determine loser based on winner */
772 if (winner
== last_hit_by
) {
778 /* we cannot be in an "impossibly high" set */
779 assert(s
->current_set
< SETS_TO_WIN
*2-1);
783 if( loser
->game
< winner
->game
-1) {
784 if( winner
->game
>= 4) {
785 winner
->game
= loser
->game
= 0;
786 winner
->sets
[s
->current_set
]++;
789 /* serving is changed when the "game" is over */
790 s
->serving_player
= (s
->serving_player
==1)?(2):(1);
792 #ifdef HAVE_VOICE_FILES
793 /* speak the current score */
794 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
);
797 /* scoring the set.. */
798 if( (winner
->sets
[s
->current_set
] == 6 && loser
->sets
[s
->current_set
] < 5) ||
799 winner
->sets
[s
->current_set
] == 7) {
801 s
->winner
= game_get_winner( s
);
806 /* forget this event - we've handled it */
807 s
->score_event
= SCORE_UNDECIDED
;
809 if (winner
== &PLAYER(s
, 1)) {
810 return WINNER_PLAYER1
;
812 return WINNER_PLAYER2
;
816 const char* format_sets(const GameState
* s
) {
817 static char sets
[100];
818 static char tmp
[100];
819 int i
, max
= s
->current_set
;
823 if( s
->winner
!= WINNER_NONE
) {
826 for( i
=0; i
<=max
; i
++) {
827 sprintf( tmp
, "%d:%d, ", PLAYER(s
, 1).sets
[i
], PLAYER(s
, 2).sets
[i
]);
831 sets
[strlen(sets
)-2] = '\0';
836 const char* format_game(const GameState
* s
) {
837 static char game
[100];
838 static const int game_scoring
[] = { 0, 15, 30, 40 };
840 if( PLAYER(s
, 1).game
< 4 && PLAYER(s
, 2).game
< 4) {
841 #ifdef HAVE_VOICE_FILES
842 if (PLAYER(s
, 1).game
> 0 || PLAYER(s
, 2).game
> 0) {
843 if (PLAYER(s
, 1).game
== PLAYER(s
, 2).game
) {
844 voice_say_list(2, VOICE_LOVE_IN
+ 2*(PLAYER(s
, 1).game
), VOICE_ALL
);
846 voice_say_list(2, VOICE_LOVE_IN
+ 2*(PLAYER(s
, 1).game
), VOICE_LOVE_OUT
+ 2*(PLAYER(s
, 2).game
));
850 sprintf( game
, "%d - %d", game_scoring
[PLAYER(s
, 1).game
], game_scoring
[PLAYER(s
, 2).game
]);
851 } else if( PLAYER(s
, 1).game
> PLAYER(s
, 2).game
) {
852 #ifdef HAVE_VOICE_FILES
853 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_ONE
);
855 strcpy( game
, "advantage player 1");
856 } else if( PLAYER(s
, 1).game
< PLAYER(s
, 2).game
) {
857 #ifdef HAVE_VOICE_FILES
858 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_TWO
);
860 strcpy( game
, "advantage player 2");
862 #ifdef HAVE_VOICE_FILES
863 voice_say_list(1, VOICE_DEUCE
);
865 strcpy( game
, "deuce");
871 const char* format_status(const GameState
* s
) {
872 static char status
[100];
873 static const char* set_names
[] = { "first", "second", "third", "fourth", "fifth" };
875 switch (s
->status_message
) {
878 case STATUSMSG_WELCOME
:
879 return "welcome to tennix " VERSION
;
880 case STATUSMSG_DEFAULT
:
881 sprintf(status
, "%d:%d in %s set",
882 PLAYER(s
, 1).sets
[s
->current_set
],
883 PLAYER(s
, 2).sets
[s
->current_set
],
884 set_names
[s
->current_set
]);
890 case STATUSMSG_FAULT
:
892 case STATUSMSG_P1SCORES
:
893 return "player 1 scores.";
894 case STATUSMSG_P2SCORES
:
895 return "player 2 scores.";
896 case STATUSMSG_VOLLEY
:
898 case STATUSMSG_DIDNTCATCH
:
899 return "did not catch the ball.";
905 int game_get_winner( GameState
* s
) {
909 for( i
=0; i
<s
->current_set
; i
++) {
910 if( PLAYER(s
, 1).sets
[i
] > PLAYER(s
, 2).sets
[i
]) {
917 if( sets
[0] == SETS_TO_WIN
) return WINNER_PLAYER1
;
918 if( sets
[1] == SETS_TO_WIN
) return WINNER_PLAYER2
;