Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
random.qc
Go to the documentation of this file.
1#include "random.qh"
2
12
14void 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
42void DistributeEvenly_Init(float amount, float totalweight)
43{
45 LOG_TRACE("DistributeEvenly_Init: UNFINISHED DISTRIBUTION (", ftos(DistributeEvenly_amount),
46 " for ", ftos(DistributeEvenly_totalweight), " left!)");
47 DistributeEvenly_amount = (totalweight) ? amount : 0;
48 DistributeEvenly_totalweight = totalweight;
49}
50
52float DistributeEvenly_Get(float weight)
53{
54 if (weight <= 0)
55 return 0;
59 return f;
60}
61
63float DistributeEvenly_GetRounded(float weight)
64{
65 if (weight <= 0)
66 return 0;
70 return f;
71}
72
75{
76 if (weight <= 0)
77 return 0;
81 return f;
82}
83
88{
89 static float lastvalue;
90 static bool lastvalue_set;
91 if (lastvalue_set)
92 {
93 lastvalue_set = false;
94 return lastvalue;
95 }
96
97 const float a = random() * (2 * M_PI);
98 const float b = sqrt(-2 * log(random()));
99 lastvalue = cos(a) * b;
100 lastvalue_set = true;
101 return sin(a) * b;
102}
103
104
105#ifdef USE_PRANDOM
108 float prandom()
109 {
110 float c;
111 c = crc16(false, strcat(ftos(prandom_seed), ftos(prandom_seed + M_PI)));
112 prandom_seed = c;
113
114 #ifdef USE_PRANDOM_DEBUG
115 LOG_TRACE("RANDOM -> ", ftos(c));
116 #endif
117
118 return c / 65536; // in [0..1[
119 }
120
122 {
123 vector v;
124
125 do
126 {
127 v.x = prandom();
128 v.y = prandom();
129 v.z = prandom();
130 }
131 while (v * v > 1);
132
133 return v;
134 }
135
136 void psrandom(float seed)
137 {
138 prandom_seed = seed;
139 #ifdef USE_PRANDOM_DEBUG
140 LOG_TRACE("SRANDOM ", ftos(seed));
141 #endif
142 }
143
144 #ifdef USE_PRANDOM_DEBUG
145 void prandom_debug()
146 {
147 LOG_TRACE("Current random seed = ", ftos(prandom_seed));
148 }
149 #endif
150#endif
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
float log(float f)
#define ERASEABLE
Definition _all.inc:37
#define LOG_TRACE(...)
Definition log.qh:76
#define M_PI
Definition mathlib.qh:108
float cos(float f)
float random(void)
float sqrt(float f)
float sin(float f)
string ftos(float f)
float floor(float f)
string string_null
Definition nil.qh:9
strcat(_("^F4Countdown stopped!"), "\n^BG", _("Teams are too unbalanced."))
#define NULL
Definition post.qh:14
ERASEABLE void RandomSelection_Add(entity e, float f, string s, vector v, float weight, float priority)
Definition random.qc:14
float prandom_seed
Definition random.qc:106
ERASEABLE void RandomSelection_Init()
Definition random.qc:4
ERASEABLE void DistributeEvenly_Init(float amount, float totalweight)
Definition random.qc:42
float DistributeEvenly_totalweight
Definition random.qc:39
float DistributeEvenly_amount
Definition random.qc:38
void psrandom(float seed)
Definition random.qc:136
ERASEABLE float DistributeEvenly_GetRounded(float weight)
Definition random.qc:63
ERASEABLE float DistributeEvenly_Get(float weight)
Definition random.qc:52
float prandom()
Predictable random number generator (not seeded yet)
Definition random.qc:108
vector prandomvec()
Definition random.qc:121
ERASEABLE float DistributeEvenly_GetRandomized(float weight)
Definition random.qc:74
ERASEABLE float gsl_ran_ugaussian()
Returns a Gaussian random variate, with mean zero and standard deviation sigma 1 From the GNU Scienti...
Definition random.qc:87
float RandomSelection_best_priority
Definition random.qh:4
float RandomSelection_chosen_float
Definition random.qh:6
string RandomSelection_chosen_string
Definition random.qh:7
float RandomSelection_totalweight
Definition random.qh:3
entity RandomSelection_chosen_ent
Definition random.qh:5
vector RandomSelection_chosen_vec
Definition random.qh:8
#define prandom_debug()
Definition random.qh:44
vector
Definition self.qh:92