]> icculus.org git repositories - taylor/freespace2.git/blob - src/mission/missiontraining.cpp
added copyright header
[taylor/freespace2.git] / src / mission / missiontraining.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/MissionTraining.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Special code for training missions.  Stuff like displaying training messages in
16  * the special training window, listing the training objectives, etc.
17  *
18  * $Log$
19  * Revision 1.3  2002/06/09 04:41:22  relnev
20  * added copyright header
21  *
22  * Revision 1.2  2002/05/07 03:16:46  theoddone33
23  * The Great Newline Fix
24  *
25  * Revision 1.1.1.1  2002/05/03 03:28:10  root
26  * Initial import.
27  *
28  * 
29  * 10    8/27/99 2:20p Andsager
30  * Sort directives by born on date
31  * 
32  * 9     8/22/99 7:57p Alanl
33  * fix bug that was scaling down mission training voice twice
34  * 
35  * 8     8/02/99 7:29p Jefff
36  * moved instructor text in hi res
37  * 
38  * 7     8/02/99 3:49p Jefff
39  * moved directives window down in 1024
40  * 
41  * 6     7/10/99 1:44p Andsager
42  * Modified directives listing so that current and recently
43  * satisfied/failed directives stay on screen.
44  * 
45  * 5     6/10/99 3:43p Dave
46  * Do a better job of syncing text colors to HUD gauges.
47  * 
48  * 4     2/17/99 2:10p Dave
49  * First full run of squad war. All freespace and tracker side stuff
50  * works.
51  * 
52  * 3     10/13/98 9:28a Dave
53  * Started neatening up freespace.h. Many variables renamed and
54  * reorganized. Added AlphaColors.[h,cpp]
55  * 
56  * 2     10/07/98 10:53a Dave
57  * Initial checkin.
58  * 
59  * 1     10/07/98 10:49a Dave
60  * 
61  * 58    8/25/98 1:48p Dave
62  * First rev of EMP effect. Player side stuff basically done. Next comes
63  * AI code.
64  * 
65  * 57    6/13/98 5:19p Hoffoss
66  * externalized control config texts.
67  * 
68  * 56    6/09/98 10:31a Hoffoss
69  * Created index numbers for all xstr() references.  Any new xstr() stuff
70  * added from here on out should be added to the end if the list.  The
71  * current list count can be found in FreeSpace.cpp (search for
72  * XSTR_SIZE).
73  * 
74  * 55    6/01/98 11:43a John
75  * JAS & MK:  Classified all strings for localization.
76  * 
77  * 54    5/23/98 4:14p John
78  * Added code to preload textures to video card for AGP.   Added in code
79  * to page in some bitmaps that weren't getting paged in at level start.
80  * 
81  * 53    5/16/98 9:13p Allender
82  * don't display the directive display in multiplayer
83  * 
84  * 52    5/07/98 11:59p Mike
85  * Don't show training popup for unbound key unless in training mission.
86  * (Happens in demo01)
87  * 
88  * 51    4/30/98 12:01a Lawrance
89  * using nprintf((Warning,...)) when audiostream can't be opened
90  * 
91  * 50    4/27/98 11:20a Hoffoss
92  * Doubled TRAINING_MSG_QUE_MAX.
93  * 
94  * 49    4/22/98 3:21p Hoffoss
95  * Fixed slight training message problems with training_failure state.
96  * 
97  * 48    4/16/98 4:33p Hoffoss
98  * Added support for detecting instructor terminating training due to
99  * player shooting at him.
100  * 
101  * 47    4/15/98 10:16p Mike
102  * Training mission #5.
103  * Fix application of subsystem damage.
104  * 
105  * 46    4/15/98 5:25p Lawrance
106  * only show damage gauge when training msg not visible
107  * 
108  * 45    4/03/98 2:52p Allender
109  * use Missiontime insteadof timestamp() for objective display
110  * 
111  * 44    3/31/98 5:23p Hoffoss
112  * Fixed bug with training message token handling.
113  * 
114  * 43    3/18/98 6:22p John
115  * Made directives display wiggle when afterburner is on
116  * 
117  * 42    3/11/98 2:46p Hoffoss
118  * Shortened the width of the training message window so it doesn't
119  * overlap other hud objects.
120  * 
121  * 41    2/23/98 8:31a John
122  * More string externalization
123  * 
124  * 40    2/22/98 4:30p John
125  * More string externalization classification
126  * 
127  * 39    2/04/98 4:32p Allender
128  * support for multiple briefings and debriefings.  Changes to mission
129  * type (now a bitfield).  Bitfield defs for multiplayer modes
130  * 
131  * 38    1/30/98 4:24p Hoffoss
132  * Added a 3 second delay for directives before they get displayed.
133  * 
134  * 37    1/26/98 6:27p Lawrance
135  * Change popups to use '&' meta-char for underlined hotkey.
136  * 
137  * 36    1/22/98 11:46p Hoffoss
138  * Fixed linking problem with Fred.
139  * 
140  * 35    1/22/98 11:00p Mike
141  * Fix Fred link error by commenting out line, email Hoffoss with info.
142  * 
143  * 34    1/22/98 4:15p Hoffoss
144  * Added code to allow popup to tell player he needs to bind a key for the
145  * training mission.
146  * 
147  * 33    1/20/98 2:26p Hoffoss
148  * Removed references to timestamp_ticker, used timestamp() instead.
149  * 
150  * 32    1/19/98 9:37p Allender
151  * Great Compiler Warning Purge of Jan, 1998.  Used pragma's in a couple
152  * of places since I was unsure of what to do with code.
153  * 
154  * 31    1/15/98 6:00p Hoffoss
155  * Added option to quit menu (in game) to restart the mission.  Doesn't
156  * seem to quite work, though.  Checking code in so someone else can look
157  * into it.
158  * 
159  * 30    1/09/98 10:47a Lawrance
160  * Only clear auto-target, linking etc. when actually starting a training
161  * mission.
162  * 
163  * 29    1/08/98 4:07p Hoffoss
164  * Made objective key text appear in Green.
165  * 
166  * 28    1/06/98 2:07p Lawrance
167  * strip leading whitespace on training msg lines
168  * 
169  * 27    1/05/98 11:05p Lawrance
170  * Clear primary/secondary linking if playing a training mission.
171  * 
172  * 26    1/05/98 10:03p Lawrance
173  * Fix HUD gauge flag that didn't get changed.
174  * 
175  * 25    1/05/98 4:04p Hoffoss
176  * Changed training-msg sexp operator to allow it to control the length of
177  * time a message is displayed for.
178  * 
179  * 24    1/05/98 11:16a Mike
180  * Clear auto-targeting and auto-speed-matching in a training mission.
181  * 
182  * 23    1/02/98 9:10p Lawrance
183  * Big changes to how colors get set on the HUD.
184  * 
185  * 22    12/22/97 6:07p Hoffoss
186  * Made directives flash when completed, fixed but with is-destroyed
187  * operator.
188  * 
189  * 21    12/19/97 12:43p Hoffoss
190  * Changed code to allow counts in directives.
191  * 
192  * 20    12/18/97 11:53a Lawrance
193  * fix bug with displaying hud directives
194  * 
195  * 19    12/16/97 9:13p Lawrance
196  * Integrate new gauges into HUD config.
197  * 
198  * 18    12/15/97 5:26p Allender
199  * temporary code to display for 5 second completion status of objectives
200  * 
201  * 17    11/20/97 1:10a Lawrance
202  * add support for voice volumes
203  * 
204  * 16    11/17/97 6:38p Lawrance
205  * move directives window down
206  * 
207  * 15    11/14/97 1:24p Lawrance
208  * draw 'directives' as the title
209  * 
210  * 14    11/14/97 1:21p Lawrance
211  * split directives lines if they get too long
212  * 
213  * 13    11/12/97 6:00p Hoffoss
214  * Added training messages to hud scrollback log.
215  * 
216  * 12    11/11/97 10:27p Lawrance
217  * implement new HUD directives gauge
218  * 
219  * 11    11/11/97 12:59a Lawrance
220  * move objective window up slightly
221  * 
222  * 10    10/21/97 7:18p Hoffoss
223  * Overhauled the key/joystick control structure and usage throughout the
224  * entire FreeSpace code.  The whole system is very different now.
225  * 
226  * 9     10/21/97 3:35p Dan
227  * ALAN: if wave file doesn't exist, don't play it
228  * 
229  * 8     10/17/97 6:39p Hoffoss
230  * Added delayability to key-pressed operator and training-msg operator.
231  * 
232  * 7     10/17/97 10:23a Hoffoss
233  * Added more comments.
234  * 
235  * 6     10/14/97 11:35p Lawrance
236  * change snd_load parameters
237  * 
238  * 5     10/13/97 4:33p Hoffoss
239  * Made training messages go away after time.
240  * 
241  * 4     10/13/97 3:24p Hoffoss
242  * Made it so training message text can be arbitrarily bolded.
243  * 
244  * 3     10/10/97 6:15p Hoffoss
245  * Implemented a training objective list display.
246  * 
247  * 2     10/09/97 4:44p Hoffoss
248  * Dimmed training window glass and made it less transparent, added flags
249  * to events, set he stage for detecting current events.
250  * 
251  * 1     10/09/97 2:41p Hoffoss
252  *
253  * $NoKeywords: $
254  */
255
256 #include "pstypes.h"
257 #include "parselo.h"
258 #include "cfile.h"
259 #include "sound.h"
260 #include "audiostr.h"
261 #include "missionmessage.h"
262 #include "missiongoals.h"
263 #include "missionparse.h"
264 #include "timer.h"
265 #include "controlsconfig.h"
266 #include "sexp.h"
267 #include "2d.h"
268 #include "hudtarget.h"
269 #include "freespace.h"
270 #include "hud.h"
271 #include "bmpman.h"
272 #include "hudconfig.h"
273 #include        "player.h"
274 #include "popup.h"
275 #include "gamesequence.h"
276 #include "emp.h"
277 #include "alphacolors.h"
278 #include "multi.h"
279
280 #define MAX_TRAINING_MSG_LINES          10
281 //#define TRAINING_MSG_WINDOW_X                 174
282 //#define TRAINING_MSG_WINDOW_Y                 40
283 #define TRAINING_MSG_WINDOW_WIDTH       266
284 #define TRAINING_LINE_WIDTH                     250  // width in pixels of actual text
285 #define TRAINING_TIMING                                 150  // milliseconds per character to display messages
286 #define TRAINING_TIMING_BASE                    1000  // Minimum milliseconds to display any message
287 //#define TRAINING_OBJ_WND_X                            0               // offset of left edge of window
288 //#define TRAINING_OBJ_WND_Y                            180     // offset of top edge of window
289 //#define TRAINING_OBJ_WND_Y                            187     // offset of top edge of window
290 #define TRAINING_OBJ_WND_WIDTH          170     // number of pixels wide window is.
291 #define TRAINING_OBJ_LINE_WIDTH         150     // number of pixels wide text can be
292 #define TRAINING_OBJ_LINES                              50              // number of lines to track in objective list
293 #define TRAINING_OBJ_DISPLAY_LINES      5               // only display this many lines on screen max
294 #define MAX_TRAINING_MSG_MODS                   20
295 #define TRAINING_MSG_QUE_MAX                    40
296
297 #define TRAINING_OBJ_STATUS_UNKNOWN             (1 << 28)       // directive status is unknown
298 #define TRAINING_OBJ_STATUS_KNOWN               (1 << 29)       // directive status is known (satisfied or failed)
299 #define TRAINING_OBJ_LINES_KEY                  (1 << 30)       // flag indicating line describes the key associated with objective
300 #define TRAINING_OBJ_LINES_EVENT_STATUS_MASK (TRAINING_OBJ_STATUS_KNOWN | TRAINING_OBJ_STATUS_UNKNOWN)
301
302 #define TRAINING_OBJ_LINES_MASK(n)      (Training_obj_lines[n] & 0xffff)
303
304 #define TMMOD_NORMAL    0
305 #define TMMOD_BOLD      1
306
307 typedef struct {
308         char *pos;
309         int mode;  // what function to perform at given position (TMMOD_*)
310 } training_msg_mods;  // training message modifiers
311
312 typedef struct {
313         int num;
314         int timestamp;
315         int length;
316 } training_msg_que;
317
318 char Training_buf[8192], Training_text[8192];
319 char *Training_lines[MAX_TRAINING_MSG_LINES];  // Training message split into lines
320 char Training_voice_filename[NAME_LENGTH];
321 int Training_msg_timestamp;
322 int Training_line_sizes[MAX_TRAINING_MSG_LINES];
323 int Training_msg_method = 1;
324 int Training_num_lines = 0;
325 int Training_voice = -1;
326 int Training_voice_type;
327 int Training_voice_handle;
328 int Training_flag = 0;
329 int Training_failure = 0;
330 int Training_msg_que_count = 0;
331 int Training_bind_warning = -1;  // Missiontime at which we last gave warning
332 int Training_msg_visible;
333 training_msg_que Training_msg_que[TRAINING_MSG_QUE_MAX];
334
335 // coordinates for training messages
336 int Training_msg_window_coords[GR_NUM_RESOLUTIONS][2] = {
337         { 174, 40 },
338         { 379, 125 }
339 };
340
341 // coordinates for "directives" window on hud
342 int Training_obj_window_coords[GR_NUM_RESOLUTIONS][2] = {
343         { 0, 187 },
344         { 0, 287 }
345 };
346
347
348 // training objectives global vars.
349 int Training_obj_num_lines;
350 int Training_obj_lines[TRAINING_OBJ_LINES];
351 training_msg_mods Training_msg_mods[MAX_TRAINING_MSG_MODS];
352
353 // local module prototypes
354 void training_process_msg(char *msg);
355 void message_translate_tokens(char *buf, char *text);
356
357
358 #define NUM_DIRECTIVE_GAUGES                    3
359 static hud_frames Directive_gauge[NUM_DIRECTIVE_GAUGES];
360 static int Directive_frames_loaded = 0;
361
362 static char *Directive_fnames[3] = 
363 {
364 //XSTR:OFF
365         "directives1",
366         "directives2",
367         "directives3"
368 //XSTR:ON
369 };
370
371 #define DIRECTIVE_H                                             9
372 #define DIRECTIVE_X                                             5
373 #define NUM_DIRECTIVE_COORDS                    3
374 #define DIRECTIVE_COORDS_TOP                    0
375 #define DIRECTIVE_COORDS_MIDDLE         1
376 #define DIRECTIVE_COORDS_TITLE          2
377 static int Directive_coords[GR_NUM_RESOLUTIONS][NUM_DIRECTIVE_COORDS][2] =
378 {
379         {
380                 // GR_640
381                 {5,178},
382                 {5,190},
383                 {7,180}
384         },
385         {
386                 // GR_1024
387                 {5,278},
388                 {5,290},
389                 {7,280}
390         }
391 };
392
393 // displays (renders) the training objectives list
394 void training_obj_display()
395 {
396         char buf[256], *second_line;
397         int i, t, x, y, z, height, end, offset, bx, by, y_count;
398         color *c;
399
400         if (!Training_obj_num_lines){
401                 return;
402         }
403
404         if ( !hud_gauge_active(HUD_DIRECTIVES_VIEW) ) {
405                 // Always draw the directives display if this is a training mission
406                 if ( !(The_mission.game_type & MISSION_TYPE_TRAINING) ) {
407                         return;
408                 }
409         }
410
411         // don't ever display directives display in multiplayer missions
412         // if ( Game_mode & GM_MULTIPLAYER ){
413         //      return;
414         // }
415
416         height = gr_get_font_height();
417
418         offset = 0;
419         end = Training_obj_num_lines;
420         if (end > TRAINING_OBJ_DISPLAY_LINES) {
421                 end = TRAINING_OBJ_DISPLAY_LINES;
422                 offset = Training_obj_num_lines - end;
423         }
424
425         // draw top of objective display
426         // hud_set_default_color();
427         hud_set_gauge_color(HUD_DIRECTIVES_VIEW);
428
429         GR_AABITMAP(Directive_gauge[0].first_frame, Directive_coords[gr_screen.res][DIRECTIVE_COORDS_TOP][0]+fl2i(HUD_offset_x), Directive_coords[gr_screen.res][DIRECTIVE_COORDS_TOP][1]+fl2i(HUD_offset_y));
430         // gr_set_bitmap(Directive_gauge[0].first_frame);
431         // gr_aabitmap(Directive_coords[DIRECTIVE_COORDS_TOP][0]+fl2i(HUD_offset_x), Directive_coords[DIRECTIVE_COORDS_TOP][1]+fl2i(HUD_offset_y));
432
433         // print out title
434         emp_hud_printf(Directive_coords[gr_screen.res][DIRECTIVE_COORDS_TITLE][0]+fl2i(HUD_offset_x), Directive_coords[gr_screen.res][DIRECTIVE_COORDS_TITLE][1]+fl2i(HUD_offset_y), EG_OBJ_TITLE, XSTR( "directives", 422));
435         // gr_printf(Directive_coords[DIRECTIVE_COORDS_TITLE][0]+fl2i(HUD_offset_x), Directive_coords[DIRECTIVE_COORDS_TITLE][1]+fl2i(HUD_offset_y), XSTR( "directives", 422));
436         
437         bx = DIRECTIVE_X+fl2i(HUD_offset_x);
438         by = Directive_coords[gr_screen.res][DIRECTIVE_COORDS_MIDDLE][1]+fl2i(HUD_offset_y);
439
440         y_count = 0;
441         for (i=0; i<end; i++) {
442                 x = DIRECTIVE_X + 3 + fl2i(HUD_offset_x);
443                 y = Training_obj_window_coords[gr_screen.res][1] + fl2i(HUD_offset_y) + y_count * height + height / 2 + 1;
444                 z = TRAINING_OBJ_LINES_MASK(i + offset);
445
446                 c = &Color_normal;
447                 if (Training_obj_lines[i + offset] & TRAINING_OBJ_LINES_KEY) {
448                         message_translate_tokens(buf, Mission_events[z].objective_key_text);  // remap keys
449 //                      gr_set_color_fast(&Color_normal);
450                         c = &Color_bright_green;
451                 } else {
452                         strcpy(buf, Mission_events[z].objective_text);
453                         if (Mission_events[z].count){
454                                 sprintf(buf + strlen(buf), NOX(" [%d]"), Mission_events[z].count);
455                         }
456
457                         // if this is a multiplayer tvt game, and this is event is not for my team, don't display it
458                         if((Game_mode & GM_MULTIPLAYER) && (Netgame.type_flags & NG_TYPE_TEAM) && (Net_player != NULL)){
459                                 if((Mission_events[z].team != -1) && (Net_player->p_info.team != Mission_events[z].team)){
460                                         continue;
461                                 }
462                         }
463
464                         switch (mission_get_event_status(z)) {
465                         case EVENT_CURRENT:
466 //                              gr_set_color_fast(&Color_bright_white);
467                                 c = &Color_bright_white;
468                                 break;
469
470                         case EVENT_FAILED:
471 //                              gr_set_color_fast(&Color_bright_red);
472                                 c = &Color_bright_red;
473                                 break;
474
475                         case EVENT_SATISFIED:
476 //                              gr_set_color_fast(&Color_bright_blue);
477                                 t = Mission_events[z].satisfied_time;
478                                 if (t + i2f(2) > Missiontime) {
479                                         if (Missiontime % fl2f(.4f) < fl2f(.2f)){
480                                                 c = &Color_bright_blue;
481                                         } else {
482                                                 c = &Color_bright_white;
483                                         }
484                                 } else {
485                                         c = &Color_bright_blue;
486                                 }
487                                 break;
488                         }
489                 }
490
491                 // maybe split the directives line
492                 second_line = split_str_once(buf, 167);
493
494                 // blit the background frames
495                 // hud_set_default_color();
496                 hud_set_gauge_color(HUD_DIRECTIVES_VIEW);
497
498                 GR_AABITMAP(Directive_gauge[1].first_frame, bx, by);
499                 // gr_set_bitmap(Directive_gauge[1].first_frame);
500                 // gr_aabitmap(bx, by);
501                 
502                 by += DIRECTIVE_H;
503
504                 if ( second_line ) {
505                         GR_AABITMAP(Directive_gauge[1].first_frame, bx, by);
506                         // gr_set_bitmap(Directive_gauge[1].first_frame);
507                         // gr_aabitmap(bx, by);
508                         
509                         by += DIRECTIVE_H;
510                 }
511
512                 // blit the text
513                 gr_set_color_fast(c);
514                 
515                 emp_hud_string(x, y, EG_OBJ1 + i, buf);
516                 // gr_printf(x, y, buf);
517                 
518                 y_count++;
519
520                 if ( second_line ) {
521                         y = Training_obj_window_coords[gr_screen.res][1] + fl2i(HUD_offset_y) + y_count * height + height / 2 + 1;
522                         
523                         emp_hud_string(x+12, y, EG_OBJ1 + i + 1, second_line);
524                         // gr_printf(x+12, y, second_line);
525                         
526                         y_count++;
527                 }
528         }
529
530         // draw the bottom of objective display
531         // hud_set_default_color();
532         hud_set_gauge_color(HUD_DIRECTIVES_VIEW);
533
534         GR_AABITMAP(Directive_gauge[2].first_frame, bx, by);
535         // gr_set_bitmap(Directive_gauge[2].first_frame);
536         // gr_aabitmap(bx, by);
537 }
538
539 // mission initializations (called once before a new mission is started)
540 void training_mission_init()
541 {
542         int i;
543
544         Assert(!Training_num_lines);
545         Training_obj_num_lines = 0;
546         Training_msg_que_count = 0;
547         Training_failure = 0;
548         for (i=0; i<TRAINING_OBJ_LINES; i++)
549                 Training_obj_lines[i] = -1;
550
551         if ( !Directive_frames_loaded ) {
552                 for ( i = 0; i < NUM_DIRECTIVE_GAUGES; i++ ) {
553                         Directive_gauge[i].first_frame = bm_load_animation(Directive_fnames[i], &Directive_gauge[i].num_frames);
554                         if ( Directive_gauge[i].first_frame < 0 ) {
555                                 Warning(LOCATION,"Cannot load hud ani: %s\n", Directive_fnames[i]);
556                         }
557                 }
558
559                 Directive_frames_loaded = 1;
560         }
561
562         // only clear player flags if this is actually a training mission
563         if ( The_mission.game_type & MISSION_TYPE_TRAINING ) {
564                 Player->flags &= ~(PLAYER_FLAGS_MATCH_TARGET | PLAYER_FLAGS_MSG_MODE | PLAYER_FLAGS_AUTO_TARGETING | PLAYER_FLAGS_AUTO_MATCH_SPEED | PLAYER_FLAGS_LINK_PRIMARY | PLAYER_FLAGS_LINK_SECONDARY );
565         }
566 }
567
568 void training_mission_page_in()
569 {
570         int i;
571         for ( i = 0; i < NUM_DIRECTIVE_GAUGES; i++ ) {
572                 bm_page_in_aabitmap( Directive_gauge[i].first_frame, Directive_gauge[i].num_frames );
573         }
574 }
575
576
577 int comp_training_lines_by_born_on_date(const void *m1, const void *m2)
578 {
579         int *e1, *e2;
580         e1 = (int*) m1;
581         e2 = (int*) m2;
582         
583         Assert(Mission_events[*e1 & 0xffff].born_on_date != 0);
584         Assert(Mission_events[*e2 & 0xffff].born_on_date != 0);
585
586         return (Mission_events[*e1 & 0xffff].born_on_date - Mission_events[*e2 & 0xffff].born_on_date);
587 }
588
589
590 // now sort list of events
591 // sort on EVENT_CURRENT and born on date, for other events (EVENT_SATISFIED, EVENT_FAILED) sort on born on date
592 #define MIN_SATISFIED_TIME              5
593 #define MIN_FAILED_TIME                 7
594 void sort_training_objectives()
595 {
596         int i, event_status, offset;
597
598         // start by sorting on born on date
599         qsort(Training_obj_lines, Training_obj_num_lines, sizeof(int), comp_training_lines_by_born_on_date);
600
601         // get the index of the first directive that will be displayed
602         // if less than 0, display all lines
603         offset = Training_obj_num_lines - TRAINING_OBJ_DISPLAY_LINES;
604
605         if (offset <= 0) {
606                 return;
607         }
608
609         // go through lines 0 to offset-1 and check if there are any CURRENT or RECENTLY_KNOWN events that should be shown
610         int num_offset_events = 0;
611         for (i=0; i<offset; i++) {
612                 event_status = mission_get_event_status(TRAINING_OBJ_LINES_MASK(i));
613
614                 if (event_status == EVENT_CURRENT)  {
615                         Training_obj_lines[i] |= TRAINING_OBJ_STATUS_UNKNOWN;
616                         num_offset_events++;
617                 } else if (event_status ==      EVENT_SATISFIED) {
618                         if (f2i(Missiontime - Mission_events[TRAINING_OBJ_LINES_MASK(i)].satisfied_time) < MIN_SATISFIED_TIME) {
619                                 Training_obj_lines[i] |= TRAINING_OBJ_STATUS_UNKNOWN;
620                                 num_offset_events++;
621                         } else {
622                                 Training_obj_lines[i] |= TRAINING_OBJ_STATUS_KNOWN;
623                         }
624                 } else if (event_status ==      EVENT_FAILED) {
625                         if (f2i(Missiontime - Mission_events[TRAINING_OBJ_LINES_MASK(i)].satisfied_time) < MIN_FAILED_TIME) {
626                                 Training_obj_lines[i] |= TRAINING_OBJ_STATUS_UNKNOWN;
627                                 num_offset_events++;
628                         } else {
629                                 Training_obj_lines[i] |= TRAINING_OBJ_STATUS_KNOWN;
630                         }
631                 }
632         }
633
634         // if there are no directives which should be moved, we're done
635         if (num_offset_events == 0) {
636                 return;
637         }
638
639         // go through lines offset to Training_obj_num_lines to check which should be shown, since some will need to be bumped
640         for (i=offset; i<Training_obj_num_lines; i++) {
641                 event_status = mission_get_event_status(TRAINING_OBJ_LINES_MASK(i));
642
643                 if (event_status == EVENT_CURRENT)  {
644                         Training_obj_lines[i] |= TRAINING_OBJ_STATUS_UNKNOWN;
645                 } else if (event_status ==      EVENT_SATISFIED) {
646                         if (f2i(Missiontime - Mission_events[TRAINING_OBJ_LINES_MASK(i)].satisfied_time) < MIN_SATISFIED_TIME) {
647                                 Training_obj_lines[i] |= TRAINING_OBJ_STATUS_UNKNOWN;
648                         } else {
649                                 Training_obj_lines[i] |= TRAINING_OBJ_STATUS_KNOWN;
650                         }
651                 } else if (event_status ==      EVENT_FAILED) {
652                         if (f2i(Missiontime - Mission_events[TRAINING_OBJ_LINES_MASK(i)].satisfied_time) < MIN_FAILED_TIME) {
653                                 Training_obj_lines[i] |= TRAINING_OBJ_STATUS_UNKNOWN;
654                         } else {
655                                 Training_obj_lines[i] |= TRAINING_OBJ_STATUS_UNKNOWN;
656                         }
657                 }
658         }
659
660
661         int slot_idx, unkn_vis;
662         // go through list and bump as needed
663         for (i=0; i<num_offset_events; i++) {
664
665                 // find most recent directive that would not be shown
666                 for (unkn_vis=offset-1; unkn_vis>=0; unkn_vis--) {
667                         if (Training_obj_lines[unkn_vis] & TRAINING_OBJ_STATUS_UNKNOWN) {
668                                 break;
669                         }
670                 }
671
672                 // find first slot that can be bumped
673                 // look at the last (N-4 to N) positions
674                 for (slot_idx=0; slot_idx<TRAINING_OBJ_DISPLAY_LINES; slot_idx++) {
675                         if ( Training_obj_lines[i+offset] & TRAINING_OBJ_STATUS_KNOWN ) {
676                                 break;
677                         }
678                 }
679
680                 // shift and replace (mark old one as STATUS_KNOWN)
681                 for (int j=slot_idx; j>0; j--) {
682                         Training_obj_lines[j+offset-1] = Training_obj_lines[j+offset-2];
683                 }
684                 Training_obj_lines[offset] = Training_obj_lines[unkn_vis];
685                 Training_obj_lines[unkn_vis] &= ~TRAINING_OBJ_LINES_EVENT_STATUS_MASK;
686                 Training_obj_lines[unkn_vis] |= TRAINING_OBJ_STATUS_KNOWN;
687         }
688
689         // remove event status
690         for (i=0; i<Training_obj_num_lines; i++) {
691                 Training_obj_lines[i] &= ~TRAINING_OBJ_LINES_EVENT_STATUS_MASK;
692         }
693 }
694
695 // called at same rate as goals/events are evaluated.  Maintains the objectives listing, adding,
696 // removing and updating items
697 void training_check_objectives()
698 {
699         int i, event_idx, event_status;
700
701         Training_obj_num_lines = 0;
702         for (event_idx=0; event_idx<Num_mission_events; event_idx++) {
703                 event_status = mission_get_event_status(event_idx);
704                 if ( (event_status != EVENT_UNBORN) && Mission_events[event_idx].objective_text && (timestamp() > Mission_events[event_idx].born_on_date + 3000) ) {
705                         if (!Training_failure || !strnicmp(Mission_events[event_idx].name, XSTR( "Training failed", 423), 15)) {
706
707                                 // check for the actual objective
708                                 for (i=0; i<Training_obj_num_lines; i++) {
709                                         if (Training_obj_lines[i] == event_idx) {
710                                                 break;
711                                         }
712                                 }
713
714                                 // not in objective list, need to add it
715                                 if (i == Training_obj_num_lines) {
716                                         if (Training_obj_num_lines == TRAINING_OBJ_LINES) {
717
718                                                 // objective list full, remove topmost objective
719                                                 for (i=1; i<TRAINING_OBJ_LINES; i++) {
720                                                         Training_obj_lines[i - 1] = Training_obj_lines[i];
721                                                 }
722                                                 Training_obj_num_lines--;
723                                         }
724                                         // add the new directive
725                                         Training_obj_lines[Training_obj_num_lines++] = event_idx;
726                                 }
727
728                                 // now check for the associated keypress text
729                                 for (i=0; i<Training_obj_num_lines; i++) {
730                                         if (Training_obj_lines[i] == (event_idx | TRAINING_OBJ_LINES_KEY)) {
731                                                 break;
732                                         }
733                                 }
734
735                                 // if there is a keypress message with directive, process that too.
736                                 if (Mission_events[event_idx].objective_key_text) {
737                                         if (event_status == EVENT_CURRENT) {
738
739                                                 // not in objective list, need to add it
740                                                 if (i == Training_obj_num_lines) {
741                                                         if (Training_obj_num_lines == TRAINING_OBJ_LINES) {
742
743                                                                 // objective list full, remove topmost objective
744                                                                 for (i=1; i<Training_obj_num_lines; i++) {
745                                                                         Training_obj_lines[i - 1] = Training_obj_lines[i];
746                                                                 }
747                                                                 Training_obj_num_lines--;
748                                                         }
749                                                         // mark objective as having key text
750                                                         Training_obj_lines[Training_obj_num_lines++] = event_idx | TRAINING_OBJ_LINES_KEY;
751                                                 }
752
753                                         } else {
754                                                 // message with key press text is no longer valid, so remove it
755                                                 if (i < Training_obj_num_lines) {
756                                                         for (; i<Training_obj_num_lines - 1; i++) {
757                                                                 Training_obj_lines[i] = Training_obj_lines[i + 1];
758                                                         }
759                                                         Training_obj_num_lines--;
760                                                 }
761                                         }
762                                 }
763                         }
764                 }
765         }
766
767         // now sort list of events
768         // sort on EVENT_CURRENT and born on date, for other events (EVENT_SATISFIED, EVENT_FAILED) sort on born on date
769         sort_training_objectives();
770 }
771
772 // called to do cleanup when leaving a mission
773 void training_mission_shutdown()
774 {
775         if (Training_voice >= 0) {
776                 if (Training_voice_type) {
777                         audiostream_close_file(Training_voice_handle, 0);
778
779                 } else {
780                         snd_stop(Training_voice_handle);
781                 }
782         }
783
784         Training_voice = -1;
785         Training_num_lines = Training_obj_num_lines = 0;
786         *Training_text = 0;
787 }
788
789 // translates special tokens.  Handles one token only.
790 char *translate_msg_token(char *str)
791 {
792         if (!stricmp(str, NOX("wp"))) {
793                 sprintf(str, "%d", Training_context_goal_waypoint + 1);
794                 return str;
795         }
796
797         return NULL;
798 }
799
800 // translates all special tokens in a message, producing the new finalized message to be displayed
801 void message_translate_tokens(char *buf, char *text)
802 {
803         char temp[40], *toke1, *toke2, *ptr, *orig_buf;
804         int r;
805
806         orig_buf = buf;
807         *buf = 0;
808         toke1 = strchr(text, '$');
809         toke2 = strchr(text, '#');
810         while (toke1 || toke2) {  // is either token types present?
811                 if (!toke2 || (toke1 && (toke1 < toke2))) {  // found $ before #
812                         strncpy(buf, text, toke1 - text + 1);  // copy text up to token
813                         buf += toke1 - text + 1;
814                         text = toke1 + 1;  // advance pointers past processed data
815
816                         toke2 = strchr(text, '$');
817                         if (!toke2)  // No second one?
818                                 break;
819
820                         strncpy(temp, text, toke2 - text);  // isolate token into seperate buffer
821                         temp[toke2 - text] = 0;  // null terminate string
822                         ptr = translate_key(temp);  // try and translate key
823                         if (ptr) {  // was key translated properly?
824                                 if (!stricmp(ptr, NOX("none")) && (Training_bind_warning != Missiontime)) {
825                                         if ( The_mission.game_type & MISSION_TYPE_TRAINING ) {
826                                                 r = popup(PF_TITLE_BIG | PF_TITLE_RED, 2, XSTR( "&Bind Control", 424), XSTR( "&Abort mission", 425),
827                                                         XSTR( "Warning\nYou have no control bound to the action \"%s\".  You must do so before you can continue with your training.", 426),
828                                                         XSTR(Control_config[Failed_key_index].text, CONTROL_CONFIG_XSTR + Failed_key_index));
829
830                                                 if (r) {  // do they want to abort the mission?
831                                                         gameseq_post_event(GS_EVENT_END_GAME);
832                                                         return;
833                                                 }
834
835                                                 gameseq_post_event(GS_EVENT_CONTROL_CONFIG);  // goto control config screen to bind the control
836                                         }
837                                 }
838
839                                 buf--;  // erase the $
840                                 strcpy(buf, ptr);  // put translated key in place of token
841                                 buf += strlen(buf);
842                                 text = toke2 + 1;
843                         }
844
845                 } else {
846                         strncpy(buf, text, toke2 - text + 1);  // copy text up to token
847                         buf += toke2 - text + 1;
848                         text = toke2 + 1;  // advance pointers past processed data
849
850                         toke1 = strchr(text, '#');
851                         if (toke1)  // No second one?
852                                 break;
853
854                         strncpy(temp, text, toke1 - text);  // isolate token into seperate buffer
855                         temp[toke1 - text] = 0;  // null terminate string
856                         ptr = translate_msg_token(temp);  // try and translate key
857                         if (ptr) {  // was key translated properly?
858                                 buf--;  // erase the #
859                                 strcpy(buf, ptr);  // put translated key in place of token
860                                 buf += strlen(buf);
861                                 text = toke1 + 1;
862                         }
863                 }
864
865                 toke1 = strchr(text, '$');
866                 toke2 = strchr(text, '#');
867         }
868
869         strcpy(buf, text);
870         return;
871 }
872
873 // plays the voice file associated with a training message.  Automatically streams the file
874 // from disk if it's over 100k, otherwise plays it as a normal file in memory.  Returns -1
875 // if it didn't play, otherwise index of voice
876 int message_play_training_voice(int index)
877 {
878         int len;
879         CFILE *fp;
880
881         if (index < 0) {
882                 if (Training_voice >= 0) {
883                         if (Training_voice_type) {
884                                 audiostream_close_file(Training_voice_handle, 0);
885
886                         } else {
887                                 snd_stop(Training_voice_handle);
888                         }
889                 }
890
891                 Training_voice = -1;
892                 return -1;
893         }
894
895         if (Message_waves[index].num < 0) {
896                 fp = cfopen(Message_waves[index].name, "rb");
897                 if (!fp)
898                         return -1;
899
900                 len = cfilelength(fp);
901                 cfclose(fp);
902                 if (len > 100000) {
903                         if ((Training_voice < 0) || !Training_voice_type || (Training_voice != index)) {
904                                 if (Training_voice >= 0) {
905                                         if (Training_voice_type) {
906                                                 if (Training_voice == index)
907                                                         audiostream_stop(Training_voice_handle);
908                                                 else
909                                                         audiostream_close_file(Training_voice_handle, 0);
910
911                                         } else {
912                                                 snd_stop(Training_voice_handle);
913                                         }
914                                 }
915
916                                 if (stricmp(Message_waves[index].name, NOX("none.wav"))) {
917                                         Training_voice_handle = audiostream_open(Message_waves[index].name, ASF_VOICE);
918                                         if (Training_voice_handle < 0) {
919                                                 nprintf(("Warning", "Unable to load voice file %s\n", Message_waves[index].name));
920                                         //      Warning(LOCATION, "Unable to load voice file %s\n", Message_waves[index].name);
921                                         }
922                                 }
923                         }  // Training_voice should be valid and loaded now
924
925                         Training_voice_type = 1;
926                         if (Training_voice_handle >= 0)
927                                 audiostream_play(Training_voice_handle, Master_voice_volume, 0);
928
929                         Training_voice = index;
930                         return Training_voice;
931
932                 } else {
933                         game_snd tmp_gs;
934                         memset(&tmp_gs, 0, sizeof(game_snd));
935                         strcpy(tmp_gs.filename, Message_waves[index].name);
936                         Message_waves[index].num = snd_load(&tmp_gs);
937                         if (Message_waves[index].num < 0) {
938                                 nprintf(("Warning", "Cannot load message wave: %s.  Will not play\n", Message_waves[index].name));
939                                 return -1;
940                         }
941                 }
942         }
943
944         if (Training_voice >= 0) {
945                 if (Training_voice_type) {
946                         audiostream_close_file(Training_voice_handle, 0);
947
948                 } else {
949                         snd_stop(Training_voice_handle);
950                 }
951         }
952
953         Training_voice = index;
954         if (Message_waves[index].num >= 0)
955                 Training_voice_handle = snd_play_raw(Message_waves[index].num, 0.0f);
956         else
957                 Training_voice_handle = -1;
958
959         Training_voice_type = 0;
960         return Training_voice;
961 }
962
963 // one time initializations done when we want to display a new training mission.  This does
964 // all the processing and setup required to actually display it, including starting the
965 // voice file playing
966 void message_training_setup(int m, int length)
967 {
968         if ((m < 0) || !Messages[m].message[0]) {  // remove current message from the screen
969                 Training_num_lines = 0;
970                 return;
971         }
972
973         message_translate_tokens(Training_buf, Messages[m].message);
974         HUD_add_to_scrollback(Training_buf, HUD_SOURCE_TRAINING);
975         strcpy(Training_text, Messages[m].message);
976
977         if (message_play_training_voice(Messages[m].wave_info.index) < 0) {
978                 if (length > 0)
979                         Training_msg_timestamp = timestamp(length * 1000);
980                 else
981                         Training_msg_timestamp = timestamp(TRAINING_TIMING_BASE + strlen(Messages[m].message) * TRAINING_TIMING);  // no voice file playing
982
983         } else
984                 Training_msg_timestamp = 0;
985 }
986
987 // adds simple text to the directives display
988 /*id message_training_add_simple( char *text )
989 {
990         int i;
991
992         training_process_msg(text);
993         HUD_add_to_scrollback(Training_buf, HUD_SOURCE_TRAINING);
994         Training_num_lines = split_str(Training_buf, TRAINING_LINE_WIDTH, Training_line_sizes, Training_lines, MAX_TRAINING_MSG_LINES);
995         Assert(Training_num_lines > 0);
996         for (i=0; i<Training_num_lines; i++)
997                 Training_lines[i][Training_line_sizes[i]] = 0;
998
999         Training_msg_timestamp = timestamp(5000);
1000 } */
1001
1002 // add a message to the que to be sent later.
1003 void message_training_que(char *text, int timestamp, int length)
1004 {
1005         int m;
1006
1007         Assert(Training_msg_que_count < TRAINING_MSG_QUE_MAX);
1008         if (Training_msg_que_count < TRAINING_MSG_QUE_MAX) {
1009                 if (!stricmp(text, NOX("none")))
1010                         m = -1;
1011
1012                 else {
1013                         for (m=0; m<Num_messages; m++)
1014                                 if (!stricmp(text, Messages[m].name))
1015                                         break;
1016
1017                         Assert(m < Num_messages);
1018                         if (m >= Num_messages)
1019                                 return;
1020                 }
1021
1022                 Training_msg_que[Training_msg_que_count].num = m;
1023                 Training_msg_que[Training_msg_que_count].timestamp = timestamp;
1024                 Training_msg_que[Training_msg_que_count].length = length;
1025                 Training_msg_que_count++;
1026         }
1027 }
1028
1029 // check the training message que to see if we should play a new message yet or not.
1030 void message_training_que_check()
1031 {
1032         int i, j, iship_num;
1033
1034         // get the instructor's ship.
1035         iship_num = ship_name_lookup(NOX("instructor"));
1036         if ( iship_num == -1 )
1037                 return;
1038
1039         // if the instructor is dying or departing, do nothing
1040         if (Ships[iship_num].flags & (SF_DYING | SF_DEPARTING))
1041                 return;
1042
1043         if (Training_failure)
1044                 return;
1045
1046         for (i=0; i<Training_msg_que_count; i++) {
1047                 if (timestamp_elapsed(Training_msg_que[i].timestamp)) {
1048                         message_training_setup(Training_msg_que[i].num, Training_msg_que[i].length);
1049                         // remove this message from the que now.
1050                         for (j=i+1; j<Training_msg_que_count; j++)
1051                                 Training_msg_que[j - 1] = Training_msg_que[j];
1052
1053                         i--;
1054                         Training_msg_que_count--;
1055                 }
1056         }
1057 }
1058
1059 // displays (renders) the training message to the screen
1060 void message_training_display()
1061 {
1062         char *str, buf[256];
1063         int i, z, x, y, height, mode, count;
1064
1065         Training_msg_visible = 0;
1066         message_training_que_check();
1067         training_obj_display();
1068
1069         if (Training_failure){
1070                 return;
1071         }
1072
1073         if (timestamp_elapsed(Training_msg_timestamp) || !strlen(Training_text)){
1074                 return;
1075         }
1076
1077         message_translate_tokens(Training_buf, Training_text);
1078         training_process_msg(Training_text);
1079         Training_num_lines = split_str(Training_buf, TRAINING_LINE_WIDTH, Training_line_sizes, Training_lines, MAX_TRAINING_MSG_LINES);
1080         Assert(Training_num_lines > 0);
1081         for (i=0; i<Training_num_lines; i++) {
1082                 Training_lines[i][Training_line_sizes[i]] = 0;
1083                 drop_leading_white_space(Training_lines[i]);
1084         }
1085
1086         if (Training_num_lines <= 0){
1087                 return;
1088         }
1089
1090         height = gr_get_font_height();
1091         gr_set_shader(&Training_msg_glass);
1092         gr_shade(Training_msg_window_coords[gr_screen.res][0], Training_msg_window_coords[gr_screen.res][1], TRAINING_MSG_WINDOW_WIDTH, Training_num_lines * height + height);
1093
1094         gr_set_color_fast(&Color_bright_blue);
1095         mode = count = 0;
1096         Training_msg_visible = 1;
1097         for (i=0; i<Training_num_lines; i++) {  // loop through all lines of message
1098                 str = Training_lines[i];
1099                 z = 0;
1100                 x = Training_msg_window_coords[gr_screen.res][0] + (TRAINING_MSG_WINDOW_WIDTH - TRAINING_LINE_WIDTH) / 2;
1101                 y = Training_msg_window_coords[gr_screen.res][1] + i * height + height / 2 + 1;
1102
1103                 while (*str) {  // loop through each character of each line
1104                         if ((count < MAX_TRAINING_MSG_MODS) && (str == Training_msg_mods[count].pos)) {
1105                                 buf[z] = 0;
1106                                 gr_printf(x, y, buf);
1107                                 gr_get_string_size(&z, NULL, buf);
1108                                 x += z;
1109                                 z = 0;
1110
1111                                 mode = Training_msg_mods[count++].mode;
1112                                 switch (mode) {
1113                                         case TMMOD_NORMAL:
1114                                                 gr_set_color_fast(&Color_bright_blue);
1115                                                 break;
1116
1117                                         case TMMOD_BOLD:
1118                                                 gr_set_color_fast(&Color_white);
1119                                                 break;
1120                                 }
1121                         }
1122
1123                         buf[z++] = *str++;
1124                 }
1125
1126                 if (z) {
1127                         buf[z] = 0;
1128                         gr_printf(x, y, "%s", buf);
1129                 }
1130         }
1131
1132         Training_msg_method = 0;
1133 //      if (Training_msg_method) {
1134 //              char *msg = "Press a key to continue";
1135
1136 //              gr_get_string_size(&i, NULL, msg);
1137 //              gr_printf(TRAINING_MSG_WINDOW_X + TRAINING_MSG_WINDOW_WIDTH / 2 - i / 2, TRAINING_MSG_WINDOW_Y + (Training_num_lines + 2) * height, msg);
1138 //      }
1139
1140         if ((Training_voice >= 0) && (Training_num_lines > 0) && !(Training_msg_timestamp)) {
1141                 if (Training_voice_type)
1142                         z = audiostream_is_playing(Training_voice_handle);
1143                 else
1144                         z = snd_is_playing(Training_voice_handle);
1145
1146                 if (!z)
1147                         Training_msg_timestamp = timestamp(2000);  // 2 second delay
1148         }
1149 }
1150
1151 // processes a new training message to get hilighting information and store it in internal structures.
1152 void training_process_msg(char *msg)
1153 {
1154         int count;
1155         char *src, *dest, buf[8192];
1156
1157         message_translate_tokens(buf, msg);
1158         count = 0;
1159         src = buf;
1160         dest = Training_buf;
1161         while (*src) {
1162                 if (!strnicmp(src, NOX("<b>"), 3)) {
1163                         Assert(count < MAX_TRAINING_MSG_MODS);
1164                         src += 3;
1165                         Training_msg_mods[count].pos = dest;
1166                         Training_msg_mods[count].mode = TMMOD_BOLD;
1167                         count++;
1168                 }
1169
1170                 if (!strnicmp(src, NOX("</b>"), 4)) {
1171                         Assert(count < MAX_TRAINING_MSG_MODS);
1172                         src += 4;
1173                         Training_msg_mods[count].pos = dest;
1174                         Training_msg_mods[count].mode = TMMOD_NORMAL;
1175                         count++;
1176                 }
1177
1178                 *dest++ = *src++;
1179         }
1180
1181         *dest = 0;
1182         if (count < MAX_TRAINING_MSG_MODS)
1183                 Training_msg_mods[count].pos = NULL;
1184 }
1185
1186 void training_fail()
1187 {
1188         Training_failure = 1;
1189         //      JasonH: Add code here to suspend training and display a directive to warp out.
1190         //      Suspend training.
1191         //      Give directive to warp out.
1192         //      Also ensure that a special failure debriefing is given.  Must mention firing at instructor.
1193         //      Ask Sandeep to write it (or you can).
1194 }
1195