]> icculus.org git repositories - taylor/freespace2.git/blob - src/menuui/playermenu.cpp
use SDL_arraysize() instead of sizeof() where appropriate
[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 const 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 const char *Player_select_background_bitmap_name[GR_NUM_RESOLUTIONS] = {
270         "ChoosePilot",
271         "2_ChoosePilot"
272 };
273 const 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         const 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(const 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
358 // flag indicating if this is the absolute first pilot created and selected. Used to determine
359 // if the main hall should display the help overlay screen
360 int Player_select_very_first_pilot = 0;                 
361 int Player_select_initial_count = 0;
362 char Player_select_very_first_pilot_callsign[CALLSIGN_LEN + 2];
363
364 extern int Main_hall_bitmap;                                                                    // bitmap handle to the main hall bitmap
365
366 int Player_select_mode;                                                                                 // single or multiplayer - never set directly. use player_select_init_player_stuff()
367 int Player_select_num_pilots;                                                                   // # of pilots on the list
368 int Player_select_list_start;                                                                   // index of first list item to start displaying in the box
369 int Player_select_pilot;                                                                            // index into the Pilot array of which is selected as the active pilot
370 int Player_select_input_mode;                                                                   // 0 if the player _isn't_ typing a callsign, 1 if he is
371 char Pilots_arr[MAX_PILOTS][MAX_FILENAME_LEN];          
372 char *Pilots[MAX_PILOTS];
373 int Player_select_clone_flag;                                                                   // clone the currently selected pilot
374 char Player_select_last_pilot[CALLSIGN_LEN + 10];               // callsign of the last used pilot, or none if there wasn't one
375 int Player_select_last_is_multi;
376
377 int Player_select_force_bastion = 0;
378
379 // notification text areas
380
381 static int Player_select_bottom_text_y[GR_NUM_RESOLUTIONS] = {
382 #ifdef MAKE_FS1
383         280,
384 #else
385         314,    // GR_640
386 #endif
387         502     // GR_1024
388 };
389
390 static int Player_select_middle_text_y[GR_NUM_RESOLUTIONS] = {
391 #ifdef MAKE_FS1
392         280,
393 #else
394         253,    // GR_640
395 #endif
396         404     // GR_1024
397 };
398
399 char Player_select_bottom_text[150] = "";
400 char Player_select_middle_text[150] = "";
401 void player_select_set_bottom_text(const char *txt);
402 void player_select_set_middle_text(const char *txt);
403
404
405 // FORWARD DECLARATIONS
406 void player_select_init_player_stuff(int mode);                 // switch between single and multiplayer modes
407 void player_select_set_input_mode(int n);                                       
408 void player_select_button_pressed(int n);
409 void player_select_scroll_list_up();
410 void player_select_scroll_list_down();
411 int player_select_create_new_pilot();
412 void player_select_delete_pilot();
413 void player_select_display_all_text();
414 void player_select_display_copyright();
415 void player_select_set_controls(int gray);
416 void player_select_draw_list();
417 void player_select_process_noninput(int k);
418 void player_select_process_input(int k);
419 int player_select_pilot_file_filter(const char *filename);
420 int player_select_get_last_pilot_info();
421 void player_select_eval_very_first_pilot();
422 void player_select_commit();
423 void player_select_cancel_create();
424
425
426 // basically, gray out all controls (gray == 1), or ungray the controls (gray == 0) 
427 void player_select_set_controls(int gray)
428 {
429         int idx;
430         
431         for(idx=0;idx<NUM_PLAYER_SELECT_BUTTONS;idx++){
432                 if(gray){
433                         Player_select_buttons[gr_screen.res][idx].button.disable();
434                 } else {
435                         Player_select_buttons[gr_screen.res][idx].button.enable();
436                 }
437         }
438 }
439
440 // functions for selecting single/multiplayer pilots at the very beginning of Freespace
441 void player_select_init()
442 {                       
443         int i;
444         barracks_buttons *b;   
445 #ifndef MAKE_FS1
446         UI_WINDOW *w;
447 #endif
448
449         // start a looping ambient sound
450         main_hall_start_ambient();
451
452         Player_select_force_bastion = 0;
453
454 #ifdef FS2_DEMO
455         /*
456         Demo_title_bitmap = bm_load(Demo_title_bitmap_filename);
457         if ( Demo_title_bitmap >= 0 ) {
458                 Demo_title_active = 1;
459                 Demo_title_expire_timestamp = timestamp(5000);
460         } else {
461                 Demo_title_active = 0;
462         }
463         */
464         Demo_title_active = 0;
465 #endif
466
467         // create the UI window
468         Player_select_window.create(0, 0, gr_screen.max_w, gr_screen.max_h, 0);
469         Player_select_window.set_mask_bmap(Player_select_background_mask_bitmap[gr_screen.res]);
470
471 #ifdef MAKE_FS1
472         common_set_interface_palette("ChoosePilotPalette");
473 #endif
474
475         // initialize the control buttons
476         for (i=0; i<NUM_PLAYER_SELECT_BUTTONS; i++) {
477                 b = &Player_select_buttons[gr_screen.res][i];
478
479                 // create the button
480                 if ( (i == SCROLL_LIST_UP_BUTTON) || (i == SCROLL_LIST_DOWN_BUTTON) )
481                         b->button.create(&Player_select_window, NULL, b->x, b->y, 60, 30, 1, 1);
482                 else
483                         b->button.create(&Player_select_window, NULL, b->x, b->y, 60, 30, 1, 1);
484
485                 // set its highlight action
486                 b->button.set_highlight_action(common_play_highlight_sound);
487
488                 // set its animation bitmaps
489                 b->button.set_bmaps(b->filename);
490
491                 // link the mask hotspot
492                 b->button.link_hotspot(b->hotspot);
493         }               
494
495 #ifndef MAKE_FS1
496         // add some text
497         w = &Player_select_window;      
498         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);        
499         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);      
500         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);  
501         
502         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);   
503         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);  
504         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);      
505         for(i=0; i<PLAYER_SELECT_NUM_TEXT; i++) {
506                 w->add_XSTR(&Player_select_text[gr_screen.res][i]);
507         }
508 #endif
509
510         // create the list button text select region
511         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);
512         Player_select_list_region.hide();
513
514         // create the pilot callsign input box
515         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);
516         Player_select_input_box.set_valid_chars(VALID_PILOT_CHARS);
517         Player_select_input_box.hide();
518         Player_select_input_box.disable();
519         
520         // not currently entering any text
521         Player_select_input_mode = 0;   
522
523         // set up hotkeys for buttons so we draw the correct animation frame when a key is pressed
524         Player_select_buttons[gr_screen.res][SCROLL_LIST_UP_BUTTON].button.set_hotkey(SDLK_UP);
525         Player_select_buttons[gr_screen.res][SCROLL_LIST_DOWN_BUTTON].button.set_hotkey(SDLK_DOWN);
526         Player_select_buttons[gr_screen.res][ACCEPT_BUTTON].button.set_hotkey(SDLK_RETURN);
527         Player_select_buttons[gr_screen.res][CREATE_PILOT_BUTTON].button.set_hotkey(SDLK_c);
528
529         // disable the single player button in the multiplayer beta
530 #ifdef MULTIPLAYER_BETA_BUILD
531         Player_select_buttons[gr_screen.res][SINGLE_BUTTON].button.hide();
532         Player_select_buttons[gr_screen.res][SINGLE_BUTTON].button.disable();
533 #elif defined(E3_BUILD) || defined(PRESS_TOUR_BUILD)
534         Player_select_buttons[gr_screen.res][MULTI_BUTTON].button.hide();
535         Player_select_buttons[gr_screen.res][MULTI_BUTTON].button.disable();
536 #endif
537
538
539         // attempt to load in the background bitmap
540         Player_select_background_bitmap = bm_load(Player_select_background_bitmap_name[gr_screen.res]);                         
541         SDL_assert(Player_select_background_bitmap >= 0);       
542
543         // load in the palette for the screen
544         // Player_select_palette = bm_load(PLAYER_SELECT_PALETTE);
545         // Player_select_palette_set = 0;
546
547         // unset the very first pilot data
548         Player_select_very_first_pilot = 0;
549         Player_select_initial_count = -1;
550         memset(Player_select_very_first_pilot_callsign, 0, CALLSIGN_LEN + 2);   
551
552 //      if(Player_select_num_pilots == 0){
553 //              Player_select_autoaccept = 1;
554 //      }
555                 
556         // if we found a pilot
557 #if defined(DEMO) || defined(OEM_BUILD) || defined(E3_BUILD) || defined(PRESS_TOUR_BUILD) // not for FS2_DEMO
558         player_select_init_player_stuff(PLAYER_SELECT_MODE_SINGLE);     
559 #elif defined(MULTIPLAYER_BETA_BUILD)
560         player_select_init_player_stuff(PLAYER_SELECT_MODE_MULTI);      
561 #else
562         if (player_select_get_last_pilot_info()) {
563                 if (Player_select_last_is_multi) {
564                         player_select_init_player_stuff(PLAYER_SELECT_MODE_MULTI);
565                 } else {
566                         player_select_init_player_stuff(PLAYER_SELECT_MODE_SINGLE);
567                 }
568         } 
569         // otherwise go to the single player mode by default
570         else {
571                 player_select_init_player_stuff(PLAYER_SELECT_MODE_SINGLE);
572         }
573 #endif  
574
575         if((Player_select_num_pilots == 1) && Player_select_input_mode){
576                 Player_select_autoaccept = 1;
577         }       
578 }
579
580 #if defined(FS2_DEMO) || defined(FS1_DEMO)
581 // Display the demo title screen
582 void demo_title_blit()
583 {
584         int k;
585
586         Mouse_hidden = 1;
587
588         if ( timestamp_elapsed(Demo_title_expire_timestamp) ) {
589                 Demo_title_active = 0;
590         }
591
592         k = game_poll();
593         if ( k > 0 ) {
594                 Demo_title_active = 0;
595         }
596
597         if ( Demo_title_need_fade_in ) {
598                 gr_fade_out(0);
599         }
600         
601         gr_set_bitmap(Demo_title_bitmap);
602         gr_bitmap(0,0);
603
604         gr_flip();
605
606         if ( Demo_title_need_fade_in ) {
607                 gr_fade_in(0);
608                 Demo_title_need_fade_in = 0;
609         }
610
611         if ( !Demo_title_active ) {
612                 gr_fade_out(0);
613                 Mouse_hidden = 0;
614         }
615 }
616
617 #endif
618
619 void player_select_do()
620 {
621         int k;
622
623 #ifdef FS2_DEMO
624         if ( Demo_title_active ) {
625                 // demo_title_blit();
626                 return;
627         }
628 #endif
629
630         // set the input box at the "virtual" line 0 to be active so the player can enter a callsign
631         if (Player_select_input_mode){
632                 Player_select_input_box.set_focus();
633         }
634
635         // process any ui window stuff
636         k = Player_select_window.process();
637         if(k){
638                 extern void game_process_cheats(int k);
639                 game_process_cheats(k);
640         }
641         switch(k){
642         // switch between single and multiplayer modes
643         case SDLK_TAB :
644 #if defined(DEMO) || defined(OEM_BUILD) // not for FS2_DEMO
645                 break;
646 #else
647
648                 if(Player_select_input_mode){
649                         gamesnd_play_iface(SND_GENERAL_FAIL);
650                         break;
651                 }
652                 // play a little sound
653                 gamesnd_play_iface(SND_USER_SELECT);
654                 if(Player_select_mode == PLAYER_SELECT_MODE_MULTI){                                     
655 #ifdef MAKE_FS1
656                         player_select_set_bottom_text(XSTR( "Single Player Mode", 376));
657 #else
658                         player_select_set_bottom_text(XSTR( "Single-Player Mode", 376));
659 #endif
660                                 
661                         // reinitialize as single player mode
662                         player_select_init_player_stuff(PLAYER_SELECT_MODE_SINGLE);
663                 } else if(Player_select_mode == PLAYER_SELECT_MODE_SINGLE){                                                                             
664                         player_select_set_bottom_text(XSTR( "Multiplayer Mode", 377));
665                                 
666                         // reinitialize as multiplayer mode
667                         player_select_init_player_stuff(PLAYER_SELECT_MODE_MULTI);
668                 }
669                 break;  
670 #endif
671         }       
672
673         // draw the player select pseudo-dialog over it
674         gr_set_bitmap(Player_select_background_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
675         gr_bitmap(0,0);
676
677         // press the accept button
678         if (Player_select_autoaccept) {
679                 Player_select_buttons[gr_screen.res][ACCEPT_BUTTON].button.press_button();
680         }
681         
682         // draw any ui window stuf
683         Player_select_window.draw();
684
685         // light up the correct mode button (single or multi)
686         if (Player_select_mode == PLAYER_SELECT_MODE_SINGLE){
687                 Player_select_buttons[gr_screen.res][SINGLE_BUTTON].button.draw_forced(2);
688         } else {
689                 Player_select_buttons[gr_screen.res][MULTI_BUTTON].button.draw_forced(2);
690         }
691
692         // draw the pilot list text
693         player_select_draw_list();      
694
695         // draw copyright message on the bottom on the screen
696         player_select_display_copyright();
697
698         if (!Player_select_input_mode) {
699                 player_select_process_noninput(k);
700         } else {
701                 player_select_process_input(k);
702         }
703         
704         // draw any pending messages on the bottom or middle of the screen
705         player_select_display_all_text();       
706
707 #ifndef RELEASE_REAL
708         // gr_set_color_fast(&Color_bright_green);
709         // gr_string(0x8000, 10, "Development version - DO NOT RELEASE");
710 #endif
711         
712         /*
713         gr_set_color(255, 0, 0);
714         vector whee[5];
715         vector *arr[5] = {&whee[0], &whee[1], &whee[2], &whee[3], &whee[4]};
716         whee[0].x = 10; whee[0].y = 10; whee[0].z = 0.0f;
717         whee[1].x = 50; whee[1].y = 50; whee[1].z = 0.0f;
718         whee[2].x = 50; whee[2].y = 90; whee[2].z = 0.0f;
719         whee[3].x = 90; whee[3].y = 130; whee[3].z = 0.0f;
720         whee[4].x = 180; whee[4].y = 130; whee[4].z = 0.0f;
721         gr_pline_special(arr, 5, 2);
722         */
723         
724
725         gr_flip();
726 }
727
728 void player_select_close()
729 {
730         // destroy the player select window
731         Player_select_window.destroy();
732
733         // if we're in input mode - we should undo the pilot create reqeust
734         if(Player_select_input_mode){
735                 player_select_cancel_create();
736         }
737         
738         // actually set up the Player struct here       
739         if((Player_select_pilot == -1) || (Player_select_num_pilots == 0)){
740                 nprintf(("General","WARNING! No pilot selected! We should be exiting the game now!\n"));
741                 return;
742         }
743
744         // unload all bitmaps
745         if(Player_select_background_bitmap >= 0){
746                 bm_release(Player_select_background_bitmap);
747                 Player_select_background_bitmap = -1;
748         } 
749         // if(Player_select_palette >= 0){
750         //      bm_release(Player_select_palette);
751                 //Player_select_palette = -1;
752         // }
753
754 #ifdef MAKE_FS1
755         common_free_interface_palette();
756 #endif
757                         
758         // setup the player  struct
759         Player_num = 0;
760         Player = &Players[0];
761         Player->flags |= PLAYER_FLAGS_STRUCTURE_IN_USE;
762                 
763         // now read in a the pilot data
764         if (read_pilot_file(Pilots[Player_select_pilot], !Player_select_mode, Player) != 0) {
765                 Error(LOCATION,"Couldn't load pilot file, bailing");
766                 Player = NULL;
767                 return;
768         }               
769
770         if (Player_select_force_bastion) {
771                 Player->on_bastion = 1;
772         }
773 }
774
775 void player_select_set_input_mode(int n)
776 {
777         int i;
778
779         // set the input mode
780         Player_select_input_mode = n;   
781         
782         // enable all the player select buttons
783         for (i=0; i<NUM_PLAYER_SELECT_BUTTONS; i++){
784                 Player_select_buttons[gr_screen.res][i].button.enable(!n);
785         }
786
787         Player_select_buttons[gr_screen.res][ACCEPT_BUTTON].button.set_hotkey(n ? -1 : SDLK_RETURN);
788         Player_select_buttons[gr_screen.res][CREATE_PILOT_BUTTON].button.set_hotkey(n ? -1 : SDLK_c);
789
790         // enable the player select input box
791         if(Player_select_input_mode){
792                 Player_select_input_box.enable();
793                 Player_select_input_box.unhide();
794         } else {
795                 Player_select_input_box.hide();
796                 Player_select_input_box.disable();
797         }
798 }
799
800 void player_select_button_pressed(int n)
801 {
802         int ret;
803
804         switch (n) {
805         case SCROLL_LIST_UP_BUTTON:
806                 player_select_set_bottom_text("");
807
808                 player_select_scroll_list_up();
809                 break;
810
811         case SCROLL_LIST_DOWN_BUTTON:
812                 player_select_set_bottom_text("");
813
814                 player_select_scroll_list_down();
815                 break;
816
817         case ACCEPT_BUTTON:
818                 // make sure he has a valid pilot selected
819                 if (Player_select_pilot < 0) {                                                          
820                         popup(PF_USE_AFFIRMATIVE_ICON,1,POPUP_OK,XSTR( "You must select a valid pilot first", 378));
821                 } else {
822                         player_select_commit();                         
823                 }
824                 break;
825
826         case CLONE_BUTTON:
827                 // if we're at max-pilots, don't allow another to be added
828                 if (Player_select_num_pilots >= MAX_PILOTS) {
829                         player_select_set_bottom_text(XSTR( "You already have the maximum # of pilots!", 379));
830                         
831                         gamesnd_play_iface(SND_GENERAL_FAIL);
832                         break;
833                 }
834
835                 if (Player_select_pilot >= 0) {                                         
836                         // first we have to make sure this guy is actually loaded for when we create the clone
837                         if (Player == NULL) {
838                                 Player = &Players[0];
839                                 Player->flags |= PLAYER_FLAGS_STRUCTURE_IN_USE;
840                         }                               
841
842                         // attempt to read in the pilot file of the guy to be cloned
843                         if (read_pilot_file(Pilots[Player_select_pilot], !Player_select_mode, Player) != 0) {
844                                 Error(LOCATION,"Couldn't load pilot file, bailing");
845                                 Player = NULL;
846                                 Int3();
847                         }                               
848
849                         // set the clone flag
850                         Player_select_clone_flag = 1;
851
852                         // create the new pilot (will be cloned with Player_select_clone_flag_set)
853                         if (!player_select_create_new_pilot()) {                                        
854                                 player_select_set_bottom_text(XSTR( "Error creating new pilot file!", 380));
855                                 Player_select_clone_flag = 0;
856                                 memset(Player,0,sizeof(player));
857                                 Player = NULL;
858                                 break;
859                         }                               
860
861                         // clear the player out
862                         // JH: What the hell?  How do you clone a pilot if you clear out the source you are copying
863                         // from?  These next 2 lines are pure stupidity, so I commented them out!
864 //                      memset(Player,0,sizeof(player));
865 //                      Player = NULL;
866                                 
867                         // display some text on the bottom of the dialog
868                         player_select_set_bottom_text(XSTR( "Type Callsign and Press Enter", 381));                             
869                         
870                         // gray out all controls in the dialog
871                         player_select_set_controls(1);                                  
872                 }
873                 break;
874
875         case CREATE_PILOT_BUTTON:
876                 // if we're at max-pilots, don't allow another to be added
877                 if(Player_select_num_pilots >= MAX_PILOTS){
878                         player_select_set_bottom_text(XSTR( "You already have the maximum # of pilots!", 379));
879
880                         gamesnd_play_iface(SND_GENERAL_FAIL);
881                         break;
882                 }
883
884                 // create a new pilot
885                 if (!player_select_create_new_pilot()) {
886                         player_select_set_bottom_text(XSTR( "Type Callsign and Press Enter", 381));
887                 }
888
889                 // don't clone anyone
890                 Player_select_clone_flag = 0;
891                         
892                 // display some text on the bottom of the dialog                        
893                 player_select_set_bottom_text(XSTR( "Type Callsign and Press Enter", 381));
894                         
895                 // gray out all controls
896                 player_select_set_controls(1);                                          
897                 break;
898
899         case DELETE_BUTTON:
900                 player_select_set_bottom_text("");
901
902                 if (Player_select_pilot >= 0) {
903                         // display a popup requesting confirmation
904                         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));
905
906                         // delete the pilot
907                         if(ret == 1){
908                                 player_select_delete_pilot();
909                         } 
910                 }
911                 break;
912
913         case SINGLE_BUTTON:
914                 player_select_set_bottom_text("");
915
916                 Player_select_autoaccept = 0;
917                 // switch to single player mode
918                 if (Player_select_mode != PLAYER_SELECT_MODE_SINGLE) {
919                         // play a little sound
920                         gamesnd_play_iface(SND_USER_SELECT);
921                                 
922                         player_select_set_bottom_text(XSTR( "Single Player Mode", 376));
923                                 
924                         // reinitialize as single player mode
925                         player_select_init_player_stuff(PLAYER_SELECT_MODE_SINGLE);
926                 } else {
927                         gamesnd_play_iface(SND_GENERAL_FAIL);
928                 }
929                 break;
930
931         case MULTI_BUTTON:
932                 player_select_set_bottom_text("");
933
934                 Player_select_autoaccept = 0;
935 #if defined(DEMO) || defined(OEM_BUILD) // not for FS2_DEMO
936                 game_feature_not_in_demo_popup();
937 #else
938                 // switch to multiplayer mode
939                 if (Player_select_mode != PLAYER_SELECT_MODE_MULTI) {
940                         // play a little sound
941                         gamesnd_play_iface(SND_USER_SELECT);
942                         
943                         player_select_set_bottom_text(XSTR( "Multiplayer Mode", 377));
944                                 
945                         // reinitialize as multiplayer mode
946                         player_select_init_player_stuff(PLAYER_SELECT_MODE_MULTI);
947                 } else {
948                         gamesnd_play_iface(SND_GENERAL_FAIL);
949                 }
950 #endif
951                 break;
952         }
953 }
954
955 int player_select_create_new_pilot()
956 {
957         int idx;
958
959         // make sure we haven't reached the max
960         if (Player_select_num_pilots >= MAX_PILOTS) {
961                 gamesnd_play_iface(SND_GENERAL_FAIL);
962                 return 0;
963         }
964
965         int play_scroll_sound = 1;
966
967 #if defined(FS2_DEMO) || defined(FS1_DEMO)
968         if ( Demo_title_active ) {
969                 play_scroll_sound = 0;
970         }
971 #endif
972
973         if ( play_scroll_sound ) {
974                 gamesnd_play_iface(SND_SCROLL);
975         }
976
977         idx = Player_select_num_pilots; 
978         
979         // move all the pilots in the list up
980         while (idx--) {
981                 SDL_strlcpy(Pilots[idx + 1], Pilots[idx], MAX_FILENAME_LEN);
982         }       
983
984         // by default, set the default netgame protocol to be VMT
985         Multi_options_g.protocol = NET_TCP;     
986
987         // select the beginning of the list
988         Player_select_pilot = 0;
989         Player_select_num_pilots++;
990         Pilots[Player_select_pilot][0] = 0;
991         Player_select_list_start= 0;
992
993         // set us to be in input mode
994         player_select_set_input_mode(1);
995         
996         // set the input box to have focus
997         Player_select_input_box.set_focus();
998         Player_select_input_box.set_text("");
999         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());      
1000
1001         return 1;
1002 }
1003
1004 void player_select_delete_pilot()
1005 {
1006         char filename[MAX_PATH_LEN + 1];
1007         int i;
1008
1009         // tack on the full path and the pilot file extension
1010         // build up the path name length
1011         // make sure we do this based upon whether we're in single or multiplayer mode
1012         SDL_strlcpy( filename, Pilots[Player_select_pilot], SDL_arraysize(filename) );
1013         SDL_strlcat( filename, NOX(".plr"), SDL_arraysize(filename) );
1014
1015         // attempt to delete the pilot
1016         if (Player_select_mode == PLAYER_SELECT_MODE_SINGLE) {
1017                 cf_delete( filename, CF_TYPE_SINGLE_PLAYERS );
1018         } else {
1019                 cf_delete( filename, CF_TYPE_MULTI_PLAYERS );
1020         }
1021
1022         // delete all the campaign save files for this pilot.
1023         mission_campaign_delete_all_savefiles( Pilots[Player_select_pilot], (Player_select_mode != PLAYER_SELECT_MODE_SINGLE) );
1024
1025         // move all the players down
1026         for (i=Player_select_pilot; i<Player_select_num_pilots-1; i++){
1027                 SDL_strlcpy(Pilots[i], Pilots[i + 1], MAX_FILENAME_LEN);
1028         }               
1029
1030         // correcly set the # of pilots and the currently selected pilot
1031         Player_select_num_pilots--;
1032         if (Player_select_pilot >= Player_select_num_pilots) {
1033                 Player_select_pilot = Player_select_num_pilots - 1;             
1034         }               
1035
1036 }
1037
1038 // scroll the list of players up
1039 void player_select_scroll_list_up()
1040 {
1041         if (Player_select_pilot == -1)
1042                 return;
1043
1044         // change the pilot selected index and play the appropriate sound
1045         if (Player_select_pilot) {
1046                 Player_select_pilot--;
1047                 gamesnd_play_iface(SND_SCROLL);
1048         } else {
1049                 gamesnd_play_iface(SND_GENERAL_FAIL);
1050         }
1051                 
1052         if (Player_select_pilot < Player_select_list_start){
1053                 Player_select_list_start = Player_select_pilot;
1054         }
1055 }
1056
1057 // scroll the list of players down
1058 void player_select_scroll_list_down()
1059 {       
1060         // change the pilot selected index and play the appropriate sound
1061         if (Player_select_pilot < Player_select_num_pilots - 1) {
1062                 Player_select_pilot++;
1063                 gamesnd_play_iface(SND_SCROLL);
1064         } else {
1065                 gamesnd_play_iface(SND_GENERAL_FAIL);
1066         }
1067                 
1068         if (Player_select_pilot >= (Player_select_list_start + Player_select_max_lines[gr_screen.res])){
1069                 Player_select_list_start++;
1070         }
1071 }
1072
1073 // fill in the data on the last played pilot (callsign and is_multi or not)
1074 int player_select_get_last_pilot_info()
1075 {
1076         const char *last_player;
1077
1078         last_player = os_config_read_string( NULL, "LastPlayer", NULL);
1079         
1080         if(last_player == NULL){
1081                 return 0;               
1082         } else {
1083                 SDL_strlcpy(Player_select_last_pilot, last_player, SDL_arraysize(Player_select_last_pilot));
1084         }
1085
1086         // determine if he was a single or multi-player based upon the last character in his callsign
1087         Player_select_last_is_multi = Player_select_last_pilot[strlen(Player_select_last_pilot)-1] == 'M' ? 1 : 0;
1088         Player_select_last_pilot[strlen(Player_select_last_pilot)-1]='\0';
1089
1090         return 1;       
1091 }
1092
1093 int player_select_get_last_pilot()
1094 {
1095         // if the player has the Cmdline_use_last_pilot command line option set, try and drop out quickly
1096         if(Cmdline_use_last_pilot){                     
1097                 int idx;                                
1098
1099                 if(!player_select_get_last_pilot_info()){
1100                         return 0;
1101                 }
1102
1103                 if(Player_select_last_is_multi){
1104                         Player_select_num_pilots = cf_get_file_list_preallocated(MAX_PILOTS, Pilots_arr, Pilots, CF_TYPE_MULTI_PLAYERS, NOX("*.plr"), CF_SORT_TIME);                            
1105                 } else {
1106                         Player_select_num_pilots = cf_get_file_list_preallocated(MAX_PILOTS, Pilots_arr, Pilots, CF_TYPE_SINGLE_PLAYERS, NOX("*.plr"), CF_SORT_TIME);                                           
1107                 }
1108
1109                 Player_select_pilot = -1;
1110
1111                 // pick the last player
1112                 for(idx=0;idx<Player_select_num_pilots;idx++){
1113                         if(strcmp(Player_select_last_pilot,Pilots_arr[idx])==0){
1114                                 Player_select_pilot = idx;
1115                                 break;
1116                         }
1117                 }               
1118
1119                 // set this so that we don't incorrectly create a "blank" pilot - .plr
1120                 // in the player_select_close() function
1121                 Player_select_num_pilots = 0;
1122
1123                 // if we've actually found a valid pilot, load him up           
1124                 if(Player_select_pilot != -1){
1125                         Player = &Players[0];                   
1126                         read_pilot_file(Pilots_arr[idx],!Player_select_last_is_multi,Player);
1127                         Player->flags |= PLAYER_FLAGS_STRUCTURE_IN_USE;
1128                         return 1;               
1129                 }                       
1130         } 
1131
1132         return 0;
1133 }
1134
1135 void player_select_init_player_stuff(int mode)
1136 {                       
1137         Player_select_list_start = 0;   
1138
1139         // set the select mode to single player for default
1140         Player_select_mode = mode;
1141
1142         // load up the list of players based upon the Player_select_mode (single or multiplayer)
1143         Get_file_list_filter = player_select_pilot_file_filter;
1144         if (mode == PLAYER_SELECT_MODE_SINGLE){
1145                 Player_select_num_pilots = cf_get_file_list_preallocated(MAX_PILOTS, Pilots_arr, Pilots, CF_TYPE_SINGLE_PLAYERS, NOX("*.plr"), CF_SORT_TIME);
1146         } else {
1147                 Player_select_num_pilots = cf_get_file_list_preallocated(MAX_PILOTS, Pilots_arr, Pilots, CF_TYPE_MULTI_PLAYERS, NOX("*.plr"), CF_SORT_TIME);
1148         }
1149
1150         Player = NULL;  
1151
1152         // if this value is -1, it means we should set it to the num pilots count
1153         if(Player_select_initial_count == -1){
1154                 Player_select_initial_count = Player_select_num_pilots;
1155         }
1156                 
1157         // select the first pilot if any exist, otherwise set to -1
1158         if (Player_select_num_pilots == 0) {            
1159                 Player_select_pilot = -1;               
1160                 player_select_set_middle_text(XSTR( "Type Callsign and Press Enter", 381));
1161                 player_select_set_controls(1);          // gray out the controls
1162                 player_select_create_new_pilot();
1163         } else {
1164                 Player_select_pilot = 0;        
1165         }
1166 }
1167
1168 void player_select_draw_list()
1169 {
1170         int idx;
1171
1172         for (idx=0; idx<Player_select_max_lines[gr_screen.res]; idx++) {
1173                 // only draw as many pilots as we have
1174                 if ((idx + Player_select_list_start) == Player_select_num_pilots)
1175                         break;
1176
1177                 // if the currently selected pilot is this line, draw it highlighted
1178                 if ( (idx + Player_select_list_start) == Player_select_pilot) {
1179                         // if he's the active pilot and is also the current selection, super-highlight him                                                                      
1180                         gr_set_color_fast(&Color_text_active);
1181                 }
1182                 // otherwise draw him normally
1183                 else {
1184                         gr_set_color_fast(&Color_text_normal);
1185                 }
1186                 
1187                 // draw the actual callsign
1188                 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]);
1189         }
1190 }
1191
1192 void player_select_process_noninput(int k)
1193 {
1194         int idx;
1195         
1196         // check for pressed buttons
1197         for (idx=0; idx<NUM_PLAYER_SELECT_BUTTONS; idx++) {
1198                 if (Player_select_buttons[gr_screen.res][idx].button.pressed()) {
1199                         player_select_button_pressed(idx);
1200                 }
1201         }       
1202
1203         // check for keypresses
1204         switch (k) {                    
1205         // quit the game entirely
1206         case SDLK_ESCAPE:
1207                 gameseq_post_event(GS_EVENT_QUIT_GAME);
1208                 break;
1209
1210         case SDLK_RETURN | KEY_CTRLED:
1211                 player_select_button_pressed(ACCEPT_BUTTON);
1212                 break;
1213
1214         // delete the currently highlighted pilot
1215         case SDLK_DELETE:
1216                 if (Player_select_pilot >= 0) {
1217                         int ret;
1218
1219                         // display a popup requesting confirmation
1220                         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));                                                                             
1221
1222                         // delete the pilot
1223                         if(ret == 1){
1224                                 player_select_delete_pilot();
1225                         } 
1226                 }
1227                 break;  
1228         }
1229
1230         // check to see if the user has clicked on the "list region" button
1231         // and change the selected pilot appropriately
1232         if (Player_select_list_region.pressed()) {
1233                 int click_y;
1234                 // get the mouse position
1235                 Player_select_list_region.get_mouse_pos(NULL, &click_y);
1236                 
1237                 // determine what index to select
1238                 //idx = (click_y+5) / 10;
1239                 idx = click_y / gr_get_font_height();
1240
1241
1242                 // if he selected a valid item
1243                 if(((idx + Player_select_list_start) < Player_select_num_pilots) && (idx >= 0)){
1244                         Player_select_pilot = idx + Player_select_list_start;                   
1245                 }
1246         }
1247
1248         // if the player has double clicked on a valid pilot, choose it and hit the accept button
1249         if (Player_select_list_region.double_clicked()) {
1250                 if ((Player_select_pilot >= 0) && (Player_select_pilot < Player_select_num_pilots)) {
1251                         player_select_button_pressed(ACCEPT_BUTTON);
1252                 }
1253         }
1254 }
1255
1256 void player_select_process_input(int k)
1257 {
1258         char buf[CALLSIGN_LEN + 1];
1259         int idx,z;
1260         
1261         // if the player is in the process of typing in a new pilot name...
1262         switch (k) {
1263         // cancel create pilot
1264         case SDLK_ESCAPE:
1265                 player_select_cancel_create();          
1266                 break;
1267
1268         // accept a new pilot name
1269         case SDLK_RETURN:
1270                 Player_select_input_box.get_text(buf);
1271                 drop_white_space(buf);
1272                 z = 0;
1273                 if (!isalpha(*buf)) {
1274                         z = 1;
1275                 } else {
1276                         for (idx=1; buf[idx]; idx++) {
1277                                 if (!isalpha(buf[idx]) && !isdigit(buf[idx]) && !SDL_strchr(VALID_PILOT_CHARS, buf[idx])) {
1278                                         z = 1;
1279                                         break;
1280                                 }
1281                         }
1282                 }
1283
1284                 for (idx=1; idx<Player_select_num_pilots; idx++) {
1285                         if (!SDL_strcasecmp(buf, Pilots[idx])) {
1286                                 // verify if it is ok to overwrite the file
1287                                 if (pilot_verify_overwrite() == 1) {
1288                                         // delete the pilot and select the beginning of the list
1289                                         Player_select_pilot = idx;
1290                                         player_select_delete_pilot();
1291                                         Player_select_pilot = 0;
1292                                         idx = Player_select_num_pilots;
1293                                         z = 0;
1294
1295                                 } else
1296                                         z = 1;
1297
1298                                 break;
1299                         }
1300                 }
1301
1302                 if (!*buf || (idx < Player_select_num_pilots)) {
1303                         z = 1;
1304                 }
1305
1306                 if (z) {
1307                         gamesnd_play_iface(SND_GENERAL_FAIL);
1308                         break;
1309                 }               
1310
1311                 // Create the new pilot, and write out his file
1312                 SDL_strlcpy(Pilots[0], buf, MAX_FILENAME_LEN);
1313
1314                 // if this is the first guy, we should set the Player struct
1315                 if (Player == NULL) {
1316                         Player = &Players[0];
1317                         memset(Player, 0, sizeof(player));
1318                         Player->flags |= PLAYER_FLAGS_STRUCTURE_IN_USE;
1319                 }
1320
1321                 SDL_strlcpy(Player->callsign, buf, SDL_arraysize(Player->callsign));
1322                 init_new_pilot(Player, !Player_select_clone_flag);
1323
1324                 // set him as being a multiplayer pilot if we're in the correct mode
1325                 if (Player_select_mode == PLAYER_SELECT_MODE_MULTI) {
1326                         Player->flags |= PLAYER_FLAGS_IS_MULTI;
1327                         Player->stats.flags |= STATS_FLAG_MULTIPLAYER;
1328                 }
1329
1330                 // create his pilot file
1331                 write_pilot_file(Player);
1332
1333                 // unset the player
1334                 memset(Player, 0, sizeof(player));
1335                 Player = NULL;
1336
1337                 // make this guy the selected pilot and put him first on the list
1338                 Player_select_pilot = 0;
1339                                 
1340                 // unset the input mode
1341                 player_select_set_input_mode(0);
1342
1343                 // clear any pending bottom text
1344                 player_select_set_bottom_text("");              
1345
1346                 // clear any pending middle text
1347                 player_select_set_middle_text("");
1348                                 
1349                 // ungray all the controls
1350                 player_select_set_controls(0);
1351
1352                 // evaluate whether or not this is the very first pilot
1353                 player_select_eval_very_first_pilot();
1354                 break;
1355
1356         case 0:
1357                 break;
1358
1359         // always kill middle text when a char is pressed in input mode
1360         default:
1361                 player_select_set_middle_text("");
1362                 break;
1363         }
1364 }
1365     
1366 // draw copyright message on the bottom on the screen
1367 void player_select_display_copyright()
1368 {
1369         int     sx, sy, w;
1370         char    Copyright_msg1[256], Copyright_msg2[256];
1371
1372 #ifdef MAKE_FS1
1373         gr_set_color_fast(&Color_bright);
1374
1375         if (Lcl_gr) {
1376                 SDL_snprintf(Copyright_msg1, SDL_arraysize(Copyright_msg1), XSTR("Descent: FreeSpace - The Great War, Copyright %c 1998, Volition, Inc.", 384), '\xA8');
1377         } else {
1378                 SDL_snprintf(Copyright_msg1, SDL_arraysize(Copyright_msg1), XSTR("Descent: FreeSpace - The Great War, Copyright %c 1998, Volition, Inc.", 384), '\x83');
1379         }
1380         SDL_snprintf(Copyright_msg2, SDL_arraysize(Copyright_msg2), XSTR("All Rights Reserved", 385));
1381 #else
1382         gr_set_color_fast(&Color_white);
1383
1384         SDL_snprintf(Copyright_msg1, SDL_arraysize(Copyright_msg1), NOX("FreeSpace 2"));
1385         if (Lcl_gr) {
1386                 SDL_snprintf(Copyright_msg2, SDL_arraysize(Copyright_msg2), XSTR("Copyright %c 1999, Volition, Inc.  All rights reserved.", 385), '\xA8');
1387         } else {
1388                 SDL_snprintf(Copyright_msg2, SDL_arraysize(Copyright_msg2), XSTR("Copyright %c 1999, Volition, Inc.  All rights reserved.", 385), '\x83');
1389         }
1390 #endif // MAKE_FS1
1391
1392         gr_get_string_size(&w, NULL, Copyright_msg1);
1393         sx = fl2i((gr_screen.max_w / 2) - w/2.0f + 0.5f);
1394         sy = (gr_screen.max_h - 2) - 2*gr_get_font_height();
1395         gr_string(sx, sy, Copyright_msg1);
1396
1397         gr_get_string_size(&w, NULL, Copyright_msg2);
1398         sx = fl2i((gr_screen.max_w / 2) - w/2.0f + 0.5f);
1399         sy = (gr_screen.max_h - 2) - gr_get_font_height();
1400         gr_string(sx, sy, Copyright_msg2);
1401 }
1402
1403 void player_select_display_all_text()
1404 {
1405         int w, h;
1406
1407         // only draw if we actually have a valid string
1408         if (strlen(Player_select_bottom_text)) {
1409                 gr_get_string_size(&w, &h, Player_select_bottom_text);
1410         
1411                 w = (gr_screen.max_w - w) / 2;
1412 #ifdef MAKE_FS1
1413                 gr_set_color_fast(&Color_bright);
1414 #else
1415                 gr_set_color_fast(&Color_bright_white);
1416 #endif
1417                 gr_printf(w, Player_select_bottom_text_y[gr_screen.res], Player_select_bottom_text);
1418         }
1419
1420         // only draw if we actually have a valid string
1421         if (strlen(Player_select_middle_text)) {
1422                 gr_get_string_size(&w, &h, Player_select_middle_text);
1423         
1424                 w = (gr_screen.max_w - w) / 2;
1425                 gr_set_color_fast(&Color_bright_white);
1426                 gr_printf(w, Player_select_middle_text_y[gr_screen.res], Player_select_middle_text);
1427         }
1428 }
1429
1430 int player_select_pilot_file_filter(const char *filename)
1431 {
1432         return !verify_pilot_file(filename, Player_select_mode == PLAYER_SELECT_MODE_SINGLE);
1433 }
1434
1435 void player_select_set_bottom_text(const char *txt)
1436 {
1437         if (txt) {
1438                 SDL_strlcpy(Player_select_bottom_text, txt, SDL_arraysize(Player_select_bottom_text));
1439         }
1440 }
1441
1442 void player_select_set_middle_text(const char *txt)
1443 {
1444         if (txt) {
1445                 SDL_strlcpy(Player_select_middle_text, txt, SDL_arraysize(Player_select_middle_text));
1446         }
1447 }
1448
1449 void player_select_eval_very_first_pilot()
1450 {       
1451         // never bring up the initial main hall help overlay
1452         // Player_select_very_first_pilot = 0;
1453
1454         // if we already have this flag set, check to see if our callsigns match
1455         if(Player_select_very_first_pilot){
1456                 // if the callsign has changed, unset the flag
1457                 if(strcmp(Player_select_very_first_pilot_callsign,Pilots[Player_select_pilot])){
1458                         Player_select_very_first_pilot = 0;
1459                 }
1460         }
1461         // otherwise check to see if there is only 1 pilot
1462         else {
1463                 if((Player_select_num_pilots == 1) && (Player_select_initial_count == 0)){
1464                         // set up the data
1465                         Player_select_very_first_pilot = 1;
1466                         SDL_strlcpy(Player_select_very_first_pilot_callsign, Pilots[Player_select_pilot], SDL_arraysize(Player_select_very_first_pilot_callsign));
1467                 }
1468         }
1469 }
1470
1471 void player_select_commit()
1472 {
1473         // if we've gotten to this point, we should have ensured this was the case
1474         SDL_assert(Player_select_num_pilots > 0);
1475         
1476         gameseq_post_event(GS_EVENT_MAIN_MENU);
1477         gamesnd_play_iface(SND_COMMIT_PRESSED);
1478
1479         // evaluate if this is the _very_ first pilot
1480         player_select_eval_very_first_pilot();
1481
1482
1483 void player_select_cancel_create()
1484 {
1485         int idx;
1486
1487         Player_select_num_pilots--;
1488
1489         // make sure we correct the Selected_pilot index to account for the cancelled action
1490         if (Player_select_num_pilots == 0) {
1491                 Player_select_pilot = -1;
1492         }
1493
1494         // move all pilots down
1495         for (idx=0; idx<Player_select_num_pilots; idx++) {
1496                 SDL_strlcpy(Pilots[idx], Pilots[idx + 1], MAX_FILENAME_LEN);
1497         }
1498
1499         // unset the input mode
1500         player_select_set_input_mode(0);
1501
1502         // clear any bottom text
1503         player_select_set_bottom_text("");
1504
1505         // clear any middle text
1506         player_select_set_middle_text("");
1507
1508         // ungray all controls
1509         player_select_set_controls(0);
1510
1511         // disable the autoaccept
1512         Player_select_autoaccept = 0;
1513 }
1514
1515 DCF(bastion,"Sets the player to be on the bastion")
1516 {
1517         if(gameseq_get_state() == GS_STATE_INITIAL_PLAYER_SELECT){
1518                 Player_select_force_bastion = 1;
1519                 dc_printf("Player is now in the Bastion\n");
1520         }
1521 }
1522
1523 #define MAX_PLAYER_TIPS                 40
1524
1525 char *Player_tips[MAX_PLAYER_TIPS];
1526 int Num_player_tips;
1527 int Player_tips_shown = 0;
1528
1529 // tooltips
1530 void player_tips_init()
1531 {
1532 #ifndef MAKE_FS1
1533         Num_player_tips = 0;
1534
1535         // begin external localization stuff
1536         lcl_ext_open();
1537
1538         try {
1539                 read_file_text("tips.tbl");
1540                 reset_parse();
1541
1542                 while(!optional_string("#end")){
1543                         required_string("+Tip:");
1544
1545                         if(Num_player_tips >= MAX_PLAYER_TIPS){
1546                                 break;
1547                         }
1548                         Player_tips[Num_player_tips++] = stuff_and_malloc_string(F_NAME, NULL, 1024);
1549                 }
1550         } catch (parse_error_t rval) {
1551                 Error(LOCATION, "Unable to parse tips.tbl!  Code = %i.\n", (int)rval);
1552         }
1553
1554         // stop externalizing, homey
1555         lcl_ext_close();
1556 #endif
1557 }
1558
1559 void player_tips_close()
1560 {
1561 #ifndef MAKE_FS1
1562         int i;
1563         
1564         for (i=0; i<MAX_PLAYER_TIPS; i++) {
1565                 if (Player_tips[i]) {
1566                         free(Player_tips[i]);
1567                         Player_tips[i] = NULL;
1568                 }
1569         }
1570 #endif
1571 }
1572
1573 void player_tips_popup()
1574 {
1575 #ifndef MAKE_FS1
1576         int tip, ret;   
1577         
1578         // player has disabled tips
1579         if((Player != NULL) && !Player->tips){
1580                 return;
1581         }
1582         // only show tips once per instance of Freespace
1583         if(Player_tips_shown == 1){
1584                 return;
1585         }
1586         Player_tips_shown = 1;
1587
1588         // randomly pick one
1589         tip = (int)frand_range(0.0f, (float)Num_player_tips - 1.0f);
1590
1591         char all_txt[2048];     
1592
1593         do {
1594                 SDL_snprintf(all_txt, SDL_arraysize(all_txt), XSTR("NEW USER TIP\n\n%s", 1565), Player_tips[tip]);
1595                 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);
1596                 
1597                 // now what?
1598                 switch(ret){
1599                 // next
1600                 case 1:
1601                         if(tip >= Num_player_tips - 1){
1602                                 tip = 0;
1603                         } else {
1604                                 tip++;
1605                         }
1606                         break;
1607
1608                 // don't show me this again
1609                 case 2:
1610                         ret = 0;
1611                         Player->tips = 0;
1612                         write_pilot_file(Player);
1613                         break;
1614                 }
1615         } while(ret > 0);
1616 #endif
1617 }