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