]> icculus.org git repositories - taylor/freespace2.git/blob - src/menuui/readyroom.cpp
switch to SDL_min/SDL_max (fixes compiler errors)
[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                         SDL_strlcpy(filename, Mission_filenames[Num_standalone_missions_with_info], SDL_arraysize(filename));
613
614                         // update popup         
615                         SDL_snprintf(str, SDL_arraysize(str), XSTR("Single Mission\n\n%s",989), filename);
616                         popup_change_text(str);
617
618                         // tack on an extension
619                         SDL_strlcat(filename, FS_MISSION_FILE_EXT, SDL_arraysize(filename));
620                         if (!get_mission_info(filename)) {                      
621                                 Standalone_mission_names[Num_standalone_missions_with_info] = strdup(The_mission.name);
622                                 Standalone_mission_flags[Num_standalone_missions_with_info] = The_mission.game_type;
623                                 int y = Num_lines * (font_height + 2);
624
625                                 // determine some extra information
626                                 int flags = 0;
627                                 fs_builtin_mission *fb = game_find_builtin_mission(filename);                           
628                                 if((fb != NULL) && (fb->flags & FSB_FROM_VOLITION)){
629                                         flags |= READYROOM_FLAG_FROM_VOLITION;
630                                 }                               
631 #ifdef MAKE_FS1
632                                 if((fb != NULL) && (fb->flags & FSB_FROM_MDISK)){
633                                         flags |= READYROOM_FLAG_FROM_MDISK;
634                                 }
635 #endif
636
637                                 // add the line
638                                 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);                     
639                         }
640                 }
641
642                 Num_standalone_missions_with_info++;
643         }
644
645         if (Num_standalone_missions_with_info == Num_standalone_missions) {
646                 Standalone_mission_names_inited = 1;
647                 return 1;
648         } else {
649                 return 0;
650         }
651 }
652
653 // builds up list of already played missions in a campaign and adds them to missions simulator
654 // processes one mission per frame
655 //
656 // returns 1 if finished with all missions, 0 otherwise
657 //
658 int build_campaign_mission_list_do_frame()
659 {
660         int font_height = gr_get_font_height();
661         char str[256];
662
663         // When no campaign files in data directory
664         if (Campaign.num_missions == 0) {
665                 Campaign_mission_names_inited = 1;
666                 return 1;
667         }
668
669         // change popup
670         SDL_snprintf(str, SDL_arraysize(str), XSTR("Campaign Mission\n\n%s",990), Campaign.missions[Num_campaign_missions_with_info].name);
671         popup_change_text(str);
672
673         // Set global variable so we we'll have list available next time
674         Campaign_mission_names[Num_campaign_missions_with_info] = NULL;
675         Campaign_mission_flags[Num_campaign_missions_with_info] = 0;
676         
677         // Only allow missions already completed
678         if (Campaign.missions[Num_campaign_missions_with_info].completed) {
679                 if (!get_mission_info(Campaign.missions[Num_campaign_missions_with_info].name)) {
680                         // add to list
681                         Campaign_mission_names[Num_campaign_missions_with_info] = strdup(The_mission.name);
682                         Campaign_mission_flags[Num_campaign_missions_with_info] = The_mission.game_type;
683                         int y = Num_campaign_missions_with_info * (font_height + 2);
684
685                         // determine some extra information
686                         int flags = 0;
687                         fs_builtin_mission *fb = game_find_builtin_mission(Campaign.missions[Num_campaign_missions_with_info].name);                            
688                         if((fb != NULL) && (fb->flags & FSB_FROM_VOLITION)){
689                                 flags |= READYROOM_FLAG_FROM_VOLITION;
690                         }                               
691
692 #ifdef MAKE_FS1
693                         if((fb != NULL) && (fb->flags & FSB_FROM_MDISK)){
694                                 flags |= READYROOM_FLAG_FROM_MDISK;
695                         }
696 #endif
697         
698                         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);
699                 }
700         }
701
702         Num_campaign_missions_with_info++;
703
704         if (Num_campaign_missions_with_info == Campaign.num_missions) {
705                 Campaign_mission_names_inited = 1;
706                 return 1;
707         } else {
708                 return 0;
709         }
710 }
711
712 // Builds list of either (1) standalone missions or (2) already played missions in current campaign
713 // First time through, it builds list, next time it uses precompiled list
714 void sim_room_build_listing()
715 {
716         int i, y;
717         int font_height = gr_get_font_height();
718         char full_filename[256];
719
720         Num_lines = y = 0;
721         list_y = Mission_list_coords[gr_screen.res][1];
722         list_h = Mission_list_coords[gr_screen.res][3];
723
724         // Stand alone single player missions.
725         if (Player->readyroom_listing_mode == MODE_MISSIONS) {
726                 if (Hash_table_inited) {
727                         if (!Standalone_mission_names_inited) {  // Is this the first time through
728                                 // build_list_do_frame builds list and adds sim room line and sets Standalone_mission_names_inited
729                                 popup_till_condition(build_standalone_mission_list_do_frame, POPUP_CANCEL, XSTR("Loading missions", 991) );
730                         } else {
731                                 for (i=0; i<Num_standalone_missions_with_info; i++) {
732                                         if (Standalone_mission_names[i]) {
733                                                 // determine some extra information
734                                                 int flags = 0;
735                                                 memset(full_filename, 0, 256);
736                                                 SDL_strlcpy(full_filename, cf_add_ext(Mission_filenames[i], FS_MISSION_FILE_EXT), SDL_arraysize(full_filename));
737                                                 fs_builtin_mission *fb = game_find_builtin_mission(full_filename);                                              
738                                                 if((fb != NULL) && (fb->flags & FSB_FROM_VOLITION)){
739                                                         flags |= READYROOM_FLAG_FROM_VOLITION;
740                                                 }
741
742 #ifdef MAKE_FS1
743                                                 if((fb != NULL) && (fb->flags & FSB_FROM_MDISK)){
744                                                         flags |= READYROOM_FLAG_FROM_MDISK;
745                                                 }
746 #endif
747                                                 
748                                                 sim_room_line_add(READYROOM_LINE_MISSION, Standalone_mission_names[i], Mission_filenames[i], list_x1 + M_TEXT_X, y, flags);
749                                                 y += font_height + 2;
750                                         }
751                                 }
752                         }
753                 }
754         } else {
755                 // Campaign missions
756                 list_y += font_height + 2;
757                 list_h -= font_height - 2;
758
759                 if (!Campaign_mission_names_inited) {  // Is this the first time through
760                         popup_till_condition(build_campaign_mission_list_do_frame, POPUP_CANCEL, XSTR("Loading campaign missions",992));
761                         // builds list, adds sim room line and sets Campaign_mission_names_inited
762                 } else {
763                         for (i=0; i<Num_campaign_missions_with_info; i++) {
764                                 if (Campaign_mission_names[i]) {
765                                         // determine some extra information
766                                         int flags = 0;
767                                         memset(full_filename, 0, 256);
768                                         SDL_strlcpy(full_filename, cf_add_ext(Campaign.missions[i].name, FS_MISSION_FILE_EXT), SDL_arraysize(full_filename));
769                                         fs_builtin_mission *fb = game_find_builtin_mission(full_filename);
770                                         if((fb != NULL) && (fb->flags & FSB_FROM_VOLITION)){
771                                                 flags |= READYROOM_FLAG_FROM_VOLITION;
772                                         }                                       
773
774 #ifdef MAKE_FS1
775                                         if((fb != NULL) && (fb->flags & FSB_FROM_MDISK)){
776                                                 flags |= READYROOM_FLAG_FROM_MDISK;
777                                         }
778 #endif
779
780                                         sim_room_line_add(READYROOM_LINE_CMISSION, Campaign_mission_names[i], Campaign.missions[i].name, list_x1 + C_SUBTEXT_X, y, flags);
781                                         y += font_height;
782                                 }
783                         }
784                 }
785         }
786 }
787
788 int sim_room_line_query_visible(int n)
789 {
790         int y;
791
792         if ((n < 0) || (n >= Num_lines))
793                 return 0;
794         
795         y = sim_room_lines[n].y - sim_room_lines[Scroll_offset].y;
796         if ((y < 0) || (y + gr_get_font_height() > list_h))
797                 return 0;
798
799         return 1;
800 }
801
802 void sim_room_scroll_screen_up()
803 {
804         if (Player->readyroom_listing_mode != MODE_MISSIONS) {
805                 if (Scroll_offset) {
806                         Scroll_offset--;
807                         gamesnd_play_iface(SND_SCROLL);
808
809                 } else
810                         gamesnd_play_iface(SND_GENERAL_FAIL);
811
812                 return;
813         }
814
815         if (Scroll_offset) {
816                 Scroll_offset--;
817                 SDL_assert(Selected_line > Scroll_offset);
818                 while (!sim_room_line_query_visible(Selected_line))
819                         Selected_line--;
820
821                 gamesnd_play_iface(SND_SCROLL);
822
823         } else
824                 gamesnd_play_iface(SND_GENERAL_FAIL);
825 }
826
827 void sim_room_scroll_line_up()
828 {
829         if (Selected_line) {
830                 Selected_line--;
831                 if (Selected_line < Scroll_offset)
832                         Scroll_offset = Selected_line;
833
834                 gamesnd_play_iface(SND_SCROLL);
835
836         } else
837                 gamesnd_play_iface(SND_GENERAL_FAIL);
838 }
839
840 void sim_room_scroll_screen_down()
841 {
842         if (Player->readyroom_listing_mode != MODE_MISSIONS) {
843                 if (sim_room_lines[Num_lines - 1].y + gr_get_font_height() > sim_room_lines[Scroll_offset].y + list_h) {
844                         Scroll_offset++;
845                         gamesnd_play_iface(SND_SCROLL);
846
847                 } else
848                         gamesnd_play_iface(SND_GENERAL_FAIL);
849
850                 return;
851         }
852
853         if (sim_room_lines[Num_lines - 1].y + gr_get_font_height() > sim_room_lines[Scroll_offset].y + list_h) {
854                 Scroll_offset++;
855                 while (!sim_room_line_query_visible(Selected_line)) {
856                         Selected_line++;
857                         SDL_assert(Selected_line < Num_lines);
858                 }
859
860                 gamesnd_play_iface(SND_SCROLL);
861
862         } else
863                 gamesnd_play_iface(SND_GENERAL_FAIL);
864 }
865
866 void sim_room_scroll_line_down()
867 {
868         if (Selected_line < Num_lines - 1) {
869                 Selected_line++;
870
871                 SDL_assert(Selected_line > Scroll_offset);
872                 while (!sim_room_line_query_visible(Selected_line))
873                         Scroll_offset++;
874
875                 gamesnd_play_iface(SND_SCROLL);
876
877         } else
878                 gamesnd_play_iface(SND_GENERAL_FAIL);
879 }
880
881 // returns: 0 = success, !0 = aborted or failed
882 int ready_room_reset_campaign()
883 {
884         int z, rval = 1;
885
886         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) );
887
888         if (z) {
889                 mission_campaign_savefile_delete(Campaign.filename);
890                 mission_campaign_load(Campaign.filename);
891                 rval = 0;
892         }
893
894         return rval;
895 }
896
897 // Decide if we should offer choice to resume this savegame
898 int sim_room_can_resume_savegame(char *savegame_filename)
899 {
900         #ifdef FREESPACE_SAVERESTORE_SYSTEM
901         char savegame_mission[MAX_FILENAME_LEN];
902
903         if (state_read_description(savegame_filename, NULL, savegame_mission)) {
904                 return 0;
905         }
906
907         if (SDL_strcasecmp(Game_current_mission_filename, savegame_mission)) {
908                 return 0;
909         }
910
911         return 1;
912         #else
913         return 0;
914         #endif
915 }
916
917 // Decide wether to resume a save game or not
918 // exit:        1       =>      savegame has been restored
919 //                      0       =>      no restore, proceed to briefing
920 //                      -1      =>      don't start mission at all
921 int sim_room_maybe_resume_savegame()
922 {
923         // MWA -- 3/26/98 -- removed all savegame references in game
924         return 0;
925
926         /*
927         char savegame_filename[_MAX_FNAME];
928         int popup_rval = -1, resume_savegame = 0;
929
930         // Generate the save-game filename for this campaign
931         memset(savegame_filename, 0, _MAX_FNAME);
932         mission_campaign_savefile_generate_root(savegame_filename);
933         strcat(savegame_filename, NOX("svg"));
934
935         // Decide if we should offer choice to resume this savegame
936         if ( sim_room_can_resume_savegame(savegame_filename) ) {
937                 popup_rval = popup(0, 3, XSTR("&Cancel",-1), XSTR("&Overwrite",-1), XSTR("&Resume",-1), XSTR("A save game for this mission exists.", -1));
938                 switch ( popup_rval ) {
939                 case 0:
940                 case -1:
941                         resume_savegame = -1;
942                         break;
943                 case 1:
944                         resume_savegame = 0;
945                         break;
946                 case 2:
947                         resume_savegame = 1;
948                         break;
949                 default:
950                         Int3();
951                         resume_savegame = -1;
952                         break;
953                 }
954         } else {
955                 resume_savegame = 0;
956         }
957
958         if (resume_savegame == 1) {
959                 if ( state_restore_all(savegame_filename) == -1 ) {
960                         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));
961                         if (popup_rval == 1) {
962                                 resume_savegame = 0;
963                         } else {
964                                 resume_savegame = -1;
965                         }
966
967                 } else {
968                         resume_savegame = 1;
969                 }
970         }
971
972         // If we are resuming this savegame, then delete the file
973         if (resume_savegame == 1) {
974                 cf_delete(savegame_filename);
975         }
976
977         return resume_savegame;
978         */
979 }
980
981 int readyroom_continue_campaign()
982 {
983         if (mission_campaign_next_mission()) {  // is campaign and next mission valid?
984
985 #if defined(FS2_DEMO) || defined(FS1_DEMO)
986                 int reset_campaign = 0;
987                 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) );
988                 if ( reset_campaign == 1 ) {
989                         mission_campaign_savefile_delete(Campaign.filename);
990                         mission_campaign_load(Campaign.filename);
991                         mission_campaign_next_mission();
992                 } else {
993                         return -1;
994                 }
995 #else
996                 gamesnd_play_iface(SND_GENERAL_FAIL);
997 #ifdef MAKE_FS1
998                 // make it the bottom button so that the graphic looks right
999                 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) );
1000 #else
1001                 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) );
1002 #endif
1003                 return -1;
1004 #endif
1005         }
1006
1007         // set the bit for campaign mode
1008         Game_mode |= GM_CAMPAIGN_MODE;
1009         gameseq_post_event( GS_EVENT_START_GAME );      
1010
1011         return 0;
1012 }
1013
1014 void sim_room_commit()
1015 {
1016         if ((Selected_line >= Num_lines) || !sim_room_lines[Selected_line].filename) {
1017                 gamesnd_play_iface(SND_GENERAL_FAIL);
1018                 return;
1019         }
1020
1021         SDL_strlcpy(Game_current_mission_filename, sim_room_lines[Selected_line].filename, SDL_arraysize(Game_current_mission_filename));
1022
1023         Game_mode &= ~(GM_CAMPAIGN_MODE);                                               // be sure this bit is clear
1024
1025         // don't resume savegame, proceed to briefing
1026         gameseq_post_event(GS_EVENT_START_GAME);
1027         gamesnd_play_iface(SND_COMMIT_PRESSED);
1028 }
1029
1030 int sim_room_button_pressed(int n)
1031 {
1032         switch (n) {
1033                 case SCROLL_UP_BUTTON:
1034                         sim_room_scroll_screen_up();
1035                         break;
1036
1037                 case SCROLL_DOWN_BUTTON:
1038                         sim_room_scroll_screen_down();
1039                         break;
1040
1041                 case MISSION_TAB:
1042 #ifdef OEM_BUILD
1043                         game_feature_not_in_demo_popup();
1044 //                      gamesnd_play_iface(SND_GENERAL_FAIL);
1045                         break;
1046 #else
1047                         Player->readyroom_listing_mode = MODE_MISSIONS;
1048                         Selected_line = Scroll_offset = 0;
1049                         gamesnd_play_iface(SND_USER_SELECT);
1050                         sim_room_build_listing();
1051                         break;
1052 #endif
1053
1054                 case CAMPAIGN_TAB:
1055                         Player->readyroom_listing_mode = MODE_CAMPAIGNS;
1056                         Scroll_offset = 0;
1057                         gamesnd_play_iface(SND_USER_SELECT);
1058                         sim_room_build_listing();
1059                         break;
1060
1061                 case COMMIT_BUTTON:
1062                         sim_room_commit();
1063                         break;
1064
1065                 case HELP_BUTTON:
1066                         launch_context_help();
1067                         gamesnd_play_iface(SND_HELP_PRESSED);
1068                         break;
1069
1070                 case OPTIONS_BUTTON:
1071                         gamesnd_play_iface(SND_SWITCH_SCREENS);
1072                         gameseq_post_event(GS_EVENT_OPTIONS_MENU);
1073                         return 1;
1074
1075                 case TECH_DATABASE_BUTTON:
1076                         gamesnd_play_iface(SND_SWITCH_SCREENS);
1077                         gameseq_post_event(GS_EVENT_TECH_MENU);
1078                         return 1;
1079
1080                 case CUTSCENES_BUTTON:
1081                         gamesnd_play_iface(SND_SWITCH_SCREENS);
1082                         gameseq_post_event(GS_EVENT_GOTO_VIEW_CUTSCENES_SCREEN);
1083                         return 1;
1084
1085                 case CREDITS_BUTTON:
1086                         gamesnd_play_iface(SND_SWITCH_SCREENS);
1087                         gameseq_post_event(GS_EVENT_CREDITS);
1088                         return 1;
1089         }
1090
1091         return 0;
1092 }
1093
1094 // ---------------------------------------------------------------------
1095 // mission_sim_room_init()
1096 //
1097 // Initialize the sim_room assignment screen system.  Called when GS_STATE_sim_room_SCREEN
1098 // is entered.
1099 //
1100 void sim_room_init()
1101 {
1102         int i;
1103         sim_room_buttons *b;
1104         char wild_card[6];
1105
1106         list_x1 = Mission_list_coords[gr_screen.res][0];
1107         list_x2 = Campaign_list_coords[gr_screen.res][0];
1108         list_y = Mission_list_coords[gr_screen.res][1];
1109         list_w1 = Mission_list_coords[gr_screen.res][2];
1110         list_w2 = Campaign_list_coords[gr_screen.res][2];
1111         list_h = Mission_list_coords[gr_screen.res][3];
1112
1113         // force mode to valid range.  I once had a bogus value here.  Don't know how that happened, though.
1114         if (Player->readyroom_listing_mode != MODE_MISSIONS)
1115                 Player->readyroom_listing_mode = MODE_CAMPAIGNS;
1116
1117         *Game_current_mission_filename = 0;
1118         common_set_interface_palette("InterfacePalette");  // set the interface palette
1119         Ui_window.create(0, 0, gr_screen.max_w, gr_screen.max_h, 0);
1120         Ui_window.set_mask_bmap(Sim_mask_filename[gr_screen.res]);
1121
1122         for (i=0; i<NUM_BUTTONS; i++) {
1123                 b = &Buttons[gr_screen.res][i];
1124
1125                 b->button.create(&Ui_window, "", b->x, b->y, 60, 30, (i < 2), 1);
1126                 // set up callback for when a mouse first goes over a button
1127                 b->button.set_highlight_action(common_play_highlight_sound);
1128                 b->button.set_bmaps(b->filename);
1129                 b->button.link_hotspot(b->hotspot);
1130         }
1131
1132 #ifndef MAKE_FS1
1133         // screen/button specific text
1134         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);
1135         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);
1136         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);
1137         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);
1138         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);
1139         
1140         // common tab button text
1141         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);
1142         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);
1143         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);
1144         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);
1145
1146         // misc text - not associated with any buttons
1147         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);
1148         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);
1149 #endif
1150
1151         for (i=0; i<LIST_BUTTONS_MAX; i++) {
1152                 List_buttons[i].create(&Ui_window, "", 0, 0, 60, 30, 0, 1);
1153                 List_buttons[i].hide();
1154                 List_buttons[i].disable();
1155         }
1156
1157         // set up sim_rooms for buttons so we draw the correct animation frame when a key is pressed
1158         Buttons[gr_screen.res][SCROLL_UP_BUTTON].button.set_hotkey(SDLK_PAGEUP);
1159         Buttons[gr_screen.res][SCROLL_DOWN_BUTTON].button.set_hotkey(SDLK_PAGEDOWN);
1160         Buttons[gr_screen.res][COMMIT_BUTTON].button.set_hotkey(KEY_CTRLED | SDLK_RETURN);
1161
1162         Background_bitmap = bm_load(Sim_filename[gr_screen.res]);
1163
1164         // load in help overlay bitmap  
1165         help_overlay_load(SIM_ROOM_OVERLAY);
1166         help_overlay_set_state(SIM_ROOM_OVERLAY,0);
1167
1168         Scroll_offset = Selected_line = 0;
1169
1170         SDL_strlcpy(Cur_campaign, Player->current_campaign, SDL_arraysize(Cur_campaign));
1171         mission_load_up_campaign();
1172         mission_campaign_next_mission();
1173
1174         Num_campaigns = Num_campaign_missions = 0;
1175         Get_file_list_filter = sim_room_campaign_mission_filter;
1176         SDL_snprintf(wild_card, SDL_arraysize(wild_card), "*%s", FS_CAMPAIGN_FILE_EXT);
1177         Num_campaigns = cf_get_file_list(MAX_CAMPAIGNS, Campaign_file_names, CF_TYPE_MISSIONS, wild_card, CF_SORT_NAME);
1178
1179         Hash_table_inited = 0;
1180         if (build_campaign_mission_filename_hash_table()) {
1181                 Hash_table_inited = 1;
1182         }
1183
1184         // HACK
1185         GR_MAYBE_CLEAR_RES(Background_bitmap);
1186         if(Background_bitmap != -1){
1187                 gr_set_bitmap(Background_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1188                 gr_bitmap(0, 0);
1189         }
1190         Ui_window.draw();
1191         gr_flip();              
1192
1193         Get_file_list_filter = sim_room_standalone_mission_filter;
1194         SDL_snprintf(wild_card, SDL_arraysize(wild_card), "*%s", FS_MISSION_FILE_EXT);
1195         Num_standalone_missions = cf_get_file_list(MAX_MISSIONS, Mission_filenames, CF_TYPE_MISSIONS, wild_card, CF_SORT_NAME);
1196
1197         Num_campaign_missions_with_info = Num_standalone_missions_with_info = Standalone_mission_names_inited = Campaign_names_inited = Campaign_mission_names_inited = 0;
1198         sim_room_build_listing();
1199
1200         // load special mission icons
1201         sim_room_load_mission_icons();
1202 }
1203
1204 // ---------------------------------------------------------------------
1205 // sim_room_close()
1206 //
1207 // Cleanup the sim_room assignment screen system.  Called when GS_STATE_sim_room_SCREEN
1208 // is left.
1209 //
1210 void sim_room_close()
1211 {
1212         int i;
1213
1214         for (i=0; i<Num_campaign_missions; i++) {
1215                 if (Campaign_missions[i] != NULL) {
1216                         free(Campaign_missions[i]);
1217                         Campaign_missions[i] = NULL;
1218                 }
1219         }
1220
1221         if (Background_bitmap >= 0)
1222                 bm_unload(Background_bitmap);
1223
1224         if (Standalone_mission_names_inited){
1225                 for (i=0; i<Num_standalone_missions; i++){
1226                         if (Standalone_mission_names[i] != NULL) {
1227                                 free(Standalone_mission_names[i]);
1228                                 Standalone_mission_names[i] = NULL;
1229                         }
1230                         Standalone_mission_flags[i] = 0;
1231                 }
1232         }
1233
1234         if (Campaign_names_inited) {
1235                 for (i=0; i<Num_campaigns; i++) {
1236                         if (Campaign_names[i] != NULL) {
1237                                 free(Campaign_names[i]);
1238                                 Campaign_names[i] = NULL;
1239                         }
1240                 }
1241         }
1242
1243         if (Campaign_mission_names_inited) {
1244                 for (i=0; i<Campaign.num_missions; i++) {
1245                         if (Campaign_mission_names[i] != NULL) {
1246                                 free(Campaign_mission_names[i]);
1247                                 Campaign_mission_names[i] = NULL;
1248                         }
1249                 }
1250         }
1251
1252         for (i=0; i<Num_campaigns; i++) {
1253                 if (Campaign_file_names[i] != NULL) {
1254                         free(Campaign_file_names[i]);
1255                         Campaign_file_names[i] = NULL;
1256                 }
1257         }
1258         
1259
1260         for (i=0; i<Num_standalone_missions; i++) {
1261                 if (Mission_filenames[i] != NULL) {
1262                         free(Mission_filenames[i]);
1263                         Mission_filenames[i] = NULL;
1264                 }
1265         }
1266
1267         // unload the overlay bitmap
1268         help_overlay_unload(SIM_ROOM_OVERLAY);
1269
1270         campaign_mission_hash_table_delete();
1271
1272         Ui_window.destroy();
1273         common_free_interface_palette();                // restore game palette
1274         write_pilot_file();
1275
1276         // unload special mission icons
1277         sim_room_unload_mission_icons();
1278 }
1279
1280 // ---------------------------------------------------------------------
1281 // sim_room_do_frame()
1282 //
1283 // Called once per frame to process user input for the sim_room Assignment Screen
1284 //
1285 void sim_room_do_frame(float frametime)
1286 {
1287         char buf[256];
1288         int i, k, y, line;
1289         int font_height = gr_get_font_height();
1290         int select_tease_line = -1;  // line mouse is down on, but won't be selected until button released      
1291
1292         if ( help_overlay_active(SIM_ROOM_OVERLAY) ) {
1293                 Buttons[gr_screen.res][HELP_BUTTON].button.reset_status();
1294                 Ui_window.set_ignore_gadgets(1);
1295         }
1296
1297         k = Ui_window.process() & ~KEY_DEBUGGED;
1298
1299         if ( (k > 0) || B1_JUST_RELEASED ) {
1300                 if ( help_overlay_active(SIM_ROOM_OVERLAY) ) {
1301                         help_overlay_set_state(SIM_ROOM_OVERLAY, 0);
1302                         Ui_window.set_ignore_gadgets(0);
1303                         k = 0;
1304                 }
1305         }
1306
1307         if ( !help_overlay_active(SIM_ROOM_OVERLAY) ) {
1308                 Ui_window.set_ignore_gadgets(0);
1309         }
1310
1311         switch (k) {
1312                 case SDLK_DOWN:  // scroll list down
1313                         sim_room_scroll_line_down();
1314                         break;
1315
1316                 case SDLK_UP:  // scroll list up
1317                         sim_room_scroll_line_up();
1318                         break;
1319
1320                 case SDLK_ESCAPE:
1321                         gameseq_post_event(GS_EVENT_MAIN_MENU);
1322                         break;
1323
1324                 case KEY_CTRLED | SDLK_UP:
1325                         sim_room_button_pressed(TECH_DATABASE_BUTTON);
1326                         break;
1327
1328                 case KEY_CTRLED | SDLK_DOWN:
1329                         sim_room_button_pressed(CUTSCENES_BUTTON);
1330                         break;
1331
1332                 case SDLK_TAB:
1333                         if (Player->readyroom_listing_mode == MODE_CAMPAIGNS)
1334                                 Player->readyroom_listing_mode = MODE_MISSIONS;
1335                         else
1336                                 Player->readyroom_listing_mode = MODE_CAMPAIGNS;
1337
1338                         Selected_line = Scroll_offset = 0;
1339                         gamesnd_play_iface(SND_USER_SELECT);
1340                         sim_room_build_listing();
1341                         break;
1342
1343                 case SDLK_F2:
1344                         gamesnd_play_iface(SND_SWITCH_SCREENS);
1345                         gameseq_post_event(GS_EVENT_OPTIONS_MENU);
1346                         break;
1347         }       // end switch
1348
1349         for (i=0; i<NUM_BUTTONS; i++){
1350                 if (Buttons[gr_screen.res][i].button.pressed()){
1351                         if (sim_room_button_pressed(i)){
1352                                 return;
1353                         }
1354                 }
1355         }
1356
1357         for (i=0; i<LIST_BUTTONS_MAX; i++) {
1358                 if (List_buttons[i].is_mouse_on())
1359                         select_tease_line = i + Scroll_offset;
1360         
1361                 if (List_buttons[i].pressed()) {
1362                         Selected_line = i + Scroll_offset;
1363                         gamesnd_play_iface(SND_USER_SELECT);
1364                 }
1365         }
1366
1367         GR_MAYBE_CLEAR_RES(Background_bitmap);
1368         if (Background_bitmap >= 0) {
1369                 gr_set_bitmap(Background_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1370                 gr_bitmap(0, 0);
1371         }
1372
1373         Ui_window.draw();
1374
1375         for (i=TECH_DATABASE_BUTTON; i<=CREDITS_BUTTON; i++){
1376                 if (Buttons[gr_screen.res][i].button.button_down()){
1377                         break;
1378                 }
1379         }
1380
1381         if (i > CREDITS_BUTTON){
1382                 Buttons[gr_screen.res][SIMULATOR_BUTTON].button.draw_forced(2);
1383         }
1384
1385         if (!Buttons[gr_screen.res][CAMPAIGN_TAB].button.button_down() && !Buttons[gr_screen.res][MISSION_TAB].button.button_down()) {
1386                 if (Player->readyroom_listing_mode == MODE_CAMPAIGNS){
1387                         Buttons[gr_screen.res][CAMPAIGN_TAB].button.draw_forced(2);
1388                 } else if (Player->readyroom_listing_mode == MODE_MISSIONS){
1389                         Buttons[gr_screen.res][MISSION_TAB].button.draw_forced(2);
1390                 }
1391         }
1392
1393         gr_set_font(FONT1);
1394         if (Player->readyroom_listing_mode == MODE_CAMPAIGNS) {
1395                 gr_set_color_fast(&Color_text_heading);
1396                 SDL_strlcpy(buf, Campaign.name, SDL_arraysize(buf));
1397                 gr_force_fit_string(buf, 255, list_w1);
1398                 gr_printf(list_x1, Mission_list_coords[gr_screen.res][1], buf);
1399
1400                 if (Campaign.filename[0]) {
1401                         SDL_snprintf(buf, SDL_arraysize(buf), NOX("%s%s"), Campaign.filename, FS_CAMPAIGN_FILE_EXT);
1402                         gr_force_fit_string(buf, 255, list_w2);
1403                         gr_printf(list_x2, Mission_list_coords[gr_screen.res][1], buf);         
1404
1405                         // blit the proper icons if necessary
1406                         char full_name[256];
1407                         SDL_strlcpy(full_name, cf_add_ext(Campaign.filename,FS_CAMPAIGN_FILE_EXT), SDL_arraysize(full_name));
1408                         fs_builtin_mission *fb = game_find_builtin_mission(full_name);
1409                         if(fb != NULL){
1410                                 // sim_room_blit_icons(0, Mission_list_coords[gr_screen.res][1], fb, 0);
1411                         }
1412                 }
1413         }
1414
1415         line = Scroll_offset;
1416         while (sim_room_line_query_visible(line)) {
1417                 y = list_y + sim_room_lines[line].y - sim_room_lines[Scroll_offset].y;
1418
1419                 if (sim_room_lines[line].type != READYROOM_LINE_CAMPAIGN) {
1420                         List_buttons[line - Scroll_offset].update_dimensions(list_x1, y, list_x2 + list_w2 - list_x1, font_height);
1421                         List_buttons[line - Scroll_offset].enable();
1422
1423                 } else
1424                         List_buttons[line - Scroll_offset].disable();
1425
1426                 if (line == Selected_line)
1427                         gr_set_color_fast(&Color_text_selected);
1428                 else if (line == select_tease_line)
1429                         gr_set_color_fast(&Color_text_subselected);
1430                 else
1431                         gr_set_color_fast(&Color_text_normal);
1432
1433                 SDL_strlcpy(buf, sim_room_lines[line].name, SDL_arraysize(buf));
1434                 gr_force_fit_string(buf, 255, list_x1 + list_w1 - sim_room_lines[line].x);
1435                 gr_printf(sim_room_lines[line].x, y, buf);
1436
1437                 if (sim_room_lines[line].filename) {
1438                         SDL_strlcpy(buf, sim_room_lines[line].filename, SDL_arraysize(buf));
1439                         gr_force_fit_string(buf, 255, list_w2);
1440                         gr_printf(list_x2, y, buf);
1441                 }
1442
1443                 // blit additional icon information
1444                 sim_room_blit_icons(line, y);
1445
1446                 line++;
1447         }
1448
1449         i = line - Scroll_offset;
1450         while (i < LIST_BUTTONS_MAX)
1451                 List_buttons[i++].disable();
1452
1453         // blit help overlay if active
1454         help_overlay_maybe_blit(SIM_ROOM_OVERLAY);
1455
1456         gr_flip();
1457 }
1458
1459 void sim_room_blit_icons(int line_index, int y_start, fs_builtin_mission *fb, int is_md)
1460 {
1461         int is_from_volition = 0;       
1462
1463         // determine icon status
1464         if(fb == NULL){
1465                 is_from_volition = (sim_room_lines[line_index].flags & READYROOM_FLAG_FROM_VOLITION) ? 1 : 0;           
1466 #ifdef MAKE_FS1
1467                 is_md = (sim_room_lines[line_index].flags & READYROOM_FLAG_FROM_MDISK) ? 1 : 0;
1468 #endif
1469         } else {
1470                 is_from_volition = (fb->flags & FSB_FROM_VOLITION) ? 1 : 0;             
1471 #ifdef MAKE_FS1
1472                 is_md = (fb->flags & FSB_FROM_MDISK) ? 1 : 0;
1473 #endif
1474         }
1475
1476         // if the line is flagged as a volition file
1477         if(is_from_volition && (Mission_icon_bitmaps[MISSION_ICON_VOLITION] >= 0)){             
1478                 gr_set_bitmap(Mission_icon_bitmaps[MISSION_ICON_VOLITION], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1479                 gr_bitmap(Sim_volition_icon_x[gr_screen.res], y_start + MISSION_ICON_VOLITION_Y_OFFSET);
1480         }       
1481
1482 #ifdef MAKE_FS1
1483         if(is_md && (Mission_icon_bitmaps[MISSION_ICON_MDISK] >= 0)){
1484                 gr_set_bitmap(Mission_icon_bitmaps[MISSION_ICON_MDISK], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1485                 gr_bitmap(Sim_silent_icon_x[gr_screen.res], y_start + MISSION_ICON_MDISK_Y_OFFSET);
1486         }
1487 #endif
1488 }
1489
1490 ///  Campaign room stuff below
1491 int Cr_list_coords[GR_NUM_RESOLUTIONS][4] = {
1492         { // GR_640
1493 #ifdef MAKE_FS1
1494                 47, 55, 561, 195
1495 #else
1496                 47, 21, 565, 233
1497 #endif
1498         },
1499         { // GR_1024
1500                 64, 34, 916, 373
1501         }
1502 };
1503
1504 int Cr_info_coords[GR_NUM_RESOLUTIONS][4] = {
1505         { // GR_640
1506 #ifdef MAKE_FS1
1507                 64, 280, 407, 75
1508 #else
1509                 28, 267, 476, 103
1510 #endif
1511         },
1512         { // GR_1024
1513                 45, 427, 761, 165
1514         },
1515 };
1516
1517 #define CR_NUM_BUTTONS                                  6
1518
1519 #define CR_SCROLL_UP_BUTTON                     0
1520 #define CR_SCROLL_DOWN_BUTTON                   1
1521 #define CR_SCROLL_INFO_UP_BUTTON                2
1522 #define CR_SCROLL_INFO_DOWN_BUTTON      3
1523 #define CR_RESET_BUTTON                                 4
1524 #define CR_COMMIT_BUTTON                                5
1525
1526 #define MAX_INFO_LINES          20
1527
1528 #define MAX_INFO_LINE_LEN       256
1529
1530 ui_button_info Cr_buttons[GR_NUM_RESOLUTIONS][CR_NUM_BUTTONS] = {
1531         { // GR_640
1532 #ifdef MAKE_FS1
1533                 ui_button_info("CAB_00",        0,              95,             -1,     -1,     0),             // scroll up
1534                 ui_button_info("CAB_01",        0,              142,    -1,     -1,     1),             // scroll down
1535                 ui_button_info("CAB_02",        17,             259,    -1,     -1,     2),             // info scroll up
1536                 ui_button_info("CAB_03",        17,             307,    -1,     -1,     3),             // info scroll down
1537                 ui_button_info("CAB_07",        545,    323,    -1,     -1,     7),             // reset
1538                 ui_button_info("CAB_04",        561,    411,    -1,     -1,     4),             // select
1539 #else
1540                 ui_button_info("CAB_00",        2,              42,     -1,     -1,     0),
1541                 ui_button_info("CAB_01",        2,              89,     -1,     -1,     1),
1542                 ui_button_info("CAB_02",        2,              279,    -1,     -1,     2),
1543                 ui_button_info("CAB_03",        2,              325,    -1,     -1,     3),
1544                 ui_button_info("CAB_04",        579,    353,    -1,     -1,     4),
1545                 ui_button_info("CAB_05",        575,    434,    -1,     -1,     5),
1546 #endif
1547         },
1548         { // GR_1024
1549                 ui_button_info("2_CAB_00",      3,              68,     -1,     -1,     0),
1550                 ui_button_info("2_CAB_01",      3,              142,    -1,     -1,     1),
1551                 ui_button_info("2_CAB_02",      3,              446,    -1,     -1,     2),
1552                 ui_button_info("2_CAB_03",      3,              520,    -1,     -1,     3),
1553                 ui_button_info("2_CAB_04",      927,    565,    -1,     -1,     4),
1554                 ui_button_info("2_CAB_05",      920,    694,    -1,     -1,     5),
1555         }
1556 };
1557
1558 // text
1559 #ifndef MAKE_FS1
1560 #define CR_NUM_TEXT                     3
1561
1562 UI_XSTR Cr_text[GR_NUM_RESOLUTIONS][CR_NUM_TEXT] = {
1563         { // GR_640
1564                 { "Restart",            1403,           569,    326, UI_XSTR_COLOR_GREEN,       -1, &Cr_buttons[0][CR_RESET_BUTTON].button },
1565                 { "Campaign",           1404,           569,    337, UI_XSTR_COLOR_GREEN,       -1, &Cr_buttons[0][CR_RESET_BUTTON].button },
1566                 { "Select",                     1409,           568,    413, UI_XSTR_COLOR_PINK,        -1, &Cr_buttons[0][CR_COMMIT_BUTTON].button },
1567         },
1568         { // GR_1024
1569                 { "Restart",            1403,           922,    523, UI_XSTR_COLOR_GREEN,       -1, &Cr_buttons[1][CR_RESET_BUTTON].button },
1570                 { "Campaign",           1404,           922,    538, UI_XSTR_COLOR_GREEN,       -1, &Cr_buttons[1][CR_RESET_BUTTON].button },
1571                 { "Select",                     1409,           921,    665, UI_XSTR_COLOR_PINK,        -1, &Cr_buttons[1][CR_COMMIT_BUTTON].button },
1572         }
1573 };
1574 #endif
1575
1576 static int Num_desc_lines;
1577 static int Desc_scroll_offset;
1578 static int Selected_campaign_index;
1579 static int Active_campaign_index;
1580
1581 char *Info_text_ptrs[MAX_INFO_LINES];
1582 int Num_info_lines, Info_text_line_size[MAX_INFO_LINES];
1583
1584 void campaign_room_build_listing()
1585 {
1586         int c, i, y, type, max_players; 
1587         int font_height = gr_get_font_height();
1588         char name[NAME_LENGTH];
1589
1590         Num_lines = y = 0;
1591         if (!Campaign_names_inited) {
1592                 for (i=0; i<Num_campaigns; i++) {
1593                         Campaign_names[i] = NULL;
1594                         if ( mission_campaign_get_info(Campaign_file_names[i], name, &type, &max_players) ) {
1595 #ifdef PD_BUILD
1596                                 if((game_find_builtin_mission(name) == NULL) && !strstr(name, "peterdrake")){
1597                                         continue;
1598                                 } else {
1599                                         Campaign_names[i] = strdup(name);
1600                                 }
1601 #else
1602                                 if (type == CAMPAIGN_TYPE_SINGLE){
1603                                         Campaign_names[i] = strdup(name);
1604                                 }
1605 #endif
1606                         }
1607                 }
1608
1609                 Campaign_names_inited = 1;
1610         }
1611
1612         for (c=0; c<Num_campaigns; c++) {
1613                 if (Campaign_names[c]) {
1614
1615                         // determine some extra information
1616                         int flags = 0;
1617                         fs_builtin_mission *fb = game_find_builtin_mission(Campaign_file_names[c]);
1618                         if(fb != NULL){
1619                                 if(fb->flags & FSB_FROM_VOLITION){
1620                                         flags |= READYROOM_FLAG_FROM_VOLITION;
1621                                 }                               
1622 #ifdef MAKE_FS1
1623                                 if(fb->flags & FSB_FROM_MDISK){
1624                                         flags |= READYROOM_FLAG_FROM_MDISK;
1625                                 }
1626 #endif
1627                         }
1628
1629                         sim_room_line_add(READYROOM_LINE_CAMPAIGN, Campaign_names[c], Campaign_file_names[c], Cr_list_coords[gr_screen.res][0], y, flags);
1630                         y += font_height + 2;
1631                 }
1632         }
1633 }
1634
1635 void set_new_campaign_line(int n)
1636 {
1637         char *str;
1638
1639         Selected_campaign_index = n;
1640         str = Campaign_descs[Selected_campaign_index];
1641         Num_info_lines = 0;
1642         if (str) {
1643                 Num_info_lines = split_str(str, Cr_info_coords[gr_screen.res][2], Info_text_line_size, Info_text_ptrs, MAX_INFO_LINES);
1644                 SDL_assert(Num_info_lines >= 0);
1645         }
1646
1647         Desc_scroll_offset = 0;
1648 }
1649
1650 void campaign_room_scroll_info_up()
1651 {
1652         if (Desc_scroll_offset) {
1653                 Desc_scroll_offset--;
1654                 gamesnd_play_iface(SND_SCROLL);
1655
1656         } else
1657                 gamesnd_play_iface(SND_GENERAL_FAIL);
1658 }
1659
1660 void campaign_room_scroll_info_down()
1661 {
1662         if ( (Num_info_lines - Desc_scroll_offset) * gr_get_font_height() > Cr_info_coords[gr_screen.res][3]) {
1663                 Desc_scroll_offset++;
1664                 gamesnd_play_iface(SND_SCROLL);
1665
1666         } else
1667                 gamesnd_play_iface(SND_GENERAL_FAIL);
1668 }
1669
1670 // returns: 0 = success, !0 = aborted or failed
1671 int campaign_room_reset_campaign(int n)
1672 {
1673         char *filename;
1674         int z;
1675
1676         // 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]);
1677         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));
1678         if (z == 1) {
1679                 int len = strlen(Campaign_file_names[n]) + 5;
1680                 filename = (char *) malloc(len);
1681                 SDL_strlcpy(filename, Campaign_file_names[n], len);
1682                 SDL_strlcat(filename, FS_CAMPAIGN_FILE_EXT, len);
1683
1684                 mission_campaign_savefile_delete(filename);
1685                 mission_campaign_load(filename);
1686                 mission_campaign_next_mission();
1687                 return 0;
1688         }
1689
1690         return 1;
1691 }
1692
1693 void campaign_room_commit()
1694 {
1695         if (Selected_campaign_index < 0) {
1696                 gamesnd_play_iface(SND_GENERAL_FAIL);
1697                 return;
1698         }
1699
1700         if (SDL_strcasecmp(Campaign_file_names[Selected_campaign_index], Campaign.filename)) {  // new campaign selected
1701                 if ((Active_campaign_index >= 0) && campaign_room_reset_campaign(Active_campaign_index)) {
1702                         gamesnd_play_iface(SND_GENERAL_FAIL);
1703                         return;
1704                 }
1705
1706                 mission_campaign_savefile_delete(Campaign_file_names[Selected_campaign_index]);
1707                 mission_campaign_load(Campaign_file_names[Selected_campaign_index]);
1708                 SDL_strlcpy(Player->current_campaign, Campaign.filename, SDL_arraysize(Player->current_campaign));  // track new campaign for player
1709         }
1710
1711         if (mission_campaign_next_mission()) {  // is campaign and next mission valid?
1712                 gamesnd_play_iface(SND_GENERAL_FAIL);
1713                 return;
1714         }
1715
1716         gameseq_post_event(GS_EVENT_MAIN_MENU);
1717         gamesnd_play_iface(SND_COMMIT_PRESSED);
1718 }
1719
1720 int campaign_room_button_pressed(int n)
1721 {
1722         switch (n) {
1723                 case CR_SCROLL_UP_BUTTON:
1724                         sim_room_scroll_screen_up();
1725                         break;
1726
1727                 case CR_SCROLL_DOWN_BUTTON:
1728                         sim_room_scroll_screen_down();
1729                         break;
1730
1731                 case CR_SCROLL_INFO_UP_BUTTON:
1732                         campaign_room_scroll_info_up();
1733                         break;
1734
1735                 case CR_SCROLL_INFO_DOWN_BUTTON:
1736                         campaign_room_scroll_info_down();
1737                         break;
1738
1739                 case CR_COMMIT_BUTTON:
1740                         campaign_room_commit();
1741                         break;
1742
1743                 /*
1744                 case CR_HELP_BUTTON:
1745                         launch_context_help();
1746                         gamesnd_play_iface(SND_HELP_PRESSED);
1747                         break;
1748
1749                 case CR_OPTIONS_BUTTON:
1750                         gamesnd_play_iface(SND_SWITCH_SCREENS);
1751                         gameseq_post_event(GS_EVENT_OPTIONS_MENU);
1752                         return 1;
1753                 */
1754
1755                 case CR_RESET_BUTTON:
1756                         if ( (Active_campaign_index < 0) || (Active_campaign_index >= Num_campaigns) )
1757                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1758                         else if (campaign_room_reset_campaign(Active_campaign_index))
1759                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1760                         else
1761                                 gamesnd_play_iface(SND_USER_SELECT);
1762
1763                         break;
1764         }
1765
1766         return 0;
1767 }
1768
1769 void campaign_room_init()
1770 {
1771         int i, j, load_failed;
1772         ui_button_info *b;
1773         char wild_card[6];
1774
1775         list_h = Mission_list_coords[gr_screen.res][3];
1776
1777 #ifdef MAKE_FS1
1778         common_set_interface_palette("InterfacePalette");  // set the interface palette
1779 #endif
1780         Ui_window.create(0, 0, gr_screen.max_w, gr_screen.max_h, 0);
1781         Ui_window.set_mask_bmap(Campaign_mask_filename[gr_screen.res]);
1782
1783         for (i=0; i<CR_NUM_BUTTONS; i++) {
1784                 b = &Cr_buttons[gr_screen.res][i];
1785
1786                 b->button.create(&Ui_window, "", b->x, b->y, 60, 30, (i < 2), 1);
1787                 // set up callback for when a mouse first goes over a button
1788                 b->button.set_highlight_action(common_play_highlight_sound);
1789                 b->button.set_bmaps(b->filename);
1790                 b->button.link_hotspot(b->hotspot);
1791         }
1792
1793         for (i=0; i<LIST_BUTTONS_MAX; i++) {
1794                 List_buttons[i].create(&Ui_window, "", 0, 0, 60, 30, 0, 1);
1795                 List_buttons[i].hide();
1796                 List_buttons[i].disable();
1797         }
1798
1799 #ifndef MAKE_FS1
1800         // add xstrs
1801         for(i=0; i<CR_NUM_TEXT; i++){
1802                 Ui_window.add_XSTR(&Cr_text[gr_screen.res][i]);
1803         }
1804 #endif
1805
1806         // set up sim_rooms for buttons so we draw the correct animation frame when a key is pressed
1807         Cr_buttons[gr_screen.res][CR_SCROLL_UP_BUTTON].button.set_hotkey(SDLK_PAGEUP);
1808         Cr_buttons[gr_screen.res][CR_SCROLL_DOWN_BUTTON].button.set_hotkey(SDLK_PAGEDOWN);
1809         Cr_buttons[gr_screen.res][CR_RESET_BUTTON].button.set_hotkey(SDLK_DELETE);
1810         Cr_buttons[gr_screen.res][CR_COMMIT_BUTTON].button.set_hotkey(KEY_CTRLED | SDLK_RETURN);
1811         // Cr_buttons[gr_screen.res][CR_HELP_BUTTON].button.set_hotkey(KEY_F2);
1812
1813         Background_bitmap = bm_load(Campaign_filename[gr_screen.res]);
1814
1815         // load in help overlay bitmap  
1816         help_overlay_load(CAMPAIGN_ROOM_OVERLAY);
1817         help_overlay_set_state(CAMPAIGN_ROOM_OVERLAY,0);
1818
1819         Num_desc_lines = 0;
1820
1821         Desc_scroll_offset = Scroll_offset = 0;
1822         load_failed = mission_load_up_campaign();
1823         if (!load_failed)
1824                 mission_campaign_next_mission();
1825
1826         else {
1827                 Campaign.filename[0] = 0;
1828                 Campaign.num_missions = 0;
1829         }
1830
1831         Num_campaigns = 0;
1832
1833         Get_file_list_filter = campaign_room_campaign_filter;
1834         SDL_snprintf(wild_card, SDL_arraysize(wild_card), "*%s", FS_CAMPAIGN_FILE_EXT);
1835         Num_campaigns = cf_get_file_list(MAX_CAMPAIGNS, Campaign_file_names, CF_TYPE_MISSIONS, wild_card, CF_SORT_NAME);
1836
1837         for (i=0; i<Num_campaigns; i++) {
1838                 for (j=0; j<Num_campaigns; j++) {
1839                         if (Campaign_file_names_temp[j]) {
1840                                 if (!strncmp(Campaign_file_names[i], Campaign_file_names_temp[j], strlen(Campaign_file_names_temp[j]) - 4)) {
1841                                         Campaign_descs[i] = Campaign_descs_temp[j];
1842                                         free(Campaign_file_names_temp[j]);
1843                                         Campaign_file_names_temp[j] = NULL;
1844                                         break;
1845                                 }
1846                         }
1847                 }
1848
1849                 SDL_assert(j < Num_campaigns);  // Campaign not found?  How is that possible?
1850         }
1851
1852         Campaign_names_inited = 0;
1853         campaign_room_build_listing();
1854
1855         Selected_campaign_index = Active_campaign_index = -1;
1856         if (!load_failed) {
1857                 for (i=0; i<Num_campaigns; i++)
1858                         if (!SDL_strcasecmp(Campaign_file_names[i], Campaign.filename)) {
1859                                 set_new_campaign_line(i);
1860                                 Active_campaign_index = i;
1861                                 break;
1862                         }
1863         }
1864 }
1865
1866 void campaign_room_close()
1867 {
1868         int i;
1869
1870         for (i=0; i<Num_campaigns; i++) {
1871                 if (Campaign_descs[i] != NULL) {
1872                         free(Campaign_descs[i]);
1873                         Campaign_descs[i] = NULL;
1874                 }
1875         }
1876
1877         if (Background_bitmap >= 0)
1878                 bm_unload(Background_bitmap);
1879
1880         if (Campaign_names_inited) {
1881                 for (i=0; i<Num_campaigns; i++) {
1882                         if (Campaign_names[i] != NULL) {
1883                                 free(Campaign_names[i]);
1884                                 Campaign_names[i] = NULL;
1885                         }
1886                 }
1887         }
1888
1889         for (i=0; i<Num_campaigns; i++) {
1890                 if (Campaign_file_names[i] != NULL) {
1891                         free(Campaign_file_names[i]);
1892                         Campaign_file_names[i] = NULL;
1893                 }
1894         }
1895
1896         // unload the overlay bitmap
1897         help_overlay_unload(CAMPAIGN_ROOM_OVERLAY);
1898
1899         Ui_window.destroy();
1900         common_free_interface_palette();                // restore game palette
1901         write_pilot_file();
1902 }
1903
1904 void campaign_room_do_frame(float frametime)
1905 {
1906         char buf[256];
1907         char line_text[MAX_INFO_LINE_LEN];
1908         int i, k, y, line;
1909         int font_height = gr_get_font_height();
1910         int select_tease_line = -1;  // line mouse is down on, but won't be selected until button released
1911
1912         if ( help_overlay_active(CAMPAIGN_ROOM_OVERLAY) ) {
1913                 // Cr_buttons[gr_screen.res][CR_HELP_BUTTON].button.reset_status();
1914                 Ui_window.set_ignore_gadgets(1);
1915         }
1916
1917         k = Ui_window.process() & ~KEY_DEBUGGED;
1918
1919         if ( (k > 0) || B1_JUST_RELEASED ) {
1920                 if ( help_overlay_active(CAMPAIGN_ROOM_OVERLAY) ) {
1921                         help_overlay_set_state(CAMPAIGN_ROOM_OVERLAY, 0);
1922                         Ui_window.set_ignore_gadgets(0);
1923                         k = 0;
1924                 }
1925         }
1926
1927         if ( !help_overlay_active(CAMPAIGN_ROOM_OVERLAY) ) {
1928                 Ui_window.set_ignore_gadgets(0);
1929         }
1930
1931         switch (k) {
1932                 case SDLK_DOWN:  // scroll list down
1933                         if (Selected_campaign_index < Num_campaigns - 1) {
1934                                 set_new_campaign_line(Selected_campaign_index + 1);
1935                                 gamesnd_play_iface(SND_SCROLL);
1936
1937                         } else
1938                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1939
1940                         break;
1941
1942                 case SDLK_UP:  // scroll list up
1943                         if (Selected_campaign_index < 0)
1944                                 Selected_campaign_index = 1;
1945
1946                         if (Selected_campaign_index) {
1947                                 set_new_campaign_line(Selected_campaign_index - 1);
1948                                 gamesnd_play_iface(SND_SCROLL);
1949
1950                         } else
1951                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1952
1953                         break;
1954
1955                 case SDLK_ESCAPE:
1956                         gameseq_post_event(GS_EVENT_MAIN_MENU);
1957                         break;
1958         }       // end switch
1959
1960         for (i=0; i<CR_NUM_BUTTONS; i++){
1961                 if (Cr_buttons[gr_screen.res][i].button.pressed()){
1962                         if (campaign_room_button_pressed(i)){
1963                                 return;
1964                         }
1965                 }
1966         }
1967
1968         for (i=0; i<LIST_BUTTONS_MAX; i++) {
1969                 if (List_buttons[i].is_mouse_on()){
1970                         select_tease_line = i + Scroll_offset;
1971                 }
1972         
1973                 if (List_buttons[i].pressed()) {
1974                         set_new_campaign_line(i + Scroll_offset);
1975                         gamesnd_play_iface(SND_USER_SELECT);
1976                 }
1977         }
1978
1979         GR_MAYBE_CLEAR_RES(Background_bitmap);
1980         if (Background_bitmap >= 0) {
1981                 gr_set_bitmap(Background_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1982                 gr_bitmap(0, 0);
1983         }
1984
1985         Ui_window.draw();
1986
1987         gr_set_font(FONT1);
1988         line = Scroll_offset;
1989         while (sim_room_line_query_visible(line)) {
1990                 y = Cr_list_coords[gr_screen.res][1] + sim_room_lines[line].y - sim_room_lines[Scroll_offset].y;
1991
1992                 List_buttons[line - Scroll_offset].update_dimensions(Cr_list_coords[gr_screen.res][0], y, Cr_list_coords[gr_screen.res][2], font_height);
1993                 List_buttons[line - Scroll_offset].enable();
1994
1995                 if (!SDL_strcasecmp(sim_room_lines[line].filename, Campaign.filename)) {
1996                         gr_set_color_fast(&Color_white);
1997                         i = y + font_height / 2 - 1;
1998                         gr_circle(Cr_list_coords[gr_screen.res][0] - 6, i, 5);
1999
2000                         gr_set_color_fast(&Color_bright_white);
2001                         gr_line(Cr_list_coords[gr_screen.res][0] - 10, i, Cr_list_coords[gr_screen.res][0] - 8, i);
2002                         gr_line(Cr_list_coords[gr_screen.res][0] - 6, i - 4, Cr_list_coords[gr_screen.res][0] - 6, i - 2);
2003                         gr_line(Cr_list_coords[gr_screen.res][0] - 4, i, Cr_list_coords[gr_screen.res][0] - 2, i);
2004                         gr_line(Cr_list_coords[gr_screen.res][0] - 6, i + 2, Cr_list_coords[gr_screen.res][0] - 6, i + 4);
2005                 }
2006
2007                 if (line == Selected_campaign_index)
2008                         gr_set_color_fast(&Color_text_selected);
2009                 else if (line == select_tease_line)
2010                         gr_set_color_fast(&Color_text_subselected);
2011                 else
2012                         gr_set_color_fast(&Color_text_normal);
2013
2014                 SDL_strlcpy(buf, sim_room_lines[line].name, SDL_arraysize(buf));
2015                 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);
2016                 gr_printf(sim_room_lines[line].x, y, buf);
2017                 line++;
2018         }
2019
2020         i = line - Scroll_offset;
2021         while (i < LIST_BUTTONS_MAX)
2022                 List_buttons[i++].disable();
2023
2024         y = 0;
2025         i = Desc_scroll_offset;
2026         gr_set_color_fast(&Color_text_normal);
2027
2028         while (y + font_height <= Cr_info_coords[gr_screen.res][3]) {
2029                 if (i >= Num_info_lines)
2030                         break;
2031
2032                 SDL_assert(Info_text_line_size[i] < MAX_INFO_LINE_LEN);
2033                 int len = SDL_min(Info_text_line_size[i] + 1, MAX_INFO_LINE_LEN);
2034                 SDL_strlcpy(line_text, Info_text_ptrs[i], len);
2035                 drop_white_space(line_text);
2036                 gr_string(Cr_info_coords[gr_screen.res][0], Cr_info_coords[gr_screen.res][1] + y, line_text);
2037                 y += font_height;
2038                 i++;
2039         }
2040
2041         // blit help overlay if active
2042         help_overlay_maybe_blit(CAMPAIGN_ROOM_OVERLAY);
2043
2044         gr_flip();
2045 }
2046
2047 void sim_room_load_mission_icons()
2048 {
2049         int idx;
2050         
2051         // load all bitmaps
2052         for(idx=0; idx<NUM_MISSION_ICONS; idx++){
2053                 Mission_icon_bitmaps[idx] = -1;
2054                 Mission_icon_bitmaps[idx] = bm_load(Mission_icon_bitmap_filenames[idx]);
2055         }
2056 }
2057
2058 void sim_room_unload_mission_icons()
2059 {
2060         int idx;
2061
2062         // unload all bitmaps
2063         for(idx=0; idx<NUM_MISSION_ICONS; idx++){
2064                 if(Mission_icon_bitmaps[idx] >= 0){
2065                         bm_unload(Mission_icon_bitmaps[idx]);
2066                         Mission_icon_bitmaps[idx] = -1;
2067                 }
2068         }
2069 }
2070