]> icculus.org git repositories - taylor/freespace2.git/blob - src/menuui/readyroom.cpp
get rid of some platform specific stuff
[taylor/freespace2.git] / src / menuui / readyroom.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/ReadyRoom.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Ready Room code, which is the UI screen for selecting Campaign/mission to play next mainly.
16  *
17  * $Log$
18  * Revision 1.9  2005/08/12 09:01:11  taylor
19  * fix strange issue where campaign and single missions could end up mixed together in the same list
20  *
21  * Revision 1.8  2005/03/29 02:18:47  taylor
22  * Various 64-bit platform fixes
23  * Fix compiler errors with MAKE_FS1 and fix gr_set_bitmap() too
24  * Make sure that turrets can fire at asteroids for FS1 (needed for a couple missions)
25  * Streaming audio support (big thanks to Pierre Willenbrock!!)
26  * Removed dependance on strings.tbl for FS1 since we don't actually need it now
27  *
28  * Revision 1.7  2004/12/15 04:10:45  taylor
29  * outwnd_unix.cpp from fs2_open for logging to file in debug mode
30  * fixes for default function values
31  * always use vm_* functions for sanity sake
32  * make cfilearchiver 64-bit compatible
33  * fix crash on exit from double free()
34  * fix crash on startup from extra long GL extension string in debug
35  *
36  * Revision 1.6  2004/09/20 01:31:44  theoddone33
37  * GCC 3.4 fixes.
38  *
39  * Revision 1.5  2004/07/04 11:39:06  taylor
40  * fix missing debrief text, crash on exit, path separator's, warning fixes, no GR_SOFT
41  *
42  * Revision 1.4  2003/05/25 02:30:42  taylor
43  * Freespace 1 support
44  *
45  * Revision 1.3  2002/06/09 04:41:22  relnev
46  * added copyright header
47  *
48  * Revision 1.2  2002/06/02 04:26:34  relnev
49  * warning cleanup
50  *
51  * Revision 1.1.1.1  2002/05/03 03:28:09  root
52  * Initial import.
53  *
54  * 
55  * 20    9/30/99 5:59p Jefff
56  * fixed compile error in OEM ver
57  * 
58  * 19    9/08/99 12:28p Jefff
59  * 
60  * 18    9/06/99 6:38p Dave
61  * Improved CD detection code.
62  * 
63  * 17    9/02/99 2:33p Jefff
64  * column heading coordinate changes
65  * 
66  * 16    8/22/99 4:16p Jefff
67  * scroll button coord fixes
68  * 
69  * 15    7/20/99 1:49p Dave
70  * Peter Drake build. Fixed some release build warnings.
71  * 
72  * 14    7/19/99 2:13p Dave
73  * Added some new strings for Heiko.
74  * 
75  * 13    7/15/99 9:20a Andsager
76  * FS2_DEMO initial checkin
77  * 
78  * 12    7/09/99 5:54p Dave
79  * Seperated cruiser types into individual types. Added tons of new
80  * briefing icons. Campaign screen.
81  * 
82  * 11    2/01/99 5:55p Dave
83  * Removed the idea of explicit bitmaps for buttons. Fixed text
84  * highlighting for disabled gadgets.
85  * 
86  * 10    1/30/99 9:01p Dave
87  * Coord fixes.
88  * 
89  * 9     1/30/99 5:08p Dave
90  * More new hi-res stuff.Support for nice D3D textures.
91  * 
92  * 8     1/29/99 12:47a Dave
93  * Put in sounds for beam weapon. A bunch of interface screens (tech
94  * database stuff).
95  * 
96  * 7     12/11/98 4:35p Andsager
97  * Fix sim room bug when no standalone missions
98  * 
99  * 6     12/07/98 5:02p Dan
100  * Removed improper use of bitmap filename with extension,
101  * 
102  * 5     10/13/98 9:28a Dave
103  * Started neatening up freespace.h. Many variables renamed and
104  * reorganized. Added AlphaColors.[h,cpp]
105  *    
106  *
107  * $NoKeywords: $
108  */
109
110 #include <ctype.h>
111
112 #include "2d.h"
113 #include "font.h"
114 #include "ui.h"
115 #include "uidefs.h"
116 #include "key.h"
117 #include "bmpman.h"
118 #include "gamesequence.h"
119 #include "missioncampaign.h"
120 #include "sound.h"
121 #include "gamesnd.h"
122 #include "missionscreencommon.h"
123 #include "freespace.h"
124 #include "missionparse.h"
125 #include "player.h"
126 #include "managepilot.h"
127 #include "popup.h"
128 #include "contexthelp.h"
129 #include "cfilesystem.h"
130 #include "freespace.h"
131 #include "alphacolors.h"
132
133 #define MAX_MISSIONS    1024
134
135 int Mission_list_coords[GR_NUM_RESOLUTIONS][4] = {
136         { // GR_640
137 #ifdef MAKE_FS1
138                 82, 108, 409, 277
139 #else
140                 33, 108, 402, 279
141 #endif
142         },
143         { // GR_1024
144                 43, 175, 402, 279
145         }
146 };
147
148 int Campaign_list_coords[GR_NUM_RESOLUTIONS][4] = {
149         { // GR_640
150 #ifdef MAKE_FS1
151                 491, 108, 113, 277
152 #else
153                 491, 108, 115, 279
154 #endif
155         },
156         { // GR_1024
157                 491, 175, 115, 279
158         }
159 };
160
161
162 // x coordinate offsets for data when campaign tab active
163 #define C_TEXT_X                0
164 #define C_SUBTEXT_X     19
165
166 // x coordinate offsets for data when mission tab active
167 #define M_TEXT_X                0
168
169 #define MODE_CAMPAIGNS  0
170 #define MODE_MISSIONS   1
171
172 #define MAX_LINES                                       200
173 #define MAX_DESC_LINES                  200
174 #define NUM_BUTTONS                             11
175 #define LIST_BUTTONS_MAX                40
176
177 #define SCROLL_UP_BUTTON                0
178 #define SCROLL_DOWN_BUTTON              1
179 #define MISSION_TAB                             2
180 #define CAMPAIGN_TAB                            3
181 #define HELP_BUTTON                             4
182 #define COMMIT_BUTTON                   5
183 #define OPTIONS_BUTTON                  6
184 #define TECH_DATABASE_BUTTON    7
185 #define SIMULATOR_BUTTON                8
186 #define CUTSCENES_BUTTON                9
187 #define CREDITS_BUTTON                  10
188
189 #define CAMPAIGN_MISSION_HASH_SIZE 307
190
191 struct sim_room_buttons {
192         const char *filename;
193         int x, y, xt, yt;
194         int hotspot;
195         UI_BUTTON button;  // because we have a class inside this struct, we need the constructor below..
196
197         sim_room_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) {}
198 };
199
200 static sim_room_buttons Buttons[GR_NUM_RESOLUTIONS][NUM_BUTTONS] = {
201 //XSTR:OFF
202         {               // GR_640
203 #ifdef MAKE_FS1
204                 sim_room_buttons("LMB_04",              7,              135,    -1, -1, 4),             // up arrows
205                 sim_room_buttons("LMB_05",              7,              182,    -1, -1, 5),             // down arrows
206                 sim_room_buttons("LMB_06",              23,             396,    -1, -1, 6),             // single missions
207                 sim_room_buttons("LMB_07",              198,    396,    -1, -1, 7),             // campaign missions
208                 sim_room_buttons("LMB_08",              469,    427,    -1, -1, 8),             // help
209                 sim_room_buttons("LMB_09",              554,    411,    -1, -1, 9),             // commit
210                 sim_room_buttons("LMB_10",              447,    452,    -1,     -1, 10),        // options
211
212                 sim_room_buttons("TDB_00",              0,              0,              -1, -1, 0),             // technical database
213                 sim_room_buttons("TDB_01",              0,              19,             -1, -1, 1),             // mission simulator
214                 sim_room_buttons("TDB_02",              0,              35,             -1, -1, 2),             // cutscenes
215                 sim_room_buttons("TDB_03",              0,              56,             -1,     -1,     3),             // credits
216 #else
217                 sim_room_buttons("LMB_04",              1,              99,     -1,     -1,     4),
218                 sim_room_buttons("LMB_05",              1,              381,    -1,     -1,     5),
219                 sim_room_buttons("LMB_06",              6,              438,    40,     445,    6),
220                 sim_room_buttons("LMB_07",              6,              457,    40,     462,    7),
221                 sim_room_buttons("LMB_08",              534,    426,    500,    440,    8),
222                 sim_room_buttons("LMB_09",              571,    426,    572,    413,    9),
223                 sim_room_buttons("LMB_10",              534,    455,    480,    462,    10),
224
225                 sim_room_buttons("TDB_00",              7,              3,              37,     7,              0),
226                 sim_room_buttons("TDB_01",              7,              18,     37,     23,     1),
227                 sim_room_buttons("TDB_02",              7,              34,     37,     38,     2),
228                 sim_room_buttons("TDB_03",              7,              49,     37,     54,     3),
229 #endif
230         },
231         {               // GR_1024
232                 sim_room_buttons("2_LMB_04",    2,              159,    -1,     -1,     4),
233                 sim_room_buttons("2_LMB_05",    2,              609,    -1,     -1,     5),
234                 sim_room_buttons("2_LMB_06",    10,     701,    64,     712,    6),
235                 sim_room_buttons("2_LMB_07",    10,     732,    64,     739,    7),
236                 sim_room_buttons("2_LMB_08",    854,    681,    800, 704,       8),
237                 sim_room_buttons("2_LMB_09",    914,    681,    915, 660,       9),
238                 sim_room_buttons("2_LMB_10",    854,    728,    800, 728,       10),
239
240                 sim_room_buttons("2_TDB_00",    12,     5,              59,     12,     0),
241                 sim_room_buttons("2_TDB_01",    12,     31,     59,     37,     1),
242                 sim_room_buttons("2_TDB_02",    12,     56,     59,     62,     2),
243                 sim_room_buttons("2_TDB_03",    12,     81,     59,     88,     3),
244         }
245 //XSTR:ON
246 };
247
248 const char *Sim_filename[GR_NUM_RESOLUTIONS] = {
249         "LoadMission",
250         "2_LoadMission"
251 };
252 const char *Sim_mask_filename[GR_NUM_RESOLUTIONS] = {
253         "LoadMission-m",
254         "2_LoadMission-m"
255 };
256
257 const char *Campaign_filename[GR_NUM_RESOLUTIONS] = {
258         "Campaign",
259         "2_Campaign"
260 };
261 const char *Campaign_mask_filename[GR_NUM_RESOLUTIONS] = {
262         "Campaign-m",
263         "2_Campaign-m"
264 };
265
266 // misc text. ("Mission" and "Filename"
267 #ifndef MAKE_FS1
268 #define NUM_SIM_MISC_TEXT                               2
269 #define SIM_MISC_TEXT_MISSION                   0
270 #define SIM_MISC_TEXT_FILENAME          1
271 int Sim_misc_text_coords[GR_NUM_RESOLUTIONS][NUM_SIM_MISC_TEXT][2] = {
272         { // GR_640
273                 {33, 95},
274                 {491, 95}
275         }, 
276         { // GR_1024
277                 {43, 155},
278                 {491, 155}
279         }
280 };
281 #endif
282
283 // readyroom text line stuff
284 #define READYROOM_LINE_CAMPAIGN 1
285 #define READYROOM_LINE_CMISSION 2
286 #define READYROOM_LINE_MISSION  3
287
288 #define READYROOM_FLAG_FROM_VOLITION                    (1<<0)                  // volition made
289 #ifdef MAKE_FS1
290 #define READYROOM_FLAG_FROM_MDISK                               (1<<1)                  // mission disk
291 #endif
292 static struct { 
293         int type;                                       // see READYROOM_LINE_* defines above
294         const char *name;
295         const char *filename;
296         int x;                                          // X coordinate of line
297         int y;                                          // Y coordinate of line
298         int flags;                                      // special flags, see READYROOM_FLAG_* defines above
299 } sim_room_lines[MAX_LINES];
300
301 static char Cur_campaign[MAX_FILENAME_LEN];
302 static char *Mission_filenames[MAX_MISSIONS];
303 static char *Standalone_mission_names[MAX_MISSIONS];
304 static int  Standalone_mission_flags[MAX_MISSIONS];
305 static char *Campaign_missions[MAX_CAMPAIGN_MISSIONS];
306 static char *Campaign_mission_names[MAX_CAMPAIGN_MISSIONS];
307 static int Campaign_mission_flags[MAX_MISSIONS];
308 static char *Campaign_descs[MAX_CAMPAIGNS];
309 static char *Campaign_descs_temp[MAX_CAMPAIGNS];
310 static char *Campaign_file_names_temp[MAX_CAMPAIGNS];
311 static int Standalone_mission_names_inited = 0;
312 static int Campaign_names_inited = 0;
313 static int Campaign_mission_names_inited = 0;
314 static int Num_standalone_missions;
315 static int Num_campaign_missions;
316 static int Scroll_offset;
317 static int Selected_line;
318 static int Num_lines;
319 static int Num_campaign_missions_with_info = 0;
320 static int Num_standalone_missions_with_info = 0;
321 static int list_x1;
322 static int list_x2;
323 static int list_y;
324 static int list_w1;
325 static int list_w2;
326 static int list_h;
327 static int Background_bitmap;
328 static UI_WINDOW Ui_window;
329 static UI_BUTTON List_buttons[LIST_BUTTONS_MAX];  // buttons for each line of text in list
330
331 typedef struct hash_node {
332         hash_node *next;
333         const char *filename;
334 } hash_node;
335
336 static hash_node *Campaign_mission_hash_table[CAMPAIGN_MISSION_HASH_SIZE];
337 static int Hash_table_inited;
338
339 // special icons (1.04 + stuff)
340 #ifdef MAKE_FS1
341 #define NUM_MISSION_ICONS                       2
342 #else
343 #define NUM_MISSION_ICONS                       1
344 #endif
345 #define MISSION_ICON_VOLITION           0                               // mini volition death's head :)
346 #ifdef MAKE_FS1
347 #define MISSION_ICON_MDISK                      1                               // Silent Threat mini icon
348 #endif
349
350 // icon offsets (see LIST_ defines above
351 //#define MISSION_ICON_VOLITION_X                               (46)
352 #define MISSION_ICON_VOLITION_Y_OFFSET          (-1)
353 #ifdef MAKE_FS1
354 #define MISSION_ICON_MDISK_Y_OFFSET                     (0)
355 #endif
356
357 // icon offsets
358 static int Sim_volition_icon_x[GR_NUM_RESOLUTIONS] = {
359 #ifdef MAKE_FS1
360         45,
361 #else
362         38,
363 #endif
364         49
365 };
366
367 #ifdef MAKE_FS1
368 static int Sim_silent_icon_x[GR_NUM_RESOLUTIONS] = {
369         58,
370         58
371 };
372 #endif
373
374 // special icons themselves
375 int Mission_icon_bitmaps[NUM_MISSION_ICONS];
376 //XSTR:OFF
377 const char *Mission_icon_bitmap_filenames[NUM_MISSION_ICONS] = {
378 #ifdef MAKE_FS1
379         "icon-volition",
380         "icon-silent"
381 #else
382         "icon-volition" 
383 #endif
384 };
385 //XSTR:ON
386 void sim_room_load_mission_icons();
387 void sim_room_unload_mission_icons();
388 void sim_room_blit_icons(int line_index, int y_start, fs_builtin_mission *fb = NULL, int is_md = 0);
389
390 // Finds a hash value for mission filename
391 //
392 // returns hash value
393 int hash_filename(const char *filename) {
394         Uint64 hash_val = 0;
395         const char *ptr = filename;
396         
397         // Dont hash .fsm extension, convert all to upper case
398         for (int i=0; i < ((signed int)(strlen(filename)) - 4); i++) {
399                 hash_val = (hash_val << 4) + toupper(*ptr++);
400         }
401
402         return int(hash_val % CAMPAIGN_MISSION_HASH_SIZE);
403 }
404
405 // insert filename into Campaign_mission_hash_table
406 //
407 // returns 1 if successful, 0 if could not allocate memory
408 int hash_insert(const char *filename) {
409         int hash_val = hash_filename(filename);
410         hash_node *cur_node;
411
412         // Check if table empty
413         if (Campaign_mission_hash_table[hash_val] == NULL) {
414                 Campaign_mission_hash_table[hash_val] = new hash_node;
415
416                 cur_node = Campaign_mission_hash_table[hash_val];
417
418                 if (cur_node == NULL) {
419                         // Unable to allocate memory
420                         return 0;
421                 }
422         } else {
423                 // Walk down list to first empty node
424                 cur_node = Campaign_mission_hash_table[hash_val];
425                 while (cur_node->next != NULL) {
426                         cur_node = cur_node->next;
427                 }
428
429                 // Create new node
430                 cur_node->next = new hash_node;
431
432                 if (cur_node->next == NULL) {
433                         // unable to allocate memory
434                         return 0;
435                 } else {
436                         cur_node = cur_node->next;
437                 }
438         }
439
440         // Initialize data
441         cur_node->next = NULL;
442         cur_node->filename = filename;
443
444         // Return successs
445         return 1;
446 }
447
448 // Checks if a filename already exitst in the hash table
449 //
450 // returns 1 if found (collision), 0 if no collision
451 int campaign_mission_hash_collision(const char *filename)
452 {
453         int hash_val = hash_filename(filename);
454         hash_node *cur_node = Campaign_mission_hash_table[hash_val];
455
456         if (cur_node == NULL) {
457                 return 0;
458         }
459
460         do {
461                 if (!SDL_strcasecmp(filename, cur_node->filename)) {
462                         return 1;
463                 }
464
465                 cur_node = cur_node->next;
466         } while (cur_node != NULL);
467
468         // Ran out of stuff to check
469         return 0;
470 }
471
472 // builds hash table of campaign mission filenames
473 //
474 // returns 1 if successful, 0 if not successful
475 int build_campaign_mission_filename_hash_table()
476 {
477         int rval;
478         // Go through all campaign missions
479         for (int i=0; i<Num_campaign_missions; i++) {
480                 rval = hash_insert(Campaign_missions[i]);
481                 if (rval == 0) {
482                         return 0;
483                 }
484         }
485
486         // successful
487         return 1;
488 }
489
490 // deletes hash table nodes
491 //
492 void campaign_mission_hash_table_delete()
493 {
494         hash_node *cur_node;
495
496         for (int i=0; i<CAMPAIGN_MISSION_HASH_SIZE; i++) {
497                 // Look for entries into array
498                 if (Campaign_mission_hash_table[i] != NULL) {
499                         cur_node = Campaign_mission_hash_table[i];
500
501                         // Walk down the list deleting self
502                         while (cur_node->next != NULL) {
503                                 hash_node *temp = cur_node->next;
504                                 delete cur_node;
505                                 cur_node = temp;
506                         }
507
508                         // Delete last node
509                         delete cur_node;
510                         Campaign_mission_hash_table[i] = NULL;
511                 }
512         }
513 }
514
515
516 // add a line of sim_room smuck to end of list
517 int sim_room_line_add(int type, char *name, char *filename, int x, int y, int flags)
518 {
519         if (Num_lines >= MAX_LINES)
520                 return 0;
521
522         sim_room_lines[Num_lines].type = type;
523         sim_room_lines[Num_lines].name = name;
524         sim_room_lines[Num_lines].filename = filename;
525         sim_room_lines[Num_lines].x = x;
526         sim_room_lines[Num_lines].y = y;
527         sim_room_lines[Num_lines].flags = flags;
528         return Num_lines++;
529 }
530
531 // filter out all multiplayer campaigns
532 int campaign_room_campaign_filter(const char *filename)
533 {
534         int type, max_players;
535         char name[NAME_LENGTH], *desc = NULL;
536
537         #ifdef OEM_BUILD
538         // also need to check if this is the builtin campaign
539         if ( game_find_builtin_mission(filename) && mission_campaign_get_info(filename, name, &type, &max_players, &desc) ) {
540         #else
541         if ( mission_campaign_get_info(filename, name, &type, &max_players, &desc) ) {
542         #endif
543                 if ( type == CAMPAIGN_TYPE_SINGLE ) {                   
544                         Campaign_file_names_temp[Num_campaigns] = strdup(filename);
545                         Campaign_descs_temp[Num_campaigns++] = desc;
546                         return 1;                       
547                 }
548         }
549
550         if (desc){
551                 free(desc);
552         }
553
554         return 0;
555 }
556
557 // build up a list of all missions in all campaigns.
558 int sim_room_campaign_mission_filter(const char *filename)
559 {
560         int num;
561
562         num = mission_campaign_get_mission_list(filename, &Campaign_missions[Num_campaign_missions], MAX_CAMPAIGN_MISSIONS - Num_campaign_missions);
563         if (num < 0)
564                 return 0;
565
566         Num_campaign_missions += num;
567         return 1;
568 }
569
570 // filter out all missions already used in existing campaigns
571 int sim_room_standalone_mission_filter(const char *filename)
572 {
573         int type;
574         char mission_name[255];
575
576         // Check if a campaign mission (single and multi)
577         if (campaign_mission_hash_collision(filename)) {
578                 return 0;
579         } 
580
581         // Check if a standalone multi mission OR Mdisk mission with data
582         type = mission_parse_is_multi(filename, mission_name);
583         if (type && !(type & MISSION_TYPE_SINGLE))
584                 return 0;
585
586         return 1;
587 }
588
589 // builds up list of standalone missions and adds them to missions simulator
590 // processes one mission per frame
591 //
592 // returns 1 if finished with all missions, 0 otherwise
593 //
594 int build_standalone_mission_list_do_frame()
595 {
596         int font_height = gr_get_font_height();
597         char filename[MAX_FILENAME_LEN];
598         char str[256];
599         
600         // When no standalone missions in data directory
601         if (Num_standalone_missions == 0) {
602                 Standalone_mission_names_inited = 1;
603                 return 1;
604         }
605
606         // Set global variable so we we'll have list available next time
607         Standalone_mission_names[Num_standalone_missions_with_info] = NULL;
608         Standalone_mission_flags[Num_standalone_missions_with_info] = 0;
609
610         if (Num_standalone_missions > 0) {  // sanity check
611                 if (strlen(Mission_filenames[Num_standalone_missions_with_info]) < MAX_FILENAME_LEN - 4) { // sanity check?
612                         strcpy(filename, Mission_filenames[Num_standalone_missions_with_info]);
613
614                         // update popup         
615                         memset(str, 0, 256);
616                         sprintf(str, XSTR("Single Mission\n\n%s",989), filename);
617                         popup_change_text(str);
618
619                         // tack on an extension
620                         strcat(filename, FS_MISSION_FILE_EXT);
621                         if (!get_mission_info(filename)) {                      
622                                 Standalone_mission_names[Num_standalone_missions_with_info] = strdup(The_mission.name);
623                                 Standalone_mission_flags[Num_standalone_missions_with_info] = The_mission.game_type;
624                                 int y = Num_lines * (font_height + 2);
625
626                                 // determine some extra information
627                                 int flags = 0;
628                                 fs_builtin_mission *fb = game_find_builtin_mission(filename);                           
629                                 if((fb != NULL) && (fb->flags & FSB_FROM_VOLITION)){
630                                         flags |= READYROOM_FLAG_FROM_VOLITION;
631                                 }                               
632 #ifdef MAKE_FS1
633                                 if((fb != NULL) && (fb->flags & FSB_FROM_MDISK)){
634                                         flags |= READYROOM_FLAG_FROM_MDISK;
635                                 }
636 #endif
637
638                                 // add the line
639                                 sim_room_line_add(READYROOM_LINE_MISSION, Standalone_mission_names[Num_standalone_missions_with_info], Mission_filenames[Num_standalone_missions_with_info], list_x1 + M_TEXT_X, y, flags);                     
640                         }
641                 }
642
643                 Num_standalone_missions_with_info++;
644         }
645
646         if (Num_standalone_missions_with_info == Num_standalone_missions) {
647                 Standalone_mission_names_inited = 1;
648                 return 1;
649         } else {
650                 return 0;
651         }
652 }
653
654 // builds up list of already played missions in a campaign and adds them to missions simulator
655 // processes one mission per frame
656 //
657 // returns 1 if finished with all missions, 0 otherwise
658 //
659 int build_campaign_mission_list_do_frame()
660 {
661         int font_height = gr_get_font_height();
662         char str[256];
663
664         // When no campaign files in data directory
665         if (Campaign.num_missions == 0) {
666                 Campaign_mission_names_inited = 1;
667                 return 1;
668         }
669
670         // change popup
671         memset(str, 0, 256);
672         sprintf(str, XSTR("Campaign Mission\n\n%s",990), Campaign.missions[Num_campaign_missions_with_info].name);
673         popup_change_text(str);
674
675         // Set global variable so we we'll have list available next time
676         Campaign_mission_names[Num_campaign_missions_with_info] = NULL;
677         Campaign_mission_flags[Num_campaign_missions_with_info] = 0;
678         
679         // Only allow missions already completed
680         if (Campaign.missions[Num_campaign_missions_with_info].completed) {
681                 if (!get_mission_info(Campaign.missions[Num_campaign_missions_with_info].name)) {
682                         // add to list
683                         Campaign_mission_names[Num_campaign_missions_with_info] = strdup(The_mission.name);
684                         Campaign_mission_flags[Num_campaign_missions_with_info] = The_mission.game_type;
685                         int y = Num_campaign_missions_with_info * (font_height + 2);
686
687                         // determine some extra information
688                         int flags = 0;
689                         fs_builtin_mission *fb = game_find_builtin_mission(Campaign.missions[Num_campaign_missions_with_info].name);                            
690                         if((fb != NULL) && (fb->flags & FSB_FROM_VOLITION)){
691                                 flags |= READYROOM_FLAG_FROM_VOLITION;
692                         }                               
693
694 #ifdef MAKE_FS1
695                         if((fb != NULL) && (fb->flags & FSB_FROM_MDISK)){
696                                 flags |= READYROOM_FLAG_FROM_MDISK;
697                         }
698 #endif
699         
700                         sim_room_line_add(READYROOM_LINE_CMISSION, Campaign_mission_names[Num_campaign_missions_with_info], Campaign.missions[Num_campaign_missions_with_info].name, list_x1 + C_SUBTEXT_X, y, flags);
701                 }
702         }
703
704         Num_campaign_missions_with_info++;
705
706         if (Num_campaign_missions_with_info == Campaign.num_missions) {
707                 Campaign_mission_names_inited = 1;
708                 return 1;
709         } else {
710                 return 0;
711         }
712 }
713
714 // Builds list of either (1) standalone missions or (2) already played missions in current campaign
715 // First time through, it builds list, next time it uses precompiled list
716 void sim_room_build_listing()
717 {
718         int i, y;
719         int font_height = gr_get_font_height();
720         char full_filename[256];
721
722         Num_lines = y = 0;
723         list_y = Mission_list_coords[gr_screen.res][1];
724         list_h = Mission_list_coords[gr_screen.res][3];
725
726         // Stand alone single player missions.
727         if (Player->readyroom_listing_mode == MODE_MISSIONS) {
728                 if (Hash_table_inited) {
729                         if (!Standalone_mission_names_inited) {  // Is this the first time through
730                                 // build_list_do_frame builds list and adds sim room line and sets Standalone_mission_names_inited
731                                 popup_till_condition(build_standalone_mission_list_do_frame, POPUP_CANCEL, XSTR("Loading missions", 991) );
732                         } else {
733                                 for (i=0; i<Num_standalone_missions_with_info; i++) {
734                                         if (Standalone_mission_names[i]) {
735                                                 // determine some extra information
736                                                 int flags = 0;
737                                                 memset(full_filename, 0, 256);
738                                                 strcpy(full_filename, cf_add_ext(Mission_filenames[i], FS_MISSION_FILE_EXT));
739                                                 fs_builtin_mission *fb = game_find_builtin_mission(full_filename);                                              
740                                                 if((fb != NULL) && (fb->flags & FSB_FROM_VOLITION)){
741                                                         flags |= READYROOM_FLAG_FROM_VOLITION;
742                                                 }
743
744 #ifdef MAKE_FS1
745                                                 if((fb != NULL) && (fb->flags & FSB_FROM_MDISK)){
746                                                         flags |= READYROOM_FLAG_FROM_MDISK;
747                                                 }
748 #endif
749                                                 
750                                                 sim_room_line_add(READYROOM_LINE_MISSION, Standalone_mission_names[i], Mission_filenames[i], list_x1 + M_TEXT_X, y, flags);
751                                                 y += font_height + 2;
752                                         }
753                                 }
754                         }
755                 }
756         } else {
757                 // Campaign missions
758                 list_y += font_height + 2;
759                 list_h -= font_height - 2;
760
761                 if (!Campaign_mission_names_inited) {  // Is this the first time through
762                         popup_till_condition(build_campaign_mission_list_do_frame, POPUP_CANCEL, XSTR("Loading campaign missions",992));
763                         // builds list, adds sim room line and sets Campaign_mission_names_inited
764                 } else {
765                         for (i=0; i<Num_campaign_missions_with_info; i++) {
766                                 if (Campaign_mission_names[i]) {
767                                         // determine some extra information
768                                         int flags = 0;
769                                         memset(full_filename, 0, 256);
770                                         strcpy(full_filename, cf_add_ext(Campaign.missions[i].name, FS_MISSION_FILE_EXT));
771                                         fs_builtin_mission *fb = game_find_builtin_mission(full_filename);
772                                         if((fb != NULL) && (fb->flags & FSB_FROM_VOLITION)){
773                                                 flags |= READYROOM_FLAG_FROM_VOLITION;
774                                         }                                       
775
776 #ifdef MAKE_FS1
777                                         if((fb != NULL) && (fb->flags & FSB_FROM_MDISK)){
778                                                 flags |= READYROOM_FLAG_FROM_MDISK;
779                                         }
780 #endif
781
782                                         sim_room_line_add(READYROOM_LINE_CMISSION, Campaign_mission_names[i], Campaign.missions[i].name, list_x1 + C_SUBTEXT_X, y, flags);
783                                         y += font_height;
784                                 }
785                         }
786                 }
787         }
788 }
789
790 int sim_room_line_query_visible(int n)
791 {
792         int y;
793
794         if ((n < 0) || (n >= Num_lines))
795                 return 0;
796         
797         y = sim_room_lines[n].y - sim_room_lines[Scroll_offset].y;
798         if ((y < 0) || (y + gr_get_font_height() > list_h))
799                 return 0;
800
801         return 1;
802 }
803
804 void sim_room_scroll_screen_up()
805 {
806         if (Player->readyroom_listing_mode != MODE_MISSIONS) {
807                 if (Scroll_offset) {
808                         Scroll_offset--;
809                         gamesnd_play_iface(SND_SCROLL);
810
811                 } else
812                         gamesnd_play_iface(SND_GENERAL_FAIL);
813
814                 return;
815         }
816
817         if (Scroll_offset) {
818                 Scroll_offset--;
819                 SDL_assert(Selected_line > Scroll_offset);
820                 while (!sim_room_line_query_visible(Selected_line))
821                         Selected_line--;
822
823                 gamesnd_play_iface(SND_SCROLL);
824
825         } else
826                 gamesnd_play_iface(SND_GENERAL_FAIL);
827 }
828
829 void sim_room_scroll_line_up()
830 {
831         if (Selected_line) {
832                 Selected_line--;
833                 if (Selected_line < Scroll_offset)
834                         Scroll_offset = Selected_line;
835
836                 gamesnd_play_iface(SND_SCROLL);
837
838         } else
839                 gamesnd_play_iface(SND_GENERAL_FAIL);
840 }
841
842 void sim_room_scroll_screen_down()
843 {
844         if (Player->readyroom_listing_mode != MODE_MISSIONS) {
845                 if (sim_room_lines[Num_lines - 1].y + gr_get_font_height() > sim_room_lines[Scroll_offset].y + list_h) {
846                         Scroll_offset++;
847                         gamesnd_play_iface(SND_SCROLL);
848
849                 } else
850                         gamesnd_play_iface(SND_GENERAL_FAIL);
851
852                 return;
853         }
854
855         if (sim_room_lines[Num_lines - 1].y + gr_get_font_height() > sim_room_lines[Scroll_offset].y + list_h) {
856                 Scroll_offset++;
857                 while (!sim_room_line_query_visible(Selected_line)) {
858                         Selected_line++;
859                         SDL_assert(Selected_line < Num_lines);
860                 }
861
862                 gamesnd_play_iface(SND_SCROLL);
863
864         } else
865                 gamesnd_play_iface(SND_GENERAL_FAIL);
866 }
867
868 void sim_room_scroll_line_down()
869 {
870         if (Selected_line < Num_lines - 1) {
871                 Selected_line++;
872
873                 SDL_assert(Selected_line > Scroll_offset);
874                 while (!sim_room_line_query_visible(Selected_line))
875                         Scroll_offset++;
876
877                 gamesnd_play_iface(SND_SCROLL);
878
879         } else
880                 gamesnd_play_iface(SND_GENERAL_FAIL);
881 }
882
883 // returns: 0 = success, !0 = aborted or failed
884 int ready_room_reset_campaign()
885 {
886         int z, rval = 1;
887
888         z = popup(PF_TITLE_BIG | PF_TITLE_RED, 2, POPUP_CANCEL, POPUP_OK, XSTR( "Warning\nThis will cause all progress in your\ncurrent campaign to be lost", 110) );
889
890         if (z) {
891                 mission_campaign_savefile_delete(Campaign.filename);
892                 mission_campaign_load(Campaign.filename);
893                 rval = 0;
894         }
895
896         return rval;
897 }
898
899 // Decide if we should offer choice to resume this savegame
900 int sim_room_can_resume_savegame(char *savegame_filename)
901 {
902         #ifdef FREESPACE_SAVERESTORE_SYSTEM
903         char savegame_mission[MAX_FILENAME_LEN];
904
905         if (state_read_description(savegame_filename, NULL, savegame_mission)) {
906                 return 0;
907         }
908
909         if (SDL_strcasecmp(Game_current_mission_filename, savegame_mission)) {
910                 return 0;
911         }
912
913         return 1;
914         #else
915         return 0;
916         #endif
917 }
918
919 // Decide wether to resume a save game or not
920 // exit:        1       =>      savegame has been restored
921 //                      0       =>      no restore, proceed to briefing
922 //                      -1      =>      don't start mission at all
923 int sim_room_maybe_resume_savegame()
924 {
925         // MWA -- 3/26/98 -- removed all savegame references in game
926         return 0;
927
928         /*
929         char savegame_filename[_MAX_FNAME];
930         int popup_rval = -1, resume_savegame = 0;
931
932         // Generate the save-game filename for this campaign
933         memset(savegame_filename, 0, _MAX_FNAME);
934         mission_campaign_savefile_generate_root(savegame_filename);
935         strcat(savegame_filename, NOX("svg"));
936
937         // Decide if we should offer choice to resume this savegame
938         if ( sim_room_can_resume_savegame(savegame_filename) ) {
939                 popup_rval = popup(0, 3, XSTR("&Cancel",-1), XSTR("&Overwrite",-1), XSTR("&Resume",-1), XSTR("A save game for this mission exists.", -1));
940                 switch ( popup_rval ) {
941                 case 0:
942                 case -1:
943                         resume_savegame = -1;
944                         break;
945                 case 1:
946                         resume_savegame = 0;
947                         break;
948                 case 2:
949                         resume_savegame = 1;
950                         break;
951                 default:
952                         Int3();
953                         resume_savegame = -1;
954                         break;
955                 }
956         } else {
957                 resume_savegame = 0;
958         }
959
960         if (resume_savegame == 1) {
961                 if ( state_restore_all(savegame_filename) == -1 ) {
962                         popup_rval = popup(PF_TITLE_BIG | PF_TITLE_RED, 2, POPUP_NO, POPUP_YES, XSTR("Error\nSaved misison could not be loaded.\nDo you wish to start this mission from the beginning?", -1));
963                         if (popup_rval == 1) {
964                                 resume_savegame = 0;
965                         } else {
966                                 resume_savegame = -1;
967                         }
968
969                 } else {
970                         resume_savegame = 1;
971                 }
972         }
973
974         // If we are resuming this savegame, then delete the file
975         if (resume_savegame == 1) {
976                 cf_delete(savegame_filename);
977         }
978
979         return resume_savegame;
980         */
981 }
982
983 int readyroom_continue_campaign()
984 {
985         if (mission_campaign_next_mission()) {  // is campaign and next mission valid?
986
987 #if defined(FS2_DEMO) || defined(FS1_DEMO)
988                 int reset_campaign = 0;
989                 reset_campaign = popup(PF_BODY_BIG, 2, POPUP_NO, POPUP_YES, XSTR( "Demo Campaign Is Over.  Would you like to play the campaign again?", 111) );
990                 if ( reset_campaign == 1 ) {
991                         mission_campaign_savefile_delete(Campaign.filename);
992                         mission_campaign_load(Campaign.filename);
993                         mission_campaign_next_mission();
994                 } else {
995                         return -1;
996                 }
997 #else
998                 gamesnd_play_iface(SND_GENERAL_FAIL);
999 #ifdef MAKE_FS1
1000                 // make it the bottom button so that the graphic looks right
1001                 popup(PF_USE_AFFIRMATIVE_ICON, 1, POPUP_OK, XSTR( "The campaign is over.  To replay the campaign, either create a new pilot or restart the campaign in the campaign room.", 112) );
1002 #else
1003                 popup(0, 1, POPUP_OK, XSTR( "The campaign is over.  To replay the campaign, either create a new pilot or restart the campaign in the campaign room.", 112) );
1004 #endif
1005                 return -1;
1006 #endif
1007         }
1008
1009         // set the bit for campaign mode
1010         Game_mode |= GM_CAMPAIGN_MODE;
1011         gameseq_post_event( GS_EVENT_START_GAME );      
1012
1013         return 0;
1014 }
1015
1016 void sim_room_commit()
1017 {
1018         if ((Selected_line >= Num_lines) || !sim_room_lines[Selected_line].filename) {
1019                 gamesnd_play_iface(SND_GENERAL_FAIL);
1020                 return;
1021         }
1022
1023         strncpy(Game_current_mission_filename, sim_room_lines[Selected_line].filename, MAX_FILENAME_LEN);
1024
1025         Game_mode &= ~(GM_CAMPAIGN_MODE);                                               // be sure this bit is clear
1026
1027         // don't resume savegame, proceed to briefing
1028         gameseq_post_event(GS_EVENT_START_GAME);
1029         gamesnd_play_iface(SND_COMMIT_PRESSED);
1030 }
1031
1032 int sim_room_button_pressed(int n)
1033 {
1034         switch (n) {
1035                 case SCROLL_UP_BUTTON:
1036                         sim_room_scroll_screen_up();
1037                         break;
1038
1039                 case SCROLL_DOWN_BUTTON:
1040                         sim_room_scroll_screen_down();
1041                         break;
1042
1043                 case MISSION_TAB:
1044 #ifdef OEM_BUILD
1045                         game_feature_not_in_demo_popup();
1046 //                      gamesnd_play_iface(SND_GENERAL_FAIL);
1047                         break;
1048 #else
1049                         Player->readyroom_listing_mode = MODE_MISSIONS;
1050                         Selected_line = Scroll_offset = 0;
1051                         gamesnd_play_iface(SND_USER_SELECT);
1052                         sim_room_build_listing();
1053                         break;
1054 #endif
1055
1056                 case CAMPAIGN_TAB:
1057                         Player->readyroom_listing_mode = MODE_CAMPAIGNS;
1058                         Scroll_offset = 0;
1059                         gamesnd_play_iface(SND_USER_SELECT);
1060                         sim_room_build_listing();
1061                         break;
1062
1063                 case COMMIT_BUTTON:
1064                         sim_room_commit();
1065                         break;
1066
1067                 case HELP_BUTTON:
1068                         launch_context_help();
1069                         gamesnd_play_iface(SND_HELP_PRESSED);
1070                         break;
1071
1072                 case OPTIONS_BUTTON:
1073                         gamesnd_play_iface(SND_SWITCH_SCREENS);
1074                         gameseq_post_event(GS_EVENT_OPTIONS_MENU);
1075                         return 1;
1076
1077                 case TECH_DATABASE_BUTTON:
1078                         gamesnd_play_iface(SND_SWITCH_SCREENS);
1079                         gameseq_post_event(GS_EVENT_TECH_MENU);
1080                         return 1;
1081
1082                 case CUTSCENES_BUTTON:
1083                         gamesnd_play_iface(SND_SWITCH_SCREENS);
1084                         gameseq_post_event(GS_EVENT_GOTO_VIEW_CUTSCENES_SCREEN);
1085                         return 1;
1086
1087                 case CREDITS_BUTTON:
1088                         gamesnd_play_iface(SND_SWITCH_SCREENS);
1089                         gameseq_post_event(GS_EVENT_CREDITS);
1090                         return 1;
1091         }
1092
1093         return 0;
1094 }
1095
1096 // ---------------------------------------------------------------------
1097 // mission_sim_room_init()
1098 //
1099 // Initialize the sim_room assignment screen system.  Called when GS_STATE_sim_room_SCREEN
1100 // is entered.
1101 //
1102 void sim_room_init()
1103 {
1104         int i;
1105         sim_room_buttons *b;
1106         char wild_card[256];
1107
1108         list_x1 = Mission_list_coords[gr_screen.res][0];
1109         list_x2 = Campaign_list_coords[gr_screen.res][0];
1110         list_y = Mission_list_coords[gr_screen.res][1];
1111         list_w1 = Mission_list_coords[gr_screen.res][2];
1112         list_w2 = Campaign_list_coords[gr_screen.res][2];
1113         list_h = Mission_list_coords[gr_screen.res][3];
1114
1115         // force mode to valid range.  I once had a bogus value here.  Don't know how that happened, though.
1116         if (Player->readyroom_listing_mode != MODE_MISSIONS)
1117                 Player->readyroom_listing_mode = MODE_CAMPAIGNS;
1118
1119         *Game_current_mission_filename = 0;
1120         common_set_interface_palette("InterfacePalette");  // set the interface palette
1121         Ui_window.create(0, 0, gr_screen.max_w, gr_screen.max_h, 0);
1122         Ui_window.set_mask_bmap(Sim_mask_filename[gr_screen.res]);
1123
1124         for (i=0; i<NUM_BUTTONS; i++) {
1125                 b = &Buttons[gr_screen.res][i];
1126
1127                 b->button.create(&Ui_window, "", b->x, b->y, 60, 30, (i < 2), 1);
1128                 // set up callback for when a mouse first goes over a button
1129                 b->button.set_highlight_action(common_play_highlight_sound);
1130                 b->button.set_bmaps(b->filename);
1131                 b->button.link_hotspot(b->hotspot);
1132         }
1133
1134 #ifndef MAKE_FS1
1135         // screen/button specific text
1136         Ui_window.add_XSTR("Single Missions", 1060, Buttons[gr_screen.res][MISSION_TAB].xt, Buttons[gr_screen.res][MISSION_TAB].yt, &Buttons[gr_screen.res][MISSION_TAB].button, UI_XSTR_COLOR_GREEN);
1137         Ui_window.add_XSTR("Campaign Missions", 1061, Buttons[gr_screen.res][CAMPAIGN_TAB].xt, Buttons[gr_screen.res][CAMPAIGN_TAB].yt, &Buttons[gr_screen.res][CAMPAIGN_TAB].button, UI_XSTR_COLOR_GREEN);
1138         Ui_window.add_XSTR("Help", 928, Buttons[gr_screen.res][HELP_BUTTON].xt, Buttons[gr_screen.res][HELP_BUTTON].yt, &Buttons[gr_screen.res][HELP_BUTTON].button, UI_XSTR_COLOR_GREEN);
1139         Ui_window.add_XSTR("Commit", 1062, Buttons[gr_screen.res][COMMIT_BUTTON].xt, Buttons[gr_screen.res][COMMIT_BUTTON].yt, &Buttons[gr_screen.res][COMMIT_BUTTON].button, UI_XSTR_COLOR_PINK);
1140         Ui_window.add_XSTR("Options", 1036, Buttons[gr_screen.res][OPTIONS_BUTTON].xt, Buttons[gr_screen.res][OPTIONS_BUTTON].yt, &Buttons[gr_screen.res][OPTIONS_BUTTON].button, UI_XSTR_COLOR_GREEN);
1141         
1142         // common tab button text
1143         Ui_window.add_XSTR("Technical Database", 1055, Buttons[gr_screen.res][TECH_DATABASE_BUTTON].xt,  Buttons[gr_screen.res][TECH_DATABASE_BUTTON].yt, &Buttons[gr_screen.res][TECH_DATABASE_BUTTON].button, UI_XSTR_COLOR_GREEN);
1144         Ui_window.add_XSTR("Mission Simulator", 1056, Buttons[gr_screen.res][SIMULATOR_BUTTON].xt,  Buttons[gr_screen.res][SIMULATOR_BUTTON].yt, &Buttons[gr_screen.res][SIMULATOR_BUTTON].button, UI_XSTR_COLOR_GREEN);
1145         Ui_window.add_XSTR("Cutscenes", 1057, Buttons[gr_screen.res][CUTSCENES_BUTTON].xt,  Buttons[gr_screen.res][CUTSCENES_BUTTON].yt, &Buttons[gr_screen.res][CUTSCENES_BUTTON].button, UI_XSTR_COLOR_GREEN);
1146         Ui_window.add_XSTR("Credits", 1058, Buttons[gr_screen.res][CREDITS_BUTTON].xt,  Buttons[gr_screen.res][CREDITS_BUTTON].yt, &Buttons[gr_screen.res][CREDITS_BUTTON].button, UI_XSTR_COLOR_GREEN);
1147
1148         // misc text - not associated with any buttons
1149         Ui_window.add_XSTR("Mission", 1063, Sim_misc_text_coords[gr_screen.res][SIM_MISC_TEXT_MISSION][0], Sim_misc_text_coords[gr_screen.res][SIM_MISC_TEXT_MISSION][1], NULL, UI_XSTR_COLOR_GREEN);
1150         Ui_window.add_XSTR("Filename", 1064, Sim_misc_text_coords[gr_screen.res][SIM_MISC_TEXT_FILENAME][0], Sim_misc_text_coords[gr_screen.res][SIM_MISC_TEXT_FILENAME][1], NULL, UI_XSTR_COLOR_GREEN);
1151 #endif
1152
1153         for (i=0; i<LIST_BUTTONS_MAX; i++) {
1154                 List_buttons[i].create(&Ui_window, "", 0, 0, 60, 30, 0, 1);
1155                 List_buttons[i].hide();
1156                 List_buttons[i].disable();
1157         }
1158
1159         // set up sim_rooms for buttons so we draw the correct animation frame when a key is pressed
1160         Buttons[gr_screen.res][SCROLL_UP_BUTTON].button.set_hotkey(SDLK_PAGEUP);
1161         Buttons[gr_screen.res][SCROLL_DOWN_BUTTON].button.set_hotkey(SDLK_PAGEDOWN);
1162         Buttons[gr_screen.res][COMMIT_BUTTON].button.set_hotkey(KEY_CTRLED | SDLK_RETURN);
1163
1164         Background_bitmap = bm_load(Sim_filename[gr_screen.res]);
1165
1166         // load in help overlay bitmap  
1167         help_overlay_load(SIM_ROOM_OVERLAY);
1168         help_overlay_set_state(SIM_ROOM_OVERLAY,0);
1169
1170         Scroll_offset = Selected_line = 0;
1171
1172         strcpy(Cur_campaign, Player->current_campaign);
1173         mission_load_up_campaign();
1174         mission_campaign_next_mission();
1175
1176         Num_campaigns = Num_campaign_missions = 0;
1177         Get_file_list_filter = sim_room_campaign_mission_filter;
1178         memset(wild_card, 0, 256);
1179         strcpy(wild_card, NOX("*"));
1180         strcat(wild_card, FS_CAMPAIGN_FILE_EXT);
1181         Num_campaigns = cf_get_file_list(MAX_CAMPAIGNS, Campaign_file_names, CF_TYPE_MISSIONS, wild_card, CF_SORT_NAME);
1182
1183         Hash_table_inited = 0;
1184         if (build_campaign_mission_filename_hash_table()) {
1185                 Hash_table_inited = 1;
1186         }
1187
1188         // HACK
1189         GR_MAYBE_CLEAR_RES(Background_bitmap);
1190         if(Background_bitmap != -1){
1191                 gr_set_bitmap(Background_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1192                 gr_bitmap(0, 0);
1193         }
1194         Ui_window.draw();
1195         gr_flip();              
1196
1197         Get_file_list_filter = sim_room_standalone_mission_filter;
1198         memset(wild_card, 0, 256);
1199         strcpy(wild_card, NOX("*"));
1200         strcat(wild_card, FS_MISSION_FILE_EXT);
1201         Num_standalone_missions = cf_get_file_list(MAX_MISSIONS, Mission_filenames, CF_TYPE_MISSIONS, wild_card, CF_SORT_NAME);
1202
1203         Num_campaign_missions_with_info = Num_standalone_missions_with_info = Standalone_mission_names_inited = Campaign_names_inited = Campaign_mission_names_inited = 0;
1204         sim_room_build_listing();
1205
1206         // load special mission icons
1207         sim_room_load_mission_icons();
1208 }
1209
1210 // ---------------------------------------------------------------------
1211 // sim_room_close()
1212 //
1213 // Cleanup the sim_room assignment screen system.  Called when GS_STATE_sim_room_SCREEN
1214 // is left.
1215 //
1216 void sim_room_close()
1217 {
1218         int i;
1219
1220         for (i=0; i<Num_campaign_missions; i++) {
1221                 if (Campaign_missions[i] != NULL) {
1222                         free(Campaign_missions[i]);
1223                         Campaign_missions[i] = NULL;
1224                 }
1225         }
1226
1227         if (Background_bitmap >= 0)
1228                 bm_unload(Background_bitmap);
1229
1230         if (Standalone_mission_names_inited){
1231                 for (i=0; i<Num_standalone_missions; i++){
1232                         if (Standalone_mission_names[i] != NULL) {
1233                                 free(Standalone_mission_names[i]);
1234                                 Standalone_mission_names[i] = NULL;
1235                         }
1236                         Standalone_mission_flags[i] = 0;
1237                 }
1238         }
1239
1240         if (Campaign_names_inited) {
1241                 for (i=0; i<Num_campaigns; i++) {
1242                         if (Campaign_names[i] != NULL) {
1243                                 free(Campaign_names[i]);
1244                                 Campaign_names[i] = NULL;
1245                         }
1246                 }
1247         }
1248
1249         if (Campaign_mission_names_inited) {
1250                 for (i=0; i<Campaign.num_missions; i++) {
1251                         if (Campaign_mission_names[i] != NULL) {
1252                                 free(Campaign_mission_names[i]);
1253                                 Campaign_mission_names[i] = NULL;
1254                         }
1255                 }
1256         }
1257
1258         for (i=0; i<Num_campaigns; i++) {
1259                 if (Campaign_file_names[i] != NULL) {
1260                         free(Campaign_file_names[i]);
1261                         Campaign_file_names[i] = NULL;
1262                 }
1263         }
1264         
1265
1266         for (i=0; i<Num_standalone_missions; i++) {
1267                 if (Mission_filenames[i] != NULL) {
1268                         free(Mission_filenames[i]);
1269                         Mission_filenames[i] = NULL;
1270                 }
1271         }
1272
1273         // unload the overlay bitmap
1274         help_overlay_unload(SIM_ROOM_OVERLAY);
1275
1276         campaign_mission_hash_table_delete();
1277
1278         Ui_window.destroy();
1279         common_free_interface_palette();                // restore game palette
1280         write_pilot_file();
1281
1282         // unload special mission icons
1283         sim_room_unload_mission_icons();
1284 }
1285
1286 // ---------------------------------------------------------------------
1287 // sim_room_do_frame()
1288 //
1289 // Called once per frame to process user input for the sim_room Assignment Screen
1290 //
1291 void sim_room_do_frame(float frametime)
1292 {
1293         char buf[256];
1294         int i, k, y, z, line;
1295         int font_height = gr_get_font_height();
1296         int select_tease_line = -1;  // line mouse is down on, but won't be selected until button released      
1297
1298         z = -1;
1299         for (i=0; i<Num_campaigns; i++)
1300                 if (!SDL_strcasecmp(Campaign_file_names[i], Campaign.filename)) {
1301                         z = i;
1302                         break;
1303                 }
1304
1305         if ( help_overlay_active(SIM_ROOM_OVERLAY) ) {
1306                 Buttons[gr_screen.res][HELP_BUTTON].button.reset_status();
1307                 Ui_window.set_ignore_gadgets(1);
1308         }
1309
1310         k = Ui_window.process() & ~KEY_DEBUGGED;
1311
1312         if ( (k > 0) || B1_JUST_RELEASED ) {
1313                 if ( help_overlay_active(SIM_ROOM_OVERLAY) ) {
1314                         help_overlay_set_state(SIM_ROOM_OVERLAY, 0);
1315                         Ui_window.set_ignore_gadgets(0);
1316                         k = 0;
1317                 }
1318         }
1319
1320         if ( !help_overlay_active(SIM_ROOM_OVERLAY) ) {
1321                 Ui_window.set_ignore_gadgets(0);
1322         }
1323
1324         switch (k) {
1325                 case SDLK_DOWN:  // scroll list down
1326                         sim_room_scroll_line_down();
1327                         break;
1328
1329                 case SDLK_UP:  // scroll list up
1330                         sim_room_scroll_line_up();
1331                         break;
1332
1333                 case SDLK_ESCAPE:
1334                         gameseq_post_event(GS_EVENT_MAIN_MENU);
1335                         break;
1336
1337                 case KEY_CTRLED | SDLK_UP:
1338                         sim_room_button_pressed(TECH_DATABASE_BUTTON);
1339                         break;
1340
1341                 case KEY_CTRLED | SDLK_DOWN:
1342                         sim_room_button_pressed(CUTSCENES_BUTTON);
1343                         break;
1344
1345                 case SDLK_TAB:
1346                         if (Player->readyroom_listing_mode == MODE_CAMPAIGNS)
1347                                 Player->readyroom_listing_mode = MODE_MISSIONS;
1348                         else
1349                                 Player->readyroom_listing_mode = MODE_CAMPAIGNS;
1350
1351                         Selected_line = Scroll_offset = 0;
1352                         gamesnd_play_iface(SND_USER_SELECT);
1353                         sim_room_build_listing();
1354                         break;
1355
1356                 case SDLK_F2:
1357                         gamesnd_play_iface(SND_SWITCH_SCREENS);
1358                         gameseq_post_event(GS_EVENT_OPTIONS_MENU);
1359                         break;
1360         }       // end switch
1361
1362         for (i=0; i<NUM_BUTTONS; i++){
1363                 if (Buttons[gr_screen.res][i].button.pressed()){
1364                         if (sim_room_button_pressed(i)){
1365                                 return;
1366                         }
1367                 }
1368         }
1369
1370         for (i=0; i<LIST_BUTTONS_MAX; i++) {
1371                 if (List_buttons[i].is_mouse_on())
1372                         select_tease_line = i + Scroll_offset;
1373         
1374                 if (List_buttons[i].pressed()) {
1375                         Selected_line = i + Scroll_offset;
1376                         gamesnd_play_iface(SND_USER_SELECT);
1377                 }
1378         }
1379
1380         GR_MAYBE_CLEAR_RES(Background_bitmap);
1381         if (Background_bitmap >= 0) {
1382                 gr_set_bitmap(Background_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1383                 gr_bitmap(0, 0);
1384         }
1385
1386         Ui_window.draw();
1387
1388         for (i=TECH_DATABASE_BUTTON; i<=CREDITS_BUTTON; i++){
1389                 if (Buttons[gr_screen.res][i].button.button_down()){
1390                         break;
1391                 }
1392         }
1393
1394         if (i > CREDITS_BUTTON){
1395                 Buttons[gr_screen.res][SIMULATOR_BUTTON].button.draw_forced(2);
1396         }
1397
1398         if (!Buttons[gr_screen.res][CAMPAIGN_TAB].button.button_down() && !Buttons[gr_screen.res][MISSION_TAB].button.button_down()) {
1399                 if (Player->readyroom_listing_mode == MODE_CAMPAIGNS){
1400                         Buttons[gr_screen.res][CAMPAIGN_TAB].button.draw_forced(2);
1401                 } else if (Player->readyroom_listing_mode == MODE_MISSIONS){
1402                         Buttons[gr_screen.res][MISSION_TAB].button.draw_forced(2);
1403                 }
1404         }
1405
1406         gr_set_font(FONT1);
1407         if (Player->readyroom_listing_mode == MODE_CAMPAIGNS) {
1408                 gr_set_color_fast(&Color_text_heading);
1409                 strcpy(buf, Campaign.name);
1410                 gr_force_fit_string(buf, 255, list_w1);
1411                 gr_printf(list_x1, Mission_list_coords[gr_screen.res][1], buf);
1412
1413                 if (Campaign.filename) {                        
1414                         sprintf(buf, NOX("%s%s"), Campaign.filename, FS_CAMPAIGN_FILE_EXT);
1415                         gr_force_fit_string(buf, 255, list_w2);
1416                         gr_printf(list_x2, Mission_list_coords[gr_screen.res][1], buf);         
1417
1418                         // blit the proper icons if necessary
1419                         char full_name[256];
1420                         memset(full_name, 0, 256);
1421                         strcpy(full_name, cf_add_ext(Campaign.filename,FS_CAMPAIGN_FILE_EXT));
1422                         fs_builtin_mission *fb = game_find_builtin_mission(full_name);
1423                         if(fb != NULL){
1424                                 // sim_room_blit_icons(0, Mission_list_coords[gr_screen.res][1], fb, 0);
1425                         }
1426                 }
1427         }
1428
1429         line = Scroll_offset;
1430         while (sim_room_line_query_visible(line)) {
1431                 y = list_y + sim_room_lines[line].y - sim_room_lines[Scroll_offset].y;
1432
1433                 if (sim_room_lines[line].type != READYROOM_LINE_CAMPAIGN) {
1434                         List_buttons[line - Scroll_offset].update_dimensions(list_x1, y, list_x2 + list_w2 - list_x1, font_height);
1435                         List_buttons[line - Scroll_offset].enable();
1436
1437                 } else
1438                         List_buttons[line - Scroll_offset].disable();
1439
1440                 if (line == Selected_line)
1441                         gr_set_color_fast(&Color_text_selected);
1442                 else if (line == select_tease_line)
1443                         gr_set_color_fast(&Color_text_subselected);
1444                 else
1445                         gr_set_color_fast(&Color_text_normal);
1446
1447                 strcpy(buf, sim_room_lines[line].name);
1448                 gr_force_fit_string(buf, 255, list_x1 + list_w1 - sim_room_lines[line].x);
1449                 gr_printf(sim_room_lines[line].x, y, buf);
1450
1451                 if (sim_room_lines[line].filename) {
1452                         strcpy(buf, sim_room_lines[line].filename);
1453                         gr_force_fit_string(buf, 255, list_w2);
1454                         gr_printf(list_x2, y, buf);
1455                 }
1456
1457                 // blit additional icon information
1458                 sim_room_blit_icons(line, y);
1459
1460                 line++;
1461         }
1462
1463         i = line - Scroll_offset;
1464         while (i < LIST_BUTTONS_MAX)
1465                 List_buttons[i++].disable();
1466
1467         // blit help overlay if active
1468         help_overlay_maybe_blit(SIM_ROOM_OVERLAY);
1469
1470         gr_flip();
1471 }
1472
1473 void sim_room_blit_icons(int line_index, int y_start, fs_builtin_mission *fb, int is_md)
1474 {
1475         int is_from_volition = 0;       
1476
1477         // determine icon status
1478         if(fb == NULL){
1479                 is_from_volition = (sim_room_lines[line_index].flags & READYROOM_FLAG_FROM_VOLITION) ? 1 : 0;           
1480 #ifdef MAKE_FS1
1481                 is_md = (sim_room_lines[line_index].flags & READYROOM_FLAG_FROM_MDISK) ? 1 : 0;
1482 #endif
1483         } else {
1484                 is_from_volition = (fb->flags & FSB_FROM_VOLITION) ? 1 : 0;             
1485 #ifdef MAKE_FS1
1486                 is_md = (fb->flags & FSB_FROM_MDISK) ? 1 : 0;
1487 #endif
1488         }
1489
1490         // if the line is flagged as a volition file
1491         if(is_from_volition && (Mission_icon_bitmaps[MISSION_ICON_VOLITION] >= 0)){             
1492                 gr_set_bitmap(Mission_icon_bitmaps[MISSION_ICON_VOLITION], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1493                 gr_bitmap(Sim_volition_icon_x[gr_screen.res], y_start + MISSION_ICON_VOLITION_Y_OFFSET);
1494         }       
1495
1496 #ifdef MAKE_FS1
1497         if(is_md && (Mission_icon_bitmaps[MISSION_ICON_MDISK] >= 0)){
1498                 gr_set_bitmap(Mission_icon_bitmaps[MISSION_ICON_MDISK], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1499                 gr_bitmap(Sim_silent_icon_x[gr_screen.res], y_start + MISSION_ICON_MDISK_Y_OFFSET);
1500         }
1501 #endif
1502 }
1503
1504 ///  Campaign room stuff below
1505 int Cr_list_coords[GR_NUM_RESOLUTIONS][4] = {
1506         { // GR_640
1507 #ifdef MAKE_FS1
1508                 47, 55, 561, 195
1509 #else
1510                 47, 21, 565, 233
1511 #endif
1512         },
1513         { // GR_1024
1514                 64, 34, 916, 373
1515         }
1516 };
1517
1518 int Cr_info_coords[GR_NUM_RESOLUTIONS][4] = {
1519         { // GR_640
1520 #ifdef MAKE_FS1
1521                 64, 280, 407, 75
1522 #else
1523                 28, 267, 476, 103
1524 #endif
1525         },
1526         { // GR_1024
1527                 45, 427, 761, 165
1528         },
1529 };
1530
1531 #define CR_NUM_BUTTONS                                  6
1532
1533 #define CR_SCROLL_UP_BUTTON                     0
1534 #define CR_SCROLL_DOWN_BUTTON                   1
1535 #define CR_SCROLL_INFO_UP_BUTTON                2
1536 #define CR_SCROLL_INFO_DOWN_BUTTON      3
1537 #define CR_RESET_BUTTON                                 4
1538 #define CR_COMMIT_BUTTON                                5
1539
1540 #define MAX_INFO_LINES          20
1541
1542 #define MAX_INFO_LINE_LEN       256
1543
1544 ui_button_info Cr_buttons[GR_NUM_RESOLUTIONS][CR_NUM_BUTTONS] = {
1545         { // GR_640
1546 #ifdef MAKE_FS1
1547                 ui_button_info("CAB_00",        0,              95,             -1,     -1,     0),             // scroll up
1548                 ui_button_info("CAB_01",        0,              142,    -1,     -1,     1),             // scroll down
1549                 ui_button_info("CAB_02",        17,             259,    -1,     -1,     2),             // info scroll up
1550                 ui_button_info("CAB_03",        17,             307,    -1,     -1,     3),             // info scroll down
1551                 ui_button_info("CAB_07",        545,    323,    -1,     -1,     7),             // reset
1552                 ui_button_info("CAB_04",        561,    411,    -1,     -1,     4),             // select
1553 #else
1554                 ui_button_info("CAB_00",        2,              42,     -1,     -1,     0),
1555                 ui_button_info("CAB_01",        2,              89,     -1,     -1,     1),
1556                 ui_button_info("CAB_02",        2,              279,    -1,     -1,     2),
1557                 ui_button_info("CAB_03",        2,              325,    -1,     -1,     3),
1558                 ui_button_info("CAB_04",        579,    353,    -1,     -1,     4),
1559                 ui_button_info("CAB_05",        575,    434,    -1,     -1,     5),
1560 #endif
1561         },
1562         { // GR_1024
1563                 ui_button_info("2_CAB_00",      3,              68,     -1,     -1,     0),
1564                 ui_button_info("2_CAB_01",      3,              142,    -1,     -1,     1),
1565                 ui_button_info("2_CAB_02",      3,              446,    -1,     -1,     2),
1566                 ui_button_info("2_CAB_03",      3,              520,    -1,     -1,     3),
1567                 ui_button_info("2_CAB_04",      927,    565,    -1,     -1,     4),
1568                 ui_button_info("2_CAB_05",      920,    694,    -1,     -1,     5),
1569         }
1570 };
1571
1572 // text
1573 #ifndef MAKE_FS1
1574 #define CR_NUM_TEXT                     3
1575
1576 UI_XSTR Cr_text[GR_NUM_RESOLUTIONS][CR_NUM_TEXT] = {
1577         { // GR_640
1578                 { "Restart",            1403,           569,    326, UI_XSTR_COLOR_GREEN,       -1, &Cr_buttons[0][CR_RESET_BUTTON].button },
1579                 { "Campaign",           1404,           569,    337, UI_XSTR_COLOR_GREEN,       -1, &Cr_buttons[0][CR_RESET_BUTTON].button },
1580                 { "Select",                     1409,           568,    413, UI_XSTR_COLOR_PINK,        -1, &Cr_buttons[0][CR_COMMIT_BUTTON].button },
1581         },
1582         { // GR_1024
1583                 { "Restart",            1403,           922,    523, UI_XSTR_COLOR_GREEN,       -1, &Cr_buttons[1][CR_RESET_BUTTON].button },
1584                 { "Campaign",           1404,           922,    538, UI_XSTR_COLOR_GREEN,       -1, &Cr_buttons[1][CR_RESET_BUTTON].button },
1585                 { "Select",                     1409,           921,    665, UI_XSTR_COLOR_PINK,        -1, &Cr_buttons[1][CR_COMMIT_BUTTON].button },
1586         }
1587 };
1588 #endif
1589
1590 static int Num_desc_lines;
1591 static int Desc_scroll_offset;
1592 static int Selected_campaign_index;
1593 static int Active_campaign_index;
1594
1595 char *Info_text_ptrs[MAX_INFO_LINES];
1596 int Num_info_lines, Info_text_line_size[MAX_INFO_LINES];
1597
1598 void campaign_room_build_listing()
1599 {
1600         int c, i, y, type, max_players; 
1601         int font_height = gr_get_font_height();
1602         char name[NAME_LENGTH];
1603
1604         Num_lines = y = 0;
1605         if (!Campaign_names_inited) {
1606                 for (i=0; i<Num_campaigns; i++) {
1607                         Campaign_names[i] = NULL;
1608                         if ( mission_campaign_get_info(Campaign_file_names[i], name, &type, &max_players) ) {
1609 #ifdef PD_BUILD
1610                                 if((game_find_builtin_mission(name) == NULL) && !strstr(name, "peterdrake")){
1611                                         continue;
1612                                 } else {
1613                                         Campaign_names[i] = strdup(name);
1614                                 }
1615 #else
1616                                 if (type == CAMPAIGN_TYPE_SINGLE){
1617                                         Campaign_names[i] = strdup(name);
1618                                 }
1619 #endif
1620                         }
1621                 }
1622
1623                 Campaign_names_inited = 1;
1624         }
1625
1626         for (c=0; c<Num_campaigns; c++) {
1627                 if (Campaign_names[c]) {
1628
1629                         // determine some extra information
1630                         int flags = 0;
1631                         fs_builtin_mission *fb = game_find_builtin_mission(Campaign_file_names[c]);
1632                         if(fb != NULL){
1633                                 if(fb->flags & FSB_FROM_VOLITION){
1634                                         flags |= READYROOM_FLAG_FROM_VOLITION;
1635                                 }                               
1636 #ifdef MAKE_FS1
1637                                 if(fb->flags & FSB_FROM_MDISK){
1638                                         flags |= READYROOM_FLAG_FROM_MDISK;
1639                                 }
1640 #endif
1641                         }
1642
1643                         sim_room_line_add(READYROOM_LINE_CAMPAIGN, Campaign_names[c], Campaign_file_names[c], Cr_list_coords[gr_screen.res][0], y, flags);
1644                         y += font_height + 2;
1645                 }
1646         }
1647 }
1648
1649 void set_new_campaign_line(int n)
1650 {
1651         char *str;
1652
1653         Selected_campaign_index = n;
1654         str = Campaign_descs[Selected_campaign_index];
1655         Num_info_lines = 0;
1656         if (str) {
1657                 Num_info_lines = split_str(str, Cr_info_coords[gr_screen.res][2], Info_text_line_size, Info_text_ptrs, MAX_INFO_LINES);
1658                 SDL_assert(Num_info_lines >= 0);
1659         }
1660
1661         Desc_scroll_offset = 0;
1662 }
1663
1664 void campaign_room_scroll_info_up()
1665 {
1666         if (Desc_scroll_offset) {
1667                 Desc_scroll_offset--;
1668                 gamesnd_play_iface(SND_SCROLL);
1669
1670         } else
1671                 gamesnd_play_iface(SND_GENERAL_FAIL);
1672 }
1673
1674 void campaign_room_scroll_info_down()
1675 {
1676         if ( (Num_info_lines - Desc_scroll_offset) * gr_get_font_height() > Cr_info_coords[gr_screen.res][3]) {
1677                 Desc_scroll_offset++;
1678                 gamesnd_play_iface(SND_SCROLL);
1679
1680         } else
1681                 gamesnd_play_iface(SND_GENERAL_FAIL);
1682 }
1683
1684 // returns: 0 = success, !0 = aborted or failed
1685 int campaign_room_reset_campaign(int n)
1686 {
1687         char *filename;
1688         int z;
1689
1690         // z = popup(PF_TITLE_BIG | PF_TITLE_RED, 2, POPUP_CANCEL, POPUP_OK, XSTR( "Warning\nThis will cause all progress in your\ncurrent campaign to be lost", 110), Campaign_names[n]);
1691         z = popup(PF_TITLE_BIG | PF_TITLE_RED, 2, POPUP_CANCEL, POPUP_OK, XSTR( "Warning\nThis will cause all progress in your\ncurrent campaign to be lost", 110));
1692         if (z == 1) {
1693                 filename = (char *) malloc(strlen(Campaign_file_names[n]) + 5);
1694                 strcpy(filename, Campaign_file_names[n]);
1695                 strcat(filename, FS_CAMPAIGN_FILE_EXT);
1696
1697                 mission_campaign_savefile_delete(filename);
1698                 mission_campaign_load(filename);
1699                 mission_campaign_next_mission();
1700                 return 0;
1701         }
1702
1703         return 1;
1704 }
1705
1706 void campaign_room_commit()
1707 {
1708         if (Selected_campaign_index < 0) {
1709                 gamesnd_play_iface(SND_GENERAL_FAIL);
1710                 return;
1711         }
1712
1713         if (SDL_strcasecmp(Campaign_file_names[Selected_campaign_index], Campaign.filename)) {  // new campaign selected
1714                 if ((Active_campaign_index >= 0) && campaign_room_reset_campaign(Active_campaign_index)) {
1715                         gamesnd_play_iface(SND_GENERAL_FAIL);
1716                         return;
1717                 }
1718
1719                 mission_campaign_savefile_delete(Campaign_file_names[Selected_campaign_index]);
1720                 mission_campaign_load(Campaign_file_names[Selected_campaign_index]);
1721                 strcpy(Player->current_campaign, Campaign.filename);  // track new campaign for player
1722         }
1723
1724         if (mission_campaign_next_mission()) {  // is campaign and next mission valid?
1725                 gamesnd_play_iface(SND_GENERAL_FAIL);
1726                 return;
1727         }
1728
1729         gameseq_post_event(GS_EVENT_MAIN_MENU);
1730         gamesnd_play_iface(SND_COMMIT_PRESSED);
1731 }
1732
1733 int campaign_room_button_pressed(int n)
1734 {
1735         switch (n) {
1736                 case CR_SCROLL_UP_BUTTON:
1737                         sim_room_scroll_screen_up();
1738                         break;
1739
1740                 case CR_SCROLL_DOWN_BUTTON:
1741                         sim_room_scroll_screen_down();
1742                         break;
1743
1744                 case CR_SCROLL_INFO_UP_BUTTON:
1745                         campaign_room_scroll_info_up();
1746                         break;
1747
1748                 case CR_SCROLL_INFO_DOWN_BUTTON:
1749                         campaign_room_scroll_info_down();
1750                         break;
1751
1752                 case CR_COMMIT_BUTTON:
1753                         campaign_room_commit();
1754                         break;
1755
1756                 /*
1757                 case CR_HELP_BUTTON:
1758                         launch_context_help();
1759                         gamesnd_play_iface(SND_HELP_PRESSED);
1760                         break;
1761
1762                 case CR_OPTIONS_BUTTON:
1763                         gamesnd_play_iface(SND_SWITCH_SCREENS);
1764                         gameseq_post_event(GS_EVENT_OPTIONS_MENU);
1765                         return 1;
1766                 */
1767
1768                 case CR_RESET_BUTTON:
1769                         if ( (Active_campaign_index < 0) || (Active_campaign_index >= Num_campaigns) )
1770                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1771                         else if (campaign_room_reset_campaign(Active_campaign_index))
1772                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1773                         else
1774                                 gamesnd_play_iface(SND_USER_SELECT);
1775
1776                         break;
1777         }
1778
1779         return 0;
1780 }
1781
1782 void campaign_room_init()
1783 {
1784         int i, j, load_failed;
1785         ui_button_info *b;
1786         char wild_card[256];
1787
1788         list_h = Mission_list_coords[gr_screen.res][3];
1789
1790 #ifdef MAKE_FS1
1791         common_set_interface_palette("InterfacePalette");  // set the interface palette
1792 #endif
1793         Ui_window.create(0, 0, gr_screen.max_w, gr_screen.max_h, 0);
1794         Ui_window.set_mask_bmap(Campaign_mask_filename[gr_screen.res]);
1795
1796         for (i=0; i<CR_NUM_BUTTONS; i++) {
1797                 b = &Cr_buttons[gr_screen.res][i];
1798
1799                 b->button.create(&Ui_window, "", b->x, b->y, 60, 30, (i < 2), 1);
1800                 // set up callback for when a mouse first goes over a button
1801                 b->button.set_highlight_action(common_play_highlight_sound);
1802                 b->button.set_bmaps(b->filename);
1803                 b->button.link_hotspot(b->hotspot);
1804         }
1805
1806         for (i=0; i<LIST_BUTTONS_MAX; i++) {
1807                 List_buttons[i].create(&Ui_window, "", 0, 0, 60, 30, 0, 1);
1808                 List_buttons[i].hide();
1809                 List_buttons[i].disable();
1810         }
1811
1812 #ifndef MAKE_FS1
1813         // add xstrs
1814         for(i=0; i<CR_NUM_TEXT; i++){
1815                 Ui_window.add_XSTR(&Cr_text[gr_screen.res][i]);
1816         }
1817 #endif
1818
1819         // set up sim_rooms for buttons so we draw the correct animation frame when a key is pressed
1820         Cr_buttons[gr_screen.res][CR_SCROLL_UP_BUTTON].button.set_hotkey(SDLK_PAGEUP);
1821         Cr_buttons[gr_screen.res][CR_SCROLL_DOWN_BUTTON].button.set_hotkey(SDLK_PAGEDOWN);
1822         Cr_buttons[gr_screen.res][CR_RESET_BUTTON].button.set_hotkey(SDLK_DELETE);
1823         Cr_buttons[gr_screen.res][CR_COMMIT_BUTTON].button.set_hotkey(KEY_CTRLED | SDLK_RETURN);
1824         // Cr_buttons[gr_screen.res][CR_HELP_BUTTON].button.set_hotkey(KEY_F2);
1825
1826         Background_bitmap = bm_load(Campaign_filename[gr_screen.res]);
1827
1828         // load in help overlay bitmap  
1829         help_overlay_load(CAMPAIGN_ROOM_OVERLAY);
1830         help_overlay_set_state(CAMPAIGN_ROOM_OVERLAY,0);
1831
1832         Num_desc_lines = 0;
1833
1834         Desc_scroll_offset = Scroll_offset = 0;
1835         load_failed = mission_load_up_campaign();
1836         if (!load_failed)
1837                 mission_campaign_next_mission();
1838
1839         else {
1840                 Campaign.filename[0] = 0;
1841                 Campaign.num_missions = 0;
1842         }
1843
1844         Num_campaigns = 0;
1845
1846         Get_file_list_filter = campaign_room_campaign_filter;
1847         memset(wild_card, 0, 256);
1848         strcpy(wild_card, NOX("*"));
1849         strcat(wild_card, FS_CAMPAIGN_FILE_EXT);
1850         Num_campaigns = cf_get_file_list(MAX_CAMPAIGNS, Campaign_file_names, CF_TYPE_MISSIONS, wild_card, CF_SORT_NAME);
1851
1852         for (i=0; i<Num_campaigns; i++) {
1853                 for (j=0; j<Num_campaigns; j++) {
1854                         if (Campaign_file_names_temp[j]) {
1855                                 if (!strncmp(Campaign_file_names[i], Campaign_file_names_temp[j], strlen(Campaign_file_names_temp[j]) - 4)) {
1856                                         Campaign_descs[i] = Campaign_descs_temp[j];
1857                                         free(Campaign_file_names_temp[j]);
1858                                         Campaign_file_names_temp[j] = NULL;
1859                                         break;
1860                                 }
1861                         }
1862                 }
1863
1864                 SDL_assert(j < Num_campaigns);  // Campaign not found?  How is that possible?
1865         }
1866
1867         Campaign_names_inited = 0;
1868         campaign_room_build_listing();
1869
1870         Selected_campaign_index = Active_campaign_index = -1;
1871         if (!load_failed) {
1872                 for (i=0; i<Num_campaigns; i++)
1873                         if (!SDL_strcasecmp(Campaign_file_names[i], Campaign.filename)) {
1874                                 set_new_campaign_line(i);
1875                                 Active_campaign_index = i;
1876                                 break;
1877                         }
1878         }
1879 }
1880
1881 void campaign_room_close()
1882 {
1883         int i;
1884
1885         for (i=0; i<Num_campaigns; i++) {
1886                 if (Campaign_descs[i] != NULL) {
1887                         free(Campaign_descs[i]);
1888                         Campaign_descs[i] = NULL;
1889                 }
1890         }
1891
1892         if (Background_bitmap >= 0)
1893                 bm_unload(Background_bitmap);
1894
1895         if (Campaign_names_inited) {
1896                 for (i=0; i<Num_campaigns; i++) {
1897                         if (Campaign_names[i] != NULL) {
1898                                 free(Campaign_names[i]);
1899                                 Campaign_names[i] = NULL;
1900                         }
1901                 }
1902         }
1903
1904         for (i=0; i<Num_campaigns; i++) {
1905                 if (Campaign_file_names[i] != NULL) {
1906                         free(Campaign_file_names[i]);
1907                         Campaign_file_names[i] = NULL;
1908                 }
1909         }
1910
1911         // unload the overlay bitmap
1912         help_overlay_unload(CAMPAIGN_ROOM_OVERLAY);
1913
1914         Ui_window.destroy();
1915         common_free_interface_palette();                // restore game palette
1916         write_pilot_file();
1917 }
1918
1919 void campaign_room_do_frame(float frametime)
1920 {
1921         char buf[256];
1922         char line_text[MAX_INFO_LINE_LEN];
1923         int i, k, y, line;
1924         int font_height = gr_get_font_height();
1925         int select_tease_line = -1;  // line mouse is down on, but won't be selected until button released
1926
1927         if ( help_overlay_active(CAMPAIGN_ROOM_OVERLAY) ) {
1928                 // Cr_buttons[gr_screen.res][CR_HELP_BUTTON].button.reset_status();
1929                 Ui_window.set_ignore_gadgets(1);
1930         }
1931
1932         k = Ui_window.process() & ~KEY_DEBUGGED;
1933
1934         if ( (k > 0) || B1_JUST_RELEASED ) {
1935                 if ( help_overlay_active(CAMPAIGN_ROOM_OVERLAY) ) {
1936                         help_overlay_set_state(CAMPAIGN_ROOM_OVERLAY, 0);
1937                         Ui_window.set_ignore_gadgets(0);
1938                         k = 0;
1939                 }
1940         }
1941
1942         if ( !help_overlay_active(CAMPAIGN_ROOM_OVERLAY) ) {
1943                 Ui_window.set_ignore_gadgets(0);
1944         }
1945
1946         switch (k) {
1947                 case SDLK_DOWN:  // scroll list down
1948                         if (Selected_campaign_index < Num_campaigns - 1) {
1949                                 set_new_campaign_line(Selected_campaign_index + 1);
1950                                 gamesnd_play_iface(SND_SCROLL);
1951
1952                         } else
1953                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1954
1955                         break;
1956
1957                 case SDLK_UP:  // scroll list up
1958                         if (Selected_campaign_index < 0)
1959                                 Selected_campaign_index = 1;
1960
1961                         if (Selected_campaign_index) {
1962                                 set_new_campaign_line(Selected_campaign_index - 1);
1963                                 gamesnd_play_iface(SND_SCROLL);
1964
1965                         } else
1966                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1967
1968                         break;
1969
1970                 case SDLK_ESCAPE:
1971                         gameseq_post_event(GS_EVENT_MAIN_MENU);
1972                         break;
1973         }       // end switch
1974
1975         for (i=0; i<CR_NUM_BUTTONS; i++){
1976                 if (Cr_buttons[gr_screen.res][i].button.pressed()){
1977                         if (campaign_room_button_pressed(i)){
1978                                 return;
1979                         }
1980                 }
1981         }
1982
1983         for (i=0; i<LIST_BUTTONS_MAX; i++) {
1984                 if (List_buttons[i].is_mouse_on()){
1985                         select_tease_line = i + Scroll_offset;
1986                 }
1987         
1988                 if (List_buttons[i].pressed()) {
1989                         set_new_campaign_line(i + Scroll_offset);
1990                         gamesnd_play_iface(SND_USER_SELECT);
1991                 }
1992         }
1993
1994         GR_MAYBE_CLEAR_RES(Background_bitmap);
1995         if (Background_bitmap >= 0) {
1996                 gr_set_bitmap(Background_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1997                 gr_bitmap(0, 0);
1998         }
1999
2000         Ui_window.draw();
2001
2002         gr_set_font(FONT1);
2003         line = Scroll_offset;
2004         while (sim_room_line_query_visible(line)) {
2005                 y = Cr_list_coords[gr_screen.res][1] + sim_room_lines[line].y - sim_room_lines[Scroll_offset].y;
2006
2007                 List_buttons[line - Scroll_offset].update_dimensions(Cr_list_coords[gr_screen.res][0], y, Cr_list_coords[gr_screen.res][2], font_height);
2008                 List_buttons[line - Scroll_offset].enable();
2009
2010                 if (!SDL_strcasecmp(sim_room_lines[line].filename, Campaign.filename)) {
2011                         gr_set_color_fast(&Color_white);
2012                         i = y + font_height / 2 - 1;
2013                         gr_circle(Cr_list_coords[gr_screen.res][0] - 6, i, 5);
2014
2015                         gr_set_color_fast(&Color_bright_white);
2016                         gr_line(Cr_list_coords[gr_screen.res][0] - 10, i, Cr_list_coords[gr_screen.res][0] - 8, i);
2017                         gr_line(Cr_list_coords[gr_screen.res][0] - 6, i - 4, Cr_list_coords[gr_screen.res][0] - 6, i - 2);
2018                         gr_line(Cr_list_coords[gr_screen.res][0] - 4, i, Cr_list_coords[gr_screen.res][0] - 2, i);
2019                         gr_line(Cr_list_coords[gr_screen.res][0] - 6, i + 2, Cr_list_coords[gr_screen.res][0] - 6, i + 4);
2020                 }
2021
2022                 if (line == Selected_campaign_index)
2023                         gr_set_color_fast(&Color_text_selected);
2024                 else if (line == select_tease_line)
2025                         gr_set_color_fast(&Color_text_subselected);
2026                 else
2027                         gr_set_color_fast(&Color_text_normal);
2028
2029                 strcpy(buf, sim_room_lines[line].name);
2030                 gr_force_fit_string(buf, 255, Cr_list_coords[gr_screen.res][0] + Cr_list_coords[gr_screen.res][2] - sim_room_lines[line].x);
2031                 gr_printf(sim_room_lines[line].x, y, buf);
2032                 line++;
2033         }
2034
2035         i = line - Scroll_offset;
2036         while (i < LIST_BUTTONS_MAX)
2037                 List_buttons[i++].disable();
2038
2039         y = 0;
2040         i = Desc_scroll_offset;
2041         gr_set_color_fast(&Color_text_normal);
2042
2043         while (y + font_height <= Cr_info_coords[gr_screen.res][3]) {
2044                 if (i >= Num_info_lines)
2045                         break;
2046
2047                 SDL_assert(Info_text_line_size[i] < MAX_INFO_LINE_LEN);
2048                 strncpy(line_text, Info_text_ptrs[i], Info_text_line_size[i]);
2049                 line_text[Info_text_line_size[i]] = 0;
2050                 drop_white_space(line_text);
2051                 gr_string(Cr_info_coords[gr_screen.res][0], Cr_info_coords[gr_screen.res][1] + y, line_text);
2052                 y += font_height;
2053                 i++;
2054         }
2055
2056         // blit help overlay if active
2057         help_overlay_maybe_blit(CAMPAIGN_ROOM_OVERLAY);
2058
2059         gr_flip();
2060 }
2061
2062 void sim_room_load_mission_icons()
2063 {
2064         int idx;
2065         
2066         // load all bitmaps
2067         for(idx=0; idx<NUM_MISSION_ICONS; idx++){
2068                 Mission_icon_bitmaps[idx] = -1;
2069                 Mission_icon_bitmaps[idx] = bm_load(Mission_icon_bitmap_filenames[idx]);
2070         }
2071 }
2072
2073 void sim_room_unload_mission_icons()
2074 {
2075         int idx;
2076
2077         // unload all bitmaps
2078         for(idx=0; idx<NUM_MISSION_ICONS; idx++){
2079                 if(Mission_icon_bitmaps[idx] >= 0){
2080                         bm_unload(Mission_icon_bitmaps[idx]);
2081                         Mission_icon_bitmaps[idx] = -1;
2082                 }
2083         }
2084 }
2085