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.18 by pippijn, Mon Dec 11 19:46:46 2006 UTC vs.
Revision 1.35 by root, Mon Jan 1 13:31:46 2007 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 LOG (llevError, "Calling random_roll with min=%lld max=%lld\n", min, max); 104 LOG (llevError, "Calling random_roll with min=%" PRId64 " max=%" PRId64 "\n", min, max);
103 return (min); /* avoids a float exception */ 105 return (min); /* avoids a float exception */
104 } 106 }
105 107
106 /* 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.
107 * 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
193 return (min); 195 return (min);
194 196
195 return (RANDOM () % diff + min); 197 return (RANDOM () % diff + min);
196} 198}
197 199
198/* decay and destroy persihable items in a map */ 200/* decay and destroy perishable items in a map */
199
200void 201void
201decay_objects (maptile *m) 202maptile::decay_objects ()
202{ 203{
203 int x, y, destroy; 204 if (!spaces)
204 object *op, *otmp;
205
206 if (m->unique)
207 return; 205 return;
208 206
209 for (x = 0; x < MAP_WIDTH (m); x++) 207 for (mapspace *ms = spaces + size (); ms-- > spaces; )
210 for (y = 0; y < MAP_HEIGHT (m); y++) 208 for (object *above, *op = ms->bot; op; op = above)
211 for (op = get_map_ob (m, x, y); op; op = otmp)
212 { 209 {
210 above = op->above;
211
213 destroy = 0; 212 bool destroy = 0;
214 otmp = op->above; 213
214 // do not decay anything above unique floor tiles (yet :)
215 if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) 215 if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE))
216 break; 216 break;
217
217 if (QUERY_FLAG (op, FLAG_IS_FLOOR) || 218 if (QUERY_FLAG (op, FLAG_IS_FLOOR)
218 QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) || 219 || QUERY_FLAG (op, FLAG_OBJ_ORIGINAL)
219 QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL) || 220 || QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL)
220 QUERY_FLAG (op, FLAG_UNIQUE) || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR) || QUERY_FLAG (op, FLAG_UNPAID) || IS_LIVE (op)) 221 || QUERY_FLAG (op, FLAG_UNIQUE)
221 continue; 222 || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR)
222 /* otherwise, we decay and destroy */ 223 || QUERY_FLAG (op, FLAG_UNPAID)
223 if (IS_WEAPON (op)) 224 || op->is_alive ())
225 ; // do not decay
226 else if (op->is_weapon ())
224 { 227 {
225 op->stats.dam--; 228 op->stats.dam--;
226 if (op->stats.dam < 0) 229 if (op->stats.dam < 0)
227 destroy = 1; 230 destroy = 1;
228 } 231 }
229 else if (IS_ARMOR (op)) 232 else if (op->is_armor ())
230 { 233 {
231 op->stats.ac--; 234 op->stats.ac--;
232 if (op->stats.ac < 0) 235 if (op->stats.ac < 0)
233 destroy = 1; 236 destroy = 1;
234 } 237 }
235 else if (op->type == FOOD) 238 else if (op->type == FOOD)
236 { 239 {
237 op->stats.food -= rndm (5, 20); 240 op->stats.food -= rndm (5, 20);
238 if (op->stats.food < 0) 241 if (op->stats.food < 0)
239 destroy = 1; 242 destroy = 1;
240 } 243 }
241 else 244 else
242 { 245 {
243 if (op->material & M_PAPER || op->material & M_LEATHER || 246 int mat = op->material;
244 op->material & M_WOOD || op->material & M_ORGANIC || op->material & M_CLOTH || op->material & M_LIQUID) 247
248 if (mat & M_PAPER
249 || mat & M_LEATHER
250 || mat & M_WOOD
251 || mat & M_ORGANIC
252 || mat & M_CLOTH
253 || mat & M_LIQUID
254 || (mat & M_IRON && rndm (1, 5) == 1)
255 || (mat & M_GLASS && rndm (1, 2) == 1)
256 || ((mat & M_STONE || mat & M_ADAMANT) && rndm (1, 10) == 1)
257 || ((mat & M_SOFT_METAL || mat & M_BONE) && rndm (1, 3) == 1)
258 || (mat & M_ICE && temp > 32))
245 destroy = 1; 259 destroy = 1;
246 if (op->material & M_IRON && rndm (1, 5) == 1)
247 destroy = 1;
248 if (op->material & M_GLASS && rndm (1, 2) == 1)
249 destroy = 1;
250 if ((op->material & M_STONE || op->material & M_ADAMANT) && rndm (1, 10) == 1)
251 destroy = 1;
252 if ((op->material & M_SOFT_METAL || op->material & M_BONE) && rndm (1, 3) == 1)
253 destroy = 1;
254 if (op->material & M_ICE && MAP_TEMP (m) > 32)
255 destroy = 1;
256 } 260 }
261
257 /* adjust overall chance below */ 262 /* adjust overall chance below */
258 if (destroy && rndm (0, 1)) 263 if (destroy && rndm (0, 1))
259 { 264 op->destroy ();
260 remove_ob (op);
261 free_object (op);
262 }
263 } 265 }
264} 266}
265 267
266/* convert materialname to materialtype_t */ 268/* convert materialname to materialtype_t */
267 269
268materialtype_t * 270materialtype_t *
296 return; 298 return;
297 299
298 if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) 300 if (change->materialname != NULL && strcmp (op->materialname, change->materialname))
299 return; 301 return;
300 302
301 if (!IS_ARMOR (op)) 303 if (!op->is_armor ())
302 return; 304 return;
303 305
304 mt = name_to_material (op->materialname); 306 mt = name_to_material (op->materialname);
305 if (!mt) 307 if (!mt)
306 { 308 {
352 { 354 {
353 if (op->material & mt->material && rndm (1, 100) <= mt->chance && 355 if (op->material & mt->material && rndm (1, 100) <= mt->chance &&
354 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) 356 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
355 { 357 {
356 lmt = mt; 358 lmt = mt;
357 if (!(IS_WEAPON (op) || IS_ARMOR (op))) 359 if (!(op->is_weapon () || op->is_armor ()))
358 break; 360 break;
359 } 361 }
360 } 362 }
361#endif 363#endif
362 } 364 }
370#ifndef NEW_MATERIAL_CODE 372#ifndef NEW_MATERIAL_CODE
371 op->materialname = lmt->name; 373 op->materialname = lmt->name;
372 return; 374 return;
373#else 375#else
374 376
375 if (op->stats.dam && IS_WEAPON (op)) 377 if (op->stats.dam && op->is_weapon ())
376 { 378 {
377 op->stats.dam += lmt->damage; 379 op->stats.dam += lmt->damage;
378 if (op->stats.dam < 1) 380 if (op->stats.dam < 1)
379 op->stats.dam = 1; 381 op->stats.dam = 1;
380 } 382 }
381 if (op->stats.sp && op->type == BOW) 383 if (op->stats.sp && op->type == BOW)
382 op->stats.sp += lmt->sp; 384 op->stats.sp += lmt->sp;
383 if (op->stats.wc && IS_WEAPON (op)) 385 if (op->stats.wc && op->is_weapon ())
384 op->stats.wc += lmt->wc; 386 op->stats.wc += lmt->wc;
385 if (IS_ARMOR (op)) 387 if (op->is_armor ())
386 { 388 {
387 if (op->stats.ac) 389 if (op->stats.ac)
388 op->stats.ac += lmt->ac; 390 op->stats.ac += lmt->ac;
389 for (j = 0; j < NROFATTACKS; j++) 391 for (j = 0; j < NROFATTACKS; j++)
390 if (op->resist[j] != 0) 392 if (op->resist[j] != 0)
396 op->resist[j] = -100; 398 op->resist[j] = -100;
397 } 399 }
398 } 400 }
399 op->materialname = add_string (lmt->name); 401 op->materialname = add_string (lmt->name);
400 /* dont make it unstackable if it doesn't need to be */ 402 /* dont make it unstackable if it doesn't need to be */
401 if (IS_WEAPON (op) || IS_ARMOR (op)) 403 if (op->is_weapon () || op->is_armor ())
402 { 404 {
403 op->weight = (op->weight * lmt->weight) / 100; 405 op->weight = (op->weight * lmt->weight) / 100;
404 op->value = (op->value * lmt->value) / 100; 406 op->value = (op->value * lmt->value) / 100;
405 } 407 }
406#endif 408#endif
541 return; 543 return;
542} 544}
543 545
544///////////////////////////////////////////////////////////////////////////// 546/////////////////////////////////////////////////////////////////////////////
545 547
546#if 0
547refcounted *refcounted::rc_first;
548
549refcounted::refcounted ()
550{
551 refcnt = 0;
552 rc_next = rc_first;
553 rc_first = this;
554}
555
556refcounted::~refcounted ()
557{
558 assert (!rc_next);
559 assert (!refcnt);
560}
561#endif
562
563void *alloc (int s) throw (std::bad_alloc) 548void *salloc_ (int n) throw (std::bad_alloc)
564{ 549{
565 void *p = g_slice_alloc (s); 550 void *ptr = g_slice_alloc (n);
566 551
567 if (!p) 552 if (!ptr)
568 throw std::bad_alloc (); 553 throw std::bad_alloc ();
569 554
570 return p; 555 return ptr;
556}
557
558void *salloc_ (int n, void *src) throw (std::bad_alloc)
559{
560 void *ptr = salloc_ (n);
561
562 if (src)
563 memcpy (ptr, src, n);
564 else
565 memset (ptr, 0, n);
566
567 return ptr;
571} 568}
572 569
573void assign (char *dst, const char *src, int maxlen) 570void assign (char *dst, const char *src, int maxlen)
574{ 571{
575 if (!src) 572 if (!src)
592 } 589 }
593 else 590 else
594 memcpy (dst, src, len + 1); 591 memcpy (dst, src, len + 1);
595} 592}
596 593
594tstamp now ()
595{
596 struct timeval tv;
597 597
598 gettimeofday (&tv, 0);
599 return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6);
600}
601
602int
603similar_direction (int a, int b)
604{
605 if (!a || !b)
606 return 0;
607
608 int diff = (b - a) & 7;
609 return diff <= 1 || diff >= 7;
610}
611

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines