forked from brikikeks/tyrant_optimize
-
Notifications
You must be signed in to change notification settings - Fork 7
/
sim.cpp
4026 lines (3719 loc) · 159 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 "card.h"
#include "cards.h"
#include "deck.h"
template<enum CardType::CardType def_cardtype>
void perform_bge_devour(Field* fd, CardStatus* att_status, CardStatus* def_status, unsigned att_dmg);
template<enum CardType::CardType def_cardtype>
void perform_bge_heroism(Field* fd, CardStatus* att_status, CardStatus* def_status, unsigned att_dmg);
void perform_poison(Field* fd, CardStatus* att_status, CardStatus* def_status);
void perform_corrosive(Field* fd, CardStatus* att_status, CardStatus* def_status);
void perform_mark(Field* fd, CardStatus* att_status, CardStatus* def_status);
void perform_berserk(Field* fd, CardStatus* att_status, CardStatus* def_status);
template<enum CardType::CardType def_cardtype>
void perform_counter(Field* fd, CardStatus* att_status, CardStatus* def_status);
void perform_hunt(Field* fd, CardStatus* att_status, CardStatus* def_status);
void perform_subdue(Field* fd, CardStatus* att_status, CardStatus* def_status);
bool check_and_perform_valor(Field* fd, CardStatus* src);
bool check_and_perform_bravery(Field* fd, CardStatus* src);
bool check_and_perform_early_enhance(Field* fd, CardStatus* src);
bool check_and_perform_later_enhance(Field* fd, CardStatus* src);
CardStatus* check_and_perform_summon(Field* fd, CardStatus* src);
//------------------------------------------------------------------------------
inline unsigned remove_absorption(Field* fd, CardStatus* status, unsigned dmg);
inline unsigned remove_absorption(CardStatus* status, unsigned dmg);
inline unsigned remove_disease(CardStatus* status, unsigned heal);
//------------------------------------------------------------------------------
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); }
inline bool will_activate_this_turn(CardStatus* c) { return is_active(c) && ((c->m_step == CardStep::none) || (c->m_step == CardStep::attacking && c->has_skill(Skill::flurry) && c->m_action_index < c->skill_base_value(Skill::flurry)));}
// 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;
}
//------------------------------------------------------------------------------
/**
* @brief Maximum health.
* This takes subduing into account by reducing the permanent health buffs by subdue.
*
* @return unsigned maximum health.
*/
inline unsigned CardStatus::max_hp() const
{
return (m_card->m_health + safe_minus(m_perm_health_buff, m_subdued));
}
/** @brief Increase of current health.
* This takes disease into account and removes as needed.
*
* @param [in] value increase of health.
* @return applied increase of health.
*/
inline unsigned CardStatus::add_hp(unsigned value)
{
value = remove_disease(this,value);
return (m_hp = std::min(m_hp + value, max_hp()));
}
/** @brief Permanent increase of maximum health.
* This takes disease into account and removes as needed.
* The increase of the maximum health entails an increase of current health by the same amount.
*
* @param [in] value increase of maximum health.
* @return applied increase of maximum health.
*/
inline unsigned CardStatus::ext_hp(unsigned value)
{
value = remove_disease(this,value);
m_perm_health_buff += value;
// we can safely call add_hp without worring about the second call to remove_disease because
// the first call will have already removed the disease or the value will be 0
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_action_index=0;
m_player = 0;
m_delay = card.m_delay;
m_hp = card.m_health;
m_absorption = 0;
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_marked = 0;
m_diseased = 0;
m_rush_attempted = false;
m_sundered = false;
//APN
m_summoned = 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);
}
//------------------------------------------------------------------------------
/**
* @brief Calculate the attack power of the CardStatus.
*
* @return unsigned
*/
inline unsigned CardStatus::attack_power() const
{
signed attack = calc_attack_power();
if(__builtin_expect(attack <0,false))
{
std::cout << m_card->m_name << " " << m_card->m_attack << " " << attack << " " << m_temp_attack_buff << " " << m_corroded_weakened << std::endl;
}
_DEBUG_ASSERT(attack >= 0);
return (unsigned)attack;
}
/**
* @brief Calculate the attack power of the CardStatus.
*
* The attack power is the base attack plus.
* The subdued value gets subtracted from the permanent attack buff, if this is above zero it is added to the attack power.
* The corroded value gets subtracted from the attack power, but not below zero.
* Finally the temporary attack buff is added.
*
* @return signed attack power.
*/
inline signed CardStatus::calc_attack_power() const
{
if(__builtin_expect(this->m_field->fixes[Fix::corrosive_protect_armor],true)) // MK - 05/01/2023 6:58 AM: "Corrosive now counts as temporary attack reduction instead of permanent, so it is calculated after Subdue and can now counter Rally even if the permanent attack is zeroed."
{
return
(signed)safe_minus(
m_card->m_attack + safe_minus(m_perm_attack_buff, m_subdued)+ m_temp_attack_buff,
m_corroded_weakened
);
}
else{
return
(signed)safe_minus(
m_card->m_attack + safe_minus(m_perm_attack_buff, m_subdued),
m_corroded_weakened
)
+ m_temp_attack_buff;
}
}
//------------------------------------------------------------------------------
const Card* card_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()) assert(false);//"UnknownCard.id[" + to_string(card_id) + "]"); }
return cardIter->second;
}
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[" + tuo::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 += ": " + tuo::to_string(c->m_attack) + "/" + tuo::to_string(c->m_health) + "/" + tuo::to_string(c->m_delay);
break;
case CardType::structure:
desc += ": " + tuo::to_string(c->m_health) + "/" + tuo::to_string(c->m_delay);
break;
case CardType::commander:
desc += ": hp:" + tuo::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" + tuo::to_string(m_player) + " ";
switch(m_card->m_type)
{
case CardType::commander: desc += "Commander "; break;
case CardType::assault: desc += "Assault " + tuo::to_string(m_index) + " "; break;
case CardType::structure: desc += "Structure " + tuo::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:[[" + tuo::to_string(m_card->m_attack) + "(base)";
if (m_perm_attack_buff)
{
desc += "+[" + tuo::to_string(m_perm_attack_buff) + "(perm)";
if (m_subdued) { desc += "-" + tuo::to_string(m_subdued) + "(subd)"; }
desc += "]";
}
if (m_corroded_weakened) { desc += "-" + tuo::to_string(m_corroded_weakened) + "(corr)"; }
desc += "]";
if (m_temp_attack_buff) { desc += (m_temp_attack_buff > 0 ? "+" : "") + tuo::to_string(m_temp_attack_buff) + "(temp)"; }
desc += "]=" + tuo::to_string(attack_power());
case CardType::structure:
case CardType::commander:
desc += " hp:" + tuo::to_string(m_hp);
if(m_absorption)desc += " absorb:" + tuo::to_string(m_absorption);
break;
case CardType::num_cardtypes:
assert(false);
break;
}
if (m_delay) { desc += " cd:" + tuo::to_string(m_delay); }
// Status w/o value
if (m_jammed) { desc += ", jammed"; }
if (m_overloaded) { desc += ", overloaded"; }
if (m_sundered) { desc += ", sundered"; }
if (m_summoned) { desc+= ", summoned"; }
// Status w/ value
if (m_corroded_weakened || m_corroded_rate) { desc += ", corroded " + tuo::to_string(m_corroded_weakened) + " (rate: " + tuo::to_string(m_corroded_rate) + ")"; }
if (m_subdued) { desc += ", subdued " + tuo::to_string(m_subdued); }
if (m_enfeebled) { desc += ", enfeebled " + tuo::to_string(m_enfeebled); }
if (m_inhibited) { desc += ", inhibited " + tuo::to_string(m_inhibited); }
if (m_sabotaged) { desc += ", sabotaged " + tuo::to_string(m_sabotaged); }
if (m_poisoned) { desc += ", poisoned " + tuo::to_string(m_poisoned); }
if (m_protected) { desc += ", protected " + tuo::to_string(m_protected); }
if (m_protected_stasis) { desc += ", stasis " + tuo::to_string(m_protected_stasis); }
if (m_enraged) { desc += ", enraged " + tuo::to_string(m_enraged); }
if (m_entrapped) { desc += ", entrapped " + tuo::to_string(m_entrapped); }
if (m_marked) { desc += ", marked " + tuo::to_string(m_marked); }
if (m_diseased) { desc += ", diseased " + tuo::to_string(m_diseased); }
// 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 += " +" + tuo::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;
total_nonsummon_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);
}
//------------------------------------------------------------------------------
inline void resolve_scavenge(Storage<CardStatus>& store)
{
for(auto status : store.m_indirect)
{
if(!is_alive(status))continue;
unsigned scavenge_value = status->skill(Skill::scavenge);
if(!scavenge_value)continue;
_DEBUG_MSG(1, "%s activates Scavenge %u\n",
status_description(status).c_str(), scavenge_value);
status->ext_hp(scavenge_value);
}
}
//------------------------------------------------------------------------------
/**
* @brief Handle death of a card
*
* @param fd Field pointer
* @param paybacked Is the death caused by payback?
*/
void prepend_on_death(Field* fd, bool paybacked=false)
{
if (fd->killed_units.empty())
return;
bool skip_all_except_summon = fd->fixes[Fix::death_from_bge] && (fd->current_phase == Field::bge_phase);
if (skip_all_except_summon)
{
_DEBUG_MSG(2, "Death from BGE Fix (skip all death depended triggers except summon)\n");
}
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)
{
// Skill: Scavenge
// Any unit dies => perm-hp-buff
if (__builtin_expect(!skip_all_except_summon, true))
{
resolve_scavenge(fd->players[0]->assaults);
resolve_scavenge(fd->players[1]->assaults);
resolve_scavenge(fd->players[0]->structures);
resolve_scavenge(fd->players[1]->structures);
}
if ((status->m_card->m_type == CardType::assault) && (!skip_all_except_summon))
{
// Skill: Avenge
const unsigned host_idx = status->m_index;
unsigned from_idx, till_idx;
//scan all assaults for Avenge
from_idx = 0;
till_idx = 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; }
// Use half value rounded up
// (for distance > 1, i. e. non-standard Avenge triggering)
if (std::abs((signed)from_idx - (signed)host_idx) > 1)
{
avenge_value = (avenge_value + 1) / 2;
}
_DEBUG_MSG(1, "%s activates %sAvenge %u\n",
status_description(adj_status).c_str(),
(std::abs((signed)from_idx - (signed)host_idx) > 1 ? "Half-" : ""),
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
// Fix::death_from_bge: should not affect passive BGE, keep it as was before
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 (auto& ss: status->m_card->m_skills_on_death)
{
if (__builtin_expect(skip_all_except_summon && (ss.id != Skill::summon), false))
{ continue; }
SkillSpec tss = ss;
_DEBUG_MSG(2, "On Death %s: Preparing (tail) skill %s\n",
status_description(status).c_str(), skill_description(fd->cards, ss).c_str());
if(fd->fixes[Fix::revenge_on_death] && is_activation_harmful_skill(ss.id) && paybacked)
{
_DEBUG_MSG(2, "On Death Revenge Fix\n");
tss.s2 = Skill::revenge;
}
fd->skill_queue.emplace_back(status, tss);
}
}
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) || ss.id == Skill::enhance); // enhance is no trigger, but queues the skill
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); }
}
}
void apply_corrosion(CardStatus * status)
{
if (status->m_corroded_rate)
{
unsigned v = std::min(status->m_corroded_rate, status->attack_power());
unsigned corrosion = std::min(v, status->m_card->m_attack
+ status->m_perm_attack_buff - status->m_corroded_weakened);
_DEBUG_MSG(1, "%s loses Attack by %u (+corrosion %u).\n", status_description(status).c_str(), v, corrosion);
status->m_corroded_weakened += corrosion;
}
}
void remove_corrosion(CardStatus * status)
{
if (status->m_corroded_rate)
{
_DEBUG_MSG(1, "%s loses Status corroded.\n", status_description(status).c_str());
status->m_corroded_rate = 0;
status->m_corroded_weakened = 0;
}
}
//------------------------------------------------------------------------------
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)
{
status->m_action_index = 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; }
//Apply corrosion
apply_corrosion(status);
}
else
{
// Remove Corrosion
remove_corrosion(status);
}
}
else
{
_DEBUG_MSG(2, "%s cannot take attack.\n", status_description(status).c_str());
// Remove Corrosion
remove_corrosion(status);
}
}
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()
{
return op<type>(false);
}
template <enum CardType::CardType type>
CardStatus* op(bool summoned)
{
setStorage<type>();
placeCard<type>();
status->m_summoned = summoned;
unsigned played_faction_mask(0);
unsigned same_faction_cards_count(0);
bool bge_megamorphosis = fd->bg_effects[status->m_player][PassiveBGE::megamorphosis];
//played_status = status;
//played_card = card;
if(__builtin_expect(fd->fixes[Fix::barrier_each_turn],true) && status->has_skill(Skill::barrier)){
_DEBUG_MSG(1,"%s gets barrier protection %u per turn\n",status_description(status).c_str(),status->skill(Skill::barrier));
status->m_protected += status->skill(Skill::barrier);
}
if (status->m_delay == 0)
{
check_and_perform_bravery(fd,status);
check_and_perform_valor(fd, status);
}
//refresh/init absorb
if(status->has_skill(Skill::absorb))
{
status->m_absorption = status->skill_base_value(Skill::absorb);
}
// 1. Evaluate skill Allegiance & count assaults with same faction (structures will be counted later)
// 2. Passive BGE Cold Sleep
for (CardStatus* status_i : fd->players[status->m_player]->assaults.m_indirect)
{
if (status_i == status || !is_alive(status_i)) { continue; } // except itself
//std::cout << status_description(status_i).c_str();
_DEBUG_ASSERT(is_alive(status_i));
if (bge_megamorphosis || (status_i->m_card->m_faction == card->m_faction))
{
++ same_faction_cards_count;
unsigned allegiance_value = status_i->skill(Skill::allegiance);
if (__builtin_expect(allegiance_value, false) && !status->m_summoned)
{
_DEBUG_MSG(1, "%s activates Allegiance %u\n", status_description(status_i).c_str(), allegiance_value);
if (! status_i->m_sundered)
{ status_i->m_perm_attack_buff += allegiance_value; }
status_i->ext_hp(allegiance_value);
}
}
if (__builtin_expect(fd->bg_effects[status->m_player][PassiveBGE::coldsleep], false)
&& status_i->m_protected_stasis && can_be_healed(status_i))
{
unsigned bge_value = (status_i->m_protected_stasis + 1) / 2;
_DEBUG_MSG(1, "Cold Sleep: %s heals itself for %u\n", status_description(status_i).c_str(), bge_value);
status_i->add_hp(bge_value);
}
}
// Setup faction marks (bitmap) for stasis (skill Stasis / Passive BGE TemporalBacklash)
// unless Passive BGE Megamorphosis is enabled
if (__builtin_expect(!bge_megamorphosis, true))
{
played_faction_mask = (1u << card->m_faction);
// do played card have stasis? mark this faction for stasis check
if (__builtin_expect(status->skill(Skill::stasis), false)
|| __builtin_expect(fd->bg_effects[status->m_player][PassiveBGE::temporalbacklash] && status->skill(Skill::counter), false))
{
fd->players[status->m_player]->stasis_faction_bitmap |= played_faction_mask;
}
}
// Evaluate Passive BGE Oath-of-Loyalty
unsigned allegiance_value;
if (__builtin_expect(fd->bg_effects[status->m_player][PassiveBGE::oath_of_loyalty], false)
&& ((allegiance_value = status->skill(Skill::allegiance)) > 0))
{
// count structures with same faction (except fortresses, dominions and other non-normal structures)
for (CardStatus * status_i : fd->players[status->m_player]->structures.m_indirect)
{
if ((status_i->m_card->m_category == CardCategory::normal)
&& (bge_megamorphosis || (status_i->m_card->m_faction == card->m_faction)))
{
++ same_faction_cards_count;
}
}
// apply Passive BGE Oath-of-Loyalty when multiplier isn't zero
if (same_faction_cards_count)
{
unsigned bge_value = allegiance_value * same_faction_cards_count;
_DEBUG_MSG(1, "Oath of Loyalty: %s activates Allegiance %u x %u = %u\n",
status_description(status).c_str(), allegiance_value, same_faction_cards_count, bge_value);
status->m_perm_attack_buff += bge_value;
status->ext_hp(bge_value);
}
}
// summarize stasis when:
// 1. Passive BGE Megamorphosis is enabled
// 2. current faction is marked for it
if ((card->m_delay > 0) && (card->m_type == CardType::assault)
&& __builtin_expect(bge_megamorphosis || (fd->players[status->m_player]->stasis_faction_bitmap & played_faction_mask), false))
{
unsigned stacked_stasis = (bge_megamorphosis || (fd->players[status->m_player]->commander.m_card->m_faction == card->m_faction))
? fd->players[status->m_player]->commander.skill(Skill::stasis)
: 0u;
#ifndef NDEBUG
if (stacked_stasis > 0)
{
_DEBUG_MSG(2, "+ Stasis [%s]: stacks +%u stasis protection from %s (total stacked: %u)\n",
faction_names[card->m_faction].c_str(), stacked_stasis,
status_description(&fd->players[status->m_player]->commander).c_str(), stacked_stasis);
}
#endif
for (CardStatus * status_i : fd->players[status->m_player]->structures.m_indirect)
{
if ((bge_megamorphosis || (status_i->m_card->m_faction == card->m_faction)) && is_alive(status_i))
{
stacked_stasis += status_i->skill(Skill::stasis);
#ifndef NDEBUG
if (status_i->skill(Skill::stasis) > 0)
{
_DEBUG_MSG(2, "+ Stasis [%s]: stacks +%u stasis protection from %s (total stacked: %u)\n",
faction_names[card->m_faction].c_str(), status_i->skill(Skill::stasis),
status_description(status_i).c_str(), stacked_stasis);
}
#endif
}
}
for (CardStatus * status_i : fd->players[status->m_player]->assaults.m_indirect)
{
if ((bge_megamorphosis || (status_i->m_card->m_faction == card->m_faction)) && is_alive(status_i))
{
stacked_stasis += status_i->skill(Skill::stasis);
#ifndef NDEBUG
if (status_i->skill(Skill::stasis) > 0)
{
_DEBUG_MSG(2, "+ Stasis [%s]: stacks +%u stasis protection from %s (total stacked: %u)\n",
faction_names[card->m_faction].c_str(), status_i->skill(Skill::stasis),
status_description(status_i).c_str(), stacked_stasis);
}
#endif
if (__builtin_expect(fd->bg_effects[status->m_player][PassiveBGE::temporalbacklash] && status_i->skill(Skill::counter), false))
{
stacked_stasis += (status_i->skill(Skill::counter) + 1) / 2;
#ifndef NDEBUG
_DEBUG_MSG(2, "Temporal Backlash: + Stasis [%s]: stacks +%u stasis protection from %s (total stacked: %u)\n",
faction_names[card->m_faction].c_str(), (status_i->skill(Skill::counter) + 1) / 2,
status_description(status_i).c_str(), stacked_stasis);
#endif
}
}
}
status->m_protected_stasis = stacked_stasis;
#ifndef NDEBUG
if (stacked_stasis > 0)
{
_DEBUG_MSG(1, "%s gains %u stasis protection\n",
status_description(status).c_str(), stacked_stasis);
}