Subversion Repositories Games.Chess Giants

Rev

Rev 171 | 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
 
193 pmbaty 88
   // reset current view to standard view or top view, depending on user preferences
89
   if (options.want_startwithtopview)
90
   {
91
      player->view_pitch = 89.99f;
92
      player->view_yaw = (player->color == COLOR_BLACK ? 90.0f : -90.0f);
93
      player->view_distance = 58.0f;
94
   }
95
   else
96
   {
97
      player->view_pitch = 55.0f;
98
      player->view_yaw = (player->color == COLOR_BLACK ? 90.0f : -90.0f);
99
      player->view_distance = 70.0f;
100
   }
1 pmbaty 101
 
102
   // reset custom view too
103
   player->custom_pitch = player->view_pitch;
104
   player->custom_yaw = player->view_yaw;
105
   player->custom_distance = player->view_distance;
106
 
107
   return; // finished
108
}
109
 
110
 
111
bool Player_RotateTable (player_t *player, float frame_time)
112
{
113
   // this function is called every frame to rotate the chess table. There is some kind of
114
   // filtering for the view. The function returns TRUE if the scene needs to be updated
115
   // (that is, the table turned significantly).
116
 
117
   float deviation_yaw;
118
   float deviation_pitch;
119
   float deviation_distance;
120
   float turnspeed_yaw;
121
   float turnspeed_pitch;
122
   float turnspeed_distance;
123
 
136 pmbaty 124
   // (do we NOT want autorotation AND has the game started) OR are we NOT playing yet OR are we still in animation ?
125
   if ((!options.want_autorotateon1vs1 && (the_board.move_count > 1))
126
       || (the_board.game_state < STATE_PLAYING)
127
       || (current_time < animation_endtime + 1.0f))
1 pmbaty 128
      return (false); // if so, don't rotate anything
129
 
130
   // compute what's left to turn yet
131
   deviation_yaw = WrapAngle (player->view_yaw - current_yaw);
132
   deviation_pitch = WrapAngle (player->view_pitch - current_pitch);
133
   deviation_distance = player->view_distance - current_distance;
134
 
135
   // compute the new turn speed
136
   turnspeed_yaw = ((float) options.rotate_speed * deviation_yaw) * min (frame_time, 0.01f);
137
   turnspeed_pitch = ((float) options.rotate_speed * deviation_pitch) * min (frame_time, 0.01f);
138
   turnspeed_distance = ((float) options.rotate_speed * deviation_distance) * min (frame_time, 0.01f);
139
 
136 pmbaty 140
   // HACK: FIXME: if we're leaving closeup mode, reevaluate the board
141
   if ((current_distance == CLOSEUP_VIEW_DISTANCE) && (turnspeed_distance != 0))
142
      the_board.reevaluate = true; // FIXME FIXME ugly yuk
143
 
1 pmbaty 144
   // move the aim cursor
145
   current_yaw = WrapAngle (current_yaw + turnspeed_yaw);
146
   current_pitch = WrapAngle (current_pitch + turnspeed_pitch);
147
   current_distance = current_distance + turnspeed_distance;
148
 
149
   // return whether the table moved significantly or not
81 pmbaty 150
   return ((fabsf (turnspeed_yaw) > 0.01f) || (fabsf (turnspeed_pitch) > 0.01f) || (fabsf (turnspeed_distance) > 0.1f));
1 pmbaty 151
}
152
 
153
 
154
bool Player_Think (player_t *player)
155
{
156
   // this function makes the players think (in case they are not human)
157
 
158
   player_t *current_player;
159
   player_t *opposite_player;
160
   bool do_update;
161
 
171 pmbaty 162
   if (player->sleep_time > current_time)
163
      return (false); // if this player should NOT think yet, then just don't
164
 
1 pmbaty 165
   do_update = false; // don't update scene until told otherwise
166
 
167
   // get current and opposite players
168
   current_player = Player_GetCurrent ();
169
   opposite_player = Player_GetOpposite ();
170
 
171
   // if we're in online gaming mode, listen to the network
172
   if (player->type == PLAYER_INTERNET)
173
      do_update |= PlayerNetwork_Think (player); // and see if we need to update the scene
174
 
175
   // else if we're running a chess engine, listen to it
176
   else if (player->type == PLAYER_COMPUTER)
177
      do_update |= PlayerEngine_Think (player); // and see if we need to update the scene
178
 
179
   // else player is human
180
   else
181
   {
182
      // is it NOT our turn AND does our opponent want to cancel a move ?
183
      if ((player != current_player) && current_player->wants_cancel)
184
      {
185
         current_player->wants_cancel = false; // don't ask twice (remember now before we switch players)
186
 
187
         // rewind game 1 move back
188
         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);
189
         the_board.move_count = max (1, the_board.move_count - 1); // figure out how many moves we have now
190
         the_board.viewed_move = the_board.move_count - 1; // take us back to the last move
136 pmbaty 191
         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 192
         do_update = true; // update the 3D scene
193
      }
194
   }
195
 
196
   return (do_update); // finished, return whether we update the scene or not
197
}
198
 
199
 
200
bool Player_SendBuffer_Add (player_t *player, int milliseconds_max, const wchar_t *fmt, ...)
201
{
202
   // helper function to append a data string to the send buffer. Returns FALSE in case buffer is inaccessible.
203
 
204
   va_list argptr;
205
   int length;
206
   int count;
207
 
208
   // as long as the buffer is locked...
209
   count = 0;
210
   while (player->sendbuffer_locked && (count < milliseconds_max))
211
   {
212
      Sleep (10); // wait 10 milliseconds
213
      count += 10; // remember we've waited 10 milliseconds more
214
   }
215
 
216
   // is send buffer STILL locked ?
217
   if (player->sendbuffer_locked)
218
      return (false); // buffer is inaccessible, return FALSE
219
 
220
   player->sendbuffer_locked = true; // lock the buffer
221
   length = wcslen (player->sendbuffer); // get current buffer length
222
 
223
   // concatenate all the arguments in one string at the end of the buffer
224
   va_start (argptr, fmt);
225
   vswprintf_s (&player->sendbuffer[length], player->sendbuffer_size - length, fmt, argptr);
226
   va_end (argptr);
227
 
228
   player->sendbuffer_locked = false; // unlock it
229
 
230
   return (true); // done
231
}
232
 
233
 
234
player_t *Player_FindByType (int player_type)
235
{
236
   // wrapper that returns a pointer to the first player representing the wanted type on a board
237
 
238
   // figure out which player is of the type we want and return it
239
   if (the_board.players[COLOR_WHITE].type == player_type)
240
      return (&the_board.players[COLOR_WHITE]); // it's the white player
241
   else if (the_board.players[COLOR_BLACK].type == player_type)
242
      return (&the_board.players[COLOR_BLACK]); // it's the black player
243
 
244
   return (NULL); // can't find any player of the given type on this board
245
}
246
 
247
 
248
player_t *Player_GetCurrent (void)
249
{
250
   // helper function that returns the current player
251
 
252
   // return the current player
253
   return (&the_board.players[Board_ColorToMove (&the_board)]);
254
}
255
 
256
 
257
player_t *Player_GetOpposite (void)
258
{
259
   // helper function that returns the opposite player
260
 
261
   // return the opposite player
262
   return (&the_board.players[1 - Board_ColorToMove (&the_board)]);
263
}