1 |
root |
1.1 |
/* |
2 |
|
|
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
|
|
* |
4 |
root |
1.6 |
* Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
root |
1.5 |
* Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
6 |
|
|
* Copyright (©) 1992 Frank Tore Johansen |
7 |
root |
1.1 |
* |
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 |
root |
1.11 |
random_roll (int r_min, int r_max, const object *op, bool prefer_high) |
78 |
root |
1.1 |
{ |
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 |
root |
1.11 |
random_roll64 (sint64 r_min, sint64 r_max, const object *op, bool prefer_high) |
102 |
root |
1.1 |
{ |
103 |
|
|
sint64 omin = r_min; |
104 |
|
|
sint64 range = max (0, r_max - r_min + 1); |
105 |
|
|
int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
106 |
|
|
|
107 |
|
|
/* |
108 |
root |
1.10 |
* Make a call to get two 32 bit unsigned random numbers, and just do |
109 |
root |
1.1 |
* a little bitshifting. |
110 |
|
|
*/ |
111 |
|
|
sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31); |
112 |
|
|
|
113 |
root |
1.10 |
if (op->stats.luck) |
114 |
|
|
{ |
115 |
|
|
int luck = op->stats.luck; |
116 |
root |
1.1 |
|
117 |
root |
1.10 |
if (rndm (base) < min (10, abs (luck))) |
118 |
|
|
{ |
119 |
|
|
/* we have a winner */ |
120 |
|
|
luck = luck > 0 ? 1 : -1; |
121 |
|
|
|
122 |
|
|
range -= luck; |
123 |
|
|
if (range < 1) |
124 |
|
|
return omin; /*check again */ |
125 |
root |
1.1 |
|
126 |
root |
1.11 |
if (prefer_high) |
127 |
root |
1.10 |
r_min += luck; |
128 |
root |
1.1 |
|
129 |
root |
1.11 |
return clamp (ran % range + r_min, omin, r_max); |
130 |
root |
1.10 |
} |
131 |
root |
1.1 |
} |
132 |
|
|
|
133 |
|
|
return ran % range + r_min; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
/* |
137 |
|
|
* 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. |
139 |
|
|
* Generally, op should be the player/caster/hitter requesting the roll, |
140 |
|
|
* not the recipient (ie, the poor slob getting hit). |
141 |
|
|
* The args are num D size (ie 4d6) [garbled 20010916] |
142 |
|
|
*/ |
143 |
|
|
int |
144 |
root |
1.11 |
die_roll (int num, int size, const object *op, bool prefer_high) |
145 |
root |
1.1 |
{ |
146 |
root |
1.4 |
int min_roll, luck, total, i, gotlucky; |
147 |
root |
1.1 |
|
148 |
|
|
int diff = size; |
149 |
root |
1.4 |
min_roll = 1; |
150 |
root |
1.1 |
luck = total = gotlucky = 0; |
151 |
|
|
int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
152 |
|
|
|
153 |
|
|
if (size < 2 || diff < 1) |
154 |
|
|
{ |
155 |
|
|
LOG (llevError, "Calling die_roll with num=%d size=%d\n", num, size); |
156 |
|
|
return num; /* avoids a float exception */ |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
if (op->type == PLAYER) |
160 |
|
|
luck = op->stats.luck; |
161 |
|
|
|
162 |
|
|
for (i = 0; i < num; i++) |
163 |
|
|
{ |
164 |
root |
1.4 |
if (rndm (base) < min (10, abs (luck)) && !gotlucky) |
165 |
root |
1.1 |
{ |
166 |
|
|
/* we have a winner */ |
167 |
|
|
gotlucky++; |
168 |
|
|
((luck > 0) ? (luck = 1) : (luck = -1)); |
169 |
|
|
diff -= luck; |
170 |
|
|
if (diff < 1) |
171 |
|
|
return (num); /*check again */ |
172 |
root |
1.11 |
((prefer_high) ? (min_roll += luck) : (diff)); |
173 |
root |
1.4 |
total += max (1, min (size, rndm (diff) + min_roll)); |
174 |
root |
1.1 |
} |
175 |
|
|
else |
176 |
|
|
total += rndm (size) + 1; |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
return total; |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
///////////////////////////////////////////////////////////////////////////// |
183 |
|
|
|
184 |
|
|
#ifndef PATH_MAX |
185 |
|
|
# define PATH_MAX 8192 |
186 |
|
|
#endif |
187 |
|
|
|
188 |
|
|
char * |
189 |
|
|
path_combine (const char *src, const char *dst) |
190 |
|
|
{ |
191 |
|
|
char *p; |
192 |
|
|
static char path[PATH_MAX]; |
193 |
|
|
|
194 |
|
|
if (*dst == '/') |
195 |
|
|
{ |
196 |
|
|
/* absolute destination path => ignore source path */ |
197 |
|
|
strcpy (path, dst); |
198 |
|
|
} |
199 |
|
|
else |
200 |
|
|
{ |
201 |
|
|
/* relative destination path => add after last '/' of source */ |
202 |
|
|
strcpy (path, src); |
203 |
|
|
p = strrchr (path, '/'); |
204 |
|
|
if (p) |
205 |
|
|
p++; |
206 |
|
|
else |
207 |
|
|
{ |
208 |
|
|
p = path; |
209 |
|
|
if (*src == '/') |
210 |
|
|
*p++ = '/'; |
211 |
|
|
} |
212 |
|
|
|
213 |
|
|
strcpy (p, dst); |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
return path; |
217 |
|
|
} |
218 |
|
|
|
219 |
root |
1.2 |
static void |
220 |
root |
1.1 |
path_normalize (char *path) |
221 |
|
|
{ |
222 |
|
|
char *p; /* points to the beginning of the path not yet processed; this is |
223 |
|
|
either a path component or a path separator character */ |
224 |
|
|
char *q; /* points to the end of the path component p points to */ |
225 |
|
|
char *w; /* points to the end of the already normalized path; w <= p is |
226 |
|
|
maintained */ |
227 |
|
|
size_t len; /* length of current component (which p points to) */ |
228 |
|
|
|
229 |
|
|
p = path; |
230 |
|
|
w = p; |
231 |
|
|
while (*p != '\0') |
232 |
|
|
{ |
233 |
|
|
if (*p == '/') |
234 |
|
|
{ |
235 |
|
|
if ((w == path && *path == '/') || (w > path && w[-1] != '/')) |
236 |
|
|
*w++ = '/'; |
237 |
|
|
|
238 |
|
|
p++; |
239 |
|
|
continue; |
240 |
|
|
} |
241 |
|
|
|
242 |
|
|
q = strchr (p, '/'); |
243 |
|
|
if (q == NULL) |
244 |
|
|
q = p + strlen (p); |
245 |
|
|
len = q - p; |
246 |
|
|
assert (len > 0); |
247 |
|
|
|
248 |
|
|
if (len == 1 && *p == '.') |
249 |
|
|
{ |
250 |
|
|
/* remove current component */ |
251 |
|
|
} |
252 |
|
|
else if (len == 2 && memcmp (p, "..", 2) == 0) |
253 |
|
|
{ |
254 |
|
|
if (w == path || (w == path + 3 && memcmp (path, "../", 3) == 0)) |
255 |
|
|
{ |
256 |
|
|
/* keep ".." at beginning of relative path ("../x" => "../x") */ |
257 |
|
|
memmove (w, p, len); |
258 |
|
|
w += len; |
259 |
|
|
} |
260 |
|
|
else if (w == path + 1 && *path == '/') |
261 |
|
|
{ |
262 |
|
|
/* remove ".." at beginning of absolute path ("/../x" => "/x") */ |
263 |
|
|
} |
264 |
|
|
else |
265 |
|
|
{ |
266 |
|
|
/* remove both current component ".." and preceding one */ |
267 |
|
|
if (w > path && w[-1] == '/') |
268 |
|
|
w--; |
269 |
|
|
while (w > path && w[-1] != '/') |
270 |
|
|
w--; |
271 |
|
|
} |
272 |
|
|
} |
273 |
|
|
else |
274 |
|
|
{ |
275 |
|
|
/* normal component ==> add it */ |
276 |
|
|
memmove (w, p, len); |
277 |
|
|
w += len; |
278 |
|
|
} |
279 |
|
|
|
280 |
|
|
p = q; |
281 |
|
|
} |
282 |
|
|
|
283 |
|
|
/* remove trailing slashes, but keep the one at the start of the path */ |
284 |
|
|
while (w > path + 1 && w[-1] == '/') |
285 |
|
|
w--; |
286 |
|
|
|
287 |
|
|
*w = '\0'; |
288 |
|
|
} |
289 |
|
|
|
290 |
|
|
char * |
291 |
|
|
path_combine_and_normalize (const char *src, const char *dst) |
292 |
|
|
{ |
293 |
|
|
char *path; |
294 |
|
|
|
295 |
|
|
path = path_combine (src, dst); |
296 |
|
|
path_normalize (path); |
297 |
|
|
return (path); |
298 |
|
|
} |
299 |
|
|
|
300 |
|
|
#define EOL_SIZE (sizeof("\n")-1) |
301 |
|
|
void |
302 |
|
|
strip_endline (char *buf) |
303 |
|
|
{ |
304 |
root |
1.9 |
if (*buf && buf [strlen (buf) - 1] == '\n') |
305 |
|
|
buf [strlen (buf) - 1] = '\0'; |
306 |
root |
1.1 |
} |
307 |
|
|
|
308 |
|
|
/** |
309 |
|
|
* Replace in string src all occurrences of key by replacement. The resulting |
310 |
|
|
* string is put into result; at most resultsize characters (including the |
311 |
|
|
* terminating null character) will be written to result. |
312 |
|
|
*/ |
313 |
|
|
void |
314 |
|
|
replace (const char *src, const char *key, const char *replacement, char *result, size_t resultsize) |
315 |
|
|
{ |
316 |
|
|
size_t resultlen; |
317 |
|
|
size_t keylen; |
318 |
|
|
|
319 |
|
|
/* special case to prevent infinite loop if key==replacement=="" */ |
320 |
|
|
if (strcmp (key, replacement) == 0) |
321 |
|
|
{ |
322 |
|
|
snprintf (result, resultsize, "%s", src); |
323 |
|
|
return; |
324 |
|
|
} |
325 |
|
|
|
326 |
|
|
keylen = strlen (key); |
327 |
|
|
|
328 |
|
|
resultlen = 0; |
329 |
|
|
while (*src != '\0' && resultlen + 1 < resultsize) |
330 |
|
|
{ |
331 |
|
|
if (strncmp (src, key, keylen) == 0) |
332 |
|
|
{ |
333 |
|
|
snprintf (result + resultlen, resultsize - resultlen, "%s", replacement); |
334 |
|
|
resultlen += strlen (result + resultlen); |
335 |
|
|
src += keylen; |
336 |
|
|
} |
337 |
|
|
else |
338 |
|
|
{ |
339 |
|
|
result[resultlen++] = *src++; |
340 |
|
|
} |
341 |
|
|
} |
342 |
|
|
result[resultlen] = '\0'; |
343 |
|
|
} |
344 |
|
|
|