forked from crawl/crawl
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathacquire.cc
1645 lines (1442 loc) · 52.7 KB
/
acquire.cc
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
* @brief Acquirement and Trog/Oka/Sif gifts.
**/
#include "AppHdr.h"
#include "acquire.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include "artefact.h"
#include "art-enum.h"
#include "decks.h"
#include "dungeon.h"
#include "food.h"
#include "goditem.h"
#include "itemname.h"
#include "itemprop.h"
#include "items.h"
#include "item_use.h"
#include "libutil.h"
#include "macro.h"
#include "message.h"
#include "output.h"
#include "random-weight.h"
#include "religion.h"
#include "skills.h"
#include "spl-book.h"
#include "spl-util.h"
#include "state.h"
#include "stringutil.h"
#include "terrain.h"
static equipment_type _acquirement_armour_slot(bool);
static armour_type _acquirement_armour_for_slot(equipment_type, bool);
static armour_type _acquirement_shield_type();
static armour_type _acquirement_body_armour(bool);
static armour_type _useless_armour_type();
/**
* Choose a random subtype of armour to generate through acquirement/divine
* gifts.
*
* Guaranteed to be wearable, in principle.
*
* @param divine Lowers the odds of high-tier body armours being chosen.
* @return The armour_type of the armour to be generated.
*/
static int _acquirement_armour_subtype(bool divine, int & /*quantity*/)
{
const equipment_type slot_type = _acquirement_armour_slot(divine);
return _acquirement_armour_for_slot(slot_type, divine);
}
/**
* Choose a random slot to acquire armour for.
*
* For most races, even odds for all armour slots when acquiring, or 50-50
* split between body armour/aux armour when getting god gifts.
*
* Centaurs & nagas get a high extra chance for bardings, especially if they
* haven't seen any yet.
*
* Guaranteed to be wearable, in principle.
*
* @param divine Whether the item is a god gift.
* @return A random equipment slot; e.g. EQ_SHIELD, EQ_BODY_ARMOUR...
*/
static equipment_type _acquirement_armour_slot(bool divine)
{
if (you.species == SP_NAGA || you.species == SP_CENTAUR)
{
const armour_type bard =
(you.species == SP_NAGA) ? ARM_NAGA_BARDING
: ARM_CENTAUR_BARDING;
if (one_chance_in(you.seen_armour[bard] ? 4 : 2))
return EQ_BOOTS;
}
vector<pair<equipment_type, int>> weights = {
{ EQ_BODY_ARMOUR, divine ? 5 : 1 },
{ EQ_SHIELD, 1 },
{ EQ_CLOAK, 1 },
{ EQ_HELMET, 1 },
{ EQ_GLOVES, 1 },
{ EQ_BOOTS, 1 },
};
for (auto &weight : weights)
if (!you_can_wear(weight.first))
weight.second = 0;
const equipment_type* slot = random_choose_weighted(weights);
ASSERT(slot);
return *slot;
}
/**
* Choose a random subtype of armour that will fit in the given equipment slot,
* to generate through acquirement/divine gifts.
*
* Guaranteed to be usable by the player & weighted weakly by their skills;
* heavy investment in armour skill, relative to dodging & spellcasting, makes
* heavier armours more likely to be generated.
*
* @param divine Whether the armour is a god gift.
* @return The armour_type of the armour to be generated.
*/
static armour_type _acquirement_armour_for_slot(equipment_type slot_type,
bool divine)
{
switch (slot_type)
{
case EQ_CLOAK:
return ARM_CLOAK;
case EQ_GLOVES:
return ARM_GLOVES;
case EQ_BOOTS:
switch (you.species)
{
case SP_NAGA:
return ARM_NAGA_BARDING;
case SP_CENTAUR:
return ARM_CENTAUR_BARDING;
default:
return ARM_BOOTS;
}
case EQ_HELMET:
if (you_can_wear(EQ_HELMET) == MB_MAYBE)
return coinflip() ? ARM_HELMET : ARM_HAT;
return ARM_HAT;
case EQ_SHIELD:
return _acquirement_shield_type();
case EQ_BODY_ARMOUR:
return _acquirement_body_armour(divine);
default:
die("Unknown armour slot %d!", slot_type);
}
}
/**
* Choose a random type of shield to be generated via acquirement or god gifts.
*
* Weighted by Shields skill & the secret racial shield bonus.
*
* Ratios by shields skill & player size (B = buckler, S = shield, L = lg. sh.)
*
* Shields 0 5 10 15 20
* Large: {6B}/5S/4L ~{1B}/1S/1L ~{1B}/5S/7L ~2S/3L 1S/2L
* Med.: 2B/1S 6B/4S/1L 2B/2S/1L 4B/8S/3L 1S/1L
* Small: ~3B/1S ~5B/2S ~2B/1S ~3B/2S ~1B/1S
*
* XXX: possibly shield skill should count for more for non-med races?
*
* @return A potentially wearable type of shield.
*/
static armour_type _acquirement_shield_type()
{
vector<pair<armour_type, int>> weights = {
{ ARM_BUCKLER, player_shield_racial_factor() * 4
- you.skills[SK_SHIELDS] },
{ ARM_SHIELD, 10 },
{ ARM_LARGE_SHIELD, 20 - player_shield_racial_factor() * 4
+ div_rand_round(you.skills[SK_SHIELDS], 2) },
};
for (auto &weight : weights)
{
if (!check_armour_size(weight.first, you.body_size(PSIZE_TORSO, true)))
weight.second = 0;
weight.second = max(weight.second, 0);
}
const armour_type* shield_type = random_choose_weighted(weights);
ASSERT(shield_type);
return *shield_type;
}
/**
* Determine the weight (likelihood) to acquire a specific type of body armour.
*
* If divine is set, returns the base weight for the armour type.
* Otherwise, if warrior is set, multiplies the base weight by the base ac^2.
* Otherwise, uses the player's Armour skill to crudely guess how likely they
* are to want the armour, based on its EVP.
*
* @param armour The type of armour in question. (E.g. ARM_ROBE.)
* @param divine Whether the 'acquirement' is actually a god gift.
* @param warrior Whether we think the player only cares about AC.
* @return A weight for the armour.
*/
static int _body_acquirement_weight(armour_type armour,
bool divine, bool warrior)
{
const int base_weight = armour_acq_weight(armour);
if (divine)
return base_weight; // gods don't care about your skills, apparently
if (warrior)
{
const int ac = armour_prop(armour, PARM_AC);
return base_weight * ac * ac;
}
// highest chance when armour skill = (displayed) evp - 3
const int evp = armour_prop(armour, PARM_EVASION);
const int skill = min(27, you.skills[SK_ARMOUR] + 3);
const int sk_diff = skill + evp / 10;
const int inv_diff = max(1, 27 - sk_diff);
// armour closest to ideal evp is 27^3 times as likely as the furthest away
return base_weight * inv_diff * inv_diff * inv_diff;
}
/**
* Choose a random type of body armour to be generated via acquirement or
* god gifts.
*
* @param divine Whether the armour is a god gift.
* @return A potentially wearable type of body armour..
*/
static armour_type _acquirement_body_armour(bool divine)
{
// Using an arbitrary legacy formula, do we think the player doesn't care
// about armour EVP?
const bool warrior = random2(you.skills[SK_SPELLCASTING] * 3
+ you.skills[SK_DODGING])
< random2(you.skills[SK_ARMOUR] * 2);
vector<pair<armour_type, int>> weights;
for (int i = ARM_FIRST_MUNDANE_BODY; i < NUM_ARMOURS; ++i)
{
const armour_type armour = (armour_type)i;
if (get_armour_slot(armour) != EQ_BODY_ARMOUR)
continue;
if (!check_armour_size(armour, you.body_size(PSIZE_TORSO, true)))
continue;
const int weight = _body_acquirement_weight(armour, divine, warrior);
if (weight)
{
const pair<armour_type, int> weight_pair = { armour, weight };
weights.push_back(weight_pair);
}
}
const armour_type* armour_ptr = random_choose_weighted(weights);
ASSERT(armour_ptr);
return *armour_ptr;
}
/**
* Choose a random type of armour that the player cannot wear, for Xom to spite
* the player with.
*
* @return A random useless armour_type.
*/
static armour_type _useless_armour_type()
{
vector<pair<equipment_type, int>> weights = {
{ EQ_BODY_ARMOUR, 1 }, { EQ_SHIELD, 1 }, { EQ_CLOAK, 1 },
{ EQ_HELMET, 1 }, { EQ_GLOVES, 1 }, { EQ_BOOTS, 1 },
};
// everyone has some kind of boot-slot item they can't wear, regardless
// of what you_can_wear() claims
for (auto &weight : weights)
if (you_can_wear(weight.first) == MB_TRUE && weight.first != EQ_BOOTS)
weight.second = 0;
const equipment_type* slot_ptr = random_choose_weighted(weights);
const equipment_type slot = slot_ptr ? *slot_ptr : EQ_BOOTS;
switch (slot)
{
case EQ_BOOTS:
// Boots-wearers get bardings, bardings-wearers get the wrong
// barding, everyone else gets boots.
if (you_can_wear(EQ_BOOTS) == MB_TRUE)
return coinflip() ? ARM_CENTAUR_BARDING : ARM_NAGA_BARDING;
if (you.species == SP_NAGA)
return ARM_CENTAUR_BARDING;
if (you.species == SP_CENTAUR)
return ARM_NAGA_BARDING;
return ARM_BOOTS;
case EQ_GLOVES:
return ARM_GLOVES;
case EQ_HELMET:
if (you_can_wear(EQ_HELMET))
return ARM_HELMET;
return coinflip() ? ARM_HELMET : ARM_HAT;
case EQ_CLOAK:
return ARM_CLOAK;
case EQ_SHIELD:
{
vector<pair<armour_type, int>> shield_weights = {
{ ARM_BUCKLER, 1 },
{ ARM_SHIELD, 1 },
{ ARM_LARGE_SHIELD, 1 },
};
const size_type player_size = you.body_size(PSIZE_TORSO, true);
// XXX: export this idiom ^ v
for (auto &weight : shield_weights)
if (check_armour_size(weight.first, player_size))
weight.second = 0;
const armour_type* shield_type
= random_choose_weighted(shield_weights);
ASSERT(shield_type);
return *shield_type;
}
case EQ_BODY_ARMOUR:
// only the rarest & most precious of unwearable armours for Xom
if (you_can_wear(EQ_BODY_ARMOUR))
return ARM_CRYSTAL_PLATE_ARMOUR;
// arbitrary selection of [unwearable] dragon armours
return random_choose(ARM_FIRE_DRAGON_ARMOUR,
ARM_ICE_DRAGON_ARMOUR,
ARM_PEARL_DRAGON_ARMOUR,
ARM_GOLD_DRAGON_ARMOUR,
ARM_SHADOW_DRAGON_ARMOUR,
ARM_STORM_DRAGON_ARMOUR);
default:
die("Unknown slot type selected for Xom bad-armour-acq!");
}
}
static armour_type _pick_unseen_armour()
{
// Consider shields uninteresting always, since unlike with other slots
// players might well prefer an empty slot to wearing one. We don't
// want to try to guess at this by looking at their weapon's handedness
// because this would encourage switching weapons or putting on a
// shield right before reading acquirement in some cases. --elliptic
// This affects only the "unfilled slot" special-case, not regular
// acquirement which can always produce (wearable) shields.
static const equipment_type armour_slots[] =
{ EQ_CLOAK, EQ_HELMET, EQ_GLOVES, EQ_BOOTS };
armour_type picked = NUM_ARMOURS;
int count = 0;
for (auto &slot : armour_slots)
{
if (!you_can_wear(slot))
continue;
const armour_type sub_type = _acquirement_armour_for_slot(slot, false);
ASSERT(sub_type != NUM_ARMOURS);
if (!you.seen_armour[sub_type] && one_chance_in(++count))
picked = sub_type;
}
return picked;
}
static int _acquirement_food_subtype(bool /*divine*/, int& quantity)
{
int type_wanted;
// Food is a little less predictable now. - bwr
if (you.species == SP_GHOUL)
type_wanted = FOOD_CHUNK;
else if (you.species == SP_VAMPIRE)
{
// Vampires really don't want any OBJ_FOOD but OBJ_CORPSES
// but it's easier to just give them a potion of blood
// class type is set elsewhere
type_wanted = POT_BLOOD;
}
else if (you_worship(GOD_FEDHAS))
{
// Fedhas worshippers get fruit to use for growth and evolution
type_wanted = FOOD_FRUIT;
}
else
{
type_wanted = coinflip()
? FOOD_ROYAL_JELLY
: player_mutation_level(MUT_HERBIVOROUS) ? FOOD_BREAD_RATION
: FOOD_MEAT_RATION;
}
quantity = 3 + random2(5);
// giving more of the lower food value items
if (type_wanted == FOOD_FRUIT)
quantity = 8 + random2avg(15, 2);
else if (type_wanted == FOOD_ROYAL_JELLY || type_wanted == FOOD_CHUNK)
quantity += 2 + random2avg(10, 2);
else if (type_wanted == POT_BLOOD)
quantity = 8 + random2(5);
return type_wanted;
}
static int _acquirement_weapon_subtype(bool divine, int & /*quantity*/)
{
// Asking for a weapon is biased towards your skills.
// First pick a skill, weighting towards those you have.
int count = 0;
skill_type skill = SK_FIGHTING;
int best_sk = 0;
for (int i = SK_FIRST_WEAPON; i <= SK_LAST_WEAPON; i++)
if (you.skills[i] > best_sk)
best_sk = you.skills[i];
for (skill_type sk = SK_FIRST_WEAPON; sk <= SK_LAST_WEAPON; ++sk)
{
// Adding a small constant allows for the occasional
// weapon in an untrained skill.
int weight = you.skills[sk] + 1;
// Exaggerate the weighting if it's a scroll acquirement.
if (!divine)
weight = (weight + 1) * (weight + 2);
count += weight;
if (x_chance_in_y(weight, count))
skill = sk;
}
if (you.skills[SK_UNARMED_COMBAT] > best_sk)
best_sk = you.skills[SK_UNARMED_COMBAT];
// Now choose a subtype which uses that skill.
int result = OBJ_RANDOM;
count = 0;
item_def item_considered;
item_considered.base_type = OBJ_WEAPONS;
// Let's guess the percentage of shield use the player did, this is
// based on empirical data where pure-shield MDs get skills like 17 sh
// 25 m&f and pure-shield Spriggans 7 sh 18 m&f. Pretend formicid
// shield skill is 0 so they always weight towards 2H.
const int shield_sk = you.species == SP_FORMICID
? 0
: you.skills[SK_SHIELDS] * species_apt_factor(SK_SHIELDS);
const int want_shield = min(2 * shield_sk, best_sk) + 10;
const int dont_shield = max(best_sk - shield_sk, 0) + 10;
// At XL 10, weapons of the handedness you want get weight *2, those of
// opposite handedness 1/2, assuming your shields usage is respectively
// 0% or 100% in the above formula. At skill 25 that's *3.5 .
for (int i = 0; i < NUM_WEAPONS; ++i)
{
const int wskill = item_attack_skill(OBJ_WEAPONS, i);
if (wskill != skill)
continue;
item_considered.sub_type = i;
// Can't get blessed weapons through acquirement, only from TSO
if (is_blessed(item_considered))
continue;
int acqweight = property(item_considered, PWPN_ACQ_WEIGHT) * 100;
if (!acqweight)
continue;
const bool two_handed = you.hands_reqd(item_considered) == HANDS_TWO;
if (two_handed && player_mutation_level(MUT_MISSING_HAND))
continue;
// For non-Trog/Okawaru acquirements, give a boost to high-end items.
if (!divine && !is_range_weapon(item_considered))
{
if (acqweight < 500)
acqweight = 500;
// Quick blades get unproportionately hit by damage weighting.
if (i == WPN_QUICK_BLADE)
acqweight = acqweight * 25 / 9;
int damage = property(item_considered, PWPN_DAMAGE);
if (!two_handed)
damage = damage * 3 / 2;
damage *= damage * damage;
acqweight *= damage / property(item_considered, PWPN_SPEED);
}
if (two_handed)
acqweight = acqweight * dont_shield / want_shield;
else
acqweight = acqweight * want_shield / dont_shield;
if (!you.seen_weapon[i])
acqweight *= 5; // strong emphasis on type variety, brands go only second
if (x_chance_in_y(acqweight, count += acqweight))
result = i;
}
return result;
}
static int _acquirement_missile_subtype(bool /*divine*/, int & /*quantity*/)
{
int count = 0;
int skill = SK_THROWING;
for (int i = SK_SLINGS; i <= SK_THROWING; i++)
{
if (you.skills[i])
{
count += you.skills[i];
if (x_chance_in_y(you.skills[i], count))
skill = i;
}
}
missile_type result = MI_TOMAHAWK;
switch (skill)
{
case SK_SLINGS: result = MI_SLING_BULLET; break;
case SK_BOWS: result = MI_ARROW; break;
case SK_CROSSBOWS: result = MI_BOLT; break;
case SK_THROWING:
{
// Choose from among all usable missile types.
vector<pair<missile_type, int> > missile_weights;
missile_weights.emplace_back(MI_TOMAHAWK, 50);
missile_weights.emplace_back(MI_NEEDLE, 75);
if (you.body_size() >= SIZE_MEDIUM)
missile_weights.emplace_back(MI_JAVELIN, 100);
if (you.can_throw_large_rocks())
missile_weights.emplace_back(MI_LARGE_ROCK, 100);
result = *random_choose_weighted(missile_weights);
}
break;
default:
break;
}
return result;
}
static int _acquirement_jewellery_subtype(bool /*divine*/, int & /*quantity*/)
{
int result = 0;
// Rings are (number of usable rings) times as common as amulets.
// XXX: unify this with the actual check for ring slots
const int ring_num = (you.species == SP_OCTOPODE ? 8 : 2)
- (player_mutation_level(MUT_MISSING_HAND) ? 1 : 0);
// Try ten times to give something the player hasn't seen.
for (int i = 0; i < 10; i++)
{
result = one_chance_in(ring_num + 1) ? get_random_amulet_type()
: get_random_ring_type();
// If we haven't seen this yet, we're done.
if (!get_ident_type(OBJ_JEWELLERY, result))
break;
}
return result;
}
static bool _want_rod()
{
// First look at skills to determine whether the player gets a rod.
int spell_skills = 0;
for (int i = SK_SPELLCASTING; i <= SK_LAST_MAGIC; i++)
spell_skills += you.skills[i];
return random2(spell_skills) < you.skills[SK_EVOCATIONS] + 3
&& !one_chance_in(5);
}
static int _acquirement_staff_subtype(bool /*divine*/, int & /*quantity*/)
{
// Try to pick an enhancer staff matching the player's best skill.
skill_type best_spell_skill = best_skill(SK_SPELLCASTING, SK_EVOCATIONS);
bool found_enhancer = false;
int result = 0;
do
{
result = random2(NUM_STAVES);
}
while (item_type_removed(OBJ_STAVES, result));
#define TRY_GIVE(x) { if (!you.type_ids[OBJ_STAVES][x]) \
{result = x; found_enhancer = true;} }
switch (best_spell_skill)
{
case SK_FIRE_MAGIC: TRY_GIVE(STAFF_FIRE); break;
case SK_ICE_MAGIC: TRY_GIVE(STAFF_COLD); break;
case SK_AIR_MAGIC: TRY_GIVE(STAFF_AIR); break;
case SK_EARTH_MAGIC: TRY_GIVE(STAFF_EARTH); break;
case SK_POISON_MAGIC: TRY_GIVE(STAFF_POISON); break;
case SK_NECROMANCY: TRY_GIVE(STAFF_DEATH); break;
case SK_CONJURATIONS: TRY_GIVE(STAFF_CONJURATION); break;
case SK_SUMMONINGS: TRY_GIVE(STAFF_SUMMONING); break;
default: break;
}
if (one_chance_in(found_enhancer ? 2 : 3))
return result;
// Otherwise pick a non-enhancer staff.
switch (random2(5))
{
case 0: case 1: result = STAFF_WIZARDRY; break;
case 2: case 3: result = STAFF_ENERGY; break;
case 4: result = STAFF_POWER; break;
}
switch (random2(5))
{
case 0: case 1: TRY_GIVE(STAFF_WIZARDRY); break;
case 2: case 3: TRY_GIVE(STAFF_ENERGY); break;
case 4: TRY_GIVE(STAFF_POWER); break;
#undef TRY_GIVE
}
return result;
}
static int _acquirement_rod_subtype(bool /*divine*/, int & /*quantity*/)
{
int result;
do
{
result = random2(NUM_RODS);
}
while (player_mutation_level(MUT_NO_LOVE)
&& (result == ROD_SWARM || result == ROD_SHADOWS)
|| item_type_removed(OBJ_RODS, result));
return result;
}
/**
* Return a miscellaneous evokable item for acquirement.
* @return The item type chosen.
*/
static int _acquirement_misc_subtype(bool /*divine*/, int & /*quantity*/)
{
// Total weight if none have been seen is 100.
int result = random_choose_weighted( // Decks given lowest weight.
1, MISC_DECK_OF_WONDERS,
2, MISC_DECK_OF_CHANGES,
2, MISC_DECK_OF_DEFENCE,
// These have charges, so
// give them a constant
// weight.
(player_mutation_level(MUT_NO_LOVE) ? 0 : 7), MISC_BOX_OF_BEASTS,
(player_mutation_level(MUT_NO_LOVE) ? 0 : 7), MISC_SACK_OF_SPIDERS,
(player_mutation_level(MUT_NO_LOVE) ? 0 : 7), MISC_PHANTOM_MIRROR,
// The player never needs
// more than one.
(you.seen_misc[MISC_DISC_OF_STORMS] ? 0 : 7), MISC_DISC_OF_STORMS,
(you.seen_misc[MISC_LAMP_OF_FIRE] ? 0 : 15), MISC_LAMP_OF_FIRE,
(you.seen_misc[MISC_PHIAL_OF_FLOODS] ? 0 : 15), MISC_PHIAL_OF_FLOODS,
(you.seen_misc[MISC_FAN_OF_GALES] ? 0 : 15), MISC_FAN_OF_GALES,
(you.seen_misc[MISC_STONE_OF_TREMORS] ? 0 : 15), MISC_STONE_OF_TREMORS,
(you.seen_misc[MISC_LANTERN_OF_SHADOWS] ? 0 : 7), MISC_LANTERN_OF_SHADOWS,
0);
// Give a crystal ball based on both evocations and either spellcasting or
// invocations if we haven't seen one.
int skills = you.skills[SK_EVOCATIONS]
* max(you.skills[SK_SPELLCASTING], you.skills[SK_INVOCATIONS]);
if (x_chance_in_y(skills, MAX_SKILL_LEVEL * MAX_SKILL_LEVEL)
&& !you.seen_misc[MISC_CRYSTAL_BALL_OF_ENERGY])
{
result = MISC_CRYSTAL_BALL_OF_ENERGY;
}
return result;
}
/**
* What weight should wands of Heal Wounds be given in wand acquirement, based
* on their utility to the player? (More utile -> higher weight -> more likely)
*/
static int _hw_wand_weight()
{
if (you.innate_mutation[MUT_NO_DEVICE_HEAL] != 3)
return 25; // quite powerful
if (!player_mutation_level(MUT_NO_LOVE))
return 5; // can be used on allies...? XXX: should be weight 1?
return 0; // with no allies, totally useless
}
/**
* What weight should wands of Haste be given in wand acquirement, based on
* their utility to the player? (More utile -> higher weight -> more likely)
*/
static int _haste_wand_weight()
{
if (you.species != SP_FORMICID)
return 25; // quite powerful
if (!player_mutation_level(MUT_NO_LOVE))
return 5; // can be used on allies...? XXX: should be weight 1?
return 0; // with no allies, totally useless
}
/**
* What weight should wands of Teleportation be given in wand acquirement,
* based on their utility to the player? (More utile -> higher weight -> more
* likely)
*/
static int _tele_wand_weight()
{
if (you.species == SP_FORMICID || crawl_state.game_is_sprint())
return 1; // can only be used to tele away enemies
return 15;
}
/**
* Choose a random type of wand to be generated via acquirement or god gifts.
*
* Heavily weighted toward more useful wands and wands the player hasn't yet
* seen.
*
* @return A random wand type.
*/
static int _acquirement_wand_subtype(bool /*divine*/, int & /*quantity*/)
{
vector<pair<wand_type, int>> weights = {
// normally 25
{ WAND_HEAL_WOUNDS, _hw_wand_weight() },
{ WAND_HASTING, _haste_wand_weight() },
// normally 15
{ WAND_TELEPORTATION, _tele_wand_weight() },
{ WAND_FIRE, 8 },
{ WAND_COLD, 8 },
{ WAND_LIGHTNING, 8 },
{ WAND_DRAINING, 8 },
{ WAND_INVISIBILITY, 8 },
{ WAND_FIREBALL, 8 },
{ WAND_DIGGING, 5 },
{ WAND_DISINTEGRATION, 5 },
{ WAND_POLYMORPH, 5 },
{ WAND_ENSLAVEMENT, player_mutation_level(MUT_NO_LOVE) ? 0 : 5 },
{ WAND_FLAME, 1 },
{ WAND_FROST, 1 },
{ WAND_CONFUSION, 1 },
{ WAND_PARALYSIS, 1 },
{ WAND_SLOWING, 1 },
{ WAND_MAGIC_DARTS, 1 },
{ WAND_RANDOM_EFFECTS, 1 },
};
// Unknown wands get a huge weight bonus.
for (auto &weight : weights)
if (!get_ident_type(OBJ_WANDS, weight.first))
weight.second *= 2;
const wand_type* wand = random_choose_weighted(weights);
ASSERT(wand);
return *wand;
}
typedef int (*acquirement_subtype_finder)(bool divine, int &quantity);
static const acquirement_subtype_finder _subtype_finders[] =
{
_acquirement_weapon_subtype,
_acquirement_missile_subtype,
_acquirement_armour_subtype,
_acquirement_wand_subtype,
_acquirement_food_subtype,
0, // no scrolls
_acquirement_jewellery_subtype,
_acquirement_food_subtype, // potion acquirement = food for vampires
0, // books handled elsewhere
_acquirement_staff_subtype,
0, // no, you can't acquire the orb
_acquirement_misc_subtype,
0, // no corpses
0, // gold handled elsewhere, and doesn't have subtypes anyway
_acquirement_rod_subtype,
0, // no runes either
};
static int _find_acquirement_subtype(object_class_type &class_wanted,
int &quantity, bool divine,
int agent = -1)
{
COMPILE_CHECK(ARRAYSZ(_subtype_finders) == NUM_OBJECT_CLASSES);
ASSERT(class_wanted != OBJ_RANDOM);
if (class_wanted == OBJ_ARMOUR && you.species == SP_FELID)
return OBJ_RANDOM;
int type_wanted = OBJ_RANDOM;
int useless_count = 0;
do
{
// Staves and rods have a common acquirement class.
if (class_wanted == OBJ_STAVES || class_wanted == OBJ_RODS)
class_wanted = _want_rod() ? OBJ_RODS : OBJ_STAVES;
// Vampires acquire blood, not food.
if (class_wanted == OBJ_FOOD && you.species == SP_VAMPIRE)
class_wanted = OBJ_POTIONS;
if (_subtype_finders[class_wanted])
type_wanted = (*_subtype_finders[class_wanted])(divine, quantity);
item_def dummy;
dummy.base_type = class_wanted;
dummy.sub_type = type_wanted;
dummy.plus = 1; // empty wands would be useless
dummy.flags |= ISFLAG_IDENT_MASK;
if (!is_useless_item(dummy, false) && !god_hates_item(dummy))
break;
}
while (useless_count++ < 200);
return type_wanted;
}
// The weight of a spell takes into account its disciplines' skill levels
// and the spell difficulty.
static int _spell_weight(spell_type spell)
{
ASSERT(spell != SPELL_NO_SPELL);
int weight = 0;
spschools_type disciplines = get_spell_disciplines(spell);
int count = 0;
for (const auto disc : spschools_type::range())
{
if (disciplines & disc)
{
int skill = you.skills[spell_type2skill(disc)];
weight += skill;
count++;
}
}
ASSERT(count > 0);
// Particularly difficult spells _reduce_ the overall weight.
int leveldiff = 5 - spell_difficulty(spell);
return max(0, 2 * weight/count + leveldiff);
}
// When randomly picking a book for acquirement, use the sum of the
// weights of all unknown spells in the book.
static int _book_weight(book_type book)
{
ASSERT_RANGE(book, 0, MAX_FIXED_BOOK + 1);
int total_weight = 0;
for (spell_type stype : spellbook_template(book))
{
// Skip over spells already seen.
if (you.seen_spell[stype])
continue;
total_weight += _spell_weight(stype);
}
return total_weight;
}
static bool _is_magic_skill(int skill)
{
return skill >= SK_SPELLCASTING && skill < SK_INVOCATIONS;
}
static bool _skill_useless_with_god(int skill)
{
switch (you.religion)
{
case GOD_TROG:
return _is_magic_skill(skill) || skill == SK_INVOCATIONS;
case GOD_ZIN:
case GOD_SHINING_ONE:
case GOD_ELYVILON:
return skill == SK_NECROMANCY;
case GOD_XOM:
case GOD_NEMELEX_XOBEH:
case GOD_KIKUBAAQUDGHA:
case GOD_VEHUMET:
case GOD_ASHENZARI:
case GOD_JIYVA:
case GOD_GOZAG:
case GOD_NO_GOD:
return skill == SK_INVOCATIONS;
default:
return false;
}
}
/**
* Randomly decide whether the player should get a manual from a given instance
* of book acquirement.
*
* @param agent The source of the acquirement (e.g. a god)
* @return Whether the player should get a manual from this book
* acquirement.
*/
static bool _should_acquire_manual(int agent)
{
if (agent == GOD_XOM || agent == GOD_SIF_MUNA)
return false;
int magic_weights = 0;
int other_weights = 0;
for (skill_type sk = SK_FIRST_SKILL; sk < NUM_SKILLS; ++sk)
{
const int weight = you.skills[sk];
if (_is_magic_skill(sk))
magic_weights += weight;
else
other_weights += weight;
}
if (you_worship(GOD_TROG))
magic_weights = 0;
// If someone has 25% or more magic skills, never give manuals.
// Otherwise, count magic skills double to bias against manuals
// for magic users.
return magic_weights * 3 < other_weights
&& x_chance_in_y(other_weights, 2*magic_weights + other_weights);
}
/**
* For the purposes of acquirement, does the player have any skill in magic,
*
* @return true iff the player has any nonzero magic skill AND if they do not
* worship Trog.
*/
static bool _knows_and_likes_magic()
{
if (you_worship(GOD_TROG))
return false;
for (skill_type sk = SK_FIRST_SKILL; sk < NUM_SKILLS; ++sk)
if (_is_magic_skill(sk) && you.skills[sk] >= 1)
return true;
return false;
}
/**
* Turn a given book into an acquirement-quality manual.
*
* @param book[out] The book to be turned into a manual.
* @return Whether a manual was successfully created.
*/
static bool _acquire_manual(item_def &book)
{
int weights[NUM_SKILLS];
int total_weights = 0;
const bool knows_magic = _knows_and_likes_magic();
for (skill_type sk = SK_FIRST_SKILL; sk < NUM_SKILLS; ++sk)
{
int skl = you.skills[sk];
if (skl == 27 || is_useless_skill(sk))
{
weights[sk] = 0;
continue;
}
int w = (skl < 12) ? skl + 3 : max(0, 25 - skl);
// Greatly reduce the chances of getting a manual for a skill
// you couldn't use unless you switched your religion.
if (_skill_useless_with_god(sk))
w /= 2;
// If we don't have any magic skills, make non-magic skills
// more likely.
if (!knows_magic && !_is_magic_skill(sk))
w *= 2;
weights[sk] = w;
total_weights += w;
}
// Are we too skilled to get any manuals?
if (total_weights == 0)
return false;