2 * $Logfile: /Freespace2/code/Mission/MissionTraining.cpp $
7 * Special code for training missions. Stuff like displaying training messages in
8 * the special training window, listing the training objectives, etc.
11 * Revision 1.1 2002/05/03 03:28:10 root
15 * 10 8/27/99 2:20p Andsager
16 * Sort directives by born on date
18 * 9 8/22/99 7:57p Alanl
19 * fix bug that was scaling down mission training voice twice
21 * 8 8/02/99 7:29p Jefff
22 * moved instructor text in hi res
24 * 7 8/02/99 3:49p Jefff
25 * moved directives window down in 1024
27 * 6 7/10/99 1:44p Andsager
28 * Modified directives listing so that current and recently
29 * satisfied/failed directives stay on screen.
31 * 5 6/10/99 3:43p Dave
32 * Do a better job of syncing text colors to HUD gauges.
34 * 4 2/17/99 2:10p Dave
35 * First full run of squad war. All freespace and tracker side stuff
38 * 3 10/13/98 9:28a Dave
39 * Started neatening up freespace.h. Many variables renamed and
40 * reorganized. Added AlphaColors.[h,cpp]
42 * 2 10/07/98 10:53a Dave
45 * 1 10/07/98 10:49a Dave
47 * 58 8/25/98 1:48p Dave
48 * First rev of EMP effect. Player side stuff basically done. Next comes
51 * 57 6/13/98 5:19p Hoffoss
52 * externalized control config texts.
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
60 * 55 6/01/98 11:43a John
61 * JAS & MK: Classified all strings for localization.
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.
67 * 53 5/16/98 9:13p Allender
68 * don't display the directive display in multiplayer
70 * 52 5/07/98 11:59p Mike
71 * Don't show training popup for unbound key unless in training mission.
74 * 51 4/30/98 12:01a Lawrance
75 * using nprintf((Warning,...)) when audiostream can't be opened
77 * 50 4/27/98 11:20a Hoffoss
78 * Doubled TRAINING_MSG_QUE_MAX.
80 * 49 4/22/98 3:21p Hoffoss
81 * Fixed slight training message problems with training_failure state.
83 * 48 4/16/98 4:33p Hoffoss
84 * Added support for detecting instructor terminating training due to
85 * player shooting at him.
87 * 47 4/15/98 10:16p Mike
88 * Training mission #5.
89 * Fix application of subsystem damage.
91 * 46 4/15/98 5:25p Lawrance
92 * only show damage gauge when training msg not visible
94 * 45 4/03/98 2:52p Allender
95 * use Missiontime insteadof timestamp() for objective display
97 * 44 3/31/98 5:23p Hoffoss
98 * Fixed bug with training message token handling.
100 * 43 3/18/98 6:22p John
101 * Made directives display wiggle when afterburner is on
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.
107 * 41 2/23/98 8:31a John
108 * More string externalization
110 * 40 2/22/98 4:30p John
111 * More string externalization classification
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
117 * 38 1/30/98 4:24p Hoffoss
118 * Added a 3 second delay for directives before they get displayed.
120 * 37 1/26/98 6:27p Lawrance
121 * Change popups to use '&' meta-char for underlined hotkey.
123 * 36 1/22/98 11:46p Hoffoss
124 * Fixed linking problem with Fred.
126 * 35 1/22/98 11:00p Mike
127 * Fix Fred link error by commenting out line, email Hoffoss with info.
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
133 * 33 1/20/98 2:26p Hoffoss
134 * Removed references to timestamp_ticker, used timestamp() instead.
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.
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
145 * 30 1/09/98 10:47a Lawrance
146 * Only clear auto-target, linking etc. when actually starting a training
149 * 29 1/08/98 4:07p Hoffoss
150 * Made objective key text appear in Green.
152 * 28 1/06/98 2:07p Lawrance
153 * strip leading whitespace on training msg lines
155 * 27 1/05/98 11:05p Lawrance
156 * Clear primary/secondary linking if playing a training mission.
158 * 26 1/05/98 10:03p Lawrance
159 * Fix HUD gauge flag that didn't get changed.
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.
165 * 24 1/05/98 11:16a Mike
166 * Clear auto-targeting and auto-speed-matching in a training mission.
168 * 23 1/02/98 9:10p Lawrance
169 * Big changes to how colors get set on the HUD.
171 * 22 12/22/97 6:07p Hoffoss
172 * Made directives flash when completed, fixed but with is-destroyed
175 * 21 12/19/97 12:43p Hoffoss
176 * Changed code to allow counts in directives.
178 * 20 12/18/97 11:53a Lawrance
179 * fix bug with displaying hud directives
181 * 19 12/16/97 9:13p Lawrance
182 * Integrate new gauges into HUD config.
184 * 18 12/15/97 5:26p Allender
185 * temporary code to display for 5 second completion status of objectives
187 * 17 11/20/97 1:10a Lawrance
188 * add support for voice volumes
190 * 16 11/17/97 6:38p Lawrance
191 * move directives window down
193 * 15 11/14/97 1:24p Lawrance
194 * draw 'directives' as the title
196 * 14 11/14/97 1:21p Lawrance
197 * split directives lines if they get too long
199 * 13 11/12/97 6:00p Hoffoss
200 * Added training messages to hud scrollback log.
202 * 12 11/11/97 10:27p Lawrance
203 * implement new HUD directives gauge
205 * 11 11/11/97 12:59a Lawrance
206 * move objective window up slightly
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.
212 * 9 10/21/97 3:35p Dan
213 * ALAN: if wave file doesn't exist, don't play it
215 * 8 10/17/97 6:39p Hoffoss
216 * Added delayability to key-pressed operator and training-msg operator.
218 * 7 10/17/97 10:23a Hoffoss
219 * Added more comments.
221 * 6 10/14/97 11:35p Lawrance
222 * change snd_load parameters
224 * 5 10/13/97 4:33p Hoffoss
225 * Made training messages go away after time.
227 * 4 10/13/97 3:24p Hoffoss
228 * Made it so training message text can be arbitrarily bolded.
230 * 3 10/10/97 6:15p Hoffoss
231 * Implemented a training objective list display.
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.
237 * 1 10/09/97 2:41p Hoffoss
246 #include "audiostr.h"
247 #include "missionmessage.h"
248 #include "missiongoals.h"
249 #include "missionparse.h"
251 #include "controlsconfig.h"
254 #include "hudtarget.h"
255 #include "freespace.h"
258 #include "hudconfig.h"
261 #include "gamesequence.h"
263 #include "alphacolors.h"
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
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)
288 #define TRAINING_OBJ_LINES_MASK(n) (Training_obj_lines[n] & 0xffff)
290 #define TMMOD_NORMAL 0
295 int mode; // what function to perform at given position (TMMOD_*)
296 } training_msg_mods; // training message modifiers
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];
321 // coordinates for training messages
322 int Training_msg_window_coords[GR_NUM_RESOLUTIONS][2] = {
327 // coordinates for "directives" window on hud
328 int Training_obj_window_coords[GR_NUM_RESOLUTIONS][2] = {
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];
339 // local module prototypes
340 void training_process_msg(char *msg);
341 void message_translate_tokens(char *buf, char *text);
344 #define NUM_DIRECTIVE_GAUGES 3
345 static hud_frames Directive_gauge[NUM_DIRECTIVE_GAUGES];
346 static int Directive_frames_loaded = 0;
348 static char *Directive_fnames[3] =
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] =
379 // displays (renders) the training objectives list
380 void training_obj_display()
382 char buf[256], *second_line;
383 int i, t, x, y, z, height, end, offset, bx, by, y_count;
386 if (!Training_obj_num_lines){
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) ) {
397 // don't ever display directives display in multiplayer missions
398 // if ( Game_mode & GM_MULTIPLAYER ){
402 height = gr_get_font_height();
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;
411 // draw top of objective display
412 // hud_set_default_color();
413 hud_set_gauge_color(HUD_DIRECTIVES_VIEW);
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));
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));
423 bx = DIRECTIVE_X+fl2i(HUD_offset_x);
424 by = Directive_coords[gr_screen.res][DIRECTIVE_COORDS_MIDDLE][1]+fl2i(HUD_offset_y);
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);
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;
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);
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)){
450 switch (mission_get_event_status(z)) {
452 // gr_set_color_fast(&Color_bright_white);
453 c = &Color_bright_white;
457 // gr_set_color_fast(&Color_bright_red);
458 c = &Color_bright_red;
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;
468 c = &Color_bright_white;
471 c = &Color_bright_blue;
477 // maybe split the directives line
478 second_line = split_str_once(buf, 167);
480 // blit the background frames
481 // hud_set_default_color();
482 hud_set_gauge_color(HUD_DIRECTIVES_VIEW);
484 GR_AABITMAP(Directive_gauge[1].first_frame, bx, by);
485 // gr_set_bitmap(Directive_gauge[1].first_frame);
486 // gr_aabitmap(bx, by);
491 GR_AABITMAP(Directive_gauge[1].first_frame, bx, by);
492 // gr_set_bitmap(Directive_gauge[1].first_frame);
493 // gr_aabitmap(bx, by);
499 gr_set_color_fast(c);
501 emp_hud_string(x, y, EG_OBJ1 + i, buf);
502 // gr_printf(x, y, buf);
507 y = Training_obj_window_coords[gr_screen.res][1] + fl2i(HUD_offset_y) + y_count * height + height / 2 + 1;
509 emp_hud_string(x+12, y, EG_OBJ1 + i + 1, second_line);
510 // gr_printf(x+12, y, second_line);
516 // draw the bottom of objective display
517 // hud_set_default_color();
518 hud_set_gauge_color(HUD_DIRECTIVES_VIEW);
520 GR_AABITMAP(Directive_gauge[2].first_frame, bx, by);
521 // gr_set_bitmap(Directive_gauge[2].first_frame);
522 // gr_aabitmap(bx, by);
525 // mission initializations (called once before a new mission is started)
526 void training_mission_init()
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;
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]);
545 Directive_frames_loaded = 1;
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 );
554 void training_mission_page_in()
557 for ( i = 0; i < NUM_DIRECTIVE_GAUGES; i++ ) {
558 bm_page_in_aabitmap( Directive_gauge[i].first_frame, Directive_gauge[i].num_frames );
563 int comp_training_lines_by_born_on_date(const void *m1, const void *m2)
569 Assert(Mission_events[*e1 & 0xffff].born_on_date != 0);
570 Assert(Mission_events[*e2 & 0xffff].born_on_date != 0);
572 return (Mission_events[*e1 & 0xffff].born_on_date - Mission_events[*e2 & 0xffff].born_on_date);
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()
582 int i, event_status, offset;
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);
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;
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));
600 if (event_status == EVENT_CURRENT) {
601 Training_obj_lines[i] |= TRAINING_OBJ_STATUS_UNKNOWN;
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;
608 Training_obj_lines[i] |= TRAINING_OBJ_STATUS_KNOWN;
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;
615 Training_obj_lines[i] |= TRAINING_OBJ_STATUS_KNOWN;
620 // if there are no directives which should be moved, we're done
621 if (num_offset_events == 0) {
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));
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;
635 Training_obj_lines[i] |= TRAINING_OBJ_STATUS_KNOWN;
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;
641 Training_obj_lines[i] |= TRAINING_OBJ_STATUS_UNKNOWN;
647 int slot_idx, unkn_vis;
648 // go through list and bump as needed
649 for (i=0; i<num_offset_events; i++) {
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) {
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 ) {
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];
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;
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;
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()
685 int i, event_idx, event_status;
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)) {
693 // check for the actual objective
694 for (i=0; i<Training_obj_num_lines; i++) {
695 if (Training_obj_lines[i] == event_idx) {
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) {
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];
708 Training_obj_num_lines--;
710 // add the new directive
711 Training_obj_lines[Training_obj_num_lines++] = event_idx;
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)) {
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) {
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) {
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];
733 Training_obj_num_lines--;
735 // mark objective as having key text
736 Training_obj_lines[Training_obj_num_lines++] = event_idx | TRAINING_OBJ_LINES_KEY;
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];
745 Training_obj_num_lines--;
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();
758 // called to do cleanup when leaving a mission
759 void training_mission_shutdown()
761 if (Training_voice >= 0) {
762 if (Training_voice_type) {
763 audiostream_close_file(Training_voice_handle, 0);
766 snd_stop(Training_voice_handle);
771 Training_num_lines = Training_obj_num_lines = 0;
775 // translates special tokens. Handles one token only.
776 char *translate_msg_token(char *str)
778 if (!stricmp(str, NOX("wp"))) {
779 sprintf(str, "%d", Training_context_goal_waypoint + 1);
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)
789 char temp[40], *toke1, *toke2, *ptr, *orig_buf;
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
802 toke2 = strchr(text, '$');
803 if (!toke2) // No second one?
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));
816 if (r) { // do they want to abort the mission?
817 gameseq_post_event(GS_EVENT_END_GAME);
821 gameseq_post_event(GS_EVENT_CONTROL_CONFIG); // goto control config screen to bind the control
825 buf--; // erase the $
826 strcpy(buf, ptr); // put translated key in place of token
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
836 toke1 = strchr(text, '#');
837 if (toke1) // No second one?
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
851 toke1 = strchr(text, '$');
852 toke2 = strchr(text, '#');
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)
868 if (Training_voice >= 0) {
869 if (Training_voice_type) {
870 audiostream_close_file(Training_voice_handle, 0);
873 snd_stop(Training_voice_handle);
881 if (Message_waves[index].num < 0) {
882 fp = cfopen(Message_waves[index].name, "rb");
886 len = cfilelength(fp);
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);
895 audiostream_close_file(Training_voice_handle, 0);
898 snd_stop(Training_voice_handle);
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);
909 } // Training_voice should be valid and loaded now
911 Training_voice_type = 1;
912 if (Training_voice_handle >= 0)
913 audiostream_play(Training_voice_handle, Master_voice_volume, 0);
915 Training_voice = index;
916 return Training_voice;
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));
930 if (Training_voice >= 0) {
931 if (Training_voice_type) {
932 audiostream_close_file(Training_voice_handle, 0);
935 snd_stop(Training_voice_handle);
939 Training_voice = index;
940 if (Message_waves[index].num >= 0)
941 Training_voice_handle = snd_play_raw(Message_waves[index].num, 0.0f);
943 Training_voice_handle = -1;
945 Training_voice_type = 0;
946 return Training_voice;
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)
954 if ((m < 0) || !Messages[m].message[0]) { // remove current message from the screen
955 Training_num_lines = 0;
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);
963 if (message_play_training_voice(Messages[m].wave_info.index) < 0) {
965 Training_msg_timestamp = timestamp(length * 1000);
967 Training_msg_timestamp = timestamp(TRAINING_TIMING_BASE + strlen(Messages[m].message) * TRAINING_TIMING); // no voice file playing
970 Training_msg_timestamp = 0;
973 // adds simple text to the directives display
974 /*id message_training_add_simple( char *text )
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;
985 Training_msg_timestamp = timestamp(5000);
988 // add a message to the que to be sent later.
989 void message_training_que(char *text, int timestamp, int length)
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")))
999 for (m=0; m<Num_messages; m++)
1000 if (!stricmp(text, Messages[m].name))
1003 Assert(m < Num_messages);
1004 if (m >= Num_messages)
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++;
1015 // check the training message que to see if we should play a new message yet or not.
1016 void message_training_que_check()
1018 int i, j, iship_num;
1020 // get the instructor's ship.
1021 iship_num = ship_name_lookup(NOX("instructor"));
1022 if ( iship_num == -1 )
1025 // if the instructor is dying or departing, do nothing
1026 if (Ships[iship_num].flags & (SF_DYING | SF_DEPARTING))
1029 if (Training_failure)
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];
1040 Training_msg_que_count--;
1045 // displays (renders) the training message to the screen
1046 void message_training_display()
1048 char *str, buf[256];
1049 int i, z, x, y, height, mode, count;
1051 Training_msg_visible = 0;
1052 message_training_que_check();
1053 training_obj_display();
1055 if (Training_failure){
1059 if (timestamp_elapsed(Training_msg_timestamp) || !strlen(Training_text)){
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]);
1072 if (Training_num_lines <= 0){
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);
1080 gr_set_color_fast(&Color_bright_blue);
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];
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;
1089 while (*str) { // loop through each character of each line
1090 if ((count < MAX_TRAINING_MSG_MODS) && (str == Training_msg_mods[count].pos)) {
1092 gr_printf(x, y, buf);
1093 gr_get_string_size(&z, NULL, buf);
1097 mode = Training_msg_mods[count++].mode;
1100 gr_set_color_fast(&Color_bright_blue);
1104 gr_set_color_fast(&Color_white);
1114 gr_printf(x, y, "%s", buf);
1118 Training_msg_method = 0;
1119 // if (Training_msg_method) {
1120 // char *msg = "Press a key to continue";
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);
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);
1130 z = snd_is_playing(Training_voice_handle);
1133 Training_msg_timestamp = timestamp(2000); // 2 second delay
1137 // processes a new training message to get hilighting information and store it in internal structures.
1138 void training_process_msg(char *msg)
1141 char *src, *dest, buf[8192];
1143 message_translate_tokens(buf, msg);
1146 dest = Training_buf;
1148 if (!strnicmp(src, NOX("<b>"), 3)) {
1149 Assert(count < MAX_TRAINING_MSG_MODS);
1151 Training_msg_mods[count].pos = dest;
1152 Training_msg_mods[count].mode = TMMOD_BOLD;
1156 if (!strnicmp(src, NOX("</b>"), 4)) {
1157 Assert(count < MAX_TRAINING_MSG_MODS);
1159 Training_msg_mods[count].pos = dest;
1160 Training_msg_mods[count].mode = TMMOD_NORMAL;
1168 if (count < MAX_TRAINING_MSG_MODS)
1169 Training_msg_mods[count].pos = NULL;
1172 void training_fail()
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).