]> icculus.org git repositories - taylor/freespace2.git/blob - src/menuui/playermenu.cpp
Various 64-bit platform fixes
[taylor/freespace2.git] / src / menuui / playermenu.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/PlayerMenu.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Code to drive the Player Select initial screen
16  *
17  * $Log$
18  * Revision 1.9  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.8  2004/09/20 01:31:44  theoddone33
26  * GCC 3.4 fixes.
27  *
28  * Revision 1.7  2003/08/09 03:18:03  taylor
29  * fix tips popup not having any tips
30  *
31  * Revision 1.6  2003/06/11 18:30:32  taylor
32  * plug memory leaks
33  *
34  * Revision 1.5  2003/05/25 02:30:42  taylor
35  * Freespace 1 support
36  *
37  * Revision 1.4  2003/05/22 16:13:35  taylor
38  * fix missed German build option for auto-lang
39  *
40  * Revision 1.3  2002/06/09 04:41:22  relnev
41  * added copyright header
42  *
43  * Revision 1.2  2002/05/07 03:16:46  theoddone33
44  * The Great Newline Fix
45  *
46  * Revision 1.1.1.1  2002/05/03 03:28:09  root
47  * Initial import.
48  * 
49  * 
50  * 43    11/02/99 11:42a Jefff
51  * fixed copyright symbol in german fonts
52  * 
53  * 42    10/27/99 12:27a Jefff
54  * localized tips correctly
55  * 
56  * 41    9/13/99 4:52p Dave
57  * RESPAWN FIX
58  * 
59  * 40    9/02/99 11:10a Jefff
60  * fixed 1024 list display bug - was only showing 8 pilots at a time
61  * 
62  * 39    8/26/99 8:51p Dave
63  * Gave multiplayer TvT messaging a heavy dose of sanity. Cheat codes.
64  * 
65  * 38    8/26/99 9:45a Dave
66  * First pass at easter eggs and cheats.
67  * 
68  * 37    8/16/99 6:39p Jefff
69  * 
70  * 36    8/16/99 6:37p Jefff
71  * minor string alterations
72  * 
73  * 35    8/05/99 4:17p Dave
74  * Tweaks to client interpolation.
75  * 
76  * 34    8/05/99 11:29a Mikek
77  * Jacked up number of comments from 20 to 40, thereby doubling the
78  * quality of our game.
79  * 
80  * 33    8/04/99 10:53a Dave
81  * Added title to the user tips popup.
82  * 
83  * 32    8/03/99 3:21p Jefff
84  * 
85  * 31    8/03/99 10:32a Jefff
86  * raised location of bottom_text to not interfere w/ logo.  changed
87  * "please enter callsign" to "type callsign and press enter"
88  * 
89  * 30    8/02/99 9:13p Dave
90  * Added popup tips.
91  * 
92  * 29    7/30/99 10:29a Jefff
93  * fixed colors of bottom display texts
94  * 
95  * 28    7/27/99 7:17p Jefff
96  * Replaced some art text with XSTR() text.
97  * 
98  * 27    7/19/99 2:06p Jasons
99  * Remove all palette stuff from player select menu.
100  * 
101  * 26    7/15/99 9:20a Andsager
102  * FS2_DEMO initial checkin
103  * 
104  * 25    7/09/99 9:51a Dave
105  * Added thick polyline code.
106  * 
107  * 24    6/11/99 11:13a Dave
108  * last minute changes before press tour build.
109  * 
110  * 23    5/21/99 6:45p Dave
111  * Sped up ui loading a bit. Sped up localization disk access stuff. Multi
112  * start game screen, multi password, and multi pxo-help screen.
113  * 
114  * 22    4/25/99 3:02p Dave
115  * Build defines for the E3 build.
116  * 
117  * 21    3/25/99 2:31p Neilk
118  * Coordinate changes to handle new artwork
119  * 
120  * 20    2/25/99 4:19p Dave
121  * Added multiplayer_beta defines. Added cd_check define. Fixed a few
122  * release build warnings. Added more data to the squad war request and
123  * response packets.
124  * 
125  * 19    2/01/99 5:55p Dave
126  * Removed the idea of explicit bitmaps for buttons. Fixed text
127  * highlighting for disabled gadgets.
128  * 
129  * 18    1/30/99 5:08p Dave
130  * More new hi-res stuff.Support for nice D3D textures.
131  * 
132  * 17    1/30/99 1:53a Dave
133  * Fix some harcoded coords.
134  * 
135  * 16    1/30/99 1:28a Dave
136  * 1024x768 full support.
137  * 
138  * 15    1/29/99 1:25p Dave
139  * New code for choose pilot screen.
140  * 
141  * 14    1/29/99 12:47a Dave
142  * Put in sounds for beam weapon. A bunch of interface screens (tech
143  * database stuff).
144  * 
145  * 13    1/12/99 12:53a Dave
146  * More work on beam weapons - made collision detection very efficient -
147  * collide against all object types properly - made 3 movement types
148  * smooth. Put in test code to check for possible non-darkening pixels on
149  * object textures.
150  * 
151  * 12    12/18/98 1:13a Dave
152  * Rough 1024x768 support for Direct3D. Proper detection and usage through
153  * the launcher.
154  * 
155  * 11    12/06/98 2:36p Dave
156  * Drastically improved nebula fogging.
157  * 
158  * 10    12/01/98 6:20p Dave
159  * Removed tga test bitmap code.
160  * 
161  * 9     12/01/98 4:46p Dave
162  * Put in targa bitmap support (16 bit).
163  * 
164  * 8     11/30/98 1:07p Dave
165  * 16 bit conversion, first run.
166  * 
167  * 7     11/20/98 11:16a Dave
168  * Fixed up IPX support a bit. Making sure that switching modes and
169  * loading/saving pilot files maintains proper state.
170  * 
171  * 6     11/19/98 4:19p Dave
172  * Put IPX sockets back in psnet. Consolidated all multiplayer config
173  * files into one.
174  * 
175  * 5     11/05/98 4:18p Dave
176  * First run nebula support. Beefed up localization a bit. Removed all
177  * conditional compiles for foreign versions. Modified mission file
178  * format.
179  * 
180  * 4     10/13/98 9:28a Dave
181  * Started neatening up freespace.h. Many variables renamed and
182  * reorganized. Added AlphaColors.[h,cpp]
183  * 
184  * 3     10/09/98 2:57p Dave
185  * Starting splitting up OS stuff.
186  * 
187  * 2     10/07/98 10:53a Dave
188  * Initial checkin.
189  * 
190  * 1     10/07/98 10:49a Dave
191  * 
192  *
193  * $NoKeywords: $
194  *
195  */
196
197 #include <ctype.h>
198
199 #include "playermenu.h"
200 #include "2d.h"
201 #include "ui.h"
202 #include "gamesnd.h"
203 #include "player.h"
204 #include "cfile.h"
205 #include "key.h"
206 #include "managepilot.h"
207 #include "missionscreencommon.h"
208 #include "bmpman.h"
209 #include "freespace.h"
210 #include "parselo.h"
211 #include "gamesequence.h"
212 #include "timer.h"
213 #include "cmdline.h"
214 #include "osregistry.h"
215 #include "palman.h"
216 #include "mainhallmenu.h"
217 #include "multi.h"
218 #include "popup.h"
219 #include "mouse.h"
220 #include "alphacolors.h"
221 #include "localize.h"
222
223 // --------------------------------------------------------------------------------------------------------
224 // Demo title screen
225 #if defined(FS2_DEMO) || defined(FS1_DEMO)
226 static int Demo_title_active = 0;
227 static int Demo_title_bitmap = -1;
228 static int Demo_title_expire_timestamp = 0;
229 static int Demo_title_need_fade_in = 1;
230 static char *Demo_title_bitmap_filename = NOX("DemoTitle1");
231 #endif
232
233 // --------------------------------------------------------------------------------------------------------
234 // PLAYER SELECT defines
235 //
236
237 //#define MAX_PLAYER_SELECT_LINES               8                                                       // max # of pilots displayed at once
238 int Player_select_max_lines[GR_NUM_RESOLUTIONS] = {                     // max # of pilots displayed at once
239         8,                      // GR_640
240         15                      // GR_1024
241 };
242
243 // button control defines
244 #define NUM_PLAYER_SELECT_BUTTONS       8                                                       // button control defines
245
246 #define CREATE_PILOT_BUTTON                     0                                                       //      
247 #define CLONE_BUTTON                                            1                                                       //
248 #define DELETE_BUTTON                                   2                                                       //
249 #define SCROLL_LIST_UP_BUTTON                   3                                                       //
250 #define SCROLL_LIST_DOWN_BUTTON         4                                                       //
251 #define ACCEPT_BUTTON                                   5                                                       //
252 #define SINGLE_BUTTON                                   6                                                       //
253 #define MULTI_BUTTON                                            7                                                       //
254
255 // list text display area
256 int Choose_list_coords[GR_NUM_RESOLUTIONS][4] = {
257         { // GR_640
258 #ifdef MAKE_FS1
259                 130, 140, 379, 68
260 #else
261                 114, 117, 400, 87
262 #endif
263         },
264         { // GR_1024
265                 183, 186, 640, 139
266         }
267 };
268
269 char *Player_select_background_bitmap_name[GR_NUM_RESOLUTIONS] = {
270         "ChoosePilot",
271         "2_ChoosePilot"
272 };
273 char *Player_select_background_mask_bitmap[GR_NUM_RESOLUTIONS] = {
274         "ChoosePilot-m",
275         "2_ChoosePilot-m"
276 };
277 // #define PLAYER_SELECT_PALETTE                                                        NOX("ChoosePilotPalette")       // palette for the screen       
278
279 #define PLAYER_SELECT_MAIN_HALL_OVERLAY         NOX("MainHall1")                                // main hall help overlay
280
281 // convenient struct for handling all button controls
282 struct barracks_buttons {
283         char *filename;
284         int x, y, xt, yt;
285         int hotspot;
286         UI_BUTTON button;  // because we have a class inside this struct, we need the constructor below..
287
288         barracks_buttons(char *name, int x1, int y1, int xt1, int yt1, int h) : filename(name), x(x1), y(y1), xt(xt1), yt(yt1), hotspot(h) {}
289 };
290
291 static barracks_buttons Player_select_buttons[GR_NUM_RESOLUTIONS][NUM_PLAYER_SELECT_BUTTONS] = {        
292         { // GR_640
293 #ifdef MAKE_FS1
294                 barracks_buttons("CHP_00",              126,    211,    -1,     -1,     0),     // create
295                 barracks_buttons("CHP_13",              181,    211,    -1,     -1,     13),    // clone
296                 barracks_buttons("CHP_11",              228,    211,    -1,     -1, 11),        // delete
297                 barracks_buttons("CHP_01",              423,    232,    -1,     -1,     1),     // scroll up
298                 barracks_buttons("CHP_02",              452,    232,    -1,     -1,     2),     // scroll down
299                 barracks_buttons("CHP_12",              475,    213,    -1,     -1,     12),    // accept
300                 barracks_buttons("CHP_14",              438,    104,    -1,     -1,     14),    // single
301                 barracks_buttons("CHP_15",              485,    104,    -1,     -1,     15)     // multi
302 #else
303                 // create, clone and delete (respectively)
304                 barracks_buttons("CPB_00",              114,    205,    117,    240,    0),
305                 barracks_buttons("CPB_01",              172,    205,    175,    240,    1),
306                 barracks_buttons("CPB_02",              226,    205,    229,    240,    2),
307
308                 // scroll up, scroll down,      and accept (respectively)
309                 barracks_buttons("CPB_03",              429,    213,    -1,     -1,     3),
310                 barracks_buttons("CPB_04",              456,    213,    -1,     -1,     4),
311                 barracks_buttons("CPB_05",              481,  207,      484,    246,    5),     
312                 
313                 // single player select and multiplayer select, respectively
314                 barracks_buttons("CPB_06",              428,    82,     430,    108,    6),
315                 barracks_buttons("CPB_07",              477,    82,     481,    108,    7)
316 #endif
317         }, 
318         { // GR_1024
319                 // create, clone and delete (respectively)
320                 barracks_buttons("2_CPB_00",    182,  328,      199,    384,    0),
321                 barracks_buttons("2_CPB_01",    275,    328,    292,    384,    1),
322                 barracks_buttons("2_CPB_02",    361,    328,    379,    384,    2),
323
324                 // scroll up, scroll down, and accept (respectively)
325                 barracks_buttons("2_CPB_03",    686,    341,    -1,     -1,     3),
326                 barracks_buttons("2_CPB_04",    729,    341,    -1,     -1,     4),
327                 barracks_buttons("2_CPB_05",    770,  332,      787,    394,    5),     
328                 
329                 // single player select and multiplayer select, respectively
330                 barracks_buttons("2_CPB_06",    685,    132,    700,    173,    6),
331                 barracks_buttons("2_CPB_07",    764,    132,    782,    173,    7)
332         }
333 };
334
335 // FIXME add to strings.tbl
336 #ifndef MAKE_FS1
337 #define PLAYER_SELECT_NUM_TEXT                  1
338
339 UI_XSTR Player_select_text[GR_NUM_RESOLUTIONS][PLAYER_SELECT_NUM_TEXT] = {
340         { // GR_640
341                 { "Choose Pilot",               1436,           122,    90,     UI_XSTR_COLOR_GREEN, -1, NULL }
342         }, 
343         { // GR_1024
344                 { "Choose Pilot",               1436,           195,    143,    UI_XSTR_COLOR_GREEN, -1, NULL }
345         }
346 };
347 #endif
348
349 UI_WINDOW Player_select_window;                                                         // ui window for this screen
350 UI_BUTTON Player_select_list_region;                                            // button for detecting mouse clicks on this screen
351 UI_INPUTBOX Player_select_input_box;                                            // input box for adding new pilot names                         
352
353 // #define PLAYER_SELECT_PALETTE_FNAME                                  NOX("InterfacePalette")
354 int Player_select_background_bitmap;                                            // bitmap for this screen
355 // int Player_select_palette;                                                                           // palette bitmap for this screen
356 int Player_select_autoaccept = 0;
357 // int Player_select_palette_set = 0;
358
359 // flag indicating if this is the absolute first pilot created and selected. Used to determine
360 // if the main hall should display the help overlay screen
361 int Player_select_very_first_pilot = 0;                 
362 int Player_select_initial_count = 0;
363 char Player_select_very_first_pilot_callsign[CALLSIGN_LEN + 2];
364
365 extern int Main_hall_bitmap;                                                                    // bitmap handle to the main hall bitmap
366
367 int Player_select_mode;                                                                                 // single or multiplayer - never set directly. use player_select_init_player_stuff()
368 int Player_select_num_pilots;                                                                   // # of pilots on the list
369 int Player_select_list_start;                                                                   // index of first list item to start displaying in the box
370 int Player_select_pilot;                                                                            // index into the Pilot array of which is selected as the active pilot
371 int Player_select_input_mode;                                                                   // 0 if the player _isn't_ typing a callsign, 1 if he is
372 char Pilots_arr[MAX_PILOTS][MAX_FILENAME_LEN];          
373 char *Pilots[MAX_PILOTS];
374 int Player_select_clone_flag;                                                                   // clone the currently selected pilot
375 char Player_select_last_pilot[CALLSIGN_LEN + 10];               // callsign of the last used pilot, or none if there wasn't one
376 int Player_select_last_is_multi;
377
378 int Player_select_force_bastion = 0;
379
380 // notification text areas
381
382 static int Player_select_bottom_text_y[GR_NUM_RESOLUTIONS] = {
383 #ifdef MAKE_FS1
384         280,
385 #else
386         314,    // GR_640
387 #endif
388         502     // GR_1024
389 };
390
391 static int Player_select_middle_text_y[GR_NUM_RESOLUTIONS] = {
392 #ifdef MAKE_FS1
393         280,
394 #else
395         253,    // GR_640
396 #endif
397         404     // GR_1024
398 };
399
400 char Player_select_bottom_text[150] = "";
401 char Player_select_middle_text[150] = "";
402 void player_select_set_bottom_text(char *txt);
403 void player_select_set_middle_text(char *txt);
404
405
406 // FORWARD DECLARATIONS
407 void player_select_init_player_stuff(int mode);                 // switch between single and multiplayer modes
408 void player_select_set_input_mode(int n);                                       
409 void player_select_button_pressed(int n);
410 void player_select_scroll_list_up();
411 void player_select_scroll_list_down();
412 int player_select_create_new_pilot();
413 void player_select_delete_pilot();
414 void player_select_display_all_text();
415 void player_select_display_copyright();
416 void player_select_set_bottom_text(char *txt);
417 void player_select_set_controls(int gray);
418 void player_select_draw_list();
419 void player_select_process_noninput(int k);
420 void player_select_process_input(int k);
421 int player_select_pilot_file_filter(char *filename);
422 int player_select_get_last_pilot_info();
423 void player_select_eval_very_first_pilot();
424 void player_select_commit();
425 void player_select_cancel_create();
426
427
428 // basically, gray out all controls (gray == 1), or ungray the controls (gray == 0) 
429 void player_select_set_controls(int gray)
430 {
431         int idx;
432         
433         for(idx=0;idx<NUM_PLAYER_SELECT_BUTTONS;idx++){
434                 if(gray){
435                         Player_select_buttons[gr_screen.res][idx].button.disable();
436                 } else {
437                         Player_select_buttons[gr_screen.res][idx].button.enable();
438                 }
439         }
440 }
441
442 // functions for selecting single/multiplayer pilots at the very beginning of Freespace
443 void player_select_init()
444 {                       
445         int i;
446         barracks_buttons *b;   
447 #ifndef MAKE_FS1
448         UI_WINDOW *w;
449 #endif
450
451         // start a looping ambient sound
452         main_hall_start_ambient();
453
454         Player_select_force_bastion = 0;
455
456 #ifdef FS2_DEMO
457         /*
458         Demo_title_bitmap = bm_load(Demo_title_bitmap_filename);
459         if ( Demo_title_bitmap >= 0 ) {
460 #ifndef HARDWARE_ONLY
461                 palette_use_bm_palette(Demo_title_bitmap);
462 #endif
463                 Demo_title_active = 1;
464                 Demo_title_expire_timestamp = timestamp(5000);
465         } else {
466                 Demo_title_active = 0;
467         }
468         */
469         Demo_title_active = 0;
470 #endif
471
472         // create the UI window
473         Player_select_window.create(0, 0, gr_screen.max_w, gr_screen.max_h, 0);
474         Player_select_window.set_mask_bmap(Player_select_background_mask_bitmap[gr_screen.res]);
475
476 #ifdef MAKE_FS1
477         common_set_interface_palette("ChoosePilotPalette");
478 #endif
479
480         // initialize the control buttons
481         for (i=0; i<NUM_PLAYER_SELECT_BUTTONS; i++) {
482                 b = &Player_select_buttons[gr_screen.res][i];
483
484                 // create the button
485                 if ( (i == SCROLL_LIST_UP_BUTTON) || (i == SCROLL_LIST_DOWN_BUTTON) )
486                         b->button.create(&Player_select_window, NULL, b->x, b->y, 60, 30, 1, 1);
487                 else
488                         b->button.create(&Player_select_window, NULL, b->x, b->y, 60, 30, 1, 1);
489
490                 // set its highlight action
491                 b->button.set_highlight_action(common_play_highlight_sound);
492
493                 // set its animation bitmaps
494                 b->button.set_bmaps(b->filename);
495
496                 // link the mask hotspot
497                 b->button.link_hotspot(b->hotspot);
498         }               
499
500 #ifndef MAKE_FS1
501         // add some text
502         w = &Player_select_window;      
503         w->add_XSTR("Create", 1034, Player_select_buttons[gr_screen.res][CREATE_PILOT_BUTTON].xt, Player_select_buttons[gr_screen.res][CREATE_PILOT_BUTTON].yt, &Player_select_buttons[gr_screen.res][CREATE_PILOT_BUTTON].button, UI_XSTR_COLOR_GREEN);        
504         w->add_XSTR("Clone", 1040, Player_select_buttons[gr_screen.res][CLONE_BUTTON].xt, Player_select_buttons[gr_screen.res][CLONE_BUTTON].yt, &Player_select_buttons[gr_screen.res][CLONE_BUTTON].button, UI_XSTR_COLOR_GREEN);      
505         w->add_XSTR("Remove", 1038, Player_select_buttons[gr_screen.res][DELETE_BUTTON].xt, Player_select_buttons[gr_screen.res][DELETE_BUTTON].yt, &Player_select_buttons[gr_screen.res][DELETE_BUTTON].button, UI_XSTR_COLOR_GREEN);  
506         
507         w->add_XSTR("Select", 1039, Player_select_buttons[gr_screen.res][ACCEPT_BUTTON].xt, Player_select_buttons[gr_screen.res][ACCEPT_BUTTON].yt, &Player_select_buttons[gr_screen.res][ACCEPT_BUTTON].button, UI_XSTR_COLOR_PINK);   
508         w->add_XSTR("Single", 1041, Player_select_buttons[gr_screen.res][SINGLE_BUTTON].xt, Player_select_buttons[gr_screen.res][SINGLE_BUTTON].yt, &Player_select_buttons[gr_screen.res][SINGLE_BUTTON].button, UI_XSTR_COLOR_GREEN);  
509         w->add_XSTR("Multi", 1042, Player_select_buttons[gr_screen.res][MULTI_BUTTON].xt, Player_select_buttons[gr_screen.res][MULTI_BUTTON].yt, &Player_select_buttons[gr_screen.res][MULTI_BUTTON].button, UI_XSTR_COLOR_GREEN);      
510         for(i=0; i<PLAYER_SELECT_NUM_TEXT; i++) {
511                 w->add_XSTR(&Player_select_text[gr_screen.res][i]);
512         }
513 #endif
514
515         // create the list button text select region
516         Player_select_list_region.create(&Player_select_window, "", Choose_list_coords[gr_screen.res][0], Choose_list_coords[gr_screen.res][1], Choose_list_coords[gr_screen.res][2], Choose_list_coords[gr_screen.res][3], 0, 1);
517         Player_select_list_region.hide();
518
519         // create the pilot callsign input box
520         Player_select_input_box.create(&Player_select_window, Choose_list_coords[gr_screen.res][0], Choose_list_coords[gr_screen.res][1], Choose_list_coords[gr_screen.res][2] , CALLSIGN_LEN - 1, "", UI_INPUTBOX_FLAG_INVIS | UI_INPUTBOX_FLAG_KEYTHRU | UI_INPUTBOX_FLAG_LETTER_FIRST);
521         Player_select_input_box.set_valid_chars(VALID_PILOT_CHARS);
522         Player_select_input_box.hide();
523         Player_select_input_box.disable();
524         
525         // not currently entering any text
526         Player_select_input_mode = 0;   
527
528         // set up hotkeys for buttons so we draw the correct animation frame when a key is pressed
529         Player_select_buttons[gr_screen.res][SCROLL_LIST_UP_BUTTON].button.set_hotkey(KEY_UP);
530         Player_select_buttons[gr_screen.res][SCROLL_LIST_DOWN_BUTTON].button.set_hotkey(KEY_DOWN);
531         Player_select_buttons[gr_screen.res][ACCEPT_BUTTON].button.set_hotkey(KEY_ENTER);
532         Player_select_buttons[gr_screen.res][CREATE_PILOT_BUTTON].button.set_hotkey(KEY_C);
533
534         // disable the single player button in the multiplayer beta
535 #ifdef MULTIPLAYER_BETA_BUILD
536         Player_select_buttons[gr_screen.res][SINGLE_BUTTON].button.hide();
537         Player_select_buttons[gr_screen.res][SINGLE_BUTTON].button.disable();
538 #elif defined(E3_BUILD) || defined(PRESS_TOUR_BUILD)
539         Player_select_buttons[gr_screen.res][MULTI_BUTTON].button.hide();
540         Player_select_buttons[gr_screen.res][MULTI_BUTTON].button.disable();
541 #endif
542
543
544         // attempt to load in the background bitmap
545         Player_select_background_bitmap = bm_load(Player_select_background_bitmap_name[gr_screen.res]);                         
546         Assert(Player_select_background_bitmap >= 0);   
547
548         // load in the palette for the screen
549         // Player_select_palette = bm_load(PLAYER_SELECT_PALETTE);
550         // Player_select_palette_set = 0;
551
552         // unset the very first pilot data
553         Player_select_very_first_pilot = 0;
554         Player_select_initial_count = -1;
555         memset(Player_select_very_first_pilot_callsign, 0, CALLSIGN_LEN + 2);   
556
557 //      if(Player_select_num_pilots == 0){
558 //              Player_select_autoaccept = 1;
559 //      }
560                 
561         // if we found a pilot
562 #if defined(DEMO) || defined(OEM_BUILD) || defined(E3_BUILD) || defined(PRESS_TOUR_BUILD) // not for FS2_DEMO
563         player_select_init_player_stuff(PLAYER_SELECT_MODE_SINGLE);     
564 #elif defined(MULTIPLAYER_BETA_BUILD)
565         player_select_init_player_stuff(PLAYER_SELECT_MODE_MULTI);      
566 #else
567         if (player_select_get_last_pilot_info()) {
568                 if (Player_select_last_is_multi) {
569                         player_select_init_player_stuff(PLAYER_SELECT_MODE_MULTI);
570                 } else {
571                         player_select_init_player_stuff(PLAYER_SELECT_MODE_SINGLE);
572                 }
573         } 
574         // otherwise go to the single player mode by default
575         else {
576                 player_select_init_player_stuff(PLAYER_SELECT_MODE_SINGLE);
577         }
578 #endif  
579
580         if((Player_select_num_pilots == 1) && Player_select_input_mode){
581                 Player_select_autoaccept = 1;
582         }       
583 }
584
585 #if defined(FS2_DEMO) || defined(FS1_DEMO)
586 // Display the demo title screen
587 void demo_title_blit()
588 {
589         int k;
590
591         Mouse_hidden = 1;
592
593         if ( timestamp_elapsed(Demo_title_expire_timestamp) ) {
594                 Demo_title_active = 0;
595         }
596
597         k = game_poll();
598         if ( k > 0 ) {
599                 Demo_title_active = 0;
600         }
601
602         if ( Demo_title_need_fade_in ) {
603                 gr_fade_out(0);
604         }
605         
606         gr_set_bitmap(Demo_title_bitmap);
607         gr_bitmap(0,0);
608
609         gr_flip();
610
611         if ( Demo_title_need_fade_in ) {
612                 gr_fade_in(0);
613                 Demo_title_need_fade_in = 0;
614         }
615
616         if ( !Demo_title_active ) {
617                 gr_fade_out(0);
618                 Mouse_hidden = 0;
619         }
620 }
621
622 #endif
623
624 void player_select_do()
625 {
626         int k;
627
628 #ifdef FS2_DEMO
629         if ( Demo_title_active ) {
630                 // demo_title_blit();
631                 return;
632         }
633 #endif
634
635         //if ( !Player_select_palette_set ) {
636         //      Assert(Player_select_palette >= 0);
637 //#ifndef HARDWARE_ONLY
638 //              palette_use_bm_palette(Player_select_palette);
639 //#endif
640 //              Player_select_palette_set = 1;
641 //      }
642                 
643         // set the input box at the "virtual" line 0 to be active so the player can enter a callsign
644         if (Player_select_input_mode){
645                 Player_select_input_box.set_focus();
646         }
647
648         // process any ui window stuff
649         k = Player_select_window.process();
650         if(k){
651                 extern void game_process_cheats(int k);
652                 game_process_cheats(k);
653         }
654         switch(k){
655         // switch between single and multiplayer modes
656         case KEY_TAB : 
657 #if defined(DEMO) || defined(OEM_BUILD) // not for FS2_DEMO
658                 break;
659 #else
660
661                 if(Player_select_input_mode){
662                         gamesnd_play_iface(SND_GENERAL_FAIL);
663                         break;
664                 }
665                 // play a little sound
666                 gamesnd_play_iface(SND_USER_SELECT);
667                 if(Player_select_mode == PLAYER_SELECT_MODE_MULTI){                                     
668 #ifdef MAKE_FS1
669                         player_select_set_bottom_text(XSTR( "Single Player Mode", 376));
670 #else
671                         player_select_set_bottom_text(XSTR( "Single-Player Mode", 376));
672 #endif
673                                 
674                         // reinitialize as single player mode
675                         player_select_init_player_stuff(PLAYER_SELECT_MODE_SINGLE);
676                 } else if(Player_select_mode == PLAYER_SELECT_MODE_SINGLE){                                                                             
677                         player_select_set_bottom_text(XSTR( "Multiplayer Mode", 377));
678                                 
679                         // reinitialize as multiplayer mode
680                         player_select_init_player_stuff(PLAYER_SELECT_MODE_MULTI);
681                 }
682                 break;  
683 #endif
684         }       
685
686         // draw the player select pseudo-dialog over it
687         gr_set_bitmap(Player_select_background_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
688         gr_bitmap(0,0);
689
690         // press the accept button
691         if (Player_select_autoaccept) {
692                 Player_select_buttons[gr_screen.res][ACCEPT_BUTTON].button.press_button();
693         }
694         
695         // draw any ui window stuf
696         Player_select_window.draw();
697
698         // light up the correct mode button (single or multi)
699         if (Player_select_mode == PLAYER_SELECT_MODE_SINGLE){
700                 Player_select_buttons[gr_screen.res][SINGLE_BUTTON].button.draw_forced(2);
701         } else {
702                 Player_select_buttons[gr_screen.res][MULTI_BUTTON].button.draw_forced(2);
703         }
704
705         // draw the pilot list text
706         player_select_draw_list();      
707
708         // draw copyright message on the bottom on the screen
709         player_select_display_copyright();
710
711         if (!Player_select_input_mode) {
712                 player_select_process_noninput(k);
713         } else {
714                 player_select_process_input(k);
715         }
716         
717         // draw any pending messages on the bottom or middle of the screen
718         player_select_display_all_text();       
719
720 #ifndef RELEASE_REAL
721         // gr_set_color_fast(&Color_bright_green);
722         // gr_string(0x8000, 10, "Development version - DO NOT RELEASE");
723 #endif
724         
725         /*
726         gr_set_color(255, 0, 0);
727         vector whee[5];
728         vector *arr[5] = {&whee[0], &whee[1], &whee[2], &whee[3], &whee[4]};
729         whee[0].x = 10; whee[0].y = 10; whee[0].z = 0.0f;
730         whee[1].x = 50; whee[1].y = 50; whee[1].z = 0.0f;
731         whee[2].x = 50; whee[2].y = 90; whee[2].z = 0.0f;
732         whee[3].x = 90; whee[3].y = 130; whee[3].z = 0.0f;
733         whee[4].x = 180; whee[4].y = 130; whee[4].z = 0.0f;
734         gr_pline_special(arr, 5, 2);
735         */
736         
737
738         gr_flip();
739 }
740
741 void player_select_close()
742 {
743         // destroy the player select window
744         Player_select_window.destroy();
745
746         // if we're in input mode - we should undo the pilot create reqeust
747         if(Player_select_input_mode){
748                 player_select_cancel_create();
749         }
750         
751         // actually set up the Player struct here       
752         if((Player_select_pilot == -1) || (Player_select_num_pilots == 0)){
753                 nprintf(("General","WARNING! No pilot selected! We should be exiting the game now!\n"));
754                 return;
755         }
756
757         // unload all bitmaps
758         if(Player_select_background_bitmap >= 0){
759                 bm_release(Player_select_background_bitmap);
760                 Player_select_background_bitmap = -1;
761         } 
762         // if(Player_select_palette >= 0){
763         //      bm_release(Player_select_palette);
764                 //Player_select_palette = -1;
765         // }
766
767 #ifdef MAKE_FS1
768         common_free_interface_palette();
769 #endif
770                         
771         // setup the player  struct
772         Player_num = 0;
773         Player = &Players[0];
774         Player->flags |= PLAYER_FLAGS_STRUCTURE_IN_USE;
775                 
776         // now read in a the pilot data
777         if (read_pilot_file(Pilots[Player_select_pilot], !Player_select_mode, Player) != 0) {
778                 Error(LOCATION,"Couldn't load pilot file, bailing");
779                 Player = NULL;
780         }               
781
782         if (Player_select_force_bastion) {
783                 Player->on_bastion = 1;
784         }
785 }
786
787 void player_select_set_input_mode(int n)
788 {
789         int i;
790
791         // set the input mode
792         Player_select_input_mode = n;   
793         
794         // enable all the player select buttons
795         for (i=0; i<NUM_PLAYER_SELECT_BUTTONS; i++){
796                 Player_select_buttons[gr_screen.res][i].button.enable(!n);
797         }
798
799         Player_select_buttons[gr_screen.res][ACCEPT_BUTTON].button.set_hotkey(n ? -1 : KEY_ENTER);
800         Player_select_buttons[gr_screen.res][CREATE_PILOT_BUTTON].button.set_hotkey(n ? -1 : KEY_C);
801
802         // enable the player select input box
803         if(Player_select_input_mode){
804                 Player_select_input_box.enable();
805                 Player_select_input_box.unhide();
806         } else {
807                 Player_select_input_box.hide();
808                 Player_select_input_box.disable();
809         }
810 }
811
812 void player_select_button_pressed(int n)
813 {
814         int ret;
815
816         switch (n) {
817         case SCROLL_LIST_UP_BUTTON:
818                 player_select_set_bottom_text("");
819
820                 player_select_scroll_list_up();
821                 break;
822
823         case SCROLL_LIST_DOWN_BUTTON:
824                 player_select_set_bottom_text("");
825
826                 player_select_scroll_list_down();
827                 break;
828
829         case ACCEPT_BUTTON:
830                 // make sure he has a valid pilot selected
831                 if (Player_select_pilot < 0) {                                                          
832                         popup(PF_USE_AFFIRMATIVE_ICON,1,POPUP_OK,XSTR( "You must select a valid pilot first", 378));
833                 } else {
834                         player_select_commit();                         
835                 }
836                 break;
837
838         case CLONE_BUTTON:
839                 // if we're at max-pilots, don't allow another to be added
840                 if (Player_select_num_pilots >= MAX_PILOTS) {
841                         player_select_set_bottom_text(XSTR( "You already have the maximum # of pilots!", 379));
842                         
843                         gamesnd_play_iface(SND_GENERAL_FAIL);
844                         break;
845                 }
846
847                 if (Player_select_pilot >= 0) {                                         
848                         // first we have to make sure this guy is actually loaded for when we create the clone
849                         if (Player == NULL) {
850                                 Player = &Players[0];
851                                 Player->flags |= PLAYER_FLAGS_STRUCTURE_IN_USE;
852                         }                               
853
854                         // attempt to read in the pilot file of the guy to be cloned
855                         if (read_pilot_file(Pilots[Player_select_pilot], !Player_select_mode, Player) != 0) {
856                                 Error(LOCATION,"Couldn't load pilot file, bailing");
857                                 Player = NULL;
858                                 Int3();
859                         }                               
860
861                         // set the clone flag
862                         Player_select_clone_flag = 1;
863
864                         // create the new pilot (will be cloned with Player_select_clone_flag_set)
865                         if (!player_select_create_new_pilot()) {                                        
866                                 player_select_set_bottom_text(XSTR( "Error creating new pilot file!", 380));
867                                 Player_select_clone_flag = 0;
868                                 memset(Player,0,sizeof(player));
869                                 Player = NULL;
870                                 break;
871                         }                               
872
873                         // clear the player out
874                         // JH: What the hell?  How do you clone a pilot if you clear out the source you are copying
875                         // from?  These next 2 lines are pure stupidity, so I commented them out!
876 //                      memset(Player,0,sizeof(player));
877 //                      Player = NULL;
878                                 
879                         // display some text on the bottom of the dialog
880                         player_select_set_bottom_text(XSTR( "Type Callsign and Press Enter", 381));                             
881                         
882                         // gray out all controls in the dialog
883                         player_select_set_controls(1);                                  
884                 }
885                 break;
886
887         case CREATE_PILOT_BUTTON:
888                 // if we're at max-pilots, don't allow another to be added
889                 if(Player_select_num_pilots >= MAX_PILOTS){
890                         player_select_set_bottom_text(XSTR( "You already have the maximum # of pilots!", 379));
891
892                         gamesnd_play_iface(SND_GENERAL_FAIL);
893                         break;
894                 }
895
896                 // create a new pilot
897                 if (!player_select_create_new_pilot()) {
898                         player_select_set_bottom_text(XSTR( "Type Callsign and Press Enter", 381));
899                 }
900
901                 // don't clone anyone
902                 Player_select_clone_flag = 0;
903                         
904                 // display some text on the bottom of the dialog                        
905                 player_select_set_bottom_text(XSTR( "Type Callsign and Press Enter", 381));
906                         
907                 // gray out all controls
908                 player_select_set_controls(1);                                          
909                 break;
910
911         case DELETE_BUTTON:
912                 player_select_set_bottom_text("");
913
914                 if (Player_select_pilot >= 0) {
915                         // display a popup requesting confirmation
916                         ret = popup(PF_TITLE_BIG | PF_TITLE_RED, 2, POPUP_NO, POPUP_YES, XSTR( "Warning!\n\nAre you sure you wish to delete this pilot?", 382));
917
918                         // delete the pilot
919                         if(ret == 1){
920                                 player_select_delete_pilot();
921                         } 
922                 }
923                 break;
924
925         case SINGLE_BUTTON:
926                 player_select_set_bottom_text("");
927
928                 Player_select_autoaccept = 0;
929                 // switch to single player mode
930                 if (Player_select_mode != PLAYER_SELECT_MODE_SINGLE) {
931                         // play a little sound
932                         gamesnd_play_iface(SND_USER_SELECT);
933                                 
934                         player_select_set_bottom_text(XSTR( "Single Player Mode", 376));
935                                 
936                         // reinitialize as single player mode
937                         player_select_init_player_stuff(PLAYER_SELECT_MODE_SINGLE);
938                 } else {
939                         gamesnd_play_iface(SND_GENERAL_FAIL);
940                 }
941                 break;
942
943         case MULTI_BUTTON:
944                 player_select_set_bottom_text("");
945
946                 Player_select_autoaccept = 0;
947 #if defined(DEMO) || defined(OEM_BUILD) // not for FS2_DEMO
948                 game_feature_not_in_demo_popup();
949 #else
950                 // switch to multiplayer mode
951                 if (Player_select_mode != PLAYER_SELECT_MODE_MULTI) {
952                         // play a little sound
953                         gamesnd_play_iface(SND_USER_SELECT);
954                         
955                         player_select_set_bottom_text(XSTR( "Multiplayer Mode", 377));
956                                 
957                         // reinitialize as multiplayer mode
958                         player_select_init_player_stuff(PLAYER_SELECT_MODE_MULTI);
959                 } else {
960                         gamesnd_play_iface(SND_GENERAL_FAIL);
961                 }
962 #endif
963                 break;
964         }
965 }
966
967 int player_select_create_new_pilot()
968 {
969         int idx;
970
971         // make sure we haven't reached the max
972         if (Player_select_num_pilots >= MAX_PILOTS) {
973                 gamesnd_play_iface(SND_GENERAL_FAIL);
974                 return 0;
975         }
976
977         int play_scroll_sound = 1;
978
979 #if defined(FS2_DEMO) || defined(FS1_DEMO)
980         if ( Demo_title_active ) {
981                 play_scroll_sound = 0;
982         }
983 #endif
984
985         if ( play_scroll_sound ) {
986                 gamesnd_play_iface(SND_SCROLL);
987         }
988
989         idx = Player_select_num_pilots; 
990         
991         // move all the pilots in the list up
992         while (idx--) {
993                 strcpy(Pilots[idx + 1], Pilots[idx]);           
994         }       
995
996         // by default, set the default netgame protocol to be VMT
997         Multi_options_g.protocol = NET_TCP;     
998
999         // select the beginning of the list
1000         Player_select_pilot = 0;
1001         Player_select_num_pilots++;
1002         Pilots[Player_select_pilot][0] = 0;
1003         Player_select_list_start= 0;
1004
1005         // set us to be in input mode
1006         player_select_set_input_mode(1);
1007         
1008         // set the input box to have focus
1009         Player_select_input_box.set_focus();
1010         Player_select_input_box.set_text("");
1011         Player_select_input_box.update_dimensions(Choose_list_coords[gr_screen.res][0], Choose_list_coords[gr_screen.res][1], Choose_list_coords[gr_screen.res][2], gr_get_font_height());      
1012
1013         return 1;
1014 }
1015
1016 void player_select_delete_pilot()
1017 {
1018         char filename[MAX_PATH_LEN + 1];
1019         int i, deleted_cur_pilot;
1020
1021         deleted_cur_pilot = 0;
1022
1023         // tack on the full path and the pilot file extension
1024         // build up the path name length
1025         // make sure we do this based upon whether we're in single or multiplayer mode
1026         strcpy( filename, Pilots[Player_select_pilot] );
1027         strcat( filename, NOX(".plr") );
1028
1029         // attempt to delete the pilot
1030         if (Player_select_mode == PLAYER_SELECT_MODE_SINGLE) {
1031                 cf_delete( filename, CF_TYPE_SINGLE_PLAYERS );
1032         } else {
1033                 cf_delete( filename, CF_TYPE_MULTI_PLAYERS );
1034         }
1035
1036         // delete all the campaign save files for this pilot.
1037         mission_campaign_delete_all_savefiles( Pilots[Player_select_pilot], (Player_select_mode != PLAYER_SELECT_MODE_SINGLE) );
1038
1039         // move all the players down
1040         for (i=Player_select_pilot; i<Player_select_num_pilots-1; i++){
1041                 strcpy(Pilots[i], Pilots[i + 1]);               
1042         }               
1043
1044         // correcly set the # of pilots and the currently selected pilot
1045         Player_select_num_pilots--;
1046         if (Player_select_pilot >= Player_select_num_pilots) {
1047                 Player_select_pilot = Player_select_num_pilots - 1;             
1048         }               
1049
1050 }
1051
1052 // scroll the list of players up
1053 void player_select_scroll_list_up()
1054 {
1055         if (Player_select_pilot == -1)
1056                 return;
1057
1058         // change the pilot selected index and play the appropriate sound
1059         if (Player_select_pilot) {
1060                 Player_select_pilot--;
1061                 gamesnd_play_iface(SND_SCROLL);
1062         } else {
1063                 gamesnd_play_iface(SND_GENERAL_FAIL);
1064         }
1065                 
1066         if (Player_select_pilot < Player_select_list_start){
1067                 Player_select_list_start = Player_select_pilot;
1068         }
1069 }
1070
1071 // scroll the list of players down
1072 void player_select_scroll_list_down()
1073 {       
1074         // change the pilot selected index and play the appropriate sound
1075         if (Player_select_pilot < Player_select_num_pilots - 1) {
1076                 Player_select_pilot++;
1077                 gamesnd_play_iface(SND_SCROLL);
1078         } else {
1079                 gamesnd_play_iface(SND_GENERAL_FAIL);
1080         }
1081                 
1082         if (Player_select_pilot >= (Player_select_list_start + Player_select_max_lines[gr_screen.res])){
1083                 Player_select_list_start++;
1084         }
1085 }
1086
1087 // fill in the data on the last played pilot (callsign and is_multi or not)
1088 int player_select_get_last_pilot_info()
1089 {
1090         char *last_player;
1091
1092         last_player = os_config_read_string( NULL, "LastPlayer", NULL);
1093         
1094         if(last_player == NULL){
1095                 return 0;               
1096         } else {
1097                 strcpy(Player_select_last_pilot,last_player);
1098         }
1099
1100         // determine if he was a single or multi-player based upon the last character in his callsign
1101         Player_select_last_is_multi = Player_select_last_pilot[strlen(Player_select_last_pilot)-1] == 'M' ? 1 : 0;
1102         Player_select_last_pilot[strlen(Player_select_last_pilot)-1]='\0';
1103
1104         return 1;       
1105 }
1106
1107 int player_select_get_last_pilot()
1108 {
1109         // if the player has the Cmdline_use_last_pilot command line option set, try and drop out quickly
1110         if(Cmdline_use_last_pilot){                     
1111                 int idx;                                
1112
1113                 if(!player_select_get_last_pilot_info()){
1114                         return 0;
1115                 }
1116
1117                 if(Player_select_last_is_multi){
1118                         Player_select_num_pilots = cf_get_file_list_preallocated(MAX_PILOTS, Pilots_arr, Pilots, CF_TYPE_MULTI_PLAYERS, NOX("*.plr"), CF_SORT_TIME);                            
1119                 } else {
1120                         Player_select_num_pilots = cf_get_file_list_preallocated(MAX_PILOTS, Pilots_arr, Pilots, CF_TYPE_SINGLE_PLAYERS, NOX("*.plr"), CF_SORT_TIME);                                           
1121                 }
1122
1123                 Player_select_pilot = -1;
1124                 idx = 0;
1125                 // pick the last player         
1126                 for(idx=0;idx<Player_select_num_pilots;idx++){
1127                         if(strcmp(Player_select_last_pilot,Pilots_arr[idx])==0){
1128                                 Player_select_pilot = idx;
1129                                 break;
1130                         }
1131                 }               
1132
1133                 // set this so that we don't incorrectly create a "blank" pilot - .plr
1134                 // in the player_select_close() function
1135                 Player_select_num_pilots = 0;
1136
1137                 // if we've actually found a valid pilot, load him up           
1138                 if(Player_select_pilot != -1){
1139                         Player = &Players[0];                   
1140                         read_pilot_file(Pilots_arr[idx],!Player_select_last_is_multi,Player);
1141                         Player->flags |= PLAYER_FLAGS_STRUCTURE_IN_USE;
1142                         return 1;               
1143                 }                       
1144         } 
1145
1146         return 0;
1147 }
1148
1149 void player_select_init_player_stuff(int mode)
1150 {                       
1151         Player_select_list_start = 0;   
1152
1153         // set the select mode to single player for default
1154         Player_select_mode = mode;
1155
1156         // load up the list of players based upon the Player_select_mode (single or multiplayer)
1157         Get_file_list_filter = player_select_pilot_file_filter;
1158         if (mode == PLAYER_SELECT_MODE_SINGLE){
1159                 Player_select_num_pilots = cf_get_file_list_preallocated(MAX_PILOTS, Pilots_arr, Pilots, CF_TYPE_SINGLE_PLAYERS, NOX("*.plr"), CF_SORT_TIME);
1160         } else {
1161                 Player_select_num_pilots = cf_get_file_list_preallocated(MAX_PILOTS, Pilots_arr, Pilots, CF_TYPE_MULTI_PLAYERS, NOX("*.plr"), CF_SORT_TIME);
1162         }
1163
1164         Player = NULL;  
1165
1166         // if this value is -1, it means we should set it to the num pilots count
1167         if(Player_select_initial_count == -1){
1168                 Player_select_initial_count = Player_select_num_pilots;
1169         }
1170                 
1171         // select the first pilot if any exist, otherwise set to -1
1172         if (Player_select_num_pilots == 0) {            
1173                 Player_select_pilot = -1;               
1174                 player_select_set_middle_text(XSTR( "Type Callsign and Press Enter", 381));
1175                 player_select_set_controls(1);          // gray out the controls
1176                 player_select_create_new_pilot();
1177         } else {
1178                 Player_select_pilot = 0;        
1179         }
1180 }
1181
1182 void player_select_draw_list()
1183 {
1184         int idx;
1185
1186         for (idx=0; idx<Player_select_max_lines[gr_screen.res]; idx++) {
1187                 // only draw as many pilots as we have
1188                 if ((idx + Player_select_list_start) == Player_select_num_pilots)
1189                         break;
1190
1191                 // if the currently selected pilot is this line, draw it highlighted
1192                 if ( (idx + Player_select_list_start) == Player_select_pilot) {
1193                         // if he's the active pilot and is also the current selection, super-highlight him                                                                      
1194                         gr_set_color_fast(&Color_text_active);
1195                 }
1196                 // otherwise draw him normally
1197                 else {
1198                         gr_set_color_fast(&Color_text_normal);
1199                 }
1200                 
1201                 // draw the actual callsign
1202                 gr_printf(Choose_list_coords[gr_screen.res][0], Choose_list_coords[gr_screen.res][1] + (idx * gr_get_font_height()), Pilots[idx + Player_select_list_start]);
1203         }
1204 }
1205
1206 void player_select_process_noninput(int k)
1207 {
1208         int idx;
1209         
1210         // check for pressed buttons
1211         for (idx=0; idx<NUM_PLAYER_SELECT_BUTTONS; idx++) {
1212                 if (Player_select_buttons[gr_screen.res][idx].button.pressed()) {
1213                         player_select_button_pressed(idx);
1214                 }
1215         }       
1216
1217         // check for keypresses
1218         switch (k) {                    
1219         // quit the game entirely
1220         case KEY_ESC:
1221                 gameseq_post_event(GS_EVENT_QUIT_GAME);
1222                 break;
1223
1224         case KEY_ENTER | KEY_CTRLED:
1225                 player_select_button_pressed(ACCEPT_BUTTON);
1226                 break;
1227
1228         // delete the currently highlighted pilot
1229         case KEY_DELETE:
1230                 if (Player_select_pilot >= 0) {
1231                         int ret;
1232
1233                         // display a popup requesting confirmation
1234                         ret = popup(PF_USE_AFFIRMATIVE_ICON | PF_USE_NEGATIVE_ICON,2,POPUP_NO,POPUP_YES,XSTR( "Are you sure you want to delete this pilot?", 383));                                                                             
1235
1236                         // delete the pilot
1237                         if(ret == 1){
1238                                 player_select_delete_pilot();
1239                         } 
1240                 }
1241                 break;  
1242         }
1243
1244         // check to see if the user has clicked on the "list region" button
1245         // and change the selected pilot appropriately
1246         if (Player_select_list_region.pressed()) {
1247                 int click_y;
1248                 // get the mouse position
1249                 Player_select_list_region.get_mouse_pos(NULL, &click_y);
1250                 
1251                 // determine what index to select
1252                 //idx = (click_y+5) / 10;
1253                 idx = click_y / gr_get_font_height();
1254
1255
1256                 // if he selected a valid item
1257                 if(((idx + Player_select_list_start) < Player_select_num_pilots) && (idx >= 0)){
1258                         Player_select_pilot = idx + Player_select_list_start;                   
1259                 }
1260         }
1261
1262         // if the player has double clicked on a valid pilot, choose it and hit the accept button
1263         if (Player_select_list_region.double_clicked()) {
1264                 if ((Player_select_pilot >= 0) && (Player_select_pilot < Player_select_num_pilots)) {
1265                         player_select_button_pressed(ACCEPT_BUTTON);
1266                 }
1267         }
1268 }
1269
1270 void player_select_process_input(int k)
1271 {
1272         char buf[CALLSIGN_LEN + 1];
1273         int idx,z;
1274         
1275         // if the player is in the process of typing in a new pilot name...
1276         switch (k) {
1277         // cancel create pilot
1278         case KEY_ESC:
1279                 player_select_cancel_create();          
1280                 break;
1281
1282         // accept a new pilot name
1283         case KEY_ENTER:
1284                 Player_select_input_box.get_text(buf);
1285                 drop_white_space(buf);
1286                 z = 0;
1287                 if (!isalpha(*buf)) {
1288                         z = 1;
1289                 } else {
1290                         for (idx=1; buf[idx]; idx++) {
1291                                 if (!isalpha(buf[idx]) && !isdigit(buf[idx]) && !strchr(VALID_PILOT_CHARS, buf[idx])) {
1292                                         z = 1;
1293                                         break;
1294                                 }
1295                         }
1296                 }
1297
1298                 for (idx=1; idx<Player_select_num_pilots; idx++) {
1299                         if (!stricmp(buf, Pilots[idx])) {
1300                                 // verify if it is ok to overwrite the file
1301                                 if (pilot_verify_overwrite() == 1) {
1302                                         // delete the pilot and select the beginning of the list
1303                                         Player_select_pilot = idx;
1304                                         player_select_delete_pilot();
1305                                         Player_select_pilot = 0;
1306                                         idx = Player_select_num_pilots;
1307                                         z = 0;
1308
1309                                 } else
1310                                         z = 1;
1311
1312                                 break;
1313                         }
1314                 }
1315
1316                 if (!*buf || (idx < Player_select_num_pilots)) {
1317                         z = 1;
1318                 }
1319
1320                 if (z) {
1321                         gamesnd_play_iface(SND_GENERAL_FAIL);
1322                         break;
1323                 }               
1324
1325                 // Create the new pilot, and write out his file
1326                 strcpy(Pilots[0], buf);
1327
1328                 // if this is the first guy, we should set the Player struct
1329                 if (Player == NULL) {
1330                         Player = &Players[0];
1331                         memset(Player, 0, sizeof(player));
1332                         Player->flags |= PLAYER_FLAGS_STRUCTURE_IN_USE;
1333                 }
1334
1335                 strcpy(Player->callsign, buf);
1336                 init_new_pilot(Player, !Player_select_clone_flag);
1337
1338                 // set him as being a multiplayer pilot if we're in the correct mode
1339                 if (Player_select_mode == PLAYER_SELECT_MODE_MULTI) {
1340                         Player->flags |= PLAYER_FLAGS_IS_MULTI;
1341                         Player->stats.flags |= STATS_FLAG_MULTIPLAYER;
1342                 }
1343
1344                 // create his pilot file
1345                 write_pilot_file(Player);
1346
1347                 // unset the player
1348                 memset(Player, 0, sizeof(player));
1349                 Player = NULL;
1350
1351                 // make this guy the selected pilot and put him first on the list
1352                 Player_select_pilot = 0;
1353                                 
1354                 // unset the input mode
1355                 player_select_set_input_mode(0);
1356
1357                 // clear any pending bottom text
1358                 player_select_set_bottom_text("");              
1359
1360                 // clear any pending middle text
1361                 player_select_set_middle_text("");
1362                                 
1363                 // ungray all the controls
1364                 player_select_set_controls(0);
1365
1366                 // evaluate whether or not this is the very first pilot
1367                 player_select_eval_very_first_pilot();
1368                 break;
1369
1370         case 0:
1371                 break;
1372
1373         // always kill middle text when a char is pressed in input mode
1374         default:
1375                 player_select_set_middle_text("");
1376                 break;
1377         }
1378 }
1379     
1380 // draw copyright message on the bottom on the screen
1381 void player_select_display_copyright()
1382 {
1383         int     sx, sy, w;
1384         char    Copyright_msg1[256], Copyright_msg2[256];
1385
1386 #ifdef MAKE_FS1
1387         gr_set_color_fast(&Color_bright);
1388
1389         if (Lcl_gr) {
1390                 sprintf(Copyright_msg1, XSTR("Descent: FreeSpace - The Great War, Copyright %c 1998, Volition, Inc.", 384), '\xA8');
1391         } else {
1392                 sprintf(Copyright_msg1, XSTR("Descent: FreeSpace - The Great War, Copyright %c 1998, Volition, Inc.", 384), '\x83');
1393         }
1394         sprintf(Copyright_msg2, XSTR("All Rights Reserved", 385));
1395 #else
1396         gr_set_color_fast(&Color_white);
1397
1398         sprintf(Copyright_msg1, NOX("FreeSpace 2"));
1399         if (Lcl_gr) {
1400                 sprintf(Copyright_msg2, XSTR("Copyright %c 1999, Volition, Inc.  All rights reserved.", 385), '\xA8');
1401         } else {
1402                 sprintf(Copyright_msg2, XSTR("Copyright %c 1999, Volition, Inc.  All rights reserved.", 385), '\x83');
1403         }
1404 #endif // MAKE_FS1
1405
1406         gr_get_string_size(&w, NULL, Copyright_msg1);
1407         sx = fl2i((gr_screen.max_w / 2) - w/2.0f + 0.5f);
1408         sy = (gr_screen.max_h - 2) - 2*gr_get_font_height();
1409         gr_string(sx, sy, Copyright_msg1);
1410
1411         gr_get_string_size(&w, NULL, Copyright_msg2);
1412         sx = fl2i((gr_screen.max_w / 2) - w/2.0f + 0.5f);
1413         sy = (gr_screen.max_h - 2) - gr_get_font_height();
1414         gr_string(sx, sy, Copyright_msg2);
1415 }
1416
1417 void player_select_display_all_text()
1418 {
1419         int w, h;
1420
1421         // only draw if we actually have a valid string
1422         if (strlen(Player_select_bottom_text)) {
1423                 gr_get_string_size(&w, &h, Player_select_bottom_text);
1424         
1425                 w = (gr_screen.max_w - w) / 2;
1426 #ifdef MAKE_FS1
1427                 gr_set_color_fast(&Color_bright);
1428 #else
1429                 gr_set_color_fast(&Color_bright_white);
1430 #endif
1431                 gr_printf(w, Player_select_bottom_text_y[gr_screen.res], Player_select_bottom_text);
1432         }
1433
1434         // only draw if we actually have a valid string
1435         if (strlen(Player_select_middle_text)) {
1436                 gr_get_string_size(&w, &h, Player_select_middle_text);
1437         
1438                 w = (gr_screen.max_w - w) / 2;
1439                 gr_set_color_fast(&Color_bright_white);
1440                 gr_printf(w, Player_select_middle_text_y[gr_screen.res], Player_select_middle_text);
1441         }
1442 }
1443
1444 int player_select_pilot_file_filter(char *filename)
1445 {
1446         return !verify_pilot_file(filename, Player_select_mode == PLAYER_SELECT_MODE_SINGLE);
1447 }
1448
1449 void player_select_set_bottom_text(char *txt)
1450 {
1451         if (txt) {
1452                 strncpy(Player_select_bottom_text, txt, 149);
1453         }
1454 }
1455
1456 void player_select_set_middle_text(char *txt)
1457 {
1458         if (txt) {
1459                 strncpy(Player_select_middle_text, txt, 149);
1460         }
1461 }
1462
1463 void player_select_eval_very_first_pilot()
1464 {       
1465         // never bring up the initial main hall help overlay
1466         // Player_select_very_first_pilot = 0;
1467
1468         // if we already have this flag set, check to see if our callsigns match
1469         if(Player_select_very_first_pilot){
1470                 // if the callsign has changed, unset the flag
1471                 if(strcmp(Player_select_very_first_pilot_callsign,Pilots[Player_select_pilot])){
1472                         Player_select_very_first_pilot = 0;
1473                 }
1474         }
1475         // otherwise check to see if there is only 1 pilot
1476         else {
1477                 if((Player_select_num_pilots == 1) && (Player_select_initial_count == 0)){
1478                         // set up the data
1479                         Player_select_very_first_pilot = 1;
1480                         strcpy(Player_select_very_first_pilot_callsign,Pilots[Player_select_pilot]);
1481                 }
1482         }
1483 }
1484
1485 void player_select_commit()
1486 {
1487         // if we've gotten to this point, we should have ensured this was the case
1488         Assert(Player_select_num_pilots > 0);
1489         
1490         gameseq_post_event(GS_EVENT_MAIN_MENU);
1491         gamesnd_play_iface(SND_COMMIT_PRESSED);
1492
1493         // evaluate if this is the _very_ first pilot
1494         player_select_eval_very_first_pilot();
1495
1496
1497 void player_select_cancel_create()
1498 {
1499         int idx;
1500
1501         Player_select_num_pilots--;
1502
1503         // make sure we correct the Selected_pilot index to account for the cancelled action
1504         if (Player_select_num_pilots == 0) {
1505                 Player_select_pilot = -1;
1506         }
1507
1508         // move all pilots down
1509         for (idx=0; idx<Player_select_num_pilots; idx++) {
1510                 strcpy(Pilots[idx], Pilots[idx + 1]);
1511         }
1512
1513         // unset the input mode
1514         player_select_set_input_mode(0);
1515
1516         // clear any bottom text
1517         player_select_set_bottom_text("");
1518
1519         // clear any middle text
1520         player_select_set_middle_text("");
1521
1522         // ungray all controls
1523         player_select_set_controls(0);
1524
1525         // disable the autoaccept
1526         Player_select_autoaccept = 0;
1527 }
1528
1529 DCF(bastion,"Sets the player to be on the bastion")
1530 {
1531         if(gameseq_get_state() == GS_STATE_INITIAL_PLAYER_SELECT){
1532                 Player_select_force_bastion = 1;
1533                 dc_printf("Player is now in the Bastion\n");
1534         }
1535 }
1536
1537 #define MAX_PLAYER_TIPS                 40
1538
1539 char *Player_tips[MAX_PLAYER_TIPS];
1540 int Num_player_tips;
1541 int Player_tips_shown = 0;
1542
1543 // tooltips
1544 void player_tips_init()
1545 {
1546 #ifndef MAKE_FS1
1547         Num_player_tips = 0;
1548
1549         // begin external localization stuff
1550         lcl_ext_open();
1551
1552         read_file_text("tips.tbl");
1553         reset_parse();
1554
1555         while(!optional_string("#end")){
1556                 required_string("+Tip:");
1557
1558                 if(Num_player_tips >= MAX_PLAYER_TIPS){
1559                         break;
1560                 }
1561                 Player_tips[Num_player_tips++] = stuff_and_malloc_string(F_NAME, NULL, 1024);                           
1562         }
1563
1564         // stop externalizing, homey
1565         lcl_ext_close();
1566 #endif
1567 }
1568
1569 void player_tips_close()
1570 {
1571 #ifndef MAKE_FS1
1572         int i;
1573         
1574         for (i=0; i<MAX_PLAYER_TIPS; i++) {
1575                 if (Player_tips[i]) {
1576                         free(Player_tips[i]);
1577                         Player_tips[i] = NULL;
1578                 }
1579         }
1580 #endif
1581 }
1582
1583 void player_tips_popup()
1584 {
1585 #ifndef MAKE_FS1
1586         int tip, ret;   
1587         
1588         // player has disabled tips
1589         if((Player != NULL) && !Player->tips){
1590                 return;
1591         }
1592         // only show tips once per instance of Freespace
1593         if(Player_tips_shown == 1){
1594                 return;
1595         }
1596         Player_tips_shown = 1;
1597
1598         // randomly pick one
1599         tip = (int)frand_range(0.0f, (float)Num_player_tips - 1.0f);
1600
1601         char all_txt[2048];     
1602
1603         do {
1604                 sprintf(all_txt, XSTR("NEW USER TIP\n\n%s", 1565), Player_tips[tip]);
1605                 ret = popup(PF_NO_SPECIAL_BUTTONS | PF_TITLE | PF_TITLE_WHITE, 3, XSTR("&Ok", 669), XSTR("&Next", 1444), XSTR("Don't show me this again", 1443), all_txt);
1606                 
1607                 // now what?
1608                 switch(ret){
1609                 // next
1610                 case 1:
1611                         if(tip >= Num_player_tips - 1){
1612                                 tip = 0;
1613                         } else {
1614                                 tip++;
1615                         }
1616                         break;
1617
1618                 // don't show me this again
1619                 case 2:
1620                         ret = 0;
1621                         Player->tips = 0;
1622                         write_pilot_file(Player);
1623                         break;
1624                 }
1625         } while(ret > 0);
1626 #endif
1627 }