FS2_Open
Open source remastering of the Freespace 2 engine
staticrand.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 #include "math/staticrand.h"
13 #include "math/vecmat.h"
14 
15 
18 
23 {
24  int i;
25 
26  Semirand_inited = 1;
27 
28  for (i=0; i<SEMIRAND_MAX; i++)
29  Semirand[i] = (myrand() << 15) + myrand();
30 }
31 
38 int static_rand(int num)
39 {
40  int a, b, c;
41 
42  if (num < 0)
43  num *= -1;
44 
45  if (!Semirand_inited)
46  init_semirand();
47 
48  a = num & (SEMIRAND_MAX - 1);
49  b = (num >> SEMIRAND_MAX_LOG) & (SEMIRAND_MAX - 1);
50  c = (num >> (2 * SEMIRAND_MAX_LOG)) & (SEMIRAND_MAX - 1);
51 
52  return Semirand[a] ^ Semirand[b] ^ Semirand[c];
53 }
54 
61 float static_randf(int num)
62 {
63  int a;
64 
65  a = static_rand(num);
66 
67  return (a & 0xffff) / 65536.0f;
68 }
69 
78 int static_rand_range(int num, int min, int max)
79 {
80  int rval = static_rand(num);
81  rval = (rval % (max - min + 1)) + min;
82  CLAMP(rval, min, max);
83  return rval;
84 }
85 
95 float static_randf_range(int num, float min, float max)
96 {
97  float rval;
98 
99  rval = static_randf(num);
100  rval = rval * (max - min) + min;
101 
102  return rval;
103 }
104 
112 {
113  vp->xyz.x = static_randf(num) - 0.5f;
114  vp->xyz.y = static_randf(num+1) - 0.5f;
115  vp->xyz.z = static_randf(num+2) - 0.5f;
116 
118 }
119 
129 void static_rand_cone(int num, vec3d *out, vec3d *in, float max_angle, matrix *orient)
130 {
131  vec3d t1, t2;
132  matrix *rot;
133  matrix m;
134 
135  // get an orientation matrix
136  if(orient != NULL){
137  rot = orient;
138  } else {
139  vm_vector_2_matrix(&m, in, NULL, NULL);
140  rot = &m;
141  }
142 
143  // axis 1
144  vm_rot_point_around_line(&t1, in, fl_radians(static_randf_range(num,-max_angle, max_angle)), &vmd_zero_vector, &rot->vec.fvec);
145 
146  // axis 2
147  vm_rot_point_around_line(&t2, &t1, fl_radians(static_randf_range(num+1,-max_angle, max_angle)), &vmd_zero_vector, &rot->vec.rvec);
148 
149  // axis 3
150  vm_rot_point_around_line(out, &t2, fl_radians(static_randf_range(num+2,-max_angle, max_angle)), &vmd_zero_vector, &rot->vec.uvec);
151 }
152 
154 // Alternate random number generator, that doesn't affect rand() sequence
156 #define RND_MASK 0x6000
157 #define RND_MAX 0x7fff
158 int Rnd_seed = 1;
159 
166 void init_static_rand_alt(int seed)
167 {
168  Rnd_seed = seed;
169 }
170 
177 {
178  static int x=Rnd_seed;
179  int old_x;
180  old_x = x;
181  x >>= 1;
182  if ( old_x & 1 ) {
183  x ^= RND_MASK;
184  }
185  return x;
186 }
187 
194 {
195  int r = static_rand_alt();
196  return i2fl(r)/RND_MAX;
197 }
int i
Definition: multi_pxo.cpp:466
#define vp(p)
Definition: modelsinc.h:70
void init_semirand()
Initialize Semirand array. Doesn't have to be called.
Definition: staticrand.cpp:22
int static_rand_alt()
Get a random integer between 1 and RND_MAX.
Definition: staticrand.cpp:176
int Rnd_seed
Definition: staticrand.cpp:158
float static_randf_alt()
Get a random integer between 0 and 1.0.
Definition: staticrand.cpp:193
float vm_vec_normalize_quick(vec3d *src)
Definition: vecmat.cpp:529
void static_rand_cone(int num, vec3d *out, vec3d *in, float max_angle, matrix *orient)
Randomly perturb a vector around a given (normalized vector) or optional orientation matrix...
Definition: staticrand.cpp:129
Definition: pstypes.h:88
struct vec3d::@225::@227 xyz
void static_randvec(int num, vec3d *vp)
[To be described]
Definition: staticrand.cpp:111
fix t2
Definition: animplay.cpp:37
hull_check orient
Definition: lua.cpp:5049
GLuint in
Definition: Glext.h:9087
#define SEMIRAND_MAX
Definition: staticrand.h:17
int Semirand[SEMIRAND_MAX]
Definition: staticrand.cpp:17
#define CLAMP(x, min, max)
Definition: pstypes.h:488
#define SEMIRAND_MAX_LOG
Definition: staticrand.h:16
matrix * vm_vector_2_matrix(matrix *m, const vec3d *fvec, const vec3d *uvec, const vec3d *rvec)
Definition: vecmat.cpp:850
GLdouble GLdouble GLdouble r
Definition: Glext.h:5337
struct matrix::@228::@230 vec
GLboolean GLboolean GLboolean GLboolean a
Definition: Glext.h:5781
void vm_rot_point_around_line(vec3d *out, const vec3d *in, float angle, const vec3d *line_point, const vec3d *line_dir)
Definition: vecmat.cpp:1395
void init_static_rand_alt(int seed)
Seed the alternative random number generator. Doesn't have to be called.
Definition: staticrand.cpp:166
GLint GLint GLint GLint GLint x
Definition: Glext.h:5182
fix t1
Definition: animplay.cpp:37
GLboolean GLboolean GLboolean b
Definition: Glext.h:5781
int Semirand_inited
Definition: staticrand.cpp:16
GLuint GLuint num
Definition: Glext.h:9089
#define RND_MAX
Definition: staticrand.cpp:157
int static_rand(int num)
Return a pseudo random 32 bit value given a reasonably small number.
Definition: staticrand.cpp:38
int static_rand_range(int num, int min, int max)
Return a random integer within a range. Note: min and max are inclusive.
Definition: staticrand.cpp:78
const GLfloat * m
Definition: Glext.h:10319
#define RND_MASK
Definition: staticrand.cpp:156
#define i2fl(i)
Definition: floating.h:32
vec3d vmd_zero_vector
Definition: vecmat.cpp:24
float static_randf(int num)
Return a random float in 0.0f .. 1.0f- (ie, it will never return 1.0f).
Definition: staticrand.cpp:61
const GLubyte * c
Definition: Glext.h:8376
int myrand()
Definition: systemvars.cpp:102
float static_randf_range(int num, float min, float max)
Return a random float within a range. Note: min and max are inclusive.
Definition: staticrand.cpp:95
#define fl_radians(fl)
Definition: floating.h:42