Changeset 707

Show
Ignore:
Timestamp:
02/16/08 09:04:45 (9 months ago)
Author:
takkaria
Message:
  • Make rand_range() a function, so assert()s can be added
  • Add an assert to Rand_div()
  • Fix store bug reported by Pete Mack
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/melee1.c

    r656 r707  
    447447 
    448448                        /* Roll out the damage */ 
    449                         damage = damroll(d_dice, d_side); 
     449                        if (d_dice > 0 && d_side > 0) 
     450                                damage = damroll(d_dice, d_side); 
     451                        else 
     452                                damage = 0; 
    450453 
    451454                        /* Apply appropriate damage */ 
  • trunk/src/store.c

    r681 r707  
    12141214 
    12151215        /* 
    1216          * Decide min/max levels  
     1216         * Decide min/max levels 
    12171217         */ 
    12181218        if (st == STORE_B_MARKET) 
     
    12271227        } 
    12281228 
    1229         if (min_level > 55) min_level = 65; 
     1229        if (min_level > 55) min_level = 55; 
    12301230        if (max_level > 70) max_level = 70; 
    12311231 
  • trunk/src/z-rand.c

    r507 r707  
    118118 * This method has no bias, and is much less affected by patterns 
    119119 * in the "low" bits of the underlying RNG's. 
    120  * 
    121  * Note that "m" must not be greater than 0x1000000, or division 
    122  * by zero will result. 
    123  * 
    124  * ToDo: Check for m > 0x1000000. 
    125120 */ 
    126121u32b Rand_div(u32b m) 
    127122{ 
    128123        u32b r, n; 
     124 
     125        /* Division by zero will result if m is larger than 0x10000000 */ 
     126        assert(m <= 0x10000000); 
    129127 
    130128        /* Hack -- simple case */ 
     
    199197static s16b Rand_normal_table[RANDNOR_NUM] = 
    200198{ 
    201         206,     613,    1022,    1430,                1838,    2245,    2652,    3058, 
    202         3463,    3867,    4271,    4673,       5075,    5475,    5874,    6271, 
    203         6667,    7061,    7454,    7845,       8234,    8621,    9006,    9389, 
    204         9770,   10148,   10524,   10898,   11269,     11638,   12004,   12367, 
     199        206,     613,    1022,    1430,               1838,    2245,     2652,     3058, 
     200        3463,    3867,    4271,    4673,    5075,    5475,     5874,     6271, 
     201        6667,    7061,    7454,    7845,    8234,    8621,     9006,     9389, 
     202        9770,    10148,   10524,   10898,   11269,    11638,   12004,   12367, 
    205203        12727,   13085,   13440,   13792,   14140,      14486,   14828,   15168, 
    206204        15504,   15836,   16166,   16492,   16814,      17133,   17449,   17761, 
     
    306304 * "external" program parts like the main-*.c files.  It preserves 
    307305 * the current RNG state to prevent influences on game-play. 
    308  * 
    309  * Could also use rand() from <stdlib.h> directly. XXX XXX XXX 
    310306 */ 
    311307u32b Rand_simple(u32b m) 
     
    361357        int sum = 0; 
    362358 
     359/*      assert(sides > 0); */ 
    363360        if (sides <= 0) return (0); 
    364361 
     
    369366} 
    370367 
     368 
     369/** 
     370 * Generates a random signed long integer X where `A` <= X <= `B`. 
     371 * The integer X falls along a uniform distribution. 
     372 * 
     373 * Note that "rand_range(0, N-1)" == "rand_int(N)". 
     374 */ 
     375int rand_range(int A, int B) 
     376{ 
     377        if (A == B) return A; 
     378 
     379        assert(A < B); 
     380        assert(B > A); 
     381 
     382        return A + (s32b)Rand_div(1 + B - A); 
     383} 
  • trunk/src/z-rand.h

    r583 r707  
    3232#define randint(M)              ((s32b) Rand_div(M) + 1) 
    3333#define rand_die(M)             (rand_int(M) + 1) 
    34  
    35  
    36 /** 
    37  * Generates a random signed long integer X where "A <= X <= B" 
    38  * Note that "rand_range(0, N-1)" == "rand_int(N)" 
    39  * 
    40  * The integer X falls along a uniform distribution. 
    41  */ 
    42 #define rand_range(A,B)         ((A) + (rand_int(1+(B)-(A)))) 
    4334 
    4435 
     
    10091int damroll(int num, int sides); 
    10192 
     93/** 
     94 * Generates a random signed long integer X where "A <= X <= B" 
     95 * Note that "rand_range(0, N-1)" == "rand_int(N)" 
     96 * 
     97 * The integer X falls along a uniform distribution. 
     98 */ 
     99int rand_range(int A, int B); 
     100 
    102101 
    103102#endif /* INCLUDED_Z_RAND_H */