Xonotic
random.qc
Go to the documentation of this file.
1 #include "random.qh"
2 
5 {
11 }
12 
14 void RandomSelection_Add(entity e, float f, string s, vector v, float weight, float priority)
15 {
16  if (priority > RandomSelection_best_priority)
17  {
24  }
25  else if (priority == RandomSelection_best_priority)
26  {
28  if (random() * RandomSelection_totalweight <= weight)
29  {
34  }
35  }
36 }
37 
40 
42 void DistributeEvenly_Init(float amount, float totalweight)
43 {
45  {
46  LOG_TRACE("DistributeEvenly_Init: UNFINISHED DISTRIBUTION (", ftos(DistributeEvenly_amount), " for ", ftos(DistributeEvenly_totalweight), " left!)");
47  }
48  if (totalweight == 0) DistributeEvenly_amount = 0;
49  else DistributeEvenly_amount = amount;
50  DistributeEvenly_totalweight = totalweight;
51 }
52 
54 float DistributeEvenly_Get(float weight)
55 {
56  float f;
57  if (weight <= 0) return 0;
61  return f;
62 }
63 
65 float DistributeEvenly_GetRandomized(float weight)
66 {
67  float f;
68  if (weight <= 0) return 0;
72  return f;
73 }
74 
75 // from the GNU Scientific Library
79 float gsl_ran_gaussian(float sigma)
80 {
82  {
84  return sigma * gsl_ran_gaussian_lastvalue;
85  }
86  else
87  {
88  float a = random() * 2 * M_PI;
89  float b = sqrt(-2 * log(random()));
92  return sigma * sin(a) * b;
93  }
94 }
95 
96 // prandom - PREDICTABLE random number generator (not seeded yet)
97 
98 #ifdef USE_PRANDOM
99  float prandom_seed;
100  float prandom()
101  {
102  float c;
103  c = crc16(false, strcat(ftos(prandom_seed), ftos(prandom_seed + M_PI)));
104  prandom_seed = c;
105 
106  #ifdef USE_PRANDOM_DEBUG
107  LOG_TRACE("RANDOM -> ", ftos(c));
108  #endif
109 
110  return c / 65536; // in [0..1[
111  }
112 
114  {
115  vector v;
116 
117  do
118  {
119  v.x = prandom();
120  v.y = prandom();
121  v.z = prandom();
122  }
123  while (v * v > 1);
124 
125  return v;
126  }
127 
128  void psrandom(float seed)
129  {
130  prandom_seed = seed;
131  #ifdef USE_PRANDOM_DEBUG
132  LOG_TRACE("SRANDOM ", ftos(seed));
133  #endif
134  }
135 
136  #ifdef USE_PRANDOM_DEBUG
137  void prandom_debug()
138  {
139  LOG_TRACE("Current random seed = ", ftos(prandom_seed));
140  }
141  #endif
142 #endif
string string_null
Definition: nil.qh:9
float RandomSelection_totalweight
Definition: random.qh:3
vector RandomSelection_chosen_vec
Definition: random.qh:8
string RandomSelection_chosen_string
Definition: random.qh:7
#define prandom_debug()
Definition: random.qh:31
ERASEABLE void RandomSelection_Init()
Definition: random.qc:4
float DistributeEvenly_totalweight
Definition: random.qc:39
entity() spawn
float prandom()
Definition: random.qc:100
ERASEABLE float gsl_ran_gaussian(float sigma)
Definition: random.qc:79
float RandomSelection_chosen_float
Definition: random.qh:6
#define ERASEABLE
Definition: _all.inc:35
float gsl_ran_gaussian_lastvalue
Definition: random.qc:76
ERASEABLE float DistributeEvenly_Get(float weight)
Definition: random.qc:54
float prandom_seed
Definition: random.qc:99
spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 f1 s1 strcat(_("Level %s: "), "^BG%s\3\, _("^BGPress ^F2%s^BG to enter the game"))
entity RandomSelection_chosen_ent
Definition: random.qh:5
#define NULL
Definition: post.qh:17
float gsl_ran_gaussian_lastvalue_set
Definition: random.qc:77
void psrandom(float seed)
Definition: random.qc:128
vector(float skel, float bonenum) _skel_get_boneabs_hidden
const float M_PI
Definition: csprogsdefs.qc:269
vector v
Definition: ent_cs.qc:116
float log(float f)
vector prandomvec()
Definition: random.qc:113
#define LOG_TRACE(...)
Definition: log.qh:81
ERASEABLE void DistributeEvenly_Init(float amount, float totalweight)
Definition: random.qc:42
ERASEABLE float DistributeEvenly_GetRandomized(float weight)
Definition: random.qc:65
float DistributeEvenly_amount
Definition: random.qc:38
float RandomSelection_best_priority
Definition: random.qh:4
ERASEABLE void RandomSelection_Add(entity e, float f, string s, vector v, float weight, float priority)
Definition: random.qc:14