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