ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/utils.C
(Generate patch)

Comparing deliantra/server/common/utils.C (file contents):
Revision 1.1 by elmex, Sun Aug 13 17:16:01 2006 UTC vs.
Revision 1.2 by root, Tue Aug 29 08:01:36 2006 UTC

1/* 1/*
2 * static char *rcsid_utils_c = 2 * static char *rcsid_utils_c =
3 * "$Id: utils.C,v 1.1 2006/08/13 17:16:01 elmex Exp $"; 3 * "$Id: utils.C,v 1.2 2006/08/29 08:01:36 root Exp $";
4 */ 4 */
5 5
6/* 6/*
7 CrossFire, A Multiplayer game for X-windows 7 CrossFire, A Multiplayer game for X-windows
8 8
63 } 63 }
64 64
65 ran = RANDOM(); 65 ran = RANDOM();
66 66
67 if (op->type != PLAYER) 67 if (op->type != PLAYER)
68 return((ran%diff)+min); 68 return((ran%diff)+min);
69 69
70 luck = op->stats.luck; 70 luck = op->stats.luck;
71 if (RANDOM()%base < MIN(10, abs(luck))) { 71 if (RANDOM()%base < MIN(10, abs(luck))) {
72 /* we have a winner */ 72 /* we have a winner */
73 ((luck > 0) ? (luck = 1) : (luck = -1)); 73 ((luck > 0) ? (luck = 1) : (luck = -1));
74 diff -= luck; 74 diff -= luck;
75 if (diff < 1) 75 if (diff < 1)
76 return(omin); /*check again*/ 76 return(omin); /*check again*/
77 ((goodbad) ? (min += luck) : (diff)); 77 ((goodbad) ? (min += luck) : (diff));
78 78
79 return(MAX(omin, MIN(max, (ran%diff)+min))); 79 return(MAX(omin, MIN(max, (ran%diff)+min)));
80 } 80 }
81 return((ran%diff)+min); 81 return((ran%diff)+min);
82} 82}
83 83
84/* 84/*
109 * 32 bit, so we don't get skewed results 109 * 32 bit, so we don't get skewed results
110 */ 110 */
111 ran = (RANDOM() & 0xffffffff) | ((sint64)RANDOM() << 32); 111 ran = (RANDOM() & 0xffffffff) | ((sint64)RANDOM() << 32);
112 112
113 if (op->type != PLAYER) 113 if (op->type != PLAYER)
114 return((ran%diff)+min); 114 return((ran%diff)+min);
115 115
116 luck = op->stats.luck; 116 luck = op->stats.luck;
117 if (RANDOM()%base < MIN(10, abs(luck))) { 117 if (RANDOM()%base < MIN(10, abs(luck))) {
118 /* we have a winner */ 118 /* we have a winner */
119 ((luck > 0) ? (luck = 1) : (luck = -1)); 119 ((luck > 0) ? (luck = 1) : (luck = -1));
120 diff -= luck; 120 diff -= luck;
121 if (diff < 1) 121 if (diff < 1)
122 return(omin); /*check again*/ 122 return(omin); /*check again*/
123 ((goodbad) ? (min += luck) : (diff)); 123 ((goodbad) ? (min += luck) : (diff));
124 124
125 return(MAX(omin, MIN(max, (ran%diff)+min))); 125 return(MAX(omin, MIN(max, (ran%diff)+min)));
126 } 126 }
127 return((ran%diff)+min); 127 return((ran%diff)+min);
128} 128}
129 129
130/* 130/*
146 LOG(llevError, "Calling die_roll with num=%d size=%d\n", num, size); 146 LOG(llevError, "Calling die_roll with num=%d size=%d\n", num, size);
147 return(num); /* avoids a float exception */ 147 return(num); /* avoids a float exception */
148 } 148 }
149 149
150 if (op->type == PLAYER) 150 if (op->type == PLAYER)
151 luck = op->stats.luck; 151 luck = op->stats.luck;
152 152
153 for (i = 0; i < num; i++) { 153 for (i = 0; i < num; i++) {
154 if (RANDOM()%base < MIN(10, abs(luck)) && !gotlucky) { 154 if (RANDOM()%base < MIN(10, abs(luck)) && !gotlucky) {
155 /* we have a winner */ 155 /* we have a winner */
156 gotlucky++; 156 gotlucky++;
157 ((luck > 0) ? (luck = 1) : (luck = -1)); 157 ((luck > 0) ? (luck = 1) : (luck = -1));
158 diff -= luck; 158 diff -= luck;
159 if (diff < 1) 159 if (diff < 1)
160 return(num); /*check again*/ 160 return(num); /*check again*/
161 ((goodbad) ? (min += luck) : (diff)); 161 ((goodbad) ? (min += luck) : (diff));
162 ran = RANDOM(); 162 ran = RANDOM();
163 total += MAX(1, MIN(size, (ran%diff)+min)); 163 total += MAX(1, MIN(size, (ran%diff)+min));
164 } else { 164 } else {
165 total += RANDOM()%size+1; 165 total += RANDOM()%size+1;
166 } 166 }
167 } 167 }
168 return(total); 168 return(total);
169} 169}
170 170
171/* 171/*
192{ 192{
193 int x, y, destroy; 193 int x, y, destroy;
194 object *op, *otmp; 194 object *op, *otmp;
195 195
196 if (m->unique) 196 if (m->unique)
197 return; 197 return;
198 198
199 for (x=0; x < MAP_WIDTH(m); x++) 199 for (x=0; x < MAP_WIDTH(m); x++)
200 for (y=0; y < MAP_HEIGHT(m); y++) 200 for (y=0; y < MAP_HEIGHT(m); y++)
201 for (op = get_map_ob(m, x, y); op; op = otmp) { 201 for (op = get_map_ob(m, x, y); op; op = otmp) {
202 destroy = 0; 202 destroy = 0;
203 otmp = op->above; 203 otmp = op->above;
204 if (QUERY_FLAG(op,FLAG_IS_FLOOR) && QUERY_FLAG(op, FLAG_UNIQUE)) 204 if (QUERY_FLAG(op,FLAG_IS_FLOOR) && QUERY_FLAG(op, FLAG_UNIQUE))
205 break; 205 break;
206 if (QUERY_FLAG(op, FLAG_IS_FLOOR) || 206 if (QUERY_FLAG(op, FLAG_IS_FLOOR) ||
207 QUERY_FLAG(op, FLAG_OBJ_ORIGINAL) || 207 QUERY_FLAG(op, FLAG_OBJ_ORIGINAL) ||
208 QUERY_FLAG(op, FLAG_OBJ_SAVE_ON_OVL) || 208 QUERY_FLAG(op, FLAG_OBJ_SAVE_ON_OVL) ||
209 QUERY_FLAG(op, FLAG_UNIQUE) || 209 QUERY_FLAG(op, FLAG_UNIQUE) ||
210 QUERY_FLAG(op, FLAG_OVERLAY_FLOOR) || 210 QUERY_FLAG(op, FLAG_OVERLAY_FLOOR) ||
211 QUERY_FLAG(op, FLAG_UNPAID) || IS_LIVE(op)) 211 QUERY_FLAG(op, FLAG_UNPAID) || IS_LIVE(op))
212 continue; 212 continue;
213 /* otherwise, we decay and destroy */ 213 /* otherwise, we decay and destroy */
214 if (IS_WEAPON(op)) { 214 if (IS_WEAPON(op)) {
215 op->stats.dam--; 215 op->stats.dam--;
216 if (op->stats.dam < 0) 216 if (op->stats.dam < 0)
217 destroy = 1; 217 destroy = 1;
218 } else if (IS_ARMOR(op)) { 218 } else if (IS_ARMOR(op)) {
219 op->stats.ac--; 219 op->stats.ac--;
220 if (op->stats.ac < 0) 220 if (op->stats.ac < 0)
221 destroy = 1; 221 destroy = 1;
222 } else if (op->type == FOOD) { 222 } else if (op->type == FOOD) {
223 op->stats.food -= rndm(5,20); 223 op->stats.food -= rndm(5,20);
224 if (op->stats.food < 0) 224 if (op->stats.food < 0)
225 destroy = 1; 225 destroy = 1;
226 } else { 226 } else {
227 if (op->material & M_PAPER || op->material & M_LEATHER || 227 if (op->material & M_PAPER || op->material & M_LEATHER ||
228 op->material & M_WOOD || op->material & M_ORGANIC || 228 op->material & M_WOOD || op->material & M_ORGANIC ||
229 op->material & M_CLOTH || op->material & M_LIQUID) 229 op->material & M_CLOTH || op->material & M_LIQUID)
230 destroy = 1; 230 destroy = 1;
231 if (op->material & M_IRON && rndm(1,5) == 1) 231 if (op->material & M_IRON && rndm(1,5) == 1)
232 destroy = 1; 232 destroy = 1;
233 if (op->material & M_GLASS && rndm(1,2) == 1) 233 if (op->material & M_GLASS && rndm(1,2) == 1)
234 destroy = 1; 234 destroy = 1;
235 if ((op->material & M_STONE || op->material & M_ADAMANT) && 235 if ((op->material & M_STONE || op->material & M_ADAMANT) &&
236 rndm(1,10) == 1) 236 rndm(1,10) == 1)
237 destroy = 1; 237 destroy = 1;
238 if ((op->material & M_SOFT_METAL || op->material & M_BONE) && 238 if ((op->material & M_SOFT_METAL || op->material & M_BONE) &&
239 rndm(1,3) == 1) 239 rndm(1,3) == 1)
240 destroy = 1; 240 destroy = 1;
241 if (op->material & M_ICE && MAP_TEMP(m) > 32) 241 if (op->material & M_ICE && MAP_TEMP(m) > 32)
242 destroy = 1; 242 destroy = 1;
243 } 243 }
244 /* adjust overall chance below */ 244 /* adjust overall chance below */
245 if (destroy && rndm(0, 1)) { 245 if (destroy && rndm(0, 1)) {
246 remove_ob(op); 246 remove_ob(op);
247 free_object(op); 247 free_object(op);
248 } 248 }
249 } 249 }
250} 250}
251 251
252/* convert materialname to materialtype_t */ 252/* convert materialname to materialtype_t */
253 253
254materialtype_t *name_to_material(const char *name) 254materialtype_t *name_to_material(const char *name)
255{ 255{
256 materialtype_t *mt, *nmt; 256 materialtype_t *mt, *nmt;
257 257
258 mt = NULL; 258 mt = NULL;
259 for (nmt = materialt; nmt != NULL && nmt->next != NULL; nmt=nmt->next) { 259 for (nmt = materialt; nmt != NULL && nmt->next != NULL; nmt=nmt->next) {
260 if (strcmp(name, nmt->name) == 0) { 260 if (strcmp(name, nmt->name) == 0) {
261 mt = nmt; 261 mt = nmt;
262 break; 262 break;
263 } 263 }
264 } 264 }
265 return mt; 265 return mt;
266} 266}
267 267
268/* when doing transmutation of objects, we have to recheck the resistances, 268/* when doing transmutation of objects, we have to recheck the resistances,
273{ 273{
274 materialtype_t *mt; 274 materialtype_t *mt;
275 int j; 275 int j;
276 276
277 if (op->materialname == NULL) 277 if (op->materialname == NULL)
278 return; 278 return;
279 279
280 if (change->materialname != NULL && 280 if (change->materialname != NULL &&
281 strcmp(op->materialname, change->materialname)) 281 strcmp(op->materialname, change->materialname))
282 return; 282 return;
283 283
284 if (!IS_ARMOR(op)) 284 if (!IS_ARMOR(op))
285 return; 285 return;
286 286
287 mt = name_to_material(op->materialname); 287 mt = name_to_material(op->materialname);
288 if (!mt) { 288 if (!mt) {
289 LOG(llevError, "archetype '%s>%s' uses nonexistent material '%s'\n", op->arch->name, op->name, op->materialname); 289 LOG(llevError, "archetype '%s>%s' uses nonexistent material '%s'\n", op->arch->name, op->name, op->materialname);
290 return; 290 return;
291 } 291 }
292 292
293 for (j=0; j < NROFATTACKS; j++) 293 for (j=0; j < NROFATTACKS; j++)
294 if (op->resist[j] == 0 && change->resist[j] != 0) { 294 if (op->resist[j] == 0 && change->resist[j] != 0) {
295 op->resist[j] += mt->mod[j]; 295 op->resist[j] += mt->mod[j];
296 if (op->resist[j] > 100) 296 if (op->resist[j] > 100)
297 op->resist[j] = 100; 297 op->resist[j] = 100;
298 if (op->resist[j] < -100) 298 if (op->resist[j] < -100)
299 op->resist[j] = -100; 299 op->resist[j] = -100;
300 } 300 }
301} 301}
302 302
303/* set the materialname and type for an item */ 303/* set the materialname and type for an item */
304void set_materialname(object *op, int difficulty, materialtype_t *nmt) 304void set_materialname(object *op, int difficulty, materialtype_t *nmt)
305{ 305{
306 materialtype_t *mt, *lmt; 306 materialtype_t *mt, *lmt;
307 int j; 307 int j;
308 308
309 if (op->materialname != NULL) 309 if (op->materialname != NULL)
310 return; 310 return;
311 311
312 312
313 313
314 if (nmt == NULL) { 314 if (nmt == NULL) {
315 lmt = NULL; 315 lmt = NULL;
316#ifndef NEW_MATERIAL_CODE 316#ifndef NEW_MATERIAL_CODE
317 for (mt = materialt; mt != NULL && mt->next != NULL; mt=mt->next) { 317 for (mt = materialt; mt != NULL && mt->next != NULL; mt=mt->next) {
318 if (op->material & mt->material) { 318 if (op->material & mt->material) {
319 lmt = mt; 319 lmt = mt;
320 break; 320 break;
321 } 321 }
322 } 322 }
323 323
324#else 324#else
325 for (mt = materialt; mt != NULL && mt->next != NULL; mt=mt->next) { 325 for (mt = materialt; mt != NULL && mt->next != NULL; mt=mt->next) {
326 if (op->material & mt->material && rndm(1, 100) <= mt->chance && 326 if (op->material & mt->material && rndm(1, 100) <= mt->chance &&
327 difficulty >= mt->difficulty && 327 difficulty >= mt->difficulty &&
328 (op->magic >= mt->magic || mt->magic == 0)) { 328 (op->magic >= mt->magic || mt->magic == 0)) {
329 lmt = mt; 329 lmt = mt;
330 if (!(IS_WEAPON(op) || IS_ARMOR(op))) 330 if (!(IS_WEAPON(op) || IS_ARMOR(op)))
331 break; 331 break;
332 } 332 }
333 } 333 }
334#endif 334#endif
335 } else { 335 } else {
336 lmt = nmt; 336 lmt = nmt;
337 } 337 }
338 338
339 if (lmt != NULL) { 339 if (lmt != NULL) {
340#ifndef NEW_MATERIAL_CODE 340#ifndef NEW_MATERIAL_CODE
341 op->materialname = add_string(lmt->name); 341 op->materialname = add_string(lmt->name);
342 return; 342 return;
343#else 343#else
344 344
345 if (op->stats.dam && IS_WEAPON(op)) { 345 if (op->stats.dam && IS_WEAPON(op)) {
346 op->stats.dam += lmt->damage; 346 op->stats.dam += lmt->damage;
347 if (op->stats.dam < 1) 347 if (op->stats.dam < 1)
348 op->stats.dam = 1; 348 op->stats.dam = 1;
349 } 349 }
350 if (op->stats.sp && op->type == BOW) 350 if (op->stats.sp && op->type == BOW)
351 op->stats.sp += lmt->sp; 351 op->stats.sp += lmt->sp;
352 if (op->stats.wc && IS_WEAPON(op)) 352 if (op->stats.wc && IS_WEAPON(op))
353 op->stats.wc += lmt->wc; 353 op->stats.wc += lmt->wc;
354 if (IS_ARMOR(op)) { 354 if (IS_ARMOR(op)) {
355 if (op->stats.ac) 355 if (op->stats.ac)
356 op->stats.ac += lmt->ac; 356 op->stats.ac += lmt->ac;
357 for (j=0; j < NROFATTACKS; j++) 357 for (j=0; j < NROFATTACKS; j++)
358 if (op->resist[j] != 0) { 358 if (op->resist[j] != 0) {
359 op->resist[j] += lmt->mod[j]; 359 op->resist[j] += lmt->mod[j];
360 if (op->resist[j] > 100) 360 if (op->resist[j] > 100)
361 op->resist[j] = 100; 361 op->resist[j] = 100;
362 if (op->resist[j] < -100) 362 if (op->resist[j] < -100)
363 op->resist[j] = -100; 363 op->resist[j] = -100;
364 } 364 }
365 } 365 }
366 op->materialname = add_string(lmt->name); 366 op->materialname = add_string(lmt->name);
367 /* dont make it unstackable if it doesn't need to be */ 367 /* dont make it unstackable if it doesn't need to be */
368 if (IS_WEAPON(op) || IS_ARMOR(op)) { 368 if (IS_WEAPON(op) || IS_ARMOR(op)) {
369 op->weight = (op->weight * lmt->weight)/100; 369 op->weight = (op->weight * lmt->weight)/100;
370 op->value = (op->value * lmt->value)/100; 370 op->value = (op->value * lmt->value)/100;
371 } 371 }
372#endif 372#endif
373 } 373 }
374} 374}
375 375
376/* 376/*
420 * string is put into result; at most resultsize characters (including the 420 * string is put into result; at most resultsize characters (including the
421 * terminating null character) will be written to result. 421 * terminating null character) will be written to result.
422 */ 422 */
423void replace(const char *src, const char *key, const char *replacement, char *result, size_t resultsize) 423void replace(const char *src, const char *key, const char *replacement, char *result, size_t resultsize)
424{ 424{
425 size_t resultlen; 425 size_t resultlen;
426 size_t keylen; 426 size_t keylen;
427 427
428 /* special case to prevent infinite loop if key==replacement=="" */ 428 /* special case to prevent infinite loop if key==replacement=="" */
429 if(strcmp(key, replacement) == 0) 429 if(strcmp(key, replacement) == 0)
430 { 430 {
431 snprintf(result, resultsize, "%s", src); 431 snprintf(result, resultsize, "%s", src);
432 return; 432 return;
433 } 433 }
434 434
435 keylen = strlen(key); 435 keylen = strlen(key);
436 436
437 resultlen = 0; 437 resultlen = 0;
438 while(*src != '\0' && resultlen+1 < resultsize) 438 while(*src != '\0' && resultlen+1 < resultsize)
439 { 439 {
440 if(strncmp(src, key, keylen) == 0) 440 if(strncmp(src, key, keylen) == 0)
441 { 441 {
442 snprintf(result+resultlen, resultsize-resultlen, "%s", replacement); 442 snprintf(result+resultlen, resultsize-resultlen, "%s", replacement);
443 resultlen += strlen(result+resultlen); 443 resultlen += strlen(result+resultlen);
444 src += keylen; 444 src += keylen;
445 } 445 }
446 else 446 else
447 { 447 {
448 result[resultlen++] = *src++; 448 result[resultlen++] = *src++;
449 } 449 }
450 } 450 }
451 result[resultlen] = '\0'; 451 result[resultlen] = '\0';
452} 452}
453 453
454/** 454/**
455 * Taking a string as an argument, mutate it into a string that looks like a list. 455 * Taking a string as an argument, mutate it into a string that looks like a list.
456 * a 'list' for the purposes here, is a string of items, seperated by commas, except 456 * a 'list' for the purposes here, is a string of items, seperated by commas, except
466 /* bad stuff would happen if we continued here, the -5 is to make space for ' and ' */ 466 /* bad stuff would happen if we continued here, the -5 is to make space for ' and ' */
467 467
468 strncpy(tmp, input, MAX_BUF-5); 468 strncpy(tmp, input, MAX_BUF-5);
469 /*trim all trailing commas, spaces etc.*/ 469 /*trim all trailing commas, spaces etc.*/
470 for (i=strlen(tmp); !isalnum(tmp[i]) && i >= 0; i--) 470 for (i=strlen(tmp); !isalnum(tmp[i]) && i >= 0; i--)
471 tmp[i]='\0'; 471 tmp[i]='\0';
472 strcat(tmp, "."); 472 strcat(tmp, ".");
473 473
474 p=strrchr(tmp, ','); 474 p=strrchr(tmp, ',');
475 if (p) { 475 if (p) {
476 *p='\0'; 476 *p='\0';
477 strcpy(input, tmp); 477 strcpy(input, tmp);
478 p++; 478 p++;
479 strcat(input, " and"); 479 strcat(input, " and");
480 strcat(input, p); 480 strcat(input, p);
481 } 481 }
482 else strcpy(input, tmp); 482 else strcpy(input, tmp);
483 return; 483 return;
484} 484}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines