File: hud.cpp

package info (click to toggle)
freespace2 3.7.0%2Brepack-2
  • links: PTS, VCS
  • area: non-free
  • in suites: jessie, jessie-kfreebsd
  • size: 22,848 kB
  • ctags: 41,897
  • sloc: cpp: 369,931; makefile: 1,060; xml: 129; sh: 112
file content (3971 lines) | stat: -rw-r--r-- 96,767 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
/*
 * Copyright (C) Volition, Inc. 1999.  All rights reserved.
 *
 * All source code herein is the property of Volition, Inc. You may not sell 
 * or otherwise commercially exploit the source or things you created based on the 
 * source.
 *
*/ 


#include "hud/hud.h"
#include "asteroid/asteroid.h"
#include "cmdline/cmdline.h"
#include "freespace2/freespace.h"
#include "gamesnd/eventmusic.h"
#include "gamesnd/gamesnd.h"
#include "globalincs/alphacolors.h"
#include "globalincs/linklist.h"
#include "graphics/font.h"
#include "hud/hudconfig.h"
#include "hud/hudescort.h"
#include "hud/hudets.h"
#include "hud/hudlock.h"
#include "hud/hudmessage.h"
#include "hud/hudobserver.h"
#include "hud/hudreticle.h"
#include "hud/hudshield.h"
#include "hud/hudsquadmsg.h"
#include "hud/hudtarget.h"
#include "hud/hudtargetbox.h"
#include "hud/hudwingmanstatus.h"
#include "hud/hudparse.h"
#include "object/objectdock.h"
#include "hud/hudnavigation.h"	//kazan
#include "io/timer.h"
#include "localization/localize.h"
#include "mission/missiongoals.h"
#include "mission/missionmessage.h"
#include "mission/missionparse.h"
#include "mission/missiontraining.h"
#include "missionui/redalert.h"
#include "model/model.h"
#include "object/object.h"
#include "playerman/player.h"
#include "radar/radar.h"
#include "render/3d.h"
#include "ai/aigoals.h"
#include "ship/ship.h"
#include "starfield/supernova.h"
#include "weapon/emp.h"
#include "weapon/weapon.h"
#include "radar/radarsetup.h"
#include "iff_defs/iff_defs.h"
#include "network/multiutil.h"
#include "network/multi_voice.h"
#include "network/multi_pmsg.h"
#include "bmpman/bmpman.h"

SCP_vector<HudGauge*> default_hud_gauges;

// new values for HUD alpha
#define HUD_NEW_ALPHA_DIM				80	
#define HUD_NEW_ALPHA_NORMAL			120
#define HUD_NEW_ALPHA_BRIGHT			220

// high contrast
#define HUD_NEW_ALPHA_DIM_HI			130
#define HUD_NEW_ALPHA_NORMAL_HI			190
#define HUD_NEW_ALPHA_BRIGHT_HI			255

// Externals not related to the HUD code itself
extern float View_zoom;

// globals that will control the color of the HUD gauges
int HUD_color_red = 0;
int HUD_color_green = 255;
int HUD_color_blue = 0;
int HUD_color_alpha = HUD_COLOR_ALPHA_DEFAULT;		// 1 -> HUD_COLOR_ALPHA_USER_MAX

int HUD_draw     = 1;
int HUD_contrast = 0;										// high or lo contrast (for nebula, etc)

// Goober5000
int HUD_disable_except_messages = 0;

color HUD_color_defaults[HUD_NUM_COLOR_LEVELS];		// array of colors with different alpha blending
color HUD_color_debug;										// grey debug text shown on HUD

static int Player_engine_snd_loop = -1;

// HUD render frame offsets
float HUD_offset_x = 0.0f;
float HUD_offset_y = 0.0f;

// the offset of the player's view vector and the ship forward vector in pixels (Swifty)
int HUD_nose_x;
int HUD_nose_y;
// Global: integrity of player's target
float Pl_target_integrity;

int Hud_max_targeting_range;

static int Hud_last_can_target;	// whether Player is able to target in the last frame
static int Hud_can_target_timer;	// timestamp to allow target gauge to draw static once targeting functions are not allowed

// centered text message gauges (collision, emp, etc)
char Hud_text_flash[512] = "";
int Hud_text_flash_timer = 0;
int Hud_text_flash_interval = 0;

void hud_init_text_flash_gauge();
void hud_start_text_flash(char *txt, int t, int interval);

// multiplayer messaging text
int Multi_msg_coords[GR_NUM_RESOLUTIONS][2] = {
	{ // GR_640
		5, 150
	},
	{ // GR_1024
		8, 240
	}
};

// multiplayer voice stuff
int Voice_coords[GR_NUM_RESOLUTIONS][2] = {
	{ // GR_640
		5, 165
	},
	{ // GR_1024
		8, 255
	}
};

// ping text coords
int Ping_coords[GR_NUM_RESOLUTIONS][2] = {
	{ // GR_640
		560, 3
	},
	{ // GR_1024
		896, 5
	}
};

// supernova coords
int Supernova_coords[GR_NUM_RESOLUTIONS][2] = {
	{ // GR_640
		100, 100
	},
	{ // GR_1024
		170, 170
	}
};

// used to draw the kills gauge
hud_frames Kills_gauge;
int Kills_gauge_loaded = 0;
int Kills_gauge_coords[GR_NUM_RESOLUTIONS][2] = {
	{ // GR_640
		497, 361
	},
	{ // GR_1024
		880, 624
	}
};
int Kills_text_coords[GR_NUM_RESOLUTIONS][2] = {
	{ // GR_640
		503, 365
	},
	{ // GR_1024
		886, 628
	}
};

int Kills_text_val_coords_gr[GR_NUM_RESOLUTIONS][2] = {
	{ // GR_640
		615, 365
	},
	{ // GR_1024
		984, 628
	}
};

int Kills_text_val_coords[GR_NUM_RESOLUTIONS][2] = {
	{ // GR_640
		571, 365
	},
	{ // GR_1024
		954, 628
	}
};

char Kills_fname[GR_NUM_RESOLUTIONS][MAX_FILENAME_LEN] = {
	"kills1",
	"kills1"
};

// used to draw the hud support view
static int Hud_support_view_active;
static int Hud_support_view_abort;		// active when we need to display abort message
static int Hud_support_view_fade;		// timer
static int Hud_support_obj_sig, Hud_support_objnum, Hud_support_target_sig;

// flashing gauges
#define HUD_GAUGE_FLASH_DURATION		5000
#define HUD_GAUGE_FLASH_INTERVAL		200
int HUD_gauge_flash_duration[NUM_HUD_GAUGES];
int HUD_gauge_flash_next[NUM_HUD_GAUGES];
int HUD_gauge_bright;

static objective_display_info Objective_display;

// Subspace notify display
static int Subspace_notify_active;
static int Objective_notify_active;
static int HUD_abort_subspace_timer = 1;

static hud_subsys_info	Pl_hud_subsys_info[SUBSYSTEM_MAX];
static int					Pl_hud_next_flash_timestamp;
static int					Pl_hud_is_bright;

#define SUBSYS_DAMAGE_FLASH_DURATION	1800
#define SUBSYS_DAMAGE_FLASH_INTERVAL	100

float Player_rearm_eta = 0;

// forward declarations
void update_throttle_sound();
void hud_damage_popup_init();
void hud_support_view_init();
void hud_gauge_flash_init();
void hud_objective_message_init();
void hud_stop_subspace_notify();
void hud_start_subspace_notify();
void hud_stop_objective_notify();
void hud_start_objective_notify();
int hud_subspace_notify_active();
int hud_objective_notify_active();
void hud_subspace_notify_abort();
void hud_maybe_display_subspace_notify();
void hud_init_kills_gauge();
int hud_maybe_render_emp_icon();
void hud_init_emp_icon();

//	Saturate a value in minv..maxv.
void saturate(int *i, int minv, int maxv)
{
	if (*i < minv)
		*i = minv;
	else if (*i > maxv)
		*i = maxv;
}

// init the colors used for the different shades of the HUD
void HUD_init_hud_color_array()
{
	int i;

	for ( i = 0; i < HUD_NUM_COLOR_LEVELS; i++ ) {
		gr_init_alphacolor( &HUD_color_defaults[i], HUD_color_red, HUD_color_green, HUD_color_blue, (i+1)*16 );
	}
}

// HUD_init will call all the various HUD gauge init functions.  This function is called at the
// start of each mission (level)
void HUD_init_colors()
{
	saturate(&HUD_color_red, 0, 255);
	saturate(&HUD_color_green, 0, 255);
	saturate(&HUD_color_blue, 0, 255);
	saturate(&HUD_color_alpha, 0, HUD_COLOR_ALPHA_USER_MAX);

	gr_init_alphacolor( &HUD_color_debug, 128, 255, 128, HUD_color_alpha*16 );
	HUD_init_hud_color_array();

	hud_init_targeting_colors();
	hud_gauge_flash_init();
}

// The following global data is used to determine if we should change the engine sound.
// We only check if the throttle has changed every THROTTLE_SOUND_CHECK_INTERVAL ms, and
// then we make sure that the throttle has actually changed.  If it has changed, we start
// a new sound and/or adjust the volume.  This occurs in update_throttle_sound()
//
static float last_percent_throttle;
#define THROTTLE_SOUND_CHECK_INTERVAL	50	// in ms
static int throttle_sound_check_id;

#define DAMAGE_FLASH_TIME 150
static int Damage_flash_bright;
static int Damage_flash_timer;

HudGauge::HudGauge():
base_w(0), base_h(0), gauge_config(-1), font_num(FONT1), lock_color(false), sexp_lock_color(false), reticle_follow(false), 
active(false), off_by_default(false), sexp_override(false), pop_up(false), disabled_views(0), custom_gauge(false),
texture_target(-1), canvas_w(-1), canvas_h(-1), target_w(-1), target_h(-1)
{
	position[0] = 0;
	position[1] = 0;

	gr_init_alphacolor(&gauge_color, 255, 255, 255, (HUD_color_alpha+1)*16);
	flash_duration = timestamp(1);
	flash_next = timestamp(1);
	flash_status = false;

	popup_timer = timestamp(1);

	texture_target_fname[0] = '\0';

	custom_name[0] = '\0';
	custom_text = "";
	custom_frame.first_frame = -1;
	custom_frame.num_frames = 0;
	custom_frame_offset = 0;
}

HudGauge::HudGauge(int _gauge_object, int _gauge_config, bool _slew, bool _message, int _disabled_views, int r, int g, int b):
base_w(0), base_h(0), gauge_config(_gauge_config), gauge_object(_gauge_object), font_num(FONT1), lock_color(false), sexp_lock_color(false),
reticle_follow(_slew), active(false), off_by_default(false), sexp_override(false), pop_up(false), message_gauge(_message),
disabled_views(_disabled_views), custom_gauge(false), textoffset_x(0), textoffset_y(0), texture_target(-1),
canvas_w(-1), canvas_h(-1), target_w(-1), target_h(-1)
{
	Assert(gauge_config <= NUM_HUD_GAUGES && gauge_config >= 0);

	position[0] = 0;
	position[1] = 0;

	if(r >= 0 && r <= 255 && 
		g >= 0 && g <= 255 && 
		b >= 0 && b <= 255) {
		gr_init_alphacolor(&gauge_color, r, g, b, (HUD_color_alpha+1)*16);
	} else {
		gr_init_alphacolor(&gauge_color, 255, 255, 255, (HUD_color_alpha+1)*16);
	}

	flash_duration = timestamp(1);
	flash_next = timestamp(1);
	flash_status = false;

	popup_timer = timestamp(1);

	texture_target_fname[0] = '\0';

	custom_name[0] = '\0';
	custom_text = "";
	default_text = "";
	custom_frame.first_frame = -1;
	custom_frame.num_frames = 0;
	custom_frame_offset = 0;
}

// constructor for custom gauges
HudGauge::HudGauge(int _gauge_config, bool _slew, int r, int g, int b, char* _custom_name, char* _custom_text, char* frame_fname, int txtoffset_x, int txtoffset_y):
base_w(0), base_h(0), gauge_config(_gauge_config), gauge_object(HUD_OBJECT_CUSTOM), font_num(FONT1), lock_color(false), sexp_lock_color(false), 
reticle_follow(_slew), active(false), off_by_default(false), sexp_override(false), pop_up(false), message_gauge(false),
disabled_views(VM_EXTERNAL | VM_DEAD_VIEW | VM_WARP_CHASE | VM_PADLOCK_ANY), custom_gauge(true), textoffset_x(txtoffset_x),
 textoffset_y(txtoffset_y), texture_target(-1), canvas_w(-1), canvas_h(-1), target_w(-1), target_h(-1)
{
	position[0] = 0;
	position[1] = 0;

	if(r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255) {
		gr_init_alphacolor(&gauge_color, r, g, b, (HUD_color_alpha+1)*16);
	} else {
		gr_init_alphacolor(&gauge_color, 255, 255, 255, (HUD_color_alpha+1)*16);
	}

	flash_duration = timestamp(1);
	flash_next = timestamp(1);
	flash_status = false;

	popup_timer = timestamp(1);

	texture_target_fname[0] = '\0';

	if(_custom_name) {
		strcpy_s(custom_name, _custom_name);
	} else {
		custom_name[0] = '\0';
	}

	if(_custom_text) {
		custom_text = _custom_text;
		default_text = _custom_text;
	} else {
		custom_text = "";
		default_text = "";
	}

	custom_frame.first_frame = -1;
	custom_frame.num_frames = 0;
	custom_frame_offset = 0;

	if(frame_fname) {
		custom_frame.first_frame = bm_load_animation(frame_fname, &custom_frame.num_frames);
		if (custom_frame.first_frame < 0 ) {
			Warning(LOCATION,"Cannot load hud ani: %s\n", frame_fname);
		}
	}
}

void HudGauge::initPosition(int x, int y)
{
	Assert(x >= 0 && y >= 0);

	position[0] = x;
	position[1] = y;
}

void HudGauge::initBaseResolution(int w, int h)
{
	Assert(w >= 640 && h >= 480);

	base_w = w;
	base_h = h;
}

void HudGauge::initSlew(bool slew)
{
	reticle_follow = slew;
}

void HudGauge::initFont(int font)
{
	if ( font >= 0 && font < Num_fonts) {
		font_num = font;
	}
}

char* HudGauge::getCustomGaugeName()
{
	return custom_name;
}

const char* HudGauge::getCustomGaugeText()
{
	return custom_text.c_str();
}

void HudGauge::updateCustomGaugeCoords(int _x, int _y)
{
	if(!custom_gauge) {
		return;
	}

	position[0] = _x;
	position[1] = _y;
}

void HudGauge::updateCustomGaugeFrame(int frame_offset)
{
	if(!custom_gauge) {
		return;
	}
	
	if (frame_offset < 0 ||frame_offset > custom_frame.num_frames) {
		return;
	}

	custom_frame_offset = frame_offset;
}

void HudGauge::updateCustomGaugeText(const char* txt)
{
	if(!custom_gauge) {
		return;
	}

	custom_text = txt;
}

void HudGauge::updateCustomGaugeText(SCP_string& txt)
{
	if(!custom_gauge) {
		return;
	}

	custom_text = txt;
}

void HudGauge::setFont()
{
	gr_set_font(font_num);
}

