]> icculus.org git repositories - taylor/freespace2.git/blob - src/menuui/barracks.cpp
use SDL_arraysize() instead of sizeof() where appropriate
[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 void barracks_init_stats(scoring_struct *stats)
452 {
453         int i;
454         float f;
455
456         Num_stat_lines = 0;
457
458         SDL_strlcpy(Stat_labels[Num_stat_lines], XSTR( "*All Time Stats", 50), STAT_COLUMN1_W);
459         Stats[Num_stat_lines][0] = 0;
460         Num_stat_lines++;
461
462         SDL_assert(Num_stat_lines < NUM_STAT_LINES);
463         Stat_labels[Num_stat_lines][0] = 0;
464         Stats[Num_stat_lines][0] = 0;
465         Num_stat_lines++;       
466
467         SDL_assert(Num_stat_lines < NUM_STAT_LINES);
468         SDL_strlcpy(Stat_labels[Num_stat_lines], XSTR( "Primary weapon shots:", 51), STAT_COLUMN1_W);
469         SDL_snprintf(Stats[Num_stat_lines], STAT_COLUMN2_W, "%d", stats->p_shots_fired);
470         Num_stat_lines++;
471
472         SDL_assert(Num_stat_lines < NUM_STAT_LINES);
473         SDL_strlcpy(Stat_labels[Num_stat_lines], XSTR( "Primary weapon hits:", 52), STAT_COLUMN1_W);
474         SDL_snprintf(Stats[Num_stat_lines], STAT_COLUMN2_W, "%d", stats->p_shots_hit);
475         Num_stat_lines++;
476
477         SDL_assert(Num_stat_lines < NUM_STAT_LINES);
478         SDL_strlcpy(Stat_labels[Num_stat_lines], XSTR( "Primary friendly hits:", 53), STAT_COLUMN1_W);
479         SDL_snprintf(Stats[Num_stat_lines], STAT_COLUMN2_W, "%d", stats->p_bonehead_hits);
480         Num_stat_lines++;
481
482         SDL_assert(Num_stat_lines < NUM_STAT_LINES);
483         SDL_strlcpy(Stat_labels[Num_stat_lines], XSTR( "Primary hit %:", 54), STAT_COLUMN1_W);
484         if (stats->p_shots_fired > 0) {
485                 f = (float) stats->p_shots_hit * 100.0f / (float) stats->p_shots_fired;
486         } else {
487                 f = 0.0f;
488         }
489         SDL_snprintf(Stats[Num_stat_lines], STAT_COLUMN2_W, XSTR( "%.1f%%", 55), f);
490         Num_stat_lines++;
491
492         SDL_assert(Num_stat_lines < NUM_STAT_LINES);
493         SDL_strlcpy(Stat_labels[Num_stat_lines], XSTR( "Primary friendly hit %:", 56), STAT_COLUMN1_W);
494         if (stats->p_bonehead_hits > 0) {
495                 f = (float) stats->p_bonehead_hits * 100.0f / (float) stats->p_shots_fired;
496         } else {
497                 f = 0.0f;
498         }
499         SDL_snprintf(Stats[Num_stat_lines], STAT_COLUMN2_W, XSTR( "%.1f%%", 55), f);
500         Num_stat_lines++;
501
502         SDL_assert(Num_stat_lines < NUM_STAT_LINES);
503         Stat_labels[Num_stat_lines][0] = 0;
504         Stats[Num_stat_lines][0] = 0;
505         Num_stat_lines++;
506
507         SDL_assert(Num_stat_lines < NUM_STAT_LINES);
508         SDL_strlcpy(Stat_labels[Num_stat_lines], XSTR( "Secondary weapon shots:", 57), STAT_COLUMN1_W);
509         SDL_snprintf(Stats[Num_stat_lines], STAT_COLUMN2_W, "%d", stats->s_shots_fired);
510         Num_stat_lines++;
511
512         SDL_assert(Num_stat_lines < NUM_STAT_LINES);
513         SDL_strlcpy(Stat_labels[Num_stat_lines], XSTR( "Secondary weapon hits:", 58), STAT_COLUMN1_W);
514         SDL_snprintf(Stats[Num_stat_lines], STAT_COLUMN2_W, "%d", stats->s_shots_hit);
515         Num_stat_lines++;
516
517         SDL_assert(Num_stat_lines < NUM_STAT_LINES);
518         SDL_strlcpy(Stat_labels[Num_stat_lines], XSTR( "Secondary friendly hits:", 59), STAT_COLUMN1_W);
519         SDL_snprintf(Stats[Num_stat_lines], STAT_COLUMN2_W, "%d", stats->s_bonehead_hits);
520         Num_stat_lines++;
521
522         SDL_assert(Num_stat_lines < NUM_STAT_LINES);
523         SDL_strlcpy(Stat_labels[Num_stat_lines], XSTR( "Secondary hit %:", 60), STAT_COLUMN1_W);
524         if (stats->s_shots_fired > 0) {
525                 f = (float) stats->s_shots_hit * 100.0f / (float) stats->s_shots_fired;
526         } else {
527                 f = 0.0f;
528         }
529         SDL_snprintf(Stats[Num_stat_lines], STAT_COLUMN2_W, XSTR( "%.1f%%", 55), f);
530         Num_stat_lines++;
531
532         SDL_assert(Num_stat_lines < NUM_STAT_LINES);
533         SDL_strlcpy(Stat_labels[Num_stat_lines], XSTR( "Secondary friendly hit %:", 61), STAT_COLUMN1_W);
534         if (stats->s_bonehead_hits > 0) {
535                 f = (float) stats->s_bonehead_hits * 100.0f / (float) stats->s_shots_fired;
536         } else {
537                 f = 0.0f;
538         }
539         SDL_snprintf(Stats[Num_stat_lines], STAT_COLUMN2_W, XSTR( "%.1f%%", 55), f);
540         Num_stat_lines++;
541
542         SDL_assert(Num_stat_lines < NUM_STAT_LINES);
543         Stat_labels[Num_stat_lines][0] = 0;
544         Stats[Num_stat_lines][0] = 0;
545         Num_stat_lines++;
546
547         SDL_assert(Num_stat_lines < NUM_STAT_LINES);
548         SDL_strlcpy(Stat_labels[Num_stat_lines], XSTR( "Total kills:", 62), STAT_COLUMN1_W);
549         SDL_snprintf(Stats[Num_stat_lines], STAT_COLUMN2_W, "%d", stats->kill_count_ok);
550         Num_stat_lines++;
551
552         SDL_assert(Num_stat_lines < NUM_STAT_LINES);
553         SDL_strlcpy(Stat_labels[Num_stat_lines], XSTR( "Assists:", 63), STAT_COLUMN1_W);
554         SDL_snprintf(Stats[Num_stat_lines], STAT_COLUMN2_W, "%d", stats->assists);
555         Num_stat_lines++;
556
557         SDL_assert(Num_stat_lines < NUM_STAT_LINES);
558         Stat_labels[Num_stat_lines][0] = 0;
559         Stats[Num_stat_lines][0] = 0;
560         Num_stat_lines++;
561
562         SDL_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         SDL_strlcpy(Stat_labels[Num_stat_lines], XSTR( "*Kills by Ship Type", 64), STAT_COLUMN1_W);
568         Stats[Num_stat_lines][0] = 0;
569         Num_stat_lines++;
570
571         SDL_assert(Num_stat_lines < NUM_STAT_LINES);
572         Stat_labels[Num_stat_lines][0] = 0;
573         Stats[Num_stat_lines][0] = 0;
574         Num_stat_lines++;
575
576         for (i=0; i<Num_ship_types; i++) {
577                 if (stats->kills[i]) {
578                         SDL_assert(Num_stat_lines < NUM_STAT_LINES);
579                         SDL_assert(strlen(Ship_info[i].name) + 1 < STAT_COLUMN1_W);
580                         SDL_snprintf(Stat_labels[Num_stat_lines], STAT_COLUMN1_W, NOX("%s:"), Ship_info[i].name);
581                         SDL_snprintf(Stats[Num_stat_lines], STAT_COLUMN2_W, "%d", stats->kills[i]);
582                         Num_stat_lines++;
583                 }
584         }
585
586         for (i=0; i<Num_stat_lines; i++) {
587                 gr_force_fit_string(Stat_labels[i], Stat_column1_w[gr_screen.res], Barracks_stats_coords[gr_screen.res][BARRACKS_W_COORD]);
588                 gr_force_fit_string(Stats[i], Stat_column2_w[gr_screen.res], Barracks_stats2_coords[gr_screen.res][BARRACKS_W_COORD]);
589         }
590 }
591
592 // sets or clears hotkeys for pilot selection.
593 void barracks_set_hotkeys(bool pilot_text_enter_mode)
594 {
595         Buttons[gr_screen.res][B_PILOT_SCROLL_UP_BUTTON].button.set_hotkey(pilot_text_enter_mode ? SDLK_UP : -1);
596         Buttons[gr_screen.res][B_PILOT_SCROLL_DOWN_BUTTON].button.set_hotkey(pilot_text_enter_mode ? SDLK_DOWN : -1);
597
598         Buttons[gr_screen.res][B_PILOT_CREATE_BOTTON].button.set_hotkey(pilot_text_enter_mode ? SDLK_c : -1);
599         Buttons[gr_screen.res][B_PILOT_SET_ACTIVE_BUTTON].button.set_hotkey(pilot_text_enter_mode ? SDLK_RETURN : -1);
600         Buttons[gr_screen.res][B_PILOT_DELETE_BUTTON].button.set_hotkey(pilot_text_enter_mode ? SDLK_DELETE : -1);
601
602         Buttons[gr_screen.res][B_PIC_PREV_PILOT_BUTTON].button.set_hotkey(pilot_text_enter_mode ? SDLK_LEFT : -1);
603         Buttons[gr_screen.res][B_PIC_NEXT_PILOT_BUTTON].button.set_hotkey(pilot_text_enter_mode ? SDLK_RIGHT : -1);
604
605         Buttons[gr_screen.res][B_ACCEPT_BUTTON].button.set_hotkey(pilot_text_enter_mode ? KEY_CTRLED | SDLK_RETURN : -1);
606 }
607
608 // strip the possible .pcx extension off a filename
609 void barracks_strip_pcx(char *str)
610 {
611         int flen = strlen(str);
612         int elen = 4;           
613         if ((flen > 4) && !SDL_strcasecmp(str + flen - elen, ".pcx")) {
614                 str[flen - elen] = '\0';
615         }
616 }
617
618 // new pilot name has focus, so update stats/pic to that pilot
619 int barracks_new_pilot_selected()
620 {
621         char stripped[MAX_FILENAME_LEN+1] = "";
622
623         // save the previous pilot first, so changes to it are kept
624         if (strlen(Cur_pilot->callsign)) {
625                 write_pilot_file();
626         }
627
628         // check if we have a valid pilot hilighted.  If so, attempt to active it
629         if ((Num_pilots < 1) || (Selected_line < 0) || (Selected_line >= Num_pilots)) {
630                 Cur_pilot->callsign[0] = 0;  // this indicates no pilot active
631                 return -1;
632         }
633
634         if (read_pilot_file(Pilots[Selected_line], !Player_sel_mode, Cur_pilot)) {
635                 Cur_pilot->callsign[0] = 0;  // this indicates no pilot active
636                 return -1;
637         }
638
639         // init stuff to reflect new pilot
640         int i;
641         barracks_init_stats(&Cur_pilot->stats);
642         for (i=0; i<Num_pilot_images; i++) {
643                 SDL_strlcpy(stripped, Cur_pilot->image_filename, SDL_arraysize(stripped));
644                 barracks_strip_pcx(stripped);
645                 if (!SDL_strcasecmp(stripped, Pilot_image_names[i])) {
646                         break;
647                 }
648         }
649         Pic_number = i;
650         for ( i=0; i<Num_pilot_squad_images; i++) {
651                 SDL_strlcpy(stripped, Cur_pilot->squad_filename, SDL_arraysize(stripped));
652                 barracks_strip_pcx(stripped);
653                 if (!SDL_strcasecmp(stripped, Pilot_squad_image_names[i])) {
654                         break;
655                 }
656         }
657         Pic_squad_number = i;   
658         
659         return 0;
660 }
661
662 void barracks_set_callsign_enter_mode(bool set_callsign_enter_mode)
663 {
664         // set global mode variable
665         Barracks_callsign_enter_mode = set_callsign_enter_mode;
666
667         // disable/enable all buttons
668         for (int idx=0; idx<BARRACKS_NUM_BUTTONS; idx++) {
669                 // don't ever mess with the prev and next squad logo buttons
670 #ifdef MAKE_FS1
671                 Buttons[gr_screen.res][idx].button.enable(!set_callsign_enter_mode);
672 #else
673                 if((idx != B_SQUAD_PREV_BUTTON) && (idx != B_SQUAD_NEXT_BUTTON)){
674                         Buttons[gr_screen.res][idx].button.enable(!set_callsign_enter_mode);
675                 }
676 #endif
677         }
678
679         // enable/disable hotkeys
680         barracks_set_hotkeys(!set_callsign_enter_mode);
681         // disable/enable inputbox
682         Inputbox.enable(set_callsign_enter_mode);
683         // hide/unhide inputbox
684         Inputbox.hide(!set_callsign_enter_mode);
685 }
686
687 // creates a new pilot file
688 void barracks_create_new_pilot()
689 {
690         // check if too many pilots
691         if (Num_pilots >= MAX_PILOTS) {
692                 gamesnd_play_iface(SND_GENERAL_FAIL);
693                 return;
694         }
695
696         // play sound for pilot creation
697         gamesnd_play_iface(SND_SCROLL);
698         
699         // only write pilot file if there is an active pilot
700         if (strlen(Player->callsign)) {
701                 write_pilot_file();
702         }
703
704         // move other pilot names and ranks down to make room for the new one
705         int idx = Num_pilots;
706         SDL_assert(Num_pilots >= 0);
707         while (idx--) {
708                 SDL_strlcpy(Pilots[idx + 1], Pilots[idx], MAX_FILENAME_LEN);
709                 Pilot_ranks[idx + 1] = Pilot_ranks[idx];
710         }
711
712         Selected_line = 0;
713         Num_pilots++;
714         Pilots[Selected_line][0] = 0;
715         Pilot_ranks[Selected_line] = 0;
716         List_scroll_offset = 0;
717
718         // set mode to accept pilot name text
719         barracks_set_callsign_enter_mode(true);
720         // set focus to input box
721         Inputbox.set_focus();
722         // set initial pilot name to ""
723         Inputbox.set_text("");
724         // reset size of input box to only 1 line
725         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());
726 }
727
728 // exiting screen without canceling, so load in the new pilot selected here
729 int barracks_pilot_accepted()
730 {
731         char str[CALLSIGN_LEN + 1];
732
733         // check if pilot active.  If not, don't allow accept.
734         if (!Cur_pilot->callsign[0]){
735                 return -1;
736         }
737
738         // set his image 
739         player_set_squad_bitmap(Cur_pilot, Cur_pilot->squad_filename);
740
741 //      Skill_level = get_default_skill_level();
742
743         // MWA -- I think that we should be writing Cur_pilot here.
744         //write_pilot_file(!is_pilot_multi(Cur_pilot));
745         write_pilot_file( Cur_pilot );
746         
747         // when we store the LastPlayer key, we have to mark it as being single or multiplayer, so we know where to look for him
748         // (since we could have a single and a multiplayer pilot with the same callsign)
749         // 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
750         SDL_strlcpy(str, Cur_pilot->callsign, SDL_arraysize(str));
751         SDL_strlcat(str, is_pilot_multi(Cur_pilot) ? NOX("M") : NOX("S"), SDL_arraysize(str));
752         os_config_write_string( NULL, "LastPlayer", str );
753         return 0;
754 }
755
756 // scroll up barracks pilot list one line
757 void barracks_scroll_callsign_up()
758 {
759         if (Selected_line > 0) {
760                 Selected_line--;
761                 gamesnd_play_iface(SND_SCROLL);
762         } else {
763                 gamesnd_play_iface(SND_GENERAL_FAIL);
764         }
765         
766         if ((Selected_line >= 0) && (Selected_line < List_scroll_offset)) {
767                 List_scroll_offset = Selected_line;
768         }
769 }
770
771 // scroll down barracks pilot list one line
772 void barracks_scroll_callsign_down()
773 {
774         if (Selected_line < Num_pilots - 1) {
775                 Selected_line++;
776                 gamesnd_play_iface(SND_SCROLL);
777         } else {
778                 gamesnd_play_iface(SND_GENERAL_FAIL);
779         }
780         
781         // num_pilots_to_fill_height is the number of pilots that can fit in given height
782         int num_pilots_to_fill_height = Barracks_list_coords[gr_screen.res][BARRACKS_H_COORD] / gr_get_font_height();
783         if (Selected_line >= List_scroll_offset + num_pilots_to_fill_height) {
784                 List_scroll_offset++;
785         }
786 }
787
788 // scroll up barracks stats list one line
789 void barracks_scroll_stats_up()
790 {
791         if (Stats_scroll_offset > 0) {
792                 Stats_scroll_offset--;
793                 gamesnd_play_iface(SND_SCROLL);
794         } else {
795                 gamesnd_play_iface(SND_GENERAL_FAIL);
796         }
797 }
798
799 // scroll down barracks stats list one line
800 void barracks_scroll_stats_down()
801 {
802         int font_height = gr_get_font_height();
803
804         if (Stats_scroll_offset + Barracks_stats_coords[gr_screen.res][BARRACKS_H_COORD] / font_height < Num_stat_lines) {
805                 Stats_scroll_offset++;
806                 gamesnd_play_iface(SND_SCROLL);
807         } else {
808                 gamesnd_play_iface(SND_GENERAL_FAIL);
809         }
810 }
811
812 // show previous pilot pic
813 void barracks_prev_pic()
814 {
815         // check if no pilot images or no pilot selected
816         if ((Num_pilot_images == 0) || (Cur_pilot->callsign[0] == '\0')) {
817                 gamesnd_play_iface(SND_GENERAL_FAIL);
818                 return;
819         }
820
821         // reset pilot pic number
822         Pic_number--;
823         if (Pic_number < 0) {
824                 Pic_number = Num_pilot_images - 1;
825         }
826
827         // copy pilot pic filename into pilot struct
828         if ((Pic_number >= 0) && (Pic_number < Num_pilot_images)) {
829                 SDL_strlcpy(Cur_pilot->image_filename, Pilot_image_names[Pic_number], SDL_arraysize(Cur_pilot->image_filename));
830         }
831
832         // play scroll sound
833         gamesnd_play_iface(SND_SCROLL);
834 }
835
836 // show next pilot pic
837 void barracks_next_pic()
838 {
839         // check if no pilot images or no pilot selected
840         if ((Num_pilot_images == 0) || (Cur_pilot->callsign[0] == '\0')) {
841                 gamesnd_play_iface(SND_GENERAL_FAIL);
842                 return;
843         }
844
845         // reset pilot pic number
846         Pic_number++;
847         if (Pic_number >= Num_pilot_images){
848                 Pic_number = 0;
849         }
850
851         // copy pilot pic filename into pilot struct
852         if ((Pic_number >= 0) && (Pic_number < Num_pilot_images)){
853                 SDL_strlcpy(Cur_pilot->image_filename, Pilot_image_names[Pic_number], SDL_arraysize(Cur_pilot->image_filename));
854         }
855
856         // play scroll sound
857         gamesnd_play_iface(SND_SCROLL);
858 }
859
860 // show previous squad pic
861 void barracks_prev_squad_pic()
862 {
863         // check if no pilot images or no pilot selected
864         if ((Num_pilot_squad_images == 0) || (Cur_pilot->callsign[0] == '\0')) {
865                 gamesnd_play_iface(SND_GENERAL_FAIL);
866                 return;
867         }
868
869         // reset pilot pic number
870         Pic_squad_number--;
871         if (Pic_squad_number < 0) {
872                 Pic_squad_number = Num_pilot_squad_images - 1;
873         }
874
875         // copy pilot pic filename into pilot struct
876         if ((Pic_squad_number >= 0) && (Pic_squad_number < Num_pilot_squad_images)) {
877                 SDL_strlcpy(Cur_pilot->squad_filename, Pilot_squad_image_names[Pic_squad_number], SDL_arraysize(Cur_pilot->squad_filename));
878         }
879
880         // play scroll sound
881         gamesnd_play_iface(SND_SCROLL);
882 }
883
884 // show next pilot pic
885 void barracks_next_squad_pic()
886 {
887         // check if no pilot images or no pilot selected
888         if ((Num_pilot_squad_images == 0) || (Cur_pilot->callsign[0] == '\0')) {
889                 gamesnd_play_iface(SND_GENERAL_FAIL);
890                 return;
891         }
892
893         // reset pilot pic number
894         Pic_squad_number++;
895         if (Pic_squad_number >= Num_pilot_squad_images){
896                 Pic_squad_number = 0;
897         }
898
899         // copy pilot pic filename into pilot struct
900         if ((Pic_squad_number >= 0) && (Pic_squad_number < Num_pilot_squad_images)){
901                 SDL_strlcpy(Cur_pilot->squad_filename, Pilot_squad_image_names[Pic_squad_number], SDL_arraysize(Cur_pilot->squad_filename));
902         }
903
904         // play scroll sound
905         gamesnd_play_iface(SND_SCROLL);
906 }
907
908 void barracks_delete_pilot()
909 {
910         char buf[MAX_FILENAME_LEN];
911         int active = 0;
912
913         if (!Num_pilots) {
914                 gamesnd_play_iface(SND_GENERAL_FAIL);
915                 return;
916         }
917
918         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));
919         if (popup_rval != 1) {
920                 return;
921         }
922
923         if (!SDL_strcasecmp(Pilots[Selected_line], Cur_pilot->callsign)) {
924                 active = 1;
925         }
926
927         SDL_strlcpy(buf, Pilots[Selected_line], SDL_arraysize(buf));
928         for (int i=Selected_line; i<Num_pilots-1; i++) {
929                 SDL_strlcpy(Pilots[i], Pilots[i + 1], MAX_FILENAME_LEN);
930                 Pilot_ranks[i] = Pilot_ranks[i + 1];
931         }
932
933         Num_pilots--;
934         if (Selected_line >= Num_pilots) {
935                 Selected_line = Num_pilots - 1;
936         }
937
938         if (active) {
939                 if (Selected_line >= 0) {
940                         barracks_new_pilot_selected();
941                 } else {
942                         Cur_pilot->callsign[0] = 0;
943                 }
944         }
945
946         delete_pilot_file(buf, Player_sel_mode == PLAYER_SELECT_MODE_SINGLE ? 1 : 0);
947         gamesnd_play_iface(SND_USER_SELECT);
948 }
949
950 // Filter out pilots of wrong type (which shouldn't be in the directory we are checking, but just to be safe..)
951 int barracks_pilot_filter(const char *filename)
952 {
953         int r, rank;
954
955         r = verify_pilot_file(filename, Player_sel_mode == PLAYER_SELECT_MODE_SINGLE, &rank);
956         if (rank >= Rank_pips_count)
957                 rank = Rank_pips_count - 1;
958
959         if (!r) {
960                 Pilot_ranks[Num_pilots++] = rank;
961         }
962
963         return !r;
964 }
965
966 // callback handler for the squadon selection buttons when they are disabled (in single player)
967 void barracks_squad_change_popup()
968 {
969         // show a popup
970         popup( PF_USE_AFFIRMATIVE_ICON | PF_NO_NETWORKING, 1, POPUP_OK, XSTR("You cannot change your squadron in Single Player mode.", 1445));
971 }
972
973
974 void barracks_init_player_stuff(int mode)
975 {
976         // determine if we should be looking for single or multiplayers at the outset
977         Player_sel_mode = mode;
978         
979         // get the list of pilots based upon whether we're in single or multiplayer mode
980         Num_pilots = 0;
981         Get_file_list_filter = barracks_pilot_filter;
982
983         // single player specific stuff
984         if (mode == PLAYER_SELECT_MODE_SINGLE) {
985                 Num_pilots = cf_get_file_list_preallocated(MAX_PILOTS, Pilots_arr, Pilots, CF_TYPE_SINGLE_PLAYERS, NOX("*.plr"), CF_SORT_TIME);
986
987 #ifndef MAKE_FS1
988                 // disable squad logo switching         
989                 Buttons[gr_screen.res][B_SQUAD_PREV_BUTTON].button.hide();
990                 Buttons[gr_screen.res][B_SQUAD_PREV_BUTTON].button.disable();
991                 Buttons[gr_screen.res][B_SQUAD_PREV_BUTTON].button.set_disabled_action(barracks_squad_change_popup);
992                 Buttons[gr_screen.res][B_SQUAD_NEXT_BUTTON].button.hide();
993                 Buttons[gr_screen.res][B_SQUAD_NEXT_BUTTON].button.disable();                   
994                 Buttons[gr_screen.res][B_SQUAD_NEXT_BUTTON].button.set_disabled_action(barracks_squad_change_popup);
995 #endif
996         }
997         // multiplayer specific stuff
998         else {
999                 Num_pilots = cf_get_file_list_preallocated(MAX_PILOTS, Pilots_arr, Pilots, CF_TYPE_MULTI_PLAYERS, NOX("*.plr"), CF_SORT_TIME);
1000
1001 #ifndef MAKE_FS1
1002                 // enable squad logo switching
1003                 Buttons[gr_screen.res][B_SQUAD_PREV_BUTTON].button.enable();
1004                 Buttons[gr_screen.res][B_SQUAD_PREV_BUTTON].button.unhide();
1005                 Buttons[gr_screen.res][B_SQUAD_NEXT_BUTTON].button.enable();
1006                 Buttons[gr_screen.res][B_SQUAD_NEXT_BUTTON].button.unhide();                    
1007 #endif
1008         }
1009
1010         int ranks[MAX_PILOTS];
1011
1012         for (int i=0; i<Num_pilots; i++) {
1013                 int j;
1014                 for (j=0; j<Num_pilots; j++) {
1015                         if (!strcmp(Pilots[i], Pilots_arr[j])) {
1016                                 ranks[i] = Pilot_ranks[j];
1017                                 break;
1018                         }
1019                 }
1020                 SDL_assert(j < Num_pilots);  // Pilot not found?  How is that possible?
1021         }
1022
1023         for (int i=0; i<Num_pilots; i++) {
1024                 Pilot_ranks[i] = ranks[i];
1025         }
1026
1027         Selected_line = List_scroll_offset = 0;
1028         barracks_new_pilot_selected();
1029
1030 }
1031
1032 void barracks_button_pressed(int n)
1033 {
1034         switch (n) {
1035                 case B_PILOT_SCROLL_UP_BUTTON:
1036                         barracks_scroll_callsign_up();
1037                         break;
1038
1039                 case B_PILOT_SCROLL_DOWN_BUTTON:
1040                         barracks_scroll_callsign_down();
1041                         break;
1042
1043                 case B_STATS_SCROLL_UP_BUTTON:
1044                         barracks_scroll_stats_up();
1045                         break;
1046
1047                 case B_STATS_SCROLL_DOWN_BUTTON:
1048                         barracks_scroll_stats_down();
1049                         break;
1050
1051                 case B_PIC_PREV_PILOT_BUTTON:
1052                         barracks_prev_pic();
1053                         break;
1054
1055                 case B_PIC_NEXT_PILOT_BUTTON:
1056                         barracks_next_pic();
1057                         break;
1058
1059 #ifndef MAKE_FS1
1060                 case B_SQUAD_PREV_BUTTON:
1061                         barracks_prev_squad_pic();
1062                         break;
1063
1064                 case B_SQUAD_NEXT_BUTTON:
1065                         barracks_next_squad_pic();
1066                         break;
1067 #endif
1068
1069                 case B_PILOT_SET_ACTIVE_BUTTON:
1070                         if (barracks_new_pilot_selected()){
1071                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1072                                 
1073                                 // throw up a popup telling the player that he should create a pilot first
1074                                 if(Player_sel_mode == PLAYER_SELECT_MODE_SINGLE){
1075                                         popup(PF_USE_AFFIRMATIVE_ICON,1,POPUP_OK,XSTR( "You must create a single player pilot.", 66));
1076                                 } else {
1077                                         popup(PF_USE_AFFIRMATIVE_ICON,1,POPUP_OK,XSTR( "You must create a multi player pilot.", 67));
1078                                 }
1079                         } else {
1080                                 gamesnd_play_iface(SND_SCROLL);
1081                         }
1082                         break;
1083
1084                 case B_ACCEPT_BUTTON:                   
1085                         if (Num_pilots && !barracks_pilot_accepted()) {
1086                                 gamesnd_play_iface(SND_COMMIT_PRESSED);
1087                                 gameseq_post_event(GS_EVENT_MAIN_MENU);
1088                         } else {
1089                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1090
1091                                 // throw up a popup telling the player that he should create a pilot first
1092                                 if(Player_sel_mode == PLAYER_SELECT_MODE_SINGLE){
1093                                         popup(PF_USE_AFFIRMATIVE_ICON,1,POPUP_OK,XSTR( "You must create a single player pilot.", 66));
1094                                 } else {
1095                                         popup(PF_USE_AFFIRMATIVE_ICON,1,POPUP_OK,XSTR( "You must create a multi player pilot.", 67));
1096                                 }
1097                         }
1098                         break;
1099
1100                 case B_PILOT_CLONE_BUTTON:
1101                         if (Num_pilots < 1) {
1102                                 gamesnd_play_error_beep();
1103                                 break;
1104                         }
1105
1106                         Clone_flag = 1;
1107                         barracks_create_new_pilot();
1108                         break;
1109
1110                 case B_PILOT_CONVERT_BUTTON: {
1111 #if defined(DEMO) || defined(OEM_BUILD)
1112                         game_feature_not_in_demo_popup();
1113 #else
1114                         const char *str;
1115                         char temp[256];
1116                         char old_pic[256] = "";
1117                         char old_squad_pic[256] = "";
1118                         char old_squad[256] = "";
1119                         int z;
1120
1121                         if (!barracks_new_pilot_selected()) {
1122                                 if (Player_sel_mode == PLAYER_SELECT_MODE_SINGLE)
1123                                         str = XSTR( "multiplayer", 68);
1124                                 else
1125                                         str = XSTR( "single player", 69);
1126
1127                                 SDL_snprintf(temp, SDL_arraysize(temp), XSTR( "This will overwrite your %s pilot.  Proceed?", 70), str);
1128                                 if (!verify_pilot_file(Cur_pilot->callsign, Player_sel_mode == PLAYER_SELECT_MODE_MULTI)) {
1129                                         z = popup(0, 2, POPUP_CANCEL, POPUP_OK, temp);
1130                                         if (z != 1)
1131                                                 break;
1132                                 }
1133
1134                                 SDL_strlcpy(old_pic, Cur_pilot->image_filename, SDL_arraysize(old_pic));
1135                                 SDL_strlcpy(old_squad_pic, Cur_pilot->squad_filename, SDL_arraysize(old_squad_pic));
1136                                 SDL_strlcpy(old_squad, Cur_pilot->squad_name, SDL_arraysize(old_squad));
1137                                 init_new_pilot(Cur_pilot, 0);
1138                                 SDL_strlcpy(Cur_pilot->image_filename, old_pic, SDL_arraysize(Cur_pilot->image_filename));
1139                                 SDL_strlcpy(Cur_pilot->squad_filename, old_squad_pic, SDL_arraysize(Cur_pilot->squad_filename));
1140                                 SDL_strlcpy(Cur_pilot->squad_name, old_squad, SDL_arraysize(Cur_pilot->squad_name));
1141                                 if (Player_sel_mode == PLAYER_SELECT_MODE_SINGLE) {
1142                                         Cur_pilot->flags |= PLAYER_FLAGS_IS_MULTI;
1143                                         write_pilot_file();
1144                                         barracks_init_player_stuff(PLAYER_SELECT_MODE_MULTI);
1145
1146                                 } else {
1147                                         write_pilot_file();
1148                                         barracks_init_player_stuff(PLAYER_SELECT_MODE_SINGLE);
1149                                 }
1150
1151                                 gamesnd_play_iface(SND_USER_SELECT);
1152
1153                         } else {
1154                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1155                         }
1156 #endif
1157                         break;
1158                 }
1159
1160                 case B_PILOT_CREATE_BOTTON:
1161                         Clone_flag = 0;
1162                         barracks_create_new_pilot();
1163                         break;
1164
1165                 case B_HELP_BUTTON:
1166                         launch_context_help();
1167                         gamesnd_play_iface(SND_HELP_PRESSED);
1168                         break;
1169
1170                 case B_OPTION_BUTTON:
1171                         gamesnd_play_iface(SND_SWITCH_SCREENS);
1172                         gameseq_post_event(GS_EVENT_OPTIONS_MENU);
1173                         break;
1174
1175                 case B_STATS_MEDAL_BUTTON:
1176 #if defined(FS2_DEMO) || defined(FS1_DEMO)
1177                         game_feature_not_in_demo_popup();
1178 #else
1179                         gamesnd_play_iface(SND_SWITCH_SCREENS);
1180                         gameseq_post_event(GS_EVENT_VIEW_MEDALS);
1181 #endif
1182                         break;
1183
1184                 case B_PILOT_DELETE_BUTTON:
1185                         barracks_delete_pilot();
1186                         break;
1187
1188                 case B_PILOT_SINGLE_MODE_BUTTON:
1189                         if (Player_sel_mode != PLAYER_SELECT_MODE_SINGLE) {
1190                                 gamesnd_play_iface(SND_USER_SELECT);
1191                                 barracks_init_player_stuff(PLAYER_SELECT_MODE_SINGLE);
1192                         }                                               
1193                         break;
1194
1195                 case B_PILOT_MULTI_MODE_BUTTON:
1196 #if defined(DEMO) || defined(OEM_BUILD) // not for FS2_DEMO
1197                         game_feature_not_in_demo_popup();
1198 #else
1199                         if (Player_sel_mode != PLAYER_SELECT_MODE_MULTI) {
1200                                 gamesnd_play_iface(SND_USER_SELECT);
1201                                 barracks_init_player_stuff(PLAYER_SELECT_MODE_MULTI);
1202                         }
1203 #endif
1204                         break;
1205         }
1206 }
1207
1208
1209 void barracks_display_pilot_callsigns(int prospective_pilot)
1210 {
1211         int y = 0;
1212         int cur_pilot_idx = List_scroll_offset;
1213
1214         int multi = 0;
1215         if (Player_sel_mode == PLAYER_SELECT_MODE_MULTI) {
1216                 multi = 1;
1217         }
1218
1219         int font_height = gr_get_font_height();
1220         while (y + font_height <= Barracks_list_coords[gr_screen.res][BARRACKS_H_COORD]) {
1221                 if (cur_pilot_idx >= Num_pilots)
1222                         break;
1223
1224                 if (!SDL_strcasecmp(Cur_pilot->callsign, Pilots[cur_pilot_idx]) && (is_pilot_multi(Cur_pilot) == multi)) {
1225                         if ((cur_pilot_idx == Selected_line) || (cur_pilot_idx == prospective_pilot)) {
1226                                 gr_set_color_fast(&Color_text_active_hi);
1227                         } else {
1228                                 gr_set_color_fast(&Color_text_active);
1229                         }
1230                 } else {
1231                         if (cur_pilot_idx == Selected_line) {
1232                                 gr_set_color_fast(&Color_text_selected);
1233                         } else if (cur_pilot_idx == prospective_pilot) {
1234                                 gr_set_color_fast(&Color_text_subselected);
1235                         } else {
1236                                 gr_set_color_fast(&Color_text_normal);
1237                         }
1238                 }
1239
1240                 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]);
1241 #ifndef FS1_DEMO
1242                 gr_set_bitmap(Rank_pips_bitmaps + Pilot_ranks[cur_pilot_idx], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1243                 gr_bitmap(Barracks_list_coords[gr_screen.res][BARRACKS_X_COORD] - 34, Barracks_list_coords[gr_screen.res][BARRACKS_Y_COORD] + y);
1244 #endif
1245  
1246                 y += font_height;
1247                 cur_pilot_idx++;
1248         }
1249 }
1250
1251
1252
1253 void barracks_display_pilot_stats()
1254 {
1255         int y = 0;
1256         int z = Stats_scroll_offset;
1257         int font_height = gr_get_font_height();
1258         char *str;
1259         int i, w, h;
1260         while (y + font_height <= Barracks_stats_coords[gr_screen.res][BARRACKS_H_COORD]) {
1261                 if (z >= Num_stat_lines) {
1262                         break;
1263                 }
1264
1265                 str = Stat_labels[z];
1266                 if (*str == '*') {
1267                         gr_set_color_fast(&Color_text_heading);
1268                         str++;
1269
1270                         gr_get_string_size(&w, &h, str);
1271                         i = Barracks_stats_coords[gr_screen.res][BARRACKS_Y_COORD] + y + h / 2 - 1;                     
1272                         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);
1273                         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);
1274
1275                 } else {
1276                         gr_set_color_fast(&Color_text_normal);
1277                 }
1278
1279                 gr_get_string_size(&w, NULL, str);
1280                 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);
1281                 str = Stats[z];
1282                 if (*str) {
1283                         gr_printf(Barracks_stats2_coords[gr_screen.res][BARRACKS_X_COORD], Barracks_stats_coords[gr_screen.res][BARRACKS_Y_COORD] + y, "%s", str);
1284                 }
1285
1286                 y += font_height;
1287                 z++;
1288         }
1289 }
1290
1291
1292 // process pilot callsign
1293 void barracks_accept_new_pilot_callsign()
1294 {
1295         char buf[CALLSIGN_LEN + 1];
1296         char name[MAX_FILENAME_LEN];
1297         int i;
1298
1299         int z = 0;
1300         Inputbox.get_text(buf);
1301         drop_white_space(buf);
1302
1303         if (!isalpha(*buf)) {
1304                 z = 1;
1305         } else {
1306                 for (i=1; buf[i]; i++) {
1307                         if (!isalpha(buf[i]) && !isdigit(buf[i]) && !SDL_strchr(VALID_PILOT_CHARS, buf[i])) {
1308                                 return;
1309                         }
1310                 }
1311         }
1312
1313         for (i=1; i<Num_pilots; i++) {
1314                 if (!SDL_strcasecmp(buf, Pilots[i])) {
1315                         if (pilot_verify_overwrite() == 1) {
1316                                 SDL_strlcpy(name, Pilots[Selected_line], SDL_arraysize(name));
1317                                 for (z=i; z<Num_pilots-1; z++) {
1318                                         SDL_strlcpy(Pilots[z], Pilots[z + 1], MAX_FILENAME_LEN);
1319                                         Pilot_ranks[z] = Pilot_ranks[z + 1];
1320                                 }
1321
1322                                 Num_pilots--;
1323                                 delete_pilot_file(name, Player_sel_mode == PLAYER_SELECT_MODE_SINGLE ? 1 : 0);
1324                         }
1325                         return;
1326                 }
1327         }
1328
1329         if (!*buf || (i < Num_pilots)) { // duplicate name, alert user
1330                 z = 1;
1331         }
1332
1333         if (z) {
1334                 gamesnd_play_iface(SND_GENERAL_FAIL);
1335                 return;
1336         }
1337
1338         SDL_strlcpy(Pilots[0], buf, MAX_FILENAME_LEN);
1339         SDL_strlcpy(Cur_pilot->callsign, buf, SDL_arraysize(Cur_pilot->callsign));
1340         init_new_pilot(Cur_pilot, !Clone_flag);
1341         
1342         // again, make sure we set his flags correctly to ensure that he gets saved to the proper directory and gets
1343         // displayed correctly
1344         if (Player_sel_mode == PLAYER_SELECT_MODE_SINGLE) {
1345                 Cur_pilot->flags &= ~(PLAYER_FLAGS_IS_MULTI);
1346         } else {
1347                 Cur_pilot->flags |= PLAYER_FLAGS_IS_MULTI;
1348                 Cur_pilot->stats.flags |= STATS_FLAG_MULTIPLAYER;
1349         }
1350
1351         if ( !(Game_mode & GM_STANDALONE_SERVER) ) {
1352                 write_pilot_file(Cur_pilot);
1353         }
1354
1355         Selected_line = 0;
1356         barracks_new_pilot_selected();
1357         barracks_set_callsign_enter_mode(false);
1358 }
1359
1360
1361 // draw pilot image and clean up afterwards
1362 void barracks_draw_pilot_pic()
1363 {
1364         // draw pilot pic
1365         if (Cur_pilot->callsign[0] && (Pic_number >= 0) && (Pic_number < Num_pilot_images)) {
1366                 if (Pilot_images[Pic_number] >= 0) {
1367                         // JAS: This code is hacked to allow the animation to use all 256 colors
1368                         extern int Palman_allow_any_color;
1369                         Palman_allow_any_color = 1;
1370                         gr_set_bitmap(Pilot_images[Pic_number], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1371                         gr_bitmap(Barracks_image_coords[gr_screen.res][BARRACKS_X_COORD], Barracks_image_coords[gr_screen.res][BARRACKS_Y_COORD]);
1372                         Palman_allow_any_color = 0;
1373
1374                         // print number of the current pic
1375                         char buf[40];                   
1376                         SDL_snprintf(buf, SDL_arraysize(buf), XSTR( "%d of %d", 71), Pic_number + 1, Num_pilot_images);
1377                         gr_printf(Barracks_image_number_coords[gr_screen.res][BARRACKS_X_COORD], Barracks_image_number_coords[gr_screen.res][BARRACKS_Y_COORD], buf);                           
1378                 }
1379         } else {
1380                 Pic_number = -1;
1381         }
1382 }
1383
1384 // draw squad image and clean up afterwards
1385 void barracks_draw_squad_pic()
1386 {
1387         char buf[40];
1388
1389         // draw pilot pic
1390         if (Cur_pilot->callsign[0] && (Pic_squad_number >= 0) && (Pic_squad_number < Num_pilot_squad_images)) {
1391                 if (Pilot_squad_images[Pic_squad_number] >= 0) {
1392                         // JAS: This code is hacked to allow the animation to use all 256 colors
1393                         extern int Palman_allow_any_color;
1394                         Palman_allow_any_color = 1;
1395                         gr_set_bitmap(Pilot_squad_images[Pic_squad_number], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1396                         gr_bitmap(Barracks_squad_coords[gr_screen.res][BARRACKS_X_COORD], Barracks_squad_coords[gr_screen.res][BARRACKS_Y_COORD]);
1397                         Palman_allow_any_color = 0;
1398
1399                         // print number of current squad pic
1400                         if(Player_sel_mode != PLAYER_SELECT_MODE_SINGLE){
1401                                 SDL_snprintf(buf, SDL_arraysize(buf), XSTR( "%d of %d", 71), Pic_squad_number+1, Num_pilot_squad_images);
1402                                 gr_printf(Barracks_squad_number_coords[gr_screen.res][BARRACKS_X_COORD], Barracks_squad_number_coords[gr_screen.res][BARRACKS_Y_COORD], buf);
1403                         }
1404                 }
1405         } else {
1406                 Pic_squad_number = -1;
1407         }       
1408 }
1409
1410 // -----------------------------------------------------------------------------
1411 void barracks_init()
1412 {
1413 #ifndef MAKE_FS1
1414         UI_WINDOW *w = &Ui_window;
1415 #endif
1416
1417         // save current pilot file, so we don't possibly loose it.
1418         write_pilot_file();
1419
1420         // create interface
1421         Ui_window.create(0, 0, gr_screen.max_w, gr_screen.max_h, 0);
1422         Ui_window.set_mask_bmap(Barracks_bitmap_mask_fname[gr_screen.res]);
1423
1424         // load background bitmap
1425         Background_bitmap = bm_load(Barracks_bitmap_fname[gr_screen.res]);
1426         if(Background_bitmap < 0){
1427                 // we failed to load the bitmap - this is very bad
1428                 Int3();
1429         }
1430
1431         // create buttons
1432         for (int i=0; i<BARRACKS_NUM_BUTTONS; i++) {
1433                 // create the object
1434                 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);
1435
1436                 // set the sound to play when highlighted
1437                 Buttons[gr_screen.res][i].button.set_highlight_action(common_play_highlight_sound);
1438
1439                 // set the ani for the button
1440                 Buttons[gr_screen.res][i].button.set_bmaps(Buttons[gr_screen.res][i].filename);
1441
1442                 // set the hotspot
1443                 Buttons[gr_screen.res][i].button.link_hotspot(Buttons[gr_screen.res][i].hotspot);
1444         }
1445
1446         // add all strings      
1447 #ifndef MAKE_FS1
1448         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);
1449         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);
1450         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);
1451         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);
1452         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);
1453         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);
1454         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);
1455         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);
1456         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);
1457         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);
1458         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);    
1459         for(int i=0; i<BARRACKS_NUM_TEXT; i++) {
1460                 w->add_XSTR(&Barracks_text[gr_screen.res][i]);
1461         }
1462 #endif
1463
1464         // button for selecting pilot
1465         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);
1466         List_region.hide();
1467
1468         // create input box (for new pilot)
1469         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);
1470         Inputbox.set_valid_chars(VALID_PILOT_CHARS);
1471         Inputbox.disable();
1472         Inputbox.hide();
1473
1474         // load in help overlay bitmap  
1475         help_overlay_load(BARRACKS_OVERLAY);
1476         help_overlay_set_state(BARRACKS_OVERLAY,0);     
1477
1478         // other init stuff
1479         Barracks_callsign_enter_mode = 0;       
1480         List_scroll_offset = Stats_scroll_offset = Pic_number = Pic_squad_number = Selected_line = 0;
1481         Cur_pilot = &Players[Player_num];
1482
1483         // disable squad logo selection buttons in single player
1484 #ifndef MAKE_FS1
1485         if(!(Cur_pilot->flags & PLAYER_FLAGS_IS_MULTI)){
1486                 // squad logo picture buttons           
1487                 Buttons[gr_screen.res][B_SQUAD_PREV_BUTTON].button.hide();
1488                 Buttons[gr_screen.res][B_SQUAD_PREV_BUTTON].button.disable();
1489                 Buttons[gr_screen.res][B_SQUAD_NEXT_BUTTON].button.hide();
1490                 Buttons[gr_screen.res][B_SQUAD_NEXT_BUTTON].button.disable();
1491         } else {
1492                 // squad logo picture buttons           
1493                 Buttons[gr_screen.res][B_SQUAD_PREV_BUTTON].button.enable();
1494                 Buttons[gr_screen.res][B_SQUAD_PREV_BUTTON].button.unhide();
1495                 Buttons[gr_screen.res][B_SQUAD_NEXT_BUTTON].button.enable();
1496                 Buttons[gr_screen.res][B_SQUAD_NEXT_BUTTON].button.unhide();            
1497         }
1498 #endif
1499
1500         // set up hotkeys for buttons so we draw the correct animation frame when a key is pressed
1501         barracks_set_hotkeys(1);
1502
1503         // load ramp pips
1504 #ifndef FS1_DEMO
1505         Rank_pips_bitmaps = bm_load_animation("IconRankMini.ani", &Rank_pips_count);    
1506 #endif
1507
1508         // load up the pilot pic list
1509         pilot_load_pic_list();  
1510         pilot_load_squad_pic_list();
1511         
1512         // don't load pilot images yet
1513         for (int i=0; i<MAX_PILOT_IMAGES; i++) {
1514                 Pilot_images[i] = BARRACKS_IMAGE_NOT_LOADED;  // while -1 is can't load
1515                 Pilot_squad_images[i] = BARRACKS_IMAGE_NOT_LOADED;
1516         }
1517         
1518         // init stats
1519         barracks_init_stats(&Cur_pilot->stats);
1520
1521         // disable some buttons for the multiplayer beta and e3 build
1522 #if defined(MULTIPLAYER_BETA_BUILD) || defined(E3_BUILD) || defined(PRESS_TOUR_BUILD)
1523         Buttons[gr_screen.res][B_PILOT_CLONE_BUTTON].button.hide();
1524         Buttons[gr_screen.res][B_PILOT_CONVERT_BUTTON].button.hide();   
1525         Buttons[gr_screen.res][B_PILOT_CLONE_BUTTON].button.disable();  
1526         Buttons[gr_screen.res][B_PILOT_CONVERT_BUTTON].button.disable();        
1527 #endif
1528
1529         // multiplayer beta build
1530 #ifdef MULTIPLAYER_BETA_BUILD
1531         Buttons[gr_screen.res][B_PILOT_SINGLE_MODE_BUTTON].button.hide();
1532         Buttons[gr_screen.res][B_PILOT_SINGLE_MODE_BUTTON].button.disable();
1533 #endif
1534
1535         // e3 build
1536 #if defined(E3_BUILD) || defined(PRESS_TOUR_BUILD)
1537         Buttons[gr_screen.res][B_PILOT_MULTI_MODE_BUTTON].button.hide();
1538         Buttons[gr_screen.res][B_PILOT_MULTI_MODE_BUTTON].button.disable();
1539 #endif
1540
1541         // base the mode we're in (single or multi) on the status of the currently selected pilot
1542 #ifdef MULTIPLAYER_BETA_BUILD
1543         barracks_init_player_stuff(1);
1544 #elif defined(E3_BUILD) || defined(PRESS_TOUR_BUILD)
1545         barracks_init_player_stuff(0);
1546 #else
1547         barracks_init_player_stuff(is_pilot_multi(Player));     
1548 #endif
1549
1550 #if defined(MAKE_FS1) && !defined(FS1_DEMO)
1551         PilotWin01 = bm_load(NOX("PilotWin01"));
1552         PilotWin02 = bm_load(NOX("PilotWin02"));
1553         PilotWin03 = bm_load(NOX("PilotWin03"));
1554         PilotWin04 = bm_load(NOX("PilotWin04"));
1555 #endif
1556 }
1557
1558 // -----------------------------------------------------------------------------
1559 void barracks_do_frame(float frametime)
1560 {
1561         int k = Ui_window.process();
1562
1563         if ( k > 0 ) {
1564                 if ( help_overlay_active(BARRACKS_OVERLAY) ) {
1565                         help_overlay_set_state(BARRACKS_OVERLAY,0);
1566                         k = 0;
1567                 }
1568         }
1569
1570         // pilot that mouse is over
1571         int prospective_pilot = -1;
1572         int i;
1573
1574         // Entering pilot callsign
1575         if (Barracks_callsign_enter_mode) {
1576                 // set focus to inputbox
1577                 Inputbox.set_focus();
1578
1579                 switch (k) {
1580                         case SDLK_ESCAPE:
1581                                 // cancel create pilot
1582                                 Num_pilots--;
1583                                 for (i=0; i<Num_pilots; i++) {
1584                                         SDL_strlcpy(Pilots[i], Pilots[i + 1], MAX_FILENAME_LEN);
1585                                         Pilot_ranks[i] = Pilot_ranks[i + 1];
1586                                 }
1587
1588                                 barracks_set_callsign_enter_mode(false);
1589                                 break;
1590
1591                         case SDLK_RETURN:
1592                                 barracks_accept_new_pilot_callsign();
1593                                 break;
1594                 } 
1595         } else {
1596                 // not entering pilot callsign
1597                 switch (k) {
1598                         case SDLK_RETURN:
1599                                 if (barracks_new_pilot_selected()) {
1600                                         gamesnd_play_iface(SND_GENERAL_FAIL);
1601                                 } else {
1602                                         gamesnd_play_iface(SND_USER_SELECT);
1603                                 }
1604                                 break;
1605
1606                         case SDLK_ESCAPE:  // cancel
1607                                 if (!help_overlay_active(BARRACKS_OVERLAY)) {
1608                                         if (Num_pilots && !barracks_pilot_accepted()) {
1609                                                 gameseq_post_event(GS_EVENT_MAIN_MENU);
1610                                         } else {
1611                                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1612                                         }
1613                                 } else {
1614                                         // kill the overlay
1615                                         help_overlay_set_state(BARRACKS_OVERLAY,0);
1616                                 }
1617                                 break;
1618
1619                         case SDLK_TAB:  // switch mode (simgle/multi)
1620 #if defined(DEMO) || defined(OEM_BUILD) // not for FS2_DEMO
1621         game_feature_not_in_demo_popup();
1622 #else
1623                                 if (Player_sel_mode == PLAYER_SELECT_MODE_SINGLE) {
1624                                         barracks_init_player_stuff(PLAYER_SELECT_MODE_MULTI);
1625                                 } else {
1626                                         barracks_init_player_stuff(PLAYER_SELECT_MODE_SINGLE);
1627                                 }
1628
1629                                 gamesnd_play_iface(SND_USER_SELECT);
1630 #endif
1631                                 break;
1632
1633                         case SDLK_F1:  // show help overlay
1634                                 gamesnd_play_iface(SND_HELP_PRESSED);
1635                                 break;
1636
1637                         case SDLK_F2:  // goto options screen
1638                                 gamesnd_play_iface(SND_SWITCH_SCREENS);
1639                                 gameseq_post_event(GS_EVENT_OPTIONS_MENU);
1640                                 break;
1641                 }       // end switch
1642
1643                 // process buttons
1644                 for (i=0; i<BARRACKS_NUM_BUTTONS; i++) {
1645                         if (Buttons[gr_screen.res][i].button.pressed()) {
1646                                 barracks_button_pressed(i);
1647                         }
1648                 }
1649
1650                 // if mouse is over a pilot, find index into Pilots array
1651                 if (List_region.is_mouse_on()) {
1652                         int y;
1653                         List_region.get_mouse_pos(NULL, &y);
1654                         int pilot_index = List_scroll_offset + (y / gr_get_font_height());
1655                         if ((pilot_index >= 0) && (pilot_index < Num_pilots)) {
1656                                 prospective_pilot = pilot_index;
1657                         }
1658                 }
1659                 
1660                 // if mouse clicked in list region, find index into Pilots array 
1661                 if (List_region.pressed()) {
1662                         if (prospective_pilot != -1) {
1663                                 Selected_line = prospective_pilot;
1664                                 gamesnd_play_iface(SND_USER_SELECT);
1665                         }
1666                 }
1667         }
1668
1669         // check mouse over help
1670         if (mouse_down(MOUSE_LEFT_BUTTON)) {
1671                 help_overlay_set_state(BARRACKS_OVERLAY, 0);
1672         }
1673
1674         // do pilot pic stuff
1675         if ((Pic_number >= 0) && (Pic_number < Num_pilot_images)) {
1676                 if (Pilot_images[Pic_number] == BARRACKS_IMAGE_NOT_LOADED) {  // haven't tried loading it yet
1677                         Pilot_images[Pic_number] = bm_load(Pilot_image_names[Pic_number]);
1678                         if (Pilot_images[Pic_number] >= 0) {
1679                                 int w, h;
1680
1681                                 bm_get_info(Pilot_images[Pic_number], &w, &h, NULL);
1682                                 // check for invalid pilot pic file
1683                                 if ((w != PLAYER_PILOT_PIC_W) || (h != PLAYER_PILOT_PIC_H)) {
1684                                         bm_unload(Pilot_images[Pic_number]);
1685                                         Pilot_images[Pic_number] = -1;
1686                                 }
1687                         }
1688                 }               
1689         } else {
1690                 Pic_number = -1;
1691         }
1692
1693         // do squad pic stuff
1694         if ((Pic_squad_number >= 0) && (Pic_squad_number < Num_pilot_squad_images)) {
1695                 if (Pilot_squad_images[Pic_squad_number] == BARRACKS_IMAGE_NOT_LOADED) {  // haven't tried loading it yet
1696                         Pilot_squad_images[Pic_squad_number] = bm_load_duplicate(Pilot_squad_image_names[Pic_squad_number]);
1697                         if (Pilot_squad_images[Pic_squad_number] >= 0) {
1698                                 int w, h;
1699
1700                                 bm_get_info(Pilot_squad_images[Pic_squad_number], &w, &h, NULL);
1701                                 // check for invalid pilot pic file
1702                                 if ((w != PLAYER_SQUAD_PIC_W) || (h != PLAYER_SQUAD_PIC_H)) {
1703                                         bm_unload(Pilot_squad_images[Pic_squad_number]);
1704                                         Pilot_squad_images[Pic_squad_number] = -1;
1705                                 }
1706                         }
1707                 }
1708         } else {
1709                 Pic_squad_number = -1;
1710         }
1711
1712         // draw the background, etc
1713         gr_reset_clip();        
1714         GR_MAYBE_CLEAR_RES(Background_bitmap);
1715         if (Background_bitmap >= 0) {
1716                 gr_set_bitmap(Background_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1717                 gr_bitmap(0, 0);        
1718         }               
1719
1720         // draw pilot image and clean up afterwards
1721         barracks_draw_pilot_pic();
1722         barracks_draw_squad_pic();
1723
1724 #if defined(MAKE_FS1) && !defined(FS1_DEMO)
1725         if (PilotWin01 != -1) {
1726                 gr_set_bitmap(PilotWin01, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1727                 gr_bitmap(447, 18);
1728         }
1729
1730         if (PilotWin02 != -1) {
1731                 gr_set_bitmap(PilotWin02, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1732                 gr_bitmap(620, 38);
1733         }
1734
1735         if (PilotWin03 != -1) {
1736                 gr_set_bitmap(PilotWin03, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1737                 gr_bitmap(447, 137);
1738         }
1739
1740         if (PilotWin04 != -1) {
1741                 gr_set_bitmap(PilotWin04, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1742                 gr_bitmap(447, 36);
1743         }
1744 #endif
1745
1746
1747         // draw the window      
1748         Ui_window.draw();       
1749
1750         // light up the correct mode button (single or multi)   
1751         if (Player_sel_mode == PLAYER_SELECT_MODE_SINGLE) {
1752                 Buttons[gr_screen.res][B_PILOT_SINGLE_MODE_BUTTON].button.draw_forced(2);
1753         } else {
1754                 Buttons[gr_screen.res][B_PILOT_MULTI_MODE_BUTTON].button.draw_forced(2);
1755         }       
1756
1757         // write out pilot call signs
1758         barracks_display_pilot_callsigns(prospective_pilot);
1759
1760         // write out current pilot stats
1761         barracks_display_pilot_stats();
1762
1763         // blit help overlay if active
1764         help_overlay_maybe_blit(BARRACKS_OVERLAY);      
1765         
1766         // flip the page
1767         gr_flip();
1768 }
1769
1770 // -----------------------------------------------------------------------------
1771 void barracks_close()
1772 {
1773 #if defined(MAKE_FS1) && !defined(FS1_DEMO)
1774     if (PilotWin01 != -1){
1775         bm_unload(PilotWin01);
1776         PilotWin01 = -1;
1777     }
1778     if (PilotWin02 != -1){
1779         bm_unload(PilotWin02);
1780         PilotWin02 = -1;
1781     }
1782     if (PilotWin03 != -1){
1783         bm_unload(PilotWin03);
1784         PilotWin03 = -1;
1785     }
1786     if (PilotWin04 != -1){
1787         bm_unload(PilotWin04);
1788         PilotWin04 = -1;
1789     }
1790 #endif
1791
1792         // destroy window
1793         Ui_window.destroy();
1794
1795         // release background bitmap
1796         if (Background_bitmap >= 0) {
1797                 bm_unload(Background_bitmap);
1798         }       
1799
1800         // release rank pip bitmaps
1801         for (int i=0; i<Rank_pips_count; i++) {
1802                 bm_release(Rank_pips_bitmaps + i);
1803         }       
1804
1805         // release pilot pic bitmaps
1806         for (int i=0; i<Num_pilot_images; i++) {
1807                 if (Pilot_images[i] >= 0) {
1808                         bm_unload(Pilot_images[i]);
1809                 }
1810         }
1811
1812         // unload the overlay bitmap
1813         help_overlay_unload(BARRACKS_OVERLAY);
1814
1815         game_flush();
1816 }
1817