]> icculus.org git repositories - taylor/freespace2.git/blob - src/missionui/redalert.cpp
use a better multi_sw_ok_to_commit() check
[taylor/freespace2.git] / src / missionui / redalert.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/RedAlert.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Module for Red Alert mission interface and code
16  *
17  * $Log$
18  * Revision 1.8  2005/03/29 02:18:47  taylor
19  * Various 64-bit platform fixes
20  * Fix compiler errors with MAKE_FS1 and fix gr_set_bitmap() too
21  * Make sure that turrets can fire at asteroids for FS1 (needed for a couple missions)
22  * Streaming audio support (big thanks to Pierre Willenbrock!!)
23  * Removed dependance on strings.tbl for FS1 since we don't actually need it now
24  *
25  * Revision 1.7  2004/09/20 01:31:44  theoddone33
26  * GCC 3.4 fixes.
27  *
28  * Revision 1.6  2003/08/03 16:10:29  taylor
29  * cleanup; compile warning fixes
30  *
31  * Revision 1.5  2003/05/25 02:30:43  taylor
32  * Freespace 1 support
33  *
34  * Revision 1.4  2002/06/09 04:41:23  relnev
35  * added copyright header
36  *
37  * Revision 1.3  2002/05/26 22:06:17  relnev
38  * makefile: disable stand_gui for now.
39  *
40  * rest: staticize some globals
41  *
42  * Revision 1.2  2002/05/03 13:34:33  theoddone33
43  * More stuff compiles
44  *
45  * Revision 1.1.1.1  2002/05/03 03:28:10  root
46  * Initial import.
47  *
48  * 
49  * 16    9/11/99 12:31a Mikek
50  * Bumped up max red alert status ships and status subsystems.
51  * 
52  * 15    9/06/99 6:38p Dave
53  * Improved CD detection code.
54  * 
55  * 14    8/24/99 5:27p Andsager
56  * Make subsystems with zero strength before mission blown off.  Protect
57  * red alert pilot against dying between orders and jump.
58  * 
59  * 13    8/11/99 2:17p Jefff
60  * changed a string
61  * 
62  * 12    7/19/99 8:56p Andsager
63  * Added Ship_exited red_alert_carry flag and hull strength for RED ALERT
64  * carry over of Exited_ships
65  * 
66  * 11    7/19/99 2:13p Dave
67  * Added some new strings for Heiko.
68  * 
69  * 10    7/16/99 12:22p Jefff
70  * Added sound FX to red alert popup
71  * 
72  * 9     7/09/99 10:32p Dave
73  * Command brief and red alert screens.
74  * 
75  * 8     5/07/99 10:34a Andsager
76  * Make red alert work in FS2
77  * 
78  * 7     2/16/99 5:07p Neilk
79  * Added hires support
80  * 
81  * 6     1/30/99 5:08p Dave
82  * More new hi-res stuff.Support for nice D3D textures.
83  * 
84  * 5     1/27/99 9:56a Dave
85  * Temporary checkin of beam weapons for Dan to make cool sounds.
86  * 
87  * 4     11/18/98 11:15a Andsager
88  * Removed old version or red_alert_read_wingman_status
89  * 
90  * 3     10/13/98 9:29a Dave
91  * Started neatening up freespace.h. Many variables renamed and
92  * reorganized. Added AlphaColors.[h,cpp]
93  * 
94  * 2     10/07/98 10:53a Dave
95  * Initial checkin.
96  * 
97  * 1     10/07/98 10:50a Dave
98  * 
99  * 21    5/14/98 11:26a Lawrance
100  * ESC will return to the main hall
101  * 
102  * 20    5/13/98 5:14p Allender
103  * red alert support to go back to previous mission
104  * 
105  * 19    5/05/98 6:19p Lawrance
106  * Fix problems with "retry mission" for red alerts
107  * 
108  * 18    5/05/98 3:14p Comet
109  * AL: Fix bug with restoring secondary weapons in red alert mode
110  * 
111  * 17    5/04/98 10:49p Lawrance
112  * Make sure old pilot files don't cause problems with the new RedAlert
113  * data format
114  * 
115  * 16    5/04/98 6:06p Lawrance
116  * Make red alert mode work!
117  * 
118  * 15    5/02/98 4:10p Lawrance
119  * Only use first stage of briefing for red alerts
120  * 
121  * 14    5/01/98 9:18p Ed
122  * mark all goals and events as failed when red alert moves ahead to next
123  * mission
124  * 
125  * 13    4/22/98 10:13a John
126  * added assert to trap a bug
127  * 
128  * 12    4/21/98 12:08a Allender
129  * only make red alert move to next mission in campaign mode
130  * 
131  * 11    4/03/98 10:31a John
132  * Made briefing and debriefing arrays be malloc'd
133  * 
134  * 10    3/28/98 2:53p Allender
135  * added hud gauge when entering a red alert mission
136  * 
137  * 9     3/12/98 10:28p Lawrance
138  * Deal with situation where wingman status may not have been properly
139  * saved
140  * 
141  * 8     3/12/98 9:44p Allender
142  * go to debrief in red alert when not in campaign mode
143  * 
144  * 6     3/09/98 9:55p Lawrance
145  * improve some comments
146  * 
147  * 5     3/09/98 4:41p Lawrance
148  * Fix up some merge problems.
149  * 
150  * 4     3/09/98 4:30p Allender
151  * multiplayer secondary weapon changes.  red-alert and cargo-known-delay
152  * sexpressions.  Add time cargo revealed to ship structure
153  * 
154  * 3     3/09/98 4:23p Lawrance
155  * Replay mission, full save/restore of wingman status
156  * 
157  * 2     3/09/98 12:13a Lawrance
158  * Add support for Red Alert missions
159  * 
160  * 1     3/08/98 4:54p Lawrance
161  *
162  * $NoKeywords: $
163  */
164
165 #include "ui.h"
166 #include "gamesnd.h"
167 #include "gamesequence.h"
168 #include "missionscreencommon.h"
169 #include "key.h"
170 #include "bmpman.h"
171 #include "font.h"
172 #include "redalert.h"
173 #include "hud.h"
174 #include "missionbriefcommon.h"
175 #include "timer.h"
176 #include "missioncampaign.h"
177 #include "missiongoals.h"
178 #include "linklist.h"
179 #include "hudwingmanstatus.h"
180 #include "audiostr.h"
181 #include "freespace.h"
182 #include "alphacolors.h"
183
184 /////////////////////////////////////////////////////////////////////////////
185 // Red Alert Mission-Level
186 /////////////////////////////////////////////////////////////////////////////
187
188 static int Red_alert_status;
189 static int Red_alert_new_mission_timestamp;             // timestamp used to give user a little warning for red alerts
190 static int Red_alert_num_slots_used = 0;
191 static int Red_alert_voice_started;
192
193 #define RED_ALERT_WARN_TIME             4000                            // time to warn user that new orders are coming
194
195 #define RED_ALERT_NONE                          0
196 #define RED_ALERT_MISSION                       1
197
198 #define MAX_RED_ALERT_SLOTS                             32
199 #ifdef MAKE_FS1
200 #define MAX_RED_ALERT_SUBSYSTEMS                33
201 #else
202 #define MAX_RED_ALERT_SUBSYSTEMS                64
203 #endif
204 #define RED_ALERT_EXITED_SHIP_CLASS             -1
205
206 typedef struct red_alert_ship_status
207 {
208         char    name[NAME_LENGTH];
209         float   hull;
210         int     ship_class;
211         float   subsys_current_hits[MAX_RED_ALERT_SUBSYSTEMS];
212         float subsys_aggregate_current_hits[SUBSYSTEM_MAX];
213         int     wep[MAX_WL_WEAPONS];
214         int     wep_count[MAX_WL_WEAPONS];
215 } red_alert_ship_status;
216
217 static red_alert_ship_status Red_alert_wingman_status[MAX_RED_ALERT_SLOTS];
218 static char     Red_alert_precursor_mission[MAX_FILENAME_LEN];
219
220 /////////////////////////////////////////////////////////////////////////////
221 // Red Alert Interface
222 /////////////////////////////////////////////////////////////////////////////
223
224 const char *Red_alert_fname[GR_NUM_RESOLUTIONS] = {
225         "RedAlert",
226         "2_RedAlert"
227 };
228
229 const char *Red_alert_mask[GR_NUM_RESOLUTIONS] = {
230         "RedAlert-m",
231         "2_RedAlert-m"
232 };
233
234 // font to use for "incoming transmission"
235 int Ra_flash_font[GR_NUM_RESOLUTIONS] = {
236         FONT2,
237         FONT2
238 };
239
240 int Ra_flash_y[GR_NUM_RESOLUTIONS] = {
241         140,
242         200
243 };
244
245 #ifdef MAKE_FS1
246 static int Ra_flash_coords[GR_NUM_RESOLUTIONS][2] = {
247         {
248                 61, 108                 // GR_640
249         },
250         {
251                 61, 108                 // GR_1024
252         }
253 };
254 #endif
255
256 #define NUM_BUTTONS                                             2
257
258 #define RA_REPLAY_MISSION                               0
259 #define RA_CONTINUE                                             1
260
261 static ui_button_info Buttons[GR_NUM_RESOLUTIONS][NUM_BUTTONS] = {
262         {       // GR_640
263 #ifdef MAKE_FS1
264                 ui_button_info("RAB_05",        0,              435,    -1,     -1,     5),             // Replay
265                 ui_button_info("RAB_04",        553,    411,    -1,     -1,     4),             // Exit
266 #else
267                 ui_button_info("RAB_00",        2,              445,    -1,     -1, 0),
268                 ui_button_info("RAB_01",        575,    432,    -1,     -1, 1),
269 #endif
270         },      
271         {       // GR_1024
272                 ui_button_info("2_RAB_00",      4,              712,    -1,     -1, 0),
273                 ui_button_info("2_RAB_01",      920,    691,    -1,     -1, 1),
274         }
275 };
276
277 #ifndef MAKE_FS1
278 #define RED_ALERT_NUM_TEXT              3
279
280 UI_XSTR Red_alert_text[GR_NUM_RESOLUTIONS][RED_ALERT_NUM_TEXT] = {
281         { // GR_640
282                 { "Replay",             1405,   46,     451,    UI_XSTR_COLOR_PINK,     -1, &Buttons[0][RA_REPLAY_MISSION].button },
283                 { "Previous Mission",   1452,   46,     462,    UI_XSTR_COLOR_PINK,     -1, &Buttons[0][RA_REPLAY_MISSION].button },
284                 { "Continue",   1069,   564,    413,    UI_XSTR_COLOR_PINK,     -1, &Buttons[0][RA_CONTINUE].button },
285         },
286         { // GR_1024
287                 { "Replay",             1405,   75,     722,    UI_XSTR_COLOR_PINK,     -1, &Buttons[1][RA_REPLAY_MISSION].button },
288                 { "Previous Mission",   1452,   75,     733,    UI_XSTR_COLOR_PINK,     -1, &Buttons[1][RA_REPLAY_MISSION].button },
289                 { "Continue",   1069,   902,    661,    UI_XSTR_COLOR_PINK,     -1, &Buttons[1][RA_CONTINUE].button },
290         }
291 };
292 #endif
293
294 // indicies for coordinates
295 #define RA_X_COORD 0
296 #define RA_Y_COORD 1
297 #define RA_W_COORD 2
298 #define RA_H_COORD 3
299
300
301 static int Text_delay;
302
303 int Ra_brief_text_wnd_coords[GR_NUM_RESOLUTIONS][4] = {
304         {
305 #ifdef MAKE_FS1
306                 117, 169, 406, 283
307 #else
308                 14, 151, 522, 289
309 #endif
310         },
311         {
312                 52, 241, 785, 463
313         }
314 };
315
316 static UI_WINDOW Ui_window;
317 #ifdef MAKE_FS1
318 static hud_anim Flash_anim;
319 #endif
320 static int Background_bitmap;
321 static int Red_alert_inited = 0;
322
323 static int Red_alert_voice;
324
325 // open and pre-load the stream buffers for the different voice streams
326 void red_alert_voice_load()
327 {
328         SDL_assert( Briefing != NULL );
329         if ( SDL_strncasecmp(Briefing->stages[0].voice, NOX("none"), 4) && (strlen(Briefing->stages[0].voice) > 0) ) {
330                 Red_alert_voice = audiostream_open( Briefing->stages[0].voice, ASF_VOICE );
331         }
332 }
333
334 // close all the briefing voice streams
335 void red_alert_voice_unload()
336 {
337         if ( Red_alert_voice != -1 ) {
338                 audiostream_close_file(Red_alert_voice, 0);
339                 Red_alert_voice = -1;
340         }
341 }
342
343 // start playback of the red alert voice
344 void red_alert_voice_play()
345 {
346         if ( Red_alert_voice == -1 ){
347                 return; // voice file doesn't exist
348         }
349
350         if ( !Briefing_voice_enabled ) {
351                 return;
352         }
353
354         if ( audiostream_is_playing(Red_alert_voice) ){
355                 return;
356         }
357
358         audiostream_play(Red_alert_voice, Master_voice_volume, 0);
359         Red_alert_voice_started = 1;
360 }
361
362 // stop playback of the red alert voice
363 void red_alert_voice_stop()
364 {
365         if ( Red_alert_voice == -1 )
366                 return;
367
368         audiostream_stop(Red_alert_voice);      // stream is automatically rewound
369 }
370
371 // a button was pressed, deal with it
372 void red_alert_button_pressed(int n)
373 {
374         switch (n) {
375         case RA_CONTINUE:               
376                 gameseq_post_event(GS_EVENT_ENTER_GAME);
377                 break;
378
379         case RA_REPLAY_MISSION:
380                 if ( Game_mode & GM_CAMPAIGN_MODE ) {
381                         // TODO: make call to campaign code to set correct mission for loading
382                         // mission_campaign_play_previous_mission(Red_alert_precursor_mission);
383                         if ( !mission_campaign_previous_mission() ) {
384                                 gamesnd_play_iface(SND_GENERAL_FAIL);
385                                 break;
386                         }
387
388                         gameseq_post_event(GS_EVENT_START_GAME);
389                 } else {
390                         gamesnd_play_iface(SND_GENERAL_FAIL);
391                 }
392                 break;
393         }
394 }
395
396 // blit "incoming transmission"
397 #define RA_FLASH_CYCLE                  0.25f
398 float Ra_flash_time = 0.0f;
399 int Ra_flash_up = 0;
400 void red_alert_blit_title()
401 {
402         const char *str = XSTR("Incoming Transmission", 1406);
403         int w, h;
404
405         // get the string size  
406         gr_set_font(Ra_flash_font[gr_screen.res]);
407         gr_get_string_size(&w, &h, str);
408
409         // set alpha color
410         color flash_color;
411         if(Ra_flash_up){
412                 gr_init_alphacolor(&flash_color, (int)(255.0f * (Ra_flash_time / RA_FLASH_CYCLE)), 0, 0, 255, AC_TYPE_HUD);
413         } else {
414                 gr_init_alphacolor(&flash_color, (int)(255.0f * (1.0f - (Ra_flash_time / RA_FLASH_CYCLE))), 0, 0, 255, AC_TYPE_HUD);
415         }
416
417         // draw
418 #ifndef MAKE_FS1
419         gr_set_color_fast(&flash_color);
420         gr_string(Ra_brief_text_wnd_coords[gr_screen.res][0] + ((Ra_brief_text_wnd_coords[gr_screen.res][2] - w) / 2), Ra_flash_y[gr_screen.res] - h - 5, str);
421         gr_set_color_fast(&Color_normal);       
422 #endif
423
424         // increment flash time
425         Ra_flash_time += flFrametime;
426         if(Ra_flash_time >= RA_FLASH_CYCLE){
427                 Ra_flash_time = 0.0f;
428                 Ra_flash_up = !Ra_flash_up;
429         }
430
431         // back to the original font
432         gr_set_font(FONT1);
433 }
434
435 // Called once when red alert interface is started
436 void red_alert_init()
437 {
438         int i;
439         ui_button_info *b;
440
441         if ( Red_alert_inited ) {
442                 return;
443         }
444
445 #ifdef MAKE_FS1
446         common_set_interface_palette("ControlConfigPalette");  // set the interface palette
447 #endif
448         Ui_window.create(0, 0, gr_screen.max_w, gr_screen.max_h, 0);
449         Ui_window.set_mask_bmap(Red_alert_mask[gr_screen.res]);
450
451         for (i=0; i<NUM_BUTTONS; i++) {
452                 b = &Buttons[gr_screen.res][i];
453
454                 b->button.create(&Ui_window, "", b->x, b->y, 60, 30, 0, 1);
455                 // set up callback for when a mouse first goes over a button
456                 b->button.set_highlight_action(common_play_highlight_sound);
457                 b->button.set_bmaps(b->filename);
458                 b->button.link_hotspot(b->hotspot);
459         }
460
461 #ifndef MAKE_FS1
462         // all text
463         for(i=0; i<RED_ALERT_NUM_TEXT; i++){
464                 Ui_window.add_XSTR(&Red_alert_text[gr_screen.res][i]);
465         }
466 #endif
467
468         // set up red alert hotkeys
469         Buttons[gr_screen.res][RA_CONTINUE].button.set_hotkey(KEY_CTRLED | SDLK_RETURN);
470
471         // load in background image and flashing red alert animation
472         Background_bitmap = bm_load(Red_alert_fname[gr_screen.res]);
473
474 #ifdef MAKE_FS1
475         hud_anim_init(&Flash_anim, Ra_flash_coords[gr_screen.res][RA_X_COORD], Ra_flash_coords[gr_screen.res][RA_Y_COORD], NOX("AlertFlash"));
476         hud_anim_load(&Flash_anim);
477 #endif
478
479         Red_alert_voice = -1;
480
481         if ( !Briefing ) {
482                 Briefing = &Briefings[0];                       
483         }
484
485         if ( Briefing->num_stages > 0 ) {
486                 SDL_assert(Briefing->stages[0].new_text);
487                 brief_color_text_init(Briefing->stages[0].new_text, Ra_brief_text_wnd_coords[gr_screen.res][RA_W_COORD], 0);
488         }
489
490         red_alert_voice_load();
491
492         Text_delay = timestamp(200);
493
494         Red_alert_voice_started = 0;
495         Red_alert_inited = 1;
496 }
497
498 // Called once when the red alert interface is exited
499 void red_alert_close()
500 {
501         if (Red_alert_inited) {
502
503                 red_alert_voice_stop();
504                 red_alert_voice_unload();
505
506                 if (Background_bitmap >= 0) {
507                         bm_unload(Background_bitmap);
508                 }
509                 
510                 Ui_window.destroy();
511 #ifdef MAKE_FS1
512                 hud_anim_release(&Flash_anim);
513 #endif
514                 common_free_interface_palette();                // restore game palette
515                 game_flush();
516         }
517
518         Red_alert_inited = 0;
519 }
520
521 // called once per frame when game state is GS_STATE_RED_ALERT
522 void red_alert_do_frame(float frametime)
523 {
524         int i, k;       
525
526         // ensure that the red alert interface has been initialized
527         if (!Red_alert_inited) {
528                 Int3();
529                 return;
530         }
531
532         k = Ui_window.process() & ~KEY_DEBUGGED;
533         switch (k) {
534                 case SDLK_ESCAPE:
535 //                      gameseq_post_event(GS_EVENT_ENTER_GAME);
536                         gameseq_post_event(GS_EVENT_MAIN_MENU);
537                         break;
538         }       // end switch
539
540         for (i=0; i<NUM_BUTTONS; i++){
541                 if (Buttons[gr_screen.res][i].button.pressed()){
542                         red_alert_button_pressed(i);
543                 }
544         }
545
546         GR_MAYBE_CLEAR_RES(Background_bitmap);
547         if (Background_bitmap >= 0) {
548                 gr_set_bitmap(Background_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
549                 gr_bitmap(0, 0);
550         } 
551
552         Ui_window.draw();
553 #ifdef MAKE_FS1
554         hud_anim_render(&Flash_anim, frametime);
555 #endif
556
557         gr_set_font(FONT1);
558
559         if ( timestamp_elapsed(Text_delay) ) {
560                 int finished_wipe = 0;
561                 if ( Briefing->num_stages > 0 ) {
562                         finished_wipe = brief_render_text(0, Ra_brief_text_wnd_coords[gr_screen.res][RA_X_COORD], Ra_brief_text_wnd_coords[gr_screen.res][RA_Y_COORD], Ra_brief_text_wnd_coords[gr_screen.res][RA_H_COORD], frametime, 0);
563                 }
564
565                 if (finished_wipe) {
566                         red_alert_voice_play();
567                 }
568         }
569
570         // blit incoming transmission
571         red_alert_blit_title();
572
573         gr_flip();
574 }
575
576 // set the red alert status for the current mission
577 void red_alert_set_status(int status)
578 {
579         Red_alert_status = status;
580         Red_alert_new_mission_timestamp = timestamp(-1);                // make invalid
581 }
582
583 // Store a ships weapons into a wingman status structure
584 void red_alert_store_weapons(red_alert_ship_status *ras, ship_weapon *swp)
585 {
586         int i, sidx;
587
588         if (swp == NULL) {
589                 return;
590         }
591
592         for ( i = 0; i < MAX_WL_PRIMARY; i++ ) {
593                 ras->wep[i] = swp->primary_bank_weapons[i];
594                 if ( ras->wep[i] >= 0 ) {
595                         ras->wep_count[i] = 1;
596                 } else {
597                         ras->wep_count[i] = -1;
598                 }
599         }
600
601         for ( i = 0; i < MAX_WL_SECONDARY; i++ ) {
602                 sidx = i+MAX_WL_PRIMARY;
603                 ras->wep[sidx] = swp->secondary_bank_weapons[i];
604                 if ( ras->wep[sidx] >= 0 ) {
605                         ras->wep_count[sidx] = swp->secondary_bank_ammo[i];
606                 } else {
607                         ras->wep_count[sidx] = -1;
608                 }
609         }
610 }
611
612 // Take the weapons stored in a wingman_status struct, and bash them into the ship weapons struct
613 void red_alert_bash_weapons(red_alert_ship_status *ras, ship_weapon *swp)
614 {
615         int i, j, sidx;
616
617         // restore from ship_exited
618         if (ras->ship_class == RED_ALERT_EXITED_SHIP_CLASS) {
619                 return;
620         }
621
622         j = 0;
623         for ( i = 0; i < MAX_WL_PRIMARY; i++ ) {
624                 if ( (ras->wep_count[i] > 0) && (ras->wep[i] >= 0) ) {
625                         swp->primary_bank_weapons[j] = ras->wep[i];                     
626                         j++;
627                 }
628         }
629         swp->num_primary_banks = j;
630
631         j = 0;
632         for ( i = 0; i < MAX_WL_SECONDARY; i++ ) {
633                 sidx = i+MAX_WL_PRIMARY;
634                 if ( ras->wep[sidx] >= 0 ) {
635                         swp->secondary_bank_weapons[j] = ras->wep[sidx];
636                         swp->secondary_bank_ammo[j] = ras->wep_count[sidx];
637                         j++;
638                 }
639         }
640         swp->num_secondary_banks = j;
641 }
642
643 void red_alert_bash_subsys_status(red_alert_ship_status *ras, ship *shipp)
644 {
645         ship_subsys *ss;
646         int                     count = 0;
647
648         // restore from ship_exited
649         if (ras->ship_class == RED_ALERT_EXITED_SHIP_CLASS) {
650                 return;
651         }
652
653         ss = GET_FIRST(&shipp->subsys_list);
654         while ( ss != END_OF_LIST( &shipp->subsys_list ) ) {
655
656                 if ( count >= MAX_RED_ALERT_SUBSYSTEMS ) {
657                         Int3(); // ran out of subsystems
658                         break;
659                 }
660
661                 ss->current_hits = ras->subsys_current_hits[count];
662                 if (ss->current_hits <= 0) {
663                         ss->submodel_info_1.blown_off = 1;
664                 }
665
666                 ss = GET_NEXT( ss );
667                 count++;
668         }
669
670         int i;
671
672         for ( i = 0; i < SUBSYSTEM_MAX; i++ ) {
673                 shipp->subsys_info[i].current_hits = ras->subsys_aggregate_current_hits[i];
674         }
675 }
676
677
678 void red_alert_store_subsys_status(red_alert_ship_status *ras, ship *shipp)
679 {
680         ship_subsys *ss;
681         int                     count = 0;
682
683         if (shipp == NULL) {
684                 return;
685         }
686
687         ss = GET_FIRST(&shipp->subsys_list);
688         while ( ss != END_OF_LIST( &shipp->subsys_list ) ) {
689
690                 if ( count >= MAX_RED_ALERT_SUBSYSTEMS ) {
691                         Int3(); // ran out of subsystems
692                         break;
693                 }
694
695                 ras->subsys_current_hits[count] = ss->current_hits;
696
697                 ss = GET_NEXT( ss );
698                 count++;
699         }
700
701         int i;
702
703         for ( i = 0; i < SUBSYSTEM_MAX; i++ ) {
704                 ras->subsys_aggregate_current_hits[i] = shipp->subsys_info[i].current_hits;
705         }
706 }
707
708
709 // Record the current state of the players wingman
710 void red_alert_store_wingman_status()
711 {
712         ship                            *shipp;
713         red_alert_ship_status   *ras;
714         ship_obj                        *so;
715         object                  *ship_objp;
716
717         Red_alert_num_slots_used = 0;
718
719         // store the mission filename for the red alert precursor mission
720         SDL_strlcpy(Red_alert_precursor_mission, Game_current_mission_filename, SDL_arraysize(Red_alert_precursor_mission));
721
722         // store status for all existing ships
723         for ( so = GET_FIRST(&Ship_obj_list); so != END_OF_LIST(&Ship_obj_list); so = GET_NEXT(so) ) {
724                 ship_objp = &Objects[so->objnum];
725                 SDL_assert(ship_objp->type == OBJ_SHIP);
726                 shipp = &Ships[ship_objp->instance];
727
728                 if ( shipp->flags & SF_DYING ) {
729                         continue;
730                 }
731
732                 if ( Red_alert_num_slots_used >= MAX_RED_ALERT_SLOTS ) {
733                         Int3(); // ran out of red alert slots
734                         continue;
735                 }
736
737                 if ( !(shipp->flags & SF_FROM_PLAYER_WING) && !(shipp->flags & SF_RED_ALERT_STORE_STATUS) ) {
738                         continue;
739                 }
740
741                 ras = &Red_alert_wingman_status[Red_alert_num_slots_used];
742                 Red_alert_num_slots_used++;
743
744                 SDL_strlcpy(ras->name, shipp->ship_name, SDL_arraysize(ras->name));
745                 ras->hull = Objects[shipp->objnum].hull_strength;
746                 ras->ship_class = shipp->ship_info_index;
747                 red_alert_store_weapons(ras, &shipp->weapons);
748                 red_alert_store_subsys_status(ras, shipp);
749         }
750
751         // store exited ships that did not die
752         for (int idx=0; idx<MAX_EXITED_SHIPS; idx++) {
753
754                 if ( Red_alert_num_slots_used >= MAX_RED_ALERT_SLOTS ) {
755                         Int3(); // ran out of red alert slots
756                         continue;
757                 }
758
759                 if (Ships_exited[idx].flags & SEF_RED_ALERT_CARRY) {
760                         ras = &Red_alert_wingman_status[Red_alert_num_slots_used];
761                         Red_alert_num_slots_used++;
762
763                         SDL_strlcpy(ras->name, Ships_exited[idx].ship_name, SDL_arraysize(ras->name));
764                         ras->hull = float(Ships_exited[idx].hull_strength);
765                         ras->ship_class = RED_ALERT_EXITED_SHIP_CLASS; //shipp->ship_info_index;
766                         red_alert_store_weapons(ras, NULL);
767                         red_alert_store_subsys_status(ras, NULL);
768                 }
769         }
770
771         SDL_assert(Red_alert_num_slots_used > 0);
772 }
773
774 // Delete a ship in a red alert mission (since it must have died/departed in the previous mission)
775 void red_alert_delete_ship(ship *shipp)
776 {
777         if ( (shipp->wing_status_wing_index >= 0) && (shipp->wing_status_wing_pos >= 0) ) {
778                 hud_set_wingman_status_dead(shipp->wing_status_wing_index, shipp->wing_status_wing_pos);
779         }
780
781         ship_add_exited_ship( shipp, SEF_PLAYER_DELETED );
782         obj_delete(shipp->objnum);
783         if ( shipp->wingnum >= 0 ) {
784                 ship_wing_cleanup( shipp-Ships, &Wings[shipp->wingnum] );
785         }
786 }
787
788 // Take the stored wingman status information, and adjust the player wing ships accordingly
789 void red_alert_bash_wingman_status()
790 {
791         int                             i;
792         ship                            *shipp;
793         red_alert_ship_status   *ras;
794         ship_obj                        *so;
795         object                  *ship_objp;
796
797         if ( !(Game_mode & GM_CAMPAIGN_MODE) ) {
798                 return;
799         }
800
801         if ( Red_alert_num_slots_used <= 0 ) {
802                 return;
803         }
804
805         // go through all ships in the game, and see if there is red alert status data for any
806
807         int remove_list[MAX_RED_ALERT_SLOTS];
808         int remove_count = 0;
809
810         for ( so = GET_FIRST(&Ship_obj_list); so != END_OF_LIST(&Ship_obj_list); so = GET_NEXT(so) ) {
811                 ship_objp = &Objects[so->objnum];
812                 SDL_assert(ship_objp->type == OBJ_SHIP);
813                 shipp = &Ships[ship_objp->instance];
814
815                 if ( !(shipp->flags & SF_FROM_PLAYER_WING) && !(shipp->flags & SF_RED_ALERT_STORE_STATUS) ) {
816                         continue;
817                 }
818
819                 int found_match = 0;
820
821                 for ( i = 0; i < Red_alert_num_slots_used; i++ ) {
822                         ras = &Red_alert_wingman_status[i];
823
824                         if ( !SDL_strcasecmp(ras->name, shipp->ship_name) ) {
825                                 found_match = 1;
826                                 if ( ras->ship_class == RED_ALERT_EXITED_SHIP_CLASS) {
827                                         // if exited ship, we can only restore hull strength
828                                         ship_objp->hull_strength = ras->hull;
829                                 } else {
830                                         // if necessary, restore correct ship class
831                                         if ( ras->ship_class != shipp->ship_info_index ) {
832                                                 change_ship_type(SHIP_INDEX(shipp), ras->ship_class);
833                                         }
834                                         // restore hull and weapons
835                                         ship_objp->hull_strength = ras->hull;
836                                         red_alert_bash_weapons(ras, &shipp->weapons);
837                                         red_alert_bash_subsys_status(ras, shipp);
838                                 }
839                         }
840                 }
841
842                 if ( !found_match ) {
843                         remove_list[remove_count++] = SHIP_INDEX(shipp);
844                 }
845         }
846
847         // remove ships
848         for ( i = 0; i < remove_count; i++ ) {
849                 // remove ship
850                 red_alert_delete_ship(&Ships[remove_list[i]]);
851         }
852 }
853
854 // write wingman status out to the specified file
855 void red_alert_write_wingman_status(CFILE *fp)
856 {
857         int                             i, j;
858         red_alert_ship_status *ras;
859
860         cfwrite_int(Red_alert_num_slots_used, fp);
861
862         if ( Red_alert_num_slots_used <= 0 ) {
863                 return;
864         }
865
866         SDL_assert(strlen(Red_alert_precursor_mission) > 0 );
867         cfwrite_string(Red_alert_precursor_mission, fp);
868
869         for ( i = 0; i < Red_alert_num_slots_used; i++ ) {
870                 ras = &Red_alert_wingman_status[i];
871                 cfwrite_string(ras->name, fp);
872                 cfwrite_float(ras->hull, fp);
873                 cfwrite_int(ras->ship_class, fp);
874
875                 for ( j = 0; j < MAX_RED_ALERT_SUBSYSTEMS; j++ ) {
876                         cfwrite_float(ras->subsys_current_hits[j], fp);
877                 }
878
879                 for ( j = 0; j < SUBSYSTEM_MAX; j++ ) {
880                         cfwrite_float(ras->subsys_aggregate_current_hits[j], fp);
881                 }
882
883                 for ( j = 0; j < MAX_WL_WEAPONS; j++ ) {
884                         cfwrite_int( ras->wep[j], fp ) ;
885                         cfwrite_int( ras->wep_count[j], fp );
886                 }
887         }
888 }
889
890 // red wingman status out of the specified file
891 void red_alert_read_wingman_status(CFILE *fp, int version)
892 {
893         int                             i, j;
894         red_alert_ship_status *ras;
895
896         Red_alert_num_slots_used = cfread_int(fp);
897
898         if ( Red_alert_num_slots_used <= 0 ) {
899                 return;
900         }
901
902         cfread_string(Red_alert_precursor_mission, MAX_FILENAME_LEN, fp);
903
904         for ( i = 0; i < Red_alert_num_slots_used; i++ ) {
905                 ras = &Red_alert_wingman_status[i];
906                 cfread_string(ras->name, NAME_LENGTH, fp);
907                 ras->hull = cfread_float(fp);
908                 ras->ship_class = cfread_int(fp);
909
910                 for ( j = 0; j < MAX_RED_ALERT_SUBSYSTEMS; j++ ) {
911                         ras->subsys_current_hits[j] = cfread_float(fp);
912                 }
913
914                 for ( j = 0; j < SUBSYSTEM_MAX; j++ ) {
915                         ras->subsys_aggregate_current_hits[j] = cfread_float(fp);
916                 }
917
918                 for ( j = 0; j < MAX_WL_WEAPONS; j++ ) {
919                         ras->wep[j] = cfread_int(fp) ;
920                         ras->wep_count[j] = cfread_int(fp);
921                 }
922         }
923 }
924
925 // return !0 if this is a red alert mission, otherwise return 0
926 int red_alert_mission()
927 {
928         if ( Red_alert_status == RED_ALERT_MISSION ) {
929                 return 1;
930         }
931
932         return 0;
933 }
934
935 // called from sexpression code to start a red alert mission
936 void red_alert_start_mission()
937 {
938         // if we are not in campaign mode, go to debriefing
939 //      if ( !(Game_mode & GM_CAMPAIGN_MODE) ) {
940 //              gameseq_post_event( GS_EVENT_DEBRIEF ); // proceed to debriefing
941 //              return;
942 //      }
943
944         // check player health here.  
945         // if we're dead (or about to die), don't start red alert mission.
946         if (Player_obj->type == OBJ_SHIP) {
947                 if (Player_obj->hull_strength > 0) {
948                         // make sure we don't die
949                         Player_obj->flags |= OF_GUARDIAN;
950
951                         // do normal red alert stuff
952                         Red_alert_new_mission_timestamp = timestamp(RED_ALERT_WARN_TIME);
953
954                         // throw down a sound here to make the warning seem ultra-important
955                         // gamesnd_play_iface(SND_USER_SELECT);
956                         snd_play(&(Snds[SND_DIRECTIVE_COMPLETE]));
957                 }
958         }
959 }
960
961 // called from main game loop to check to see if we should move to a red alert mission
962 int red_alert_check_status()
963 {
964         // if the timestamp is invalid, do nothing.
965         if ( !timestamp_valid(Red_alert_new_mission_timestamp) )
966                 return 0;
967
968         // return if the timestamp hasn't elapsed yet
969         if ( timestamp_elapsed(Red_alert_new_mission_timestamp) ) {
970
971                 // basic premise here is to stop the current mission, and then set the next mission in the campaign
972                 // which better be a red alert mission
973                 if ( Game_mode & GM_CAMPAIGN_MODE ) {
974                         red_alert_store_wingman_status();
975                         mission_goal_fail_incomplete();
976                         mission_campaign_store_goals_and_events();
977                         scoring_level_close();
978                         mission_campaign_eval_next_mission();
979                         mission_campaign_mission_over();
980
981                         // CD CHECK
982                         gameseq_post_event(GS_EVENT_START_GAME);
983                 } else {
984                         gameseq_post_event(GS_EVENT_END_GAME);
985                 }
986         }
987
988         return 1;
989 }
990