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