Add support for marshalling GameState + packed values
[tennix.git] / game.c
blobcd816ab31b0e0d1044370b1ff8659e32f66d37a0
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) {
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 step(s);
215 quit = handle_input(s);
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 void step(GameState* s) {
242 bool ground_event = false;
243 int p;
244 fprintf(stderr, "ball(%f, %f, %f, %f, %f, %f)\n",
245 s->ball.x, s->ball.y, s->ball.z,
246 s->ball.move_x, s->ball.move_y, s->ball.move_z);
248 s->ball.z += s->ball.move_z;
249 if (!s->ball.inhibit_gravity) {
250 s->ball.move_z += GRAVITY;
253 s->ball.x += s->ball.move_x;
254 s->ball.y += s->ball.move_y;
256 for (p=1; p<=MAXPLAYERS; p++) {
257 if (PLAYER(s, p).use_power) {
258 PLAYER(s, p).power = fmaxf(0.0, fminf(
259 PLAYER(s, p).power*POWER_DOWN_FACTOR,
260 PLAYER_POWER_MAX));
264 if (s->ball.z < 0) {
265 /* ground event */
266 ground_event = true;
268 s->referee = REFEREE_NORMAL;
270 /* bounce from the ground */
271 if (fabsf(s->ball.move_z) > 0.3) {
272 s->sound_events ^= SOUND_EVENT_GROUND;
273 s->ball.move_z *= -BALL_RESTITUTION;
274 } else {
275 s->ball.move_z = 0;
277 s->ball.z = 0;
280 if (NET_COLLISION_BALL(s->ball)) {
281 /* the net blocks movement of the ball */
282 while (NET_COLLISION_BALL(s->ball)) {
283 /* make sure the ball appears OUTSIDE of the net */
284 if (s->ball.move_x < 0) {
285 s->ball.x += 1;
286 } else {
287 s->ball.x -= 1;
290 s->ball.move_x = 0;
291 s->ball.move_y = 0;
294 /* see if we have something to score */
295 if (s->score_event == SCORE_UNDECIDED) {
296 if (NET_COLLISION_BALL(s->ball)) {
297 /* the ball "fell" into the net */
298 s->score_event = SCORE_EVENT_NET;
299 s->sound_events ^= SOUND_EVENT_OUT;
300 s->status_message = STATUSMSG_NET;
301 } else if (IS_OFFSCREEN(s->ball.x, s->ball.y)) {
302 /* ball flew offscreen */
303 s->score_event = SCORE_EVENT_OFFSCREEN;
304 s->sound_events ^= SOUND_EVENT_OUT;
305 s->status_message = STATUSMSG_OUT;
306 } else if (ground_event) {
307 /* the ball hit the ground on the screen */
308 if (IS_OUT(s->ball.x, s->ball.y)) {
309 /* the ball bounced in the OUT area */
310 s->score_event = SCORE_EVENT_OUT;
311 s->sound_events ^= SOUND_EVENT_OUT;
312 s->status_message = STATUSMSG_OUT;
313 } else if (GROUND_IS_VALID(s->ball.last_hit_by, s->ball.x, s->ball.y)) {
314 if (s->ball.ground_hit) {
315 s->score_event = SCORE_EVENT_GROUND_VALID;
316 s->sound_events ^= SOUND_EVENT_OUT;
317 s->status_message = STATUSMSG_DIDNTCATCH;
318 } else {
319 /* first ground hit in valid area */
320 s->ball.ground_hit = true;
322 } else {
323 /* ball hit somewhere invalid */
324 s->score_event = SCORE_EVENT_GROUND_INVALID;
325 s->sound_events ^= SOUND_EVENT_OUT;
326 s->status_message = STATUSMSG_FAULT;
331 if (s->score_event != SCORE_UNDECIDED) {
332 /* we have some scoring to do */
333 if (s->score_time < SCORING_DELAY/GAME_TICKS) {
334 /* schedule scoring in the future */
335 s->score_time++;
336 s->referee = REFEREE_OUT;
337 } else if (s->score_time >= SCORING_DELAY/GAME_TICKS) {
338 /* time has ran out - score now */
339 switch (score_game(s)) {
340 case WINNER_PLAYER1:
341 s->status_message = STATUSMSG_P1SCORES;
342 s->referee = REFEREE_PLAYER1;
343 break;
344 case WINNER_PLAYER2:
345 s->status_message = STATUSMSG_P2SCORES;
346 s->referee = REFEREE_PLAYER2;
347 break;
348 default:
349 assert(0);
350 break;
352 s->score_time = 0;
354 game_setup_serve(s);
355 if (s->location->max_visitors > 100) {
356 s->sound_events ^= SOUND_EVENT_APPLAUSE;
359 } else {
360 /* score is still undecided - do the racket swing thing */
361 for (p=1; p<=2; p++) {
362 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) {
363 /* RACKET HIT */
364 if (!s->ball.ground_hit && s->ball.move_x != 0.0) {
365 s->status_message = STATUSMSG_VOLLEY;
366 } else {
367 s->status_message = STATUSMSG_DEFAULT;
369 switch (PLAYER(s, p).desire) {
370 case DESIRE_NORMAL:
371 /* normal swing */
372 s->ball.move_x = 2.7 + 2.0*PLAYER(s, p).power/PLAYER_POWER_MAX;
373 s->ball.move_z = 1.2*PLAYER(s, p).power/PLAYER_POWER_MAX;
374 break;
375 case DESIRE_TOPSPIN:
376 /* top spin */
377 s->ball.move_x = 1.1 + 2.2*PLAYER(s, p).power/PLAYER_POWER_MAX;
378 s->ball.move_z = 2.5*PLAYER(s, p).power/PLAYER_POWER_MAX;
379 break;
380 case DESIRE_SMASH:
381 /* smash */
382 s->ball.move_x = 4.0 + 3.0*PLAYER(s, p).power/PLAYER_POWER_MAX;
383 s->ball.move_z = 1.1*PLAYER(s, p).power/PLAYER_POWER_MAX;
384 break;
386 s->ball.move_y = get_move_y( s, p);
387 s->sound_events ^= SOUND_EVENT_RACKET;
388 s->ball.ground_hit = false;
389 s->ball.inhibit_gravity = false;
390 s->ball.last_hit_by = p;
391 if (p==2) {
392 s->ball.move_x *= -1;
399 bool handle_input(GameState* s) {
400 static NetworkGameState tmp;
401 Uint8* keys = NULL;
402 int p;
404 SDL_PumpEvents();
405 keys = SDL_GetKeyState(NULL);
407 if (keys['1']) {
408 net_serialize_gamestate(s, &tmp);
409 } else if (keys['2']) {
410 net_unserialize_gamestate(&tmp, s);
413 if (s->winner == WINNER_NONE) {
414 for (p=1; p<=MAXPLAYERS; p++) {
415 if( PLAYER(s, p).type == PLAYER_TYPE_HUMAN) {
416 input_human(s, p);
417 } else {
418 input_ai(s, p);
421 /* Make sure player coordinates are valid */
422 if (PLAYER(s, p).y < PLAYER_Y_MIN) {
423 PLAYER(s, p).y = PLAYER_Y_MIN;
424 } else if (PLAYER(s, p).y > PLAYER_Y_MAX) {
425 PLAYER(s, p).y = PLAYER_Y_MAX;
430 /* Maemo: The "F4" button is the "Open menu" button */
431 return (keys[SDLK_ESCAPE] || keys['q']);
434 void render(const GameState* s, RenderState* r) {
435 int x, y, b;
436 unsigned int i;
437 float zoom;
438 float rotate;
439 int t=1000;
440 soundevent_t sounds;
441 Uint32 time = SDL_GetTicks();
443 /* The bits in sound_events flip when the sound should play */
444 if ((sounds = (r->sound_events ^ s->sound_events)) != 0) {
445 if (sounds & SOUND_EVENT_GROUND) {
446 sample_volume_group(SOUND_GROUND_FIRST, SOUND_GROUND_LAST,
447 fmaxf(0.0, fminf(1.0, fabsf(s->ball.move_z)/2)));
448 pan_sample_group(SOUND_GROUND_FIRST, SOUND_GROUND_LAST,
449 fmaxf(0.0, fminf(1.0, (s->ball.x)/WIDTH)));
450 play_sample(SOUND_GROUND);
452 if (sounds & SOUND_EVENT_OUT) {
453 play_sample(SOUND_OUT);
455 if (sounds & SOUND_EVENT_APPLAUSE) {
456 play_sample(SOUND_APPLAUSE);
458 if (sounds & SOUND_EVENT_RACKET) {
459 if (((s->ball.x)/WIDTH) < 0.5) {
460 pan_sample_group(SOUND_RACKET_FIRST, SOUND_RACKET_LAST, 0.3);
461 } else {
462 pan_sample_group(SOUND_RACKET_FIRST, SOUND_RACKET_LAST, 0.7);
464 play_sample(SOUND_RACKET);
466 r->sound_events = s->sound_events;
469 if( s->winner != WINNER_NONE) {
470 clear_screen();
471 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
472 store_screen();
473 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);
474 /*sprintf( s->game_score_str, "player %d wins the match with %s", s->winner, format_sets( s));
475 font_draw_string(FONT_LARGE, s->game_score_str, (WIDTH-font_get_string_width(FONT_LARGE, s->game_score_str))/2, HEIGHT/2 + 30);*/
476 updatescr();
477 return;
479 if ((r->referee != s->referee) ||
480 (r->ec_game != s->ec_game) ||
481 (r->ec_sets != s->ec_sets) ||
482 (r->status_message != s->status_message)) {
483 /* Update status message text */
484 if (r->status_message != s->status_message) {
485 r->text_status = format_status(s);
486 r->status_message = s->status_message;
488 /* Update game status text */
489 if (r->ec_game != s->ec_game) {
490 r->text_game = format_game(s);
491 r->ec_game = s->ec_game;
493 /* Update set status text */
494 if (r->ec_sets != s->ec_sets) {
495 r->text_sets = format_sets(s);
496 if (r->status_message == STATUSMSG_DEFAULT) {
497 r->text_status = format_status(s);
499 r->ec_sets = s->ec_sets;
501 clear_screen();
502 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
503 fill_image(s->location->court_type, 120, 120, 400, 250);
504 show_image(GR_COURT, 0, 0, 255);
505 font_draw_string(FONT_XLARGE, r->text_game, 14, 14);
506 font_draw_string(FONT_XLARGE, r->text_sets,
507 (WIDTH-font_get_string_width(FONT_XLARGE, r->text_sets))-14, 14);
508 if (s->location->has_referee) {
509 switch (s->referee) {
510 case REFEREE_NORMAL:
511 t = 1000;
512 break;
513 case REFEREE_OUT:
514 t = 200;
515 break;
516 case REFEREE_PLAYER1:
517 case REFEREE_PLAYER2:
518 t = 400;
519 break;
521 t = (time/t)%4;
522 switch (t) {
523 case 0:
524 t=0;
525 break;
526 case 1:
527 t=1;
528 break;
529 case 2:
530 t=0;
531 break;
532 case 3:
533 t=2;
534 break;
536 show_sprite( GR_REFEREE, s->referee*3+t, 12, 250, 10, 255);
537 if (voice_finished_flag == 0) {
538 show_sprite(GR_TALK, (time/150)%2, 2, 280, 45, 255);
541 r->referee = s->referee;
542 store_screen();
545 /* show_sprite( GR_RACKET, (!PLAYER(s, 1).state), 4, PLAYER(s, 1).x-RACKET_X_MID, PLAYER(s, 1).y-RACKET_Y_MID, 255);
546 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);*/
547 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);
548 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);
550 rectangle(10, HEIGHT-30, PLAYER_POWER_MAX, 10, 50, 50, 50);
551 rectangle(WIDTH-PLAYER_POWER_MAX-10, HEIGHT-30, PLAYER_POWER_MAX, 10, 50, 50, 50);
553 rectangle(10, HEIGHT-30, (int)(PLAYER(s, 1).power), 10, 200, 200, 200);
554 rectangle(WIDTH-PLAYER_POWER_MAX-10, HEIGHT-30, (int)(PLAYER(s, 2).power), 10, 200, 200, 200);
556 if( s->ball.move_x > 0) {
557 b = (time/100)%BALL_STATES;
558 } else if( s->ball.move_x < 0) {
559 b = BALL_STATES-1-(time/100)%BALL_STATES;
560 } else {
561 b = 0;
564 rotate = 0.0;
565 zoom = fmaxf(0.5, fminf(1.0, (float)(30.-(s->ball.z))/10.));
566 show_image_rotozoom(GR_SHADOW, s->ball.x, s->ball.y+3, rotate, zoom);
568 rotate = (-s->ball.move_x * (float)((float)time/10.));
569 zoom = 1.0 + fmaxf(0.0, (s->ball.z-10.)/100.);
570 show_image_rotozoom(GR_BALL, s->ball.x, s->ball.y - s->ball.z, rotate, zoom);
572 /* Player 1's mouse rectangle */
573 if (PLAYER(s, 1).input != NULL && PLAYER(s, 1).input->type == INPUT_TYPE_MOUSE) {
574 rectangle(PLAYER(s, 1).x-2+5, PLAYER(s, 1).input->my-2, 4, 4, 255, 255, 255);
575 rectangle(PLAYER(s, 1).x-1+5, PLAYER(s, 1).input->my-1, 2, 2, 0, 0, 0);
578 /* Player 2's mouse rectangle */
579 if (PLAYER(s, 2).input != NULL && PLAYER(s, 2).input->type == INPUT_TYPE_MOUSE) {
580 rectangle(PLAYER(s, 2).x-2+5, PLAYER(s, 2).input->my-2, 4, 4, 255, 255, 255);
581 rectangle(PLAYER(s, 2).x-1+5, PLAYER(s, 2).input->my-1, 2, 2, 0, 0, 0);
584 font_draw_string(FONT_MEDIUM, r->text_status,
585 (WIDTH-font_get_string_width(FONT_MEDIUM, r->text_status))/2, HEIGHT-50);
587 for (i=0; i<s->location->rain; i++) {
588 x = rand()%WIDTH;
589 y = rand()%HEIGHT;
590 draw_line_faded(x, y, x+10, y+30, 0, 0, 255, 100, 200, 255);
592 if (s->location->rain) {
594 * Cheap-ish update of the whole screen. This can
595 * probably be optimized.
597 update_rect(0, 0, WIDTH, HEIGHT);
600 #ifdef DEBUG
601 line_horiz( PLAYER(s, 1).y, 255, 0, 0);
602 line_horiz( PLAYER(s, 2).y, 0, 0, 255);
603 line_horiz( s->ball.y, 0, 255, 0);
605 line_vert( PLAYER(s, 1).x, 255, 0, 0);
606 line_vert( PLAYER(s, 2).x, 0, 0, 255);
607 line_vert( s->ball.x, 0, 255, 0);
609 line_horiz( GAME_Y_MIN, 100, 100, 100);
610 line_horiz( GAME_Y_MAX, 100, 100, 100);
611 #endif
612 switch (s->location->fog) {
613 default:
614 case 4:
615 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -time/150, 0);
616 case 3:
617 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, -time/100, 20);
618 case 2:
619 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -time/180, 80);
620 case 1:
621 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, time/200, 0);
622 case 0:
623 break;
625 if (s->location->night) {
626 show_image(GR_NIGHT, 0, 0, 255);
629 updatescr();
632 float get_move_y( GameState* s, unsigned char player) {
633 float pct, dest, x_len, y_len;
634 float py, by, pa, move_x;
636 py = (player==1)?(PLAYER(s, 1).y):(PLAYER(s, 2).y);
637 by = s->ball.y - s->ball.z;
638 pa = RACKET_Y_MID*2;
639 move_x = s->ball.move_x;
641 /* -1.0 .. 1.0 for racket hit position */
642 pct = fmaxf(-1.0, fminf(1.0, (by-py)/(pa/2)));
644 /* Y destination for ball */
645 dest = GAME_Y_MID + pct*(GAME_Y_MAX-GAME_Y_MIN);
647 /* lengths for the ball's journey */
648 if( player == 1) {
649 x_len = GAME_X_MAX - s->ball.x;
650 } else {
651 x_len = s->ball.x - GAME_X_MIN;
653 y_len = dest - by;
655 /* return the should-be value for move_y */
656 return (y_len*move_x)/(x_len);
659 void input_human(GameState* s, int player) {
660 bool hit, topspin, smash;
661 float move_y;
663 /* For mouse input, hand the player coordinates to the InputDevice */
664 if (PLAYER(s, player).input->type == INPUT_TYPE_MOUSE) {
665 PLAYER(s, player).input->player_x = (int)(PLAYER(s, player).x);
666 PLAYER(s, player).input->player_y = (int)(PLAYER(s, player).y);
669 move_y = PLAYER_MOVE_Y*input_device_get_axis(PLAYER(s, player).input, INPUT_AXIS_Y);
671 hit = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_HIT);
672 topspin = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_TOPSPIN);
673 smash = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_SMASH);
675 if (move_y != 0) {
676 if (fabsf(move_y) > fabsf(move_y*PLAYER(s, player).accelerate)) {
677 move_y *= PLAYER(s, player).accelerate;
679 PLAYER(s, player).y += move_y;
680 PLAYER(s, player).accelerate *= PLAYER_ACCEL_INCREASE;
681 } else {
682 PLAYER(s, player).accelerate = PLAYER_ACCEL_DEFAULT;
685 if(hit || topspin || smash) {
686 PLAYER(s, player).desire = (topspin)?(DESIRE_TOPSPIN):(DESIRE_NORMAL);
687 PLAYER(s, player).desire = (smash)?(DESIRE_SMASH):(PLAYER(s, player).desire);
689 PLAYER(s, player).power = fmaxf(10.0, fminf(PLAYER(s, player).power*POWER_UP_FACTOR, PLAYER_POWER_MAX));
690 PLAYER(s, player).use_power = false;
691 } else {
692 PLAYER(s, player).use_power = true;
696 void input_ai(GameState* s, int player) {
697 float fact = 1.7;
698 int ball_approaching = 0;
700 if ((PLAYER(s, player).x < GAME_X_MID && s->ball.move_x <= 0) ||
701 (PLAYER(s, player).x > GAME_X_MID && s->ball.move_x >= 0)) {
702 ball_approaching = 1;
705 /* FIXME - this is broken since the new physics model has been introduced */
707 if (fabsf(PLAYER(s, player).y - (s->ball.y-s->ball.z)) > RACKET_Y_MID*5) {
708 fact = 3.5;
711 if(1) {
712 if( PLAYER(s, player).desire == DESIRE_NORMAL && !IS_NEAR_Y_AI(PLAYER(s, player).y, (s->ball.y-s->ball.z)) && ball_approaching) {
713 if( PLAYER(s, player).y < (s->ball.y-s->ball.z)) {
714 PLAYER(s, player).y += fminf(2*fact, (s->ball.y-s->ball.z) - PLAYER(s, player).y);
715 } else if( PLAYER(s, player).y > (s->ball.y-s->ball.z)) {
716 PLAYER(s, player).y -= fminf(2*fact, PLAYER(s, player).y - (s->ball.y-s->ball.z));
720 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.) {
721 PLAYER(s, player).use_power = true;
722 } else if (ball_approaching) {
723 PLAYER(s, player).power = fmaxf(10., fminf(PLAYER(s, player).power*POWER_UP_FACTOR, PLAYER_POWER_MAX));
724 PLAYER(s, player).use_power = false;
729 void game_setup_serve( GameState* s) {
730 s->ball.z = 10.0;
731 s->ball.y = GAME_Y_MID;
732 s->ball.move_x = 0.0;
733 s->ball.move_y = 0.0;
734 s->ball.move_z = 0.0;
735 s->ball.last_hit_by = -1;
736 s->ball.inhibit_gravity = true;
738 if( s->serving_player == 1) {
739 s->ball.x = GAME_X_MIN-RACKET_X_MID*1.5;
740 } else {
741 s->ball.x = GAME_X_MAX+RACKET_X_MID*1.5;
745 int score_game(GameState* s) {
746 Player *last_hit_by = NULL;
747 Player *other = NULL;
749 Player* winner = NULL;
750 Player* loser = NULL;
752 /* determine "last hit by" and "other" */
753 if (s->ball.last_hit_by == 1) {
754 last_hit_by = &(PLAYER(s, 1));
755 other = &(PLAYER(s, 2));
756 } else {
757 last_hit_by = &(PLAYER(s, 2));
758 other = &(PLAYER(s, 1));
761 switch (s->score_event) {
762 case SCORE_EVENT_NET:
763 winner = other;
764 break;
765 case SCORE_EVENT_OUT:
766 case SCORE_EVENT_GROUND_INVALID:
767 case SCORE_EVENT_OFFSCREEN:
768 case SCORE_EVENT_GROUND_VALID:
769 if (s->ball.ground_hit) {
770 winner = last_hit_by;
771 } else {
772 winner = other;
774 break;
775 default:
776 break;
779 /* determine loser based on winner */
780 if (winner == last_hit_by) {
781 loser = other;
782 } else {
783 loser = last_hit_by;
786 /* we cannot be in an "impossibly high" set */
787 assert(s->current_set < SETS_TO_WIN*2-1);
789 winner->game++;
790 s->ec_game++;
791 if( loser->game < winner->game-1) {
792 if( winner->game >= 4) {
793 winner->game = loser->game = 0;
794 winner->sets[s->current_set]++;
795 s->ec_sets++;
797 /* serving is changed when the "game" is over */
798 s->serving_player = (s->serving_player==1)?(2):(1);
800 #ifdef HAVE_VOICE_FILES
801 /* speak the current score */
802 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);
803 #endif
805 /* scoring the set.. */
806 if( (winner->sets[s->current_set] == 6 && loser->sets[s->current_set] < 5) ||
807 winner->sets[s->current_set] == 7) {
808 s->current_set++;
809 s->winner = game_get_winner( s);
814 /* forget this event - we've handled it */
815 s->score_event = SCORE_UNDECIDED;
817 if (winner == &PLAYER(s, 1)) {
818 return WINNER_PLAYER1;
819 } else {
820 return WINNER_PLAYER2;
824 const char* format_sets(const GameState* s) {
825 static char sets[100];
826 static char tmp[100];
827 int i, max = s->current_set;
829 sets[0] = '\0';
831 if( s->winner != WINNER_NONE) {
832 max--;
834 for( i=0; i<=max; i++) {
835 sprintf( tmp, "%d:%d, ", PLAYER(s, 1).sets[i], PLAYER(s, 2).sets[i]);
836 strcat( sets, tmp);
839 sets[strlen(sets)-2] = '\0';
841 return sets;
844 const char* format_game(const GameState* s) {
845 static char game[100];
846 static const int game_scoring[] = { 0, 15, 30, 40 };
848 if( PLAYER(s, 1).game < 4 && PLAYER(s, 2).game < 4) {
849 #ifdef HAVE_VOICE_FILES
850 if (PLAYER(s, 1).game > 0 || PLAYER(s, 2).game > 0) {
851 if (PLAYER(s, 1).game == PLAYER(s, 2).game) {
852 voice_say_list(2, VOICE_LOVE_IN + 2*(PLAYER(s, 1).game), VOICE_ALL);
853 } else {
854 voice_say_list(2, VOICE_LOVE_IN + 2*(PLAYER(s, 1).game), VOICE_LOVE_OUT + 2*(PLAYER(s, 2).game));
857 #endif
858 sprintf( game, "%d - %d", game_scoring[PLAYER(s, 1).game], game_scoring[PLAYER(s, 2).game]);
859 } else if( PLAYER(s, 1).game > PLAYER(s, 2).game) {
860 #ifdef HAVE_VOICE_FILES
861 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_ONE);
862 #endif
863 strcpy( game, "advantage player 1");
864 } else if( PLAYER(s, 1).game < PLAYER(s, 2).game) {
865 #ifdef HAVE_VOICE_FILES
866 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_TWO);
867 #endif
868 strcpy( game, "advantage player 2");
869 } else {
870 #ifdef HAVE_VOICE_FILES
871 voice_say_list(1, VOICE_DEUCE);
872 #endif
873 strcpy( game, "deuce");
876 return game;
879 const char* format_status(const GameState* s) {
880 static char status[100];
881 static const char* set_names[] = { "first", "second", "third", "fourth", "fifth" };
883 switch (s->status_message) {
884 case STATUSMSG_NONE:
885 return "";
886 case STATUSMSG_WELCOME:
887 return "welcome to tennix " VERSION;
888 case STATUSMSG_DEFAULT:
889 sprintf(status, "%d:%d in %s set",
890 PLAYER(s, 1).sets[s->current_set],
891 PLAYER(s, 2).sets[s->current_set],
892 set_names[s->current_set]);
893 return status;
894 case STATUSMSG_NET:
895 return "net!";
896 case STATUSMSG_OUT:
897 return "out!";
898 case STATUSMSG_FAULT:
899 return "fault!";
900 case STATUSMSG_P1SCORES:
901 return "player 1 scores.";
902 case STATUSMSG_P2SCORES:
903 return "player 2 scores.";
904 case STATUSMSG_VOLLEY:
905 return "volley!";
906 case STATUSMSG_DIDNTCATCH:
907 return "did not catch the ball.";
908 default:
909 return "";
913 winner_t game_get_winner(const GameState* s) {
914 unsigned int i;
915 int sets[2] = {0};
917 for( i=0; i<s->current_set; i++) {
918 if( PLAYER(s, 1).sets[i] > PLAYER(s, 2).sets[i]) {
919 sets[0]++;
920 } else {
921 sets[1]++;
925 if( sets[0] == SETS_TO_WIN) return WINNER_PLAYER1;
926 if( sets[1] == SETS_TO_WIN) return WINNER_PLAYER2;
928 return WINNER_NONE;