forked from Jsdemonsim/demonsim
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimwithqsprayer.c
3561 lines (3262 loc) · 92.7 KB
/
simwithqsprayer.c
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
/*
* Copyright (C) 2014 JS <[email protected]>
*
* Note: JS (aka John Smythe) is the pseudonym of the author.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* HOW THIS SIMULATOR WORKS
* ------------------------
*
* The basic unit of simulation is the State structure. The State structure
* holds every piece of information that is needed to run one simulation
* from start to end. Since the program is multithreaded, one State
* structure is created for each thread. That way, the program can be
* run on multiple cores, with each core operating on a separate State.
* After all simulations are run, the main thread will total the results
* from each of the States and print the results.
*
* The main way that abilities are handled is that each card has an array
* of "Attributes". An attribute can either be an ability, such as "Dodge:60",
* or it can be a temporary buff or debuff applied by another card's ability,
* such as "Toxic Clouds:200". Whenever a buff/debuff affects a card, we
* simply add an attribute to the card. When the buff/debuff disappears, we
* remove the attribute from the card. At many places in the simulation, we
* check whether the card has a particular attribute with the HasAttr()
* function.
*
* Many times, the demon simulator will reuse the same attribute as both the
* ability and the debuff. It can do this because the demon is immune to all
* debuffs. So if the demon has "Fire God:200", we know it has the ability
* and isn't affected by it. Conversely, the player card will never have the
* "Fire God" ability because we make sure never to add abilities like this to
* the player cards in the cards file. So if a player card has "Fire God", we
* know that it was put there by the demon and the player card should lose hp
* every turn.
*
* In other cases, we create a separate attribute for an ability to distinguish
* its buff from the ability itself. For example, if a card has the
* "Forest force" ability (ATTR_FOREST_ATK), it will place the
* "Force force buff" attribute (ATTR_FOREST_ATK_BUFF) on all other cards.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#if defined(_MSC_VER)
// Compiling for Windows.
#include <windows.h>
#define USING_WINDOWS
// Would you believe that the microsoft compiler doesn't have stdbool?
// So I have to define my own bool type here.
#define true 1
#define false 0
#define bool int
#define strcasecmp stricmp
#else
// Not Windows.
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#endif
/*---------------------------------------------------------------------------*/
/* CONSTANTS */
/*---------------------------------------------------------------------------*/
#define MAX_LEVEL 150
#define FIRST_DEMON_ROUND 5
#define FIRST_PLAYER_ROUND 6
#define DEFAULT_ITERS 50000
#define DEFAULT_LEVEL 61
#define DEFAULT_MAX_ROUNDS 500
#define DEFAULT_THREADS 8
#define MAX_ATTR 40
#define MAX_RUNES 4
#define MAX_CARDS_IN_SET 20
#define MAX_CARDS_IN_DECK 10
#define MAX_CARD_TYPES 1000
#define MAX_CARDS_IN_HAND 5
#define MAX_THREADS 64
#define SET_HAND 1
#define SET_FIELD 2
#define SET_GRAVE 3
#define MAX_LINE_SIZE 4096
#define dprintf(fmt, ...) \
do { if (doDebug) {fprintf(output, fmt, ## __VA_ARGS__);} } while (0)
#define vprintf(fmt, ...) \
do { if (verbose) {fprintf(output, fmt, ## __VA_ARGS__);} } while (0)
/*---------------------------------------------------------------------------*/
/* GLOBALS */
/*---------------------------------------------------------------------------*/
static FILE *output;
static bool doDebug;
static bool doAppend;
static bool verbose;
static bool showDamage;
static bool avgConcentrate;
static const char *outputFilename;
static const char *deckFile = "deck.txt";
static int numIters = DEFAULT_ITERS;
static int numThreads = 8;
// Initial state:
static int initialLevel = DEFAULT_LEVEL;
static int initialHp = 0;
static int maxRounds = DEFAULT_MAX_ROUNDS;
static const char *theDeck[MAX_CARDS_IN_DECK];
static int numDeckCards;
static const char *theRunes[MAX_RUNES];
static int numRunes;
static const char *theDemon = "DarkTitan";
// This is the big list of attributes that are supported (i.e. abilities).
enum attrTypes {
ATTR_NONE,
ATTR_ADVANCED_STRIKE,
ATTR_BACKSTAB,
ATTR_BACKSTAB_BUFF,
ATTR_BITE,
ATTR_BLOODSUCKER,
ATTR_BLOODTHIRSTY,
ATTR_CHAIN_ATTACK,
ATTR_CONCENTRATE,
ATTR_COUNTERATTACK,
ATTR_CRAZE,
ATTR_CURSE,
ATTR_D_REANIMATE,
ATTR_D_REINCARNATE,
ATTR_DAMNATION,
ATTR_DEAD,
ATTR_DESTROY,
ATTR_DODGE,
ATTR_EVASION,
ATTR_EXILE,
ATTR_FIRE_GOD,
ATTR_FOREST,
ATTR_FOREST_ATK,
ATTR_FOREST_ATK_BUFF,
ATTR_FOREST_HP,
ATTR_FOREST_HP_BUFF,
ATTR_GUARD,
ATTR_HEALING,
ATTR_HOT_CHASE,
ATTR_ICE_SHIELD,
ATTR_IMMUNITY,
ATTR_LACERATE,
ATTR_LACERATE_BUFF,
ATTR_MANA_CORRUPT,
ATTR_MANIA,
ATTR_MTN,
ATTR_MTN_ATK,
ATTR_MTN_ATK_BUFF,
ATTR_MTN_HP,
ATTR_MTN_HP_BUFF,
ATTR_OBSTINACY,
ATTR_PARRY,
ATTR_PRAYER,
ATTR_QS_PRAYER, // Added_qs_prayer
ATTR_QS_REGENERATE,
ATTR_QS_REINCARNATE,
ATTR_REANIMATE,
ATTR_REANIM_SICKNESS,
ATTR_REFLECTION,
ATTR_REGENERATE,
ATTR_REINCARNATE,
ATTR_REJUVENATE,
ATTR_RESISTANCE,
ATTR_RESURRECTION,
ATTR_RETALIATION,
ATTR_SACRIFICE,
ATTR_SNIPE,
ATTR_SWAMP,
ATTR_SWAMP_ATK,
ATTR_SWAMP_ATK_BUFF,
ATTR_SWAMP_HP,
ATTR_SWAMP_HP_BUFF,
ATTR_TOXIC_CLOUDS,
ATTR_TRAP,
ATTR_TRAP_BUFF,
ATTR_TUNDRA,
ATTR_TUNDRA_ATK,
ATTR_TUNDRA_ATK_BUFF,
ATTR_TUNDRA_HP,
ATTR_TUNDRA_HP_BUFF,
ATTR_VENDETTA,
ATTR_WARPATH,
ATTR_WICKED_LEECH,
// Runes
ATTR_ARCTIC_FREEZE,
ATTR_BLOOD_STONE,
ATTR_CLEAR_SPRING,
ATTR_FROST_BITE,
ATTR_RED_VALLEY,
ATTR_LORE,
ATTR_LEAF,
ATTR_REVIVAL,
ATTR_FIRE_FORGE,
ATTR_STONEWALL,
ATTR_SPRING_BREEZE,
ATTR_THUNDER_SHIELD,
ATTR_NIMBLE_SOUL,
ATTR_DIRT,
ATTR_FLYING_STONE,
ATTR_TSUNAMI,
};
// An attribute will have a type and an optional "level". The level will be
// either an amount or percent. For example, "Dodge:60" will have a type
// of ATTR_DODGE and a level of 60.
typedef struct attribute {
int type;
int level;
} Attr;
// The dead attribute is used to mark a card as dead so we can identify
// a dead card that way instead of looking at the hit points. Some cards
// can "die" without losing all their hit points (e.g. exile).
const Attr DeadAttr = { ATTR_DEAD };
// This is the structure for one card. There are two sections. The first
// section is set by the type of card and can't change. The second section
// is the current state of the card and may change over the course of the
// battle. The first section is used to initialize the second section when
// a card needs to be reset to its original stats (e.g. when first played,
// when reincarnated, etc).
typedef struct card {
// Not changeable info.
const char *name;
int cost;
int timing;
int baseAtk;
int baseHp;
Attr baseAttr[MAX_ATTR];
// Current state.
int curTiming;
int atk;
int curBaseAtk;
int hp;
int maxHp;
int numAttr;
Attr attr[MAX_ATTR];
} Card;
// A card set is basically an array of cards with a count. There are four
// sets for each simulation: the field, the hand, the graveyard, and the deck.
// See the State structure below.
typedef struct cardSet {
int numCards;
Card cards[MAX_CARDS_IN_SET];
} CardSet;
// This is the structure for a rune. Like a Card, it has the constant
// section and the current state section.
typedef struct rune {
// Not changeable.
const char *name;
Attr attr;
int maxCharges;
// Changeable.
int chargesUsed;
int usedThisRound;
} Rune;
// The State structure holds the entire state of a simulation.
typedef struct state {
int dmgDone; // Damage done to demon.
int hp; // Player's current hp.
int maxHp; // Player's max hp.
int round; // Current round.
int numRunes; // Number of runes.
Card demon; // Demon state.
CardSet deck; // Cards in deck.
CardSet hand; // Cards in hand.
CardSet field; // Cards on field.
CardSet grave; // Cards in grave.
Rune runes[MAX_RUNES]; // Array of runes.
unsigned int seedW; // Random seed part 1.
unsigned int seedZ; // Random seed part 2.
} State;
typedef struct result {
long long total;
long long totalRounds;
int lowRounds;
int highRounds;
int lowDamage;
int highDamage;
int timesRoundX;
} Result;
typedef struct task {
State *state;
int numIterations;
Result *result;
} Task;
#define DIM(a) (sizeof(a)/sizeof(a[0]))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
static Card *FindLowestHpCard(State *state, CardSet *cs, bool mostDamaged);
static void CardPlayedToField(State *state, Card *c);
static void SimAdvancedStrike(State *state);
static void SimRegenerate(State *state, const char *name, int heal);
static void SimReincarnate(State *state, const char *attrName, int level);
static void SimReanimate(State *state, const char *attrName);
static int PickAliveCardFromSet(State *state, const CardSet *cs);
static void AddCardToSetRandomly(State *state, CardSet *cs, const Card *c);
static Card cardTypes[MAX_CARD_TYPES];
static int numCardTypes;
typedef struct AttrLookup {
const char *name;
int attrType;
} AttrLookup;
static const AttrLookup allAttrs[] = {
{ "NONE", ATTR_NONE },
{ "ADVANCED STRIKE", ATTR_ADVANCED_STRIKE },
{ "BACKSTAB", ATTR_BACKSTAB },
{ "BITE", ATTR_BITE },
{ "BLOODSUCKER", ATTR_BLOODSUCKER },
{ "BLOODTHIRSTY", ATTR_BLOODTHIRSTY },
{ "CHAIN ATTACK", ATTR_CHAIN_ATTACK },
{ "CONCENTRATE", ATTR_CONCENTRATE },
{ "COUNTERATTACK", ATTR_COUNTERATTACK },
{ "CRAZE", ATTR_CRAZE },
{ "CURSE", ATTR_CURSE },
{ "D_REANIMATE", ATTR_D_REANIMATE },
{ "D_REINCARNATE", ATTR_D_REINCARNATE },
{ "DAMNATION", ATTR_DAMNATION },
{ "DEAD", ATTR_DEAD },
{ "DESTROY", ATTR_DESTROY },
{ "DODGE", ATTR_DODGE },
{ "EXILE", ATTR_EXILE },
{ "EVASION", ATTR_EVASION },
{ "FIRE GOD", ATTR_FIRE_GOD },
{ "FOREST", ATTR_FOREST },
{ "FOREST FORCE", ATTR_FOREST_ATK },
{ "FOREST GUARD", ATTR_FOREST_HP },
{ "GUARD", ATTR_GUARD },
{ "HEALING", ATTR_HEALING },
{ "HOT CHASE", ATTR_HOT_CHASE },
{ "ICE SHIELD", ATTR_ICE_SHIELD },
{ "IMMUNITY", ATTR_IMMUNITY },
{ "LACERATE", ATTR_LACERATE },
{ "MANA CORRUPT", ATTR_MANA_CORRUPT },
{ "MANIA", ATTR_MANIA },
{ "MTN", ATTR_MTN },
{ "MTN FORCE", ATTR_MTN_ATK },
{ "MTN GUARD", ATTR_MTN_HP },
{ "OBSTINACY", ATTR_OBSTINACY },
{ "PARRY", ATTR_PARRY },
{ "PRAYER", ATTR_PRAYER },
{ "QS_PRAYER", ATTR_QS_PRAYER }, // Added_qs_prayer
{ "QS_REGENERATE", ATTR_QS_REGENERATE },
{ "QS_REINCARNATE", ATTR_QS_REINCARNATE },
{ "REANIMATE", ATTR_REANIMATE },
{ "REFLECTION", ATTR_REFLECTION },
{ "REGENERATE", ATTR_REGENERATE },
{ "REINCARNATE", ATTR_REINCARNATE },
{ "REJUVENATE", ATTR_REJUVENATE },
{ "RESISTANCE", ATTR_RESISTANCE },
{ "RESURRECTION", ATTR_RESURRECTION },
{ "RETALIATION", ATTR_RETALIATION },
{ "SACRIFICE", ATTR_SACRIFICE },
{ "SNIPE", ATTR_SNIPE },
{ "SWAMP", ATTR_SWAMP },
{ "SWAMP FORCE", ATTR_SWAMP_ATK },
{ "SWAMP GUARD", ATTR_SWAMP_HP },
{ "TOXIC CLOUDS", ATTR_TOXIC_CLOUDS },
{ "TRAP", ATTR_TRAP },
{ "TUNDRA", ATTR_TUNDRA },
{ "TUNDRA FORCE", ATTR_TUNDRA_ATK },
{ "TUNDRA GUARD", ATTR_TUNDRA_HP },
{ "VENDETTA", ATTR_VENDETTA },
{ "WARPATH", ATTR_WARPATH },
{ "WICKED LEECH", ATTR_WICKED_LEECH },
};
static const Rune allRunes[] = {
{ "Arctic Freeze", { ATTR_ARCTIC_FREEZE, 100 }, 3 },
{ "Blood Stone", { ATTR_BLOOD_STONE, 270 }, 5 },
{ "Clear Spring", { ATTR_CLEAR_SPRING, 225 }, 4 },
{ "Frost Bite", { ATTR_FROST_BITE, 140 }, 3 },
{ "Red Valley", { ATTR_RED_VALLEY, 90 }, 5 },
{ "Lore", { ATTR_LORE, 150 }, 4 },
{ "Leaf", { ATTR_LEAF, 240 }, 4 },
{ "Revival", { ATTR_REVIVAL, 120 }, 4 },
{ "Fire Forge", { ATTR_FIRE_FORGE, 210 }, 4 },
{ "Stonewall", { ATTR_STONEWALL, 180 }, 4 },
{ "Spring Breeze", { ATTR_SPRING_BREEZE, 240 }, 4 },
{ "Thunder Shield", { ATTR_THUNDER_SHIELD, 200 }, 4 },
{ "Nimble Soul", { ATTR_NIMBLE_SOUL, 65 }, 3 },
{ "Dirt", { ATTR_DIRT, 70 }, 4 },
{ "Flying Stone", { ATTR_FLYING_STONE, 270 }, 4 },
{ "Tsunami", { ATTR_TSUNAMI, 80 }, 4 },
};
static const Card DeadCard = {
/* name = */ "Dead Card",
/* cost = */ 0,
/* timing = */ 0,
/* baseAtk = */ 0,
/* baseHp = */ 0,
/* baseAttr[0] = */ {{ ATTR_DEAD, 0 }, {ATTR_NONE, 0}},
/* curTiming = */ 0,
/* atk = */ 0,
/* curBaseAtk = */ 0,
/* hp = */ 0,
/* maxHp = */ 0,
/* numAttr = */ 1,
/* attr[0]; = */ {{ ATTR_DEAD, 0 }, {ATTR_NONE, 0}}
};
static const int hpPerLevel[MAX_LEVEL+1] = {
0, 1000, 1070, 1140, 1210, 1280, 1350, 1420, 1490, 1560, 1630,
1800, 1880, 1960, 2040, 2120, 2200, 2280, 2360, 2440, 2520,
2800, 2890, 2980, 3070, 3160, 3250, 3340, 3430, 3520, 3610,
4000, 4100, 4200, 4300, 4400, 4500, 4600, 4700, 4800, 4900,
5400, 5510, 5620, 5730, 5840, 5950, 6060, 6170, 6280, 6390,
7000, 7120, 7240, 7360, 7480, 7600, 7720, 7840, 7960, 8080,
8800, 8930, 9060, 9190, 9320, 9450, 9580, 9710, 9840, 9970,
10800, 10940, 11080, 11220, 11360, 11500, 11640, 11780, 11920, 12060,
13000, 13150, 13300, 13450, 13600, 13750, 13900, 14050, 14200, 14350,
15400, 15560, 15720, 15880, 16040, 16200, 16360, 16520, 16680, 16840,
18000, 18170, 18340, 18510, 18680, 18850, 19020, 19190, 19360, 19530,
20800, 20980, 21160, 21340, 21520, 21700, 21880, 22060, 22240, 22420,
23800, 23990, 24180, 24370, 24560, 24750, 24940, 25130, 25320, 25510,
27000, 27200, 27400, 27600, 27800, 28000, 28200, 28400, 28600, 28800,
30400, 30610, 30820, 31030, 31240, 31450, 31660, 31870, 32080, 32290,
};
// Array of states, one per thread.
static State **states;
// Space for the states. Instead of malloc'ing each state one by one,
// we allocate a large buffer and then pick page aligned states within the
// buffer.
static char *stateBuffer;
// The one original state. When we start a new simulation, we copy from
// this state.
static State defaultState;
//static State state;
static int roundX = 50;
/**
* At initialization time, this function is called to create the specified
* number of State structures. The tricky part here is that we want to
* make sure that the states do not overlap cache lines, so that the cores
* will not interfere with each other by accessing shared cache lines. To
* do this, we make sure that each State is aligned to a 4KB boundary.
* The created states are put into the states global array.
*
* @param numStates The number of states to create.
*/
static void AllocateStates(int numStates)
{
int stateSize = sizeof(State);
int statePageSize = (stateSize + 0xfff) & ~0xfff;
int totalSize = statePageSize * numStates + 0x1000;
int i = 0;
char *alignedPtr = NULL;
// Allocate the big space.
stateBuffer = calloc(1, totalSize);
// Find the first page aligned address.
alignedPtr = (char *) (((uintptr_t)(stateBuffer) + 0xfff) & ~0xfff);
// Allocate array.
states = calloc(numStates, sizeof(State *));
for (i=0;i<numStates;i++) {
// Allocate each state on a page aligned address.
states[i] = (State *) alignedPtr;
alignedPtr += statePageSize;
}
}
/**
* Finds an attribute by name from the global array of attributes (allAttrs).
*
* @param name The name of the attribute to find.
* @return The index of the attribute in allAttrs, or
* -1 if not found.
*/
static int LookupAttr(const char *name)
{
int i = 0;
for (i=0;i<DIM(allAttrs);i++) {
if (!strcasecmp(name, allAttrs[i].name))
return allAttrs[i].attrType;
}
return -1;
}
/**
* Finds a card type by name from the global array of card types (cardTypes).
*
* @param name The name of the card to find.
* @return A pointer to the card type, or NULL if not
* found.
*/
static const Card *FindCard(const char *name)
{
int i = 0;
for (i=0;i<numCardTypes;i++) {
if (!strcasecmp(name, cardTypes[i].name))
return &cardTypes[i];
}
return NULL;
}
/**
* Finds a rune type by name from the global array of rune types (allRunes).
*
* @param name The name of the rune to find.
* @return A pointer to the rune type, or NULL if not
* found.
*/
static const Rune *FindRune(const char *name)
{
int i = 0;
for (i=0;i<DIM(allRunes);i++) {
if (!strcasecmp(name, allRunes[i].name))
return &allRunes[i];
}
return NULL;
}
/**
* Initializes a card's current state back to its base state. This is done
* at the start of each simulation, and whenever a card is recycled back
* into play (e.g. reincarnation). The card's attributes are also reset
* to its base attributes.
*
* @param card The card to initialize.
*/
static void InitCard(Card *card)
{
int i = 0;
int j = 0;
card->curTiming = card->timing;
card->atk = card->baseAtk;
card->curBaseAtk = card->baseAtk;
card->hp = card->baseHp;
card->maxHp = card->baseHp;
card->numAttr = card->baseHp;
memset(card->attr, 0, sizeof(card->attr));
card->numAttr = 0;
for (i=0, j=0;i<MAX_ATTR;i++) {
if (card->baseAttr[i].type != ATTR_NONE) {
card->attr[j++] = card->baseAttr[i];
card->numAttr++;
}
}
}
/**
* Returns a random number. This function is based on the MWC generator,
* which concatenates two 16-bit multiply with carry generators. It uses
* two seeds stored in the state (seedW and seedZ). The reason why I use
* this rng is because it is reentrant (it can be run simultaneously by
* multiple cores).
*
* @param state The simulator state. Both seedW and seedZ
* are updated to new values on each call.
* @return A 32-bit random number.
*/
static unsigned int myRand(State *state)
{
state->seedW = 18000*(state->seedW & 65535) + (state->seedW >> 16);
state->seedZ = 36969*(state->seedZ & 65535) + (state->seedZ >> 16);
return (state->seedZ << 16) + state->seedW;
}
/**
* Returns a random number in the given range. Calls the previous
* function to get the random number.
*
* @param state The simulator state.
* @param range Range of random number.
* @return A random number in the range [0..range-1].
*/
static unsigned int Rnd(State *state, unsigned int range)
{
return (((unsigned int) myRand(state)) % range);
}
/**
* Given a card set, shuffle the cards into a random order.
*
* @param state The simulator state.
* @param cs The card set to shuffle.
*/
static void ShuffleSet(State *state, CardSet *cs)
{
int i = 0;
for (i=0;i<cs->numCards-1;i++) {
unsigned int r = Rnd(state, cs->numCards - i);
if (r != 0) {
Card tmp = cs->cards[i];
cs->cards[i] = cs->cards[i+r];
cs->cards[i+r] = tmp;
}
}
}
/**
* Prints the card state (debug mode only). The type of printout depends
* on which set we are printing.
*
* @param c The card to print.
* @param whichSet Which set the card belongs to.
*/
static void PrintCard(const Card *c, int whichSet)
{
switch (whichSet) {
case SET_HAND:
dprintf("%-20s (%d)\n", c->name, c->curTiming);
break;
case SET_FIELD:
dprintf("%-20s (%d atk) (%4d/%4d hp)\n", c->name,
c->atk, c->hp, c->maxHp);
break;
case SET_GRAVE:
default:
dprintf("%-20s\n", c->name);
break;
}
#if 0
{
int i = 0;
dprintf("Attrs = ");
for (i=0;i<c->numAttr;i++) {
dprintf("%d ", c->attr[i].type);
}
dprintf("\n");
}
#endif
}
/**
* Prints all the cards in a set (debug mode only).
*
* @param cs The set of cards to print.
* @param whichSet Which set of cards (e.g. SET_GRAVE).
*/
static void PrintCardSet(const CardSet *cs, int whichSet)
{
int i = 0;
for (i=0;i<cs->numCards;i++) {
PrintCard(&cs->cards[i], whichSet);
}
}
/**
* Prints the current state (debug mode only). This is done once per round.
*
* @param state The simulator state.
*/
void PrintState(const State *state)
{
dprintf("\nPlayer: Hp = %d, Damage done = %d\n", state->hp, state->dmgDone);
PrintCard(&state->demon, SET_FIELD);
if (state->field.numCards != 0) {
dprintf("\nField:\n");
PrintCardSet(&state->field, SET_FIELD);
}
if (state->hand.numCards != 0) {
dprintf("\nHand:\n");
PrintCardSet(&state->hand, SET_HAND);
}
if (state->grave.numCards != 0) {
dprintf("\nGrave:\n");
PrintCardSet(&state->grave, SET_GRAVE);
}
}
/**
* Returns whether a card has a particular attribute, and also what
* level the attribute is.
*
* @param c The card.
* @param attrType The attribute type.
* @param pLevel Returns the level of the attribute, if found.
* @return True if the attribute was found, false if not.
*/
static bool HasAttr(const Card *c, int attrType, int *pLevel)
{
int i = 0;
for (i=0;i<c->numAttr;i++) {
if (c->attr[i].type == attrType) {
if (pLevel != NULL)
*pLevel = c->attr[i].level;
return true;
}
}
return false;
}
/**
* Adds an attribute to a card.
*
* @param c The card.
* @param attr The attribute to add to the card.
*/
static void AddAttr(Card *c, const Attr *attr)
{
if (c->numAttr >= MAX_ATTR) {
int i = 0;
fprintf(stderr, "Too many attrs on %s\n", c->name);
for (i=0;i<c->numAttr;i++) {
fprintf(stderr, "%d ", c->attr[i].type);
}
fprintf(stderr, "\n");
exit(1);
}
c->attr[c->numAttr++] = *attr;
}
/**
* Removes an attribute from a card. Can either remove just one of that
* attribute or all of that attribute, depending on the level argument.
*
* @param c The card.
* @param attrType The attribute type to remove from the card.
* @param level If level is -1, then all attributes of the
* given type will be removed. If the level is
* not -1, then only one attribute matching the
* specified level will be removed.
*/
static void RemoveAttr(Card *c, int attrType, int level)
{
int i = 0;
for (i=0;i<c->numAttr;i++) {
if (c->attr[i].type == attrType &&
(c->attr[i].level == level || level == -1)) {
int j = i;
c->numAttr--;
for (j=i;j<c->numAttr;j++) {
c->attr[j] = c->attr[j+1];
}
i--;
if (level != -1)
return;
}
}
}
/**
* Removes one card from a card set.
*
* @param cs Card set.
* @param n Index of card to remove from set.
*/
static void RemoveCardFromSet(CardSet *cs, int n)
{
int i = 0;
cs->numCards--;
for (i=n;i<cs->numCards;i++)
cs->cards[i] = cs->cards[i+1];
}
/**
* Adds one card to a set. Note that a copy will be made of the given card.
* The card will be added to the end of the set. This is important because
* reincarnation currently uses this function to add a card back to the deck.
* Since the deck is played from the end, the last reincarnated card will
* be played from the deck as the next card.
*
* @param cs Card set.
* @param c Card to be added to set (a copy will be made).
*/
static void AddCardToSet(CardSet *cs, const Card *c)
{
if (cs->numCards >= MAX_CARDS_IN_SET) {
fprintf(stderr, "Too many cards\n");
exit(1);
}
cs->cards[cs->numCards++] = *c;
}
/**
* Adds one card to a set in a random position. This is used when adding
* a card back to the deck in a random order. Note that currently this is
* only used when a card is exiled. It has been determined that reincarnating
* a card from the graveyard puts the card at the top of the deck and not
* randomly.
*
* @param state The simulator state.
* @param cs Card set.
* @param c Card to be added to set (a copy will be made).
*/
static void AddCardToSetRandomly(State *state, CardSet *cs, const Card *c)
{
int r = Rnd(state, cs->numCards+1);
int i = 0;
if (cs->numCards >= MAX_CARDS_IN_SET) {
fprintf(stderr, "Too many cards\n");
exit(1);
}
// Shift cards to the right to make room for new card at slot r.
for (i=cs->numCards;i>r;i--) {
cs->cards[i] = cs->cards[i-1];
}
// Insert card at r.
cs->cards[r] = *c;
cs->numCards++;
}
/**
* Initializes the "default" state. The default state is the master initial
* state that will be copied to each state at the beginning of each simulation
* run. That way, we only have to look up the demon, cards, and runes once
* to create the default state. Then we simply make a copy of the default
* state when we want to start over a new run.
*
* @param state The default state to create.
*/
static void InitDefaultState(State *state)
{
int i = 0;
const Card *c = NULL;
state->dmgDone = 0;
state->hp = initialHp;
state->maxHp = initialHp;
state->round = 1;
// Look up demon.
c = FindCard(theDemon);
if (c == NULL) {
fprintf(stderr, "Couldn't find demon card: %s\n", theDemon);
exit(1);
}
memcpy(&state->demon, c, sizeof(Card));
InitCard(&state->demon);
// Look up cards.
state->deck.numCards = 0;
for (i=0;i<numDeckCards;i++) {
c = FindCard(theDeck[i]);
AddCardToSet(&state->deck, c);
InitCard(&state->deck.cards[i]);
}
state->hand.numCards = 0;
state->field.numCards = 0;
state->grave.numCards = 0;
// Look up runes.
for (i=0;i<numRunes;i++) {
const Rune *rune = FindRune(theRunes[i]);
state->runes[i] = *rune;
state->runes[i].chargesUsed = 0;
state->runes[i].usedThisRound = 0;
}
state->numRunes = i;
}
/**
* Initializes a state in order to start a new simulation run. This merely
* copies the default state, but preserves the current rng seeds so that
* we have new random numbers on each run.
*
* @param state The simulator state to initialize.
*/
static void InitState(State *state)
{
unsigned int seedSaveW = state->seedW;
unsigned int seedSaveZ = state->seedZ;
memcpy(state, &defaultState, sizeof(State));
state->seedW = seedSaveW;
state->seedZ = seedSaveZ;
}
/**
* Removes any dead cards from the field. Note that these cards should
* already be added to the grave or the deck before calling this function.
*
* @param state The simulator state.
*/
static void RemoveDeadCards(State *state)
{
int i = 0;
CardSet *f = &state->field;
for (i=0;i<f->numCards;i++) {
if (HasAttr(&f->cards[i], ATTR_DEAD, NULL)) {
RemoveCardFromSet(f, i);
i--;
}
}
}
/**
* Removes a buff from all cards on the field (except the card that originated
* the buff). This is called when a card dies and had a "force" or "guard"
* type ability that added attack or hp to all other cards of a certain type.
*
* @param state The simulator state.
* @param c The card that created the buff. We need to
* know this so we can skip this card.
* @param buff The attribute type to remove.
* @param level The level of the buff to remove. We need to
* know this because there could be many of the
* same type of buff on the card of different
* levels.
*/
static void RemoveBuffFromField(State *state, Card *c, int buff, int level)
{
int i = 0;
CardSet *f = &state->field;
for (i=0;i<f->numCards;i++) {
Card *c2 = &f->cards[i];
if (c == c2)
continue;
if (HasAttr(c2, buff, NULL)) {
switch (buff) {
case ATTR_TUNDRA_HP_BUFF:
case ATTR_FOREST_HP_BUFF:
case ATTR_MTN_HP_BUFF:
case ATTR_SWAMP_HP_BUFF:
{
int oldHp = c2->hp;
RemoveAttr(c2, buff, level);
c2->maxHp -= level;
if (c2->hp > c2->maxHp)
c2->hp = c2->maxHp;
dprintf("Hp buff removed: %s loses %d max hp and %d hp "
"(now %d)\n", c2->name, level, oldHp - c2->hp,
c2->hp);
break;
}
case ATTR_TUNDRA_ATK_BUFF:
case ATTR_FOREST_ATK_BUFF:
case ATTR_MTN_ATK_BUFF:
case ATTR_SWAMP_ATK_BUFF:
{
RemoveAttr(c2, buff, level);
c2->atk -= level;
c2->curBaseAtk -= level;
if (c2->atk < 0)
c2->atk = 0;
if (c2->curBaseAtk < 0)
c2->curBaseAtk = 0;
dprintf("Atk buff removed: %s loses %d atk and "
"base atk (now %d)\n", c2->name, level, c2->atk);
break;
}