-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
decks.cc
2344 lines (1984 loc) · 66.9 KB
/
decks.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 Functions with decks of cards.
**/
#include "AppHdr.h"
#include "decks.h"
#include <algorithm>
#include <iostream>
#include <sstream>
#include "abyss.h"
#include "act-iter.h"
#include "artefact.h"
#include "attitude-change.h"
#include "cloud.h"
#include "coordit.h"
#include "dactions.h"
#include "database.h"
#include "directn.h"
#include "dungeon.h"
#include "english.h"
#include "evoke.h"
#include "food.h"
#include "ghost.h"
#include "god-passive.h" // passive_t::no_haste
#include "god-wrath.h"
#include "invent.h"
#include "item-prop.h"
#include "item-status-flag-type.h"
#include "items.h"
#include "item-use.h"
#include "libutil.h"
#include "macro.h"
#include "message.h"
#include "mon-cast.h"
#include "mon-clone.h"
#include "mon-death.h"
#include "mon-place.h"
#include "mon-poly.h"
#include "mon-project.h"
#include "mon-tentacle.h"
#include "mon-util.h"
#include "mutation.h"
#include "nearby-danger.h"
#include "notes.h"
#include "output.h"
#include "player-equip.h"
#include "player-stats.h"
#include "potion.h"
#include "prompt.h"
#include "random.h"
#include "religion.h"
#include "scroller.h"
#include "spl-clouds.h"
#include "spl-goditem.h"
#include "spl-miscast.h"
#include "spl-monench.h"
#include "spl-other.h"
#include "spl-selfench.h"
#include "spl-summoning.h"
#include "spl-transloc.h"
#include "spl-wpnench.h"
#include "state.h"
#include "stringutil.h"
#include "teleport.h"
#include "terrain.h"
#include "transform.h"
#include "traps.h"
#include "uncancel.h"
#include "view.h"
#include "xom.h"
using namespace ui;
// For information on deck structure, reference the comment near the beginning
#define STACKED_KEY "stacked"
// 'deck is stacked' prop
static void _deck_ident(item_def& deck);
struct card_with_weights
{
card_type card;
int weight[3];
};
typedef vector<card_with_weights> deck_archetype;
deck_archetype deck_of_escape =
{
{ CARD_TOMB, {5, 5, 5} },
{ CARD_EXILE, {0, 1, 2} },
{ CARD_ELIXIR, {5, 5, 5} },
{ CARD_CLOUD, {5, 5, 5} },
{ CARD_VELOCITY, {5, 5, 5} },
{ CARD_SHAFT, {5, 5, 5} },
};
deck_archetype deck_of_destruction =
{
{ CARD_VITRIOL, {5, 5, 5} },
{ CARD_STORM, {5, 5, 5} },
{ CARD_PAIN, {5, 5, 3} },
{ CARD_ORB, {5, 5, 5} },
{ CARD_DEGEN, {3, 3, 3} },
{ CARD_WILD_MAGIC, {5, 5, 3} },
};
deck_archetype deck_of_summoning =
{
{ CARD_ELEMENTS, {5, 5, 5} },
{ CARD_SUMMON_DEMON, {5, 5, 5} },
{ CARD_SUMMON_WEAPON, {5, 5, 5} },
{ CARD_SUMMON_FLYING, {5, 5, 5} },
{ CARD_RANGERS, {5, 5, 5} },
{ CARD_ILLUSION, {5, 5, 5} },
};
deck_archetype deck_of_punishment =
{
{ CARD_WRAITH, {5, 5, 5} },
{ CARD_WRATH, {5, 5, 5} },
{ CARD_FAMINE, {5, 5, 5} },
{ CARD_SWINE, {5, 5, 5} },
{ CARD_TORMENT, {5, 5, 5} },
};
#if TAG_MAJOR_VERSION == 34
deck_archetype removed_deck =
{
{ CARD_XOM, {5, 5, 5} },
};
#endif
struct deck_type_data
{
/// The name of the deck. (Doesn't include "deck of ".)
string name;
/// The list of cards this deck contains.
const deck_archetype* cards;
};
static map<misc_item_type, deck_type_data> all_decks =
{
{ MISC_DECK_OF_ESCAPE, {
"escape", &deck_of_escape,
} },
{ MISC_DECK_OF_DESTRUCTION, {
"destruction", &deck_of_destruction,
} },
#if TAG_MAJOR_VERSION == 34
{ MISC_DECK_OF_DUNGEONS, {
"dungeons", &removed_deck,
} },
#endif
{ MISC_DECK_OF_SUMMONING, {
"summoning", &deck_of_summoning,
} },
#if TAG_MAJOR_VERSION == 34
{ MISC_DECK_OF_WONDERS, {
"wonders", &removed_deck,
} },
{ MISC_DECK_OF_ODDITIES, {
"oddities", &removed_deck,
} },
#endif
{ MISC_DECK_OF_PUNISHMENT, {
"punishment", &deck_of_punishment,
} },
#if TAG_MAJOR_VERSION == 34
{ MISC_DECK_OF_WAR, {
"war", &removed_deck,
} },
{ MISC_DECK_OF_CHANGES, {
"changes", &removed_deck,
} },
{ MISC_DECK_OF_DEFENCE, {
"defence", &removed_deck,
} },
#endif
};
int cards_in_deck(const item_def &deck)
{
ASSERT(is_deck(deck));
const CrawlHashTable &props = deck.props;
ASSERT(props.exists(CARD_KEY));
return props[CARD_KEY].get_vector().size();
}
card_type get_card_and_flags(const item_def& deck, int idx,
uint8_t& _flags)
{
const CrawlHashTable &props = deck.props;
const CrawlVector &cards = props[CARD_KEY].get_vector();
const CrawlVector &flags = props[CARD_FLAG_KEY].get_vector();
// Negative idx means read from the end.
if (idx < 0)
idx += static_cast<int>(cards.size());
_flags = (uint8_t) flags[idx].get_byte();
return static_cast<card_type>(cards[idx].get_byte());
}
static void _set_card_and_flags(item_def& deck, int idx, card_type card,
uint8_t _flags)
{
CrawlHashTable &props = deck.props;
CrawlVector &cards = props[CARD_KEY].get_vector();
CrawlVector &flags = props[CARD_FLAG_KEY].get_vector();
if (idx < 0)
idx = static_cast<int>(cards.size()) + idx;
cards[idx].get_byte() = card;
flags[idx].get_byte() = _flags;
}
const char* card_name(card_type card)
{
switch (card)
{
case CARD_VELOCITY: return "Velocity";
case CARD_EXILE: return "Exile";
case CARD_ELIXIR: return "the Elixir";
case CARD_STAIRS: return "the Stairs";
case CARD_TOMB: return "the Tomb";
case CARD_WILD_MAGIC: return "Wild Magic";
case CARD_ELEMENTS: return "the Elements";
case CARD_SUMMON_DEMON: return "the Pentagram";
case CARD_SUMMON_WEAPON: return "the Dance";
case CARD_SUMMON_FLYING: return "Foxfire";
case CARD_RANGERS: return "the Rangers";
case CARD_SHAFT: return "the Shaft";
case CARD_VITRIOL: return "Vitriol";
case CARD_CLOUD: return "the Cloud";
case CARD_STORM: return "the Storm";
case CARD_PAIN: return "Pain";
case CARD_TORMENT: return "Torment";
case CARD_WRATH: return "Wrath";
case CARD_WRAITH: return "the Wraith";
case CARD_SWINE: return "the Swine";
case CARD_ORB: return "the Orb";
case CARD_ILLUSION: return "the Illusion";
case CARD_DEGEN: return "Degeneration";
case CARD_FAMINE: return "Famine";
#if TAG_MAJOR_VERSION == 34
// Removed cards.
case CARD_MERCENARY: return "the Mercenary";
case CARD_ALCHEMIST: return "the Alchemist";
case CARD_CURSE: return "the Curse";
case CARD_VENOM: return "Venom";
case CARD_FORTITUDE: return "Fortitude";
case CARD_HAMMER: return "the Hammer";
case CARD_XOM: return "Xom";
case CARD_FEAST: return "the Feast";
case CARD_WARPWRIGHT: return "Warpwright";
case CARD_SUMMON_UGLY: return "Repulsiveness";
case CARD_PLACID_MAGIC: return "Placid Magic";
case CARD_CRUSADE: return "the Crusade";
case CARD_HELM: return "the Helm";
case CARD_BLADE: return "the Blade";
case CARD_SHADOW: return "the Shadow";
case CARD_POTION: return "the Potion";
case CARD_FOCUS: return "Focus";
case CARD_HELIX: return "the Helix";
case CARD_DOWSING: return "Dowsing";
case CARD_BANSHEE: return "the Banshee";
case CARD_SOLITUDE: return "Solitude";
case CARD_PORTAL: return "the Portal";
case CARD_WARP: return "the Warp";
case CARD_BATTLELUST: return "Battlelust";
case CARD_METAMORPHOSIS: return "Metamorphosis";
case CARD_SHUFFLE: return "Shuffle";
case CARD_EXPERIENCE: return "Experience";
case CARD_SAGE: return "the Sage";
case CARD_TROWEL: return "the Trowel";
case CARD_MINEFIELD: return "the Minefield";
case CARD_GENIE: return "the Genie";
case CARD_GLASS: return "Vitrification";
case CARD_BARGAIN: return "the Bargain";
case CARD_SUMMON_ANIMAL: return "the Herd";
case CARD_SUMMON_SKELETON: return "the Bones";
case CARD_WATER: return "Water";
case CARD_SWAP: return "Swap";
#endif
case NUM_CARDS: return "a buggy card";
}
return "a very buggy card";
}
card_type name_to_card(string name)
{
for (int i = 0; i < NUM_CARDS; i++)
{
if (card_name(static_cast<card_type>(i)) == name)
return static_cast<card_type>(i);
}
return NUM_CARDS;
}
static const deck_archetype* _cards_in_deck(uint8_t deck_type)
{
deck_type_data *deck_data = map_find(all_decks, (misc_item_type)deck_type);
if (deck_data)
return deck_data->cards;
#ifdef ASSERTS
die("No cards found for %u", unsigned(deck_type));
#endif
return {};
}
const string deck_contents(uint8_t deck_type)
{
string output = "\nIt may contain the following cards: ";
// This way of doing things is intended to prevent a card
// that appears in multiple subdecks from showing up twice in the
// output.
set<card_type> cards;
const deck_archetype* pdeck =_cards_in_deck(deck_type);
for (const card_with_weights& cww : *pdeck)
cards.insert(cww.card);
output += comma_separated_fn(cards.begin(), cards.end(), card_name);
output += ".";
return output;
}
static card_type _choose_from_deck(const deck_archetype* pdeck,
deck_rarity_type rarity)
{
// Random rarity should have been replaced by one of the others by now.
ASSERT_RANGE(rarity, DECK_RARITY_COMMON, DECK_RARITY_LEGENDARY + 1);
// FIXME: We should use one of the various choose_random_weighted
// functions here, probably with an iterator, instead of
// duplicating the implementation.
int totalweight = 0;
card_type result = NUM_CARDS;
for (const card_with_weights cww : *pdeck)
{
totalweight += cww.weight[rarity - DECK_RARITY_COMMON];
if (x_chance_in_y(cww.weight[rarity - DECK_RARITY_COMMON], totalweight))
result = cww.card;
}
return result;
}
static card_type _random_card(uint8_t deck_type, deck_rarity_type rarity)
{
const deck_archetype *pdeck = _cards_in_deck(deck_type);
return _choose_from_deck(pdeck, rarity);
}
static card_type _random_card(const item_def& item)
{
return _random_card(item.sub_type, item.deck_rarity);
}
static card_type _draw_top_card(item_def& deck, bool message,
uint8_t &_flags)
{
CrawlHashTable &props = deck.props;
CrawlVector &cards = props[CARD_KEY].get_vector();
CrawlVector &flags = props[CARD_FLAG_KEY].get_vector();
int num_cards = cards.size();
int idx = num_cards - 1;
ASSERT(num_cards > 0);
card_type card = get_card_and_flags(deck, idx, _flags);
cards.pop_back();
flags.pop_back();
if (message)
{
const char *verb = (_flags & CFLAG_DEALT) ? "deal" : "draw";
mprf("You %s a card... It is %s.", verb, card_name(card));
}
return card;
}
static void _push_top_card(item_def& deck, card_type card,
uint8_t _flags)
{
CrawlHashTable &props = deck.props;
CrawlVector &cards = props[CARD_KEY].get_vector();
CrawlVector &flags = props[CARD_FLAG_KEY].get_vector();
cards.push_back((char) card);
flags.push_back((char) _flags);
}
static void _remember_drawn_card(item_def& deck, card_type card, bool allow_id)
{
ASSERT(is_deck(deck));
CrawlHashTable &props = deck.props;
CrawlVector &drawn = props[DRAWN_CARD_KEY].get_vector();
drawn.push_back(static_cast<char>(card));
// Once you've drawn two cards, you know the deck.
if (allow_id && (drawn.size() >= 2 || origin_as_god_gift(deck)))
_deck_ident(deck);
}
const vector<card_type> get_drawn_cards(const item_def& deck)
{
vector<card_type> result;
if (is_deck(deck))
{
const CrawlHashTable &props = deck.props;
const CrawlVector &drawn = props[DRAWN_CARD_KEY].get_vector();
for (unsigned int i = 0; i < drawn.size(); ++i)
{
const char tmp = drawn[i];
result.push_back(static_cast<card_type>(tmp));
}
}
return result;
}
static bool _check_buggy_deck(item_def& deck)
{
ostream& strm = msg::streams(MSGCH_DIAGNOSTICS);
if (!is_deck(deck))
{
crawl_state.zero_turns_taken();
strm << "This isn't a deck at all!" << endl;
return true;
}
CrawlHashTable &props = deck.props;
if (!props.exists(CARD_KEY)
|| props[CARD_KEY].get_type() != SV_VEC
|| props[CARD_KEY].get_vector().get_type() != SV_BYTE
|| cards_in_deck(deck) == 0)
{
crawl_state.zero_turns_taken();
if (!props.exists(CARD_KEY))
strm << "Seems this deck never had any cards in the first place!";
else if (props[CARD_KEY].get_type() != SV_VEC)
strm << "'cards' property isn't a vector.";
else
{
if (props[CARD_KEY].get_vector().get_type() != SV_BYTE)
strm << "'cards' vector doesn't contain bytes.";
if (cards_in_deck(deck) == 0)
{
strm << "Strange, this deck is already empty.";
int cards_left = 0;
if (deck.used_count >= 0)
cards_left = deck.initial_cards - deck.used_count;
else
cards_left = -deck.used_count;
if (cards_left != 0)
{
strm << " But there should have been " << cards_left
<< " cards left.";
}
}
}
strm << endl
<< "A swarm of software bugs snatches the deck from you "
"and whisks it away." << endl;
if (deck.link == you.equip[EQ_WEAPON])
unwield_item();
dec_inv_item_quantity(deck.link, 1);
return true;
}
bool problems = false;
CrawlVector &cards = props[CARD_KEY].get_vector();
CrawlVector &flags = props[CARD_FLAG_KEY].get_vector();
vec_size num_cards = cards.size();
vec_size num_flags = flags.size();
unsigned int num_buggy = 0;
for (vec_size i = 0; i < num_cards; ++i)
{
uint8_t card = cards[i].get_byte();
uint8_t _flags = flags[i].get_byte();
// Bad card, or "dealt" card not on the top.
if (card >= NUM_CARDS
|| (_flags & CFLAG_DEALT) && i < num_cards - 1)
{
cards.erase(i);
flags.erase(i);
i--;
num_cards--;
num_buggy++;
}
}
if (num_buggy > 0)
{
strm << num_buggy << " buggy cards found in the deck, discarding them."
<< endl;
deck.used_count += num_buggy;
num_cards = cards.size();
num_flags = cards.size();
problems = true;
}
if (num_cards == 0)
{
crawl_state.zero_turns_taken();
strm << "Oops, all of the cards seem to be gone." << endl
<< "A swarm of software bugs snatches the deck from you "
"and whisks it away." << endl;
if (deck.link == you.equip[EQ_WEAPON])
unwield_item();
dec_inv_item_quantity(deck.link, 1);
return true;
}
if (num_cards > deck.initial_cards)
{
if (deck.initial_cards == 0)
strm << "Deck was created with zero cards???" << endl;
else if (deck.initial_cards < 0)
strm << "Deck was created with *negative* cards?!" << endl;
else
strm << "Deck has more cards than it was created with?" << endl;
deck.initial_cards = num_cards;
problems = true;
}
if (num_cards > num_flags)
{
strm << (num_cards - num_flags) << " more cards than flags.";
strm << endl;
for (unsigned int i = num_flags + 1; i <= num_cards; ++i)
flags[i] = static_cast<char>(0);
problems = true;
}
else if (num_flags > num_cards)
{
strm << (num_cards - num_flags) << " more cards than flags.";
strm << endl;
for (unsigned int i = num_flags; i > num_cards; --i)
flags.erase(i);
problems = true;
}
if (deck.used_count >= 0)
{
if (deck.initial_cards != (deck.used_count + num_cards))
{
strm << "Have you used " << deck.used_count << " cards, or "
<< (deck.initial_cards - num_cards) << "? Oops.";
strm << endl;
deck.used_count = deck.initial_cards - num_cards;
problems = true;
}
}
else
{
if (-deck.used_count != num_cards)
{
strm << "There are " << num_cards << " cards left, not "
<< (-deck.used_count) << ". Oops.";
strm << endl;
deck.used_count = -num_cards;
problems = true;
}
}
if (!problems)
return false;
you.wield_change = true;
if (!yesno("Problems might not have been completely fixed; "
"still use deck?", true, 'n'))
{
crawl_state.zero_turns_taken();
return true;
}
return false;
}
/**
* What message should be printed when a deck of the given rarity is exhausted?
*
* @param rarity The deck_rarity of the deck that's been exhausted.
* @return A message to print;
* e.g. "the deck of cards disappears without a trace."
*/
static string _empty_deck_msg(deck_rarity_type rarity)
{
static const map<deck_rarity_type, const char*> empty_deck_messages = {
{ DECK_RARITY_COMMON,
"disappears without a trace." },
{ DECK_RARITY_RARE,
"glows slightly and disappears." },
{ DECK_RARITY_LEGENDARY,
"glows with a rainbow of weird colours and disappears." },
};
const char* const *rarity_message = map_find(empty_deck_messages, rarity);
ASSERT(rarity_message);
return make_stringf("The deck of cards %s", *rarity_message);
}
// Choose a deck from inventory and return its slot (or -1).
static int _choose_inventory_deck(const char* prompt)
{
const int slot = prompt_invent_item(prompt, MT_INVLIST,
OSEL_DRAW_DECK, OPER_EVOKE);
if (prompt_failed(slot))
return -1;
if (!is_deck(you.inv[slot]))
{
mpr("That isn't a deck!");
return -1;
}
return slot;
}
static void _deck_ident(item_def& deck)
{
if (in_inventory(deck) && !item_ident(deck, ISFLAG_KNOW_TYPE))
{
set_ident_flags(deck, ISFLAG_KNOW_TYPE);
mprf("This is %s.", deck.name(DESC_A).c_str());
you.wield_change = true;
}
}
// Draw the top four cards of an unstacked deck and play them all.
// Discards the rest of the deck. Return false if the operation was
// failed/aborted along the way.
bool deck_deal()
{
const int slot = _choose_inventory_deck("Deal from which deck?");
if (slot == -1)
{
crawl_state.zero_turns_taken();
return false;
}
item_def& deck(you.inv[slot]);
if (_check_buggy_deck(deck))
return false;
CrawlHashTable &props = deck.props;
if (props[STACKED_KEY].get_bool())
{
mpr("This deck seems insufficiently random for dealing.");
crawl_state.zero_turns_taken();
return false;
}
const int num_cards = cards_in_deck(deck);
_deck_ident(deck);
if (num_cards == 1)
mpr("There's only one card left!");
else if (num_cards < 4)
mprf("The deck only has %d cards.", num_cards);
const int num_to_deal = (num_cards < 4 ? num_cards : 4);
for (int i = 0; i < num_to_deal; ++i)
{
int last = cards_in_deck(deck) - 1;
uint8_t flags;
// Flag the card as dealt (changes messages and gives no piety).
card_type card = get_card_and_flags(deck, last, flags);
_set_card_and_flags(deck, last, card, flags | CFLAG_DEALT);
evoke_deck(deck);
redraw_screen();
}
// Nemelex doesn't like dealers with inadequate decks.
if (num_to_deal < 4)
{
mpr("Nemelex gives you another card to finish dealing.");
draw_from_deck_of_punishment(true);
}
// If the deck had cards left, exhaust it.
if (deck.quantity > 0)
{
mpr(_empty_deck_msg(deck.deck_rarity));
if (slot == you.equip[EQ_WEAPON])
unwield_item();
dec_inv_item_quantity(slot, 1);
}
return true;
}
static bool _card_in_deck(card_type card, const deck_archetype *pdeck)
{
return any_of(pdeck->begin(), pdeck->end(),
[card](const card_with_weights& cww){return cww.card == card;});
}
string which_decks(card_type card)
{
vector<string> decks;
string output = "\n";
bool punishment = false;
for (auto &deck_data : all_decks)
{
misc_item_type deck = (misc_item_type)deck_data.first;
if (!_card_in_deck(card, deck_data.second.cards))
continue;
if (deck == MISC_DECK_OF_PUNISHMENT)
punishment = true;
else
decks.push_back(deck_data.second.name);
}
if (!decks.empty())
{
output += "It is usually found in decks of "
+ comma_separated_line(decks.begin(), decks.end());
if (punishment)
output += ", or in Nemelex Xobeh's deck of punishment";
output += ".";
}
else if (punishment)
{
output += "It is usually only found in Nemelex Xobeh's deck of "
"punishment.";
}
else
output += "It is normally not part of any deck.";
return output;
}
static void _describe_cards(vector<card_type> cards)
{
ASSERT(!cards.empty());
auto scroller = make_shared<Scroller>();
auto vbox = make_shared<Box>(Widget::VERT);
#ifdef USE_TILE_WEB
tiles.json_open_object();
tiles.json_open_array("cards");
#endif
bool seen[NUM_CARDS] = {0};
ostringstream data;
bool first = true;
for (card_type card : cards)
{
if (seen[card])
continue;
seen[card] = true;
string name = card_name(card);
string desc = getLongDescription(name + " card");
if (desc.empty())
desc = "No description found.\n";
string decks = which_decks(card);
name = uppercase_first(name);
desc = desc + decks;
auto title_hbox = make_shared<Box>(Widget::HORZ);
#ifdef USE_TILE
auto icon = make_shared<Image>();
icon->set_tile(tile_def(TILE_MISC_CARD, TEX_DEFAULT));
title_hbox->add_child(move(icon));
#endif
auto title = make_shared<Text>(formatted_string(name, WHITE));
title->set_margin_for_crt({0, 0, 0, 0});
title->set_margin_for_sdl({0, 0, 0, 10});
title_hbox->add_child(move(title));
title_hbox->align_items = Widget::CENTER;
title_hbox->set_margin_for_crt({first ? 0 : 1, 0, 1, 0});
title_hbox->set_margin_for_sdl({first ? 0 : 20, 0, 20, 0});
vbox->add_child(move(title_hbox));
auto text = make_shared<Text>(desc);
text->wrap_text = true;
vbox->add_child(move(text));
#ifdef USE_TILE_WEB
tiles.json_open_object();
tiles.json_write_string("name", name);
tiles.json_write_string("desc", desc);
tiles.json_close_object();
#endif
first = false;
}
#ifdef USE_TILE_LOCAL
vbox->max_size()[0] = tiles.get_crt_font()->char_width()*80;
#endif
scroller->set_child(move(vbox));
auto popup = make_shared<ui::Popup>(scroller);
bool done = false;
popup->on(Widget::slots.event, [&done, &scroller](wm_event ev) {
if (ev.type != WME_KEYDOWN)
return false;
done = !scroller->on_event(ev);
return true;
});
#ifdef USE_TILE_WEB
tiles.json_close_array();
tiles.push_ui_layout("describe-cards", 0);
#endif
ui::run_layout(move(popup), done);
#ifdef USE_TILE_WEB
tiles.pop_ui_layout();
#endif
}
// Stack a deck: look at the next five cards, put them back in any
// order, discard the rest of the deck.
// Return false if the operation was failed/aborted along the way.
bool deck_stack()
{
const int slot = _choose_inventory_deck("Stack which deck?");
if (slot == -1)
{
crawl_state.zero_turns_taken();
return false;
}
item_def& deck(you.inv[slot]);
if (_check_buggy_deck(deck))
return false;
_deck_ident(deck);
const int num_cards = cards_in_deck(deck);
if (num_cards == 1)
mpr("There's only one card left!");
else if (num_cards < 5)
mprf("The deck only has %d cards.", num_cards);
else if (num_cards == 5)
mpr("The deck has exactly five cards.");
else
{
mprf("You draw the first five cards out of %d and discard the rest.",
num_cards);
}
// these are included in default force_more_message to show them before menu
run_uncancel(UNC_STACK_FIVE, slot);
return true;
}
class StackFiveMenu : public Menu
{
virtual bool process_key(int keyin) override;
vector<card_type>& draws;
vector<uint8_t>& flags;
public:
StackFiveMenu(vector<card_type>& d, vector<uint8_t>& f)
: Menu(MF_NOSELECT | MF_ALWAYS_SHOW_MORE), draws(d), flags(f) {};
};
bool StackFiveMenu::process_key(int keyin)
{
if (keyin == CK_ENTER)
{
formatted_string old_more = more;
set_more(formatted_string::parse_string(
"Are you done? (press y or Y to confirm)"));
if (yesno(nullptr, true, 'n', false, false, true))
return false;
set_more(old_more);
}
else if (keyin == '?')
_describe_cards(draws);
else if (keyin >= '1' && keyin <= '0' + static_cast<int>(draws.size()))
{
const unsigned int i = keyin - '1';
for (unsigned int j = 0; j < items.size(); j++)
if (items[j]->selected())
{
swap(draws[i], draws[j]);
swap(flags[i], flags[j]);
swap(items[i]->text, items[j]->text);
items[j]->colour = LIGHTGREY;
select_item_index(i, 0, false); // this also updates the item
select_item_index(j, 0, false);
return true;
}
items[i]->colour = WHITE;
select_item_index(i, 1, false);
}
else if (keyin == CK_ESCAPE)
return !crawl_state.seen_hups;
else
Menu::process_key(keyin);
return true;
}
bool stack_five(int slot)
{
item_def& deck(you.inv[slot]);
if (_check_buggy_deck(deck))
return false;
const int num_cards = cards_in_deck(deck);
const int num_to_stack = (num_cards < 5 ? num_cards : 5);
vector<card_type> draws;
vector<uint8_t> flags;
for (int i = 0; i < num_cards; ++i)
{
uint8_t _flags;
card_type card = _draw_top_card(deck, false, _flags);
if (i < num_to_stack)
{
draws.push_back(card);
flags.push_back(_flags | CFLAG_SEEN);
}
// Rest of deck is discarded.
}
CrawlHashTable &props = deck.props;
deck.used_count = -num_to_stack;
props[STACKED_KEY] = true;
you.wield_change = true;
StackFiveMenu menu(draws, flags);
MenuEntry *const title = new MenuEntry("Select two cards to swap them:", MEL_TITLE);
menu.set_title(title);
for (unsigned int i = 0; i < draws.size(); i++)
{
MenuEntry * const entry = new MenuEntry(card_name(draws[i]), MEL_ITEM, 1, '1'+i);
#ifdef USE_TILE
entry->add_tile(tile_def(TILE_MISC_CARD, TEX_DEFAULT));
#endif
menu.add_entry(entry);
}
menu.set_more(formatted_string::parse_string(
"<lightgrey>Press <w>?</w> for the card descriptions"
" or <w>Enter</w> to accept."));
menu.show();
for (unsigned int i = 0; i < draws.size(); ++i)
{
_push_top_card(deck, draws[draws.size() - 1 - i],
flags[flags.size() - 1 - i]);
}