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