]> icculus.org git repositories - taylor/freespace2.git/blob - src/radar/radar.cpp
Initial revision
[taylor/freespace2.git] / src / radar / radar.cpp
1 /*
2  * $Logfile: /Freespace2/code/Radar/Radar.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * C module containg functions to display and manage the radar
8  *
9  * $Log$
10  * Revision 1.1  2002/05/03 03:28:10  root
11  * Initial revision
12  *
13  * 
14  * 14    8/23/99 8:38p Andsager
15  * Added see_all debug console command for turning radar range infinite in
16  * nebula (but not targeting).
17  * 
18  * 13    6/10/99 3:43p Dave
19  * Do a better job of syncing text colors to HUD gauges.
20  * 
21  * 12    6/07/99 4:21p Andsager
22  * Add HUD color for tagged object.  Apply to target and radar.
23  * 
24  * 11    6/02/99 12:52p Andsager
25  * Added team-wide ship visibility.  Implemented for player.
26  * 
27  * 10    1/25/99 5:03a Dave
28  * First run of stealth, AWACS and TAG missile support. New mission type
29  * :)
30  * 
31  * 9     1/12/99 5:45p Dave
32  * Moved weapon pipeline in multiplayer to almost exclusively client side.
33  * Very good results. Bandwidth goes down, playability goes up for crappy
34  * connections. Fixed object update problem for ship subsystems.
35  * 
36  * 8     12/30/98 9:34a Jasen
37  * updated coords for hi res
38  * 
39  * 7     12/29/98 7:29p Dave
40  * Added some missing hi-res hud coord globalizations.
41  * 
42  * 6     12/29/98 2:30p Jasen
43  * added some new coords for 1024 HUD stuff
44  * 
45  * 5     12/28/98 3:17p Dave
46  * Support for multiple hud bitmap filenames for hi-res mode.
47  * 
48  * 4     11/05/98 4:18p Dave
49  * First run nebula support. Beefed up localization a bit. Removed all
50  * conditional compiles for foreign versions. Modified mission file
51  * format.
52  * 
53  * 3     10/13/98 9:29a Dave
54  * Started neatening up freespace.h. Many variables renamed and
55  * reorganized. Added AlphaColors.[h,cpp]
56  * 
57  * 2     10/07/98 10:53a Dave
58  * Initial checkin.
59  * 
60  * 1     10/07/98 10:51a Dave
61  * 
62  * 90    8/28/98 3:29p Dave
63  * EMP effect done. AI effects may need some tweaking as required.
64  * 
65  * 89    8/25/98 1:48p Dave
66  * First rev of EMP effect. Player side stuff basically done. Next comes
67  * AI code.
68  * 
69  * 88    6/12/98 4:52p Hoffoss
70  * Added support for special characters in in forgeign languages.
71  * 
72  * 87    6/09/98 10:31a Hoffoss
73  * Created index numbers for all xstr() references.  Any new xstr() stuff
74  * added from here on out should be added to the end if the list.  The
75  * current list count can be found in FreeSpace.cpp (search for
76  * XSTR_SIZE).
77  * 
78  * 86    5/19/98 10:26a John
79  * Fixed bug with radar blips not drawing in hardware.
80  * 
81  * 85    5/19/98 9:12a John
82  * Made radar blips render as font characters 132 and 133.   Needs a new
83  * font01.vf in the data tree.
84  * 
85  * 84    5/08/98 11:22a Allender
86  * fix ingame join trouble.  Small messaging fix.  Enable collisions for
87  * friendlies again
88  * 
89  * 83    5/01/98 12:24p Jim
90  * don't process radar_plot_obj on the standalone server
91  * 
92  * 82    4/07/98 4:05p Lawrance
93  * Only show hostile bombs on radar.
94  * 
95  * 81    3/26/98 5:26p John
96  * added new paging code. nonfunctional.
97  * 
98  * 80    3/15/98 3:11p Lawrance
99  * Always draw target radar blip bright.
100  * 
101  * 79    3/11/98 5:33p Lawrance
102  * Support rendering and targeting of jump nodes
103  * 
104  * 78    3/03/98 8:12p Lawrance
105  * Draw cargo as gray dots
106  * 
107  * 77    2/22/98 4:30p John
108  * More string externalization classification
109  * 
110  * 76    2/22/98 2:48p John
111  * More String Externalization Classification
112  * 
113  * 75    2/21/98 3:26p Lawrance
114  * Improve how blips get drawn for ships immune to sensors.
115  * 
116  * 74    2/16/98 11:58p Lawrance
117  * Add support for SF_HIDDEN_FROM_SENSORS flag.
118  * 
119  * 73    2/13/98 4:08p Lawrance
120  * Use more accurate distance formula when plotting radar dots... fixes
121  * "dead zone" black spot.
122  * 
123  * 72    2/12/98 4:58p Lawrance
124  * Change to new flashing method.
125  * 
126  * 71    2/11/98 12:04a Lawrance
127  * Only show bombs on radar, change code to use much less data.
128  * 
129  * 70    2/10/98 11:46a Lawrance
130  * Ensure TEAM_TRAITOR views other TEAM_TRAITOR ships as hostile.
131  * 
132  *
133  * $NoKeywords: $
134  *
135  */
136
137
138 #include "pstypes.h"
139 #include "font.h"
140 #include "floating.h"
141 #include "2d.h"
142 #include "3d.h"
143 #include "vecmat.h"
144 #include "palman.h"
145 #include "bmpman.h"
146 #include "object.h"
147 #include "ship.h"
148 #include "player.h"
149 #include "weapon.h"
150 #include "timer.h"
151 #include "ai.h"
152 #include "key.h"
153 #include "hud.h"
154 #include "hudtarget.h"
155 #include "hudconfig.h"
156 #include "subsysdamage.h"
157 #include "gamesnd.h"
158 #include "radar.h"
159 #include "linklist.h"
160 #include "multi.h"
161 #include "emp.h"
162 #include "freespace.h"
163 #include "localize.h"
164 #include "awacs.h"
165
166 int Radar_radius[GR_NUM_RESOLUTIONS][2] = {
167         { // GR_640
168                 120, 100
169         },
170         { // GR_1024
171                 192, 160
172         }
173 };
174
175 float Radar_center[GR_NUM_RESOLUTIONS][2] = {
176         { // GR_640
177                 322.0f, 422.0f
178         },
179         { // GR_1024
180                 515.0f, 675.0f
181         }
182 };
183
184 int Radar_coords[GR_NUM_RESOLUTIONS][2] = {
185         { // GR_640
186                 257, 369
187         }, 
188         { // GR_1024
189                 411, 590
190         }
191 };
192 char Radar_fname[GR_NUM_RESOLUTIONS][MAX_FILENAME_LEN] = {
193         "radar1",
194         "2_radar1"
195 };
196
197 int Radar_blip_radius_normal[GR_NUM_RESOLUTIONS] = {
198         2,
199         4
200 };
201 int Radar_blip_radius_target[GR_NUM_RESOLUTIONS] = {
202         5,
203         8
204 };
205
206 #define BLIP_MUTATE_TIME        100
207
208 float radx, rady;
209
210 #define MAX_RADAR_LEVELS        2               // bright and dim radar dots are allowed
211
212 #define BLIP_CURRENT_TARGET     (1<<0)
213 #define BLIP_DRAW_DIM                   (1<<1)  // object is farther than Radar_dim_range units away
214 #define BLIP_DRAW_DISTORTED     (1<<2)  // object is resistant to sensors, so draw distorted
215
216 typedef struct blip     {
217         blip    *prev, *next;
218         int     x, y, rad;
219         int     flags;  // BLIP_ flags defined above
220 } blip;
221
222 #define MAX_BLIPS 150
223 typedef struct rcol {
224         ubyte   r, g, b;
225 } rcol;
226
227 #define MAX_RADAR_COLORS                9
228
229 #define RCOL_HOSTILE                    0
230 #define RCOL_FRIENDLY                   1
231 #define RCOL_UNKNOWN                    2
232 #define RCOL_NEUTRAL                    3
233 #define RCOL_BOMB                               4
234 #define RCOL_NAVBUOYS                   5
235 #define  RCOL_WARPING_SHIP              6
236 #define RCOL_JUMP_NODE                  7
237 #define RCOL_TAGGED                             8
238
239 static float    Radar_dim_range;                                        // range at which we start dimming the radar blips
240 static int              Radar_calc_dim_dist_timer;              // timestamp at which we recalc Radar_dim_range
241
242 #define NUM_FLICKER_TIMERS      2
243 static int Radar_flicker_timer[NUM_FLICKER_TIMERS];                                     // timestamp used to flicker blips on and off
244 static int Radar_flicker_on[NUM_FLICKER_TIMERS];                                                // status of flickering
245
246 #define RADAR_BLIP_BRIGHT               0                               
247 #define RADAR_BLIP_DIM                  1
248
249 rcol Radar_color_rgb[MAX_RADAR_LEVELS][MAX_RADAR_COLORS] = {
250         {{ 0xff, 0x00, 0x00},           // hostile                      (red)
251         { 0x00, 0xff, 0x00},                    // friendly                     (green)
252         { 0xff, 0x00, 0xff},                    // unknown                      (purple)
253         { 0xff, 0x00, 0x00},                    //      neutral                 (red)
254         { 0x7f, 0x7f, 0x00},                    // homing missile (yellow)
255         { 0x7f, 0x7f, 0x7f},                    // navbuoy or cargo     (gray)
256         { 0x00, 0x00, 0xff},                    // warp ship            (blue)
257         { 0x7f, 0x7f, 0x7f},                    // jump node            (gray)
258         { 0xff, 0xff, 0x00}},           // tagged                       (yellow)
259
260         // 1/3 intensity of above colors
261         {{ 0x7f, 0x00, 0x00},           // hostile                      (red)
262         { 0x00, 0x7f, 0x00},                    // friendly                     (green)
263         { 0x7f, 0x00, 0x7f},                    // unknown                      (purple)
264         { 0x7f, 0x00, 0x00},                    //      neutral                 (red)
265         { 0x40, 0x40, 0x00},                    // homing missile (yellow)
266         { 0x40, 0x40, 0x40},                    // navbuoy or cargo     (gray)
267         { 0x00, 0x00, 0x7f},                    // warp ship            (blue)
268         { 0x40, 0x40, 0x40},                    // jump node            (gray)
269         { 0x7f, 0x7f, 0x00}},           // tagged                       (yellow)
270 };
271
272 color Radar_colors[MAX_RADAR_LEVELS][MAX_RADAR_COLORS];
273
274 blip    Blip_bright_list[MAX_RADAR_COLORS];             // linked list of bright blips
275 blip    Blip_dim_list[MAX_RADAR_COLORS];                        // linked list of dim blips
276 blip    Blips[MAX_BLIPS];                                                               // blips pool
277 int     N_blips;                                                                                        // next blip index to take from pool
278
279 float Radar_farthest_dist = 1000.0f;
280 static int Blip_mutate_id;
281
282 static int Radar_static_playing;                        // is static currently playing on the radar?
283 static int Radar_static_next;                           // next time to toggle static on radar
284 static int Radar_avail_prev_frame;              // was radar active last frame?
285 static int Radar_death_timer;                           // timestamp used to play static on radar
286 int Radar_static_looping;                                       // id for looping radar static sound
287
288 static hud_frames Radar_gauge;
289
290 int Radar_dist_coords[GR_NUM_RESOLUTIONS][RR_MAX_RANGES][2] = 
291 {
292         { // GR_640
293                 {367, 461},                                                             // short
294                 {364, 461},                                                             // long
295                 {368, 461}                                                              // infinity
296         },
297         { // GR_1024
298                 {595, 740},                                                             // short
299                 {592, 740},                                                             // long
300                 {596, 741}                                                              // infinity
301         }
302 };
303
304 // forward declarations
305 void draw_radar_blips(int desired_color, int is_dim, int distort=0);
306
307 void radar_init()
308 {
309         int i,j;
310
311         Radar_gauge.first_frame = bm_load_animation(Radar_fname[gr_screen.res], &Radar_gauge.num_frames);
312         if ( Radar_gauge.first_frame < 0 ) {
313                 Warning(LOCATION,"Cannot load hud ani: %s\n", Radar_fname[gr_screen.res]);
314         }
315
316         for (i=0; i<MAX_RADAR_LEVELS; i++ )     {
317                 for (j=0; j<MAX_RADAR_COLORS; j++ )     {
318                         gr_init_alphacolor( &Radar_colors[i][j], Radar_color_rgb[i][j].r, Radar_color_rgb[i][j].g, Radar_color_rgb[i][j].b, 255 );
319                 }
320         }
321
322         Blip_mutate_id  = 1;
323 }
324
325 // determine what color the object blip should be drawn as
326 int radar_blip_color(object *objp)
327 {
328         int     color = 0;
329         ship    *shipp = NULL;
330
331         switch(objp->type) {
332         case OBJ_SHIP:
333                 shipp = &Ships[objp->instance];
334                 if ( shipp->flags & SF_ARRIVING_STAGE_1 )       {
335                         color = RCOL_WARPING_SHIP;
336                 } else if ( ship_is_tagged(objp) ) {
337                         color = RCOL_TAGGED;
338                 } else if ( Ship_info[shipp->ship_info_index].flags & (SIF_NAVBUOY|SIF_CARGO) ) {
339                         color = RCOL_NAVBUOYS;
340                 } else {
341                         if ( (Player_ship->team == shipp->team) && (Player_ship->team != TEAM_TRAITOR) ) {
342                                 color = RCOL_FRIENDLY;
343                         } else {
344                                 switch (shipp->team) {
345                                 case TEAM_FRIENDLY:
346                                 case TEAM_HOSTILE:
347                                 case TEAM_TRAITOR:
348                                         color = RCOL_HOSTILE;
349                                         break;
350                                 case TEAM_NEUTRAL:
351                                         color = RCOL_NEUTRAL;
352                                         break;
353                                 case TEAM_UNKNOWN:
354                                         color = RCOL_UNKNOWN;
355                                         break;
356                                 default:
357                                         color = RCOL_HOSTILE;
358                                         Int3(); //      Bogus team id in shipp->team
359                                         break;
360                                 }
361                         }
362                 }
363                 break;
364         case OBJ_WEAPON:
365                 color = RCOL_BOMB;
366                 break;
367         case OBJ_JUMP_NODE:
368                 color = RCOL_JUMP_NODE;
369                 break;
370         default:
371                 Error(LOCATION, "Illegal ship type in radar.");
372                 break;
373         }
374
375         return color;
376 }
377
378 int See_all = FALSE;
379 DCF_BOOL(see_all, See_all)
380
381 void radar_plot_object( object *objp )  
382 {
383         vector  pos, tempv;
384         float           dist, rscale, zdist, max_radar_dist;
385         int             xpos, ypos, color=0;
386         vector  *world_pos = &objp->pos;        
387         float           awacs_level;
388
389         // don't process anything here.  Somehow, a jumpnode object caused this function
390         // to get entered on server side.
391         if( Game_mode & GM_STANDALONE_SERVER ){
392                 return;
393         }
394
395         // multiplayer clients ingame joining should skip this function
396         if ( MULTIPLAYER_CLIENT && (Net_player->flags & NETINFO_FLAG_INGAME_JOIN) ){
397                 return;
398         }
399
400         // get team-wide awacs level for the object if not ship
401         int ship_is_visible = 0;
402         if (objp->type == OBJ_SHIP) {
403                 if (Player_ship != NULL) {
404                         if (ship_is_visible_by_team(objp->instance, Player_ship->team)) {
405                                 ship_is_visible = 1;
406                         }
407                 }
408         }
409
410         // only check awacs level if ship is not visible by team
411         awacs_level = 1.5f;
412         if (Player_ship != NULL && !ship_is_visible) {
413                 awacs_level = awacs_get_level(objp, Player_ship);
414         }
415
416         // if the awacs level is unviewable - bail
417         if(awacs_level < 0.0f && !See_all){
418                 return;
419         }
420
421         // Apply object type filters    
422         switch ( objp->type ) {
423         case OBJ_SHIP:
424                 // Place to cull ships, such as NavBuoys                
425                 break;
426                 
427         case OBJ_JUMP_NODE:
428                 // filter jump nodes here if required
429                 break;
430
431         case OBJ_WEAPON: {
432                 // if not a bomb, return
433                 if ( !(Weapon_info[Weapons[objp->instance].weapon_info_index].wi_flags & WIF_BOMB) ) {
434                         return;
435                 }
436
437                 // if bomb is on same team as player, return
438                 if ( (obj_team(objp) == Player_ship->team) && (Player_ship->team != TEAM_TRAITOR) ) {
439                         return;
440                 }
441                 break;
442         }
443
444         default:
445                 return;                 // if any other kind of object, don't want to show on radar
446                 break;
447         } // end switch
448
449         
450         // JAS -- new way of getting the rotated point that doesn't require this to be
451         // in a g3_start_frame/end_frame block.
452         vm_vec_sub(&tempv,world_pos,&Player_obj->pos);
453         vm_vec_rotate( &pos, &tempv, &Player_obj->orient );
454
455         // Apply range filter
456         dist = vm_vec_dist(world_pos, &Player_obj->pos);
457         max_radar_dist = Radar_ranges[HUD_config.rp_dist];
458         if ( dist > max_radar_dist ){
459                 return;
460         }
461
462         if ( dist < pos.z ) {
463                 rscale = 0.0f;
464         } else {
465                 rscale = (float) acos( pos.z/dist ) / 3.14159f;         //2.0f;  
466         }
467
468         zdist = fl_sqrt( (pos.x*pos.x)+(pos.y*pos.y) );
469
470         float new_x_dist, clipped_x_dist;
471         float new_y_dist, clipped_y_dist;
472
473         if (zdist < 0.01f ) {
474                 new_x_dist = 0.0f;
475                 new_y_dist = 0.0f;
476         }
477         else {
478                 new_x_dist = (pos.x/zdist) * rscale * radx;
479                 new_y_dist = (pos.y/zdist) * rscale * rady;
480
481                 // force new_x_dist and new_y_dist to be inside the radar
482
483                 float hypotenuse;
484                 float max_radius;
485
486                 hypotenuse = (float)_hypot(new_x_dist, new_y_dist);
487                 max_radius = i2fl(Radar_radius[gr_screen.res][0] - 5);
488
489                 if (hypotenuse >= (max_radius) ) {
490                         clipped_x_dist = max_radius * (new_x_dist / hypotenuse);
491                         clipped_y_dist = max_radius * (new_y_dist / hypotenuse);
492                         new_x_dist = clipped_x_dist;
493                         new_y_dist = clipped_y_dist;
494                 }
495         }
496
497         xpos = fl2i( Radar_center[gr_screen.res][0] + new_x_dist );
498         ypos = fl2i( Radar_center[gr_screen.res][1] - new_y_dist );
499
500         color = radar_blip_color(objp);
501
502         // Determine the distance at which we will dim the radar blip
503         if ( timestamp_elapsed(Radar_calc_dim_dist_timer) ) {
504                 Radar_calc_dim_dist_timer=timestamp(1000);
505                 Radar_dim_range = player_farthest_weapon_range();
506                 if ( Radar_dim_range <= 0 ) {
507                         Radar_dim_range=1500.0f;
508                 }
509         }
510
511         blip    *b;
512         int blip_dim=0;
513
514         if ( dist > Radar_dim_range ) {
515                 blip_dim=1;
516         }
517
518         if ( N_blips >= MAX_BLIPS ) {
519                 // out of blips, don't plot
520                 Int3();
521                 return;
522         }
523
524         b = &Blips[N_blips];
525         b->flags=0;
526
527         // flag the blip as a current target if it is
528         if (OBJ_INDEX(objp) == Player_ai->target_objnum)        {
529                 b->flags |= BLIP_CURRENT_TARGET;
530                 blip_dim = 0;
531         }
532
533         if ( blip_dim ) {
534                 list_append( &Blip_dim_list[color], b );
535         } else {
536                 list_append( &Blip_bright_list[color], b );
537         }
538
539         b->x = xpos;
540         b->y = ypos;
541
542         // see if blip should be drawn distorted
543         if (objp->type == OBJ_SHIP) {
544                 // ships specifically hidden from sensors
545                 if ( Ships[objp->instance].flags & SF_HIDDEN_FROM_SENSORS ) {
546                         b->flags |= BLIP_DRAW_DISTORTED;
547                 }
548
549                 // determine if its AWACS distorted
550                 if ( awacs_level < 1.0f ){
551                         b->flags |= BLIP_DRAW_DISTORTED;
552                 }
553         }                               
554
555         N_blips++;
556 }
557
558 // set N_blips for each color/brightness level to zero
559 void radar_null_nblips()
560 {
561         int i;
562
563         N_blips=0;
564
565         for (i=0; i<MAX_RADAR_COLORS; i++) {
566                 list_init(&Blip_bright_list[i]);
567                 list_init(&Blip_dim_list[i]);
568         }
569 }
570
571 // radar_mission_init() is called at the start of each mission.  
572 void radar_mission_init()
573 {
574         int i;
575
576         Blip_mutate_id                          = 1;
577         Radar_death_timer                       = 0;
578         Radar_static_playing            = 0;
579         Radar_static_next                       = 0;
580         Radar_avail_prev_frame  = 1;
581         Radar_calc_dim_dist_timer = timestamp(0);
582
583         for ( i=0; i<NUM_FLICKER_TIMERS; i++ ) {
584                 Radar_flicker_timer[i]=timestamp(0);
585                 Radar_flicker_on[i]=0;
586         }
587 }
588
589 #define SMALL_BLIP_CHAR (Lcl_special_chars + 5)
590 #define LARGE_BLIP_CHAR (Lcl_special_chars + 6)
591
592 int Small_blip_offset_x = 0;
593 int Small_blip_offset_y = 0;
594 int Large_blip_offset_x = 0;
595 int Large_blip_offset_y = 0;
596
597 char Small_blip_string[2];
598 char Large_blip_string[2];
599
600 void radar_frame_init()
601 {
602         radar_null_nblips();
603         radx = i2fl(Radar_radius[gr_screen.res][0])/2.0f;
604         rady = i2fl(Radar_radius[gr_screen.res][1])/2.0f;
605
606         int w,h;
607         gr_set_font(FONT1);
608
609         Small_blip_string[0] = ubyte(SMALL_BLIP_CHAR);
610         Small_blip_string[1] = 0;
611         gr_get_string_size( &w, &h, Small_blip_string );
612         Small_blip_offset_x = -w/2;
613         Small_blip_offset_y = -h/2;
614
615         Large_blip_string[0] = ubyte(LARGE_BLIP_CHAR);
616         Large_blip_string[1] = 0;
617         gr_get_string_size( &w, &h, Large_blip_string );
618         Large_blip_offset_x = -w/2;
619         Large_blip_offset_y = -h/2;
620 }
621
622 void radar_draw_circle( int x, int y, int rad )
623 {
624         if ( rad == Radar_blip_radius_target[gr_screen.res] )   {
625                 gr_string( Large_blip_offset_x+x, Large_blip_offset_y+y, Large_blip_string );
626         } else {
627                 // rad = RADAR_BLIP_RADIUS_NORMAL;
628                 gr_string( Small_blip_offset_x+x, Small_blip_offset_y+y, Small_blip_string );
629         }
630 }
631
632 // radar is damaged, so make blips dance around
633 void radar_blip_draw_distorted(blip *b)
634 {
635         int xdiff, ydiff;
636         float scale;
637         xdiff = -10 + rand()%20;
638         ydiff = -10 + rand()%20;
639
640         // maybe scale the effect if EMP is active
641         if(emp_active_local()){
642                 scale = emp_current_intensity();
643
644                 xdiff = (int)((float)xdiff * scale);
645                 ydiff = (int)((float)ydiff * scale);
646         }
647
648         radar_draw_circle( b->x+xdiff, b->y+ydiff, b->rad ); 
649 }
650
651 // blip is for a target immune to sensors, so cause to flicker in/out with mild distortion
652 void radar_blip_draw_flicker(blip *b)
653 {
654         int xdiff=0, ydiff=0, flicker_index;
655
656         if ( (b-Blips) & 1 ) {
657                 flicker_index=0;
658         } else {
659                 flicker_index=1;
660         }
661
662         if ( timestamp_elapsed(Radar_flicker_timer[flicker_index]) ) {
663                 Radar_flicker_timer[flicker_index] = timestamp_rand(50,1000);
664                 Radar_flicker_on[flicker_index] ^= 1;
665         }
666
667         if ( !Radar_flicker_on[flicker_index] ) {
668                 return;
669         }
670
671         if ( rand() & 1 ) {
672                 xdiff = -2 + rand()%4;
673                 ydiff = -2 + rand()%4;
674         }
675
676         radar_draw_circle( b->x+xdiff, b->y+ydiff, b->rad ); 
677 }
678
679 // Draw all the active radar blips
680 void draw_radar_blips(int rcol, int is_dim, int distort)
681 {
682         blip    *b=NULL;
683         blip    *blip_head=NULL;
684
685         // Need to set font.
686         gr_set_font(FONT1);
687
688         if ( is_dim ) {
689                 blip_head = &Blip_dim_list[rcol];
690         } else {
691                 blip_head = &Blip_bright_list[rcol];
692         }
693
694         for ( b = GET_FIRST(blip_head); b !=END_OF_LIST(blip_head); b = GET_NEXT(b) )   {
695
696                 Assert((rcol >= 0) && (rcol < MAX_RADAR_COLORS));
697
698                 if ( is_dim ) {
699                         gr_set_color_fast( &Radar_colors[RADAR_BLIP_DIM][rcol] );
700                 } else {
701                         gr_set_color_fast( &Radar_colors[RADAR_BLIP_BRIGHT][rcol] );
702                 }
703
704                 if (b->flags & BLIP_CURRENT_TARGET) {
705                         // draw cool blip to indicate current target
706                         b->rad = Radar_blip_radius_target[gr_screen.res];                               
707                 } else {
708                         b->rad = Radar_blip_radius_normal[gr_screen.res];
709                 }
710
711                 if ( distort ) { 
712                         radar_blip_draw_distorted(b);
713                 } else if ( b->flags & BLIP_DRAW_DISTORTED ) {
714                         radar_blip_draw_flicker(b);
715                 } else{
716                         radar_draw_circle( b->x, b->y, b->rad );
717                 }
718         }
719 }
720
721 // Draw the radar blips
722 // input:       distorted       =>              0 (default) to draw normal, 1 to draw distorted 
723 void radar_draw_blips_sorted(int distort)
724 {
725         // draw dim blips first
726         draw_radar_blips(RCOL_JUMP_NODE, 1, distort);
727         draw_radar_blips(RCOL_WARPING_SHIP, 1, distort);
728         draw_radar_blips(RCOL_NAVBUOYS, 1, distort);
729         draw_radar_blips(RCOL_FRIENDLY, 1, distort);
730         draw_radar_blips(RCOL_UNKNOWN, 1, distort);
731         draw_radar_blips(RCOL_BOMB, 1, distort);
732         draw_radar_blips(RCOL_NEUTRAL, 1, distort);
733         draw_radar_blips(RCOL_HOSTILE, 1, distort);
734         draw_radar_blips(RCOL_TAGGED, 1, distort);
735
736         // draw bright blips
737         draw_radar_blips(RCOL_JUMP_NODE, 0, distort);
738         draw_radar_blips(RCOL_WARPING_SHIP, 0, distort);
739         draw_radar_blips(RCOL_NAVBUOYS, 0, distort);
740         draw_radar_blips(RCOL_FRIENDLY, 0, distort);
741         draw_radar_blips(RCOL_UNKNOWN, 0, distort);
742         draw_radar_blips(RCOL_BOMB, 0, distort);
743         draw_radar_blips(RCOL_NEUTRAL, 0, distort);
744         draw_radar_blips(RCOL_HOSTILE, 0, distort);
745         draw_radar_blips(RCOL_TAGGED, 0, distort);
746 }
747
748 static int test_time = 1;
749 void radar_draw_range()
750 {
751         char buf[32];
752
753         // hud_set_bright_color();
754         hud_set_gauge_color(HUD_RADAR, HUD_C_BRIGHT);
755
756         switch ( HUD_config.rp_dist ) {
757
758         case RR_SHORT:
759                 gr_printf(Radar_dist_coords[gr_screen.res][RR_SHORT][0], Radar_dist_coords[gr_screen.res][RR_SHORT][1], XSTR( "2k", 467));
760                 break;
761
762         case RR_LONG:
763                 gr_printf(Radar_dist_coords[gr_screen.res][RR_LONG][0], Radar_dist_coords[gr_screen.res][RR_LONG][1], XSTR( "10k", 468));
764                 break;
765
766         case RR_INFINITY:
767                 sprintf(buf, NOX("%c"), Lcl_special_chars);
768                 gr_printf(Radar_dist_coords[gr_screen.res][RR_INFINITY][0], Radar_dist_coords[gr_screen.res][RR_INFINITY][1], buf);
769                 break;
770
771         default:
772                 Int3(); // can't happen (get Alan if it does)
773                 break;
774         }
775
776         hud_set_default_color();
777 }
778
779 void radar_frame_render(float frametime)
780 {
781         float   sensors_str;
782         int ok_to_blit_radar;
783
784         ok_to_blit_radar = 1;
785
786         sensors_str = ship_get_subsystem_strength( Player_ship, SUBSYSTEM_SENSORS );
787
788         if ( ship_subsys_disrupted(Player_ship, SUBSYSTEM_SENSORS) ) {
789                 sensors_str = MIN_SENSOR_STR_TO_RADAR-1;
790         }
791
792         // note that on lowest skill level, there is no radar effects due to sensors damage
793         if ( (Game_skill_level == 0) || (sensors_str > SENSOR_STR_RADAR_NO_EFFECTS) ) {
794                 Radar_static_playing = 0;
795                 Radar_static_next = 0;
796                 Radar_death_timer = 0;
797                 Radar_avail_prev_frame = 1;
798         } else if ( sensors_str < MIN_SENSOR_STR_TO_RADAR ) {
799                 if ( Radar_avail_prev_frame ) {
800                         Radar_death_timer = timestamp(2000);
801                         Radar_static_next = 1;
802                 }
803                 Radar_avail_prev_frame = 0;
804         } else {
805                 Radar_death_timer = 0;
806                 if ( Radar_static_next == 0 )
807                         Radar_static_next = 1;
808         }
809
810         if ( timestamp_elapsed(Radar_death_timer) ) {
811                 ok_to_blit_radar = 0;
812         }
813
814         hud_set_gauge_color(HUD_RADAR);
815         radar_blit_gauge();
816         radar_draw_range();
817
818         if ( timestamp_elapsed(Radar_static_next) ) {
819                 Radar_static_playing ^= 1;
820                 Radar_static_next = timestamp_rand(50, 750);
821         }
822
823         // if the emp effect is active, always draw the radar wackily
824         if(emp_active_local()){
825                 Radar_static_playing = 1;
826         }
827
828         if ( ok_to_blit_radar ) {
829                 if ( Radar_static_playing ) {
830                         radar_draw_blips_sorted(1);     // passing 1 means to draw distorted
831                         if ( Radar_static_looping == -1 ) {
832                                 Radar_static_looping = snd_play_looping(&Snds[SND_STATIC]);
833                         }
834                 } else {
835                         radar_draw_blips_sorted();
836                         if ( Radar_static_looping != -1 ) {
837                                 snd_stop(Radar_static_looping);
838                                 Radar_static_looping = -1;
839                         }
840                 }
841         } else {
842                 if ( Radar_static_looping != -1 ) {
843                         snd_stop(Radar_static_looping);
844                         Radar_static_looping = -1;
845                 }
846         }
847 }
848
849 void radar_blit_gauge()
850 {
851         gr_set_bitmap(Radar_gauge.first_frame+1);
852         gr_aabitmap( Radar_coords[gr_screen.res][0], Radar_coords[gr_screen.res][1] );
853
854
855 void radar_page_in()
856 {
857         bm_page_in_aabitmap( Radar_gauge.first_frame, Radar_gauge.num_frames );
858 }