void HudGauge::setGaugeColor(int bright_index)
{
	int alpha;

	// if we're drawing it as bright
	if(bright_index != HUD_C_NONE){
		switch(bright_index){
		case HUD_C_DIM:
			alpha = HUD_contrast ? HUD_NEW_ALPHA_DIM_HI : HUD_NEW_ALPHA_DIM;
			gr_init_alphacolor(&gauge_color, gauge_color.red, gauge_color.green, gauge_color.blue, alpha);
			break;

		case HUD_C_NORMAL:
			alpha = HUD_contrast ? HUD_NEW_ALPHA_NORMAL_HI : HUD_NEW_ALPHA_NORMAL;
			gr_init_alphacolor(&gauge_color, gauge_color.red, gauge_color.green, gauge_color.blue, alpha);
			break;

		case HUD_C_BRIGHT:
			alpha = HUD_contrast ? HUD_NEW_ALPHA_BRIGHT_HI : HUD_NEW_ALPHA_BRIGHT;
			gr_init_alphacolor(&gauge_color, gauge_color.red, gauge_color.green, gauge_color.blue, alpha);
			break;

		// intensity
		default: 
			Assert((bright_index >= 0) && (bright_index < HUD_NUM_COLOR_LEVELS));
			if(bright_index < 0){
				bright_index = 0;
			}
			if(bright_index >= HUD_NUM_COLOR_LEVELS){
				bright_index = HUD_NUM_COLOR_LEVELS - 1;
			}

			// alpha = 255 - (255 / (bright_index + 1));
			// alpha = (int)((float)alpha * 1.5f);
			int level = 255 / (HUD_NUM_COLOR_LEVELS);
			alpha = level * bright_index;
			if(alpha > 255){
				alpha = 255;
			}
			if(alpha < 0){
				alpha = 0;
			}
			gr_init_alphacolor(&gauge_color, gauge_color.red, gauge_color.green, gauge_color.blue, alpha);
			break;
		}
	} else {
		switch(maybeFlashSexp()) {
		case 0:
			alpha = HUD_contrast ? HUD_NEW_ALPHA_DIM_HI : HUD_NEW_ALPHA_DIM;
			gr_init_alphacolor(&gauge_color, gauge_color.red, gauge_color.green, gauge_color.blue, alpha);
			break;
		case 1:			
			alpha = HUD_contrast ? HUD_NEW_ALPHA_BRIGHT_HI : HUD_NEW_ALPHA_BRIGHT;
			gr_init_alphacolor(&gauge_color, gauge_color.red, gauge_color.green, gauge_color.blue, alpha);
			break;
		default:
			alpha = HUD_contrast ? HUD_NEW_ALPHA_NORMAL_HI : HUD_NEW_ALPHA_NORMAL;
			gr_init_alphacolor(&gauge_color, gauge_color.red, gauge_color.green, gauge_color.blue, alpha);
		}
	}

	gr_set_color_fast(&gauge_color);	
}

int HudGauge::getConfigType()
{
	//return gauge_type;
	return gauge_config;
}

int HudGauge::getObjectType()
{
	return gauge_object;
}

void HudGauge::lockConfigColor(bool lock)
{
	lock_color = lock;
}

void HudGauge::sexpLockConfigColor(bool lock)
{
	sexp_lock_color = lock;
}

void HudGauge::updateColor(int r, int g, int b, int a)
{
	if(sexp_lock_color || lock_color)
		return;

	gr_init_alphacolor(&gauge_color, r, g, b, a);
}

void HudGauge::updateActive(bool show)
{
	active = show;
}

void HudGauge::initRenderStatus(bool render)
{
	off_by_default = !render;
}

bool HudGauge::isOffbyDefault()
{
	return off_by_default;
}

bool HudGauge::isActive()
{
	return active && !sexp_override;
}

void HudGauge::updateSexpOverride(bool sexp)
{
	sexp_override = sexp;
}

void HudGauge::updatePopUp(bool pop_up_flag)
{
	pop_up = pop_up_flag;
}

void HudGauge::startPopUp(int time) 
{
	//Assert(gauge_index >=0 && gauge_index < NUM_HUD_GAUGES);
	if ( !pop_up ) {
		return;
	}

	popup_timer = timestamp(time);
}

int HudGauge::popUpActive()
{
	//Assert(gauge_index >=0 && gauge_index < NUM_HUD_GAUGES);
	if ( !pop_up ) {
		return 0;
	}

	if ( !timestamp_elapsed(popup_timer) ) {
		return 1;
	} else {
		return 0;
	}
}

void HudGauge::resetTimers()
{
	flash_duration = timestamp(1);
	flash_next = timestamp(1);
	flash_status = false;

	popup_timer = timestamp(1);
}

void HudGauge::startFlashSexp()
{
	flash_duration = timestamp(HUD_GAUGE_FLASH_DURATION);
	flash_next = timestamp(1);
	flash_status = false;
}

bool HudGauge::flashExpiredSexp()
{
	if(timestamp_elapsed(flash_duration)) {
		return true;
	}
	
	return false;
}

int HudGauge::maybeFlashSexp()
{
	if ( !timestamp_elapsed(flash_duration) ) {
		if ( timestamp_elapsed(flash_next) ) {
			flash_next = timestamp(HUD_GAUGE_FLASH_INTERVAL);

			// toggle between default and bright frames
			flash_status = !flash_status;
		}
		return (int)flash_status;
	}

	return -1;
}

void HudGauge::preprocess()
{

}

void HudGauge::onFrame(float frametime)
{
	
}

void HudGauge::render(float frametime)
{
	if(!custom_gauge) {
		return;
	}

	setGaugeColor();

	if( !custom_text.empty() ) {
		char *text = new char[custom_text.size()+1];
		strcpy(text, custom_text.c_str());

		hud_num_make_mono(text);
		renderString(position[0] + textoffset_x, position[1] + textoffset_y, text);

		delete[] text;
	}

	if(custom_frame.first_frame > -1) {
		renderBitmap(custom_frame.first_frame + custom_frame_offset, position[0], position[1]);
	}
}

void HudGauge::renderString(int x, int y, char *str)
{
	int nx = 0, ny = 0;

	if ( gr_screen.rendering_to_texture != -1 ) {
		gr_set_screen_scale(canvas_w, canvas_h, target_w, target_h);
	} else {
		if ( reticle_follow ) {
			nx = HUD_nose_x;
			ny = HUD_nose_y;

			gr_resize_screen_pos(&nx, &ny);
			gr_set_screen_scale(base_w, base_h);
			gr_unsize_screen_pos(&nx, &ny);
		} else {
			gr_set_screen_scale(base_w, base_h);
		}
	}

	gr_string(x + nx, y + ny, str);
	gr_reset_screen_scale();
}

void HudGauge::renderString(int x, int y, int gauge_id, char *str)
{
	int nx = 0, ny = 0;

	if ( gr_screen.rendering_to_texture != -1 ) {
		gr_set_screen_scale(canvas_w, canvas_h, target_w, target_h);
	} else {
		if ( reticle_follow ) {
			nx = HUD_nose_x;
			ny = HUD_nose_y;

			gr_resize_screen_pos(&nx, &ny);
			gr_set_screen_scale(base_w, base_h);
			gr_unsize_screen_pos(&nx, &ny);
		} else {
			gr_set_screen_scale(base_w, base_h);
		}
	}

	if ( gauge_id > -2 ) {
		emp_hud_string(x + nx, y + ny, gauge_id, str, true);
	} else {
		gr_string(x + nx, y + ny, str);
	}

	gr_reset_screen_scale();
}

void HudGauge::renderStringAlignCenter(int x, int y, int area_width, char *s)
{
	int w, h;

	gr_get_string_size(&w, &h, s);
	renderString(x + ((area_width - w) / 2), y, s);
}

void HudGauge::renderPrintf(int x, int y, char* format, ...)
{
	char tmp[256] = "";
	va_list args;	
	
	// format the text
	va_start(args, format);
	vsprintf(tmp, format, args);
	va_end(args);

	renderString(x, y, tmp);
}

void HudGauge::renderPrintf(int x, int y, int gauge_id, char* format, ...)
{
	char tmp[256] = "";
	va_list args;	
	
	// format the text
	va_start(args, format);
	vsprintf(tmp, format, args);
	va_end(args);

	renderString(x, y, gauge_id, tmp);
}

void HudGauge::renderBitmapColor(int frame, int x, int y)
{
	int nx = 0, ny = 0;

	if( !emp_should_blit_gauge() ) {
		return;
	}

	emp_hud_jitter(&x, &y);

	if ( gr_screen.rendering_to_texture != -1 ) {
		gr_set_screen_scale(canvas_w, canvas_h, target_w, target_h);
	} else {
		if ( reticle_follow ) {
			nx = HUD_nose_x;
			ny = HUD_nose_y;

			gr_resize_screen_pos(&nx, &ny);
			gr_set_screen_scale(base_w, base_h);
			gr_unsize_screen_pos(&nx, &ny);
		} else {
			gr_set_screen_scale(base_w, base_h);
		}
	}

	gr_set_bitmap(frame);
	gr_bitmap(x + nx, y + ny);
	gr_reset_screen_scale();
}

void HudGauge::renderBitmap(int x, int y)
{
	int nx = 0, ny = 0;

	if( !emp_should_blit_gauge() ) {
		return;
	}

	emp_hud_jitter(&x, &y);

	if ( gr_screen.rendering_to_texture != -1 ) {
		gr_set_screen_scale(canvas_w, canvas_h, target_w, target_h);
	} else {
		if ( reticle_follow ) {
			nx = HUD_nose_x;
			ny = HUD_nose_y;

			gr_resize_screen_pos(&nx, &ny);
			gr_set_screen_scale(base_w, base_h);
			gr_unsize_screen_pos(&nx, &ny);
		} else {
			gr_set_screen_scale(base_w, base_h);
		}
	}
	
	gr_aabitmap(x + nx, y + ny);

	gr_reset_screen_scale();
}

void HudGauge::renderBitmap(int frame, int x, int y)
{
	gr_set_bitmap(frame);
	renderBitmap(x, y);
}

void HudGauge::renderBitmapEx(int frame, int x, int y, int w, int h, int sx, int sy)
{
	int nx = 0, ny = 0; 
	
	if( !emp_should_blit_gauge() ) { 
		return;
	}

	emp_hud_jitter(&x, &y); 

	gr_set_bitmap(frame);

	if( gr_screen.rendering_to_texture != -1 ) {
		gr_set_screen_scale(canvas_w, canvas_h, target_w, target_h);
	} else {
		if ( reticle_follow ) {
			nx = HUD_nose_x;
			ny = HUD_nose_y;

			gr_resize_screen_pos(&nx, &ny);
			gr_set_screen_scale(base_w, base_h);
			gr_unsize_screen_pos(&nx, &ny);
		} else {
			gr_set_screen_scale(base_w, base_h);
		}
	}

	gr_aabitmap_ex(x + nx, y + ny, w, h, sx, sy);

	gr_reset_screen_scale();
}

void HudGauge::renderLine(int x1, int y1, int x2, int y2)
{
	int nx = 0, ny = 0;

	if ( gr_screen.rendering_to_texture != -1 ) {
		gr_set_screen_scale(canvas_w, canvas_h, target_w, target_h);
	} else {
		if ( reticle_follow ) {
			nx = HUD_nose_x;
			ny = HUD_nose_y;

			gr_resize_screen_pos(&nx, &ny);
			gr_set_screen_scale(base_w, base_h);
			gr_unsize_screen_pos(&nx, &ny);
		} else {
			gr_set_screen_scale(base_w, base_h);
		}
	}

	gr_line(x1+nx, y1+ny, x2+nx, y2+ny);
	gr_reset_screen_scale();
}

void HudGauge::renderGradientLine(int x1, int y1, int x2, int y2)
{
	int nx = 0, ny = 0;

	if ( gr_screen.rendering_to_texture != -1 ) {
		gr_set_screen_scale(canvas_w, canvas_h, target_w, target_h);
	} else {
		if(reticle_follow) {
			nx = HUD_nose_x;
			ny = HUD_nose_y;

			gr_resize_screen_pos(&nx, &ny);
			gr_set_screen_scale(base_w, base_h);
			gr_unsize_screen_pos(&nx, &ny);
		} else {
			gr_set_screen_scale(base_w, base_h);
		}
	}

	gr_gradient(x1+nx, y1+ny, x2+nx, y2+ny);
	gr_reset_screen_scale();
}

void HudGauge::renderRect(int x, int y, int w, int h)
{
	int nx = 0, ny = 0;

	if ( gr_screen.rendering_to_texture != -1 ) {
		gr_set_screen_scale(canvas_w, canvas_h, target_w, target_h);
	} else {
		if ( reticle_follow ) {
			nx = HUD_nose_x;
			ny = HUD_nose_y;

			gr_resize_screen_pos(&nx, &ny);
			gr_set_screen_scale(base_w, base_h);
			gr_unsize_screen_pos(&nx, &ny);
		} else {
			gr_set_screen_scale(base_w, base_h);
		}
	}

	gr_rect(x+nx, y+ny, w, h);
	gr_reset_screen_scale();
}

void HudGauge::renderCircle(int x, int y, int diameter) 
{
	int nx = 0, ny = 0;

	if ( gr_screen.rendering_to_texture != -1 ) {
		gr_set_screen_scale(canvas_w, canvas_h, target_w, target_h);
	} else {
		if ( reticle_follow ) {
			nx = HUD_nose_x;
			ny = HUD_nose_y;

			gr_resize_screen_pos(&nx, &ny);
			gr_set_screen_scale(base_w, base_h);
			gr_unsize_screen_pos(&nx, &ny);
		} else {
			gr_set_screen_scale(base_w, base_h);
		}
	}
	
	gr_circle(x+nx, y+ny, diameter);
	gr_reset_screen_scale();
}

void HudGauge::setClip(int x, int y, int w, int h)
{
	int hx = fl2i(HUD_offset_x);
	int hy = fl2i(HUD_offset_y);

	if ( gr_screen.rendering_to_texture != -1 ) {
		gr_set_screen_scale(canvas_w, canvas_h, target_w, target_h);

		hx = display_offset_x;
		hy = display_offset_y;

		gr_resize_screen_pos(&x, &y);

		hx += x;
		hy += y;

		gr_unsize_screen_pos(&hx, &hy);

		gr_set_clip(hx, hy, w, h);
	} else {
		if ( reticle_follow ) {
			hx += HUD_nose_x;
			hy += HUD_nose_y;
		}

		gr_resize_screen_pos(&hx, &hy);

		gr_set_screen_scale(base_w, base_h);
		gr_resize_screen_pos(&x, &y);
		gr_resize_screen_pos(&w, &h);

		gr_set_clip(hx+x, hy+y, w, h, false);
	}

	gr_reset_screen_scale();
}

void HudGauge::resetClip()
{
	int hx = 0, hy = 0;
	int w, h;

	if ( gr_screen.rendering_to_texture != -1 ) {
		gr_set_screen_scale(canvas_w, canvas_h, target_w, target_h);
		
		hx = display_offset_x;
		hy = display_offset_y;

		gr_unsize_screen_pos(&hx, &hy);

		w = canvas_w;
		h = canvas_h;

		gr_set_clip(hx, hy, w, h);
	} else {
		hx = fl2i(HUD_offset_x);
		hy = fl2i(HUD_offset_y);

		gr_resize_screen_pos(&hx, &hy);
		gr_set_screen_scale(base_w, base_h);

		w = gr_screen.max_w;
		h = gr_screen.max_h;

		// clip the screen based on the actual resolution
		gr_set_clip(hx, hy, w, h, false);
	}

	gr_reset_screen_scale();
}

