home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Games / NetHack 3.1.3 / source / src / rnd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-01  |  2.1 KB  |  134 lines  |  [TEXT/R*ch]

  1. /*    SCCS Id: @(#)rnd.c    3.1    90/22/02
  2. /* NetHack may be freely redistributed.  See license for details. */
  3.  
  4. #include    "hack.h"
  5.  
  6. #if defined(LINT) && defined(UNIX)    /* rand() is long... */
  7. extern int NDECL(rand);
  8. #define RND(x)    (rand() % x)
  9. #else /* LINT */
  10. /* rand() is either random() or lrand48() - see config.h. */
  11. #ifdef UNIX
  12. #define RND(x)    (int)(Rand() % (long)(x))
  13. #else
  14. /* Good luck: the bottom order bits are cyclic. */
  15. #define RND(x)    (int)((Rand()>>3) % (x))
  16. #endif
  17. #endif /* LINT */
  18.  
  19. #ifdef OVL0
  20.  
  21. int
  22. rn2(x)        /* 0 <= rn2(x) < x */
  23. register int x;
  24. {
  25. #ifdef DEBUG
  26.     if (x == 0) {
  27.         impossible("rn2(0) attempted");
  28.         return(0);
  29.     }
  30.     x = RND(x);
  31.     return(x);
  32. #else
  33.     return(RND(x));
  34. #endif
  35. }
  36.  
  37. #endif /* OVL0 */
  38. #ifdef OVLB
  39.  
  40. int
  41. rnl(x)        /* 0 <= rnl(x) < x; sometimes subtracting Luck */
  42. register int x;    /* good luck approaches 0, bad luck approaches (x-1) */
  43. {
  44.     register int i;
  45.  
  46. #ifdef DEBUG
  47.     if (x == 0) {
  48.         impossible("rnl(0) attempted");
  49.         return(0);
  50.     }
  51. #endif
  52.     i = RND(x);
  53.  
  54.     if (Luck && rn2(50 - Luck)) {
  55.         i -= (x <= 15 && Luck >= -5 ? Luck/3 : Luck);
  56.         if (i < 0) i = 0;
  57.         else if (i >= x) i = x-1;
  58.     }
  59.  
  60.     return i;
  61. }
  62.  
  63. #endif /* OVLB */
  64. #ifdef OVL0
  65.  
  66. int
  67. rnd(x)        /* 1 <= rnd(x) <= x */
  68. register int x;
  69. {
  70. #ifdef DEBUG
  71.     if (x == 0) {
  72.         impossible("rnd(0) attempted");
  73.         return(1);
  74.     }
  75.     x = RND(x)+1;
  76.     return(x);
  77. #else
  78.     return(RND(x)+1);
  79. #endif
  80. }
  81.  
  82. #endif /* OVL0 */
  83. #ifdef OVL1
  84.  
  85. int
  86. d(n,x)        /* n <= d(n,x) <= (n*x) */
  87. register int n, x;
  88. {
  89.     register int tmp = n;
  90.  
  91. #ifdef DEBUG
  92.     if (x == 0 && n != 0) {
  93.         impossible("d(n,0) attempted");
  94.         return(1);
  95.     }
  96. #endif
  97.     while(n--) tmp += RND(x);
  98.     return(tmp); /* Alea iacta est. -- J.C. */
  99. }
  100.  
  101. #endif /* OVL1 */
  102. #ifdef OVLB
  103.  
  104. int
  105. rne(x)      /* by stewr 870807 */
  106. register int x;
  107. {
  108.     register int tmp = 1;
  109.     while(!rn2(x)) tmp++;
  110.     return(min(tmp,(u.ulevel < 15) ? 5 : (int)u.ulevel/3));
  111. }
  112.  
  113. int
  114. rnz(i)
  115. int i;
  116. {
  117. #ifdef LINT
  118.     int x = i;
  119.     int tmp = 1000;
  120. #else
  121.     register long x = i;
  122.     register long tmp = 1000;
  123. #endif
  124.     tmp += rn2(1000);
  125.     tmp *= rne(4);
  126.     if (rn2(2)) { x *= tmp; x /= 1000; }
  127.     else { x *= 1000; x /= tmp; }
  128.     return((int)x);
  129. }
  130.  
  131. #endif /* OVLB */
  132.  
  133. /*rnd.c*/
  134.