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.17 by root, Fri Nov 17 19:40:53 2006 UTC vs.
Revision 1.33 by root, Sun Dec 31 18:10:40 2006 UTC

27 27
28#include <global.h> 28#include <global.h>
29#include <funcpoint.h> 29#include <funcpoint.h>
30#include <material.h> 30#include <material.h>
31 31
32#include <sys/time.h>
33#include <time.h>
32#include <glib.h> 34#include <glib.h>
33 35
34/* 36/*
35 * The random functions here take luck into account when rolling random 37 * The random functions here take luck into account when rolling random
36 * 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
97 diff = max - min + 1; 99 diff = max - min + 1;
98 ((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 */
99 101
100 if (max < 1 || diff < 1) 102 if (max < 1 || diff < 1)
101 { 103 {
102#ifndef WIN32
103 LOG (llevError, "Calling random_roll with min=%lld max=%lld\n", min, max);
104#else
105 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);
106#endif
107 return (min); /* avoids a float exception */ 105 return (min); /* avoids a float exception */
108 } 106 }
109 107
110 /* 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.
111 * 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
197 return (min); 195 return (min);
198 196
199 return (RANDOM () % diff + min); 197 return (RANDOM () % diff + min);
200} 198}
201 199
202/* decay and destroy persihable items in a map */ 200/* decay and destroy perishable items in a map */
203
204void 201void
205decay_objects (maptile *m) 202maptile::decay_objects ()
206{ 203{
207 int x, y, destroy; 204 int x, y, destroy;
208 object *op, *otmp; 205 object *op, *otmp;
209 206
210 if (m->unique) 207 for (x = 0; x < width; x++)
211 return; 208 for (y = 0; y < height; y++)
212
213 for (x = 0; x < MAP_WIDTH (m); x++)
214 for (y = 0; y < MAP_HEIGHT (m); y++)
215 for (op = get_map_ob (m, x, y); op; op = otmp) 209 for (op = at (x, y).bot; op; op = otmp)
216 { 210 {
217 destroy = 0; 211 destroy = 0;
218 otmp = op->above; 212 otmp = op->above;
213
219 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))
220 break; 215 break;
216
221 if (QUERY_FLAG (op, FLAG_IS_FLOOR) || 217 if (QUERY_FLAG (op, FLAG_IS_FLOOR)
222 QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) || 218 || QUERY_FLAG (op, FLAG_OBJ_ORIGINAL)
223 QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL) || 219 || QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL)
224 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 ())
225 continue; 224 continue;
225
226 /* otherwise, we decay and destroy */ 226 /* otherwise, we decay and destroy */
227 if (IS_WEAPON (op)) 227 if (op->is_weapon ())
228 { 228 {
229 op->stats.dam--; 229 op->stats.dam--;
230 if (op->stats.dam < 0) 230 if (op->stats.dam < 0)
231 destroy = 1; 231 destroy = 1;
232 } 232 }
233 else if (IS_ARMOR (op)) 233 else if (op->is_armor ())
234 { 234 {
235 op->stats.ac--; 235 op->stats.ac--;
236 if (op->stats.ac < 0) 236 if (op->stats.ac < 0)
237 destroy = 1; 237 destroy = 1;
238 } 238 }
245 else 245 else
246 { 246 {
247 if (op->material & M_PAPER || op->material & M_LEATHER || 247 if (op->material & M_PAPER || op->material & M_LEATHER ||
248 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)
249 destroy = 1; 249 destroy = 1;
250
250 if (op->material & M_IRON && rndm (1, 5) == 1) 251 if (op->material & M_IRON && rndm (1, 5) == 1)
251 destroy = 1; 252 destroy = 1;
253
252 if (op->material & M_GLASS && rndm (1, 2) == 1) 254 if (op->material & M_GLASS && rndm (1, 2) == 1)
253 destroy = 1; 255 destroy = 1;
256
254 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)
255 destroy = 1; 258 destroy = 1;
259
256 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)
257 destroy = 1; 261 destroy = 1;
262
258 if (op->material & M_ICE && MAP_TEMP (m) > 32) 263 if (op->material & M_ICE && temp > 32)
259 destroy = 1; 264 destroy = 1;
260 } 265 }
266
261 /* adjust overall chance below */ 267 /* adjust overall chance below */
262 if (destroy && rndm (0, 1)) 268 if (destroy && rndm (0, 1))
263 { 269 op->destroy ();
264 remove_ob (op);
265 free_object (op);
266 }
267 } 270 }
268} 271}
269 272
270/* convert materialname to materialtype_t */ 273/* convert materialname to materialtype_t */
271 274
300 return; 303 return;
301 304
302 if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) 305 if (change->materialname != NULL && strcmp (op->materialname, change->materialname))
303 return; 306 return;
304 307
305 if (!IS_ARMOR (op)) 308 if (!op->is_armor ())
306 return; 309 return;
307 310
308 mt = name_to_material (op->materialname); 311 mt = name_to_material (op->materialname);
309 if (!mt) 312 if (!mt)
310 { 313 {
356 { 359 {
357 if (op->material & mt->material && rndm (1, 100) <= mt->chance && 360 if (op->material & mt->material && rndm (1, 100) <= mt->chance &&
358 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) 361 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
359 { 362 {
360 lmt = mt; 363 lmt = mt;
361 if (!(IS_WEAPON (op) || IS_ARMOR (op))) 364 if (!(op->is_weapon () || op->is_armor ()))
362 break; 365 break;
363 } 366 }
364 } 367 }
365#endif 368#endif
366 } 369 }
374#ifndef NEW_MATERIAL_CODE 377#ifndef NEW_MATERIAL_CODE
375 op->materialname = lmt->name; 378 op->materialname = lmt->name;
376 return; 379 return;
377#else 380#else
378 381
379 if (op->stats.dam && IS_WEAPON (op)) 382 if (op->stats.dam && op->is_weapon ())
380 { 383 {
381 op->stats.dam += lmt->damage; 384 op->stats.dam += lmt->damage;
382 if (op->stats.dam < 1) 385 if (op->stats.dam < 1)
383 op->stats.dam = 1; 386 op->stats.dam = 1;
384 } 387 }
385 if (op->stats.sp && op->type == BOW) 388 if (op->stats.sp && op->type == BOW)
386 op->stats.sp += lmt->sp; 389 op->stats.sp += lmt->sp;
387 if (op->stats.wc && IS_WEAPON (op)) 390 if (op->stats.wc && op->is_weapon ())
388 op->stats.wc += lmt->wc; 391 op->stats.wc += lmt->wc;
389 if (IS_ARMOR (op)) 392 if (op->is_armor ())
390 { 393 {
391 if (op->stats.ac) 394 if (op->stats.ac)
392 op->stats.ac += lmt->ac; 395 op->stats.ac += lmt->ac;
393 for (j = 0; j < NROFATTACKS; j++) 396 for (j = 0; j < NROFATTACKS; j++)
394 if (op->resist[j] != 0) 397 if (op->resist[j] != 0)
400 op->resist[j] = -100; 403 op->resist[j] = -100;
401 } 404 }
402 } 405 }
403 op->materialname = add_string (lmt->name); 406 op->materialname = add_string (lmt->name);
404 /* dont make it unstackable if it doesn't need to be */ 407 /* dont make it unstackable if it doesn't need to be */
405 if (IS_WEAPON (op) || IS_ARMOR (op)) 408 if (op->is_weapon () || op->is_armor ())
406 { 409 {
407 op->weight = (op->weight * lmt->weight) / 100; 410 op->weight = (op->weight * lmt->weight) / 100;
408 op->value = (op->value * lmt->value) / 100; 411 op->value = (op->value * lmt->value) / 100;
409 } 412 }
410#endif 413#endif
545 return; 548 return;
546} 549}
547 550
548///////////////////////////////////////////////////////////////////////////// 551/////////////////////////////////////////////////////////////////////////////
549 552
550#if 0
551refcounted *refcounted::rc_first;
552
553refcounted::refcounted ()
554{
555 refcnt = 0;
556 rc_next = rc_first;
557 rc_first = this;
558}
559
560refcounted::~refcounted ()
561{
562 assert (!rc_next);
563 assert (!refcnt);
564}
565#endif
566
567void *alloc (int s) throw (std::bad_alloc) 553void *salloc_ (int n) throw (std::bad_alloc)
568{ 554{
569 void *p = g_slice_alloc (s); 555 void *ptr = g_slice_alloc (n);
570 556
571 if (!p) 557 if (!ptr)
572 throw std::bad_alloc (); 558 throw std::bad_alloc ();
573 559
574 return p; 560 return ptr;
561}
562
563void *salloc_ (int n, void *src) throw (std::bad_alloc)
564{
565 void *ptr = salloc_ (n);
566
567 if (src)
568 memcpy (ptr, src, n);
569 else
570 memset (ptr, 0, n);
571
572 return ptr;
575} 573}
576 574
577void assign (char *dst, const char *src, int maxlen) 575void assign (char *dst, const char *src, int maxlen)
578{ 576{
579 if (!src) 577 if (!src)
596 } 594 }
597 else 595 else
598 memcpy (dst, src, len + 1); 596 memcpy (dst, src, len + 1);
599} 597}
600 598
599tstamp now ()
600{
601 struct timeval tv;
601 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