… | |
… | |
74 | * not the recipient (ie, the poor slob getting hit). [garbled 20010916] |
74 | * not the recipient (ie, the poor slob getting hit). [garbled 20010916] |
75 | */ |
75 | */ |
76 | int |
76 | int |
77 | random_roll (int r_min, int r_max, const object *op, bool prefer_high) |
77 | random_roll (int r_min, int r_max, const object *op, bool prefer_high) |
78 | { |
78 | { |
79 | r_max = max (r_min, r_max); |
79 | if (r_min > r_max) |
|
|
80 | swap (r_min, r_max); |
80 | |
81 | |
|
|
82 | int range = r_max - r_min + 1; |
|
|
83 | int num = rndm (r_min, r_max); |
|
|
84 | |
|
|
85 | if (op->stats.luck) |
|
|
86 | { |
81 | int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */ |
87 | int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
82 | |
88 | |
83 | if (op->type == PLAYER) |
|
|
84 | { |
|
|
85 | int luck = op->stats.luck; |
|
|
86 | |
|
|
87 | if (rndm (base) < min (10, abs (luck))) |
89 | if (rndm (base) < min (10, abs (op->stats.luck))) |
88 | { |
|
|
89 | //TODO: take luck into account |
|
|
90 | } |
90 | { |
91 | } |
91 | // we have a winner, increase/decrease number by one accordingly |
|
|
92 | int adjust = sign (op->stats.luck); |
92 | |
93 | |
93 | return rndm (r_min, r_max); |
94 | if (!prefer_high) |
|
|
95 | adjust = -adjust; |
|
|
96 | |
|
|
97 | num = clamp (num + adjust, r_min, r_max); |
|
|
98 | } |
|
|
99 | } |
|
|
100 | |
|
|
101 | return num; |
94 | } |
102 | } |
95 | |
103 | |
96 | /* |
104 | /* |
97 | * This is a 64 bit version of random_roll above. This is needed |
105 | * This is a 64 bit version of random_roll above. This is needed |
98 | * for exp loss calculations for players changing religions. |
106 | * for exp loss calculations for players changing religions. |
99 | */ |
107 | */ |
100 | sint64 |
108 | sint64 |
101 | random_roll64 (sint64 r_min, sint64 r_max, const object *op, bool prefer_high) |
109 | random_roll64 (sint64 r_min, sint64 r_max, const object *op, bool prefer_high) |
102 | { |
110 | { |
|
|
111 | if (r_min > r_max) |
|
|
112 | swap (r_min, r_max); |
|
|
113 | |
103 | sint64 range = max (0, r_max - r_min + 1); |
114 | sint64 range = r_max - r_min + 1; |
104 | int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
|
|
105 | |
|
|
106 | /* |
115 | /* |
107 | * Make a call to get two 32 bit unsigned random numbers, and just do |
116 | * Make a call to get two 32 bit unsigned random numbers, and just do |
108 | * a little bitshifting. |
117 | * a little bitshifting. |
109 | */ |
118 | */ |
110 | sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31); |
119 | sint64 num = rndm.next () ^ (sint64 (rndm.next ()) << 31); |
|
|
120 | |
|
|
121 | num = num % range + r_min; |
111 | |
122 | |
112 | if (op->stats.luck) |
123 | if (op->stats.luck) |
113 | { |
124 | { |
114 | int luck = op->stats.luck; |
125 | int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
115 | |
126 | |
116 | if (rndm (base) < min (10, abs (luck))) |
127 | if (rndm (base) < min (10, abs (op->stats.luck))) |
117 | { |
128 | { |
118 | /* we have a winner */ |
129 | // we have a winner, increase/decrease number by one accordingly |
119 | luck = luck > 0 ? 1 : -1; |
130 | int adjust = sign (op->stats.luck); |
|
|
131 | |
|
|
132 | if (!prefer_high) |
|
|
133 | adjust = -adjust; |
120 | |
134 | |
121 | range -= luck; |
135 | num = clamp (num + adjust, r_min, r_max); |
122 | if (range < 1) |
|
|
123 | return r_min; /*check again */ |
|
|
124 | |
|
|
125 | if (prefer_high) |
|
|
126 | r_min += luck; |
|
|
127 | |
|
|
128 | return clamp (ran % range + r_min, r_min, r_max); |
|
|
129 | } |
136 | } |
130 | } |
137 | } |
131 | |
138 | |
132 | return ran % range + r_min; |
139 | return num; |
133 | } |
140 | } |
134 | |
141 | |
135 | /* |
142 | /* |
136 | * Roll a number of dice (2d3, 4d6). Uses op to determine luck, |
143 | * Roll a number of dice (2d3, 4d6). Uses op to determine luck, |
137 | * If goodbad is non-zero, luck increases the roll, if zero, it decreases. |
144 | * If goodbad is non-zero, luck increases the roll, if zero, it decreases. |