1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
|
/**
*
* Tennix! SDL Port
* Copyright (C) 2003, 2007, 2008 Thomas Perl <thp@perli.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
**/
#include <stdio.h>
#include <math.h>
#include "tennix.h"
#include "game.h"
#include "graphics.h"
#include "input.h"
#include "sound.h"
void game( bool singleplayer) {
GameState s = {
{ 0, 0, 0.0, 0.0, 0.0 },
{ 0, 0, 0, 0, 0 },
{ GAME_X_MIN-RACKET_X_MID*2, GAME_Y_MID, 0, 0, 1, DESIRE_NORMAL, PLAYER_TYPE_HUMAN, GAME_Y_MID, false, 0, {0}, 0, 0, PLAYER_ACCEL_DEFAULT, true },
{ GAME_X_MAX+RACKET_X_MID*2, GAME_Y_MID, 0, 0, 0, DESIRE_NORMAL, PLAYER_TYPE_HUMAN, GAME_Y_MID, false, 0, {0}, 0, 0, PLAYER_ACCEL_DEFAULT, true },
0,
0,
0,
true,
"welcome to tennix " VERSION,
{ 0 },
{ 0 },
REFEREE_NORMAL,
0,
WINNER_NONE,
false,
GR_CTT_GRASS,
-1,
{ 0 },
0,
false,
{ { { 0 } } },
0.0,
SOUND_MAX,
0.0,
0.0,
0
};
strcpy( s.game_score_str, format_game( &s));
strcpy( s.sets_score_str, format_sets( &s));
Uint32 ot = SDL_GetTicks();
Uint32 nt;
Uint32 dt = GAME_TICKS;
Uint32 diff;
Uint32 accumulator = 0;
bool quit = false;
int x, y, z;
#ifdef ENABLE_FPS_LIMIT
Uint32 ft, frames; /* frame timer and frames */
#endif
if( singleplayer) {
#ifdef DEBUG
s.player1.type = PLAYER_TYPE_AI;
#endif
s.player2.type = PLAYER_TYPE_AI;
}
game_setup_serve( &s);
/* smoothen n-gram */
for( x = 0; x<NGRAM_STEPS; x++) {
for( y = 0; y<NGRAM_STEPS; y++) {
for( z = 0; z<NGRAM_STEPS; z++) {
s.ngram[x][y][z] = 1;
}
}
}
#ifdef ENABLE_FPS_LIMIT
frames = 0;
ft = SDL_GetTicks();
#endif
while( !quit) {
nt = SDL_GetTicks();
diff = nt-ot;
if( diff > 2000) {
diff = 0;
}
accumulator += diff;
ot = nt;
while( accumulator >= dt) {
quit = step( &s);
s.time += dt;
accumulator -= dt;
if( s.was_stopped) {
ot = SDL_GetTicks();
s.was_stopped = false;
}
}
#ifdef ENABLE_FPS_LIMIT
while (frames*1000.0/((float)(SDL_GetTicks()-ft+1))>(float)(DEFAULT_FPS)) {
SDL_Delay(10);
}
frames++;
#endif
render( &s);
}
}
bool step( GameState* s) {
Uint8 *keys;
SDL_Event e;
if( get_phase( s) < 1.0) {
if( !s->ground.jump) {
s->play_sound = SOUND_GROUND;
if( IS_OUT_Y( s->ball.y)) {
/* out - responsibilities stay the same */
s->status = "out!";
s->play_sound = SOUND_OUT;
s->referee = REFEREE_OUT;
} else {
/* not out - responsibilities change */
s->player1.responsible = !(s->player2.responsible = !s->player2.responsible);
s->status = format_status( s);
s->referee = REFEREE_NORMAL;
}
}
s->ground.jump = 3;
s->ground.x = s->ball.x;
s->ground.y = s->ball.y;
} else {
if( s->ground.jump && !(s->time%5)) s->ground.jump--;
}
if( IS_OUT_X(s->ball.x) || IS_OFFSCREEN_Y(s->ball.y)) {
if( IS_OFFSCREEN( s->ball.x, s->ball.y)) {
s->player1_serves = s->player1.responsible;
score_game( s, s->player2.responsible);
strcpy( s->game_score_str, format_game( s));
strcpy( s->sets_score_str, format_sets( s));
if( s->player1.responsible) {
if( s->player1.type == PLAYER_TYPE_HUMAN && s->player2.type == PLAYER_TYPE_AI) {
s->status = "computer scores";
} else {
s->status = "player 2 scores";
}
s->referee = REFEREE_PLAYER2;
} else {
if( s->player1.type == PLAYER_TYPE_HUMAN && s->player2.type == PLAYER_TYPE_AI) {
s->status = "player scores";
} else {
s->status = "player 1 scores";
}
s->referee = REFEREE_PLAYER1;
}
game_setup_serve( s);
s->play_sound = SOUND_APPLAUSE;
SDL_Delay( 500);
start_fade();
s->was_stopped = true;
s->history_size = 0;
s->history_is_locked = 0;
s->ngram_prediction = 0.0;
#ifdef DEBUG
printf( "-- game reset --\n");
#endif
}
if( IS_OUT_X(s->ball.x)) {
if( !s->history_is_locked && s->referee != REFEREE_OUT) {
s->history[s->history_size] = (int)(NGRAM_STEPS*s->ball.y/HEIGHT);
s->history_size++;
if( s->history_size == 3) {
s->ngram[s->history[0]][s->history[1]][s->history[2]] += 10;
#ifdef DEBUG
printf( "history: %d, %d, %d\n", s->history[0], s->history[1], s->history[2]);
#endif
s->ngram_prediction = ngram_predictor( s);
s->history[0] = s->history[1];
s->history[1] = s->history[2];
s->history_size--;
}
s->history_is_locked = true;
}
if( s->ball.move_x <= 0 && IS_NEAR_X( s->player1.x, s->ball.x) && IS_NEAR_Y( s->player1.y, s->ball.y) && s->player1.state && s->referee != REFEREE_OUT) {
s->ball.x = GAME_X_MIN;
if( s->player1.state == PLAYER_STATE_MAX) {
s->ball.move_x = PLAYER_POWERSHOT;
} else {
s->ball.move_x = 2.5 + 2.0*s->player1.state/PLAYER_STATE_MAX;
}
s->ball.move_y = get_move_y( s, 1);
s->player2.responsible = !(s->player1.responsible = 1);
s->ball.jump += 1.0-2.0*(s->player1.state<5);
s->play_sound = SOUND_RACKET;
pan_sample(SOUND_RACKET, 0.4);
} else if( s->ball.move_x >= 0 && IS_NEAR_X( s->player2.x, s->ball.x) && IS_NEAR_Y( s->player2.y, s->ball.y) && s->player2.state && s->referee != REFEREE_OUT) {
s->ball.x = GAME_X_MAX;
if( s->player2.state == PLAYER_STATE_MAX) {
s->ball.move_x = -PLAYER_POWERSHOT;
} else {
s->ball.move_x = -(2.5 + 2.0*s->player2.state/PLAYER_STATE_MAX);
}
s->ball.move_y = get_move_y( s, 2);
s->player1.responsible = !(s->player2.responsible = 1);
s->ball.jump += 1.0-2.0*(s->player2.state<5);
s->play_sound = SOUND_RACKET;
pan_sample(SOUND_RACKET, 0.6);
}
}
} else {
s->history_is_locked = false;
}
SDL_PollEvent( &e);
keys = SDL_GetKeyState( NULL);
switch(e.type) {
case SDL_JOYAXISMOTION:
if (e.jaxis.axis == JOYSTICK_Y_AXIS) {
s->joystick_y = JOYSTICK_PERCENTIZE(e.jaxis.value);
} else if (e.jaxis.axis == JOYSTICK_X_AXIS) {
s->joystick_x = JOYSTICK_PERCENTIZE(e.jaxis.value);
}
break;
case SDL_JOYBUTTONUP: case SDL_JOYBUTTONDOWN:
if (e.jbutton.button == JOYSTICK_BUTTON_A) {
s->joystick_a = (e.jbutton.state == SDL_PRESSED);
}
break;
}
if( keys['c'] && s->time%50==0) {
s->court_type++;
if( s->court_type > GR_CTT_LAST) {
s->court_type = GR_CTT_FIRST;
}
}
if( !is_fading() && !s->is_over) {
if( s->player1.type == PLAYER_TYPE_HUMAN) {
input_human( &s->player1,
keys['w'] || keys[SDLK_UP] || s->joystick_y < -JOYSTICK_TRESHOLD,
keys['s'] || keys[SDLK_DOWN] || s->joystick_y > JOYSTICK_TRESHOLD,
keys['d'] || keys[SDLK_SPACE] || keys[SDLK_LCTRL] || s->joystick_a,
#ifdef ENABLE_MOUSE
true,
#else
false,
#endif
s);
} else {
input_ai( &s->player1, &s->ball, &s->player2, s);
}
if( s->player2.type == PLAYER_TYPE_HUMAN) {
input_human( &s->player2, keys['o'], keys['l'], keys['k'], false, s);
} else {
input_ai( &s->player2, &s->ball, &s->player1, s);
}
}
if( keys['f']) SDL_WM_ToggleFullScreen( screen);
if( keys['y']) SDL_SaveBMP( screen, "screenshot.bmp");
if( keys['p']) {
while( keys['p']) {
SDL_PollEvent( &e);
keys = SDL_GetKeyState( NULL);
SDL_Delay( 10);
}
while( keys['p'] == 0) {
SDL_PollEvent( &e);
keys = SDL_GetKeyState( NULL);
SDL_Delay( 10);
}
while( keys['p']) {
SDL_PollEvent( &e);
keys = SDL_GetKeyState( NULL);
SDL_Delay( 10);
}
s->was_stopped = true;
}
if( keys[SDLK_ESCAPE] || keys['q']) return true;
limit_value( &s->player1.y, PLAYER_Y_MIN, PLAYER_Y_MAX);
limit_value( &s->player2.y, PLAYER_Y_MIN, PLAYER_Y_MAX);
limit_value( &s->ball.jump, BALL_JUMP_MIN, BALL_JUMP_MAX);
/* Update ball_dest for debugging purposes */
get_move_y(s, 1);
get_move_y(s, 2);
s->ball.x += s->ball.move_x;
s->ball.y += s->ball.move_y;
if(s->player1.state) s->player1.state--;
if(s->player2.state) s->player2.state--;
return false;
}
void render( GameState* s) {
if (s->play_sound != SOUND_MAX) {
play_sample(s->play_sound);
s->play_sound = SOUND_MAX;
}
if( s->winner != WINNER_NONE) {
if( !s->is_over) {
start_fade();
s->is_over = true;
}
clear_screen();
store_screen();
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);
sprintf( s->game_score_str, "player %d wins the match with %s", s->winner, format_sets( s));
font_draw_string( GR_DKC2_FONT, s->game_score_str, (WIDTH-font_get_string_width( GR_DKC2_FONT, s->game_score_str))/2, HEIGHT/2 + 30, s->time/20, ANIMATION_WAVE | ANIMATION_BUNGEE);
updatescr();
return;
}
if (s->old_court_type != s->court_type) {
clear_screen();
fill_image(s->court_type, 120, 120, 400, 250);
show_image(GR_COURT, 0, 0, 255);
s->old_court_type = s->court_type;
store_screen();
}
show_sprite( GR_REFEREE, s->referee, 4, 250, 10, 255);
show_image( GR_SHADOW, s->ball.x-BALL_X_MID, s->ball.y + get_phase( s) - BALL_Y_MID, 255);
show_sprite( GR_RACKET, (!s->player1.state), 4, s->player1.x-RACKET_X_MID, s->player1.y-RACKET_Y_MID, 255);
show_sprite( GR_RACKET, (!s->player2.state)+2, 4, s->player2.x-RACKET_X_MID, s->player2.y-RACKET_Y_MID, 255);
if( s->ground.jump) {
show_sprite( GR_GROUND, s->ground.jump-1, 3, s->ground.x - BALL_X_MID, s->ground.y - BALL_Y_MID, 128);
}
if( s->ball.move_x > 0) {
show_sprite( GR_BALL, (s->time/500)%4, 4, s->ball.x-BALL_X_MID, s->ball.y-BALL_Y_MID, 255);
} else if( s->ball.move_x < 0) {
show_sprite( GR_BALL, 3-(s->time/500)%4, 4, s->ball.x-BALL_X_MID, s->ball.y-BALL_Y_MID, 255);
} else {
show_sprite( GR_BALL, 0, 4, s->ball.x-BALL_X_MID, s->ball.y-BALL_Y_MID, 255);
}
/* Player 1's mouse rectangle */
if (!(s->player1.mouse_locked)) {
rectangle(s->player1.x-2+5, s->player1.mouse_y-2, 4, 4, 255, 255, 255);
rectangle(s->player1.x-1+5, s->player1.mouse_y-1, 2, 2, 0, 0, 0);
}
font_draw_string( GR_DKC2_FONT, s->game_score_str, 14, 14, 0, ANIMATION_NONE);
font_draw_string( GR_DKC2_FONT, s->sets_score_str, (WIDTH-font_get_string_width( GR_DKC2_FONT, s->sets_score_str))-14, 14, 0, ANIMATION_NONE);
font_draw_string( GR_DKC2_FONT, s->status, (WIDTH-font_get_string_width( GR_DKC2_FONT, s->status))/2, HEIGHT-50, s->time/30, ANIMATION_WAVE);
#ifdef DEBUG
line_horiz( s->player1.y, 255, 0, 0);
line_horiz( s->player2.y, 0, 0, 255);
line_horiz( s->ball.y, 0, 255, 0);
line_vert( s->player1.x, 255, 0, 0);
line_vert( s->player2.x, 0, 0, 255);
line_vert( s->ball.x, 0, 255, 0);
line_horiz( s->player1.ball_dest, 255, 0, 255);
line_horiz( s->player2.ball_dest, 0, 255, 255);
line_horiz( GAME_Y_MIN, 100, 100, 100);
line_horiz( GAME_Y_MAX, 100, 100, 100);
#endif
updatescr();
}
void limit_value( float* value, float min, float max) {
if( *value < min) {
*value = min;
} else if( *value > max) {
*value = max;
}
}
float get_phase( GameState* s) {
float pos, fract;
float x, min, max, direction;
x = s->ball.x;
min = GAME_X_MIN;
max = GAME_X_MAX;
direction = s->ball.move_x;
pos = (direction>0)?(1-GROUND_PHASE):(GROUND_PHASE);
fract = (x-min)/(max-min);
if( fract < pos) {
fract = fract/pos;
return fabsf( cosf(PI*fract/2))*PHASE_AMP*s->ball.jump;
} else {
fract = (pos-fract)/(1-pos);
return fabsf( sinf(PI*fract/2))*PHASE_AMP*s->ball.jump;
}
}
float get_move_y( GameState* s, unsigned char player) {
float pct, dest, x_len, y_len;
float py, by, pa, move_x;
py = (player==1)?(s->player1.y):(s->player2.y);
by = s->ball.y;
pa = RACKET_Y_MID*2;
move_x = s->ball.move_x;
/* -1.0 .. 1.0 for racket hit position */
pct = (by-py)/(pa/2);
limit_value( &pct, -1.0, 1.0);
/* Y destination for ball */
dest = GAME_Y_MID + pct*(GAME_Y_MAX-GAME_Y_MIN);
if( player == 1) {
s->player1.ball_dest = dest;
} else {
s->player2.ball_dest = dest;
}
/* lengths for the ball's journey */
if( player == 1) {
x_len = fabsf(GAME_X_MAX - s->ball.x);
y_len = dest - by + MOVE_Y_SEED-rand()%MOVE_Y_SEED*2;
} else {
x_len = s->ball.x - GAME_X_MIN;
y_len = by - dest + MOVE_Y_SEED-rand()%MOVE_Y_SEED*2;
}
/* return the should-be value for move_y */
return (y_len*move_x)/(x_len);
}
void input_human( Player* player, bool up, bool down, bool hit, bool use_mouse, GameState* s) {
int diff = PLAYER_MOVE_Y;
int mb;
/**
* Only use mouse control if the user isn't pressing any buttons
* this way, keyboard control still works when mouse control is
* enabled.
**/
if (use_mouse && (down || up || hit)) {
/**
* this is here so if the user decides to play
* with keyboard controls, we will lock the
* mouse and disable displaying the mouse cursor
**/
player->mouse_locked = true;
}
if (use_mouse && !down && !up && !hit) {
mb = SDL_GetMouseState(&(player->mouse_x), &(player->mouse_y));
if (mb&SDL_BUTTON(SDL_BUTTON_LEFT)) {
if (player->mouse_y < player->y) {
down = false;
up = true;
diff = (player->y-player->mouse_y<diff)?(player->y-player->mouse_y):(diff);
} else if (player->mouse_y > player->y) {
up = false;
down = true;
diff = (player->mouse_y-player->y<diff)?(player->mouse_y-player->y):(diff);
}
player->mouse_locked = false;
} else if (!player->mouse_locked) {
hit = true;
}
}
if (fabsf(s->joystick_y) > JOYSTICK_TRESHOLD) {
diff = PLAYER_MOVE_Y*fabsf(s->joystick_y)/40.0;
if (diff > PLAYER_MOVE_Y) {
diff = PLAYER_MOVE_Y;
}
}
if (up) {
player->y -= fminf(diff, diff*player->accelerate);
player->accelerate *= PLAYER_ACCEL_INCREASE;
} else if (down) {
player->y += fminf(diff, diff*player->accelerate);
player->accelerate *= PLAYER_ACCEL_INCREASE;
} else {
player->accelerate = PLAYER_ACCEL_DEFAULT;
}
if( hit) {
if( !player->state && !player->state_locked) {
player->state = PLAYER_STATE_MAX;
player->state_locked = true;
}
} else {
player->state_locked = false;
}
}
void input_ai( Player* player, Ball* ball, Player* opponent, GameState* s) {
float fact = 1.7;
float target;
if( fabsf( player->y - ball->y) > RACKET_Y_MID*5) {
fact = 3.5;
}
target = GAME_Y_MID + (opponent->ball_dest - GAME_Y_MID)/5;
if( player->responsible) {
if( player->desire == DESIRE_NORMAL && !IS_NEAR_Y_AI( player->y, ball->y)) {
if( player->y < ball->y) {
player->y += fmin( 2*fact, ball->y - player->y);
} else if( player->y > ball->y) {
player->y -= fmin( 2*fact, player->y - ball->y);
}
}
if( (ball->move_x != 0 || IS_NEAR_Y_AI( player->y, ball->y)) && IS_NEAR_X_AI( player->x, ball->x) && !player->state && rand()%4) {
player->state = PLAYER_STATE_MAX;
}
} else if( ball->move_x == 0) {
if( player->desire == DESIRE_NORMAL && !IS_NEAR_Y_AI( player->y, target)) {
if( player->y < target) {
player->y += fmin( fact, (target-player->y)/40.0);
} else if( player->y > target) {
player->y -= fmin( fact, (player->y-target)/40.0);
}
}
} else if( s->ngram_prediction > 0.0) {
target = s->ngram_prediction*((float)HEIGHT)/((float)(NGRAM_STEPS));
target = GAME_Y_MID + (target-GAME_Y_MID)*1.5;
if( player->desire == DESIRE_NORMAL && !IS_NEAR_Y_AI( player->y, target)) {
if( player->y < target) {
player->y += fmin( fact, (target-player->y)/40.0);
} else if( player->y > target) {
player->y -= fmin( fact, (player->y-target)/40.0);
}
}
} else {/*
if( player->desire == DESIRE_NORMAL) {
if( !IS_NEAR_Y_AI( player->y, target)) {
player->y += (target - player->y)/40.0;
}
}*/
}
}
void game_setup_serve( GameState* s) {
s->ball.jump = 7.5;
s->ball.y = GAME_Y_MID;
s->ball.move_x = 0.0;
s->ball.move_y = 0.0;
if( s->player1_serves) {
s->player1.responsible = true;
s->player1.ball_dest = 0.0;
s->ball.x = GAME_X_MIN-RACKET_X_MID*1.5;
} else {
s->player1.responsible = false;
s->player2.ball_dest = 0.0;
s->ball.x = GAME_X_MAX+RACKET_X_MID*1.5;
}
s->player2.responsible = !(s->player1.responsible);
}
float ngram_predictor( GameState* s) {
unsigned int count = 0;
unsigned long sum = 0;
int x, y, z;
float result;
if( s->history_size < 3) {
return 0.0;
}
x = s->history[1];
y = s->history[2];
for( z = 0; z<NGRAM_STEPS; z++) {
count += s->ngram[x][y][z];
sum += z * s->ngram[x][y][z];
}
result = ((float)(sum))/((float)(count));
#ifdef DEBUG
printf( "predicting next = %.2f\n", result);
#endif
return result;
}
void score_game( GameState* s, bool player1_scored) {
Player* winner = (player1_scored)?(&(s->player1)):(&(s->player2));
Player* loser = (player1_scored)?(&(s->player2)):(&(s->player1));
if( s->current_set >= SETS_TO_WIN*2-1) {
return;
}
winner->game++;
if( loser->game < winner->game-1) {
if( winner->game >= 4) {
winner->game = loser->game = 0;
winner->sets[s->current_set]++;
/* scoring the set.. */
if( (winner->sets[s->current_set] == 6 && loser->sets[s->current_set] < 5) ||
winner->sets[s->current_set] == 7) {
s->current_set++;
s->winner = game_get_winner( s);
}
}
}
}
char* format_sets( GameState* s) {
static char sets[100];
static char tmp[100];
int i, max = s->current_set;
sets[0] = '\0';
if( s->winner != WINNER_NONE) {
max--;
}
for( i=0; i<=max; i++) {
sprintf( tmp, "%d:%d, ", s->player1.sets[i], s->player2.sets[i]);
strcat( sets, tmp);
}
sets[strlen(sets)-2] = '\0';
return sets;
}
char* format_game( GameState* s) {
static char game[100];
static const int game_scoring[] = { 0, 15, 30, 40 };
if( s->player1.game < 4 && s->player2.game < 4) {
sprintf( game, "%d - %d", game_scoring[s->player1.game], game_scoring[s->player2.game]);
} else if( s->player1.game > s->player2.game) {
strcpy( game, "advantage player 1");
} else if( s->player1.game < s->player2.game) {
strcpy( game, "advantage player 2");
} else {
strcpy( game, "deuce");
}
return game;
}
char* format_status( GameState* s) {
static char status[100];
static const char* set_names[] = { "first", "second", "third", "fourth", "fifth" };
sprintf( status, "%d:%d in %s set", s->player1.sets[s->current_set], s->player2.sets[s->current_set], set_names[s->current_set]);
return status;
}
int game_get_winner( GameState* s) {
int i;
int sets[2] = {0};
for( i=0; i<s->current_set; i++) {
if( s->player1.sets[i] > s->player2.sets[i]) {
sets[0]++;
} else {
sets[1]++;
}
}
if( sets[0] == SETS_TO_WIN) return WINNER_PLAYER1;
if( sets[1] == SETS_TO_WIN) return WINNER_PLAYER2;
return WINNER_NONE;
}
|