Sound volume + panning moved into rendering function
[tennix.git] / game.c
blob8be01575ae47ba325ec568a829cb000eba0894d9
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 GameState *s;
41 GameState template = {
42 NULL,
43 -1,
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 },
51 REFEREE_NORMAL,
53 WINNER_NONE,
54 SOUND_EVENT_NONE, /* sound events */
55 SCORE_UNDECIDED,
57 EVENTCOUNTER_GAMESTATE_START,
58 EVENTCOUNTER_GAMESTATE_START,
59 STATUSMSG_WELCOME,
62 s = (GameState*)malloc(sizeof(GameState));
63 if (s == NULL) abort();
65 memcpy(s, &template, sizeof(GameState));
67 game_setup_serve(s);
69 return s;
73 int gamestate_save(GameState* s, const char* filename)
75 Location *location;
76 InputDevice* input_devices[MAXPLAYERS];
77 int i, result = 0;
78 FILE* fp = NULL;
79 #ifndef WIN32
80 char tmp[MAXPATHLEN];
82 assert(getcwd(tmp, MAXPATHLEN) == tmp);
83 assert(chdir(getenv("HOME")) == 0);
84 #endif
86 /**
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).
92 **/
94 fp = fopen(filename, "w");
95 if (fp == NULL) {
96 return -1;
99 /* Save data for later recovery + clear */
100 location = s->location;
101 s->location = NULL;
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) {
108 result = -2;
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];
117 fclose(fp);
119 #ifndef WIN32
120 assert(chdir(tmp) == 0);
121 #endif
123 return result;
127 GameState* gamestate_load(const char* filename)
129 FILE* fp;
130 GameState* s = NULL;
131 #ifndef WIN32
132 char tmp[MAXPATHLEN];
134 assert(getcwd(tmp, MAXPATHLEN) == tmp);
135 assert(chdir(getenv("HOME")) == 0);
136 #endif
138 fp = fopen(filename, "r");
140 if (fp != NULL) {
141 s = (GameState*)malloc(sizeof(GameState));
142 if (s != NULL) {
143 if (fread(s, sizeof(GameState), 1, fp) == 1) {
144 /* Restore/create process-specific data */
145 /* FIXME: s->location, players' "input" */
146 } else {
147 free(s);
148 s = NULL;
151 fclose(fp);
154 #ifndef WIN32
155 assert(chdir(tmp) == 0);
156 #endif
158 return s;
162 void gameloop(GameState *s) {
163 Uint32 ot = SDL_GetTicks();
164 Uint32 nt;
165 Uint32 dt = GAME_TICKS;
166 Uint32 diff;
167 Uint32 accumulator = 0;
168 bool quit = false;
169 int p;
170 RenderState r = {
171 SOUND_EVENT_NONE,
172 REFEREE_COUNT,
173 EVENTCOUNTER_RENDERSTATE_START,
174 EVENTCOUNTER_RENDERSTATE_START,
175 STATUSMSG_NONE,
176 NULL,
177 NULL,
178 NULL,
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 */
186 #endif
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
200 frames = 0;
201 ft = SDL_GetTicks();
202 #endif
203 while( !quit) {
204 nt = SDL_GetTicks();
205 diff = nt-ot;
206 if( diff > 2000) {
207 diff = 0;
210 accumulator += diff;
211 ot = nt;
213 while( accumulator >= dt) {
214 quit = step(s);
215 s->time += dt;
216 accumulator -= dt;
219 #ifdef ENABLE_FPS_LIMIT
220 while (frames*1000.0/((float)(SDL_GetTicks()-ft+1))>(float)(DEFAULT_FPS)) {
221 SDL_Delay(10);
223 frames++;
224 #endif
226 render(s, &r);
229 for (p=1; p<=MAXPLAYERS; p++) {
230 input_device_part_game(PLAYER(s, p).input);
233 clear_screen();
234 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
235 store_screen();
237 stop_sample(SOUND_AUDIENCE);
238 stop_sample(SOUND_RAIN);
241 bool step( GameState* s) {
242 Uint8 *keys;
243 bool ground_event = false;
244 int p;
246 if (s->ball.z < 0) {
247 /* ground event */
248 ground_event = true;
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 s->ball.move_z *= -s->ball.restitution;
256 } else {
257 s->ball.move_z = 0;
259 s->ball.z = 0;
262 if (NET_COLLISION_BALL(s->ball)) {
263 /* the net blocks movement of the ball */
264 while (NET_COLLISION_BALL(s->ball)) {
265 /* make sure the ball appears OUTSIDE of the net */
266 if (s->ball.move_x < 0) {
267 s->ball.x += 1;
268 } else {
269 s->ball.x -= 1;
272 s->ball.move_x = 0;
273 s->ball.move_y = 0;
276 /* see if we have something to score */
277 if (s->score_event == SCORE_UNDECIDED) {
278 if (NET_COLLISION_BALL(s->ball)) {
279 /* the ball "fell" into the net */
280 s->score_event = SCORE_EVENT_NET;
281 s->sound_events ^= SOUND_EVENT_OUT;
282 s->status_message = STATUSMSG_NET;
283 } else if (IS_OFFSCREEN(s->ball.x, s->ball.y)) {
284 /* ball flew offscreen */
285 s->score_event = SCORE_EVENT_OFFSCREEN;
286 s->sound_events ^= SOUND_EVENT_OUT;
287 s->status_message = STATUSMSG_OUT;
288 } else if (ground_event) {
289 /* the ball hit the ground on the screen */
290 if (IS_OUT(s->ball.x, s->ball.y)) {
291 /* the ball bounced in the OUT area */
292 s->score_event = SCORE_EVENT_OUT;
293 s->sound_events ^= SOUND_EVENT_OUT;
294 s->status_message = STATUSMSG_OUT;
295 } else if (GROUND_IS_VALID(s->ball.last_hit_by, s->ball.x, s->ball.y)) {
296 if (s->ball.ground_hit) {
297 s->score_event = SCORE_EVENT_GROUND_VALID;
298 s->sound_events ^= SOUND_EVENT_OUT;
299 s->status_message = STATUSMSG_DIDNTCATCH;
300 } else {
301 /* first ground hit in valid area */
302 s->ball.ground_hit = true;
304 } else {
305 /* ball hit somewhere invalid */
306 s->score_event = SCORE_EVENT_GROUND_INVALID;
307 s->sound_events ^= SOUND_EVENT_OUT;
308 s->status_message = STATUSMSG_FAULT;
313 if (s->score_event != SCORE_UNDECIDED) {
314 /* we have some scoring to do */
315 if (s->score_time == 0) {
316 /* schedule scoring in the future */
317 s->score_time = s->time + SCORING_DELAY;
318 s->referee = REFEREE_OUT;
319 } else if (s->time >= s->score_time) {
320 /* time has ran out - score now */
321 switch (score_game(s)) {
322 case WINNER_PLAYER1:
323 s->status_message = STATUSMSG_P1SCORES;
324 s->referee = REFEREE_PLAYER1;
325 break;
326 case WINNER_PLAYER2:
327 s->status_message = STATUSMSG_P2SCORES;
328 s->referee = REFEREE_PLAYER2;
329 break;
330 default:
331 assert(0);
332 break;
334 s->score_time = 0;
336 game_setup_serve(s);
337 if (s->location->max_visitors > 100) {
338 s->sound_events ^= SOUND_EVENT_APPLAUSE;
341 } else {
342 /* score is still undecided - do the racket swing thing */
343 for (p=1; p<=2; p++) {
344 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) {
345 /* RACKET HIT */
346 if (!s->ball.ground_hit && s->ball.move_x != 0.0) {
347 s->status_message = STATUSMSG_VOLLEY;
348 } else {
349 s->status_message = STATUSMSG_DEFAULT;
351 switch (PLAYER(s, p).desire) {
352 case DESIRE_NORMAL:
353 /* normal swing */
354 s->ball.move_x = 2.7 + 2.0*PLAYER(s, p).power/PLAYER_POWER_MAX;
355 s->ball.move_z = 1.2*PLAYER(s, p).power/PLAYER_POWER_MAX;
356 break;
357 case DESIRE_TOPSPIN:
358 /* top spin */
359 s->ball.move_x = 1.1 + 2.2*PLAYER(s, p).power/PLAYER_POWER_MAX;
360 s->ball.move_z = 2.5*PLAYER(s, p).power/PLAYER_POWER_MAX;
361 break;
362 case DESIRE_SMASH:
363 /* smash */
364 s->ball.move_x = 4.0 + 3.0*PLAYER(s, p).power/PLAYER_POWER_MAX;
365 s->ball.move_z = 1.1*PLAYER(s, p).power/PLAYER_POWER_MAX;
366 break;
368 s->ball.move_y = get_move_y( s, p);
369 s->sound_events ^= SOUND_EVENT_RACKET;
370 s->ball.ground_hit = false;
371 s->ball.inhibit_gravity = false;
372 s->ball.last_hit_by = p;
373 if (p==2) {
374 s->ball.move_x *= -1;
380 SDL_PumpEvents();
381 keys = SDL_GetKeyState(NULL);
383 if(s->winner == WINNER_NONE) {
384 for (p=1; p<=MAXPLAYERS; p++) {
385 if( PLAYER(s, p).type == PLAYER_TYPE_HUMAN) {
386 input_human(s, p);
387 } else {
388 input_ai(s, p);
393 /* Maemo: The "F6" button is the "Fullscreen" button */
394 /*if( keys['f'] || keys[SDLK_F6]) SDL_WM_ToggleFullScreen( screen);*/
395 if( keys['y']) SDL_SaveBMP( screen, "screenshot.bmp");
397 /* Maemo: The "F4" button is the "Open menu" button */
398 if(keys[SDLK_ESCAPE] || keys['q']
399 || keys['p'] || keys[SDLK_F4]) {
400 return true;
403 limit_value( &PLAYER(s, 1).y, PLAYER_Y_MIN, PLAYER_Y_MAX);
404 limit_value( &PLAYER(s, 2).y, PLAYER_Y_MIN, PLAYER_Y_MAX);
406 s->ball.z += s->ball.move_z;
407 if (!s->ball.inhibit_gravity) {
408 s->ball.move_z += GRAVITY;
411 s->ball.x += s->ball.move_x;
412 s->ball.y += s->ball.move_y;
414 for (p=1; p<=MAXPLAYERS; p++) {
415 if (PLAYER(s, p).use_power) {
416 PLAYER(s, p).power = fmaxf(0.0, fminf(PLAYER(s, p).power*PLAYER(s, p).power_down_factor, PLAYER_POWER_MAX));
420 return false;
423 void render(const GameState* s, RenderState* r) {
424 int x, y, b;
425 unsigned int i;
426 float zoom;
427 float rotate;
428 int t=1000;
429 soundevent_t sounds;
431 /* The bits in sound_events flip when the sound should play */
432 if ((sounds = (r->sound_events ^ s->sound_events)) != 0) {
433 if (sounds & SOUND_EVENT_GROUND) {
434 sample_volume_group(SOUND_GROUND_FIRST, SOUND_GROUND_LAST,
435 fmaxf(0.0, fminf(1.0, fabsf(s->ball.move_z)/2)));
436 pan_sample_group(SOUND_GROUND_FIRST, SOUND_GROUND_LAST,
437 fmaxf(0.0, fminf(1.0, (s->ball.x)/WIDTH)));
438 play_sample(SOUND_GROUND);
440 if (sounds & SOUND_EVENT_OUT) {
441 play_sample(SOUND_OUT);
443 if (sounds & SOUND_EVENT_APPLAUSE) {
444 play_sample(SOUND_APPLAUSE);
446 if (sounds & SOUND_EVENT_RACKET) {
447 if (((s->ball.x)/WIDTH) < 0.5) {
448 pan_sample_group(SOUND_RACKET_FIRST, SOUND_RACKET_LAST, 0.3);
449 } else {
450 pan_sample_group(SOUND_RACKET_FIRST, SOUND_RACKET_LAST, 0.7);
452 play_sample(SOUND_RACKET);
454 r->sound_events = s->sound_events;
457 if( s->winner != WINNER_NONE) {
458 clear_screen();
459 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
460 store_screen();
461 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);
462 /*sprintf( s->game_score_str, "player %d wins the match with %s", s->winner, format_sets( s));
463 font_draw_string(FONT_LARGE, s->game_score_str, (WIDTH-font_get_string_width(FONT_LARGE, s->game_score_str))/2, HEIGHT/2 + 30);*/
464 updatescr();
465 return;
467 if ((r->referee != s->referee) ||
468 (r->ec_game != s->ec_game) ||
469 (r->ec_sets != s->ec_sets) ||
470 (r->status_message != s->status_message)) {
471 /* Update status message text */
472 if (r->status_message != s->status_message) {
473 r->text_status = format_status(s);
474 r->status_message = s->status_message;
476 /* Update game status text */
477 if (r->ec_game != s->ec_game) {
478 r->text_game = format_game(s);
479 r->ec_game = s->ec_game;
481 /* Update set status text */
482 if (r->ec_sets != s->ec_sets) {
483 r->text_sets = format_sets(s);
484 if (r->status_message == STATUSMSG_DEFAULT) {
485 r->text_status = format_status(s);
487 r->ec_sets = s->ec_sets;
489 clear_screen();
490 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
491 fill_image(s->location->court_type, 120, 120, 400, 250);
492 show_image(GR_COURT, 0, 0, 255);
493 font_draw_string(FONT_XLARGE, r->text_game, 14, 14);
494 font_draw_string(FONT_XLARGE, r->text_sets,
495 (WIDTH-font_get_string_width(FONT_XLARGE, r->text_sets))-14, 14);
496 if (s->location->has_referee) {
497 switch (s->referee) {
498 case REFEREE_NORMAL:
499 t = 1000;
500 break;
501 case REFEREE_OUT:
502 t = 200;
503 break;
504 case REFEREE_PLAYER1:
505 case REFEREE_PLAYER2:
506 t = 400;
507 break;
509 t = (s->time/t)%4;
510 switch (t) {
511 case 0:
512 t=0;
513 break;
514 case 1:
515 t=1;
516 break;
517 case 2:
518 t=0;
519 break;
520 case 3:
521 t=2;
522 break;
524 show_sprite( GR_REFEREE, s->referee*3+t, 12, 250, 10, 255);
525 if (voice_finished_flag == 0) {
526 show_sprite(GR_TALK, (s->time/150)%2, 2, 280, 45, 255);
529 r->referee = s->referee;
530 store_screen();
533 /* show_sprite( GR_RACKET, (!PLAYER(s, 1).state), 4, PLAYER(s, 1).x-RACKET_X_MID, PLAYER(s, 1).y-RACKET_Y_MID, 255);
534 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);*/
535 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);
536 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);
538 rectangle(10, HEIGHT-30, PLAYER_POWER_MAX, 10, 50, 50, 50);
539 rectangle(WIDTH-PLAYER_POWER_MAX-10, HEIGHT-30, PLAYER_POWER_MAX, 10, 50, 50, 50);
541 rectangle(10, HEIGHT-30, (int)(PLAYER(s, 1).power), 10, 200, 200, 200);
542 rectangle(WIDTH-PLAYER_POWER_MAX-10, HEIGHT-30, (int)(PLAYER(s, 2).power), 10, 200, 200, 200);
544 if( s->ball.move_x > 0) {
545 b = (s->time/100)%BALL_STATES;
546 } else if( s->ball.move_x < 0) {
547 b = BALL_STATES-1-(s->time/100)%BALL_STATES;
548 } else {
549 b = 0;
552 rotate = 0.0;
553 zoom = fmaxf(0.5, fminf(1.0, (float)(30.-(s->ball.z))/10.));
554 show_image_rotozoom(GR_SHADOW, s->ball.x, s->ball.y+3, rotate, zoom);
556 rotate = (-s->ball.move_x * (float)((float)s->time/10.));
557 zoom = 1.0 + fmaxf(0.0, (s->ball.z-10.)/100.);
558 show_image_rotozoom(GR_BALL, s->ball.x, s->ball.y - s->ball.z, rotate, zoom);
560 /* Player 1's mouse rectangle */
561 if (PLAYER(s, 1).input != NULL && PLAYER(s, 1).input->type == INPUT_TYPE_MOUSE) {
562 rectangle(PLAYER(s, 1).x-2+5, PLAYER(s, 1).input->my-2, 4, 4, 255, 255, 255);
563 rectangle(PLAYER(s, 1).x-1+5, PLAYER(s, 1).input->my-1, 2, 2, 0, 0, 0);
566 /* Player 2's mouse rectangle */
567 if (PLAYER(s, 2).input != NULL && PLAYER(s, 2).input->type == INPUT_TYPE_MOUSE) {
568 rectangle(PLAYER(s, 2).x-2+5, PLAYER(s, 2).input->my-2, 4, 4, 255, 255, 255);
569 rectangle(PLAYER(s, 2).x-1+5, PLAYER(s, 2).input->my-1, 2, 2, 0, 0, 0);
572 font_draw_string(FONT_MEDIUM, r->text_status,
573 (WIDTH-font_get_string_width(FONT_MEDIUM, r->text_status))/2, HEIGHT-50);
575 for (i=0; i<s->location->rain; i++) {
576 x = rand()%WIDTH;
577 y = rand()%HEIGHT;
578 draw_line_faded(x, y, x+10, y+30, 0, 0, 255, 100, 200, 255);
580 if (s->location->rain) {
582 * Cheap-ish update of the whole screen. This can
583 * probably be optimized.
585 update_rect(0, 0, WIDTH, HEIGHT);
588 #ifdef DEBUG
589 line_horiz( PLAYER(s, 1).y, 255, 0, 0);
590 line_horiz( PLAYER(s, 2).y, 0, 0, 255);
591 line_horiz( s->ball.y, 0, 255, 0);
593 line_vert( PLAYER(s, 1).x, 255, 0, 0);
594 line_vert( PLAYER(s, 2).x, 0, 0, 255);
595 line_vert( s->ball.x, 0, 255, 0);
597 line_horiz( GAME_Y_MIN, 100, 100, 100);
598 line_horiz( GAME_Y_MAX, 100, 100, 100);
599 #endif
600 switch (s->location->fog) {
601 default:
602 case 4:
603 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -s->time/150, 0);
604 case 3:
605 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, -s->time/100, 20);
606 case 2:
607 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -s->time/180, 80);
608 case 1:
609 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, s->time/200, 0);
610 case 0:
611 break;
613 if (s->location->night) {
614 show_image(GR_NIGHT, 0, 0, 255);
617 updatescr();
620 void limit_value( float* value, float min, float max) {
621 if( *value < min) {
622 *value = min;
623 } else if( *value > max) {
624 *value = max;
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;
634 pa = RACKET_Y_MID*2;
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 */
644 if( player == 1) {
645 x_len = GAME_X_MAX - s->ball.x;
646 } else {
647 x_len = s->ball.x - GAME_X_MIN;
649 y_len = dest - by;
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;
657 float move_y;
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);
671 if (move_y != 0) {
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;
677 } else {
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;
687 } else {
688 PLAYER(s, player).use_power = true;
692 void input_ai(GameState* s, int player) {
693 float fact = 1.7;
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) {
704 fact = 3.5;
707 if(1) {
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) {
726 s->ball.z = 10.0;
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;
736 } else {
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));
752 } else {
753 last_hit_by = &(PLAYER(s, 2));
754 other = &(PLAYER(s, 1));
757 switch (s->score_event) {
758 case SCORE_EVENT_NET:
759 winner = other;
760 break;
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;
767 } else {
768 winner = other;
770 break;
771 default:
772 break;
775 /* determine loser based on winner */
776 if (winner == last_hit_by) {
777 loser = other;
778 } else {
779 loser = last_hit_by;
782 /* we cannot be in an "impossibly high" set */
783 assert(s->current_set < SETS_TO_WIN*2-1);
785 winner->game++;
786 s->ec_game++;
787 if( loser->game < winner->game-1) {
788 if( winner->game >= 4) {
789 winner->game = loser->game = 0;
790 winner->sets[s->current_set]++;
791 s->ec_sets++;
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);
799 #endif
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) {
804 s->current_set++;
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;
815 } else {
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;
825 sets[0] = '\0';
827 if( s->winner != WINNER_NONE) {
828 max--;
830 for( i=0; i<=max; i++) {
831 sprintf( tmp, "%d:%d, ", PLAYER(s, 1).sets[i], PLAYER(s, 2).sets[i]);
832 strcat( sets, tmp);
835 sets[strlen(sets)-2] = '\0';
837 return sets;
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);
849 } else {
850 voice_say_list(2, VOICE_LOVE_IN + 2*(PLAYER(s, 1).game), VOICE_LOVE_OUT + 2*(PLAYER(s, 2).game));
853 #endif
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);
858 #endif
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);
863 #endif
864 strcpy( game, "advantage player 2");
865 } else {
866 #ifdef HAVE_VOICE_FILES
867 voice_say_list(1, VOICE_DEUCE);
868 #endif
869 strcpy( game, "deuce");
872 return game;
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) {
880 case STATUSMSG_NONE:
881 return "";
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]);
889 return status;
890 case STATUSMSG_NET:
891 return "net!";
892 case STATUSMSG_OUT:
893 return "out!";
894 case STATUSMSG_FAULT:
895 return "fault!";
896 case STATUSMSG_P1SCORES:
897 return "player 1 scores.";
898 case STATUSMSG_P2SCORES:
899 return "player 2 scores.";
900 case STATUSMSG_VOLLEY:
901 return "volley!";
902 case STATUSMSG_DIDNTCATCH:
903 return "did not catch the ball.";
904 default:
905 return "";
909 int game_get_winner( GameState* s) {
910 unsigned int i;
911 int sets[2] = {0};
913 for( i=0; i<s->current_set; i++) {
914 if( PLAYER(s, 1).sets[i] > PLAYER(s, 2).sets[i]) {
915 sets[0]++;
916 } else {
917 sets[1]++;
921 if( sets[0] == SETS_TO_WIN) return WINNER_PLAYER1;
922 if( sets[1] == SETS_TO_WIN) return WINNER_PLAYER2;
924 return WINNER_NONE;