]> icculus.org git repositories - taylor/freespace2.git/blob - src/mission/missionbriefcommon.cpp
remove mission_load_menu
[taylor/freespace2.git] / src / mission / missionbriefcommon.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/Mission/MissionBriefCommon.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * C module for briefing code common to FreeSpace and FRED
16  *
17  * $Log$
18  * Revision 1.12  2005/10/01 22:04:58  taylor
19  * fix FS1 (de)briefing voices, the directory names are different in FS1
20  * hard code the table values so that the fs1.vp file isn't needed
21  * hard code a mission fix for sm2-08a since a have no idea how to fix it otherwise
22  * generally cleanup some FS1 code
23  * fix volume sliders in the options screen that never went all the way up
24  *
25  * Revision 1.11  2004/09/20 01:31:44  theoddone33
26  * GCC 3.4 fixes.
27  *
28  * Revision 1.10  2004/07/04 11:39:06  taylor
29  * fix missing debrief text, crash on exit, path separator's, warning fixes, no GR_SOFT
30  *
31  * Revision 1.9  2003/06/11 18:30:32  taylor
32  * plug memory leaks
33  *
34  * Revision 1.8  2003/06/03 04:00:40  taylor
35  * Polish language support (Janusz Dziemidowicz)
36  *
37  * Revision 1.7  2003/05/25 02:30:42  taylor
38  * Freespace 1 support
39  *
40  * Revision 1.6  2002/06/17 06:33:09  relnev
41  * ryan's struct patch for gcc 2.95
42  *
43  * Revision 1.5  2002/06/09 04:41:22  relnev
44  * added copyright header
45  *
46  * Revision 1.4  2002/06/02 04:26:34  relnev
47  * warning cleanup
48  *
49  * Revision 1.3  2002/06/01 07:12:33  relnev
50  * a few NDEBUG updates.
51  *
52  * removed a few warnings.
53  *
54  * Revision 1.2  2002/05/07 03:16:46  theoddone33
55  * The Great Newline Fix
56  *
57  * Revision 1.1.1.1  2002/05/03 03:28:10  root
58  * Initial import.
59  *
60  * 
61  * 19    11/02/99 3:23p Jefff
62  * translate briefing icon names
63  * 
64  * 18    9/09/99 9:44a Jefff
65  * doh, fixed reversed brief text color thingy.  i am stoopid.
66  * 
67  * 17    9/08/99 11:14a Jefff
68  * toned down hostile/friendly colors in briefing text
69  * 
70  * 16    9/07/99 12:20p Mikeb
71  * return pos of briefing icon even it does not fit on screen.
72  * 
73  * 15    9/03/99 1:32a Dave
74  * CD checking by act. Added support to play 2 cutscenes in a row
75  * seamlessly. Fixed super low level cfile bug related to files in the
76  * root directory of a CD. Added cheat code to set campaign mission # in
77  * main hall.
78  * 
79  * 14    8/10/99 7:28p Jefff
80  * shuffled some text around
81  * 
82  * 13    7/30/99 3:05p Jefff
83  * Fixed briefing icon fades -- in and out were reversed.
84  * 
85  * 12    7/26/99 1:52p Mikeb
86  * Fixed strange briefing bug where a NULL wasn't being checked for when
87  * copying briefing stage text. Odd.
88  * 
89  * 11    7/24/99 6:15p Jefff
90  * moved "stage x of y" text in multiplayer mode so its not covered by the
91  * chatbox
92  * 
93  * 10    7/20/99 7:09p Jefff
94  * briefing text occupies full window in 1024x768
95  * 
96  * 9     7/09/99 5:54p Dave
97  * Seperated cruiser types into individual types. Added tons of new
98  * briefing icons. Campaign screen.
99  * 
100  * 8     6/29/99 7:39p Dave
101  * Lots of small bug fixes.
102  * 
103  * 7     2/05/99 7:19p Neilk
104  * Removed black part from mission screen, fixed info text coords
105  * 
106  * 6     1/29/99 4:17p Dave
107  * New interface screens.
108  * 
109  * 5     12/18/98 1:13a Dave
110  * Rough 1024x768 support for Direct3D. Proper detection and usage through
111  * the launcher.
112  * 
113  * 4     10/23/98 3:51p Dave
114  * Full support for tstrings.tbl and foreign languages. All that remains
115  * is to make it active in Fred.
116  * 
117  * 3     10/13/98 9:28a Dave
118  * Started neatening up freespace.h. Many variables renamed and
119  * reorganized. Added AlphaColors.[h,cpp]
120  * 
121  * 2     10/07/98 10:53a Dave
122  * Initial checkin.
123  * 
124  * 1     10/07/98 10:49a Dave
125  * 
126  * 122   6/09/98 10:31a Hoffoss
127  * Created index numbers for all xstr() references.  Any new xstr() stuff
128  * added from here on out should be added to the end if the list.  The
129  * current list count can be found in FreeSpace.cpp (search for
130  * XSTR_SIZE).
131  * 
132  * 121   6/05/98 9:54a Lawrance
133  * OEM changes
134  * 
135  * 120   6/01/98 11:43a John
136  * JAS & MK:  Classified all strings for localization.
137  * 
138  * 119   5/23/98 10:38p Lawrance
139  * Avoid doing a cfile refresh when running debug
140  * 
141  * 118   5/23/98 6:49p Lawrance
142  * Fix problems with refreshing the file list when a CD is inserted
143  * 
144  * 117   5/21/98 6:57p Lawrance
145  * Don't prompt for the CD if voice not found
146  * 
147  * 116   5/21/98 12:35a Lawrance
148  * Tweak how CD is checked for
149  * 
150  * 115   5/12/98 11:46a John
151  * Changed the way the "glowing movement" type text draw.   Use Hoffoss's
152  * gr_get_string_size optional length parameter to determine length of
153  * string which accounts for kerning on the last character and then I only
154  * draw each character only once.
155  * 
156  * 114   5/08/98 5:32p Lawrance
157  * prompt for CD if can't load animations or voice
158  * 
159  * 113   5/06/98 5:30p John
160  * Removed unused cfilearchiver.  Removed/replaced some unused/little used
161  * graphics functions, namely gradient_h and _v and pixel_sp.   Put in new
162  * DirectX header files and libs that fixed the Direct3D alpha blending
163  * problems.
164  * 
165  * 112   4/27/98 9:08p Allender
166  * fix the debriefing stage problems when clients get to screen long after
167  * server
168  * 
169  * 111   4/25/98 3:49p Lawrance
170  * Save briefing auto-advance pref
171  * 
172  * 110   4/20/98 3:53p Lawrance
173  * Fix various bugs with auto-advancing through briefings.
174  * 
175  * $NoKeywords: $
176  */
177
178 #include "freespace.h"
179 #include "ship.h"
180 #include "key.h"
181 #include "2d.h"
182 #include "3d.h"
183 #include "line.h"
184 #include "timer.h"
185 #include "math.h"
186 #include "linklist.h"
187 #include "mouse.h"
188 #include "hud.h"
189 #include "osapi.h"
190 #include "object.h"
191 #include "multi.h"
192 #include "bmpman.h"
193 #include "missionbrief.h"
194 #include "missiongrid.h"
195 #include "missionbriefcommon.h"
196 #include "animplay.h"
197 #include "fvi.h"
198 #include "float.h"
199 #include "gamesnd.h"
200 #include "cmdline.h"
201 #include "parselo.h"
202 #include "audiostr.h"
203 #include "missioncmdbrief.h"
204 #include "missiondebrief.h"
205 #include "alphacolors.h"
206 #include "localize.h"
207 #include "vecmat.h"
208
209 // --------------------------------------------------------------------------------------
210 // briefing icons
211 // --------------------------------------------------------------------------------------
212 hud_frames      Icon_bitmaps[MAX_BRIEF_ICONS][MAX_SPECIES_NAMES];
213 hud_anim                Icon_highlight_anims[MAX_BRIEF_ICONS][MAX_SPECIES_NAMES];
214 hud_anim                Icon_fade_anims[MAX_BRIEF_ICONS][MAX_SPECIES_NAMES];
215
216
217 // --------------------------------------------------------------------------------------
218 // briefing screen
219 // --------------------------------------------------------------------------------------
220
221 brief_screen bscreen;
222
223 // briefing screen sections
224 #define BRIEF_CUP_X1                    400
225 #define BRIEF_CUP_Y1                    70
226 #define BRIEF_CUP_X2                    639
227 #define BRIEF_CUP_Y2                    245
228 #define BRIEF_CUPINFO_X1        445
229 #define BRIEF_CUPINFO_Y1        247
230 #define BRIEF_CUPINFO_X2        639
231 #define BRIEF_CUPINFO_Y2        438
232
233 const char *Brief_static_name[GR_NUM_RESOLUTIONS] = {
234         "BriefMap",
235         "2_BriefMap"
236 };
237
238 int Brief_static_coords[GR_NUM_RESOLUTIONS][2] = {
239         { // GR_640
240 #ifdef MAKE_FS1
241                 42, 122
242 #else
243                 10, 130
244 #endif
245         },
246         { // GR_1024
247                 15, 208
248         }
249 };
250
251 int Brief_bmap_coords[GR_NUM_RESOLUTIONS][2] = {
252         { // GR_640
253 #ifdef MAKE_FS1
254                 42, 122
255 #else
256                 0, 115
257 #endif
258         },
259         { // GR_1024
260                 0, 184
261         }
262 };
263
264 int Brief_grid_coords[GR_NUM_RESOLUTIONS][4] = {
265         { // GR_640
266 #ifdef MAKE_FS1
267                 54, 133, 530, 239
268 #else
269                 19, 147, 555, 232
270 #endif
271         },
272         { // GR_1024
273                 30, 235, 888, 371
274         }
275 };
276
277 int Brief_text_coords[GR_NUM_RESOLUTIONS][4] = {
278         { // GR_640
279 #ifdef MAKE_FS1
280                 49, 415, 374, 56
281 #else
282                 28, 399, 395, 74
283 #endif
284         },
285         { // GR_1024
286                 46, 637, 630, 120
287         }
288 };
289
290 int Brief_stage_text_coords[GR_NUM_RESOLUTIONS][2] = {
291         { // GR_640
292 #ifdef MAKE_FS1
293                 61, 387
294 #else
295                 138, 117
296 #endif
297         },
298         { // GR_1024
299                 227, 194
300         }
301 };
302
303 int Brief_stage_text_coords_multi[GR_NUM_RESOLUTIONS][2] = {
304         { // GR_640
305 #ifdef MAKE_FS1
306                 61, 387
307 #else
308                 479, 385
309 #endif
310         },
311         { // GR_1024
312                 821, 616
313         }
314 };
315
316 int Brief_text_max_lines[GR_NUM_RESOLUTIONS] = {
317         6, 6
318 };
319
320 #define LOOKAT_DIST     500.0f
321
322 // --------------------------------------------------------------------------------------
323 // Game-wide global data
324 // --------------------------------------------------------------------------------------
325 briefing                Briefings[MAX_TEAMS];                   // there is exactly one briefing per mission
326 debriefing      Debriefings[MAX_TEAMS];                 // there can be multiple debriefings per mission
327 briefing                *Briefing;                                                      // pointer used in code -- points to correct briefing
328 debriefing      *Debriefing;                                            // pointer to correct debriefing
329
330 int                     Briefing_voice_enabled=1;               // flag which turn on/off voice playback of briefings/debriefings
331
332 // --------------------------------------------------------------------------------------
333 // Module global data
334 // --------------------------------------------------------------------------------------
335
336 static int Last_new_stage;
337 int     Cur_brief_id;
338
339 const char BRIEF_META_CHAR = '$';
340
341 // static int Brief_voice_ask_for_cd;
342
343 // camera related
344 static vector   Current_cam_pos;                // current camera position
345 static vector   Target_cam_pos;         // desired camera position
346 static matrix   Current_cam_orient;     // current camera orientation
347 static matrix   Target_cam_orient;      // desired camera orientation
348 static matrix   Start_cam_orient;               // start camera orientation
349 static vector   Start_cam_pos;                  // position of camera at the start of a translation
350 static vector   Cam_vel;                                        //      camera velocity
351 static vector   Current_lookat_pos;     // lookat point
352 static vector   Target_lookat_pos;      // lookat point
353 static vector   Start_lookat_pos;
354 static vector   Lookat_vel;                             //      lookat point velocity
355
356 static float    Start_cam_move;         // time at which camera started moving (seconds)
357 static float    Total_move_time;                // time in which camera should move from current pos to target pos (seconds)
358 static float    Elapsed_time;
359
360 static float    Start_dist;
361 static float    End_dist;
362 static float    Dist_change_rate;
363
364 static vector   Acc_limit;
365 static vector   Vel_limit;
366
367 static float    Total_dist;
368 static float    Peak_speed;
369 static float    Cam_accel;
370 static float    Last_dist;
371 static vector   W_init;
372
373 // flag to indicate that the sound for a spinning highlight animation has played
374 static int Brief_stage_highlight_sound_handle = -1;
375
376 // used for scrolling briefing text ( if necessary )
377 int             Num_brief_text_lines[MAX_TEXT_STREAMS];
378 int             Top_brief_text_line;
379 static char             Brief_text[MAX_BRIEF_LINES][MAX_BRIEF_LINE_LEN];
380
381 // Used to support drawing colored text for the briefing.  Gets complicates since we
382 // need to be able to draw one character at a time as well when the briefing text
383 // first appears.
384 typedef struct colored_char
385 {
386         char    letter;
387         ubyte   color;          // index into Brief_text_colors[]
388 } colored_char;
389
390 static colored_char Colored_text[MAX_TEXT_STREAMS][MAX_BRIEF_LINES][MAX_BRIEF_LINE_LEN];
391 static int Colored_text_len[MAX_TEXT_STREAMS][MAX_BRIEF_LINES];
392
393 #define MAX_BRIEF_TEXT_COLORS                   9
394 #define BRIEF_TEXT_WHITE                                0
395 #define BRIEF_TEXT_BRIGHT_WHITE         1
396 #define BRIEF_TEXT_RED                                  2
397 #define BRIEF_TEXT_GREEN                                3
398 #define BRIEF_TEXT_YELLOW                               4
399 #define BRIEF_TEXT_BLUE                                 5
400 #define BRIEF_IFF_FRIENDLY                              6
401 #define BRIEF_IFF_HOSTILE                               7
402 #define BRIEF_IFF_NEUTRAL                               8
403
404 color Brief_color_red, Brief_color_green;
405
406 color *Brief_text_colors[MAX_BRIEF_TEXT_COLORS] = 
407 {
408         &Color_white,
409         &Color_bright_white,
410         &Color_red,
411         &Color_green,
412         &Color_yellow,
413         &Color_blue,
414         &Brief_color_green,
415         &Brief_color_red,
416         &IFF_colors[IFF_COLOR_NEUTRAL][0],
417 };
418
419 #define BRIGHTEN_LEAD   2
420
421 float Brief_text_wipe_time_elapsed;
422 static int Max_briefing_line_len;
423
424 static int Brief_voice_ended;
425 static int Brief_textdraw_finished;
426 static int Brief_voice_started;
427 static int Brief_stage_time;
428
429 const float             BRIEF_TEXT_WIPE_TIME    = 1.5f;         // time in seconds for wipe to occur
430 static int              Brief_text_wipe_snd;                                    // sound handle of sound effect for text wipe
431 static int              Play_brief_voice;
432
433 // animation stuff
434 static int              Play_highlight_flag;
435 static int              Cam_target_reached;
436 static int              Cam_movement_done;
437
438 // moving icons
439 typedef struct icon_move_info
440 {
441         icon_move_info  *next, *prev;
442         int                             used;
443         int                             id;
444         vector                  start;
445         vector                  finish;
446         vector                  current;
447
448         // used to move icons smoothly
449         vector                  direction;
450         float                           total_dist;
451         float                           accel;
452         float                           total_move_time;
453         float                           peak_speed;
454         int                             reached_dest;
455         float                           last_dist;
456 } icon_move_info;
457
458 #define MAX_MOVE_ICONS  10
459 icon_move_info  Icon_movers[MAX_MOVE_ICONS];
460 icon_move_info  Icon_move_list; // head of linked list
461
462 // fading out icons
463 typedef struct fade_icon
464 {
465         hud_anim        fade_anim;              // anim info
466         vector  pos;
467         int             team;
468 } fade_icon;
469
470 #define         MAX_FADE_ICONS  30
471 fade_icon       Fading_icons[MAX_FADE_ICONS];
472 int                     Num_fade_icons;
473
474 // voice id's for briefing text
475 int Brief_voices[MAX_BRIEF_STAGES];
476
477 cmd_brief *Cur_cmd_brief;
478 cmd_brief Cmd_briefs[MAX_TEAMS];
479
480 #ifdef MAKE_FS1
481 static const char *fs1_icon_tbl[MAX_BRIEF_ICONS][3] = {
482         { "iconfighter", "FadeFighter", "iconhighlight04" },
483         { "iconfighterW", "FadeFighterW", "iconhighlight02" },
484         { "iconcargo", "FadeCargo", "iconhighlight04" },
485         { "iconcargoW", "FadeCargoW", "iconhighlight02" },
486         { "iconbigship", "FadeBigShip", "iconhighlight07" },
487         { "iconbigshipW", "FadeBigShipW", "iconhighlight05" },
488         { "iconcapital", "FadeCapital", "iconhighlight06" },
489         { "iconplanet", "FadePlanet", "iconhighlight03" },
490         { "iconasteroid", "FadeAsteroid", "iconhighlight03" },
491         { "iconwaypoint", "FadeWaypoint", "iconhighlight04" },
492         { "iconsupport", "FadeSupport", "iconhighlight04" },
493         { "iconfreighter", "FadeFreighter", "iconhighlight01" },
494         { "iconfreighterc", "FadeFreighterC", "iconhighlight01" },
495         { "iconfreighterwing", "FadeFreighterW", "iconhighlight05" },
496         { "iconfreightercwing", "FadeFreighterCW", "iconhighlight05" },
497         { "iconInstallation", "FadeInstallation", "iconhighlight03" },
498         { "iconbomber", "FadeBomber", "iconhighlight07" },
499         { "iconbomberW", "FadeBomberW", "iconhighlight05" },
500         { "iconcruiser", "FadeCruiser", "iconhighlight01" },
501         { "iconcruiserW", "FadeCruiserW", "iconhighlight05" },
502         { "iconunknown", "FadeUnknown", "iconhighlight04" },
503         { "iconunknownW", "FadeUnknownW", "iconhighlight01" },
504         { "iconfighterP", "FadeFighterP", "iconhighlight04" },
505         { "iconfighterWP", "FadeFighterWP", "iconhighlight02" },
506         { "iconbomberP", "FadeBomberP", "iconhighlight07" },
507         { "iconbomberWP", "FadeBomberWP", "iconhighlight05" },
508         { "iconwaypoint", "FadeWaypoint", "iconhighlight04" }
509 };
510 #endif
511
512 // --------------------------------------------------------------------------------------
513 // forward declarations
514 // --------------------------------------------------------------------------------------
515 void    brief_render_elements(vector *pos, grid *gridp);
516 void    brief_render_icons(int stage_num, float frametime);
517 void    brief_parse_icon_tbl();
518 void    brief_grid_read_camera_controls( control_info * ci, float frametime );
519 void    brief_maybe_create_new_grid(grid *gridp, vector *pos, matrix *orient, int force = 0);
520 grid    *brief_create_grid(grid *gridp, vector *forward, vector *right, vector *center, int nrows, int ncols, float square_size);
521 grid    *brief_create_default_grid(void);
522 void    brief_render_grid(grid *gridp);
523 void    brief_modify_grid(grid *gridp);
524 void    brief_rpd_line(vector *v0, vector *v1);
525 void    brief_set_text_color(int color_index);
526 extern void get_camera_limits(matrix *start_camera, matrix *end_camera, float time, vector *acc_max, vector *w_max);
527 int brief_text_wipe_finished();
528
529 void brief_set_icon_color(int team)
530 {
531         switch (team) { 
532         case TEAM_FRIENDLY:     SET_COLOR_FRIENDLY;     break;
533         case TEAM_HOSTILE:      SET_COLOR_HOSTILE;      break;
534         case TEAM_NEUTRAL:      SET_COLOR_NEUTRAL;      break;
535         case TEAM_TRAITOR:      SET_COLOR_HOSTILE;      break;
536         default:
537                 SET_COLOR_HOSTILE;      break;
538         } // end switch
539 }
540
541 // --------------------------------------------------------------------------------------
542 //      brief_move_icon_reset()
543 //
544 //
545 void brief_move_icon_reset()
546 {
547         int i;
548
549         list_init(&Icon_move_list);
550         for ( i = 0; i < MAX_MOVE_ICONS; i++ )
551                 Icon_movers[i].used = 0;
552 }
553
554
555 // --------------------------------------------------------------------------------------
556 // Does one time initialization of the briefing and debriefing structures.
557 // Namely setting all malloc'ble pointers to NULL.  Called once at game startup.
558 void mission_brief_common_init()
559 {
560         int i,j;
561
562         // setup brief text colors
563         gr_init_alphacolor( &Brief_color_green, 50, 100, 50, 255, AC_TYPE_HUD);
564         gr_init_alphacolor( &Brief_color_red, 140, 20, 20, 255, AC_TYPE_HUD);
565
566         // extra catch to reset anything that's already loaded (ie. mission restart)
567         mission_brief_common_reset();
568         mission_debrief_common_reset();
569
570         if ( Fred_running )     {
571                 // If Fred is running malloc out max space
572                 for (i=0; i<MAX_TEAMS; i++ )    {
573                         for (j=0; j<MAX_BRIEF_STAGES; j++ )     {
574                                 Briefings[i].stages[j].new_text = (char *)malloc(MAX_BRIEF_LEN);
575                                 SDL_assert(Briefings[i].stages[j].new_text!=NULL);
576                                 Briefings[i].stages[j].icons = (brief_icon *)malloc(sizeof(brief_icon)*MAX_STAGE_ICONS);
577                                 SDL_assert(Briefings[i].stages[j].icons!=NULL);
578                                 Briefings[i].stages[j].lines = (brief_line *)malloc(sizeof(brief_line)*MAX_BRIEF_STAGE_LINES);
579                                 SDL_assert(Briefings[i].stages[j].lines!=NULL);
580                                 Briefings[i].stages[j].num_icons = 0;
581                                 Briefings[i].stages[j].num_lines = 0;
582                         }
583                 }
584
585                 for (i=0; i<MAX_TEAMS; i++ )    {
586                         for (j=0; j<MAX_DEBRIEF_STAGES; j++ )   {
587                                 Debriefings[i].stages[j].new_text = (char *)malloc(MAX_DEBRIEF_LEN);
588                                 SDL_assert(Debriefings[i].stages[j].new_text!=NULL);
589                                 Debriefings[i].stages[j].new_recommendation_text = (char *)malloc(MAX_RECOMMENDATION_LEN);
590                                 SDL_assert(Debriefings[i].stages[j].new_recommendation_text!=NULL);
591                         }
592                 }
593
594         } else {
595                 // If game is running don't malloc anything
596                 for (i=0; i<MAX_TEAMS; i++ )    {
597                         for (j=0; j<MAX_BRIEF_STAGES; j++ )     {
598                                 Briefings[i].stages[j].new_text = NULL;
599                                 Briefings[i].stages[j].num_icons = 0;
600                                 Briefings[i].stages[j].icons = NULL;
601                                 Briefings[i].stages[j].num_lines = 0;
602                                 Briefings[i].stages[j].lines = NULL;
603                         }
604                 }
605
606                 for (i=0; i<MAX_TEAMS; i++ )    {
607                         for (j=0; j<MAX_DEBRIEF_STAGES; j++ )   {
608                                 Debriefings[i].stages[j].new_text = NULL;
609                                 Debriefings[i].stages[j].new_recommendation_text = NULL;
610                         }
611                 }
612
613         }
614
615                 
616 }
617
618 //--------------------------------------------------------------------------------------
619 // Frees all the memory allocated in the briefing and debriefing structures
620 // and sets all pointers to NULL.
621 void mission_brief_common_reset()
622 {
623         int i,j;
624
625         if ( Fred_running )     {
626                 return;                                         // Don't free these under Fred.
627         }
628
629         for (i=0; i<MAX_TEAMS; i++ )    {
630                 for (j=0; j<MAX_BRIEF_STAGES; j++ )     {
631                         if ( Briefings[i].stages[j].new_text )  {
632                                 free(Briefings[i].stages[j].new_text);
633                                 Briefings[i].stages[j].new_text = NULL;                 
634                         }
635         
636                         if ( Briefings[i].stages[j].icons )     {
637                                 free(Briefings[i].stages[j].icons);
638                                 Briefings[i].stages[j].icons = NULL;
639                         }
640
641                         if ( Briefings[i].stages[j].lines )     {
642                                 free(Briefings[i].stages[j].lines);
643                                 Briefings[i].stages[j].lines = NULL;
644                         }
645                 }
646         }
647 }
648
649 // split from above since we need to clear them separately
650 void mission_debrief_common_reset()
651 {
652         int i, j;
653
654         if ( Fred_running ) {
655                 return;                                         // Don't free these under Fred.
656         }
657
658         for (i=0; i<MAX_TEAMS; i++ )    {
659                 for (j=0; j<MAX_DEBRIEF_STAGES; j++ )   {
660                         if ( Debriefings[i].stages[j].new_text )        {
661                                 free(Debriefings[i].stages[j].new_text);
662                                 Debriefings[i].stages[j].new_text = NULL;
663                         }
664
665                         if ( Debriefings[i].stages[j].new_recommendation_text ) {
666                                 free(Debriefings[i].stages[j].new_recommendation_text);
667                                 Debriefings[i].stages[j].new_recommendation_text = NULL;
668                         }
669                 }
670         }               
671 }
672
673
674
675
676 // --------------------------------------------------------------------------------------
677 //      brief_reset()
678 //
679 //
680 void brief_reset()
681 {
682         int i;
683
684         Briefing = NULL;
685         for ( i = 0; i < MAX_TEAMS; i++ ) 
686                 Briefings[i].num_stages = 0;
687         Cur_brief_id = 1;
688 }
689
690 // --------------------------------------------------------------------------------------
691 //      debrief_reset()
692 //
693 //
694 void debrief_reset()
695 {
696         int i,j;
697
698         Debriefing = NULL;
699         for ( i = 0; i < MAX_TEAMS; i++ ) {
700                 Debriefings[i].num_stages = 0;
701                 for (j=0; j<MAX_DEBRIEF_STAGES; j++ )   {
702                         if ( Debriefings[i].stages[j].new_recommendation_text ) {
703                                 Debriefings[i].stages[j].new_recommendation_text[0] = 0;
704                         }
705                 }
706         }
707
708         // MWA 4/27/98 -- must initialize this variable here since we cannot do it as debrief
709         // init time because race conditions between all players in the game make that type of
710         // initialization unsafe.
711         Debrief_multi_stages_loaded = 0;
712 }
713
714 // --------------------------------------------------------------------------------------
715 //      brief_init_screen()
716 //
717 //      Set up the screen regions.  A mulitplayer briefing will look different than a single player
718 // briefing.
719 //
720 void brief_init_screen(int multiplayer_flag)
721 {
722         bscreen.map_x1                  = Brief_grid_coords[gr_screen.res][0];
723         bscreen.map_x2                  = Brief_grid_coords[gr_screen.res][0] + Brief_grid_coords[gr_screen.res][2];
724         bscreen.map_y1                  = Brief_grid_coords[gr_screen.res][1];
725         bscreen.map_y2                  = Brief_grid_coords[gr_screen.res][1] + Brief_grid_coords[gr_screen.res][3];
726         /*
727         bscreen.map_x1                  = BRIEF_GRID3_X1;
728         bscreen.map_x2                  = BRIEF_GRID0_X2;
729         bscreen.map_y1                  = BRIEF_GRID3_Y1;
730         bscreen.map_y2                  = BRIEF_GRID0_Y2+4;
731         bscreen.btext_x1                = BRIEF_TEXT_X1;
732         bscreen.btext_x2                = BRIEF_TEXT_X2;
733         bscreen.btext_y1                = BRIEF_TEXT_Y1;
734         bscreen.btext_y2                = BRIEF_TEXT_Y2;
735         bscreen.cup_x1                  = BRIEF_CUP_X1;
736         bscreen.cup_y1                  = BRIEF_CUP_Y1;
737         bscreen.cup_x2                  = BRIEF_CUP_X2;
738         bscreen.cup_y2                  = BRIEF_CUP_Y2;
739         bscreen.cupinfo_x1      = BRIEF_CUPINFO_X1;
740         bscreen.cupinfo_y1      = BRIEF_CUPINFO_Y1;
741         bscreen.cupinfo_x2      = BRIEF_CUPINFO_X2;
742         bscreen.cupinfo_y2      = BRIEF_CUPINFO_Y2;
743         */
744 }
745
746 // --------------------------------------------------------------------------------------
747 //      brief_init_icons()
748 //
749 //
750 void brief_init_icons()
751 {
752         if ( Fred_running ) {
753                 gr_init_alphacolor( &IFF_colors[IFF_COLOR_HOSTILE][0],  0xff, 0x00, 0x00, 15*16, AC_TYPE_HUD);
754                 gr_init_alphacolor( &IFF_colors[IFF_COLOR_FRIENDLY][0], 0x00, 0xff, 0x00, 15*16, AC_TYPE_HUD);
755         }
756
757         int idx;
758         int i;
759         
760         for (i=0; i<MAX_BRIEF_ICONS; i++) {
761                 for (idx=0; idx<MAX_SPECIES_NAMES; idx++) {
762                         Icon_bitmaps[i][idx].first_frame = -1;
763                         Icon_bitmaps[i][idx].num_frames = 0;
764                 }
765         }
766
767         // Load in the bitmaps for the icons from icons.tbl
768         brief_parse_icon_tbl();
769 }
770
771 // Reset the highlight and fade anims... call before brief_parse_icon_tbl();
772 void brief_init_anims()
773 {
774         int idx;
775         int i;
776
777         for (i=0; i<MAX_BRIEF_ICONS; i++) {
778                 for (idx=0; idx<MAX_SPECIES_NAMES; idx++) {
779                         Icon_highlight_anims[i][idx].first_frame = -1;
780                         Icon_highlight_anims[i][idx].num_frames = 0;
781                         Icon_fade_anims[i][idx].first_frame = -1;
782                         Icon_fade_anims[i][idx].num_frames = 0;
783                 }
784         }
785 }
786
787 // ------------------------------------------------------------------------
788 //      brief_unload_icons() 
789 //
790 //
791 void brief_unload_icons()
792 {
793         hud_frames              *ib;
794         int                             i, j, idx;
795
796         for ( i = 0; i < MAX_BRIEF_ICONS; i++ ) {
797                 for(idx=0; idx<MAX_SPECIES_NAMES; idx++){
798                         ib = &Icon_bitmaps[i][idx];
799
800                         if (ib->first_frame < 0)
801                                 continue;
802
803                         for ( j=0; j<ib->num_frames; j++ ) {
804                                 bm_unload(ib->first_frame+j);
805                         }
806                 }
807         }
808 }
809
810 // determine if icon type is used in the current briefing
811 int brief_icon_used_in_briefing(int icon_type)
812 {
813         int num_icons, num_stages, i, j;
814
815         num_stages = Briefing->num_stages;
816
817         for ( i = 0; i < num_stages; i++ ) {
818                 num_icons = Briefing->stages[i].num_icons;
819                 for ( j = 0; j < num_icons; j++ ) {
820                         if ( Briefing->stages[i].icons[j].type == icon_type ) {
821                                 return 1;
822                         }
823                 }
824         }
825
826         return 0;
827 }
828
829 // --------------------------------------------------------------------------------------
830 //      brief_parse_icon_tbl()
831 //
832 //
833 void brief_parse_icon_tbl()
834 {
835 #ifndef MAKE_FS1
836         int                     num_icons, rval;
837         char                    name[NAME_LENGTH];
838         hud_frames      *hf;
839         hud_anim                *ha;
840         int idx;
841
842         // open localization
843         lcl_ext_open();
844
845         if ((rval = setjmp(parse_abort)) != 0) {
846                 Error(LOCATION, "Unable to parse icons.tbl!  Code = %i.\n", rval);
847         }
848         else {
849                 read_file_text("icons.tbl");
850                 reset_parse();          
851         }
852
853         num_icons = 0;
854         required_string("#Start");
855
856
857         int load_this_icon = 0;
858
859         while (required_string_either("#End","$Name:")) {
860                 for(idx=0; idx<MAX_SPECIES_NAMES; idx++){
861                         SDL_assert( num_icons < MAX_BRIEF_ICONS);
862                         hf = &Icon_bitmaps[num_icons][idx];
863
864                         // load in regular frames
865                         required_string("$Name:");
866                         stuff_string(name, F_NAME, NULL);
867
868                         if ( Fred_running ) {
869                                 load_this_icon = 1;
870                         } else {
871                                 load_this_icon = brief_icon_used_in_briefing(num_icons);
872                         }
873
874                         if ( load_this_icon ) {
875                                 hf->first_frame = bm_load_animation(name, &hf->num_frames);
876                                 if ( hf->first_frame == -1 ) {
877                                         Int3(); // missing briefing icon
878                                 }
879                         }
880
881                         // load in fade frames
882                         required_string("$Name:");
883                         stuff_string(name, F_NAME, NULL);
884                         ha = &Icon_fade_anims[num_icons][idx];
885                         hud_anim_init(ha, 0, 0, name);
886
887                         // load in highlighting frames
888                         required_string("$Name:");
889                         stuff_string(name, F_NAME, NULL);
890                         ha = &Icon_highlight_anims[num_icons][idx];
891                         hud_anim_init(ha, 0, 0, name);
892                 }
893
894                 // next icon _type_
895                 num_icons++;
896         }
897         required_string("#End");
898
899         // close localization
900         lcl_ext_close();
901 #else
902         // load up hard coded values for FS1
903         char name[NAME_LENGTH];
904         hud_frames *hf;
905         hud_anim *ha;
906         int idx;
907         int load_this_icon = 0;
908
909         for (idx = 0; idx < MAX_BRIEF_ICONS; idx++) {
910                 hf = &Icon_bitmaps[idx][0];
911
912                 // load in regular frames
913                 SDL_strlcpy(name, fs1_icon_tbl[idx][0], NAME_LENGTH);
914
915                 if ( Fred_running ) {
916                         load_this_icon = 1;
917                 } else {
918                         load_this_icon = brief_icon_used_in_briefing(idx);
919                 }
920                 
921                 if ( load_this_icon ) {
922                         hf->first_frame = bm_load_animation(name, &hf->num_frames);
923                         if ( hf->first_frame == -1 ) {
924                                 Int3(); // missing briefing icon
925                         }
926                 }
927
928                 // load in fade frames
929                 SDL_strlcpy(name, fs1_icon_tbl[idx][1], NAME_LENGTH);
930                 ha = &Icon_fade_anims[idx][0];
931                 hud_anim_init(ha, 0, 0, name);
932
933                 // load in highlighting frames
934                 SDL_strlcpy(name, fs1_icon_tbl[idx][2], NAME_LENGTH);
935                 ha = &Icon_highlight_anims[idx][0];
936                 hud_anim_init(ha, 0, 0, name);
937         }
938 #endif
939 }
940
941 // --------------------------------------------------------------------------------------
942 //      brief_close_map()
943 //
944 //
945 void brief_close_map()
946 {
947         brief_unload_icons();
948 }
949
950 void brief_preload_highlight_anim(brief_icon *bi)
951 {
952         hud_anim *ha;
953         int species = ship_get_species_by_type(bi->ship_class);
954
955 #ifdef MAKE_FS1
956         species = 0;
957 #endif
958
959         if(species < 0){
960                 return;
961         }
962
963         ha = &Icon_highlight_anims[bi->type][species];
964         if ( !SDL_strcasecmp(NOX("none"), ha->name) ) {
965                 return;
966         }
967
968         // force read of data from disk, so we don't glitch on initial playback
969         if ( ha->first_frame == -1 ) {
970                 hud_anim_load(ha);
971                 SDL_assert(ha->first_frame >= 0);
972         }
973
974         bi->highlight_anim = *ha;
975
976         gr_set_bitmap(ha->first_frame, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
977         gr_aabitmap(0, 0);
978 }
979
980 void brief_preload_fade_anim(brief_icon *bi)
981 {
982         hud_anim *ha;
983         int species = ship_get_species_by_type(bi->ship_class);
984
985 #ifdef MAKE_FS1
986         species = 0;
987 #endif
988
989         if(species < 0){
990                 return;
991         }
992
993         ha = &Icon_fade_anims[bi->type][species];
994         if ( !SDL_strcasecmp(NOX("none"), ha->name) ) {
995                 return;
996         }
997
998         // force read of data from disk, so we don't glitch on initial playback
999         if ( ha->first_frame == -1 ) {
1000                 hud_anim_load(ha);
1001                 SDL_assert(ha->first_frame >= 0);
1002         }
1003
1004         gr_set_bitmap(ha->first_frame, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1005         gr_aabitmap(0, 0);
1006 }
1007
1008 // preload highlight, fadein and fadeout animations that are used in this stage
1009 void brief_preload_anims()
1010 {
1011         int                     num_icons, num_stages, i, j;
1012         brief_icon      *bi;
1013
1014         num_stages = Briefing->num_stages;
1015
1016         for ( i = 0; i < num_stages; i++ ) {
1017                 num_icons = Briefing->stages[i].num_icons;
1018                 for ( j = 0; j < num_icons; j++ ) {
1019                         bi = &Briefing->stages[i].icons[j];
1020                         if ( bi->flags & BI_HIGHLIGHT ) {
1021                                 brief_preload_highlight_anim(bi);
1022                         }
1023                         brief_preload_fade_anim(bi);
1024                 }
1025         }
1026 }
1027
1028 // --------------------------------------------------------------------------------------
1029 //      brief_init_map()
1030 //
1031 //
1032 void brief_init_map()
1033 {
1034         vector *pos;
1035         matrix *orient;
1036
1037         SDL_assert( Briefing != NULL );
1038
1039         pos = &Briefing->stages[0].camera_pos;
1040         orient = &Briefing->stages[0].camera_orient;
1041         vm_vec_zero(&Current_lookat_pos);
1042         vm_vec_zero(&Target_lookat_pos);
1043         Elapsed_time = 0.0f;
1044         Total_move_time = 0.0f;
1045
1046         The_grid = brief_create_default_grid();
1047         brief_maybe_create_new_grid(The_grid, pos, orient, 1);
1048
1049         brief_init_anims();
1050         brief_init_icons();
1051         brief_move_icon_reset();
1052         brief_preload_anims();
1053
1054         Brief_text_wipe_snd = -1;
1055         Last_new_stage = -1;
1056         Num_fade_icons=0;
1057 }
1058
1059 #ifndef PLAT_UNIX
1060 #pragma optimize("", off)
1061 #endif
1062
1063 // render fade-out anim frame
1064
1065 void brief_render_fade_outs(float frametime)
1066 {
1067         int                     i,bx,by,w,h;
1068         float                   bxf,byf;
1069         vertex          tv;                     // temp vertex used to find screen position for text
1070         fade_icon       *fi;
1071         
1072
1073         for (i=0; i<Num_fade_icons; i++) {
1074                 fi = &Fading_icons[i];
1075
1076                 g3_rotate_vertex(&tv, &fi->pos);
1077
1078                 if (!(tv.flags & PF_PROJECTED))
1079                         g3_project_vertex(&tv);
1080
1081                 if (!(tv.flags & PF_OVERFLOW) ) {  // make sure point projected before drawing text
1082
1083                         brief_set_icon_color(fi->team);
1084
1085                         if ( fi->fade_anim.first_frame < 0 ) {
1086                                 continue;
1087                         }
1088
1089                         bm_get_info( fi->fade_anim.first_frame, &w, &h, NULL);
1090                         
1091                         bxf = tv.sx - w / 2.0f + 0.5f;
1092                         byf = tv.sy - h / 2.0f + 0.5f;
1093                         bx = fl2i(bxf);
1094                         by = fl2i(byf);
1095
1096                         if ( fi->fade_anim.first_frame >= 0 ) {
1097                                 fi->fade_anim.sx = bx;
1098                                 fi->fade_anim.sy = by;
1099 #ifdef MAKE_FS1
1100                                 // FS1 has the anis reversed from FS2 so play them backwards
1101                                 hud_anim_render(&fi->fade_anim, frametime, 1, 0, 0, 1);
1102 #else                   
1103                                 hud_anim_render(&fi->fade_anim, frametime, 1, 0, 0, 0);
1104 #endif
1105                         }
1106                 }
1107         }
1108 }
1109
1110 // figure out how far an icon should move based on the elapsed time
1111 float brief_icon_get_dist_moved(icon_move_info *mi, float elapsed_time)
1112 {
1113         float time, dist_moved=0.0f;
1114         
1115         // first half of movement
1116         if ( elapsed_time < mi->total_move_time/2.0f ) {
1117                 dist_moved=0.5f*mi->accel*elapsed_time*elapsed_time;    // d = 1/2at^2
1118                 return dist_moved;
1119         }
1120
1121         // second half of movement
1122         time=elapsed_time - mi->total_move_time/2.0f;
1123         dist_moved=(mi->total_dist/2.0f)+(mi->peak_speed*time) - 0.5f*mi->accel*time*time;
1124         return dist_moved;
1125 }
1126
1127 // Draw a line between two icons on the briefing screen
1128 void brief_render_icon_line(int stage_num, int line_num)
1129 {
1130         brief_line      *bl;
1131         brief_icon      *icon[2];
1132         vertex          icon_vertex[2];
1133         int                     icon_status[2] = {0,0};
1134         int                     icon_w, icon_h, i;
1135         float                   icon_x[2], icon_y[2];
1136
1137         bl = &Briefing->stages[stage_num].lines[line_num];
1138         icon[0] = &Briefing->stages[stage_num].icons[bl->start_icon];
1139         icon[1] = &Briefing->stages[stage_num].icons[bl->end_icon];
1140
1141         // project icons
1142         for (i=0; i<2; i++) {
1143                 g3_rotate_vertex(&icon_vertex[i],&icon[i]->pos);
1144                 if (!(icon_vertex[i].flags&PF_PROJECTED))
1145                         g3_project_vertex(&icon_vertex[i]);
1146
1147                 if (!(icon_vertex[i].flags & PF_OVERFLOW) ) {  // make sure point projected before drawing text
1148                         icon_status[i]=1;
1149                 }
1150         }
1151
1152         if ( !icon_status[0] || !icon_status[1] ) {
1153                 return;
1154         }
1155
1156         // get screen (x,y) for icons
1157         for (i=0; i<2; i++) {
1158                 brief_common_get_icon_dimensions(&icon_w, &icon_h, icon[i]->type, icon[i]->ship_class);
1159                 icon_x[i] = icon_vertex[i].sx;
1160                 icon_y[i] = icon_vertex[i].sy;
1161         }
1162
1163         brief_set_icon_color(icon[0]->team);
1164
1165         gr_line(fl2i(icon_x[0]), fl2i(icon_y[0]), fl2i(icon_x[1]), fl2i(icon_y[1]));
1166 }
1167
1168 // -------------------------------------------------------------------------------------
1169 // Draw a briefing icon
1170 //
1171 // parameters:          stage_num               =>              briefing stage number (start at 0)
1172 //                                              icon_num                        =>              icon number in stage
1173 //                                              frametime               =>              time elapsed in seconds
1174 //                                              selected                        =>              FRED only (will be 0 or non-zero)
1175 //                                              w_scale_factor  =>              scale icon in width by this amount (default 1.0f)
1176 //                                              h_scale_factor  =>              scale icon in height by this amount (default 1.0f)
1177 void brief_render_icon(int stage_num, int icon_num, float frametime, int selected, float w_scale_factor, float h_scale_factor)
1178 {
1179         brief_icon      *bi, *closeup_icon;
1180         hud_frames      *ib;
1181         vertex          tv;     // temp vertex used to find screen position for text
1182         vector          *pos = NULL;
1183         int                     bx,by,bc,w,h,icon_w,icon_h,icon_bitmap=-1;
1184         float                   bxf, byf, dist=0.0f;
1185
1186         SDL_assert( Briefing != NULL );
1187         
1188         bi = &Briefing->stages[stage_num].icons[icon_num];
1189
1190         icon_move_info *mi, *next;
1191         int interp_pos_found = 0;
1192         
1193         mi = GET_FIRST(&Icon_move_list);
1194         if (mi)
1195                 while ( mi != &Icon_move_list ) {
1196                         next = mi->next;
1197                         if ( ( mi->id != 0 ) && ( mi->id == bi->id ) ) {
1198
1199                                 if ( !mi->reached_dest ) {
1200                                         dist = brief_icon_get_dist_moved(mi, Elapsed_time);
1201                                         if ( dist < mi->last_dist ) {
1202                                                 mi->reached_dest=1;
1203                                                 mi->last_dist=0.0f;
1204                                         }
1205                                         mi->last_dist=dist;
1206                                 }
1207
1208                                 if ( !mi->reached_dest ) {
1209                                         vector dist_moved;
1210                                         vm_vec_copy_scale(&dist_moved, &mi->direction, dist);
1211                                         vm_vec_add(&mi->current, &mi->start, &dist_moved);
1212                                 } else {
1213                                         mi->current = mi->finish;
1214                                 }
1215
1216                                 pos = &mi->current;
1217                                 interp_pos_found = 1;
1218                                 break;
1219                         }
1220                         mi = next;
1221                 }
1222         
1223         if ( !interp_pos_found )
1224                 pos = &bi->pos;
1225                 
1226         brief_render_elements(pos, The_grid);
1227         g3_rotate_vertex(&tv,pos);
1228
1229         if (!(tv.flags&PF_PROJECTED))
1230                 g3_project_vertex(&tv);
1231
1232         if (!(tv.flags & PF_OVERFLOW) ) {  // make sure point projected before drawing text
1233
1234                 brief_set_icon_color(bi->team);
1235
1236                 int species = ship_get_species_by_type(bi->ship_class);
1237
1238 #ifdef MAKE_FS1
1239                 species = 0;
1240 #endif
1241
1242                 if(species < 0){
1243                         return;
1244                 }
1245
1246                 ib = &Icon_bitmaps[bi->type][species];
1247                 if ( ib->first_frame < 0 ) {
1248                         Int3();
1249                         return;
1250                 }
1251
1252                 brief_common_get_icon_dimensions(&icon_w, &icon_h, bi->type, bi->ship_class);
1253
1254                 closeup_icon = brief_get_closeup_icon();
1255
1256                 if ( bi == closeup_icon || selected ) {
1257                         icon_bitmap=ib->first_frame+1;
1258 //                      gr_set_bitmap(ib->first_frame+1);
1259                 }
1260                 else {
1261                         icon_bitmap=ib->first_frame;
1262 //                      gr_set_bitmap(ib->first_frame);
1263                 }
1264         
1265                 // draw icon centered at draw position
1266 //              bx = fl2i(tv.sx - ib->icon_w/2.0f);
1267 //              by = fl2i(tv.sy - ib->icon_h/2.0f);
1268 //              bc = bx + fl2i(ib->icon_w/2.0f);
1269                 //gr_aabitmap(bx, by);
1270
1271                 float scaled_w, scaled_h;
1272                 
1273                 scaled_w = icon_w * w_scale_factor;
1274                 scaled_h = icon_h * h_scale_factor;
1275                 bxf = tv.sx - scaled_w / 2.0f + 0.5f;
1276                 byf = tv.sy - scaled_h / 2.0f + 0.5f;
1277                 bx = fl2i(bxf);
1278                 by = fl2i(byf);
1279                 bc = fl2i(tv.sx);
1280
1281                 if ( !Fred_running && ((bx < 0) || (bx > gr_screen.max_w) || (by < 0) || (by > gr_screen.max_h)) ) {
1282                         bi->x = bx;
1283                         bi->y = by;
1284                         return;
1285                 }
1286
1287                 vertex va, vb;
1288                 va.sx = bxf;
1289                 va.sy = byf;
1290                 va.u = 0.0f;
1291                 va.v = 0.0f;
1292
1293                 vb.sx = bxf+scaled_w-1;
1294                 vb.sy = byf+scaled_h-1;
1295                 vb.u = 1.0f;
1296                 vb.v = 1.0f;
1297
1298                 // render highlight anim frame
1299                 if ( (bi->flags&BI_SHOWHIGHLIGHT) && (bi->flags&BI_HIGHLIGHT) ) {
1300                         hud_anim *ha = &bi->highlight_anim;
1301                         if ( ha->first_frame >= 0 ) {
1302                                 ha->sx = bi->hold_x;
1303                                 if ( strlen(bi->label) > 0 ) {
1304                                         ha->sy = bi->hold_y - fl2i(gr_get_font_height()/2.0f +0.5) - 2;
1305                                 } else {
1306                                         ha->sy = bi->hold_y;
1307                                 }
1308
1309                                 //hud_set_iff_color(bi->team);
1310                                 brief_set_icon_color(bi->team);
1311
1312                                 hud_anim_render(ha, frametime, 1, 0, 1);
1313
1314                                 if ( Brief_stage_highlight_sound_handle < 0 ) {
1315                                         if ( !Fred_running) {
1316                                                 Brief_stage_highlight_sound_handle = snd_play(&Snds_iface[SND_ICON_HIGHLIGHT]);                                 
1317                                         }
1318                                 }
1319                         }
1320                 }
1321
1322                 // render fade-in anim frame
1323                 if ( bi->flags & BI_FADEIN ) {
1324                         hud_anim *ha = &bi->fadein_anim;
1325                         if ( ha->first_frame >= 0 ) {
1326                                 ha->sx = bx;
1327                                 ha->sy = by;
1328 //                              hud_set_iff_color(bi->team);
1329                                 brief_set_icon_color(bi->team);
1330
1331 #ifdef MAKE_FS1
1332                                 // FS1 has the anims backwards from FS2 so play in reverse
1333                                 if ( hud_anim_render(ha, frametime, 1, 0, 0, 0) == 0 ) {
1334 #else
1335                                 if ( hud_anim_render(ha, frametime, 1, 0, 0, 1) == 0 ) {
1336 #endif
1337                                         bi->flags &= ~BI_FADEIN;
1338                                 }
1339                         } else {
1340                                 bi->flags &= ~BI_FADEIN;
1341                         }
1342                 }               
1343
1344                 if ( !(bi->flags & BI_FADEIN) ) {
1345                         gr_set_bitmap(icon_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1346
1347                         if ( Fred_running )     {
1348                                 gr_aascaler(&va, &vb);
1349                         } else {
1350                                 // Don't bother scaling for the game
1351                                 gr_aabitmap(bx, by);
1352                         }
1353
1354                         // draw text centered over the icon (make text darker)
1355                         if ( bi->type == ICON_FIGHTER_PLAYER || bi->type == ICON_BOMBER_PLAYER ) {
1356                                 gr_get_string_size(&w,&h,Players[Player_num].callsign);
1357                                 gr_printf(bc - fl2i(w/2.0f), by - h, Players[Player_num].callsign);
1358                         }
1359                         else {
1360                                 if (Lcl_gr) {
1361                                         char buf[128];
1362                                         SDL_strlcpy(buf, bi->label, sizeof(buf));
1363                                         lcl_translate_brief_icon_name(buf, sizeof(buf));
1364                                         gr_get_string_size(&w, &h, buf);
1365                                         gr_printf(bc - fl2i(w/2.0f), by - h, buf);
1366                                 } else if(Lcl_pl) {
1367                                         char buf[128];
1368                                         SDL_strlcpy(buf, bi->label, sizeof(buf));
1369                                         lcl_translate_brief_icon_name_pl(buf, sizeof(buf));
1370                                         gr_get_string_size(&w, &h, buf);
1371                                         gr_printf(bc - fl2i(w/2.0f), by - h, buf);
1372                                 } else {
1373                                         gr_get_string_size(&w,&h,bi->label);
1374                                         gr_printf(bc - fl2i(w/2.0f), by - h, bi->label);
1375                                 }
1376                         }
1377
1378                         // show icon as selected (FRED only)
1379                         if ( selected ) {
1380                                 gr_get_string_size(&w,&h,NOX("(S)"));
1381                                 gr_printf(bc - fl2i(w/2.0f), by - h*2, NOX("(S)"));
1382                         }
1383                 }
1384
1385                 // store screen x,y,w,h
1386                 bi->x = bx;
1387                 bi->y = by;
1388                 bi->w = fl2i(scaled_w);
1389                 bi->h = fl2i(scaled_h);
1390
1391         }  // end if vertex is projected
1392 }
1393
1394 #ifndef PLAT_UNIX
1395 #pragma optimize("", on)
1396 #endif
1397
1398 // -------------------------------------------------------------------------------------
1399 // brief_render_icons()
1400 //
1401 void brief_render_icons(int stage_num, float frametime)
1402 {
1403         int i, num_icons, num_lines;
1404
1405         SDL_assert( Briefing != NULL );
1406         
1407         num_icons = Briefing->stages[stage_num].num_icons;
1408         num_lines = Briefing->stages[stage_num].num_lines;
1409
1410         if ( Cam_target_reached ) {
1411                 for ( i = 0; i < num_lines; i++ ) {
1412                         brief_render_icon_line(stage_num, i);
1413                 }
1414         }
1415
1416         for ( i = 0; i < num_icons; i++ ) {
1417                 brief_render_icon(stage_num, i, frametime, 0);
1418         }
1419 }
1420
1421 // ------------------------------------------------------------------------------------
1422 // brief_start_highlight_anims()
1423 //
1424 //      see if there are any highlight animations to play
1425 //
1426 void brief_start_highlight_anims(int stage_num)
1427 {
1428         brief_stage             *bs;
1429         brief_icon              *bi;
1430         int                             x,y,i,anim_w,anim_h;
1431
1432         SDL_assert( Briefing != NULL );
1433         bs = &Briefing->stages[stage_num];
1434         
1435         for ( i = 0; i < bs->num_icons; i++ ) {
1436                 bi = &bs->icons[i];
1437                 if ( bi->flags & BI_HIGHLIGHT ) {
1438                         bi->flags &= ~BI_SHOWHIGHLIGHT;
1439                         if ( bi->highlight_anim.first_frame < 0 ) {
1440                                 continue;
1441                         }
1442
1443                         bi->highlight_anim.time_elapsed=0.0f;
1444
1445                         bm_get_info( bi->highlight_anim.first_frame, &anim_w, &anim_h, NULL);
1446                         x = fl2i( i2fl(bi->x) + bi->w/2.0f - anim_w/2.0f );
1447                         y = fl2i( i2fl(bi->y) + bi->h/2.0f - anim_h/2.0f );
1448                         bi->hold_x = x;
1449                         bi->hold_y = y;
1450                         bi->flags |= BI_SHOWHIGHLIGHT;
1451                         bi->highlight_anim.time_elapsed=0.0f;
1452                 }
1453         }
1454 }
1455
1456 // -------------------------------------------------------------------------------------
1457 // brief_render_map()
1458 //
1459 //
1460 void brief_render_map(int stage_num, float frametime)
1461 {
1462         gr_set_clip(bscreen.map_x1 + 1, bscreen.map_y1 + 1, bscreen.map_x2 - bscreen.map_x1 - 1, bscreen.map_y2 - bscreen.map_y1 - 2);
1463         
1464         // REMOVED by neilk: removed gr_clear for FS2 because interface no longer calls for black background on grid
1465 #ifdef MAKE_FS1
1466         gr_clear();
1467 #endif
1468
1469         SDL_assert(Briefing);
1470
1471         if (stage_num >= Briefing->num_stages) {
1472                 gr_reset_clip();
1473                 return;
1474         }
1475
1476         g3_start_frame(0);
1477         g3_set_view_matrix(&Current_cam_pos, &Current_cam_orient, 0.5f);
1478
1479         brief_maybe_create_new_grid(The_grid, &Current_cam_pos, &Current_cam_orient);
1480         brief_render_grid(The_grid);
1481
1482         brief_render_fade_outs(frametime);
1483
1484         // go ahead and render everything that is in the active objects list
1485         brief_render_icons(stage_num, frametime);
1486
1487         if ( Cam_target_reached && brief_text_wipe_finished() ) {
1488
1489                 if ( Brief_textdraw_finished == 0 ) {
1490                         Brief_textdraw_finished = 1;
1491                         Brief_stage_time = 0;
1492                 }
1493
1494                 if ( Play_highlight_flag ) {
1495                         brief_start_highlight_anims(stage_num);
1496                         Play_highlight_flag = 0;
1497                 }
1498         }
1499
1500         anim_render_all(ON_BRIEFING_SELECT, frametime);
1501
1502         gr_reset_clip();
1503         g3_end_frame();
1504 }
1505
1506
1507 // -------------------------------------------------------------------------------------
1508 // brief_text_set_color()
1509 //
1510 void brief_text_set_color(char c) {
1511         switch ( c ) {
1512                 case 'f':
1513                         SET_COLOR_FRIENDLY;
1514                         break;
1515                 case 'h':
1516                         SET_COLOR_HOSTILE;
1517                         break;
1518                 case 'n':
1519                         SET_COLOR_NEUTRAL;
1520                         break;
1521                 case 'r':
1522                         gr_set_color_fast(&Color_red);
1523                         break;
1524                 case 'g':
1525                         gr_set_color_fast(&Color_green);
1526                         break;
1527                 case 'b':
1528                         gr_set_color_fast(&Color_blue);
1529                         break;
1530                 default:        
1531                         Int3(); // unsupported meta-code
1532                         break;
1533         } // end switch
1534 }
1535
1536 // Display what stage of the briefing is active
1537 void brief_blit_stage_num(int stage_num, int stage_max)
1538 {
1539         char buf[64];
1540         // int w;
1541
1542         SDL_assert( Briefing != NULL );
1543 #ifdef MAKE_FS1
1544         gr_set_color_fast(&Color_bright_blue);
1545 #else
1546         gr_set_color_fast(&Color_text_heading);
1547 #endif
1548         SDL_snprintf(buf, sizeof(buf), XSTR( "Stage %d of %d", 394), stage_num + 1, stage_max);
1549         if (Game_mode & GM_MULTIPLAYER) {
1550                 gr_printf(Brief_stage_text_coords_multi[gr_screen.res][0], Brief_stage_text_coords_multi[gr_screen.res][1], buf);
1551         } else {
1552                 gr_printf(Brief_stage_text_coords[gr_screen.res][0], Brief_stage_text_coords[gr_screen.res][1], buf);
1553         }
1554
1555         // draw the title of the mission        
1556         // if this goes above briefing text, it will need to be raised 10 pixels in multiplayer to make
1557         // room for stage num, which makes toom for chat box
1558         /*
1559         if (Game_mode & GM_MULTIPLAYER) {
1560                 gr_get_string_size(&w,NULL,The_mission.name);
1561                 gr_string(bscreen.map_x2 - w, bscreen.map_y2 + 5, The_mission.name);            
1562         } else {
1563                 gr_get_string_size(&w,NULL,The_mission.name);
1564                 gr_string(bscreen.map_x2 - w, bscreen.map_y2 + 5, The_mission.name);            
1565         }
1566         */
1567 }
1568
1569 // Render a line of text for the briefings.  Lines are drawn in as a wipe, with leading bright
1570 // white characters.  Have to jump through some hoops since we support colored words.  This means
1571 // that we need to process the line one character at a time.
1572 void brief_render_line(int line_num, int x, int y, int instance)
1573 {
1574         int len, count, next, truncate_len, last_color, offset, w, h, bright_len, i;
1575         colored_char *src;
1576         char line[MAX_BRIEF_LINE_LEN];
1577
1578         src = &Colored_text[instance][line_num][0];
1579         len = Colored_text_len[instance][line_num];
1580
1581         if (len <= 0){
1582                 return;
1583         }
1584
1585         truncate_len = fl2i(Brief_text_wipe_time_elapsed / BRIEF_TEXT_WIPE_TIME * Max_briefing_line_len);
1586         if (truncate_len > len){
1587                 truncate_len = len;
1588         }
1589
1590         bright_len = 0;
1591         if (truncate_len < len) {
1592                 if (truncate_len <= BRIGHTEN_LEAD) {
1593                         bright_len = truncate_len;
1594                         truncate_len = 0;
1595
1596                 } else {
1597                         bright_len = BRIGHTEN_LEAD;
1598                         truncate_len -= BRIGHTEN_LEAD; 
1599                 }
1600         }
1601
1602         offset = 0;
1603         count  = 0;
1604         next     = 0;
1605
1606         gr_set_color_fast(&Color_white);
1607         last_color = BRIEF_TEXT_WHITE;
1608         for (i=0; i<truncate_len; i++) {
1609                 if (count >= truncate_len){
1610                         break;
1611                 }
1612
1613                 line[next] = src[count].letter;
1614
1615                 if (is_white_space(line[next])) {
1616                         // end of word reached, blit it
1617                         line[next + 1] = 0;
1618                         gr_string(x + offset, y, line);
1619                         gr_get_string_size(&w, &h, line);
1620                         offset += w;
1621                         next = 0;
1622
1623                         // reset color
1624                         if (last_color != BRIEF_TEXT_WHITE) {
1625                                 brief_set_text_color(BRIEF_TEXT_WHITE);
1626                                 last_color = BRIEF_TEXT_WHITE;
1627                         }
1628
1629                         count++;
1630                         continue;
1631                 }
1632
1633                 if (src[count].color != last_color) {
1634                         brief_set_text_color(src[count].color);
1635                         last_color = src[count].color;
1636                 }
1637
1638                 count++;
1639                 next++;
1640         }       // end for
1641
1642         line[next] = 0;
1643         gr_string(x + offset, y, line);
1644
1645
1646         // draw leading portion of the line bright white
1647         if (bright_len) {
1648
1649                 gr_set_color_fast(&Color_bright_white);
1650                 for (i=0; i<truncate_len+bright_len; i++) {
1651                         line[i] = src[i].letter;
1652                 }
1653
1654                 line[i] = 0;
1655
1656
1657                 if ( truncate_len > 0 ) {
1658                         int width_dim, height_dim;
1659                         gr_get_string_size(&width_dim, &height_dim, line, truncate_len );
1660                         gr_string(x+width_dim, y, &line[truncate_len]);
1661                 } else {
1662                         gr_string(x, y, line);
1663                 }
1664
1665                 // JAS: Not needed?
1666                 //              // now erase the part we don't want to be bright white
1667                 //              gr_set_color_fast(&Color_black);
1668                 //              if (i > BRIGHTEN_LEAD) {
1669                 //                      line[i - BRIGHTEN_LEAD] = 0;
1670                 //                      gr_get_string_size(&w, &h, line);
1671                 //                      gr_set_clip(x, y, w, gr_get_font_height());
1672                 //                      gr_clear();
1673                 //                      gr_reset_clip();
1674                 //              }
1675         }
1676 }
1677
1678 int brief_text_wipe_finished()
1679 {
1680         if ( Brief_text_wipe_time_elapsed > (BRIEF_TEXT_WIPE_TIME+0.5f) ) {
1681                 return 1;
1682         }
1683
1684         return 0;
1685 }
1686
1687 // -------------------------------------------------------------------------------------
1688 // brief_render_text()
1689 //
1690 // input:       frametime       =>      Time in seconds of previous frame
1691 //                              instance                =>      Optional parameter.  Used to indicate which text stream is used.
1692 //                                                                      This value is 0 unless multiple text streams are required
1693 int brief_render_text(int line_offset, int x, int y, int h, float frametime, int instance, int line_spacing)
1694 {
1695         int fh, line, yy;
1696
1697         fh = gr_get_font_height();
1698         if (Brief_text_wipe_time_elapsed == 0) {
1699                 if (snd_is_playing(Brief_text_wipe_snd)) {
1700                         snd_stop(Brief_text_wipe_snd);
1701                 }
1702                 gamesnd_play_iface(SND_BRIEF_TEXT_WIPE);
1703                 Play_brief_voice = 1;
1704         }
1705
1706         Brief_text_wipe_time_elapsed += frametime;
1707
1708         line = line_offset;
1709         yy = 0;
1710         while (yy + fh <= h) {
1711                 if (line >= Num_brief_text_lines[instance])
1712                         break;
1713
1714                 brief_render_line(line, x, y + yy, instance);
1715
1716                 line++;
1717                 yy += fh + line_spacing;
1718         }
1719
1720         if ( brief_text_wipe_finished() && (Play_brief_voice) ) {
1721                 Play_brief_voice = 0;
1722                 return 1;
1723         }
1724
1725         return 0;
1726 }
1727
1728 // ------------------------------------------------------------------------------------
1729 // brief_render_elements()
1730 //
1731 // Draw the lines that show objects positions on the grid
1732 //
1733 void brief_render_elements(vector *pos, grid* gridp)
1734 {
1735         vector  gpos;   //      Location of point on grid.
1736 //      vector  tpos;
1737 //      float           dxz;
1738         plane           tplane;
1739         vector  *gv;
1740         
1741         if ( pos->xyz.y < 1 && pos->xyz.y > -1 )
1742                 return;
1743
1744         tplane.A = gridp->gmatrix.v.uvec.xyz.x;
1745         tplane.B = gridp->gmatrix.v.uvec.xyz.y;
1746         tplane.C = gridp->gmatrix.v.uvec.xyz.z;
1747         tplane.D = gridp->planeD;
1748
1749         compute_point_on_plane(&gpos, &tplane, pos);
1750
1751 //      dxz = vm_vec_dist(pos, &gpos)/8.0f;
1752
1753         gv = &gridp->gmatrix.v.uvec;
1754         if (gv->xyz.x * pos->xyz.x + gv->xyz.y * pos->xyz.y + gv->xyz.z * pos->xyz.z < -gridp->planeD)
1755                 gr_set_color(127, 127, 127);
1756         else
1757                 gr_set_color(255, 255, 255);   // white
1758
1759 // AL 11-20-97: don't draw elevation lines.. they are confusing
1760 /*
1761         brief_rpd_line(&gpos, pos);     //      Line from grid to object center.
1762
1763         tpos = gpos;
1764
1765         vm_vec_scale_add2(&gpos, &gridp->gmatrix.v.rvec, -dxz/2);
1766         vm_vec_scale_add2(&gpos, &gridp->gmatrix.v.fvec, -dxz/2);
1767         
1768         vm_vec_scale_add2(&tpos, &gridp->gmatrix.v.rvec, dxz/2);
1769         vm_vec_scale_add2(&tpos, &gridp->gmatrix.v.fvec, dxz/2);
1770         
1771         brief_rpd_line(&gpos, &tpos);
1772
1773         vm_vec_scale_add2(&gpos, &gridp->gmatrix.v.rvec, dxz);
1774         vm_vec_scale_add2(&tpos, &gridp->gmatrix.v.rvec, -dxz);
1775
1776         brief_rpd_line(&gpos, &tpos);
1777 */
1778 }
1779
1780
1781 // ------------------------------------------------------------------------------------
1782 // brief_reset_icons()
1783 //
1784 void brief_reset_icons(int stage_num)
1785 {
1786         brief_stage             *bs;
1787         brief_icon              *bi;
1788         int                             i;
1789
1790         SDL_assert( Briefing != NULL );
1791         bs = &Briefing->stages[stage_num];
1792
1793         for ( i = 0; i < bs->num_icons; i++ ) {
1794                 bi = &bs->icons[i];
1795                 bi->flags &= ~BI_SHOWHIGHLIGHT;
1796         }
1797 }
1798
1799 // ------------------------------------------------------------------------------------
1800 // brief_set_camera_target()
1801 //
1802 //      input:  pos             =>              target position for the camera
1803 //                              orient  =>              target orientation for the camera
1804 //                              time            =>              time in ms to reach target
1805 //
1806 void brief_set_camera_target(vector *pos, matrix *orient, int time)
1807 {
1808         float time_in_seconds;
1809         
1810         time_in_seconds = time / 1000.0f;
1811
1812         if ( time == 0 ) {
1813                 Current_cam_pos = *pos;
1814                 Current_cam_orient = *orient;
1815         }
1816
1817         Target_cam_pos = *pos;
1818         Target_cam_orient = *orient;
1819         Start_cam_orient = Current_cam_orient;
1820         Start_cam_pos = Current_cam_pos;                                                                // we need this when checking if camera movement complete
1821         Start_cam_move = timer_get_milliseconds()*1000.0f;              // start time, convert to seconds
1822         Total_move_time = time_in_seconds;
1823         Elapsed_time = 0.0f;
1824
1825         vm_vec_scale_add(&Start_lookat_pos, &Start_cam_pos, &Start_cam_orient.v.fvec, LOOKAT_DIST);
1826         vm_vec_scale_add(&Target_lookat_pos, &Target_cam_pos, &Target_cam_orient.v.fvec, LOOKAT_DIST);
1827
1828         Play_highlight_flag = 1;                                                                // once target reached, play highlight anims
1829         Cam_target_reached = 0;
1830         Cam_movement_done=0;
1831         anim_release_all_instances(ON_BRIEFING_SELECT); // stop any briefing-specific anims
1832         
1833         // calculate camera velocity
1834         vm_vec_sub(&Cam_vel, pos, &Current_cam_pos);
1835 //      vm_vec_scale(&Cam_vel, 1.0f/time_in_seconds);
1836         if ( !IS_VEC_NULL(&Cam_vel) ) {
1837                 vm_vec_normalize(&Cam_vel);
1838         }
1839
1840         // calculate lookat point velocity
1841         vm_vec_sub(&Lookat_vel, &Target_lookat_pos, &Current_lookat_pos);
1842         vm_vec_scale(&Lookat_vel, 1.0f/time_in_seconds);
1843
1844         Start_dist = vm_vec_dist(&Start_cam_pos, &Start_lookat_pos);
1845         End_dist = vm_vec_dist(&Target_cam_pos, &Target_lookat_pos);
1846         Dist_change_rate = (End_dist - Start_dist) / time_in_seconds;
1847
1848         Total_dist=vm_vec_dist(&Start_cam_pos, &Target_cam_pos);
1849
1850 //      Peak_speed=Total_dist/Total_move_time*1.5f;
1851 //      Cam_accel = Peak_speed/Total_move_time*3.0f;
1852
1853         Peak_speed=Total_dist/Total_move_time*2.0f;
1854         Cam_accel = 4*Total_dist/(Total_move_time*Total_move_time);
1855         Last_dist=0.0f;
1856
1857         vm_vec_zero(&W_init);
1858
1859         get_camera_limits(&Start_cam_orient, &Target_cam_orient, Total_move_time, &Acc_limit, &Vel_limit);
1860 }
1861
1862
1863 ubyte brief_return_color_index(char c)
1864 {
1865         switch (c) {
1866                 case 'f':
1867                         return BRIEF_IFF_FRIENDLY;
1868
1869                 case 'h':
1870                         return BRIEF_IFF_HOSTILE;
1871
1872                 case 'n':
1873                         return BRIEF_IFF_NEUTRAL;
1874
1875                 case 'r':
1876                         return BRIEF_TEXT_RED;
1877
1878                 case 'g':
1879                         return BRIEF_TEXT_GREEN;
1880
1881                 case 'b':
1882                         return BRIEF_TEXT_BLUE;
1883
1884                 default:        
1885                         Int3(); // unsupported meta-code
1886                         break;
1887         } // end switch
1888
1889         return BRIEF_TEXT_WHITE;
1890 }
1891
1892 void brief_set_text_color(int color_index)
1893 {
1894         SDL_assert(color_index < MAX_BRIEF_TEXT_COLORS);
1895         gr_set_color_fast(Brief_text_colors[color_index]);
1896 }
1897
1898 // Set up the Colored_text array.
1899 // input:               index           =>              Index into Brief_text[] for source text.
1900 //                                      instance        =>              Which instance of Colored_text[] to use.  
1901 //                                                                              Value is 0 unless multiple text streams are required.
1902 int brief_text_colorize(int index, int instance)
1903 {
1904         char *src;
1905         int len, i, skip_to_next_word, dest_len;
1906         colored_char *dest;
1907         ubyte active_color_index;
1908
1909         src = Brief_text[index];
1910         dest = &Colored_text[instance][index][0];
1911         len = strlen(src);
1912
1913         skip_to_next_word = 0;
1914         dest_len = 0;
1915         active_color_index = BRIEF_TEXT_WHITE;
1916         for (i=0; i<len; i++) {
1917                 if (skip_to_next_word) {
1918                         if (is_white_space(src[i])) {
1919                                 skip_to_next_word = 0;
1920                         }
1921
1922                         continue;
1923                 }
1924
1925                 if ( src[i] == BRIEF_META_CHAR && is_white_space(src[i + 2]) ) {
1926                         active_color_index = brief_return_color_index(src[i + 1]);
1927                         skip_to_next_word = 1;
1928                         continue;
1929                 }
1930
1931                 if (is_white_space(src[i])) {
1932                         active_color_index = BRIEF_TEXT_WHITE;
1933                 }
1934
1935                 dest[dest_len].letter = src[i];
1936                 dest[dest_len].color  = active_color_index;
1937                 dest_len++;
1938         } // end for
1939
1940         dest[dest_len].letter = 0;
1941         Colored_text_len[instance][index] = dest_len;
1942         return len;
1943 }
1944
1945 // ------------------------------------------------------------------------------------
1946 // brief_color_text_init()
1947 //
1948 //      input:  src             =>              paragraph of text to process
1949 //                              w                       =>              max width of line in pixels
1950 //                              instance        =>              optional parameter, used when multiple text streams are required
1951 //                                                                      (default value is 0)
1952 int brief_color_text_init(char *src, int w, int instance)
1953 {
1954         int i, n_lines, len;
1955         int n_chars[MAX_BRIEF_LINES];
1956         char *p_str[MAX_BRIEF_LINES];
1957         
1958         SDL_assert(src);
1959         n_lines = split_str(src, w, n_chars, p_str, MAX_BRIEF_LINES, BRIEF_META_CHAR);
1960         SDL_assert(n_lines >= 0);
1961
1962         Max_briefing_line_len = 1;
1963         for (i=0; i<n_lines; i++) {
1964                 SDL_assert(n_chars[i] < MAX_BRIEF_LINE_LEN);
1965                 len = min(n_chars[i] + 1, MAX_BRIEF_LINE_LEN);
1966                 SDL_strlcpy(Brief_text[i], p_str[i], len);
1967                 Brief_text[i][n_chars[i]] = 0;
1968                 drop_leading_white_space(Brief_text[i]);
1969                 len = brief_text_colorize(i, instance);
1970                 if (len > Max_briefing_line_len)
1971                         Max_briefing_line_len = len;
1972         }
1973
1974         Brief_text_wipe_time_elapsed = 0.0f;
1975         Play_brief_voice = 0;
1976
1977         Num_brief_text_lines[instance] = n_lines;
1978         return n_lines;
1979 }
1980
1981 // ------------------------------------------------------------------------------------
1982 // brief_get_free_move_icon()
1983 //
1984 //      returns:                failure =>              -1
1985 //                                      success =>              handle to a free move icon struct
1986 //
1987 int brief_get_free_move_icon()
1988 {
1989         int i;
1990
1991         for ( i = 0; i < MAX_MOVE_ICONS; i++ ) {
1992                 if ( Icon_movers[i].used == 0 )
1993                         break;
1994         }
1995         
1996         if ( i == MAX_MOVE_ICONS ) 
1997                 return -1;
1998
1999         Icon_movers[i].used = 1;
2000         return i;
2001 }
2002
2003
2004 // ------------------------------------------------------------------------------------
2005 // brief_set_move_list()
2006 //
2007 //      input:  new_stage               =>              new stage number that briefing is now moving to
2008 //                              current_stage   =>              current stage that the briefing is on
2009 //                              time                            =>              time in seconds
2010 //
2011 int brief_set_move_list(int new_stage, int current_stage, float time)
2012 {
2013         brief_stage             *newb, *cb;     
2014         icon_move_info  *imi;   
2015         int                             i,j,k,num_movers,is_gone=0;
2016         vector                  zero_v = ZERO_VECTOR;
2017
2018         SDL_assert(new_stage != current_stage);
2019         
2020         SDL_assert( Briefing != NULL );
2021         newb = &Briefing->stages[new_stage];
2022         cb = &Briefing->stages[current_stage];
2023         num_movers = 0;
2024         
2025         for ( i = 0; i < cb->num_icons; i++ ) {
2026                 is_gone=1;
2027                 for ( j = 0; j < newb->num_icons; j++ ) {
2028                         if ( ( cb->icons[i].id != 0 ) && ( cb->icons[i].id == newb->icons[j].id ) ) {
2029                                 is_gone=0;
2030                                 if ( vm_vec_cmp(&cb->icons[i].pos, &newb->icons[j].pos) ) {
2031                                         //nprintf(("Alan","We found a match in icon %s\n", cb->icons[i].label));
2032                                         k = brief_get_free_move_icon();                         
2033                                         if ( k == -1 ) {
2034                                                 Int3(); // should never happen, get Alan
2035                                                 return 0;
2036                                         }
2037                                         imi = &Icon_movers[k];
2038                                         imi->id = cb->icons[i].id;
2039                                         imi->start = cb->icons[i].pos;
2040                                         imi->finish = newb->icons[j].pos;
2041                                         imi->current = imi->start;
2042                                         list_append(&Icon_move_list, imi);
2043
2044                                         imi->total_dist = vm_vec_dist(&imi->start, &imi->finish);
2045                                         imi->total_move_time = time;
2046                                         imi->peak_speed = imi->total_dist/imi->total_move_time*2.0f;
2047                                         imi->accel = 4*imi->total_dist/(time*time);
2048                                         imi->last_dist=0.0f;
2049                                         imi->reached_dest=0;
2050                                         imi->direction = zero_v;
2051
2052                                         vm_vec_sub(&imi->direction, &imi->finish, &imi->start);
2053                                         if ( !IS_VEC_NULL(&imi->direction) ) {
2054                                                 vm_vec_normalize(&imi->direction);
2055                                         }
2056
2057                                         num_movers++;
2058                                 }
2059                         }
2060                 }
2061
2062                 // Set up fading icon (to fade out)
2063                 if (is_gone == 1) {
2064                         if ( Num_fade_icons >= MAX_FADE_ICONS ) {
2065                                 Int3();
2066                                 Num_fade_icons=0;
2067                         }
2068
2069                         int species = ship_get_species_by_type(cb->icons[i].ship_class);
2070
2071 #ifdef MAKE_FS1
2072                         species = 0;
2073 #endif
2074
2075                         if(species < 0) {
2076                                 return 0;
2077                         }
2078
2079                         Fading_icons[Num_fade_icons].fade_anim = Icon_fade_anims[cb->icons[i].type][species];
2080                         Fading_icons[Num_fade_icons].pos = cb->icons[i].pos;
2081                         Fading_icons[Num_fade_icons].team = cb->icons[i].team;
2082                         Num_fade_icons++;
2083                 }
2084         }
2085
2086         // flag new icons for fading in
2087         for ( i=0; i<newb->num_icons; i++ ) {
2088                 int is_new = 1;
2089                 newb->icons[i].flags &= ~BI_FADEIN;
2090                 for ( j=0; j<cb->num_icons; j++ ) {
2091                         if ( ( cb->icons[j].id != 0 ) && ( cb->icons[j].id == newb->icons[i].id ) ) {
2092                                 is_new=0;
2093                         }
2094                 }
2095                 if ( is_new ) {
2096                         int species = ship_get_species_by_type(newb->icons[i].ship_class);
2097
2098 #ifdef MAKE_FS1
2099                         species = 0;
2100 #endif
2101
2102                         if(species < 0) {
2103                                 return 0;
2104                         }
2105
2106                         newb->icons[i].flags |= BI_FADEIN;
2107                         newb->icons[i].fadein_anim = Icon_fade_anims[newb->icons[i].type][species];
2108                         newb->icons[i].fadein_anim.time_elapsed = 0.0f;
2109                 }
2110         }
2111
2112         return num_movers;
2113 }
2114
2115 void brief_clear_fade_out_icons()
2116 {
2117         Num_fade_icons = 0;
2118 }
2119
2120
2121 // ------------------------------------------------------------------------------------
2122 // brief_set_new_stage()
2123 //
2124 //      input:  pos                     =>              target position for the camera
2125 //                              orient          =>              target orientation for the camera
2126 //                              time                    =>              time in ms to reach target
2127 //                              stage_num       =>              stage number of briefing (start numbering at 0)
2128 //
2129
2130 void brief_set_new_stage(vector *pos, matrix *orient, int time, int stage_num)
2131 {
2132         char msg[MAX_BRIEF_LEN];
2133         int num_movers, new_time, not_objv = 1;
2134
2135         SDL_assert( Briefing != NULL );
2136         new_time = time;
2137
2138         if (stage_num >= Briefing->num_stages) {
2139                 not_objv = 0;  // turns out this is an objectives stage
2140                 new_time = 0;
2141         }
2142
2143         if ( stage_num == Last_new_stage ) {
2144                 return;
2145         }
2146
2147         num_movers = 0;
2148         brief_move_icon_reset();
2149         brief_clear_fade_out_icons();
2150         if ( (Last_new_stage != -1) && not_objv ) {
2151                 num_movers = brief_set_move_list(stage_num, Last_new_stage, new_time / 1000.0f);
2152         }
2153
2154         if ( (Last_new_stage != -1) && (num_movers == 0) && not_objv ) {
2155                 if ( !vm_vec_cmp( &Briefing->stages[stage_num].camera_pos, &Briefing->stages[Last_new_stage].camera_pos) ) {
2156                         if ( !vm_vec_cmp( &Briefing->stages[stage_num].camera_orient.v.fvec, &Briefing->stages[Last_new_stage].camera_orient.v.fvec) ){
2157                                 new_time = 0;
2158                         }
2159                 }
2160         }
2161
2162         if (not_objv) {
2163                 if(Briefing->stages[stage_num].new_text == NULL){
2164                         SDL_strlcpy(msg, "", sizeof(msg));
2165                 } else {
2166                         SDL_strlcpy(msg, Briefing->stages[stage_num].new_text, sizeof(msg));
2167                 }
2168         } else {
2169                 SDL_strlcpy(msg, XSTR( "Please review your objectives for this mission.", 395), sizeof(msg));
2170         }
2171
2172         if (gr_screen.res == GR_640) {
2173                 // GR_640
2174                 Num_brief_text_lines[0] = brief_color_text_init(msg, MAX_BRIEF_LINE_W_640);
2175         } else {
2176                 // GR_1024
2177                 Num_brief_text_lines[0] = brief_color_text_init(msg, MAX_BRIEF_LINE_W_1024);            
2178         }
2179         Top_brief_text_line = 0;
2180
2181         if (not_objv){
2182                 brief_set_camera_target(pos, orient, new_time);
2183         }
2184
2185         if ( snd_is_playing(Brief_stage_highlight_sound_handle) ) {
2186                 snd_stop(Brief_stage_highlight_sound_handle);
2187         }
2188
2189         Brief_voice_ended = 0;
2190         Brief_textdraw_finished = 0;
2191         Brief_voice_started = 0;
2192         Brief_stage_time = 0;
2193
2194
2195         Brief_stage_highlight_sound_handle = -1;
2196         Last_new_stage = stage_num;
2197 }
2198
2199 // ------------------------------------------------------------------------------------
2200 // camera_pos_past_target()
2201 //
2202 //
2203 int camera_pos_past_target(vector *start, vector *current, vector *dest)
2204 {
2205         vector num, den;
2206         float ratio;
2207
2208         vm_vec_sub(&num, current, start);
2209         vm_vec_sub(&den, start, dest);
2210
2211         ratio = vm_vec_mag_quick(&num) / vm_vec_mag_quick(&den);
2212         if (ratio >= 1.0f)
2213                 return TRUE;
2214         
2215         return FALSE;
2216 }
2217
2218 // ------------------------------------------------------------------------------------
2219 // Interpolate between matrices.
2220 // elapsed_time/total_time gives percentage of interpolation between cur
2221 // and goal.
2222 void interpolate_matrix(matrix *result, matrix *goal, matrix *start, float elapsed_time, float total_time)
2223 {
2224         vector fvec, rvec;
2225         float   time0, time1;
2226         
2227         if ( !vm_matrix_cmp( goal, start ) ) {
2228                 return;
2229         }       
2230
2231         time0 = elapsed_time / total_time;
2232         time1 = (total_time - elapsed_time) / total_time;
2233
2234         vm_vec_copy_scale(&fvec, &start->v.fvec, time1);
2235         vm_vec_scale_add2(&fvec, &goal->v.fvec, time0);
2236
2237         vm_vec_copy_scale(&rvec, &start->v.rvec, time1);
2238         vm_vec_scale_add2(&rvec, &goal->v.rvec, time0);
2239
2240         vm_vector_2_matrix(result, &fvec, NULL, &rvec);
2241  }
2242
2243 // calculate how far the camera should have moved
2244 float brief_camera_get_dist_moved(float elapsed_time)
2245 {
2246         float time, dist_moved=0.0f;
2247         
2248         // first half of movement
2249         if ( elapsed_time < Total_move_time/2.0f ) {
2250                 dist_moved=0.5f*Cam_accel*elapsed_time*elapsed_time;    // d = 1/2at^2
2251                 return dist_moved;
2252         }
2253
2254         // second half of movement
2255         time=elapsed_time - Total_move_time/2.0f;
2256         dist_moved=(Total_dist/2.0f)+(Peak_speed*time) - 0.5f*Cam_accel*time*time;
2257         return dist_moved;
2258
2259 }
2260
2261 // ------------------------------------------------------------------------------------
2262 // Update the camera position
2263 void brief_camera_move(float frametime, int stage_num)
2264 {
2265         vector  dist_moved;
2266         float           dist;
2267         vector  w_out;
2268         matrix  result;
2269
2270         Elapsed_time += frametime;
2271
2272         if ( Cam_target_reached ) { 
2273 //              Current_cam_pos = Target_cam_pos;
2274 //              Current_lookat_pos = Target_lookat_pos;
2275 //              Current_cam_orient = Target_cam_orient;
2276                 return;
2277         }
2278
2279         // Update orientation
2280         if ( (Elapsed_time < Total_move_time) ) {
2281 //              interpolate_matrix(&Current_cam_orient, &Target_cam_orient, &Start_cam_orient, Elapsed_time, Total_move_time );
2282                 vm_matrix_interpolate(&Target_cam_orient, &Current_cam_orient, &W_init, frametime, &result, &w_out, &Vel_limit, &Acc_limit);
2283                 Current_cam_orient = result;
2284                 W_init = w_out;
2285         }
2286
2287         /*
2288         // interpolate lookat position
2289         if ( vm_vec_cmp( &Current_lookat_pos, &Target_lookat_pos ) ) {
2290                 vm_vec_copy_scale(&dist_moved, &Lookat_vel, Elapsed_time);
2291                 vm_vec_add(&Current_lookat_pos, &Start_lookat_pos, &dist_moved);
2292
2293                 if ( camera_pos_past_target(&Start_lookat_pos, &Current_lookat_pos, &Target_lookat_pos) ) {
2294                         Current_lookat_pos = Target_lookat_pos;
2295                 }
2296         }
2297
2298         cur_dist = Start_dist + Dist_change_rate * Elapsed_time;
2299         vm_vec_copy_scale(&dist_moved, &Current_cam_orient.v.fvec, -cur_dist);
2300         vm_vec_add(&Current_cam_pos, &Current_lookat_pos, &dist_moved);
2301         */
2302
2303         // use absolute pos to update position
2304         if ( vm_vec_cmp( &Current_cam_pos, &Target_cam_pos ) ) {
2305                 dist = brief_camera_get_dist_moved(Elapsed_time);
2306                 if ( dist < Last_dist ) {
2307                         Cam_movement_done=1;
2308                         Last_dist=0.0f;
2309                 }
2310                 Last_dist=dist;
2311
2312                 if ( Cam_movement_done == 0 ) {
2313                         vm_vec_copy_scale(&dist_moved, &Cam_vel, dist);
2314                         vm_vec_add(&Current_cam_pos, &Start_cam_pos, &dist_moved);
2315                 } else {
2316                         Current_cam_pos=Target_cam_pos;
2317                 }
2318         }
2319         else {
2320                 Cam_movement_done=1;
2321                 Current_cam_pos=Target_cam_pos;
2322         }
2323
2324         if ( Cam_movement_done && (Elapsed_time >= Total_move_time) ) {
2325                 Cam_target_reached=1;
2326         }
2327 }
2328
2329 //      Project the viewer's position onto the grid plane.  If more than threshold distance
2330 //      from grid center, move grid center.
2331 void brief_maybe_create_new_grid(grid* gridp, vector *pos, matrix *orient, int force)
2332 {
2333         int roundoff;
2334         plane   tplane;
2335         vector  gpos, tmp, c;
2336         float   dist_to_plane;
2337         float   square_size, ux, uy, uz;
2338
2339         ux = tplane.A = gridp->gmatrix.v.uvec.xyz.x;
2340         uy = tplane.B = gridp->gmatrix.v.uvec.xyz.y;
2341         uz = tplane.C = gridp->gmatrix.v.uvec.xyz.z;
2342         tplane.D = gridp->planeD;
2343
2344         compute_point_on_plane(&c, &tplane, pos);
2345         dist_to_plane = fl_abs(vm_dist_to_plane(pos, &gridp->gmatrix.v.uvec, &c));
2346         square_size = 1.0f;
2347
2348         while (dist_to_plane >= 25.0f)
2349         {
2350                 square_size *= 10.0f;
2351                 dist_to_plane /= 10.0f;
2352         }
2353         
2354         if (fvi_ray_plane(&gpos, &gridp->center, &gridp->gmatrix.v.uvec, pos, &orient->v.fvec, 0.0f)<0.0f)      {
2355                 vector p;
2356                 vm_vec_scale_add(&p,pos,&orient->v.fvec, 100.0f );
2357                 compute_point_on_plane(&gpos, &tplane, &p );
2358         }
2359
2360         if (vm_vec_dist(&gpos, &c) > 50.0f * square_size)
2361         {
2362                 vm_vec_sub(&tmp, &gpos, &c);
2363                 vm_vec_normalize(&tmp);
2364                 vm_vec_scale_add(&gpos, &c, &tmp, 50.0f * square_size);
2365         }
2366
2367         roundoff = (int) square_size * 10;
2368         if (!ux)
2369                 gpos.xyz.x = fl_roundoff(gpos.xyz.x, roundoff);
2370         if (!uy)
2371                 gpos.xyz.y = fl_roundoff(gpos.xyz.y, roundoff);
2372         if (!uz)
2373                 gpos.xyz.z = fl_roundoff(gpos.xyz.z, roundoff);
2374
2375         if ((square_size != gridp->square_size) ||
2376                 (gpos.xyz.x != gridp->center.xyz.x) ||
2377                 (gpos.xyz.y != gridp->center.xyz.y) ||
2378                 (gpos.xyz.z != gridp->center.xyz.z) || force)
2379         {
2380                 gridp->square_size = square_size;
2381                 gridp->center = gpos;
2382                 brief_modify_grid(gridp);
2383         }
2384 }
2385
2386 //      Create a grid
2387 //      *forward is vector pointing forward
2388 //      *right is vector pointing right
2389 //      *center is center point of grid
2390 //      length is length of grid
2391 //      width is width of grid
2392 //      square_size is size of a grid square
2393 //      For example:
2394 //              *forward = (0.0, 0.0, 1.0)
2395 //              *right   = (1.0, 0.0, 0.0)
2396 //              *center = (0.0, 0.0, 0.0)
2397 //              nrows = 10
2398 //              ncols =  50.0
2399 //              square_size = 10.0
2400 //      will generate a grid of squares 10 long by 5 wide.
2401 //      Each grid square will be 10.0 x 10.0 units.
2402 //      The center of the grid will be at the global origin.
2403 //      The grid will be parallel to the xz plane (because the normal is 0,1,0).
2404 //      (In fact, it will be the xz plane because it is centered on the origin.)
2405 //
2406 //      Stuffs grid in *gridp.  If gridp == NULL, mallocs and returns a grid.
2407 grid *brief_create_grid(grid *gridp, vector *forward, vector *right, vector *center, int nrows, int ncols, float square_size)
2408 {
2409         int     i, ncols2, nrows2, d = 1;
2410         vector  dfvec, drvec, cur, cur2, tvec, uvec, save, save2;
2411
2412         SDL_assert(square_size > 0.0);
2413         if (double_fine_gridlines)
2414                 d = 2;
2415
2416         if (gridp == NULL)
2417                 gridp = (grid *) malloc(sizeof(grid));
2418
2419         SDL_assert(gridp);
2420
2421         gridp->center = *center;
2422         gridp->square_size = square_size;
2423
2424         //      Create the plane equation.
2425         SDL_assert(!IS_VEC_NULL(forward));
2426         SDL_assert(!IS_VEC_NULL(right));
2427
2428         vm_vec_copy_normalize(&dfvec, forward);
2429         vm_vec_copy_normalize(&drvec, right);
2430
2431         vm_vec_cross(&uvec, &dfvec, &drvec);
2432         
2433         SDL_assert(!IS_VEC_NULL(&uvec));
2434
2435         gridp->gmatrix.v.uvec = uvec;
2436
2437         gridp->planeD = -(center->xyz.x * uvec.xyz.x + center->xyz.y * uvec.xyz.y + center->xyz.z * uvec.xyz.z);
2438         SDL_assert(!isnan(gridp->planeD));
2439
2440         gridp->gmatrix.v.fvec = dfvec;
2441         gridp->gmatrix.v.rvec = drvec;
2442
2443         vm_vec_scale(&dfvec, square_size);
2444         vm_vec_scale(&drvec, square_size);
2445
2446         vm_vec_scale_add(&cur, center, &dfvec, (float) -nrows * d / 2);
2447         vm_vec_scale_add2(&cur, &drvec, (float) -ncols * d / 2);
2448         vm_vec_scale_add(&cur2, center, &dfvec, (float) -nrows * 5 / 2);
2449         vm_vec_scale_add2(&cur2, &drvec, (float) -ncols * 5 / 2);
2450         save = cur;
2451         save2 = cur2;
2452
2453         gridp->ncols = ncols;
2454         gridp->nrows = nrows;
2455         ncols2 = ncols / 2;
2456         nrows2 = nrows / 2;
2457         SDL_assert(ncols < MAX_GRIDLINE_POINTS && nrows < MAX_GRIDLINE_POINTS);
2458
2459         // Create the points along the edges of the grid, so we can just draw lines
2460         // between them to form the grid.  
2461         for (i=0; i<=ncols*d; i++) {
2462                 gridp->gpoints1[i] = cur;  // small, dark gridline points
2463                 vm_vec_scale_add(&tvec, &cur, &dfvec, (float) nrows * d);
2464                 gridp->gpoints2[i] = tvec;
2465                 vm_vec_add2(&cur, &drvec);
2466         }
2467
2468         for (i=0; i<=ncols2; i++) {
2469                 gridp->gpoints5[i] = cur2;  // large, brighter gridline points
2470                 vm_vec_scale_add(&tvec, &cur2, &dfvec, (float) nrows2 * 10);
2471                 gridp->gpoints6[i] = tvec;
2472                 vm_vec_scale_add2(&cur2, &drvec, 10.0f);
2473         }
2474
2475         cur = save;
2476         cur2 = save2;
2477         for (i=0; i<=nrows*d; i++) {
2478                 gridp->gpoints3[i] = cur;  // small, dark gridline points
2479                 vm_vec_scale_add(&tvec, &cur, &drvec, (float) ncols * d);
2480                 gridp->gpoints4[i] = tvec;
2481                 vm_vec_add2(&cur, &dfvec);
2482         }
2483
2484         for (i=0; i<=nrows2; i++) {
2485                 gridp->gpoints7[i] = cur2;  // large, brighter gridline points
2486                 vm_vec_scale_add(&tvec, &cur2, &drvec, (float) ncols2 * 10);
2487                 gridp->gpoints8[i] = tvec;
2488                 vm_vec_scale_add2(&cur2, &dfvec, 10.0f);
2489         }
2490
2491         return gridp;
2492 }
2493
2494 //      Create a nice grid -- centered at origin, 10x10, 10.0 size squares, in xz plane.
2495 grid *brief_create_default_grid(void)
2496 {
2497         grid    *rgrid;
2498         vector  fvec, rvec, cvec;
2499
2500         rgrid = brief_create_grid(&Global_grid, vm_vec_make(&fvec, 0.0f, 0.0f, 1.0f),
2501                 vm_vec_make(&rvec, 1.0f, 0.0f, 0.0f),
2502                 vm_vec_make(&cvec, 0.0f, -10.0f, 0.0f), 100, 100, 5.0f);
2503
2504         physics_init(&rgrid->physics);
2505         rgrid->physics.flags |= (PF_ACCELERATES | PF_SLIDE_ENABLED);
2506         return rgrid;
2507 }
2508
2509 //      Rotate and project points and draw a line.
2510 void brief_rpd_line(vector *v0, vector *v1)
2511 {
2512         vertex  tv0, tv1;
2513         g3_rotate_vertex(&tv0, v0);
2514         g3_rotate_vertex(&tv1, v1);
2515
2516 /*
2517         g3_project_vertex(&tv0);        
2518         g3_project_vertex(&tv1);
2519
2520         if ( (tv0.flags & PF_OVERFLOW) || (tv1.flags & PF_OVERFLOW) )
2521                 return;
2522 */
2523
2524         gr_set_color_fast(&Color_grey);
2525         g3_draw_line(&tv0, &tv1);
2526 }
2527
2528 //      Renders a grid defined in a grid struct
2529 void brief_render_grid(grid *gridp)
2530 {
2531         int     i, ncols, nrows;
2532
2533         ncols = gridp->ncols;
2534         nrows = gridp->nrows;
2535         if (double_fine_gridlines)
2536         {
2537                 ncols *= 2;
2538                 nrows *= 2;
2539         }
2540
2541         gr_set_color(30,30,30);
2542 //      SET_DARK;
2543
2544         //      Draw the column lines.
2545         for (i=0; i<=ncols; i++)
2546                 brief_rpd_line(&gridp->gpoints1[i], &gridp->gpoints2[i]);
2547
2548         //      Draw the row lines.
2549         for (i=0; i<=nrows; i++)
2550                 brief_rpd_line(&gridp->gpoints3[i], &gridp->gpoints4[i]);
2551
2552         ncols = gridp->ncols / 2;
2553         nrows = gridp->nrows / 2;
2554
2555         // now draw the larger, brighter gridlines that is x10 the scale of smaller one.
2556 //      SET_MEDIUM;
2557 /*
2558         for (i=0; i<=ncols; i++)
2559                 brief_rpd_line(&gridp->gpoints5[i], &gridp->gpoints6[i]);
2560
2561         for (i=0; i<=nrows; i++)
2562                 brief_rpd_line(&gridp->gpoints7[i], &gridp->gpoints8[i]);
2563 */
2564 }
2565
2566 void brief_modify_grid(grid *gridp)
2567 {
2568         brief_create_grid(gridp, &gridp->gmatrix.v.fvec, &gridp->gmatrix.v.rvec, &gridp->center,
2569                 gridp->nrows, gridp->ncols, gridp->square_size);
2570 }
2571
2572 void brief_unload_anims()
2573 {
2574         int idx;
2575         int i;
2576         
2577         for (i=0; i<MAX_BRIEF_ICONS; i++) {
2578                 for(idx=0; idx<MAX_SPECIES_NAMES; idx++){
2579                         hud_anim_release(&Icon_highlight_anims[i][idx]);
2580                         hud_anim_release(&Icon_fade_anims[i][idx]);
2581                 }
2582         }
2583 }
2584
2585 void brief_common_close()
2586 {
2587         brief_close_map();
2588         brief_unload_anims();
2589 }
2590
2591
2592 void brief_restart_text_wipe()
2593 {
2594         Brief_stage_time = 0;
2595         Brief_voice_ended = 0;
2596         Brief_voice_started = 0;
2597         Brief_text_wipe_time_elapsed = 0.0f;
2598 }
2599
2600 // initialize the array of handles to the different voice streams
2601 void brief_voice_init()
2602 {
2603         int i;
2604         for ( i = 0; i < MAX_BRIEF_STAGES; i++ ) {
2605                 Brief_voices[i] = -1;
2606         }
2607 }
2608
2609 void brief_load_voice_file(int voice_num, char *name)
2610 {
2611         int load_attempts = 0;
2612         while(1) {
2613
2614                 if ( load_attempts++ > 5 ) {
2615                         break;
2616                 }
2617
2618                 Brief_voices[voice_num] = audiostream_open( name, ASF_VOICE );
2619                 if ( Brief_voices[voice_num] >= 0 ) {
2620                         break;
2621                 }
2622
2623                 // Don't bother to ask for the CD in multiplayer
2624                 if ( Game_mode & GM_MULTIPLAYER ) {
2625                         break;
2626                 }
2627
2628                 // couldn't load animation, ask user to insert CD (if necessary)
2629                 // if ( Brief_voice_ask_for_cd ) {
2630                         // if ( game_do_cd_check() == 0 ) {
2631                                 // Brief_voice_ask_for_cd = 0;
2632                                 // break;
2633                         // }
2634                 // }
2635         }
2636 }
2637
2638 // open and pre-load the stream buffers for the different voice streams
2639 void brief_voice_load_all()
2640 {
2641         int                     i;
2642         brief_stage     *bs;
2643
2644         // Brief_voice_ask_for_cd = 1;
2645
2646         SDL_assert( Briefing != NULL );
2647         for ( i = 0; i < Briefing->num_stages; i++ ) {
2648                 bs = &Briefing->stages[i];
2649                 if ( SDL_strncasecmp(bs->voice, NOX("none"), 4) ) {
2650                         brief_load_voice_file(i, bs->voice);
2651 //                      Brief_voices[i] = audiostream_open( bs->voice, ASF_VOICE );
2652                 }
2653         }
2654 }
2655
2656 // close all the briefing voice streams
2657 void brief_voice_unload_all()
2658 {
2659         int i;
2660
2661         for ( i = 0; i < MAX_BRIEF_STAGES; i++ ) {
2662                 if ( Brief_voices[i] != -1 ) {
2663                         audiostream_close_file(Brief_voices[i], 0);
2664                         Brief_voices[i] = -1;
2665                 }
2666         }
2667 }
2668
2669 // start playback of the voice for a particular briefing stage
2670 void brief_voice_play(int stage_num)
2671 {
2672         if ( Brief_voices[stage_num] == -1 )
2673                 return; // voice file doesn't exist
2674
2675         if ( !Briefing_voice_enabled ) {
2676                 return;
2677         }
2678
2679         if ( audiostream_is_playing( Brief_voices[stage_num]) )
2680                 return;
2681
2682         audiostream_play(Brief_voices[stage_num], Master_voice_volume, 0);
2683         Brief_voice_started = 1;
2684 }
2685
2686 // stop playback of the voice for a particular briefing stage
2687 void brief_voice_stop(int stage_num)
2688 {
2689         if ( Brief_voices[stage_num] == -1 )
2690                 return;
2691
2692         audiostream_stop(Brief_voices[stage_num]);      // stream is automatically rewound
2693 }
2694
2695 // pause playback of the voice for a particular briefing stage, to resume just
2696 // call brief_voice_unpause() again
2697 void brief_voice_pause(int stage_num)
2698 {
2699         if ( Brief_voices[stage_num] == -1 )
2700                 return;
2701
2702         audiostream_pause(Brief_voices[stage_num]);
2703 }
2704
2705 void brief_voice_unpause(int stage_num)
2706 {
2707         if ( Brief_voices[stage_num] == -1 )
2708                 return;
2709
2710         audiostream_unpause(Brief_voices[stage_num]);
2711 }
2712
2713 void brief_reset_last_new_stage()
2714 {
2715         Last_new_stage = -1;
2716 }
2717
2718 // get the dimensions for a briefing icon
2719 void brief_common_get_icon_dimensions(int *w, int *h, int type, int ship_class)
2720 {
2721         SDL_assert(type >= 0 && type < MAX_BRIEF_ICONS);
2722
2723         // in case anything goes wrong
2724         *w=0;
2725         *h=0;
2726
2727         int species = ship_get_species_by_type(ship_class);
2728
2729 #ifdef MAKE_FS1
2730         species = 0;
2731 #endif
2732
2733         if(species < 0){
2734                 return;
2735         }
2736
2737         if ( Icon_bitmaps[type][species].first_frame >= 0 ) {
2738                 bm_get_info( Icon_bitmaps[type][species].first_frame, w, h, NULL);
2739         } else {
2740                 *w=0;
2741                 *h=0;
2742         }
2743 }
2744
2745 void cmd_brief_reset()
2746 {
2747         int i, j;
2748         static int inited = 0;
2749
2750         if (inited) {
2751                 for (i=0; i<MAX_TEAMS; i++) {
2752                         for (j=0; j<Cmd_briefs[i].num_stages; j++) {
2753                                 if (Cmd_briefs[i].stage[j].text)
2754                                         free(Cmd_briefs[i].stage[j].text);
2755                         }
2756                 }
2757         }
2758
2759         inited = 1;
2760         for (i=0; i<MAX_TEAMS; i++)
2761                 Cmd_briefs[i].num_stages = 0;
2762 }
2763
2764 #define STAGE_ADVANCE_DELAY     1000            // time in ms to wait after voice stops before advancing stage
2765
2766 // should briefing advance to the next stage?
2767 int brief_time_to_advance(int stage_num, float frametime)
2768 {
2769         int voice_active, advance = 0;
2770         brief_icon *closeup_icon;
2771
2772         closeup_icon = brief_get_closeup_icon();
2773
2774         if ( closeup_icon ) {
2775                 return 0;
2776         }
2777
2778         if ( !Player->auto_advance ) {
2779                 return 0;
2780         }
2781
2782         Brief_stage_time += fl2i(frametime*1000 + 0.5f);
2783
2784         if ( (Brief_voices[stage_num] >= 0) && Briefing_voice_enabled ) {
2785                 voice_active = 1;
2786         } else {
2787                 voice_active = 0;
2788         }
2789
2790         if ( voice_active && (Brief_voice_ended == 0) && Brief_voice_started) {
2791                 if ( !audiostream_is_playing( Brief_voices[stage_num]) ) {
2792                         Brief_voice_ended = 1;
2793                         Brief_stage_time = 0;
2794                 }
2795         }
2796         
2797         if ( Brief_voice_ended ) {
2798                 if ( Brief_stage_time > STAGE_ADVANCE_DELAY ) {
2799                         advance = 1;
2800                 }
2801         }
2802
2803         if ( !voice_active && (Brief_textdraw_finished > 0) ) {
2804                 if ( Brief_stage_time > max(5000, Num_brief_text_lines[0] * 3500) ) {
2805                         advance = 1;
2806                 }
2807         }
2808
2809         return advance;
2810 }