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
},
53 SOUND_EVENT_NONE
, /* sound events */
56 EVENTCOUNTER_GAMESTATE_START
,
57 EVENTCOUNTER_GAMESTATE_START
,
61 s
= (GameState
*)malloc(sizeof(GameState
));
62 if (s
== NULL
) abort();
64 memcpy(s
, &template, sizeof(GameState
));
72 int gamestate_save(GameState
* s
, const char* filename
)
74 const Location
*location
;
75 InputDevice
* input_devices
[MAXPLAYERS
];
81 assert(getcwd(tmp
, MAXPATHLEN
) == tmp
);
82 assert(chdir(getenv("HOME")) == 0);
86 * Process-specific data (pointers to data
87 * structures only of interest to this process)
88 * has to be "removed" before saving.
90 * It can later be restored (in gamestate_load).
93 fp
= fopen(filename
, "w");
98 /* Save data for later recovery + clear */
99 location
= s
->location
;
101 for (i
=1; i
<=MAXPLAYERS
; i
++) {
102 input_devices
[i
-1] = PLAYER(s
, i
).input
;
103 PLAYER(s
, i
).input
= NULL
;
106 if (fwrite(s
, sizeof(GameState
), 1, fp
) != 1) {
110 /* Restore process-specific data */
111 s
->location
= location
;
112 for (i
=1; i
<=MAXPLAYERS
; i
++) {
113 PLAYER(s
, i
).input
= input_devices
[i
-1];
119 assert(chdir(tmp
) == 0);
126 GameState
* gamestate_load(const char* filename
)
131 char tmp
[MAXPATHLEN
];
133 assert(getcwd(tmp
, MAXPATHLEN
) == tmp
);
134 assert(chdir(getenv("HOME")) == 0);
137 fp
= fopen(filename
, "r");
140 s
= (GameState
*)malloc(sizeof(GameState
));
142 if (fread(s
, sizeof(GameState
), 1, fp
) == 1) {
143 /* Restore/create process-specific data */
144 /* FIXME: s->location, players' "input" */
154 assert(chdir(tmp
) == 0);
161 void gameloop(GameState
*s
) {
162 Uint32 ot
= SDL_GetTicks();
164 Uint32 dt
= GAME_TICKS
;
166 Uint32 accumulator
= 0;
172 EVENTCOUNTER_RENDERSTATE_START
,
173 EVENTCOUNTER_RENDERSTATE_START
,
180 /* Catch-up with existing sound events */
181 r
.sound_events
= s
->sound_events
;
183 #ifdef ENABLE_FPS_LIMIT
184 Uint32 ft
, frames
; /* frame timer and frames */
187 if (s
->location
->rain
> 0) {
188 play_sample_background(SOUND_RAIN
);
190 if (s
->location
->max_visitors
> 100) {
191 play_sample_loop(SOUND_AUDIENCE
);
194 for (p
=1; p
<=MAXPLAYERS
; p
++) {
195 input_device_join_game(PLAYER(s
, p
).input
, s
, p
);
198 #ifdef ENABLE_FPS_LIMIT
212 while( accumulator
>= dt
) {
214 quit
= handle_input(s
);
218 #ifdef ENABLE_FPS_LIMIT
219 while (frames
*1000.0/((float)(SDL_GetTicks()-ft
+1))>(float)(DEFAULT_FPS
)) {
228 for (p
=1; p
<=MAXPLAYERS
; p
++) {
229 input_device_part_game(PLAYER(s
, p
).input
);
233 rectangle(0, 0, WIDTH
, HEIGHT
, 80, 80, 80);
236 stop_sample(SOUND_AUDIENCE
);
237 stop_sample(SOUND_RAIN
);
240 void step(GameState
* s
) {
241 bool ground_event
= false;
244 s
->ball
.z
+= s
->ball
.move_z
;
245 if (!s
->ball
.inhibit_gravity
) {
246 s
->ball
.move_z
+= GRAVITY
;
249 s
->ball
.x
+= s
->ball
.move_x
;
250 s
->ball
.y
+= s
->ball
.move_y
;
252 for (p
=1; p
<=MAXPLAYERS
; p
++) {
253 if (PLAYER(s
, p
).use_power
) {
254 PLAYER(s
, p
).power
= fmaxf(0.0, fminf(
255 PLAYER(s
, p
).power
*PLAYER(s
, p
).power_down_factor
,
264 s
->referee
= REFEREE_NORMAL
;
266 /* bounce from the ground */
267 if (fabsf(s
->ball
.move_z
) > 0.3) {
268 s
->sound_events
^= SOUND_EVENT_GROUND
;
269 s
->ball
.move_z
*= -s
->ball
.restitution
;
276 if (NET_COLLISION_BALL(s
->ball
)) {
277 /* the net blocks movement of the ball */
278 while (NET_COLLISION_BALL(s
->ball
)) {
279 /* make sure the ball appears OUTSIDE of the net */
280 if (s
->ball
.move_x
< 0) {
290 /* see if we have something to score */
291 if (s
->score_event
== SCORE_UNDECIDED
) {
292 if (NET_COLLISION_BALL(s
->ball
)) {
293 /* the ball "fell" into the net */
294 s
->score_event
= SCORE_EVENT_NET
;
295 s
->sound_events
^= SOUND_EVENT_OUT
;
296 s
->status_message
= STATUSMSG_NET
;
297 } else if (IS_OFFSCREEN(s
->ball
.x
, s
->ball
.y
)) {
298 /* ball flew offscreen */
299 s
->score_event
= SCORE_EVENT_OFFSCREEN
;
300 s
->sound_events
^= SOUND_EVENT_OUT
;
301 s
->status_message
= STATUSMSG_OUT
;
302 } else if (ground_event
) {
303 /* the ball hit the ground on the screen */
304 if (IS_OUT(s
->ball
.x
, s
->ball
.y
)) {
305 /* the ball bounced in the OUT area */
306 s
->score_event
= SCORE_EVENT_OUT
;
307 s
->sound_events
^= SOUND_EVENT_OUT
;
308 s
->status_message
= STATUSMSG_OUT
;
309 } else if (GROUND_IS_VALID(s
->ball
.last_hit_by
, s
->ball
.x
, s
->ball
.y
)) {
310 if (s
->ball
.ground_hit
) {
311 s
->score_event
= SCORE_EVENT_GROUND_VALID
;
312 s
->sound_events
^= SOUND_EVENT_OUT
;
313 s
->status_message
= STATUSMSG_DIDNTCATCH
;
315 /* first ground hit in valid area */
316 s
->ball
.ground_hit
= true;
319 /* ball hit somewhere invalid */
320 s
->score_event
= SCORE_EVENT_GROUND_INVALID
;
321 s
->sound_events
^= SOUND_EVENT_OUT
;
322 s
->status_message
= STATUSMSG_FAULT
;
327 if (s
->score_event
!= SCORE_UNDECIDED
) {
328 /* we have some scoring to do */
329 if (s
->score_time
< SCORING_DELAY
/GAME_TICKS
) {
330 /* schedule scoring in the future */
332 s
->referee
= REFEREE_OUT
;
333 } else if (s
->score_time
>= SCORING_DELAY
/GAME_TICKS
) {
334 /* time has ran out - score now */
335 switch (score_game(s
)) {
337 s
->status_message
= STATUSMSG_P1SCORES
;
338 s
->referee
= REFEREE_PLAYER1
;
341 s
->status_message
= STATUSMSG_P2SCORES
;
342 s
->referee
= REFEREE_PLAYER2
;
351 if (s
->location
->max_visitors
> 100) {
352 s
->sound_events
^= SOUND_EVENT_APPLAUSE
;
356 /* score is still undecided - do the racket swing thing */
357 for (p
=1; p
<=2; p
++) {
358 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
) {
360 if (!s
->ball
.ground_hit
&& s
->ball
.move_x
!= 0.0) {
361 s
->status_message
= STATUSMSG_VOLLEY
;
363 s
->status_message
= STATUSMSG_DEFAULT
;
365 switch (PLAYER(s
, p
).desire
) {
368 s
->ball
.move_x
= 2.7 + 2.0*PLAYER(s
, p
).power
/PLAYER_POWER_MAX
;
369 s
->ball
.move_z
= 1.2*PLAYER(s
, p
).power
/PLAYER_POWER_MAX
;
373 s
->ball
.move_x
= 1.1 + 2.2*PLAYER(s
, p
).power
/PLAYER_POWER_MAX
;
374 s
->ball
.move_z
= 2.5*PLAYER(s
, p
).power
/PLAYER_POWER_MAX
;
378 s
->ball
.move_x
= 4.0 + 3.0*PLAYER(s
, p
).power
/PLAYER_POWER_MAX
;
379 s
->ball
.move_z
= 1.1*PLAYER(s
, p
).power
/PLAYER_POWER_MAX
;
382 s
->ball
.move_y
= get_move_y( s
, p
);
383 s
->sound_events
^= SOUND_EVENT_RACKET
;
384 s
->ball
.ground_hit
= false;
385 s
->ball
.inhibit_gravity
= false;
386 s
->ball
.last_hit_by
= p
;
388 s
->ball
.move_x
*= -1;
395 bool handle_input(GameState
* s
) {
396 static GameState tmp
;
401 keys
= SDL_GetKeyState(NULL
);
404 memcpy(&tmp
, s
, sizeof(GameState
));
405 } else if (keys
['2']) {
406 memcpy(s
, &tmp
, sizeof(GameState
));
409 if (s
->winner
== WINNER_NONE
) {
410 for (p
=1; p
<=MAXPLAYERS
; p
++) {
411 if( PLAYER(s
, p
).type
== PLAYER_TYPE_HUMAN
) {
417 /* Make sure player coordinates are valid */
418 if (PLAYER(s
, p
).y
< PLAYER_Y_MIN
) {
419 PLAYER(s
, p
).y
= PLAYER_Y_MIN
;
420 } else if (PLAYER(s
, p
).y
> PLAYER_Y_MAX
) {
421 PLAYER(s
, p
).y
= PLAYER_Y_MAX
;
426 /* Maemo: The "F4" button is the "Open menu" button */
427 return (keys
[SDLK_ESCAPE
] || keys
['q']);
430 void render(const GameState
* s
, RenderState
* r
) {
437 Uint32 time
= SDL_GetTicks();
439 /* The bits in sound_events flip when the sound should play */
440 if ((sounds
= (r
->sound_events
^ s
->sound_events
)) != 0) {
441 if (sounds
& SOUND_EVENT_GROUND
) {
442 sample_volume_group(SOUND_GROUND_FIRST
, SOUND_GROUND_LAST
,
443 fmaxf(0.0, fminf(1.0, fabsf(s
->ball
.move_z
)/2)));
444 pan_sample_group(SOUND_GROUND_FIRST
, SOUND_GROUND_LAST
,
445 fmaxf(0.0, fminf(1.0, (s
->ball
.x
)/WIDTH
)));
446 play_sample(SOUND_GROUND
);
448 if (sounds
& SOUND_EVENT_OUT
) {
449 play_sample(SOUND_OUT
);
451 if (sounds
& SOUND_EVENT_APPLAUSE
) {
452 play_sample(SOUND_APPLAUSE
);
454 if (sounds
& SOUND_EVENT_RACKET
) {
455 if (((s
->ball
.x
)/WIDTH
) < 0.5) {
456 pan_sample_group(SOUND_RACKET_FIRST
, SOUND_RACKET_LAST
, 0.3);
458 pan_sample_group(SOUND_RACKET_FIRST
, SOUND_RACKET_LAST
, 0.7);
460 play_sample(SOUND_RACKET
);
462 r
->sound_events
= s
->sound_events
;
465 if( s
->winner
!= WINNER_NONE
) {
467 rectangle(0, 0, WIDTH
, HEIGHT
, 80, 80, 80);
469 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);
470 /*sprintf( s->game_score_str, "player %d wins the match with %s", s->winner, format_sets( s));
471 font_draw_string(FONT_LARGE, s->game_score_str, (WIDTH-font_get_string_width(FONT_LARGE, s->game_score_str))/2, HEIGHT/2 + 30);*/
475 if ((r
->referee
!= s
->referee
) ||
476 (r
->ec_game
!= s
->ec_game
) ||
477 (r
->ec_sets
!= s
->ec_sets
) ||
478 (r
->status_message
!= s
->status_message
)) {
479 /* Update status message text */
480 if (r
->status_message
!= s
->status_message
) {
481 r
->text_status
= format_status(s
);
482 r
->status_message
= s
->status_message
;
484 /* Update game status text */
485 if (r
->ec_game
!= s
->ec_game
) {
486 r
->text_game
= format_game(s
);
487 r
->ec_game
= s
->ec_game
;
489 /* Update set status text */
490 if (r
->ec_sets
!= s
->ec_sets
) {
491 r
->text_sets
= format_sets(s
);
492 if (r
->status_message
== STATUSMSG_DEFAULT
) {
493 r
->text_status
= format_status(s
);
495 r
->ec_sets
= s
->ec_sets
;
498 rectangle(0, 0, WIDTH
, HEIGHT
, 80, 80, 80);
499 fill_image(s
->location
->court_type
, 120, 120, 400, 250);
500 show_image(GR_COURT
, 0, 0, 255);
501 font_draw_string(FONT_XLARGE
, r
->text_game
, 14, 14);
502 font_draw_string(FONT_XLARGE
, r
->text_sets
,
503 (WIDTH
-font_get_string_width(FONT_XLARGE
, r
->text_sets
))-14, 14);
504 if (s
->location
->has_referee
) {
505 switch (s
->referee
) {
512 case REFEREE_PLAYER1
:
513 case REFEREE_PLAYER2
:
532 show_sprite( GR_REFEREE
, s
->referee
*3+t
, 12, 250, 10, 255);
533 if (voice_finished_flag
== 0) {
534 show_sprite(GR_TALK
, (time
/150)%2, 2, 280, 45, 255);
537 r
->referee
= s
->referee
;
541 /* show_sprite( GR_RACKET, (!PLAYER(s, 1).state), 4, PLAYER(s, 1).x-RACKET_X_MID, PLAYER(s, 1).y-RACKET_Y_MID, 255);
542 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);*/
543 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);
544 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);
546 rectangle(10, HEIGHT
-30, PLAYER_POWER_MAX
, 10, 50, 50, 50);
547 rectangle(WIDTH
-PLAYER_POWER_MAX
-10, HEIGHT
-30, PLAYER_POWER_MAX
, 10, 50, 50, 50);
549 rectangle(10, HEIGHT
-30, (int)(PLAYER(s
, 1).power
), 10, 200, 200, 200);
550 rectangle(WIDTH
-PLAYER_POWER_MAX
-10, HEIGHT
-30, (int)(PLAYER(s
, 2).power
), 10, 200, 200, 200);
552 if( s
->ball
.move_x
> 0) {
553 b
= (time
/100)%BALL_STATES
;
554 } else if( s
->ball
.move_x
< 0) {
555 b
= BALL_STATES
-1-(time
/100)%BALL_STATES
;
561 zoom
= fmaxf(0.5, fminf(1.0, (float)(30.-(s
->ball
.z
))/10.));
562 show_image_rotozoom(GR_SHADOW
, s
->ball
.x
, s
->ball
.y
+3, rotate
, zoom
);
564 rotate
= (-s
->ball
.move_x
* (float)((float)time
/10.));
565 zoom
= 1.0 + fmaxf(0.0, (s
->ball
.z
-10.)/100.);
566 show_image_rotozoom(GR_BALL
, s
->ball
.x
, s
->ball
.y
- s
->ball
.z
, rotate
, zoom
);
568 /* Player 1's mouse rectangle */
569 if (PLAYER(s
, 1).input
!= NULL
&& PLAYER(s
, 1).input
->type
== INPUT_TYPE_MOUSE
) {
570 rectangle(PLAYER(s
, 1).x
-2+5, PLAYER(s
, 1).input
->my
-2, 4, 4, 255, 255, 255);
571 rectangle(PLAYER(s
, 1).x
-1+5, PLAYER(s
, 1).input
->my
-1, 2, 2, 0, 0, 0);
574 /* Player 2's mouse rectangle */
575 if (PLAYER(s
, 2).input
!= NULL
&& PLAYER(s
, 2).input
->type
== INPUT_TYPE_MOUSE
) {
576 rectangle(PLAYER(s
, 2).x
-2+5, PLAYER(s
, 2).input
->my
-2, 4, 4, 255, 255, 255);
577 rectangle(PLAYER(s
, 2).x
-1+5, PLAYER(s
, 2).input
->my
-1, 2, 2, 0, 0, 0);
580 font_draw_string(FONT_MEDIUM
, r
->text_status
,
581 (WIDTH
-font_get_string_width(FONT_MEDIUM
, r
->text_status
))/2, HEIGHT
-50);
583 for (i
=0; i
<s
->location
->rain
; i
++) {
586 draw_line_faded(x
, y
, x
+10, y
+30, 0, 0, 255, 100, 200, 255);
588 if (s
->location
->rain
) {
590 * Cheap-ish update of the whole screen. This can
591 * probably be optimized.
593 update_rect(0, 0, WIDTH
, HEIGHT
);
597 line_horiz( PLAYER(s
, 1).y
, 255, 0, 0);
598 line_horiz( PLAYER(s
, 2).y
, 0, 0, 255);
599 line_horiz( s
->ball
.y
, 0, 255, 0);
601 line_vert( PLAYER(s
, 1).x
, 255, 0, 0);
602 line_vert( PLAYER(s
, 2).x
, 0, 0, 255);
603 line_vert( s
->ball
.x
, 0, 255, 0);
605 line_horiz( GAME_Y_MIN
, 100, 100, 100);
606 line_horiz( GAME_Y_MAX
, 100, 100, 100);
608 switch (s
->location
->fog
) {
611 fill_image_offset(GR_FOG2
, 0, 0, WIDTH
, HEIGHT
, -time
/150, 0);
613 fill_image_offset(GR_FOG
, 0, 0, WIDTH
, HEIGHT
, -time
/100, 20);
615 fill_image_offset(GR_FOG2
, 0, 0, WIDTH
, HEIGHT
, -time
/180, 80);
617 fill_image_offset(GR_FOG
, 0, 0, WIDTH
, HEIGHT
, time
/200, 0);
621 if (s
->location
->night
) {
622 show_image(GR_NIGHT
, 0, 0, 255);
628 float get_move_y( GameState
* s
, unsigned char player
) {
629 float pct
, dest
, x_len
, y_len
;
630 float py
, by
, pa
, move_x
;
632 py
= (player
==1)?(PLAYER(s
, 1).y
):(PLAYER(s
, 2).y
);
633 by
= s
->ball
.y
- s
->ball
.z
;
635 move_x
= s
->ball
.move_x
;
637 /* -1.0 .. 1.0 for racket hit position */
638 pct
= fmaxf(-1.0, fminf(1.0, (by
-py
)/(pa
/2)));
640 /* Y destination for ball */
641 dest
= GAME_Y_MID
+ pct
*(GAME_Y_MAX
-GAME_Y_MIN
);
643 /* lengths for the ball's journey */
645 x_len
= GAME_X_MAX
- s
->ball
.x
;
647 x_len
= s
->ball
.x
- GAME_X_MIN
;
651 /* return the should-be value for move_y */
652 return (y_len
*move_x
)/(x_len
);
655 void input_human(GameState
* s
, int player
) {
656 bool hit
, topspin
, smash
;
659 /* For mouse input, hand the player coordinates to the InputDevice */
660 if (PLAYER(s
, player
).input
->type
== INPUT_TYPE_MOUSE
) {
661 PLAYER(s
, player
).input
->player_x
= (int)(PLAYER(s
, player
).x
);
662 PLAYER(s
, player
).input
->player_y
= (int)(PLAYER(s
, player
).y
);
665 move_y
= PLAYER_MOVE_Y
*input_device_get_axis(PLAYER(s
, player
).input
, INPUT_AXIS_Y
);
667 hit
= input_device_get_key(PLAYER(s
, player
).input
, INPUT_KEY_HIT
);
668 topspin
= input_device_get_key(PLAYER(s
, player
).input
, INPUT_KEY_TOPSPIN
);
669 smash
= input_device_get_key(PLAYER(s
, player
).input
, INPUT_KEY_SMASH
);
672 if (fabsf(move_y
) > fabsf(move_y
*PLAYER(s
, player
).accelerate
)) {
673 move_y
*= PLAYER(s
, player
).accelerate
;
675 PLAYER(s
, player
).y
+= move_y
;
676 PLAYER(s
, player
).accelerate
*= PLAYER_ACCEL_INCREASE
;
678 PLAYER(s
, player
).accelerate
= PLAYER_ACCEL_DEFAULT
;
681 if(hit
|| topspin
|| smash
) {
682 PLAYER(s
, player
).desire
= (topspin
)?(DESIRE_TOPSPIN
):(DESIRE_NORMAL
);
683 PLAYER(s
, player
).desire
= (smash
)?(DESIRE_SMASH
):(PLAYER(s
, player
).desire
);
685 PLAYER(s
, player
).power
= fmaxf(10.0, fminf(PLAYER(s
, player
).power
*PLAYER(s
, player
).power_up_factor
, PLAYER_POWER_MAX
));
686 PLAYER(s
, player
).use_power
= false;
688 PLAYER(s
, player
).use_power
= true;
692 void input_ai(GameState
* s
, int player
) {
694 int ball_approaching
= 0;
696 if ((PLAYER(s
, player
).x
< GAME_X_MID
&& s
->ball
.move_x
<= 0) ||
697 (PLAYER(s
, player
).x
> GAME_X_MID
&& s
->ball
.move_x
>= 0)) {
698 ball_approaching
= 1;
701 /* FIXME - this is broken since the new physics model has been introduced */
703 if (fabsf(PLAYER(s
, player
).y
- (s
->ball
.y
-s
->ball
.z
)) > RACKET_Y_MID
*5) {
708 if( PLAYER(s
, player
).desire
== DESIRE_NORMAL
&& !IS_NEAR_Y_AI(PLAYER(s
, player
).y
, (s
->ball
.y
-s
->ball
.z
)) && ball_approaching
) {
709 if( PLAYER(s
, player
).y
< (s
->ball
.y
-s
->ball
.z
)) {
710 PLAYER(s
, player
).y
+= fminf(2*fact
, (s
->ball
.y
-s
->ball
.z
) - PLAYER(s
, player
).y
);
711 } else if( PLAYER(s
, player
).y
> (s
->ball
.y
-s
->ball
.z
)) {
712 PLAYER(s
, player
).y
-= fminf(2*fact
, PLAYER(s
, player
).y
- (s
->ball
.y
-s
->ball
.z
));
716 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.) {
717 PLAYER(s
, player
).use_power
= true;
718 } else if (ball_approaching
) {
719 PLAYER(s
, player
).power
= fmaxf(10., fminf(PLAYER(s
, player
).power
*PLAYER(s
, player
).power_up_factor
, PLAYER_POWER_MAX
));
720 PLAYER(s
, player
).use_power
= false;
725 void game_setup_serve( GameState
* s
) {
727 s
->ball
.y
= GAME_Y_MID
;
728 s
->ball
.move_x
= 0.0;
729 s
->ball
.move_y
= 0.0;
730 s
->ball
.move_z
= 0.0;
731 s
->ball
.last_hit_by
= -1;
732 s
->ball
.inhibit_gravity
= true;
734 if( s
->serving_player
== 1) {
735 s
->ball
.x
= GAME_X_MIN
-RACKET_X_MID
*1.5;
737 s
->ball
.x
= GAME_X_MAX
+RACKET_X_MID
*1.5;
741 int score_game(GameState
* s
) {
742 Player
*last_hit_by
= NULL
;
743 Player
*other
= NULL
;
745 Player
* winner
= NULL
;
746 Player
* loser
= NULL
;
748 /* determine "last hit by" and "other" */
749 if (s
->ball
.last_hit_by
== 1) {
750 last_hit_by
= &(PLAYER(s
, 1));
751 other
= &(PLAYER(s
, 2));
753 last_hit_by
= &(PLAYER(s
, 2));
754 other
= &(PLAYER(s
, 1));
757 switch (s
->score_event
) {
758 case SCORE_EVENT_NET
:
761 case SCORE_EVENT_OUT
:
762 case SCORE_EVENT_GROUND_INVALID
:
763 case SCORE_EVENT_OFFSCREEN
:
764 case SCORE_EVENT_GROUND_VALID
:
765 if (s
->ball
.ground_hit
) {
766 winner
= last_hit_by
;
775 /* determine loser based on winner */
776 if (winner
== last_hit_by
) {
782 /* we cannot be in an "impossibly high" set */
783 assert(s
->current_set
< SETS_TO_WIN
*2-1);
787 if( loser
->game
< winner
->game
-1) {
788 if( winner
->game
>= 4) {
789 winner
->game
= loser
->game
= 0;
790 winner
->sets
[s
->current_set
]++;
793 /* serving is changed when the "game" is over */
794 s
->serving_player
= (s
->serving_player
==1)?(2):(1);
796 #ifdef HAVE_VOICE_FILES
797 /* speak the current score */
798 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
);
801 /* scoring the set.. */
802 if( (winner
->sets
[s
->current_set
] == 6 && loser
->sets
[s
->current_set
] < 5) ||
803 winner
->sets
[s
->current_set
] == 7) {
805 s
->winner
= game_get_winner( s
);
810 /* forget this event - we've handled it */
811 s
->score_event
= SCORE_UNDECIDED
;
813 if (winner
== &PLAYER(s
, 1)) {
814 return WINNER_PLAYER1
;
816 return WINNER_PLAYER2
;
820 const char* format_sets(const GameState
* s
) {
821 static char sets
[100];
822 static char tmp
[100];
823 int i
, max
= s
->current_set
;
827 if( s
->winner
!= WINNER_NONE
) {
830 for( i
=0; i
<=max
; i
++) {
831 sprintf( tmp
, "%d:%d, ", PLAYER(s
, 1).sets
[i
], PLAYER(s
, 2).sets
[i
]);
835 sets
[strlen(sets
)-2] = '\0';
840 const char* format_game(const GameState
* s
) {
841 static char game
[100];
842 static const int game_scoring
[] = { 0, 15, 30, 40 };
844 if( PLAYER(s
, 1).game
< 4 && PLAYER(s
, 2).game
< 4) {
845 #ifdef HAVE_VOICE_FILES
846 if (PLAYER(s
, 1).game
> 0 || PLAYER(s
, 2).game
> 0) {
847 if (PLAYER(s
, 1).game
== PLAYER(s
, 2).game
) {
848 voice_say_list(2, VOICE_LOVE_IN
+ 2*(PLAYER(s
, 1).game
), VOICE_ALL
);
850 voice_say_list(2, VOICE_LOVE_IN
+ 2*(PLAYER(s
, 1).game
), VOICE_LOVE_OUT
+ 2*(PLAYER(s
, 2).game
));
854 sprintf( game
, "%d - %d", game_scoring
[PLAYER(s
, 1).game
], game_scoring
[PLAYER(s
, 2).game
]);
855 } else if( PLAYER(s
, 1).game
> PLAYER(s
, 2).game
) {
856 #ifdef HAVE_VOICE_FILES
857 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_ONE
);
859 strcpy( game
, "advantage player 1");
860 } else if( PLAYER(s
, 1).game
< PLAYER(s
, 2).game
) {
861 #ifdef HAVE_VOICE_FILES
862 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_TWO
);
864 strcpy( game
, "advantage player 2");
866 #ifdef HAVE_VOICE_FILES
867 voice_say_list(1, VOICE_DEUCE
);
869 strcpy( game
, "deuce");
875 const char* format_status(const GameState
* s
) {
876 static char status
[100];
877 static const char* set_names
[] = { "first", "second", "third", "fourth", "fifth" };
879 switch (s
->status_message
) {
882 case STATUSMSG_WELCOME
:
883 return "welcome to tennix " VERSION
;
884 case STATUSMSG_DEFAULT
:
885 sprintf(status
, "%d:%d in %s set",
886 PLAYER(s
, 1).sets
[s
->current_set
],
887 PLAYER(s
, 2).sets
[s
->current_set
],
888 set_names
[s
->current_set
]);
894 case STATUSMSG_FAULT
:
896 case STATUSMSG_P1SCORES
:
897 return "player 1 scores.";
898 case STATUSMSG_P2SCORES
:
899 return "player 2 scores.";
900 case STATUSMSG_VOLLEY
:
902 case STATUSMSG_DIDNTCATCH
:
903 return "did not catch the ball.";
909 winner_t
game_get_winner(const GameState
* s
) {
913 for( i
=0; i
<s
->current_set
; i
++) {
914 if( PLAYER(s
, 1).sets
[i
] > PLAYER(s
, 2).sets
[i
]) {
921 if( sets
[0] == SETS_TO_WIN
) return WINNER_PLAYER1
;
922 if( sets
[1] == SETS_TO_WIN
) return WINNER_PLAYER2
;