]> icculus.org git repositories - taylor/freespace2.git/blob - src/missionui/missionscreencommon.cpp
fix issue with looping audio streams
[taylor/freespace2.git] / src / missionui / missionscreencommon.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/MissionUI/MissionScreenCommon.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * $Log$
16  * Revision 1.11  2005/10/02 09:30:10  taylor
17  * sync up rest of big-endian network changes.  it should at least be as good as what's in FS2_Open now, only better :)
18  *
19  * Revision 1.10  2005/08/12 08:58:41  taylor
20  * fix the strange mouse click issues on briefing, ship select and weapon select screens
21  *
22  * Revision 1.9  2004/09/20 01:31:44  theoddone33
23  * GCC 3.4 fixes.
24  *
25  * Revision 1.8  2004/07/04 11:31:43  taylor
26  * amd64 support, compiler warning fixes, don't use software rendering
27  *
28  * Revision 1.7  2003/08/03 16:10:29  taylor
29  * cleanup; compile warning fixes
30  *
31  * Revision 1.6  2003/06/11 18:30:33  taylor
32  * plug memory leaks
33  *
34  * Revision 1.5  2003/05/25 02:30:43  taylor
35  * Freespace 1 support
36  *
37  * Revision 1.4  2002/06/09 04:41:23  relnev
38  * added copyright header
39  *
40  * Revision 1.3  2002/06/01 07:12:33  relnev
41  * a few NDEBUG updates.
42  *
43  * removed a few warnings.
44  *
45  * Revision 1.2  2002/05/07 03:16:46  theoddone33
46  * The Great Newline Fix
47  *
48  * Revision 1.1.1.1  2002/05/03 03:28:10  root
49  * Initial import.
50  *
51  * 
52  * 13    10/14/99 2:51p Jefff
53  * localiztion fixes
54  * 
55  * 12    8/05/99 3:40p Jefff
56  * hi-res text adjustments
57  * 
58  * 11    7/15/99 9:20a Andsager
59  * FS2_DEMO initial checkin
60  * 
61  * 10    2/01/99 5:55p Dave
62  * Removed the idea of explicit bitmaps for buttons. Fixed text
63  * highlighting for disabled gadgets.
64  * 
65  * 9     1/30/99 7:33p Neilk
66  * Fixed coords problems for mission briefing screens
67  * 
68  * 8     1/30/99 5:08p Dave
69  * More new hi-res stuff.Support for nice D3D textures.
70  * 
71  * 7     1/29/99 4:17p Dave
72  * New interface screens.
73  * 
74  * 6     1/24/99 11:37p Dave
75  * First full rev of beam weapons. Very customizable. Removed some bogus
76  * Int3()'s in low level net code.
77  * 
78  * 5     11/30/98 1:07p Dave
79  * 16 bit conversion, first run.
80  * 
81  * 4     11/17/98 11:12a Dave
82  * Removed player identification by address. Now assign explicit id #'s.
83  * 
84  * 3     10/16/98 9:40a Andsager
85  * Remove ".h" files from model.h
86  * 
87  * 2     10/07/98 10:53a Dave
88  * Initial checkin.
89  * 
90  * 1     10/07/98 10:50a Dave
91  * 
92  * 85    6/09/98 10:31a Hoffoss
93  * Created index numbers for all xstr() references.  Any new xstr() stuff
94  * added from here on out should be added to the end if the list.  The
95  * current list count can be found in FreeSpace.cpp (search for
96  * XSTR_SIZE).
97  * 
98  * 84    5/08/98 7:52p Lawrance
99  * fix bug where wing slot icons were not getting loaded
100  * 
101  * 83    5/08/98 10:17a Lawrance
102  * don't play briefing slide-in animation if detail level is below high
103  * 
104  * 82    5/06/98 11:50p Lawrance
105  * Clean up help overlay code for loadout screens
106  * 
107  * 81    5/03/98 1:55a Lawrance
108  * Fix some sound problems with loadout screens
109  * 
110  * 80    4/30/98 6:03p Lawrance
111  * Make drag and drop work better.
112  * 
113  * 79    4/22/98 5:52p Dave
114  * Large reworking of endgame sequencing. Updated host options screen for
115  * new artwork. Put in checks to end game if host leaves or if team
116  * captains leave mid-game. 
117  * 
118  * 78    4/07/98 7:51p Hoffoss
119  * Implemented changed to options screen due to artwork changes.
120  * 
121  * 77    4/07/98 5:42p Dave
122  * Put in support for ui display of voice system status (recording,
123  * playing back, etc). Make sure main hall music is stopped before
124  * entering a multiplayer game via ingame join.
125  * 
126  * 76    4/02/98 11:40a Lawrance
127  * check for #ifdef DEMO instead of #ifdef DEMO_RELEASE
128  * 
129  * 75    4/01/98 11:19p Dave
130  * Put in auto-loading of xferred pilot pic files. Grey out background
131  * behind pinfo popup. Put a chatbox message in when players are kicked.
132  * Moved mission title down in briefing. Other ui fixes.
133  * 
134  * 74    4/01/98 9:21p John
135  * Made NDEBUG, optimized build with no warnings or errors.
136  * 
137  * 73    3/31/98 11:47p Lawrance
138  * Fix some bugs related to wingmen selection when doing a quick mission
139  * restart.
140  * 
141  * 72    3/31/98 5:31p Lawrance
142  * Fix sound bug that happenned when entering briefing
143  * 
144  * 71    3/30/98 5:16p Lawrance
145  * centralize a check for disabled loadout screens
146  * 
147  * 70    3/30/98 12:18a Lawrance
148  * change some DEMO_RELEASE code to not compile code rather than return
149  * early
150  * 
151  * 69    3/29/98 10:16p Allender
152  * scramble missions need to have ship/weapon selection disabled (this
153  * code was somehow lost)
154  * 
155  * 68    3/29/98 1:24p Dave
156  * Make chatbox not clear between multiplayer screens. Select player ship
157  * as default in mp team select and weapons select screens. Made create
158  * game mission list use 2 fixed size columns.
159  * 
160  * 67    3/29/98 12:55a Lawrance
161  * Get demo build working with limited set of data.
162  * 
163  * 66    3/25/98 8:43p Hoffoss
164  * Changed anim_play() to not be so damn complex when you try and call it.
165  * 
166  * 65    3/24/98 4:59p Dave
167  * Fixed several ui bugs. Put in pre and post voice stream playback sound
168  * fx. Put in error specific popups for clients getting dropped from games
169  * through actions other than their own.
170  * 
171  * 64    3/19/98 5:32p Lawrance
172  * Added music to the background of command brief screen.
173  * 
174  * 63    3/05/98 5:02p Dave
175  * More work on team vs. team support for multiplayer. Need to fix bugs in
176  * weapon select.
177  * 
178  * 62    3/03/98 8:55p Dave
179  * Finished pre-briefing team vs. team support.
180  * 
181  * 61    3/03/98 8:12p Lawrance
182  * Double timeout before flashing buttons
183  * 
184  * 60    3/02/98 3:27p Lawrance
185  * Don't call muti_ts_init() in single player
186  * 
187  * 59    3/01/98 3:26p Dave
188  * Fixed a few team select bugs. Put in multiplayer intertface sounds.
189  * Corrected how ships are disabled/enabled in team select/weapon select
190  * screens.
191  * 
192  * 58    2/27/98 4:55p Sandeep
193  * Fixed a signed/unsigned bug in the wss packet type
194  * 
195  * 57    2/26/98 4:59p Allender
196  * groundwork for team vs team briefings.  Moved weaponry pool into the
197  * Team_data structure.  Added team field into the p_info structure.
198  * Allow for mutliple structures in the briefing code.
199  * 
200  * 56    2/25/98 10:24a Lawrance
201  * Don't ask for confirmation when ESC is pressed.
202  * 
203  * 55    2/24/98 12:22a Lawrance
204  * New coords for revamped briefing graphics
205  * 
206  * 54    2/22/98 4:17p John
207  * More string externalization classification... 190 left to go!
208  * 
209  * 53    2/22/98 12:19p John
210  * Externalized some strings
211  * 
212  * 52    2/21/98 2:50p Lawrance
213  * Tell players that their campaign position will get saved if they return
214  * to main hall from loadout screens.
215  * 
216  * 51    2/19/98 6:26p Dave
217  * Fixed a few file xfer bugs. Tweaked mp team select screen. Put in
218  * initial support for player data uploading.
219  * 
220  * 50    2/18/98 3:56p Dave
221  * Several bugs fixed for mp team select screen. Put in standalone packet
222  * routing for team select.
223  * 
224  * 49    2/17/98 6:07p Dave
225  * Tore out old multiplayer team select screen, installed new one.
226  * 
227  * 48    2/13/98 3:46p Dave
228  * Put in dynamic chatbox sizing. Made multiplayer file lookups use cfile
229  * functions.
230  * 
231  * 47    2/10/98 8:40p Dave
232  * Fixed some debriefing bugs.
233  * 
234  * 46    2/04/98 11:06p Lawrance
235  * Fix a couple of bugs with save/restore of player weapon loadout.
236  * 
237  * 45    2/04/98 4:32p Allender
238  * support for multiple briefings and debriefings.  Changes to mission
239  * type (now a bitfield).  Bitfield defs for multiplayer modes
240  * 
241  * 44    1/30/98 10:40a Lawrance
242  * remove any binding references to hotkey screen
243  * 
244  * 43    1/20/98 5:52p Lawrance
245  * prompt user before returning to main hall (in single player)
246  * 
247  * 42    1/17/98 5:51p Dave
248  * Bug fixes for bugs generated by multiplayer testing.
249  * 
250  * 41    1/14/98 11:39a Dave
251  * Polished up a bunch of popup support items.
252  * 
253  * 40    1/13/98 5:35p Lawrance
254  * Show popup when trying to go to weapons loadout without any ships
255  * selected.
256  * 
257  * 39    1/10/98 5:48p Lawrance
258  * Don't allow Tab to go to ship/weapon loadout screen in training, play
259  * negative feedback sounds if user tries to.
260  * 
261  * 38    1/10/98 12:45a Lawrance
262  * Don't restore loadout if mission was modified.
263  * 
264  * 37    1/09/98 6:06p Dave
265  * Put in network sound support for multiplayer ship/weapon select
266  * screens. Made clients exit game correctly through warp effect. Fixed
267  * main hall menu help overlay bug.
268  * 
269  * 36    1/09/98 11:04a Lawrance
270  * Fix bug that prevented buttons from flashing in the loadout screens.
271  * 
272  * 35    1/07/98 6:45p Lawrance
273  * Play first briefing music if none specified.
274  * 
275  * 34    1/05/98 4:57p Lawrance
276  * reset flash timers when a button is pressed or a key is pressed
277  * 
278  * 33    12/30/97 5:46p Lawrance
279  * Rename rnd() to rand_alt().
280  * 
281  * 32    12/30/97 4:27p Lawrance
282  * Add new rnd() function that doesn't affect rand() sequence.
283  * 
284  * 31    12/29/97 4:21p Lawrance
285  * Flash buttons on briefing/ship select/weapons loadout when enough time
286  * has elapsed without activity.
287  * 
288  * 30    12/28/97 5:52p Lawrance
289  * Add support for debriefing success/fail music.
290  * 
291  * 29    12/24/97 8:54p Lawrance
292  * Integrating new popup code
293  * 
294  * 28    12/24/97 1:19p Lawrance
295  * fix some bugs with the multiplayer ship/weapons loadout
296  * 
297  * 27    12/23/97 11:58a Allender
298  * changes to ship/wespon selection for multplayer.  added sounds to some
299  * interface screens.  Update and modiied end-of-briefing packets -- yet
300  * to be tested.
301  *
302  * $NoKeywords: $
303  *
304  */
305
306 #include <limits.h>
307 #include "freespace.h"
308 #include "eventmusic.h"
309 #include "key.h"
310 #include "missionscreencommon.h"
311 #include "missionshipchoice.h"
312 #include "missionweaponchoice.h"
313 #include "missionbrief.h"
314 #include "multi.h"
315 #include "multimsgs.h"
316 #include "timer.h"
317 #include "sound.h"
318 #include "gamesequence.h"
319 #include "bmpman.h"
320 // #include "movie.h"
321 #include "gamesnd.h"
322 #include "palman.h"
323 #include "mouse.h"
324 #include "contexthelp.h"
325 #include "chatbox.h"
326 #include "time.h"
327 #include "joy.h"
328 #include "cmdline.h"
329 #include "linklist.h"
330 #include "staticrand.h" // for rand_alt()
331 #include "popup.h"
332 #include "multiutil.h"
333 #include "multiteamselect.h"
334 #include "hudwingmanstatus.h"
335 #include "multi_endgame.h"
336 #include "uidefs.h"
337 #include "animplay.h"
338
339 //////////////////////////////////////////////////////////////////
340 // Game Globals
341 //////////////////////////////////////////////////////////////////
342
343 int Common_select_inited = 0;
344
345 // Dependent on when mouse button goes up
346 int Drop_icon_mflag, Drop_on_wing_mflag, Brief_mouse_up_flag;
347
348 int Mouse_down_last_frame = 0;
349
350 // Timers used to flash buttons after timeouts
351 #define MSC_FLASH_AFTER_TIME    60000           //      time before flashing a button
352 #define MSC_FLASH_INTERVAL              200             // time between flashes
353 int Flash_timer;                                                                //      timestamp used to start flashing
354 int Flash_toggle;                                                               // timestamp used to toggle flashing
355 int Flash_bright;                                                               // state of button to flash
356
357 //////////////////////////////////////////////////////////////////
358 // Global to modulde
359 //////////////////////////////////////////////////////////////////
360 int Current_screen;
361 int Next_screen;
362 static int InterfacePaletteBitmap = -1; // PCX file that holds the interface palette
363
364 //////////////////////////////////////////////////////////////////
365 // UI 
366 //////////////////////////////////////////////////////////////////
367 UI_WINDOW       *Active_ui_window;
368
369 brief_common_buttons Common_buttons[3][GR_NUM_RESOLUTIONS][NUM_COMMON_BUTTONS] = {      
370 // icons for briefing, ship selection, weapon selection screens 
371         {       // UGH
372                 { // GR_640
373 #ifdef MAKE_FS1
374                         brief_common_buttons("CB_00",   0,              0,              -1,     -1,     0),     // briefing
375                         brief_common_buttons("CB_01",   0,              19,             -1,     -1,     1),     // ship selection
376                         brief_common_buttons("CB_02",   0,              33,             -1,     -1,     2),     // weapons loadout
377                         brief_common_buttons("CB_05",   562,    411,    -1,     -1,     5),     // commit
378                         brief_common_buttons("CB_06",   469,    424,    -1,     -1,     6),     // help
379                         brief_common_buttons("CB_07",   447,    452,    -1,     -1,     7),     // options
380 #else
381                         brief_common_buttons("CB_00",                   7,              3,              37,     7,              0),
382                         brief_common_buttons("CB_01",                   7,              19,     37,     23,     1),
383                         brief_common_buttons("CB_02",                   7,              35,     37,     39,     2),
384                         brief_common_buttons("CB_05",                   571,    425,    572,    413,    5),
385                         brief_common_buttons("CB_06",                   533,    425,    500,    440,    6),
386                         brief_common_buttons("CB_07",                   533,    455,    479,    464,    7),                     
387 #endif
388                 }, 
389                 { // GR_1024                    
390                         brief_common_buttons("2_CB_00",         12,     5,              59,     12,     0),
391                         brief_common_buttons("2_CB_01",         12,     31,     59,     37,     1),
392                         brief_common_buttons("2_CB_02",         12,     56,     59,     62,     2),
393                         brief_common_buttons("2_CB_05",         914,    681,    937,    671,    5),
394                         brief_common_buttons("2_CB_06",         854,    681,    822,    704,    6),
395                         brief_common_buttons("2_CB_07",         854,    724,    800,    743,    7),                     
396                 }
397         },      
398         {       // UGH
399                 { // GR_640
400 #ifdef MAKE_FS1
401                         brief_common_buttons("CB_00",   0,              0,              -1,     -1,     0),     // briefing
402                         brief_common_buttons("CB_01",   0,              19,             -1,     -1,     1),     // ship selection
403                         brief_common_buttons("CB_02",   0,              33,             -1,     -1,     2),     // weapons loadout
404                         brief_common_buttons("CB_05",   562,    411,    -1,     -1,     5),     // commit
405                         brief_common_buttons("CB_06",   469,    424,    -1,     -1,     6),     // help
406                         brief_common_buttons("CB_07",   447,    452,    -1,     -1,     7),     // options
407 #else
408                         brief_common_buttons("CB_00",                   7,              3,              37,     7,              0),
409                         brief_common_buttons("CB_01",                   7,              19,     37,     23,     1),
410                         brief_common_buttons("CB_02",                   7,              35,     37,     39,     2),
411                         brief_common_buttons("CB_05",                   571,    425,    572,    413,    5),
412                         brief_common_buttons("CB_06",                   533,    425,    500,    440,    6),
413                         brief_common_buttons("CB_07",                   533,    455,    479,    464,    7),                     
414 #endif
415                 }, 
416                 { // GR_1024                    
417                         brief_common_buttons("2_CB_00",         12,     5,              59,     12,     0),
418                         brief_common_buttons("2_CB_01",         12,     31,     59,     37,     1),
419                         brief_common_buttons("2_CB_02",         12,     56,     59,     62,     2),
420                         brief_common_buttons("2_CB_05",         914,    681,    937,    671,    5),
421                         brief_common_buttons("2_CB_06",         854,    681,    822,    704,    6),
422                         brief_common_buttons("2_CB_07",         854,    724,    800,    743,    7),                     
423                 }
424         },      
425         {       // UGH
426                 { // GR_640
427 #ifdef MAKE_FS1
428                         brief_common_buttons("CB_00",   0,              0,              -1,     -1,     0),     // briefing
429                         brief_common_buttons("CB_01",   0,              19,             -1,     -1,     1),     // ship selection
430                         brief_common_buttons("CB_02",   0,              33,             -1,     -1,     2),     // weapons loadout
431                         brief_common_buttons("CB_05",   562,    411,    -1,     -1,     5),     // commit
432                         brief_common_buttons("CB_06",   469,    424,    -1,     -1,     6),     // help
433                         brief_common_buttons("CB_07",   447,    452,    -1,     -1,     7),     // options
434 #else
435                         brief_common_buttons("CB_00",                   7,              3,              37,     7,              0),
436                         brief_common_buttons("CB_01",                   7,              19,     37,     23,     1),
437                         brief_common_buttons("CB_02",                   7,              35,     37,     39,     2),
438                         brief_common_buttons("CB_05",                   571,    425,    572,    413,    5),
439                         brief_common_buttons("CB_06",                   533,    425,    500,    440,    6),
440                         brief_common_buttons("CB_07",                   533,    455,    479,    464,    7),                     
441 #endif
442                 }, 
443                 { // GR_1024                    
444                         brief_common_buttons("2_CB_00",         12,     5,              59,     12,     0),
445                         brief_common_buttons("2_CB_01",         12,     31,     59,     37,     1),
446                         brief_common_buttons("2_CB_02",         12,     56,     59,     62,     2),
447                         brief_common_buttons("2_CB_05",         914,    681,    937,    671,    5),
448                         brief_common_buttons("2_CB_06",         854,    681,    822,    704,    6),
449                         brief_common_buttons("2_CB_07",         854,    724,    800,    743,    7),                     
450                 }
451         } 
452 };
453
454 #define COMMON_BRIEFING_BUTTON                                  0
455 #define COMMON_SS_BUTTON                                                        1
456 #define COMMON_WEAPON_BUTTON                                            2
457 #define COMMON_COMMIT_BUTTON                                            3
458 #define COMMON_HELP_BUTTON                                                      4
459 #define COMMON_OPTIONS_BUTTON                                           5
460
461 int Background_playing;                 // Flag to indicate background animation is playing
462 static anim *Background_anim;   // Ids for the anim data that is loaded
463
464 // value for which Team_data entry to use
465 int     Common_team;
466
467 // Ids for the instance of the anim that is playing
468 #ifdef MAKE_FS1
469 static anim_instance *Background_anim_instance;
470 #endif
471
472 int Wing_slot_empty_bitmap;
473 int Wing_slot_disabled_bitmap;
474
475 // prototypes
476 int wss_slots_all_empty();
477
478 // Display the no ships selected error
479 void common_show_no_ship_error()
480 {
481         popup(PF_USE_AFFIRMATIVE_ICON, 1, POPUP_OK, XSTR( "At least one ship must be selected before proceeding to weapons loadout", 460));
482 }
483
484 // Check the status of the buttons common to the loadout screens
485 void common_check_buttons()
486 {
487         int                     i;
488         UI_BUTTON       *b;
489
490         for ( i = 0; i < NUM_COMMON_BUTTONS; i++ ) {
491                 b = &Common_buttons[Current_screen-1][gr_screen.res][i].button;
492                 if ( b->pressed() ) {
493                         
494                         common_button_do(i);
495                 }
496         }
497
498 /*
499         // AL 11-23-97: let a joystick button press commit
500         if ( joy_down_count(0) || joy_down_count(1) ) {
501                 Commit_pressed = 1;
502         }
503 */
504
505 }
506
507 // -------------------------------------------------------------------
508 // common_redraw_pressed_buttons()
509 //
510 // Redraw any common buttons that are pressed down.  This function is needed
511 // since we sometimes need to draw pressed buttons last to ensure the entire
512 // button gets drawn (and not overlapped by other buttons)
513 //
514 void common_redraw_pressed_buttons()
515 {
516         int                     i;
517         UI_BUTTON       *b;
518
519         for ( i = 0; i < NUM_COMMON_BUTTONS; i++ ) {
520                 b = &Common_buttons[Current_screen-1][gr_screen.res][i].button;
521                 if ( b->button_down() ) {
522                         b->draw_forced(2);
523                 }
524         }
525 }
526
527 void common_buttons_maybe_reload(UI_WINDOW *ui_window)
528 {
529         UI_BUTTON       *b;
530         int                     i;
531
532         for ( i = 0; i < NUM_COMMON_BUTTONS; i++ ) {
533                 b = &Common_buttons[Current_screen-1][gr_screen.res][i].button;
534                 b->set_bmaps(Common_buttons[Current_screen-1][gr_screen.res][i].filename);
535         }
536 }
537
538 void common_buttons_init(UI_WINDOW *ui_window)
539 {
540         UI_BUTTON       *b;
541         int                     i;
542
543         for ( i = 0; i < NUM_COMMON_BUTTONS; i++ ) {
544                 b = &Common_buttons[Current_screen-1][gr_screen.res][i].button;
545                 b->create( ui_window, "", Common_buttons[Current_screen-1][gr_screen.res][i].x, Common_buttons[Current_screen-1][gr_screen.res][i].y,  60, 30, 0, 1);
546                 // set up callback for when a mouse first goes over a button
547                 b->set_highlight_action( common_play_highlight_sound );
548                 b->set_bmaps(Common_buttons[Current_screen-1][gr_screen.res][i].filename);
549                 b->link_hotspot(Common_buttons[Current_screen-1][gr_screen.res][i].hotspot);
550         }       
551
552 #ifndef MAKE_FS1
553         // add some text        
554         ui_window->add_XSTR("Briefing", 1504, Common_buttons[Current_screen-1][gr_screen.res][COMMON_BRIEFING_BUTTON].xt, Common_buttons[Current_screen-1][gr_screen.res][COMMON_BRIEFING_BUTTON].yt, &Common_buttons[Current_screen-1][gr_screen.res][COMMON_BRIEFING_BUTTON].button, UI_XSTR_COLOR_GREEN);
555         ui_window->add_XSTR("Ship Selection", 1067, Common_buttons[Current_screen-1][gr_screen.res][COMMON_SS_BUTTON].xt, Common_buttons[Current_screen-1][gr_screen.res][COMMON_SS_BUTTON].yt, &Common_buttons[Current_screen-1][gr_screen.res][COMMON_SS_BUTTON].button, UI_XSTR_COLOR_GREEN);
556         ui_window->add_XSTR("Weapon Loadout", 1068, Common_buttons[Current_screen-1][gr_screen.res][COMMON_WEAPON_BUTTON].xt, Common_buttons[Current_screen-1][gr_screen.res][COMMON_WEAPON_BUTTON].yt, &Common_buttons[Current_screen-1][gr_screen.res][COMMON_WEAPON_BUTTON].button, UI_XSTR_COLOR_GREEN);
557         ui_window->add_XSTR("Commit", 1062, Common_buttons[Current_screen-1][gr_screen.res][COMMON_COMMIT_BUTTON].xt, Common_buttons[Current_screen-1][gr_screen.res][COMMON_COMMIT_BUTTON].yt, &Common_buttons[Current_screen-1][gr_screen.res][COMMON_COMMIT_BUTTON].button, UI_XSTR_COLOR_PINK);
558         ui_window->add_XSTR("Help", 928, Common_buttons[Current_screen-1][gr_screen.res][COMMON_HELP_BUTTON].xt, Common_buttons[Current_screen-1][gr_screen.res][COMMON_HELP_BUTTON].yt, &Common_buttons[Current_screen-1][gr_screen.res][COMMON_HELP_BUTTON].button, UI_XSTR_COLOR_GREEN);
559         ui_window->add_XSTR("Options", 1036, Common_buttons[Current_screen-1][gr_screen.res][COMMON_OPTIONS_BUTTON].xt, Common_buttons[Current_screen-1][gr_screen.res][COMMON_OPTIONS_BUTTON].yt, &Common_buttons[Current_screen-1][gr_screen.res][COMMON_OPTIONS_BUTTON].button, UI_XSTR_COLOR_GREEN);
560 #endif
561
562         common_reset_buttons();
563
564         Common_buttons[Current_screen-1][gr_screen.res][COMMON_COMMIT_BUTTON].button.set_hotkey(KEY_CTRLED+SDLK_RETURN);
565         Common_buttons[Current_screen-1][gr_screen.res][COMMON_HELP_BUTTON].button.set_hotkey(SDLK_F1);
566         Common_buttons[Current_screen-1][gr_screen.res][COMMON_OPTIONS_BUTTON].button.set_hotkey(SDLK_F2);
567
568         // for scramble or training missions, disable the ship/weapon selection regions
569         if ( brief_only_allow_briefing() ) {
570                 Common_buttons[Current_screen-1][gr_screen.res][COMMON_SS_REGION].button.disable();
571                 Common_buttons[Current_screen-1][gr_screen.res][COMMON_WEAPON_REGION].button.disable();
572         }
573
574         #ifdef DEMO // allow for FS2_DEMO
575                 Common_buttons[Current_screen-1][gr_screen.res][COMMON_SS_REGION].button.disable();
576                 Common_buttons[Current_screen-1][gr_screen.res][COMMON_WEAPON_REGION].button.disable();
577         #endif
578 }
579
580 void set_active_ui(UI_WINDOW *ui_window)
581 {
582         Active_ui_window = ui_window;
583 }
584
585 void common_music_init(int score_index)
586 {
587         if ( Cmdline_freespace_no_music ) {
588                 return;
589         }
590
591         if ( score_index >= NUM_SCORES ) {
592                 Int3();
593                 return;
594         }
595
596         if ( Mission_music[score_index] < 0 ) {
597                 if ( Num_music_files > 0 ) {
598                         Mission_music[score_index] = 0;
599                         nprintf(("Sound","No briefing music is selected, so play first briefing track: %s\n",Spooled_music[Mission_music[score_index]].name));
600                 } else {
601                         return;
602                 }
603         }
604
605         briefing_load_music( Spooled_music[Mission_music[score_index]].filename );
606         // Use this id to trigger the start of music playing on the briefing screen
607         Briefing_music_begin_timestamp = timestamp(BRIEFING_MUSIC_DELAY);
608 }
609
610 void common_music_do()
611 {
612         if ( Cmdline_freespace_no_music ) {
613                 return;
614         }
615
616         // Use this id to trigger the start of music playing on the briefing screen
617         if ( timestamp_elapsed( Briefing_music_begin_timestamp) ) {
618                 Briefing_music_begin_timestamp = 0;
619                 briefing_start_music();
620         }
621 }
622
623 void common_music_close()
624 {
625         if ( Cmdline_freespace_no_music ) {
626                 return;
627         }
628
629         if ( Num_music_files <= 0 )
630                 return;
631
632         briefing_stop_music();
633 }
634
635 // function that sets the current palette to the interface palette.  This function
636 // needs to be followed by common_free_interface_palette() to restore the game palette.
637 void common_set_interface_palette(const char *filename)
638 {
639         static char buf[MAX_FILENAME_LEN + 1] = {0};
640
641         if (!filename)
642                 filename = NOX("palette01");
643
644         SDL_assert(strlen(filename) <= MAX_FILENAME_LEN);
645         if ( (InterfacePaletteBitmap != -1) && !SDL_strcasecmp(filename, buf) )
646                 return;  // already set to this palette
647
648         SDL_strlcpy(buf, filename, SDL_arraysize(buf));
649
650         // unload the interface bitmap from memory
651         if (InterfacePaletteBitmap != -1) {
652                 bm_unload(InterfacePaletteBitmap);
653                 InterfacePaletteBitmap = -1;
654         }
655
656         // ugh - we don't need this anymore
657 #ifdef MAKE_FS1
658         InterfacePaletteBitmap = bm_load(filename);
659         if (InterfacePaletteBitmap < 0) {
660                 Error(LOCATION, "Could not load in \"%s\"!", filename);
661         }
662 #endif
663 }
664
665 // release the interface palette .pcx file, and restore the game palette
666 void common_free_interface_palette()
667 {
668         // unload the interface bitmap from memory
669         if (InterfacePaletteBitmap != -1) {
670                 bm_unload(InterfacePaletteBitmap);
671                 InterfacePaletteBitmap = -1;
672         }
673
674         // restore the normal game palette
675         palette_restore_palette();
676 }
677
678 // Init timers used for flashing buttons
679 void common_flash_button_init()
680 {
681         Flash_timer = timestamp(MSC_FLASH_AFTER_TIME);
682         Flash_toggle = 1;
683         Flash_bright = 0;
684 }
685
686 // determine if we should draw a button as bright
687 int common_flash_bright()
688 {
689         if ( timestamp_elapsed(Flash_timer) ) {
690                 if ( timestamp_elapsed(Flash_toggle) ) {
691                         Flash_toggle = timestamp(MSC_FLASH_INTERVAL);
692                         Flash_bright ^= 1;
693                 }
694         }
695
696         return Flash_bright;
697 }
698
699 // common_select_init() will load in animations and bitmaps that are common to the 
700 // briefing/ship select/weapon select screens.  The global Common_select_inited is set
701 // after this function is called once, and is only cleared when common_select_close()
702 // is called.  This prevents multiple loadings of animations/bitmaps.
703 //
704 // This function also sets the palette based on the file palette01.pcx
705 void common_select_init()
706 {
707         if ( Common_select_inited ) {
708                 nprintf(("Alan","common_select_init() returning without doing anything\n"));
709                 return;
710         }
711
712         nprintf(("Alan","entering common_select_init()\n"));
713
714         // No anims are playing
715         Background_playing = 0;
716         Background_anim = NULL;
717
718         #ifndef DEMO // not for FS2_DEMO
719
720 #ifdef MAKE_FS1
721         if ( current_detail_level() >= (NUM_DEFAULT_DETAIL_LEVELS-2) ) {
722
723                 anim_play_struct aps;
724
725                 // Load in the background transition anim
726                 if ( Game_mode & GM_MULTIPLAYER )
727                         Background_anim = anim_load("BriefTransMulti");
728                 else  {
729                         Background_anim = anim_load("BriefTrans");
730                 }
731
732                 SDL_assert( Background_anim != NULL );
733                 anim_play_init(&aps, Background_anim, 0, 0);
734                 aps.framerate_independent = 1;
735                 aps.skip_frames = 0;
736                 Background_anim_instance = anim_play(&aps);
737                 Background_playing = 1;         // start playing the Background anim
738         }
739 #endif
740
741         Current_screen = Next_screen = ON_BRIEFING_SELECT;
742
743         // load in the icons for the wing slots
744         load_wing_icons(NOX("iconwing01"));
745
746
747         #endif
748
749         Current_screen = Next_screen = ON_BRIEFING_SELECT;
750         
751         Commit_pressed = 0;
752
753         Common_select_inited = 1;
754
755         // this handles the case where the player played a multiplayer game but now is in single player (in one instance
756         // of Freespace)
757         if(!(Game_mode & GM_MULTIPLAYER)){
758                 chatbox_close();
759         }
760
761         // get the value of the team
762         Common_team = 0;                                                        // assume the first team -- we'll change this value if we need to
763         if ( (Game_mode & GM_MULTIPLAYER) && IS_MISSION_MULTI_TEAMS )
764                 Common_team = Net_player->p_info.team;
765
766         ship_select_common_init();      
767         weapon_select_common_init();
768         common_flash_button_init();
769
770         if ( Game_mode & GM_MULTIPLAYER ) {
771                 multi_ts_common_init();
772         }
773
774         // restore loadout from Player_loadout if this is the same mission as the one previously played
775         if ( !(Game_mode & GM_MULTIPLAYER) ) {
776                 if ( !SDL_strcasecmp(Player_loadout.filename, Game_current_mission_filename) ) {
777                         wss_restore_loadout();
778                         ss_synch_interface();
779                         wl_synch_interface();
780                 }
781         }
782         
783         Drop_icon_mflag = 0;
784         Drop_on_wing_mflag = 0;
785 }
786
787 void common_reset_buttons()
788 {
789         int                     i;
790         UI_BUTTON       *b;
791
792         for ( i = 0; i < NUM_COMMON_BUTTONS; i++ ) {
793                 b = &Common_buttons[Current_screen-1][gr_screen.res][i].button;
794                 b->reset_status();
795         }
796
797         switch(Current_screen) {
798         case ON_BRIEFING_SELECT:
799                 Common_buttons[Current_screen-1][gr_screen.res][COMMON_BRIEFING_REGION].button.skip_first_highlight_callback();
800                 break;
801         case ON_SHIP_SELECT:
802                 Common_buttons[Current_screen-1][gr_screen.res][COMMON_SS_REGION].button.skip_first_highlight_callback();
803                 break;
804         case ON_WEAPON_SELECT:
805                 Common_buttons[Current_screen-1][gr_screen.res][COMMON_WEAPON_REGION].button.skip_first_highlight_callback();
806                 break;
807         }
808 }
809
810 // common_select_do() is called once per loop in the interface screens and is used
811 // for drawing and changing the common animations and blitting common bitmaps.
812 int common_select_do(float frametime)
813 {
814         int     k, new_k;
815
816         if ( help_overlay_active(BR_OVERLAY) || help_overlay_active(SS_OVERLAY) || help_overlay_active(WL_OVERLAY) ) {
817                 Common_buttons[0][gr_screen.res][COMMON_HELP_BUTTON].button.reset_status();
818                 Common_buttons[1][gr_screen.res][COMMON_HELP_BUTTON].button.reset_status();
819                 Common_buttons[2][gr_screen.res][COMMON_HELP_BUTTON].button.reset_status();
820                 Active_ui_window->set_ignore_gadgets(1);
821         } else {
822                 Active_ui_window->set_ignore_gadgets(0);
823         }
824
825         k = chatbox_process();
826         if ( Game_mode & GM_NORMAL ) {
827                 new_k = Active_ui_window->process(k);
828         } else {
829                 new_k = Active_ui_window->process(k, 0);
830         }
831
832         if ( (k > 0) || (new_k > 0) || B1_JUST_RELEASED ) {
833                 if ( help_overlay_active(BR_OVERLAY) || help_overlay_active(SS_OVERLAY) || help_overlay_active(WL_OVERLAY) ) {
834                         help_overlay_set_state(BR_OVERLAY, 0);
835                         help_overlay_set_state(SS_OVERLAY, 0);
836                         help_overlay_set_state(WL_OVERLAY, 0);
837                         Active_ui_window->set_ignore_gadgets(0);
838                         k = 0;
839                         new_k = 0;
840                 }
841         }
842
843         // test for mouse buttons,  must be done after Active_ui_window->process()
844         // has been called to work properly
845         //
846         Drop_icon_mflag = 0;
847         Drop_on_wing_mflag = 0;
848         Brief_mouse_up_flag = 0;
849         Mouse_down_last_frame = 0;
850
851         // if the left mouse button was released...
852         if ( B1_RELEASED ) {
853                 Drop_icon_mflag = 1;
854                 Drop_on_wing_mflag = 1;
855         }
856
857         // if the left mouse button was pressed...
858         if ( B1_PRESSED ) {
859                 Mouse_down_last_frame = 1;
860         }
861
862         // basically a "click", only check for the click here to avoid action-on-over on briefing map
863         if ( B1_JUST_PRESSED ) {
864                 Brief_mouse_up_flag = 1;
865         }
866
867         // reset timers for flashing buttons if key pressed
868         if ( (k>0) || (new_k>0) ) {
869                 common_flash_button_init();
870         }
871
872         common_music_do();
873
874 #ifdef MAKE_FS1
875         if ( Background_playing ) {
876
877                 if ( Background_anim_instance->frame_num == BUTTON_SLIDE_IN_FRAME ) {
878                         gamesnd_play_iface(SND_BTN_SLIDE);
879                 }       
880         
881                 if ( Background_anim_instance->frame_num == Background_anim_instance->stop_at ) {
882                         // Free up the big honking background animation, since we won't be playing it again
883                         anim_release_render_instance(Background_anim_instance);
884                         anim_free(Background_anim);
885
886                         Background_playing = 0;         
887                         Current_screen = Next_screen = ON_BRIEFING_SELECT;
888                 }
889         }
890 #endif
891
892         if ( Current_screen != Next_screen ) {
893                 switch( Next_screen ) {
894                         case ON_BRIEFING_SELECT:
895                                 gameseq_post_event( GS_EVENT_START_BRIEFING );
896                                 break;
897
898                         case ON_SHIP_SELECT:
899                                 // go to the specialized multiplayer team/ship select screen
900                                 if(Game_mode & GM_MULTIPLAYER){
901                                         gameseq_post_event(GS_EVENT_TEAM_SELECT);
902                                 }
903                                 // go to the normal ship select screen
904                                 else {
905                                         gameseq_post_event(GS_EVENT_SHIP_SELECTION);
906                                 }
907                                 break;
908
909                         case ON_WEAPON_SELECT:
910                                 if ( !wss_slots_all_empty() ) {
911                                         gameseq_post_event(GS_EVENT_WEAPON_SELECTION);
912                                 } else {
913                                         common_show_no_ship_error();
914                                 }
915                                 break;
916                 } // end switch
917         }
918
919    return new_k;
920 }
921
922 // -------------------------------------------------------------------------------------
923 // common_render()
924 //
925 void common_render(float frametime)
926 {
927         if ( !Background_playing ) {
928                 gr_set_bitmap(Brief_background_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
929                 gr_bitmap(0, 0);
930         }
931
932         anim_render_all(0, frametime);
933         anim_render_all(ON_SHIP_SELECT, frametime);
934 }
935
936 // -------------------------------------------------------------------------------------
937 // common_render_selected_screen_button()
938 //
939 //      A very ugly piece of special purpose code.  This is used to draw the pressed button
940 // frame for whatever stage of the briefing/ship select/weapons loadout we are on. 
941 //
942 void common_render_selected_screen_button()
943 {
944         Common_buttons[Next_screen-1][gr_screen.res][Next_screen-1].button.draw_forced(2);
945 }
946
947 // -------------------------------------------------------------------------------------
948 // common_button_do() do the button action for the specified pressed button
949 //
950 void common_button_do(int i)
951 {
952         if ( i == COMMON_COMMIT_BUTTON ) {
953                 Commit_pressed = 1;
954                 return;
955         }
956
957         if ( Background_playing )
958                 return;
959
960         switch ( i ) {
961
962         case COMMON_BRIEFING_BUTTON:
963                 if ( Current_screen != ON_BRIEFING_SELECT ) {
964                         gamesnd_play_iface(SND_SCREEN_MODE_PRESSED);
965                         Next_screen = ON_BRIEFING_SELECT;
966                 }
967                 break;
968
969         case COMMON_WEAPON_BUTTON:
970                 if ( Current_screen != ON_WEAPON_SELECT ) {
971                         if ( !wss_slots_all_empty() ) {
972                                 gamesnd_play_iface(SND_SCREEN_MODE_PRESSED);
973                                 Next_screen = ON_WEAPON_SELECT;
974                         } else {
975                                 common_show_no_ship_error();
976                         }
977                 }
978                 break;
979
980         case COMMON_SS_BUTTON:
981                 if ( Current_screen != ON_SHIP_SELECT ) {
982                         gamesnd_play_iface(SND_SCREEN_MODE_PRESSED);
983                         Next_screen = ON_SHIP_SELECT;
984                 }
985                 break;
986
987         case COMMON_OPTIONS_BUTTON:
988                 gamesnd_play_iface(SND_SWITCH_SCREENS);
989                 gameseq_post_event( GS_EVENT_OPTIONS_MENU );
990                 break;
991
992         case COMMON_HELP_BUTTON:
993                 gamesnd_play_iface(SND_HELP_PRESSED);
994                 launch_context_help();
995                 break;
996
997         } // end switch
998 }
999
1000 // common_check_keys() will check for keypresses common to all the interface screens.
1001 void common_check_keys(int k)
1002 {
1003         switch (k) {
1004
1005                 case SDLK_ESCAPE: {
1006
1007                         if ( Current_screen == ON_BRIEFING_SELECT ) {
1008                                 if ( brief_get_closeup_icon() != NULL ) {
1009                                         brief_turn_off_closeup_icon();
1010                                         break;
1011                                 }
1012                         }
1013
1014                         // prompt the host of a multiplayer game
1015                         if(Game_mode & GM_MULTIPLAYER){
1016                                 multi_quit_game(PROMPT_ALL);
1017                         }
1018                         // go through the single player quit process
1019                         else {
1020                                 // return to the main menu
1021 /*
1022                                 int return_to_menu, pf_flags;
1023                                 pf_flags = PF_USE_AFFIRMATIVE_ICON|PF_USE_NEGATIVE_ICON;
1024                                 return_to_menu = popup(pf_flags, 2, POPUP_NO, POPUP_YES, XSTR( "Do you want to return to the Main Hall?\n(Your campaign position will be saved)", -1));
1025                                 if ( return_to_menu == 1 ) {
1026                                         gameseq_post_event(GS_EVENT_MAIN_MENU);
1027                                 }
1028 */
1029                                 gameseq_post_event(GS_EVENT_MAIN_MENU);
1030                         }                       
1031                         break;
1032                 }
1033
1034                 case KEY_CTRLED + SDLK_RETURN:
1035                         Commit_pressed = 1;
1036                         break;
1037
1038                 case SDLK_b:
1039                         if ( Current_screen != ON_BRIEFING_SELECT && !Background_playing ) {
1040                                 Next_screen = ON_BRIEFING_SELECT;
1041                         }
1042                         break;
1043
1044                 case SDLK_w:
1045                         if ( brief_only_allow_briefing() ) {
1046                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1047                                 break;
1048                         }
1049
1050                         #ifndef DEMO // not for FS2_DEMO
1051                                 if ( Current_screen != ON_WEAPON_SELECT && !Background_playing ) {
1052                                         if ( !wss_slots_all_empty() ) {
1053                                                 Next_screen = ON_WEAPON_SELECT;
1054                                         } else {
1055                                                 common_show_no_ship_error();
1056                                         }
1057                                 }
1058                         #else
1059                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1060                         #endif
1061
1062                         break;
1063
1064                 case SDLK_s:
1065
1066                         if ( brief_only_allow_briefing() ) {
1067                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1068                                 break;
1069                         }
1070
1071                         #ifndef DEMO // not for FS2_DEMO
1072                                 if ( Current_screen != ON_SHIP_SELECT && !Background_playing ) {
1073                                         Next_screen = ON_SHIP_SELECT;
1074                                 }
1075                         #else
1076                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1077                         #endif
1078
1079                         break;
1080
1081                 case KEY_SHIFTED+SDLK_TAB:
1082
1083                         if ( brief_only_allow_briefing() ) {
1084                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1085                                 break;
1086                         }
1087
1088                         #ifndef DEMO // not for FS2_DEMO
1089                                 if ( !Background_playing ) {
1090                                         switch ( Current_screen ) {
1091                                                 case ON_BRIEFING_SELECT:
1092                                                         if ( !wss_slots_all_empty() ) {
1093                                                                 Next_screen = ON_WEAPON_SELECT;
1094                                                         } else {
1095                                                                 common_show_no_ship_error();
1096                                                         }
1097                                                         break;
1098
1099                                                 case ON_SHIP_SELECT:
1100                                                         Next_screen = ON_BRIEFING_SELECT;
1101                                                         break;
1102
1103                                                 case ON_WEAPON_SELECT:
1104                                                         Next_screen = ON_SHIP_SELECT;
1105                                                         break;
1106                                                 default:
1107                                                         Int3();
1108                                                         break;
1109                                         }       // end switch
1110                                 }
1111                         #else
1112                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1113                         #endif
1114
1115                         break;
1116
1117                 case SDLK_TAB:
1118
1119                         if ( brief_only_allow_briefing() ) {
1120                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1121                                 break;
1122                         }
1123
1124                         #ifndef DEMO // not for FS2_DEMO
1125                                 if ( !Background_playing ) {
1126                                         switch ( Current_screen ) {
1127                                                 case ON_BRIEFING_SELECT:
1128                                                         Next_screen = ON_SHIP_SELECT;
1129                                                         break;
1130
1131                                                 case ON_SHIP_SELECT:
1132                                                         if ( !wss_slots_all_empty() ) {
1133                                                                 Next_screen = ON_WEAPON_SELECT;
1134                                                         } else {
1135                                                                 common_show_no_ship_error();
1136                                                         }
1137                                                         break;
1138
1139                                                 case ON_WEAPON_SELECT:
1140                                                         Next_screen = ON_BRIEFING_SELECT;
1141                                                         break;
1142                                                 default:
1143                                                         Int3();
1144                                                         break;
1145                                         }       // end switch
1146                                 }
1147                         #else
1148                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1149                         #endif
1150
1151                         break;
1152
1153                 case SDLK_p:
1154                         if ( Anim_paused )
1155                                 Anim_paused = 0;
1156                         else
1157                                 Anim_paused = 1;
1158                         break;
1159         } // end switch
1160 }
1161
1162 // common_select_close() will release the memory for animations and bitmaps that
1163 // were loaded in common_select_init().  This function will abort if the Common_select_inited
1164 // flag is not set.  The last thing common_select_close() does in clear the Common_select_inited
1165 // flag.  
1166 //
1167 // weapon_select_close() and ship_select_close() are both called, since common_select_close()
1168 // is the function that is called the interface screens are finally exited.
1169 void common_select_close()
1170 {
1171         if ( !Common_select_inited ) {
1172                 nprintf(("Alan","common_select_close() returning without doing anything\n"));
1173                 return;
1174         }
1175
1176         nprintf(("Alan","entering common_select_close()\n"));
1177         
1178         // catch open anims that weapon_select_init_team() opened when not in weapon_select
1179         weapon_select_close_team();
1180
1181         weapon_select_close();
1182         if(Game_mode & GM_MULTIPLAYER){
1183                 multi_ts_close();
1184         } 
1185         ship_select_close();    
1186         brief_close();  
1187
1188         common_free_interface_palette();
1189
1190         // release the bitmpas that were previously extracted from anim files
1191         unload_wing_icons();
1192
1193         // Release any instances that may still exist
1194         anim_release_all_instances();
1195
1196         // free the anim's that were loaded into memory
1197 #ifdef MAKE_FS1
1198         if ( Background_anim ) {
1199                 anim_free(Background_anim);
1200                 Background_anim = NULL;
1201         }
1202 #endif
1203
1204         common_music_close();
1205         Common_select_inited = 0;
1206 }
1207
1208 // ------------------------------------------------------------------------
1209 //      load_wing_icons() creates the bitmaps for wing icons 
1210 //
1211 void load_wing_icons(const char *filename)
1212 {
1213         int first_frame, num_frames;
1214
1215         first_frame = bm_load_animation(filename, &num_frames);
1216         if ( first_frame == -1 ) {
1217                 Error(LOCATION, "Could not load icons from %s\n", filename);
1218                 return;
1219         }
1220
1221         Wing_slot_disabled_bitmap = first_frame;
1222         Wing_slot_empty_bitmap = first_frame + 1;
1223 //      Wing_slot_player_empty_bitmap = first_frame + 2;
1224 }
1225
1226 // ------------------------------------------------------------------------
1227 //      common_scroll_up_pressed()
1228 //
1229 int common_scroll_up_pressed(int *start, int size, int max_show)
1230 {
1231         // check if we even need to scroll at all
1232         if ( size <= max_show ) {
1233                 return 0;
1234         }
1235
1236         if ( (size - *start) > max_show ) {
1237                 *start += 1;
1238                 return 1;
1239         }
1240         return 0;
1241 }
1242
1243 // ------------------------------------------------------------------------
1244 //      common_scroll_down_pressed()
1245 //
1246 int common_scroll_down_pressed(int *start, int size, int max_show)
1247 {
1248         // check if we even need to scroll at all
1249         if ( size <= max_show ) {
1250                 return 0;
1251         }
1252
1253         if ( *start > 0 ) {
1254                 *start -= 1;
1255                 return 1;
1256         }
1257         return 0;
1258 }
1259
1260 // NEWSTUFF BEGIN
1261
1262 loadout_data Player_loadout;    // what the ship and weapon loadout is... used since we want to use the 
1263                                                                                 // same loadout if the mission is played again
1264
1265 //wss_unit      Wss_slots[MAX_WSS_SLOTS];                               // slot data struct
1266 //int           Wl_pool[MAX_WEAPON_TYPES];                              // weapon pool 
1267 //int           Ss_pool[MAX_SHIP_TYPES];                                // ship pool
1268 //int           Wss_num_wings;                                                          // number of player wings
1269
1270 wss_unit        Wss_slots_teams[MAX_TEAMS][MAX_WSS_SLOTS];
1271 int             Wl_pool_teams[MAX_TEAMS][MAX_WEAPON_TYPES];
1272 int             Ss_pool_teams[MAX_TEAMS][MAX_SHIP_TYPES];
1273 int             Wss_num_wings_teams[MAX_TEAMS];
1274
1275 wss_unit        *Wss_slots;
1276 int             *Wl_pool;
1277 int             *Ss_pool;
1278 int             Wss_num_wings;
1279
1280 // save ship selection loadout to the Player_loadout struct
1281 void wss_save_loadout()
1282 {
1283         int i,j;
1284
1285         // save the ship pool
1286         for ( i = 0; i < MAX_SHIP_TYPES; i++ ) {
1287                 Player_loadout.ship_pool[i] = Ss_pool[i]; 
1288         }
1289
1290         // save the weapons pool
1291         for ( i = 0; i < MAX_WEAPON_TYPES; i++ ) {
1292                 Player_loadout.weapon_pool[i] = Wl_pool[i]; 
1293         }
1294
1295         // save the ship class / weapons for each slot
1296         for ( i = 0; i < MAX_WSS_SLOTS; i++ ) {
1297                 Player_loadout.unit_data[i].ship_class = Wss_slots[i].ship_class;
1298
1299                 for ( j = 0; j < MAX_WL_WEAPONS; j++ ) {
1300                         Player_loadout.unit_data[i].wep[j] = Wss_slots[i].wep[j];
1301                         Player_loadout.unit_data[i].wep_count[j] = Wss_slots[i].wep_count[j];
1302                 }
1303         }
1304 }
1305
1306 // restore ship/weapons loadout from the Player_loadout struct
1307 void wss_restore_loadout()
1308 {
1309         int i,j;
1310         wss_unit        *slot;
1311
1312         // only restore if mission hasn't changed
1313         if ( SDL_strcasecmp(Player_loadout.last_modified, The_mission.modified) ) {
1314                 return;
1315         }
1316
1317         // restore the ship pool
1318         for ( i = 0; i < MAX_SHIP_TYPES; i++ ) {
1319                 Ss_pool[i] = Player_loadout.ship_pool[i]; 
1320         }
1321
1322         // restore the weapons pool
1323         for ( i = 0; i < MAX_WEAPON_TYPES; i++ ) {
1324                 Wl_pool[i] = Player_loadout.weapon_pool[i]; 
1325         }
1326
1327         // restore the ship class / weapons for each slot
1328         for ( i = 0; i < MAX_WSS_SLOTS; i++ ) {
1329                 slot = &Player_loadout.unit_data[i];
1330                 Wss_slots[i].ship_class = slot->ship_class;
1331
1332                 for ( j = 0; j < MAX_WL_WEAPONS; j++ ) {
1333                         Wss_slots[i].wep[j]= slot->wep[j];
1334                         Wss_slots[i].wep_count[j] = slot->wep_count[j];
1335                 }
1336         }
1337 }
1338
1339 // Do a direct restore of the Player_loadout ship/weapon data to the wings
1340 void wss_direct_restore_loadout()
1341 {
1342         int                             i, j;
1343         wing                            *wp;
1344         wss_unit                        *slot;
1345
1346         // only restore if mission hasn't changed
1347         if ( SDL_strcasecmp(Player_loadout.last_modified, The_mission.modified) ) {
1348                 return;
1349         }
1350
1351         for ( i = 0; i < MAX_WING_BLOCKS; i++ ) {
1352
1353                 if ( Starting_wings[i] < 0 )
1354                         continue;
1355
1356                 wp = &Wings[Starting_wings[i]];
1357
1358                 // If this wing is still on the arrival list, then update the parse objects
1359                 if ( wp->ship_index[0] == -1 ) {
1360                         p_object *p_objp;
1361                         j=0;
1362                         for ( p_objp = GET_FIRST(&ship_arrival_list); p_objp != END_OF_LIST(&ship_arrival_list); p_objp = GET_NEXT(p_objp) ) {
1363                                 slot = &Player_loadout.unit_data[i*4+j];
1364                                 if ( p_objp->wingnum == WING_INDEX(wp) ) {
1365                                         p_objp->ship_class = slot->ship_class;
1366                                         wl_update_parse_object_weapons(p_objp, slot);
1367                                         j++;
1368                                 }
1369                         }
1370                 } else {
1371                         int     k;
1372                         int cleanup_ship_index[MAX_WING_SLOTS];
1373                         ship    *shipp;
1374
1375                         for ( k = 0; k < MAX_WING_SLOTS; k++ ) {
1376                                 cleanup_ship_index[k] = -1;
1377                         }
1378
1379                         // This wing is already created, so directly update the ships
1380                         for ( j = 0; j < MAX_WING_SLOTS; j++ ) {
1381                                 slot = &Player_loadout.unit_data[i*4+j];
1382                                 shipp = &Ships[wp->ship_index[j]];
1383                                 if ( shipp->ship_info_index != slot->ship_class ) {
1384
1385                                         if ( wp->ship_index[j] == -1 ) {
1386                                                 continue;
1387                                         }
1388
1389                                         if ( slot->ship_class == -1 ) {
1390                                                 cleanup_ship_index[j] = wp->ship_index[j];
1391                                                 ship_add_exited_ship( shipp, SEF_PLAYER_DELETED );
1392                                                 obj_delete(shipp->objnum);
1393                                                 hud_set_wingman_status_none( shipp->wing_status_wing_index, shipp->wing_status_wing_pos);
1394                                                 continue;
1395                                         } else {
1396                                                 change_ship_type(wp->ship_index[j], slot->ship_class);
1397                                         }
1398                                 }
1399                                 wl_bash_ship_weapons(&Ships[wp->ship_index[j]].weapons, slot);
1400                         }
1401
1402                         for ( k = 0; k < MAX_WING_SLOTS; k++ ) {
1403                                 if ( cleanup_ship_index[k] != -1 ) {
1404                                         ship_wing_cleanup( cleanup_ship_index[k], wp );
1405                                 }
1406                         }
1407
1408                 }
1409         } // end for 
1410 }
1411 int wss_slots_all_empty()
1412 {
1413         int i;
1414
1415         for ( i = 0; i < MAX_WSS_SLOTS; i++ ) {
1416                 if ( Wss_slots[i].ship_class >= 0 ) 
1417                         break;
1418         }
1419
1420         if ( i == MAX_WSS_SLOTS )
1421                 return 1;
1422         else
1423                 return 0;
1424 }
1425
1426 // determine the mode (WSS_...) based on slot/list index values
1427 int wss_get_mode(int from_slot, int from_list, int to_slot, int to_list, int wl_ship_slot)
1428 {
1429         int mode, to_slot_empty=0;
1430
1431         if ( wl_ship_slot >= 0 ) {
1432                 // weapons loadout
1433                 if ( to_slot >= 0 ) {
1434                         if ( Wss_slots[wl_ship_slot].wep_count[to_slot] == 0 ) {
1435                                 to_slot_empty = 1;
1436                         }
1437                 }
1438         } else {
1439                 // ship select
1440                 if ( to_slot >= 0 ) {
1441                         if ( Wss_slots[to_slot].ship_class == -1 ){
1442                                 to_slot_empty = 1;
1443                         }
1444                 }
1445         }
1446
1447         // determine mode
1448         if ( from_slot >= 0 && to_slot >= 0 ) {
1449                 mode = WSS_SWAP_SLOT_SLOT;
1450         } else if ( from_slot >= 0 && to_list >= 0 ) {
1451                 mode = WSS_DUMP_TO_LIST;
1452         } else if ( (from_list >= 0) && (to_slot >= 0) && (to_slot_empty) ) {
1453                 mode = WSS_GRAB_FROM_LIST;
1454         } else if ( (from_list >= 0) && (to_slot >= 0) && (!to_slot_empty) ) {
1455                 mode = WSS_SWAP_LIST_SLOT;
1456         } else {
1457                 mode = -1;      // no changes required
1458         }
1459
1460         return mode;
1461 }
1462
1463 // store all the unit data and pool data 
1464 int store_wss_data(ubyte *block, int max_size, int sound,int player_index)
1465 {
1466         int j, i,offset=0;      
1467         short player_id;        
1468         short ishort;
1469
1470         // this is intended for multi only since it byteswaps
1471         SDL_assert( Game_mode & GM_MULTIPLAYER );
1472
1473         // write the ship pool 
1474         for ( i = 0; i < MAX_SHIP_TYPES; i++ ) {
1475                 if ( Ss_pool[i] > 0 ) { 
1476                         block[offset++] = (ubyte)i;
1477                         SDL_assert( Ss_pool[i] < UCHAR_MAX );
1478                         
1479                         // take care of sign issues
1480                         if(Ss_pool[i] == -1){
1481                                 block[offset++] = 0xff;
1482                         } else {
1483                                 block[offset++] = (ubyte)Ss_pool[i];
1484                         }
1485                 }
1486         }
1487
1488         block[offset++] = 0xff; // signals start of weapons pool
1489
1490         // write the weapon pool
1491         for ( i = 0; i < MAX_WEAPON_TYPES; i++ ) {
1492                 if ( Wl_pool[i] > 0 ) {
1493                         block[offset++] = (ubyte)i;
1494                         ishort = INTEL_SHORT( (short)Wl_pool[i] );
1495                         memcpy(block+offset, &ishort, sizeof(short));
1496                         offset += sizeof(short);
1497                 }
1498         }
1499
1500         // write the unit data
1501
1502         block[offset++] = 0xff; // signals start of unit data
1503
1504         for ( i=0; i<MAX_WSS_SLOTS; i++ ) {
1505                 SDL_assert( Wss_slots[i].ship_class < UCHAR_MAX );
1506                 if(Wss_slots[i].ship_class == -1){
1507                         block[offset++] = 0xff;
1508                 } else {
1509                         block[offset++] = (ubyte)(Wss_slots[i].ship_class);
1510                 }
1511                 for ( j = 0; j < MAX_WL_WEAPONS; j++ ) {
1512                         // take care of sign issues
1513                         SDL_assert( Wss_slots[i].wep[j] < UCHAR_MAX );                  
1514                         if(Wss_slots[i].wep[j] == -1){
1515                                 block[offset++] = 0xff;
1516                         } else {
1517                                 block[offset++] = (ubyte)(Wss_slots[i].wep[j]);
1518                         }
1519
1520                         SDL_assert( Wss_slots[i].wep_count[j] < SHRT_MAX );
1521                         ishort = INTEL_SHORT( (short)Wss_slots[i].wep_count[j] );
1522
1523                         memcpy(&(block[offset]), &(ishort), sizeof(short) );
1524                         offset += sizeof(short);
1525                 }
1526
1527                 // mwa -- old way below -- too much space
1528                 //memcpy(block+offset, &Wss_slots[i], sizeof(wss_unit));
1529                 //offset += sizeof(wss_unit);
1530         }
1531
1532         // any sound index
1533         if(sound == -1){
1534                 block[offset++] = 0xff;
1535         } else {
1536                 block[offset++] = (ubyte)sound;
1537         }
1538
1539         // add a netplayer address to identify who should play the sound
1540         player_id = -1;
1541         if(player_index != -1){
1542                 player_id = Net_players[player_index].player_id;                
1543         }
1544         player_id = INTEL_SHORT( player_id );
1545         memcpy(block+offset,&player_id,sizeof(player_id));
1546         offset += sizeof(player_id);
1547
1548         SDL_assert( offset < max_size );
1549         return offset;
1550 }
1551
1552 int restore_wss_data(ubyte *block)
1553 {
1554         int     i, j, sanity, offset=0;
1555         ubyte   b1, b2,sound;   
1556         short ishort;
1557         short player_id;        
1558
1559         // this is intended for multi only since it byteswaps
1560         SDL_assert( Game_mode & GM_MULTIPLAYER );
1561
1562         // restore ship pool
1563         sanity=0;
1564         memset(Ss_pool, 0, MAX_SHIP_TYPES*sizeof(int));
1565         for (;;) {
1566                 if ( sanity++ > MAX_SHIP_TYPES ) {
1567                         Int3();
1568                         break;
1569                 }
1570
1571                 b1 = block[offset++];
1572                 if ( b1 == 0xff ) {
1573                         break;
1574                 }
1575         
1576                 // take care of sign issues
1577                 b2 = block[offset++];
1578                 if(b2 == 0xff){
1579                         Ss_pool[b1] = -1;
1580                 } else {
1581                         Ss_pool[b1] = b2;
1582                 }
1583         }
1584
1585         // restore weapons pool
1586         sanity=0;
1587         memset(Wl_pool, 0, MAX_WEAPON_TYPES*sizeof(int));
1588         for (;;) {
1589                 if ( sanity++ > MAX_WEAPON_TYPES ) {
1590                         Int3();
1591                         break;
1592                 }
1593
1594                 b1 = block[offset++];
1595                 if ( b1 == 0xff ) {
1596                         break;
1597                 }
1598         
1599                 memcpy(&ishort, block+offset, sizeof(short));
1600                 offset += sizeof(short);
1601                 Wl_pool[b1] = INTEL_SHORT( ishort );
1602         }
1603
1604         for ( i=0; i<MAX_WSS_SLOTS; i++ ) {
1605                 if(block[offset] == 0xff){
1606                         Wss_slots[i].ship_class = -1;
1607                 } else {
1608                         Wss_slots[i].ship_class = block[offset];
1609                 }
1610                 offset++;               
1611                 for ( j = 0; j < MAX_WL_WEAPONS; j++ ) {
1612                         // take care of sign issues
1613                         if(block[offset] == 0xff){
1614                                 Wss_slots[i].wep[j] = -1;
1615                                 offset++;
1616                         } else {
1617                                 Wss_slots[i].wep[j] = (int)(block[offset++]);
1618                         }
1619                 
1620                         memcpy( &ishort, &(block[offset]), sizeof(short) );
1621                         ishort = INTEL_SHORT( ishort );
1622                         Wss_slots[i].wep_count[j] = (int)ishort;
1623                         offset += sizeof(short);
1624                 }
1625
1626                 // mwa -- old way below
1627                 //memcpy(&Wss_slots[i], block+offset, sizeof(wss_unit));
1628                 //offset += sizeof(wss_unit);
1629         }
1630
1631         // read in the sound data
1632         sound = block[offset++];                                        // the sound index
1633
1634         // read in the player address
1635         memcpy(&player_id,block+offset,sizeof(player_id));
1636         player_id = INTEL_SHORT( player_id );
1637         offset += sizeof(short);
1638         
1639         // determine if I'm the guy who should be playing the sound
1640         if((Net_player != NULL) && (Net_player->player_id == player_id)){
1641                 // play the sound
1642                 if(sound != 0xff){
1643                         gamesnd_play_iface((int)sound);
1644                 }
1645         }
1646
1647         if(!(Game_mode & GM_MULTIPLAYER)){
1648                 ss_synch_interface();
1649         }       
1650         return offset;
1651 }
1652
1653 // NEWSTUFF END
1654