]> icculus.org git repositories - taylor/freespace2.git/blob - src/menuui/barracks.cpp
SDL2 port - stage 2
[taylor/freespace2.git] / src / menuui / barracks.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/MenuUI/Barracks.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * C file for implementing barracks section
16  *
17  * $Log$
18  * Revision 1.6  2005/03/29 02:18:47  taylor
19  * Various 64-bit platform fixes
20  * Fix compiler errors with MAKE_FS1 and fix gr_set_bitmap() too
21  * Make sure that turrets can fire at asteroids for FS1 (needed for a couple missions)
22  * Streaming audio support (big thanks to Pierre Willenbrock!!)
23  * Removed dependance on strings.tbl for FS1 since we don't actually need it now
24  *
25  * Revision 1.5  2004/09/20 01:31:44  theoddone33
26  * GCC 3.4 fixes.
27  *
28  * Revision 1.4  2003/05/25 02:30:42  taylor
29  * Freespace 1 support
30  *
31  * Revision 1.3  2002/06/09 04:41:22  relnev
32  * added copyright header
33  *
34  * Revision 1.2  2002/05/07 03:16:46  theoddone33
35  * The Great Newline Fix
36  *
37  * Revision 1.1.1.1  2002/05/03 03:28:09  root
38  * Initial import.
39  *
40  * 
41  * 42    10/25/99 5:47p Jefff
42  * reassigned some xstr ids
43  * 
44  * 41    9/14/99 11:01p Jefff
45  * stats area coord fix
46  * 
47  * 40    9/09/99 11:24a Jefff
48  * 
49  * 39    9/02/99 11:26a Jefff
50  * fixed incorrect pilot list dimensions
51  * 
52  * 38    8/17/99 2:24p Dave
53  * Fixed wacky squad color stuff.
54  * 
55  * 37    8/16/99 4:27p Jefff
56  * fix button position on squad change error popup
57  * 
58  * 36    8/11/99 3:58p Jefff
59  * fixed player list box coord bug
60  * 
61  * 35    8/05/99 2:46p Jefff
62  * added popup to the disabled squad selection buttons in single player
63  * 
64  * 34    8/02/99 11:02p Dave
65  * Fixed up squad filename and squad name copying when converting a pilot
66  * between single and multiplayer.
67  * 
68  * 33    7/27/99 7:17p Jefff
69  * Replaced some art text with XSTR() text.
70  * 
71  * 32    7/15/99 9:20a Andsager
72  * FS2_DEMO initial checkin
73  * 
74  * 31    6/29/99 7:39p Dave
75  * Lots of small bug fixes.
76  * 
77  * 30    6/16/99 4:06p Dave
78  * New pilot info popup. Added new draw-bitmap-as-poly function.
79  * 
80  * 29    6/11/99 11:13a Dave
81  * last minute changes before press tour build.
82  * 
83  * 28    4/25/99 7:43p Dave
84  * Misc small bug fixes. Made sun draw properly.
85  * 
86  * 27    4/25/99 3:02p Dave
87  * Build defines for the E3 build.
88  * 
89  * 26    3/24/99 4:05p Dave
90  * Put in support for assigning the player to a specific squadron with a
91  * specific logo. Preliminary work for doing pos/orient checksumming in
92  * multiplayer to reduce bandwidth.
93  * 
94  * 25    2/25/99 4:19p Dave
95  * Added multiplayer_beta defines. Added cd_check define. Fixed a few
96  * release build warnings. Added more data to the squad war request and
97  * response packets.
98  * 
99  * 24    2/02/99 12:09p Neilk
100  * Fixed text coords, centered pilot and squad pics in 1024x768, added vss
101  * comment template
102  *
103  * $NoKeywords: $
104  */
105
106 #include "barracks.h"
107 #include "managepilot.h"
108 #include "ui.h"
109 #include "font.h"
110 #include "key.h"
111 #include "gamesnd.h"
112 #include "osapi.h"
113 #include "popup.h"
114 #include "playermenu.h"
115 #include "gamesequence.h"
116 #include "contexthelp.h"
117 #include "2d.h"
118 #include "ctype.h"
119 #include "freespace.h"
120 #include "systemvars.h"
121 #include "bmpman.h"
122 #include "mouse.h"
123 #include "osregistry.h"
124 #include "alphacolors.h"
125
126 void delete_pilot_file( const char *pilot_name, int single );           // manage_pilot.cpp
127
128 // stats defines
129 #define NUM_STAT_LINES 85
130 #define STAT_COLUMN1_W 40
131 #define STAT_COLUMN2_W 10
132
133 static int Stat_column1_w[GR_NUM_RESOLUTIONS] =
134 {
135         40,             // GR_640
136         40                      // GR_1024
137 };
138
139 static int Stat_column2_w[GR_NUM_RESOLUTIONS] =
140 {
141         10,             // GR_640
142         10                      // GR_1024
143 };
144
145 // constants for coordinate lookup
146 #define BARRACKS_X_COORD 0
147 #define BARRACKS_Y_COORD 1
148 #define BARRACKS_W_COORD 2
149 #define BARRACKS_H_COORD 3
150
151 // area ints
152
153 // pilot selection field
154 static int Barracks_list_coords[GR_NUM_RESOLUTIONS][4] = {
155         { // GR_640
156 #ifdef MAKE_FS1
157                 58, 42, 400, 105
158 #else
159                 42, 34, 400, 90
160 #endif
161         },
162         { // GR_1024
163                 45, 51, 646, 144
164         }
165 };
166
167 // pilot stats field
168 static int Barracks_stats_coords[GR_NUM_RESOLUTIONS][4] = {
169         { // GR_640
170 #ifdef MAKE_FS1
171                 35, 215, 240, 250
172 #else
173                 32, 212, 240, 250
174 #endif
175         },
176         { // GR_1024
177                 42, 351, 240, 400
178         }
179 };
180
181 static int Barracks_stats2_coords[GR_NUM_RESOLUTIONS][3] = {
182         { // GR_640
183                 276, 212, 81            // X2, , W2
184         },
185         { // GR_1024
186                 286, 351, 81            // X2, , W2
187         }
188 };
189
190 // pilot picture field
191 static int Barracks_image_coords[GR_NUM_RESOLUTIONS][4] = {
192         { // GR_640
193 #ifdef MAKE_FS1
194                 460, 33, 160, 120
195 #else
196                 461, 23, 160, 120
197 #endif
198         },
199         { // GR_1024
200                 782, 58, 160, 120
201         }
202 };
203
204 // pilot picture # of # location
205 static int Barracks_image_number_coords[GR_NUM_RESOLUTIONS][2] = {
206         { // GR_640
207 #ifdef MAKE_FS1
208 #ifndef FS1_DEMO
209                 489, 171
210 #else
211                 489, 183
212 #endif  // FS1_DEMO
213 #else
214                 461, 145
215 #endif
216         },
217         { // GR_1024
218                 732, 239
219         }
220 };
221
222 // pilot squad logo field
223 int Barracks_squad_coords[GR_NUM_RESOLUTIONS][4] = {
224         { // GR_640
225                 495, 177, 128, 128
226         },
227         { // GR_1024
228                 829, 323, 128, 128
229         }
230 };
231
232 // pilot squad # of # location
233 int Barracks_squad_number_coords[GR_NUM_RESOLUTIONS][2] = {
234         { // GR_640
235                 492, 307
236         },
237         { // GR_1024
238                 794, 500
239         }
240 };
241
242 // button defines
243 #ifdef MAKE_FS1
244         #define BARRACKS_NUM_BUTTONS            17
245 #else
246         #define BARRACKS_NUM_BUTTONS            19
247 #endif
248
249 // pilot selection buttons
250 #define B_PILOT_CREATE_BOTTON                   0       // B_PILOT_CREATE_BOTTON
251 #define B_PILOT_SCROLL_UP_BUTTON                1       // B_PILOT_SCROLL_UP_BUTTON
252 #define B_PILOT_SCROLL_DOWN_BUTTON      2       // B_PILOT_SCROLL_DOWN_BUTTON
253 #define B_PILOT_DELETE_BUTTON                   11      // B_PILOT_B_PILOT_DELETE_BUTTON
254 #define B_PILOT_SET_ACTIVE_BUTTON       12      // B_PILOT_B_PILOT_SET_ACTIVE_BUTTON
255 #define B_PILOT_CLONE_BUTTON                    13      // B_PILOT_B_PILOT_CLONE_BUTTON
256 #define B_PILOT_SINGLE_MODE_BUTTON      14      // B_PILOT_SINGLE_MODE_BUTTON
257 #define B_PILOT_MULTI_MODE_BUTTON       15      // B_PILOT_MULTI_MODE_BUTTON
258 #define B_PILOT_CONVERT_BUTTON          16      // B_PILOT_B_PILOT_CONVERT_BUTTON
259
260 #ifndef MAKE_FS1
261 // squad logo picture buttons
262 #define B_SQUAD_PREV_BUTTON                     17
263 #define B_SQUAD_NEXT_BUTTON                     18
264 #endif
265
266 // pilot picture buttons
267 #define B_PIC_PREV_PILOT_BUTTON         3       // B_PILOT_B_PIC_PREV_PILOT_BUTTON
268 #define B_PIC_NEXT_PILOT_BUTTON         4       // B_PILOT_B_PIC_NEXT_PILOT_BUTTON
269
270 // pilot stat buttons
271 #define B_STATS_MEDAL_BUTTON                    8       // B_STATS_MEDAL_BUTTON
272 #define B_STATS_SCROLL_DOWN_BUTTON      9       // B_STATS_SCROLL_DOWN_BUTTON
273 #define B_STATS_SCROLL_UP_BUTTON                10      // B_STATS_SCROLL_UP_BUTTON
274
275 // general buttons
276 #define B_ACCEPT_BUTTON                                 5       // B_B_ACCEPT_BUTTON
277 #define B_HELP_BUTTON                                   6       // B_B_HELP_BUTTON
278 #define B_OPTION_BUTTON                                 7       // B_OPTION_BUTTON
279
280 //XSTR:OFF
281 // bitmaps defs
282 static const char *Barracks_bitmap_fname[GR_NUM_RESOLUTIONS] = {
283         "Barracks",             // GR_640
284         "2_Barracks"            // GR_1024
285 };
286
287 static const char *Barracks_bitmap_mask_fname[GR_NUM_RESOLUTIONS] = {
288         "Barracks-M",           // GR_640
289         "2_Barracks-M"          // GR_1024
290 };
291
292 //XSTR:ON
293
294 #define BARRACKS_IMAGE_NOT_LOADED       -2
295
296 struct barracks_bitmaps {
297         const char *filename;
298         int x, y;
299         int b;
300 };
301
302 struct barracks_buttons {
303         const char *filename;
304         int x, y;
305         int text_x, text_y;     // this is where the text label is
306         int hotspot;
307         int repeat;
308         UI_BUTTON button;  // because we have a class inside this struct, we need the constructor below..
309
310         barracks_buttons(const char *name, int x1, int y1, int x2, int y2, int h, int r = 0) : filename(name), x(x1), y(y1), text_x(x2), text_y(y2), hotspot(h), repeat(r) {}
311 };
312
313 #if defined(MAKE_FS1) && !defined(FS1_DEMO)
314 static int Background_bitmap;
315 static int PilotWin01 = -1;
316 static int PilotWin02 = -1;
317 static int PilotWin03 = -1;
318 static int PilotWin04 = -1;
319 #else
320 static int Background_bitmap = -1;
321 #endif
322 static UI_WINDOW Ui_window;
323 static UI_BUTTON List_region;
324 static UI_INPUTBOX Inputbox;
325
326 static barracks_buttons Buttons[GR_NUM_RESOLUTIONS][BARRACKS_NUM_BUTTONS] = {
327 //XSTR:OFF
328         {               // GR_640
329 #ifdef MAKE_FS1
330                         barracks_buttons("BAB_00",      12,             121,    -1,     -1,             0),             // create
331                         barracks_buttons("BAB_01",      322,    137,    -1,     -1,     1,      1),             // barracks - arrow up
332                         barracks_buttons("BAB_02",      347,    137,    -1,     -1,     2,      1),             // barracks - arrow down
333 #ifndef FS1_DEMO
334                         barracks_buttons("BAB_03",      570,    140,    -1,     -1,     3,      1),             // pilot window - arrow left
335                         barracks_buttons("BAB_04",      591,    140,    -1,     -1,     4,      1),             // pilot window - arrow right
336 #else
337                         barracks_buttons("BAB_03",      570,    157,    -1,     -1,     3,      1),             // pilot window - arrow left
338                         barracks_buttons("BAB_04",      591,    157,    -1,     -1,     4,      1),             // pilot window - arrow right
339 #endif
340                         barracks_buttons("BAB_05",      562,    410,    -1,     -1,             5),             // accept
341                         barracks_buttons("BAB_06",      469,    427,    -1,     -1,             6),             // help
342                         barracks_buttons("BAB_07",      447,    452,    -1,     -1,             7),             // options
343                         barracks_buttons("BAB_08",      365,    415,    -1,     -1,             8),             // medals
344                         barracks_buttons("BAB_09",      0,              225,    -1,     -1,     9,      1),             // pilot stats - arrows down
345                         barracks_buttons("BAB_10",      0,              185,    -1,     -1,     10,     1),             // pilot stats - arrows up
346                         barracks_buttons("BAB_11",      124,    121,    -1,     -1,             11),    // remove
347                         barracks_buttons("BAB_12",      374,    127,    -1,     -1,             12),    // barracks - accept
348                         barracks_buttons("BAB_13",      70,             121,    -1,     -1,             13),    // clone
349                         barracks_buttons("BAB_14",      329,    13,             -1,     -1,             14),    // single
350                         barracks_buttons("BAB_15",      380,    13,             -1,     -1,             15),    // multi
351                         barracks_buttons("BAB_16",      183,    121,    -1,     -1,             16)             // convert
352 #else
353                         barracks_buttons("BAB_00", 8,           122,    11,     157,    0),             
354                         barracks_buttons("BAB_01", 323, 130,    0,              0,              1, 1),
355                         barracks_buttons("BAB_02", 350, 130,    0,              0,              2,      1),
356                         barracks_buttons("BAB_03", 559, 143,    0,              0,              3,      1),                     
357                         barracks_buttons("BAB_04", 598, 143,    0,              0,              4,      1),
358                         barracks_buttons("BAB_05", 571, 425,    578,    413,    5),
359                         barracks_buttons("BAB_06", 533, 425,    500,    438,    6),
360                         barracks_buttons("BAB_07", 533, 453,    481,    465,    7),
361                         barracks_buttons("BAB_08", 361, 425,    401,    465,    8),
362                         barracks_buttons("BAB_09", 0,           267,    0,              0,              9,      1),
363                         barracks_buttons("BAB_10", 0,           224,    0,              0,              10,1),
364                         barracks_buttons("BAB_11", 120, 122,    123,    157,    11),    
365                         barracks_buttons("BAB_12", 376, 124,    378,    163,    12),
366                         barracks_buttons("BAB_13", 66,  122,    69,     157,    13),
367                         barracks_buttons("BAB_14", 323, 0,              324,    25,     14),
368                         barracks_buttons("BAB_15", 372, 0,              374,    25,     15),
369                         barracks_buttons("BAB_16", 180, 122,    182,    157,    16),
370                         barracks_buttons("BAB_17", 559, 306,    0,              0,              17),
371                         barracks_buttons("BAB_18", 598, 306,    0,              0,              18)
372 #endif
373         },
374         {               // GR_1024
375                         barracks_buttons("2_BAB_00", 14,                196,    35,     252,    0),
376                         barracks_buttons("2_BAB_01", 518,       209,    0,              0,              1,      1),
377                         barracks_buttons("2_BAB_02", 561,       209,    0,              0,              2,      1),
378                         barracks_buttons("2_BAB_03", 896,       229,    0,              0,              3,      1),
379                         barracks_buttons("2_BAB_04", 958,       229,    0,              0,              4,      1),
380                         barracks_buttons("2_BAB_05", 914,       681,    932,    665,    5),
381                         barracks_buttons("2_BAB_06", 854,       681,    800,    704,    6),
382                         barracks_buttons("2_BAB_07", 854,       724,    778,    743,    7),
383                         barracks_buttons("2_BAB_08", 579,       681,    641,    743,    8),
384                         barracks_buttons("2_BAB_09", 0,         428,    0,              0,              9,      1),
385                         barracks_buttons("2_BAB_10", 0,         360,    0,              0,              10,1),
386                         barracks_buttons("2_BAB_11", 193,       196,    214,    252,    11),
387                         barracks_buttons("2_BAB_12", 602,       200,    617,    262,    12),
388                         barracks_buttons("2_BAB_13", 107,       196,    128,    252,    13),
389                         barracks_buttons("2_BAB_14", 517,       0,              532,    40,     14),
390                         barracks_buttons("2_BAB_15", 596,       0,              614,    40,     15),
391                         barracks_buttons("2_BAB_16", 289,       196,    309,    252,    16),
392 #ifndef MAKE_FS1
393                         barracks_buttons("2_BAB_17", 896,       491,    0,              0,              17),
394                         barracks_buttons("2_BAB_18", 958,       491,    0,              0,              18)
395 #endif
396         }
397 //XSTR:ON
398 };
399
400
401 // FIXME add to strings.tbl, set correct coords
402 #ifndef MAKE_FS1
403 #define BARRACKS_NUM_TEXT                       2
404
405 UI_XSTR Barracks_text[GR_NUM_RESOLUTIONS][BARRACKS_NUM_TEXT] = {
406         { 
407                 // GR_640
408                 { "Barracks",                   1434,           17, 7,          UI_XSTR_COLOR_GREEN, -1, NULL },
409                 { "Pilot Stats",                1435,           17, 180,                UI_XSTR_COLOR_GREEN, -1, NULL }
410         }, 
411         { 
412                 // GR_1024
413                 { "Barracks",                   1434,           27, 11,         UI_XSTR_COLOR_GREEN, -1, NULL },
414                 { "Pilot Stats",                1435,           27, 288,                UI_XSTR_COLOR_GREEN, -1, NULL }
415         }
416 };
417 #endif
418
419
420 static int Num_stat_lines;
421 static char Stat_labels[NUM_STAT_LINES][STAT_COLUMN1_W];
422 static char Stats[NUM_STAT_LINES][STAT_COLUMN2_W];
423
424 extern int Player_sel_mode;
425
426 static player *Cur_pilot;
427 static int Num_pilots;
428 static int Selected_line;
429 static char Pilots_arr[MAX_PILOTS][MAX_FILENAME_LEN];
430 static char *Pilots[MAX_PILOTS];
431 static int Pic_number;
432 static int Pic_squad_number;
433 static bool Barracks_callsign_enter_mode;
434 static int Pilot_ranks[MAX_PILOTS];
435 static int List_scroll_offset;
436 static int Stats_scroll_offset;
437 static int Clone_flag;
438 static int Pilot_images[MAX_PILOT_IMAGES];
439 static int Pilot_squad_images[MAX_PILOT_IMAGES];
440 static int Rank_pips_bitmaps;
441 static int Rank_pips_count;
442
443 void barracks_squad_change_popup();
444
445
446 // -------------------------------------------------------------------------------------------------
447 //
448 // BARRACKS screen
449 //
450
451 #define STRCPY1(a, b) do {      \
452         Assert(strlen(b) < STAT_COLUMN1_W); \
453         strcpy(a, b); \
454 } while (0)
455
456 void barracks_init_stats(scoring_struct *stats)
457 {
458         int i;
459         float f;
460
461         Num_stat_lines = 0;
462
463         STRCPY1(Stat_labels[Num_stat_lines], XSTR( "*All Time Stats", 50));
464         Stats[Num_stat_lines][0] = 0;
465         Num_stat_lines++;
466
467         Assert(Num_stat_lines < NUM_STAT_LINES);
468         Stat_labels[Num_stat_lines][0] = 0;
469         Stats[Num_stat_lines][0] = 0;
470         Num_stat_lines++;       
471
472         Assert(Num_stat_lines < NUM_STAT_LINES);
473         STRCPY1(Stat_labels[Num_stat_lines], XSTR( "Primary weapon shots:", 51));
474         sprintf(Stats[Num_stat_lines], "%d", stats->p_shots_fired);
475         Num_stat_lines++;
476
477         Assert(Num_stat_lines < NUM_STAT_LINES);
478         STRCPY1(Stat_labels[Num_stat_lines], XSTR( "Primary weapon hits:", 52));
479         sprintf(Stats[Num_stat_lines], "%d", stats->p_shots_hit);
480         Num_stat_lines++;
481
482         Assert(Num_stat_lines < NUM_STAT_LINES);
483         STRCPY1(Stat_labels[Num_stat_lines], XSTR( "Primary friendly hits:", 53));
484         sprintf(Stats[Num_stat_lines], "%d", stats->p_bonehead_hits);
485         Num_stat_lines++;
486
487         Assert(Num_stat_lines < NUM_STAT_LINES);
488         STRCPY1(Stat_labels[Num_stat_lines], XSTR( "Primary hit %:", 54));
489         if (stats->p_shots_fired > 0) {
490                 f = (float) stats->p_shots_hit * 100.0f / (float) stats->p_shots_fired;
491         } else {
492                 f = 0.0f;
493         }
494         sprintf(Stats[Num_stat_lines], XSTR( "%.1f%%", 55), f);
495         Num_stat_lines++;
496
497         Assert(Num_stat_lines < NUM_STAT_LINES);
498         STRCPY1(Stat_labels[Num_stat_lines], XSTR( "Primary friendly hit %:", 56));
499         if (stats->p_bonehead_hits > 0) {
500                 f = (float) stats->p_bonehead_hits * 100.0f / (float) stats->p_shots_fired;
501         } else {
502                 f = 0.0f;
503         }
504         sprintf(Stats[Num_stat_lines], XSTR( "%.1f%%", 55), f);
505         Num_stat_lines++;
506
507         Assert(Num_stat_lines < NUM_STAT_LINES);
508         Stat_labels[Num_stat_lines][0] = 0;
509         Stats[Num_stat_lines][0] = 0;
510         Num_stat_lines++;
511
512         Assert(Num_stat_lines < NUM_STAT_LINES);
513         STRCPY1(Stat_labels[Num_stat_lines], XSTR( "Secondary weapon shots:", 57));
514         sprintf(Stats[Num_stat_lines], "%d", stats->s_shots_fired);
515         Num_stat_lines++;
516
517         Assert(Num_stat_lines < NUM_STAT_LINES);
518         STRCPY1(Stat_labels[Num_stat_lines], XSTR( "Secondary weapon hits:", 58));
519         sprintf(Stats[Num_stat_lines], "%d", stats->s_shots_hit);
520         Num_stat_lines++;
521
522         Assert(Num_stat_lines < NUM_STAT_LINES);
523         STRCPY1(Stat_labels[Num_stat_lines], XSTR( "Secondary friendly hits:", 59));
524         sprintf(Stats[Num_stat_lines], "%d", stats->s_bonehead_hits);
525         Num_stat_lines++;
526
527         Assert(Num_stat_lines < NUM_STAT_LINES);
528         STRCPY1(Stat_labels[Num_stat_lines], XSTR( "Secondary hit %:", 60));
529         if (stats->s_shots_fired > 0) {
530                 f = (float) stats->s_shots_hit * 100.0f / (float) stats->s_shots_fired;
531         } else {
532                 f = 0.0f;
533         }
534         sprintf(Stats[Num_stat_lines], XSTR( "%.1f%%", 55), f);
535         Num_stat_lines++;
536
537         Assert(Num_stat_lines < NUM_STAT_LINES);
538         STRCPY1(Stat_labels[Num_stat_lines], XSTR( "Secondary friendly hit %:", 61));
539         if (stats->s_bonehead_hits > 0) {
540                 f = (float) stats->s_bonehead_hits * 100.0f / (float) stats->s_shots_fired;
541         } else {
542                 f = 0.0f;
543         }
544         sprintf(Stats[Num_stat_lines], XSTR( "%.1f%%", 55), f);
545         Num_stat_lines++;
546
547         Assert(Num_stat_lines < NUM_STAT_LINES);
548         Stat_labels[Num_stat_lines][0] = 0;
549         Stats[Num_stat_lines][0] = 0;
550         Num_stat_lines++;
551
552         Assert(Num_stat_lines < NUM_STAT_LINES);
553         STRCPY1(Stat_labels[Num_stat_lines], XSTR( "Total kills:", 62));
554         sprintf(Stats[Num_stat_lines], "%d", stats->kill_count_ok);
555         Num_stat_lines++;
556
557         Assert(Num_stat_lines < NUM_STAT_LINES);
558         STRCPY1(Stat_labels[Num_stat_lines], XSTR( "Assists:", 63));
559         sprintf(Stats[Num_stat_lines], "%d", stats->assists);
560         Num_stat_lines++;
561
562         Assert(Num_stat_lines < NUM_STAT_LINES);
563         Stat_labels[Num_stat_lines][0] = 0;
564         Stats[Num_stat_lines][0] = 0;
565         Num_stat_lines++;
566
567         Assert(Num_stat_lines < NUM_STAT_LINES);
568         Stat_labels[Num_stat_lines][0] = 0;
569         Stats[Num_stat_lines][0] = 0;
570         Num_stat_lines++;
571
572         STRCPY1(Stat_labels[Num_stat_lines], XSTR( "*Kills by Ship Type", 64));
573         Stats[Num_stat_lines][0] = 0;
574         Num_stat_lines++;
575
576         Assert(Num_stat_lines < NUM_STAT_LINES);
577         Stat_labels[Num_stat_lines][0] = 0;
578         Stats[Num_stat_lines][0] = 0;
579         Num_stat_lines++;
580
581         for (i=0; i<Num_ship_types; i++) {
582                 if (stats->kills[i]) {
583                         Assert(Num_stat_lines < NUM_STAT_LINES);
584                         Assert(strlen(Ship_info[i].name) + 1 < STAT_COLUMN1_W);
585                         sprintf(Stat_labels[Num_stat_lines], NOX("%s:"), Ship_info[i].name);
586                         sprintf(Stats[Num_stat_lines], "%d", stats->kills[i]);
587                         Num_stat_lines++;
588                 }
589         }
590
591         for (i=0; i<Num_stat_lines; i++) {
592                 gr_force_fit_string(Stat_labels[i], Stat_column1_w[gr_screen.res], Barracks_stats_coords[gr_screen.res][BARRACKS_W_COORD]);
593                 gr_force_fit_string(Stats[i], Stat_column2_w[gr_screen.res], Barracks_stats2_coords[gr_screen.res][BARRACKS_W_COORD]);
594         }
595 }
596
597 // sets or clears hotkeys for pilot selection.
598 void barracks_set_hotkeys(bool pilot_text_enter_mode)
599 {
600         Buttons[gr_screen.res][B_PILOT_SCROLL_UP_BUTTON].button.set_hotkey(pilot_text_enter_mode ? SDLK_UP : -1);
601         Buttons[gr_screen.res][B_PILOT_SCROLL_DOWN_BUTTON].button.set_hotkey(pilot_text_enter_mode ? SDLK_DOWN : -1);
602
603         Buttons[gr_screen.res][B_PILOT_CREATE_BOTTON].button.set_hotkey(pilot_text_enter_mode ? SDLK_c : -1);
604         Buttons[gr_screen.res][B_PILOT_SET_ACTIVE_BUTTON].button.set_hotkey(pilot_text_enter_mode ? SDLK_RETURN : -1);
605         Buttons[gr_screen.res][B_PILOT_DELETE_BUTTON].button.set_hotkey(pilot_text_enter_mode ? SDLK_DELETE : -1);
606
607         Buttons[gr_screen.res][B_PIC_PREV_PILOT_BUTTON].button.set_hotkey(pilot_text_enter_mode ? SDLK_LEFT : -1);
608         Buttons[gr_screen.res][B_PIC_NEXT_PILOT_BUTTON].button.set_hotkey(pilot_text_enter_mode ? SDLK_RIGHT : -1);
609
610         Buttons[gr_screen.res][B_ACCEPT_BUTTON].button.set_hotkey(pilot_text_enter_mode ? KEY_CTRLED | SDLK_RETURN : -1);
611 }
612
613 // strip the possible .pcx extension off a filename
614 void barracks_strip_pcx(char *str)
615 {
616         int flen = strlen(str);
617         int elen = 4;           
618         if ((flen > 4) && !stricmp(str + flen - elen, ".pcx")) {
619                 str[flen - elen] = '\0';
620         }
621 }
622
623 // new pilot name has focus, so update stats/pic to that pilot
624 int barracks_new_pilot_selected()
625 {
626         char stripped[MAX_FILENAME_LEN+1] = "";
627
628         // save the previous pilot first, so changes to it are kept
629         if (strlen(Cur_pilot->callsign)) {
630                 write_pilot_file();
631         }
632
633         // check if we have a valid pilot hilighted.  If so, attempt to active it
634         if ((Num_pilots < 1) || (Selected_line < 0) || (Selected_line >= Num_pilots)) {
635                 Cur_pilot->callsign[0] = 0;  // this indicates no pilot active
636                 return -1;
637         }
638
639         if (read_pilot_file(Pilots[Selected_line], !Player_sel_mode, Cur_pilot)) {
640                 Cur_pilot->callsign[0] = 0;  // this indicates no pilot active
641                 return -1;
642         }
643
644         // init stuff to reflect new pilot
645         int i;
646         barracks_init_stats(&Cur_pilot->stats);
647         for (i=0; i<Num_pilot_images; i++) {
648                 strcpy(stripped, Cur_pilot->image_filename);
649                 barracks_strip_pcx(stripped);
650                 if (!stricmp(stripped, Pilot_image_names[i])) {
651                         break;
652                 }
653         }
654         Pic_number = i;
655         for ( i=0; i<Num_pilot_squad_images; i++) {
656                 strcpy(stripped, Cur_pilot->squad_filename);
657                 barracks_strip_pcx(stripped);
658                 if (!stricmp(stripped, Pilot_squad_image_names[i])) {
659                         break;
660                 }
661         }
662         Pic_squad_number = i;   
663         
664         return 0;
665 }
666
667 void barracks_set_callsign_enter_mode(bool set_callsign_enter_mode)
668 {
669         // set global mode variable
670         Barracks_callsign_enter_mode = set_callsign_enter_mode;
671
672         // disable/enable all buttons
673         for (int idx=0; idx<BARRACKS_NUM_BUTTONS; idx++) {
674                 // don't ever mess with the prev and next squad logo buttons
675 #ifdef MAKE_FS1
676                 Buttons[gr_screen.res][idx].button.enable(!set_callsign_enter_mode);
677 #else
678                 if((idx != B_SQUAD_PREV_BUTTON) && (idx != B_SQUAD_NEXT_BUTTON)){
679                         Buttons[gr_screen.res][idx].button.enable(!set_callsign_enter_mode);
680                 }
681 #endif
682         }
683
684         // enable/disable hotkeys
685         barracks_set_hotkeys(!set_callsign_enter_mode);
686         // disable/enable inputbox
687         Inputbox.enable(set_callsign_enter_mode);
688         // hide/unhide inputbox
689         Inputbox.hide(!set_callsign_enter_mode);
690 }
691
692 // creates a new pilot file
693 void barracks_create_new_pilot()
694 {
695         // check if too many pilots
696         if (Num_pilots >= MAX_PILOTS) {
697                 gamesnd_play_iface(SND_GENERAL_FAIL);
698                 return;
699         }
700
701         // play sound for pilot creation
702         gamesnd_play_iface(SND_SCROLL);
703         
704         // only write pilot file if there is an active pilot
705         if (strlen(Player->callsign)) {
706                 write_pilot_file();
707         }
708
709         // move other pilot names and ranks down to make room for the new one
710         int idx = Num_pilots;
711         Assert(Num_pilots >= 0);
712         while (idx--) {
713                 strcpy(Pilots[idx + 1], Pilots[idx]);
714                 Pilot_ranks[idx + 1] = Pilot_ranks[idx];
715         }
716
717         Selected_line = 0;
718         Num_pilots++;
719         Pilots[Selected_line][0] = 0;
720         Pilot_ranks[Selected_line] = 0;
721         List_scroll_offset = 0;
722
723         // set mode to accept pilot name text
724         barracks_set_callsign_enter_mode(true);
725         // set focus to input box
726         Inputbox.set_focus();
727         // set initial pilot name to ""
728         Inputbox.set_text("");
729         // reset size of input box to only 1 line
730         Inputbox.update_dimensions(Barracks_list_coords[gr_screen.res][BARRACKS_X_COORD], Barracks_list_coords[gr_screen.res][BARRACKS_Y_COORD], Barracks_list_coords[gr_screen.res][BARRACKS_W_COORD], gr_get_font_height());
731 }
732
733 // exiting screen without canceling, so load in the new pilot selected here
734 int barracks_pilot_accepted()
735 {
736         char str[CALLSIGN_LEN + 1];
737
738         // check if pilot active.  If not, don't allow accept.
739         if (!Cur_pilot->callsign[0]){
740                 return -1;
741         }
742
743         // set his image 
744         player_set_squad_bitmap(Cur_pilot, Cur_pilot->squad_filename);
745
746 //      Skill_level = get_default_skill_level();
747
748         // MWA -- I think that we should be writing Cur_pilot here.
749         //write_pilot_file(!is_pilot_multi(Cur_pilot));
750         write_pilot_file( Cur_pilot );
751         
752         // when we store the LastPlayer key, we have to mark it as being single or multiplayer, so we know where to look for him
753         // (since we could have a single and a multiplayer pilot with the same callsign)
754         // we'll distinguish them by putting an M and the end of the multiplayer callsign and a P at the end of a single player
755         strcpy(str, Cur_pilot->callsign);
756         strcat(str, is_pilot_multi(Cur_pilot) ? NOX("M") : NOX("S"));
757         os_config_write_string( NULL, "LastPlayer", str );
758         return 0;
759 }
760
761 // scroll up barracks pilot list one line
762 void barracks_scroll_callsign_up()
763 {
764         if (Selected_line > 0) {
765                 Selected_line--;
766                 gamesnd_play_iface(SND_SCROLL);
767         } else {
768                 gamesnd_play_iface(SND_GENERAL_FAIL);
769         }
770         
771         if ((Selected_line >= 0) && (Selected_line < List_scroll_offset)) {
772                 List_scroll_offset = Selected_line;
773         }
774 }
775
776 // scroll down barracks pilot list one line
777 void barracks_scroll_callsign_down()
778 {
779         if (Selected_line < Num_pilots - 1) {
780                 Selected_line++;
781                 gamesnd_play_iface(SND_SCROLL);
782         } else {
783                 gamesnd_play_iface(SND_GENERAL_FAIL);
784         }
785         
786         // num_pilots_to_fill_height is the number of pilots that can fit in given height
787         int num_pilots_to_fill_height = Barracks_list_coords[gr_screen.res][BARRACKS_H_COORD] / gr_get_font_height();
788         if (Selected_line >= List_scroll_offset + num_pilots_to_fill_height) {
789                 List_scroll_offset++;
790         }
791 }
792
793 // scroll up barracks stats list one line
794 void barracks_scroll_stats_up()
795 {
796         if (Stats_scroll_offset > 0) {
797                 Stats_scroll_offset--;
798                 gamesnd_play_iface(SND_SCROLL);
799         } else {
800                 gamesnd_play_iface(SND_GENERAL_FAIL);
801         }
802 }
803
804 // scroll down barracks stats list one line
805 void barracks_scroll_stats_down()
806 {
807         int font_height = gr_get_font_height();
808
809         if (Stats_scroll_offset + Barracks_stats_coords[gr_screen.res][BARRACKS_H_COORD] / font_height < Num_stat_lines) {
810                 Stats_scroll_offset++;
811                 gamesnd_play_iface(SND_SCROLL);
812         } else {
813                 gamesnd_play_iface(SND_GENERAL_FAIL);
814         }
815 }
816
817 // show previous pilot pic
818 void barracks_prev_pic()
819 {
820         // check if no pilot images or no pilot selected
821         if ((Num_pilot_images == 0) || (Cur_pilot->callsign[0] == '\0')) {
822                 gamesnd_play_iface(SND_GENERAL_FAIL);
823                 return;
824         }
825
826         // reset pilot pic number
827         Pic_number--;
828         if (Pic_number < 0) {
829                 Pic_number = Num_pilot_images - 1;
830         }
831
832         // copy pilot pic filename into pilot struct
833         if ((Pic_number >= 0) && (Pic_number < Num_pilot_images)) {
834                 strcpy(Cur_pilot->image_filename, Pilot_image_names[Pic_number]);
835         }
836
837         // play scroll sound
838         gamesnd_play_iface(SND_SCROLL);
839 }
840
841 // show next pilot pic
842 void barracks_next_pic()
843 {
844         // check if no pilot images or no pilot selected
845         if ((Num_pilot_images == 0) || (Cur_pilot->callsign[0] == '\0')) {
846                 gamesnd_play_iface(SND_GENERAL_FAIL);
847                 return;
848         }
849
850         // reset pilot pic number
851         Pic_number++;
852         if (Pic_number >= Num_pilot_images){
853                 Pic_number = 0;
854         }
855
856         // copy pilot pic filename into pilot struct
857         if ((Pic_number >= 0) && (Pic_number < Num_pilot_images)){
858                 strcpy(Cur_pilot->image_filename, Pilot_image_names[Pic_number]);
859         }
860
861         // play scroll sound
862         gamesnd_play_iface(SND_SCROLL);
863 }
864
865 // show previous squad pic
866 void barracks_prev_squad_pic()
867 {
868         // check if no pilot images or no pilot selected
869         if ((Num_pilot_squad_images == 0) || (Cur_pilot->callsign[0] == '\0')) {
870                 gamesnd_play_iface(SND_GENERAL_FAIL);
871                 return;
872         }
873
874         // reset pilot pic number
875         Pic_squad_number--;
876         if (Pic_squad_number < 0) {
877                 Pic_squad_number = Num_pilot_squad_images - 1;
878         }
879
880         // copy pilot pic filename into pilot struct
881         if ((Pic_squad_number >= 0) && (Pic_squad_number < Num_pilot_squad_images)) {
882                 strcpy(Cur_pilot->squad_filename, Pilot_squad_image_names[Pic_squad_number]);
883         }
884
885         // play scroll sound
886         gamesnd_play_iface(SND_SCROLL);
887 }
888
889 // show next pilot pic
890 void barracks_next_squad_pic()
891 {
892         // check if no pilot images or no pilot selected
893         if ((Num_pilot_squad_images == 0) || (Cur_pilot->callsign[0] == '\0')) {
894                 gamesnd_play_iface(SND_GENERAL_FAIL);
895                 return;
896         }
897
898         // reset pilot pic number
899         Pic_squad_number++;
900         if (Pic_squad_number >= Num_pilot_squad_images){
901                 Pic_squad_number = 0;
902         }
903
904         // copy pilot pic filename into pilot struct
905         if ((Pic_squad_number >= 0) && (Pic_squad_number < Num_pilot_squad_images)){
906                 strcpy(Cur_pilot->squad_filename, Pilot_squad_image_names[Pic_squad_number]);
907         }
908
909         // play scroll sound
910         gamesnd_play_iface(SND_SCROLL);
911 }
912
913 void barracks_delete_pilot()
914 {
915         char buf[MAX_FILENAME_LEN];
916         int active = 0;
917
918         if (!Num_pilots) {
919                 gamesnd_play_iface(SND_GENERAL_FAIL);
920                 return;
921         }
922
923         int popup_rval = popup(PF_TITLE_BIG | PF_TITLE_RED, 2, POPUP_NO, POPUP_YES, XSTR( "Warning!\n\nAre you sure you wish to delete this pilot?", 65));
924         if (popup_rval != 1) {
925                 return;
926         }
927
928         if (!stricmp(Pilots[Selected_line], Cur_pilot->callsign)) {
929                 active = 1;
930         }
931
932         strcpy(buf, Pilots[Selected_line]);
933         for (int i=Selected_line; i<Num_pilots-1; i++) {
934                 strcpy(Pilots[i], Pilots[i + 1]);
935                 Pilot_ranks[i] = Pilot_ranks[i + 1];
936         }
937
938         Num_pilots--;
939         if (Selected_line >= Num_pilots) {
940                 Selected_line = Num_pilots - 1;
941         }
942
943         if (active) {
944                 if (Selected_line >= 0) {
945                         barracks_new_pilot_selected();
946                 } else {
947                         Cur_pilot->callsign[0] = 0;
948                 }
949         }
950
951         delete_pilot_file(buf, Player_sel_mode == PLAYER_SELECT_MODE_SINGLE ? 1 : 0);
952         gamesnd_play_iface(SND_USER_SELECT);
953 }
954
955 // Filter out pilots of wrong type (which shouldn't be in the directory we are checking, but just to be safe..)
956 int barracks_pilot_filter(const char *filename)
957 {
958         int r, rank;
959
960         r = verify_pilot_file(filename, Player_sel_mode == PLAYER_SELECT_MODE_SINGLE, &rank);
961         if (rank >= Rank_pips_count)
962                 rank = Rank_pips_count - 1;
963
964         if (!r) {
965                 Pilot_ranks[Num_pilots++] = rank;
966         }
967
968         return !r;
969 }
970
971 // callback handler for the squadon selection buttons when they are disabled (in single player)
972 void barracks_squad_change_popup()
973 {
974         // show a popup
975         popup( PF_USE_AFFIRMATIVE_ICON | PF_NO_NETWORKING, 1, POPUP_OK, XSTR("You cannot change your squadron in Single Player mode.", 1445));
976 }
977
978
979 void barracks_init_player_stuff(int mode)
980 {
981         // determine if we should be looking for single or multiplayers at the outset
982         Player_sel_mode = mode;
983         
984         // get the list of pilots based upon whether we're in single or multiplayer mode
985         Num_pilots = 0;
986         Get_file_list_filter = barracks_pilot_filter;
987
988         // single player specific stuff
989         if (mode == PLAYER_SELECT_MODE_SINGLE) {
990                 Num_pilots = cf_get_file_list_preallocated(MAX_PILOTS, Pilots_arr, Pilots, CF_TYPE_SINGLE_PLAYERS, NOX("*.plr"), CF_SORT_TIME);
991
992 #ifndef MAKE_FS1
993                 // disable squad logo switching         
994                 Buttons[gr_screen.res][B_SQUAD_PREV_BUTTON].button.hide();
995                 Buttons[gr_screen.res][B_SQUAD_PREV_BUTTON].button.disable();
996                 Buttons[gr_screen.res][B_SQUAD_PREV_BUTTON].button.set_disabled_action(barracks_squad_change_popup);
997                 Buttons[gr_screen.res][B_SQUAD_NEXT_BUTTON].button.hide();
998                 Buttons[gr_screen.res][B_SQUAD_NEXT_BUTTON].button.disable();                   
999                 Buttons[gr_screen.res][B_SQUAD_NEXT_BUTTON].button.set_disabled_action(barracks_squad_change_popup);
1000 #endif
1001         }
1002         // multiplayer specific stuff
1003         else {
1004                 Num_pilots = cf_get_file_list_preallocated(MAX_PILOTS, Pilots_arr, Pilots, CF_TYPE_MULTI_PLAYERS, NOX("*.plr"), CF_SORT_TIME);
1005
1006 #ifndef MAKE_FS1
1007                 // enable squad logo switching
1008                 Buttons[gr_screen.res][B_SQUAD_PREV_BUTTON].button.enable();
1009                 Buttons[gr_screen.res][B_SQUAD_PREV_BUTTON].button.unhide();
1010                 Buttons[gr_screen.res][B_SQUAD_NEXT_BUTTON].button.enable();
1011                 Buttons[gr_screen.res][B_SQUAD_NEXT_BUTTON].button.unhide();                    
1012 #endif
1013         }
1014
1015         int ranks[MAX_PILOTS];
1016
1017         for (int i=0; i<Num_pilots; i++) {
1018                 int j;
1019                 for (j=0; j<Num_pilots; j++) {
1020                         if (!strcmp(Pilots[i], Pilots_arr[j])) {
1021                                 ranks[i] = Pilot_ranks[j];
1022                                 break;
1023                         }
1024                 }
1025                 Assert(j < Num_pilots);  // Pilot not found?  How is that possible?
1026         }
1027
1028         for (int i=0; i<Num_pilots; i++) {
1029                 Pilot_ranks[i] = ranks[i];
1030         }
1031
1032         Selected_line = List_scroll_offset = 0;
1033         barracks_new_pilot_selected();
1034
1035 }
1036
1037 void barracks_button_pressed(int n)
1038 {
1039         switch (n) {
1040                 case B_PILOT_SCROLL_UP_BUTTON:
1041                         barracks_scroll_callsign_up();
1042                         break;
1043
1044                 case B_PILOT_SCROLL_DOWN_BUTTON:
1045                         barracks_scroll_callsign_down();
1046                         break;
1047
1048                 case B_STATS_SCROLL_UP_BUTTON:
1049                         barracks_scroll_stats_up();
1050                         break;
1051
1052                 case B_STATS_SCROLL_DOWN_BUTTON:
1053                         barracks_scroll_stats_down();
1054                         break;
1055
1056                 case B_PIC_PREV_PILOT_BUTTON:
1057                         barracks_prev_pic();
1058                         break;
1059
1060                 case B_PIC_NEXT_PILOT_BUTTON:
1061                         barracks_next_pic();
1062                         break;
1063
1064 #ifndef MAKE_FS1
1065                 case B_SQUAD_PREV_BUTTON:
1066                         barracks_prev_squad_pic();
1067                         break;
1068
1069                 case B_SQUAD_NEXT_BUTTON:
1070                         barracks_next_squad_pic();
1071                         break;
1072 #endif
1073
1074                 case B_PILOT_SET_ACTIVE_BUTTON:
1075                         if (barracks_new_pilot_selected()){
1076                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1077                                 
1078                                 // throw up a popup telling the player that he should create a pilot first
1079                                 if(Player_sel_mode == PLAYER_SELECT_MODE_SINGLE){
1080                                         popup(PF_USE_AFFIRMATIVE_ICON,1,POPUP_OK,XSTR( "You must create a single player pilot.", 66));
1081                                 } else {
1082                                         popup(PF_USE_AFFIRMATIVE_ICON,1,POPUP_OK,XSTR( "You must create a multi player pilot.", 67));
1083                                 }
1084                         } else {
1085                                 gamesnd_play_iface(SND_SCROLL);
1086                         }
1087                         break;
1088
1089                 case B_ACCEPT_BUTTON:                   
1090                         if (Num_pilots && !barracks_pilot_accepted()) {
1091                                 gamesnd_play_iface(SND_COMMIT_PRESSED);
1092                                 gameseq_post_event(GS_EVENT_MAIN_MENU);
1093                         } else {
1094                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1095
1096                                 // throw up a popup telling the player that he should create a pilot first
1097                                 if(Player_sel_mode == PLAYER_SELECT_MODE_SINGLE){
1098                                         popup(PF_USE_AFFIRMATIVE_ICON,1,POPUP_OK,XSTR( "You must create a single player pilot.", 66));
1099                                 } else {
1100                                         popup(PF_USE_AFFIRMATIVE_ICON,1,POPUP_OK,XSTR( "You must create a multi player pilot.", 67));
1101                                 }
1102                         }
1103                         break;
1104
1105                 case B_PILOT_CLONE_BUTTON:
1106                         if (Num_pilots < 1) {
1107                                 gamesnd_play_error_beep();
1108                                 break;
1109                         }
1110
1111                         Clone_flag = 1;
1112                         barracks_create_new_pilot();
1113                         break;
1114
1115                 case B_PILOT_CONVERT_BUTTON: {
1116 #if defined(DEMO) || defined(OEM_BUILD)
1117                         game_feature_not_in_demo_popup();
1118 #else
1119                         const char *str;
1120                         char temp[256];
1121                         char old_pic[256] = "";
1122                         char old_squad_pic[256] = "";
1123                         char old_squad[256] = "";
1124                         int z;
1125
1126                         if (!barracks_new_pilot_selected()) {
1127                                 if (Player_sel_mode == PLAYER_SELECT_MODE_SINGLE)
1128                                         str = XSTR( "multiplayer", 68);
1129                                 else
1130                                         str = XSTR( "single player", 69);
1131
1132                                 sprintf(temp, XSTR( "This will overwrite your %s pilot.  Proceed?", 70), str);
1133                                 if (!verify_pilot_file(Cur_pilot->callsign, Player_sel_mode == PLAYER_SELECT_MODE_MULTI)) {
1134                                         z = popup(0, 2, POPUP_CANCEL, POPUP_OK, temp);
1135                                         if (z != 1)
1136                                                 break;
1137                                 }
1138
1139                                 strcpy(old_pic, Cur_pilot->image_filename);
1140                                 strcpy(old_squad_pic, Cur_pilot->squad_filename);
1141                                 strcpy(old_squad, Cur_pilot->squad_name);
1142                                 init_new_pilot(Cur_pilot, 0);
1143                                 strcpy(Cur_pilot->image_filename, old_pic);
1144                                 strcpy(Cur_pilot->squad_filename, old_squad_pic);
1145                                 strcpy(Cur_pilot->squad_name, old_squad);
1146                                 if (Player_sel_mode == PLAYER_SELECT_MODE_SINGLE) {
1147                                         Cur_pilot->flags |= PLAYER_FLAGS_IS_MULTI;
1148                                         write_pilot_file();
1149                                         barracks_init_player_stuff(PLAYER_SELECT_MODE_MULTI);
1150
1151                                 } else {
1152                                         write_pilot_file();
1153                                         barracks_init_player_stuff(PLAYER_SELECT_MODE_SINGLE);
1154                                 }
1155
1156                                 gamesnd_play_iface(SND_USER_SELECT);
1157
1158                         } else {
1159                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1160                         }
1161 #endif
1162                         break;
1163                 }
1164
1165                 case B_PILOT_CREATE_BOTTON:
1166                         Clone_flag = 0;
1167                         barracks_create_new_pilot();
1168                         break;
1169
1170                 case B_HELP_BUTTON:
1171                         launch_context_help();
1172                         gamesnd_play_iface(SND_HELP_PRESSED);
1173                         break;
1174
1175                 case B_OPTION_BUTTON:
1176                         gamesnd_play_iface(SND_SWITCH_SCREENS);
1177                         gameseq_post_event(GS_EVENT_OPTIONS_MENU);
1178                         break;
1179
1180                 case B_STATS_MEDAL_BUTTON:
1181 #if defined(FS2_DEMO) || defined(FS1_DEMO)
1182                         game_feature_not_in_demo_popup();
1183 #else
1184                         gamesnd_play_iface(SND_SWITCH_SCREENS);
1185                         gameseq_post_event(GS_EVENT_VIEW_MEDALS);
1186 #endif
1187                         break;
1188
1189                 case B_PILOT_DELETE_BUTTON:
1190                         barracks_delete_pilot();
1191                         break;
1192
1193                 case B_PILOT_SINGLE_MODE_BUTTON:
1194                         if (Player_sel_mode != PLAYER_SELECT_MODE_SINGLE) {
1195                                 gamesnd_play_iface(SND_USER_SELECT);
1196                                 barracks_init_player_stuff(PLAYER_SELECT_MODE_SINGLE);
1197                         }                                               
1198                         break;
1199
1200                 case B_PILOT_MULTI_MODE_BUTTON:
1201 #if defined(DEMO) || defined(OEM_BUILD) // not for FS2_DEMO
1202                         game_feature_not_in_demo_popup();
1203 #else
1204                         if (Player_sel_mode != PLAYER_SELECT_MODE_MULTI) {
1205                                 gamesnd_play_iface(SND_USER_SELECT);
1206                                 barracks_init_player_stuff(PLAYER_SELECT_MODE_MULTI);
1207                         }
1208 #endif
1209                         break;
1210         }
1211 }
1212
1213
1214 void barracks_display_pilot_callsigns(int prospective_pilot)
1215 {
1216         int y = 0;
1217         int cur_pilot_idx = List_scroll_offset;
1218
1219         int multi = 0;
1220         if (Player_sel_mode == PLAYER_SELECT_MODE_MULTI) {
1221                 multi = 1;
1222         }
1223
1224         int font_height = gr_get_font_height();
1225         while (y + font_height <= Barracks_list_coords[gr_screen.res][BARRACKS_H_COORD]) {
1226                 if (cur_pilot_idx >= Num_pilots)
1227                         break;
1228
1229                 if (!stricmp(Cur_pilot->callsign, Pilots[cur_pilot_idx]) && (is_pilot_multi(Cur_pilot) == multi)) {
1230                         if ((cur_pilot_idx == Selected_line) || (cur_pilot_idx == prospective_pilot)) {
1231                                 gr_set_color_fast(&Color_text_active_hi);
1232                         } else {
1233                                 gr_set_color_fast(&Color_text_active);
1234                         }
1235                 } else {
1236                         if (cur_pilot_idx == Selected_line) {
1237                                 gr_set_color_fast(&Color_text_selected);
1238                         } else if (cur_pilot_idx == prospective_pilot) {
1239                                 gr_set_color_fast(&Color_text_subselected);
1240                         } else {
1241                                 gr_set_color_fast(&Color_text_normal);
1242                         }
1243                 }
1244
1245                 gr_printf(Barracks_list_coords[gr_screen.res][BARRACKS_X_COORD], Barracks_list_coords[gr_screen.res][BARRACKS_Y_COORD] + y, Pilots[cur_pilot_idx]);
1246 #ifndef FS1_DEMO
1247                 gr_set_bitmap(Rank_pips_bitmaps + Pilot_ranks[cur_pilot_idx], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1248                 gr_bitmap(Barracks_list_coords[gr_screen.res][BARRACKS_X_COORD] - 34, Barracks_list_coords[gr_screen.res][BARRACKS_Y_COORD] + y);
1249 #endif
1250  
1251                 y += font_height;
1252                 cur_pilot_idx++;
1253         }
1254 }
1255
1256
1257
1258 void barracks_display_pilot_stats()
1259 {
1260         int y = 0;
1261         int z = Stats_scroll_offset;
1262         int font_height = gr_get_font_height();
1263         char *str;
1264         int i, w, h;
1265         while (y + font_height <= Barracks_stats_coords[gr_screen.res][BARRACKS_H_COORD]) {
1266                 if (z >= Num_stat_lines) {
1267                         break;
1268                 }
1269
1270                 str = Stat_labels[z];
1271                 if (*str == '*') {
1272                         gr_set_color_fast(&Color_text_heading);
1273                         str++;
1274
1275                         gr_get_string_size(&w, &h, str);
1276                         i = Barracks_stats_coords[gr_screen.res][BARRACKS_Y_COORD] + y + h / 2 - 1;                     
1277                         gr_line(Barracks_stats_coords[gr_screen.res][BARRACKS_X_COORD], i, Barracks_stats_coords[gr_screen.res][BARRACKS_X_COORD] + Barracks_stats_coords[gr_screen.res][BARRACKS_W_COORD] - w - 2, i);
1278                         gr_line(Barracks_stats_coords[gr_screen.res][BARRACKS_X_COORD] + Barracks_stats_coords[gr_screen.res][BARRACKS_W_COORD] + 1, i, Barracks_stats2_coords[gr_screen.res][BARRACKS_X_COORD] + Barracks_stats2_coords[gr_screen.res][BARRACKS_W_COORD], i);
1279
1280                 } else {
1281                         gr_set_color_fast(&Color_text_normal);
1282                 }
1283
1284                 gr_get_string_size(&w, NULL, str);
1285                 gr_printf(Barracks_stats_coords[gr_screen.res][BARRACKS_X_COORD] + Barracks_stats_coords[gr_screen.res][BARRACKS_W_COORD] - w, Barracks_stats_coords[gr_screen.res][BARRACKS_Y_COORD] + y, "%s", str);
1286                 str = Stats[z];
1287                 if (*str) {
1288                         gr_printf(Barracks_stats2_coords[gr_screen.res][BARRACKS_X_COORD], Barracks_stats_coords[gr_screen.res][BARRACKS_Y_COORD] + y, "%s", str);
1289                 }
1290
1291                 y += font_height;
1292                 z++;
1293         }
1294 }
1295
1296
1297 // process pilot callsign
1298 void barracks_accept_new_pilot_callsign()
1299 {
1300         char buf[CALLSIGN_LEN + 1];
1301         char name[MAX_FILENAME_LEN];
1302         int i;
1303
1304         int z = 0;
1305         Inputbox.get_text(buf);
1306         drop_white_space(buf);
1307
1308         if (!isalpha(*buf)) {
1309                 z = 1;
1310         } else {
1311                 for (i=1; buf[i]; i++) {
1312                         if (!isalpha(buf[i]) && !isdigit(buf[i]) && !strchr(VALID_PILOT_CHARS, buf[i])) {
1313                                 z = 1;
1314                                 return;
1315                         }
1316                 }
1317         }
1318
1319         for (i=1; i<Num_pilots; i++) {
1320                 if (!stricmp(buf, Pilots[i])) {
1321                         z = 1;
1322                         if (pilot_verify_overwrite() == 1) {
1323                                 strcpy(name, Pilots[Selected_line]);
1324                                 for (z=i; z<Num_pilots-1; z++) {
1325                                         strcpy(Pilots[z], Pilots[z + 1]);
1326                                         Pilot_ranks[z] = Pilot_ranks[z + 1];
1327                                 }
1328
1329                                 Num_pilots--;
1330                                 delete_pilot_file(name, Player_sel_mode == PLAYER_SELECT_MODE_SINGLE ? 1 : 0);
1331                                 z = 0;
1332                         }
1333                         return;
1334                 }
1335         }
1336
1337         if (!*buf || (i < Num_pilots)) { // duplicate name, alert user
1338                 z = 1;
1339         }
1340
1341         if (z) {
1342                 gamesnd_play_iface(SND_GENERAL_FAIL);
1343                 return;
1344         }
1345
1346         strcpy(Pilots[0], buf);
1347         strcpy(Cur_pilot->callsign, buf);
1348         init_new_pilot(Cur_pilot, !Clone_flag);
1349         
1350         // again, make sure we set his flags correctly to ensure that he gets saved to the proper directory and gets
1351         // displayed correctly
1352         if (Player_sel_mode == PLAYER_SELECT_MODE_SINGLE) {
1353                 Cur_pilot->flags &= ~(PLAYER_FLAGS_IS_MULTI);
1354         } else {
1355                 Cur_pilot->flags |= PLAYER_FLAGS_IS_MULTI;
1356                 Cur_pilot->stats.flags |= STATS_FLAG_MULTIPLAYER;
1357         }
1358
1359         if ( !(Game_mode & GM_STANDALONE_SERVER) ) {
1360                 write_pilot_file(Cur_pilot);
1361         }
1362
1363         Selected_line = 0;
1364         barracks_new_pilot_selected();
1365         barracks_set_callsign_enter_mode(false);
1366 }
1367
1368
1369 // draw pilot image and clean up afterwards
1370 void barracks_draw_pilot_pic()
1371 {
1372         // draw pilot pic
1373         if (Cur_pilot->callsign[0] && (Pic_number >= 0) && (Pic_number < Num_pilot_images)) {
1374                 if (Pilot_images[Pic_number] >= 0) {
1375                         // JAS: This code is hacked to allow the animation to use all 256 colors
1376                         extern int Palman_allow_any_color;
1377                         Palman_allow_any_color = 1;
1378                         gr_set_bitmap(Pilot_images[Pic_number], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1379                         gr_bitmap(Barracks_image_coords[gr_screen.res][BARRACKS_X_COORD], Barracks_image_coords[gr_screen.res][BARRACKS_Y_COORD]);
1380                         Palman_allow_any_color = 0;
1381
1382                         // print number of the current pic
1383                         char buf[40];                   
1384                         sprintf(buf, XSTR( "%d of %d", 71), Pic_number + 1, Num_pilot_images);
1385                         gr_printf(Barracks_image_number_coords[gr_screen.res][BARRACKS_X_COORD], Barracks_image_number_coords[gr_screen.res][BARRACKS_Y_COORD], buf);                           
1386                 }
1387         } else {
1388                 Pic_number = -1;
1389         }
1390 }
1391
1392 // draw squad image and clean up afterwards
1393 void barracks_draw_squad_pic()
1394 {
1395         char buf[40];
1396
1397         // draw pilot pic
1398         if (Cur_pilot->callsign[0] && (Pic_squad_number >= 0) && (Pic_squad_number < Num_pilot_squad_images)) {
1399                 if (Pilot_squad_images[Pic_squad_number] >= 0) {
1400                         // JAS: This code is hacked to allow the animation to use all 256 colors
1401                         extern int Palman_allow_any_color;
1402                         Palman_allow_any_color = 1;
1403                         gr_set_bitmap(Pilot_squad_images[Pic_squad_number], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1404                         gr_bitmap(Barracks_squad_coords[gr_screen.res][BARRACKS_X_COORD], Barracks_squad_coords[gr_screen.res][BARRACKS_Y_COORD]);
1405                         Palman_allow_any_color = 0;
1406
1407                         // print number of current squad pic
1408                         if(Player_sel_mode != PLAYER_SELECT_MODE_SINGLE){
1409                                 sprintf(buf,XSTR( "%d of %d", 71), Pic_squad_number+1, Num_pilot_squad_images);
1410                                 gr_printf(Barracks_squad_number_coords[gr_screen.res][BARRACKS_X_COORD], Barracks_squad_number_coords[gr_screen.res][BARRACKS_Y_COORD], buf);
1411                         }
1412                 }
1413         } else {
1414                 Pic_squad_number = -1;
1415         }       
1416 }
1417
1418 // -----------------------------------------------------------------------------
1419 void barracks_init()
1420 {
1421 #ifndef MAKE_FS1
1422         UI_WINDOW *w = &Ui_window;
1423 #endif
1424
1425         // save current pilot file, so we don't possibly loose it.
1426         write_pilot_file();
1427
1428         // create interface
1429         Ui_window.create(0, 0, gr_screen.max_w, gr_screen.max_h, 0);
1430         Ui_window.set_mask_bmap(Barracks_bitmap_mask_fname[gr_screen.res]);
1431
1432         // load background bitmap
1433         Background_bitmap = bm_load(Barracks_bitmap_fname[gr_screen.res]);
1434         if(Background_bitmap < 0){
1435                 // we failed to load the bitmap - this is very bad
1436                 Int3();
1437         }
1438
1439         // create buttons
1440         for (int i=0; i<BARRACKS_NUM_BUTTONS; i++) {
1441                 // create the object
1442                 Buttons[gr_screen.res][i].button.create(&Ui_window, "", Buttons[gr_screen.res][i].x, Buttons[gr_screen.res][i].y, 60, 30, Buttons[gr_screen.res][i].repeat, 1);
1443
1444                 // set the sound to play when highlighted
1445                 Buttons[gr_screen.res][i].button.set_highlight_action(common_play_highlight_sound);
1446
1447                 // set the ani for the button
1448                 Buttons[gr_screen.res][i].button.set_bmaps(Buttons[gr_screen.res][i].filename);
1449
1450                 // set the hotspot
1451                 Buttons[gr_screen.res][i].button.link_hotspot(Buttons[gr_screen.res][i].hotspot);
1452         }
1453
1454         // add all strings      
1455 #ifndef MAKE_FS1
1456         w->add_XSTR("Create", 1034, Buttons[gr_screen.res][0].text_x,  Buttons[gr_screen.res][0].text_y, &Buttons[gr_screen.res][0].button, UI_XSTR_COLOR_GREEN);
1457         w->add_XSTR("Accept", 1035, Buttons[gr_screen.res][5].text_x,  Buttons[gr_screen.res][5].text_y, &Buttons[gr_screen.res][5].button, UI_XSTR_COLOR_PINK);
1458         w->add_XSTR("Help",   928, Buttons[gr_screen.res][6].text_x,  Buttons[gr_screen.res][6].text_y, &Buttons[gr_screen.res][6].button, UI_XSTR_COLOR_GREEN);
1459         w->add_XSTR("Options",1036, Buttons[gr_screen.res][7].text_x,  Buttons[gr_screen.res][7].text_y, &Buttons[gr_screen.res][7].button, UI_XSTR_COLOR_GREEN);
1460         w->add_XSTR("Medals", 1037, Buttons[gr_screen.res][8].text_x,  Buttons[gr_screen.res][8].text_y, &Buttons[gr_screen.res][8].button, UI_XSTR_COLOR_GREEN);
1461         w->add_XSTR("Remove", 1038, Buttons[gr_screen.res][11].text_x,  Buttons[gr_screen.res][11].text_y, &Buttons[gr_screen.res][11].button, UI_XSTR_COLOR_GREEN);
1462         w->add_XSTR("Select", 1552, Buttons[gr_screen.res][12].text_x,  Buttons[gr_screen.res][12].text_y, &Buttons[gr_screen.res][12].button, UI_XSTR_COLOR_GREEN);
1463         w->add_XSTR("Clone",  1040, Buttons[gr_screen.res][13].text_x,  Buttons[gr_screen.res][13].text_y, &Buttons[gr_screen.res][13].button, UI_XSTR_COLOR_GREEN);
1464         w->add_XSTR("Single", 1041, Buttons[gr_screen.res][14].text_x,  Buttons[gr_screen.res][14].text_y,  &Buttons[gr_screen.res][14].button, UI_XSTR_COLOR_GREEN);
1465         w->add_XSTR("Multi",  1042, Buttons[gr_screen.res][15].text_x,  Buttons[gr_screen.res][15].text_y,  &Buttons[gr_screen.res][15].button, UI_XSTR_COLOR_GREEN);
1466         w->add_XSTR("Convert",1043, Buttons[gr_screen.res][16].text_x,  Buttons[gr_screen.res][16].text_y, &Buttons[gr_screen.res][16].button, UI_XSTR_COLOR_GREEN);    
1467         for(int i=0; i<BARRACKS_NUM_TEXT; i++) {
1468                 w->add_XSTR(&Barracks_text[gr_screen.res][i]);
1469         }
1470 #endif
1471
1472         // button for selecting pilot
1473         List_region.create(&Ui_window, "", Barracks_list_coords[gr_screen.res][BARRACKS_X_COORD], Barracks_list_coords[gr_screen.res][BARRACKS_Y_COORD], Barracks_list_coords[gr_screen.res][BARRACKS_W_COORD], Barracks_list_coords[gr_screen.res][BARRACKS_H_COORD], 0, 1);
1474         List_region.hide();
1475
1476         // create input box (for new pilot)
1477         Inputbox.create(&Ui_window, Barracks_list_coords[gr_screen.res][BARRACKS_X_COORD], Barracks_list_coords[gr_screen.res][BARRACKS_Y_COORD], Barracks_list_coords[gr_screen.res][BARRACKS_W_COORD], CALLSIGN_LEN - 1, "", UI_INPUTBOX_FLAG_INVIS | UI_INPUTBOX_FLAG_KEYTHRU | UI_INPUTBOX_FLAG_LETTER_FIRST);
1478         Inputbox.set_valid_chars(VALID_PILOT_CHARS);
1479         Inputbox.disable();
1480         Inputbox.hide();
1481
1482         // load in help overlay bitmap  
1483         help_overlay_load(BARRACKS_OVERLAY);
1484         help_overlay_set_state(BARRACKS_OVERLAY,0);     
1485
1486         // other init stuff
1487         Barracks_callsign_enter_mode = 0;       
1488         List_scroll_offset = Stats_scroll_offset = Pic_number = Pic_squad_number = Selected_line = 0;
1489         Cur_pilot = &Players[Player_num];
1490
1491         // disable squad logo selection buttons in single player
1492 #ifndef MAKE_FS1
1493         if(!(Cur_pilot->flags & PLAYER_FLAGS_IS_MULTI)){
1494                 // squad logo picture buttons           
1495                 Buttons[gr_screen.res][B_SQUAD_PREV_BUTTON].button.hide();
1496                 Buttons[gr_screen.res][B_SQUAD_PREV_BUTTON].button.disable();
1497                 Buttons[gr_screen.res][B_SQUAD_NEXT_BUTTON].button.hide();
1498                 Buttons[gr_screen.res][B_SQUAD_NEXT_BUTTON].button.disable();
1499         } else {
1500                 // squad logo picture buttons           
1501                 Buttons[gr_screen.res][B_SQUAD_PREV_BUTTON].button.enable();
1502                 Buttons[gr_screen.res][B_SQUAD_PREV_BUTTON].button.unhide();
1503                 Buttons[gr_screen.res][B_SQUAD_NEXT_BUTTON].button.enable();
1504                 Buttons[gr_screen.res][B_SQUAD_NEXT_BUTTON].button.unhide();            
1505         }
1506 #endif
1507
1508         // set up hotkeys for buttons so we draw the correct animation frame when a key is pressed
1509         barracks_set_hotkeys(1);
1510
1511         // load ramp pips
1512 #ifndef FS1_DEMO
1513         Rank_pips_bitmaps = bm_load_animation("IconRankMini.ani", &Rank_pips_count);    
1514 #endif
1515
1516         // load up the pilot pic list
1517         pilot_load_pic_list();  
1518         pilot_load_squad_pic_list();
1519         
1520         // don't load pilot images yet
1521         for (int i=0; i<MAX_PILOT_IMAGES; i++) {
1522                 Pilot_images[i] = BARRACKS_IMAGE_NOT_LOADED;  // while -1 is can't load
1523                 Pilot_squad_images[i] = BARRACKS_IMAGE_NOT_LOADED;
1524         }
1525         
1526         // init stats
1527         barracks_init_stats(&Cur_pilot->stats);
1528
1529         // disable some buttons for the multiplayer beta and e3 build
1530 #if defined(MULTIPLAYER_BETA_BUILD) || defined(E3_BUILD) || defined(PRESS_TOUR_BUILD)
1531         Buttons[gr_screen.res][B_PILOT_CLONE_BUTTON].button.hide();
1532         Buttons[gr_screen.res][B_PILOT_CONVERT_BUTTON].button.hide();   
1533         Buttons[gr_screen.res][B_PILOT_CLONE_BUTTON].button.disable();  
1534         Buttons[gr_screen.res][B_PILOT_CONVERT_BUTTON].button.disable();        
1535 #endif
1536
1537         // multiplayer beta build
1538 #ifdef MULTIPLAYER_BETA_BUILD
1539         Buttons[gr_screen.res][B_PILOT_SINGLE_MODE_BUTTON].button.hide();
1540         Buttons[gr_screen.res][B_PILOT_SINGLE_MODE_BUTTON].button.disable();
1541 #endif
1542
1543         // e3 build
1544 #if defined(E3_BUILD) || defined(PRESS_TOUR_BUILD)
1545         Buttons[gr_screen.res][B_PILOT_MULTI_MODE_BUTTON].button.hide();
1546         Buttons[gr_screen.res][B_PILOT_MULTI_MODE_BUTTON].button.disable();
1547 #endif
1548
1549         // base the mode we're in (single or multi) on the status of the currently selected pilot
1550 #ifdef MULTIPLAYER_BETA_BUILD
1551         barracks_init_player_stuff(1);
1552 #elif defined(E3_BUILD) || defined(PRESS_TOUR_BUILD)
1553         barracks_init_player_stuff(0);
1554 #else
1555         barracks_init_player_stuff(is_pilot_multi(Player));     
1556 #endif
1557
1558 #if defined(MAKE_FS1) && !defined(FS1_DEMO)
1559         PilotWin01 = bm_load(NOX("PilotWin01"));
1560         PilotWin02 = bm_load(NOX("PilotWin02"));
1561         PilotWin03 = bm_load(NOX("PilotWin03"));
1562         PilotWin04 = bm_load(NOX("PilotWin04"));
1563 #endif
1564 }
1565
1566 // -----------------------------------------------------------------------------
1567 void barracks_do_frame(float frametime)
1568 {
1569         int k = Ui_window.process();
1570
1571         if ( k > 0 ) {
1572                 if ( help_overlay_active(BARRACKS_OVERLAY) ) {
1573                         help_overlay_set_state(BARRACKS_OVERLAY,0);
1574                         k = 0;
1575                 }
1576         }
1577
1578         // pilot that mouse is over
1579         int prospective_pilot = -1;
1580         int i;
1581
1582         // Entering pilot callsign
1583         if (Barracks_callsign_enter_mode) {
1584                 // set focus to inputbox
1585                 Inputbox.set_focus();
1586
1587                 switch (k) {
1588                         case SDLK_ESCAPE:
1589                                 // cancel create pilot
1590                                 Num_pilots--;
1591                                 for (i=0; i<Num_pilots; i++) {
1592                                         strcpy(Pilots[i], Pilots[i + 1]);
1593                                         Pilot_ranks[i] = Pilot_ranks[i + 1];
1594                                 }
1595
1596                                 barracks_set_callsign_enter_mode(false);
1597                                 break;
1598
1599                         case SDLK_RETURN:
1600                                 barracks_accept_new_pilot_callsign();
1601                                 break;
1602                 } 
1603         } else {
1604                 // not entering pilot callsign
1605                 switch (k) {
1606                         case SDLK_RETURN:
1607                                 if (barracks_new_pilot_selected()) {
1608                                         gamesnd_play_iface(SND_GENERAL_FAIL);
1609                                 } else {
1610                                         gamesnd_play_iface(SND_USER_SELECT);
1611                                 }
1612                                 break;
1613
1614                         case SDLK_ESCAPE:  // cancel
1615                                 if (!help_overlay_active(BARRACKS_OVERLAY)) {
1616                                         if (Num_pilots && !barracks_pilot_accepted()) {
1617                                                 gameseq_post_event(GS_EVENT_MAIN_MENU);
1618                                         } else {
1619                                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1620                                         }
1621                                 } else {
1622                                         // kill the overlay
1623                                         help_overlay_set_state(BARRACKS_OVERLAY,0);
1624                                 }
1625                                 break;
1626
1627                         case SDLK_TAB:  // switch mode (simgle/multi)
1628 #if defined(DEMO) || defined(OEM_BUILD) // not for FS2_DEMO
1629         game_feature_not_in_demo_popup();
1630 #else
1631                                 if (Player_sel_mode == PLAYER_SELECT_MODE_SINGLE) {
1632                                         barracks_init_player_stuff(PLAYER_SELECT_MODE_MULTI);
1633                                 } else {
1634                                         barracks_init_player_stuff(PLAYER_SELECT_MODE_SINGLE);
1635                                 }
1636
1637                                 gamesnd_play_iface(SND_USER_SELECT);
1638 #endif
1639                                 break;
1640
1641                         case SDLK_F1:  // show help overlay
1642                                 gamesnd_play_iface(SND_HELP_PRESSED);
1643                                 break;
1644
1645                         case SDLK_F2:  // goto options screen
1646                                 gamesnd_play_iface(SND_SWITCH_SCREENS);
1647                                 gameseq_post_event(GS_EVENT_OPTIONS_MENU);
1648                                 break;
1649                 }       // end switch
1650
1651                 // process buttons
1652                 for (i=0; i<BARRACKS_NUM_BUTTONS; i++) {
1653                         if (Buttons[gr_screen.res][i].button.pressed()) {
1654                                 barracks_button_pressed(i);
1655                         }
1656                 }
1657
1658                 // if mouse is over a pilot, find index into Pilots array
1659                 if (List_region.is_mouse_on()) {
1660                         int y;
1661                         List_region.get_mouse_pos(NULL, &y);
1662                         int pilot_index = List_scroll_offset + (y / gr_get_font_height());
1663                         if ((pilot_index >= 0) && (pilot_index < Num_pilots)) {
1664                                 prospective_pilot = pilot_index;
1665                         }
1666                 }
1667                 
1668                 // if mouse clicked in list region, find index into Pilots array 
1669                 if (List_region.pressed()) {
1670                         if (prospective_pilot != -1) {
1671                                 Selected_line = prospective_pilot;
1672                                 gamesnd_play_iface(SND_USER_SELECT);
1673                         }
1674                 }
1675         }
1676
1677         // check mouse over help
1678         if (mouse_down(MOUSE_LEFT_BUTTON)) {
1679                 help_overlay_set_state(BARRACKS_OVERLAY, 0);
1680         }
1681
1682         // do pilot pic stuff
1683         if ((Pic_number >= 0) && (Pic_number < Num_pilot_images)) {
1684                 if (Pilot_images[Pic_number] == BARRACKS_IMAGE_NOT_LOADED) {  // haven't tried loading it yet
1685                         Pilot_images[Pic_number] = bm_load(Pilot_image_names[Pic_number]);
1686                         if (Pilot_images[Pic_number] >= 0) {
1687                                 int w, h;
1688
1689                                 bm_get_info(Pilot_images[Pic_number], &w, &h, NULL);
1690                                 // check for invalid pilot pic file
1691                                 if ((w != PLAYER_PILOT_PIC_W) || (h != PLAYER_PILOT_PIC_H)) {
1692                                         bm_unload(Pilot_images[Pic_number]);
1693                                         Pilot_images[Pic_number] = -1;
1694                                 }
1695                         }
1696                 }               
1697         } else {
1698                 Pic_number = -1;
1699         }
1700
1701         // do squad pic stuff
1702         if ((Pic_squad_number >= 0) && (Pic_squad_number < Num_pilot_squad_images)) {
1703                 if (Pilot_squad_images[Pic_squad_number] == BARRACKS_IMAGE_NOT_LOADED) {  // haven't tried loading it yet
1704                         Pilot_squad_images[Pic_squad_number] = bm_load_duplicate(Pilot_squad_image_names[Pic_squad_number]);
1705                         if (Pilot_squad_images[Pic_squad_number] >= 0) {
1706                                 int w, h;
1707
1708                                 bm_get_info(Pilot_squad_images[Pic_squad_number], &w, &h, NULL);
1709                                 // check for invalid pilot pic file
1710                                 if ((w != PLAYER_SQUAD_PIC_W) || (h != PLAYER_SQUAD_PIC_H)) {
1711                                         bm_unload(Pilot_squad_images[Pic_squad_number]);
1712                                         Pilot_squad_images[Pic_squad_number] = -1;
1713                                 }
1714                         }
1715                 }
1716         } else {
1717                 Pic_squad_number = -1;
1718         }
1719
1720         // draw the background, etc
1721         gr_reset_clip();        
1722         GR_MAYBE_CLEAR_RES(Background_bitmap);
1723         if (Background_bitmap >= 0) {
1724                 gr_set_bitmap(Background_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1725                 gr_bitmap(0, 0);        
1726         }               
1727
1728         // draw pilot image and clean up afterwards
1729         barracks_draw_pilot_pic();
1730         barracks_draw_squad_pic();
1731
1732 #if defined(MAKE_FS1) && !defined(FS1_DEMO)
1733         if (PilotWin01 != -1) {
1734                 gr_set_bitmap(PilotWin01, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1735                 gr_bitmap(447, 18);
1736         }
1737
1738         if (PilotWin02 != -1) {
1739                 gr_set_bitmap(PilotWin02, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1740                 gr_bitmap(620, 38);
1741         }
1742
1743         if (PilotWin03 != -1) {
1744                 gr_set_bitmap(PilotWin03, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1745                 gr_bitmap(447, 137);
1746         }
1747
1748         if (PilotWin04 != -1) {
1749                 gr_set_bitmap(PilotWin04, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1750                 gr_bitmap(447, 36);
1751         }
1752 #endif
1753
1754
1755         // draw the window      
1756         Ui_window.draw();       
1757
1758         // light up the correct mode button (single or multi)   
1759         if (Player_sel_mode == PLAYER_SELECT_MODE_SINGLE) {
1760                 Buttons[gr_screen.res][B_PILOT_SINGLE_MODE_BUTTON].button.draw_forced(2);
1761         } else {
1762                 Buttons[gr_screen.res][B_PILOT_MULTI_MODE_BUTTON].button.draw_forced(2);
1763         }       
1764
1765         // write out pilot call signs
1766         barracks_display_pilot_callsigns(prospective_pilot);
1767
1768         // write out current pilot stats
1769         barracks_display_pilot_stats();
1770
1771         // blit help overlay if active
1772         help_overlay_maybe_blit(BARRACKS_OVERLAY);      
1773         
1774         // flip the page
1775         gr_flip();
1776 }
1777
1778 // -----------------------------------------------------------------------------
1779 void barracks_close()
1780 {
1781 #if defined(MAKE_FS1) && !defined(FS1_DEMO)
1782     if (PilotWin01 != -1){
1783         bm_unload(PilotWin01);
1784         PilotWin01 = -1;
1785     }
1786     if (PilotWin02 != -1){
1787         bm_unload(PilotWin02);
1788         PilotWin02 = -1;
1789     }
1790     if (PilotWin03 != -1){
1791         bm_unload(PilotWin03);
1792         PilotWin03 = -1;
1793     }
1794     if (PilotWin04 != -1){
1795         bm_unload(PilotWin04);
1796         PilotWin04 = -1;
1797     }
1798 #endif
1799
1800         // destroy window
1801         Ui_window.destroy();
1802
1803         // release background bitmap
1804         if (Background_bitmap >= 0) {
1805                 bm_unload(Background_bitmap);
1806         }       
1807
1808         // release rank pip bitmaps
1809         for (int i=0; i<Rank_pips_count; i++) {
1810                 bm_release(Rank_pips_bitmaps + i);
1811         }       
1812
1813         // release pilot pic bitmaps
1814         for (int i=0; i<Num_pilot_images; i++) {
1815                 if (Pilot_images[i] >= 0) {
1816                         bm_unload(Pilot_images[i]);
1817                 }
1818         }
1819
1820         // unload the overlay bitmap
1821         help_overlay_unload(BARRACKS_OVERLAY);
1822
1823         game_flush();
1824 }
1825