1 |
/* |
2 |
* static char *rcsid_utils_c = |
3 |
* "$Id: utils.c,v 1.22 2006/01/08 22:47:56 qal21 Exp $"; |
4 |
*/ |
5 |
|
6 |
/* |
7 |
CrossFire, A Multiplayer game for X-windows |
8 |
|
9 |
Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
10 |
Copyright (C) 1992 Frank Tore Johansen |
11 |
|
12 |
This program is free software; you can redistribute it and/or modify |
13 |
it under the terms of the GNU General Public License as published by |
14 |
the Free Software Foundation; either version 2 of the License, or |
15 |
(at your option) any later version. |
16 |
|
17 |
This program is distributed in the hope that it will be useful, |
18 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 |
GNU General Public License for more details. |
21 |
|
22 |
You should have received a copy of the GNU General Public License |
23 |
along with this program; if not, write to the Free Software |
24 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
25 |
|
26 |
The authors can be reached via e-mail at crossfire-devel@real-time.com |
27 |
*/ |
28 |
|
29 |
/* |
30 |
* General convenience functions for crossfire. |
31 |
*/ |
32 |
|
33 |
#include <global.h> |
34 |
#include <funcpoint.h> |
35 |
|
36 |
/* |
37 |
* The random functions here take luck into account when rolling random |
38 |
* dice or numbers. This function has less of an impact the larger the |
39 |
* difference becomes in the random numbers. IE, the effect is lessened |
40 |
* on a 1-1000 roll, vs a 1-6 roll. This can be used by crafty programmers, |
41 |
* to specifically disable luck in certain rolls, simply by making the |
42 |
* numbers larger (ie, 1d1000 > 500 vs 1d6 > 3) |
43 |
*/ |
44 |
|
45 |
/* |
46 |
* Roll a random number between min and max. Uses op to determine luck, |
47 |
* and if goodbad is non-zero, luck increases the roll, if zero, it decreases. |
48 |
* Generally, op should be the player/caster/hitter requesting the roll, |
49 |
* not the recipient (ie, the poor slob getting hit). [garbled 20010916] |
50 |
*/ |
51 |
|
52 |
int random_roll(int min, int max, object *op, int goodbad) { |
53 |
int omin, diff, luck, base, ran; |
54 |
|
55 |
omin = min; |
56 |
diff = max - min + 1; |
57 |
((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ |
58 |
|
59 |
if (max < 1 || diff < 1) { |
60 |
LOG(llevError, "Calling random_roll with min=%d max=%d\n", min, max); |
61 |
return(min); /* avoids a float exception */ |
62 |
} |
63 |
|
64 |
ran = RANDOM(); |
65 |
|
66 |
if (op->type != PLAYER) |
67 |
return((ran%diff)+min); |
68 |
|
69 |
luck = op->stats.luck; |
70 |
if (RANDOM()%base < MIN(10, abs(luck))) { |
71 |
/* we have a winner */ |
72 |
((luck > 0) ? (luck = 1) : (luck = -1)); |
73 |
diff -= luck; |
74 |
if (diff < 1) |
75 |
return(omin); /*check again*/ |
76 |
((goodbad) ? (min += luck) : (diff)); |
77 |
|
78 |
return(MAX(omin, MIN(max, (ran%diff)+min))); |
79 |
} |
80 |
return((ran%diff)+min); |
81 |
} |
82 |
|
83 |
/* |
84 |
* This is a 64 bit version of random_roll above. This is needed |
85 |
* for exp loss calculations for players changing religions. |
86 |
*/ |
87 |
|
88 |
sint64 random_roll64(sint64 min, sint64 max, object *op, int goodbad) { |
89 |
sint64 omin, diff, luck, ran; |
90 |
int base; |
91 |
|
92 |
omin = min; |
93 |
diff = max - min + 1; |
94 |
((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ |
95 |
|
96 |
if (max < 1 || diff < 1) { |
97 |
#ifndef WIN32 |
98 |
LOG(llevError, "Calling random_roll with min=%lld max=%lld\n", min, max); |
99 |
#else |
100 |
LOG(llevError, "Calling random_roll with min=%I64d max=%I64d\n", min, max); |
101 |
#endif |
102 |
return(min); /* avoids a float exception */ |
103 |
} |
104 |
|
105 |
/* Don't know of a portable call to get 64 bit random values. |
106 |
* So make a call to get two 32 bit random numbers, and just to |
107 |
* a little byteshifting. Do make sure the first one is only |
108 |
* 32 bit, so we don't get skewed results |
109 |
*/ |
110 |
ran = (RANDOM() & 0xffffffff) | ((sint64)RANDOM() << 32); |
111 |
|
112 |
if (op->type != PLAYER) |
113 |
return((ran%diff)+min); |
114 |
|
115 |
luck = op->stats.luck; |
116 |
if (RANDOM()%base < MIN(10, abs(luck))) { |
117 |
/* we have a winner */ |
118 |
((luck > 0) ? (luck = 1) : (luck = -1)); |
119 |
diff -= luck; |
120 |
if (diff < 1) |
121 |
return(omin); /*check again*/ |
122 |
((goodbad) ? (min += luck) : (diff)); |
123 |
|
124 |
return(MAX(omin, MIN(max, (ran%diff)+min))); |
125 |
} |
126 |
return((ran%diff)+min); |
127 |
} |
128 |
|
129 |
/* |
130 |
* Roll a number of dice (2d3, 4d6). Uses op to determine luck, |
131 |
* If goodbad is non-zero, luck increases the roll, if zero, it decreases. |
132 |
* Generally, op should be the player/caster/hitter requesting the roll, |
133 |
* not the recipient (ie, the poor slob getting hit). |
134 |
* The args are num D size (ie 4d6) [garbled 20010916] |
135 |
*/ |
136 |
|
137 |
int die_roll(int num, int size, object *op, int goodbad) { |
138 |
int min, diff, luck, total, i, gotlucky, base, ran; |
139 |
|
140 |
diff = size; |
141 |
min = 1; |
142 |
luck = total = gotlucky = 0; |
143 |
((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ |
144 |
if (size < 2 || diff < 1) { |
145 |
LOG(llevError, "Calling die_roll with num=%d size=%d\n", num, size); |
146 |
return(num); /* avoids a float exception */ |
147 |
} |
148 |
|
149 |
if (op->type == PLAYER) |
150 |
luck = op->stats.luck; |
151 |
|
152 |
for (i = 0; i < num; i++) { |
153 |
if (RANDOM()%base < MIN(10, abs(luck)) && !gotlucky) { |
154 |
/* we have a winner */ |
155 |
gotlucky++; |
156 |
((luck > 0) ? (luck = 1) : (luck = -1)); |
157 |
diff -= luck; |
158 |
if (diff < 1) |
159 |
return(num); /*check again*/ |
160 |
((goodbad) ? (min += luck) : (diff)); |
161 |
ran = RANDOM(); |
162 |
total += MAX(1, MIN(size, (ran%diff)+min)); |
163 |
} else { |
164 |
total += RANDOM()%size+1; |
165 |
} |
166 |
} |
167 |
return(total); |
168 |
} |
169 |
|
170 |
/* |
171 |
* Another convenience function. Returns a number between min and max. |
172 |
* It is suggested one use these functions rather than RANDOM()%, as it |
173 |
* would appear that a number of off-by-one-errors exist due to improper |
174 |
* use of %. This should also prevent SIGFPE. |
175 |
*/ |
176 |
|
177 |
int rndm(int min, int max) |
178 |
{ |
179 |
int diff; |
180 |
|
181 |
diff = max - min + 1; |
182 |
if (max < 1 || diff < 1) |
183 |
return(min); |
184 |
|
185 |
return(RANDOM()%diff+min); |
186 |
} |
187 |
|
188 |
/* decay and destroy persihable items in a map */ |
189 |
|
190 |
void decay_objects(mapstruct *m) |
191 |
{ |
192 |
int x, y, destroy; |
193 |
object *op, *otmp; |
194 |
|
195 |
if (m->unique) |
196 |
return; |
197 |
|
198 |
for (x=0; x < MAP_WIDTH(m); x++) |
199 |
for (y=0; y < MAP_HEIGHT(m); y++) |
200 |
for (op = get_map_ob(m, x, y); op; op = otmp) { |
201 |
destroy = 0; |
202 |
otmp = op->above; |
203 |
if (QUERY_FLAG(op,FLAG_IS_FLOOR) && QUERY_FLAG(op, FLAG_UNIQUE)) |
204 |
break; |
205 |
if (QUERY_FLAG(op, FLAG_IS_FLOOR) || |
206 |
QUERY_FLAG(op, FLAG_OBJ_ORIGINAL) || |
207 |
QUERY_FLAG(op, FLAG_OBJ_SAVE_ON_OVL) || |
208 |
QUERY_FLAG(op, FLAG_UNIQUE) || |
209 |
QUERY_FLAG(op, FLAG_OVERLAY_FLOOR) || |
210 |
QUERY_FLAG(op, FLAG_UNPAID) || IS_LIVE(op)) |
211 |
continue; |
212 |
/* otherwise, we decay and destroy */ |
213 |
if (IS_WEAPON(op)) { |
214 |
op->stats.dam--; |
215 |
if (op->stats.dam < 0) |
216 |
destroy = 1; |
217 |
} else if (IS_ARMOR(op)) { |
218 |
op->stats.ac--; |
219 |
if (op->stats.ac < 0) |
220 |
destroy = 1; |
221 |
} else if (op->type == FOOD) { |
222 |
op->stats.food -= rndm(5,20); |
223 |
if (op->stats.food < 0) |
224 |
destroy = 1; |
225 |
} else { |
226 |
if (op->material & M_PAPER || op->material & M_LEATHER || |
227 |
op->material & M_WOOD || op->material & M_ORGANIC || |
228 |
op->material & M_CLOTH || op->material & M_LIQUID) |
229 |
destroy = 1; |
230 |
if (op->material & M_IRON && rndm(1,5) == 1) |
231 |
destroy = 1; |
232 |
if (op->material & M_GLASS && rndm(1,2) == 1) |
233 |
destroy = 1; |
234 |
if ((op->material & M_STONE || op->material & M_ADAMANT) && |
235 |
rndm(1,10) == 1) |
236 |
destroy = 1; |
237 |
if ((op->material & M_SOFT_METAL || op->material & M_BONE) && |
238 |
rndm(1,3) == 1) |
239 |
destroy = 1; |
240 |
if (op->material & M_ICE && MAP_TEMP(m) > 32) |
241 |
destroy = 1; |
242 |
} |
243 |
/* adjust overall chance below */ |
244 |
if (destroy && rndm(0, 1)) { |
245 |
remove_ob(op); |
246 |
free_object(op); |
247 |
} |
248 |
} |
249 |
} |
250 |
|
251 |
/* convert materialname to materialtype_t */ |
252 |
|
253 |
materialtype_t *name_to_material(const char *name) |
254 |
{ |
255 |
materialtype_t *mt, *nmt; |
256 |
|
257 |
mt = NULL; |
258 |
for (nmt = materialt; nmt != NULL && nmt->next != NULL; nmt=nmt->next) { |
259 |
if (strcmp(name, nmt->name) == 0) { |
260 |
mt = nmt; |
261 |
break; |
262 |
} |
263 |
} |
264 |
return mt; |
265 |
} |
266 |
|
267 |
/* when doing transmutation of objects, we have to recheck the resistances, |
268 |
* as some that did not apply previously, may apply now. |
269 |
*/ |
270 |
|
271 |
void transmute_materialname(object *op, object *change) |
272 |
{ |
273 |
materialtype_t *mt; |
274 |
int j; |
275 |
|
276 |
if (op->materialname == NULL) |
277 |
return; |
278 |
|
279 |
if (change->materialname != NULL && |
280 |
strcmp(op->materialname, change->materialname)) |
281 |
return; |
282 |
|
283 |
if (!IS_ARMOR(op)) |
284 |
return; |
285 |
|
286 |
mt = name_to_material(op->materialname); |
287 |
|
288 |
for (j=0; j < NROFATTACKS; j++) |
289 |
if (op->resist[j] == 0 && change->resist[j] != 0) { |
290 |
op->resist[j] += mt->mod[j]; |
291 |
if (op->resist[j] > 100) |
292 |
op->resist[j] = 100; |
293 |
if (op->resist[j] < -100) |
294 |
op->resist[j] = -100; |
295 |
} |
296 |
} |
297 |
|
298 |
/* set the materialname and type for an item */ |
299 |
void set_materialname(object *op, int difficulty, materialtype_t *nmt) |
300 |
{ |
301 |
materialtype_t *mt, *lmt; |
302 |
int j; |
303 |
|
304 |
if (op->materialname != NULL) |
305 |
return; |
306 |
|
307 |
|
308 |
|
309 |
if (nmt == NULL) { |
310 |
lmt = NULL; |
311 |
#ifndef NEW_MATERIAL_CODE |
312 |
for (mt = materialt; mt != NULL && mt->next != NULL; mt=mt->next) { |
313 |
if (op->material & mt->material) { |
314 |
lmt = mt; |
315 |
break; |
316 |
} |
317 |
} |
318 |
|
319 |
#else |
320 |
for (mt = materialt; mt != NULL && mt->next != NULL; mt=mt->next) { |
321 |
if (op->material & mt->material && rndm(1, 100) <= mt->chance && |
322 |
difficulty >= mt->difficulty && |
323 |
(op->magic >= mt->magic || mt->magic == 0)) { |
324 |
lmt = mt; |
325 |
if (!(IS_WEAPON(op) || IS_ARMOR(op))) |
326 |
break; |
327 |
} |
328 |
} |
329 |
#endif |
330 |
} else { |
331 |
lmt = nmt; |
332 |
} |
333 |
|
334 |
if (lmt != NULL) { |
335 |
#ifndef NEW_MATERIAL_CODE |
336 |
op->materialname = add_string(lmt->name); |
337 |
return; |
338 |
#else |
339 |
|
340 |
if (op->stats.dam && IS_WEAPON(op)) { |
341 |
op->stats.dam += lmt->damage; |
342 |
if (op->stats.dam < 1) |
343 |
op->stats.dam = 1; |
344 |
} |
345 |
if (op->stats.sp && op->type == BOW) |
346 |
op->stats.sp += lmt->sp; |
347 |
if (op->stats.wc && IS_WEAPON(op)) |
348 |
op->stats.wc += lmt->wc; |
349 |
if (IS_ARMOR(op)) { |
350 |
if (op->stats.ac) |
351 |
op->stats.ac += lmt->ac; |
352 |
for (j=0; j < NROFATTACKS; j++) |
353 |
if (op->resist[j] != 0) { |
354 |
op->resist[j] += lmt->mod[j]; |
355 |
if (op->resist[j] > 100) |
356 |
op->resist[j] = 100; |
357 |
if (op->resist[j] < -100) |
358 |
op->resist[j] = -100; |
359 |
} |
360 |
} |
361 |
op->materialname = add_string(lmt->name); |
362 |
/* dont make it unstackable if it doesn't need to be */ |
363 |
if (IS_WEAPON(op) || IS_ARMOR(op)) { |
364 |
op->weight = (op->weight * lmt->weight)/100; |
365 |
op->value = (op->value * lmt->value)/100; |
366 |
} |
367 |
#endif |
368 |
} |
369 |
} |
370 |
|
371 |
/* |
372 |
* Strip out the media tags from a String. |
373 |
* Warning the input string will contain the result string |
374 |
*/ |
375 |
void strip_media_tag(char *message){ |
376 |
int in_tag=0; |
377 |
char* dest; |
378 |
char* src; |
379 |
src=dest=message; |
380 |
while (*src!='\0'){ |
381 |
if (*src=='['){ |
382 |
in_tag=1; |
383 |
} else if (in_tag && (*src==']')) |
384 |
in_tag=0; |
385 |
else if (!in_tag){ |
386 |
*dest=*src; |
387 |
dest++; |
388 |
} |
389 |
src++; |
390 |
} |
391 |
*dest='\0'; |
392 |
} |
393 |
|
394 |
const char* strrstr(const char* haystack, const char* needle){ |
395 |
const char* lastneedle; |
396 |
lastneedle=NULL; |
397 |
while((haystack=strstr(haystack,needle))!=NULL){ |
398 |
lastneedle=haystack; |
399 |
haystack++; |
400 |
} |
401 |
return lastneedle; |
402 |
|
403 |
} |
404 |
#define EOL_SIZE (sizeof("\n")-1) |
405 |
void strip_endline(char* buf){ |
406 |
if (strlen(buf)<sizeof("\n")){ |
407 |
return; |
408 |
} |
409 |
if (!strcmp(buf+strlen(buf)-EOL_SIZE,"\n")) |
410 |
buf[strlen(buf)-EOL_SIZE]='\0'; |
411 |
} |
412 |
|
413 |
/** |
414 |
* Replace in string src all occurrences of key by replacement. The resulting |
415 |
* string is put into result; at most resultsize characters (including the |
416 |
* terminating null character) will be written to result. |
417 |
*/ |
418 |
void replace(const char *src, const char *key, const char *replacement, char *result, size_t resultsize) |
419 |
{ |
420 |
size_t resultlen; |
421 |
size_t keylen; |
422 |
|
423 |
/* special case to prevent infinite loop if key==replacement=="" */ |
424 |
if(strcmp(key, replacement) == 0) |
425 |
{ |
426 |
snprintf(result, resultsize, "%s", src); |
427 |
return; |
428 |
} |
429 |
|
430 |
keylen = strlen(key); |
431 |
|
432 |
resultlen = 0; |
433 |
while(*src != '\0' && resultlen+1 < resultsize) |
434 |
{ |
435 |
if(strncmp(src, key, keylen) == 0) |
436 |
{ |
437 |
snprintf(result+resultlen, resultsize-resultlen, "%s", replacement); |
438 |
resultlen += strlen(result+resultlen); |
439 |
src += keylen; |
440 |
} |
441 |
else |
442 |
{ |
443 |
result[resultlen++] = *src++; |
444 |
} |
445 |
} |
446 |
result[resultlen] = '\0'; |
447 |
} |
448 |
|
449 |
/** |
450 |
* Taking a string as an argument, mutate it into a string that looks like a list. |
451 |
* a 'list' for the purposes here, is a string of items, seperated by commas, except |
452 |
* for the last entry, which has an 'and' before it, and a full stop (period) after it. |
453 |
* This function will also strip all trailing non alphanumeric characters. |
454 |
* It does not insert an oxford comma. |
455 |
*/ |
456 |
|
457 |
void make_list_like(char *input) { |
458 |
char *p, tmp[MAX_BUF]; |
459 |
int i; |
460 |
if (!input || strlen(input) > MAX_BUF-5) return; |
461 |
/* bad stuff would happen if we continued here, the -5 is to make space for ' and ' */ |
462 |
|
463 |
strncpy(tmp, input, MAX_BUF-5); |
464 |
/*trim all trailing commas, spaces etc.*/ |
465 |
for (i=strlen(tmp); !isalnum(tmp[i]) && i >= 0; i--) |
466 |
tmp[i]='\0'; |
467 |
strcat(tmp, "."); |
468 |
|
469 |
p=strrchr(tmp, ','); |
470 |
if (p) { |
471 |
*p='\0'; |
472 |
strcpy(input, tmp); |
473 |
p++; |
474 |
strcat(input, " and"); |
475 |
strcat(input, p); |
476 |
} |
477 |
else strcpy(input, tmp); |
478 |
return; |
479 |
} |