FS2_Open
Open source remastering of the Freespace 2 engine
jumpnode.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 #include "hud/hud.h"
12 #include "jumpnode/jumpnode.h"
13 #include "model/model.h"
14 #include "model/modelrender.h"
15 
17 
21 CJumpNode::CJumpNode() : m_radius(0.0f), m_modelnum(-1), m_objnum(-1), m_flags(0)
22 {
23  gr_init_alphacolor(&m_display_color, 0, 255, 0, 255);
24 
25  m_name[0] = '\0';
26 
27  m_pos.xyz.x = 0.0f;
28  m_pos.xyz.y = 0.0f;
29  m_pos.xyz.z = 0.0f;
30 }
31 
35 CJumpNode::CJumpNode(vec3d *position) : m_radius(0.0f), m_modelnum(-1), m_objnum(-1), m_flags(0)
36 {
37  Assert(position != NULL);
38 
39  gr_init_alphacolor(&m_display_color, 0, 255, 0, 255);
40 
41  // Set m_name
42  sprintf(m_name, XSTR( "Jump Node %d", 632), Jump_nodes.size());
43 
44  // Set m_modelnum and m_radius
45  m_modelnum = model_load(NOX("subspacenode.pof"), 0, NULL, 0);
46  if (m_modelnum == -1)
47  Warning(LOCATION, "Could not load default model for %s", m_name);
48  else
49  m_radius = model_get_radius(m_modelnum);
50 
51  m_pos.xyz.x = position->xyz.x;
52  m_pos.xyz.y = position->xyz.y;
53  m_pos.xyz.z = position->xyz.z;
54 
55  // Create the object
56  m_objnum = obj_create(OBJ_JUMP_NODE, -1, -1, NULL, &m_pos, m_radius, OF_RENDERS);
57 }
58 
60  : m_radius(other.m_radius), m_modelnum(other.m_modelnum), m_objnum(other.m_objnum), m_flags(other.m_flags)
61 {
62  other.m_radius = 0.0f;
63  other.m_modelnum = -1;
64  other.m_objnum = -1;
65  other.m_flags = 0;
66 
67  m_display_color = other.m_display_color;
68  m_pos = other.m_pos;
69 
70  strcpy_s(m_name, other.m_name);
71 }
72 
73 CJumpNode& CJumpNode::operator=(CJumpNode&& other)
74 {
75  if (this != &other)
76  {
77  m_radius = other.m_radius;
78  m_modelnum = other.m_modelnum;
79  m_objnum = other.m_objnum;
80  m_flags = other.m_flags;
81 
82  other.m_radius = 0.0f;
83  other.m_modelnum = -1;
84  other.m_objnum = -1;
85  other.m_flags = 0;
86 
87  m_display_color = other.m_display_color;
88  m_pos = other.m_pos;
89 
90  strcpy_s(m_name, other.m_name);
91  }
92 
93  return *this;
94 }
95 
100 {
101  if (m_modelnum >= 0)
102  {
103  model_unload(m_modelnum);
104  }
105 
106  if (m_objnum >= 0 && Objects[m_objnum].type != OBJ_NONE)
107  {
108  obj_delete(m_objnum);
109  }
110 }
111 
112 // Accessor functions for private variables
113 
118 {
119  return m_name;
120 }
121 
126 {
127  return m_modelnum;
128 }
129 
134 {
135  return m_objnum;
136 }
137 
142 {
143  Assert(m_objnum != -1);
144  return &Objects[m_objnum];
145 }
146 
151 {
152  return m_display_color;
153 }
154 
159 {
160  return &m_pos;
161 }
162 
163 // Settor functions for private variables
164 
173 void CJumpNode::SetAlphaColor(int r, int g, int b, int alpha)
174 {
175  CLAMP(r, 0, 255);
176  CLAMP(g, 0, 255);
177  CLAMP(b, 0, 255);
178  CLAMP(alpha, 0, 255);
179 
180  m_flags |= JN_USE_DISPLAY_COLOR;
181  gr_init_alphacolor(&m_display_color, r, g, b, alpha);
182 }
183 
190 void CJumpNode::SetModel(char *model_name, bool show_polys)
191 {
192  Assert(model_name != NULL);
193 
194  //Try to load the new model; if we can't, then we can't set it
195  int new_model = model_load(model_name, 0, NULL, 0);
196 
197  if(new_model == -1)
198  {
199  Warning(LOCATION, "Couldn't load model file %s for jump node %s", model_name, m_name);
200  return;
201  }
202 
203  //If there's an old model, unload it
204  if(m_modelnum != -1)
205  model_unload(m_modelnum);
206 
207  //Now actually set stuff
208  m_modelnum = new_model;
209  m_flags |= JN_SPECIAL_MODEL;
210  m_radius = model_get_radius(m_modelnum);
211 
212  //Do we want to change poly showing?
213  if(show_polys)
214  m_flags |= JN_SHOW_POLYS;
215  else
216  m_flags &= ~JN_SHOW_POLYS;
217 }
218 
224 void CJumpNode::SetName(const char *new_name)
225 {
226  Assert(new_name != NULL);
227 
228  #ifndef NDEBUG
229  CJumpNode* check = jumpnode_get_by_name(new_name);
230  Assert((check == this || !check));
231  #endif
232 
233  strcpy_s(m_name, new_name);
234 }
235 
242 {
243  if(enabled)
244  {
245  m_flags&=~JN_HIDE;
246  }
247  else
248  {
249  // Untarget this node if it is already targeted
250  if ( Player_ai->target_objnum == m_objnum )
251  Player_ai->target_objnum = -1;
252  m_flags|=JN_HIDE;
253  }
254 }
255 
256 // Query functions
257 
262 {
263  if(m_flags & JN_HIDE)
264  return true;
265  else
266  return false;
267 }
268 
273 {
274  return ((m_flags & JN_USE_DISPLAY_COLOR) != 0);
275 }
276 
281 {
282  return ((m_flags & JN_SPECIAL_MODEL) != 0);
283 }
284 
292 {
293  Assert(pos != NULL);
294  // Assert(view_pos != NULL); - view_pos can be NULL
295 
296  if(m_flags & JN_HIDE)
297  return;
298 
299  if(m_modelnum < 0)
300  return;
301 
302  matrix node_orient = IDENTITY_MATRIX;
303 
304  int mr_flags = MR_NO_LIGHTING;
305  if(!(m_flags & JN_SHOW_POLYS)) {
307  }
308 
309  if ( Fred_running ) {
310  gr_set_color_fast(&m_display_color);
311  model_render_DEPRECATED(m_modelnum, &node_orient, pos, mr_flags );
312  } else {
313  if (m_flags & JN_USE_DISPLAY_COLOR) {
314  gr_set_color_fast(&m_display_color);
315  }
316  else if ( view_pos != NULL) {
317  int alpha_index = HUD_color_alpha;
318 
319  // generate alpha index based on distance to jump this
320  float dist;
321 
322  dist = vm_vec_dist_quick(view_pos, pos);
323 
324  // linearly interpolate alpha. At 1000m or less, full intensity. At 10000m or more 1/2 intensity.
325  if ( dist < 1000 ) {
326  alpha_index = HUD_COLOR_ALPHA_USER_MAX - 2;
327  } else if ( dist > 10000 ) {
328  alpha_index = HUD_COLOR_ALPHA_USER_MIN;
329  } else {
330  alpha_index = fl2i( HUD_COLOR_ALPHA_USER_MAX - 2 + (dist-1000) * (HUD_COLOR_ALPHA_USER_MIN-HUD_COLOR_ALPHA_USER_MAX-2) / (9000) + 0.5f);
331  if ( alpha_index < HUD_COLOR_ALPHA_USER_MIN ) {
332  alpha_index = HUD_COLOR_ALPHA_USER_MIN;
333  }
334  }
335 
336  gr_set_color_fast(&HUD_color_defaults[alpha_index]);
337 
338  } else {
340  }
341 
342  model_render_DEPRECATED(m_modelnum, &node_orient, pos, mr_flags );
343  }
344 
345 }
346 
348 {
349  draw_list scene;
350 
351  Render(&scene, pos, view_pos);
352 
353  scene.render_all();
354  scene.render_outlines();
355 
357  gr_clear_states();
358  gr_set_buffer(-1);
359 }
360 
362 {
363  Assert(pos != NULL);
364  // Assert(view_pos != NULL); - view_pos can be NULL
365 
366  if(m_flags & JN_HIDE)
367  return;
368 
369  if(m_modelnum < 0)
370  return;
371 
372  matrix node_orient = IDENTITY_MATRIX;
373 
374  int mr_flags = MR_NO_LIGHTING | MR_NO_BATCH;
375  if(!(m_flags & JN_SHOW_POLYS)) {
377  }
378 
379  model_render_params render_info;
380 
381  render_info.set_detail_level_lock(0);
382  render_info.set_flags(mr_flags);
383 
384  if ( Fred_running ) {
385  render_info.set_color(m_display_color);
386 
387  model_render_queue(&render_info, scene, m_modelnum, &node_orient, pos);
388  } else {
389  if (m_flags & JN_USE_DISPLAY_COLOR) {
390  //gr_set_color_fast(&m_display_color);
391  render_info.set_color(m_display_color);
392  }
393  else if ( view_pos != NULL) {
394  int alpha_index = HUD_color_alpha;
395 
396  // generate alpha index based on distance to jump this
397  float dist;
398 
399  dist = vm_vec_dist_quick(view_pos, pos);
400 
401  // linearly interpolate alpha. At 1000m or less, full intensity. At 10000m or more 1/2 intensity.
402  if ( dist < 1000 ) {
403  alpha_index = HUD_COLOR_ALPHA_USER_MAX - 2;
404  } else if ( dist > 10000 ) {
405  alpha_index = HUD_COLOR_ALPHA_USER_MIN;
406  } else {
407  alpha_index = fl2i( HUD_COLOR_ALPHA_USER_MAX - 2 + (dist-1000) * (HUD_COLOR_ALPHA_USER_MIN-HUD_COLOR_ALPHA_USER_MAX-2) / (9000) + 0.5f);
408  if ( alpha_index < HUD_COLOR_ALPHA_USER_MIN ) {
409  alpha_index = HUD_COLOR_ALPHA_USER_MIN;
410  }
411  }
412 
413  render_info.set_color(HUD_color_defaults[alpha_index]);
414  } else {
416  }
417 
418  model_render_queue(&render_info, scene, m_modelnum, &node_orient, pos);
419  }
420 
421 }
422 
430 {
431  Assert(name != NULL);
433 
434  for (jnp = Jump_nodes.begin(); jnp != Jump_nodes.end(); ++jnp) {
435  if(!stricmp(jnp->GetName(), name))
436  return &(*jnp);
437  }
438 
439  return NULL;
440 }
441 
449 {
450  Assert(objp != NULL);
452  float radius, dist;
453 
454  for (jnp = Jump_nodes.begin(); jnp != Jump_nodes.end(); ++jnp) {
455  if(jnp->GetModelNumber() < 0)
456  continue;
457 
458  radius = model_get_radius( jnp->GetModelNumber() );
459  dist = vm_vec_dist( &objp->pos, &jnp->GetSCPObject()->pos );
460  if ( dist <= radius ) {
461  return &(*jnp);
462  }
463  }
464 
465  return NULL;
466 }
467 
474 {
476 
477  for (jnp = Jump_nodes.begin(); jnp != Jump_nodes.end(); ++jnp) {
478  jnp->Render(&jnp->GetSCPObject()->pos);
479  }
480 }
481 
486 {
487  Jump_nodes.clear();
488 }
void RenderDEPRECATED(vec3d *pos, vec3d *view_pos=NULL)
Definition: jumpnode.cpp:291
int HUD_color_alpha
Definition: hud.cpp:76
#define MR_SHOW_OUTLINE_HTL
Definition: model.h:885
ai_info * Player_ai
Definition: ai.cpp:24
#define MR_NO_CULL
Definition: model.h:879
void Render(vec3d *pos, vec3d *view_pos=NULL)
Definition: jumpnode.cpp:347
int HUD_color_green
Definition: hud.cpp:74
int Fred_running
Definition: fred.cpp:44
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 target_objnum
Definition: ai.h:339
#define MR_NO_POLYS
Definition: model.h:866
Definition: pstypes.h:88
void gr_init_alphacolor(color *clr, int r, int g, int b, int alpha, int type)
Definition: 2d.cpp:1173
#define OF_RENDERS
Definition: object.h:103
Definition: 2d.h:95
#define MR_SHOW_OUTLINE_PRESET
Definition: model.h:873
bool IsHidden()
Definition: jumpnode.cpp:261
void set_flags(uint flags)
#define MR_NO_LIGHTING
Definition: model.h:867
struct vec3d::@225::@227 xyz
GLclampf f
Definition: Glext.h:7097
#define MR_SHOW_OUTLINE
Definition: model.h:859
void set_detail_level_lock(int detail_level_lock)
void gr_set_color_fast(color *dst)
Definition: 2d.cpp:1197
#define GR_FILL_MODE_SOLID
Definition: 2d.h:670
#define JN_HIDE
Definition: jumpnode.h:28
object * objp
Definition: lua.cpp:3105
#define JN_SHOW_POLYS
Definition: jumpnode.h:27
int GetSCPObjectNumber()
Definition: jumpnode.cpp:133
vec3d pos
Definition: object.h:152
vec3d view_pos
Definition: fredrender.cpp:103
bool IsColored()
Definition: jumpnode.cpp:272
SCP_list< CJumpNode > Jump_nodes
Definition: jumpnode.cpp:16
#define HUD_COLOR_ALPHA_USER_MAX
Definition: hud.h:70
GLenum type
Definition: Gl.h:1492
#define JN_USE_DISPLAY_COLOR
Definition: jumpnode.h:26
int GetModelNumber()
Definition: jumpnode.cpp:125
#define CLAMP(x, min, max)
Definition: pstypes.h:488
void gr_set_color(int r, int g, int b)
Definition: 2d.cpp:1188
void model_render_queue(model_render_params *interp, draw_list *scene, int model_num, matrix *orient, vec3d *pos)
CJumpNode * jumpnode_get_by_name(const char *name)
Definition: jumpnode.cpp:429
GLdouble GLdouble GLdouble r
Definition: Glext.h:5337
void SetModel(char *model_name, bool show_polys=false)
Definition: jumpnode.cpp:190
vec3d * GetPosition()
Definition: jumpnode.cpp:158
#define HUD_COLOR_ALPHA_USER_MIN
Definition: hud.h:71
GLboolean GLboolean g
Definition: Glext.h:5781
GLenum GLenum GLsizei const GLuint GLboolean enabled
Definition: Glext.h:7307
#define MR_NO_TEXTURING
Definition: model.h:868
int obj_create(ubyte type, int parent_obj, int instance, matrix *orient, vec3d *pos, float radius, uint flags)
Definition: object.cpp:467
#define gr_set_buffer
Definition: 2d.h:891
object * GetSCPObject()
Definition: jumpnode.cpp:141
color HUD_color_defaults[HUD_NUM_COLOR_LEVELS]
Definition: hud.cpp:84
sprintf(buf,"(%f,%f,%f)", v3->xyz.x, v3->xyz.y, v3->xyz.z)
bool IsSpecialModel()
Definition: jumpnode.cpp:280
void SetVisibility(bool enabled)
Definition: jumpnode.cpp:241
float vm_vec_dist(const vec3d *v0, const vec3d *v1)
Definition: vecmat.cpp:355
int model_load(char *filename, int n_subsystems, model_subsystem *subsystems, int ferror=1, int duplicate=0)
Definition: modelread.cpp:2573
void set_color(color &clr)
CJumpNode * jumpnode_get_which_in(object *objp)
Definition: jumpnode.cpp:448
void jumpnode_render_all()
Definition: jumpnode.cpp:473
object Objects[MAX_OBJECTS]
Definition: object.cpp:62
const char * XSTR(const char *str, int index)
Definition: localize.cpp:851
#define NOX(s)
Definition: pstypes.h:473
void jumpnode_level_close()
Definition: jumpnode.cpp:485
GLuint const GLchar * name
Definition: Glext.h:5608
#define IDENTITY_MATRIX
Definition: vecmat.h:64
GLboolean GLboolean GLboolean b
Definition: Glext.h:5781
void obj_delete(int objnum)
Definition: object.cpp:522
void SetName(const char *new_name)
Definition: jumpnode.cpp:224
float vm_vec_dist_quick(const vec3d *v0, const vec3d *v1)
Definition: vecmat.cpp:417
void model_render_DEPRECATED(int model_num, matrix *orient, vec3d *pos, uint flags=MR_DEPRECATED_NORMAL, int objnum=-1, int lighting_skip=-1, int *replacement_textures=NULL, int render=MODEL_RENDER_ALL, const bool is_skybox=false)
#define fl2i(fl)
Definition: floating.h:33
int HUD_color_red
Definition: hud.cpp:73
#define gr_clear_states
Definition: 2d.h:946
float model_get_radius(int modelnum)
Definition: modelread.cpp:3105
void render_all(int depth_mode=-1)
#define LOCATION
Definition: pstypes.h:245
#define gr_set_fill_mode
Definition: 2d.h:933
hull_check pos
Definition: lua.cpp:5050
void SetAlphaColor(int r, int g, int b, int alpha)
Definition: jumpnode.cpp:173
void render_outlines()
#define OBJ_NONE
Definition: object.h:31
#define JN_SPECIAL_MODEL
Definition: jumpnode.h:29
char * GetName()
Definition: jumpnode.cpp:117
#define MR_NO_BATCH
Definition: model.h:881
GLclampf GLclampf GLclampf alpha
Definition: Glext.h:5177
#define stricmp(s1, s2)
Definition: config.h:271
#define OBJ_JUMP_NODE
Definition: object.h:45
~CJumpNode()
Definition: jumpnode.cpp:99
color GetColor()
Definition: jumpnode.cpp:150
void model_unload(int modelnum, int force=0)
Definition: modelread.cpp:162
#define strcpy_s(...)
Definition: safe_strings.h:67
int HUD_color_blue
Definition: hud.cpp:75