summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/skills.cc
blob: 59afd9ce3cfe0ee6c0be025917796ad63e901354 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 *  File:       skills.cc
 *  Summary:    Skill exercising functions.
 *  Written by: Linley Henzell
 */

#include "AppHdr.h"

#include "skills.h"

#include <algorithm>
#include <string.h>
#include <stdlib.h>

#include "externs.h"

#include "itemprop.h"
#include "notes.h"
#include "output.h"
#include "player.h"
#include "random.h"
#include "skills2.h"
#include "spl-cast.h"
#include "tutorial.h"


// MAX_COST_LIMIT is the maximum XP amount it will cost to raise a skill
//                by 10 skill points (ie one standard practice).
//
// MAX_SPENDING_LIMIT is the maximum XP amount we allow the player to
//                    spend on a skill in a single raise.
//
// Note that they don't have to be equal, but it is important to make
// sure that they're set so that the spending limit will always allow
// for 1 skill point to be earned.
#define MAX_COST_LIMIT           250
#define MAX_SPENDING_LIMIT       250

static int _exercise2( int exsk );

// These values were calculated by running a simulation of gaining skills.
// The goal is to try and match the old cost system which used the player's
// experience level (which has a number of problems) so things shouldn't
// seem too different to the player... but we still try to err on the
// high side for the lower levels. -- bwr
int skill_cost_needed( int level )
{
    // The average starting skill total is actually lower, but
    // some classes get about 2200, and they would probably be
    // start around skill cost level 3 if we used the average.  -- bwr
    int ret = 2200;

    switch (level)
    {
    case 1: ret = 0; break;

    case 2:  ret +=   250; break; //  250 -- big because of initial 25 pool
    case 3:  ret +=   350; break; //  100
    case 4:  ret +=   550; break; //  200
    case 5:  ret +=   900; break; //  350
    case 6:  ret +=  1300; break; //  400
    case 7:  ret +=  1900; break; //  600
    case 8:  ret +=  2800; break; //  900
    case 9:  ret +=  4200; break; // 1400
    case 10: ret +=  5900; break; // 1700
    case 11: ret +=  9000; break; // 3100

    default:
        ret += 9000 + (4000 * (level - 11));
        break;
    }

    return (ret);
}

void calc_total_skill_points( void )
{
    int i;

    you.total_skill_points = 0;

    for (i = 0; i < NUM_SKILLS; i++)
        you.total_skill_points += you.skill_points[i];

    for (i = 1; i <= 27; i++)
        if (you.total_skill_points < skill_cost_needed(i))
            break;

    you.skill_cost_level = i - 1;

#if DEBUG_DIAGNOSTICS
    you.redraw_experience = true;
#endif
}

// skill_cost_level makes skills more expensive for more experienced characters
// skill_level      makes higher skills more expensive
static int _calc_skill_cost( int skill_cost_level, int skill_level )
{
    int ret = 1 + skill_level;

    // does not yet allow for loss of skill levels.
    if (skill_level > 9)
    {
        ret *= (skill_level - 7);
        ret /= 3;
    }

    if (skill_cost_level > 4)
        ret += skill_cost_level - 4;
    if (skill_cost_level > 7)
        ret += skill_cost_level - 7;
    if (skill_cost_level > 10)
        ret += skill_cost_level - 10;
    if (skill_cost_level > 13)
        ret += skill_cost_level - 13;
    if (skill_cost_level > 16)
        ret += skill_cost_level - 16;

    if (skill_cost_level > 10)
    {
        ret *= (skill_cost_level - 5);
        ret /= 5;
    }

    if (skill_level > 7)
        ret += 1;
    if (skill_level > 9)
        ret += 2;
    if (skill_level > 11)
        ret += 3;
    if (skill_level > 13)
        ret += 4;
    if (skill_level > 15)
        ret += 5;

    if (ret > MAX_COST_LIMIT)
        ret = MAX_COST_LIMIT;

    return (ret);
}

// returns total number of skill points gained
int exercise(int exsk, int deg)
{
    int ret = 0;

    while (deg > 0)
    {
        if (you.exp_available <= 0 || you.skills[exsk] >= 27)
            break;

        if (you.practise_skill[exsk] || one_chance_in(4))
            ret += _exercise2( exsk );

        deg--;
    }

    if (ret)
        dprf("Exercised %s (deg: %d) by %d", skill_name(exsk), deg, ret);

    return (ret);
}                               // end exercise()

