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 = crc16(false, strcat(ftos(prandom_seed), ftos(prandom_seed + M_PI)));
111 prandom_seed = c;
112
113 #ifdef USE_PRANDOM_DEBUG
114 LOG_TRACE("RANDOM -> ", ftos(c));
115 #endif
116
117 return c / 65536; // in [0..1[
118 }
119
121 {
122 vector v;
123
124 do
125 {
126 v.x = prandom();
127 v.y = prandom();
128 v.z = prandom();
129 }
130 while (v * v > 1);
131
132 return v;
133 }
134
135 void psrandom(float seed)
136 {
137 prandom_seed = seed;
138 #ifdef USE_PRANDOM_DEBUG
139 LOG_TRACE("SRANDOM ", ftos(seed));
140 #endif
141 }
142
143 #ifdef USE_PRANDOM_DEBUG
144 void prandom_debug()
145 {
146 LOG_TRACE("Current random seed = ", ftos(prandom_seed));
147 }
148 #endif
149#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:135
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:120
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