0 <= rnl(x) < x; sometimes subtracting Luck; good luck approaches 0, bad luck approaches (x-1) */
| 109 | /* 0 <= rnl(x) < x; sometimes subtracting Luck; |
| 110 | good luck approaches 0, bad luck approaches (x-1) */ |
| 111 | int |
| 112 | rnl(int x) |
| 113 | { |
| 114 | int i, adjustment; |
| 115 | |
| 116 | #if (NH_DEVEL_STATUS != NH_STATUS_RELEASED) |
| 117 | if (x <= 0) { |
| 118 | impossible("rnl(%d) attempted", x); |
| 119 | return 0; |
| 120 | } |
| 121 | #endif |
| 122 | |
| 123 | adjustment = Luck; |
| 124 | if (x <= 15) { |
| 125 | /* for small ranges, use Luck/3 (rounded away from 0); |
| 126 | also guard against architecture-specific differences |
| 127 | of integer division involving negative values */ |
| 128 | adjustment = (abs(adjustment) + 1) / 3 * sgn(adjustment); |
| 129 | /* |
| 130 | * 11..13 -> 4 |
| 131 | * 8..10 -> 3 |
| 132 | * 5.. 7 -> 2 |
| 133 | * 2.. 4 -> 1 |
| 134 | * -1,0,1 -> 0 (no adjustment) |
| 135 | * -4..-2 -> -1 |
| 136 | * -7..-5 -> -2 |
| 137 | * -10..-8 -> -3 |
| 138 | * -13..-11-> -4 |
| 139 | */ |
| 140 | } |
| 141 | |
| 142 | i = RND(x); |
| 143 | if (adjustment && rn2(37 + abs(adjustment))) { |
| 144 | i -= adjustment; |
| 145 | if (i < 0) |
| 146 | i = 0; |
| 147 | else if (i >= x) |
| 148 | i = x - 1; |
| 149 | } |
| 150 | return i; |
| 151 | } |
| 152 | |
| 153 | /* 1 <= rnd(x) <= x */ |
| 154 | int |
no test coverage detected