2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
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
10 * $Logfile: /Freespace2/code/Mission/MissionTraining.cpp $
15 * Special code for training missions. Stuff like displaying training messages in
16 * the special training window, listing the training objectives, etc.
19 * Revision 1.3 2002/06/09 04:41:22 relnev
20 * added copyright header
22 * Revision 1.2 2002/05/07 03:16:46 theoddone33
23 * The Great Newline Fix
25 * Revision 1.1.1.1 2002/05/03 03:28:10 root
29 * 10 8/27/99 2:20p Andsager
30 * Sort directives by born on date
32 * 9 8/22/99 7:57p Alanl
33 * fix bug that was scaling down mission training voice twice
35 * 8 8/02/99 7:29p Jefff
36 * moved instructor text in hi res
38 * 7 8/02/99 3:49p Jefff
39 * moved directives window down in 1024
41 * 6 7/10/99 1:44p Andsager
42 * Modified directives listing so that current and recently
43 * satisfied/failed directives stay on screen.
45 * 5 6/10/99 3:43p Dave
46 * Do a better job of syncing text colors to HUD gauges.
48 * 4 2/17/99 2:10p Dave
49 * First full run of squad war. All freespace and tracker side stuff
52 * 3 10/13/98 9:28a Dave
53 * Started neatening up freespace.h. Many variables renamed and
54 * reorganized. Added AlphaColors.[h,cpp]
56 * 2 10/07/98 10:53a Dave
59 * 1 10/07/98 10:49a Dave
61 * 58 8/25/98 1:48p Dave
62 * First rev of EMP effect. Player side stuff basically done. Next comes
65 * 57 6/13/98 5:19p Hoffoss
66 * externalized control config texts.
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
74 * 55 6/01/98 11:43a John
75 * JAS & MK: Classified all strings for localization.
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.
81 * 53 5/16/98 9:13p Allender
82 * don't display the directive display in multiplayer
84 * 52 5/07/98 11:59p Mike
85 * Don't show training popup for unbound key unless in training mission.
88 * 51 4/30/98 12:01a Lawrance
89 * using nprintf((Warning,...)) when audiostream can't be opened
91 * 50 4/27/98 11:20a Hoffoss
92 * Doubled TRAINING_MSG_QUE_MAX.
94 * 49 4/22/98 3:21p Hoffoss
95 * Fixed slight training message problems with training_failure state.
97 * 48 4/16/98 4:33p Hoffoss
98 * Added support for detecting instructor terminating training due to
99 * player shooting at him.
101 * 47 4/15/98 10:16p Mike
102 * Training mission #5.
103 * Fix application of subsystem damage.
105 * 46 4/15/98 5:25p Lawrance
106 * only show damage gauge when training msg not visible
108 * 45 4/03/98 2:52p Allender
109 * use Missiontime insteadof timestamp() for objective display
111 * 44 3/31/98 5:23p Hoffoss
112 * Fixed bug with training message token handling.
114 * 43 3/18/98 6:22p John
115 * Made directives display wiggle when afterburner is on
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.
121 * 41 2/23/98 8:31a John
122 * More string externalization
124 * 40 2/22/98 4:30p John
125 * More string externalization classification
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
131 * 38 1/30/98 4:24p Hoffoss
132 * Added a 3 second delay for directives before they get displayed.
134 * 37 1/26/98 6:27p Lawrance
135 * Change popups to use '&' meta-char for underlined hotkey.
137 * 36 1/22/98 11:46p Hoffoss
138 * Fixed linking problem with Fred.
140 * 35 1/22/98 11:00p Mike
141 * Fix Fred link error by commenting out line, email Hoffoss with info.
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
147 * 33 1/20/98 2:26p Hoffoss
148 * Removed references to timestamp_ticker, used timestamp() instead.
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.
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
159 * 30 1/09/98 10:47a Lawrance
160 * Only clear auto-target, linking etc. when actually starting a training
163 * 29 1/08/98 4:07p Hoffoss
164 * Made objective key text appear in Green.
166 * 28 1/06/98 2:07p Lawrance
167 * strip leading whitespace on training msg lines
169 * 27 1/05/98 11:05p Lawrance
170 * Clear primary/secondary linking if playing a training mission.
172 * 26 1/05/98 10:03p Lawrance
173 * Fix HUD gauge flag that didn't get changed.
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.
179 * 24 1/05/98 11:16a Mike
180 * Clear auto-targeting and auto-speed-matching in a training mission.
182 * 23 1/02/98 9:10p Lawrance
183 * Big changes to how colors get set on the HUD.
185 * 22 12/22/97 6:07p Hoffoss
186 * Made directives flash when completed, fixed but with is-destroyed
189 * 21 12/19/97 12:43p Hoffoss
190 * Changed code to allow counts in directives.
192 * 20 12/18/97 11:53a Lawrance
193 * fix bug with displaying hud directives
195 * 19 12/16/97 9:13p Lawrance
196 * Integrate new gauges into HUD config.
198 * 18 12/15/97 5:26p Allender
199 * temporary code to display for 5 second completion status of objectives
201 * 17 11/20/97 1:10a Lawrance
202 * add support for voice volumes
204 * 16 11/17/97 6:38p Lawrance
205 * move directives window down
207 * 15 11/14/97 1:24p Lawrance
208 * draw 'directives' as the title
210 * 14 11/14/97 1:21p Lawrance
211 * split directives lines if they get too long
213 * 13 11/12/97 6:00p Hoffoss
214 * Added training messages to hud scrollback log.
216 * 12 11/11/97 10:27p Lawrance
217 * implement new HUD directives gauge
219 * 11 11/11/97 12:59a Lawrance
220 * move objective window up slightly
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.
226 * 9 10/21/97 3:35p Dan
227 * ALAN: if wave file doesn't exist, don't play it
229 * 8 10/17/97 6:39p Hoffoss
230 * Added delayability to key-pressed operator and training-msg operator.
232 * 7 10/17/97 10:23a Hoffoss
233 * Added more comments.
235 * 6 10/14/97 11:35p Lawrance
236 * change snd_load parameters
238 * 5 10/13/97 4:33p Hoffoss
239 * Made training messages go away after time.
241 * 4 10/13/97 3:24p Hoffoss
242 * Made it so training message text can be arbitrarily bolded.
244 * 3 10/10/97 6:15p Hoffoss
245 * Implemented a training objective list display.
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.
251 * 1 10/09/97 2:41p Hoffoss
260 #include "audiostr.h"
261 #include "missionmessage.h"
262 #include "missiongoals.h"
263 #include "missionparse.h"
265 #include "controlsconfig.h"
268 #include "hudtarget.h"
269 #include "freespace.h"
272 #include "hudconfig.h"
275 #include "gamesequence.h"
277 #include "alphacolors.h"
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
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)
302 #define TRAINING_OBJ_LINES_MASK(n) (Training_obj_lines[n] & 0xffff)
304 #define TMMOD_NORMAL 0
309 int mode; // what function to perform at given position (TMMOD_*)
310 } training_msg_mods; // training message modifiers
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];
335 // coordinates for training messages
336 int Training_msg_window_coords[GR_NUM_RESOLUTIONS][2] = {
341 // coordinates for "directives" window on hud
342 int Training_obj_window_coords[GR_NUM_RESOLUTIONS][2] = {
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];
353 // local module prototypes
354 void training_process_msg(char *msg);
355 void message_translate_tokens(char *buf, char *text);
358 #define NUM_DIRECTIVE_GAUGES 3
359 static hud_frames Directive_gauge[NUM_DIRECTIVE_GAUGES];
360 static int Directive_frames_loaded = 0;
362 static const char *Directive_fnames[3] =
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] =
393 // displays (renders) the training objectives list
394 void training_obj_display()
396 char buf[256], *second_line;
397 int i, t, x, y, z, height, end, offset, bx, by, y_count;
400 if (!Training_obj_num_lines){
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) ) {
411 // don't ever display directives display in multiplayer missions
412 // if ( Game_mode & GM_MULTIPLAYER ){
416 height = gr_get_font_height();
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;
425 // draw top of objective display
426 // hud_set_default_color();
427 hud_set_gauge_color(HUD_DIRECTIVES_VIEW);
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));
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));
437 bx = DIRECTIVE_X+fl2i(HUD_offset_x);
438 by = Directive_coords[gr_screen.res][DIRECTIVE_COORDS_MIDDLE][1]+fl2i(HUD_offset_y);
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);
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;
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);
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)){
464 switch (mission_get_event_status(z)) {
466 // gr_set_color_fast(&Color_bright_white);
467 c = &Color_bright_white;
471 // gr_set_color_fast(&Color_bright_red);
472 c = &Color_bright_red;
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;
482 c = &Color_bright_white;
485 c = &Color_bright_blue;
491 // maybe split the directives line
492 second_line = split_str_once(buf, 167);
494 // blit the background frames
495 // hud_set_default_color();
496 hud_set_gauge_color(HUD_DIRECTIVES_VIEW);
498 GR_AABITMAP(Directive_gauge[1].first_frame, bx, by);
499 // gr_set_bitmap(Directive_gauge[1].first_frame);
500 // gr_aabitmap(bx, by);
505 GR_AABITMAP(Directive_gauge[1].first_frame, bx, by);
506 // gr_set_bitmap(Directive_gauge[1].first_frame);
507 // gr_aabitmap(bx, by);
513 gr_set_color_fast(c);
515 emp_hud_string(x, y, EG_OBJ1 + i, buf);
516 // gr_printf(x, y, buf);
521 y = Training_obj_window_coords[gr_screen.res][1] + fl2i(HUD_offset_y) + y_count * height + height / 2 + 1;
523 emp_hud_string(x+12, y, EG_OBJ1 + i + 1, second_line);
524 // gr_printf(x+12, y, second_line);
530 // draw the bottom of objective display
531 // hud_set_default_color();
532 hud_set_gauge_color(HUD_DIRECTIVES_VIEW);
534 GR_AABITMAP(Directive_gauge[2].first_frame, bx, by);
535 // gr_set_bitmap(Directive_gauge[2].first_frame);
536 // gr_aabitmap(bx, by);
539 // mission initializations (called once before a new mission is started)
540 void training_mission_init()
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;
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]);
559 Directive_frames_loaded = 1;
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 );
568 void training_mission_page_in()
571 for ( i = 0; i < NUM_DIRECTIVE_GAUGES; i++ ) {
572 bm_page_in_aabitmap( Directive_gauge[i].first_frame, Directive_gauge[i].num_frames );
577 int comp_training_lines_by_born_on_date(const void *m1, const void *m2)
583 Assert(Mission_events[*e1 & 0xffff].born_on_date != 0);
584 Assert(Mission_events[*e2 & 0xffff].born_on_date != 0);
586 return (Mission_events[*e1 & 0xffff].born_on_date - Mission_events[*e2 & 0xffff].born_on_date);
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()
596 int i, event_status, offset;
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);
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;
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));
614 if (event_status == EVENT_CURRENT) {
615 Training_obj_lines[i] |= TRAINING_OBJ_STATUS_UNKNOWN;
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;
622 Training_obj_lines[i] |= TRAINING_OBJ_STATUS_KNOWN;
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;
629 Training_obj_lines[i] |= TRAINING_OBJ_STATUS_KNOWN;
634 // if there are no directives which should be moved, we're done
635 if (num_offset_events == 0) {
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));
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;
649 Training_obj_lines[i] |= TRAINING_OBJ_STATUS_KNOWN;
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;
655 Training_obj_lines[i] |= TRAINING_OBJ_STATUS_UNKNOWN;
661 int slot_idx, unkn_vis;
662 // go through list and bump as needed
663 for (i=0; i<num_offset_events; i++) {
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) {
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 ) {
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];
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;
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;
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()
699 int i, event_idx, event_status;
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)) {
707 // check for the actual objective
708 for (i=0; i<Training_obj_num_lines; i++) {
709 if (Training_obj_lines[i] == event_idx) {
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) {
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];
722 Training_obj_num_lines--;
724 // add the new directive
725 Training_obj_lines[Training_obj_num_lines++] = event_idx;
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)) {
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) {
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) {
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];
747 Training_obj_num_lines--;
749 // mark objective as having key text
750 Training_obj_lines[Training_obj_num_lines++] = event_idx | TRAINING_OBJ_LINES_KEY;
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];
759 Training_obj_num_lines--;
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();
772 // called to do cleanup when leaving a mission
773 void training_mission_shutdown()
775 if (Training_voice >= 0) {
776 if (Training_voice_type) {
777 audiostream_close_file(Training_voice_handle, 0);
780 snd_stop(Training_voice_handle);
785 Training_num_lines = Training_obj_num_lines = 0;
789 // translates special tokens. Handles one token only.
790 char *translate_msg_token(char *str)
792 if (!stricmp(str, NOX("wp"))) {
793 sprintf(str, "%d", Training_context_goal_waypoint + 1);
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)
803 char temp[40], *toke1, *toke2, *ptr, *orig_buf;
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
816 toke2 = strchr(text, '$');
817 if (!toke2) // No second one?
820 strncpy(temp, text, toke2 - text); // isolate token into seperate buffer
821 temp[toke2 - text] = 0; // null terminate string
822 ptr = (char *)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));
830 if (r) { // do they want to abort the mission?
831 gameseq_post_event(GS_EVENT_END_GAME);
835 gameseq_post_event(GS_EVENT_CONTROL_CONFIG); // goto control config screen to bind the control
839 buf--; // erase the $
840 strcpy(buf, ptr); // put translated key in place of token
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
850 toke1 = strchr(text, '#');
851 if (toke1) // No second one?
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
865 toke1 = strchr(text, '$');
866 toke2 = strchr(text, '#');
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)
882 if (Training_voice >= 0) {
883 if (Training_voice_type) {
884 audiostream_close_file(Training_voice_handle, 0);
887 snd_stop(Training_voice_handle);
895 if (Message_waves[index].num < 0) {
896 fp = cfopen(Message_waves[index].name, "rb");
900 len = cfilelength(fp);
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);
909 audiostream_close_file(Training_voice_handle, 0);
912 snd_stop(Training_voice_handle);
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);
923 } // Training_voice should be valid and loaded now
925 Training_voice_type = 1;
926 if (Training_voice_handle >= 0)
927 audiostream_play(Training_voice_handle, Master_voice_volume, 0);
929 Training_voice = index;
930 return Training_voice;
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));
944 if (Training_voice >= 0) {
945 if (Training_voice_type) {
946 audiostream_close_file(Training_voice_handle, 0);
949 snd_stop(Training_voice_handle);
953 Training_voice = index;
954 if (Message_waves[index].num >= 0)
955 Training_voice_handle = snd_play_raw(Message_waves[index].num, 0.0f);
957 Training_voice_handle = -1;
959 Training_voice_type = 0;
960 return Training_voice;
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)
968 if ((m < 0) || !Messages[m].message[0]) { // remove current message from the screen
969 Training_num_lines = 0;
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);
977 if (message_play_training_voice(Messages[m].wave_info.index) < 0) {
979 Training_msg_timestamp = timestamp(length * 1000);
981 Training_msg_timestamp = timestamp(TRAINING_TIMING_BASE + strlen(Messages[m].message) * TRAINING_TIMING); // no voice file playing
984 Training_msg_timestamp = 0;
987 // adds simple text to the directives display
988 /*id message_training_add_simple( char *text )
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;
999 Training_msg_timestamp = timestamp(5000);
1002 // add a message to the que to be sent later.
1003 void message_training_que(char *text, int timestamp, int length)
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")))
1013 for (m=0; m<Num_messages; m++)
1014 if (!stricmp(text, Messages[m].name))
1017 Assert(m < Num_messages);
1018 if (m >= Num_messages)
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++;
1029 // check the training message que to see if we should play a new message yet or not.
1030 void message_training_que_check()
1032 int i, j, iship_num;
1034 // get the instructor's ship.
1035 iship_num = ship_name_lookup(NOX("instructor"));
1036 if ( iship_num == -1 )
1039 // if the instructor is dying or departing, do nothing
1040 if (Ships[iship_num].flags & (SF_DYING | SF_DEPARTING))
1043 if (Training_failure)
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];
1054 Training_msg_que_count--;
1059 // displays (renders) the training message to the screen
1060 void message_training_display()
1062 char *str, buf[256];
1063 int i, z, x, y, height, mode, count;
1065 Training_msg_visible = 0;
1066 message_training_que_check();
1067 training_obj_display();
1069 if (Training_failure){
1073 if (timestamp_elapsed(Training_msg_timestamp) || !strlen(Training_text)){
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]);
1086 if (Training_num_lines <= 0){
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);
1094 gr_set_color_fast(&Color_bright_blue);
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];
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;
1103 while (*str) { // loop through each character of each line
1104 if ((count < MAX_TRAINING_MSG_MODS) && (str == Training_msg_mods[count].pos)) {
1106 gr_printf(x, y, buf);
1107 gr_get_string_size(&z, NULL, buf);
1111 mode = Training_msg_mods[count++].mode;
1114 gr_set_color_fast(&Color_bright_blue);
1118 gr_set_color_fast(&Color_white);
1128 gr_printf(x, y, "%s", buf);
1132 Training_msg_method = 0;
1133 // if (Training_msg_method) {
1134 // char *msg = "Press a key to continue";
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);
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);
1144 z = snd_is_playing(Training_voice_handle);
1147 Training_msg_timestamp = timestamp(2000); // 2 second delay
1151 // processes a new training message to get hilighting information and store it in internal structures.
1152 void training_process_msg(char *msg)
1155 char *src, *dest, buf[8192];
1157 message_translate_tokens(buf, msg);
1160 dest = Training_buf;
1162 if (!strnicmp(src, NOX("<b>"), 3)) {
1163 Assert(count < MAX_TRAINING_MSG_MODS);
1165 Training_msg_mods[count].pos = dest;
1166 Training_msg_mods[count].mode = TMMOD_BOLD;
1170 if (!strnicmp(src, NOX("</b>"), 4)) {
1171 Assert(count < MAX_TRAINING_MSG_MODS);
1173 Training_msg_mods[count].pos = dest;
1174 Training_msg_mods[count].mode = TMMOD_NORMAL;
1182 if (count < MAX_TRAINING_MSG_MODS)
1183 Training_msg_mods[count].pos = NULL;
1186 void training_fail()
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).