First cut of multi-player support
[tennix.git] / game.c
blob5b83a62e4346194b250e77996ab0a967790089bb
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 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, &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 assert(c != NULL);
184 /* Catch-up with existing sound events */
185 r.sound_events = s->sound_events;
187 #ifdef ENABLE_FPS_LIMIT
188 Uint32 ft, frames; /* frame timer and frames */
189 #endif
191 if (s->location->rain > 0) {
192 play_sample_background(SOUND_RAIN);
194 if (s->location->max_visitors > 100) {
195 play_sample_loop(SOUND_AUDIENCE);
198 for (p=1; p<=MAXPLAYERS; p++) {
199 input_device_join_game(PLAYER(s, p).input, s, p);
202 #ifdef ENABLE_FPS_LIMIT
203 frames = 0;
204 ft = SDL_GetTicks();
205 #endif
206 while( !quit) {
207 nt = SDL_GetTicks();
208 diff = nt-ot;
209 if( diff > 2000) {
210 diff = 0;
213 accumulator += diff;
214 ot = nt;
216 while( accumulator >= dt) {
217 step(s);
219 network_receive(c);
220 network_get_gamestate(c, s);
222 if ((i++) % 10 == 0) {
223 network_send_state(c, s);
226 quit = handle_input(s, c);
227 accumulator -= dt;
230 #ifdef ENABLE_FPS_LIMIT
231 while (frames*1000.0/((float)(SDL_GetTicks()-ft+1))>(float)(DEFAULT_FPS)) {
232 SDL_Delay(10);
234 frames++;
235 #endif
237 render(s, &r);
240 for (p=1; p<=MAXPLAYERS; p++) {
241 input_device_part_game(PLAYER(s, p).input);
244 clear_screen();
245 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
246 store_screen();
248 stop_sample(SOUND_AUDIENCE);
249 stop_sample(SOUND_RAIN);
252 void step(GameState* s) {
253 bool ground_event = false;
254 int p;
256 s->ball.z += s->ball.move_z;
257 if (!s->ball.inhibit_gravity) {
258 s->ball.move_z += GRAVITY;
261 s->ball.x += s->ball.move_x;
262 s->ball.y += s->ball.move_y;
264 for (p=1; p<=MAXPLAYERS; p++) {
265 if (PLAYER(s, p).use_power) {
266 PLAYER(s, p).power = fmaxf(0.0, fminf(
267 PLAYER(s, p).power*POWER_DOWN_FACTOR,
268 PLAYER_POWER_MAX));
272 if (s->ball.z < 0) {
273 /* ground event */
274 ground_event = true;
276 s->referee = REFEREE_NORMAL;
278 /* bounce from the ground */
279 if (fabsf(s->ball.move_z) > 0.3) {
280 s->sound_events ^= SOUND_EVENT_GROUND;
281 s->ball.move_z *= -BALL_RESTITUTION;
282 } else {
283 s->ball.move_z = 0;
285 s->ball.z = 0;
288 if (NET_COLLISION_BALL(s->ball)) {
289 /* the net blocks movement of the ball */
290 while (NET_COLLISION_BALL(s->ball)) {
291 /* make sure the ball appears OUTSIDE of the net */
292 if (s->ball.move_x < 0) {
293 s->ball.x += 1;
294 } else {
295 s->ball.x -= 1;
298 s->ball.move_x = 0;
299 s->ball.move_y = 0;
302 /* see if we have something to score */
303 if (s->score_event == SCORE_UNDECIDED) {
304 if (NET_COLLISION_BALL(s->ball)) {
305 /* the ball "fell" into the net */
306 s->score_event = SCORE_EVENT_NET;
307 s->sound_events ^= SOUND_EVENT_OUT;
308 s->status_message = STATUSMSG_NET;
309 } else if (IS_OFFSCREEN(s->ball.x, s->ball.y)) {
310 /* ball flew offscreen */
311 s->score_event = SCORE_EVENT_OFFSCREEN;
312 s->sound_events ^= SOUND_EVENT_OUT;
313 s->status_message = STATUSMSG_OUT;
314 } else if (ground_event) {
315 /* the ball hit the ground on the screen */
316 if (IS_OUT(s->ball.x, s->ball.y)) {
317 /* the ball bounced in the OUT area */
318 s->score_event = SCORE_EVENT_OUT;
319 s->sound_events ^= SOUND_EVENT_OUT;
320 s->status_message = STATUSMSG_OUT;
321 } else if (GROUND_IS_VALID(s->ball.last_hit_by, s->ball.x, s->ball.y)) {
322 if (s->ball.ground_hit) {
323 s->score_event = SCORE_EVENT_GROUND_VALID;
324 s->sound_events ^= SOUND_EVENT_OUT;
325 s->status_message = STATUSMSG_DIDNTCATCH;
326 } else {
327 /* first ground hit in valid area */
328 s->ball.ground_hit = true;
330 } else {
331 /* ball hit somewhere invalid */
332 s->score_event = SCORE_EVENT_GROUND_INVALID;
333 s->sound_events ^= SOUND_EVENT_OUT;
334 s->status_message = STATUSMSG_FAULT;
339 if (s->score_event != SCORE_UNDECIDED) {
340 /* we have some scoring to do */
341 if (s->score_time < SCORING_DELAY/GAME_TICKS) {
342 /* schedule scoring in the future */
343 s->score_time++;
344 s->referee = REFEREE_OUT;
345 } else if (s->score_time >= SCORING_DELAY/GAME_TICKS) {
346 /* time has ran out - score now */
347 switch (score_game(s)) {
348 case WINNER_PLAYER1:
349 s->status_message = STATUSMSG_P1SCORES;
350 s->referee = REFEREE_PLAYER1;
351 break;
352 case WINNER_PLAYER2:
353 s->status_message = STATUSMSG_P2SCORES;
354 s->referee = REFEREE_PLAYER2;
355 break;
356 default:
357 assert(0);
358 break;
360 s->score_time = 0;
362 game_setup_serve(s);
363 if (s->location->max_visitors > 100) {
364 s->sound_events ^= SOUND_EVENT_APPLAUSE;
367 } else {
368 /* score is still undecided - do the racket swing thing */
369 for (p=1; p<=2; p++) {
370 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) {
371 /* RACKET HIT */
372 if (!s->ball.ground_hit && s->ball.move_x != 0.0) {
373 s->status_message = STATUSMSG_VOLLEY;
374 } else {
375 s->status_message = STATUSMSG_DEFAULT;
377 switch (PLAYER(s, p).desire) {
378 case DESIRE_NORMAL:
379 /* normal swing */
380 s->ball.move_x = 2.7 + 2.0*PLAYER(s, p).power/PLAYER_POWER_MAX;
381 s->ball.move_z = 1.2*PLAYER(s, p).power/PLAYER_POWER_MAX;
382 break;
383 case DESIRE_TOPSPIN:
384 /* top spin */
385 s->ball.move_x = 1.1 + 2.2*PLAYER(s, p).power/PLAYER_POWER_MAX;
386 s->ball.move_z = 2.5*PLAYER(s, p).power/PLAYER_POWER_MAX;
387 break;
388 case DESIRE_SMASH:
389 /* smash */
390 s->ball.move_x = 4.0 + 3.0*PLAYER(s, p).power/PLAYER_POWER_MAX;
391 s->ball.move_z = 1.1*PLAYER(s, p).power/PLAYER_POWER_MAX;
392 break;
394 s->ball.move_y = get_move_y( s, p);
395 s->sound_events ^= SOUND_EVENT_RACKET;
396 s->ball.ground_hit = false;
397 s->ball.inhibit_gravity = false;
398 s->ball.last_hit_by = p;
399 if (p==2) {
400 s->ball.move_x *= -1;
407 bool handle_input(GameState* s, TennixNet* c) {
408 static NetworkGameState tmp;
409 static NetworkInputData net_input;
410 Uint8* keys = NULL;
411 int p;
413 SDL_PumpEvents();
414 keys = SDL_GetKeyState(NULL);
416 if (keys['1']) {
417 net_serialize_gamestate(s, &tmp);
418 } else if (keys['2']) {
419 net_unserialize_gamestate(&tmp, s);
422 network_get_input(c, &net_input);
423 if (s->winner == WINNER_NONE) {
424 for (p=1; p<=MAXPLAYERS; p++) {
425 if( PLAYER(s, p).type == PLAYER_TYPE_HUMAN) {
426 if (PLAYER(s, p).input->type == INPUT_TYPE_NETWORK) {
427 memcpy(&(PLAYER(s, p).input->net), &net_input,
428 sizeof(NetworkInputData));
430 input_human(s, p);
431 if (PLAYER(s, p).input->type != INPUT_TYPE_NETWORK) {
432 network_send_input(c, &(PLAYER(s, p).input->net));
434 } else {
435 input_ai(s, p);
438 /* Make sure player coordinates are valid */
439 if (PLAYER(s, p).y < PLAYER_Y_MIN) {
440 PLAYER(s, p).y = PLAYER_Y_MIN;
441 } else if (PLAYER(s, p).y > PLAYER_Y_MAX) {
442 PLAYER(s, p).y = PLAYER_Y_MAX;
447 /* Maemo: The "F4" button is the "Open menu" button */
448 return (keys[SDLK_ESCAPE] || keys['q']);
451 void render(const GameState* s, RenderState* r) {
452 int x, y, b;
453 unsigned int i;
454 float zoom;
455 float rotate;
456 int t=1000;
457 soundevent_t sounds;
458 Uint32 time = SDL_GetTicks();
460 /* The bits in sound_events flip when the sound should play */
461 if ((sounds = (r->sound_events ^ s->sound_events)) != 0) {
462 if (sounds & SOUND_EVENT_GROUND) {
463 sample_volume_group(SOUND_GROUND_FIRST, SOUND_GROUND_LAST,
464 fmaxf(0.0, fminf(1.0, fabsf(s->ball.move_z)/2)));
465 pan_sample_group(SOUND_GROUND_FIRST, SOUND_GROUND_LAST,
466 fmaxf(0.0, fminf(1.0, (s->ball.x)/WIDTH)));
467 play_sample(SOUND_GROUND);
469 if (sounds & SOUND_EVENT_OUT) {
470 play_sample(SOUND_OUT);
472 if (sounds & SOUND_EVENT_APPLAUSE) {
473 play_sample(SOUND_APPLAUSE);
475 if (sounds & SOUND_EVENT_RACKET) {
476 if (((s->ball.x)/WIDTH) < 0.5) {
477 pan_sample_group(SOUND_RACKET_FIRST, SOUND_RACKET_LAST, 0.3);
478 } else {
479 pan_sample_group(SOUND_RACKET_FIRST, SOUND_RACKET_LAST, 0.7);
481 play_sample(SOUND_RACKET);
483 r->sound_events = s->sound_events;
486 if( s->winner != WINNER_NONE) {
487 clear_screen();
488 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
489 store_screen();
490 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);
491 /*sprintf( s->game_score_str, "player %d wins the match with %s", s->winner, format_sets( s));
492 font_draw_string(FONT_LARGE, s->game_score_str, (WIDTH-font_get_string_width(FONT_LARGE, s->game_score_str))/2, HEIGHT/2 + 30);*/
493 updatescr();
494 return;
496 if ((r->referee != s->referee) ||
497 (r->ec_game != s->ec_game) ||
498 (r->ec_sets != s->ec_sets) ||
499 (r->status_message != s->status_message)) {
500 /* Update status message text */
501 if (r->status_message != s->status_message) {
502 r->text_status = format_status(s);
503 r->status_message = s->status_message;
505 /* Update game status text */
506 if (r->ec_game != s->ec_game) {
507 r->text_game = format_game(s);
508 r->ec_game = s->ec_game;
510 /* Update set status text */
511 if (r->ec_sets != s->ec_sets) {
512 r->text_sets = format_sets(s);
513 if (r->status_message == STATUSMSG_DEFAULT) {
514 r->text_status = format_status(s);
516 r->ec_sets = s->ec_sets;
518 clear_screen();
519 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
520 fill_image(s->location->court_type, 120, 120, 400, 250);
521 show_image(GR_COURT, 0, 0, 255);
522 font_draw_string(FONT_XLARGE, r->text_game, 14, 14);
523 font_draw_string(FONT_XLARGE, r->text_sets,
524 (WIDTH-font_get_string_width(FONT_XLARGE, r->text_sets))-14, 14);
525 if (s->location->has_referee) {
526 switch (s->referee) {
527 case REFEREE_NORMAL:
528 t = 1000;
529 break;
530 case REFEREE_OUT:
531 t = 200;
532 break;
533 case REFEREE_PLAYER1:
534 case REFEREE_PLAYER2:
535 t = 400;
536 break;
538 t = (time/t)%4;
539 switch (t) {
540 case 0:
541 t=0;
542 break;
543 case 1:
544 t=1;
545 break;
546 case 2:
547 t=0;
548 break;
549 case 3:
550 t=2;
551 break;
553 show_sprite( GR_REFEREE, s->referee*3+t, 12, 250, 10, 255);
554 if (voice_finished_flag == 0) {
555 show_sprite(GR_TALK, (time/150)%2, 2, 280, 45, 255);
558 r->referee = s->referee;
559 store_screen();
562 /* show_sprite( GR_RACKET, (!PLAYER(s, 1).state), 4, PLAYER(s, 1).x-RACKET_X_MID, PLAYER(s, 1).y-RACKET_Y_MID, 255);
563 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);*/
564 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);
565 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);
567 rectangle(10, HEIGHT-30, PLAYER_POWER_MAX, 10, 50, 50, 50);
568 rectangle(WIDTH-PLAYER_POWER_MAX-10, HEIGHT-30, PLAYER_POWER_MAX, 10, 50, 50, 50);
570 rectangle(10, HEIGHT-30, (int)(PLAYER(s, 1).power), 10, 200, 200, 200);
571 rectangle(WIDTH-PLAYER_POWER_MAX-10, HEIGHT-30, (int)(PLAYER(s, 2).power), 10, 200, 200, 200);
573 if( s->ball.move_x > 0) {
574 b = (time/100)%BALL_STATES;
575 } else if( s->ball.move_x < 0) {
576 b = BALL_STATES-1-(time/100)%BALL_STATES;
577 } else {
578 b = 0;
581 rotate = 0.0;
582 zoom = fmaxf(0.5, fminf(1.0, (float)(30.-(s->ball.z))/10.));
583 show_image_rotozoom(GR_SHADOW, s->ball.x, s->ball.y+3, rotate, zoom);
585 rotate = (-s->ball.move_x * (float)((float)time/10.));
586 zoom = 1.0 + fmaxf(0.0, (s->ball.z-10.)/100.);
587 show_image_rotozoom(GR_BALL, s->ball.x, s->ball.y - s->ball.z, rotate, zoom);
589 /* Player 1's mouse rectangle */
590 if (PLAYER(s, 1).input != NULL && PLAYER(s, 1).input->type == INPUT_TYPE_MOUSE) {
591 rectangle(PLAYER(s, 1).x-2+5, PLAYER(s, 1).input->my-2, 4, 4, 255, 255, 255);
592 rectangle(PLAYER(s, 1).x-1+5, PLAYER(s, 1).input->my-1, 2, 2, 0, 0, 0);
595 /* Player 2's mouse rectangle */
596 if (PLAYER(s, 2).input != NULL && PLAYER(s, 2).input->type == INPUT_TYPE_MOUSE) {
597 rectangle(PLAYER(s, 2).x-2+5, PLAYER(s, 2).input->my-2, 4, 4, 255, 255, 255);
598 rectangle(PLAYER(s, 2).x-1+5, PLAYER(s, 2).input->my-1, 2, 2, 0, 0, 0);
601 font_draw_string(FONT_MEDIUM, r->text_status,
602 (WIDTH-font_get_string_width(FONT_MEDIUM, r->text_status))/2, HEIGHT-50);
604 for (i=0; i<s->location->rain; i++) {
605 x = rand()%WIDTH;
606 y = rand()%HEIGHT;
607 draw_line_faded(x, y, x+10, y+30, 0, 0, 255, 100, 200, 255);
609 if (s->location->rain) {
611 * Cheap-ish update of the whole screen. This can
612 * probably be optimized.
614 update_rect(0, 0, WIDTH, HEIGHT);
617 #ifdef DEBUG
618 line_horiz( PLAYER(s, 1).y, 255, 0, 0);
619 line_horiz( PLAYER(s, 2).y, 0, 0, 255);
620 line_horiz( s->ball.y, 0, 255, 0);
622 line_vert( PLAYER(s, 1).x, 255, 0, 0);
623 line_vert( PLAYER(s, 2).x, 0, 0, 255);
624 line_vert( s->ball.x, 0, 255, 0);
626 line_horiz( GAME_Y_MIN, 100, 100, 100);
627 line_horiz( GAME_Y_MAX, 100, 100, 100);
628 #endif
629 switch (s->location->fog) {
630 default:
631 case 4:
632 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -time/150, 0);
633 case 3:
634 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, -time/100, 20);
635 case 2:
636 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -time/180, 80);
637 case 1:
638 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, time/200, 0);
639 case 0:
640 break;
642 if (s->location->night) {
643 show_image(GR_NIGHT, 0, 0, 255);
646 updatescr();
649 float get_move_y( GameState* s, unsigned char player) {
650 float pct, dest, x_len, y_len;
651 float py, by, pa, move_x;
653 py = (player==1)?(PLAYER(s, 1).y):(PLAYER(s, 2).y);
654 by = s->ball.y - s->ball.z;
655 pa = RACKET_Y_MID*2;
656 move_x = s->ball.move_x;
658 /* -1.0 .. 1.0 for racket hit position */
659 pct = fmaxf(-1.0, fminf(1.0, (by-py)/(pa/2)));
661 /* Y destination for ball */
662 dest = GAME_Y_MID + pct*(GAME_Y_MAX-GAME_Y_MIN);
664 /* lengths for the ball's journey */
665 if( player == 1) {
666 x_len = GAME_X_MAX - s->ball.x;
667 } else {
668 x_len = s->ball.x - GAME_X_MIN;
670 y_len = dest - by;
672 /* return the should-be value for move_y */
673 return (y_len*move_x)/(x_len);
676 void input_human(GameState* s, int player) {
677 bool hit, topspin, smash;
678 float move_y;
680 /* For mouse input, hand the player coordinates to the InputDevice */
681 if (PLAYER(s, player).input->type == INPUT_TYPE_MOUSE) {
682 PLAYER(s, player).input->player_x = (int)(PLAYER(s, player).x);
683 PLAYER(s, player).input->player_y = (int)(PLAYER(s, player).y);
686 move_y = PLAYER_MOVE_Y*input_device_get_axis(PLAYER(s, player).input, INPUT_AXIS_Y);
688 hit = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_HIT);
689 topspin = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_TOPSPIN);
690 smash = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_SMASH);
692 if (move_y != 0) {
693 if (fabsf(move_y) > fabsf(move_y*PLAYER(s, player).accelerate)) {
694 move_y *= PLAYER(s, player).accelerate;
696 PLAYER(s, player).y += move_y;
697 PLAYER(s, player).accelerate *= PLAYER_ACCEL_INCREASE;
698 if (PLAYER(s, player).accelerate > 190) {
699 PLAYER(s, player).accelerate = 190;
701 } else {
702 PLAYER(s, player).accelerate = PLAYER_ACCEL_DEFAULT;
705 if(hit || topspin || smash) {
706 PLAYER(s, player).desire = (topspin)?(DESIRE_TOPSPIN):(DESIRE_NORMAL);
707 PLAYER(s, player).desire = (smash)?(DESIRE_SMASH):(PLAYER(s, player).desire);
709 PLAYER(s, player).power = fmaxf(10.0, fminf(PLAYER(s, player).power*POWER_UP_FACTOR, PLAYER_POWER_MAX));
710 PLAYER(s, player).use_power = false;
711 } else {
712 PLAYER(s, player).use_power = true;
716 void input_ai(GameState* s, int player) {
717 float fact = 1.7;
718 int ball_approaching = 0;
720 if ((PLAYER(s, player).x < GAME_X_MID && s->ball.move_x <= 0) ||
721 (PLAYER(s, player).x > GAME_X_MID && s->ball.move_x >= 0)) {
722 ball_approaching = 1;
725 /* FIXME - this is broken since the new physics model has been introduced */
727 if (fabsf(PLAYER(s, player).y - (s->ball.y-s->ball.z)) > RACKET_Y_MID*5) {
728 fact = 3.5;
731 if(1) {
732 if( PLAYER(s, player).desire == DESIRE_NORMAL && !IS_NEAR_Y_AI(PLAYER(s, player).y, (s->ball.y-s->ball.z)) && ball_approaching) {
733 if( PLAYER(s, player).y < (s->ball.y-s->ball.z)) {
734 PLAYER(s, player).y += fminf(2*fact, (s->ball.y-s->ball.z) - PLAYER(s, player).y);
735 } else if( PLAYER(s, player).y > (s->ball.y-s->ball.z)) {
736 PLAYER(s, player).y -= fminf(2*fact, PLAYER(s, player).y - (s->ball.y-s->ball.z));
740 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.) {
741 PLAYER(s, player).use_power = true;
742 } else if (ball_approaching) {
743 PLAYER(s, player).power = fmaxf(10., fminf(PLAYER(s, player).power*POWER_UP_FACTOR, PLAYER_POWER_MAX));
744 PLAYER(s, player).use_power = false;
749 void game_setup_serve( GameState* s) {
750 s->ball.z = 10.0;
751 s->ball.y = GAME_Y_MID;
752 s->ball.move_x = 0.0;
753 s->ball.move_y = 0.0;
754 s->ball.move_z = 0.0;
755 s->ball.last_hit_by = -1;
756 s->ball.inhibit_gravity = true;
758 if( s->serving_player == 1) {
759 s->ball.x = GAME_X_MIN-RACKET_X_MID*1.5;
760 } else {
761 s->ball.x = GAME_X_MAX+RACKET_X_MID*1.5;
765 int score_game(GameState* s) {
766 Player *last_hit_by = NULL;
767 Player *other = NULL;
769 Player* winner = NULL;
770 Player* loser = NULL;
772 /* determine "last hit by" and "other" */
773 if (s->ball.last_hit_by == 1) {
774 last_hit_by = &(PLAYER(s, 1));
775 other = &(PLAYER(s, 2));
776 } else {
777 last_hit_by = &(PLAYER(s, 2));
778 other = &(PLAYER(s, 1));
781 switch (s->score_event) {
782 case SCORE_EVENT_NET:
783 winner = other;
784 break;
785 case SCORE_EVENT_OUT:
786 case SCORE_EVENT_GROUND_INVALID:
787 case SCORE_EVENT_OFFSCREEN:
788 case SCORE_EVENT_GROUND_VALID:
789 if (s->ball.ground_hit) {
790 winner = last_hit_by;
791 } else {
792 winner = other;
794 break;
795 default:
796 break;
799 /* determine loser based on winner */
800 if (winner == last_hit_by) {
801 loser = other;
802 } else {
803 loser = last_hit_by;
806 /* we cannot be in an "impossibly high" set */
807 assert(s->current_set < SETS_TO_WIN*2-1);
809 winner->game++;
810 s->ec_game++;
811 if( loser->game < winner->game-1) {
812 if( winner->game >= 4) {
813 winner->game = loser->game = 0;
814 winner->sets[s->current_set]++;
815 s->ec_sets++;
817 /* serving is changed when the "game" is over */
818 s->serving_player = (s->serving_player==1)?(2):(1);
820 #ifdef HAVE_VOICE_FILES
821 /* speak the current score */
822 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);
823 #endif
825 /* scoring the set.. */
826 if( (winner->sets[s->current_set] == 6 && loser->sets[s->current_set] < 5) ||
827 winner->sets[s->current_set] == 7) {
828 s->current_set++;
829 s->winner = game_get_winner( s);
834 /* forget this event - we've handled it */
835 s->score_event = SCORE_UNDECIDED;
837 if (winner == &PLAYER(s, 1)) {
838 return WINNER_PLAYER1;
839 } else {
840 return WINNER_PLAYER2;
844 const char* format_sets(const GameState* s) {
845 static char sets[100];
846 static char tmp[100];
847 int i, max = s->current_set;
849 sets[0] = '\0';
851 if( s->winner != WINNER_NONE) {
852 max--;
854 for( i=0; i<=max; i++) {
855 sprintf( tmp, "%d:%d, ", PLAYER(s, 1).sets[i], PLAYER(s, 2).sets[i]);
856 strcat( sets, tmp);
859 sets[strlen(sets)-2] = '\0';
861 return sets;
864 const char* format_game(const GameState* s) {
865 static char game[100];
866 static const int game_scoring[] = { 0, 15, 30, 40 };
868 if( PLAYER(s, 1).game < 4 && PLAYER(s, 2).game < 4) {
869 #ifdef HAVE_VOICE_FILES
870 if (PLAYER(s, 1).game > 0 || PLAYER(s, 2).game > 0) {
871 if (PLAYER(s, 1).game == PLAYER(s, 2).game) {
872 voice_say_list(2, VOICE_LOVE_IN + 2*(PLAYER(s, 1).game), VOICE_ALL);
873 } else {
874 voice_say_list(2, VOICE_LOVE_IN + 2*(PLAYER(s, 1).game), VOICE_LOVE_OUT + 2*(PLAYER(s, 2).game));
877 #endif
878 sprintf( game, "%d - %d", game_scoring[PLAYER(s, 1).game], game_scoring[PLAYER(s, 2).game]);
879 } else if( PLAYER(s, 1).game > PLAYER(s, 2).game) {
880 #ifdef HAVE_VOICE_FILES
881 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_ONE);
882 #endif
883 strcpy( game, "advantage player 1");
884 } else if( PLAYER(s, 1).game < PLAYER(s, 2).game) {
885 #ifdef HAVE_VOICE_FILES
886 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_TWO);
887 #endif
888 strcpy( game, "advantage player 2");
889 } else {
890 #ifdef HAVE_VOICE_FILES
891 voice_say_list(1, VOICE_DEUCE);
892 #endif
893 strcpy( game, "deuce");
896 return game;
899 const char* format_status(const GameState* s) {
900 static char status[100];
901 static const char* set_names[] = { "first", "second", "third", "fourth", "fifth" };
903 switch (s->status_message) {
904 case STATUSMSG_NONE:
905 return "";
906 case STATUSMSG_WELCOME:
907 return "welcome to tennix " VERSION;
908 case STATUSMSG_DEFAULT:
909 sprintf(status, "%d:%d in %s set",
910 PLAYER(s, 1).sets[s->current_set],
911 PLAYER(s, 2).sets[s->current_set],
912 set_names[s->current_set]);
913 return status;
914 case STATUSMSG_NET:
915 return "net!";
916 case STATUSMSG_OUT:
917 return "out!";
918 case STATUSMSG_FAULT:
919 return "fault!";
920 case STATUSMSG_P1SCORES:
921 return "player 1 scores.";
922 case STATUSMSG_P2SCORES:
923 return "player 2 scores.";
924 case STATUSMSG_VOLLEY:
925 return "volley!";
926 case STATUSMSG_DIDNTCATCH:
927 return "did not catch the ball.";
928 default:
929 return "";
933 winner_t game_get_winner(const GameState* s) {
934 unsigned int i;
935 int sets[2] = {0};
937 for( i=0; i<s->current_set; i++) {
938 if( PLAYER(s, 1).sets[i] > PLAYER(s, 2).sets[i]) {
939 sets[0]++;
940 } else {
941 sets[1]++;
945 if( sets[0] == SETS_TO_WIN) return WINNER_PLAYER1;
946 if( sets[1] == SETS_TO_WIN) return WINNER_PLAYER2;
948 return WINNER_NONE;