Subversion Repositories Games.Chess Giants

Rev

Rev 75 | Rev 116 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
// scene.cpp
2
 
3
#include "common.h"
4
 
5
 
6
// prototypes of local functions
7
static void Scene_AddPart (scene_t *scene, int part_type, int part_color, float pos_x, float pos_y, float pos_z, float turn_yaw, float pitch);
8
static void Scene_AddTile (scene_t *scene, int tile_type, float scale, float pos_x, float pos_y, float pos_z, float turn_yaw);
9
 
10
 
11
// global variables used in this module only
12
static wchar_t connected_comment[4096];
13
static wchar_t filename[MAX_PATH];
14
static float simpleshadow_sizes[] =
15
{
16
   0, // #define PART_NONE 0
17
   2.5f, // #define PART_KING 1
18
   2.45f, // #define PART_QUEEN 2
19
   2.35f, // #define PART_BISHOP 3
20
   2.25f, // #define PART_KNIGHT 4
21
   2.15f, // #define PART_ROOK 5
22
   2.0f, // #define PART_PAWN 6
23
};
24
 
25
 
26
void Scene_Init (scene_t *scene, board_t *board)
27
{
28
   // this function initializes the scene objects array and inserts the chess table in it
29
 
30
   wchar_t format_string[4096];
31
 
32
   // allocate array for the board (3 objects) and zero it out
33
   scene->objects = (sceneobject_t *) SAFE_malloc (3, sizeof (sceneobject_t), true);
34
 
35
   // insert the table edges, the table and the board
36
   scene->objects[0].mesh_index = theme->trim_meshindex;
37
   scene->objects[0].texture_index = theme->trim_texture;
38
   scene->objects[0].scale = 1.0f;
39
   scene->objects[1].mesh_index = theme->table_meshindex;
40
   scene->objects[1].texture_index = theme->table_texture;
41
   scene->objects[1].scale = 1.0f;
42
   scene->objects[2].mesh_index = theme->board_meshindex;
43
   scene->objects[2].texture_index = theme->board_texture;
44
   scene->objects[2].scale = 1.0f;
45
   scene->object_count = 3;
46
 
47
   // reset the camera position for a cool slide-in effect, but only if autorotate is enabled
48
   if (options.want_autorotateon1vs1)
49
   {
21 pmbaty 50
      current_pitch = 6.0f; // autorotate is enabled, prepare for slide-in effect
1 pmbaty 51
      current_yaw = (Board_ColorToMove (board) == COLOR_WHITE ? 90.0f : -90.0f);
52
      current_distance = 40.0f;
53
   }
54
   else
55
   {
56
      current_pitch = 55.0f; // no autorotate, reset to standard view position directly
57
      current_yaw = (Board_ColorToMove (board) == COLOR_BLACK ? 90.0f : -90.0f);
58
      current_distance = 70.0f;
59
   }
60
 
21 pmbaty 61
   // HACK to prevent the player to click and block the view angles while the slide-in is not finished
62
   command_ignoretime = current_time + 2.0f; // allow 2 seconds
63
 
1 pmbaty 64
   // build the connected comment string (we use it as a global variable)
65
   wcscpy_s (format_string, WCHAR_SIZEOF (format_string), LOCALIZE (L"YouAreConnectedToX"));
66
   wcscat_s (format_string, WCHAR_SIZEOF (format_string), L"\n");
67
   wcscat_s (format_string, WCHAR_SIZEOF (format_string), LOCALIZE (L"YouMayEitherDisplayThePlayersListOrTheSoughtGamesList"));
68
   wcscat_s (format_string, WCHAR_SIZEOF (format_string), L"\n");
69
   wcscat_s (format_string, WCHAR_SIZEOF (format_string), LOCALIZE (L"PleaseReferToTheInternetMenu"));
70
   swprintf_s (connected_comment, WCHAR_SIZEOF (connected_comment), format_string, options.network.server_address);
71
 
72
   // completely reset the whole GUI structure so as to NULL out all pointers
73
   memset (&scene->gui, 0, sizeof (scene->gui));
74
 
75
   // set the buttons locations
76
   Scene_SetButton (&scene->gui.larrow, 0.3f, 0.5f, 3.0f, 4.0f, larrow_spriteindex);
77
   Scene_SetButton (&scene->gui.rarrow, 3.3f, 0.5f, 3.0f, 4.0f, rarrow_spriteindex);
78
   Scene_SetButton (&scene->gui.chatbutton, 1.0f, 10.0f, 10.0f, 13.0f, chatbutton_spriteindex);
79
   Scene_SetButton (&scene->gui.gamesbutton, 1.0f, 35.0f, 10.0f, 13.0f, gamesbutton_spriteindex);
80
   Scene_SetButton (&scene->gui.peoplebutton, 1.0f, 60.0f, 10.0f, 13.0f, peoplebutton_spriteindex);
81
 
82
   // remember to update the scene
83
   scene->update = true;
84
 
85
   return; // finished
86
}
87
 
88
 
89
void Scene_Shutdown (scene_t *scene)
90
{
91
   // this function frees the memory space allocated for the 3D scene and clears its pointers
92
 
93
   int cchistory_index;
94
 
95
   // free GUI mallocated buffers
96
   SAFE_free ((void **) &scene->gui.arrow_text.buffer);
97
   scene->gui.arrow_text.is_displayed = false;
98
   SAFE_free ((void **) &scene->gui.comment_text.buffer);
99
   scene->gui.comment_text.is_displayed = false;
100
   SAFE_free ((void **) &scene->gui.history_text.buffer);
101
   scene->gui.history_text.is_displayed = false;
102
   SAFE_free ((void **) &scene->gui.clock_text.buffer);
103
   scene->gui.clock_text.is_displayed = false;
104
   SAFE_free ((void **) &scene->gui.turn_text.buffer);
105
   scene->gui.turn_text.is_displayed = false;
106
   SAFE_free ((void **) &scene->gui.central_text.buffer);
107
   scene->gui.central_text.is_displayed = false;
108
 
109
   // for each entry in the CC history...
110
   for (cchistory_index = 0; cchistory_index < scene->gui.cchistory_count; cchistory_index++)
111
      SAFE_free ((void **) &scene->gui.cchistory[cchistory_index].text); // free its text buffer
112
   SAFE_free ((void **) &scene->gui.cchistory); // free the GUI's CC history
113
   scene->gui.cchistory_count = 0;
114
 
115
   SAFE_free ((void **) &scene->objects); // free the scene objects array
116
   scene->object_count = 0;
117
 
118
   return; // finished
119
}
120
 
121
 
