Split input handling + stepping; type changes in GameState
[tennix.git] / game.c
blobb05b0ca1b4d22e58a9954fefdab0e6d6b5755451
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 },
50 REFEREE_NORMAL,
52 WINNER_NONE,
53 SOUND_EVENT_NONE, /* sound events */
54 SCORE_UNDECIDED,
56 EVENTCOUNTER_GAMESTATE_START,
57 EVENTCOUNTER_GAMESTATE_START,
58 STATUSMSG_WELCOME,
61 s = (GameState*)malloc(sizeof(GameState));
62 if (s == NULL) abort();
64 memcpy(s, &template, sizeof(GameState));
66 game_setup_serve(s);
68 return s;
72 int gamestate_save(GameState* s, const char* filename)
74 const Location *location;
75 InputDevice* input_devices[MAXPLAYERS];
76 int i, result = 0;
77 FILE* fp = NULL;
78 #ifndef WIN32
79 char tmp[MAXPATHLEN];
81 assert(getcwd(tmp, MAXPATHLEN) == tmp);
82 assert(chdir(getenv("HOME")) == 0);
83 #endif
85 /**
86 * Process-specific data (pointers to data
87 * structures only of interest to this process)
88 * has to be "removed" before saving.
90 * It can later be restored (in gamestate_load).
91 **/
93 fp = fopen(filename, "w");
94 if (fp == NULL) {
95 return -1;
98 /* Save data for later recovery + clear */
99 location = s->location;
100 s->location = NULL;
101 for (i=1; i<=MAXPLAYERS; i++) {
102 input_devices[i-1] = PLAYER(s, i).input;
103 PLAYER(s, i).input = NULL;
106 if (fwrite(s, sizeof(GameState), 1, fp) != 1) {
107 result = -2;
110 /* Restore process-specific data */
111 s->location = location;
112 for (i=1; i<=MAXPLAYERS; i++) {
113 PLAYER(s, i).input = input_devices[i-1];
116 fclose(fp);
118 #ifndef WIN32
119 assert(chdir(tmp) == 0);
120 #endif
122 return result;
126 GameState* gamestate_load(const char* filename)
128 FILE* fp;
129 GameState* s = NULL;
130 #ifndef WIN32
131 char tmp[MAXPATHLEN];
133 assert(getcwd(tmp, MAXPATHLEN) == tmp);
134 assert(chdir(getenv("HOME")) == 0);
135 #endif
137 fp = fopen(filename, "r");
139 if (fp != NULL) {
140 s = (GameState*)malloc(sizeof(GameState));
141 if (s != NULL) {
142 if (fread(s, sizeof(GameState), 1, fp) == 1) {
143 /* Restore/create process-specific data */
144 /* FIXME: s->location, players' "input" */
145 } else {
146 free(s);
147 s = NULL;
150 fclose(fp);
153 #ifndef WIN32
154 assert(chdir(tmp) == 0);
155 #endif
157 return s;
161 void gameloop(GameState *s) {
162 Uint32 ot = SDL_GetTicks();
163 Uint32 nt;
164 Uint32 dt = GAME_TICKS;
165 Uint32 diff;
166 Uint32 accumulator = 0;
167 bool quit = false;
168 int p;
169 RenderState r = {
170 SOUND_EVENT_NONE,
171 REFEREE_COUNT,
172 EVENTCOUNTER_RENDERSTATE_START,
173 EVENTCOUNTER_RENDERSTATE_START,
174 STATUSMSG_NONE,
175 NULL,
176 NULL,
177 NULL,
180 /* Catch-up with existing sound events */
181 r.sound_events = s->sound_events;
183 #ifdef ENABLE_FPS_LIMIT
184 Uint32 ft, frames; /* frame timer and frames */
185 #endif
187 if (s->location->rain > 0) {
188 play_sample_background(SOUND_RAIN);
190 if (s->location->max_visitors > 100) {
191 play_sample_loop(SOUND_AUDIENCE);
194 for (p=1; p<=MAXPLAYERS; p++) {
195 input_device_join_game(PLAYER(s, p).input, s, p);
198 #ifdef ENABLE_FPS_LIMIT
199 frames = 0;
200 ft = SDL_GetTicks();
201 #endif
202 while( !quit) {
203 nt = SDL_GetTicks();
204 diff = nt-ot;
205 if( diff > 2000) {
206 diff = 0;
209 accumulator += diff;
210 ot = nt;
212 while( accumulator >= dt) {
213 step(s);
214 quit = handle_input(s);
215 accumulator -= dt;
218 #ifdef ENABLE_FPS_LIMIT
219 while (frames*1000.0/((float)(SDL_GetTicks()-ft+1))>(float)(DEFAULT_FPS)) {
220 SDL_Delay(10);
222 frames++;
223 #endif
225 render(s, &r);
228 for (p=1; p<=MAXPLAYERS; p++) {
229 input_device_part_game(PLAYER(s, p).input);
232 clear_screen();
233 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
234 store_screen();
236 stop_sample(SOUND_AUDIENCE);
237 stop_sample(SOUND_RAIN);
240 void step(GameState* s) {
241 bool ground_event = false;
242 int p;
244 s->ball.z += s->ball.move_z;
245 if (!s->ball.inhibit_gravity) {
246 s->ball.move_z += GRAVITY;
249 s->ball.x += s->ball.move_x;
250 s->ball.y += s->ball.move_y;
252 for (p=1; p<=MAXPLAYERS; p++) {
253 if (PLAYER(s, p).use_power) {
254 PLAYER(s, p).power = fmaxf(0.0, fminf(
255 PLAYER(s, p).power*PLAYER(s, p).power_down_factor,
256 PLAYER_POWER_MAX));
260 if (s->ball.z < 0) {
261 /* ground event */
262 ground_event = true;
264 s->referee = REFEREE_NORMAL;
266 /* bounce from the ground */
267 if (fabsf(s->ball.move_z) > 0.3) {
268 s->sound_events ^= SOUND_EVENT_GROUND;
269 s->ball.move_z *= -s->ball.restitution;
270 } else {
271 s->ball.move_z = 0;
273 s->ball.z = 0;
276 if (NET_COLLISION_BALL(s->ball)) {
277 /* the net blocks movement of the ball */
278 while (NET_COLLISION_BALL(s->ball)) {
279 /* make sure the ball appears OUTSIDE of the net */
280 if (s->ball.move_x < 0) {
281 s->ball.x += 1;
282 } else {
283 s->ball.x -= 1;
286 s->ball.move_x = 0;
287 s->ball.move_y = 0;
290 /* see if we have something to score */
291 if (s->score_event == SCORE_UNDECIDED) {
292 if (NET_COLLISION_BALL(s->ball)) {
293 /* the ball "fell" into the net */
294 s->score_event = SCORE_EVENT_NET;
295 s->sound_events ^= SOUND_EVENT_OUT;
296 s->status_message = STATUSMSG_NET;
297 } else if (IS_OFFSCREEN(s->ball.x, s->ball.y)) {
298 /* ball flew offscreen */
299 s->score_event = SCORE_EVENT_OFFSCREEN;
300 s->sound_events ^= SOUND_EVENT_OUT;
301 s->status_message = STATUSMSG_OUT;
302 } else if (ground_event) {
303 /* the ball hit the ground on the screen */
304 if (IS_OUT(s->ball.x, s->ball.y)) {
305 /* the ball bounced in the OUT area */
306 s->score_event = SCORE_EVENT_OUT;
307 s->sound_events ^= SOUND_EVENT_OUT;
308 s->status_message = STATUSMSG_OUT;
309 } else if (GROUND_IS_VALID(s->ball.last_hit_by, s->ball.x, s->ball.y)) {
310 if (s->ball.ground_hit) {
311 s->score_event = SCORE_EVENT_GROUND_VALID;
312 s->sound_events ^= SOUND_EVENT_OUT;
313 s->status_message = STATUSMSG_DIDNTCATCH;
314 } else {
315 /* first ground hit in valid area */
316 s->ball.ground_hit = true;
318 } else {
319 /* ball hit somewhere invalid */
320 s->score_event = SCORE_EVENT_GROUND_INVALID;
321 s->sound_events ^= SOUND_EVENT_OUT;
322 s->status_message = STATUSMSG_FAULT;
327 if (s->score_event != SCORE_UNDECIDED) {
328 /* we have some scoring to do */
329 if (s->score_time < SCORING_DELAY/GAME_TICKS) {
330 /* schedule scoring in the future */
331 s->score_time++;
332 s->referee = REFEREE_OUT;
333 } else if (s->score_time >= SCORING_DELAY/GAME_TICKS) {
334 /* time has ran out - score now */
335 switch (score_game(s)) {
336 case WINNER_PLAYER1:
337 s->status_message = STATUSMSG_P1SCORES;
338 s->referee = REFEREE_PLAYER1;
339 break;
340 case WINNER_PLAYER2:
341 s->status_message = STATUSMSG_P2SCORES;
342 s->referee = REFEREE_PLAYER2;
343 break;
344 default:
345 assert(0);
346 break;
348 s->score_time = 0;
350 game_setup_serve(s);
351 if (s->location->max_visitors > 100) {
352 s->sound_events ^= SOUND_EVENT_APPLAUSE;
355 } else {
356 /* score is still undecided - do the racket swing thing */
357 for (p=1; p<=2; p++) {
358 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) {
359 /* RACKET HIT */
360 if (!s->ball.ground_hit && s->ball.move_x != 0.0) {
361 s->status_message = STATUSMSG_VOLLEY;
362 } else {
363 s->status_message = STATUSMSG_DEFAULT;
365 switch (PLAYER(s, p).desire) {
366 case DESIRE_NORMAL:
367 /* normal swing */
368 s->ball.move_x = 2.7 + 2.0*PLAYER(s, p).power/PLAYER_POWER_MAX;
369 s->ball.move_z = 1.2*PLAYER(s, p).power/PLAYER_POWER_MAX;
370 break;
371 case DESIRE_TOPSPIN:
372 /* top spin */
373 s->ball.move_x = 1.1 + 2.2*PLAYER(s, p).power/PLAYER_POWER_MAX;
374 s->ball.move_z = 2.5*PLAYER(s, p).power/PLAYER_POWER_MAX;
375 break;
376 case DESIRE_SMASH:
377 /* smash */
378 s->ball.move_x = 4.0 + 3.0*PLAYER(s, p).power/PLAYER_POWER_MAX;
379 s->ball.move_z = 1.1*PLAYER(s, p).power/PLAYER_POWER_MAX;
380 break;
382 s->ball.move_y = get_move_y( s, p);
383 s->sound_events ^= SOUND_EVENT_RACKET;
384 s->ball.ground_hit = false;
385 s->ball.inhibit_gravity = false;
386 s->ball.last_hit_by = p;
387 if (p==2) {
388 s->ball.move_x *= -1;
395 bool handle_input(GameState* s) {
396 static GameState tmp;
397 Uint8* keys = NULL;
398 int p;
400 SDL_PumpEvents();
401 keys = SDL_GetKeyState(NULL);
403 if (keys['1']) {
404 memcpy(&tmp, s, sizeof(GameState));
405 } else if (keys['2']) {
406 memcpy(s, &tmp, sizeof(GameState));
409 if (s->winner == WINNER_NONE) {
410 for (p=1; p<=MAXPLAYERS; p++) {
411 if( PLAYER(s, p).type == PLAYER_TYPE_HUMAN) {
412 input_human(s, p);
413 } else {
414 input_ai(s, p);
417 /* Make sure player coordinates are valid */
418 if (PLAYER(s, p).y < PLAYER_Y_MIN) {
419 PLAYER(s, p).y = PLAYER_Y_MIN;
420 } else if (PLAYER(s, p).y > PLAYER_Y_MAX) {
421 PLAYER(s, p).y = PLAYER_Y_MAX;
426 /* Maemo: The "F4" button is the "Open menu" button */
427 return (keys[SDLK_ESCAPE] || keys['q']);
430 void render(const GameState* s, RenderState* r) {
431 int x, y, b;
432 unsigned int i;
433 float zoom;
434 float rotate;
435 int t=1000;
436 soundevent_t sounds;
437 Uint32 time = SDL_GetTicks();
439 /* The bits in sound_events flip when the sound should play */
440 if ((sounds = (r->sound_events ^ s->sound_events)) != 0) {
441 if (sounds & SOUND_EVENT_GROUND) {
442 sample_volume_group(SOUND_GROUND_FIRST, SOUND_GROUND_LAST,
443 fmaxf(0.0, fminf(1.0, fabsf(s->ball.move_z)/2)));
444 pan_sample_group(SOUND_GROUND_FIRST, SOUND_GROUND_LAST,
445 fmaxf(0.0, fminf(1.0, (s->ball.x)/WIDTH)));
446 play_sample(SOUND_GROUND);
448 if (sounds & SOUND_EVENT_OUT) {
449 play_sample(SOUND_OUT);
451 if (sounds & SOUND_EVENT_APPLAUSE) {
452 play_sample(SOUND_APPLAUSE);
454 if (sounds & SOUND_EVENT_RACKET) {
455 if (((s->ball.x)/WIDTH) < 0.5) {
456 pan_sample_group(SOUND_RACKET_FIRST, SOUND_RACKET_LAST, 0.3);
457 } else {
458 pan_sample_group(SOUND_RACKET_FIRST, SOUND_RACKET_LAST, 0.7);
460 play_sample(SOUND_RACKET);
462 r->sound_events = s->sound_events;
465 if( s->winner != WINNER_NONE) {
466 clear_screen();
467 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
468 store_screen();
469 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);
470 /*sprintf( s->game_score_str, "player %d wins the match with %s", s->winner, format_sets( s));
471 font_draw_string(FONT_LARGE, s->game_score_str, (WIDTH-font_get_string_width(FONT_LARGE, s->game_score_str))/2, HEIGHT/2 + 30);*/
472 updatescr();
473 return;
475 if ((r->referee != s->referee) ||
476 (r->ec_game != s->ec_game) ||
477 (r->ec_sets != s->ec_sets) ||
478 (r->status_message != s->status_message)) {
479 /* Update status message text */
480 if (r->status_message != s->status_message) {
481 r->text_status = format_status(s);
482 r->status_message = s->status_message;
484 /* Update game status text */
485 if (r->ec_game != s->ec_game) {
486 r->text_game = format_game(s);
487 r->ec_game = s->ec_game;
489 /* Update set status text */
490 if (r->ec_sets != s->ec_sets) {
491 r->text_sets = format_sets(s);
492 if (r->status_message == STATUSMSG_DEFAULT) {
493 r->text_status = format_status(s);
495 r->ec_sets = s->ec_sets;
497 clear_screen();
498 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
499 fill_image(s->location->court_type, 120, 120, 400, 250);
500 show_image(GR_COURT, 0, 0, 255);
501 font_draw_string(FONT_XLARGE, r->text_game, 14, 14);
502 font_draw_string(FONT_XLARGE, r->text_sets,
503 (WIDTH-font_get_string_width(FONT_XLARGE, r->text_sets))-14, 14);
504 if (s->location->has_referee) {
505 switch (s->referee) {
506 case REFEREE_NORMAL:
507 t = 1000;
508 break;
509 case REFEREE_OUT:
510 t = 200;
511 break;
512 case REFEREE_PLAYER1:
513 case REFEREE_PLAYER2:
514 t = 400;
515 break;
517 t = (time/t)%4;
518 switch (t) {
519 case 0:
520 t=0;
521 break;
522 case 1:
523 t=1;
524 break;
525 case 2:
526 t=0;
527 break;
528 case 3:
529 t=2;
530 break;
532 show_sprite( GR_REFEREE, s->referee*3+t, 12, 250, 10, 255);
533 if (voice_finished_flag == 0) {
534 show_sprite(GR_TALK, (time/150)%2, 2, 280, 45, 255);
537 r->referee = s->referee;
538 store_screen();
541 /* show_sprite( GR_RACKET, (!PLAYER(s, 1).state), 4, PLAYER(s, 1).x-RACKET_X_MID, PLAYER(s, 1).y-RACKET_Y_MID, 255);
542 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);*/
543 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);
544 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);
546 rectangle(10, HEIGHT-30, PLAYER_POWER_MAX, 10, 50, 50, 50);
547 rectangle(WIDTH-PLAYER_POWER_MAX-10, HEIGHT-30, PLAYER_POWER_MAX, 10, 50, 50, 50);
549 rectangle(10, HEIGHT-30, (int)(PLAYER(s, 1).power), 10, 200, 200, 200);
550 rectangle(WIDTH-PLAYER_POWER_MAX-10, HEIGHT-30, (int)(PLAYER(s, 2).power), 10, 200, 200, 200);
552 if( s->ball.move_x > 0) {
553 b = (time/100)%BALL_STATES;
554 } else if( s->ball.move_x < 0) {
555 b = BALL_STATES-1-(time/100)%BALL_STATES;
556 } else {
557 b = 0;
560 rotate = 0.0;
561 zoom = fmaxf(0.5, fminf(1.0, (float)(30.-(s->ball.z))/10.));
562 show_image_rotozoom(GR_SHADOW, s->ball.x, s->ball.y+3, rotate, zoom);
564 rotate = (-s->ball.move_x * (float)((float)time/10.));
565 zoom = 1.0 + fmaxf(0.0, (s->ball.z-10.)/100.);
566 show_image_rotozoom(GR_BALL, s->ball.x, s->ball.y - s->ball.z, rotate, zoom);
568 /* Player 1's mouse rectangle */
569 if (PLAYER(s, 1).input != NULL && PLAYER(s, 1).input->type == INPUT_TYPE_MOUSE) {
570 rectangle(PLAYER(s, 1).x-2+5, PLAYER(s, 1).input->my-2, 4, 4, 255, 255, 255);
571 rectangle(PLAYER(s, 1).x-1+5, PLAYER(s, 1).input->my-1, 2, 2, 0, 0, 0);
574 /* Player 2's mouse rectangle */
575 if (PLAYER(s, 2).input != NULL && PLAYER(s, 2).input->type == INPUT_TYPE_MOUSE) {
576 rectangle(PLAYER(s, 2).x-2+5, PLAYER(s, 2).input->my-2, 4, 4, 255, 255, 255);
577 rectangle(PLAYER(s, 2).x-1+5, PLAYER(s, 2).input->my-1, 2, 2, 0, 0, 0);
580 font_draw_string(FONT_MEDIUM, r->text_status,
581 (WIDTH-font_get_string_width(FONT_MEDIUM, r->text_status))/2, HEIGHT-50);
583 for (i=0; i<s->location->rain; i++) {
584 x = rand()%WIDTH;
585 y = rand()%HEIGHT;
586 draw_line_faded(x, y, x+10, y+30, 0, 0, 255, 100, 200, 255);
588 if (s->location->rain) {
590 * Cheap-ish update of the whole screen. This can
591 * probably be optimized.
593 update_rect(0, 0, WIDTH, HEIGHT);
596 #ifdef DEBUG
597 line_horiz( PLAYER(s, 1).y, 255, 0, 0);
598 line_horiz( PLAYER(s, 2).y, 0, 0, 255);
599 line_horiz( s->ball.y, 0, 255, 0);
601 line_vert( PLAYER(s, 1).x, 255, 0, 0);
602 line_vert( PLAYER(s, 2).x, 0, 0, 255);
603 line_vert( s->ball.x, 0, 255, 0);
605 line_horiz( GAME_Y_MIN, 100, 100, 100);
606 line_horiz( GAME_Y_MAX, 100, 100, 100);
607 #endif
608 switch (s->location->fog) {
609 default:
610 case 4:
611 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -time/150, 0);
612 case 3:
613 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, -time/100, 20);
614 case 2:
615 fill_image_offset(GR_FOG2, 0, 0, WIDTH, HEIGHT, -time/180, 80);
616 case 1:
617 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, time/200, 0);
618 case 0:
619 break;
621 if (s->location->night) {
622 show_image(GR_NIGHT, 0, 0, 255);
625 updatescr();
628 float get_move_y( GameState* s, unsigned char player) {
629 float pct, dest, x_len, y_len;
630 float py, by, pa, move_x;
632 py = (player==1)?(PLAYER(s, 1).y):(PLAYER(s, 2).y);
633 by = s->ball.y - s->ball.z;
634 pa = RACKET_Y_MID*2;
635 move_x = s->ball.move_x;
637 /* -1.0 .. 1.0 for racket hit position */
638 pct = fmaxf(-1.0, fminf(1.0, (by-py)/(pa/2)));
640 /* Y destination for ball */
641 dest = GAME_Y_MID + pct*(GAME_Y_MAX-GAME_Y_MIN);
643 /* lengths for the ball's journey */
644 if( player == 1) {
645 x_len = GAME_X_MAX - s->ball.x;
646 } else {
647 x_len = s->ball.x - GAME_X_MIN;
649 y_len = dest - by;
651 /* return the should-be value for move_y */
652 return (y_len*move_x)/(x_len);
655 void input_human(GameState* s, int player) {
656 bool hit, topspin, smash;
657 float move_y;
659 /* For mouse input, hand the player coordinates to the InputDevice */
660 if (PLAYER(s, player).input->type == INPUT_TYPE_MOUSE) {
661 PLAYER(s, player).input->player_x = (int)(PLAYER(s, player).x);
662 PLAYER(s, player).input->player_y = (int)(PLAYER(s, player).y);
665 move_y = PLAYER_MOVE_Y*input_device_get_axis(PLAYER(s, player).input, INPUT_AXIS_Y);
667 hit = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_HIT);
668 topspin = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_TOPSPIN);
669 smash = input_device_get_key(PLAYER(s, player).input, INPUT_KEY_SMASH);
671 if (move_y != 0) {
672 if (fabsf(move_y) > fabsf(move_y*PLAYER(s, player).accelerate)) {
673 move_y *= PLAYER(s, player).accelerate;
675 PLAYER(s, player).y += move_y;
676 PLAYER(s, player).accelerate *= PLAYER_ACCEL_INCREASE;
677 } else {
678 PLAYER(s, player).accelerate = PLAYER_ACCEL_DEFAULT;
681 if(hit || topspin || smash) {
682 PLAYER(s, player).desire = (topspin)?(DESIRE_TOPSPIN):(DESIRE_NORMAL);
683 PLAYER(s, player).desire = (smash)?(DESIRE_SMASH):(PLAYER(s, player).desire);
685 PLAYER(s, player).power = fmaxf(10.0, fminf(PLAYER(s, player).power*PLAYER(s, player).power_up_factor, PLAYER_POWER_MAX));
686 PLAYER(s, player).use_power = false;
687 } else {
688 PLAYER(s, player).use_power = true;
692 void input_ai(GameState* s, int player) {
693 float fact = 1.7;
694 int ball_approaching = 0;
696 if ((PLAYER(s, player).x < GAME_X_MID && s->ball.move_x <= 0) ||
697 (PLAYER(s, player).x > GAME_X_MID && s->ball.move_x >= 0)) {
698 ball_approaching = 1;
701 /* FIXME - this is broken since the new physics model has been introduced */
703 if (fabsf(PLAYER(s, player).y - (s->ball.y-s->ball.z)) > RACKET_Y_MID*5) {
704 fact = 3.5;
707 if(1) {
708 if( PLAYER(s, player).desire == DESIRE_NORMAL && !IS_NEAR_Y_AI(PLAYER(s, player).y, (s->ball.y-s->ball.z)) && ball_approaching) {
709 if( PLAYER(s, player).y < (s->ball.y-s->ball.z)) {
710 PLAYER(s, player).y += fminf(2*fact, (s->ball.y-s->ball.z) - PLAYER(s, player).y);
711 } else if( PLAYER(s, player).y > (s->ball.y-s->ball.z)) {
712 PLAYER(s, player).y -= fminf(2*fact, PLAYER(s, player).y - (s->ball.y-s->ball.z));
716 if (IS_NEAR_Y(PLAYER(s, player).y, (s->ball.y-s->ball.z)) && IS_NEAR_X_AI(PLAYER(s, player).x, s->ball.x) && PLAYER(s, player).power > 90.) {
717 PLAYER(s, player).use_power = true;
718 } else if (ball_approaching) {
719 PLAYER(s, player).power = fmaxf(10., fminf(PLAYER(s, player).power*PLAYER(s, player).power_up_factor, PLAYER_POWER_MAX));
720 PLAYER(s, player).use_power = false;
725 void game_setup_serve( GameState* s) {
726 s->ball.z = 10.0;
727 s->ball.y = GAME_Y_MID;
728 s->ball.move_x = 0.0;
729 s->ball.move_y = 0.0;
730 s->ball.move_z = 0.0;
731 s->ball.last_hit_by = -1;
732 s->ball.inhibit_gravity = true;
734 if( s->serving_player == 1) {
735 s->ball.x = GAME_X_MIN-RACKET_X_MID*1.5;
736 } else {
737 s->ball.x = GAME_X_MAX+RACKET_X_MID*1.5;
741 int score_game(GameState* s) {
742 Player *last_hit_by = NULL;
743 Player *other = NULL;
745 Player* winner = NULL;
746 Player* loser = NULL;
748 /* determine "last hit by" and "other" */
749 if (s->ball.last_hit_by == 1) {
750 last_hit_by = &(PLAYER(s, 1));
751 other = &(PLAYER(s, 2));
752 } else {
753 last_hit_by = &(PLAYER(s, 2));
754 other = &(PLAYER(s, 1));
757 switch (s->score_event) {
758 case SCORE_EVENT_NET:
759 winner = other;
760 break;
761 case SCORE_EVENT_OUT:
762 case SCORE_EVENT_GROUND_INVALID:
763 case SCORE_EVENT_OFFSCREEN:
764 case SCORE_EVENT_GROUND_VALID:
765 if (s->ball.ground_hit) {
766 winner = last_hit_by;
767 } else {
768 winner = other;
770 break;
771 default:
772 break;
775 /* determine loser based on winner */
776 if (winner == last_hit_by) {
777 loser = other;
778 } else {
779 loser = last_hit_by;
782 /* we cannot be in an "impossibly high" set */
783 assert(s->current_set < SETS_TO_WIN*2-1);
785 winner->game++;
786 s->ec_game++;
787 if( loser->game < winner->game-1) {
788 if( winner->game >= 4) {
789 winner->game = loser->game = 0;
790 winner->sets[s->current_set]++;
791 s->ec_sets++;
793 /* serving is changed when the "game" is over */
794 s->serving_player = (s->serving_player==1)?(2):(1);
796 #ifdef HAVE_VOICE_FILES
797 /* speak the current score */
798 voice_say_list(4, VOICE_ZERO_IN + (PLAYER(s, 1).sets[s->current_set])*2, VOICE_TO, VOICE_ZERO_OUT + (PLAYER(s, 2).sets[s->current_set])*2, VOICE_IN_THE_FIRST_SET+s->current_set);
799 #endif
801 /* scoring the set.. */
802 if( (winner->sets[s->current_set] == 6 && loser->sets[s->current_set] < 5) ||
803 winner->sets[s->current_set] == 7) {
804 s->current_set++;
805 s->winner = game_get_winner( s);
810 /* forget this event - we've handled it */
811 s->score_event = SCORE_UNDECIDED;
813 if (winner == &PLAYER(s, 1)) {
814 return WINNER_PLAYER1;
815 } else {
816 return WINNER_PLAYER2;
820 const char* format_sets(const GameState* s) {
821 static char sets[100];
822 static char tmp[100];
823 int i, max = s->current_set;
825 sets[0] = '\0';
827 if( s->winner != WINNER_NONE) {
828 max--;
830 for( i=0; i<=max; i++) {
831 sprintf( tmp, "%d:%d, ", PLAYER(s, 1).sets[i], PLAYER(s, 2).sets[i]);
832 strcat( sets, tmp);
835 sets[strlen(sets)-2] = '\0';
837 return sets;
840 const char* format_game(const GameState* s) {
841 static char game[100];
842 static const int game_scoring[] = { 0, 15, 30, 40 };
844 if( PLAYER(s, 1).game < 4 && PLAYER(s, 2).game < 4) {
845 #ifdef HAVE_VOICE_FILES
846 if (PLAYER(s, 1).game > 0 || PLAYER(s, 2).game > 0) {
847 if (PLAYER(s, 1).game == PLAYER(s, 2).game) {
848 voice_say_list(2, VOICE_LOVE_IN + 2*(PLAYER(s, 1).game), VOICE_ALL);
849 } else {
850 voice_say_list(2, VOICE_LOVE_IN + 2*(PLAYER(s, 1).game), VOICE_LOVE_OUT + 2*(PLAYER(s, 2).game));
853 #endif
854 sprintf( game, "%d - %d", game_scoring[PLAYER(s, 1).game], game_scoring[PLAYER(s, 2).game]);
855 } else if( PLAYER(s, 1).game > PLAYER(s, 2).game) {
856 #ifdef HAVE_VOICE_FILES
857 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_ONE);
858 #endif
859 strcpy( game, "advantage player 1");
860 } else if( PLAYER(s, 1).game < PLAYER(s, 2).game) {
861 #ifdef HAVE_VOICE_FILES
862 voice_say_list(1, VOICE_ADVANTAGE_PLAYER_TWO);
863 #endif
864 strcpy( game, "advantage player 2");
865 } else {
866 #ifdef HAVE_VOICE_FILES
867 voice_say_list(1, VOICE_DEUCE);
868 #endif
869 strcpy( game, "deuce");
872 return game;
875 const char* format_status(const GameState* s) {
876 static char status[100];
877 static const char* set_names[] = { "first", "second", "third", "fourth", "fifth" };
879 switch (s->status_message) {
880 case STATUSMSG_NONE:
881 return "";
882 case STATUSMSG_WELCOME:
883 return "welcome to tennix " VERSION;
884 case STATUSMSG_DEFAULT:
885 sprintf(status, "%d:%d in %s set",
886 PLAYER(s, 1).sets[s->current_set],
887 PLAYER(s, 2).sets[s->current_set],
888 set_names[s->current_set]);
889 return status;
890 case STATUSMSG_NET:
891 return "net!";
892 case STATUSMSG_OUT:
893 return "out!";
894 case STATUSMSG_FAULT:
895 return "fault!";
896 case STATUSMSG_P1SCORES:
897 return "player 1 scores.";
898 case STATUSMSG_P2SCORES:
899 return "player 2 scores.";
900 case STATUSMSG_VOLLEY:
901 return "volley!";
902 case STATUSMSG_DIDNTCATCH:
903 return "did not catch the ball.";
904 default:
905 return "";
909 winner_t game_get_winner(const GameState* s) {
910 unsigned int i;
911 int sets[2] = {0};
913 for( i=0; i<s->current_set; i++) {
914 if( PLAYER(s, 1).sets[i] > PLAYER(s, 2).sets[i]) {
915 sets[0]++;
916 } else {
917 sets[1]++;
921 if( sets[0] == SETS_TO_WIN) return WINNER_PLAYER1;
922 if( sets[1] == SETS_TO_WIN) return WINNER_PLAYER2;
924 return WINNER_NONE;