void HudGauge::unsize(int *x, int *y)
{
	gr_set_screen_scale(base_w, base_h);
	gr_unsize_screen_pos(x, y);
	gr_reset_screen_scale();
}

void HudGauge::unsize(float *x, float *y)
{
	gr_set_screen_scale(base_w, base_h);
	gr_unsize_screen_posf(x, y);
	gr_reset_screen_scale();
}

void HudGauge::resize(int *x, int *y)
{
	gr_set_screen_scale(base_w, base_h);
	gr_resize_screen_pos(x, y);
	gr_reset_screen_scale();
}

void HudGauge::resize(float *x, float *y)
{
	gr_set_screen_scale(base_w, base_h);
	gr_resize_screen_posf(x, y);
	gr_reset_screen_scale();
}

void HudGauge::pageIn()
{
	if(custom_gauge) {
		if(custom_frame.first_frame > -1 && custom_frame.num_frames > 0) {
			bm_page_in_aabitmap( custom_frame.first_frame, custom_frame.num_frames );
		}
	}
}

void HudGauge::initialize()
{
	//Reset text to default
	custom_text = default_text;

	custom_frame_offset = 0;

	sexp_lock_color = false;
}

bool HudGauge::canRender()
{
	if (sexp_override) {
		return false;
	}

	if(hud_disabled_except_messages() && !message_gauge) {
		return false;
	}

	if (hud_disabled() && !hud_disabled_except_messages()) {
		return false;
	}

	if(!active)
		return false;
	
	if ( !(Game_detail_flags & DETAIL_FLAG_HUD) ) {
		return false;
	}

	if ((Viewer_mode & disabled_views)) {
		return false;
	}

	if(pop_up) {
		if(!popUpActive()) {
			return false;
		}
	}

	if (gauge_config == HUD_ETS_GAUGE) {
		if (Ships[Player_obj->instance].flags2 & SF2_NO_ETS) {
			return false;
		}
	}

	return true;
}

void HudGauge::initCockpitTarget(char* display_name, int _target_x, int _target_y, int _target_w, int _target_h, int _canvas_w, int _canvas_h)
{
	if ( strlen(display_name) <= 0 ) {
		return;
	}

	target_x = _target_x;
	target_y = _target_y;

	strcpy_s(texture_target_fname, display_name);
	target_w = _target_w;
	target_h = _target_h;

	if ( _canvas_w > 0 && _canvas_h > 0 ) {
		canvas_w = _canvas_w;
		canvas_h = _canvas_h;
	} else {
		canvas_w = _target_w;
		canvas_h = _target_h;
	}
}

bool HudGauge::setupRenderCanvas(int render_target)
{
	if (texture_target_fname[0] != '\0') {
		if ( render_target >= 0 && render_target == texture_target ) {
			return true;
		}
	} else {
		if ( render_target < 0 ) {
			return true;
		}
	}

	return false;
}

void HudGauge::setCockpitTarget(cockpit_display *display)
{
	if ( display == NULL ) {
		return;
	}

	if ( strcmp(texture_target_fname, display->name) ) {
		return;
	}

	if ( display->target < 0 ) {
		return;
	}

	texture_target = display->target;
	display_offset_x = display->offset[0] + target_x;
	display_offset_y = display->offset[1] + target_y;
}

void HudGauge::resetCockpitTarget()
{
	texture_target = -1;
}

// ----------------------------------------------------------------------
/**
 * @brief Called each level to initialize HUD systems
 */
void HUD_init()
{
	HUD_init_colors();
	hud_init_msg_window();
	hud_init_targeting();
	hud_init_reticle();
	hud_shield_level_init();
	hud_targetbox_init_flash();
	hud_escort_init();
	hud_damage_popup_init();
	hud_support_view_init();
	hud_init_squadmsg();		// initialize the vars needed for squadmate messaging
	hud_objective_message_init();
	hud_init_wingman_status_gauge();
	hud_init_target_static();
	hud_init_text_flash_gauge();
	hud_init_kills_gauge();
	hud_stop_subspace_notify();
	hud_stop_objective_notify();
	hud_target_last_transmit_level_init();

	throttle_sound_check_id = timestamp(THROTTLE_SOUND_CHECK_INTERVAL);
	HUD_abort_subspace_timer = 1;
	Hud_last_can_target = 1;
	Hud_can_target_timer = 1;
	last_percent_throttle = 0.0f;

	// default to high contrast in the nebula
	HUD_contrast = 0;
	HUD_draw     = 1;
	HUD_disable_except_messages = 0;

	if(The_mission.flags & MISSION_FLAG_FULLNEB){
		HUD_contrast = 1;
	}

	// reset to infinite
	Hud_max_targeting_range = 0;

	int i;
	size_t j, num_gauges;

	// go through all HUD gauges and call their initialization functions
	for (i = 0; i < Num_ship_classes; i++) {
		if(Ship_info[i].hud_enabled) {
			num_gauges = Ship_info[i].hud_gauges.size();

			for(j = 0; j < num_gauges; j++) {
				Ship_info[i].hud_gauges[j]->initialize();
				Ship_info[i].hud_gauges[j]->resetTimers();
				Ship_info[i].hud_gauges[j]->updateSexpOverride(false);
			}
		}
	}

	num_gauges = default_hud_gauges.size();

	for(j = 0; j < num_gauges; j++) {
		default_hud_gauges[j]->initialize();
		default_hud_gauges[j]->resetTimers();
		default_hud_gauges[j]->updateSexpOverride(false);
	}
}

/**
 * @brief Do post mission cleanup of HUD
 */
void hud_level_close()
{
	int i;
	size_t j, num_gauges;

	for ( i = 0; i < Num_ship_classes; i++ ) {
		if(Ship_info[i].hud_enabled) {
			num_gauges = Ship_info[i].hud_gauges.size();

			for(j = 0; j < num_gauges; j++) {
				Ship_info[i].hud_gauges[j]->resetCockpitTarget();
			}
		}
	}
}

/**
 * @brief Delete all HUD gauge objects, for all ships 
 */
void hud_close()
{
	int i;
	size_t j, num_gauges = 0;

	for (i = 0; i < Num_ship_classes; i++) {
		num_gauges = Ship_info[i].hud_gauges.size();

		for(j = 0; j < num_gauges; j++) {
			vm_free(Ship_info[i].hud_gauges[j]);
			Ship_info[i].hud_gauges[j] = NULL;
		}
		Ship_info[i].hud_gauges.clear();
	}

	num_gauges = default_hud_gauges.size();

	for(j = 0; j < num_gauges; j++) {
		vm_free(default_hud_gauges[j]);
		default_hud_gauges[j] = NULL;
	}
	default_hud_gauges.clear();
}

void hud_toggle_draw()
{
	HUD_draw = !HUD_draw;
}

// Goober5000
void hud_set_draw(int draw)
{
	HUD_draw = draw;
}

//WMC
int hud_get_draw()
{
	return HUD_draw;
}

// Goober5000
void hud_disable_except_messages(int disable)
{
	HUD_disable_except_messages = disable;
}

// Goober5000
/**
 * @brief Like ::hud_disabled(), except messages are still drawn
 */
int hud_disabled_except_messages()
{
	return HUD_disable_except_messages;
}

/**
 * @brief Checks if HUD disabled
 * @return !0 if HUD is disabled (ie no gauges are shown/usable), otherwise return 0
 */
int hud_disabled()
{
	return !HUD_draw;
}

/**
 * @brief Determine if we should popup the weapons gauge on the HUD.
 */
void hud_maybe_popup_weapons_gauge()
{
	if ( hud_gauge_is_popup(HUD_WEAPONS_GAUGE) ) {
		ship_weapon *swp = &Player_ship->weapons;
		int			i;

		for ( i = 0; i < swp->num_secondary_banks; i++ ) {
			if ( swp->secondary_bank_ammo[i] > 0 ) {
				int ms_till_fire = timestamp_until(swp->next_secondary_fire_stamp[i]);
				if ( ms_till_fire >= 1000 ) {
					hud_gauge_popup_start(HUD_WEAPONS_GAUGE, 2500);
				}
			}
		}
	}
}

/**
 * @brief Updates HUD systems each frame
 *
 * @details This function updates those parts of the HUD that are not dependant on the rendering of the HUD.
 */
void hud_update_frame(float frametime)
{
	object	*targetp;
	vec3d target_pos;
	int		can_target;

	update_throttle_sound();
	hud_check_reticle_list();
	hud_wingman_status_update();

	// Check hotkey selections to see if any ships need to be removed
	hud_prune_hotkeys();

	// Remove dead/departed ships from the escort list
	hud_escort_cull_list();
	hud_escort_update_list();

	hud_update_reticle( Player );

	hud_shield_hit_update();
	hud_update_weapon_flash();
	hud_maybe_popup_weapons_gauge();
	hud_update_objective_message();
	hud_support_view_update();
	message_training_update_frame();

	// if emp is active we have to allow targeting by the "random emp" system
	// we will intercept player targeting requests in hud_sensors_ok() when checking key commands
	// DB - 8/24/98
	can_target = hud_sensors_ok(Player_ship, 0);
	if(emp_active_local()){
		can_target = 1;
	}
	if ( !can_target && Hud_last_can_target ) {
		Hud_can_target_timer = timestamp(1200);		
	}
	Hud_last_can_target = can_target;

	if ( timestamp_elapsed(Hud_can_target_timer) ) {
		if ( (Player_ai->target_objnum != -1) && !can_target ){
			Player_ai->target_objnum = -1;
		}
	}

	// if there is no target, check if auto-targeting is enabled, and select new target
	int retarget = 0;
	int retarget_turret = 0;

	if (Player_ai->target_objnum == -1){
		retarget = 1;
	} else if (Objects[Player_ai->target_objnum].type == OBJ_SHIP) {
		if (Ships[Objects[Player_ai->target_objnum].instance].flags & SF_DYING){
			if (timestamp_elapsed(Ships[Objects[Player_ai->target_objnum].instance].final_death_time)) {
				retarget = 1;
			}
		}
	}

	// check if big ship and currently selected subsys is turret and turret is dead
	// only do this is not retargeting
	if ((!retarget) && (Player_ai->target_objnum != -1)) {
		if (Objects[Player_ai->target_objnum].type == OBJ_SHIP) {
			if ( !(Ships[Objects[Player_ai->target_objnum].instance].flags & SF_DYING) ) {
				if ( Ship_info[Ships[Objects[Player_ai->target_objnum].instance].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP) ) {
					ship_subsys *ss = Player_ai->targeted_subsys;
					if (ss != NULL) {
						if ((ss->system_info->type == SUBSYSTEM_TURRET) && (ss->current_hits == 0)) {
							retarget_turret = 1;
						}
					}
				}
			}
		}
	}

	if ( retarget && can_target ) {
		Player_ai->current_target_is_locked = 0;
		if ( Players[Player_num].flags & PLAYER_FLAGS_AUTO_TARGETING ) {
			Player_ai->target_objnum = -1;
			hud_target_auto_target_next();
		}
	}

	if (retarget_turret && can_target) {
		Assert(!retarget);
		void hud_update_closest_turret();
		hud_update_closest_turret();
	}

	hud_target_change_check();

	// check to see if we are in messaging mode.  If so, send the key to the code
	// to deal with the message.  hud_sqaudmsg_do_frame will return 0 if the key
	// wasn't used in messaging mode, otherwise 1.  In the event the key was used,
	// return immediately out of this function.
	if ( Players[Player_num].flags & PLAYER_FLAGS_MSG_MODE ) {
		hud_squadmsg_do_frame();
	}

	if (Player_ai->target_objnum == -1) {
		if ( Target_static_looping != -1 ) {
			snd_stop(Target_static_looping);
		}
		return;
	}

	targetp = &Objects[Player_ai->target_objnum];

	if ( Player_ai->targeted_subsys != NULL ) {
		get_subsystem_world_pos(targetp, Player_ai->targeted_subsys, &target_pos);

		// get new distance of current target
		Player_ai->current_target_distance = vm_vec_dist_quick(&target_pos,&Player_obj->pos);

		float shield_strength = Player_ai->targeted_subsys->current_hits/Player_ai->targeted_subsys->max_hits * 100.0f;
		int screen_integrity = fl2i(shield_strength+0.5f);

		if ( screen_integrity < 0 ) {
			screen_integrity = 0;
		}

		if ( screen_integrity == 0 ) {
			if ( shield_strength > 0 ) {
				screen_integrity = 1;
			}
		}

		// Goober5000 - don't flash if this subsystem can't be destroyed
		if ( ship_subsys_takes_damage(Player_ai->targeted_subsys) )
		{
			if ( screen_integrity <= 0 ){
				hud_targetbox_start_flash(TBOX_FLASH_SUBSYS);	// need to flash 0% continuously
			}
		}
	} else {
		target_pos = targetp->pos;

		Player_ai->current_target_distance = hud_find_target_distance(targetp,Player_obj);
	}

	int stop_targetting_this_thing = 0;

	// check to see if the target is still alive
	if ( targetp->flags&OF_SHOULD_BE_DEAD ) {
		stop_targetting_this_thing = 1;
	}

	Player->target_is_dying = FALSE;
	ship	*target_shipp = NULL;
	
	if ( targetp->type == OBJ_SHIP ) {
		Assert ( targetp->instance >=0 && targetp->instance < MAX_SHIPS );
		target_shipp = &Ships[targetp->instance];
		Player->target_is_dying = target_shipp->flags & SF_DYING;

		// If it is warping out (or exploded), turn off targeting
		if ( target_shipp->flags & (SF_DEPART_WARP|SF_EXPLODED) ) {
			stop_targetting_this_thing = 1;
		}
	}

	// Check if can still be seen in Nebula
	if ( hud_target_invalid_awacs(targetp) ) {
		stop_targetting_this_thing = 1;
	}

	// If this was found to be something we shouldn't
	// target anymore, just remove it
	if ( stop_targetting_this_thing )	{
		Player_ai->target_objnum = -1;
		Player_ai->targeted_subsys = NULL;
		hud_stop_looped_locking_sounds();
	}
	
	if (Player->target_is_dying) {
		hud_stop_looped_locking_sounds();
		if ( Players[Player_num].flags & PLAYER_FLAGS_AUTO_TARGETING ) {
			hud_target_auto_target_next();
		}
	}

	// Switch to battle track when a targeted ship is hostile (it attacks you) and within BATTLE_START_MIN_TARGET_DIST
	if (targetp->type == OBJ_SHIP && Event_Music_battle_started == 0 ) {
		Assert( target_shipp != NULL );

		// Goober5000
		if (iff_x_attacks_y(target_shipp->team, Player_ship->team))
		{
			float	dist_to_target;

			dist_to_target = vm_vec_dist_quick(&targetp->pos, &Player_obj->pos);
			if (dist_to_target < BATTLE_START_MIN_TARGET_DIST) {

				// If the target has an AI class of none, it is a Cargo, NavBuoy or other non-aggressive
				// ship, so don't start the battle music	
				if (stricmp(Ai_class_names[Ai_info[target_shipp->ai_index].ai_class], NOX("none")))
					event_music_battle_start();
			}
		}
	}

	// Make sure that the player isn't targeting a 3rd stage local ssm
	if (Objects[Player_ai->target_objnum].type == OBJ_WEAPON)
	{
		if (Weapons[Objects[Player_ai->target_objnum].instance].lssm_stage==3)
		{
			set_target_objnum( Player_ai, -1 );
			hud_lock_reset();
		}
	}

	// Since we need to reference the player's target integrity in several places this upcoming 
	// frame, only calculate once here
	if ( target_shipp ) {
		Pl_target_integrity = get_hull_pct(targetp);
	}

	// Update cargo scanning
	hud_update_cargo_scan_sound();

	if ( Viewer_mode & ( VM_EXTERNAL | VM_WARP_CHASE ) ) {
		Player->cargo_inspect_time = 0;
		player_stop_cargo_scan_sound();
	}

	hud_update_target_static();
	if ( (targetp->instance >=0) && (targetp->instance < MAX_SHIPS) ) {
		hud_update_ship_status(targetp);
	}
}

