… | |
… | |
37 | #include <funcpoint.h> |
37 | #include <funcpoint.h> |
38 | #include <material.h> |
38 | #include <material.h> |
39 | |
39 | |
40 | #include <glib.h> |
40 | #include <glib.h> |
41 | |
41 | |
42 | rand_gen rndm (time (0)); |
42 | rand_gen rndm; |
43 | |
43 | |
|
|
44 | void |
44 | tausworthe_random_generator::tausworthe_random_generator (uint32_t seed) |
45 | tausworthe_random_generator::seed (uint32_t seed) |
45 | { |
46 | { |
46 | state [0] = max ( 2, seed * 69069U); |
47 | state [0] = max ( 2, seed * 69069U); |
47 | state [1] = max ( 8, state [0] * 69069U); |
48 | state [1] = max ( 8, state [0] * 69069U); |
48 | state [2] = max ( 16, state [1] * 69069U); |
49 | state [2] = max ( 16, state [1] * 69069U); |
49 | state [3] = max (128, state [2] * 69069U); |
50 | state [3] = max (128, state [2] * 69069U); |
… | |
… | |
59 | state [1] = ((state [1] & 0xFFFFFFF8U) << 2U) ^ (((state [1] << 2U) ^ state [1]) >> 27U); |
60 | state [1] = ((state [1] & 0xFFFFFFF8U) << 2U) ^ (((state [1] << 2U) ^ state [1]) >> 27U); |
60 | state [2] = ((state [2] & 0xFFFFFFF0U) << 7U) ^ (((state [2] << 13U) ^ state [2]) >> 21U); |
61 | state [2] = ((state [2] & 0xFFFFFFF0U) << 7U) ^ (((state [2] << 13U) ^ state [2]) >> 21U); |
61 | state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U); |
62 | state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U); |
62 | |
63 | |
63 | return state [0] ^ state [1] ^ state [2] ^ state [3]; |
64 | return state [0] ^ state [1] ^ state [2] ^ state [3]; |
|
|
65 | } |
|
|
66 | |
|
|
67 | uint32_t |
|
|
68 | tausworthe_random_generator::get_range (uint32_t r_max) |
|
|
69 | { |
|
|
70 | return next () % r_max; |
|
|
71 | } |
|
|
72 | |
|
|
73 | // return a number within (min .. max) |
|
|
74 | int |
|
|
75 | tausworthe_random_generator::get_range (int r_min, int r_max) |
|
|
76 | { |
|
|
77 | return r_min + (*this) (max (r_max - r_min + 1, 1)); |
64 | } |
78 | } |
65 | |
79 | |
66 | /* |
80 | /* |
67 | * The random functions here take luck into account when rolling random |
81 | * The random functions here take luck into account when rolling random |
68 | * dice or numbers. This function has less of an impact the larger the |
82 | * dice or numbers. This function has less of an impact the larger the |
… | |
… | |
77 | * and if goodbad is non-zero, luck increases the roll, if zero, it decreases. |
91 | * and if goodbad is non-zero, luck increases the roll, if zero, it decreases. |
78 | * Generally, op should be the player/caster/hitter requesting the roll, |
92 | * Generally, op should be the player/caster/hitter requesting the roll, |
79 | * not the recipient (ie, the poor slob getting hit). [garbled 20010916] |
93 | * not the recipient (ie, the poor slob getting hit). [garbled 20010916] |
80 | */ |
94 | */ |
81 | int |
95 | int |
82 | random_roll (int min, int max, const object *op, int goodbad) |
96 | random_roll (int r_min, int r_max, const object *op, int goodbad) |
83 | { |
97 | { |
84 | int omin, diff, luck, base, ran; |
98 | int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */ |
85 | |
99 | |
86 | omin = min; |
100 | if (r_max < 1 || r_max < r_min) |
87 | diff = max - min + 1; |
|
|
88 | ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ |
|
|
89 | |
|
|
90 | if (max < 1 || diff < 1) |
|
|
91 | { |
101 | { |
92 | LOG (llevError, "Calling random_roll with min=%d max=%d\n", min, max); |
102 | LOG (llevError, "Calling random_roll with min=%d max=%d\n", r_min, r_max); |
93 | return (min); /* avoids a float exception */ |
103 | return r_min; |
94 | } |
104 | } |
95 | |
105 | |
96 | ran = RANDOM (); |
|
|
97 | |
|
|
98 | if (op->type != PLAYER) |
106 | if (op->type == PLAYER) |
99 | return ((ran % diff) + min); |
107 | { |
100 | |
|
|
101 | luck = op->stats.luck; |
108 | int luck = op->stats.luck; |
102 | if (RANDOM () % base < MIN (10, abs (luck))) |
109 | |
|
|
110 | if (rndm (base) < min (10, abs (luck))) |
|
|
111 | { |
|
|
112 | //TODO: take luck into account |
|
|
113 | } |
103 | { |
114 | } |
104 | /* we have a winner */ |
|
|
105 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
|
|
106 | diff -= luck; |
|
|
107 | if (diff < 1) |
|
|
108 | return (omin); /*check again */ |
|
|
109 | ((goodbad) ? (min += luck) : (diff)); |
|
|
110 | |
115 | |
111 | return (MAX (omin, MIN (max, (ran % diff) + min))); |
116 | return rndm (r_min, r_max); |
112 | } |
|
|
113 | return ((ran % diff) + min); |
|
|
114 | } |
117 | } |
115 | |
118 | |
116 | /* |
119 | /* |
117 | * This is a 64 bit version of random_roll above. This is needed |
120 | * This is a 64 bit version of random_roll above. This is needed |
118 | * for exp loss calculations for players changing religions. |
121 | * for exp loss calculations for players changing religions. |