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.10 by root, Mon Sep 11 01:16:20 2006 UTC vs.
Revision 1.33 by root, Sun Dec 31 18:10:40 2006 UTC

1
2/*
3 * static char *rcsid_utils_c =
4 * "$Id: utils.C,v 1.10 2006/09/11 01:16:20 root Exp $";
5 */
6
7/* 1/*
8 CrossFire, A Multiplayer game for X-windows 2 CrossFire, A Multiplayer game for X-windows
9 3
10 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team
11 Copyright (C) 1992 Frank Tore Johansen 5 Copyright (C) 1992 Frank Tore Johansen
22 16
23 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software 18 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 20
27 The authors can be reached via e-mail at crossfire-devel@real-time.com 21 The authors can be reached via e-mail at <crossfire@schmorp.de>
28*/ 22*/
29 23
30/* 24/*
31 * General convenience functions for crossfire. 25 * General convenience functions for crossfire.
32 */ 26 */
33 27
34#include <global.h> 28#include <global.h>
35#include <funcpoint.h> 29#include <funcpoint.h>
36#include <material.h> 30#include <material.h>
37 31
32#include <sys/time.h>
33#include <time.h>
38#include <glib.h> 34#include <glib.h>
39 35
40/* 36/*
41 * The random functions here take luck into account when rolling random 37 * The random functions here take luck into account when rolling random
42 * dice or numbers. This function has less of an impact the larger the 38 * dice or numbers. This function has less of an impact the larger the
103 diff = max - min + 1; 99 diff = max - min + 1;
104 ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ 100 ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */
105 101
106 if (max < 1 || diff < 1) 102 if (max < 1 || diff < 1)
107 { 103 {
108#ifndef WIN32
109 LOG (llevError, "Calling random_roll with min=%lld max=%lld\n", min, max);
110#else
111 LOG (llevError, "Calling random_roll with min=%I64d max=%I64d\n", min, max); 104 LOG (llevError, "Calling random_roll with min=%" PRId64 " max=%" PRId64 "\n", min, max);
112#endif
113 return (min); /* avoids a float exception */ 105 return (min); /* avoids a float exception */
114 } 106 }
115 107
116 /* Don't know of a portable call to get 64 bit random values. 108 /* Don't know of a portable call to get 64 bit random values.
117 * So make a call to get two 32 bit random numbers, and just to 109 * So make a call to get two 32 bit random numbers, and just to
203 return (min); 195 return (min);
204 196
205 return (RANDOM () % diff + min); 197 return (RANDOM () % diff + min);
206} 198}
207 199
208/* decay and destroy persihable items in a map */ 200/* decay and destroy perishable items in a map */
209
210void 201void
211decay_objects (mapstruct *m) 202maptile::decay_objects ()
212{ 203{
213 int x, y, destroy; 204 int x, y, destroy;
214 object *op, *otmp; 205 object *op, *otmp;
215 206
216 if (m->unique) 207 for (x = 0; x < width; x++)
217 return; 208 for (y = 0; y < height; y++)
218
219 for (x = 0; x < MAP_WIDTH (m); x++)
220 for (y = 0; y < MAP_HEIGHT (m); y++)
221 for (op = get_map_ob (m, x, y); op; op = otmp) 209 for (op = at (x, y).bot; op; op = otmp)
222 { 210 {
223 destroy = 0; 211 destroy = 0;
224 otmp = op->above; 212 otmp = op->above;
213
225 if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) 214 if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE))
226 break; 215 break;
216
227 if (QUERY_FLAG (op, FLAG_IS_FLOOR) || 217 if (QUERY_FLAG (op, FLAG_IS_FLOOR)
228 QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) || 218 || QUERY_FLAG (op, FLAG_OBJ_ORIGINAL)
229 QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL) || 219 || QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL)
230 QUERY_FLAG (op, FLAG_UNIQUE) || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR) || QUERY_FLAG (op, FLAG_UNPAID) || IS_LIVE (op)) 220 || QUERY_FLAG (op, FLAG_UNIQUE)
221 || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR)
222 || QUERY_FLAG (op, FLAG_UNPAID)
223 || op->is_alive ())
231 continue; 224 continue;
225
232 /* otherwise, we decay and destroy */ 226 /* otherwise, we decay and destroy */
233 if (IS_WEAPON (op)) 227 if (op->is_weapon ())
234 { 228 {
235 op->stats.dam--; 229 op->stats.dam--;
236 if (op->stats.dam < 0) 230 if (op->stats.dam < 0)
237 destroy = 1; 231 destroy = 1;
238 } 232 }
239 else if (IS_ARMOR (op)) 233 else if (op->is_armor ())
240 { 234 {
241 op->stats.ac--; 235 op->stats.ac--;
242 if (op->stats.ac < 0) 236 if (op->stats.ac < 0)
243 destroy = 1; 237 destroy = 1;
244 } 238 }
251 else 245 else
252 { 246 {
253 if (op->material & M_PAPER || op->material & M_LEATHER || 247 if (op->material & M_PAPER || op->material & M_LEATHER ||
254 op->material & M_WOOD || op->material & M_ORGANIC || op->material & M_CLOTH || op->material & M_LIQUID) 248 op->material & M_WOOD || op->material & M_ORGANIC || op->material & M_CLOTH || op->material & M_LIQUID)
255 destroy = 1; 249 destroy = 1;
250
256 if (op->material & M_IRON && rndm (1, 5) == 1) 251 if (op->material & M_IRON && rndm (1, 5) == 1)
257 destroy = 1; 252 destroy = 1;
253
258 if (op->material & M_GLASS && rndm (1, 2) == 1) 254 if (op->material & M_GLASS && rndm (1, 2) == 1)
259 destroy = 1; 255 destroy = 1;
256
260 if ((op->material & M_STONE || op->material & M_ADAMANT) && rndm (1, 10) == 1) 257 if ((op->material & M_STONE || op->material & M_ADAMANT) && rndm (1, 10) == 1)
261 destroy = 1; 258 destroy = 1;
259
262 if ((op->material & M_SOFT_METAL || op->material & M_BONE) && rndm (1, 3) == 1) 260 if ((op->material & M_SOFT_METAL || op->material & M_BONE) && rndm (1, 3) == 1)
263 destroy = 1; 261 destroy = 1;
262
264 if (op->material & M_ICE && MAP_TEMP (m) > 32) 263 if (op->material & M_ICE && temp > 32)
265 destroy = 1; 264 destroy = 1;
266 } 265 }
266
267 /* adjust overall chance below */ 267 /* adjust overall chance below */
268 if (destroy && rndm (0, 1)) 268 if (destroy && rndm (0, 1))
269 { 269 op->destroy ();
270 remove_ob (op);
271 free_object (op);
272 }
273 } 270 }
274} 271}
275 272
276/* convert materialname to materialtype_t */ 273/* convert materialname to materialtype_t */
277 274
306 return; 303 return;
307 304
308 if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) 305 if (change->materialname != NULL && strcmp (op->materialname, change->materialname))
309 return; 306 return;
310 307
311 if (!IS_ARMOR (op)) 308 if (!op->is_armor ())
312 return; 309 return;
313 310
314 mt = name_to_material (op->materialname); 311 mt = name_to_material (op->materialname);
315 if (!mt) 312 if (!mt)
316 { 313 {
362 { 359 {
363 if (op->material & mt->material && rndm (1, 100) <= mt->chance && 360 if (op->material & mt->material && rndm (1, 100) <= mt->chance &&
364 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) 361 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
365 { 362 {
366 lmt = mt; 363 lmt = mt;
367 if (!(IS_WEAPON (op) || IS_ARMOR (op))) 364 if (!(op->is_weapon () || op->is_armor ()))
368 break; 365 break;
369 } 366 }
370 } 367 }
371#endif 368#endif
372 } 369 }
380#ifndef NEW_MATERIAL_CODE 377#ifndef NEW_MATERIAL_CODE
381 op->materialname = lmt->name; 378 op->materialname = lmt->name;
382 return; 379 return;
383#else 380#else
384 381
385 if (op->stats.dam && IS_WEAPON (op)) 382 if (op->stats.dam && op->is_weapon ())
386 { 383 {
387 op->stats.dam += lmt->damage; 384 op->stats.dam += lmt->damage;
388 if (op->stats.dam < 1) 385 if (op->stats.dam < 1)
389 op->stats.dam = 1; 386 op->stats.dam = 1;
390 } 387 }
391 if (op->stats.sp && op->type == BOW) 388 if (op->stats.sp && op->type == BOW)
392 op->stats.sp += lmt->sp; 389 op->stats.sp += lmt->sp;
393 if (op->stats.wc && IS_WEAPON (op)) 390 if (op->stats.wc && op->is_weapon ())
394 op->stats.wc += lmt->wc; 391 op->stats.wc += lmt->wc;
395 if (IS_ARMOR (op)) 392 if (op->is_armor ())
396 { 393 {
397 if (op->stats.ac) 394 if (op->stats.ac)
398 op->stats.ac += lmt->ac; 395 op->stats.ac += lmt->ac;
399 for (j = 0; j < NROFATTACKS; j++) 396 for (j = 0; j < NROFATTACKS; j++)
400 if (op->resist[j] != 0) 397 if (op->resist[j] != 0)
406 op->resist[j] = -100; 403 op->resist[j] = -100;
407 } 404 }
408 } 405 }
409 op->materialname = add_string (lmt->name); 406 op->materialname = add_string (lmt->name);
410 /* dont make it unstackable if it doesn't need to be */ 407 /* dont make it unstackable if it doesn't need to be */
411 if (IS_WEAPON (op) || IS_ARMOR (op)) 408 if (op->is_weapon () || op->is_armor ())
412 { 409 {
413 op->weight = (op->weight * lmt->weight) / 100; 410 op->weight = (op->weight * lmt->weight) / 100;
414 op->value = (op->value * lmt->value) / 100; 411 op->value = (op->value * lmt->value) / 100;
415 } 412 }
416#endif 413#endif
531 528
532 strncpy (tmp, input, MAX_BUF - 5); 529 strncpy (tmp, input, MAX_BUF - 5);
533 /*trim all trailing commas, spaces etc. */ 530 /*trim all trailing commas, spaces etc. */
534 for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--) 531 for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--)
535 tmp[i] = '\0'; 532 tmp[i] = '\0';
533
536 strcat (tmp, "."); 534 strcat (tmp, ".");
537 535
538 p = strrchr (tmp, ','); 536 p = strrchr (tmp, ',');
539 if (p) 537 if (p)
540 { 538 {
544 strcat (input, " and"); 542 strcat (input, " and");
545 strcat (input, p); 543 strcat (input, p);
546 } 544 }
547 else 545 else
548 strcpy (input, tmp); 546 strcpy (input, tmp);
547
549 return; 548 return;
550} 549}
551 550
552void * 551/////////////////////////////////////////////////////////////////////////////
553zero_initialised::operator new (size_t s, void *p) 552
553void *salloc_ (int n) throw (std::bad_alloc)
554{ 554{
555 memset (p, 0, s); 555 void *ptr = g_slice_alloc (n);
556
557 if (!ptr)
558 throw std::bad_alloc ();
559
556 return p; 560 return ptr;
557} 561}
558 562
559void * 563void *salloc_ (int n, void *src) throw (std::bad_alloc)
560zero_initialised::operator new (size_t s)
561{ 564{
562 //return calloc (1, s); 565 void *ptr = salloc_ (n);
563 return g_slice_alloc0 (s);
564}
565 566
566void * 567 if (src)
567 zero_initialised::operator new[] (size_t s) 568 memcpy (ptr, src, n);
568{ 569 else
569 //return calloc (1, s); 570 memset (ptr, 0, n);
570 return g_slice_alloc0 (s);
571}
572 571
573void 572 return ptr;
574zero_initialised::operator delete (void *p, size_t s)
575{
576 //free (p); return;
577 g_slice_free1 (s, p);
578} 573}
579 574
580void 575void assign (char *dst, const char *src, int maxlen)
581zero_initialised::operator delete[] (void *p, size_t s)
582{ 576{
583 //free (p); return; 577 if (!src)
584 g_slice_free1 (s, p); 578 src = "";
579
580 int len = strlen (src);
581
582 if (len >= maxlen - 1)
583 {
584 if (maxlen <= 4)
585 {
586 memset (dst, '.', maxlen - 1);
587 dst [maxlen - 1] = 0;
588 }
589 else
590 {
591 memcpy (dst, src, maxlen - 4);
592 memcpy (dst + maxlen - 4, "...", 4);
593 }
594 }
595 else
596 memcpy (dst, src, len + 1);
585} 597}
598
599tstamp now ()
600{
601 struct timeval tv;
602
603 gettimeofday (&tv, 0);
604 return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6);
605}
606
607int
608similar_direction (int a, int b)
609{
610 if (!a || !b)
611 return 0;
612
613 int diff = (b - a) & 7;
614 return diff <= 1 || diff >= 7;
615}
616

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines