]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/random.qc
Merge branch 'master' into TimePath/items
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / random.qc
1 #include "random.qh"
2
3 void RandomSelection_Init()
4 {
5         RandomSelection_totalweight = 0;
6         RandomSelection_chosen_ent = NULL;
7         RandomSelection_chosen_float = 0;
8         RandomSelection_chosen_string = string_null;
9         RandomSelection_best_priority = -1;
10 }
11
12 void RandomSelection_Add(entity e, float f, string s, float weight, float priority)
13 {
14         if (priority > RandomSelection_best_priority)
15         {
16                 RandomSelection_best_priority = priority;
17                 RandomSelection_chosen_ent = e;
18                 RandomSelection_chosen_float = f;
19                 RandomSelection_chosen_string = s;
20                 RandomSelection_totalweight = weight;
21         }
22         else if (priority == RandomSelection_best_priority)
23         {
24                 RandomSelection_totalweight += weight;
25                 if (random() * RandomSelection_totalweight <= weight)
26                 {
27                         RandomSelection_chosen_ent = e;
28                         RandomSelection_chosen_float = f;
29                         RandomSelection_chosen_string = s;
30                 }
31         }
32 }
33
34
35 // prandom - PREDICTABLE random number generator (not seeded yet)
36
37 #ifdef USE_PRANDOM
38         float prandom_seed;
39         float prandom()
40         {
41                 float c;
42                 c = crc16(false, strcat(ftos(prandom_seed), ftos(prandom_seed + M_PI)));
43                 prandom_seed = c;
44
45         #ifdef USE_PRANDOM_DEBUG
46                         LOG_TRACE("RANDOM -> ", ftos(c), "\n");
47         #endif
48
49                 return c / 65536;  // in [0..1[
50         }
51
52         vector prandomvec()
53         {
54                 vector v;
55
56                 do
57                 {
58                         v.x = prandom();
59                         v.y = prandom();
60                         v.z = prandom();
61                 }
62                 while (v * v > 1);
63
64                 return v;
65         }
66
67         void psrandom(float seed)
68         {
69                 prandom_seed = seed;
70         #ifdef USE_PRANDOM_DEBUG
71                         LOG_TRACE("SRANDOM ", ftos(seed), "\n");
72         #endif
73         }
74
75         #ifdef USE_PRANDOM_DEBUG
76                 void prandom_debug()
77                 {
78                         LOG_TRACE("Current random seed = ", ftos(prandom_seed), "\n");
79                 }
80         #endif
81 #endif