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.28 by root, Sat Dec 23 16:05:19 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)
217 return; 209 return;
218 210
219 for (x = 0; x < MAP_WIDTH (m); x++) 211 for (x = 0; x < MAP_WIDTH (m); x++)
220 for (y = 0; y < MAP_HEIGHT (m); y++) 212 for (y = 0; y < MAP_HEIGHT (m); y++)
221 for (op = get_map_ob (m, x, y); op; op = otmp) 213 for (op = GET_MAP_OB (m, x, y); op; op = otmp)
222 { 214 {
223 destroy = 0; 215 destroy = 0;
224 otmp = op->above; 216 otmp = op->above;
225 if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) 217 if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE))
226 break; 218 break;
227 if (QUERY_FLAG (op, FLAG_IS_FLOOR) || 219 if (QUERY_FLAG (op, FLAG_IS_FLOOR)
228 QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) || 220 || QUERY_FLAG (op, FLAG_OBJ_ORIGINAL)
229 QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL) || 221 || 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)) 222 || QUERY_FLAG (op, FLAG_UNIQUE)
223 || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR)
224 || QUERY_FLAG (op, FLAG_UNPAID)
225 || op->is_alive ())
231 continue; 226 continue;
227
232 /* otherwise, we decay and destroy */ 228 /* otherwise, we decay and destroy */
233 if (IS_WEAPON (op)) 229 if (op->is_weapon ())
234 { 230 {
235 op->stats.dam--; 231 op->stats.dam--;
236 if (op->stats.dam < 0) 232 if (op->stats.dam < 0)
237 destroy = 1; 233 destroy = 1;
238 } 234 }
239 else if (IS_ARMOR (op)) 235 else if (op->is_armor ())
240 { 236 {
241 op->stats.ac--; 237 op->stats.ac--;
242 if (op->stats.ac < 0) 238 if (op->stats.ac < 0)
243 destroy = 1; 239 destroy = 1;
244 } 240 }
264 if (op->material & M_ICE && MAP_TEMP (m) > 32) 260 if (op->material & M_ICE && MAP_TEMP (m) > 32)
265 destroy = 1; 261 destroy = 1;
266 } 262 }
267 /* adjust overall chance below */ 263 /* adjust overall chance below */
268 if (destroy && rndm (0, 1)) 264 if (destroy && rndm (0, 1))
269 { 265 op->destroy ();
270 remove_ob (op);
271 free_object (op);
272 }
273 } 266 }
274} 267}
275 268
276/* convert materialname to materialtype_t */ 269/* convert materialname to materialtype_t */
277 270
306 return; 299 return;
307 300
308 if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) 301 if (change->materialname != NULL && strcmp (op->materialname, change->materialname))
309 return; 302 return;
310 303
311 if (!IS_ARMOR (op)) 304 if (!op->is_armor ())
312 return; 305 return;
313 306
314 mt = name_to_material (op->materialname); 307 mt = name_to_material (op->materialname);
315 if (!mt) 308 if (!mt)
316 { 309 {
362 { 355 {
363 if (op->material & mt->material && rndm (1, 100) <= mt->chance && 356 if (op->material & mt->material && rndm (1, 100) <= mt->chance &&
364 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) 357 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
365 { 358 {
366 lmt = mt; 359 lmt = mt;
367 if (!(IS_WEAPON (op) || IS_ARMOR (op))) 360 if (!(op->is_weapon () || op->is_armor ()))
368 break; 361 break;
369 } 362 }
370 } 363 }
371#endif 364#endif
372 } 365 }
380#ifndef NEW_MATERIAL_CODE 373#ifndef NEW_MATERIAL_CODE
381 op->materialname = lmt->name; 374 op->materialname = lmt->name;
382 return; 375 return;
383#else 376#else
384 377
385 if (op->stats.dam && IS_WEAPON (op)) 378 if (op->stats.dam && op->is_weapon ())
386 { 379 {
387 op->stats.dam += lmt->damage; 380 op->stats.dam += lmt->damage;
388 if (op->stats.dam < 1) 381 if (op->stats.dam < 1)
389 op->stats.dam = 1; 382 op->stats.dam = 1;
390 } 383 }
391 if (op->stats.sp && op->type == BOW) 384 if (op->stats.sp && op->type == BOW)
392 op->stats.sp += lmt->sp; 385 op->stats.sp += lmt->sp;
393 if (op->stats.wc && IS_WEAPON (op)) 386 if (op->stats.wc && op->is_weapon ())
394 op->stats.wc += lmt->wc; 387 op->stats.wc += lmt->wc;
395 if (IS_ARMOR (op)) 388 if (op->is_armor ())
396 { 389 {
397 if (op->stats.ac) 390 if (op->stats.ac)
398 op->stats.ac += lmt->ac; 391 op->stats.ac += lmt->ac;
399 for (j = 0; j < NROFATTACKS; j++) 392 for (j = 0; j < NROFATTACKS; j++)
400 if (op->resist[j] != 0) 393 if (op->resist[j] != 0)
406 op->resist[j] = -100; 399 op->resist[j] = -100;
407 } 400 }
408 } 401 }
409 op->materialname = add_string (lmt->name); 402 op->materialname = add_string (lmt->name);
410 /* dont make it unstackable if it doesn't need to be */ 403 /* dont make it unstackable if it doesn't need to be */
411 if (IS_WEAPON (op) || IS_ARMOR (op)) 404 if (op->is_weapon () || op->is_armor ())
412 { 405 {
413 op->weight = (op->weight * lmt->weight) / 100; 406 op->weight = (op->weight * lmt->weight) / 100;
414 op->value = (op->value * lmt->value) / 100; 407 op->value = (op->value * lmt->value) / 100;
415 } 408 }
416#endif 409#endif
531 524
532 strncpy (tmp, input, MAX_BUF - 5); 525 strncpy (tmp, input, MAX_BUF - 5);
533 /*trim all trailing commas, spaces etc. */ 526 /*trim all trailing commas, spaces etc. */
534 for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--) 527 for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--)
535 tmp[i] = '\0'; 528 tmp[i] = '\0';
529
536 strcat (tmp, "."); 530 strcat (tmp, ".");
537 531
538 p = strrchr (tmp, ','); 532 p = strrchr (tmp, ',');
539 if (p) 533 if (p)
540 { 534 {
544 strcat (input, " and"); 538 strcat (input, " and");
545 strcat (input, p); 539 strcat (input, p);
546 } 540 }
547 else 541 else
548 strcpy (input, tmp); 542 strcpy (input, tmp);
543
549 return; 544 return;
550} 545}
551 546
552void * 547/////////////////////////////////////////////////////////////////////////////
553 zero_initialised::operator
554new (size_t s)
555{
556 //return calloc (1, s);
557 return g_slice_alloc0 (s);
558}
559 548
560void * 549#if 0
561 zero_initialised::operator 550refcounted *refcounted::rc_first;
562new[] (size_t s)
563{
564 //return calloc (1, s);
565 return g_slice_alloc0 (s);
566}
567 551
568void 552refcounted::refcounted ()
569 zero_initialised::operator
570delete (void *p, size_t s)
571{ 553{
572 //free (p); return; 554 refcnt = 0;
573 g_slice_free1 (s, p); 555 rc_next = rc_first;
556 rc_first = this;
574} 557}
558#endif
575 559
576void 560refcounted::~refcounted ()
577 zero_initialised::operator
578delete[] (void *p, size_t s)
579{ 561{
580 //free (p); return; 562#if 0
581 g_slice_free1 (s, p); 563 assert (!rc_next);
564 assert (!refcnt);
565#endif
582} 566}
567
568void *salloc_ (int n) throw (std::bad_alloc)
569{
570 void *ptr = g_slice_alloc (n);
571
572 if (!ptr)
573 throw std::bad_alloc ();
574
575 return ptr;
576}
577
578void *salloc_ (int n, void *src) throw (std::bad_alloc)
579{
580 void *ptr = salloc_ (n);
581
582 if (src)
583 memcpy (ptr, src, n);
584 else
585 memset (ptr, 0, n);
586
587 return ptr;
588}
589
590void assign (char *dst, const char *src, int maxlen)
591{
592 if (!src)
593 src = "";
594
595 int len = strlen (src);
596
597 if (len >= maxlen - 1)
598 {
599 if (maxlen <= 4)
600 {
601 memset (dst, '.', maxlen - 1);
602 dst [maxlen - 1] = 0;
603 }
604 else
605 {
606 memcpy (dst, src, maxlen - 4);
607 memcpy (dst + maxlen - 4, "...", 4);
608 }
609 }
610 else
611 memcpy (dst, src, len + 1);
612}
613
614tstamp now ()
615{
616 struct timeval tv;
617
618 gettimeofday (&tv, 0);
619 return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6);
620}
621

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines