ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/utils.c
Revision: 1.2
Committed: Wed Mar 8 18:28:54 2006 UTC (18 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: LAST_C_VERSION, difficulty_fix_merge_060810_2300
Branch point for: difficulty_fix
Changes since 1.1: +9 -5 lines
Log Message:
transmute_materialname crashed on every op with an undefined material, make
it log an error instead.

File Contents

# User Rev Content
1 root 1.1 /*
2     * static char *rcsid_utils_c =
3 root 1.2 * "$Id$";
4 root 1.1 */
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 root 1.2 int random_roll(int min, int max, const object *op, int goodbad) {
53 root 1.1 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 root 1.2 sint64 random_roll64(sint64 min, sint64 max, const object *op, int goodbad) {
89 root 1.1 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 root 1.2 int die_roll(int num, int size, const object *op, int goodbad) {
138 root 1.1 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 root 1.2 void transmute_materialname(object *op, const object *change)
272 root 1.1 {
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 root 1.2 if (!mt) {
288     LOG(llevError, "archetype '%s>%s' uses nonexistent material '%s'\n", op->arch->name, op->name, op->materialname);
289     return;
290     }
291 root 1.1
292     for (j=0; j < NROFATTACKS; j++)
293     if (op->resist[j] == 0 && change->resist[j] != 0) {
294     op->resist[j] += mt->mod[j];
295     if (op->resist[j] > 100)
296     op->resist[j] = 100;
297     if (op->resist[j] < -100)
298     op->resist[j] = -100;
299     }
300     }
301    
302     /* set the materialname and type for an item */
303     void set_materialname(object *op, int difficulty, materialtype_t *nmt)
304     {
305     materialtype_t *mt, *lmt;
306     int j;
307    
308     if (op->materialname != NULL)
309     return;
310    
311    
312    
313     if (nmt == NULL) {
314     lmt = NULL;
315     #ifndef NEW_MATERIAL_CODE
316     for (mt = materialt; mt != NULL && mt->next != NULL; mt=mt->next) {
317     if (op->material & mt->material) {
318     lmt = mt;
319     break;
320     }
321     }
322    
323     #else
324     for (mt = materialt; mt != NULL && mt->next != NULL; mt=mt->next) {
325     if (op->material & mt->material && rndm(1, 100) <= mt->chance &&
326     difficulty >= mt->difficulty &&
327     (op->magic >= mt->magic || mt->magic == 0)) {
328     lmt = mt;
329     if (!(IS_WEAPON(op) || IS_ARMOR(op)))
330     break;
331     }
332     }
333     #endif
334     } else {
335     lmt = nmt;
336     }
337    
338     if (lmt != NULL) {
339     #ifndef NEW_MATERIAL_CODE
340     op->materialname = add_string(lmt->name);
341     return;
342     #else
343    
344     if (op->stats.dam && IS_WEAPON(op)) {
345     op->stats.dam += lmt->damage;
346     if (op->stats.dam < 1)
347     op->stats.dam = 1;
348     }
349     if (op->stats.sp && op->type == BOW)
350     op->stats.sp += lmt->sp;
351     if (op->stats.wc && IS_WEAPON(op))
352     op->stats.wc += lmt->wc;
353     if (IS_ARMOR(op)) {
354     if (op->stats.ac)
355     op->stats.ac += lmt->ac;
356     for (j=0; j < NROFATTACKS; j++)
357     if (op->resist[j] != 0) {
358     op->resist[j] += lmt->mod[j];
359     if (op->resist[j] > 100)
360     op->resist[j] = 100;
361     if (op->resist[j] < -100)
362     op->resist[j] = -100;
363     }
364     }
365     op->materialname = add_string(lmt->name);
366     /* dont make it unstackable if it doesn't need to be */
367     if (IS_WEAPON(op) || IS_ARMOR(op)) {
368     op->weight = (op->weight * lmt->weight)/100;
369     op->value = (op->value * lmt->value)/100;
370     }
371     #endif
372     }
373     }
374    
375     /*
376     * Strip out the media tags from a String.
377     * Warning the input string will contain the result string
378     */
379     void strip_media_tag(char *message){
380     int in_tag=0;
381     char* dest;
382     char* src;
383     src=dest=message;
384     while (*src!='\0'){
385     if (*src=='['){
386     in_tag=1;
387     } else if (in_tag && (*src==']'))
388     in_tag=0;
389     else if (!in_tag){
390     *dest=*src;
391     dest++;
392     }
393     src++;
394     }
395     *dest='\0';
396     }
397    
398     const char* strrstr(const char* haystack, const char* needle){
399     const char* lastneedle;
400     lastneedle=NULL;
401     while((haystack=strstr(haystack,needle))!=NULL){
402     lastneedle=haystack;
403     haystack++;
404     }
405     return lastneedle;
406    
407     }
408     #define EOL_SIZE (sizeof("\n")-1)
409     void strip_endline(char* buf){
410     if (strlen(buf)<sizeof("\n")){
411     return;
412     }
413     if (!strcmp(buf+strlen(buf)-EOL_SIZE,"\n"))
414     buf[strlen(buf)-EOL_SIZE]='\0';
415     }
416    
417     /**
418     * Replace in string src all occurrences of key by replacement. The resulting
419     * string is put into result; at most resultsize characters (including the
420     * terminating null character) will be written to result.
421     */
422     void replace(const char *src, const char *key, const char *replacement, char *result, size_t resultsize)
423     {
424     size_t resultlen;
425     size_t keylen;
426    
427     /* special case to prevent infinite loop if key==replacement=="" */
428     if(strcmp(key, replacement) == 0)
429     {
430     snprintf(result, resultsize, "%s", src);
431     return;
432     }
433    
434     keylen = strlen(key);
435    
436     resultlen = 0;
437     while(*src != '\0' && resultlen+1 < resultsize)
438     {
439     if(strncmp(src, key, keylen) == 0)
440     {
441     snprintf(result+resultlen, resultsize-resultlen, "%s", replacement);
442     resultlen += strlen(result+resultlen);
443     src += keylen;
444     }
445     else
446     {
447     result[resultlen++] = *src++;
448     }
449     }
450     result[resultlen] = '\0';
451     }
452    
453     /**
454     * Taking a string as an argument, mutate it into a string that looks like a list.
455     * a 'list' for the purposes here, is a string of items, seperated by commas, except
456     * for the last entry, which has an 'and' before it, and a full stop (period) after it.
457     * This function will also strip all trailing non alphanumeric characters.
458     * It does not insert an oxford comma.
459     */
460    
461     void make_list_like(char *input) {
462     char *p, tmp[MAX_BUF];
463     int i;
464     if (!input || strlen(input) > MAX_BUF-5) return;
465     /* bad stuff would happen if we continued here, the -5 is to make space for ' and ' */
466    
467     strncpy(tmp, input, MAX_BUF-5);
468     /*trim all trailing commas, spaces etc.*/
469     for (i=strlen(tmp); !isalnum(tmp[i]) && i >= 0; i--)
470     tmp[i]='\0';
471     strcat(tmp, ".");
472    
473     p=strrchr(tmp, ',');
474     if (p) {
475     *p='\0';
476     strcpy(input, tmp);
477     p++;
478     strcat(input, " and");
479     strcat(input, p);
480     }
481     else strcpy(input, tmp);
482     return;
483     }