Rev 81 | Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | pmbaty | 1 | // player.cpp |
| 2 | |||
| 3 | #include "common.h" |
||
| 4 | |||
| 5 | |||
| 6 | void Player_Init (player_t *player, int color, int type) |
||
| 7 | { |
||
| 8 | // helper function to initialize a player structure |
||
| 9 | |||
| 10 | unsigned long buffer_size; |
||
| 11 | |||
| 12 | // first reset the whole structure |
||
| 13 | memset (player, 0, sizeof (player_t)); |
||
| 14 | |||
| 15 | // allocate space for send and receive buffers and reset them |
||
| 16 | player->sendbuffer_locked = false; |
||
| 17 | player->sendbuffer_size = 1048576; |
||
| 18 | player->sendbuffer = (wchar_t *) SAFE_malloc (player->sendbuffer_size, sizeof (wchar_t), false); |
||
| 19 | player->sendbuffer[0] = 0; |
||
| 20 | player->recvbuffer_size = 1048576; |
||
| 21 | player->ascii_recvbuffer = (char *) SAFE_malloc (player->recvbuffer_size, sizeof (char), false); |
||
| 22 | player->ascii_recvbuffer[0] = 0; |
||
| 23 | player->recvbuffer = (wchar_t *) SAFE_malloc (player->recvbuffer_size, sizeof (wchar_t), false); |
||
| 24 | player->recvbuffer[0] = 0; |
||
| 25 | |||
| 26 | // save player type and color |
||
| 27 | player->type = type; |
||
| 28 | player->color = color; |
||
| 29 | |||
| 30 | // if internet or computer play, initialize the right interface |
||
| 31 | if (player->type == PLAYER_INTERNET) |
||
| 32 | { |
||
| 33 | // if we want online gaming, initialize the network |
||
| 34 | if (!PlayerNetwork_Init (player)) |
||
| 35 | player->type = PLAYER_HUMAN; // if it fails, fallback to human opponent |
||
| 36 | } |
||
| 37 | if (player->type == PLAYER_COMPUTER) |
||
| 38 | { |
||
| 39 | // if we want the chess engine, initialize it |
||
| 40 | if (!PlayerEngine_Init (player)) |
||
| 41 | player->type = PLAYER_HUMAN; // if it fails, fallback to human opponent |
||
| 42 | } |
||
| 43 | if (player->type == PLAYER_HUMAN) |
||
| 44 | { |
||
| 45 | // player is human, assume his name to be his Windows session name (until told otherwise) |
||
| 46 | buffer_size = WCHAR_SIZEOF (player->name); |
||
| 47 | GetUserName (player->name, &buffer_size); |
||
| 48 | } |
||
| 49 | |||
| 50 | // reset its view |
||
| 51 | Player_ResetView (player); |
||
| 52 | |||
| 53 | return; // finished |
||
| 54 | } |
||
| 55 | |||
| 56 | |||
| 57 | void Player_Shutdown (player_t *player) |
||
| 58 | { |
||
| 59 | // helper function to release a player structure |
||
| 60 | |||
| 61 | // if the chess engine was started, shut it down |
||
| 62 | if (player->type == PLAYER_COMPUTER) |
||
| 63 | PlayerEngine_Shutdown (player); |
||
| 64 | |||
| 65 | // else if we were online, shutdown the network |
||
| 66 | else if (player->type == PLAYER_INTERNET) |
||
| 67 | PlayerNetwork_Shutdown (player); |
||
| 68 | |||
| 69 | // free the send and receive buffers |
||
| 70 | SAFE_free ((void **) &player->recvbuffer); |
||
| 71 | SAFE_free ((void **) &player->ascii_recvbuffer); |
||
| 72 | SAFE_free ((void **) &player->sendbuffer); |
||
| 73 | |||
| 74 | // and finally reset the whole structure |
||
| 75 | memset (player, 0, sizeof (player_t)); |
||
| 76 | |||
| 77 | return; // finished |
||
| 78 | } |
||
| 79 | |||
| 80 | |||
| 81 | void Player_ResetView (player_t *player) |
||
| 82 | { |
||
| 83 | // helper function to reset a player's current and custom view |
||
| 84 | |||
| 85 | // reset current view |
||
| 86 | player->view_pitch = 55.0f; |
||
| 87 | player->view_yaw = (player->color == COLOR_BLACK ? 90.0f : -90.0f); |
||
| 88 | player->view_distance = 70.0f; |
||
| 89 | |||
| 90 | // reset custom view too |
||
| 91 | player->custom_pitch = player->view_pitch; |
||
| 92 | player->custom_yaw = player->view_yaw; |
||
| 93 | player->custom_distance = player->view_distance; |
||
| 94 | |||
| 95 | return; // finished |
||
| 96 | } |
||
| 97 | |||
| 98 | |||
| 99 | bool Player_RotateTable (player_t *player, float frame_time) |
||
| 100 | { |
||
| 101 | // this function is called every frame to rotate the chess table. There is some kind of |
||
| 102 | // filtering for the view. The function returns TRUE if the scene needs to be updated |
||
| 103 | // (that is, the table turned significantly). |
||
| 104 | |||
| 105 | float deviation_yaw; |
||
| 106 | float deviation_pitch; |
||
| 107 | float deviation_distance; |
||
| 108 | float turnspeed_yaw; |
||
| 109 | float turnspeed_pitch; |
||
| 110 | float turnspeed_distance; |
||
| 111 | |||
| 112 | // do we NOT want autorotation OR are we NOT playing yet OR are we still in animation ? |
||
| 113 | if (!options.want_autorotateon1vs1 || (the_board.game_state < STATE_PLAYING) || (current_time < animation_endtime + 1.0f)) |
||
| 114 | return (false); // if so, don't rotate anything |
||
| 115 | |||
| 116 | // compute what's left to turn yet |
||
| 117 | deviation_yaw = WrapAngle (player->view_yaw - current_yaw); |
||
| 118 | deviation_pitch = WrapAngle (player->view_pitch - current_pitch); |
||
| 119 | deviation_distance = player->view_distance - current_distance; |
||
| 120 | |||
| 121 | // compute the new turn speed |
||
| 122 | turnspeed_yaw = ((float) options.rotate_speed * deviation_yaw) * min (frame_time, 0.01f); |
||
| 123 | turnspeed_pitch = ((float) options.rotate_speed * deviation_pitch) * min (frame_time, 0.01f); |
||
| 124 | turnspeed_distance = ((float) options.rotate_speed * deviation_distance) * min (frame_time, 0.01f); |
||
| 125 | |||
| 126 | // move the aim cursor |
||
| 127 | current_yaw = WrapAngle (current_yaw + turnspeed_yaw); |
||
| 128 | current_pitch = WrapAngle (current_pitch + turnspeed_pitch); |
||
| 129 | current_distance = current_distance + turnspeed_distance; |
||
| 130 | |||
| 131 | // return whether the table moved significantly or not |
||
| 132 | return ((abs (turnspeed_yaw) > 0.01f) || (abs (turnspeed_pitch) > 0.01f) || (abs (turnspeed_distance) > 0.1f)); |
||
| 133 | } |
||
| 134 | |||
| 135 | |||
| 136 | bool Player_Think (player_t *player) |
||
| 137 | { |
||
| 138 | // this function makes the players think (in case they are not human) |
||
| 139 | |||
| 140 | player_t *current_player; |
||
| 141 | player_t *opposite_player; |
||
| 142 | bool do_update; |
||
| 143 | |||
| 144 | do_update = false; // don't update scene until told otherwise |
||
| 145 | |||
| 146 | // get current and opposite players |
||
| 147 | current_player = Player_GetCurrent (); |
||
| 148 | opposite_player = Player_GetOpposite (); |
||
| 149 | |||
| 150 | // if we're in online gaming mode, listen to the network |
||
| 151 | if (player->type == PLAYER_INTERNET) |
||
| 152 | do_update |= PlayerNetwork_Think (player); // and see if we need to update the scene |
||
| 153 | |||
| 154 | // else if we're running a chess engine, listen to it |
||
| 155 | else if (player->type == PLAYER_COMPUTER) |
||
| 156 | do_update |= PlayerEngine_Think (player); // and see if we need to update the scene |
||
| 157 | |||
| 158 | // else player is human |
||
| 159 | else |
||
| 160 | { |
||
| 161 | // is it NOT our turn AND does our opponent want to cancel a move ? |
||
| 162 | if ((player != current_player) && current_player->wants_cancel) |
||
| 163 | { |
||
| 164 | current_player->wants_cancel = false; // don't ask twice (remember now before we switch players) |
||
| 165 | |||
| 166 | // rewind game 1 move back |
||
| 167 | 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); |
||
| 168 | the_board.move_count = max (1, the_board.move_count - 1); // figure out how many moves we have now |
||
| 169 | the_board.viewed_move = the_board.move_count - 1; // take us back to the last move |
||
| 170 | |||
| 171 | do_update = true; // update the 3D scene |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | return (do_update); // finished, return whether we update the scene or not |
||
| 176 | } |
||
| 177 | |||
| 178 | |||
| 179 | bool Player_SendBuffer_Add (player_t *player, int milliseconds_max, const wchar_t *fmt, ...) |
||
| 180 | { |
||
| 181 | // helper function to append a data string to the send buffer. Returns FALSE in case buffer is inaccessible. |
||
| 182 | |||
| 183 | va_list argptr; |
||
| 184 | int length; |
||
| 185 | int count; |
||
| 186 | |||
| 187 | // as long as the buffer is locked... |
||
| 188 | count = 0; |
||
| 189 | while (player->sendbuffer_locked && (count < milliseconds_max)) |
||
| 190 | { |
||
| 191 | Sleep (10); // wait 10 milliseconds |
||
| 192 | count += 10; // remember we've waited 10 milliseconds more |
||
| 193 | } |
||
| 194 | |||
| 195 | // is send buffer STILL locked ? |
||
| 196 | if (player->sendbuffer_locked) |
||
| 197 | return (false); // buffer is inaccessible, return FALSE |
||
| 198 | |||
| 199 | player->sendbuffer_locked = true; // lock the buffer |
||
| 200 | length = wcslen (player->sendbuffer); // get current buffer length |
||
| 201 | |||
| 202 | // concatenate all the arguments in one string at the end of the buffer |
||
| 203 | va_start (argptr, fmt); |
||
| 204 | vswprintf_s (&player->sendbuffer[length], player->sendbuffer_size - length, fmt, argptr); |
||
| 205 | va_end (argptr); |
||
| 206 | |||
| 207 | player->sendbuffer_locked = false; // unlock it |
||
| 208 | |||
| 209 | return (true); // done |
||
| 210 | } |
||
| 211 | |||
| 212 | |||
| 213 | player_t *Player_FindByType (int player_type) |
||
| 214 | { |
||
| 215 | // wrapper that returns a pointer to the first player representing the wanted type on a board |
||
| 216 | |||
| 217 | // figure out which player is of the type we want and return it |
||
| 218 | if (the_board.players[COLOR_WHITE].type == player_type) |
||
| 219 | return (&the_board.players[COLOR_WHITE]); // it's the white player |
||
| 220 | else if (the_board.players[COLOR_BLACK].type == player_type) |
||
| 221 | return (&the_board.players[COLOR_BLACK]); // it's the black player |
||
| 222 | |||
| 223 | return (NULL); // can't find any player of the given type on this board |
||
| 224 | } |
||
| 225 | |||
| 226 | |||
| 227 | player_t *Player_GetCurrent (void) |
||
| 228 | { |
||
| 229 | // helper function that returns the current player |
||
| 230 | |||
| 231 | // return the current player |
||
| 232 | return (&the_board.players[Board_ColorToMove (&the_board)]); |
||
| 233 | } |
||
| 234 | |||
| 235 | |||
| 236 | player_t *Player_GetOpposite (void) |
||
| 237 | { |
||
| 238 | // helper function that returns the opposite player |
||
| 239 | |||
| 240 | // return the opposite player |
||
| 241 | return (&the_board.players[1 - Board_ColorToMove (&the_board)]); |
||
| 242 | } |