Just a little reminder for C devs: be careful when using macros evaluating variables more than once, because unwanted effects could occur. I've screwed myself more than once with such macros...
Here's a good example from mon-spell.c:
The MIN macro is defined as:
So writing something like MIN(random, fixed) is actually (random > fixed? fixed: random).
In the previous example, nothing can ensure you that the result will be lower than 300, since randcalc will be called twice, once to compare the result to 300 and once to return the result!
The fix is simple:
Here's a good example from mon-spell.c:
Code:
inven_damage(p_ptr, re_ptr->gf, MIN(dam * randcalc(re_ptr->dam, 0, RANDOMISE), 300));
Code:
#define MIN(a,b) (((a) > (b))? (b) : (a))
In the previous example, nothing can ensure you that the result will be lower than 300, since randcalc will be called twice, once to compare the result to 300 and once to return the result!
The fix is simple:
Code:
int calcdam = dam * randcalc(re_ptr->dam, 0, RANDOMISE); inven_damage(p_ptr, re_ptr->gf, MIN(calcdam, 300));
Comment