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.22 by root, Tue Dec 12 21:39:56 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
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 int x, y, destroy;
204 object *op, *otmp; 205 object *op, *otmp;
205 206
206 if (m->unique) 207 for (x = 0; x < width; x++)
207 return; 208 for (y = 0; y < height; y++)
208
209 for (x = 0; x < MAP_WIDTH (m); x++)
210 for (y = 0; y < MAP_HEIGHT (m); y++)
211 for (op = get_map_ob (m, x, y); op; op = otmp) 209 for (op = at (x, y).bot; op; op = otmp)
212 { 210 {
213 destroy = 0; 211 destroy = 0;
214 otmp = op->above; 212 otmp = op->above;
213
215 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))
216 break; 215 break;
216
217 if (QUERY_FLAG (op, FLAG_IS_FLOOR) || 217 if (QUERY_FLAG (op, FLAG_IS_FLOOR)
218 QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) || 218 || QUERY_FLAG (op, FLAG_OBJ_ORIGINAL)
219 QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL) || 219 || 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)) 220 || QUERY_FLAG (op, FLAG_UNIQUE)
221 || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR)
222 || QUERY_FLAG (op, FLAG_UNPAID)
223 || op->is_alive ())
221 continue; 224 continue;
225
222 /* otherwise, we decay and destroy */ 226 /* otherwise, we decay and destroy */
223 if (IS_WEAPON (op)) 227 if (op->is_weapon ())
224 { 228 {
225 op->stats.dam--; 229 op->stats.dam--;
226 if (op->stats.dam < 0) 230 if (op->stats.dam < 0)
227 destroy = 1; 231 destroy = 1;
228 } 232 }
229 else if (IS_ARMOR (op)) 233 else if (op->is_armor ())
230 { 234 {
231 op->stats.ac--; 235 op->stats.ac--;
232 if (op->stats.ac < 0) 236 if (op->stats.ac < 0)
233 destroy = 1; 237 destroy = 1;
234 } 238 }
241 else 245 else
242 { 246 {
243 if (op->material & M_PAPER || op->material & M_LEATHER || 247 if (op->material & M_PAPER || op->material & M_LEATHER ||
244 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)
245 destroy = 1; 249 destroy = 1;
250
246 if (op->material & M_IRON && rndm (1, 5) == 1) 251 if (op->material & M_IRON && rndm (1, 5) == 1)
247 destroy = 1; 252 destroy = 1;
253
248 if (op->material & M_GLASS && rndm (1, 2) == 1) 254 if (op->material & M_GLASS && rndm (1, 2) == 1)
249 destroy = 1; 255 destroy = 1;
256
250 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)
251 destroy = 1; 258 destroy = 1;
259
252 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)
253 destroy = 1; 261 destroy = 1;
262
254 if (op->material & M_ICE && MAP_TEMP (m) > 32) 263 if (op->material & M_ICE && temp > 32)
255 destroy = 1; 264 destroy = 1;
256 } 265 }
266
257 /* adjust overall chance below */ 267 /* adjust overall chance below */
258 if (destroy && rndm (0, 1)) 268 if (destroy && rndm (0, 1))
259 op->destroy (); 269 op->destroy ();
260 } 270 }
261} 271}
293 return; 303 return;
294 304
295 if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) 305 if (change->materialname != NULL && strcmp (op->materialname, change->materialname))
296 return; 306 return;
297 307
298 if (!IS_ARMOR (op)) 308 if (!op->is_armor ())
299 return; 309 return;
300 310
301 mt = name_to_material (op->materialname); 311 mt = name_to_material (op->materialname);
302 if (!mt) 312 if (!mt)
303 { 313 {
349 { 359 {
350 if (op->material & mt->material && rndm (1, 100) <= mt->chance && 360 if (op->material & mt->material && rndm (1, 100) <= mt->chance &&
351 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) 361 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
352 { 362 {
353 lmt = mt; 363 lmt = mt;
354 if (!(IS_WEAPON (op) || IS_ARMOR (op))) 364 if (!(op->is_weapon () || op->is_armor ()))
355 break; 365 break;
356 } 366 }
357 } 367 }
358#endif 368#endif
359 } 369 }
367#ifndef NEW_MATERIAL_CODE 377#ifndef NEW_MATERIAL_CODE
368 op->materialname = lmt->name; 378 op->materialname = lmt->name;
369 return; 379 return;
370#else 380#else
371 381
372 if (op->stats.dam && IS_WEAPON (op)) 382 if (op->stats.dam && op->is_weapon ())
373 { 383 {
374 op->stats.dam += lmt->damage; 384 op->stats.dam += lmt->damage;
375 if (op->stats.dam < 1) 385 if (op->stats.dam < 1)
376 op->stats.dam = 1; 386 op->stats.dam = 1;
377 } 387 }
378 if (op->stats.sp && op->type == BOW) 388 if (op->stats.sp && op->type == BOW)
379 op->stats.sp += lmt->sp; 389 op->stats.sp += lmt->sp;
380 if (op->stats.wc && IS_WEAPON (op)) 390 if (op->stats.wc && op->is_weapon ())
381 op->stats.wc += lmt->wc; 391 op->stats.wc += lmt->wc;
382 if (IS_ARMOR (op)) 392 if (op->is_armor ())
383 { 393 {
384 if (op->stats.ac) 394 if (op->stats.ac)
385 op->stats.ac += lmt->ac; 395 op->stats.ac += lmt->ac;
386 for (j = 0; j < NROFATTACKS; j++) 396 for (j = 0; j < NROFATTACKS; j++)
387 if (op->resist[j] != 0) 397 if (op->resist[j] != 0)
393 op->resist[j] = -100; 403 op->resist[j] = -100;
394 } 404 }
395 } 405 }
396 op->materialname = add_string (lmt->name); 406 op->materialname = add_string (lmt->name);
397 /* dont make it unstackable if it doesn't need to be */ 407 /* dont make it unstackable if it doesn't need to be */
398 if (IS_WEAPON (op) || IS_ARMOR (op)) 408 if (op->is_weapon () || op->is_armor ())
399 { 409 {
400 op->weight = (op->weight * lmt->weight) / 100; 410 op->weight = (op->weight * lmt->weight) / 100;
401 op->value = (op->value * lmt->value) / 100; 411 op->value = (op->value * lmt->value) / 100;
402 } 412 }
403#endif 413#endif
538 return; 548 return;
539} 549}
540 550
541///////////////////////////////////////////////////////////////////////////// 551/////////////////////////////////////////////////////////////////////////////
542 552
543#if 0
544refcounted *refcounted::rc_first;
545
546refcounted::refcounted ()
547{
548 refcnt = 0;
549 rc_next = rc_first;
550 rc_first = this;
551}
552
553refcounted::~refcounted ()
554{
555 assert (!rc_next);
556 assert (!refcnt);
557}
558#endif
559
560void *alloc (int s) throw (std::bad_alloc) 553void *salloc_ (int n) throw (std::bad_alloc)
561{ 554{
562 void *p = g_slice_alloc (s); 555 void *ptr = g_slice_alloc (n);
563 556
564 if (!p) 557 if (!ptr)
565 throw std::bad_alloc (); 558 throw std::bad_alloc ();
566 559
567 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;
568} 573}
569 574
570void assign (char *dst, const char *src, int maxlen) 575void assign (char *dst, const char *src, int maxlen)
571{ 576{
572 if (!src) 577 if (!src)
589 } 594 }
590 else 595 else
591 memcpy (dst, src, len + 1); 596 memcpy (dst, src, len + 1);
592} 597}
593 598
599tstamp now ()
600{
601 struct timeval tv;
594 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