122
void Scene_Update (scene_t *scene, board_t *board)
123
{
124
   // this function updates the scene objects to display with what's currently on the board
125
 
126
   static bool rooksound_played = false; // hack to have two sounds when a king castles
127
 
128
   boardslot_t *boardslot;
129
   boardmove_t *currentmove;
130
   player_t *local_player;
131
   player_t *network_player;
132
   player_t *current_player;
24 pmbaty 133
   player_t *opposite_player;
1 pmbaty 134
   wchar_t *history_text; // mallocated
135
   int historytext_size;
136
   unsigned char takenpart_type;
137
   int movement_direction;
138
   int line;
139
   int column;
140
   int pos_index;
141
   int part_index;
142
   int move_index;
143
   int start_index;
144
   int length;
145
   int threat_line;
146
   int threat_column;
147
   int minutes;
148
   int seconds;
25 pmbaty 149
   float flaticons_yaw;
1 pmbaty 150
   float source_x;
151
   float source_y;
152
   float target_x;
153
   float target_y;
154
   float current_x;
155
   float current_y;
156
   float current_z;
157
   int movement_diffco;
158
   int movement_diffli;
159
   float movement_maxheight;
160
   float movement_ratio;
161
 
24 pmbaty 162
   // get the current player (we'll need it), its opponent and see if we're online
1 pmbaty 163
   current_player = Player_GetCurrent ();
24 pmbaty 164
   opposite_player = Player_GetOpposite ();
1 pmbaty 165
   network_player = Player_FindByType (PLAYER_INTERNET);
166
 
25 pmbaty 167
   // determine display yaw (for flat icons) according to camera angle
168
   if ((current_yaw > 45.0f) && (current_yaw <= 135.0f))
169
      flaticons_yaw = 180.0f;
170
   else if ((current_yaw > -45.0f) && (current_yaw <= 45.0f))
171
      flaticons_yaw = 90.0f;
172
   else if ((current_yaw > -135.0f) && (current_yaw <= -45.0f))
173
      flaticons_yaw = 0.0f;
174
   else
175
      flaticons_yaw = -90.0f;
176
 
24 pmbaty 177
   // get the current move
178
   currentmove = &board->moves[board->viewed_move]; // quick access to current move
179
 
1 pmbaty 180
   // fetch the background sprite from the theme
181
   if (want_custombackground)
182
      scene->background_spriteindex = custombg.sprite_index; // use the custom background if specified
183
   else
184
      scene->background_spriteindex = theme->bg.sprite_index; // else use the theme's supplied background
185
 
186
   // shrink the scene objects array to leave just the board (3 objects)
187
   scene->objects = (sceneobject_t *) SAFE_realloc (scene->objects, scene->object_count, 3, sizeof (sceneobject_t), false);
188
   scene->object_count = 3;
189
 
190
   // update the board theme
191
   scene->objects[0].mesh_index = theme->trim_meshindex;
192
   scene->objects[0].texture_index = theme->trim_texture;
193
   scene->objects[0].material_index = theme->trim_material;
194
   scene->objects[0].scale = 1.0f;
195
   scene->objects[1].mesh_index = theme->table_meshindex;
196
   scene->objects[1].texture_index = theme->table_texture;
197
   scene->objects[1].material_index = theme->table_material;
198
   scene->objects[1].scale = 1.0f;
199
   scene->objects[2].mesh_index = theme->board_meshindex;
200
   scene->objects[2].texture_index = theme->board_texture;
201
   scene->objects[2].material_index = theme->board_material;
202
   scene->objects[2].scale = 1.0f;
203
 
204
   // draw the grid numbers if we want them
205
   if (want_grid)
206
      Scene_AddTile (scene, theme->grid_texture, 30.0f, 0.0f, 0.0f, 0.01f, 0.0f);
207
 
208
   ////////////////////////////////////////////////////////////////////////////////////////////////
24 pmbaty 209
   // recompute the slot textures (only in play mode when we render the real board)
1 pmbaty 210
 
211
   // erase all the slot flags
212
   for (line = 0; line < 8; line++)
213
      for (column = 0; column < 8; column++)
214
         currentmove->slots[line][column].flags = FLAG_NONE; // because we will recompute them
215
 
216
   // cycle through all the grid again and see if either king is in check
217
   for (line = 0; line < 8; line++)
218
      for (column = 0; column < 8; column++)
219
      {
220
         boardslot = &currentmove->slots[line][column]; // quick access to grid slot
221
 
222
         if (boardslot->part != PART_KING)
223
            continue; // if this slot is not a king, skip it
224
 
225
         // is this king currently threatened ?
226
         if (Move_IsKingThreatenedAtLocation (currentmove, boardslot->color, line, column, &threat_line, &threat_column))
227
         {
228
            currentmove->slots[line][column].flags |= FLAG_CHECK; // mark it as threatened
229
            currentmove->slots[threat_line][threat_column].flags |= FLAG_THREAT; // and who threatens it
230
         }
231
      }
232
 
233
   // are we in play mode ? i.e, are we rendering the last move ?
234
   if (board->viewed_move == board->move_count - 1)
235
   {
236
      // mark the selected position as selected
237
      if (IS_VALID_POSITION (board->selected_position))
238
         currentmove->slots[board->selected_position[0]][board->selected_position[1]].flags |= FLAG_SELECTED;
239
 
240
      // now cycle through all the grid again and see if some slots need to be coloured
241
      for (line = 0; line < 8; line++)
242
         for (column = 0; column < 8; column++)
243
         {
244
            boardslot = &currentmove->slots[line][column]; // quick access to grid slot
245
 
246
            if (!(boardslot->flags & FLAG_SELECTED))
247
               continue; // if this slot is not selected, skip it
248
 
249
            //////////////////////////////////////////////////////////////////////////////////////////
250
            ////////////////////////////////////////// PAWN //////////////////////////////////////////
251
            //////////////////////////////////////////////////////////////////////////////////////////
252
            // is it a pawn ?
253
            if (boardslot->part == PART_PAWN)
254
            {
255
               // figure out movement direction
256
               if (boardslot->color == COLOR_WHITE)
257
                  movement_direction = 1;
258
               else
259
                  movement_direction = -1;
260
 
261
               // if pawn has still room to move forward, it can
262
               if ((((line < 7) && (movement_direction == 1)) || ((line > 0) && (movement_direction == -1)))
263
                   && (currentmove->slots[line + movement_direction][column].part == PART_NONE))
264
               {
265
                  currentmove->slots[line + movement_direction][column].flags |= FLAG_POSSIBLEMOVE;
266
 
267
                  // if pawn is still in its initial slot, it can move twice forward
268
                  if ((((line == 1) && (movement_direction == 1)) || ((line == 6) && (movement_direction == -1)))
269
                      && (currentmove->slots[line + movement_direction * 2][column].part == PART_NONE))
270
                     currentmove->slots[line + movement_direction * 2][column].flags |= FLAG_POSSIBLEMOVE;
271
               }
272
 
273
               // see if pawn can take a piece on its left
274
               if ((column > 0) && (currentmove->slots[line + movement_direction][column - 1].part != PART_NONE)
275
                   && (currentmove->slots[line + movement_direction][column - 1].color != currentmove->slots[line][column].color))
276
                  currentmove->slots[line + movement_direction][column - 1].flags |= FLAG_TAKEABLE;
277
 
278
               // see if pawn can take a piece on its right
279
               if ((column < 7) && (currentmove->slots[line + movement_direction][column + 1].part != PART_NONE)
280
                   && (currentmove->slots[line + movement_direction][column + 1].color != currentmove->slots[line][column].color))
281
                  currentmove->slots[line + movement_direction][column + 1].flags |= FLAG_TAKEABLE;
282
 
283
               // if previous move was a pawn rush, see if pawn can take "en passant"
284
               if ((currentmove->part == PART_PAWN)
285
                     && (currentmove->target[1] == currentmove->source[1]) // pawn moved in column
286
                     && (abs (currentmove->target[0] - currentmove->source[0]) == 2) // pawn rushed
287
                     && (currentmove->target[0] == line) // pawn is in line with us
288
                     && (abs (currentmove->target[1] - column) == 1)) // pawn is next to us
289
                  currentmove->slots[line + movement_direction][currentmove->target[1]].flags |= FLAG_TAKEABLE;
290
            }
291
 
292
            //////////////////////////////////////////////////////////////////////////////////////////
293
            ////////////////////////////////////////// ROOK //////////////////////////////////////////
294
            //////////////////////////////////////////////////////////////////////////////////////////
295
            // else is it a rook ?
296
            else if (boardslot->part == PART_ROOK)
297
            {
298
               // see how far rook can move upwards
299
               for (pos_index = line + 1; pos_index < 8; pos_index++)
300
               {
301
                  if (currentmove->slots[pos_index][column].part != PART_NONE)
302
                  {
303
                     if (currentmove->slots[pos_index][column].color != currentmove->slots[line][column].color)
304
                        currentmove->slots[pos_index][column].flags |= FLAG_TAKEABLE;
305
                     break;
306
                  }
307
                  currentmove->slots[pos_index][column].flags |= FLAG_POSSIBLEMOVE;
308
               }
309
 
310
               // see how far rook can move downwards
311
               for (pos_index = line - 1; pos_index >= 0; pos_index--)
312
               {
313
                  if (currentmove->slots[pos_index][column].part != PART_NONE)
314
                  {
315
                     if (currentmove->slots[pos_index][column].color != currentmove->slots[line][column].color)
316
                        currentmove->slots[pos_index][column].flags |= FLAG_TAKEABLE;
317
                     break;
318
                  }
319
                  currentmove->slots[pos_index][column].flags |= FLAG_POSSIBLEMOVE;
320
               }
321
 
322
               // see how far rook can move left
323
               for (pos_index = column - 1; pos_index >= 0; pos_index--)
324
               {
325
                  if (currentmove->slots[line][pos_index].part != PART_NONE)
326
                  {
327
                     if (currentmove->slots[line][pos_index].color != currentmove->slots[line][column].color)
328
                        currentmove->slots[line][pos_index].flags |= FLAG_TAKEABLE;
329
                     break;
330
                  }
331
                  currentmove->slots[line][pos_index].flags |= FLAG_POSSIBLEMOVE;
332
               }
333
 
334
               // see how far rook can move right
335
               for (pos_index = column + 1; pos_index < 8; pos_index++)
336
               {
337
                  if (currentmove->slots[line][pos_index].part != PART_NONE)
338
                  {
339
                     if (currentmove->slots[line][pos_index].color != currentmove->slots[line][column].color)
340
                        currentmove->slots[line][pos_index].flags |= FLAG_TAKEABLE;
341
                     break;
342
                  }
343
                  currentmove->slots[line][pos_index].flags |= FLAG_POSSIBLEMOVE;
344
               }
345
            }
346
 
347
            //////////////////////////////////////////////////////////////////////////////////////////
348
            ///////////////////////////////////////// KNIGHT /////////////////////////////////////////
349
            //////////////////////////////////////////////////////////////////////////////////////////
350
            // else is it a knight ?
351
            else if (boardslot->part == PART_KNIGHT)
352
            {
353
               // peek knight's NNW move
354
               if ((column > 0) && (line < 6))
355
                  if (currentmove->slots[line + 2][column - 1].part == PART_NONE)
356
                     currentmove->slots[line + 2][column - 1].flags |= FLAG_POSSIBLEMOVE;
357
                  else if (currentmove->slots[line + 2][column - 1].color != currentmove->slots[line][column].color)
358
                     currentmove->slots[line + 2][column - 1].flags |= FLAG_TAKEABLE;
359
 
360
               // peek knight's NNE move
361
               if ((column < 7) && (line < 6))
362
                  if (currentmove->slots[line + 2][column + 1].part == PART_NONE)
363
                     currentmove->slots[line + 2][column + 1].flags |= FLAG_POSSIBLEMOVE;
364
                  else if (currentmove->slots[line + 2][column + 1].color != currentmove->slots[line][column].color)
365
                     currentmove->slots[line + 2][column + 1].flags |= FLAG_TAKEABLE;
366
 
367
               // peek knight's ENE move
368
               if ((column < 6) && (line < 7))
369
                  if (currentmove->slots[line + 1][column + 2].part == PART_NONE)
370
                     currentmove->slots[line + 1][column + 2].flags |= FLAG_POSSIBLEMOVE;
371
                  else if (currentmove->slots[line + 1][column + 2].color != currentmove->slots[line][column].color)
372
                     currentmove->slots[line + 1][column + 2].flags |= FLAG_TAKEABLE;
373
 
374
               // peek knight's ESE move
375
               if ((column < 6) && (line > 0))
376
                  if (currentmove->slots[line - 1][column + 2].part == PART_NONE)
377
                     currentmove->slots[line - 1][column + 2].flags |= FLAG_POSSIBLEMOVE;
378
                  else if (currentmove->slots[line - 1][column + 2].color != currentmove->slots[line][column].color)
379
                     currentmove->slots[line - 1][column + 2].flags |= FLAG_TAKEABLE;
380
 
381
               // peek knight's SSW move
382
               if ((column > 0) && (line > 1))
383
                  if (currentmove->slots[line - 2][column - 1].part == PART_NONE)
384
                     currentmove->slots[line - 2][column - 1].flags |= FLAG_POSSIBLEMOVE;
385
                  else if (currentmove->slots[line - 2][column - 1].color != currentmove->slots[line][column].color)
386
                     currentmove->slots[line - 2][column - 1].flags |= FLAG_TAKEABLE;
387
 
388
               // peek knight's SSE move
389
               if ((column < 7) && (line > 1))
390
                  if (currentmove->slots[line - 2][column + 1].part == PART_NONE)
391
                     currentmove->slots[line - 2][column + 1].flags |= FLAG_POSSIBLEMOVE;
392
                  else if (currentmove->slots[line - 2][column + 1].color != currentmove->slots[line][column].color)
393
                     currentmove->slots[line - 2][column + 1].flags |= FLAG_TAKEABLE;
394
 
395
               // peek knight's WNW move
396
               if ((column > 1) && (line < 7))
397
                  if (currentmove->slots[line + 1][column - 2].part == PART_NONE)
398
                     currentmove->slots[line + 1][column - 2].flags |= FLAG_POSSIBLEMOVE;
399
                  else if (currentmove->slots[line + 1][column - 2].color != currentmove->slots[line][column].color)
400
                     currentmove->slots[line + 1][column - 2].flags |= FLAG_TAKEABLE;
401
 
402
               // peek knight's WSW move
403
               if ((column > 1) && (line > 0))
404
                  if (currentmove->slots[line - 1][column - 2].part == PART_NONE)
405
                     currentmove->slots[line - 1][column - 2].flags |= FLAG_POSSIBLEMOVE;
406
                  else if (currentmove->slots[line - 1][column - 2].color != currentmove->slots[line][column].color)
407
                     currentmove->slots[line - 1][column - 2].flags |= FLAG_TAKEABLE;
408
            }
409
 
410
            //////////////////////////////////////////////////////////////////////////////////////////
411
            ///////////////////////////////////////// BISHOP /////////////////////////////////////////
412
            //////////////////////////////////////////////////////////////////////////////////////////
413
            // else is it a bishop ?
414
            else if (boardslot->part == PART_BISHOP)
415
            {
416
               // see how far bishop can move NE
417
               for (pos_index = 1; pos_index < 8; pos_index++)
418
               {
419
                  if ((line + pos_index > 7) || (column + pos_index > 7))
420
                     break;
421
                  if (currentmove->slots[line + pos_index][column + pos_index].part != PART_NONE)
422
                  {
423
                     if (currentmove->slots[line + pos_index][column + pos_index].color != currentmove->slots[line][column].color)
424
                        currentmove->slots[line + pos_index][column + pos_index].flags |= FLAG_TAKEABLE;
425
                     break;
426
                  }
427
                  currentmove->slots[line + pos_index][column + pos_index].flags |= FLAG_POSSIBLEMOVE;
428
               }
429
 
430
               // see how far bishop can move SE
431
               for (pos_index = 1; pos_index < 8; pos_index++)
432
               {
433
                  if ((line - pos_index < 0) || (column + pos_index > 7))
434
                     break;
435
                  if (currentmove->slots[line - pos_index][column + pos_index].part != PART_NONE)
436
                  {
437
                     if (currentmove->slots[line - pos_index][column + pos_index].color != currentmove->slots[line][column].color)
438
                        currentmove->slots[line - pos_index][column + pos_index].flags |= FLAG_TAKEABLE;
439
                     break;
440
                  }
441
                  currentmove->slots[line - pos_index][column + pos_index].flags |= FLAG_POSSIBLEMOVE;
442
               }
443
 
444
               // see how far bishop can move NW
445
               for (pos_index = 1; pos_index < 8; pos_index++)
446
               {
447
                  if ((line + pos_index > 7) || (column - pos_index < 0))
448
                     break;
449
                  if (currentmove->slots[line + pos_index][column - pos_index].part != PART_NONE)
450
                  {
451
                     if (currentmove->slots[line + pos_index][column - pos_index].color != currentmove->slots[line][column].color)
452
                        currentmove->slots[line + pos_index][column - pos_index].flags |= FLAG_TAKEABLE;
453
                     break;
454
                  }
455
                  currentmove->slots[line + pos_index][column - pos_index].flags |= FLAG_POSSIBLEMOVE;
456
               }
457
 
458
               // see how far bishop can move SW
459
               for (pos_index = 1; pos_index < 8; pos_index++)
460
               {
461
                  if ((line - pos_index < 0) || (column - pos_index < 0))
462
                     break;
463
                  if (currentmove->slots[line - pos_index][column - pos_index].part != PART_NONE)
464
                  {
465
                     if (currentmove->slots[line - pos_index][column - pos_index].color != currentmove->slots[line][column].color)
466
                        currentmove->slots[line - pos_index][column - pos_index].flags |= FLAG_TAKEABLE;
467
                     break;
468
                  }
469
                  currentmove->slots[line - pos_index][column - pos_index].flags |= FLAG_POSSIBLEMOVE;
470
               }
471
            }
472
 
473
            //////////////////////////////////////////////////////////////////////////////////////////
474
            ///////////////////////////////////////// QUEEN //////////////////////////////////////////
475
            //////////////////////////////////////////////////////////////////////////////////////////
476
            // else is it a queen ?
477
            else if (boardslot->part == PART_QUEEN)
478
            {
479
               // see how far queen can move NE
480
               for (pos_index = 1; pos_index < 8; pos_index++)
481
               {
482
                  if ((line + pos_index > 7) || (column + pos_index > 7))
483
                     break;
484
                  if (currentmove->slots[line + pos_index][column + pos_index].part != PART_NONE)
485
                  {
486
                     if (currentmove->slots[line + pos_index][column + pos_index].color != currentmove->slots[line][column].color)
487
                        currentmove->slots[line + pos_index][column + pos_index].flags |= FLAG_TAKEABLE;
488
                     break;
489
                  }
490
                  currentmove->slots[line + pos_index][column + pos_index].flags |= FLAG_POSSIBLEMOVE;
491
               }
492
 
493
               // see how far queen can move SE
494
               for (pos_index = 1; pos_index < 8; pos_index++)
495
               {
496
                  if ((line - pos_index < 0) || (column + pos_index > 7))
497
                     break;
498
                  if (currentmove->slots[line - pos_index][column + pos_index].part != PART_NONE)
499
                  {
500
                     if (currentmove->slots[line - pos_index][column + pos_index].color != currentmove->slots[line][column].color)
501
                        currentmove->slots[line - pos_index][column + pos_index].flags |= FLAG_TAKEABLE;
502
                     break;
503
                  }
504
                  currentmove->slots[line - pos_index][column + pos_index].flags |= FLAG_POSSIBLEMOVE;
505
               }
506
 
507
               // see how far queen can move NW
508
               for (pos_index = 1; pos_index < 8; pos_index++)
509
               {
510
                  if ((line + pos_index > 7) || (column - pos_index < 0))
511
                     break;
512
                  if (currentmove->slots[line + pos_index][column - pos_index].part != PART_NONE)
513
                  {
514
                     if (currentmove->slots[line + pos_index][column - pos_index].color != currentmove->slots[line][column].color)
515
                        currentmove->slots[line + pos_index][column - pos_index].flags |= FLAG_TAKEABLE;
516
                     break;
517
                  }
518
                  currentmove->slots[line + pos_index][column - pos_index].flags |= FLAG_POSSIBLEMOVE;
519
               }
520
 
521
               // see how far queen can move SW
522
               for (pos_index = 1; pos_index < 8; pos_index++)
523
               {
524
                  if ((line - pos_index < 0) || (column - pos_index < 0))
525
                     break;
526
                  if (currentmove->slots[line - pos_index][column - pos_index].part != PART_NONE)
527
                  {
528
                     if (currentmove->slots[line - pos_index][column - pos_index].color != currentmove->slots[line][column].color)
529
                        currentmove->slots[line - pos_index][column - pos_index].flags |= FLAG_TAKEABLE;
530
                     break;
531
                  }
532
                  currentmove->slots[line - pos_index][column - pos_index].flags |= FLAG_POSSIBLEMOVE;
533
               }
534
 
535
               // see how far queen can move upwards
536
               for (pos_index = line + 1; pos_index < 8; pos_index++)
537
               {
538
                  if (currentmove->slots[pos_index][column].part != PART_NONE)
539
                  {
540
                     if (currentmove->slots[pos_index][column].color != currentmove->slots[line][column].color)
541
                        currentmove->slots[pos_index][column].flags |= FLAG_TAKEABLE;
542
                     break;
543
                  }
544
                  currentmove->slots[pos_index][column].flags |= FLAG_POSSIBLEMOVE;
545
               }
546
 
547
               // see how far queen can move downwards
548
               for (pos_index = line - 1; pos_index >= 0; pos_index--)
549
               {
550
                  if (currentmove->slots[pos_index][column].part != PART_NONE)
551
                  {
552
                     if (currentmove->slots[pos_index][column].color != currentmove->slots[line][column].color)
553
                        currentmove->slots[pos_index][column].flags |= FLAG_TAKEABLE;
554
                     break;
555
                  }
556
                  currentmove->slots[pos_index][column].flags |= FLAG_POSSIBLEMOVE;
557
               }
558
 
559
               // see how far queen can move left
560
               for (pos_index = column - 1; pos_index >= 0; pos_index--)
561
               {
562
                  if (currentmove->slots[line][pos_index].part != PART_NONE)
563
                  {
564
                     if (currentmove->slots[line][pos_index].color != currentmove->slots[line][column].color)
565
                        currentmove->slots[line][pos_index].flags |= FLAG_TAKEABLE;
566
                     break;
567
                  }
568
                  currentmove->slots[line][pos_index].flags |= FLAG_POSSIBLEMOVE;
569
               }
570
 
571
               // see how far queen can move right
572
               for (pos_index = column + 1; pos_index < 8; pos_index++)
573
               {
574
                  if (currentmove->slots[line][pos_index].part != PART_NONE)
575
                  {
576
                     if (currentmove->slots[line][pos_index].color != currentmove->slots[line][column].color)
577
                        currentmove->slots[line][pos_index].flags |= FLAG_TAKEABLE;
578
                     break;
579
                  }
580
                  currentmove->slots[line][pos_index].flags |= FLAG_POSSIBLEMOVE;
581
               }
582
            }
583
 
584
            //////////////////////////////////////////////////////////////////////////////////////////
585
            ////////////////////////////////////////// KING //////////////////////////////////////////
586
            //////////////////////////////////////////////////////////////////////////////////////////
587
            // else is it a king ?
588
            else if (boardslot->part == PART_KING)
589
            {
590
               // see if king can move NE
591
               if ((line < 7) && (column < 7))
592
               {
593
                  if (currentmove->slots[line + 1][column + 1].part == PART_NONE)
594
                     currentmove->slots[line + 1][column + 1].flags |= FLAG_POSSIBLEMOVE;
595
                  else if (currentmove->slots[line + 1][column + 1].color != currentmove->slots[line][column].color)
596
                     currentmove->slots[line + 1][column + 1].flags |= FLAG_TAKEABLE;
597
               }
598
 
599
               // see if king can move SE
600
               if ((line > 0) && (column < 7))
601
               {
602
                  if (currentmove->slots[line - 1][column + 1].part == PART_NONE)
603
                     currentmove->slots[line - 1][column + 1].flags |= FLAG_POSSIBLEMOVE;
604
                  else if (currentmove->slots[line - 1][column + 1].color != currentmove->slots[line][column].color)
605
                     currentmove->slots[line - 1][column + 1].flags |= FLAG_TAKEABLE;
606
               }
607
 
608
               // see if king can move NW
609
               if ((line < 7) && (column > 0))
610
               {
611
                  if (currentmove->slots[line + 1][column - 1].part == PART_NONE)
612
                     currentmove->slots[line + 1][column - 1].flags |= FLAG_POSSIBLEMOVE;
613
                  else if (currentmove->slots[line + 1][column - 1].color != currentmove->slots[line][column].color)
614
                     currentmove->slots[line + 1][column - 1].flags |= FLAG_TAKEABLE;
615
               }
616
 
617
               // see if king can move SW
618
               if ((line > 0) && (column > 0))
619
               {
620
                  if (currentmove->slots[line - 1][column - 1].part == PART_NONE)
621
                     currentmove->slots[line - 1][column - 1].flags |= FLAG_POSSIBLEMOVE;
622
                  else if (currentmove->slots[line - 1][column - 1].color != currentmove->slots[line][column].color)
623
                     currentmove->slots[line - 1][column - 1].flags |= FLAG_TAKEABLE;
624
               }
625
 
626
               // see if king can move upwards
627
               if (line < 7)
628
               {
629
                  if (currentmove->slots[line + 1][column].part == PART_NONE)
630
                     currentmove->slots[line + 1][column].flags |= FLAG_POSSIBLEMOVE;
631
                  else if (currentmove->slots[line + 1][column].color != currentmove->slots[line][column].color)
632
                     currentmove->slots[line + 1][column].flags |= FLAG_TAKEABLE;
633
               }
634
 
635
               // see if king can move downwards
636
               if (line > 0)
637
               {
638
                  if (currentmove->slots[line - 1][column].part == PART_NONE)
639
                     currentmove->slots[line - 1][column].flags |= FLAG_POSSIBLEMOVE;
640
                  else if (currentmove->slots[line - 1][column].color != currentmove->slots[line][column].color)
641
                     currentmove->slots[line - 1][column].flags |= FLAG_TAKEABLE;
642
               }
643
 
644
               // see if king can move right
645
               if (column < 7)
646
               {
647
                  if (currentmove->slots[line][column + 1].part == PART_NONE)
648
                     currentmove->slots[line][column + 1].flags |= FLAG_POSSIBLEMOVE;
649
                  else if (currentmove->slots[line][column + 1].color != currentmove->slots[line][column].color)
650
                     currentmove->slots[line][column + 1].flags |= FLAG_TAKEABLE;
651
               }
652
 
653
               // see if king can move left
654
               if (column > 0)
655
               {
656
                  if (currentmove->slots[line][column - 1].part == PART_NONE)
657
                     currentmove->slots[line][column - 1].flags |= FLAG_POSSIBLEMOVE;
658
                  else if (currentmove->slots[line][column - 1].color != currentmove->slots[line][column].color)
659
                     currentmove->slots[line][column - 1].flags |= FLAG_TAKEABLE;
660
               }
661
 
662
               // can king castle bishopside ?
663
               if (currentmove->sides[boardslot->color].shortcastle_allowed // no parts have moved yet
664
                   && (currentmove->slots[line][5].part == PART_NONE) // no other part...
665
                   && (currentmove->slots[line][6].part == PART_NONE) // ...in the way
666
                   && !Move_IsCheck (currentmove, boardslot->color) // king not currently in check
667
                   && !Move_IsKingThreatenedAtLocation (currentmove, boardslot->color, line, 5, &threat_line, &threat_column)) // king's way safe
668
                  currentmove->slots[line][column + 2].flags |= FLAG_POSSIBLEMOVE; // allow castling bishopside
669
 
670
               // can king castle queenside ?
671
               if (currentmove->sides[boardslot->color].longcastle_allowed // no parts have moved yet
672
                   && (currentmove->slots[line][3].part == PART_NONE) // no other part...
673
                   && (currentmove->slots[line][2].part == PART_NONE) // ...is...
674
                   && (currentmove->slots[line][1].part == PART_NONE) // ...in the way
675
                   && !Move_IsCheck (currentmove, boardslot->color) // king not currently in check
676
                   && !Move_IsKingThreatenedAtLocation (currentmove, boardslot->color, line, 3, &threat_line, &threat_column)) // king's way safe
677
                  currentmove->slots[line][column - 2].flags |= FLAG_POSSIBLEMOVE; // allow castling queenside
678
            }
679
         }
680
   } // end if (play mode)
681
 
682
   ////////////////////////////////////////////////////////////////////////////////////////////////
25 pmbaty 683
   // now place the parts that are off-board (taken parts)
1 pmbaty 684
 
685
   // but only if we want them displayed
686
   if (options.want_takenparts)
687
   {
688
      // draw the white player's taken parts, place the part off the board (watch out for the hack...)
689
      for (part_index = 0; part_index < currentmove->sides[COLOR_WHITE].takenpart_count; part_index++)
690
      {
691
         takenpart_type = currentmove->sides[COLOR_WHITE].takenparts[part_index];
25 pmbaty 692
 
693
         // do we want the 3D models or the flat icons ?
694
         // we do when EITHER we don't want flat icons OR the current player is looking downstraight
695
         if (!want_flaticons || (current_pitch < MAX_PITCH_FOR_FLAT_ICONS))
696
            Scene_AddPart (scene, takenpart_type, COLOR_BLACK, (part_index < MAX_STACKABLE_PARTS ? 23.2f : 26.8f),
697
                           (part_index % MAX_STACKABLE_PARTS == 0 ? 23.2f : scene->objects[scene->object_count - 1].y - (scene->objects[scene->object_count - 1].simpleshadow_size + simpleshadow_sizes[takenpart_type]) * 0.75f),
698
                           0.04f, (takenpart_type == PART_PAWN ? -90.0f : 100.0f), 0.0f); // rotate pawns 90°
699
         else
700
            Scene_AddTile (scene, theme->flattextures[COLOR_BLACK][takenpart_type], 1.25f, (part_index < MAX_STACKABLE_PARTS ? 23.2f : 26.8f),
701
                           20.0f - (part_index < MAX_STACKABLE_PARTS ? part_index : (part_index - MAX_STACKABLE_PARTS)) * 2.5f, 0.07f, flaticons_yaw);
1 pmbaty 702
      }
703
 
704
      // now draw the black player's taken parts, place the part off the board (watch out for the hack...)
705
      for (part_index = 0; part_index < currentmove->sides[COLOR_BLACK].takenpart_count; part_index++)
706
      {
707
         takenpart_type = currentmove->sides[COLOR_BLACK].takenparts[part_index];
25 pmbaty 708
 
709
         // do we want the 3D models or the flat icons ?
710
         // we do when EITHER we don't want flat icons OR the current player is looking downstraight
711
         if (!want_flaticons || (current_pitch < MAX_PITCH_FOR_FLAT_ICONS))
712
            Scene_AddPart (scene, takenpart_type, COLOR_WHITE, (part_index < MAX_STACKABLE_PARTS ? -23.2f : -26.8f),
713
                           (part_index % MAX_STACKABLE_PARTS == 0 ? -23.2f : scene->objects[scene->object_count - 1].y + (scene->objects[scene->object_count - 1].simpleshadow_size + simpleshadow_sizes[takenpart_type]) * 0.75f),
714
                           0.04f, (takenpart_type == PART_PAWN ? -90.0f : 100.0f), 0.0f); // rotate pawns 90°
715
         else
716
            Scene_AddTile (scene, theme->flattextures[COLOR_WHITE][takenpart_type], 1.25f, (part_index < MAX_STACKABLE_PARTS ? -23.2f : -26.8f),
717
                           -20.0f + (part_index < MAX_STACKABLE_PARTS ? part_index : (part_index - MAX_STACKABLE_PARTS)) * 2.5f, 0.07f, flaticons_yaw);
1 pmbaty 718
      }
719
   }
720
 
721
   ////////////////////////////////////////////////////////////////////////////////////////////////
722
   // now draw the textured slots
723
 
724
   // cycle through all the board slots...
725
   for (line = 0; line < 8; line++)
726
      for (column = 0; column < 8; column++)
727
      {
728
         if (currentmove->slots[line][column].flags == FLAG_NONE)
729
            continue; // skip everything that doesn't need to be drawn
730
 
731
         // draw the texture we want. Only one texture is allowed, so PRIORITY MATTERS.
732
         if (currentmove->slots[line][column].flags & FLAG_SELECTED)
733
            Scene_AddTile (scene, theme->selected_textureindex, 2.5f, 17.5f - (7 - column) * 5.0f, 17.5f - line * 5.0f, 0.02f, 0.0f);
734
         else if (options.want_threats && currentmove->slots[line][column].flags & FLAG_CHECK)
735
            Scene_AddTile (scene, theme->check_textureindex, 2.5f, 17.5f - (7 - column) * 5.0f, 17.5f - line * 5.0f, 0.02f, 0.0f);
736
         else if (options.want_possiblemoves && currentmove->slots[line][column].flags & FLAG_POSSIBLEMOVE)
737
            Scene_AddTile (scene, theme->possiblemove_textureindex, 2.5f, 17.5f - (7 - column) * 5.0f, 17.5f - line * 5.0f, 0.02f, 0.0f);
738
         else if (options.want_possiblemoves && currentmove->slots[line][column].flags & FLAG_TAKEABLE)
739
            Scene_AddTile (scene, theme->takeable_textureindex, 2.5f, 17.5f - (7 - column) * 5.0f, 17.5f - line * 5.0f, 0.02f, 0.0f);
740
         else if (options.want_threats && currentmove->slots[line][column].flags & FLAG_THREAT)
741
            Scene_AddTile (scene, theme->threat_textureindex, 2.5f, 17.5f - (7 - column) * 5.0f, 17.5f - line * 5.0f, 0.02f, 0.0f);
742
      }
743
 
744
   // is it time to draw the hovered slot and last move textures on the board slots ?
745
   if (animation_endtime < current_time)
746
   {
747
      // add the hovered slot tile (only when it should not be hidden because of highlighting)
75 pmbaty 748
      if (!is_paused && IS_VALID_POSITION (board->hovered_position)
1 pmbaty 749
          && ((highlight_endtime < current_time) || ((int) ((highlight_endtime - current_time) * 20.0f) % 2 == 1)))
750
         Scene_AddTile (scene, theme->hovered_textureindex, 3.1f,
751
                        17.5f - (7 - board->hovered_position[1]) * 5.0f,
752
                        17.5f - board->hovered_position[0] * 5.0f,
753
                        0.03f, 0.0f); // hovered tile
754
 
755
      // and the previous move source and target
756
      if (options.want_lastmove && (board->move_count > 1) && (board->viewed_move > 0)
757
          && IS_VALID_POSITION (board->moves[board->viewed_move].source)
758
          && IS_VALID_POSITION (board->moves[board->viewed_move].target))
759
      {
760
         Scene_AddTile (scene, theme->lastmovesource_textureindex, 2.5f,
761
                        17.5f - (7 - board->moves[board->viewed_move].source[1]) * 5.0f,
762
                        17.5f - board->moves[board->viewed_move].source[0] * 5.0f,
763
                        0.04f, 0.0f); // previous move source
764
         Scene_AddTile (scene, theme->lastmovetarget_textureindex, 2.5f,
765
                        17.5f - (7 - board->moves[board->viewed_move].target[1]) * 5.0f,
766
                        17.5f - board->moves[board->viewed_move].target[0] * 5.0f,
767
                        0.04f, 0.0f); // previous move target
768
      }
769
   }
770
 
771
   ////////////////////////////////////////////////////////////////////////////////////////////////
24 pmbaty 772
   // now draw the 3D parts that are still in play
1 pmbaty 773
 
24 pmbaty 774
   // cycle through all the grid and place all static parts
775
   for (line = 0; line < 8; line++)
1 pmbaty 776
   {
24 pmbaty 777
      for (column = 0; column < 8; column++)
778
      {
779
         boardslot = &currentmove->slots[line][column]; // quick access to grid slot
1 pmbaty 780
 
24 pmbaty 781
         // is there nothing on this grid slot ?
782
         if (boardslot->part == PART_NONE)
783
            continue; // then don't draw anything on it
784
 
785
         // is this part not animated ? i.e.:
786
         // has no movement happened yet
787
         // OR does the movement NOT land on this slot
788
         //    AND is it NOT the tower concerned by a castle ?
789
         if ((board->viewed_move == 0)
790
             || (((line != currentmove->target[0]) || (column != currentmove->target[1]))
791
                 && ((towupper (currentmove->pgntext[0]) != L'O')
792
                     || (line != (currentmove->color == COLOR_WHITE ? 0 : 7)) || (column != (currentmove->target[1] == 2 ? 3 : 5)))))
1 pmbaty 793
         {
25 pmbaty 794
            // do we want the 3D models or the flat icons ?
795
            // we do when EITHER we don't want flat icons OR the player playing the current move is looking downstraight
796
            if (!want_flaticons || (current_pitch < MAX_PITCH_FOR_FLAT_ICONS))
797
               Scene_AddPart (scene, boardslot->part, boardslot->color, 17.5f - (7 - column) * 5.0f, 17.5f - line * 5.0f, 0.04f, 0.0f, 0.0f);
24 pmbaty 798
            else
25 pmbaty 799
               Scene_AddTile (scene, theme->flattextures[boardslot->color][boardslot->part], 2.5f, 17.5f - (7 - column) * 5.0f, 17.5f - line * 5.0f, 0.05f, flaticons_yaw);
24 pmbaty 800
         }
801
      } // end for (column = 0; column < 8; column++)
802
   } // end for (line = 0; line < 8; line++)
1 pmbaty 803
 
24 pmbaty 804
   // now do another pass and draw the part(s) that are currently moving
805
   for (line = 0; line < 8; line++)
806
   {
807
      for (column = 0; column < 8; column++)
808
      {
809
         boardslot = &currentmove->slots[line][column]; // quick access to grid slot
1 pmbaty 810
 
24 pmbaty 811
         // is there nothing on this grid slot ?
812
         if (boardslot->part == PART_NONE)
813
            continue; // then don't draw anything on it
814
 
815
         // has a movement happened yet AND does it land on this slot ?
816
         if ((board->viewed_move > 0)
817
             && (line == currentmove->target[0]) && (column == currentmove->target[1]))
818
         {
819
            // do we want animations AND is it still time to play the animation ?
820
            if (options.want_animations && (animation_endtime > current_time))
821
            {
822
               // get the source and target X and Y positions
823
               source_x = 17.5f - (7 - currentmove->source[1]) * 5.0f;
824
               source_y = 17.5f - currentmove->source[0] * 5.0f;
825
               target_x = 17.5f - (7 - currentmove->target[1]) * 5.0f;
826
               target_y = 17.5f - currentmove->target[0] * 5.0f;
827
 
828
               // compute the movement completion ratio between 0 and 1
829
               movement_ratio = 1.0f - (animation_endtime - current_time) / ANIMATION_DURATION;
830
 
831
               // compute the current X an Y based on movement timing
832
               current_x = source_x + (target_x - source_x) * movement_ratio;
833
               current_y = source_y + (target_y - source_y) * movement_ratio;
834
 
835
               // do we want the 3D models or the flat icons ?
836
               // we do when EITHER we don't want flat icons OR the player playing the current move is looking downstraight
25 pmbaty 837
               if (!want_flaticons || (current_pitch < MAX_PITCH_FOR_FLAT_ICONS))
24 pmbaty 838
               {
839
                  // height is a sine positive, max height is proportional to travelled distance
840
                  movement_diffco = abs (currentmove->target[1] - currentmove->source[1]);
841
                  movement_diffli = abs (currentmove->target[0] - currentmove->source[0]);
842
                  movement_maxheight = 0.5f + (float) movement_diffco + (float) movement_diffli;
843
                  if (currentmove->part == PART_KNIGHT)
844
                     movement_maxheight *= 2.0f; // knights jump high
845
                  else if ((currentmove->part == PART_KING) && (movement_diffco == 2))
846
                     movement_maxheight *= 5.0f; // kings jump VERY high when castling too
847
                  else if (movement_maxheight > 5.0f)
848
                     movement_maxheight = 5.0f; // all other parts just hover above the ground
81 pmbaty 849
                  current_z = 0.04f + (float) sin (MATH_PI * movement_ratio) * movement_maxheight;
24 pmbaty 850
 
851
                  // make this part move realistically
852
                  Scene_AddPart (scene, boardslot->part, boardslot->color, current_x, current_y, current_z, 0.0f, (boardslot->color == COLOR_BLACK ? -1 : 1) * min (current_z * 3.0f, 10.0f));
853
               }
854
               else
25 pmbaty 855
                  Scene_AddTile (scene, theme->flattextures[boardslot->color][boardslot->part], 2.5f, current_x, current_y, 0.07f, flaticons_yaw);
24 pmbaty 856
            }
857
            else
858
            {
25 pmbaty 859
               // do we want the 3D models or the flat icons ?
860
               // we do when EITHER we don't want flat icons OR the current player is looking downstraight
861
               if (!want_flaticons || (current_pitch < MAX_PITCH_FOR_FLAT_ICONS))
862
                  Scene_AddPart (scene, boardslot->part, boardslot->color, 17.5f - (7 - column) * 5.0f, 17.5f - line * 5.0f, 0.04f, 0.0f, 0.0f);
24 pmbaty 863
               else
25 pmbaty 864
                  Scene_AddTile (scene, theme->flattextures[boardslot->color][boardslot->part], 2.5f, 17.5f - (7 - column) * 5.0f, 17.5f - line * 5.0f, 0.05f, flaticons_yaw);
24 pmbaty 865
            }
866
 
867
            // is it time to play a move sound ?
868
            if ((sound_playtime != 0) && (sound_playtime < current_time))
869
            {
870
               // is the current player in check ? (to play the right sound)
871
               // read as: was the last move an opponent's move AND did it put us to check ?
872
               if (currentmove->is_check)
873
               {
874
                  // is it a checkmate or a normal check ? (checkmate == check + stalemate)
875
                  if (currentmove->is_stalemate)
876
                     Audio_PlaySound (board->players[currentmove->color].type == PLAYER_HUMAN ? SOUNDTYPE_VICTORY : SOUNDTYPE_DEFEAT); // if so, play endgame sound
877
                  else
878
                     Audio_PlaySound (SOUNDTYPE_CHECK); // else play the check sound
879
               }
880
               else
881
               {
882
                  // is it a stalemate, a capture or a normal move ?
883
                  if (currentmove->is_stalemate)
884
                     Audio_PlaySound (SOUNDTYPE_DEFEAT); // on stalemate, play defeat sound
885
                  else if (currentmove->has_captured)
886
                     Audio_PlaySound (SOUNDTYPE_PIECETAKEN); // on capture, play the capture sound
887
                  else
888
                     Audio_PlaySound (SOUNDTYPE_MOVE); // else play the normal move sound
889
               }
890
 
891
               sound_playtime = 0; // mark this animation as completed and sound played
892
            }
1 pmbaty 893
         }
894
 
24 pmbaty 895
         // else has a movement happened yet AND is this movement a castle AND is this the concerned tower ?
896
         else if ((board->viewed_move > 0)
897
                  && (towupper (currentmove->pgntext[0]) == L'O') // either O-O-O or O-O
898
                  && (line == (currentmove->color == COLOR_WHITE ? 0 : 7)) && (column == (currentmove->target[1] == 2 ? 3 : 5)))
899
         {
900
            // do we want animations AND is it still time to play the animation ? (castling rooks move faster)
901
            if (options.want_animations && (animation_endtime - (0.5f * ANIMATION_DURATION) > current_time))
902
            {
903
               // get the source and target X and Y positions
904
               source_x = 17.5f - (7 - (currentmove->target[1] == 2 ? 0 : 7)) * 5.0f; // if king moves left, then rook starts on column a, else it starts on column h
905
               source_y = 17.5f - line * 5.0f;
906
               target_x = 17.5f - (7 - column) * 5.0f;
907
               target_y = 17.5f - line * 5.0f;
908
 
909
               // compute the movement completion ratio between 0 and 1 (castling rooks move faster)
910
               movement_ratio = min (1.0f, 1.0f - (animation_endtime - (0.5f * ANIMATION_DURATION) - current_time) / (0.5f * ANIMATION_DURATION));
911
 
912
               // compute the current X an Y based on movement timing
913
               current_x = source_x + (target_x - source_x) * movement_ratio;
914
               current_y = source_y + (target_y - source_y) * movement_ratio;
915
 
916
               // height is a sine positive, max height is proportional to travelled distance
917
               movement_maxheight = 1.0f; // castling rook will barely hover above the ground
81 pmbaty 918
               current_z = 0.04f + (float) sin (MATH_PI * movement_ratio) * movement_maxheight;
24 pmbaty 919
 
920
               // make this part move realistically - do we want the 3D models or the flat icons ?
921
               // we do when EITHER we don't want flat icons OR the player playing the current move is looking downstraight
25 pmbaty 922
               if (!want_flaticons || (current_pitch < MAX_PITCH_FOR_FLAT_ICONS))
24 pmbaty 923
                  Scene_AddPart (scene, boardslot->part, boardslot->color, current_x, current_y, current_z, 0.0f, (boardslot->color == COLOR_BLACK ? -1 : 1) * min (current_z * 3.0f, 10.0f));
924
               else
25 pmbaty 925
                  Scene_AddTile (scene, theme->flattextures[boardslot->color][boardslot->part], 2.5f, current_x, current_y, 0.09f, flaticons_yaw);
24 pmbaty 926
 
927
               if (movement_ratio < 0.9f)
928
                  rooksound_played = false; // if the rook is still moving, reset the "sound played" flag
929
               else if (!rooksound_played)
930
               {
931
                  Audio_PlaySound (SOUNDTYPE_MOVE); // when the rook has landed, play a move sound
932
                  rooksound_played = true; // remember this is no longer to be done
933
               }
934
            }
935
            else
936
            {
25 pmbaty 937
               // do we want the 3D models or the flat icons ?
938
               // we do when EITHER we don't want flat icons OR the current player is looking downstraight
939
               if (!want_flaticons || (current_pitch < MAX_PITCH_FOR_FLAT_ICONS))
940
                  Scene_AddPart (scene, boardslot->part, boardslot->color, 17.5f - (7 - column) * 5.0f, 17.5f - line * 5.0f, 0.04f, 0.0f, 0.0f);
24 pmbaty 941
               else
25 pmbaty 942
                  Scene_AddTile (scene, theme->flattextures[boardslot->color][boardslot->part], 2.5f, 17.5f - (7 - column) * 5.0f, 17.5f - line * 5.0f, 0.05f, flaticons_yaw);
24 pmbaty 943
            }
944
         }
945
      } // end for (column = 0; column < 8; column++)
946
   } // end for (line = 0; line < 8; line++)
947
 
75 pmbaty 948
   ////////////////////////////////////////////////////////////////////////////////////
949
   // now draw the sepia overlay if we're in history view mode or if the game is paused
1 pmbaty 950
 
75 pmbaty 951
   if (options.want_sepiafilter && (is_paused || ((board->move_count > 1) && (board->viewed_move != board->move_count - 1))))
1 pmbaty 952
      scene->overlay_spriteindex = sepia_spriteindex; // use the sepia filter
953
   else
954
      scene->overlay_spriteindex = -1; // else use natural colors
955
 
956
   ////////////////////////////////////////////////////////////////////////////////////////////////
957
   // now draw the move comment text
958
 
959
   // does the move we are viewing have a comment ? if so, copy it, else leave it clear. Also if we're online, display a help text
960
   if ((currentmove->comment != NULL) && (currentmove->comment[0] != 0))
961
      Scene_SetText (&scene->gui.comment_text, 53.3f, 0.5f, 93.0f, ALIGN_CENTER, ALIGN_TOP, ALIGN_LEFT, chat_fontindex, RGBA_TO_RGBACOLOR (255, 255, 255, 191), 999999.0f, false, currentmove->comment);
962
   else if ((network_player != NULL) && network_player->is_logged_in && !network_player->is_in_game)
963
      Scene_SetText (&scene->gui.comment_text, 53.3f, 0.5f, 93.0f, ALIGN_CENTER, ALIGN_TOP, ALIGN_LEFT, chat_fontindex, RGBA_TO_RGBACOLOR (255, 255, 255, 191), 999999.0f, false, connected_comment);
964
   else if (RGBACOLOR_ALPHA (scene->gui.comment_text.color) >= 128) // HACK: don't clear if a dimmed hint text is already here
965
      scene->gui.comment_text.is_displayed = false; // else clear comment text
966
 
967
   ////////////////////////////////////////////////////////////////////////////////////////////////
968
   // now draw the game clock
969
 
970
   // do we want to print the game clock ?
971
   if (options.want_clock && (board->move_count > 1) && (board->game_state == STATE_PLAYING))
972
   {
973
      network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player
974
 
975
      // are we in Internet play ? if so, count time down, else count it up
976
      if ((network_player != NULL) && network_player->is_in_game)
977
         seconds = Player_GetCurrent ()->remaining_seconds - (int) (current_time - board->lastmove_time); // total seconds first
978
      else
75 pmbaty 979
         seconds = (int) (current_time - stoppage_time - board->lastmove_time); // total seconds first, take pauses in account
1 pmbaty 980
 
981
      minutes = seconds / 60; // minutes
982
      seconds -= 60 * minutes; // remaining seconds
983
 
984
      Scene_SetText (&the_scene.gui.clock_text, 99.0f, 66.6f, -1, ALIGN_RIGHT, ALIGN_CENTER, ALIGN_RIGHT, players_fontindex, RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, L"%02d:%02d  ", minutes, seconds); // note: last space is alt+255
985
   }
986
   else
987
      scene->gui.clock_text.is_displayed = false;
988
 
989
   ////////////////////////////////////////////////////////////////////////////////////////////////
990
   // now draw the turn text
991
 
992
   // do we want to print the player's turn AND has the game not ended yet ? if so, copy it, else leave it clear
993
   if (options.want_turn && (board->game_state == STATE_PLAYING))
994
   {
995
      if (Board_ColorToMove (board) == COLOR_BLACK)
996
      {
997
         if (the_scene.gui.turn_text.color != 0x000000C0)
998
            Scene_SetText (&the_scene.gui.turn_text, 99.0f, 100.0f, -1, ALIGN_RIGHT, ALIGN_BOTTOM, ALIGN_RIGHT, players_fontindex, 0x000000C0, 999999.0f, true, LOCALIZE (L"BlackMoves"));
999
      }
1000
      else
1001
      {
1002
         if (the_scene.gui.turn_text.color != 0xFFFFFF80)
1003
            Scene_SetText (&the_scene.gui.turn_text, 99.0f, 100.0f, -1, ALIGN_RIGHT, ALIGN_BOTTOM, ALIGN_RIGHT, players_fontindex, 0xFFFFFF80, 999999.0f, true, LOCALIZE (L"WhiteMoves"));
1004
      }
1005
   }
1006
   else
1007
      scene->gui.turn_text.is_displayed = false;
1008
 
1009
   ////////////////////////////////////////////////////////////////////////////////////////////////
1010
   // now draw the game history text
1011
 
1012
   // do we want to display the game history ? if so, display the game history text in PGN
1013
   if (options.want_history && (board->move_count > 1))
1014
   {
1015
      // first off, see where we start (for long games, we don't want to display EVERYTHING...
1016
      if (board->viewed_move > 30)
1017
         start_index = board->viewed_move - 30; // display 30 moves maximum
1018
      else
1019
         start_index = 1;
1020
 
1021
      // allocate an arbitrary length history text string buffer (assume each move can't be longer than 15 characters)
1022
      historytext_size = 15 * (1 + (board->viewed_move + 1) - start_index);
1023
      history_text = (wchar_t *) SAFE_malloc (historytext_size, sizeof (wchar_t), false);
1024
      history_text[0] = 0; // and reset it
1025
 
1026
      // now for each move we want to display...
1027
      for (move_index = start_index; move_index <= board->viewed_move; move_index++)
1028
      {
1029
         length = wcslen (history_text); // get current text length
1030
 
1031
         // every move pair, append move pair number
1032
         if (move_index % 2 == 1)
1033
            swprintf_s (&history_text[length], historytext_size - length, L"%d.   ", 1 + move_index / 2);
1034
 
1035
         // append move text
1036
         wcscat_s (history_text, historytext_size, board->moves[move_index].pgntext);
1037
         wcscat_s (history_text, historytext_size, L"   ");
1038
 
1039
         // every odd move, drop a newline
1040
         if (move_index % 2 == 0)
1041
            wcscat_s (history_text, historytext_size, L"\n");
1042
      }
1043
 
1044
      // add 50% alpha to game history color and transmit it to 3D engine
1045
      Scene_SetText (&the_scene.gui.history_text, 100.0f, 50.0f, -1, ALIGN_RIGHT, ALIGN_BOTTOM, ALIGN_LEFT, chat_fontindex, RGBACOLOR_SETALPHA (options.history_color, 0x7f), 999999.0f, false, history_text);
1046
      SAFE_free ((void **) &history_text);
1047
   }
1048
   else
1049
      scene->gui.history_text.is_displayed = false; // if we don't need to display the game history, free the buffer
1050
 
1051
   ////////////////////////////////////////////////////////////////////////////////////////////////
1052
   // now draw the chat area
1053
 
1054
   // is a valid chatter channel selected ?
1055
   if ((selected_chatterchannel != NULL) && (chatterchannel_count > 0) && ((local_player = Player_FindByType (PLAYER_HUMAN)) != NULL))
1056
   {
1057
      // set the chatter's name
1058
      wcscpy_s (scene->gui.entered_ccreply.nickname, WCHAR_SIZEOF (scene->gui.entered_ccreply.nickname), local_player->name);
1059
 
1060
      // correct or update the channel name and color
1061
      if (selected_chatterchannel->theme[0] != 0)
1062
         wcscpy_s (scene->gui.entered_ccreply.channelname, WCHAR_SIZEOF (scene->gui.entered_ccreply.channelname), selected_chatterchannel->theme);
1063
      else
1064
         swprintf_s (scene->gui.entered_ccreply.channelname, WCHAR_SIZEOF (scene->gui.entered_ccreply.channelname), L"%s %d", LOCALIZE (L"ChatterChannels_ColumnChannelNumber"), selected_chatterchannel->id);
1065
      scene->gui.entered_ccreply.color = RGBACOLOR_FULLALPHA (selected_chatterchannel->color); // full bright for entering text
1066
   }
1067
 
1068
   // display parts pick line in position setup mode only
1069
   scene->gui.is_partspick_displayed = (board->game_state == STATE_SETUPPOSITION ? true : false);
1070
 
1071
   //////////////////////////
1072
   // error and notifications
1073
 
1074
   // is the current player a computer AND are we playing a game right now
1075
   // AND has the computer been thinking for more than 5 seconds AND is there no "thinking" text yet ?
1076
   if ((board->players[Board_ColorToMove (board)].type == PLAYER_COMPUTER) && (board->game_state == STATE_PLAYING)
1077
       && (board->lastmove_time + 5.0f < current_time) && !scene->gui.central_text.is_displayed)
1078
   {
1079
      Scene_SetText (&the_scene.gui.central_text, 50.0f, 40.0f, -1, ALIGN_CENTER, ALIGN_CENTER, ALIGN_CENTER, centermsg_fontindex, RGBA_TO_RGBACOLOR (255, 255, 255, 191),
1080
                     999999.0f, true, LOCALIZE (L"Thinking")); // if so, display the "thinking" phrase in the middle of the screen
1081
      the_scene.gui.want_spinwheel = true; // start spinning wheel
1082
   }
1083
 
1084
   // is there a network player AND is our socket gone AWOL ?
1085
   else if ((network_player != NULL) && (network_player->our_socket == INVALID_SOCKET))
1086
   {
1087
      // is there nothing in the center of the screen yet ?
1088
      if (!the_scene.gui.central_text.is_displayed)
1089
         Scene_SetText (&the_scene.gui.central_text, 50.0f, 50.0f, -1, ALIGN_CENTER, ALIGN_CENTER, ALIGN_CENTER, centermsg_fontindex, RGBA_TO_RGBACOLOR (255, 255, 255, 191),
1090
                        999999.0f, true, LOCALIZE (L"Error_ConnectionToChessServerLost")); // display "error" in the middle of the screen
1091
      the_scene.overlay_spriteindex = sepia_spriteindex; // display sepia filter if no connection
1092
   }
1093
 
75 pmbaty 1094
   // is the game paused ?
1095
   if (is_paused)
1096
      Scene_SetText (&the_scene.gui.central_text, 50.0f, 50.0f, -1, ALIGN_CENTER, ALIGN_CENTER, ALIGN_CENTER, centermsg_fontindex, RGBA_TO_RGBACOLOR (255, 255, 255, 255),
1097
                     1.0f, false, LOCALIZE (L"Paused")); // if so, display the "paused" phrase in the middle of the screen
1098
 
1 pmbaty 1099
   return; // finished, scene is updated
1100
}
1101
 
