- // player.cpp 
-   
- #include "common.h" 
-   
-   
- void Player_Init (player_t *player, int color, int type) 
- { 
-    // helper function to initialize a player structure 
-   
-    unsigned long buffer_size; 
-   
-    // first reset the whole structure 
-    memset (player, 0, sizeof (player_t)); 
-   
-    // allocate space for send and receive buffers and reset them 
-    player->sendbuffer_locked = false; 
-    player->sendbuffer_size = 1048576; 
-    player->sendbuffer = (wchar_t *) SAFE_malloc (player->sendbuffer_size, sizeof (wchar_t), false); 
-    player->sendbuffer[0] = 0; 
-    player->recvbuffer_size = 1048576; 
-    player->ascii_recvbuffer = (char *) SAFE_malloc (player->recvbuffer_size, sizeof (char), false); 
-    player->ascii_recvbuffer[0] = 0; 
-    player->recvbuffer = (wchar_t *) SAFE_malloc (player->recvbuffer_size, sizeof (wchar_t), false); 
-    player->recvbuffer[0] = 0; 
-   
-    // save player type and color 
-    player->type = type; 
-    player->color = color; 
-   
-    // if internet or computer play, initialize the right interface 
-    if (player->type == PLAYER_INTERNET) 
-    { 
-       // if we want online gaming, initialize the network 
-       if (!PlayerNetwork_Init (player)) 
-          player->type = PLAYER_HUMAN; // if it fails, fallback to human opponent 
-    } 
-    if (player->type == PLAYER_COMPUTER) 
-    { 
-       // if we want the chess engine, initialize it 
-       if (!PlayerEngine_Init (player)) 
-          player->type = PLAYER_HUMAN; // if it fails, fallback to human opponent 
-    } 
-    if (player->type == PLAYER_HUMAN) 
-    { 
-       // player is human, assume his name to be his Windows session name (until told otherwise) 
-       buffer_size = WCHAR_SIZEOF (player->name); 
-       GetUserName (player->name, &buffer_size); 
-    } 
-   
-    // reset its view 
-    Player_ResetView (player); 
-   
-    return; // finished 
- } 
-   
-   
- void Player_Shutdown (player_t *player) 
- { 
-    // helper function to release a player structure 
-   
-    // if the chess engine was started, shut it down 
-    if (player->type == PLAYER_COMPUTER) 
-       PlayerEngine_Shutdown (player); 
-   
-    // else if we were online, shutdown the network 
-    else if (player->type == PLAYER_INTERNET) 
-       PlayerNetwork_Shutdown (player); 
-   
-    // free the send and receive buffers 
-    SAFE_free ((void **) &player->recvbuffer); 
-    SAFE_free ((void **) &player->ascii_recvbuffer); 
-    SAFE_free ((void **) &player->sendbuffer); 
-   
-    // and finally reset the whole structure 
-    memset (player, 0, sizeof (player_t)); 
-   
-    return; // finished 
- } 
-   
-   
- void Player_ResetView (player_t *player) 
- { 
-    // helper function to reset a player's current and custom view 
-   
-    // reset current view 
-    player->view_pitch = 55.0f; 
-    player->view_yaw = (player->color == COLOR_BLACK ? 90.0f : -90.0f); 
-    player->view_distance = 70.0f; 
-   
-    // reset custom view too 
-    player->custom_pitch = player->view_pitch; 
-    player->custom_yaw = player->view_yaw; 
-    player->custom_distance = player->view_distance; 
-   
-    return; // finished 
- } 
-   
-   
- bool Player_RotateTable (player_t *player, float frame_time) 
- { 
-    // this function is called every frame to rotate the chess table. There is some kind of 
-    // filtering for the view. The function returns TRUE if the scene needs to be updated 
-    // (that is, the table turned significantly). 
-   
-    float deviation_yaw; 
-    float deviation_pitch; 
-    float deviation_distance; 
-    float turnspeed_yaw; 
-    float turnspeed_pitch; 
-    float turnspeed_distance; 
-   
-    // do we NOT want autorotation OR are we NOT playing yet OR are we still in animation ? 
-    if (!options.want_autorotateon1vs1 || (the_board.game_state < STATE_PLAYING) || (current_time < animation_endtime + 1.0f)) 
-       return (false); // if so, don't rotate anything 
-   
-    // compute what's left to turn yet 
-    deviation_yaw = WrapAngle (player->view_yaw - current_yaw); 
-    deviation_pitch = WrapAngle (player->view_pitch - current_pitch); 
-    deviation_distance = player->view_distance - current_distance; 
-   
-    // compute the new turn speed 
-    turnspeed_yaw = ((float) options.rotate_speed * deviation_yaw) * min (frame_time, 0.01f); 
-    turnspeed_pitch = ((float) options.rotate_speed * deviation_pitch) * min (frame_time, 0.01f); 
-    turnspeed_distance = ((float) options.rotate_speed * deviation_distance) * min (frame_time, 0.01f); 
-   
-    // move the aim cursor 
-    current_yaw = WrapAngle (current_yaw + turnspeed_yaw); 
-    current_pitch = WrapAngle (current_pitch + turnspeed_pitch); 
-    current_distance = current_distance + turnspeed_distance; 
-   
-    // return whether the table moved significantly or not 
-    return ((fabsf (turnspeed_yaw) > 0.01f) || (fabsf (turnspeed_pitch) > 0.01f) || (fabsf (turnspeed_distance) > 0.1f)); 
- } 
-   
-   
- bool Player_Think (player_t *player) 
- { 
-    // this function makes the players think (in case they are not human) 
-   
-    player_t *current_player; 
-    player_t *opposite_player; 
-    bool do_update; 
-   
-    do_update = false; // don't update scene until told otherwise 
-   
-    // get current and opposite players 
-    current_player = Player_GetCurrent (); 
-    opposite_player = Player_GetOpposite (); 
-   
-    // if we're in online gaming mode, listen to the network 
-    if (player->type == PLAYER_INTERNET) 
-       do_update |= PlayerNetwork_Think (player); // and see if we need to update the scene 
-   
-    // else if we're running a chess engine, listen to it 
-    else if (player->type == PLAYER_COMPUTER) 
-       do_update |= PlayerEngine_Think (player); // and see if we need to update the scene 
-   
-    // else player is human 
-    else 
-    { 
-       // is it NOT our turn AND does our opponent want to cancel a move ? 
-       if ((player != current_player) && current_player->wants_cancel) 
-       { 
-          current_player->wants_cancel = false; // don't ask twice (remember now before we switch players) 
-   
-          // rewind game 1 move back 
-          the_board.moves = (boardmove_t *) SAFE_realloc (the_board.moves, the_board.move_count, max (1, the_board.move_count - 1), sizeof (boardmove_t), false); 
-          the_board.move_count = max (1, the_board.move_count - 1); // figure out how many moves we have now 
-          the_board.viewed_move = the_board.move_count - 1; // take us back to the last move 
-   
-          do_update = true; // update the 3D scene 
-       } 
-    } 
-   
-    return (do_update); // finished, return whether we update the scene or not 
- } 
-   
-   
- bool Player_SendBuffer_Add (player_t *player, int milliseconds_max, const wchar_t *fmt, ...) 
- { 
-    // helper function to append a data string to the send buffer. Returns FALSE in case buffer is inaccessible. 
-   
-    va_list argptr; 
-    int length; 
-    int count; 
-   
-    // as long as the buffer is locked... 
-    count = 0; 
-    while (player->sendbuffer_locked && (count < milliseconds_max)) 
-    { 
-       Sleep (10); // wait 10 milliseconds 
-       count += 10; // remember we've waited 10 milliseconds more 
-    } 
-   
-    // is send buffer STILL locked ? 
-    if (player->sendbuffer_locked) 
-       return (false); // buffer is inaccessible, return FALSE 
-   
-    player->sendbuffer_locked = true; // lock the buffer 
-    length = wcslen (player->sendbuffer); // get current buffer length 
-   
-    // concatenate all the arguments in one string at the end of the buffer 
-    va_start (argptr, fmt); 
-    vswprintf_s (&player->sendbuffer[length], player->sendbuffer_size - length, fmt, argptr); 
-    va_end (argptr); 
-   
-    player->sendbuffer_locked = false; // unlock it 
-   
-    return (true); // done 
- } 
-   
-   
- player_t *Player_FindByType (int player_type) 
- { 
-    // wrapper that returns a pointer to the first player representing the wanted type on a board 
-   
-    // figure out which player is of the type we want and return it 
-    if (the_board.players[COLOR_WHITE].type == player_type) 
-       return (&the_board.players[COLOR_WHITE]); // it's the white player 
-    else if (the_board.players[COLOR_BLACK].type == player_type) 
-       return (&the_board.players[COLOR_BLACK]); // it's the black player 
-   
-    return (NULL); // can't find any player of the given type on this board 
- } 
-   
-   
- player_t *Player_GetCurrent (void) 
- { 
-    // helper function that returns the current player 
-   
-    // return the current player 
-    return (&the_board.players[Board_ColorToMove (&the_board)]); 
- } 
-   
-   
- player_t *Player_GetOpposite (void) 
- { 
-    // helper function that returns the opposite player 
-   
-    // return the opposite player 
-    return (&the_board.players[1 - Board_ColorToMove (&the_board)]); 
- } 
-