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.11 by root, Tue Sep 12 19:20:06 2006 UTC vs.
Revision 1.32 by root, Sat Dec 30 10:16:10 2006 UTC

1
2/*
3 * static char *rcsid_utils_c =
4 * "$Id: utils.C,v 1.11 2006/09/12 19:20:06 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
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 persihable 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
549 strcpy (input, tmp); 546 strcpy (input, tmp);
550 547
551 return; 548 return;
552} 549}
553 550
554void * 551/////////////////////////////////////////////////////////////////////////////
555zero_initialised::operator new (size_t s, void *p) 552
553void *salloc_ (int n) throw (std::bad_alloc)
556{ 554{
557 memset (p, 0, s); 555 void *ptr = g_slice_alloc (n);
556
557 if (!ptr)
558 throw std::bad_alloc ();
559
558 return p; 560 return ptr;
559} 561}
560 562
561void * 563void *salloc_ (int n, void *src) throw (std::bad_alloc)
562zero_initialised::operator new (size_t s)
563{ 564{
564 //return calloc (1, s); 565 void *ptr = salloc_ (n);
565 return g_slice_alloc0 (s);
566}
567 566
568void * 567 if (src)
569 zero_initialised::operator new[] (size_t s) 568 memcpy (ptr, src, n);
570{ 569 else
571 //return calloc (1, s); 570 memset (ptr, 0, n);
572 return g_slice_alloc0 (s);
573}
574 571
575void 572 return ptr;
576zero_initialised::operator delete (void *p, size_t s)
577{
578 //free (p); return;
579 g_slice_free1 (s, p);
580}
581
582void
583zero_initialised::operator delete[] (void *p, size_t s)
584{
585 //free (p); return;
586 g_slice_free1 (s, p);
587} 573}
588 574
589void assign (char *dst, const char *src, int maxlen) 575void assign (char *dst, const char *src, int maxlen)
590{ 576{
591 if (!src) 577 if (!src)
608 } 594 }
609 else 595 else
610 memcpy (dst, src, len + 1); 596 memcpy (dst, src, len + 1);
611} 597}
612 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