1102
 
1103
void Scene_AddCCReply (scene_t *scene, wchar_t *nickname, wchar_t *channelname, unsigned long color_rgbx, wchar_t *fmt, ...)
1104
{
1105
   // helper function to add a CC reply on display
1106
 
1107
   static wchar_t message[4096];
1108
   ccreply_t re;
1109
   va_list argptr;
1110
 
1111
   // concatenate all the arguments in one string
1112
   va_start (argptr, fmt);
1113
   _vsnwprintf_s (message, WCHAR_SIZEOF (message), _TRUNCATE, fmt, argptr);
1114
   va_end (argptr);
1115
 
1116
   // now put the text in place
1117
   memset (&re, 0, sizeof (re)); // reset the structure we're about to fill
1118
 
1119
   wcscpy_s (re.nickname, WCHAR_SIZEOF (re.nickname), nickname); // copy nickname
1120
   wcscpy_s (re.channelname, WCHAR_SIZEOF (re.channelname), channelname); // copy channel name
1121
 
1122
   re.text_length = wcslen (message); // get text length
1123
   re.text = (wchar_t *) SAFE_malloc (re.text_length + 1, sizeof (wchar_t), false); // allocate text space (include null terminator)
1124
   wcscpy_s (re.text, re.text_length + 1, message); // get the text
1125
 
1126
   re.color = RGBACOLOR_SETALPHA (color_rgbx, 0xC0); // copy reply color and force a slightly transparent alpha
1127
   re.arrival_time = current_time; // save CC reply arrival time
1128
 
1129
   // reallocate CC history array to hold now one reply more
1130
   the_scene.gui.cchistory = (ccreply_t *) SAFE_realloc (the_scene.gui.cchistory, the_scene.gui.cchistory_count, the_scene.gui.cchistory_count + 1, sizeof (ccreply_t), false);
1131
   memcpy (&the_scene.gui.cchistory[the_scene.gui.cchistory_count], &re, sizeof (ccreply_t));
1132
   the_scene.gui.cchistory_count++; // CC history holds now one reply more
1133
 
1134
   return; // finished, announcement text is set
1135
}
1136
 
1137
 
1138
void Scene_AddAnnouncement (scene_t *scene, wchar_t *fmt, ...)
1139
{
1140
   // helper function to set the announcement (red) text on display
1141
 
1142
   static wchar_t message[4096];
1143
   ccreply_t re;
1144
   va_list argptr;
1145
 
1146
   // concatenate all the arguments in one string
1147
   va_start (argptr, fmt);
1148
   _vsnwprintf_s (message, WCHAR_SIZEOF (message), _TRUNCATE, fmt, argptr);
1149
   va_end (argptr);
1150
 
1151
   // now put the text in place
1152
   memset (&re, 0, sizeof (re)); // reset the structure we're about to fill
1153
 
1154
   re.text_length = wcslen (message); // get text length
1155
   re.text = (wchar_t *) SAFE_malloc (re.text_length + 1, sizeof (wchar_t), false); // allocate text space (include null terminator)
1156
   wcscpy_s (re.text, re.text_length + 1, message); // get the text
1157
 
1158
   wcscpy_s (re.channelname, WCHAR_SIZEOF (re.channelname), LOCALIZE (L"ImportantMessage"));
1159
   re.color = RGBA_TO_RGBACOLOR (192, 0, 0, 0xE0); // fair red, a bit more opaque than normal messages
1160
   re.arrival_time = current_time; // save announcement arrival time
1161
 
1162
   // reallocate CC history array to hold now one reply more
1163
   the_scene.gui.cchistory = (ccreply_t *) SAFE_realloc (the_scene.gui.cchistory, the_scene.gui.cchistory_count, the_scene.gui.cchistory_count + 1, sizeof (ccreply_t), false);
1164
   memcpy (&the_scene.gui.cchistory[the_scene.gui.cchistory_count], &re, sizeof (ccreply_t));
1165
   the_scene.gui.cchistory_count++; // CC history holds now one reply more
1166
 
1167
   return; // finished, announcement text is set
1168
}
1169
 
1170
 
1171
void Scene_SetButton (guibutton_t *button, float left, float top, float width, float height, int sprite_index)
1172
{
1173
   // helper function to set up a GUI button
1174
 
1175
   button->left = left;
1176
   button->top = top;
1177
   button->width = width;
1178
   button->height = height;
1179
   button->sprite_index = sprite_index;
1180
 
1181
   return; // finished, button is set
1182
}
1183
 
1184
 
1185
void Scene_SetText (guitext_t *text, float xpos_percent, float ypos_percent, float maxwidth_percent, int horizontal_align, int vertical_align, int text_align, int font_index, unsigned long color_rgba, float duration, bool want_fade, wchar_t *fmt, ...)
1186
{
1187
   // helper function to set some text on display
1188
 
1189
   static wchar_t message[4096];
1190
   va_list argptr;
1191
   int length;
1192
 
1193
   // concatenate all the arguments in one string
1194
   va_start (argptr, fmt);
1195
   _vsnwprintf_s (message, WCHAR_SIZEOF (message), _TRUNCATE, fmt, argptr);
1196
   va_end (argptr);
1197
 
1198
   text->xpos_percent = xpos_percent; // save text's X position, in percents from left to right
1199
   text->ypos_percent = ypos_percent; // save text's Y position, in percents from top to bottom
1200
   text->maxwidth_percent = maxwidth_percent; // save text's max width before word wrapping
1201
   text->horizontal_align = horizontal_align; // save text's horizontal alignment regarding the X position
1202
   text->vertical_align = vertical_align; // save text's vertical alignment regarding the Y position
1203
   text->text_align = text_align; // save text's horizontal alignment inside the bounding rectangle
1204
   text->font_index = font_index; // save the index of the font with which to display this text
1205
   text->color = color_rgba; // text's color, in RGBA
1206
 
1207
   // now put the text in place
1208
   length = wcslen (message) + 1; // include null terminator
1209
   text->buffer = (wchar_t *) SAFE_realloc (text->buffer, text->buffer_size, length, sizeof (wchar_t), false);
1210
   wcscpy_s (text->buffer, length, message); // copy message text
1211
   text->buffer_size = length; // and save buffer length
1212
 
1213
   text->appear_time = current_time; // save text arrival time
1214
   text->disappear_time = current_time + duration; // make it last duration seconds
1215
   text->want_fade = want_fade; // remember if text needs to be faded in and out
1216
 
1217
   // mark this text for display
1218
   text->is_displayed = true;
1219
 
1220
   return; // finished, text is set
1221
}
1222
 
1223
 
1224
static void Scene_AddPart (scene_t *scene, int part_type, int part_color, float pos_x, float pos_y, float pos_z, float turn_yaw, float pitch)
1225
{
1226
   // helper function to add a specified part of the specified color to the rendered scene
1227
 
1228
   sceneobject_t *object;
1229
   partcolor_t *partcolor;
1230
 
1231
   // reallocate space to hold one object more and blank it out
1232
   scene->objects = (sceneobject_t *) SAFE_realloc (scene->objects, scene->object_count, scene->object_count + 1, sizeof (sceneobject_t), true);
1233
 
1234
   object = &scene->objects[scene->object_count]; // quick access to object
1235
 
1236
   object->mesh_index = theme->part_meshes[part_type]; // retrieve object mesh
1237
   object->simpleshadow_size = simpleshadow_sizes[part_type]; // retrieve simple shadow size according to part type
1238
   object->scale = 1.0f; // scale at 1 so far
1239
 
1240
   // set object texture and material
1241
   partcolor = &theme->part_colors[part_color]; // quick access to part color struct
1242
   object->texture_index = partcolor->texture;
1243
   object->material_index = partcolor->material;
1244
 
1245
   // figure out object position on board
1246
   object->x = pos_x;
1247
   object->y = pos_y;
1248
   object->z = pos_z;
1249
 
1250
   // turn a color's all parts' yaw 180 degrees so as both sides face each other
1251
   if (part_color == COLOR_WHITE)
1252
      object->yaw = 180.0f;
1253
   else
1254
      object->yaw = 0.0f;
1255
 
1256
   // and add the final turn pitch and yaw
1257
   object->pitch = pitch;
1258
   object->yaw = WrapAngle (object->yaw + turn_yaw);
1259
 
1260
   scene->object_count++; // array holds now one object more
1261
   return; // finished
1262
}
1263
 
1264
 
1265
static void Scene_AddTile (scene_t *scene, int texture_index, float scale, float pos_x, float pos_y, float pos_z, float turn_yaw)
1266
{
1267
   // helper function to add a specified part of the specified color to the rendered scene
1268
 
1269
   sceneobject_t *object;
1270
 
1271
   // reallocate space to hold one object more and blank it out
1272
   scene->objects = (sceneobject_t *) SAFE_realloc (scene->objects, scene->object_count, scene->object_count + 1, sizeof (sceneobject_t), true);
1273
 
1274
   object = &scene->objects[scene->object_count]; // quick access to object
1275
 
1276
   // save object data
1277
   object->mesh_index = -1; // objects that have -1 as mesh index are tiles
1278
   object->texture_index = texture_index;
1279
   object->material_index = -1; // objects will use the default material
1280
 
1281
   // figure out object position on board
1282
   object->x = pos_x;
1283
   object->y = pos_y;
1284
   object->z = pos_z;
1285
   object->scale = scale;
1286
 
1287
   // turn tile as requested
1288
   object->yaw = turn_yaw;
1289
 
1290
   scene->object_count++; // array holds now one object more
1291
 
1292
   return; // finished
1293
}