FS2_Open
Open source remastering of the Freespace 2 engine
radar.cpp
Go to the documentation of this file.
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 the
6  * source.
7  *
8 */
9 
10 
11 
12 
13 
14 #include "bmpman/bmpman.h"
15 #include "freespace2/freespace.h"
16 #include "gamesnd/gamesnd.h"
17 #include "globalincs/linklist.h"
18 #include "graphics/font.h"
19 #include "iff_defs/iff_defs.h"
20 #include "io/timer.h"
21 #include "jumpnode/jumpnode.h"
22 #include "localization/localize.h"
23 #include "network/multi.h"
24 #include "object/object.h"
25 #include "playerman/player.h"
26 #include "radar/radar.h"
27 #include "ship/awacs.h"
28 #include "ship/ship.h"
29 #include "ship/subsysdamage.h"
30 #include "weapon/emp.h"
31 #include "weapon/weapon.h"
32 
33 extern int radar_target_id_flags;
34 
36 HudGaugeRadar(HUD_OBJECT_RADAR_STD, 255, 255, 255)
37 {
38  gr_init_alphacolor( &radar_crosshairs, 255, 255, 255, 196);
39 }
40 
42 {
43  Radar_center_offsets[0] = x;
44  Radar_center_offsets[1] = y;
45 }
46 
48 {
49  Radar_gauge.first_frame = bm_load_animation(fname, &Radar_gauge.num_frames);
50  if ( Radar_gauge.first_frame < 0 ) {
51  Warning(LOCATION,"Cannot load hud ani: %s\n", fname);
52  }
53 }
54 
56 {
57  int xdiff, ydiff;
58  float scale;
59  xdiff = -10 + rand()%20;
60  ydiff = -10 + rand()%20;
61 
62  // maybe scale the effect if EMP is active
63  if(emp_active_local()){
64  scale = emp_current_intensity();
65 
66  xdiff = (int)((float)xdiff * scale);
67  ydiff = (int)((float)ydiff * scale);
68  }
69 
70  drawContactCircle(x + xdiff, y + ydiff, b->rad);
71 }
73 {
74  int xdiff=0, ydiff=0, flicker_index;
75 
76  if ( (b-Blips) & 1 ) {
77  flicker_index=0;
78  } else {
79  flicker_index=1;
80  }
81 
82  if ( timestamp_elapsed(Radar_flicker_timer[flicker_index]) ) {
83  Radar_flicker_timer[flicker_index] = timestamp_rand(50,1000);
84  Radar_flicker_on[flicker_index] ^= 1;
85  }
86 
87  if ( !Radar_flicker_on[flicker_index] ) {
88  return;
89  }
90 
91  if ( rand() & 1 ) {
92  xdiff = -2 + rand()%4;
93  ydiff = -2 + rand()%4;
94  }
95 
96  drawContactCircle(x + xdiff, y + ydiff, b->rad);
97 }
99 {
100  renderBitmap(Radar_gauge.first_frame+1, position[0], position[1] );
101 }
102 void HudGaugeRadarStd::drawBlips(int blip_type, int bright, int distort)
103 {
104  blip *b = NULL;
105  blip *blip_head = NULL;
106  int x, y;
107 
108  Assert((blip_type >= 0) && (blip_type < MAX_BLIP_TYPES));
109 
110 
111  // Need to set font.
113 
114 
115  // get the appropriate blip list
116  if (bright)
117  blip_head = &Blip_bright_list[blip_type];
118  else
119  blip_head = &Blip_dim_list[blip_type];
120 
121 
122  // draw all blips of this type
123  for (b = GET_FIRST(blip_head); b != END_OF_LIST(blip_head); b = GET_NEXT(b))
124  {
126  plotBlip(b, &x, &y);
127 
128  // maybe draw cool blip to indicate current target
129  if (b->flags & BLIP_CURRENT_TARGET)
130  {
132  current_target_x = x;
133  current_target_y = y;
134  }
135  else
136  {
138  }
139 
140  // maybe distort blip
141  if (distort)
142  {
143  blipDrawDistorted(b, x, y);
144  }
145  else if (b->flags & BLIP_DRAW_DISTORTED)
146  {
147  blipDrawFlicker(b, x, y);
148  }
149  else
150  {
151  if (b->radar_image_2d == -1 && b->radar_color_image_2d == -1)
152  drawContactCircle(x, y, b->rad);
153  else
155  }
156  }
157 }
159 {
160  current_target_x = 0;
161  current_target_y = 0;
162  // draw dim blips first, then bright blips
163  for (int is_bright = 0; is_bright < 2; is_bright++)
164  {
165  drawBlips(BLIP_TYPE_JUMP_NODE, is_bright, distort);
166  drawBlips(BLIP_TYPE_WARPING_SHIP, is_bright, distort);
167  drawBlips(BLIP_TYPE_NAVBUOY_CARGO, is_bright, distort);
168  drawBlips(BLIP_TYPE_NORMAL_SHIP, is_bright, distort);
169  drawBlips(BLIP_TYPE_BOMB, is_bright, distort);
170  drawBlips(BLIP_TYPE_TAGGED_SHIP, is_bright, distort);
171  }
172  // draw crosshairs last - if at all.
174  drawCrosshairs(current_target_x, current_target_y);
175  }
176 }
177 void HudGaugeRadarStd::drawContactCircle( int x, int y, int rad )
178 {
179  if ( rad == Radar_blip_radius_target ) {
181  if (Missiontime & 8192)
182  return;
183  }
185  } else {
186  // rad = RADAR_BLIP_RADIUS_NORMAL;
188  }
189 }
190 void HudGaugeRadarStd::drawContactImage( int x, int y, int rad, int idx, int clr_idx, int size )
191 {
192  // this we will move as ships.tbl option (or use for radar scaling etc etc)
193  //int size = 24;
194 
195  int w, h, old_bottom, old_bottom_unscaled, old_right, old_right_unscaled;
196  float scalef, wf, hf, xf, yf;
197  vec3d blip_scaler;
198 
199  if(bm_get_info(idx, &w, &h) < 0)
200  {
201  // Just if something goes terribly wrong
202  drawContactCircle(x, y, rad);
203  return;
204  }
205 
206  // just to make sure the missing casts wont screw the math
207  wf = (float) w;
208  hf = (float) h;
209  xf = (float) x;
210  yf = (float) y;
211 
212  // make sure we use the larger dimension for the scaling
213  // lets go case by case to make sure there are no probs
214  if (size == -1)
215  scalef = 1.0f;
216  else if ((h == w) && (size == h))
217  scalef = 1.0f;
218  else if ( h > w)
219  scalef = ((float) size) / hf;
220  else
221  scalef = ((float) size) / wf;
222 
223  Assert(scalef != 0);
224 
225  // animate the targeted icon - option 1 of highlighting the targets
226  if ( rad == Radar_blip_radius_target ) {
228  scalef *= 1.3f + (sinf(10 * f2fl(Missiontime)) * 0.3f);
229  }
231  if (Missiontime & 8192)
232  return;
233  }
235  scalef *= 1.3f;
236  }
237  }
238 
239  // setup the scaler
240  blip_scaler.xyz.x = scalef;
241  blip_scaler.xyz.y = scalef;
242  blip_scaler.xyz.z = 1.0f;
243 
244  old_bottom = gr_screen.clip_bottom;
245  old_bottom_unscaled = gr_screen.clip_bottom_unscaled;
246  gr_screen.clip_bottom = (int) (old_bottom/scalef);
247  gr_screen.clip_bottom_unscaled = (int) (old_bottom_unscaled/scalef);
248 
249  old_right = gr_screen.clip_right;
250  old_right_unscaled = gr_screen.clip_right_unscaled;
251  gr_screen.clip_right = (int) (old_right/scalef);
252  gr_screen.clip_right_unscaled = (int) (old_right_unscaled/scalef);
253 
254  // scale the drawing coordinates
255  x = (int) ((xf / scalef) - wf/2.0f);
256  y = (int) ((yf / scalef) - hf/2.0f);
257 
258  gr_push_scale_matrix(&blip_scaler);
259 
260  if ( idx >= 0 ) {
262  renderBitmap( x, y );
263  }
264 
265  if ( clr_idx >= 0 ) {
267  gr_bitmap(x, y);
269  }
270 
272 
273  gr_screen.clip_bottom = old_bottom;
274  gr_screen.clip_bottom_unscaled = old_bottom_unscaled;
275 
276  gr_screen.clip_right = old_right;
277  gr_screen.clip_right_unscaled = old_right_unscaled;
278 }
279 
280 void HudGaugeRadarStd::render(float frametime)
281 {
282  //WMC - This strikes me as a bit hackish
283  bool g3_yourself = !g3_in_frame();
284  if(g3_yourself)
285  g3_start_frame(1);
286 
287  float sensors_str;
288  int ok_to_blit_radar;
289 
290  ok_to_blit_radar = 1;
291 
293 
295  sensors_str = MIN_SENSOR_STR_TO_RADAR-1;
296  }
297 
298  // note that on lowest skill level, there is no radar effects due to sensors damage
299  if ( (Game_skill_level == 0) || (sensors_str > SENSOR_STR_RADAR_NO_EFFECTS) ) {
301  Radar_static_next = 0;
302  Radar_death_timer = 0;
304  } else if ( sensors_str < MIN_SENSOR_STR_TO_RADAR ) {
305  if ( Radar_avail_prev_frame ) {
307  Radar_static_next = 1;
308  }
310  } else {
311  Radar_death_timer = 0;
312  if ( Radar_static_next == 0 )
313  Radar_static_next = 1;
314  }
315 
317  ok_to_blit_radar = 0;
318  }
319 
320  setGaugeColor();
321  blitGauge();
322  drawRange();
323 
327  }
328 
329  // if the emp effect is active, always draw the radar wackily
330  if(emp_active_local()){
332  }
333 
334  if ( ok_to_blit_radar ) {
335  if ( Radar_static_playing ) {
336  drawBlipsSorted(1); // passing 1 means to draw distorted
337  if ( Radar_static_looping == -1 ) {
339  }
340  } else {
341  drawBlipsSorted(0);
342  if ( Radar_static_looping != -1 ) {
345  }
346  }
347  } else {
348  if ( Radar_static_looping != -1 ) {
351  }
352  }
353 
354  if(g3_yourself)
355  g3_end_frame();
356 }
358 {
359  bm_page_in_aabitmap( Radar_gauge.first_frame, Radar_gauge.num_frames );
360 }
361 
362 void HudGaugeRadarStd::plotBlip(blip *b, int *x, int *y)
363 {
364  float zdist, rscale;
365  vec3d *pos = &b->position;
366 
367  if (b->dist < pos->xyz.z) {
368  rscale = 0.0f;
369  } else {
370  rscale = (float) acosf(pos->xyz.z / b->dist) / PI; //2.0f;
371  }
372 
373  zdist = fl_sqrt((pos->xyz.x * pos->xyz.x) + (pos->xyz.y * pos->xyz.y));
374 
375  float new_x_dist, clipped_x_dist;
376  float new_y_dist, clipped_y_dist;
377 
378  if (zdist < 0.01f)
379  {
380  new_x_dist = 0.0f;
381  new_y_dist = 0.0f;
382  }
383  else
384  {
385  new_x_dist = (pos->xyz.x / zdist) * rscale * (Radar_radius[0]/2.0f);
386  new_y_dist = (pos->xyz.y / zdist) * rscale * (Radar_radius[1]/2.0f);
387 
388  // force new_x_dist and new_y_dist to be inside the radar
389 
390  float hypotenuse;
391  float max_radius;
392 
393  hypotenuse = (float) _hypot(new_x_dist, new_y_dist);
394  max_radius = i2fl(Radar_radius[0] - 5);
395 
396  if (hypotenuse >= max_radius)
397  {
398  clipped_x_dist = max_radius * (new_x_dist / hypotenuse);
399  clipped_y_dist = max_radius * (new_y_dist / hypotenuse);
400  new_x_dist = clipped_x_dist;
401  new_y_dist = clipped_y_dist;
402  }
403  }
404 
405  *x = fl2i(position[0] + Radar_center_offsets[0] + new_x_dist);
406  *y = fl2i(position[1] + Radar_center_offsets[1] - new_y_dist);
407 }
408 
410 {
411  int i,j,m;
412 
413  gr_set_color_fast(&radar_crosshairs);
414 
415  for(i = 0; i < 2; i++) {
416  m = (i * 2) - 1;
417  renderGradientLine(x + m*4,y,x + m*8,y);
418  }
419  for(j = 0; j < 2; j++) {
420  m = (j * 2) - 1;
421  renderGradientLine(x,y + m*4,x,y + m*8);
422  }
423 }
int timestamp(int delta_ms)
Definition: timer.cpp:226
void blipDrawFlicker(blip *b, int x, int y)
Definition: radar.cpp:72
int i
Definition: multi_pxo.cpp:466
fix Missiontime
Definition: systemvars.cpp:19
void pageIn()
Definition: radar.cpp:357
void render(float frametime)
Definition: radar.cpp:280
#define BLIP_TYPE_JUMP_NODE
Definition: radarsetup.h:62
int Large_blip_offset_x
Definition: radarsetup.h:123
GLfloat GLfloat GLfloat GLfloat h
Definition: Glext.h:7280
#define gr_pop_scale_matrix
Definition: 2d.h:898
SCP_vector< game_snd > Snds
Definition: gamesnd.cpp:19
blip Blips[MAX_BLIPS]
Definition: radarsetup.cpp:76
void drawContactImage(int x, int y, int rad, int idx, int clr_idx, int size)
Definition: radar.cpp:190
blip Blip_bright_list[MAX_BLIP_TYPES]
Definition: radarsetup.cpp:73
void initBitmaps(char *fname)
Definition: radar.cpp:47
void setGaugeColor(int bright_index=-4)
Definition: hud.cpp:445
void _cdecl void void _cdecl void _cdecl Warning(char *filename, int line, SCP_FORMAT_STRING const char *format,...) SCP_FORMAT_STRING_ARGS(3
Assert(pm!=NULL)
int rad
Definition: radarsetup.h:35
#define SENSOR_STR_RADAR_NO_EFFECTS
Definition: subsysdamage.h:40
Definition: pstypes.h:88
void gr_init_alphacolor(color *clr, int r, int g, int b, int alpha, int type)
Definition: 2d.cpp:1173
int bm_get_info(int handle, int *w, int *h, ubyte *flags, int *nframes, int *fps)
Gets info on the bitmap indexed by handle.
Definition: bmpman.cpp:769
struct vec3d::@225::@227 xyz
int base_h
Definition: hud.h:205
GLclampf f
Definition: Glext.h:7097
int Small_blip_offset_x
Definition: radarsetup.h:121
int Radar_radius[2]
Definition: radarsetup.h:105
#define f2fl(fx)
Definition: floating.h:37
int Radar_death_timer
Definition: radarsetup.h:116
void gr_set_screen_scale(int w, int h, int zoom_w, int zoom_h, int max_w, int max_h, int center_w, int center_h, bool force_stretch)
Definition: 2d.cpp:104
void gr_set_color_fast(color *dst)
Definition: 2d.cpp:1197
int Radar_static_looping
Definition: radarsetup.cpp:34
GLenum GLenum GLenum GLenum GLenum scale
Definition: Glext.h:8503
void gr_set_bitmap(int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha)
Definition: 2d.cpp:2105
GLsizeiptr size
Definition: Glext.h:5496
#define _hypot(x, y)
Definition: config.h:281
void renderGradientLine(int x1, int y1, int x2, int y2)
Definition: hud.cpp:873
color * blip_color
Definition: radarsetup.h:37
void drawContactCircle(int x, int y, int rad)
Definition: radar.cpp:177
int bm_load_animation(const char *real_filename, int *nframes, int *fps, int *keyframe, int can_drop_frames, int dir_type)
Loads a bitmap sequance so we can draw with it.
Definition: bmpman.cpp:1420
#define MIN_SENSOR_STR_TO_RADAR
Definition: subsysdamage.h:41
typedef int(SCP_EXT_CALLCONV *SCPDLL_PFVERSION)(SCPDLL_Version *)
hud gauge static
Definition: gamesnd.h:116
float ship_get_subsystem_strength(ship *shipp, int type)
Definition: ship.cpp:13446
#define HUD_OBJECT_RADAR_STD
Definition: hudparse.h:142
#define BLIP_DRAW_DISTORTED
Definition: radarsetup.h:80
int radar_image_2d
Definition: radarsetup.h:39
#define BLIP_TYPE_NORMAL_SHIP
Definition: radarsetup.h:67
int clip_right_unscaled
Definition: 2d.h:390
int Radar_blip_radius_target
Definition: radarsetup.h:111
vec3d position
Definition: radarsetup.h:38
int ship_subsys_disrupted(ship_subsys *ss)
Definition: ship.cpp:9033
#define MAX_BLIP_TYPES
Definition: radarsetup.h:69
int num_frames
Definition: hud.h:34
int radar_image_size
Definition: radarsetup.h:41
int base_w
Definition: hud.h:205
char Small_blip_string[2]
Definition: radarsetup.h:126
#define w(p)
Definition: modelsinc.h:68
float emp_current_intensity()
Definition: emp.cpp:634
#define RTIF_CROSSHAIRS
Definition: hud.h:83
int Radar_flicker_timer[NUM_FLICKER_TIMERS]
Definition: radarsetup.h:118
void drawRange()
Definition: radarsetup.cpp:467
int clip_bottom
Definition: 2d.h:388
int idx
Definition: multiui.cpp:761
#define RTIF_PULSATE
Definition: hud.h:85
void renderString(int x, int y, const char *str)
Definition: hud.cpp:665
int Radar_blip_radius_normal
Definition: radarsetup.h:110
GLint GLint GLint GLint GLint x
Definition: Glext.h:5182
#define gr_push_scale_matrix
Definition: 2d.h:897
ship * Player_ship
Definition: ship.cpp:124
int radar_target_id_flags
Definition: radarsetup.cpp:69
void drawBlipsSorted(int distort)
Definition: radar.cpp:158
float dist
Definition: radarsetup.h:44
void plotBlip(blip *b, int *x, int *y)
Definition: radar.cpp:362
int Small_blip_offset_y
Definition: radarsetup.h:122
int first_frame
Definition: hud.h:33
#define BLIP_TYPE_WARPING_SHIP
Definition: radarsetup.h:65
GLboolean GLboolean GLboolean b
Definition: Glext.h:5781
int clip_right
Definition: 2d.h:388
typedef float(SCP_EXT_CALLCONV *SCPTRACKIR_PFFLOATVOID)()
#define BLIP_TYPE_NAVBUOY_CARGO
Definition: radarsetup.h:63
#define BLIP_TYPE_BOMB
Definition: radarsetup.h:64
#define FONT1
Definition: font.h:65
#define RTIF_BLINK
Definition: hud.h:84
#define RTIF_ENLARGE
Definition: hud.h:86
char Large_blip_string[2]
Definition: radarsetup.h:127
int position[2]
Definition: hud.h:204
int clip_bottom_unscaled
Definition: 2d.h:390
void bm_page_in_aabitmap(int bitmapnum, int nframes)
Marks a texture as being used for this level, and is anti-aliased.
Definition: bmpman.cpp:2354
int g3_in_frame()
Definition: 3dsetup.cpp:66
#define SUBSYSTEM_SENSORS
Definition: model.h:59
#define fl2i(fl)
Definition: floating.h:33
#define g3_end_frame()
Definition: 3d.h:49
screen gr_screen
Definition: 2d.cpp:46
void drawBlips(int blip_type, int bright, int distort)
Definition: radar.cpp:102
blip Blip_dim_list[MAX_BLIP_TYPES]
Definition: radarsetup.cpp:74
int snd_play_looping(game_snd *gs, float pan, int start_loop, int stop_loop, float vol_scale, int scriptingUpdateVolume)
Definition: sound.cpp:822
#define fl_sqrt(fl)
Definition: floating.h:29
#define LOCATION
Definition: pstypes.h:245
void gr_reset_screen_scale()
Definition: 2d.cpp:176
#define timestamp_elapsed(stamp)
Definition: timer.h:102
#define PI
Definition: pstypes.h:303
hull_check pos
Definition: lua.cpp:5050
int Game_skill_level
Definition: fredstubs.cpp:170
const GLfloat * m
Definition: Glext.h:10319
int Radar_static_next
Definition: radarsetup.h:114
#define i2fl(i)
Definition: floating.h:32
#define BLIP_TYPE_TAGGED_SHIP
Definition: radarsetup.h:66
#define timestamp_rand(a, b)
Definition: timer.h:92
void gr_bitmap(int _x, int _y, int resize_mode)
Definition: 2d.cpp:1303
#define GR_BITBLT_MODE_NORMAL
Definition: 2d.h:351
int Large_blip_offset_y
Definition: radarsetup.h:124
void blipDrawDistorted(blip *b, int x, int y)
Definition: radar.cpp:55
void initCenterOffsets(float x, float y)
Definition: radar.cpp:41
int emp_active_local()
Definition: emp.cpp:428
int flags
Definition: radarsetup.h:36
int Radar_avail_prev_frame
Definition: radarsetup.h:115
void blitGauge()
Definition: radar.cpp:98
int Radar_flicker_on[NUM_FLICKER_TIMERS]
Definition: radarsetup.h:119
#define GR_ALPHABLEND_NONE
Definition: 2d.h:348
void snd_stop(int sig)
Definition: sound.cpp:875
void gr_set_font(int fontnum)
Definition: font.cpp:566
void drawCrosshairs(int x, int y)
Definition: radar.cpp:409
int Radar_static_playing
Definition: radarsetup.h:113
int radar_color_image_2d
Definition: radarsetup.h:40
GLint y
Definition: Gl.h:1505
#define g3_start_frame(zbuffer_flag)
Definition: 3d.h:39
#define BLIP_CURRENT_TARGET
Definition: radarsetup.h:78
void renderBitmap(int x, int y)
Definition: hud.cpp:782