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