1 |
/* |
2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
* |
4 |
* Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
* Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
6 |
* Copyright (©) 1992 Frank Tore Johansen |
7 |
* |
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 |
10 |
* Free Software Foundation, either version 3 of the License, or (at your |
11 |
* option) any later version. |
12 |
* |
13 |
* This program is distributed in the hope that it will be useful, |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
* GNU General Public License for more details. |
17 |
* |
18 |
* You should have received a copy of the Affero GNU General Public License |
19 |
* and the GNU General Public License along with this program. If not, see |
20 |
* <http://www.gnu.org/licenses/>. |
21 |
* |
22 |
* The authors can be reached via e-mail to <support@deliantra.net> |
23 |
*/ |
24 |
|
25 |
/* |
26 |
* compatibility functions for older (GPL) source code parts |
27 |
*/ |
28 |
|
29 |
#include <cstring> |
30 |
#include <assert.h> |
31 |
#include <stdio.h> |
32 |
#include <string.h> |
33 |
#include <limits.h> |
34 |
|
35 |
#include <global.h> |
36 |
#include "define.h" |
37 |
#include "path.h" |
38 |
|
39 |
/* buf_overflow() - we don't want to exceed the buffer size of |
40 |
* buf1 by adding on buf2! Returns true if overflow will occur. |
41 |
*/ |
42 |
int |
43 |
buf_overflow (const char *buf1, const char *buf2, int bufsize) |
44 |
{ |
45 |
int len1 = 0, len2 = 0; |
46 |
|
47 |
if (buf1) |
48 |
len1 = strlen (buf1); |
49 |
|
50 |
if (buf2) |
51 |
len2 = strlen (buf2); |
52 |
|
53 |
if ((len1 + len2) >= bufsize) |
54 |
return 1; |
55 |
|
56 |
return 0; |
57 |
} |
58 |
|
59 |
///////////////////////////////////////////////////////////////////////////// |
60 |
|
61 |
/* |
62 |
* The random functions here take luck into account when rolling random |
63 |
* dice or numbers. This function has less of an impact the larger the |
64 |
* difference becomes in the random numbers. IE, the effect is lessened |
65 |
* on a 1-1000 roll, vs a 1-6 roll. This can be used by crafty programmers, |
66 |
* to specifically disable luck in certain rolls, simply by making the |
67 |
* numbers larger (ie, 1d1000 > 500 vs 1d6 > 3) |
68 |
*/ |
69 |
|
70 |
/* |
71 |
* Roll a random number between min and max. Uses op to determine luck, |
72 |
* and if goodbad is non-zero, luck increases the roll, if zero, it decreases. |
73 |
* Generally, op should be the player/caster/hitter requesting the roll, |
74 |
* not the recipient (ie, the poor slob getting hit). [garbled 20010916] |
75 |
*/ |
76 |
int |
77 |
random_roll (int r_min, int r_max, const object *op, bool prefer_high) |
78 |
{ |
79 |
r_max = max (r_min, r_max); |
80 |
|
81 |
int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */ |
82 |
|
83 |
if (op->type == PLAYER) |
84 |
{ |
85 |
int luck = op->stats.luck; |
86 |
|
87 |
if (rndm (base) < min (10, abs (luck))) |
88 |
{ |
89 |
//TODO: take luck into account |
90 |
} |
91 |
} |
92 |
|
93 |
return rndm (r_min, r_max); |
94 |
} |
95 |
|
96 |
/* |
97 |
* This is a 64 bit version of random_roll above. This is needed |
98 |
* for exp loss calculations for players changing religions. |
99 |
*/ |
100 |
sint64 |
101 |
random_roll64 (sint64 r_min, sint64 r_max, const object *op, bool prefer_high) |
102 |
{ |
103 |
sint64 range = max (0, r_max - r_min + 1); |
104 |
int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
105 |
|
106 |
/* |
107 |
* Make a call to get two 32 bit unsigned random numbers, and just do |
108 |
* a little bitshifting. |
109 |
*/ |
110 |
sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31); |
111 |
|
112 |
if (op->stats.luck) |
113 |
{ |
114 |
int luck = op->stats.luck; |
115 |
|
116 |
if (rndm (base) < min (10, abs (luck))) |
117 |
{ |
118 |
/* we have a winner */ |
119 |
luck = luck > 0 ? 1 : -1; |
120 |
|
121 |
range -= luck; |
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 |
} |
130 |
} |
131 |
|
132 |
return ran % range + r_min; |
133 |
} |
134 |
|
135 |
/* |
136 |
* 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. |
138 |
* Generally, op should be the player/caster/hitter requesting the roll, |
139 |
* not the recipient (ie, the poor slob getting hit). |
140 |
* The args are num D size (ie 4d6) [garbled 20010916] |
141 |
*/ |
142 |
int |
143 |
die_roll (int num, int size, const object *op, bool prefer_high) |
144 |
{ |
145 |
int min_roll, luck, total, i, gotlucky; |
146 |
|
147 |
int diff = size; |
148 |
min_roll = 1; |
149 |
luck = total = gotlucky = 0; |
150 |
int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
151 |
|
152 |
if (size < 2 || diff < 1) |
153 |
{ |
154 |
LOG (llevError | logBacktrace, "Calling die_roll with num=%d size=%d\n", num, size); |
155 |
return num; /* avoids a float exception */ |
156 |
} |
157 |
|
158 |
if (op->type == PLAYER) |
159 |
luck = op->stats.luck; |
160 |
|
161 |
for (i = 0; i < num; i++) |
162 |
{ |
163 |
if (rndm (base) < min (10, abs (luck)) && !gotlucky) |
164 |
{ |
165 |
/* we have a winner */ |
166 |
gotlucky++; |
167 |
((luck > 0) ? (luck = 1) : (luck = -1)); |
168 |
diff -= luck; |
169 |
if (diff < 1) |
170 |
return (num); /*check again */ |
171 |
((prefer_high) ? (min_roll += luck) : (diff)); |
172 |
total += max (1, min (size, rndm (diff) + min_roll)); |
173 |
} |
174 |
else |
175 |
total += rndm (size) + 1; |
176 |
} |
177 |
|
178 |
return total; |
179 |
} |
180 |
|
181 |
///////////////////////////////////////////////////////////////////////////// |
182 |
|
183 |
#ifndef PATH_MAX |
184 |
# define PATH_MAX 8192 |
185 |
#endif |
186 |
|
187 |
char * |
188 |
path_combine (const char *src, const char *dst) |
189 |
{ |
190 |
char *p; |
191 |
static char path[PATH_MAX]; |
192 |
|
193 |
if (*dst == '/') |
194 |
{ |
195 |
/* absolute destination path => ignore source path */ |
196 |
strcpy (path, dst); |
197 |
} |
198 |
else |
199 |
{ |
200 |
/* relative destination path => add after last '/' of source */ |
201 |
strcpy (path, src); |
202 |
p = strrchr (path, '/'); |
203 |
if (p) |
204 |
p++; |
205 |
else |
206 |
{ |
207 |
p = path; |
208 |
if (*src == '/') |
209 |
*p++ = '/'; |
210 |
} |
211 |
|
212 |
strcpy (p, dst); |
213 |
} |
214 |
|
215 |
return path; |
216 |
} |
217 |
|
218 |
static void |
219 |
path_normalize (char *path) |
220 |
{ |
221 |
char *p; /* points to the beginning of the path not yet processed; this is |
222 |
either a path component or a path separator character */ |
223 |
char *q; /* points to the end of the path component p points to */ |
224 |
char *w; /* points to the end of the already normalized path; w <= p is |
225 |
maintained */ |
226 |
size_t len; /* length of current component (which p points to) */ |
227 |
|
228 |
p = path; |
229 |
w = p; |
230 |
while (*p != '\0') |
231 |
{ |
232 |
if (*p == '/') |
233 |
{ |
234 |
if ((w == path && *path == '/') || (w > path && w[-1] != '/')) |
235 |
*w++ = '/'; |
236 |
|
237 |
p++; |
238 |
continue; |
239 |
} |
240 |
|
241 |
q = strchr (p, '/'); |
242 |
if (q == NULL) |
243 |
q = p + strlen (p); |
244 |
len = q - p; |
245 |
assert (len > 0); |
246 |
|
247 |
if (len == 1 && *p == '.') |
248 |
{ |
249 |
/* remove current component */ |
250 |
} |
251 |
else if (len == 2 && memcmp (p, "..", 2) == 0) |
252 |
{ |
253 |
if (w == path || (w == path + 3 && memcmp (path, "../", 3) == 0)) |
254 |
{ |
255 |
/* keep ".." at beginning of relative path ("../x" => "../x") */ |
256 |
memmove (w, p, len); |
257 |
w += len; |
258 |
} |
259 |
else if (w == path + 1 && *path == '/') |
260 |
{ |
261 |
/* remove ".." at beginning of absolute path ("/../x" => "/x") */ |
262 |
} |
263 |
else |
264 |
{ |
265 |
/* remove both current component ".." and preceding one */ |
266 |
if (w > path && w[-1] == '/') |
267 |
w--; |
268 |
while (w > path && w[-1] != '/') |
269 |
w--; |
270 |
} |
271 |
} |
272 |
else |
273 |
{ |
274 |
/* normal component ==> add it */ |
275 |
memmove (w, p, len); |
276 |
w += len; |
277 |
} |
278 |
|
279 |
p = q; |
280 |
} |
281 |
|
282 |
/* remove trailing slashes, but keep the one at the start of the path */ |
283 |
while (w > path + 1 && w[-1] == '/') |
284 |
w--; |
285 |
|
286 |
*w = '\0'; |
287 |
} |
288 |
|
289 |
char * |
290 |
path_combine_and_normalize (const char *src, const char *dst) |
291 |
{ |
292 |
char *path; |
293 |
|
294 |
path = path_combine (src, dst); |
295 |
path_normalize (path); |
296 |
return (path); |
297 |
} |
298 |
|
299 |
#define EOL_SIZE (sizeof("\n")-1) |
300 |
void |
301 |
strip_endline (char *buf) |
302 |
{ |
303 |
if (*buf && buf [strlen (buf) - 1] == '\n') |
304 |
buf [strlen (buf) - 1] = '\0'; |
305 |
} |
306 |
|
307 |
/** |
308 |
* Replace in string src all occurrences of key by replacement. The resulting |
309 |
* string is put into result; at most resultsize characters (including the |
310 |
* terminating null character) will be written to result. |
311 |
*/ |
312 |
void |
313 |
replace (const char *src, const char *key, const char *replacement, char *result, size_t resultsize) |
314 |
{ |
315 |
size_t resultlen; |
316 |
size_t keylen; |
317 |
|
318 |
/* special case to prevent infinite loop if key==replacement=="" */ |
319 |
if (strcmp (key, replacement) == 0) |
320 |
{ |
321 |
snprintf (result, resultsize, "%s", src); |
322 |
return; |
323 |
} |
324 |
|
325 |
keylen = strlen (key); |
326 |
|
327 |
resultlen = 0; |
328 |
while (*src != '\0' && resultlen + 1 < resultsize) |
329 |
{ |
330 |
if (strncmp (src, key, keylen) == 0) |
331 |
{ |
332 |
snprintf (result + resultlen, resultsize - resultlen, "%s", replacement); |
333 |
resultlen += strlen (result + resultlen); |
334 |
src += keylen; |
335 |
} |
336 |
else |
337 |
{ |
338 |
result[resultlen++] = *src++; |
339 |
} |
340 |
} |
341 |
result[resultlen] = '\0'; |
342 |
} |
343 |
|