forked from dsuchka/tyrant_optimize_closed
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sim.cpp
3011 lines (2777 loc) · 115 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 <cmath>
#include "tyrant.h"
#include "card.h"
#include "cards.h"
#include "deck.h"
bool check_and_perform_valor(Field* fd, CardStatus* src);
CardStatus* check_and_perform_summon(Field* fd, CardStatus* src);
//------------------------------------------------------------------------------
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->max_hp()); }
// Strange Transmission [Gilians] features
#ifdef TUO_GILIAN
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)); }
#else
# define is_gilian(c) (false)
# define is_alive_gilian(c) (false)
#endif
//------------------------------------------------------------------------------
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 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 protects itself for %u (barrier %u x %u damage taken)\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)
+ (skill_id == Skill::counter ? m_entrapped : 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 unsigned CardStatus::max_hp() const
{
return (m_card->m_health + safe_minus(m_perm_health_buff, m_subdued));
}
//------------------------------------------------------------------------------
inline unsigned CardStatus::add_hp(unsigned value)
{
return (m_hp = std::min(m_hp + value, max_hp()));
}
//------------------------------------------------------------------------------
inline unsigned CardStatus::ext_hp(unsigned value)
{
m_perm_health_buff += value;
return add_hp(value);
}
//------------------------------------------------------------------------------
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_hp = card.m_health;
m_step = CardStep::none;
m_perm_health_buff = 0;
m_perm_attack_buff = 0;
m_temp_attack_buff = 0;
m_corroded_rate = 0;
m_corroded_weakened = 0;
m_subdued = 0;
m_enfeebled = 0;
m_evaded = 0;
m_inhibited = 0;
m_sabotaged = 0;
m_jammed = false;
m_overloaded = false;
m_paybacked = 0;
m_tributed = 0;
m_poisoned = 0;
m_protected = 0;
m_protected_stasis = 0;
m_enraged = 0;
m_entrapped = 0;
m_rush_attempted = false;
m_sundered = false;
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 CardStatus::attack_power() const
{
signed attack =
(signed)safe_minus(
m_card->m_attack + safe_minus(m_perm_attack_buff, m_subdued),
m_corroded_weakened
)
+ m_temp_attack_buff;
_DEBUG_ASSERT(attack >= 0);
return (unsigned)attack;
}
//------------------------------------------------------------------------------
std::string card_name_by_id_safe(const Cards& cards, const unsigned card_id)
{
const auto cardIter = cards.cards_by_id.find(card_id);
if (cardIter == cards.cards_by_id.end()) { return "UnknownCard.id[" + to_string(card_id) + "]"; }
return cardIter->second->m_name;
}
//------------------------------------------------------------------------------
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_on_play) { desc += ", " + skill_description(cards, skill, Skill::Trigger::play); }
for (auto& skill: c->m_skills) { desc += ", " + skill_description(cards, skill, Skill::Trigger::activate); }
//APN
for (auto& skill: c->m_skills_on_attacked) { desc += ", " + skill_description(cards, skill, Skill::Trigger::attacked); }
for (auto& skill: c->m_skills_on_death) { desc += ", " + skill_description(cards, skill, Skill::Trigger::death); }
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_card->m_attack) + "(base)";
if (m_perm_attack_buff)
{
desc += "+[" + to_string(m_perm_attack_buff) + "(perm)";
if (m_subdued) { desc += "-" + to_string(m_subdued) + "(subd)"; }
desc += "]";
}
if (m_corroded_weakened) { desc += "-" + to_string(m_corroded_weakened) + "(corr)"; }
desc += "]";
if (m_temp_attack_buff) { desc += (m_temp_attack_buff > 0 ? "+" : "") + to_string(m_temp_attack_buff) + "(temp)"; }
desc += "]=" + to_string(attack_power());
case CardType::structure:
case CardType::commander:
desc += " hp:" + to_string(m_hp);
break;
case CardType::num_cardtypes:
assert(false);
break;
}
if (m_delay) { 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_weakened || m_corroded_rate) { desc += ", corroded " + to_string(m_corroded_weakened) + " (rate: " + to_string(m_corroded_rate) + ")"; }
if (m_subdued) { desc += ", subdued " + to_string(m_subdued); }
if (m_enfeebled) { desc += ", enfeebled " + to_string(m_enfeebled); }
if (m_inhibited) { desc += ", inhibited " + to_string(m_inhibited); }
if (m_sabotaged) { desc += ", sabotaged " + to_string(m_sabotaged); }
if (m_poisoned) { desc += ", poisoned " + to_string(m_poisoned); }
if (m_protected) { desc += ", protected " + to_string(m_protected); }
if (m_protected_stasis) { desc += ", stasis " + to_string(m_protected_stasis); }
if (m_enraged) { desc += ", enraged " + to_string(m_enraged); }
if (m_entrapped) { desc += ", entrapped " + to_string(m_entrapped); }
// if(m_step != CardStep::none) { desc += ", Step " + to_string(static_cast<int>(m_step)); }
//APN
const Skill::Trigger s_triggers[] = { Skill::Trigger::play, Skill::Trigger::activate, Skill::Trigger::death , Skill::Trigger::attacked};
for (const Skill::Trigger& trig: s_triggers)
{
std::vector<SkillSpec> card_skills(
(trig == Skill::Trigger::play) ? m_card->m_skills_on_play :
(trig == Skill::Trigger::activate) ? m_card->m_skills :
//APN
(trig == Skill::Trigger::attacked) ? m_card->m_skills_on_attacked :
(trig == Skill::Trigger::death) ? m_card->m_skills_on_death :
std::vector<SkillSpec>());
// emulate Berserk/Counter by status Enraged/Entrapped unless such skills exist (only for normal skill triggering)
if (trig == Skill::Trigger::activate)
{
if (m_enraged && !std::count_if(card_skills.begin(), card_skills.end(), [](const SkillSpec ss) { return (ss.id == Skill::berserk); }))
{
SkillSpec ss{Skill::berserk, m_enraged, allfactions, 0, 0, Skill::no_skill, Skill::no_skill, false, 0,};
card_skills.emplace_back(ss);
}
if (m_entrapped && !std::count_if(card_skills.begin(), card_skills.end(), [](const SkillSpec ss) { return (ss.id == Skill::counter); }))
{
SkillSpec ss{Skill::counter, m_entrapped, allfactions, 0, 0, Skill::no_skill, Skill::no_skill, false, 0,};
card_skills.emplace_back(ss);
}
}
for (const auto& ss : card_skills)
{
std::string skill_desc;
if (m_evolved_skill_offset[ss.id]) { skill_desc += "->" + skill_names[ss.id + m_evolved_skill_offset[ss.id]]; }
if (m_enhanced_value[ss.id]) { skill_desc += " +" + to_string(m_enhanced_value[ss.id]); }
if (!skill_desc.empty())
{
desc += ", " + (
(trig == Skill::Trigger::play) ? "(On Play)" :
(trig == Skill::Trigger::attacked) ? "(On Attacked)" :
(trig == Skill::Trigger::death) ? "(On Death)" :
std::string("")) + skill_names[ss.id] + skill_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_card->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;
auto& assaults = fd->players[fd->killed_units[0]->m_player]->assaults;
unsigned stacked_poison_value = 0;
unsigned last_index = 99999;
CardStatus* left_virulence_victim = nullptr;
for (auto status: fd->killed_units)
{
if (status->m_card->m_type == CardType::assault)
{
// Skill: Avenge
const unsigned host_idx = status->m_index;
unsigned from_idx, till_idx;
if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::bloodvengeance], false))
{
// Passive BGE Blood Vengeance: scan all assaults for Avenge
from_idx = 0;
till_idx = assaults.size() - 1;
}
else
{
from_idx = safe_minus(host_idx, 1);
till_idx = std::min(host_idx + 1, safe_minus(assaults.size(), 1));
}
for (; from_idx <= till_idx; ++ from_idx)
{
if (from_idx == host_idx) { continue; }
CardStatus* adj_status = &assaults[from_idx];
if (!is_alive(adj_status)) { continue; }
unsigned avenge_value = adj_status->skill(Skill::avenge);
if (!avenge_value) { continue; }
// Passive BGE Blood Vengeance: use half value rounded up
// (for distance > 1, i. e. non-standard Avenge triggering)
if (__builtin_expect((std::abs((signed)from_idx - (signed)host_idx) > 1), false))
{
avenge_value = (avenge_value + 1) / 2;
}
_DEBUG_MSG(1, "%s%s activates Avenge %u\n",
(std::abs((signed)from_idx - (signed)host_idx) > 1 ? "BGE BloodVengeance: " : ""),
status_description(adj_status).c_str(), avenge_value);
if (!adj_status->m_sundered)
{ adj_status->m_perm_attack_buff += avenge_value; }
adj_status->ext_hp(avenge_value);
}
// Passive BGE: Virulence
if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::virulence], false))
{
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;
}
}
// Passive BGE: Revenge
if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::revenge], false))
{
if (fd->bg_effects[fd->tapi][PassiveBGE::revenge] < 0)
throw std::runtime_error("BGE Revenge: value must be defined & positive");
SkillSpec ss_heal{Skill::heal, (unsigned)fd->bg_effects[fd->tapi][PassiveBGE::revenge], allfactions, 0, 0, Skill::no_skill, Skill::no_skill, true, 0,};
SkillSpec ss_rally{Skill::rally, (unsigned)fd->bg_effects[fd->tapi][PassiveBGE::revenge], allfactions, 0, 0, Skill::no_skill, Skill::no_skill, true, 0,};
CardStatus* commander = &fd->players[status->m_player]->commander;
_DEBUG_MSG(2, "Revenge: Preparing (head) skills %s and %s\n",
skill_description(fd->cards, ss_heal).c_str(),
skill_description(fd->cards, ss_rally).c_str());
fd->skill_queue.emplace(fd->skill_queue.begin()+0, commander, ss_heal);
fd->skill_queue.emplace(fd->skill_queue.begin()+1, commander, ss_rally); // +1: keep ss_heal at first place
}
// resolve On-Death skills
for (const auto& ss: status->m_card->m_skills_on_death)
{
_DEBUG_MSG(2, "On Death %s: Preparing (tail) skill %s\n",
status_description(status).c_str(), skill_description(fd->cards, ss).c_str());
fd->skill_queue.emplace_back(status, ss);
}
}
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 (__builtin_expect(status->m_card->m_skill_trigger[ss.id] == Skill::Trigger::activate, true))
{
if (!is_alive(status))
{
_DEBUG_MSG(2, "%s failed to %s because it is dead.\n",
status_description(status).c_str(), skill_description(fd->cards, 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(fd->cards, ss).c_str());
continue;
}
}
// is summon? (non-activation skill)
if (ss.id == Skill::summon)
{
check_and_perform_summon(fd, status);
continue;
}
_DEBUG_ASSERT(is_activation_skill(ss.id));
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(fd->cards, ss).c_str());
continue;
}
else { skill_table[modified_s.id](fd, status, modified_s); }
}
}
//------------------------------------------------------------------------------
bool attack_phase(Field* fd);
template<Skill::Skill skill_id>
inline bool check_and_perform_skill(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s, bool is_evadable
#ifndef NQUEST
, bool & has_counted_quest
#endif
);
template <enum CardType::CardType type>
void evaluate_skills(Field* fd, CardStatus* status, const std::vector<SkillSpec>& skills, bool* attacked=nullptr)
{
_DEBUG_ASSERT(status);
unsigned num_actions(1);
for (unsigned action_index(0); action_index < num_actions; ++ action_index)
{
fd->prepare_action();
_DEBUG_ASSERT(fd->skill_queue.size() == 0);
for (auto & ss: skills)
{
if (!is_activation_skill(ss.id)) { 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(fd->cards, ss).c_str());
fd->skill_queue.emplace_back(status, ss);
resolve_skill(fd);
}
if (type == CardType::assault)
{
// Attack
if (can_act(status))
{
if (attack_phase(fd))
{
*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) && 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;
const unsigned actor_index;
const CardStatus* actor_status;
PlayCard(const Card* card_, Field* fd_, unsigned ai_, CardStatus* as_) :
card{card_},
fd{fd_},
status{nullptr},
storage{nullptr},
actor_index{ai_},
actor_status{as_}
{}
template <enum CardType::CardType type>
CardStatus* op()
{
setStorage<type>();
placeCard<type>();
// resolve On-Play skills
for (const auto& ss: card->m_skills_on_play)
{
_DEBUG_MSG(2, "On Play %s: Preparing (tail) skill %s\n",
status_description(status).c_str(), skill_description(fd->cards, ss).c_str());
fd->skill_queue.emplace_back(status, ss);
}
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 = actor_index;
#ifndef NQUEST
if (actor_index == 0)
{
if (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(actor_status).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->players[actor_index]->assaults;
}
// structure
template <>
void PlayCard::setStorage<CardType::structure>()
{
storage = &fd->players[actor_index]->structures;
}
// Check if a skill actually proc'ed.
template<Skill::Skill skill_id>
inline bool skill_check(Field* fd, CardStatus* c, CardStatus* ref)
{
return is_defensive_skill(skill_id) || is_alive(c);
}
template<>
inline bool skill_check<Skill::heal>(Field* fd, CardStatus* c, CardStatus* ref)
{
return can_be_healed(c);
}
template<>
inline bool skill_check<Skill::mend>(Field* fd, CardStatus* c, CardStatus* ref)
{
return can_be_healed(c);
}
template<>
inline bool skill_check<Skill::rally>(Field* fd, CardStatus* c, CardStatus* ref)
{
return !c->m_sundered;
}
template<>
inline bool skill_check<Skill::overload>(Field* fd, CardStatus* c, CardStatus* ref)
{
return is_active(c) && !c->m_overloaded && !has_attacked(c);
}
template<>
inline bool skill_check<Skill::jam>(Field* fd, CardStatus* c, CardStatus* ref)
{
// active player performs Jam
if (fd->tapi == ref->m_player)
{ return is_active_next_turn(c); }
// inactive player performs Jam
return is_active(c) && (c->m_step == CardStep::none);
}
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::payback>(Field* fd, CardStatus* c, CardStatus* ref)
{
return (ref->m_card->m_type == CardType::assault);
}
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::tribute>(Field* fd, CardStatus* c, CardStatus* ref)
{
return (ref->m_card->m_type == CardType::assault) && (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; }
_DEBUG_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());
_DEBUG_ASSERT(status->m_card->m_type != CardType::commander);
fd->killed_units.push_back(status);
++fd->players[status->m_player]->total_cards_destroyed;
if (__builtin_expect((status->m_player == 0) && (fd->players[0]->deck->vip_cards.count(status->m_card->m_id)), false))
{
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;
}
return false; // nope still kickin'
}
inline bool is_it_dominion(CardStatus* c)
{
return (c->m_card->m_category == CardCategory::dominion_alpha);
}
inline void remove_dead(Storage<CardStatus>& storage)
{
storage.remove(is_it_dead);
}
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);
check_and_perform_summon(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;
if (status->m_delay == 0)
{
check_and_perform_summon(fd, status);
}
}
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
status.m_entrapped = 0;
}
}
// 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
}
}