/*
* Regenerate mana points
- * Get ((new_mana) / 2^32) mana point once called.
*/
static void regenmana(int percent)
{
*/
if (p_ptr->csp > p_ptr->msp)
{
- u32b decay_frac = (p_ptr->msp * 32 * PY_REGEN_NORMAL + PY_REGEN_MNBASE) << 16;
+ /* PY_REGEN_NORMAL is Regen factor in unit (1/2^16) */
+ s32b decay = 0;
+ u32b decay_frac = (p_ptr->msp * 32 * PY_REGEN_NORMAL + PY_REGEN_MNBASE);
+
+ /* Convert the unit (1/2^16) to (1/2^32) */
+ s64b_LSHIFT(decay, decay_frac, 16);
/* Decay */
- s64b_sub(&(p_ptr->csp), &(p_ptr->csp_frac), 0, decay_frac);
+ s64b_sub(&(p_ptr->csp), &(p_ptr->csp_frac), decay, decay_frac);
/* Stop decaying */
if (p_ptr->csp < p_ptr->msp)
}
}
- /* Regerating mana (unless the player has excess mana) */
+ /* Regenerating mana (unless the player has excess mana) */
else if (percent > 0)
{
- /*
- * (percent/100) is from constants PY_REGEN_* ,
- * which is Regen factor*2^16 .
- */
+ /* (percent/100) is Regen factor in unit (1/2^16) */
s32b new_mana = 0;
u32b new_mana_frac = (p_ptr->msp * percent / 100 + PY_REGEN_MNBASE);
- /* Convert the unit x2^16 -> x2^32 */
- s64b_mul(&new_mana, &new_mana_frac, 0, 0x1L<<16);
+ /* Convert the unit (1/2^16) to (1/2^32) */
+ s64b_LSHIFT(new_mana, new_mana_frac, 16);
/* Regenerate */
s64b_add(&(p_ptr->csp), &(p_ptr->csp_frac), new_mana, new_mana_frac);
/* Reduce mana (even when the player has excess mana) */
if (percent < 0)
{
- u32b reduce_mana_frac = (p_ptr->msp * PY_REGEN_NORMAL + PY_REGEN_MNBASE) << 16;
+ /* PY_REGEN_NORMAL is Regen factor in unit (1/2^16) */
+ s32b reduce_mana = 0;
+ u32b reduce_mana_frac = (p_ptr->msp * PY_REGEN_NORMAL + PY_REGEN_MNBASE);
+
+ /* Convert the unit (1/2^16) to (1/2^32) */
+ s64b_LSHIFT(reduce_mana, reduce_mana_frac, 16);
/* Reduce mana */
- s64b_sub(&(p_ptr->csp), &(p_ptr->csp_frac), 0, reduce_mana_frac);
+ s64b_sub(&(p_ptr->csp), &(p_ptr->csp_frac), reduce_mana, reduce_mana_frac);
- /* check for overflow */
+ /* Check overflow */
if (p_ptr->csp < 0)
{
p_ptr->csp = 0;
new_mana = ((long)p_ptr->magic_num2[i]+adj_mag_mana[A_INT]+13) * percent / 8;
p_ptr->magic_num1[i] += new_mana;
- /* Must set frac to zero even if equal */
+ /* Check maximum charge */
if (p_ptr->magic_num1[i] > (p_ptr->magic_num2[i] << 16))
{
p_ptr->magic_num1[i] = ((long)p_ptr->magic_num2[i] << 16);
need_mana = mod_need_mana(s_ptr->smana, spell, REALM_MUSIC);
need_mana_frac = 0;
- s64b_RSHIFT(need_mana, need_mana_frac);
+ /* Divide by 2 */
+ s64b_RSHIFT(need_mana, need_mana_frac, 1);
if (s64b_cmp(p_ptr->csp, p_ptr->csp_frac, need_mana, need_mana_frac) < 0)
{
/* 64-bit integer operations */
-#define s64b_LSHIFT(V1, V2) {V1 = (V1<<1) | (V2>>31); V2 <<= 1;}
-#define s64b_RSHIFT(V1, V2) {V2 = (V1<<31) | (V2>>1); V1 >>= 1;}
+#define s64b_LSHIFT(V1, V2, N) {V1 = (V1<<(N)) | (V2>>(32-(N))); V2 <<= (N);}
+#define s64b_RSHIFT(V1, V2, N) {V2 = (V1<<(32-(N))) | (V2>>(N)); V1 >>= (N);}
extern void s64b_add(s32b *A1, u32b *A2, s32b B1, u32b B2);
extern void s64b_sub(s32b *A1, u32b *A2, s32b B1, u32b B2);
extern int s64b_cmp(s32b A1, u32b A2, s32b B1, u32b B2);