/**
 * @brief Draw white brackets around asteroids which has the AF_DRAW_BRACKETS flag set
 */
void hud_show_asteroid_brackets()
{
	if ( hud_sensors_ok(Player_ship, 0) ) {
		asteroid_show_brackets();
	}
}

/**
 * @brief Render gauges that need to be between a ::g3_start_frame() and a ::g3_end_frame()
 */
void hud_render_preprocess(float frametime)
{
	Player->subsys_in_view = -1;
	hud_target_clear_display_list();

	if ( (Game_detail_flags & DETAIL_FLAG_HUD) && (supernova_active() >= 3) ) {
		return;
	}

	if ( hud_disabled() ) {
		return;
	}

	if ( Viewer_mode & ( VM_DEAD_VIEW ) ) {
		return;
	}

	if ( Viewer_mode & (VM_EXTERNAL | VM_WARP_CHASE | VM_PADLOCK_ANY ) ) {
		// If the player is warping out, don't draw the targeting gauges
		Assert(Player != NULL);
		if ( Player->control_mode != PCM_NORMAL ) {
			return;
		}
	}
	
	// process targeting data around any message sender
	hud_show_message_sender();

	// if messages are disabled then skip everything else
	if ( hud_disabled_except_messages() ) {
		return;
	}

	// process navigation stuff
	hud_draw_navigation();

	// process current selection set, if any
	hud_show_selection_set();

	// process asteroid brackets if necessary
	hud_show_asteroid_brackets();

	// process targeting data around the current target
	hud_show_targeting_gauges(frametime);

	// process brackets and distance to remote detonate missile
	hud_process_remote_detonate_missile();

	// process any incoming missiles
	hud_process_homing_missiles();
}

HudGaugeMissionTime::HudGaugeMissionTime():
HudGauge(HUD_OBJECT_MISSION_TIME, HUD_MISSION_TIME, false, false, (VM_EXTERNAL | VM_DEAD_VIEW | VM_WARP_CHASE | VM_PADLOCK_ANY | VM_OTHER_SHIP), 255, 255, 255)
{
}

void HudGaugeMissionTime::initTextOffsets(int x, int y)
{
	time_text_offsets[0] = x;
	time_text_offsets[1] = y;
}

void HudGaugeMissionTime::initValueOffsets(int x, int y)
{
	time_val_offsets[0] = x;
	time_val_offsets[1] = y;
}

void HudGaugeMissionTime::initBitmaps(char *fname)
{
	time_gauge.first_frame = bm_load_animation(fname, &time_gauge.num_frames);

	if ( time_gauge.first_frame == -1 ) {
		Warning(LOCATION, "Could not load in ani: %s\n", fname);
	}
}

void HudGaugeMissionTime::pageIn()
{
	bm_page_in_aabitmap(time_gauge.first_frame, time_gauge.num_frames );
}

void HudGaugeMissionTime::render(float frametime)
{
	float mission_time, time_comp;
	int minutes=0;
	int seconds=0;
	
	mission_time = f2fl(Missiontime);  // convert to seconds

	minutes=(int)(mission_time/60);
	seconds=(int)mission_time%60;

	setGaugeColor();

	// blit background frame
	if ( time_gauge.first_frame >= 0 ) {
		renderBitmap(time_gauge.first_frame, position[0], position[1]);				
	}

	// print out mission time in MM:SS format
	renderPrintf(position[0] + time_text_offsets[0], position[1] + time_text_offsets[1], NOX("%02d:%02d"), minutes, seconds);

	// display time compression as xN
	time_comp = f2fl(Game_time_compression);
	if ( time_comp < 1 ) {
		renderPrintf(position[0] + time_val_offsets[0], position[1] + time_val_offsets[1], /*XSTR( "x%.1f", 215), time_comp)*/ NOX("%.2f"), time_comp);
	} else {
		renderPrintf(position[0] + time_val_offsets[0], position[1] + time_val_offsets[1], XSTR( "x%.2f", 216), time_comp);
	}
}

/**
 * @brief Show supernova warning it there's a supernova coming
 */
void hud_maybe_display_supernova()
{
	float time_left;

	time_left = supernova_time_left();
	if(time_left < 0.0f){
		return;
	}

	gr_set_color_fast(&Color_bright_red);
	gr_printf(Supernova_coords[gr_screen.res][0], Supernova_coords[gr_screen.res][1], "Supernova Warning: %.2f s", time_left);
}

/**
 * @brief Undertakes main HUD render. 
 */
void hud_render_all()
{
	size_t i;

	hud_render_gauges();

	// start rendering cockpit dependent gauges if possible
	for ( i = 0; i < Player_displays.size(); ++i ) {
		hud_render_gauges(i);
	}

	hud_clear_msg_buffer();

	// set font back the way it was
	gr_set_font(FONT1);
}

void hud_render_gauges(int cockpit_display_num)
{
	size_t j, num_gauges;
	ship_info* sip = &Ship_info[Player_ship->ship_info_index];
	int render_target = -1;

	if ( cockpit_display_num >= 0 ) {
		if ( sip->cockpit_model_num < 0 ) {
			return;
		}

		if ( !sip->hud_enabled ) {
			return;
		}

		render_target = ship_start_render_cockpit_display(cockpit_display_num);

		if ( render_target <= 0 ) {
			return;
		}
	} else {
		if( supernova_active() >= 3 ) {
			return;
		}
	}

	// Check if this ship has its own HUD gauges. 
	if ( sip->hud_enabled ) {
		num_gauges = sip->hud_gauges.size();

		for(j = 0; j < num_gauges; j++) {
			// only preprocess gauges if we're not rendering to cockpit
			if ( cockpit_display_num < 0 ) {
				sip->hud_gauges[j]->preprocess();
			}

			sip->hud_gauges[j]->onFrame(flFrametime);

			if ( !sip->hud_gauges[j]->setupRenderCanvas(render_target) ) {
				continue;
			}

			if ( !sip->hud_gauges[j]->canRender() ) {
				continue;
			}

			sip->hud_gauges[j]->resetClip();
			sip->hud_gauges[j]->setFont();
			sip->hud_gauges[j]->render(flFrametime);
		}
	} else {
		num_gauges = default_hud_gauges.size();

		for(j = 0; j < num_gauges; j++) {
			default_hud_gauges[j]->preprocess();

			default_hud_gauges[j]->onFrame(flFrametime);

			if ( !default_hud_gauges[j]->canRender() ) {
				continue;
			}

			default_hud_gauges[j]->resetClip();
			default_hud_gauges[j]->setFont();
			default_hud_gauges[j]->render(flFrametime);
		}
	}

	if ( cockpit_display_num >= 0 ) {
		ship_end_render_cockpit_display(cockpit_display_num);

		if ( gr_screen.rendering_to_texture != -1 ) {
			// are we still are rendering to a texture at this point? uh oh.
			bm_set_render_target(-1);
		}
	}
}

/**
 * @brief Called when the game decides to stop all looping sounds
 *
 * @details This function will set the loop id's for the engine noises to -1, this will force any looping engine sounds to stop. 
 */
void hud_stop_looped_engine_sounds()
{
	if ( Player_engine_snd_loop > -1 )	{
		snd_stop(Player_engine_snd_loop);
		Player_engine_snd_loop = -1;
	}
}

#define ZERO_PERCENT			0.01f
#define ENGINE_MAX_VOL		1.0f
#define ENGINE_MAX_PITCH	44100

/**
 * @brief If the throttle has changed, modify the sound
 *
 * @details Determine what engine sound to play, based upon the percentage throttle set. 
 * If we're a multiplayer observer, stop any engine sounds from playing and return.
 */
void update_throttle_sound()
{
	float percent_throttle;

	if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_OBSERVER)){
		if(Player_engine_snd_loop != -1){
			snd_stop(Player_engine_snd_loop);
			Player_engine_snd_loop = -1;
		}

		return;
	}

	if ( timestamp_elapsed(throttle_sound_check_id) ) {

		throttle_sound_check_id = timestamp(THROTTLE_SOUND_CHECK_INTERVAL);
	
		if ( object_get_gliding(Player_obj) ) {	// Backslash
			percent_throttle = Player_obj->phys_info.forward_thrust;
		} else if ( Ships[Player_obj->instance].current_max_speed == 0 ) {
			percent_throttle = Player_obj->phys_info.fspeed / Ship_info[Ships[Player_obj->instance].ship_info_index].max_speed;
		} else {
			percent_throttle = Player_obj->phys_info.fspeed / Ships[Player_obj->instance].current_max_speed;
		}

		if ( percent_throttle != last_percent_throttle || Player_engine_snd_loop == -1 ) {

			if ( percent_throttle < ZERO_PERCENT ) {
				if ( Player_engine_snd_loop > -1 )	{
					snd_stop(Player_engine_snd_loop); // Backslash - otherwise, long engine loops keep playing
					Player_engine_snd_loop = -1;
				}
			}
			else {
				if ( Player_engine_snd_loop == -1 ){
					Player_engine_snd_loop = snd_play_looping( &Snds[ship_get_sound(Player_obj, SND_ENGINE)], 0.0f , -1, -1, percent_throttle * ENGINE_MAX_VOL, FALSE);
				} else {
					// The sound may have been trashed at the low-level if sound channel overflow.
					// TODO: implement system where certain sounds cannot be interrupted (priority?)
					if ( snd_is_playing(Player_engine_snd_loop) ) {
						snd_set_volume(Player_engine_snd_loop, percent_throttle * ENGINE_MAX_VOL);
					}
					else {
						Player_engine_snd_loop = -1;
					}
				}
			}
		}	// end if (percent_throttle != last_percent_throttle)

		last_percent_throttle = percent_throttle;

	}	// end if ( timestamp_elapsed(throttle_sound_check_id) )
}

/**
 * @brief Called at the beginning of each level. Loads frame data in once, and initializes any damage
 * gauge specific data.
 */
void hud_damage_popup_init()
{
	int i;

	Damage_flash_bright = 0;
	Damage_flash_timer =	1;

	for ( i = 0; i < SUBSYSTEM_MAX; i++ ) {
		Pl_hud_subsys_info[i].last_str = 1000.0f;
		Pl_hud_subsys_info[i].flash_duration_timestamp = 1;
		Pl_hud_next_flash_timestamp = 1;
		Pl_hud_is_bright = 0;
	}
}

HudGaugeDamage::HudGaugeDamage():
HudGauge(HUD_OBJECT_DAMAGE, HUD_DAMAGE_GAUGE, false, false, (VM_EXTERNAL | VM_DEAD_VIEW | VM_WARP_CHASE | VM_PADLOCK_ANY | VM_OTHER_SHIP), 255, 255, 255)
{
}

void HudGaugeDamage::initHeaderOffsets(int x, int y)
{
	header_offsets[0] = x;
	header_offsets[1] = y;
}

void HudGaugeDamage::initHullIntegOffsets(int x, int y)
{
	hull_integ_offsets[0] = x;
	hull_integ_offsets[1] = y;
}

void HudGaugeDamage::initHullIntegValueOffsetX(int x)
{
	hull_integ_val_offset_x = x;
}

void HudGaugeDamage::initMiddleFrameStartOffsetY(int y)
{
	middle_frame_start_offset_y = y;
}

void HudGaugeDamage::initSubsysIntegStartOffsets(int x, int y)
{
	subsys_integ_start_offsets[0] = x;
	subsys_integ_start_offsets[1] = y;
}

void HudGaugeDamage::initSubsysIntegValueOffsetX(int x)
{
	subsys_integ_val_offset_x = x;
}

void HudGaugeDamage::initBottomBgOffset(int offset)
{
	bottom_bg_offset = offset;
}

void HudGaugeDamage::initLineHeight(int h)
{
	line_h = h;
}

void HudGaugeDamage::initBitmaps(char *fname_top, char *fname_middle, char *fname_bottom)
{
	damage_top.first_frame = bm_load_animation(fname_top, &damage_top.num_frames);
	if ( damage_top.first_frame == -1 ) {
		Warning(LOCATION, "Could not load in the ani: %s\n", fname_top);
	}

	damage_middle.first_frame = bm_load_animation(fname_middle, &damage_middle.num_frames);
	if ( damage_middle.first_frame == -1 ) {
		Warning(LOCATION, "Could not load in the ani: %s\n", fname_middle);
	}

	damage_bottom.first_frame = bm_load_animation(fname_bottom, &damage_bottom.num_frames);
	if ( damage_bottom.first_frame == -1 ) {
		Warning(LOCATION, "Could not load in the ani: %s\n", fname_bottom);
	}
}

void HudGaugeDamage::initialize()
{
	Damage_flash_bright = false;
	Damage_flash_timer = timestamp(1);
	next_flash = timestamp(1);
	flash_status = false;

	HudGauge::initialize();
}

void HudGaugeDamage::pageIn()
{
	bm_page_in_aabitmap(damage_top.first_frame, damage_top.num_frames);
	bm_page_in_aabitmap(damage_middle.first_frame, damage_middle.num_frames);
	bm_page_in_aabitmap(damage_bottom.first_frame, damage_bottom.num_frames);
}

