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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines