Tennix 1.1 "Classic Championship Tour 2011" released
[tennix.git] / game.c
blob967263cee66efb5380844ddc3418ad16d5322378
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"
36 #include "network.h"
39 GameState *gamestate_new() {
40 GameState *s;
42 GameState gs_template = {
43 NULL,
44 -1,
45 { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, false, -1, false },
47 { NULL, -1, GAME_X_MIN-RACKET_X_MID*2, GAME_Y_MID, 0.0, true, 0, DESIRE_NORMAL, PLAYER_TYPE_AI, 0, {0}, PLAYER_ACCEL_DEFAULT },
48 { NULL, -1, GAME_X_MAX+RACKET_X_MID*2, GAME_Y_MID, 0.0, 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, &gs_template, sizeof(GameState));
67 game_setup_serve(s);
69 return s;
73 int gamestate_save(GameState* s, const char* filename)
75 const 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, TennixNet* c) {
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 int i=0;
171 RenderState r = {
172 SOUND_EVENT_NONE,
173 REFEREE_COUNT,
174 EVENTCOUNTER_RENDERSTATE_START,
175 EVENTCOUNTER_RENDERSTATE_START,
176 STATUSMSG_NONE,
177 NULL,
178 NULL,
179 NULL,
182 /* Catch-up with existing sound events */
183 r.sound_events = s->sound_events;
185 #ifdef ENABLE_FPS_LIMIT
186 Uint32 ft, frames; /* frame timer and frames */
187 #endif
189 if (s->location->rain > 0) {
190 play_sample_background(SOUND_RAIN);
192 if (s->location->max_visitors > 100) {
193 play_sample_loop(SOUND_AUDIENCE);
196 for (p=1; p<=MAXPLAYERS; p++) {
197 input_device_join_game(PLAYER(s, p).input, s, p);
200 #ifdef ENABLE_FPS_LIMIT
201 frames = 0;
202 ft = SDL_GetTicks();
203 #endif
204 while( !quit) {
205 nt = SDL_GetTicks();
206 diff = nt-ot;
207 if( diff > 2000) {
208 diff = 0;
211 accumulator += diff;
212 ot = nt;
214 while( accumulator >= dt) {
215 step(s);
217 network_receive(c);
218 network_get_gamestate(c, s);
220 if ((i++) % 10 == 0) {
221 network_send_state(c, s);
224 quit = handle_input(s, c);
225 accumulator -= dt;
228 #ifdef ENABLE_FPS_LIMIT
229 while (frames*1000.0/((float)(SDL_GetTicks()-ft+1))>(float)(DEFAULT_FPS)) {
230 SDL_Delay(10);
232 frames++;
233 #endif
235 render(s, &r);
238 for (p=1; p<=MAXPLAYERS; p++) {
239 input_device_part_game(PLAYER(s, p).input);
242 clear_screen();
243 //rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
244 store_screen();
246 stop_sample(SOUND_AUDIENCE);
247 stop_sample(SOUND_RAIN);
250 void step(GameState* s) {
251 bool ground_event = false;
252 int p;
254 s->ball.z += s->ball.move_z;
255 if (!s->ball.inhibit_gravity) {
256 s->ball.move_z += GRAVITY;
259 s->ball.x += s->ball.move_x;
260 s->ball.y += s->ball.move_y;
262 for (p=1; p<=MAXPLAYERS; p++) {
263 if (PLAYER(s, p).use_power) {
264 PLAYER(s, p).power = fmaxf(0.0, fminf(
265 PLAYER(s, p).power*POWER_DOWN_FACTOR,
266 PLAYER_POWER_MAX));
270 if (s->ball.z < 0) {
271 /* ground event */
272 ground_event = true;
274 s->referee = REFEREE_NORMAL;
276 /* bounce from the ground */
277 if (fabsf(s->ball.move_z) > 0.3) {
278 s->sound_events ^= SOUND_EVENT_GROUND;
279 s->ball.move_z *= -BALL_RESTITUTION;
280 } else {
281 s->ball.move_z = 0;
283 s->ball.z = 0;
286 if (NET_COLLISION_BALL(s->ball)) {
287 /* the net blocks movement of the ball */
288 while (NET_COLLISION_BALL(s->ball)) {
289 /* make sure the ball appears OUTSIDE of the net */
290 if (s->ball.move_x < 0) {
291 s->ball.x += 1;
292 } else {
293 s->ball.x -= 1;
296 s->ball.move_x = 0;
297 s->ball.move_y = 0;
300 /* see if we have something to score */
301 if (s->score_event == SCORE_UNDECIDED) {
302 if (NET_COLLISION_BALL(s->ball)) {
303 /* the ball "fell" into the net */
304 s->score_event = SCORE_EVENT_NET;
305 s->sound_events ^= SOUND_EVENT_OUT;
306 s->status_message = STATUSMSG_NET;
307 } else if (IS_OFFSCREEN(s->ball.x, s->ball.y)) {
308 /* ball flew offscreen */
309 s->score_event = SCORE_EVENT_OFFSCREEN;
310 s->sound_events ^= SOUND_EVENT_OUT;
311 s->status_message = STATUSMSG_OUT;
312 } else if (ground_event) {
313 /* the ball hit the ground on the screen */
314 if (IS_OUT(s->ball.x, s->ball.y)) {
315 /* the ball bounced in the OUT area */
316 s->score_event = SCORE_EVENT_OUT;
317 s->sound_events ^= SOUND_EVENT_OUT;
318 s->status_message = STATUSMSG_OUT;
319 } else if (GROUND_IS_VALID(s->ball.last_hit_by, s->ball.x, s->ball.y)) {
320 if (s->ball.ground_hit) {
321 s->score_event = SCORE_EVENT_GROUND_VALID;
322 s->sound_events ^= SOUND_EVENT_OUT;
323 s->status_message = STATUSMSG_DIDNTCATCH;
324 } else {
325 /* first ground hit in valid area */
326 s->ball.ground_hit = true;
328 } else {
329 /* ball hit somewhere invalid */
330 s->score_event = SCORE_EVENT_GROUND_INVALID;
331 s->sound_events ^= SOUND_EVENT_OUT;
332 s->status_message = STATUSMSG_FAULT;
337 if (s->score_event != SCORE_UNDECIDED) {
338 /* we have some scoring to do */
339 if (s->score_time < SCORING_DELAY/GAME_TICKS) {
340 /* schedule scoring in the future */
341 s->score_time++;
342 s->referee = REFEREE_OUT;
343 } else if (s->score_time >= SCORING_DELAY/GAME_TICKS) {
344 /* time has ran out - score now */
345 switch (score_game(s)) {
346 case WINNER_PLAYER1:
347 s->status_message = STATUSMSG_P1SCORES;
348 s->referee = REFEREE_PLAYER1;
349 break;
350 case WINNER_PLAYER2:
351 s->status_message = STATUSMSG_P2SCORES;
352 s->referee = REFEREE_PLAYER2;
353 break;
354 default:
355 assert(0);
356 break;
358 s->score_time = 0;
360 game_setup_serve(s);
361 if (s->location->max_visitors > 100) {
362 s->sound_events ^= SOUND_EVENT_APPLAUSE;
365 } else {
366 /* score is still undecided - do the racket swing thing */
367 for (p=1; p<=2; p++) {
368 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) {
369 /* RACKET HIT */
370 if (!s->ball.ground_hit && s->ball.move_x != 0.0) {
371 s->status_message = STATUSMSG_VOLLEY;
372 } else {
373 s->status_message = STATUSMSG_DEFAULT;
375 switch (PLAYER(s, p).desire) {
376 case DESIRE_NORMAL:
377 /* normal swing */
378 s->ball.move_x = 2.7 + 2.0*PLAYER(s, p).power/PLAYER_POWER_MAX;
379 s->ball.move_z = 1.2*PLAYER(s, p).power/PLAYER_POWER_MAX;
380 break;
381 case DESIRE_TOPSPIN:
382 /* top spin */
383 s->ball.move_x = 1.1 + 2.2*PLAYER(s, p).power/PLAYER_POWER_MAX;
384 s->ball.move_z = 2.5*PLAYER(s, p).power/PLAYER_POWER_MAX;
385 break;
386 case DESIRE_SMASH:
387 /* smash */
388 s->ball.move_x = 4.0 + 3.0*PLAYER(s, p).power/PLAYER_POWER_MAX;
389 s->ball.move_z = 1.1*PLAYER(s, p).power/PLAYER_POWER_MAX;
390 break;
392 s->ball.move_y = get_move_y( s, p);
393 s->sound_events ^= SOUND_EVENT_RACKET;
394 s->ball.ground_hit = false;
395 s->ball.inhibit_gravity = false;
396 s->ball.last_hit_by = p;
397 if (p==2) {
398 s->ball.move_x *= -1;
405 bool handle_input(GameState* s, TennixNet* c) {
406 static NetworkGameState tmp;
407 static NetworkInputData net_input;
408 Uint8* keys = NULL;
409 int p;
411 SDL_PumpEvents();
412 keys = SDL_GetKeyState(NULL);
414 if (keys['1']) {
415 net_serialize_gamestate(s, &tmp);
416 } else if (keys['2']) {
417 net_unserialize_gamestate(&tmp, s);
420 network_get_input(c, &net_input);
421 if (s->winner == WINNER_NONE) {
422 for (p=1; p<=MAXPLAYERS; p++) {
423 if( PLAYER(s, p).type == PLAYER_TYPE_HUMAN) {
424 if (PLAYER(s, p).input->type == INPUT_TYPE_NETWORK) {
425 memcpy(&(PLAYER(s, p).input->net), &net_input,
426 sizeof(NetworkInputData));
428 input_human(s, p);
429 if (PLAYER(s, p).input->type != INPUT_TYPE_NETWORK) {
430 network_send_input(c, &(PLAYER(s, p).input->net));
432 } else {
433 input_ai(s, p);
436 /* Make sure player coordinates are valid */
437 if (PLAYER(s, p).y < PLAYER_Y_MIN) {
438 PLAYER(s, p).y = PLAYER_Y_MIN;
439 } else if (PLAYER(s, p).y > PLAYER_Y_MAX) {
440 PLAYER(s, p).y = PLAYER_Y_MAX;
445 /* Maemo: The "F4" button is the "Open menu" button */
446 return (keys[SDLK_ESCAPE] || keys['q']);
449 void render(const GameState* s, RenderState* r) {
450 int x, y, b;
451 unsigned int i;
452 float zoom;
453 float rotate;
454 int t=1000;
455 soundevent_t sounds;
456 Uint32 time = SDL_GetTicks();
458 /* The bits in sound_events flip when the sound should play */
459 if ((sounds = (r->sound_events ^ s->sound_events)) != 0) {
460 if (sounds & SOUND_EVENT_GROUND) {
461 sample_volume_group(SOUND_GROUND_FIRST, SOUND_GROUND_LAST,
462 fmaxf(0.0, fminf(1.0, fabsf(s->ball.move_z)/2)));
463 pan_sample_group(SOUND_GROUND_FIRST, SOUND_GROUND_LAST,
464 fmaxf(0.0, fminf(1.0, (s->ball.x)/WIDTH)));
465 play_sample(SOUND_GROUND);
467 if (sounds & SOUND_EVENT_OUT) {
468 play_sample(SOUND_OUT);
470 if (sounds & SOUND_EVENT_APPLAUSE) {
471 play_sample(SOUND_APPLAUSE);
473 if (sounds & SOUND_EVENT_RACKET) {
474 if (((s->ball.x)/WIDTH) < 0.5) {
475 pan_sample_group(SOUND_RACKET_FIRST, SOUND_RACKET_LAST, 0.3);
476 } else {
477 pan_sample_group(SOUND_RACKET_FIRST, SOUND_RACKET_LAST, 0.7);
479 play_sample(SOUND_RACKET);
481 r->sound_events = s->sound_events;
484 if( s->winner != WINNER_NONE) {
485 clear_screen();
486 //rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
487 store_screen();
488 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);
489 /*sprintf( s->game_score_str, "player %d wins the match with %s", s->winner, format_sets( s));
490 font_draw_string(FONT_LARGE, s->game_score_str, (WIDTH-font_get_string_width(FONT_LARGE, s->game_score_str))/2, HEIGHT/2 + 30);*/
491 updatescr();
492 return;
494 if ((r->referee != s->referee) ||
495 (r->ec_game != s->ec_game) ||
496 (r->ec_sets != s->ec_sets) ||
497 (r->status_message != s->status_message)) {
498 /* Update status message text */
499 if (r->status_message != s->status_message) {
500 r->text_status = format_status(s);
501 r->status_message = s->status_message;
503 /* Update game status text */
504 if (r->ec_game != s->ec_game) {
505 r->text_game = format_game(s);
506 r->ec_game = s->ec_game;
508 /* Update set status text */
509 if (r->ec_sets != s->ec_sets) {
510 r->text_sets = format_sets(s);
511 if (r->status_message == STATUSMSG_DEFAULT) {
512 r->text_status = format_status(s);
514 r->ec_sets = s->ec_sets;
516 clear_screen();
517 //rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
518 fill_image(s->location->court_type, 120, 120, 400, 250);
519 show_image(GR_COURT, 0, 0, 255);
520 font_draw_string(FONT_XLARGE, r->text_game, 14, 14);
521 font_draw_string(FONT_XLARGE, r->text_sets,
522 (WIDTH-font_get_string_width(FONT_XLARGE, r->text_sets))-14, 14);
523 if (s->location->has_referee) {
524 switch (s->referee) {
525 case REFEREE_NORMAL:
526 t = 1000;
527 break;
528 case REFEREE_OUT:
529 t = 200;
530 break;
531 case REFEREE_PLAYER1:
532 case REFEREE_PLAYER2:
533 t = 400;
534 break;
536 t = (time/t)%4;
537 switch (t) {
538 case 0:
539 t=0;
540 break;
541 case 1:
542 t=1;
543 break;
544 case 2:
545 t=0;
546 break;
547 case 3:
548 t=2;
549 break;
551 show_sprite( GR_REFEREE, s->referee*3+t, 12, 250, 10, 255);
552 if (voice_finished_flag == 0) {
553 show_sprite(GR_TALK, (time/150)%2, 2, 280, 45, 255);
556 r->referee = s->referee;
557 store_screen();
560 /* show_sprite( GR_RACKET, (!PLAYER(s, 1).state), 4, PLAYER(s, 1).x-RACKET_X_MID, PLAYER(s, 1).y-RACKET_Y_MID, 255);
561 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);*/
562 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);
563 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);
565 rectangle(10, HEIGHT-30, PLAYER_POWER_MAX, 10, 50, 50, 50);
566 rectangle(WIDTH-PLAYER_POWER_MAX-10, HEIGHT-30, PLAYER_POWER_MAX, 10, 50, 50, 50);
568 rectangle(10, HEIGHT-30, (int)(PLAYER(s, 1).power), 10, 200, 200, 200);
569 rectangle(WIDTH-PLAYER_POWER_MAX-10, HEIGHT-30, (int)(PLAYER(s, 2).power), 10, 200, 200, 200);
571 if( s->ball.move_x > 0) {
572 b = (time/100)%BALL_STATES;
573 } else if( s->ball.move_x < 0) {
574 b = BALL_STATES-1-(time/100)%BALL_STATES;
575 } else {
576 b = 0;
579 rotate = 0.0;
580 zoom = fmaxf(0.5, fminf(1.0, (float)(30.-(s->ball.z))/10.));
581 show_image_rotozoom(GR_SHADOW, s->ball.x, s->ball.y+3, rotate, zoom);
583 rotate = (-s->ball.move_x * (float)((float)time/10.));
584 zoom = 1.0 + fmaxf(0.0, (s->ball.z-10.)/100.);
585 show_image_rotozoom(GR_BALL, s->ball.x, s->ball.y - s->ball.z, rotate, zoom);
587 /* Player 1's mouse rectangle */
588 if (PLAYER(s, 1).input != NULL && PLAYER(s, 1).input->type == INPUT_TYPE_MOUSE) {
589 rectangle(PLAYER(s, 1).x-2+5, PLAYER(s, 1).input->my-2, 4, 4, 255, 255, 255);
590 rectangle(PLAYER(s, 1).x-1+5, PLAYER(s, 1).input->my-1, 2, 2, 0, 0, 0);
593 /* Player 2's mouse rectangle */
594 if (PLAYER(s, 2).input != NULL && PLAYER(s, 2).input->type == INPUT_TYPE_MOUSE) {
595 rectangle(PLAYER(s, 2).x-2+5, PLAYER(s, 2).input->my-2, 4, 4, 255, 255, 255);
596 rectangle(PLAYER(s, 2).x-1+5, PLAYER(s, 2).input->my-1, 2, 2, 0, 0, 0);
599 font_draw_string(FONT_MEDIUM, r->text_status,
600 (WIDTH-font_get_string_width(FONT_MEDIUM, r->text_status))/2, HEIGHT-50);
602 for (i=0; i<s->location->rain; i++) {
603 x = rand()%WIDTH;
604 y = rand()%HEIGHT;
605 draw_line_faded(x, y, x+10, y+30, 0, 0, 255, 100, 200, 255);
607 if (s->location->rain) {
609 * Cheap-ish update of the whole screen. This can
610 * probably be optimized.
612 update_rect(0, 0, WIDTH, HEIGHT);
615 #ifdef DEBUG
616 line_horiz( PLAYER(s, 1).y, 255, 0, 0);
617 line_horiz( PLAYER(s, 2).y, 0, 0, 255);
618 line_horiz( s->ball.y, 0, 255, 0);
620 line_vert( PLAYER(s, 1).x, 255, 0, 0);
621 line_vert( PLAYER(s, 2).x, 0, 0, 255);
622 line_vert( s->ball.x, 0, 255, 0);
624 line_horiz( GAME_Y_MIN, 100, 100, 100);
625 line_horiz( GAME_Y_MAX, 100, 100, 100);
626 #endif
627 switch (s->location->fog) {
628 default:
629 case 4:
630 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -time/150, 0);
631 case 3:
632 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, -time/100, 20);
633 case 2:
634 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -time/180, 80);
635 case 1:
636 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, time/200, 0);
637 case 0:
638 break;
640 if (s->location->night) {
641 show_image(GR_NIGHT, 0, 0, 255);
644 updatescr();
647 float get_move_y( GameState* s, unsigned char player) {
648 float pct, dest, x_len, y_len;
649 float py, by, pa, move_x;
651 py = (player==1)?(PLAYER(s, 1).y):(PLAYER(s, 2).y);
652 by = s->ball.y - s->ball.z;
653 pa = RACKET_Y_MID*2;
654 move_x = s->ball.move_x;
656 /* -1.0 .. 1.0 for racket hit position */
657 pct = fmaxf(-1.0, fminf(1.0, (by-py)/(pa/2)));
659 /* Y destination for ball */
660 dest = GAME_Y_MID + pct*(GAME_Y_MAX-GAME_Y_MIN);
662 /* lengths for the ball's journey */
663 if( player == 1) {
664 x_len = GAME_X_MAX - s->ball.x;
665 } else {
666 x_len = s->ball.x - GAME_X_MIN;
668 y_len = dest - by;
670 /* return the should-be value for move_y */
671 return (y_len*move_x)/(x_len);
674 void input_human(GameState* s, int player) {
675 bool hit, topspin, smash;
676 float move_y;
678 /* For mouse input, hand the player coordinates to the InputDevice */
679 if (PLAYER(s, player).input->type == INPUT_TYPE_MOUSE) {
680 PLAYER(s, player).input->player_x = (int)(PLAYER(s, player).x);
681 PLAYER(s, player).input->player_y = (int)(PLAYER(s, player).y);
684 move_y = PLAYER_MOVE_Y*input_device_get_axis(PLAYER(s, player).input, INPUT_AXIS_Y);
686 hit = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_HIT);
687 topspin = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_TOPSPIN);
688 smash = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_SMASH);
690 if (move_y != 0) {
691 if (fabsf(move_y) > fabsf(move_y*PLAYER(s, player).accelerate)) {
692 move_y *= PLAYER(s, player).accelerate;
694 PLAYER(s, player).y += move_y;
695 PLAYER(s, player).accelerate *= PLAYER_ACCEL_INCREASE;
696 if (PLAYER(s, player).accelerate > 190) {
697 PLAYER(s, player).accelerate = 190;
699 } else {
700 PLAYER(s, player).accelerate = PLAYER_ACCEL_DEFAULT;
703 if(hit || topspin || smash) {
704 PLAYER(s, player).desire = (topspin)?(DESIRE_TOPSPIN):(DESIRE_NORMAL);
705 PLAYER(s, player).desire = (smash)?(DESIRE_SMASH):(PLAYER(s, player).desire);
707 PLAYER(s, player).power = fmaxf(10.0, fminf(PLAYER(s, player).power*POWER_UP_FACTOR, PLAYER_POWER_MAX));
708 PLAYER(s, player).use_power = false;
709 } else {
710 PLAYER(s, player).use_power = true;
714 void input_ai(GameState* s, int player) {
715 float fact = 1.7;
716 int ball_approaching = 0;
718 if ((PLAYER(s, player).x < GAME_X_MID && s->ball.move_x <= 0) ||
719 (PLAYER(s, player).x > GAME_X_MID && s->ball.move_x >= 0)) {
720 ball_approaching = 1;
723 /* FIXME - this is broken since the new physics model has been introduced */
725 if (fabsf(PLAYER(s, player).y - (s->ball.y-s->ball.z)) > RACKET_Y_MID*5) {
726 fact = 3.5;
729 if(1) {
730 if( PLAYER(s, player).desire == DESIRE_NORMAL && !IS_NEAR_Y_AI(PLAYER(s, player).y, (s->ball.y-s->ball.z)) && ball_approaching) {
731 if( PLAYER(s, player).y < (s->ball.y-s->ball.z)) {
732 PLAYER(s, player).y += fminf(2*fact, (s->ball.y-s->ball.z) - PLAYER(s, player).y);
733 } else if( PLAYER(s, player).y > (s->ball.y-s->ball.z)) {
734 PLAYER(s, player).y -= fminf(2*fact, PLAYER(s, player).y - (s->ball.y-s->ball.z));
738 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.) {
739 PLAYER(s, player).use_power = true;
740 } else if (ball_approaching) {
741 PLAYER(s, player).power = fmaxf(10., fminf(PLAYER(s, player).power*POWER_UP_FACTOR, PLAYER_POWER_MAX));
742 PLAYER(s, player).use_power = false;
747 void game_setup_serve( GameState* s) {
748 s->ball.z = 10.0;
749 s->ball.y = GAME_Y_MID;
750 s->ball.move_x = 0.0;
751 s->ball.move_y = 0.0;
752 s->ball.move_z = 0.0;
753 s->ball.last_hit_by = -1;
754 s->ball.inhibit_gravity = true;
756 if( s->serving_player == 1) {
757 s->ball.x = GAME_X_MIN-RACKET_X_MID*1.5;
758 } else {
759 s->ball.x = GAME_X_MAX+RACKET_X_MID*1.5;
763 int score_game(GameState* s) {
764 Player *last_hit_by = NULL;
765 Player *other = NULL;
767 Player* winner = NULL;
768 Player* loser = NULL;
770 /* determine "last hit by" and "other" */
771 if (s->ball.last_hit_by == 1) {
772 last_hit_by = &(PLAYER(s, 1));
773 other = &(PLAYER(s, 2));
774 } else {
775 last_hit_by = &(PLAYER(s, 2));
776 other = &(PLAYER(s, 1));
779 switch (s->score_event) {
780 case SCORE_EVENT_NET:
781 winner = other;
782 break;
783 case SCORE_EVENT_OUT:
784 case SCORE_EVENT_GROUND_INVALID:
785 case SCORE_EVENT_OFFSCREEN:
786 case SCORE_EVENT_GROUND_VALID:
787 if (s->ball.ground_hit) {
788 winner = last_hit_by;
789 } else {
790 winner = other;
792 break;
793 default:
794 break;
797 /* determine loser based on winner */
798 if (winner == last_hit_by) {
799 loser = other;
800 } else {
801 loser = last_hit_by;
804 /* we cannot be in an "impossibly high" set */
805 assert(s->current_set < SETS_TO_WIN*2-1);
807 winner->game++;
808 s->ec_game++;
809 if( loser->game < winner->game-1) {
810 if( winner->game >= 4) {
811 winner->game = loser->game = 0;
812 winner->sets[s->current_set]++;
813 s->ec_sets++;
815 /* serving is changed when the "game" is over */
816 s->serving_player = (s->serving_player==1)?(2):(1);
818 #ifdef HAVE_VOICE_FILES
819 /* speak the current score */
820 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);
821 #endif
823 /* scoring the set.. */
824 if( (winner->sets[s->current_set] == 6 && loser->sets[s->current_set] < 5) ||
825 winner->sets[s->current_set] == 7) {
826 s->current_set++;
827 s->winner = game_get_winner( s);
832 /* forget this event - we've handled it */
833 s->score_event = SCORE_UNDECIDED;
835 if (winner == &PLAYER(s, 1)) {
836 return WINNER_PLAYER1;
837 } else {
838 return WINNER_PLAYER2;
842 const char* format_sets(const GameState* s) {
843 static char sets[100];
844 static char tmp[100];
845 int i, max = s->current_set;
847 sets[0] = '\0';
849 if( s->winner != WINNER_NONE) {
850 max--;
852 for( i=0; i<=max; i++) {
853 sprintf( tmp, "%d:%d, ", PLAYER(s, 1).sets[i], PLAYER(s, 2).sets[i]);
854 strcat( sets, tmp);
857 sets[strlen(sets)-2] = '\0';
859 return sets;
862 const char* format_game(const GameState* s) {
863 static char game[100];
864 static const int game_scoring[] = { 0, 15, 30, 40 };
866 if( PLAYER(s, 1).game < 4 && PLAYER(s, 2).game < 4) {
867 #ifdef HAVE_VOICE_FILES
868 if (PLAYER(s, 1).game > 0 || PLAYER(s, 2).game > 0) {
869 if (PLAYER(s, 1).game == PLAYER(s, 2).game) {
870 voice_say_list(2, VOICE_LOVE_IN + 2*(PLAYER(s, 1).game), VOICE_ALL);
871 } else {
872 voice_say_list(2, VOICE_LOVE_IN + 2*(PLAYER(s, 1).game), VOICE_LOVE_OUT + 2*(PLAYER(s, 2).game));
875 #endif
876 sprintf( game, "%d - %d", game_scoring[PLAYER(s, 1).game], game_scoring[PLAYER(s, 2).game]);
877 } else if( PLAYER(s, 1).game > PLAYER(s, 2).game) {
878 #ifdef HAVE_VOICE_FILES
879 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_ONE);
880 #endif
881 strcpy( game, "advantage player 1");
882 } else if( PLAYER(s, 1).game < PLAYER(s, 2).game) {
883 #ifdef HAVE_VOICE_FILES
884 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_TWO);
885 #endif
886 strcpy( game, "advantage player 2");
887 } else {
888 #ifdef HAVE_VOICE_FILES
889 voice_say_list(1, VOICE_DEUCE);
890 #endif
891 strcpy( game, "deuce");
894 return game;
897 const char* format_status(const GameState* s) {
898 static char status[100];
899 static const char* set_names[] = { "first", "second", "third", "fourth", "fifth" };
901 switch (s->status_message) {
902 case STATUSMSG_NONE:
903 return "";
904 case STATUSMSG_WELCOME:
905 return "welcome to tennix " VERSION;
906 case STATUSMSG_DEFAULT:
907 sprintf(status, "%d:%d in %s set",
908 PLAYER(s, 1).sets[s->current_set],
909 PLAYER(s, 2).sets[s->current_set],
910 set_names[s->current_set]);
911 return status;
912 case STATUSMSG_NET:
913 return "net!";
914 case STATUSMSG_OUT:
915 return "out!";
916 case STATUSMSG_FAULT:
917 return "fault!";
918 case STATUSMSG_P1SCORES:
919 return "player 1 scores.";
920 case STATUSMSG_P2SCORES:
921 return "player 2 scores.";
922 case STATUSMSG_VOLLEY:
923 return "volley!";
924 case STATUSMSG_DIDNTCATCH:
925 return "did not catch the ball.";
926 default:
927 return "";
931 winner_t game_get_winner(const GameState* s) {
932 unsigned int i;
933 int sets[2] = {0};
935 for( i=0; i<s->current_set; i++) {
936 if( PLAYER(s, 1).sets[i] > PLAYER(s, 2).sets[i]) {
937 sets[0]++;
938 } else {
939 sets[1]++;
943 if( sets[0] == SETS_TO_WIN) return WINNER_PLAYER1;
944 if( sets[1] == SETS_TO_WIN) return WINNER_PLAYER2;
946 return WINNER_NONE;