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.9 by root, Sun Sep 10 16:00:23 2006 UTC vs.
Revision 1.25 by root, Sun Dec 17 23:10:34 2006 UTC

1
2/*
3 * static char *rcsid_utils_c =
4 * "$Id: utils.C,v 1.9 2006/09/10 16:00:23 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
206} 198}
207 199
208/* decay and destroy persihable items in a map */ 200/* decay and destroy persihable items in a map */
209 201
210void 202void
211decay_objects (mapstruct *m) 203decay_objects (maptile *m)
212{ 204{
213 int x, y, destroy; 205 int x, y, destroy;
214 object *op, *otmp; 206 object *op, *otmp;
215 207
216 if (m->unique) 208 if (m->unique)
264 if (op->material & M_ICE && MAP_TEMP (m) > 32) 256 if (op->material & M_ICE && MAP_TEMP (m) > 32)
265 destroy = 1; 257 destroy = 1;
266 } 258 }
267 /* adjust overall chance below */ 259 /* adjust overall chance below */
268 if (destroy && rndm (0, 1)) 260 if (destroy && rndm (0, 1))
269 { 261 op->destroy ();
270 remove_ob (op);
271 free_object (op);
272 }
273 } 262 }
274} 263}
275 264
276/* convert materialname to materialtype_t */ 265/* convert materialname to materialtype_t */
277 266
531 520
532 strncpy (tmp, input, MAX_BUF - 5); 521 strncpy (tmp, input, MAX_BUF - 5);
533 /*trim all trailing commas, spaces etc. */ 522 /*trim all trailing commas, spaces etc. */
534 for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--) 523 for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--)
535 tmp[i] = '\0'; 524 tmp[i] = '\0';
525
536 strcat (tmp, "."); 526 strcat (tmp, ".");
537 527
538 p = strrchr (tmp, ','); 528 p = strrchr (tmp, ',');
539 if (p) 529 if (p)
540 { 530 {
544 strcat (input, " and"); 534 strcat (input, " and");
545 strcat (input, p); 535 strcat (input, p);
546 } 536 }
547 else 537 else
548 strcpy (input, tmp); 538 strcpy (input, tmp);
539
549 return; 540 return;
550} 541}
551 542
552void * 543/////////////////////////////////////////////////////////////////////////////
553 zero_initialised::operator
554new (size_t s)
555{
556 //return calloc (1, s);
557 return g_slice_alloc0 (s);
558}
559 544
560void * 545#if 0
561 zero_initialised::operator 546refcounted *refcounted::rc_first;
562new[] (size_t s)
563{
564 //return calloc (1, s);
565 return g_slice_alloc0 (s);
566}
567 547
568void 548refcounted::refcounted ()
569 zero_initialised::operator
570delete (void *p, size_t s)
571{ 549{
572 //free (p); return; 550 refcnt = 0;
573 g_slice_free1 (s, p); 551 rc_next = rc_first;
552 rc_first = this;
574} 553}
575 554
576void 555refcounted::~refcounted ()
577 zero_initialised::operator
578delete[] (void *p, size_t s)
579{ 556{
580 //free (p); return; 557 assert (!rc_next);
581 g_slice_free1 (s, p); 558 assert (!refcnt);
582} 559}
560#endif
561
562void *salloc_ (int n) throw (std::bad_alloc)
563{
564 void *ptr = g_slice_alloc (n);
565
566 if (!ptr)
567 throw std::bad_alloc ();
568
569 return ptr;
570}
571
572void *salloc_ (int n, void *src) throw (std::bad_alloc)
573{
574 void *ptr = salloc_ (n);
575
576 if (src)
577 memcpy (ptr, src, n);
578 else
579 memset (ptr, 0, n);
580
581 return ptr;
582}
583
584void assign (char *dst, const char *src, int maxlen)
585{
586 if (!src)
587 src = "";
588
589 int len = strlen (src);
590
591 if (len >= maxlen - 1)
592 {
593 if (maxlen <= 4)
594 {
595 memset (dst, '.', maxlen - 1);
596 dst [maxlen - 1] = 0;
597 }
598 else
599 {
600 memcpy (dst, src, maxlen - 4);
601 memcpy (dst + maxlen - 4, "...", 4);
602 }
603 }
604 else
605 memcpy (dst, src, len + 1);
606}
607
608tstamp now ()
609{
610 struct timeval tv;
611
612 gettimeofday (&tv, 0);
613 return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6);
614}
615

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines