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