forked from dsuchka/tyrant_optimize_closed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim.cpp
2651 lines (2469 loc) · 98.2 KB
/
sim.cpp
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
#include "sim.h"
#include <boost/range/adaptors.hpp>
#include <boost/range/join.hpp>
#include <iostream>
#include <random>
#include <string>
#include <sstream>
#include <vector>
#include "tyrant.h"
#include "card.h"
#include "cards.h"
#include "deck.h"
//------------------------------------------------------------------------------
inline bool has_attacked(CardStatus* c) { return(c->m_step == CardStep::attacked); }
inline bool is_alive(CardStatus* c) { return(c->m_hp > 0); }
inline bool can_act(CardStatus* c) { return(is_alive(c) && !c->m_jammed); }
inline bool is_active(CardStatus* c) { return(can_act(c) && c->m_delay == 0); }
inline bool is_active_next_turn(CardStatus* c) { return(can_act(c) && c->m_delay <= 1); }
// Can be healed / repaired
inline bool can_be_healed(CardStatus* c) { return(is_alive(c) && c->m_hp < c->m_max_hp); }
// Strange Transmission [Gilians] features
inline bool is_gilian(CardStatus* c) { return(
(c->m_card->m_id >= 25054 && c->m_card->m_id <= 25063) // Gilian Commander
|| (c->m_card->m_id >= 38348 && c->m_card->m_id <= 38388) // Gilian assaults plus the Gil's Shard
); }
inline bool is_alive_gilian(CardStatus* c) { return(is_alive(c) && is_gilian(c)); }
//------------------------------------------------------------------------------
inline std::string status_description(const CardStatus* status)
{
return status->description();
}
//------------------------------------------------------------------------------
template <typename CardsIter, typename Functor>
inline unsigned Field::make_selection_array(CardsIter first, CardsIter last, Functor f)
{
this->selection_array.clear();
for(auto c = first; c != last; ++c)
{
if (f(*c))
{
this->selection_array.push_back(*c);
}
}
return(this->selection_array.size());
}
inline CardStatus * Field::left_assault(const CardStatus * status)
{
return left_assault(status, 1);
}
inline CardStatus * Field::left_assault(const CardStatus * status, const unsigned n)
{
auto & assaults = this->players[status->m_player]->assaults;
if (status->m_index >= n)
{
auto left_status = &assaults[status->m_index - n];
if (is_alive(left_status))
{
return left_status;
}
}
return nullptr;
}
inline CardStatus * Field::right_assault(const CardStatus * status)
{
return right_assault(status, 1);
}
inline CardStatus * Field::right_assault(const CardStatus * status, const unsigned n)
{
auto & assaults = this->players[status->m_player]->assaults;
if ((status->m_index + n) < assaults.size())
{
auto right_status = &assaults[status->m_index + n];
if (is_alive(right_status))
{
return right_status;
}
}
return nullptr;
}
inline const std::vector<CardStatus *> Field::adjacent_assaults(const CardStatus * status)
{
return adjacent_assaults(status, 1);
}
inline const std::vector<CardStatus *> Field::adjacent_assaults(const CardStatus * status, const unsigned n)
{
std::vector<CardStatus *> res;
for (unsigned i(n); i > 0; -- i)
{
auto left_status = left_assault(status, i);
if (left_status)
{ res.push_back(left_status); }
}
for (unsigned i(1); i <= n; ++ i)
{
auto right_status = right_assault(status, i);
if (right_status)
{ res.push_back(right_status); }
}
return res;
}
inline void Field::print_selection_array()
{
#ifndef NDEBUG
for(auto c: this->selection_array)
{
_DEBUG_MSG(2, "+ %s\n", status_description(c).c_str());
}
#endif
}
//------------------------------------------------------------------------------
inline void Field::prepare_action()
{
damaged_units_to_times.clear();
}
//------------------------------------------------------------------------------
inline void Field::finalize_action()
{
for (auto unit_it = damaged_units_to_times.begin(); unit_it != damaged_units_to_times.end(); ++ unit_it)
{
if (__builtin_expect(!unit_it->second, false))
{ continue; }
CardStatus * dmg_status = unit_it->first;
if (__builtin_expect(!is_alive(dmg_status), false))
{ continue; }
unsigned barrier_base = dmg_status->skill(Skill::barrier);
if (barrier_base)
{
unsigned protect_value = barrier_base * unit_it->second;
_DEBUG_MSG(1, "%s protect itself for %u (barrier %u x %u damage taken times)\n",
status_description(dmg_status).c_str(), protect_value, barrier_base, unit_it->second);
dmg_status->m_protected += protect_value;
}
}
}
//------------------------------------------------------------------------------
inline unsigned CardStatus::skill_base_value(Skill::Skill skill_id) const
{
return m_card->m_skill_value[skill_id + m_primary_skill_offset[skill_id]]
+ (skill_id == Skill::berserk ? m_enraged : 0);
}
//------------------------------------------------------------------------------
inline unsigned CardStatus::skill(Skill::Skill skill_id) const
{
return (is_activation_skill_with_x(skill_id)
? safe_minus(skill_base_value(skill_id), m_sabotaged)
: skill_base_value(skill_id))
+ enhanced(skill_id);
}
//------------------------------------------------------------------------------
inline bool CardStatus::has_skill(Skill::Skill skill_id) const
{
return skill_base_value(skill_id);
}
//------------------------------------------------------------------------------
inline unsigned CardStatus::enhanced(Skill::Skill skill_id) const
{
return m_enhanced_value[skill_id + m_primary_skill_offset[skill_id]];
}
//------------------------------------------------------------------------------
inline unsigned CardStatus::protected_value() const
{
return m_protected + m_protected_stasis;
}
//------------------------------------------------------------------------------
inline void CardStatus::set(const Card* card)
{
this->set(*card);
}
//------------------------------------------------------------------------------
inline void CardStatus::set(const Card& card)
{
m_card = &card;
m_index = 0;
m_player = 0;
m_delay = card.m_delay;
m_faction = card.m_faction;
m_attack = card.m_attack;
m_hp = m_max_hp = card.m_health;
m_step = CardStep::none;
m_corroded_rate = 0;
m_corroded_weakened = 0;
m_enfeebled = 0;
m_evaded = 0;
m_inhibited = 0;
m_sabotaged = 0;
m_jammed = false;
m_overloaded = false;
m_paybacked = 0;
m_poisoned = 0;
m_protected = 0;
m_protected_stasis = 0;
m_rallied = 0;
m_enraged = 0;
m_derallied = 0;
m_rush_attempted = false;
m_sundered = false;
m_weakened = 0;
std::memset(m_primary_skill_offset, 0, sizeof m_primary_skill_offset);
std::memset(m_evolved_skill_offset, 0, sizeof m_evolved_skill_offset);
std::memset(m_enhanced_value, 0, sizeof m_enhanced_value);
std::memset(m_skill_cd, 0, sizeof m_skill_cd);
}
//------------------------------------------------------------------------------
inline unsigned attack_power(const CardStatus* att)
{
return safe_minus(safe_minus(att->m_attack, att->m_weakened + att->m_corroded_weakened) + att->m_rallied, att->m_derallied);
}
//------------------------------------------------------------------------------
std::string skill_description(const SkillSpec& s)
{
return skill_names[s.id] +
(s.all ? " all" : s.n == 0 ? "" : std::string(" ") + to_string(s.n)) +
(s.y == allfactions ? "" : std::string(" ") + faction_names[s.y]) +
(s.s == Skill::no_skill ? "" : std::string(" ") + skill_names[s.s]) +
(s.s2 == Skill::no_skill ? "" : std::string(" ") + skill_names[s.s2]) +
(s.x == 0 ? "" : std::string(" ") + to_string(s.x)) +
(s.c == 0 ? "" : std::string(" every ") + to_string(s.c));
}
std::string skill_short_description(const SkillSpec& s)
{
// NOTE: not support summon
return skill_names[s.id] +
(s.s == Skill::no_skill ? "" : std::string(" ") + skill_names[s.s]) +
(s.s2 == Skill::no_skill ? "" : std::string(" ") + skill_names[s.s2]) +
(s.x == 0 ? "" : std::string(" ") + to_string(s.x));
}
//------------------------------------------------------------------------------
std::string card_description(const Cards& cards, const Card* c)
{
std::string desc;
desc = c->m_name;
switch(c->m_type)
{
case CardType::assault:
desc += ": " + to_string(c->m_attack) + "/" + to_string(c->m_health) + "/" + to_string(c->m_delay);
break;
case CardType::structure:
desc += ": " + to_string(c->m_health) + "/" + to_string(c->m_delay);
break;
case CardType::commander:
desc += ": hp:" + to_string(c->m_health);
break;
case CardType::num_cardtypes:
assert(false);
break;
}
if(c->m_rarity >= 4) { desc += " " + rarity_names[c->m_rarity]; }
if(c->m_faction != allfactions) { desc += " " + faction_names[c->m_faction]; }
for(auto& skill: c->m_skills) { desc += ", " + skill_description(skill); }
return(desc);
}
//------------------------------------------------------------------------------
std::string CardStatus::description() const
{
std::string desc = "P" + to_string(m_player) + " ";
switch(m_card->m_type)
{
case CardType::commander: desc += "Commander "; break;
case CardType::assault: desc += "Assault " + to_string(m_index) + " "; break;
case CardType::structure: desc += "Structure " + to_string(m_index) + " "; break;
case CardType::num_cardtypes: assert(false); break;
}
desc += "[" + m_card->m_name;
switch(m_card->m_type)
{
case CardType::assault:
desc += " att:[" + to_string(m_attack);
{
std::string att_desc;
if(m_weakened > 0) { att_desc += "-" + to_string(m_weakened) + "(weakened)"; }
if(m_corroded_weakened > 0) { att_desc += "-" + to_string(m_corroded_weakened) + "(corroded)"; }
att_desc += "]";
if(m_rallied > 0) { att_desc += "+" + to_string(m_rallied) + "(rallied)"; }
if(m_derallied > 0) { att_desc += "-" + to_string(m_derallied) + "(derallied)"; }
if(!att_desc.empty()) { desc += att_desc + "=" + to_string(attack_power(this)); }
}
case CardType::structure:
case CardType::commander:
desc += " hp:" + to_string(m_hp);
break;
case CardType::num_cardtypes:
assert(false);
break;
}
if(m_delay > 0) {
desc += " cd:" + to_string(m_delay);
}
// Status w/o value
if(m_jammed) { desc += ", jammed"; }
if(m_overloaded) { desc += ", overloaded"; }
if(m_sundered) { desc += ", sundered"; }
// Status w/ value
if(m_corroded_rate > 0) { desc += ", corroded " + to_string(m_corroded_rate); }
if(m_enfeebled > 0) { desc += ", enfeebled " + to_string(m_enfeebled); }
if(m_inhibited > 0) { desc += ", inhibited " + to_string(m_inhibited); }
if(m_sabotaged > 0) { desc += ", sabotaged " + to_string(m_sabotaged); }
if(m_poisoned > 0) { desc += ", poisoned " + to_string(m_poisoned); }
if(m_protected > 0) { desc += ", protected " + to_string(m_protected); }
if(m_protected_stasis > 0) { desc += ", stasis " + to_string(m_protected_stasis); }
if(m_enraged > 0) { desc += ", enraged " + to_string(m_enraged); }
// if(m_step != CardStep::none) { desc += ", Step " + to_string(static_cast<int>(m_step)); }
for (const auto & ss: m_card->m_skills)
{
std::string skill_desc;
if (m_evolved_skill_offset[ss.id] != 0) { skill_desc += "->" + skill_names[ss.id + m_evolved_skill_offset[ss.id]]; }
if (m_enhanced_value[ss.id] != 0) { skill_desc += " +" + to_string(m_enhanced_value[ss.id]); }
if (!skill_desc.empty()) { desc += ", " + skill_names[ss.id] + skill_desc; }
}
desc += "]";
return(desc);
}
//------------------------------------------------------------------------------
void Hand::reset(std::mt19937& re)
{
assaults.reset();
structures.reset();
deck->shuffle(re);
commander.set(deck->shuffled_commander);
total_cards_destroyed = 0;
if (commander.skill(Skill::stasis))
{
stasis_faction_bitmap |= (1u << commander.m_faction);
}
}
//---------------------- $40 Game rules implementation -------------------------
// Everything about how a battle plays out, except the following:
// the implementation of the attack by an assault card is in the next section;
// the implementation of the active skills is in the section after that.
unsigned turn_limit{50};
//------------------------------------------------------------------------------
inline unsigned opponent(unsigned player)
{
return((player + 1) % 2);
}
//------------------------------------------------------------------------------
SkillSpec apply_evolve(const SkillSpec& s, signed offset)
{
SkillSpec evolved_s = s;
evolved_s.id = static_cast<Skill::Skill>(evolved_s.id + offset);
return(evolved_s);
}
//------------------------------------------------------------------------------
SkillSpec apply_enhance(const SkillSpec& s, unsigned enhanced_value)
{
SkillSpec enahnced_s = s;
enahnced_s.x += enhanced_value;
return(enahnced_s);
}
//------------------------------------------------------------------------------
SkillSpec apply_sabotage(const SkillSpec& s, unsigned sabotaged_value)
{
SkillSpec sabotaged_s = s;
sabotaged_s.x -= std::min(sabotaged_s.x, sabotaged_value);
return(sabotaged_s);
}
//------------------------------------------------------------------------------
void prepend_on_death(Field* fd)
{
if (fd->killed_units.empty())
{
return;
}
std::vector<std::tuple<CardStatus*, SkillSpec>> od_skills;
auto & assaults = fd->players[fd->killed_units[0]->m_player]->assaults;
unsigned stacked_poison_value = 0;
unsigned last_index = 99;
CardStatus * left_virulence_victim = nullptr;
for (auto status: fd->killed_units)
{
if (status->m_card->m_type == CardType::assault)
{
// Avenge
for (auto && adj_status: fd->adjacent_assaults(status))
{
unsigned avenge_value = adj_status->skill(Skill::avenge);
if (avenge_value > 0)
{
_DEBUG_MSG(1, "%s activates Avenge %u\n", status_description(adj_status).c_str(), avenge_value);
if (! adj_status->m_sundered)
{ adj_status->m_attack += avenge_value; }
adj_status->m_max_hp += avenge_value;
adj_status->m_hp += avenge_value;
}
}
// Virulence
if (fd->bg_effects.count(PassiveBGE::virulence))
{
if (status->m_index != last_index + 1)
{
stacked_poison_value = 0;
left_virulence_victim = nullptr;
if (status->m_index > 0)
{
auto left_status = &assaults[status->m_index - 1];
if (is_alive(left_status))
{
left_virulence_victim = left_status;
}
}
}
if (status->m_poisoned > 0)
{
if (left_virulence_victim != nullptr)
{
_DEBUG_MSG(1, "Virulence: %s spreads left poison +%u to %s\n",
status_description(status).c_str(), status->m_poisoned,
status_description(left_virulence_victim).c_str());
left_virulence_victim->m_poisoned += status->m_poisoned;
}
stacked_poison_value += status->m_poisoned;
_DEBUG_MSG(1, "Virulence: %s spreads right poison +%u = %u\n",
status_description(status).c_str(), status->m_poisoned, stacked_poison_value);
}
if (status->m_index + 1 < assaults.size())
{
auto right_status = &assaults[status->m_index + 1];
if (is_alive(right_status))
{
_DEBUG_MSG(1, "Virulence: spreads stacked poison +%u to %s\n",
stacked_poison_value, status_description(right_status).c_str());
right_status->m_poisoned += stacked_poison_value;
}
}
last_index = status->m_index;
}
}
// Revenge
if (fd->bg_effects.count(PassiveBGE::revenge))
{
SkillSpec ss_heal{Skill::heal, fd->bg_effects.at(PassiveBGE::revenge), allfactions, 0, 0, Skill::no_skill, Skill::no_skill, true,};
SkillSpec ss_rally{Skill::rally, fd->bg_effects.at(PassiveBGE::revenge), allfactions, 0, 0, Skill::no_skill, Skill::no_skill, true,};
CardStatus * commander = &fd->players[status->m_player]->commander;
_DEBUG_MSG(2, "Revenge: Preparing skill %s and %s\n",
skill_description(ss_heal).c_str(), skill_description(ss_rally).c_str());
od_skills.emplace_back(commander, ss_heal);
od_skills.emplace_back(commander, ss_rally);
}
}
fd->skill_queue.insert(fd->skill_queue.begin(), od_skills.begin(), od_skills.end());
fd->killed_units.clear();
}
//------------------------------------------------------------------------------
void(*skill_table[Skill::num_skills])(Field*, CardStatus* src, const SkillSpec&);
void resolve_skill(Field* fd)
{
while(!fd->skill_queue.empty())
{
auto skill_instance(fd->skill_queue.front());
auto& status(std::get<0>(skill_instance));
const auto& ss(std::get<1>(skill_instance));
fd->skill_queue.pop_front();
if (!is_alive(status))
{
_DEBUG_MSG(2, "%s failed to %s because it is dead.\n",
status_description(status).c_str(), skill_description(ss).c_str());
continue;
}
if (status->m_jammed)
{
_DEBUG_MSG(2, "%s failed to %s because it is Jammed.\n",
status_description(status).c_str(), skill_description(ss).c_str());
continue;
}
SkillSpec modified_s = ss;
// apply evolve
signed evolved_offset = status->m_evolved_skill_offset[modified_s.id];
if (evolved_offset != 0)
{ modified_s = apply_evolve(modified_s, evolved_offset); }
// apply sabotage (only for X-based activation skills)
unsigned sabotaged_value = status->m_sabotaged;
if ((sabotaged_value > 0) && is_activation_skill_with_x(modified_s.id))
{ modified_s = apply_sabotage(modified_s, sabotaged_value); }
// apply enhance
unsigned enhanced_value = status->enhanced(modified_s.id);
if (enhanced_value > 0)
{ modified_s = apply_enhance(modified_s, enhanced_value); }
// perform skill (if it is still applicable)
if (is_activation_skill_with_x(modified_s.id) && !modified_s.x)
{
_DEBUG_MSG(2, "%s failed to %s because its X value is zeroed (sabotaged).\n",
status_description(status).c_str(), skill_description(ss).c_str());
continue;
}
else { skill_table[modified_s.id](fd, status, modified_s); }
}
}
//------------------------------------------------------------------------------
bool attack_phase(Field* fd);
template<Skill::Skill skill_id>
bool check_and_perform_skill(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s, bool is_evadable
#ifndef NQUEST
, bool & has_counted_quest
#endif
);
bool check_and_perform_valor(Field* fd, CardStatus* src);
template <enum CardType::CardType type>
void evaluate_skills(Field* fd, CardStatus* status, const std::vector<SkillSpec>& skills, bool* attacked=nullptr)
{
assert(status);
unsigned num_actions(1);
for (unsigned action_index(0); action_index < num_actions; ++ action_index)
{
fd->prepare_action();
assert(fd->skill_queue.size() == 0);
for (auto & ss: skills)
{
// check if activation skill, assuming activation skills can be evolved from only activation skills
if (skill_table[ss.id] == nullptr)
{
continue;
}
if (status->m_skill_cd[ss.id] > 0)
{
continue;
}
_DEBUG_MSG(2, "Evaluating %s skill %s\n",
status_description(status).c_str(), skill_description(ss).c_str());
fd->skill_queue.emplace_back(status, ss);
resolve_skill(fd);
if(__builtin_expect(fd->end, false)) { break; }
}
if (type == CardType::assault)
{
// Attack
if (can_act(status))
{
if (attack_phase(fd) && !*attacked)
{
*attacked = true;
if (__builtin_expect(fd->end, false)) { break; }
}
}
else
{
_DEBUG_MSG(2, "%s cannot take attack.\n", status_description(status).c_str());
}
}
fd->finalize_action();
// Flurry
if (can_act(status) && is_alive(&fd->tip->commander) && status->has_skill(Skill::flurry) && status->m_skill_cd[Skill::flurry] == 0)
{
#ifndef NQUEST
if (status->m_player == 0)
{
fd->inc_counter(QuestType::skill_use, Skill::flurry);
}
#endif
_DEBUG_MSG(1, "%s activates Flurry x %d\n",
status_description(status).c_str(), status->skill_base_value(Skill::flurry));
num_actions += status->skill_base_value(Skill::flurry);
for (const auto & ss : skills)
{
Skill::Skill evolved_skill_id = static_cast<Skill::Skill>(ss.id + status->m_evolved_skill_offset[ss.id]);
if (evolved_skill_id == Skill::flurry)
{
status->m_skill_cd[ss.id] = ss.c;
}
}
}
}
}
struct PlayCard
{
const Card* card;
Field* fd;
CardStatus* status;
Storage<CardStatus>* storage;
PlayCard(const Card* card_, Field* fd_) :
card{card_},
fd{fd_},
status{nullptr},
storage{nullptr}
{}
template <enum CardType::CardType type>
CardStatus* op()
{
setStorage<type>();
placeCard<type>();
return(status);
}
template <enum CardType::CardType>
void setStorage()
{
}
template <enum CardType::CardType type>
void placeCard()
{
status = &storage->add_back();
status->set(card);
status->m_index = storage->size() - 1;
status->m_player = fd->tapi;
#ifndef NQUEST
if (status->m_player == 0)
{
if (status->m_card->m_type == CardType::assault)
{
fd->inc_counter(QuestType::faction_assault_card_use, card->m_faction);
}
fd->inc_counter(QuestType::type_card_use, type);
}
#endif
_DEBUG_MSG(1, "%s plays %s %u [%s]\n",
status_description(&fd->tap->commander).c_str(), cardtype_names[type].c_str(),
static_cast<unsigned>(storage->size() - 1), card_description(fd->cards, card).c_str());
if (status->m_delay == 0)
{
check_and_perform_valor(fd, status);
}
}
};
// assault
template <>
void PlayCard::setStorage<CardType::assault>()
{
storage = &fd->tap->assaults;
}
// structure
template <>
void PlayCard::setStorage<CardType::structure>()
{
storage = &fd->tap->structures;
}
// Check if a skill actually proc'ed.
template<Skill::Skill>
inline bool skill_check(Field* fd, CardStatus* c, CardStatus* ref)
{ return(true); }
template<>
inline bool skill_check<Skill::evade>(Field* fd, CardStatus* c, CardStatus* ref)
{
return(c->m_player != ref->m_player);
}
template<>
inline bool skill_check<Skill::leech>(Field* fd, CardStatus* c, CardStatus* ref)
{
return(can_be_healed(c));
}
template<>
inline bool skill_check<Skill::coalition>(Field* fd, CardStatus* c, CardStatus* ref)
{
return(is_active(c));
}
template<>
inline bool skill_check<Skill::legion>(Field* fd, CardStatus* c, CardStatus* ref)
{
return(is_active(c));
}
template<>
inline bool skill_check<Skill::payback>(Field* fd, CardStatus* c, CardStatus* ref)
{
return(ref->m_card->m_type == CardType::assault && is_alive(ref));
}
template<>
inline bool skill_check<Skill::revenge>(Field* fd, CardStatus* c, CardStatus* ref)
{
return skill_check<Skill::payback>(fd, c, ref);
}
template<>
inline bool skill_check<Skill::refresh>(Field* fd, CardStatus* c, CardStatus* ref)
{
return(can_be_healed(c));
}
template<>
inline bool skill_check<Skill::drain>(Field* fd, CardStatus* c, CardStatus* ref)
{
return(can_be_healed(c));
}
void remove_hp(Field* fd, CardStatus* status, unsigned dmg)
{
if (__builtin_expect(!dmg, false)) { return; }
assert(is_alive(status));
_DEBUG_MSG(2, "%s takes %u damage\n", status_description(status).c_str(), dmg);
status->m_hp = safe_minus(status->m_hp, dmg);
if (fd->current_phase < Field::end_phase && status->has_skill(Skill::barrier))
{
++ fd->damaged_units_to_times[status];
_DEBUG_MSG(2, "%s damaged %u times\n",
status_description(status).c_str(), fd->damaged_units_to_times[status]);
}
if (status->m_hp == 0)
{
#ifndef NQUEST
if (status->m_player == 1)
{
if (status->m_card->m_type == CardType::assault)
{
fd->inc_counter(QuestType::faction_assault_card_kill, status->m_card->m_faction);
}
fd->inc_counter(QuestType::type_card_kill, status->m_card->m_type);
}
#endif
_DEBUG_MSG(1, "%s dies\n", status_description(status).c_str());
if(status->m_card->m_type != CardType::commander)
{
fd->killed_units.push_back(status);
++ fd->players[status->m_player]->total_cards_destroyed;
}
if (status->m_player == 0 && fd->players[0]->deck->vip_cards.count(status->m_card->m_id))
{
fd->players[0]->commander.m_hp = 0;
fd->end = true;
}
}
}
inline bool is_it_dead(CardStatus& c)
{
if(c.m_hp == 0) // yes it is
{
_DEBUG_MSG(1, "Dead and removed: %s\n", status_description(&c).c_str());
return(true);
}
else { return(false); } // nope still kickin'
}
inline void remove_dead(Storage<CardStatus>& storage)
{
storage.remove(is_it_dead);
}
inline void add_hp(Field* fd, CardStatus* target, unsigned v)
{
target->m_hp = std::min(target->m_hp + v, target->m_max_hp);
}
void cooldown_skills(CardStatus * status)
{
for (const auto & ss : status->m_card->m_skills)
{
if (status->m_skill_cd[ss.id] > 0)
{
_DEBUG_MSG(2, "%s reduces timer (%u) of skill %s\n",
status_description(status).c_str(), status->m_skill_cd[ss.id], skill_names[ss.id].c_str());
-- status->m_skill_cd[ss.id];
}
}
}
void turn_start_phase(Field* fd)
{
// Active player's commander card:
cooldown_skills(&fd->tap->commander);
// Active player's assault cards:
// update index
// reduce delay; reduce skill cooldown
{
auto& assaults(fd->tap->assaults);
for(unsigned index(0), end(assaults.size());
index < end;
++index)
{
CardStatus * status = &assaults[index];
status->m_index = index;
if(status->m_delay > 0)
{
_DEBUG_MSG(1, "%s reduces its timer\n", status_description(status).c_str());
-- status->m_delay;
if (status->m_delay == 0)
{
check_and_perform_valor(fd, status);
}
}
else
{
cooldown_skills(status);
}
}
}
// Active player's structure cards:
// update index
// reduce delay; reduce skill cooldown
{
auto& structures(fd->tap->structures);
for(unsigned index(0), end(structures.size());
index < end;
++index)
{
CardStatus * status = &structures[index];
status->m_index = index;
if(status->m_delay > 0)
{
_DEBUG_MSG(1, "%s reduces its timer\n", status_description(status).c_str());
--status->m_delay;
}
else
{
cooldown_skills(status);
}
}
}
// Defending player's assault cards:
// update index
{
auto& assaults(fd->tip->assaults);
for(unsigned index(0), end(assaults.size());
index < end;
++index)
{
CardStatus& status(assaults[index]);
status.m_index = index;
}
}
// Defending player's structure cards:
// update index
{
auto& structures(fd->tip->structures);
for(unsigned index(0), end(structures.size());
index < end;
++index)
{
CardStatus& status(structures[index]);
status.m_index = index;
}
}
}
void turn_end_phase(Field* fd)
{
// Inactive player's assault cards:
{
auto& assaults(fd->tip->assaults);
for(unsigned index(0), end(assaults.size()); index < end; ++ index)
{
CardStatus& status(assaults[index]);
if (status.m_hp <= 0)
{
continue;
}
status.m_enfeebled = 0;
status.m_protected = 0;
std::memset(status.m_primary_skill_offset, 0, sizeof status.m_primary_skill_offset);
std::memset(status.m_evolved_skill_offset, 0, sizeof status.m_evolved_skill_offset);
std::memset(status.m_enhanced_value, 0, sizeof status.m_enhanced_value);
status.m_evaded = 0; // so far only useful in Inactive turn
status.m_paybacked = 0; // ditto
}
}
// Inactive player's structure cards:
{
auto& structures(fd->tip->structures);
for(unsigned index(0), end(structures.size()); index < end; ++ index)
{
CardStatus& status(structures[index]);
if (status.m_hp <= 0)
{
continue;
}
status.m_evaded = 0; // so far only useful in Inactive turn
}
}
// Active player's assault cards:
{
auto& assaults(fd->tap->assaults);
for(unsigned index(0), end(assaults.size()); index < end; ++ index)
{
CardStatus& status(assaults[index]);
if (status.m_hp <= 0)
{
continue;
}
unsigned refresh_value = status.skill(Skill::refresh);
if (refresh_value > 0 && skill_check<Skill::refresh>(fd, &status, nullptr))
{
_DEBUG_MSG(1, "%s refreshes %u health\n", status_description(&status).c_str(), refresh_value);
add_hp(fd, &status, refresh_value);
}
if (status.m_poisoned > 0)
{
unsigned poison_dmg = safe_minus(status.m_poisoned + status.m_enfeebled, status.protected_value());
if (poison_dmg > 0)
{
#ifndef NQUEST
if (status.m_player == 1)
{
fd->inc_counter(QuestType::skill_damage, Skill::poison, 0, poison_dmg);
}
#endif
_DEBUG_MSG(1, "%s takes poison damage %u\n", status_description(&status).c_str(), poison_dmg);
remove_hp(fd, &status, poison_dmg); // simultaneous
}
}
// end of the opponent's next turn for enemy units
status.m_jammed = false;
status.m_rallied = 0;
status.m_enraged = 0;
status.m_derallied = 0;
status.m_sundered = false;
status.m_weakened = 0;
status.m_inhibited = 0;
status.m_sabotaged = 0;
status.m_overloaded = false;
status.m_step = CardStep::none;
}
}
// Active player's structure cards:
// nothing so far
prepend_on_death(fd); // poison
resolve_skill(fd);
remove_dead(fd->tap->assaults);
remove_dead(fd->tap->structures);
remove_dead(fd->tip->assaults);
remove_dead(fd->tip->structures);
}
//---------------------- $50 attack by assault card implementation -------------
// Counter damage dealt to the attacker (att) by defender (def)
// pre-condition: only valid if m_card->m_counter > 0
inline unsigned counter_damage(Field* fd, CardStatus* att, CardStatus* def)
{
assert(att->m_card->m_type == CardType::assault);
return(safe_minus(def->skill(Skill::counter) + att->m_enfeebled, att->protected_value()));
}
inline CardStatus* select_first_enemy_wall(Field* fd)
{
for(unsigned i(0); i < fd->tip->structures.size(); ++i)
{
CardStatus& c(fd->tip->structures[i]);
if(c.has_skill(Skill::wall) && is_alive(&c) && skill_check<Skill::wall>(fd, &c, nullptr))
{
return(&c);
}
}
return(nullptr);
}
inline bool alive_assault(Storage<CardStatus>& assaults, unsigned index)
{
return(assaults.size() > index && is_alive(&assaults[index]));
}
void remove_commander_hp(Field* fd, CardStatus& status, unsigned dmg, bool count_points)
{
//assert(status.m_hp > 0);
assert(status.m_card->m_type == CardType::commander);
_DEBUG_MSG(2, "%s takes %u damage\n", status_description(&status).c_str(), dmg);
status.m_hp = safe_minus(status.m_hp, dmg);
if(status.m_hp == 0)
{
_DEBUG_MSG(1, "%s dies\n", status_description(&status).c_str());
fd->end = true;
}
}
//------------------------------------------------------------------------------
// implementation of one attack by an assault card, against either an enemy
// assault card, the first enemy wall, or the enemy commander.
struct PerformAttack
{
Field* fd;
CardStatus* att_status;
CardStatus* def_status;
unsigned att_dmg;
PerformAttack(Field* fd_, CardStatus* att_status_, CardStatus* def_status_) :
fd(fd_), att_status(att_status_), def_status(def_status_), att_dmg(0)
{}
template<enum CardType::CardType def_cardtype>
unsigned op()
{
unsigned pre_modifier_dmg = attack_power(att_status);
// Evaluation order:
// modify damage
// deal damage
// assaults only: (poison)
// counter, berserk
// assaults only: (leech if still alive)
modify_attack_damage<def_cardtype>(pre_modifier_dmg);
if (att_dmg == 0) { return 0; }
attack_damage<def_cardtype>();
if(__builtin_expect(fd->end, false)) { return att_dmg; }
damage_dependant_pre_oa<def_cardtype>();
// Enemy Skill: Counter
if (def_status->has_skill(Skill::counter) && skill_check<Skill::counter>(fd, def_status, att_status))
{