void HudGaugeDamage::render(float frametime)
{
	model_subsystem	*psub;
	ship_subsys			*pss;
	int					sx, sy, bx, by, w, h, screen_integrity, num, best_str, best_index;
	float					strength, shield, integrity;
	char					buf[128];
	hud_subsys_damage	hud_subsys_list[SUBSYSTEM_MAX];	

	if ( (Player_ship->ship_max_hull_strength - Player_obj->hull_strength) <= 1.0f ) {
		return;
	}

	if ( damage_top.first_frame == -1 ) {
		return;
	}

	if ( (The_mission.game_type & MISSION_TYPE_TRAINING) && Training_message_visible ){
		return;
	}
		
	hud_get_target_strength(Player_obj, &shield, &integrity);
	screen_integrity = fl2i(integrity*100);

	if ( hud_gauge_is_popup(gauge_config) ) {
		if ( screen_integrity >= 100 ) {
			return;
		}
	}

	if ( timestamp_elapsed(Damage_flash_timer) ) {
		Damage_flash_timer = timestamp(DAMAGE_FLASH_TIME);
		Damage_flash_bright = !Damage_flash_bright;
	}

	setGaugeColor();

	// Draw the top of the damage pop-up
	renderBitmap(damage_top.first_frame, position[0], position[1]);	
	renderString(position[0] + header_offsets[0], position[1] + header_offsets[1], XSTR( "damage", 218));

	// Show hull integrity
	if ( screen_integrity < 100 ) {		
		if ( screen_integrity == 0 ) {
			screen_integrity = 1;
		}
		sprintf(buf, XSTR( "%d%%", 219), screen_integrity);
		hud_num_make_mono(buf);
		gr_get_string_size(&w, &h, buf);
		if ( screen_integrity < 30 ) {
			gr_set_color_fast(&Color_red);
		}
		renderString(position[0] + hull_integ_offsets[0], position[1] + hull_integ_offsets[1], XSTR( "Hull Integrity", 220));
		renderString(position[0] + hull_integ_val_offset_x - w, position[1] + hull_integ_offsets[1], buf);
	} 

	// Show damaged subsystems
	sx = position[0] + subsys_integ_start_offsets[0];
	sy = position[1] + subsys_integ_start_offsets[1];
	bx = position[0];
	by = position[1] + middle_frame_start_offset_y;

	num = 0;
	for ( pss = GET_FIRST(&Player_ship->subsys_list); pss !=END_OF_LIST(&Player_ship->subsys_list); pss = GET_NEXT(pss) ) {
		psub = pss->system_info;
		strength = ship_get_subsystem_strength(Player_ship, psub->type);
		if ( strength < 1 ) {
			screen_integrity = fl2i(strength*100);
			if ( screen_integrity == 0 ) {
				if ( strength > 0 ) {
					screen_integrity = 1;
				}
			}

			if (strlen(psub->alt_dmg_sub_name))
				hud_subsys_list[num].name = psub->alt_dmg_sub_name;
			else {
				hud_subsys_list[num].name = ship_subsys_get_name(pss);
			}

			hud_subsys_list[num].str  = screen_integrity;
			hud_subsys_list[num].type = psub->type;
			num++;

			if ( strength < Pl_hud_subsys_info[psub->type].last_str ) {
				Pl_hud_subsys_info[psub->type].flash_duration_timestamp = timestamp(SUBSYS_DAMAGE_FLASH_DURATION);
			}
			Pl_hud_subsys_info[psub->type].last_str = strength;

			// Don't display more than the max number of damaged subsystems.
			if (num >= SUBSYSTEM_MAX)
			{
				break;
			}
		}
	}

	int type;
	for ( int i = 0; i < num; i++ ) {
		best_str = 1000;
		best_index = -1;
		for ( int j = 0; j < num-i; j++ ) {
			if ( hud_subsys_list[j].str < best_str ) {
				best_str = hud_subsys_list[j].str;
				best_index = j;
			}
		}

		Assert(best_index >= 0);
		Assert(best_str >= 0);

		setGaugeColor();

		renderBitmap(damage_middle.first_frame, bx, by);
		by += line_h;

		type = hud_subsys_list[best_index].type;
		if ( !timestamp_elapsed( Pl_hud_subsys_info[type].flash_duration_timestamp ) ) {
			if ( timestamp_elapsed(next_flash) ) {
				flash_status = !flash_status;
				next_flash = timestamp(SUBSYS_DAMAGE_FLASH_INTERVAL);
			}
			
			if ( flash_status ) {
				int alpha_color = MIN(HUD_COLOR_ALPHA_MAX,HUD_color_alpha+HUD_BRIGHT_DELTA);
				setGaugeColor(alpha_color);
			} else {				
				setGaugeColor();
			}
		}

		// Draw the text
		if ( best_str < 30 ) {
			if ( best_str <= 0 ) {
				if ( Damage_flash_bright ) {
					gr_set_color_fast(&Color_bright_red);
				} else {
					gr_set_color_fast(&Color_red);
				}

			} else {
				gr_set_color_fast(&Color_red);
			}
		} else {
			setGaugeColor();
		}		

		char *n_firstline;
		n_firstline = strrchr(hud_subsys_list[best_index].name, '|');
		if (n_firstline) {
			// Print only the last line
			n_firstline++;
			renderString(sx, sy, n_firstline);
		} else {
			char temp_name[NAME_LENGTH];
			strcpy_s(temp_name, hud_subsys_list[best_index].name);
			hud_targetbox_truncate_subsys_name(temp_name);
			renderString(sx, sy, temp_name);
		}

		sprintf(buf, XSTR( "%d%%", 219), best_str);
		hud_num_make_mono(buf);
		gr_get_string_size(&w, &h, buf);
		renderString(position[0] + subsys_integ_val_offset_x - w, sy, buf);
		sy += line_h;

		// Remove it from hud_subsys_list
		if ( best_index < (num-i-1) ) {
			hud_subsys_list[best_index] = hud_subsys_list[num-i-1];
		}
	}

	setGaugeColor();
	renderBitmap(damage_bottom.first_frame, bx, by + bottom_bg_offset);		
}

/** 
 * @brief Initialise the members of the ::hud_anim struct to default values
 */
void hud_anim_init(hud_anim *ha, int sx, int sy, char *filename)
{
	ha->first_frame		= -1;
	ha->num_frames		= 0;
	ha->total_time		= 0.0f;
	ha->time_elapsed	= 0.0f;
	ha->sx				= sx;
	ha->sy				= sy;
	strcpy_s(ha->filename, filename);
}

/**
 * @brief Initialise the members of the ::hud_frames struct to default values
 */
void hud_frames_init(hud_frames *hf)
{
	hf->first_frame		= -1;
	hf->num_frames		= 0;
}

/**
 * @brief Load a ::hud_anim
 * @return If successful return 0, otherwise return -1
 */
int hud_anim_load(hud_anim *ha)
{
	int		fps;

	ha->first_frame = bm_load_animation(ha->filename, &ha->num_frames, &fps);

	// Goober5000 - try to bypass the Volition bug
	if ( (ha->first_frame == -1) && !stricmp(ha->filename, "FadeIconS-FreighterCW") )
	{
		ha->first_frame = bm_load_animation("FadeIconS-FreighterWC", &ha->num_frames, &fps);
	}

	if ( ha->first_frame == -1 )
	{
		Warning(LOCATION, "Couldn't load hud animation for file '%s'", ha->filename);
		return -1;
	}

	Assert(fps != 0);
	ha->total_time = i2fl(ha->num_frames)/fps;
	return 0;
}

/**
 * @brief Render out a frame of the targetbox static animation, based on how much time has elapsed 
 *
 * @param ha			Pointer to ::hud_anim info
 * @param frametime		Seconds elapsed since last frame
 * @param draw_alpha	Draw bitmap as alpha-bitmap (default 0)
 * @param loop			Anim should loop (default 1)
 * @param hold_last		Should last frame be held (default 0)
 * @param reverse		Play animation in reverse (default 0)
 * @param resize		Resize for non-standard resolutions
 * @param mirror		Mirror along y-axis so icon points left instead of right
 */
int hud_anim_render(hud_anim *ha, float frametime, int draw_alpha, int loop, int hold_last, int reverse, bool resize, bool mirror)
{
	int framenum;

	if ( ha->num_frames <= 0 ) {
		if ( hud_anim_load(ha) == -1 )
			return 0;
	}

	ha->time_elapsed += frametime;
	if ( ha->time_elapsed > ha->total_time ) {
		if ( loop ) {
			ha->time_elapsed = 0.0f;
		} else {
			if ( !hold_last ) {
				return 0;
			}
		}
	}

	// Draw the correct frame of animation
	framenum = fl2i( (ha->time_elapsed * ha->num_frames) / ha->total_time );
	if (reverse) {
		framenum = (ha->num_frames-1) - framenum;
	}

	if ( framenum < 0 )
		framenum = 0;
	if ( framenum >= ha->num_frames )
		framenum = ha->num_frames-1;

	// Blit the bitmap for this frame
	if(emp_should_blit_gauge()){
		gr_set_bitmap(ha->first_frame + framenum);
		if ( draw_alpha ){
			gr_aabitmap(ha->sx, ha->sy, resize, mirror);
		} else {
			gr_bitmap(ha->sx, ha->sy, resize);
		}
	}

	return 1;
}

/**
 * @brief Convert a number string to use mono-spaced 1 character
 */
void hud_num_make_mono(char *num_str)
{
	int len, i, sc;
	len = strlen(num_str);

	sc = Lcl_special_chars;
	for ( i = 0; i < len; i++ ) {
		if ( num_str[i] == '1' ) {
			num_str[i] = (char)(sc + 1);
		}
	}
}

/**
 * @brief Flashing text gauge
 */
void hud_init_text_flash_gauge()
{
	strcpy_s(Hud_text_flash, "");
	Hud_text_flash_timer = timestamp(0);
	Hud_text_flash_interval = 0;
}

void hud_start_text_flash(char *txt, int t, int interval)
{
	// bogus
	if(txt == NULL){
		strcpy_s(Hud_text_flash, "");
		return;
	}

	// HACK. don't override EMP if its still going    :)
	// An additional hack: don't interrupt other warnings if this is a missile launch alert (Swifty)
	if(!timestamp_elapsed(Hud_text_flash_timer))
		if( !stricmp(Hud_text_flash, NOX("Emp")) || !stricmp(txt, NOX("Launch")) )
			return;

	strncpy(Hud_text_flash, txt, 500);
	Hud_text_flash_timer = timestamp(t);
	Hud_text_flash_interval = interval;
}

HudGaugeTextWarnings::HudGaugeTextWarnings():
HudGauge(HUD_OBJECT_TEXT_WARNINGS, HUD_TEXT_FLASH, true, false, (VM_EXTERNAL | VM_DEAD_VIEW | VM_WARP_CHASE | VM_PADLOCK_ANY | VM_OTHER_SHIP), 255, 255, 255)
{
	
}

void HudGaugeTextWarnings::initialize()
{
	next_flash = timestamp(0);
	flash_flags = false;

	HudGauge::initialize();
}

int HudGaugeTextWarnings::maybeTextFlash()
{
	if ( !timestamp_elapsed(Hud_text_flash_timer) ) {
		if ( timestamp_elapsed(next_flash) ) {
			next_flash = timestamp(Hud_text_flash_interval);

			// toggle between default and bright frames
			flash_flags = !flash_flags;
		}
	}

	return flash_flags;
}

void HudGaugeTextWarnings::render(float frametime)
{
	if ( timestamp_elapsed(Hud_text_flash_timer) || !Hud_text_flash) {
		return;
	}

	if(strlen(Hud_text_flash) <= 0) {
		return;
	}

	int w, h;

	// string size
	gr_get_string_size(&w, &h, Hud_text_flash);

	// set color
	if(maybeTextFlash()){
		setGaugeColor(HUD_C_DIM);
		
		// draw the box	
		renderRect( (int)( (float)position[0] - (float)w / 2.0f - 1.0f), (int)((float)position[1] - 1.0f), w + 2, h + 1);
	}

	// string
	setGaugeColor(HUD_C_BRIGHT);
	renderString(fl2i((float)position[0] - ((float)w / 2.0f)), position[1], Hud_text_flash);
}

HudGaugeKills::HudGaugeKills():
HudGauge(HUD_OBJECT_KILLS, HUD_KILLS_GAUGE, false, false, (VM_EXTERNAL | VM_DEAD_VIEW | VM_WARP_CHASE | VM_PADLOCK_ANY | VM_OTHER_SHIP), 255, 255, 255)
{
}

void HudGaugeKills::initBitmaps(char *fname)
{
	Kills_gauge.first_frame = bm_load_animation(fname, &Kills_gauge.num_frames);
	if ( Kills_gauge.first_frame == -1 ) {
		Warning(LOCATION, "Could not load in the ani: %s\n", fname);
	}
}

void HudGaugeKills::initTextOffsets(int x, int y)
{
	text_offsets[0] = x;
	text_offsets[1] = y;
}

void HudGaugeKills::initTextValueOffsets(int x, int y)
{
	text_value_offsets[0] = x;
	text_value_offsets[1] = y;
}

void HudGaugeKills::pageIn()
{
	bm_page_in_aabitmap(Kills_gauge.first_frame, Kills_gauge.num_frames);
}

/**
 * @brief Display the kills gauge on the HUD
 */
void HudGaugeKills::render(float frametime)
{
	if ( Kills_gauge.first_frame < 0 ) {
		return;
	}

	setGaugeColor();

	// Draw background
	renderBitmap(Kills_gauge.first_frame, position[0], position[1]);	
	renderString(position[0] + text_offsets[0], position[1] + text_offsets[1], XSTR( "kills:", 223));

	// Display how many kills the player has so far
	char num_kills_string[32];
	int	w,h;

	if ( !Player ) {
		Int3();
		return;
	}

	sprintf(num_kills_string, "%d", Player->stats.m_kill_count_ok);

	gr_get_string_size(&w, &h, num_kills_string);
	renderString(position[0]+text_value_offsets[0]-w, position[1]+text_value_offsets[1], num_kills_string);
}

HudGaugeLag::HudGaugeLag():
HudGauge(HUD_OBJECT_LAG, HUD_LAG_GAUGE, false, false, (VM_EXTERNAL | VM_DEAD_VIEW | VM_WARP_CHASE | VM_PADLOCK_ANY | VM_OTHER_SHIP), 255, 255, 255)
{

}

void HudGaugeLag::initBitmaps(char *fname)
{
	Netlag_icon.first_frame = bm_load_animation(fname, &Netlag_icon.num_frames);

	if ( Netlag_icon.first_frame == -1 ) {
		Warning(LOCATION, "Could not load in the netlag ani: %s\n", fname);
	}
}

void HudGaugeLag::pageIn()
{
	bm_page_in_aabitmap(Netlag_icon.first_frame, Netlag_icon.num_frames);
}

void HudGaugeLag::startFlashLag(int duration)
{
	flash_timer[0] = timestamp(duration);
}

bool HudGaugeLag::maybeFlashLag(bool flash_fast)
{
	bool draw_bright = false;

	if(!timestamp_elapsed(flash_timer[0])) {
		if(timestamp_elapsed(flash_timer[1])) {
			if(flash_fast) {
				flash_timer[1] = timestamp(fl2i(TBOX_FLASH_INTERVAL/2.0f));
			} else {
				flash_timer[1] = timestamp(TBOX_FLASH_INTERVAL);
			}
			flash_flag = !flash_flag;
		}

		if(flash_flag) {
			draw_bright = true;
		} 
	}

	return draw_bright;
}

void HudGaugeLag::render(float frametime)
{
	int lag_status;

	if( !(Game_mode & GM_MULTIPLAYER) ){
		return;
	}

	if ( Netlag_icon.first_frame == -1 ) {
		Int3();
		return;
	}

	lag_status = multi_query_lag_status();

	switch(lag_status) {
	case 0:
		// Draw the net lag icon flashing
		startFlashLag();
		if(maybeFlashLag()){
			setGaugeColor(HUD_C_BRIGHT);
		} else {
			setGaugeColor();
		}
		renderBitmap(Netlag_icon.first_frame, position[0], position[1]);
		break;
	case 1:
		// Draw the disconnected icon flashing fast
		if(maybeFlashLag(true)){
			setGaugeColor(HUD_C_BRIGHT);
		} else {
			setGaugeColor();
		}
		renderBitmap(Netlag_icon.first_frame+1, position[0], position[1]);
		break;
	default:
		// Nothing to draw
		return;
	}
}

/**
 * @brief Obsolete initializer for the kills gauge. Now superseded by the new HUD code.
 */
void hud_init_kills_gauge()
{
	Kills_gauge_loaded = 1;
}

/**
 * @brief Called at mission start to init data, and load support view bitmap if required
 */
void hud_support_view_init()
{
	Hud_support_view_fade = 1;
	Hud_support_obj_sig = -1;
	Hud_support_target_sig = -1;
	Hud_support_objnum = -1;
	Hud_support_view_active = 0;
	Hud_support_view_abort = 0;
}

/**
 * @brief Start displaying the support view pop-up. This will remain up until ::hud_support_view_stop() is called.
 */
void hud_support_view_start()
{
	Hud_support_view_active = 1;
	Hud_support_view_fade = 1;
}

/**
 * @brief Stop displaying the support view pop-up.
 *
 * @param stop_now If set, stop now, otherwise in 2 seconds.
 */
void hud_support_view_stop(int stop_now)
{
	if ( stop_now ) {
		Hud_support_view_active = 0;
		Hud_support_view_fade = 1;
		Hud_support_view_abort = 0;
	} else {
		Hud_support_view_fade = timestamp(2000);
	}

	Hud_support_obj_sig = -1;
	Hud_support_target_sig = -1;
	Hud_support_objnum = -1;
}

void hud_support_view_abort()
{
	hud_support_view_stop(0);
	Hud_support_view_abort = 1;
}

/**
 * @brief Get the number of seconds until repair ship will dock with ther player
 * @details mwa made this function more general purpose. Goober5000 made clearer
 *
 * @param docker_objp The object which is attempting to dock with the player
 * @return The number of seconds, 0 if already docked, -1 if error
 *
 * @todo This function is pretty stupid now.  It just assumes the dockee is sitting still, and
 * the docker is moving directly to the dockee.
 */
int hud_get_dock_time( object *docker_objp )
{
	ai_info	*aip;
	object	*dockee_objp;
	float		dist, rel_speed, docker_speed;
	vec3d	rel_vel;

	aip = &Ai_info[Ships[docker_objp->instance].ai_index];

	// Get the dockee object pointer
	if (aip->goal_objnum == -1) {
		// This can happen when you target a support ship as it warps in
		// just give a debug warning instead of a fault - taylor
		mprintf(("'aip->goal_objnum == -1' in hud_get_dock_time(), line %i\n", __LINE__));
		return 0;
	}

	dockee_objp = &Objects[aip->goal_objnum];

	// If the ship is docked, return 0
	if ( dock_check_find_direct_docked_object(docker_objp, dockee_objp) )
		return 0;

	vm_vec_sub(&rel_vel, &docker_objp->phys_info.vel, &dockee_objp->phys_info.vel);
	rel_speed = vm_vec_mag_quick(&rel_vel);

	dist = vm_vec_dist_quick(&dockee_objp->pos, &docker_objp->pos);

	docker_speed = docker_objp->phys_info.speed;

	if ( rel_speed <= docker_speed/2.0f) {	// This means the player is moving away fast from the support ship.
		return (int) (dist/docker_speed);
	} else {
		float	d1;
		float	d = dist;
		float	time = 0.0f;
		
		if (rel_speed < 20.0f)
			rel_speed = 20.0f;

		// When far away, use max speed, not current speed.  Might not have sped up yet.
		if (d > 100.0f) {
			time += (d - 100.0f)/docker_objp->phys_info.max_vel.xyz.z;
		}

		// For mid-range, use current speed.
		if (d > 60.0f) {
			d1 = MIN(d, 100.0f);

			time += (d1 - 60.0f)/rel_speed;
		}

		// For nearby, ship will have to slow down a bit for docking maneuver.
		if (d > 30.0f) {
			d1 = MIN(d, 60.0f);

			time += (d1 - 30.0f)/5.0f;
		}

		// For very nearby, ship moves quite slowly.
		d1 = MIN(d, 30.0f);
		time += d1/7.5f;

		return fl2i(time);
	}
}

/**
 * @brief Locate the closest support ship which is trying to dock with player
 * 
 * @param objnum Object number of player
 * @return Number of support ship, -1 if there is no support ship currently trying to dock
 */
int hud_support_find_closest( int objnum )
{
	ship_obj		*sop;
	ai_info		*aip;
	object		*objp;
	int i;

	objp = &Objects[objnum];

	sop = GET_FIRST(&Ship_obj_list);
	while(sop != END_OF_LIST(&Ship_obj_list)){
		if ( Ship_info[Ships[Objects[sop->objnum].instance].ship_info_index].flags & SIF_SUPPORT ) {
			int pship_index, sindex;

			// make sure support ship is not dying
			if ( !(Ships[Objects[sop->objnum].instance].flags & (SF_DYING|SF_EXPLODED)) ) {

				Assert( objp->type == OBJ_SHIP );
				aip = &Ai_info[Ships[Objects[sop->objnum].instance].ai_index];
				pship_index = objp->instance;

				// we must check all goals for this support ship -- not just the first one
				for ( i = 0; i < MAX_AI_GOALS; i++ ) {

					// we can use == in the next statement (and should) since a ship will only ever be
					// following one order at a time.
					if ( aip->goals[i].ai_mode == AI_GOAL_REARM_REPAIR ) {
						Assert( aip->goals[i].target_name );
						sindex = ship_name_lookup( aip->goals[i].target_name );
						if ( sindex == pship_index )
							return sop->objnum;
					}
				}
			}
		}
		sop = GET_NEXT(sop);
	}

	return -1;
}

void hud_support_view_update()
{
	if ( !Hud_support_view_active ) {
		return;
	}

	if((Game_mode & GM_MULTIPLAYER) && ((Net_player->flags & NETINFO_FLAG_OBSERVER) || (Player_obj->type == OBJ_OBSERVER))){
		return;
	}

	// If we haven't determined yet who the rearm ship is, try to!
	if (Hud_support_objnum == -1) {
		Hud_support_objnum = hud_support_find_closest( OBJ_INDEX(Player_obj) );
		if ( Hud_support_objnum >= 0 ) {
			Hud_support_obj_sig = Objects[Hud_support_objnum].signature;
			Hud_support_target_sig = Player_obj->signature;
		}
	} else {
		// Check to see if support ship is still alive
		if ( (Objects[Hud_support_objnum].signature != Hud_support_obj_sig) || (Hud_support_target_sig != Player_obj->signature) ) {
			hud_support_view_stop(1);
			return;
		}
	}

	if ( Hud_support_view_fade > 1 ) {
		if ( timestamp_elapsed(Hud_support_view_fade) ) {

			Hud_support_view_abort = 0;
			Hud_support_view_active = 0;
			Hud_support_view_fade = 1;
			Hud_support_objnum = -1;
		}
	}
}

HudGaugeSupport::HudGaugeSupport():
HudGauge(HUD_OBJECT_SUPPORT, HUD_SUPPORT_GAUGE, false, false, (VM_EXTERNAL | VM_DEAD_VIEW | VM_WARP_CHASE | VM_PADLOCK_ANY | VM_OTHER_SHIP), 255, 255, 255)
{
}

void HudGaugeSupport::initHeaderOffsets(int x, int y)
{
	Header_offsets[0] = x;
	Header_offsets[1] = y;
}

void HudGaugeSupport::initTextValueOffsetY(int y)
{
	text_val_offset_y = y;
}

void HudGaugeSupport::initTextDockOffsetX(int x)
{
	text_dock_offset_x = x;
}

void HudGaugeSupport::initTextDockValueOffsetX(int x)
{
	text_dock_val_offset_x = x;
}

void HudGaugeSupport::initBitmaps(char *fname)
{
	background.first_frame = bm_load_animation(fname, &background.num_frames);
	if ( background.first_frame == -1 ) {
		Warning(LOCATION, "Could not load in ani: %s\n", fname);
	}
}

void HudGaugeSupport::pageIn()
{
	bm_page_in_aabitmap( background.first_frame, background.num_frames);
}

void HudGaugeSupport::render(float frametime)
{
	int	show_time, w, h;
	char	outstr[64];

	if ( !Hud_support_view_active ) {
		return;
	}

	// Don't render this gauge for multiplayer observers
	if((Game_mode & GM_MULTIPLAYER) && ((Net_player->flags & NETINFO_FLAG_OBSERVER) || (Player_obj->type == OBJ_OBSERVER))){
		return;
	}

	if ( Hud_support_objnum >= 0 ) {
		// Check to see if support ship is still alive
		if ( (Objects[Hud_support_objnum].signature != Hud_support_obj_sig) || (Hud_support_target_sig != Player_obj->signature) ) {
			return;
		}
	}

	bm_get_info(background.first_frame, &w, &h);

	// Set hud color
	setGaugeColor();

	renderBitmap(background.first_frame, position[0], position[1]);	

	renderString(position[0] + Header_offsets[0], position[1] + Header_offsets[1], XSTR( "support", 224));

	if ( Hud_support_view_fade > 1 ) {
		if ( !timestamp_elapsed(Hud_support_view_fade) ) {
			if ( Hud_support_view_abort){
				renderStringAlignCenter(position[0], position[1] + text_val_offset_y, w, XSTR( "aborted", 225));
			} else {
				renderStringAlignCenter(position[0], position[1] + text_val_offset_y, w, XSTR( "complete", 1407));
			}
		}
		return;
	}

	show_time = 0;
	if (Player_ai->ai_flags & AIF_BEING_REPAIRED) {
		Assert(Player_ship->ship_max_hull_strength > 0);
		
		if (!Cmdline_rearm_timer)
		{
			int i;
			bool repairing = false;
			for (i = 0; i < SUBSYSTEM_MAX; i++)
			{
				if (Player_ship->subsys_info[i].type_count > 0) 
				{
					if (ship_get_subsystem_strength(Player_ship, i) < 1.0f)
					{
						repairing = true;
						break;
					}
				}
			}

			if (repairing)
				strcpy_s(outstr, XSTR("repairing", 227));
			else
				strcpy_s(outstr, XSTR("rearming", 228));
		}
		else
		{
			if (Player_rearm_eta > 0)
			{
				int min, sec, hund;
		
				min = (int)Player_rearm_eta / 60;
				sec = (int)Player_rearm_eta % 60;
				hund = (int)(Player_rearm_eta * 100) % 100;

				sprintf(outstr, "%02d:%02d.%02d", min, sec, hund);
			}
			else
			{
				sprintf(outstr, "Waiting...");
			}	
		}
		renderStringAlignCenter(position[0], position[1] + text_val_offset_y, w, outstr);
	}
	else if (Player_ai->ai_flags & AIF_REPAIR_OBSTRUCTED) {
		strcpy_s(outstr, XSTR( "obstructed", 229));
		renderStringAlignCenter(position[0], position[1] + text_val_offset_y, w, outstr);
	} else {
		if ( Hud_support_objnum == -1 ) {
			if (The_mission.support_ships.arrival_location == ARRIVE_FROM_DOCK_BAY)
			{
				strcpy_s(outstr, XSTR( "exiting hangar", 1622));
			}
			else
			{
				strcpy_s(outstr, XSTR( "warping in", 230));
			}
			renderStringAlignCenter(position[0], position[1] + text_val_offset_y, w, outstr);
		} else {
			ai_info *aip;

			// Display "busy" when support ship isn't actually enroute to me
			aip = &Ai_info[Ships[Objects[Hud_support_objnum].instance].ai_index];
			if ( aip->goal_objnum != OBJ_INDEX(Player_obj) ) {
				strcpy_s(outstr, XSTR( "busy", 231));
				show_time = 0;

			} else {
				strcpy_s(outstr, XSTR( "dock in:", 232));
				show_time = 1;
			}		

			renderString(position[0] + text_dock_offset_x, position[1] + text_val_offset_y, outstr);
		}
	}

	if ( show_time ) {
		int seconds, minutes;

		Assert( Hud_support_objnum != -1 );

		// Ensure support ship is still alive
		if ( (Objects[Hud_support_objnum].signature != Hud_support_obj_sig) || (Hud_support_target_sig != Player_obj->signature) ) {
			seconds = 0;
		} else {
			seconds = hud_get_dock_time( &Objects[Hud_support_objnum] );
		}

		if ( seconds >= 0 ) {
			minutes = seconds/60;
			seconds = seconds%60;
			if ( minutes > 99 ) {
				minutes = 99;
				seconds = 99;
			}
		} else {
			minutes = 99;
			seconds = 99;
		}
		renderPrintf(position[0] + text_dock_val_offset_x, position[1] + text_val_offset_y, NOX("%02d:%02d"), minutes, seconds);
	}
}

/**
 * @brief Set the current color to the default HUD color (with default alpha)
 */
void hud_set_default_color()
{
	Assert(HUD_color_alpha >= 0 && HUD_color_alpha < HUD_NUM_COLOR_LEVELS);
	gr_set_color_fast(&HUD_color_defaults[HUD_color_alpha]);
}

/**
 * @brief Set the current color to a bright HUD color (ie high alpha)
 */
void hud_set_bright_color()
{
	int alpha_color;
	alpha_color = MIN(HUD_COLOR_ALPHA_MAX,HUD_color_alpha+HUD_BRIGHT_DELTA);
	gr_set_color_fast(&HUD_color_defaults[alpha_color]);
}

/**
 * @brief Set the current color to a dim HUD color (ie low alpha)
 */
void hud_set_dim_color()
{
	if ( HUD_color_alpha > 2 ) {
		gr_set_color_fast(&HUD_color_defaults[2]);
	}
}

/**
 * @brief Will set the color to the IFF color based on the team
 *
 * @param objp			Object to test for team color to base on
 * @param is_bright		Default parameter (value 0) which uses bright version of IFF color
 */
void hud_set_iff_color(object *objp, int is_bright)
{
	color *use_color;

	if (ship_is_tagged(objp))
	{
		use_color = iff_get_color(IFF_COLOR_TAGGED, is_bright);
	}
	else
	{
		if (objp->type == OBJ_ASTEROID)
		{
			if (OBJ_INDEX(objp) == Player_ai->target_objnum)
			{
				use_color = iff_get_color_by_team(Iff_traitor, Player_ship->team, is_bright);
			}
			else
			{
				use_color = iff_get_color(IFF_COLOR_SELECTION, is_bright);
			}
		}
		else
		{
			use_color = iff_get_color_by_team_and_object(obj_team(objp), Player_ship->team, is_bright, objp);
		}
	}

	gr_set_color_fast(use_color);
}

/**
 * @brief Reset gauge flashing data
 */
void hud_gauge_flash_init()
{
	int i;
	for ( i=0; i<NUM_HUD_GAUGES; i++ ) {
		HUD_gauge_flash_duration[i]=timestamp(0);
		HUD_gauge_flash_next[i]=timestamp(0);
	}
	HUD_gauge_bright=0;
}

#define NUM_VM_OTHER_SHIP_GAUGES 5
static int Vm_other_ship_gauges[NUM_VM_OTHER_SHIP_GAUGES] = 
{
	HUD_CENTER_RETICLE,
	HUD_TARGET_MONITOR,
	HUD_TARGET_MONITOR_EXTRA_DATA,
	HUD_MESSAGE_LINES,
	HUD_TALKING_HEAD
};

/**
 * @brief Determine if the specified HUD gauge should be displayed
 */
int hud_gauge_active(int gauge_index)
{
	Assert(gauge_index >=0 && gauge_index < NUM_HUD_GAUGES);

	// AL: Special code: Only show two gauges when not viewing from own ship
	if ( Viewer_mode & VM_OTHER_SHIP ) {
		for ( int i = 0; i < NUM_VM_OTHER_SHIP_GAUGES; i++ ) {
			if ( gauge_index == Vm_other_ship_gauges[i] ) {
				return 1;
			}
		}
		return 0;
	}

	return hud_config_show_flag_is_set(gauge_index);
}

/**
 * @brief Determine if gauge is in pop-up mode or not
 */
int hud_gauge_is_popup(int gauge_index)
{
	Assert(gauge_index >=0 && gauge_index < NUM_HUD_GAUGES);
	return hud_config_popup_flag_is_set(gauge_index);
}

/**
 * @brief Start a gauge to pop-up
 * @details Load gauge properties defined in the HUD config if gauge is not customized.
 */
void hud_gauge_popup_start(int gauge_index, int time) 
{
	Assert(gauge_index >=0 && gauge_index < NUM_HUD_GAUGES);
	if ( !hud_gauge_is_popup(gauge_index) ) {
		return;
	}

	size_t num_gauges, i;

	if(Ship_info[Player_ship->ship_info_index].hud_gauges.size() > 0) {
		num_gauges = Ship_info[Player_ship->ship_info_index].hud_gauges.size();

		for(i = 0; i < num_gauges; i++) {
			if(Ship_info[Player_ship->ship_info_index].hud_gauges[i]->getConfigType() == gauge_index)
				Ship_info[Player_ship->ship_info_index].hud_gauges[i]->startPopUp(time);
		}
	} else {
		num_gauges = default_hud_gauges.size();

		for(i = 0; i < num_gauges; i++) {
			if(default_hud_gauges[i]->getConfigType() == gauge_index)
				default_hud_gauges[i]->startPopUp(time);
		}
	}
}

/**
 * @brief Call HUD function to flash gauge
 * @details Load gauge properties defined in the HUD config if gauge is not customized.
 */
void hud_gauge_start_flash(int gauge_index)
{
	Assert(gauge_index >=0 && gauge_index < NUM_HUD_GAUGES);

	size_t num_gauges, i; 

	HUD_gauge_flash_duration[gauge_index] = timestamp(HUD_GAUGE_FLASH_DURATION);
	HUD_gauge_flash_next[gauge_index] = 1;
 
	if(Ship_info[Player_ship->ship_info_index].hud_gauges.size() > 0) {
		num_gauges = Ship_info[Player_ship->ship_info_index].hud_gauges.size();

		for(i = 0; i < num_gauges; i++) {
			if(Ship_info[Player_ship->ship_info_index].hud_gauges[i]->getConfigType() == gauge_index)
				Ship_info[Player_ship->ship_info_index].hud_gauges[i]->startFlashSexp();
		}
	} else {
		num_gauges = default_hud_gauges.size();

		for(i = 0; i < num_gauges; i++) {
			if(default_hud_gauges[i]->getConfigType() == gauge_index)
				default_hud_gauges[i]->startFlashSexp();
		}
	}
}

/**
 * @brief Set the HUD color for the gauge, based on whether it is flashing or not
 */
void hud_set_gauge_color(int gauge_index, int bright_index)
{
	int flash_status = hud_gauge_maybe_flash(gauge_index);
	color *use_color = &HUD_config.clr[gauge_index];
	int alpha;

	// If we're drawing it as bright
	if(bright_index != HUD_C_NONE){
		switch(bright_index){
		case HUD_C_DIM:
			alpha = HUD_contrast ? HUD_NEW_ALPHA_DIM_HI : HUD_NEW_ALPHA_DIM;
			gr_init_alphacolor(use_color, use_color->red, use_color->green, use_color->blue, alpha);
			break;

		case HUD_C_NORMAL:
			alpha = HUD_contrast ? HUD_NEW_ALPHA_NORMAL_HI : HUD_NEW_ALPHA_NORMAL;
			gr_init_alphacolor(use_color, use_color->red, use_color->green, use_color->blue, alpha);
			break;

		case HUD_C_BRIGHT:
			alpha = HUD_contrast ? HUD_NEW_ALPHA_BRIGHT_HI : HUD_NEW_ALPHA_BRIGHT;
			gr_init_alphacolor(use_color, use_color->red, use_color->green, use_color->blue, alpha);
			break;

		// Intensity
		default: 
			Assert((bright_index >= 0) && (bright_index < HUD_NUM_COLOR_LEVELS));
			if(bright_index < 0){
				bright_index = 0;
			}
			if(bright_index >= HUD_NUM_COLOR_LEVELS){
				bright_index = HUD_NUM_COLOR_LEVELS - 1;
			}

			// alpha = 255 - (255 / (bright_index + 1));
			// alpha = (int)((float)alpha * 1.5f);
			int level = 255 / (HUD_NUM_COLOR_LEVELS);
			alpha = level * bright_index;
			if(alpha > 255){
				alpha = 255;
			}
			if(alpha < 0){
				alpha = 0;
			}
			gr_init_alphacolor(use_color, use_color->red, use_color->green, use_color->blue, alpha);
			break;
		}
	} else {
		switch(flash_status) {
		case 0:
			alpha = HUD_contrast ? HUD_NEW_ALPHA_DIM_HI : HUD_NEW_ALPHA_DIM;
			gr_init_alphacolor(use_color, use_color->red, use_color->green, use_color->blue, alpha);
			break;
		case 1:			
			alpha = HUD_contrast ? HUD_NEW_ALPHA_BRIGHT_HI : HUD_NEW_ALPHA_BRIGHT;
			gr_init_alphacolor(use_color, use_color->red, use_color->green, use_color->blue, alpha);
			break;
		default:			
			alpha = HUD_contrast ? HUD_NEW_ALPHA_NORMAL_HI : HUD_NEW_ALPHA_NORMAL;	
			gr_init_alphacolor(use_color, use_color->red, use_color->green, use_color->blue, alpha);
			break;
		}
	}

	gr_set_color_fast(use_color);	
}

/**
 * @brief Set the color for a gauge that may be flashing
 * @param gauge_index Gauge to test
 * @return Gauge is not flashing -1; gauge is flashing, draw dim 0; gauge is flashing, draw bright 1
 */
int hud_gauge_maybe_flash(int gauge_index)
{
	Assert(gauge_index >=0 && gauge_index < NUM_HUD_GAUGES);
	int flash_status=-1;
	if ( !timestamp_elapsed(HUD_gauge_flash_duration[gauge_index]) ) {
		if ( timestamp_elapsed(HUD_gauge_flash_next[gauge_index]) ) {
			HUD_gauge_flash_next[gauge_index] = timestamp(HUD_GAUGE_FLASH_INTERVAL);
			HUD_gauge_bright ^= (1<<gauge_index);	// toggle between default and bright frames
		}

		if ( HUD_gauge_bright & (1<<gauge_index) ) {
			flash_status=1;
		} else {
			flash_status=0;
		}
	}
	return flash_status;
}

/**
 * @brief Initialise the objective message display data
 */
void hud_objective_message_init()
{
	Objective_display.display_timer=timestamp(0);
}

void hud_update_objective_message()
{
	// Find out if we should display the subspace status notification
	if ( (Player->control_mode == PCM_WARPOUT_STAGE1) || (Player->control_mode == PCM_WARPOUT_STAGE2) || (Player->control_mode == PCM_WARPOUT_STAGE3) 
		|| (Sexp_hud_display_warpout > 0) ) {
		if (!hud_subspace_notify_active()) {
			// Keep sound from being played 1e06 times
			hud_start_subspace_notify();
		}
	} else {
		if ( timestamp_elapsed(HUD_abort_subspace_timer) ) {
			hud_stop_subspace_notify();
		}
	}

	if ( (Sexp_hud_display_warpout > 1) && hud_subspace_notify_active() ) {
		if ( Sexp_hud_display_warpout < timestamp()) {
			Sexp_hud_display_warpout = 0;
		}
	}
	
	// Find out if we should display the objective status notification
	if ( timestamp_elapsed(Objective_display.display_timer) ) {
		hud_stop_objective_notify();
	} else if (!hud_objective_notify_active() && !hud_subspace_notify_active()) {
		hud_start_objective_notify();
	}
}

/**
 * @brief Add objective status on the HUD
 *
 * @param type		Type of goal, one of: ::PRIMARY_GOAL, ::SECONDARY_GOAL, ::BONUS_GOAL
 * @param status	Status of goal, one of:	::GOAL_FAILED, ::GOAL_COMPLETE, ::GOAL_INCOMPLETE
 * @todo Play a sound?
 */
void hud_add_objective_messsage(int type, int status)
{
	Objective_display.display_timer=timestamp(7000);
	Objective_display.goal_type=type;
	Objective_display.goal_status=status;

	// if this is a multiplayer tvt game
	if(MULTI_TEAM && (Net_player != NULL)) {
		mission_goal_fetch_num_resolved(type, &Objective_display.goal_nresolved, &Objective_display.goal_ntotal, Net_player->p_info.team);
	} else {
		mission_goal_fetch_num_resolved(type, &Objective_display.goal_nresolved, &Objective_display.goal_ntotal);
	}

	// TODO: play a sound?
}

HudGaugeObjectiveNotify::HudGaugeObjectiveNotify():
HudGauge(HUD_OBJECT_OBJ_NOTIFY, HUD_OBJECTIVES_NOTIFY_GAUGE, false, false, VM_EXTERNAL | VM_DEAD_VIEW | VM_WARP_CHASE | VM_PADLOCK_ANY | VM_OTHER_SHIP, 255, 255, 255)
{

}

void HudGaugeObjectiveNotify::initObjTextOffsetY(int y)
{
	Objective_text_offset_y = y;
}

void HudGaugeObjectiveNotify::initObjValueOffsetY(int y)
{
	Objective_text_val_offset_y = y;
}

void HudGaugeObjectiveNotify::initSubspaceTextOffsetY(int y)
{
	Subspace_text_offset_y = y;
}

void HudGaugeObjectiveNotify::initSubspaceValueOffsetY(int y)
{
	Subspace_text_val_offset_y = y;
}

void HudGaugeObjectiveNotify::initRedAlertTextOffsetY(int y)
{
	Red_text_offset_y = y;
}

void HudGaugeObjectiveNotify::initRedAlertValueOffsetY(int y)
{
	Red_text_val_offset_y = y;
}

void HudGaugeObjectiveNotify::initBitmaps(char *fname)
{
	Objective_display_gauge.first_frame = bm_load_animation(fname, &Objective_display_gauge.num_frames);
	if ( Objective_display_gauge.first_frame == -1 ) {
		Warning(LOCATION, "Could not load in ani: %s\n", fname);
	}
}

void HudGaugeObjectiveNotify::initialize()
{
	flash_timer[0] = timestamp(1);
	flash_timer[1] = timestamp(1);
	flash_flag = false;

	HudGauge::initialize();
}

void HudGaugeObjectiveNotify::pageIn()
{
	bm_page_in_aabitmap( Objective_display_gauge.first_frame, Objective_display_gauge.num_frames);
}

void HudGaugeObjectiveNotify::startFlashNotify(int duration)
{
	flash_timer[0] = timestamp(duration);
}

bool HudGaugeObjectiveNotify::maybeFlashNotify(bool flash_fast)
{
	bool draw_bright = false;

	if(!timestamp_elapsed(flash_timer[0])) {
		if(timestamp_elapsed(flash_timer[1])) {
			if(flash_fast) {
				flash_timer[1] = timestamp(fl2i(TBOX_FLASH_INTERVAL/2.0f));
			} else {
				flash_timer[1] = timestamp(TBOX_FLASH_INTERVAL);
			}
			flash_flag = !flash_flag;
		}

		if(flash_flag) {
			draw_bright = true;
		} 
	}

	return draw_bright;
}

void HudGaugeObjectiveNotify::render(float frametime)
{
	renderSubspace();
	renderRedAlert();
	renderObjective();
}

void HudGaugeObjectiveNotify::renderSubspace()
{
	int w, h;
	int warp_aborted = 0;

	if ( (Player->control_mode != PCM_WARPOUT_STAGE1) && (Player->control_mode != PCM_WARPOUT_STAGE2) && (Player->control_mode != PCM_WARPOUT_STAGE3) 
		&& (Sexp_hud_display_warpout <= 0) ) {
		if ( !timestamp_elapsed(HUD_abort_subspace_timer) ) {
			warp_aborted = 1;
		}
	}

	if ( !hud_subspace_notify_active() ) {
		return;
	}

	if ( Objective_display_gauge.first_frame < 0 ) {
		return;
	}

	// Blit the background	
	setGaugeColor();
	renderBitmap(Objective_display_gauge.first_frame, position[0], position[1]);

	startFlashNotify();
	maybeFlashNotify() ? setGaugeColor(HUD_C_BRIGHT) : setGaugeColor();

	bm_get_info(Objective_display_gauge.first_frame, &w, &h);

	renderStringAlignCenter(position[0], position[1] + Subspace_text_offset_y, w, XSTR( "subspace drive", 233));
	if ( warp_aborted ) {
		renderStringAlignCenter(position[0], position[1] + Subspace_text_val_offset_y, w, XSTR( "aborted", 225));
	} else {
		renderStringAlignCenter(position[0], position[1] + Subspace_text_val_offset_y, w, XSTR( "engaged", 234));
	}
}

/**
 * @todo Play a sound?
 */
void HudGaugeObjectiveNotify::renderRedAlert()
{
	int w, h;

	if ( !red_alert_in_progress() ) {
		return;
	}

	if ( Objective_display_gauge.first_frame < 0 ) {
		return;
	}

	if ( hud_subspace_notify_active() ) {
		return;
	}

	if ( hud_objective_notify_active() ) {
		return;
	}

	// Blit the background
	gr_set_color_fast(&Color_red);		// Color box red, because it's an emergency

	renderBitmap(Objective_display_gauge.first_frame, position[0], position[1]);

	startFlashNotify();
	if(maybeFlashNotify()) {
		gr_set_color_fast(&Color_red);
	} else {
		gr_set_color_fast(&Color_bright_red);
	}

	bm_get_info(Objective_display_gauge.first_frame, &w, &h);

	renderStringAlignCenter(position[0], position[1] + Red_text_offset_y, w, XSTR( "downloading new", 235));
	renderStringAlignCenter(position[0], position[1] + Red_text_val_offset_y, w, XSTR( "orders...", 236));

	// TODO: play a sound?
}

void HudGaugeObjectiveNotify::renderObjective()
{
	int w, h;
	char buf[128];

	if ( timestamp_elapsed(Objective_display.display_timer) ) {
		return;
	}

	if ( Objective_display_gauge.first_frame < 0 ) {
		return;
	}

	if ( hud_subspace_notify_active() ) {
		return;
	}
	
	// Blit the background
	setGaugeColor();
	renderBitmap(Objective_display_gauge.first_frame, position[0], position[1]);	

	startFlashNotify();
	if(maybeFlashNotify()){
		setGaugeColor(HUD_C_BRIGHT);
	} else {
		setGaugeColor();
	}

	bm_get_info(Objective_display_gauge.first_frame, &w, &h);

	// Draw the correct goal type
	switch(Objective_display.goal_type) {
	case PRIMARY_GOAL:
		renderStringAlignCenter(position[0], position[1] + Objective_text_offset_y, w, XSTR( "primary objective", 237));
		break;
	case SECONDARY_GOAL:
		renderStringAlignCenter(position[0], position[1] + Objective_text_offset_y, w, XSTR( "secondary objective", 238));
		break;
	case BONUS_GOAL:
		renderStringAlignCenter(position[0], position[1] + Objective_text_offset_y, w, XSTR( "bonus objective", 239));
		break;
	}

	// Show the status
	switch(Objective_display.goal_type) {
	case PRIMARY_GOAL:
	case SECONDARY_GOAL:
		switch(Objective_display.goal_status) {
		case GOAL_FAILED:
			sprintf(buf, XSTR( "failed (%d/%d)", 240), Objective_display.goal_nresolved, Objective_display.goal_ntotal);
			renderStringAlignCenter(position[0], position[1] + Objective_text_val_offset_y, w, buf);
			break;
		default:
			sprintf(buf, XSTR( "complete (%d/%d)", 241), Objective_display.goal_nresolved, Objective_display.goal_ntotal);
			renderStringAlignCenter(position[0], position[1] + Objective_text_val_offset_y, w, buf);
			break;
		}		
		break;
	case BONUS_GOAL:
		switch(Objective_display.goal_status) {
		case GOAL_FAILED:
			renderStringAlignCenter(position[0], position[1] + Objective_text_val_offset_y, w, XSTR( "failed", 242));
			break;
		default:
			renderStringAlignCenter(position[0], position[1] + Objective_text_val_offset_y, w, XSTR( "complete", 226));
			break;
		}		
		break;
	}
}

void hud_subspace_notify_abort()
{
	HUD_abort_subspace_timer = timestamp(1500);
}

void hud_stop_subspace_notify()
{
	Subspace_notify_active=0;
}

void hud_start_subspace_notify()
{

	Subspace_notify_active=1;
}

int hud_subspace_notify_active()
{
	return Subspace_notify_active;
}

void hud_stop_objective_notify()
{
	Objective_notify_active = 0;
}

void hud_start_objective_notify()
{
	snd_play(&(Snds[SND_DIRECTIVE_COMPLETE]));
	Objective_notify_active = 1;
}

int hud_objective_notify_active()
{
	return Objective_notify_active;
}

/** 
 * @brief Set the offset values for this render frame
 * @details Since the player's view vector may be different from the ship's forward vector,
 * we calculate the offset of those two in pixels and store the x and y offsets in
 * variables HUD_nose_x and HUD_nose_y (Swifty)
 *
 * @param viewer_obj Object, likely to be player
 * @param wiggedy_wack
 * @param eye_orient 
 */
void HUD_set_offsets(object *viewer_obj, int wiggedy_wack, matrix *eye_orient)
{
	if ( (viewer_obj == Player_obj) && wiggedy_wack ){		
		vec3d tmp;
		vertex pt;
		ubyte flags;		

		HUD_offset_x = 0.0f;
		HUD_offset_y = 0.0f;

		vm_vec_scale_add( &tmp, &Eye_position, &eye_orient->vec.fvec, 100.0f );
		
		flags = g3_rotate_vertex(&pt,&tmp);

		g3_project_vertex(&pt);

		gr_unsize_screen_posf( &pt.screen.xyw.x, &pt.screen.xyw.y );
		HUD_offset_x -= 0.45f * (i2fl(gr_screen.clip_width_unscaled)*0.5f - pt.screen.xyw.x);
		HUD_offset_y -= 0.45f * (i2fl(gr_screen.clip_height_unscaled)*0.5f - pt.screen.xyw.y);

		if ( HUD_offset_x > 100.0f )	{
			HUD_offset_x = 100.0f;
		} else if ( HUD_offset_x < -100.0f )	{
			HUD_offset_x += 100.0f;
		}

		if ( HUD_offset_y > 100.0f )	{
			HUD_offset_y = 100.0f;
		} else if ( HUD_offset_y < -100.0f )	{
			HUD_offset_y += 100.0f;
		}

	} else {
		HUD_offset_x = 0.0f;
		HUD_offset_y = 0.0f;
	}

	if ( Viewer_mode & ( VM_TOPDOWN | VM_CHASE ) ) {
		HUD_nose_x = 0;
		HUD_nose_y = 0;
	} else {
		HUD_get_nose_coordinates(&HUD_nose_x, &HUD_nose_y);
	}
}

/**
 * @brief Returns the offset between the player's view vector and the forward vector of the ship in pixels (Swifty)
 */
void HUD_get_nose_coordinates(int *x, int *y)
{
	vertex	v0;
	vec3d	p0;

	float x_nose;
	float y_nose;
	float x_center = gr_screen.clip_center_x;
	float y_center = gr_screen.clip_center_y;

	*x = 0;
	*y = 0;
	
	vm_vec_scale_add(&p0, &Player_obj->pos, &Player_obj->orient.vec.fvec, 10000.0f);
	g3_rotate_vertex(&v0, &p0);

	if (v0.codes == 0) {
		g3_project_vertex(&v0);

		if ( !(v0.codes & PF_OVERFLOW) ) {
			x_nose = v0.screen.xyw.x;
			y_nose = v0.screen.xyw.y;
		} else {
			// Means that the ship forward vector is not going through the frame buffer.
			// We're assigning a high negative value so that the the bitmaps will be drawn offscreen so that
			// we can give the illusion that the player is looking away from the slewable HUD reticle.
			*x = -100000;
			*y = -100000;
			return;
		}
	} else {
		// Means that the ship forward vector is not going through the frame buffer.
		// We're assigning a high negative value so that the the bitmaps will be drawn offscreen so that
		// we can give the illusion that the player is looking away from the slewable HUD reticle.
		*x = -100000;
		*y = -100000;
		return;
	}

	gr_unsize_screen_posf(&x_nose, &y_nose);
	gr_unsize_screen_posf(&x_center, &y_center);

	*x = fl2i(x_nose - x_center);
	*y = fl2i(y_nose - y_center);
	return;
}

/**
 * @brief Like ::gr_reset_clip() only it accounts for HUD jittering
 */
void HUD_reset_clip()
{
	int hx = fl2i(HUD_offset_x);
	int hy = fl2i(HUD_offset_y);

	gr_set_clip(hx, hy, gr_screen.max_w_unscaled, gr_screen.max_h_unscaled);
}

/**
 * @brief Like ::gr_set_clip() only it accounts for HUD jittering
 */
void HUD_set_clip(int x, int y, int w, int h)
{
	int hx = fl2i(HUD_offset_x);
	int hy = fl2i(HUD_offset_y);

	gr_set_clip(hx+x, hy+y, w, h);
}

/**
 * @brief Called to save and restore the 3D camera settings.
 * @param save Save global view variables if 1, restore them if not 1 
 */
void hud_save_restore_camera_data(int save)
{
	static vec3d	save_view_position;
	static float	save_view_zoom;
	static matrix	save_view_matrix;
	static matrix	save_eye_matrix;
	static vec3d	save_eye_position;

	if ( save ) {
		save_view_position		= View_position;
		save_view_zoom			= View_zoom;
		save_view_matrix		= View_matrix;
		save_eye_matrix			= Eye_matrix;
		save_eye_position		= Eye_position;
	}
	else {
		// restore global view variables
		View_position	= save_view_position;
		View_zoom		= save_view_zoom;
		View_matrix		= save_view_matrix;
		Eye_matrix		= save_eye_matrix;
		Eye_position	= save_eye_position;
	}
}


void hud_toggle_contrast()
{
	HUD_contrast = !HUD_contrast;
}

void hud_set_contrast(int high)
{
	HUD_contrast = high;
}

// Paging functions for the rest of the HUD code
extern void hudtarget_page_in();

/**
 * @brief Page in all HUD bitmaps
 */
void hud_page_in()
{
	// Go through all hud gauges to page them in 
	int i;
	size_t j, num_gauges = 0;
	for (i = 0; i < Num_ship_classes; i++) {
		if(Ship_info[i].hud_enabled) {
			if(Ship_info[i].hud_gauges.size() > 0) {
				num_gauges = Ship_info[i].hud_gauges.size();

				for(j = 0; j < num_gauges; j++) {
					Ship_info[i].hud_gauges[j]->pageIn();
				}
			}
		}
	}

	num_gauges = default_hud_gauges.size();

	for(j = 0; j < num_gauges; j++) {
		default_hud_gauges[j]->pageIn();
	}
}

HudGauge* hud_get_gauge(char* name)
{
	char* gauge_name;
	size_t j;

	// go through all gauges and return the gauge that matches
	if(Ship_info[Player_ship->ship_info_index].hud_gauges.size() > 0) {
		for(j = 0; j < Ship_info[Player_ship->ship_info_index].hud_gauges.size(); j++) {

			gauge_name = Ship_info[Player_ship->ship_info_index].hud_gauges[j]->getCustomGaugeName();
			if(!strcmp(name, gauge_name)) {
				return Ship_info[Player_ship->ship_info_index].hud_gauges[j];
			}
		}
	} else {
		for(j = 0; j < default_hud_gauges.size(); j++) {

			gauge_name = default_hud_gauges[j]->getCustomGaugeName();
			if(!strcmp(name, gauge_name)) {
				return default_hud_gauges[j];
			}
		}
	}

	return NULL;
}

HudGaugeMultiMsg::HudGaugeMultiMsg():
HudGauge(HUD_OBJECT_MULTI_MSG, HUD_MESSAGE_LINES, false, true, 0, 255, 255, 255)
{
}

bool HudGaugeMultiMsg::canRender() 
{
	return true;
}

/**
 * @brief Render multiplayer text message currently being entered, if any
 */
void HudGaugeMultiMsg::render(float frametime)
{
	char txt[MULTI_MSG_MAX_TEXT_LEN+20];

	// clear the text
	memset(txt,0,MULTI_MSG_MAX_TEXT_LEN+20);

	// if there is valid multiplayer message text to be displayed
	if(multi_msg_message_text(txt)){
		gr_set_color_fast(&Color_normal);
		renderString(position[0], position[1], txt);
	}
}

HudGaugeVoiceStatus::HudGaugeVoiceStatus():
HudGauge(HUD_OBJECT_VOICE_STATUS, HUD_MESSAGE_LINES, false, true, VM_EXTERNAL | VM_DEAD_VIEW | VM_WARP_CHASE | VM_PADLOCK_ANY, 255, 255, 255) 
{
}

void HudGaugeVoiceStatus::render(float frametime)
{
	if(!(Game_mode & GM_MULTIPLAYER)){
		return;
	}
	
	// if we are currently playing a rtvoice sound stream from another player back
	switch(multi_voice_status()){
	// the player has been denied the voice token
	case MULTI_VOICE_STATUS_DENIED:
		// show a red indicator or something
		renderString(position[0], position[1], XSTR( "[voice denied]", 243));
		break;

	// the player is currently recording
	case MULTI_VOICE_STATUS_RECORDING:
		renderString(position[0], position[1], XSTR( "[recording voice]", 244));
		break;
		
	// the player is current playing back voice from someone
	case MULTI_VOICE_STATUS_PLAYING:
		renderString(position[0], position[1], XSTR( "[playing voice]", 245));
		break;

	// nothing voice related is happening on my machine
	case MULTI_VOICE_STATUS_IDLE:
		// probably shouldn't be displaying anything
		break;
	}	
}

HudGaugePing::HudGaugePing():
HudGauge(HUD_OBJECT_PING, HUD_LAG_GAUGE, false, false, 0, 255, 255, 255)
{

}

/**
 * @brief Render multiplayer ping time to the server, if appropriate
 */
void HudGaugePing::render(float frametime)
{
	// If we shouldn't be displaying a ping time, return here
	if(!multi_show_ingame_ping()){
		return;
	}
	
	// If we're in multiplayer mode, display our ping time to the server
	if(MULTIPLAYER_CLIENT && (Net_player != NULL)){
		char ping_str[50];
		memset(ping_str,0,50);

		// If our ping is positive, display it
		if((Netgame.server != NULL) && (Netgame.server->s_info.ping.ping_avg > 0)){
			// Get the string
			if(Netgame.server->s_info.ping.ping_avg >= 1000){
				strcpy_s(ping_str,XSTR("> 1 sec",628));
			} else {
				sprintf(ping_str,XSTR("%d ms",629),Netgame.server->s_info.ping.ping_avg);
			}

			// Blit the string out
			hud_set_default_color();
			renderString(position[0], position[1], ping_str);
		}
	}
}

HudGaugeSupernova::HudGaugeSupernova():
HudGauge(HUD_OBJECT_SUPERNOVA, HUD_DIRECTIVES_VIEW, false, false, 0, 255, 255, 255)
{
}

void HudGaugeSupernova::render(float frametime)
{
	float time_left;

	// if there's a supernova coming
	time_left = supernova_time_left();
	if(time_left < 0.0f){
		return;
	}

	gr_set_color_fast(&Color_bright_red);
	renderPrintf(position[0], position[1], "Supernova Warning: %.2f s", time_left);
}

HudGaugeFlightPath::HudGaugeFlightPath():
HudGauge(HUD_OBJECT_FLIGHT_PATH, HUD_CENTER_RETICLE, false, false, VM_EXTERNAL | VM_DEAD_VIEW | VM_WARP_CHASE | VM_PADLOCK_ANY, 255, 255, 255)
{
}

void HudGaugeFlightPath::initBitmap(char *fname)
{
	Marker.first_frame = bm_load_animation(fname, &Marker.num_frames);

	if ( Marker.first_frame < 0 ) {
		Warning(LOCATION,"Cannot load hud ani: %s\n", fname);
	}
}

void HudGaugeFlightPath::initHalfSize(int w, int h)
{
	Marker_half[0] = w;
	Marker_half[1] = h;
}

void HudGaugeFlightPath::render(float frametime)
{
	object *obj;
	vec3d p0,v;
	vertex v0;
	int sx, sy;

	bool in_frame = g3_in_frame() > 0;
	if(!in_frame) {
		g3_start_frame(0);
	}

	obj = Player_obj;

	vm_vec_scale_add( &v, &obj->phys_info.vel, &obj->orient.vec.fvec, 1.0f );
	vm_vec_normalize( &v );
			
	vm_vec_scale_add( &p0, &obj->pos, &v, 1000000.0f );

	g3_rotate_vertex( &v0, &p0 );

	if (v0.codes == 0) { // on screen
		g3_project_vertex(&v0);

		if (!(v0.flags & PF_OVERFLOW)) {
			if ( Marker.first_frame >= 0 ) {
				sx = fl2i(v0.screen.xyw.x);
				sy = fl2i(v0.screen.xyw.y);

				unsize(&sx, &sy);
				renderBitmap(Marker.first_frame, sx - Marker_half[0], sy - Marker_half[1]);
			}
		}
	}
	
	if(!in_frame) {
		g3_end_frame();
	}
}