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.25 by root, Sun Dec 17 23:10:34 2006 UTC vs.
Revision 1.37 by root, Mon Jan 15 02:39:40 2007 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 CrossFire, A Multiplayer game for X-windows
3 3
4 Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 Copyright (C) 2002 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen 6 Copyright (C) 1992 Frank Tore Johansen
6 7
7 This program is free software; you can redistribute it and/or modify 8 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by 9 it under the terms of the GNU General Public License as published by
23 24
24/* 25/*
25 * General convenience functions for crossfire. 26 * General convenience functions for crossfire.
26 */ 27 */
27 28
29#include <cstdlib>
30#include <sys/types.h>
31#include <unistd.h>
32#include <sys/time.h>
33#include <time.h>
34#include <signal.h>
35
28#include <global.h> 36#include <global.h>
29#include <funcpoint.h> 37#include <funcpoint.h>
30#include <material.h> 38#include <material.h>
31 39
32#include <sys/time.h>
33#include <time.h>
34#include <glib.h> 40#include <glib.h>
35 41
36/* 42/*
37 * The random functions here take luck into account when rolling random 43 * The random functions here take luck into account when rolling random
38 * dice or numbers. This function has less of an impact the larger the 44 * dice or numbers. This function has less of an impact the larger the
195 return (min); 201 return (min);
196 202
197 return (RANDOM () % diff + min); 203 return (RANDOM () % diff + min);
198} 204}
199 205
200/* decay and destroy persihable items in a map */ 206/* decay and destroy perishable items in a map */
201
202void 207void
203decay_objects (maptile *m) 208maptile::decay_objects ()
204{ 209{
205 int x, y, destroy; 210 if (!spaces)
206 object *op, *otmp;
207
208 if (m->unique)
209 return; 211 return;
210 212
211 for (x = 0; x < MAP_WIDTH (m); x++) 213 for (mapspace *ms = spaces + size (); ms-- > spaces; )
212 for (y = 0; y < MAP_HEIGHT (m); y++) 214 for (object *above, *op = ms->bot; op; op = above)
213 for (op = get_map_ob (m, x, y); op; op = otmp)
214 { 215 {
216 above = op->above;
217
215 destroy = 0; 218 bool destroy = 0;
216 otmp = op->above; 219
220 // do not decay anything above unique floor tiles (yet :)
217 if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) 221 if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE))
218 break; 222 break;
223
219 if (QUERY_FLAG (op, FLAG_IS_FLOOR) || 224 if (QUERY_FLAG (op, FLAG_IS_FLOOR)
220 QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) || 225 || QUERY_FLAG (op, FLAG_OBJ_ORIGINAL)
221 QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL) || 226 || QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL)
222 QUERY_FLAG (op, FLAG_UNIQUE) || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR) || QUERY_FLAG (op, FLAG_UNPAID) || IS_LIVE (op)) 227 || QUERY_FLAG (op, FLAG_UNIQUE)
223 continue; 228 || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR)
224 /* otherwise, we decay and destroy */ 229 || QUERY_FLAG (op, FLAG_UNPAID)
225 if (IS_WEAPON (op)) 230 || op->is_alive ())
231 ; // do not decay
232 else if (op->is_weapon ())
226 { 233 {
227 op->stats.dam--; 234 op->stats.dam--;
228 if (op->stats.dam < 0) 235 if (op->stats.dam < 0)
229 destroy = 1; 236 destroy = 1;
230 } 237 }
231 else if (IS_ARMOR (op)) 238 else if (op->is_armor ())
232 { 239 {
233 op->stats.ac--; 240 op->stats.ac--;
234 if (op->stats.ac < 0) 241 if (op->stats.ac < 0)
235 destroy = 1; 242 destroy = 1;
236 } 243 }
237 else if (op->type == FOOD) 244 else if (op->type == FOOD)
238 { 245 {
239 op->stats.food -= rndm (5, 20); 246 op->stats.food -= rndm (5, 20);
240 if (op->stats.food < 0) 247 if (op->stats.food < 0)
241 destroy = 1; 248 destroy = 1;
242 } 249 }
243 else 250 else
244 { 251 {
245 if (op->material & M_PAPER || op->material & M_LEATHER || 252 int mat = op->material;
246 op->material & M_WOOD || op->material & M_ORGANIC || op->material & M_CLOTH || op->material & M_LIQUID) 253
254 if (mat & M_PAPER
255 || mat & M_LEATHER
256 || mat & M_WOOD
257 || mat & M_ORGANIC
258 || mat & M_CLOTH
259 || mat & M_LIQUID
260 || (mat & M_IRON && rndm (1, 5) == 1)
261 || (mat & M_GLASS && rndm (1, 2) == 1)
262 || ((mat & M_STONE || mat & M_ADAMANT) && rndm (1, 10) == 1)
263 || ((mat & M_SOFT_METAL || mat & M_BONE) && rndm (1, 3) == 1)
264 || (mat & M_ICE && temp > 32))
247 destroy = 1; 265 destroy = 1;
248 if (op->material & M_IRON && rndm (1, 5) == 1)
249 destroy = 1;
250 if (op->material & M_GLASS && rndm (1, 2) == 1)
251 destroy = 1;
252 if ((op->material & M_STONE || op->material & M_ADAMANT) && rndm (1, 10) == 1)
253 destroy = 1;
254 if ((op->material & M_SOFT_METAL || op->material & M_BONE) && rndm (1, 3) == 1)
255 destroy = 1;
256 if (op->material & M_ICE && MAP_TEMP (m) > 32)
257 destroy = 1;
258 } 266 }
267
259 /* adjust overall chance below */ 268 /* adjust overall chance below */
260 if (destroy && rndm (0, 1)) 269 if (destroy && rndm (0, 1))
261 op->destroy (); 270 op->destroy ();
262 } 271 }
263} 272}
264 273
265/* convert materialname to materialtype_t */ 274/* convert materialname to materialtype_t */
266 275
267materialtype_t * 276materialtype_t *
295 return; 304 return;
296 305
297 if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) 306 if (change->materialname != NULL && strcmp (op->materialname, change->materialname))
298 return; 307 return;
299 308
300 if (!IS_ARMOR (op)) 309 if (!op->is_armor ())
301 return; 310 return;
302 311
303 mt = name_to_material (op->materialname); 312 mt = name_to_material (op->materialname);
304 if (!mt) 313 if (!mt)
305 { 314 {
351 { 360 {
352 if (op->material & mt->material && rndm (1, 100) <= mt->chance && 361 if (op->material & mt->material && rndm (1, 100) <= mt->chance &&
353 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) 362 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
354 { 363 {
355 lmt = mt; 364 lmt = mt;
356 if (!(IS_WEAPON (op) || IS_ARMOR (op))) 365 if (!(op->is_weapon () || op->is_armor ()))
357 break; 366 break;
358 } 367 }
359 } 368 }
360#endif 369#endif
361 } 370 }
369#ifndef NEW_MATERIAL_CODE 378#ifndef NEW_MATERIAL_CODE
370 op->materialname = lmt->name; 379 op->materialname = lmt->name;
371 return; 380 return;
372#else 381#else
373 382
374 if (op->stats.dam && IS_WEAPON (op)) 383 if (op->stats.dam && op->is_weapon ())
375 { 384 {
376 op->stats.dam += lmt->damage; 385 op->stats.dam += lmt->damage;
377 if (op->stats.dam < 1) 386 if (op->stats.dam < 1)
378 op->stats.dam = 1; 387 op->stats.dam = 1;
379 } 388 }
380 if (op->stats.sp && op->type == BOW) 389 if (op->stats.sp && op->type == BOW)
381 op->stats.sp += lmt->sp; 390 op->stats.sp += lmt->sp;
382 if (op->stats.wc && IS_WEAPON (op)) 391 if (op->stats.wc && op->is_weapon ())
383 op->stats.wc += lmt->wc; 392 op->stats.wc += lmt->wc;
384 if (IS_ARMOR (op)) 393 if (op->is_armor ())
385 { 394 {
386 if (op->stats.ac) 395 if (op->stats.ac)
387 op->stats.ac += lmt->ac; 396 op->stats.ac += lmt->ac;
388 for (j = 0; j < NROFATTACKS; j++) 397 for (j = 0; j < NROFATTACKS; j++)
389 if (op->resist[j] != 0) 398 if (op->resist[j] != 0)
395 op->resist[j] = -100; 404 op->resist[j] = -100;
396 } 405 }
397 } 406 }
398 op->materialname = add_string (lmt->name); 407 op->materialname = add_string (lmt->name);
399 /* dont make it unstackable if it doesn't need to be */ 408 /* dont make it unstackable if it doesn't need to be */
400 if (IS_WEAPON (op) || IS_ARMOR (op)) 409 if (op->is_weapon () || op->is_armor ())
401 { 410 {
402 op->weight = (op->weight * lmt->weight) / 100; 411 op->weight = (op->weight * lmt->weight) / 100;
403 op->value = (op->value * lmt->value) / 100; 412 op->value = (op->value * lmt->value) / 100;
404 } 413 }
405#endif 414#endif
540 return; 549 return;
541} 550}
542 551
543///////////////////////////////////////////////////////////////////////////// 552/////////////////////////////////////////////////////////////////////////////
544 553
545#if 0 554void
546refcounted *refcounted::rc_first; 555fork_abort (const char *msg)
547
548refcounted::refcounted ()
549{ 556{
550 refcnt = 0; 557 LOG (llevError, "fork abort: %s\n", msg);
551 rc_next = rc_first; 558 if (!fork ())
552 rc_first = this; 559 {
553} 560 signal (SIGABRT, SIG_DFL);
561 abort ();
562 }
554 563
555refcounted::~refcounted ()
556{
557 assert (!rc_next);
558 assert (!refcnt);
559} 564}
560#endif
561
562void *salloc_ (int n) throw (std::bad_alloc) 565void *salloc_ (int n) throw (std::bad_alloc)
563{ 566{
564 void *ptr = g_slice_alloc (n); 567 void *ptr = g_slice_alloc (n);
565 568
566 if (!ptr) 569 if (!ptr)
611 614
612 gettimeofday (&tv, 0); 615 gettimeofday (&tv, 0);
613 return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); 616 return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6);
614} 617}
615 618
619int
620similar_direction (int a, int b)
621{
622 if (!a || !b)
623 return 0;
624
625 int diff = (b - a) & 7;
626 return diff <= 1 || diff >= 7;
627}
628

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines