1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992 Frank Tore Johansen |
6 | * Copyright (©) 1992 Frank Tore Johansen |
7 | * |
7 | * |
8 | * Deliantra is free software: you can redistribute it and/or modify it under |
8 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * the terms of the Affero GNU General Public License as published by the |
9 | * the terms of the Affero GNU General Public License as published by the |
… | |
… | |
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 | { |
103 | sint64 omin = r_min; |
111 | if (r_min > r_max) |
|
|
112 | swap (r_min, r_max); |
|
|
113 | |
104 | sint64 range = max (0, r_max - r_min + 1); |
114 | sint64 range = r_max - r_min + 1; |
105 | int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
|
|
106 | |
|
|
107 | /* |
115 | /* |
108 | * 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 |
109 | * a little bitshifting. |
117 | * a little bitshifting. |
110 | */ |
118 | */ |
111 | 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; |
112 | |
122 | |
113 | if (op->stats.luck) |
123 | if (op->stats.luck) |
114 | { |
124 | { |
115 | int luck = op->stats.luck; |
125 | int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
116 | |
126 | |
117 | if (rndm (base) < min (10, abs (luck))) |
127 | if (rndm (base) < min (10, abs (op->stats.luck))) |
118 | { |
128 | { |
119 | /* we have a winner */ |
129 | // we have a winner, increase/decrease number by one accordingly |
120 | luck = luck > 0 ? 1 : -1; |
130 | int adjust = sign (op->stats.luck); |
|
|
131 | |
|
|
132 | if (!prefer_high) |
|
|
133 | adjust = -adjust; |
121 | |
134 | |
122 | range -= luck; |
135 | num = clamp (num + adjust, r_min, r_max); |
123 | if (range < 1) |
|
|
124 | return omin; /*check again */ |
|
|
125 | |
|
|
126 | if (prefer_high) |
|
|
127 | r_min += luck; |
|
|
128 | |
|
|
129 | return clamp (ran % range + r_min, omin, r_max); |
|
|
130 | } |
136 | } |
131 | } |
137 | } |
132 | |
138 | |
133 | return ran % range + r_min; |
139 | return num; |
134 | } |
140 | } |
135 | |
141 | |
136 | /* |
142 | /* |
137 | * 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, |
138 | * 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. |
… | |
… | |
150 | luck = total = gotlucky = 0; |
156 | luck = total = gotlucky = 0; |
151 | int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
157 | int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
152 | |
158 | |
153 | if (size < 2 || diff < 1) |
159 | if (size < 2 || diff < 1) |
154 | { |
160 | { |
155 | LOG (llevError, "Calling die_roll with num=%d size=%d\n", num, size); |
161 | LOG (llevError | logBacktrace, "Calling die_roll with num=%d size=%d\n", num, size); |
156 | return num; /* avoids a float exception */ |
162 | return num; /* avoids a float exception */ |
157 | } |
163 | } |
158 | |
164 | |
159 | if (op->type == PLAYER) |
165 | if (op->type == PLAYER) |
160 | luck = op->stats.luck; |
166 | luck = op->stats.luck; |