static int _exercise2(int exsk)
{
    int deg = 10;
    int bonus = 0;
    char old_best_skill = best_skill(SK_FIGHTING, (NUM_SKILLS - 1), 99);

    // Being good at some weapons makes others easier to learn.
    if (exsk < SK_ARMOUR)
    {
        // Short Blades and Long Blades.
        if ((exsk == SK_SHORT_BLADES || exsk == SK_LONG_BLADES)
            && (you.skills[SK_SHORT_BLADES] > you.skills[exsk]
                || you.skills[SK_LONG_BLADES] > you.skills[exsk]))
        {
            bonus += random2(30);
        }

        // Axes and Polearms.
        if ((exsk == SK_AXES || exsk == SK_POLEARMS)
            && (you.skills[SK_AXES] > you.skills[exsk]
                || you.skills[SK_POLEARMS] > you.skills[exsk]))
        {
            bonus += random2(30);
        }

        // Polearms and Staves.
        if ((exsk == SK_POLEARMS || exsk == SK_STAVES)
            && (you.skills[SK_POLEARMS] > you.skills[exsk]
                || you.skills[SK_STAVES] > you.skills[exsk]))
        {
            bonus += random2(30);
        }

        // Axes and Maces.
        if ((exsk == SK_AXES || exsk == SK_MACES_FLAILS)
            && (you.skills[SK_AXES] > you.skills[exsk]
                || you.skills[SK_MACES_FLAILS] > you.skills[exsk]))
        {
            bonus += random2(30);
        }

        // Maces and Staves.
        if ((exsk == SK_MACES_FLAILS || exsk == SK_STAVES)
            && (you.skills[SK_MACES_FLAILS] > you.skills[exsk]
                || you.skills[SK_STAVES] > you.skills[exsk]))
        {
            bonus += random2(30);
        }

        // Slings and Throwing.
        if ((exsk == SK_SLINGS || exsk == SK_THROWING)
            && (you.skills[SK_SLINGS] > you.skills[exsk]
                || you.skills[SK_THROWING] > you.skills[exsk]))
        {
            bonus += random2(30);
        }
    }

    // Quick fix for the fact that stealth can't be gained fast enough
    // to keep up with the monster levels.  This should speed its
    // advancement.
    if (exsk == SK_STEALTH)
        bonus += random2(30);

    int skill_change = _calc_skill_cost(you.skill_cost_level, you.skills[exsk]);

    // Spellcasting is cheaper early on, and elementals hinder each
    // other.
    if (exsk >= SK_SPELLCASTING)
    {
        if (you.skill_cost_level < 5)
            skill_change /= 2;
        else if (you.skill_cost_level < 15)
        {
            skill_change *= (10 + (you.skill_cost_level - 5));
            skill_change /= 20;
        }

        // Being good at elemental magic makes other elements harder to
        // learn.
        if (exsk >= SK_FIRE_MAGIC && exsk <= SK_EARTH_MAGIC
            && (you.skills[SK_FIRE_MAGIC] > you.skills[exsk]
                || you.skills[SK_ICE_MAGIC] > you.skills[exsk]
                || you.skills[SK_AIR_MAGIC] > you.skills[exsk]
                || you.skills[SK_EARTH_MAGIC] > you.skills[exsk]))
        {
            if (one_chance_in(3))
                return (0);
        }

        // Some are direct opposites.
        if ((exsk == SK_FIRE_MAGIC || exsk == SK_ICE_MAGIC)
            && (you.skills[SK_FIRE_MAGIC] > you.skills[exsk]
                || you.skills[SK_ICE_MAGIC] > you.skills[exsk]))
        {
            // Of course, this is cumulative with the one above.
            if (!one_chance_in(3))
                return (0);
        }

        if ((exsk == SK_AIR_MAGIC || exsk == SK_EARTH_MAGIC)
            && (you.skills[SK_AIR_MAGIC] > you.skills[exsk]
                || you.skills[SK_EARTH_MAGIC] > you.skills[exsk]))
        {
            if (!one_chance_in(3))
                return (0);
        }

        // Experimental restriction (too many spell schools). -- bwr
        int skill_rank = 1;

        for (int i = SK_CONJURATIONS; i < SK_FIRE_MAGIC; ++i)
        {
            if (you.skills[exsk] < you.skills[i])
                skill_rank++;
        }

        // Things get progressively harder, but not harder than
        // the Fire-Air or Ice-Earth level.
        if (skill_rank > 3 && one_chance_in(10 - skill_rank))
            return (0);
    }

    int spending_limit = std::min(MAX_SPENDING_LIMIT, you.exp_available);

    // Handle fractional learning.
    if (skill_change > spending_limit)
    {
        // This system is a bit hard on missile weapons in the late
        // game, since they require expendable ammo in order to
        // practise.  Increasing the "deg"ree of exercise would make
        // missile weapons too easy earlier on, so, instead, we're
        // giving them a special case here.
        if (exsk != SK_THROWING && exsk != SK_BOWS && exsk != SK_CROSSBOWS
            || skill_change > you.exp_available)
        {
            int fraction = (spending_limit * 10) / skill_change;

            deg = (deg * fraction) / 10;

            if (deg == 0)
                bonus = (bonus * fraction) / 10;
        }
        else
            deg = ((skill_change / 2) > MAX_SPENDING_LIMIT) ? 5 : 10;

        skill_change = spending_limit;
    }

    skill_change -= random2(5);

    if (skill_change <= 0)
    {
        // No free lunch.  This is a problem now that we don't have
        // overspending.
        skill_change = (deg > 0 || bonus > 0) ? 1 : 0;
    }

    // We can safely return at any stage before this.
    int skill_inc = deg + bonus;

    // Starting to learn skills is easier if the appropriate stat is
    // high.
    if (you.skills[exsk] == 0)
    {
        if ((exsk >= SK_FIGHTING && exsk <= SK_STAVES) || exsk == SK_ARMOUR)
        {
            // These skills are easier for the strong.
            skill_inc *= std::max<int>(5, you.strength);
            skill_inc /= 10;
        }
        else if (exsk >= SK_SLINGS && exsk <= SK_UNARMED_COMBAT)
        {
            // These skills are easier for the dexterous.
            // Note: Armour is handled above.
            skill_inc *= std::max<int>(5, you.dex);
            skill_inc /= 10;
        }
        else if (exsk >= SK_SPELLCASTING && exsk <= SK_POISON_MAGIC)
        {
            // These skills are easier for the smart.
            skill_inc *= std::max<int>(5, you.intel);
            skill_inc /= 10;
        }
    }

    if (skill_inc <= 0)
        return (0);

    you.skill_points[exsk] += skill_inc;
    you.exp_available -= skill_change;

    you.total_skill_points += skill_inc;
    if (you.skill_cost_level < 27
        && you.total_skill_points
               >= skill_cost_needed(you.skill_cost_level + 1))
    {
        you.skill_cost_level++;
    }

    you.exp_available = std::max(0, you.exp_available);

    you.redraw_experience = true;

/*
    New (LH): debugging bit: when you exercise a skill, displays the skill
    exercised and how much you spent on it. Too irritating to be a regular
    WIZARD feature.


#if DEBUG_DIAGNOSTICS
    mprf( MSGCH_DIAGNOSTICS, "Exercised %s * %d for %d xp.",
          skill_name(exsk), skill_inc, skill_change );
#endif
*/

    if (you.skill_points[exsk] >
           (skill_exp_needed(you.skills[exsk] + 1)
            * species_skills(exsk, you.species) / 100))
    {

        you.skills[exsk]++;
        take_note(Note(NOTE_GAIN_SKILL, exsk, you.skills[exsk]));

        if (you.skills[exsk] == 27)
        {
            mprf(MSGCH_INTRINSIC_GAIN,
                 "You have mastered %s!", skill_name( exsk ) );
        }
        else if (you.skills[exsk] == 1)
        {
            mprf(MSGCH_INTRINSIC_GAIN,
                 "You have gained %s skill!", skill_name( exsk ) );

            tut_gained_new_skill(exsk);
        }
        else
        {
            mprf(MSGCH_INTRINSIC_GAIN, "Your %s skill increases to level %d!",
                 skill_name( exsk ), you.skills[exsk] );
        }

        learned_something_new(TUT_SKILL_RAISE);

        // Recalculate this skill's order for tie breaking skills
        // at its new level.   See skills2.cc::init_skill_order()
        // for more details.  -- bwr
        you.skill_order[exsk] = 0;

        for (int i = SK_FIGHTING; i < NUM_SKILLS; ++i)
        {
            if (i == exsk)
                continue;

            if (you.skills[i] >= you.skills[exsk])
                you.skill_order[exsk]++;
        }

        if (exsk == SK_FIGHTING)
            calc_hp();

        if (exsk == SK_INVOCATIONS || exsk == SK_SPELLCASTING)
            calc_mp();

        if (exsk == SK_DODGING || exsk == SK_ARMOUR)
            you.redraw_evasion = true;

        if (exsk == SK_ARMOUR || exsk == SK_SHIELDS
            || exsk == SK_ICE_MAGIC || exsk == SK_EARTH_MAGIC
            || you.duration[DUR_TRANSFORMATION] > 0)
        {
            you.redraw_armour_class = true;
        }

        const unsigned char best = best_skill(SK_FIGHTING,
                                              (NUM_SKILLS - 1), 99);

        const unsigned char best_spell = best_skill(SK_SPELLCASTING,
                                                    SK_POISON_MAGIC, 99);

        if (exsk == SK_SPELLCASTING
            && you.skills[exsk] == 1 && best_spell == SK_SPELLCASTING)
        {
            mpr("You're starting to get the hang of this magic thing.");
        }

        if (best != old_best_skill || old_best_skill == exsk)
            redraw_skill(you.your_name, player_title());

        if (you.weapon() && item_is_staff(*you.weapon()))
            maybe_identify_staff(*you.weapon());
    }

    return (skill_inc);
}