Remove dependency on local state in step()
[tennix.git] / game.c
blob06941f5d65943f5f8fdf8d662805a8995422c003
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 sample_volume_group(SOUND_GROUND_FIRST, SOUND_GROUND_LAST, fmaxf(0.0, fminf(1.0, fabsf(s->ball.move_z)/2)));
256 pan_sample_group(SOUND_GROUND_FIRST, SOUND_GROUND_LAST, fmaxf(0.0, fminf(1.0, (s->ball.x)/WIDTH)));
257 s->ball.move_z *= -s->ball.restitution;
258 } else {
259 s->ball.move_z = 0;
261 s->ball.z = 0;
264 if (NET_COLLISION_BALL(s->ball)) {
265 /* the net blocks movement of the ball */
266 while (NET_COLLISION_BALL(s->ball)) {
267 /* make sure the ball appears OUTSIDE of the net */
268 if (s->ball.move_x < 0) {
269 s->ball.x += 1;
270 } else {
271 s->ball.x -= 1;
274 s->ball.move_x = 0;
275 s->ball.move_y = 0;
278 /* see if we have something to score */
279 if (s->score_event == SCORE_UNDECIDED) {
280 if (NET_COLLISION_BALL(s->ball)) {
281 /* the ball "fell" into the net */
282 s->score_event = SCORE_EVENT_NET;
283 s->sound_events ^= SOUND_EVENT_OUT;
284 s->status_message = STATUSMSG_NET;
285 } else if (IS_OFFSCREEN(s->ball.x, s->ball.y)) {
286 /* ball flew offscreen */
287 s->score_event = SCORE_EVENT_OFFSCREEN;
288 s->sound_events ^= SOUND_EVENT_OUT;
289 s->status_message = STATUSMSG_OUT;
290 } else if (ground_event) {
291 /* the ball hit the ground on the screen */
292 if (IS_OUT(s->ball.x, s->ball.y)) {
293 /* the ball bounced in the OUT area */
294 s->score_event = SCORE_EVENT_OUT;
295 s->sound_events ^= SOUND_EVENT_OUT;
296 s->status_message = STATUSMSG_OUT;
297 } else if (GROUND_IS_VALID(s->ball.last_hit_by, s->ball.x, s->ball.y)) {
298 if (s->ball.ground_hit) {
299 s->score_event = SCORE_EVENT_GROUND_VALID;
300 s->sound_events ^= SOUND_EVENT_OUT;
301 s->status_message = STATUSMSG_DIDNTCATCH;
302 } else {
303 /* first ground hit in valid area */
304 s->ball.ground_hit = true;
306 } else {
307 /* ball hit somewhere invalid */
308 s->score_event = SCORE_EVENT_GROUND_INVALID;
309 s->sound_events ^= SOUND_EVENT_OUT;
310 s->status_message = STATUSMSG_FAULT;
315 if (s->score_event != SCORE_UNDECIDED) {
316 /* we have some scoring to do */
317 if (s->score_time == 0) {
318 /* schedule scoring in the future */
319 s->score_time = s->time + SCORING_DELAY;
320 s->referee = REFEREE_OUT;
321 } else if (s->time >= s->score_time) {
322 /* time has ran out - score now */
323 switch (score_game(s)) {
324 case WINNER_PLAYER1:
325 s->status_message = STATUSMSG_P1SCORES;
326 s->referee = REFEREE_PLAYER1;
327 break;
328 case WINNER_PLAYER2:
329 s->status_message = STATUSMSG_P2SCORES;
330 s->referee = REFEREE_PLAYER2;
331 break;
332 default:
333 assert(0);
334 break;
336 s->score_time = 0;
338 game_setup_serve(s);
339 if (s->location->max_visitors > 100) {
340 s->sound_events ^= SOUND_EVENT_APPLAUSE;
343 } else {
344 /* score is still undecided - do the racket swing thing */
345 for (p=1; p<=2; p++) {
346 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) {
347 /* RACKET HIT */
348 if (!s->ball.ground_hit && s->ball.move_x != 0.0) {
349 s->status_message = STATUSMSG_VOLLEY;
350 } else {
351 s->status_message = STATUSMSG_DEFAULT;
353 switch (PLAYER(s, p).desire) {
354 case DESIRE_NORMAL:
355 /* normal swing */
356 s->ball.move_x = 2.7 + 2.0*PLAYER(s, p).power/PLAYER_POWER_MAX;
357 s->ball.move_z = 1.2*PLAYER(s, p).power/PLAYER_POWER_MAX;
358 break;
359 case DESIRE_TOPSPIN:
360 /* top spin */
361 s->ball.move_x = 1.1 + 2.2*PLAYER(s, p).power/PLAYER_POWER_MAX;
362 s->ball.move_z = 2.5*PLAYER(s, p).power/PLAYER_POWER_MAX;
363 break;
364 case DESIRE_SMASH:
365 /* smash */
366 s->ball.move_x = 4.0 + 3.0*PLAYER(s, p).power/PLAYER_POWER_MAX;
367 s->ball.move_z = 1.1*PLAYER(s, p).power/PLAYER_POWER_MAX;
368 break;
370 s->ball.move_y = get_move_y( s, p);
371 s->sound_events ^= SOUND_EVENT_RACKET;
372 s->ball.ground_hit = false;
373 s->ball.inhibit_gravity = false;
374 s->ball.last_hit_by = p;
375 if (p==1) {
376 pan_sample_group(SOUND_RACKET_FIRST, SOUND_RACKET_LAST, 0.3);
377 } else {
378 pan_sample_group(SOUND_RACKET_FIRST, SOUND_RACKET_LAST, 0.7);
379 s->ball.move_x *= -1;
385 SDL_PumpEvents();
386 keys = SDL_GetKeyState(NULL);
388 if(s->winner == WINNER_NONE) {
389 for (p=1; p<=MAXPLAYERS; p++) {
390 if( PLAYER(s, p).type == PLAYER_TYPE_HUMAN) {
391 input_human(s, p);
392 } else {
393 input_ai(s, p);
398 /* Maemo: The "F6" button is the "Fullscreen" button */
399 /*if( keys['f'] || keys[SDLK_F6]) SDL_WM_ToggleFullScreen( screen);*/
400 if( keys['y']) SDL_SaveBMP( screen, "screenshot.bmp");
402 /* Maemo: The "F4" button is the "Open menu" button */
403 if(keys[SDLK_ESCAPE] || keys['q']
404 || keys['p'] || keys[SDLK_F4]) {
405 return true;
408 limit_value( &PLAYER(s, 1).y, PLAYER_Y_MIN, PLAYER_Y_MAX);
409 limit_value( &PLAYER(s, 2).y, PLAYER_Y_MIN, PLAYER_Y_MAX);
411 s->ball.z += s->ball.move_z;
412 if (!s->ball.inhibit_gravity) {
413 s->ball.move_z += GRAVITY;
416 s->ball.x += s->ball.move_x;
417 s->ball.y += s->ball.move_y;
419 for (p=1; p<=MAXPLAYERS; p++) {
420 if (PLAYER(s, p).use_power) {
421 PLAYER(s, p).power = fmaxf(0.0, fminf(PLAYER(s, p).power*PLAYER(s, p).power_down_factor, PLAYER_POWER_MAX));
425 return false;
428 void render(const GameState* s, RenderState* r) {
429 int x, y, b;
430 unsigned int i;
431 float zoom;
432 float rotate;
433 int t=1000;
434 soundevent_t sounds;
436 /* The bits in sound_events flip when the sound should play */
437 if ((sounds = (r->sound_events ^ s->sound_events)) != 0) {
438 if (sounds & SOUND_EVENT_GROUND) {
439 play_sample(SOUND_GROUND);
441 if (sounds & SOUND_EVENT_OUT) {
442 play_sample(SOUND_OUT);
444 if (sounds & SOUND_EVENT_APPLAUSE) {
445 play_sample(SOUND_APPLAUSE);
447 if (sounds & SOUND_EVENT_RACKET) {
448 play_sample(SOUND_RACKET);
450 r->sound_events = s->sound_events;
453 if( s->winner != WINNER_NONE) {
454 clear_screen();
455 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
456 store_screen();
457 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);
458 /*sprintf( s->game_score_str, "player %d wins the match with %s", s->winner, format_sets( s));
459 font_draw_string(FONT_LARGE, s->game_score_str, (WIDTH-font_get_string_width(FONT_LARGE, s->game_score_str))/2, HEIGHT/2 + 30);*/
460 updatescr();
461 return;
463 if ((r->referee != s->referee) ||
464 (r->ec_game != s->ec_game) ||
465 (r->ec_sets != s->ec_sets) ||
466 (r->status_message != s->status_message)) {
467 /* Update status message text */
468 if (r->status_message != s->status_message) {
469 r->text_status = format_status(s);
470 r->status_message = s->status_message;
472 /* Update game status text */
473 if (r->ec_game != s->ec_game) {
474 r->text_game = format_game(s);
475 r->ec_game = s->ec_game;
477 /* Update set status text */
478 if (r->ec_sets != s->ec_sets) {
479 r->text_sets = format_sets(s);
480 if (r->status_message == STATUSMSG_DEFAULT) {
481 r->text_status = format_status(s);
483 r->ec_sets = s->ec_sets;
485 clear_screen();
486 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
487 fill_image(s->location->court_type, 120, 120, 400, 250);
488 show_image(GR_COURT, 0, 0, 255);
489 font_draw_string(FONT_XLARGE, r->text_game, 14, 14);
490 font_draw_string(FONT_XLARGE, r->text_sets,
491 (WIDTH-font_get_string_width(FONT_XLARGE, r->text_sets))-14, 14);
492 if (s->location->has_referee) {
493 switch (s->referee) {
494 case REFEREE_NORMAL:
495 t = 1000;
496 break;
497 case REFEREE_OUT:
498 t = 200;
499 break;
500 case REFEREE_PLAYER1:
501 case REFEREE_PLAYER2:
502 t = 400;
503 break;
505 t = (s->time/t)%4;
506 switch (t) {
507 case 0:
508 t=0;
509 break;
510 case 1:
511 t=1;
512 break;
513 case 2:
514 t=0;
515 break;
516 case 3:
517 t=2;
518 break;
520 show_sprite( GR_REFEREE, s->referee*3+t, 12, 250, 10, 255);
521 if (voice_finished_flag == 0) {
522 show_sprite(GR_TALK, (s->time/150)%2, 2, 280, 45, 255);
525 r->referee = s->referee;
526 store_screen();
529 /* show_sprite( GR_RACKET, (!PLAYER(s, 1).state), 4, PLAYER(s, 1).x-RACKET_X_MID, PLAYER(s, 1).y-RACKET_Y_MID, 255);
530 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);*/
531 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);
532 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);
534 rectangle(10, HEIGHT-30, PLAYER_POWER_MAX, 10, 50, 50, 50);
535 rectangle(WIDTH-PLAYER_POWER_MAX-10, HEIGHT-30, PLAYER_POWER_MAX, 10, 50, 50, 50);
537 rectangle(10, HEIGHT-30, (int)(PLAYER(s, 1).power), 10, 200, 200, 200);
538 rectangle(WIDTH-PLAYER_POWER_MAX-10, HEIGHT-30, (int)(PLAYER(s, 2).power), 10, 200, 200, 200);
540 if( s->ball.move_x > 0) {
541 b = (s->time/100)%BALL_STATES;
542 } else if( s->ball.move_x < 0) {
543 b = BALL_STATES-1-(s->time/100)%BALL_STATES;
544 } else {
545 b = 0;
548 rotate = 0.0;
549 zoom = fmaxf(0.5, fminf(1.0, (float)(30.-(s->ball.z))/10.));
550 show_image_rotozoom(GR_SHADOW, s->ball.x, s->ball.y+3, rotate, zoom);
552 rotate = (-s->ball.move_x * (float)((float)s->time/10.));
553 zoom = 1.0 + fmaxf(0.0, (s->ball.z-10.)/100.);
554 show_image_rotozoom(GR_BALL, s->ball.x, s->ball.y - s->ball.z, rotate, zoom);
556 /* Player 1's mouse rectangle */
557 if (PLAYER(s, 1).input != NULL && PLAYER(s, 1).input->type == INPUT_TYPE_MOUSE) {
558 rectangle(PLAYER(s, 1).x-2+5, PLAYER(s, 1).input->my-2, 4, 4, 255, 255, 255);
559 rectangle(PLAYER(s, 1).x-1+5, PLAYER(s, 1).input->my-1, 2, 2, 0, 0, 0);
562 /* Player 2's mouse rectangle */
563 if (PLAYER(s, 2).input != NULL && PLAYER(s, 2).input->type == INPUT_TYPE_MOUSE) {
564 rectangle(PLAYER(s, 2).x-2+5, PLAYER(s, 2).input->my-2, 4, 4, 255, 255, 255);
565 rectangle(PLAYER(s, 2).x-1+5, PLAYER(s, 2).input->my-1, 2, 2, 0, 0, 0);
568 font_draw_string(FONT_MEDIUM, r->text_status,
569 (WIDTH-font_get_string_width(FONT_MEDIUM, r->text_status))/2, HEIGHT-50);
571 for (i=0; i<s->location->rain; i++) {
572 x = rand()%WIDTH;
573 y = rand()%HEIGHT;
574 draw_line_faded(x, y, x+10, y+30, 0, 0, 255, 100, 200, 255);
576 if (s->location->rain) {
578 * Cheap-ish update of the whole screen. This can
579 * probably be optimized.
581 update_rect(0, 0, WIDTH, HEIGHT);
584 #ifdef DEBUG
585 line_horiz( PLAYER(s, 1).y, 255, 0, 0);
586 line_horiz( PLAYER(s, 2).y, 0, 0, 255);
587 line_horiz( s->ball.y, 0, 255, 0);
589 line_vert( PLAYER(s, 1).x, 255, 0, 0);
590 line_vert( PLAYER(s, 2).x, 0, 0, 255);
591 line_vert( s->ball.x, 0, 255, 0);
593 line_horiz( GAME_Y_MIN, 100, 100, 100);
594 line_horiz( GAME_Y_MAX, 100, 100, 100);
595 #endif
596 switch (s->location->fog) {
597 default:
598 case 4:
599 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -s->time/150, 0);
600 case 3:
601 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, -s->time/100, 20);
602 case 2:
603 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -s->time/180, 80);
604 case 1:
605 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, s->time/200, 0);
606 case 0:
607 break;
609 if (s->location->night) {
610 show_image(GR_NIGHT, 0, 0, 255);
613 updatescr();
616 void limit_value( float* value, float min, float max) {
617 if( *value < min) {
618 *value = min;
619 } else if( *value > max) {
620 *value = max;
624 float get_move_y( GameState* s, unsigned char player) {
625 float pct, dest, x_len, y_len;
626 float py, by, pa, move_x;
628 py = (player==1)?(PLAYER(s, 1).y):(PLAYER(s, 2).y);
629 by = s->ball.y - s->ball.z;
630 pa = RACKET_Y_MID*2;
631 move_x = s->ball.move_x;
633 /* -1.0 .. 1.0 for racket hit position */
634 pct = fmaxf(-1.0, fminf(1.0, (by-py)/(pa/2)));
636 /* Y destination for ball */
637 dest = GAME_Y_MID + pct*(GAME_Y_MAX-GAME_Y_MIN);
639 /* lengths for the ball's journey */
640 if( player == 1) {
641 x_len = GAME_X_MAX - s->ball.x;
642 } else {
643 x_len = s->ball.x - GAME_X_MIN;
645 y_len = dest - by;
647 /* return the should-be value for move_y */
648 return (y_len*move_x)/(x_len);
651 void input_human(GameState* s, int player) {
652 bool hit, topspin, smash;
653 float move_y;
655 /* For mouse input, hand the player coordinates to the InputDevice */
656 if (PLAYER(s, player).input->type == INPUT_TYPE_MOUSE) {
657 PLAYER(s, player).input->player_x = (int)(PLAYER(s, player).x);
658 PLAYER(s, player).input->player_y = (int)(PLAYER(s, player).y);
661 move_y = PLAYER_MOVE_Y*input_device_get_axis(PLAYER(s, player).input, INPUT_AXIS_Y);
663 hit = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_HIT);
664 topspin = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_TOPSPIN);
665 smash = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_SMASH);
667 if (move_y != 0) {
668 if (fabsf(move_y) > fabsf(move_y*PLAYER(s, player).accelerate)) {
669 move_y *= PLAYER(s, player).accelerate;
671 PLAYER(s, player).y += move_y;
672 PLAYER(s, player).accelerate *= PLAYER_ACCEL_INCREASE;
673 } else {
674 PLAYER(s, player).accelerate = PLAYER_ACCEL_DEFAULT;
677 if(hit || topspin || smash) {
678 PLAYER(s, player).desire = (topspin)?(DESIRE_TOPSPIN):(DESIRE_NORMAL);
679 PLAYER(s, player).desire = (smash)?(DESIRE_SMASH):(PLAYER(s, player).desire);
681 PLAYER(s, player).power = fmaxf(10.0, fminf(PLAYER(s, player).power*PLAYER(s, player).power_up_factor, PLAYER_POWER_MAX));
682 PLAYER(s, player).use_power = false;
683 } else {
684 PLAYER(s, player).use_power = true;
688 void input_ai(GameState* s, int player) {
689 float fact = 1.7;
690 int ball_approaching = 0;
692 if ((PLAYER(s, player).x < GAME_X_MID && s->ball.move_x <= 0) ||
693 (PLAYER(s, player).x > GAME_X_MID && s->ball.move_x >= 0)) {
694 ball_approaching = 1;
697 /* FIXME - this is broken since the new physics model has been introduced */
699 if (fabsf(PLAYER(s, player).y - (s->ball.y-s->ball.z)) > RACKET_Y_MID*5) {
700 fact = 3.5;
703 if(1) {
704 if( PLAYER(s, player).desire == DESIRE_NORMAL && !IS_NEAR_Y_AI(PLAYER(s, player).y, (s->ball.y-s->ball.z)) && ball_approaching) {
705 if( PLAYER(s, player).y < (s->ball.y-s->ball.z)) {
706 PLAYER(s, player).y += fminf(2*fact, (s->ball.y-s->ball.z) - PLAYER(s, player).y);
707 } else if( PLAYER(s, player).y > (s->ball.y-s->ball.z)) {
708 PLAYER(s, player).y -= fminf(2*fact, PLAYER(s, player).y - (s->ball.y-s->ball.z));
712 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.) {
713 PLAYER(s, player).use_power = true;
714 } else if (ball_approaching) {
715 PLAYER(s, player).power = fmaxf(10., fminf(PLAYER(s, player).power*PLAYER(s, player).power_up_factor, PLAYER_POWER_MAX));
716 PLAYER(s, player).use_power = false;
721 void game_setup_serve( GameState* s) {
722 s->ball.z = 10.0;
723 s->ball.y = GAME_Y_MID;
724 s->ball.move_x = 0.0;
725 s->ball.move_y = 0.0;
726 s->ball.move_z = 0.0;
727 s->ball.last_hit_by = -1;
728 s->ball.inhibit_gravity = true;
730 if( s->serving_player == 1) {
731 s->ball.x = GAME_X_MIN-RACKET_X_MID*1.5;
732 } else {
733 s->ball.x = GAME_X_MAX+RACKET_X_MID*1.5;
737 int score_game(GameState* s) {
738 Player *last_hit_by = NULL;
739 Player *other = NULL;
741 Player* winner = NULL;
742 Player* loser = NULL;
744 /* determine "last hit by" and "other" */
745 if (s->ball.last_hit_by == 1) {
746 last_hit_by = &(PLAYER(s, 1));
747 other = &(PLAYER(s, 2));
748 } else {
749 last_hit_by = &(PLAYER(s, 2));
750 other = &(PLAYER(s, 1));
753 switch (s->score_event) {
754 case SCORE_EVENT_NET:
755 winner = other;
756 break;
757 case SCORE_EVENT_OUT:
758 case SCORE_EVENT_GROUND_INVALID:
759 case SCORE_EVENT_OFFSCREEN:
760 case SCORE_EVENT_GROUND_VALID:
761 if (s->ball.ground_hit) {
762 winner = last_hit_by;
763 } else {
764 winner = other;
766 break;
767 default:
768 break;
771 /* determine loser based on winner */
772 if (winner == last_hit_by) {
773 loser = other;
774 } else {
775 loser = last_hit_by;
778 /* we cannot be in an "impossibly high" set */
779 assert(s->current_set < SETS_TO_WIN*2-1);
781 winner->game++;
782 s->ec_game++;
783 if( loser->game < winner->game-1) {
784 if( winner->game >= 4) {
785 winner->game = loser->game = 0;
786 winner->sets[s->current_set]++;
787 s->ec_sets++;
789 /* serving is changed when the "game" is over */
790 s->serving_player = (s->serving_player==1)?(2):(1);
792 #ifdef HAVE_VOICE_FILES
793 /* speak the current score */
794 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);
795 #endif
797 /* scoring the set.. */
798 if( (winner->sets[s->current_set] == 6 && loser->sets[s->current_set] < 5) ||
799 winner->sets[s->current_set] == 7) {
800 s->current_set++;
801 s->winner = game_get_winner( s);
806 /* forget this event - we've handled it */
807 s->score_event = SCORE_UNDECIDED;
809 if (winner == &PLAYER(s, 1)) {
810 return WINNER_PLAYER1;
811 } else {
812 return WINNER_PLAYER2;
816 const char* format_sets(const GameState* s) {
817 static char sets[100];
818 static char tmp[100];
819 int i, max = s->current_set;
821 sets[0] = '\0';
823 if( s->winner != WINNER_NONE) {
824 max--;
826 for( i=0; i<=max; i++) {
827 sprintf( tmp, "%d:%d, ", PLAYER(s, 1).sets[i], PLAYER(s, 2).sets[i]);
828 strcat( sets, tmp);
831 sets[strlen(sets)-2] = '\0';
833 return sets;
836 const char* format_game(const GameState* s) {
837 static char game[100];
838 static const int game_scoring[] = { 0, 15, 30, 40 };
840 if( PLAYER(s, 1).game < 4 && PLAYER(s, 2).game < 4) {
841 #ifdef HAVE_VOICE_FILES
842 if (PLAYER(s, 1).game > 0 || PLAYER(s, 2).game > 0) {
843 if (PLAYER(s, 1).game == PLAYER(s, 2).game) {
844 voice_say_list(2, VOICE_LOVE_IN + 2*(PLAYER(s, 1).game), VOICE_ALL);
845 } else {
846 voice_say_list(2, VOICE_LOVE_IN + 2*(PLAYER(s, 1).game), VOICE_LOVE_OUT + 2*(PLAYER(s, 2).game));
849 #endif
850 sprintf( game, "%d - %d", game_scoring[PLAYER(s, 1).game], game_scoring[PLAYER(s, 2).game]);
851 } else if( PLAYER(s, 1).game > PLAYER(s, 2).game) {
852 #ifdef HAVE_VOICE_FILES
853 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_ONE);
854 #endif
855 strcpy( game, "advantage player 1");
856 } else if( PLAYER(s, 1).game < PLAYER(s, 2).game) {
857 #ifdef HAVE_VOICE_FILES
858 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_TWO);
859 #endif
860 strcpy( game, "advantage player 2");
861 } else {
862 #ifdef HAVE_VOICE_FILES
863 voice_say_list(1, VOICE_DEUCE);
864 #endif
865 strcpy( game, "deuce");
868 return game;
871 const char* format_status(const GameState* s) {
872 static char status[100];
873 static const char* set_names[] = { "first", "second", "third", "fourth", "fifth" };
875 switch (s->status_message) {
876 case STATUSMSG_NONE:
877 return "";
878 case STATUSMSG_WELCOME:
879 return "welcome to tennix " VERSION;
880 case STATUSMSG_DEFAULT:
881 sprintf(status, "%d:%d in %s set",
882 PLAYER(s, 1).sets[s->current_set],
883 PLAYER(s, 2).sets[s->current_set],
884 set_names[s->current_set]);
885 return status;
886 case STATUSMSG_NET:
887 return "net!";
888 case STATUSMSG_OUT:
889 return "out!";
890 case STATUSMSG_FAULT:
891 return "fault!";
892 case STATUSMSG_P1SCORES:
893 return "player 1 scores.";
894 case STATUSMSG_P2SCORES:
895 return "player 2 scores.";
896 case STATUSMSG_VOLLEY:
897 return "volley!";
898 case STATUSMSG_DIDNTCATCH:
899 return "did not catch the ball.";
900 default:
901 return "";
905 int game_get_winner( GameState* s) {
906 unsigned int i;
907 int sets[2] = {0};
909 for( i=0; i<s->current_set; i++) {
910 if( PLAYER(s, 1).sets[i] > PLAYER(s, 2).sets[i]) {
911 sets[0]++;
912 } else {
913 sets[1]++;
917 if( sets[0] == SETS_TO_WIN) return WINNER_PLAYER1;
918 if( sets[1] == SETS_TO_WIN) return WINNER_PLAYER2;
920 return WINNER_NONE;