]> icculus.org git repositories - taylor/freespace2.git/blob - src/hud/hudmessage.cpp
automatic language selection support
[taylor/freespace2.git] / src / hud / hudmessage.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Hud/HUDmessage.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * C module that controls and manages the message window on the HUD
16  *
17  * $Log$
18  * Revision 1.6  2002/07/28 05:05:08  relnev
19  * removed some old stuff
20  *
21  * Revision 1.5  2002/07/13 06:46:48  theoddone33
22  * Warning cleanups
23  *
24  * Revision 1.4  2002/06/09 04:41:21  relnev
25  * added copyright header
26  *
27  * Revision 1.3  2002/06/02 04:26:34  relnev
28  * warning cleanup
29  *
30  * Revision 1.2  2002/05/03 13:34:33  theoddone33
31  * More stuff compiles
32  *
33  * Revision 1.1.1.1  2002/05/03 03:28:09  root
34  * Initial import.
35  *
36  * 
37  * 23    9/08/99 5:38p Jefff
38  * 
39  * 22    9/08/99 2:38p Jefff
40  * sound pausing going to menu from game
41  * 
42  * 21    9/07/99 9:42p Jefff
43  * 
44  * 20    9/02/99 11:47a Jefff
45  * yet another hud message length adjustment.  yeesh.
46  * 
47  * 19    8/25/99 10:08a Jefff
48  * fixed another messge cut-off bug
49  * 
50  * 18    8/23/99 11:12a Jefff
51  * fixed 1024 message cut-off bug
52  * 
53  * 17    8/20/99 2:26p Jefff
54  * hud message text wrapping problem in hires fixed
55  * 
56  * 16    8/03/99 7:27p Jefff
57  * hud messages go completely across screnn in high res now
58  * 
59  * 15    8/03/99 6:21p Jefff
60  * fixed stupid bug with objectives screen key
61  * 
62  * 14    8/01/99 12:39p Dave
63  * Added HUD contrast control key (for nebula).
64  * 
65  * 13    7/29/99 2:58p Jefff
66  * Ingame objective screen icon key now uses normal objective icons and
67  * text is drawn in code.
68  * 
69  * 12    6/16/99 5:26p Dave
70  * Fixed some bitmap and coordinate problems on the mission scrollback
71  * screen.
72  * 
73  * 11    6/10/99 3:43p Dave
74  * Do a better job of syncing text colors to HUD gauges.
75  * 
76  * 10    2/02/99 10:13a Neilk
77  * fixed more coords
78  * 
79  * 9     2/01/99 5:55p Dave
80  * Removed the idea of explicit bitmaps for buttons. Fixed text
81  * highlighting for disabled gadgets.
82  * 
83  * 8     1/30/99 5:08p Dave
84  * More new hi-res stuff.Support for nice D3D textures.
85  * 
86  * 7     1/30/99 3:09p Neilk
87  * More mission log coord fixes
88  * 
89  * 6     1/30/99 2:59p Neilk
90  * Fixed more mission log coords
91  * 
92  * 5     1/29/99 7:57p Neilk
93  * Added support for multiresolutions
94  * 
95  * 4     1/06/99 2:24p Dave
96  * Stubs and release build fixes.
97  * 
98  * 3     10/13/98 9:28a Dave
99  * Started neatening up freespace.h. Many variables renamed and
100  * reorganized. Added AlphaColors.[h,cpp]
101  * 
102  * 2     10/07/98 10:53a Dave
103  * Initial checkin.
104  * 
105  * 1     10/07/98 10:49a Dave
106  * 
107  * 89    6/09/98 5:17p Lawrance
108  * French/German localization
109  * 
110  * 88    6/09/98 10:31a Hoffoss
111  * Created index numbers for all xstr() references.  Any new xstr() stuff
112  * added from here on out should be added to the end if the list.  The
113  * current list count can be found in FreeSpace.cpp (search for
114  * XSTR_SIZE).
115  * 
116  * 87    5/19/98 8:36p Andsager
117  * Fix bug where last line of message log would not show if more than one
118  * screen of text.
119  * 
120  * 86    4/27/98 8:49p Allender
121  * make terran command display in white (with correct code anyway).
122  * 
123  * 85    4/25/98 11:49p Lawrance
124  * init pos to zero
125  * 
126  * 84    4/25/98 9:10p Hoffoss
127  * Fixed bug with scrollback origin.
128  * 
129  * 83    4/25/98 5:22p Hoffoss
130  * Fixed some problems with scrolling and positioning, and added code to
131  * support pageup/pagedown.
132  * 
133  * 82    4/14/98 5:06p Dave
134  * Don't load or send invalid pilot pics. Fixed chatbox graphic errors.
135  * Made chatbox display team icons in a team vs. team game. Fixed up pause
136  * and endgame sequencing issues.
137  * 
138  * 81    4/14/98 2:44p Hoffoss
139  * Made arrow keys do what tab does.
140  * 
141  * 80    4/08/98 4:10p John
142  * Removed all remaining traces of the evil gr_init_font_ex.
143  * 
144  * 79    4/07/98 11:33a Hoffoss
145  * Fixed bug where scroll offset was wrong when first entering the F4
146  * screen.
147  * 
148  * 78    4/05/98 3:30p Dave
149  * Print netplayer messages in brighter green on the hud, with
150  * accompanying sound. Echo netplayer messages on sending machine. Fixed
151  * standalone sequencing bug where host never get the "enter mission"
152  * button.
153  * 
154  * 77    3/27/98 11:57a Dave
155  * Put in expression checking for text messages.
156  * 
157  * 76    3/17/98 4:01p Hoffoss
158  * Added HUD_SOURCE_TERRAN_CMD and changed code to utilize it when a
159  * message is being sent from Terran Command.
160  * 
161  * 75    3/16/98 5:55p Lawrance
162  * Increase width of HUD message line, don't draw lines while comm menu is
163  * up
164  * 
165  * 74    3/12/98 4:03p Hoffoss
166  * Changed formatting used in hug scrollbacl log.
167  * 
168  * 73    3/09/98 4:47p Hoffoss
169  * Changed F4 screen to start in objectives mode rather than HUD messages
170  * mode.
171  * 
172  * 72    3/09/98 2:50p Hoffoss
173  * Changed to use different palette file, and fixed bug with text
174  * overrunning the right edge of screen.
175  * 
176  * 71    3/02/98 5:42p John
177  * Removed WinAVI stuff from Freespace.  Made all HUD gauges wriggle from
178  * afterburner.  Made gr_set_clip work good with negative x &y.  Made
179  * model_caching be on by default.  Made each cached model have it's own
180  * bitmap id.  Made asteroids not rotate when model_caching is on.  
181  * 
182  * 70    2/27/98 4:55p Hoffoss
183  * Fixed some alignment problems.
184  * 
185  * 69    2/27/98 4:37p Hoffoss
186  * Combined Objectives screen into Mission Log screen.
187  * 
188  * 68    2/22/98 4:17p John
189  * More string externalization classification... 190 left to go!
190  * 
191  * 67    2/22/98 12:19p John
192  * Externalized some strings
193  * 
194  * 66    2/06/98 2:58p Hoffoss
195  * Fixed bug where mission log scrolls twice when using the arrow keys.
196  * 
197  * 65    1/29/98 10:26a Hoffoss
198  * Made changes so arrow buttons repeat scrolling when held down.
199  * 
200  * 64    1/20/98 4:39p Hoffoss
201  * Added mission time display to scrollback log screen.
202  * 
203  * 63    1/18/98 5:09p Lawrance
204  * Added support for TEAM_TRAITOR
205  * 
206  * 62    1/12/98 11:16p Lawrance
207  * Wonderful HUD config.
208  * 
209  * 61    1/08/98 1:33p Hoffoss
210  * Made scroll offset reset to bottom of list instead of top.
211  * 
212  * 60    1/05/98 2:59p Hoffoss
213  * Fixed bug with messages drawing outside of limits.
214  * 
215  * 59    1/02/98 9:10p Lawrance
216  * Big changes to how colors get set on the HUD.
217  * 
218  * 58    1/02/98 10:23a Hoffoss
219  * Fixed incorrect scrolling directions in message scrollback log.
220  * 
221  * 57    12/11/97 10:17p Dave
222  * Put in some checks to make sure HUD_printfs aren't
223  * done in certain multiplayer situations.
224  * 
225  * 56    12/05/97 2:16p Hoffoss
226  * Made hidden hud messages actually not show up in scrollback.
227  * 
228  * 55    12/03/97 6:07p Dave
229  * Added assert that hud scrollback initialized before adding messages to
230  * it.
231  * 
232  * 54    12/03/97 4:16p Hoffoss
233  * Changed sound stuff used in interface screens for interface purposes.
234  * 
235  * 53    12/03/97 11:35a Hoffoss
236  * Made changes to HUD messages send throughout the game.
237  * 
238  * 52    12/02/97 5:57p Hoffoss
239  * Changed Hud messaging code to align text to right after sending ship's
240  * name.
241  * 
242  * 51    12/01/97 4:30p Hoffoss
243  * Changed code to list hud messages in scrollback from top down.
244  * 
245  * 50    11/25/97 10:01a Jasen
246  * Remoced excess buttons from MessageLog screen.
247  * 
248  * 49    11/24/97 10:03p Jasen
249  * Dang... had to make an entirely new revision of the last button.  :)
250  * 
251  * 48    11/24/97 9:44p Jasen
252  * Changed button name and coords to new exit button.
253  * 
254  * 47    11/20/97 12:02p Lawrance
255  * change Error to nprintf at warning level
256  * 
257  * 46    11/17/97 6:37p Lawrance
258  * new gauges: extended target view, new lock triangles, support ship view
259  * 
260  * 45    11/14/97 2:46p Lawrance
261  * decrease width of HUD message line to 435, so it doesn overlap with
262  * message menu
263  * 
264  * 44    11/13/97 10:16p Hoffoss
265  * Added icons to mission log scrollback.
266  * 
267  * 43    11/13/97 4:05p Hoffoss
268  * Added hiding code for mission log entries.
269  * 
270  * 42    11/12/97 6:00p Hoffoss
271  * Added training messages to hud scrollback log.
272  * 
273  * 41    11/11/97 11:16a Hoffoss
274  * Changed hud scrollback to color entire lines.
275  * 
276  * 40    11/06/97 5:42p Hoffoss
277  * Added support for fixed size timstamp rendering.
278  * 
279  * 39    11/05/97 7:11p Hoffoss
280  * Made changed to the hud message system.  Hud messages can now have
281  * sources so they can be color coded.
282  * 
283  * 38    11/04/97 4:56p Jasen
284  * Updated coordinates for buttons
285  * 
286  * 37    11/03/97 10:12p Hoffoss
287  * Finished up work on the hud message/mission log scrollback screen.
288  * 
289  * 36    11/03/97 5:38p Dave
290  * Cleaned up more multiplayer sequencing. Added OBJ_OBSERVER module/type.
291  * Restructured HUD_config structs/flags.
292  * 
293  * 35    10/25/97 4:02p Lawrance
294  * took out unused hud_message struct members
295  * 
296  * 34    10/02/97 9:53p Hoffoss
297  * Added event evaluation analysis debug screen so we can determine the
298  * state of events and their sexp trees to track down logic problems and
299  * such.
300  * 
301  * 33    9/17/97 5:12p John
302  * Restructured collision routines.  Probably broke a lot of stuff.
303  * 
304  * 32    9/08/97 12:01p Lawrance
305  * when re-using HUD scrollback entries, ensure memory gets free'ed
306  * properly
307  * 
308  * 31    8/31/97 6:38p Lawrance
309  * pass in frametime to do_frame loop
310  * 
311  * 30    8/22/97 10:03a Lawrance
312  * fix exception that occurred when hud scrollback was selected from the
313  * main menu
314  * 
315  * 29    6/23/97 12:03p Lawrance
316  * move split_str() to Parselo
317  * 
318  * 28    6/17/97 12:25p Lawrance
319  * HUD message lines are split into multiple lines when they exceed
320  * display width
321  * 
322  * 27    6/12/97 10:23a John
323  * added new colors to freespace.  made most menus display the background
324  * bitmap rather than dull black screen.
325  * 
326  * 26    6/11/97 1:12p John
327  * Started fixing all the text colors in the game.
328  * 
329  * 25    4/22/97 3:14p Lawrance
330  * only free HUD scrollback messages if they exist
331  * 
332  * 24    4/15/97 1:26p Lawrance
333  * using a static array of nodes to store hud scrollback messages, storage
334  * for text is dynamic
335  * 
336  * 23    4/14/97 9:55a Mike
337  * Fixed HUD message system.
338  * Better game sequencing.
339  * 
340  * 22    1/28/97 5:33p Lawrance
341  * saving number of msg window lines in save game and player file
342  * 
343  * 21    1/28/97 4:59p Lawrance
344  * allowing number of lines on hud message bar to be configured
345  * 
346  * 20    1/24/97 9:47a Lawrance
347  * made number of message lines in HUD message area confiurable
348  * 
349  * 19    1/22/97 10:56a Lawrance
350  * added check for NULL after malloc()
351  * 
352  * 18    1/07/97 5:36p Lawrance
353  * Enabled save/restore for  old/present/pending hud messages
354  * 
355  * 17    12/10/96 12:28p Lawrance
356  * adding new offscreen target indicator
357  * 
358  * 16    12/08/96 1:54a Lawrance
359  * integrating hud configuration
360  * 
361  * 15    11/29/96 6:12p Lawrance
362  * took out duplicate include of timer.h
363  * 
364  * 14    11/29/96 11:17a Lawrance
365  * added comments, put check in for zero-length HUD messages in the
366  * scrollback
367  * 
368  * 13    11/28/96 6:27p Lawrance
369  * added some additional comments
370  * 
371  * 12    11/27/96 3:20p Lawrance
372  * added scroll-back message code
373  * 
374  * 11    11/22/96 1:00p Lawrance
375  * fixed bug when a key held down and created tons of messages
376  * 
377  * 10    11/22/96 12:35p Lawrance
378  * 
379  * 9     11/20/96 11:51a Lawrance
380  * trying out losing the HUD message shaded area
381  * 
382  * 8     11/19/96 4:46p Lawrance
383  * fixed problem when too many messages were to be displayed at once
384  * 
385  * 7     11/19/96 3:55p Lawrance
386  * modifed HUD message details (scroll speed, fade time etc)
387  * 
388  * 6     11/19/96 10:16a Lawrance
389  * changing to new use of color scheme
390  * 
391  * 5     11/17/96 5:28p Lawrance
392  * using HUD color globals instead of hard-coded numbers
393  * 
394  * 4     11/15/96 11:46a Lawrance
395  * tweaked size of HUD message bar to prevent clipping of g's etc on the
396  * bottom line
397  * 
398  * 3     11/15/96 11:39a Lawrance
399  * got HUD messages scrolling
400  * 
401  * 2     11/15/96 12:11a Lawrance
402  * HUD message bar working
403  *
404  * $NoKeywords: $
405  *
406 */
407
408 #include <stdlib.h>
409 #include <stdarg.h>
410
411 #include "hud.h"
412 #include "hudmessage.h"
413 #include "hudtarget.h"
414 #include "freespace.h"
415 #include "gamesequence.h"
416 #include "2d.h"
417 #include "key.h"
418 #include "timer.h"
419 #include "math.h"
420 #include "mouse.h"
421 #include "player.h"
422 #include "linklist.h"
423 #include "missionlog.h"
424 #include "ui.h"
425 #include "missionscreencommon.h"
426 #include "bmpman.h"
427 #include "font.h"
428 #include "gamesnd.h"
429 #include "multi.h"
430 #include "missiongoals.h"
431 #include "alphacolors.h"
432 #include "beam.h"
433 #include "audiostr.h"
434
435 /* replaced with those static ints that follow
436 #define LIST_X          46
437 #define LIST_X2 108  // second column x start position
438 #define LIST_Y          60
439 #define LIST_W          558  // total width including both columns
440 #define LIST_W2 (LIST_W + LIST_X - LIST_X2)  // width of second column
441 #define LIST_H          297
442 #define LIST_H_O        275  // applies only to objectives mode
443 */
444
445 // 1st column, width includes both columns
446 static int Hud_mission_log_list_coords[GR_NUM_RESOLUTIONS][4] = {
447         {
448                 46,60,558,269           // GR_640
449         },
450         {
451                 74,96,558,297           // GR_1024
452         }
453 };
454
455 // 2nd column, width is just of second column
456 static int Hud_mission_log_list2_coords[GR_NUM_RESOLUTIONS][4] = {
457         {
458                 108, 60, 496, 297               // GR_640
459         },
460         {
461                 136, 96, 496, 436               // GR_1024
462         }
463 };
464
465 static int Hud_mission_log_list_objective_x_coord[GR_NUM_RESOLUTIONS] = {
466         275,    // GR_640
467         440     // GR_1024
468 };
469
470 static int Hud_mission_log_time_coords[GR_NUM_RESOLUTIONS][2] = {
471         {
472                 41, 372 // GR_640
473         },
474         {
475                 66, 595 // GR_1024
476         }
477 };
478
479 static int Hud_mission_log_time2_coords[GR_NUM_RESOLUTIONS][2] = {
480         {
481                 103, 372        // GR_640
482         },
483         {
484                 128, 595        // GR_1024
485         }
486 };
487
488
489 #define SCROLLBACK_MODE_MSGS_LOG                0
490 #define SCROLLBACK_MODE_EVENT_LOG       1
491 #define SCROLLBACK_MODE_OBJECTIVES      2
492
493 #define NUM_BUTTONS                     6
494
495 #define SCROLL_UP_BUTTON        0
496 #define SCROLL_DOWN_BUTTON      1
497 #define SHOW_MSGS_BUTTON        2
498 #define SHOW_EVENTS_BUTTON      3
499 #define SHOW_OBJS_BUTTON        4
500 #define ACCEPT_BUTTON           5
501
502 #define HUD_MESSAGE_TOTAL_LIFE  14000   // total time a HUD message is alive (in milliseconds)
503
504 #define HUD_MSG_LENGTH_MAX              2048
505 //#define HUD_MSG_MAX_PIXEL_W   439     // maximum number of pixels wide message display area is
506 //#define HUD_MSG_MAX_PIXEL_W   619     // maximum number of pixels wide message display area is
507
508 /* // No Longer Used - DDOI
509 static int Hud_mission_log_status_coords[GR_NUM_RESOLUTIONS][2] = {
510         {
511                 170, 339                // GR_640
512         },
513         {
514                 361, 542                // GR_1024
515         }
516 };
517 */
518
519 struct scrollback_buttons {
520         char *filename;
521         int x, y;
522         int xt, yt;
523         int hotspot;
524         UI_BUTTON button;  // because we have a class inside this struct, we need the constructor below..
525
526         scrollback_buttons(char *name, int x1, int y1, int x2, int y2, int h) : filename(name), x(x1), y(y1), xt(x2), yt(y2), hotspot(h) {}
527 };
528
529 int Scroll_time_id;
530
531 int MSG_WINDOW_X_START = 5;
532 int MSG_WINDOW_Y_START = 5;
533 int MSG_WINDOW_WIDTH;           // initialed in hud_init_msg_window()
534 int MSG_WINDOW_HEIGHT;
535 int MSG_WINDOW_FONT_HEIGHT;
536 int ACTIVE_BUFFER_LINES = 4;                            // number of HUD messages that can be shown at one time + 1
537 int OLD_ACTIVE_BUFFER_LINES;
538
539 int Hud_list_start;                     // points to the next msg to be printed in the queue
540 int Hud_list_end;                               // points to the last msg in the queue
541
542 int Active_index=0;
543 int Scroll_needed=0;
544 int Scroll_in_progress=0;
545
546 HUD_message_data HUD_pending[SCROLL_BUFFER_LINES];
547 Hud_display_info HUD_active_msgs_list[MAX_ACTIVE_BUFFER_LINES];
548
549 int HUD_msg_inited = FALSE;
550
551 // There is a maximum number of lines that will be stored in the message scrollback.  Oldest
552 // messages are deleted to make way for newest messages.
553 #define MAX_MSG_SCROLLBACK_LINES        100
554 line_node Msg_scrollback_lines[MAX_MSG_SCROLLBACK_LINES];
555
556 line_node Msg_scrollback_free_list;
557 line_node Msg_scrollback_used_list;
558
559 #define MAX_HUD_FT      1
560
561 typedef struct HUD_ft {
562         int     end_time;                                               //      Timestamp at which this message will go away.
563         char    text[MAX_HUD_LINE_LEN];         //      Text to display.
564         int     color;                                                  //      0rgb color, 8 bit fields.
565 } HUD_ft;
566
567 HUD_ft HUD_fixed_text[MAX_HUD_FT];
568
569 static int Num_obj_lines;
570 static int Scroll_offset;
571 static int Scroll_max;
572 static int Scrollback_mode = SCROLLBACK_MODE_OBJECTIVES;
573 // static int Status_bitmap;
574 static int Background_bitmap;
575 static UI_WINDOW Ui_window;
576
577 static char* Hud_mission_log_fname[GR_NUM_RESOLUTIONS] = {
578         "MissionLog",           // GR_640
579         "2_MissionLog"          // GR_1024
580 };
581
582 /* // No longer used - DDOI
583 static char* Hud_mission_log_status_fname[GR_NUM_RESOLUTIONS] = {
584         "MLStatus",             // GR_640
585         "MLStatus"              // GR_1024
586 };
587 */
588
589 static char* Hud_mission_log_mask_fname[GR_NUM_RESOLUTIONS] = {
590         "MissionLog-m",         // GR_640
591         "2_MissionLog-m"                // GR_1024
592 };
593
594 static scrollback_buttons Buttons[GR_NUM_RESOLUTIONS][NUM_BUTTONS] = {
595         {       // GR_640
596         //XSTR:OFF
597                 scrollback_buttons("LB_00",     1,              67,     -1,     -1,     0),
598                 scrollback_buttons("LB_01",     1,              307,    -1,     -1,     1),
599                 scrollback_buttons("LB_02",     111,    376,    108,    413,    2),
600                 scrollback_buttons("LB_03",     209,    376,    205,    413,    3),
601                 scrollback_buttons("LB_04",     12,     376,    7,              413,    4),
602                 scrollback_buttons("CB_05a",    571,    425,    564,    413,    5)
603         //XSTR:ON
604         },
605         {       // GR_1024
606         //XSTR:OFF
607                 scrollback_buttons("2_LB_00",   1,              108,    -1,     -1,     0),
608                 scrollback_buttons("2_LB_01",   1,              492,    -1,     -1,     1),
609                 scrollback_buttons("2_LB_02",   177,    602,    173,    661,    2),
610                 scrollback_buttons("2_LB_03",   335,    602,    335,    661,    3),
611                 scrollback_buttons("2_LB_04",   20,     602,    11,     661,    4),
612                 scrollback_buttons("2_CB_05a",914,      681,    946,    661,    5)
613         //XSTR:ON
614         }
615 }
616 ;
617
618 // ----------------------------------------------------------------------
619 // HUD_init_fixed_text()
620 //
621 void HUD_init_fixed_text()
622 {
623         int     i;
624
625         for (i=0; i<MAX_HUD_FT; i++)
626                 HUD_fixed_text[i].end_time = timestamp(0);
627 }
628
629 // ----------------------------------------------------------------------
630 // hud_init_msg_window()
631 //
632 // Called from HUD_init(), which is called from game_level_init()
633 //
634 void hud_init_msg_window()
635 {
636         int i, h;
637
638         MSG_WINDOW_WIDTH = gr_screen.clip_width - 20;
639
640         Hud_list_start = 0;
641         Hud_list_end = 0;
642
643         for (i=0; i<SCROLL_BUFFER_LINES; i++)
644                 HUD_pending[i].text[0] = HUD_pending[i].text[MAX_HUD_LINE_LEN - 1] = 0;
645         
646         for ( i=0; i < MAX_ACTIVE_BUFFER_LINES; i++ ) {
647                 HUD_active_msgs_list[i].total_life = 1;
648         }
649
650         Scroll_time_id = 1;
651
652         // determine the height of the msg window, which depends on the font height     
653         gr_set_font(FONT1);
654         h = gr_get_font_height();
655
656         //ACTIVE_BUFFER_LINES = Players[Player_num].HUD_config.num_msg_window_lines;
657 //      ACTIVE_BUFFER_LINES = HUD_config.num_msg_window_lines;
658         ACTIVE_BUFFER_LINES = 4;
659
660         MSG_WINDOW_FONT_HEIGHT = h;
661         MSG_WINDOW_HEIGHT = MSG_WINDOW_FONT_HEIGHT * (ACTIVE_BUFFER_LINES-1);
662
663         // starting a mission, free the scroll-back buffers, but only if we've been
664         // through this function once already
665         if ( HUD_msg_inited == TRUE ) {
666                 hud_free_scrollback_list();
667         }
668
669         list_init( &Msg_scrollback_free_list );
670         list_init( &Msg_scrollback_used_list );
671
672         // first slot is reserved for dummy node
673         for (i=1; i < MAX_MSG_SCROLLBACK_LINES; i++)    {
674                 Msg_scrollback_lines[i].text = NULL;
675                 list_append(&Msg_scrollback_free_list, &Msg_scrollback_lines[i]);
676         }
677
678         Active_index=0;
679         Scroll_needed=0;
680         Scroll_in_progress=0;
681
682         OLD_ACTIVE_BUFFER_LINES = ACTIVE_BUFFER_LINES;
683
684         HUD_init_fixed_text();
685         HUD_msg_inited = TRUE;
686 }
687
688 // ---------------------------------------------------------------------------------------
689 // hud_show_msg_window() will display the active HUD messages on the HUD.  It will scroll
690 // the messages up when a new message arrives.  
691 //
692 void hud_show_msg_window()
693 {
694         int i, index;
695
696         hud_set_default_color();
697         gr_set_font(FONT1);
698
699         HUD_set_clip(MSG_WINDOW_X_START,MSG_WINDOW_Y_START, MSG_WINDOW_WIDTH, MSG_WINDOW_HEIGHT+2);
700
701         if ( OLD_ACTIVE_BUFFER_LINES != ACTIVE_BUFFER_LINES ) {
702                 // the size of the message window has changed, the best thing to do is to put all
703                 // the blank out the current hud messages.  There is no need to add them to the 
704                 // scrollback buffer, since they are already there!
705         
706                 for ( i=0; i < ACTIVE_BUFFER_LINES; i++ ) {
707                         if ( !timestamp_elapsed(HUD_active_msgs_list[i].total_life) ) {
708                                 HUD_active_msgs_list[i].total_life = 1;
709                                 Active_index=0;
710                         }
711                 }
712         }
713         
714         OLD_ACTIVE_BUFFER_LINES = ACTIVE_BUFFER_LINES;
715
716         // check if there is a message to display on the HUD, and if there is room to display it
717         if ( Hud_list_start != Hud_list_end && !Scroll_needed) {
718
719                 Hud_list_start++;
720
721                 // if the pointer exceeds the array size, wrap around to element 1.  element 0 is not used.             
722                 if (Hud_list_start >= SCROLL_BUFFER_LINES)
723                         Hud_list_start = 1;
724
725                 HUD_active_msgs_list[Active_index].msg = HUD_pending[Hud_list_start];
726                 HUD_active_msgs_list[Active_index].total_life = timestamp(HUD_MESSAGE_TOTAL_LIFE);
727
728                 for (i=Active_index+1; i < Active_index+ACTIVE_BUFFER_LINES; i++) {
729                         index = i % ACTIVE_BUFFER_LINES;
730
731                         // determine if there are any existing messages, if so need to scroll them up
732
733                         if ( !timestamp_elapsed(HUD_active_msgs_list[index].total_life) ) {
734                                 HUD_active_msgs_list[index].target_y -=  MSG_WINDOW_FONT_HEIGHT;
735                                 Scroll_needed=1;
736                         }
737
738                 }
739
740                 if (Scroll_needed) {
741                         HUD_active_msgs_list[Active_index].y = (ACTIVE_BUFFER_LINES-1)*MSG_WINDOW_FONT_HEIGHT;
742                         HUD_active_msgs_list[Active_index].target_y = HUD_active_msgs_list[Active_index].y - MSG_WINDOW_FONT_HEIGHT;
743                 }
744                 else {
745                         HUD_active_msgs_list[Active_index].y = (ACTIVE_BUFFER_LINES-2)*MSG_WINDOW_FONT_HEIGHT;
746                         HUD_active_msgs_list[Active_index].target_y = HUD_active_msgs_list[Active_index].y;
747                 }
748
749                 Active_index++;
750                 if (Active_index >= ACTIVE_BUFFER_LINES) Active_index = 0;
751
752                 if (Hud_list_end == Hud_list_start) {   // just printed the last msg
753                         Hud_list_start = Hud_list_end = 0;
754                 }
755         }
756
757         Scroll_in_progress=0;
758         Scroll_needed = 0;
759
760         for ( i=0; i < ACTIVE_BUFFER_LINES; i++ ) {
761
762                 if ( !timestamp_elapsed(HUD_active_msgs_list[i].total_life) ) {
763
764                         if (HUD_active_msgs_list[i].y > HUD_active_msgs_list[i].target_y) {
765                                 Scroll_needed=1;
766                                 if (timestamp_elapsed(Scroll_time_id) ){
767                                         HUD_active_msgs_list[i].y -= SCROLL_STEP_SIZE;
768                                         if (HUD_active_msgs_list[i].y < HUD_active_msgs_list[i].target_y)
769                                                 HUD_active_msgs_list[i].y = HUD_active_msgs_list[i].target_y;
770
771                                         Scroll_in_progress=1;
772                                 }
773
774                         }
775
776                         if ( hud_gauge_active(HUD_MESSAGE_LINES) ) {
777                                 if ( !(Player->flags & PLAYER_FLAGS_MSG_MODE) ) {
778                                         // set the appropriate color                                    
779                                         if(HUD_active_msgs_list[i].msg.source){
780                                                 hud_set_gauge_color(HUD_MESSAGE_LINES, HUD_C_BRIGHT);
781                                         } else {
782                                                 hud_set_gauge_color(HUD_MESSAGE_LINES);
783                                         }
784
785                                         // print the message out
786                                         gr_printf(MSG_WINDOW_X_START + HUD_active_msgs_list[i].msg.x - 2, HUD_active_msgs_list[i].y, "%s", HUD_active_msgs_list[i].msg.text);
787                                 }
788                         }
789                 }
790
791         } // end for
792
793         if (Scroll_in_progress)
794                 Scroll_time_id = timestamp(SCROLL_TIME);
795
796         HUD_reset_clip();
797 }
798
799 void hud_show_fixed_text()
800 {
801         HUD_ft  *hp;
802
803         hp = &HUD_fixed_text[0];
804
805         if (!timestamp_elapsed(hp->end_time)) {
806                 //gr_set_color((hp->color >> 16) & 0xff, (hp->color >> 8) & 0xff, hp->color & 0xff);
807                 gr_printf(0x8000, MSG_WINDOW_Y_START + MSG_WINDOW_HEIGHT + 8, hp->text);
808         }
809 }
810
811 //      Similar to HUD printf, but shows only one message at a time, at a fixed location.
812 void HUD_fixed_printf(float duration, char * format, ...)
813 {
814         va_list args;
815         char            tmp[HUD_MSG_LENGTH_MAX];
816         int             msg_length;
817
818         // make sure we only print these messages if we're in the correct state
819         if((Game_mode & GM_MULTIPLAYER) && (Netgame.game_state != NETGAME_STATE_IN_MISSION)){
820                 nprintf(("Network","HUD_fixed_printf bailing because not in multiplayer game play state\n"));
821                 return;
822         }
823
824         va_start(args, format);
825         vsprintf(tmp, format, args);
826         va_end(args);
827
828         msg_length = strlen(tmp);
829         Assert(msg_length < HUD_MSG_LENGTH_MAX);        //      If greater than this, probably crashed anyway.
830
831         if ( !msg_length ) {
832                 nprintf(("Warning", "HUD_fixed_printf ==> attempt to print a 0 length string in msg window\n"));
833                 return;
834
835         } else if (msg_length > MAX_HUD_LINE_LEN - 1){
836                 nprintf(("Warning", "HUD_fixed_printf ==> Following string truncated to %d chars: %s\n",MAX_HUD_LINE_LEN,tmp));
837         }
838
839         if (duration == 0.0f){
840                 HUD_fixed_text[0].end_time = timestamp(-1);
841         } else {
842                 HUD_fixed_text[0].end_time = timestamp((int) (1000.0f * duration));
843         }
844
845         strncpy(HUD_fixed_text[0].text, tmp, MAX_HUD_LINE_LEN - 1);
846         HUD_fixed_text[0].color = 0xff0000;
847 }
848
849 //      Clear all pending text.
850 void HUD_fixed_printf_reset()
851 {
852         HUD_init_fixed_text();
853 }
854
855
856 // --------------------------------------------------------------------------------------
857 // HUD_printf_line() 
858 //
859 //      Print a single line of text to the HUD.  We know that the text will fit on the screen,
860 // since that was taken care of in HUD_printf();
861 //
862 void HUD_printf_line(char *text, int source, int time = 0, int x = 0)
863 {
864         Assert(text != NULL);
865
866         // if the pointer exceeds the array size, wrap around to element 1.  element 0 is not used.             
867         Hud_list_end++;
868         if (Hud_list_end >= SCROLL_BUFFER_LINES)
869                 Hud_list_end = 1;
870
871         if (Hud_list_end == Hud_list_start) {
872                 nprintf(("Warning", "HUD ==> Exceeded msg scroll buffer, discarding message %s\n", text));
873                 Hud_list_end--;
874                 if (Hud_list_end == 0)
875                         Hud_list_end = SCROLL_BUFFER_LINES - 1;
876                 return;
877         }
878
879         if ( strlen(text) > MAX_HUD_LINE_LEN - 1 ){
880                 nprintf(("Warning", "HUD_printf_line() ==> Following string truncated to %d chars: %s\n", MAX_HUD_LINE_LEN, text));
881         }
882
883         strncpy(HUD_pending[Hud_list_end].text, text, MAX_HUD_LINE_LEN - 1);
884         HUD_pending[Hud_list_end].text[MAX_HUD_LINE_LEN - 1] = 0;
885         HUD_pending[Hud_list_end].source = source;
886         HUD_pending[Hud_list_end].time = time;
887         HUD_pending[Hud_list_end].x = x;
888 }
889
890 // converts a TEAM_* define to a HUD_SOURCE_* define
891 int HUD_get_team_source(int team)
892 {
893         switch (team) {
894                 case TEAM_FRIENDLY:
895                         return HUD_SOURCE_FRIENDLY;
896
897                 case TEAM_HOSTILE:
898                         return HUD_SOURCE_HOSTILE;
899
900                 case TEAM_NEUTRAL:
901                         return HUD_SOURCE_NEUTRAL;
902
903                 case TEAM_UNKNOWN:
904                         return HUD_SOURCE_UNKNOWN;
905
906                 case TEAM_TRAITOR:
907                         return HUD_SOURCE_TRAITOR;
908         }
909
910         nprintf(("warning", "Unknown TEAM_* define used! (%d)", team));
911         return 0;
912 }
913
914 void HUD_printf(char *format, ...)
915 {
916         va_list args;
917         char tmp[HUD_MSG_LENGTH_MAX];
918         int len;
919
920         // make sure we only print these messages if we're in the correct state
921         if((Game_mode & GM_MULTIPLAYER) && (Net_player->state != NETPLAYER_STATE_IN_MISSION)){
922                 nprintf(("Network","HUD_printf bailing because not in multiplayer game play state\n"));
923                 return;
924         }
925
926         va_start(args, format);
927         vsprintf(tmp, format, args);
928         va_end(args);
929
930         len = strlen(tmp);
931         Assert(len < HUD_MSG_LENGTH_MAX);       //      If greater than this, probably crashed anyway.
932         hud_sourced_print(HUD_SOURCE_COMPUTER, tmp);
933 }
934
935 void HUD_ship_sent_printf(int sh, char *format, ...)
936 {
937         va_list args;
938         char tmp[HUD_MSG_LENGTH_MAX];
939         int len;
940
941         sprintf(tmp, NOX("%s: "), Ships[sh].ship_name);
942         len = strlen(tmp);
943         Assert(len < HUD_MSG_LENGTH_MAX);
944
945         va_start(args, format);
946         vsprintf(tmp + len, format, args);
947         va_end(args);
948
949         len = strlen(tmp);
950         Assert(len < HUD_MSG_LENGTH_MAX);       //      If greater than this, probably crashed anyway.
951         hud_sourced_print(HUD_get_team_source(Ships[sh].team), tmp);
952 }
953
954 // --------------------------------------------------------------------------------------
955 // HUD_sourced_printf() 
956 //
957 // HUD_sourced_printf() has the same parameters as printf(), but displays the text as a scrolling
958 // message on the HUD.  Text is split into multiple lines if width exceeds msg display area
959 // width.  'source' is used to indicate who send the message, and is used to color code text.
960 //
961 void HUD_sourced_printf(int source, char *format, ...)
962 {
963         va_list args;
964         char tmp[HUD_MSG_LENGTH_MAX];
965
966         // make sure we only print these messages if we're in the correct state
967         if((Game_mode & GM_MULTIPLAYER) && (Net_player->state != NETPLAYER_STATE_IN_MISSION)){
968                 nprintf(("Network","HUD_sourced_printf bailing because not in multiplayer game play state\n"));
969                 return;
970         }
971         
972         va_start(args, format);
973         vsprintf(tmp, format, args);
974         va_end(args);
975         Assert(strlen(tmp) < HUD_MSG_LENGTH_MAX);       //      If greater than this, probably crashed anyway.
976         hud_sourced_print(source, tmp);
977 }
978
979 void hud_sourced_print(int source, char *msg)
980 {
981         char *ptr, *str;
982         //char *src_str, *msg_str;
983         int sw, t, x, offset = 0;
984         //int fudge = (gr_screen.res == GR_640) ? 15 : 50;              // prevents string from running off screen
985
986         if ( !strlen(msg) ) {
987                 nprintf(("Warning", "HUD ==> attempt to print a 0 length string in msg window\n"));
988                 return;
989         }
990
991         // add message to the scrollback log first
992         hud_add_msg_to_scrollback(msg, source, timestamp());
993
994         ptr = strstr(msg, NOX(": ")) + 2;
995         if (ptr) {
996                 gr_get_string_size(&sw, NULL, msg, ptr - msg);                  // get width of the speaker field
997                 //if (sw < MSG_WINDOW_WIDTH - 20)
998                 offset = sw;
999         }
1000
1001         x = 0;
1002         t = timestamp();
1003         str = msg;
1004         while ((ptr = split_str_once(str, MSG_WINDOW_WIDTH - x - 7)) != NULL) {         // the 7 is a fudge hack
1005                 HUD_printf_line(str, source, t, x);
1006                 str = ptr;
1007                 x = offset;
1008                 t = 0;
1009         }
1010
1011         HUD_printf_line(str, source, t, x);
1012 }
1013
1014 int hud_query_scrollback_size()
1015 {
1016         int count = 0, y_add = 0;
1017         int font_height = gr_get_font_height();
1018         line_node *ptr;
1019
1020         if (EMPTY(&Msg_scrollback_used_list) || !HUD_msg_inited)
1021                 return 0;
1022
1023         ptr = GET_FIRST(&Msg_scrollback_used_list);
1024         while (ptr != END_OF_LIST(&Msg_scrollback_used_list)) {
1025                 if (ptr->source != HUD_SOURCE_HIDDEN) {
1026                         y_add = ptr->y;
1027                         count += font_height + ptr->y;
1028                 }
1029
1030                 ptr = GET_NEXT(ptr);
1031         }
1032
1033         count -= y_add;
1034         return count;
1035 }
1036
1037 // add text directly to the hud scrollback log, without displaying on the hud
1038 void HUD_add_to_scrollback(char *text, int source)
1039 {
1040         if (!strlen(text)) {
1041                 nprintf(("Warning", "HUD ==> attempt to print a 0 length string in msg window\n"));
1042                 return;
1043         }
1044
1045         hud_add_msg_to_scrollback(text, source, timestamp());
1046 }
1047
1048 // hud_add_msg_to_scrollback() adds the new_msg to the scroll-back message list.  If there
1049 // are no more free slots, the first slot is released to make room for the new message.
1050 //
1051 void hud_add_line_to_scrollback(char *text, int source, int t, int x, int y, int underline_width)
1052 {
1053         line_node *new_line;
1054
1055         Assert(HUD_msg_inited);
1056         if (!text || !strlen(text))
1057                 return;
1058
1059         if ( EMPTY(&Msg_scrollback_free_list) ) {
1060                 new_line = GET_FIRST(&Msg_scrollback_used_list);
1061                 list_remove(&Msg_scrollback_used_list, new_line);
1062                 free(new_line->text);
1063
1064         } else {
1065                 new_line = GET_FIRST(&Msg_scrollback_free_list);
1066                 list_remove(&Msg_scrollback_free_list, new_line);
1067         }
1068
1069         new_line->x = x;
1070         new_line->y = y;
1071         new_line->underline_width = underline_width;
1072         new_line->time = t;
1073         new_line->source = source;
1074         new_line->text = (char *) malloc( strlen(text) + 1 );
1075         strcpy(new_line->text, text);
1076         list_append(&Msg_scrollback_used_list, new_line);
1077 }
1078
1079 void hud_add_msg_to_scrollback(char *text, int source, int t)
1080 {
1081         char buf[HUD_MSG_LENGTH_MAX], *ptr, *str;
1082         int msg_len, w, max_width, x, offset = 0;
1083
1084         max_width = Hud_mission_log_list2_coords[gr_screen.res][2];
1085         msg_len = strlen(text);
1086         if (msg_len == 0)
1087                 return;
1088
1089         w = 0;
1090         Assert(msg_len < HUD_MSG_LENGTH_MAX);
1091         strcpy(buf, text);
1092         ptr = strstr(buf, NOX(": "));
1093         if (ptr) {
1094                 gr_get_string_size(&w, NULL, buf, ptr - buf);
1095         }
1096
1097 //      if (ptr) {
1098 //              gr_get_string_size(&w, NULL, buf, ptr - buf + 2);
1099 //              if (w < max_width - 20)
1100 //                      offset = w;
1101 //      }
1102
1103         x = 0;
1104         str = buf;
1105         while ((ptr = split_str_once(str, max_width - x)) != NULL) {
1106                 hud_add_line_to_scrollback(str, source, t, x, 1, w);
1107                 str = ptr;
1108                 x = offset;
1109                 t = w = 0;
1110         }
1111
1112         hud_add_line_to_scrollback(str, source, t, x, 3, w);
1113 }
1114
1115 // hud_free_scrollback_list() will free the memory that was allocated to store the messages
1116 // for the scroll-back list
1117 //
1118 void hud_free_scrollback_list()
1119 {
1120         line_node *A;
1121
1122         // check if the list has been inited yet.  If not, return with doing nothing.
1123         if ( Msg_scrollback_used_list.next == NULL || Msg_scrollback_used_list.prev == NULL )
1124                 return;
1125
1126         A = GET_FIRST(&Msg_scrollback_used_list);
1127         while( A !=END_OF_LIST(&Msg_scrollback_used_list) )     {
1128                 if ( A->text != NULL ) {
1129                         free(A->text);
1130                         A->text = NULL;
1131                 }
1132
1133                 A = GET_NEXT(A);
1134         }
1135 }
1136
1137 // how many lines to skip
1138 int hud_get_scroll_max_pos()
1139 {
1140         int max = 0, font_height = gr_get_font_height();
1141
1142         if (Scrollback_mode == SCROLLBACK_MODE_MSGS_LOG) {
1143                 int count = 0;
1144                 line_node *ptr;
1145                 // number of pixels in excess of what can be displayed
1146                 int excess = Scroll_max - Hud_mission_log_list_coords[gr_screen.res][3];
1147
1148                 if (EMPTY(&Msg_scrollback_used_list) || !HUD_msg_inited) {
1149                         max = 0;
1150
1151                 } else {
1152                         ptr = GET_FIRST(&Msg_scrollback_used_list);
1153                         while (ptr != END_OF_LIST(&Msg_scrollback_used_list)) {
1154                                 if (ptr->source != HUD_SOURCE_HIDDEN) {
1155
1156                                         if (excess > 0) {
1157                                                 excess -= font_height;
1158                                                 count++;
1159                                         }
1160
1161                                         if (excess <= 0) {
1162                                                 max = count;
1163                                                 break;
1164                                         }
1165
1166                                         // spacing between lines
1167                                         excess -= ptr->y;
1168
1169                                 }
1170
1171                                 ptr = GET_NEXT(ptr);
1172                         }
1173                 }
1174
1175         } else {
1176                 max = (Scroll_max - Hud_mission_log_list_coords[gr_screen.res][3]) / font_height;
1177         }
1178
1179         if (max < 0)
1180                 max = 0;
1181
1182         return max;
1183 }
1184
1185 void hud_scroll_reset()
1186 {
1187         if (Scrollback_mode == SCROLLBACK_MODE_OBJECTIVES) {
1188                 Scroll_offset = 0;
1189
1190         } else {
1191                 Scroll_offset = hud_get_scroll_max_pos();
1192         }
1193 }
1194
1195 void hud_scroll_list(int dir)
1196 {
1197         if (dir) {
1198                 if (Scroll_offset) {
1199                         Scroll_offset--;
1200                         gamesnd_play_iface(SND_SCROLL);
1201
1202                 } else
1203                         gamesnd_play_iface(SND_GENERAL_FAIL);
1204
1205         } else {
1206                 if (Scroll_offset < hud_get_scroll_max_pos()) {
1207                         Scroll_offset++;
1208                         gamesnd_play_iface(SND_SCROLL);
1209
1210                 } else
1211                         gamesnd_play_iface(SND_GENERAL_FAIL);
1212         }
1213 }
1214
1215 void hud_goto_pos(int delta)
1216 {
1217         int pos=0, font_height = gr_get_font_height();
1218
1219         if (Scrollback_mode == SCROLLBACK_MODE_MSGS_LOG) {
1220                 int count = 0, y = 0;
1221                 line_node *ptr;
1222
1223                 if (EMPTY(&Msg_scrollback_used_list) || !HUD_msg_inited)
1224                         return;
1225
1226                 ptr = GET_FIRST(&Msg_scrollback_used_list);
1227                 while (ptr != END_OF_LIST(&Msg_scrollback_used_list)) {
1228                         if (ptr->source != HUD_SOURCE_HIDDEN) {
1229                                 if (count == Scroll_offset) {
1230                                         pos = y;
1231                                         break;
1232                                 }
1233
1234                                 y += font_height + ptr->y;
1235                                 count++;
1236                         }
1237
1238                         ptr = GET_NEXT(ptr);
1239                 }
1240
1241                 Scroll_offset = count = y = 0;
1242                 ptr = GET_FIRST(&Msg_scrollback_used_list);
1243                 while (ptr != END_OF_LIST(&Msg_scrollback_used_list)) {
1244                         if (ptr->source != HUD_SOURCE_HIDDEN) {
1245                                 if (y <= pos + delta)
1246                                         Scroll_offset = count;
1247
1248                                 y += font_height + ptr->y;
1249                                 count++;
1250                         }
1251
1252                         ptr = GET_NEXT(ptr);
1253                 }
1254
1255         } else {
1256                 pos = Scroll_offset * font_height;
1257                 pos += delta;
1258                 Scroll_offset = pos / font_height;
1259         }
1260 }
1261
1262 void hud_page_scroll_list(int dir)
1263 {
1264         int max = hud_get_scroll_max_pos();
1265
1266         if (dir) {
1267                 if (Scroll_offset) {
1268                         hud_goto_pos(-Hud_mission_log_list_coords[gr_screen.res][3]);
1269                         if (Scroll_offset < 0)
1270                                 Scroll_offset = 0;
1271
1272                         gamesnd_play_iface(SND_SCROLL);
1273
1274                 } else
1275                         gamesnd_play_iface(SND_GENERAL_FAIL);
1276
1277         } else {
1278                 if (Scroll_offset < max) {
1279                         hud_goto_pos(Hud_mission_log_list_coords[gr_screen.res][3]);
1280                         if (Scroll_offset > max)
1281                                 Scroll_offset = max;
1282
1283                         gamesnd_play_iface(SND_SCROLL);
1284
1285                 } else
1286                         gamesnd_play_iface(SND_GENERAL_FAIL);
1287         }
1288 }
1289
1290 void hud_scrollback_button_pressed(int n)
1291 {
1292         switch (n) {
1293                 case SCROLL_UP_BUTTON:
1294                         hud_scroll_list(1);
1295                         break;
1296
1297                 case SCROLL_DOWN_BUTTON:
1298                         hud_scroll_list(0);
1299                         break;
1300
1301                 case SHOW_MSGS_BUTTON:
1302                         Scrollback_mode = SCROLLBACK_MODE_MSGS_LOG;
1303                         Scroll_max = hud_query_scrollback_size();
1304                         hud_scroll_reset();
1305                         break;
1306
1307                 case SHOW_EVENTS_BUTTON:
1308                         Scrollback_mode = SCROLLBACK_MODE_EVENT_LOG;
1309                         Scroll_max = Num_log_lines * gr_get_font_height();
1310                         hud_scroll_reset();
1311                         break;
1312
1313                 case SHOW_OBJS_BUTTON:
1314                         Scrollback_mode = SCROLLBACK_MODE_OBJECTIVES;
1315                         Scroll_max = Num_obj_lines * gr_get_font_height();
1316                         Scroll_offset = 0;
1317                         break;
1318
1319                 case ACCEPT_BUTTON:
1320                         hud_scrollback_exit();                  
1321                         break;
1322         }
1323 }
1324
1325 void hud_scrollback_init()
1326 {
1327         int i;
1328         scrollback_buttons *b;
1329
1330         // pause all game sounds
1331         beam_pause_sounds();
1332         audiostream_pause_all();
1333
1334         common_set_interface_palette("BriefingPalette");  // set the interface palette
1335         Ui_window.create(0, 0, gr_screen.max_w, gr_screen.max_h, 0);
1336         Ui_window.set_mask_bmap(Hud_mission_log_mask_fname[gr_screen.res]);
1337
1338         for (i=0; i<NUM_BUTTONS; i++) {
1339                 b = &Buttons[gr_screen.res][i];
1340
1341                 b->button.create(&Ui_window, "", b->x, b->y, 60, 30, (i < 2), 1);
1342                 // set up callback for when a mouse first goes over a button
1343                 b->button.set_highlight_action(common_play_highlight_sound);
1344                 b->button.set_bmaps(b->filename);
1345                 b->button.link_hotspot(b->hotspot);
1346         }
1347
1348         // add all strings      
1349         Ui_window.add_XSTR("Continue", 1069, Buttons[gr_screen.res][ACCEPT_BUTTON].xt,  Buttons[gr_screen.res][ACCEPT_BUTTON].yt, &Buttons[gr_screen.res][ACCEPT_BUTTON].button, UI_XSTR_COLOR_PINK);
1350         Ui_window.add_XSTR("Events", 1070, Buttons[gr_screen.res][SHOW_EVENTS_BUTTON].xt,  Buttons[gr_screen.res][SHOW_EVENTS_BUTTON].yt, &Buttons[gr_screen.res][SHOW_EVENTS_BUTTON].button, UI_XSTR_COLOR_GREEN);
1351         Ui_window.add_XSTR("Objectives", 1071, Buttons[gr_screen.res][SHOW_OBJS_BUTTON].xt,  Buttons[gr_screen.res][SHOW_OBJS_BUTTON].yt, &Buttons[gr_screen.res][SHOW_OBJS_BUTTON].button, UI_XSTR_COLOR_GREEN);
1352         Ui_window.add_XSTR("Messages", 1072, Buttons[gr_screen.res][SHOW_MSGS_BUTTON].xt,  Buttons[gr_screen.res][SHOW_MSGS_BUTTON].yt, &Buttons[gr_screen.res][SHOW_MSGS_BUTTON].button, UI_XSTR_COLOR_GREEN);
1353
1354         // set up hotkeys for buttons so we draw the correct animation frame when a key is pressed
1355         Buttons[gr_screen.res][SCROLL_UP_BUTTON].button.set_hotkey(KEY_UP);
1356         Buttons[gr_screen.res][SCROLL_DOWN_BUTTON].button.set_hotkey(KEY_DOWN);
1357
1358         Background_bitmap = bm_load(Hud_mission_log_fname[gr_screen.res]);
1359         // Status_bitmap = bm_load(Hud_mission_log_status_fname[gr_screen.res]);
1360
1361         message_log_init_scrollback(Hud_mission_log_list_coords[gr_screen.res][2]);
1362         if (Scrollback_mode == SCROLLBACK_MODE_EVENT_LOG)
1363                 Scroll_max = Num_log_lines * gr_get_font_height();
1364         else if (Scrollback_mode == SCROLLBACK_MODE_OBJECTIVES)
1365                 Scroll_max = Num_obj_lines * gr_get_font_height();
1366         else
1367                 Scroll_max = hud_query_scrollback_size();
1368
1369         Num_obj_lines = ML_objectives_init(Hud_mission_log_list_coords[gr_screen.res][0], Hud_mission_log_list_coords[gr_screen.res][1], Hud_mission_log_list_coords[gr_screen.res][2], Hud_mission_log_list_objective_x_coord[gr_screen.res]);
1370         hud_scroll_reset();
1371 }
1372
1373 void hud_scrollback_close()
1374 {
1375         ML_objectives_close();
1376         message_log_shutdown_scrollback();
1377         if (Background_bitmap >= 0)
1378                 bm_unload(Background_bitmap);
1379         //if (Status_bitmap >= 0)
1380         //      bm_unload(Status_bitmap);
1381
1382         Ui_window.destroy();
1383         common_free_interface_palette();                // restore game palette
1384         game_flush();
1385
1386         // unpause all game sounds
1387         beam_unpause_sounds();
1388         audiostream_unpause_all();
1389
1390 }
1391
1392 void hud_scrollback_do_frame(float frametime)
1393 {
1394         int i, k, x, y;
1395         int font_height = gr_get_font_height();
1396
1397         k = Ui_window.process();
1398         switch (k) {
1399                 case KEY_RIGHT:
1400                 case KEY_TAB:
1401                         if (Scrollback_mode == SCROLLBACK_MODE_OBJECTIVES) {
1402                                 Scrollback_mode = SCROLLBACK_MODE_MSGS_LOG;
1403                                 Scroll_max = hud_query_scrollback_size();
1404                                 hud_scroll_reset();
1405
1406                         } else if (Scrollback_mode == SCROLLBACK_MODE_MSGS_LOG) {
1407                                 Scrollback_mode = SCROLLBACK_MODE_EVENT_LOG;
1408                                 Scroll_max = Num_log_lines * gr_get_font_height();
1409                                 hud_scroll_reset();
1410
1411                         } else {
1412                                 Scrollback_mode = SCROLLBACK_MODE_OBJECTIVES;
1413                                 Scroll_max = Num_obj_lines * gr_get_font_height();
1414                                 Scroll_offset = 0;
1415                         }
1416
1417                         break;
1418
1419                 case KEY_LEFT:
1420                 case KEY_SHIFTED | KEY_TAB:
1421                         if (Scrollback_mode == SCROLLBACK_MODE_OBJECTIVES) {
1422                                 Scrollback_mode = SCROLLBACK_MODE_EVENT_LOG;
1423                                 Scroll_max = Num_log_lines * gr_get_font_height();
1424                                 hud_scroll_reset();
1425
1426                         } else if (Scrollback_mode == SCROLLBACK_MODE_MSGS_LOG) {
1427                                 Scrollback_mode = SCROLLBACK_MODE_OBJECTIVES;
1428                                 Scroll_max = Num_obj_lines * gr_get_font_height();
1429                                 Scroll_offset = 0;
1430
1431                         } else {
1432                                 Scrollback_mode = SCROLLBACK_MODE_MSGS_LOG;
1433                                 Scroll_max = hud_query_scrollback_size();
1434                                 hud_scroll_reset();
1435                         }
1436
1437                         break;
1438
1439                 case KEY_PAGEUP:
1440                         hud_page_scroll_list(1);
1441                         break;
1442
1443                 case KEY_PAGEDOWN:
1444                         hud_page_scroll_list(0);
1445                         break;
1446
1447                 case KEY_ENTER:
1448                 case KEY_CTRLED | KEY_ENTER:
1449                 case KEY_ESC:                   
1450                         hud_scrollback_exit();
1451                         break;
1452
1453                 case KEY_F1:  // show help overlay
1454                         break;
1455
1456                 case KEY_F2:  // goto options screen
1457                         gameseq_post_event(GS_EVENT_OPTIONS_MENU);
1458                         break;
1459         }       // end switch
1460
1461         for (i=0; i<NUM_BUTTONS; i++){
1462                 if (Buttons[gr_screen.res][i].button.pressed()){
1463                         hud_scrollback_button_pressed(i);               
1464                 }
1465         }
1466
1467         GR_MAYBE_CLEAR_RES(Background_bitmap);
1468         if (Background_bitmap >= 0) {
1469                 gr_set_bitmap(Background_bitmap);
1470                 gr_bitmap(0, 0);
1471         }
1472
1473         /*
1474         if ((Scrollback_mode == SCROLLBACK_MODE_OBJECTIVES) && (Status_bitmap >= 0)) {
1475                 gr_set_bitmap(Status_bitmap);
1476                 gr_bitmap(Hud_mission_log_status_coords[gr_screen.res][0], Hud_mission_log_status_coords[gr_screen.res][1]);
1477         }
1478         */
1479
1480         // draw the objectives key at the bottom of the ingame objectives screen
1481         if (Scrollback_mode == SCROLLBACK_MODE_OBJECTIVES) {
1482                 ML_render_objectives_key();
1483         }
1484
1485         Ui_window.draw();
1486
1487         if (Scrollback_mode == SCROLLBACK_MODE_EVENT_LOG) {
1488                 Buttons[gr_screen.res][SHOW_EVENTS_BUTTON].button.draw_forced(2);
1489                 mission_log_scrollback(Scroll_offset, Hud_mission_log_list_coords[gr_screen.res][0], Hud_mission_log_list_coords[gr_screen.res][1], Hud_mission_log_list_coords[gr_screen.res][2], Hud_mission_log_list_coords[gr_screen.res][3]);
1490
1491         } else if (Scrollback_mode == SCROLLBACK_MODE_OBJECTIVES) {
1492                 Buttons[gr_screen.res][SHOW_OBJS_BUTTON].button.draw_forced(2);
1493                 ML_objectives_do_frame(Scroll_offset);
1494
1495         } else {
1496                 line_node *node_ptr;
1497
1498                 Buttons[gr_screen.res][SHOW_MSGS_BUTTON].button.draw_forced(2);
1499 //              y = ((LIST_H / font_height) - 1) * font_height;
1500                 y = 0;
1501                 if ( !EMPTY(&Msg_scrollback_used_list) && HUD_msg_inited ) {
1502                         node_ptr = GET_FIRST(&Msg_scrollback_used_list);
1503                         i = 0;
1504                         while ( node_ptr != END_OF_LIST(&Msg_scrollback_used_list) ) {
1505                                 if ((node_ptr->source == HUD_SOURCE_HIDDEN) || (i++ < Scroll_offset)) {
1506                                         node_ptr = GET_NEXT(node_ptr);
1507
1508                                 } else {
1509                                         switch (node_ptr->source) {
1510                                                 case HUD_SOURCE_FRIENDLY:
1511                                                         SET_COLOR_FRIENDLY;
1512                                                         break;
1513
1514                                                 case HUD_SOURCE_HOSTILE:
1515                                                         SET_COLOR_HOSTILE;
1516                                                         break;
1517
1518                                                 case HUD_SOURCE_NEUTRAL:
1519                                                         SET_COLOR_NEUTRAL;
1520                                                         break;
1521
1522                                                 case HUD_SOURCE_UNKNOWN:
1523                                                         SET_COLOR_UNKNOWN;
1524                                                         break;
1525
1526                                                 case HUD_SOURCE_TRAINING:
1527                                                         gr_set_color_fast(&Color_bright_blue);
1528                                                         break;
1529
1530                                                 case HUD_SOURCE_TERRAN_CMD:
1531                                                         gr_set_color_fast(&Color_bright_white);
1532                                                         break;
1533
1534                                                 case HUD_SOURCE_IMPORTANT:
1535                                                 case HUD_SOURCE_FAILED:
1536                                                 case HUD_SOURCE_SATISFIED:
1537                                                         gr_set_color_fast(&Color_bright_white);
1538                                                         break;
1539
1540                                                 default:
1541                                                         gr_set_color_fast(&Color_text_normal);
1542                                                         break;
1543                                         }
1544
1545                                         if (node_ptr->time)
1546                                                 gr_print_timestamp(Hud_mission_log_list_coords[gr_screen.res][0], Hud_mission_log_list_coords[gr_screen.res][1] + y, node_ptr->time);
1547
1548                                         x = Hud_mission_log_list2_coords[gr_screen.res][0] + node_ptr->x;
1549                                         gr_printf(x, Hud_mission_log_list_coords[gr_screen.res][1] + y, "%s", node_ptr->text);
1550                                         if (node_ptr->underline_width)
1551                                                 gr_line(x, Hud_mission_log_list_coords[gr_screen.res][1] + y + font_height - 1, x + node_ptr->underline_width, Hud_mission_log_list_coords[gr_screen.res][1] + y + font_height - 1);
1552
1553                                         if ((node_ptr->source == HUD_SOURCE_FAILED) || (node_ptr->source == HUD_SOURCE_SATISFIED)) {
1554                                                 // draw goal icon
1555                                                 if (node_ptr->source == HUD_SOURCE_FAILED)
1556                                                         gr_set_color_fast(&Color_bright_red);
1557                                                 else
1558                                                         gr_set_color_fast(&Color_bright_green);
1559
1560                                                 i = Hud_mission_log_list_coords[gr_screen.res][1] + y + font_height / 2 - 1;
1561                                                 gr_circle(Hud_mission_log_list2_coords[gr_screen.res][0] - 6, i, 5);
1562
1563                                                 gr_set_color_fast(&Color_bright);
1564                                                 gr_line(Hud_mission_log_list2_coords[gr_screen.res][0] - 10, i, Hud_mission_log_list2_coords[gr_screen.res][0] - 8, i);
1565                                                 gr_line(Hud_mission_log_list2_coords[gr_screen.res][0] - 6, i - 4, Hud_mission_log_list2_coords[gr_screen.res][0] - 6, i - 2);
1566                                                 gr_line(Hud_mission_log_list2_coords[gr_screen.res][0] - 4, i, Hud_mission_log_list2_coords[gr_screen.res][0] - 2, i);
1567                                                 gr_line(Hud_mission_log_list2_coords[gr_screen.res][0] - 6, i + 2, Hud_mission_log_list2_coords[gr_screen.res][0] - 6, i + 4);
1568                                         }
1569
1570                                         y += font_height + node_ptr->y;
1571                                         node_ptr = GET_NEXT(node_ptr);
1572                                         if (y + font_height > Hud_mission_log_list_coords[gr_screen.res][3])
1573                                                 break;
1574                                 }
1575                         }
1576                 }
1577         }
1578
1579         gr_set_color_fast(&Color_text_heading);
1580         gr_print_timestamp(Hud_mission_log_time_coords[gr_screen.res][0], Hud_mission_log_time_coords[gr_screen.res][1] - font_height, (int) (f2fl(Missiontime) * 1000));
1581         gr_string(Hud_mission_log_time2_coords[gr_screen.res][0], Hud_mission_log_time_coords[gr_screen.res][1] - font_height, XSTR( "Current time", 289));
1582         gr_flip();
1583 }
1584
1585 void hud_scrollback_exit()
1586 {
1587         gameseq_post_event(GS_EVENT_PREVIOUS_STATE);
1588 }
1589