Ticket #1700: 0002-Add-PF_MAGICAL-player-flag.txt​

File 0002-Add-PF_MAGICAL-player-flag.txt​, 5.2 KB (added by daniel.santos, 7 years ago)

updated patch

Line 
1From 01f1ad8fa2618dbef9df6fcd3361549592b9b610 Mon Sep 17 00:00:00 2001
2From: Daniel Santos <daniel.santos@pobox.com>
3Date: Wed, 7 Nov 2012 13:17:52 -0600
4Subject: Add PF_MAGICAL player flag
5
6Intended for "magical races", who are able to use magic more easily and
7has two parts.
8
91. Reduced spell failures - Reduces the chance for spell failure by 1/3.
10   Classes w/o ZERO_FAIL have their minimum fail rate reduced to 3%
11   instead of 5%.
12
132. Grants chance to cast spells/recite prayers at a reduced cost.
14
15   o Base chance is 10% at level 1 and 50% at level 50, minus the
16     spell's failure chance:
17       10 + (lev - 1) * 40 / 49 - fail_chance
18
19   o Spell cost reduction upon a success check is randomly selected
20     within the range of the spell's failure chance and 75%, but in
21     fractional form so as to evenly affect all spells, including those
22     who's cost is one mana point:
23       rand_range(fail_chance * 0x10000 / 100, 75 * 0x10000 / 100)
24
25    As such, classes with ZERO_FAIL end up getting a slight bonus to
26    success once their fail rate drops below 3 for a spell.
27
28In order to facilitate the second bonus, this patch converts the manna
29usage code in spell_cast to use a 32-bit manna + fraction value instead
30of only working with the non-fraction portion.  Otherwise, this wouldn't
31behave correctly and would have to either allow spell casting sometimes
32for free, or never reduce the cost of spells that take 1 manna.
33---
34 src/list-player-flags.h |    1 +
35 src/player/spell.c      |   44 ++++++++++++++++++++++++++++++++++++--------
36 src/ui-birth.c          |    1 +
37 3 files changed, 38 insertions(+), 8 deletions(-)
38
39diff --git a/src/list-player-flags.h b/src/list-player-flags.h
40index 0dae25a..7b2c217 100644
41--- a/src/list-player-flags.h
42+++ b/src/list-player-flags.h
43@@ -20,3 +20,4 @@ PF(KNOW_MUSHROOM,    "easily recognize mushrooms")
44 PF(KNOW_ZAPPER,      "easily recognize magic devices")
45 PF(SEE_ORE,          "can sense ore in the walls")
46 PF(FAST,             "receive one extra haste at levels 1, 13, 25, 38 and 50")
47+PF(MAGICAL,          "reduces spell failures and gives chance to reduce mana costs")
48diff --git a/src/player/spell.c b/src/player/spell.c
49index 3f3cbe8..ea36c6c 100644
50--- a/src/player/spell.c
51+++ b/src/player/spell.c
52@@ -250,6 +250,13 @@ s16b spell_chance(int spell)
53                minfail = 5;
54        }
55 
56+       /* magical race bonus (also an exception to the above, but 3% limit) */
57+       if (player_has(PF_MAGICAL))
58+       {
59+               minfail = (minfail * 2 + 1) / 3;
60+               chance = (chance * 2 + 1) / 3;
61+       }
62+
63        /* Priest prayer penalty for "edged" weapons (before minfail) */
64        if (p_ptr->state.icky_wield)
65        {
66@@ -339,16 +346,18 @@ void spell_learn(int spell)
67 /* Cas the specified spell */
68 bool spell_cast(int spell, int dir)
69 {
70-       int chance;
71+       /* Spell failure chance */
72+       int fail_chance = spell_chance(spell);
73 
74        /* Get the spell */
75        const magic_type *s_ptr = &p_ptr->class->spells.info[spell];
76 
77-       /* Spell failure chance */
78-       chance = spell_chance(spell);
79+       /* convert cur manna & spell cost to manna * 2^16 + fraction */
80+       u32b cmana = (p_ptr->csp << 16) + p_ptr->csp_frac;
81+       u32b smana = s_ptr->smana << 16;
82 
83        /* Failed spell */
84-       if (randint0(100) < chance)
85+       if (randint0(100) < fail_chance)
86        {
87                flush();
88                msg("You failed to concentrate hard enough!");
89@@ -376,19 +385,38 @@ bool spell_cast(int spell, int dir)
90                        /* Redraw object recall */
91                        p_ptr->redraw |= (PR_OBJECT);
92                }
93+
94+               /* Chance for magical races to use less manna, can even avoid the
95+                * over-exertion penalty. */
96+               if (player_has(PF_MAGICAL)) {
97+                       /* Chance is 10%-50% (from level 1 to 50) minus spell failure chance. */
98+                       int difficulty = 90 - (p_ptr->lev - 1) * 40 / 49 + fail_chance;
99+                       int roll = randint0(100);
100+
101+                       if (roll >= difficulty) {
102+                               int frac = rand_range(fail_chance * 0x10000 / 100, 75 * 0x10000 / 100);
103+                               int pct = frac * 100 / 0x10000;
104+                               const char *great_str = pct > 20 ? "" : " great";
105+
106+                               smana = s_ptr->smana * frac;
107+                               msg("You cast the spell with%s ease.", great_str);
108+                       }
109+               }
110        }
111 
112        /* Sufficient mana */
113-       if (s_ptr->smana <= p_ptr->csp)
114+       if (smana <= cmana)
115        {
116                /* Use some mana */
117-               p_ptr->csp -= s_ptr->smana;
118+               cmana -= smana;
119+               p_ptr->csp = cmana >> 16;
120+               p_ptr->csp_frac = cmana & 0xffff;
121        }
122 
123        /* Over-exert the player */
124        else
125        {
126-               int oops = s_ptr->smana - p_ptr->csp;
127+               int oops = smana - cmana;
128 
129                /* No mana left */
130                p_ptr->csp = 0;
131@@ -398,7 +426,7 @@ bool spell_cast(int spell, int dir)
132                msg("You faint from the effort!");
133 
134                /* Bypass free action */
135-               (void)player_inc_timed(p_ptr, TMD_PARALYZED, randint1(5 * oops + 1), TRUE, FALSE);
136+               (void)player_inc_timed(p_ptr, TMD_PARALYZED, randint1(5 * oops + 0x10000) >> 16, TRUE, FALSE);
137 
138                /* Damage CON (possibly permanently) */
139                if (randint0(100) < 50)
140diff --git a/src/ui-birth.c b/src/ui-birth.c
141index b1cfa6e..18e0871 100644
142--- a/src/ui-birth.c
143+++ b/src/ui-birth.c
144@@ -255,6 +255,7 @@ static const char *get_pflag_desc(bitflag flag)
145                case PF_KNOW_ZAPPER: return "Identifies magic devices";
146                case PF_SEE_ORE: return "Senses ore/minerals";
147                case PF_FAST: return "Gains speed bonus";
148+               case PF_MAGICAL: return "Spells fail less and sometimes use less mana";
149                default: return "Undocumented pflag";
150        }
151 }
152--
1531.7.10.4
154