forked from voxnav/tyrant_optimize
-
Notifications
You must be signed in to change notification settings - Fork 30
/
sim.cpp
1950 lines (1844 loc) · 72.9 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 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 const std::vector<CardStatus *> Field::adjacent_assaults(const CardStatus * status)
{
std::vector<CardStatus *> res;
auto & assaults = this->players[status->m_player]->assaults;
if (status->m_index > 0)
{
auto left_status = &assaults[status->m_index - 1];
if (left_status->m_hp > 0)
{
res.push_back(left_status);
}
}
if (status->m_index + 1 < assaults.size())
{
auto right_status = &assaults[status->m_index + 1];
if (right_status->m_hp > 0)
{
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 unsigned CardStatus::skill_base_value(Skill skill_id) const
{
return m_card->m_skill_value[skill_id + m_primary_skill_offset[skill_id]];
}
//------------------------------------------------------------------------------
inline unsigned CardStatus::skill(Skill skill_id) const
{
return skill_base_value(skill_id) + enhanced(skill_id);
}
//------------------------------------------------------------------------------
inline bool CardStatus::has_skill(Skill skill_id) const
{
return skill_base_value(skill_id);
}
//------------------------------------------------------------------------------
inline unsigned CardStatus::enhanced(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;
}
//------------------------------------------------------------------------------
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_jammed = false;
m_overloaded = false;
m_paybacked = 0;
m_poisoned = 0;
m_protected = 0;
m_rallied = 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(att->m_attack + att->m_rallied, att->m_weakened + att->m_corroded_weakened));
}
//------------------------------------------------------------------------------
std::string skill_description(const Cards& cards, 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 == no_skill ? "" : std::string(" ") + skill_names[s.s]) +
(s.s2 == 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 == no_skill ? "" : std::string(" ") + skill_names[s.s]) +
(s.s2 == 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(cards, 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_rallied > 0) { att_desc += "+" + to_string(m_rallied) + "(rallied)"; }
if(m_weakened > 0) { att_desc += "-" + to_string(m_weakened) + "(weakened)"; }
if(m_corroded_weakened > 0) { att_desc += "-" + to_string(m_corroded_weakened) + "(corroded)"; }
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_poisoned > 0) { desc += ", poisoned " + to_string(m_poisoned); }
if(m_protected > 0) { desc += ", protected " + to_string(m_protected); }
// 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);
}
//---------------------- $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>(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);
}
//------------------------------------------------------------------------------
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(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(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 (left_status->m_hp > 0)
{
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 (right_status->m_hp > 0)
{
_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(revenge))
{
SkillSpec ss_heal{heal, fd->bg_effects.at(revenge), allfactions, 0, 0, no_skill, no_skill, true,};
SkillSpec ss_rally{rally, fd->bg_effects.at(revenge), allfactions, 0, 0, no_skill, no_skill, true,};
CardStatus * commander = &fd->players[status->m_player]->commander;
_DEBUG_MSG(2, "Revenge: Preparing skill %s and %s\n", skill_description(fd->cards, ss_heal).c_str(), skill_description(fd->cards, 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[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 (status->m_jammed)
{
_DEBUG_MSG(2, "%s failed to %s because it is Jammed.", status_description(status).c_str(), skill_description(fd->cards, ss).c_str());
continue;
}
signed evolved_offset = status->m_evolved_skill_offset[ss.id];
auto& evolved_s = status->m_evolved_skill_offset[ss.id] != 0 ? apply_evolve(ss, evolved_offset) : ss;
unsigned enhanced_value = status->enhanced(evolved_s.id);
auto& enhanced_s = enhanced_value > 0 ? apply_enhance(evolved_s, enhanced_value) : evolved_s;
auto& modified_s = enhanced_s;
skill_table[modified_s.id](fd, status, modified_s);
}
}
//------------------------------------------------------------------------------
inline bool has_attacked(CardStatus* c) { return(c->m_step == CardStep::attacked); }
inline bool can_act(CardStatus* c) { return(c->m_hp > 0 && !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(c->m_hp > 0 && c->m_hp < c->m_max_hp); }
//------------------------------------------------------------------------------
bool attack_phase(Field* fd);
template<Skill skill_id>
bool check_and_perform_skill(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s, bool is_evadable, bool & has_counted_quest);
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)
{
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(fd->cards, 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());
}
}
// Flurry
if (can_act(status) && fd->tip->commander.m_hp > 0 && status->has_skill(flurry) && status->m_skill_cd[flurry] == 0)
{
if (status->m_player == 0)
{
fd->inc_counter(QuestType::skill_use, flurry);
}
_DEBUG_MSG(1, "%s activates Flurry\n", status_description(status).c_str());
num_actions = 2;
for (const auto & ss : skills)
{
Skill evolved_skill_id = static_cast<Skill>(ss.id + status->m_evolved_skill_offset[ss.id]);
if (evolved_skill_id == 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>
bool op()
{
setStorage<type>();
placeCard<type>();
return(true);
}
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;
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);
}
_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>
inline bool skill_check(Field* fd, CardStatus* c, CardStatus* ref)
{ return(true); }
template<>
inline bool skill_check<evade>(Field* fd, CardStatus* c, CardStatus* ref)
{
return(c->m_player != ref->m_player);
}
template<>
inline bool skill_check<leech>(Field* fd, CardStatus* c, CardStatus* ref)
{
return(can_be_healed(c));
}
template<>
inline bool skill_check<legion>(Field* fd, CardStatus* c, CardStatus* ref)
{
return(is_active(c));
}
template<>
inline bool skill_check<payback>(Field* fd, CardStatus* c, CardStatus* ref)
{
return(ref->m_card->m_type == CardType::assault && ref->m_hp > 0);
}
template<>
inline bool skill_check<refresh>(Field* fd, CardStatus* c, CardStatus* ref)
{
return(can_be_healed(c));
}
void remove_hp(Field* fd, CardStatus* status, unsigned dmg)
{
assert(status->m_hp > 0);
_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)
{
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);
}
_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);
}
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(refresh);
if (refresh_value > 0 && skill_check<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)
{
if (status.m_player == 1)
{
fd->inc_counter(QuestType::skill_damage, poison, 0, poison_dmg);
}
_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_sundered = false;
status.m_weakened = 0;
status.m_inhibited = 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(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(wall) && c.m_hp > 0 && skill_check<wall>(fd, &c, nullptr))
{
return(&c);
}
}
return(nullptr);
}
inline bool alive_assault(Storage<CardStatus>& assaults, unsigned index)
{
return(assaults.size() > index && assaults[index].m_hp > 0);
}
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>();
if (att_status->m_hp > 0 && def_status->has_skill(counter) && skill_check<counter>(fd, def_status, att_status))
{
// perform_skill_counter
unsigned counter_dmg(counter_damage(fd, att_status, def_status));
if (def_status->m_player == 0)
{
fd->inc_counter(QuestType::skill_use, counter);
fd->inc_counter(QuestType::skill_damage, counter, 0, counter_dmg);
}
_DEBUG_MSG(1, "%s takes %u counter damage from %s\n", status_description(att_status).c_str(), counter_dmg, status_description(def_status).c_str());
remove_hp(fd, att_status, counter_dmg);
prepend_on_death(fd);
resolve_skill(fd);
if (def_cardtype == CardType::assault && def_status->m_hp > 0 && fd->bg_effects.count(counterflux))
{
unsigned flux_denominator = fd->bg_effects.at(counterflux) ? fd->bg_effects.at(counterflux) : 4;
unsigned flux_value = (def_status->skill(counter) - 1) / flux_denominator + 1;
_DEBUG_MSG(1, "Counterflux: %s heals itself and berserks for %u\n", status_description(def_status).c_str(), flux_value);
add_hp(fd, def_status, flux_value);
if (! def_status->m_sundered)
{ def_status->m_attack += flux_value; }
}
}
unsigned corrosive_value = def_status->skill(corrosive);
if (att_status->m_hp > 0 && corrosive_value > att_status->m_corroded_rate && skill_check<corrosive>(fd, def_status, att_status))
{
// perform_skill_corrosive
_DEBUG_MSG(1, "%s corrodes %s by %u\n", status_description(def_status).c_str(), status_description(att_status).c_str(), corrosive_value);
att_status->m_corroded_rate = corrosive_value;
}
unsigned berserk_value = att_status->skill(berserk);
if (att_status->m_hp > 0 && ! att_status->m_sundered && berserk_value > 0 && skill_check<berserk>(fd, att_status, nullptr))
{
// perform_skill_berserk
att_status->m_attack += berserk_value;
if (att_status->m_player == 0)
{
fd->inc_counter(QuestType::skill_use, berserk);
}
if (fd->bg_effects.count(enduringrage))
{
unsigned bge_denominator = fd->bg_effects.at(enduringrage) ? fd->bg_effects.at(enduringrage) : 2;
unsigned bge_value = (berserk_value - 1) / bge_denominator + 1;
_DEBUG_MSG(1, "EnduringRage: %s heals and protects itself for %u\n", status_description(att_status).c_str(), bge_value);
add_hp(fd, att_status, bge_value);
att_status->m_protected += bge_value;
}
}
do_leech<def_cardtype>();
unsigned valor_value = att_status->skill(valor);
if (valor_value > 0 && ! att_status->m_sundered && fd->bg_effects.count(heroism) && def_cardtype == CardType::assault && def_status->m_hp <= 0)
{
_DEBUG_MSG(1, "Heroism: %s gain %u attack\n", status_description(att_status).c_str(), valor_value);
att_status->m_attack += valor_value;
}
return att_dmg;
}
template<enum CardType::CardType>
void modify_attack_damage(unsigned pre_modifier_dmg)
{
assert(att_status->m_card->m_type == CardType::assault);
att_dmg = pre_modifier_dmg;
if (att_dmg == 0)
{ return; }
std::string desc;
unsigned legion_value = 0;
if (! att_status->m_sundered)
{
// enhance damage
unsigned legion_base = att_status->skill(legion);
if (legion_base > 0)
{
auto & assaults = fd->tap->assaults;
legion_value += att_status->m_index > 0 && assaults[att_status->m_index - 1].m_hp > 0 && assaults[att_status->m_index - 1].m_faction == att_status->m_faction;
legion_value += att_status->m_index + 1 < assaults.size() && assaults[att_status->m_index + 1].m_hp > 0 && assaults[att_status->m_index + 1].m_faction == att_status->m_faction;
if (legion_value > 0 && skill_check<legion>(fd, att_status, nullptr))
{
legion_value *= legion_base;
if (debug_print > 0) { desc += "+" + to_string(legion_value) + "(legion)"; }
att_dmg += legion_value;
}
}
unsigned rupture_value = att_status->skill(rupture);
if (rupture_value > 0)
{
if (debug_print > 0) { desc += "+" + to_string(rupture_value) + "(rupture)"; }
att_dmg += rupture_value;
}
unsigned venom_value = att_status->skill(venom);
if (venom_value > 0 && def_status->m_poisoned > 0)
{
if (debug_print > 0) { desc += "+" + to_string(venom_value) + "(venom)"; }
att_dmg += venom_value;
}
if (fd->bloodlust_value > 0)
{
if (debug_print > 0) { desc += "+" + to_string(fd->bloodlust_value) + "(bloodlust)"; }
att_dmg += fd->bloodlust_value;
}
if(def_status->m_enfeebled > 0)
{
if(debug_print > 0) { desc += "+" + to_string(def_status->m_enfeebled) + "(enfeebled)"; }
att_dmg += def_status->m_enfeebled;
}
}
// prevent damage
std::string reduced_desc;
unsigned reduced_dmg(0);
unsigned armor_value = def_status->skill(armor);
if (def_status->m_card->m_type == CardType::assault && fd->bg_effects.count(fortification))
{
for (auto && adj_status: fd->adjacent_assaults(def_status))
{
armor_value = std::max(armor_value, adj_status->skill(armor));
}
}
if(armor_value > 0)
{
if(debug_print > 0) { reduced_desc += to_string(armor_value) + "(armor)"; }
reduced_dmg += armor_value;
}
if(def_status->protected_value() > 0)
{
if(debug_print > 0) { reduced_desc += (reduced_desc.empty() ? "" : "+") + to_string(def_status->protected_value()) + "(protected)"; }
reduced_dmg += def_status->protected_value();
}
unsigned pierce_value = att_status->skill(pierce) + att_status->skill(rupture);
if (reduced_dmg > 0 && pierce_value > 0)
{
if (debug_print > 0) { reduced_desc += "-" + to_string(pierce_value) + "(pierce)"; }
reduced_dmg = safe_minus(reduced_dmg, pierce_value);
}
att_dmg = safe_minus(att_dmg, reduced_dmg);
if(debug_print > 0)
{
if(!reduced_desc.empty()) { desc += "-[" + reduced_desc + "]"; }
if(!desc.empty()) { desc += "=" + to_string(att_dmg); }
_DEBUG_MSG(1, "%s attacks %s for %u%s damage\n", status_description(att_status).c_str(), status_description(def_status).c_str(), pre_modifier_dmg, desc.c_str());
}
if (legion_value > 0 && can_be_healed(att_status) && fd->bg_effects.count(brigade))
{
_DEBUG_MSG(1, "Brigade: %s heals itself for %u\n", status_description(att_status).c_str(), legion_value);
add_hp(fd, att_status, legion_value);
}
}
template<enum CardType::CardType>
void attack_damage()
{
remove_hp(fd, def_status, att_dmg);
prepend_on_death(fd);
resolve_skill(fd);
}
template<enum CardType::CardType>
void damage_dependant_pre_oa() {}
template<enum CardType::CardType>
void do_leech() {}
};
template<>
void PerformAttack::attack_damage<CardType::commander>()
{
remove_commander_hp(fd, *def_status, att_dmg, true);
}
template<>