Subversion Repositories Games.Chess Giants

Rev

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