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.14 by root, Thu Sep 14 01:34:41 2006 UTC vs.
Revision 1.28 by root, Sat Dec 23 16:05:19 2006 UTC

16 16
17 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
18 along with this program; if not, write to the Free Software 18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 20
21 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>
22*/ 22*/
23 23
24/* 24/*
25 * General convenience functions for crossfire. 25 * General convenience functions for crossfire.
26 */ 26 */
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
200} 198}
201 199
202/* decay and destroy persihable items in a map */ 200/* decay and destroy persihable items in a map */
203 201
204void 202void
205decay_objects (mapstruct *m) 203decay_objects (maptile *m)
206{ 204{
207 int x, y, destroy; 205 int x, y, destroy;
208 object *op, *otmp; 206 object *op, *otmp;
209 207
210 if (m->unique) 208 if (m->unique)
211 return; 209 return;
212 210
213 for (x = 0; x < MAP_WIDTH (m); x++) 211 for (x = 0; x < MAP_WIDTH (m); x++)
214 for (y = 0; y < MAP_HEIGHT (m); y++) 212 for (y = 0; y < MAP_HEIGHT (m); y++)
215 for (op = get_map_ob (m, x, y); op; op = otmp) 213 for (op = GET_MAP_OB (m, x, y); op; op = otmp)
216 { 214 {
217 destroy = 0; 215 destroy = 0;
218 otmp = op->above; 216 otmp = op->above;
219 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))
220 break; 218 break;
221 if (QUERY_FLAG (op, FLAG_IS_FLOOR) || 219 if (QUERY_FLAG (op, FLAG_IS_FLOOR)
222 QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) || 220 || QUERY_FLAG (op, FLAG_OBJ_ORIGINAL)
223 QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL) || 221 || 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)) 222 || QUERY_FLAG (op, FLAG_UNIQUE)
223 || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR)
224 || QUERY_FLAG (op, FLAG_UNPAID)
225 || op->is_alive ())
225 continue; 226 continue;
227
226 /* otherwise, we decay and destroy */ 228 /* otherwise, we decay and destroy */
227 if (IS_WEAPON (op)) 229 if (op->is_weapon ())
228 { 230 {
229 op->stats.dam--; 231 op->stats.dam--;
230 if (op->stats.dam < 0) 232 if (op->stats.dam < 0)
231 destroy = 1; 233 destroy = 1;
232 } 234 }
233 else if (IS_ARMOR (op)) 235 else if (op->is_armor ())
234 { 236 {
235 op->stats.ac--; 237 op->stats.ac--;
236 if (op->stats.ac < 0) 238 if (op->stats.ac < 0)
237 destroy = 1; 239 destroy = 1;
238 } 240 }
258 if (op->material & M_ICE && MAP_TEMP (m) > 32) 260 if (op->material & M_ICE && MAP_TEMP (m) > 32)
259 destroy = 1; 261 destroy = 1;
260 } 262 }
261 /* adjust overall chance below */ 263 /* adjust overall chance below */
262 if (destroy && rndm (0, 1)) 264 if (destroy && rndm (0, 1))
263 { 265 op->destroy ();
264 remove_ob (op);
265 free_object (op);
266 }
267 } 266 }
268} 267}
269 268
270/* convert materialname to materialtype_t */ 269/* convert materialname to materialtype_t */
271 270
300 return; 299 return;
301 300
302 if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) 301 if (change->materialname != NULL && strcmp (op->materialname, change->materialname))
303 return; 302 return;
304 303
305 if (!IS_ARMOR (op)) 304 if (!op->is_armor ())
306 return; 305 return;
307 306
308 mt = name_to_material (op->materialname); 307 mt = name_to_material (op->materialname);
309 if (!mt) 308 if (!mt)
310 { 309 {
356 { 355 {
357 if (op->material & mt->material && rndm (1, 100) <= mt->chance && 356 if (op->material & mt->material && rndm (1, 100) <= mt->chance &&
358 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) 357 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
359 { 358 {
360 lmt = mt; 359 lmt = mt;
361 if (!(IS_WEAPON (op) || IS_ARMOR (op))) 360 if (!(op->is_weapon () || op->is_armor ()))
362 break; 361 break;
363 } 362 }
364 } 363 }
365#endif 364#endif
366 } 365 }
374#ifndef NEW_MATERIAL_CODE 373#ifndef NEW_MATERIAL_CODE
375 op->materialname = lmt->name; 374 op->materialname = lmt->name;
376 return; 375 return;
377#else 376#else
378 377
379 if (op->stats.dam && IS_WEAPON (op)) 378 if (op->stats.dam && op->is_weapon ())
380 { 379 {
381 op->stats.dam += lmt->damage; 380 op->stats.dam += lmt->damage;
382 if (op->stats.dam < 1) 381 if (op->stats.dam < 1)
383 op->stats.dam = 1; 382 op->stats.dam = 1;
384 } 383 }
385 if (op->stats.sp && op->type == BOW) 384 if (op->stats.sp && op->type == BOW)
386 op->stats.sp += lmt->sp; 385 op->stats.sp += lmt->sp;
387 if (op->stats.wc && IS_WEAPON (op)) 386 if (op->stats.wc && op->is_weapon ())
388 op->stats.wc += lmt->wc; 387 op->stats.wc += lmt->wc;
389 if (IS_ARMOR (op)) 388 if (op->is_armor ())
390 { 389 {
391 if (op->stats.ac) 390 if (op->stats.ac)
392 op->stats.ac += lmt->ac; 391 op->stats.ac += lmt->ac;
393 for (j = 0; j < NROFATTACKS; j++) 392 for (j = 0; j < NROFATTACKS; j++)
394 if (op->resist[j] != 0) 393 if (op->resist[j] != 0)
400 op->resist[j] = -100; 399 op->resist[j] = -100;
401 } 400 }
402 } 401 }
403 op->materialname = add_string (lmt->name); 402 op->materialname = add_string (lmt->name);
404 /* dont make it unstackable if it doesn't need to be */ 403 /* dont make it unstackable if it doesn't need to be */
405 if (IS_WEAPON (op) || IS_ARMOR (op)) 404 if (op->is_weapon () || op->is_armor ())
406 { 405 {
407 op->weight = (op->weight * lmt->weight) / 100; 406 op->weight = (op->weight * lmt->weight) / 100;
408 op->value = (op->value * lmt->value) / 100; 407 op->value = (op->value * lmt->value) / 100;
409 } 408 }
410#endif 409#endif
545 return; 544 return;
546} 545}
547 546
548///////////////////////////////////////////////////////////////////////////// 547/////////////////////////////////////////////////////////////////////////////
549 548
549#if 0
550refcounted *refcounted::rc_first;
551
552refcounted::refcounted ()
553{
554 refcnt = 0;
555 rc_next = rc_first;
556 rc_first = this;
557}
558#endif
559
560refcounted::~refcounted ()
561{
562#if 0
563 assert (!rc_next);
564 assert (!refcnt);
565#endif
566}
567
550void *alloc (int s) throw (std::bad_alloc) 568void *salloc_ (int n) throw (std::bad_alloc)
551{ 569{
552 void *p = g_slice_alloc (s); 570 void *ptr = g_slice_alloc (n);
553 571
554 if (!p) 572 if (!ptr)
555 throw std::bad_alloc (); 573 throw std::bad_alloc ();
556 574
557 return p; 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;
558} 588}
559 589
560void assign (char *dst, const char *src, int maxlen) 590void assign (char *dst, const char *src, int maxlen)
561{ 591{
562 if (!src) 592 if (!src)
579 } 609 }
580 else 610 else
581 memcpy (dst, src, len + 1); 611 memcpy (dst, src, len + 1);
582} 612}
583 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