• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

APN-Pucky / tyrant_optimize / 41357367141

01 Nov 2026 10:29PM UTC coverage: 70.512% (+0.004%) from 70.508%
41357367141

push

github

APN-Pucky
fix missing grab of structures

1 of 1 new or added line in 1 file covered. (100.0%)

153 existing lines in 1 file now uncovered.

4804 of 6813 relevant lines covered (70.51%)

9635929.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.11
/sim.cpp
1
#include "sim.h"
2

3
#include <boost/range/adaptors.hpp>
4
#include <boost/range/join.hpp>
5
#include <iostream>
6
#include <random>
7
#include <string>
8
#include <sstream>
9
#include <vector>
10
#include <cmath>
11

12
#include "card.h"
13
#include "cards.h"
14
#include "deck.h"
15

16
template<enum CardType::CardType def_cardtype>
17
void perform_bge_devour(Field* fd, CardStatus* att_status, CardStatus* def_status, unsigned att_dmg);
18
template<enum CardType::CardType def_cardtype>
19
void perform_bge_heroism(Field* fd, CardStatus* att_status, CardStatus* def_status, unsigned att_dmg);
20
void perform_poison(Field* fd, CardStatus* att_status, CardStatus* def_status);
21
void perform_corrosive(Field* fd, CardStatus* att_status, CardStatus* def_status);
22
void perform_mark(Field* fd, CardStatus* att_status, CardStatus* def_status);
23
void perform_berserk(Field* fd, CardStatus* att_status, CardStatus* def_status);
24
template<enum CardType::CardType def_cardtype>
25
void perform_counter(Field* fd, CardStatus* att_status, CardStatus* def_status);
26
void perform_hunt(Field* fd, CardStatus* att_status, CardStatus* def_status);
27
void perform_subdue(Field* fd, CardStatus* att_status, CardStatus* def_status);
28
bool check_and_perform_valor(Field* fd, CardStatus* src);
29
bool check_and_perform_bravery(Field* fd, CardStatus* src);
30
bool check_and_perform_early_enhance(Field* fd, CardStatus* src);
31
bool check_and_perform_later_enhance(Field* fd, CardStatus* src);
32
CardStatus* check_and_perform_summon(Field* fd, CardStatus* src);
33
//------------------------------------------------------------------------------
34
inline unsigned remove_absorption(Field* fd, CardStatus* status, unsigned dmg);
35
inline unsigned remove_absorption(CardStatus* status, unsigned dmg);
36
inline unsigned remove_disease(CardStatus* status, unsigned heal);
37
//------------------------------------------------------------------------------
38
inline bool has_attacked(CardStatus* c) { return (c->m_step == CardStep::attacked); }
16,134,146 ✔
39
inline bool is_alive(CardStatus* c) { return (c->m_hp > 0); }
16,149,910 ✔
40
inline bool can_act(CardStatus* c) { return is_alive(c) && !c->m_jammed; }
429,364,295 ✔
41
inline bool is_active(CardStatus* c) { return can_act(c) && (c->m_delay == 0); }
357,498,306 ✔
42
inline bool is_active_next_turn(CardStatus* c) { return can_act(c) && (c->m_delay <= 1); }
49,461,204 ✔
43
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)));}
257,237 ✔
44
// Can be healed / repaired
45
inline bool can_be_healed(CardStatus* c) { return is_alive(c) && (c->m_hp < c->max_hp()); }
35,658 ✔
46
// Strange Transmission [Gilians] features
47
#ifdef TUO_GILIAN
48
inline bool is_gilian(CardStatus* c) { return(
49
        (c->m_card->m_id >= 25054 && c->m_card->m_id <= 25063) // Gilian Commander
50
        ||  (c->m_card->m_id >= 38348 && c->m_card->m_id <= 38388) // Gilian assaults plus the Gil's Shard
51
        ); }
52
inline bool is_alive_gilian(CardStatus* c) { return(is_alive(c) && is_gilian(c)); }
53
#else
54
# define is_gilian(c) (false)
55
# define is_alive_gilian(c) (false)
56
#endif
57

58
//------------------------------------------------------------------------------
59
inline std::string status_description(const CardStatus* status)
57,744,885 ✔
60
{
61
    return status->description();
21,986,079 ✔
62
}
63
//------------------------------------------------------------------------------
64
template <typename CardsIter, typename Functor>
65
inline unsigned Field::make_selection_array(CardsIter first, CardsIter last, Functor f)
124,050,748 ✔
66
{
67
    this->selection_array.clear();
124,050,748 ✔
68
    for(auto c = first; c != last; ++c)
505,313,498 ✔
69
    {
70
        if (f(*c))
764,214,361 ✔
71
        {
72
            this->selection_array.push_back(*c);
191,975,628 ✔
73
        }
74
    }
75
    return(this->selection_array.size());
124,050,748 ✔
76
}
77
inline CardStatus * Field::left_assault(const CardStatus * status)
1,026,540 ✔
78
{
79
    return left_assault(status, 1);
3,860,539 ✔
80
}
81
inline CardStatus * Field::left_assault(const CardStatus * status, const unsigned n)
1,026,540 ✔
82
{
83
    auto & assaults = this->players[status->m_player]->assaults;
1,026,540 ✔
84
    if (status->m_index >= n)
1,026,540 ✔
85
    {
86
        auto left_status = &assaults[status->m_index - n];
791,367 ✔
87
        if (is_alive(left_status))
791,367 ✔
88
        {
89
            return left_status;
90
        }
91
    }
92
    return nullptr;
93
}
94
inline CardStatus * Field::right_assault(const CardStatus * status)
1,026,540 ✔
95
{
96
    return right_assault(status, 1);
3,841,806 ✔
97
}
98
inline CardStatus * Field::right_assault(const CardStatus * status, const unsigned n)
1,026,540 ✔
99
{
100
    auto & assaults = this->players[status->m_player]->assaults;
1,026,540 ✔
101
    if ((status->m_index + n) < assaults.size())
1,026,540 ✔
102
    {
103
        auto right_status = &assaults[status->m_index + n];
783,745 ✔
104
        if (is_alive(right_status))
783,745 ✔
105
        {
106
            return right_status;
107
        }
108
    }
109
    return nullptr;
110
}
111
inline void Field::print_selection_array()
×
112
{
113
#ifndef NDEBUG
114
    for(auto c: this->selection_array)
×
115
    {
116
        _DEBUG_MSG(2, "+ %s\n", status_description(c).c_str());
×
117
    }
118
#endif
119
}
×
120

121
//------------------------------------------------------------------------------
122
inline void Field::prepare_action()
103,733,645 ✔
123
{
124
    damaged_units_to_times.clear();
207,467,290 ✔
125
}
126

127
//------------------------------------------------------------------------------
128
inline void Field::finalize_action()
102,388,712 ✔
129
{
130
    for (auto unit_it = damaged_units_to_times.begin(); unit_it != damaged_units_to_times.end(); ++ unit_it)
102,621,694 ✔
131
    {
132
        if (__builtin_expect(!unit_it->second, false))
232,982 ✔
133
        { continue; }
×
134
        CardStatus * dmg_status = unit_it->first;
232,982 ✔
135
        if (__builtin_expect(!is_alive(dmg_status), false))
232,982 ✔
136
        { continue; }
96,815 ✔
137
        unsigned barrier_base = dmg_status->skill(Skill::barrier);
136,167 ✔
138
        if (barrier_base)
136,167 ✔
139
        {
140
            unsigned protect_value = barrier_base * unit_it->second;
136,167 ✔
141
            _DEBUG_MSG(1, "%s protects itself for %u (barrier %u x %u damage taken)\n",
136,167 ✔
142
                    status_description(dmg_status).c_str(), protect_value, barrier_base, unit_it->second);
136,167 ✔
143
            dmg_status->m_protected += protect_value;
136,167 ✔
144
        }
145
    }
146
}
102,388,712 ✔
147

148
//------------------------------------------------------------------------------
149
inline unsigned CardStatus::skill_base_value(Skill::Skill skill_id) const
1,269,931,579 ✔
150
{
151
    return m_card->m_skill_value[skill_id + m_primary_skill_offset[skill_id]]
1,269,931,579 ✔
152
        + (skill_id == Skill::berserk ? m_enraged : 0)
×
153
        + (skill_id == Skill::counter ? m_entrapped : 0)
34,480,524 ✔
154
        ;
155
}
156
//------------------------------------------------------------------------------
157
inline unsigned CardStatus::skill(Skill::Skill skill_id) const
805,042,903 ✔
158
{
159
    return (is_activation_skill_with_x(skill_id)
805,042,903 ✔
160
            ? safe_minus(skill_base_value(skill_id), m_sabotaged)
161
            : skill_base_value(skill_id))
807,152,234 ✔
162
        + enhanced(skill_id);
807,152,234 ✔
163
}
164
//------------------------------------------------------------------------------
165
inline bool CardStatus::has_skill(Skill::Skill skill_id) const
462,993,699 ✔
166
{
167
    return skill_base_value(skill_id);
53,385 ✔
168
}
169
//------------------------------------------------------------------------------
170
inline unsigned CardStatus::enhanced(Skill::Skill skill_id) const
915,542,317 ✔
171
{
172
    return m_enhanced_value[skill_id + m_primary_skill_offset[skill_id]];
136,167 ✔
173
}
174
//------------------------------------------------------------------------------
175
inline unsigned CardStatus::protected_value() const
53,901,802 ✔
176
{
177
    return m_protected + m_protected_stasis;
53,901,802 ✔
178
}
179
//------------------------------------------------------------------------------
180
/**
181
 * @brief Maximum health.
182
 * This takes subduing into account by reducing the permanent health buffs by subdue.
183
 * 
184
 * @return unsigned maximum health.
185
 */
186
inline unsigned CardStatus::max_hp() const
102,552,049 ✔
187
{
188
    return (m_card->m_health + safe_minus(m_perm_health_buff, m_subdued));
98,916,489 ✔
189
}
190
/** @brief Increase of current health.
191
 *  This takes disease into account and removes as needed.
192
 *
193
 *  @param [in] value increase of health.
194
 *  @return applied increase of health.
195
 */
196
inline unsigned CardStatus::add_hp(unsigned value)
22,329,755 ✔
197
{
198
    value = remove_disease(this,value);
22,329,755 ✔
199
    return (m_hp = std::min(m_hp + value, max_hp()));
39,802,125 ✔
200
}
201
/** @brief Permanent increase of maximum health.
202
 *  This takes disease into account and removes as needed.
203
 *  The increase of the maximum health entails an increase of current health by the same amount.
204
 *
205
 *  @param [in] value increase of maximum health.
206
 *  @return applied increase of maximum health.
207
 */ 
208
inline unsigned CardStatus::ext_hp(unsigned value)
13,628,552 ✔
209
{
210
    value = remove_disease(this,value);
13,628,552 ✔
211
    m_perm_health_buff += value;
13,628,552 ✔
212
    // we can safely call add_hp without worring about the second call to remove_disease because
213
    // the first call will have already removed the disease or the value will be 0
214
    return add_hp(value);
13,628,552 ✔
215
}
216
//------------------------------------------------------------------------------
217
inline void CardStatus::set(const Card* card)
25,090,650 ✔
218
{
219
    this->set(*card);
25,090,650 ✔
220
}
221
//------------------------------------------------------------------------------
222
inline void CardStatus::set(const Card& card)
25,090,650 ✔
223
{
224
    m_card = &card;
25,090,650 ✔
225
    m_index = 0;
25,090,650 ✔
226
    m_action_index=0;
25,090,650 ✔
227
    m_player = 0;
25,090,650 ✔
228
    m_delay = card.m_delay;
25,090,650 ✔
229
    m_hp = card.m_health;
25,090,650 ✔
230
    m_absorption = 0;
25,090,650 ✔
231
    m_step = CardStep::none;
25,090,650 ✔
232
    m_perm_health_buff = 0;
25,090,650 ✔
233
    m_perm_attack_buff = 0;
25,090,650 ✔
234
    m_temp_attack_buff = 0;
25,090,650 ✔
235
    m_corroded_rate = 0;
25,090,650 ✔
236
    m_corroded_weakened = 0;
25,090,650 ✔
237
    m_subdued = 0;
25,090,650 ✔
238
    m_enfeebled = 0;
25,090,650 ✔
239
    m_evaded = 0;
25,090,650 ✔
240
    m_inhibited = 0;
25,090,650 ✔
241
    m_sabotaged = 0;
25,090,650 ✔
242
    m_jammed = false;
25,090,650 ✔
243
    m_overloaded = false;
25,090,650 ✔
244
    m_paybacked = 0;
25,090,650 ✔
245
    m_tributed = 0;
25,090,650 ✔
246
    m_poisoned = 0;
25,090,650 ✔
247
    m_protected = 0;
25,090,650 ✔
248
    m_protected_stasis = 0;
25,090,650 ✔
249
    m_enraged = 0;
25,090,650 ✔
250
    m_entrapped = 0;
25,090,650 ✔
251
    m_marked = 0;
25,090,650 ✔
252
    m_diseased = 0;
25,090,650 ✔
253
    m_rush_attempted = false;
25,090,650 ✔
254
    m_sundered = false;
25,090,650 ✔
255
    //APN
256
    m_summoned = false;
25,090,650 ✔
257
    m_skills_modified = false;
25,090,650 ✔
258

259
    // the four skill arrays are adjacent members: one memset is much cheaper than four
260
    static_assert(offsetof(CardStatus, m_skill_cd) + sizeof m_skill_cd - offsetof(CardStatus, m_primary_skill_offset)
25,090,650 ✔
261
            == sizeof m_primary_skill_offset + sizeof m_evolved_skill_offset + sizeof m_enhanced_value + sizeof m_skill_cd,
262
            "CardStatus skill arrays must be adjacent");
263
    std::memset(m_primary_skill_offset, 0, offsetof(CardStatus, m_skill_cd) + sizeof m_skill_cd - offsetof(CardStatus, m_primary_skill_offset));
25,090,650 ✔
264
}
25,090,650 ✔
265
//------------------------------------------------------------------------------
266
/**
267
 * @brief Calculate the attack power of the CardStatus.
268
 * 
269
 * @return unsigned 
270
 */
271
inline unsigned CardStatus::attack_power() const
192,461,175 ✔
272
{
273
    signed attack = calc_attack_power();
192,461,175 ✔
274
    if(__builtin_expect(attack <0,false))
192,461,175 ✔
275
    {
276
        std::cout << m_card->m_name << " " << m_card->m_attack  << " " << attack << " " << m_temp_attack_buff << " " << m_corroded_weakened << std::endl;
×
277
    }
278
    _DEBUG_ASSERT(attack >= 0);
192,461,175 ✔
279
    return (unsigned)attack;
192,461,175 ✔
280
}
281

282
/**
283
 * @brief Calculate the attack power of the CardStatus.
284
 * 
285
 * The attack power is the base attack plus. 
286
 * The subdued value gets subtracted from the permanent attack buff, if this is above zero it is added to the attack power.
287
 * The corroded value gets subtracted from the attack power, but not below zero.
288
 * Finally the temporary attack buff is added.
289
 * 
290
 * @return signed attack power.
291
 */
292
inline signed CardStatus::calc_attack_power() const
193,288,247 ✔
293
{
294
    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."
193,288,247 ✔
295
    {
296
        return
193,288,247 ✔
297
        (signed)safe_minus(
386,576,494 ✔
298
                m_card->m_attack + safe_minus(m_perm_attack_buff, m_subdued)+ m_temp_attack_buff,
193,288,247 ✔
299
                m_corroded_weakened
193,288,247 ✔
300
                );
193,288,247 ✔
301
    }
302
    else{
303
        return
×
304
        (signed)safe_minus(
×
305
                m_card->m_attack + safe_minus(m_perm_attack_buff, m_subdued),
×
306
                m_corroded_weakened
×
307
                )
308
        + m_temp_attack_buff;
×
309
    }
310

311
}
312
//------------------------------------------------------------------------------
313
const Card* card_by_id_safe(const Cards& cards, const unsigned card_id)
×
314
{
315
    const auto cardIter = cards.cards_by_id.find(card_id);
×
316
    if (cardIter == cards.cards_by_id.end()) assert(false);//"UnknownCard.id[" + to_string(card_id) + "]"); }
×
317
    return cardIter->second;
×
318
}
319
std::string card_name_by_id_safe(const Cards& cards, const unsigned card_id)
109,297 ✔
320
{
321
    const auto cardIter = cards.cards_by_id.find(card_id);
109,297 ✔
322
    if (cardIter == cards.cards_by_id.end()) { return "UnknownCard.id[" + tuo::to_string(card_id) + "]"; }
109,297 ✔
323
    return cardIter->second->m_name;
218,594 ✔
324
}
325
//------------------------------------------------------------------------------
326
std::string card_description(const Cards& cards, const Card* c)
2,516,340 ✔
327
{
328
    std::string desc;
2,516,340 ✔
329
    desc = c->m_name;
2,516,340 ✔
330
    switch(c->m_type)
2,516,340 ✔
331
    {
332
        case CardType::assault:
2,078,267 ✔
333
            desc += ": " + tuo::to_string(c->m_attack) + "/" + tuo::to_string(c->m_health) + "/" + tuo::to_string(c->m_delay);
10,391,335 ✔
334
            break;
2,078,267 ✔
335
        case CardType::structure:
437,505 ✔
336
            desc += ": " + tuo::to_string(c->m_health) + "/" + tuo::to_string(c->m_delay);
1,750,020 ✔
337
            break;
437,505 ✔
338
        case CardType::commander:
568 ✔
339
            desc += ": hp:" + tuo::to_string(c->m_health);
1,704 ✔
340
            break;
568 ✔
341
        case CardType::num_cardtypes:
×
342
            assert(false);
×
343
            break;
344
    }
345
    if (c->m_rarity >= 4) { desc += " " + rarity_names[c->m_rarity]; }
4,223,259 ✔
346
    if (c->m_faction != allfactions) { desc += " " + faction_names[c->m_faction]; }
5,032,680 ✔
347
    for (auto& skill: c->m_skills_on_play) { desc += ", " + skill_description(cards, skill, Skill::Trigger::play); }
2,649,262 ✔
348
    for (auto& skill: c->m_skills) { desc += ", " + skill_description(cards, skill, Skill::Trigger::activate); }
16,643,578 ✔
349
    //APN
350
    for (auto& skill: c->m_skills_on_attacked) { desc += ", " + skill_description(cards, skill, Skill::Trigger::attacked); }
3,081,686 ✔
351
    for (auto& skill: c->m_skills_on_death) { desc += ", " + skill_description(cards, skill, Skill::Trigger::death); }
2,691,518 ✔
352
    return desc;
2,516,340 ✔
353
}
×
354
//------------------------------------------------------------------------------
355
std::string CardStatus::description() const
57,744,885 ✔
356
{
357
    std::string desc = "P" + tuo::to_string(m_player) + " ";
173,234,655 ✔
358
    switch(m_card->m_type)
57,744,885 ✔
359
    {
360
        case CardType::commander: desc += "Commander "; break;
17,036,895 ✔
361
        case CardType::assault: desc += "Assault " + tuo::to_string(m_index) + " "; break;
135,512,376 ✔
362
        case CardType::structure: desc += "Structure " + tuo::to_string(m_index) + " "; break;
27,319,584 ✔
363
        case CardType::num_cardtypes: assert(false); break;
×
364
    }
365
    desc += "[" + m_card->m_name;
115,489,770 ✔
366
    switch (m_card->m_type)
57,744,885 ✔
367
    {
368
        case CardType::assault:
33,878,094 ✔
369
            desc += " att:[[" + tuo::to_string(m_card->m_attack) + "(base)";
135,512,376 ✔
370
            if (m_perm_attack_buff)
33,878,094 ✔
371
            {
372
                desc += "+[" + tuo::to_string(m_perm_attack_buff) + "(perm)";
28,839,940 ✔
373
                if (m_subdued) { desc += "-" + tuo::to_string(m_subdued) + "(subd)"; }
8,466,100 ✔
374
                desc += "]";
7,209,985 ✔
375
            }
376
            if (m_corroded_weakened) { desc += "-" + tuo::to_string(m_corroded_weakened) + "(corr)"; }
35,301,489 ✔
377
            desc += "]";
33,878,094 ✔
378
            if (m_temp_attack_buff) { desc += (m_temp_attack_buff > 0 ? "+" : "") + tuo::to_string(m_temp_attack_buff) + "(temp)"; }
50,986,077 ✔
379
            desc += "]=" + tuo::to_string(attack_power());
101,634,282 ✔
380
        case CardType::structure:
57,744,885 ✔
381
        case CardType::commander:
57,744,885 ✔
382
            desc += " hp:" + tuo::to_string(m_hp);
173,234,655 ✔
383
            if(m_absorption)desc += " absorb:" + tuo::to_string(m_absorption);
63,451,963 ✔
384
            break;
385
        case CardType::num_cardtypes:
×
386
            assert(false);
×
387
            break;
388
    }
389
    if (m_delay) { desc += " cd:" + tuo::to_string(m_delay); }
86,421,233 ✔
390
    // Status w/o value
391
    if (m_jammed) { desc += ", jammed"; }
57,744,885 ✔
392
    if (m_overloaded) { desc += ", overloaded"; }
57,744,885 ✔
393
    if (m_sundered) { desc += ", sundered"; }
57,744,885 ✔
394
    if (m_summoned) { desc+= ", summoned"; }
57,744,885 ✔
395
    // Status w/ value
396
    if (m_corroded_weakened || m_corroded_rate) { desc += ", corroded " + tuo::to_string(m_corroded_weakened) + " (rate: " + tuo::to_string(m_corroded_rate) + ")"; }
59,777,545 ✔
397
    if (m_subdued) { desc += ", subdued " + tuo::to_string(m_subdued); }
59,860,769 ✔
398
    if (m_enfeebled) { desc += ", enfeebled " + tuo::to_string(m_enfeebled); }
63,008,037 ✔
399
    if (m_inhibited) { desc += ", inhibited " + tuo::to_string(m_inhibited); }
60,751,889 ✔
400
    if (m_sabotaged) { desc += ", sabotaged " + tuo::to_string(m_sabotaged); }
60,924,171 ✔
401
    if (m_poisoned) { desc += ", poisoned " + tuo::to_string(m_poisoned); }
59,534,151 ✔
402
    if (m_protected) { desc += ", protected " + tuo::to_string(m_protected); }
78,856,797 ✔
403
    if (m_protected_stasis) { desc += ", stasis " + tuo::to_string(m_protected_stasis); }
62,549,587 ✔
404
    if (m_enraged) { desc += ", enraged " + tuo::to_string(m_enraged); }
63,937,177 ✔
405
    if (m_entrapped) { desc += ", entrapped " + tuo::to_string(m_entrapped); }
77,379,967 ✔
406
    if (m_marked) { desc += ", marked " + tuo::to_string(m_marked); }
58,047,477 ✔
407
    if (m_diseased) { desc += ", diseased " + tuo::to_string(m_diseased); }
58,835,987 ✔
408
    //    if(m_step != CardStep::none) { desc += ", Step " + to_string(static_cast<int>(m_step)); }
409
    //APN
410
    const Skill::Trigger s_triggers[] = { Skill::Trigger::play, Skill::Trigger::activate, Skill::Trigger::death , Skill::Trigger::attacked};
57,744,885 ✔
411
    for (const Skill::Trigger& trig: s_triggers)
288,724,425 ✔
412
    {
413
        std::vector<SkillSpec> card_skills(
230,979,540 ✔
414
                (trig == Skill::Trigger::play) ? m_card->m_skills_on_play :
230,979,540 ✔
415
                (trig == Skill::Trigger::activate) ? m_card->m_skills :
173,234,655 ✔
416
                //APN
417
                (trig == Skill::Trigger::attacked) ? m_card->m_skills_on_attacked :
115,489,770 ✔
418
                (trig == Skill::Trigger::death) ? m_card->m_skills_on_death :
57,744,885 ✔
419
                std::vector<SkillSpec>());
230,979,540 ✔
420

421
        // emulate Berserk/Counter by status Enraged/Entrapped unless such skills exist (only for normal skill triggering)
422
        if (trig == Skill::Trigger::activate)
230,979,540 ✔
423
        {
424
            if (m_enraged && !std::count_if(card_skills.begin(), card_skills.end(), [](const SkillSpec ss) { return (ss.id == Skill::berserk); }))
60,841,031 ✔
425
            {
426
                SkillSpec ss{Skill::berserk, m_enraged, allfactions, 0, 0, Skill::no_skill, Skill::no_skill, false, 0,};
3,055,563 ✔
427
                card_skills.emplace_back(ss);
3,055,563 ✔
428
            }
429
            if (m_entrapped && !std::count_if(card_skills.begin(), card_skills.end(), [](const SkillSpec ss) { return (ss.id == Skill::counter); }))
67,562,426 ✔
430
            {
431
                SkillSpec ss{Skill::counter, m_entrapped, allfactions, 0, 0, Skill::no_skill, Skill::no_skill, false, 0,};
9,609,698 ✔
432
                card_skills.emplace_back(ss);
9,609,698 ✔
433
            }
434
        }
435
        for (const auto& ss : card_skills)
416,096,123 ✔
436
        {
437
            std::string skill_desc;
185,116,583 ✔
438
            if (m_evolved_skill_offset[ss.id]) { skill_desc += "->" + skill_names[ss.id + m_evolved_skill_offset[ss.id]]; }
185,510,566 ✔
439
            if (m_enhanced_value[ss.id]) { skill_desc += " +" + tuo::to_string(m_enhanced_value[ss.id]); }
187,989,673 ✔
440
            if (!skill_desc.empty())
185,116,583 ✔
441
            {
442
                desc += ", " + (
3,661,056 ✔
443
                        (trig == Skill::Trigger::play) ? "(On Play)" :
3,661,056 ✔
444
                        (trig == Skill::Trigger::attacked) ? "(On Attacked)" :
1,830,528 ✔
445
                        (trig == Skill::Trigger::death) ? "(On Death)" :
1,830,528 ✔
446
                        std::string("")) + skill_names[ss.id] + skill_desc;
9,152,640 ✔
447
            }
448
        }
185,116,583 ✔
449
    }
230,979,540 ✔
450
    return desc + "]";
57,744,885 ✔
451
}
57,744,885 ✔
452
//------------------------------------------------------------------------------
453
void Hand::reset(std::mt19937& re)
2,731,732 ✔
454
{
455
    assaults.reset();
2,731,732 ✔
456
    structures.reset();
2,731,732 ✔
457
    deck->shuffle(re);
2,731,732 ✔
458
    commander.set(deck->shuffled_commander);
2,731,732 ✔
459
    total_cards_destroyed = 0;
2,731,732 ✔
460
    total_nonsummon_cards_destroyed = 0;
2,731,732 ✔
461
    if (commander.skill(Skill::stasis))
2,731,732 ✔
462
    {
463
        stasis_faction_bitmap |= (1u << commander.m_card->m_faction);
×
464
    }
465
}
2,731,732 ✔
466

467
//---------------------- $40 Game rules implementation -------------------------
468
// Everything about how a battle plays out, except the following:
469
// the implementation of the attack by an assault card is in the next section;
470
// the implementation of the active skills is in the section after that.
471
unsigned turn_limit{50};
472

473
//------------------------------------------------------------------------------
474
inline unsigned opponent(unsigned player)
51,932,243 ✔
475
{
476
    return((player + 1) % 2);
51,932,243 ✔
477
}
478

479
//------------------------------------------------------------------------------
480
SkillSpec apply_evolve(const SkillSpec& s, signed offset)
263,255 ✔
481
{
482
    SkillSpec evolved_s = s;
263,255 ✔
483
    evolved_s.id = static_cast<Skill::Skill>(evolved_s.id + offset);
263,255 ✔
484
    return(evolved_s);
263,255 ✔
485
}
486

487
//------------------------------------------------------------------------------
488
SkillSpec apply_enhance(const SkillSpec& s, unsigned enhanced_value)
×
489
{
490
    SkillSpec enahnced_s = s;
×
491
    enahnced_s.x += enhanced_value;
×
492
    return(enahnced_s);
×
493
}
494

495
//------------------------------------------------------------------------------
496
SkillSpec apply_sabotage(const SkillSpec& s, unsigned sabotaged_value)
141,417 ✔
497
{
498
    SkillSpec sabotaged_s = s;
141,417 ✔
499
    sabotaged_s.x -= std::min(sabotaged_s.x, sabotaged_value);
×
500
    return(sabotaged_s);
141,417 ✔
501
}
502

503
//------------------------------------------------------------------------------
504
inline void resolve_scavenge(Storage<CardStatus>& store)
39,260,168 ✔
505
{
506
    for(auto status : store.m_indirect)
150,320,459 ✔
507
    {
508
        if(!is_alive(status))continue;
111,060,291 ✔
509
        unsigned scavenge_value = status->skill(Skill::scavenge);
97,145,241 ✔
510
        if(!scavenge_value)continue;
97,145,241 ✔
511

512
        _DEBUG_MSG(1, "%s activates Scavenge %u\n",
6,745,308 ✔
513
                status_description(status).c_str(), scavenge_value);
6,745,308 ✔
514
        status->ext_hp(scavenge_value);
6,745,308 ✔
515
    }
516
}
39,260,168 ✔
517
//------------------------------------------------------------------------------
518
/**
519
 * @brief Handle death of a card
520
 * 
521
 * @param fd Field pointer 
522
 * @param paybacked Is the death caused by payback?
523
 */
524
void prepend_on_death(Field* fd, bool paybacked=false)
124,430,982 ✔
525
{
526
    if (fd->killed_units.empty())
124,430,982 ✔
527
        return;
528
    bool skip_all_except_summon = fd->fixes[Fix::death_from_bge] && (fd->current_phase == Field::bge_phase);
9,687,501 ✔
529
    if (skip_all_except_summon)
×
530
    {
531
        _DEBUG_MSG(2, "Death from BGE Fix (skip all death depended triggers except summon)\n");
×
532
    }
533
    auto& assaults = fd->players[fd->killed_units[0]->m_player]->assaults;
9,687,501 ✔
534
    unsigned stacked_poison_value = 0;
9,687,501 ✔
535
    unsigned last_index = 99999;
9,687,501 ✔
536
    CardStatus* left_virulence_victim = nullptr;
9,687,501 ✔
537
    for (auto status: fd->killed_units)
19,502,543 ✔
538
    {
539
        // Skill: Scavenge
540
        // Any unit dies => perm-hp-buff
541
        if (__builtin_expect(!skip_all_except_summon, true))
9,815,042 ✔
542
        {
543
            resolve_scavenge(fd->players[0]->assaults);
9,815,042 ✔
544
            resolve_scavenge(fd->players[1]->assaults);
9,815,042 ✔
545
            resolve_scavenge(fd->players[0]->structures);
9,815,042 ✔
546
            resolve_scavenge(fd->players[1]->structures);
9,815,042 ✔
547
        }
548

549
        if ((status->m_card->m_type == CardType::assault) && (!skip_all_except_summon))
9,815,042 ✔
550
        {
551
            // Skill: Avenge
552
            const unsigned host_idx = status->m_index;
8,833,504 ✔
553
            unsigned from_idx, till_idx;
8,833,504 ✔
554
            //scan all assaults for Avenge
555
            from_idx = 0;
8,833,504 ✔
556
            till_idx = assaults.size() - 1;
8,833,504 ✔
557
            for (; from_idx <= till_idx; ++ from_idx)
34,680,211 ✔
558
            {
559
                if (from_idx == host_idx) { continue; }
25,846,707 ✔
560
                CardStatus* adj_status = &assaults[from_idx];
17,013,203 ✔
561
                if (!is_alive(adj_status)) { continue; }
17,013,203 ✔
562
                unsigned avenge_value = adj_status->skill(Skill::avenge);
14,825,908 ✔
563
                if (!avenge_value) { continue; }
14,825,908 ✔
564

565
                // Use half value rounded up
566
                // (for distance > 1, i. e. non-standard Avenge triggering)
567
                if (std::abs((signed)from_idx - (signed)host_idx) > 1)
3,396,317 ✔
568
                {
569
                    avenge_value = (avenge_value + 1) / 2;
1,860,915 ✔
570
                }
571
                _DEBUG_MSG(1, "%s activates %sAvenge %u\n",
3,552,734 ✔
572
                        status_description(adj_status).c_str(),
573
                        (std::abs((signed)from_idx - (signed)host_idx) > 1 ? "Half-" : ""),
574
                         avenge_value);
3,396,317 ✔
575
                if (!adj_status->m_sundered)
3,396,317 ✔
576
                { adj_status->m_perm_attack_buff += avenge_value; }
3,161,270 ✔
577
                adj_status->ext_hp(avenge_value);
3,396,317 ✔
578
            }
579

580
            // Passive BGE: Virulence
581
            if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::virulence], false))
8,833,504 ✔
582
            {
583
                if (status->m_index != last_index + 1)
34,415 ✔
584
                {
585
                    stacked_poison_value = 0;
34,028 ✔
586
                    left_virulence_victim = nullptr;
34,028 ✔
587
                    if (status->m_index > 0)
34,028 ✔
588
                    {
589
                        auto left_status = &assaults[status->m_index - 1];
7,137 ✔
590
                        if (is_alive(left_status))
7,137 ✔
591
                        {
592
                            left_virulence_victim = left_status;
34,415 ✔
593
                        }
594
                    }
595
                }
596
                if (status->m_poisoned > 0)
34,415 ✔
597
                {
598
                    if (left_virulence_victim != nullptr)
1,623 ✔
599
                    {
600
                        _DEBUG_MSG(1, "Virulence: %s spreads left poison +%u to %s\n",
164 ✔
601
                                status_description(status).c_str(), status->m_poisoned,
602
                                status_description(left_virulence_victim).c_str());
164 ✔
603
                        left_virulence_victim->m_poisoned += status->m_poisoned;
164 ✔
604
                    }
605
                    stacked_poison_value += status->m_poisoned;
1,623 ✔
606
                    _DEBUG_MSG(1, "Virulence: %s spreads right poison +%u = %u\n",
1,623 ✔
607
                            status_description(status).c_str(), status->m_poisoned, stacked_poison_value);
608
                }
609
                if (status->m_index + 1 < assaults.size())
34,415 ✔
610
                {
611
                    auto right_status = &assaults[status->m_index + 1];
10,958 ✔
612
                    if (is_alive(right_status))
10,958 ✔
613
                    {
614
                        _DEBUG_MSG(1, "Virulence: spreads stacked poison +%u to %s\n",
10,134 ✔
615
                                stacked_poison_value, status_description(right_status).c_str());
10,134 ✔
616
                        right_status->m_poisoned += stacked_poison_value;
10,134 ✔
617
                    }
618
                }
619
                last_index = status->m_index;
34,415 ✔
620
            }
621
        }
622

623
        // Passive BGE: Revenge
624
        // Fix::death_from_bge: should not affect passive BGE, keep it as was before
625
        if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::revenge], false))
9,815,042 ✔
626
        {
627
            if (fd->bg_effects[fd->tapi][PassiveBGE::revenge] < 0)
73,298 ✔
628
                throw std::runtime_error("BGE Revenge: value must be defined & positive");
×
629
            SkillSpec ss_heal{Skill::heal, (unsigned)fd->bg_effects[fd->tapi][PassiveBGE::revenge], allfactions, 0, 0, Skill::no_skill, Skill::no_skill, true, 0,};
73,298 ✔
630
            SkillSpec ss_rally{Skill::rally, (unsigned)fd->bg_effects[fd->tapi][PassiveBGE::revenge], allfactions, 0, 0, Skill::no_skill, Skill::no_skill, true, 0,};
73,298 ✔
631
            CardStatus* commander = &fd->players[status->m_player]->commander;
73,298 ✔
632
            _DEBUG_MSG(2, "Revenge: Preparing (head) skills  %s and %s\n",
73,298 ✔
633
                    skill_description(fd->cards, ss_heal).c_str(),
634
                    skill_description(fd->cards, ss_rally).c_str());
73,298 ✔
635
            fd->skill_queue.emplace(fd->skill_queue.begin()+0, commander, ss_heal);
73,298 ✔
636
            fd->skill_queue.emplace(fd->skill_queue.begin()+1, commander, ss_rally); // +1: keep ss_heal at first place
73,298 ✔
637
        }
638

639
        // resolve On-Death skills
640
        for (auto& ss: status->m_card->m_skills_on_death)
10,215,077 ✔
641
        {
642
            if (__builtin_expect(skip_all_except_summon && (ss.id != Skill::summon), false))
400,035 ✔
643
            { continue; }
×
644
                SkillSpec tss = ss;
400,035 ✔
645
            _DEBUG_MSG(2, "On Death %s: Preparing (tail) skill %s\n",
400,035 ✔
646
                    status_description(status).c_str(), skill_description(fd->cards, ss).c_str());
400,035 ✔
647
            if(fd->fixes[Fix::revenge_on_death] && is_activation_harmful_skill(ss.id) && paybacked)
400,035 ✔
648
            {
649
                    _DEBUG_MSG(2, "On Death Revenge Fix\n");
309 ✔
650
                    tss.s2 = Skill::revenge;
309 ✔
651
            }
652
            fd->skill_queue.emplace_back(status, tss);
400,035 ✔
653
        }
654
    }
655
    fd->killed_units.clear();
134,118,483 ✔
656
}
657

658
//------------------------------------------------------------------------------
659
void(*skill_table[Skill::num_skills])(Field*, CardStatus* src, const SkillSpec&);
660
void resolve_skill(Field* fd)
183,087,475 ✔
661
{
662
    while (!fd->skill_queue.empty())
296,434,839 ✔
663
    {
664
        auto skill_instance(fd->skill_queue.front());
113,347,364 ✔
665
        auto& status(std::get<0>(skill_instance));
113,347,364 ✔
666
        const auto& ss(std::get<1>(skill_instance));
113,347,364 ✔
667
        fd->skill_queue.pop_front();
113,347,364 ✔
668
        if (__builtin_expect(status->m_card->m_skill_trigger[ss.id] == Skill::Trigger::activate, true))
113,347,364 ✔
669
        {
670
            if (!is_alive(status))
108,973,315 ✔
671
            {
672
                _DEBUG_MSG(2, "%s failed to %s because it is dead.\n",
3,875 ✔
673
                        status_description(status).c_str(), skill_description(fd->cards, ss).c_str());
3,875 ✔
674
                continue;
3,175,270 ✔
675
            }
3,875 ✔
676
            if (status->m_jammed)
108,969,440 ✔
677
            {
678
                _DEBUG_MSG(2, "%s failed to %s because it is Jammed.\n",
1,334 ✔
679
                        status_description(status).c_str(), skill_description(fd->cards, ss).c_str());
1,334 ✔
680
                continue;
1,334 ✔
681
            }
1,334 ✔
682
        }
683

684
        // is summon? (non-activation skill)
685
        if (ss.id == Skill::summon)
113,342,155 ✔
686
        {
687
            check_and_perform_summon(fd, status);
3,098,675 ✔
688
            continue;
3,098,675 ✔
689
        }
690
        _DEBUG_ASSERT(is_activation_skill(ss.id) || ss.id == Skill::enhance); // enhance is no trigger, but  queues the skill
110,335,837 ✔
691

692
        SkillSpec modified_s = ss;
110,243,480 ✔
693

694
        // apply evolve
695
        signed evolved_offset = status->m_evolved_skill_offset[modified_s.id];
110,243,480 ✔
696
        if (evolved_offset != 0)
110,243,480 ✔
697
        { modified_s = apply_evolve(modified_s, evolved_offset); }
263,255 ✔
698

699
        // apply sabotage (only for X-based activation skills)
700
        unsigned sabotaged_value = status->m_sabotaged;
110,243,480 ✔
701
        if ((sabotaged_value > 0) && is_activation_skill_with_x(modified_s.id))
110,243,480 ✔
702
        { modified_s = apply_sabotage(modified_s, sabotaged_value); }
211,448 ✔
703

704
        // apply enhance
705
        unsigned enhanced_value = status->enhanced(modified_s.id);
110,243,480 ✔
706
        if (enhanced_value > 0)
110,243,480 ✔
707
        { modified_s = apply_enhance(modified_s, enhanced_value); }
×
708

709
        // perform skill (if it is still applicable)
710
        if (is_activation_skill_with_x(modified_s.id) && !modified_s.x)
110,243,480 ✔
711
        {
712
            _DEBUG_MSG(2, "%s failed to %s because its X value is zeroed (sabotaged).\n",
71,386 ✔
713
                    status_description(status).c_str(), skill_description(fd->cards, ss).c_str());
71,386 ✔
714
            continue;
71,386 ✔
715
        }
71,386 ✔
716
        else { skill_table[modified_s.id](fd, status, modified_s); }
110,172,094 ✔
717
    }
718
}
183,087,475 ✔
719

720
void apply_corrosion(CardStatus * status)
21,293,535 ✔
721
{
722
      if (status->m_corroded_rate)
21,293,535 ✔
723
      {
724
        unsigned v = std::min(status->m_corroded_rate, status->attack_power());
94,274 ✔
725
        unsigned corrosion = std::min(v, status->m_card->m_attack
188,548 ✔
726
                + status->m_perm_attack_buff - status->m_corroded_weakened);
94,274 ✔
727
        _DEBUG_MSG(1, "%s loses Attack by %u (+corrosion %u).\n", status_description(status).c_str(), v, corrosion);
94,274 ✔
728
        status->m_corroded_weakened += corrosion;
94,274 ✔
729
      }
730
}
21,293,535 ✔
731
void remove_corrosion(CardStatus * status)
2,823,515 ✔
732
{
733
       if (status->m_corroded_rate)
2,823,515 ✔
734
       {
735
           _DEBUG_MSG(1, "%s loses Status corroded.\n", status_description(status).c_str());
4,259 ✔
736
           status->m_corroded_rate = 0;
4,259 ✔
737
           status->m_corroded_weakened = 0;
4,259 ✔
738
       }
739
}
2,823,515 ✔
740
//------------------------------------------------------------------------------
741
bool attack_phase(Field* fd);
742

743
template<Skill::Skill skill_id>
744
inline bool check_and_perform_skill(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s, bool is_evadable
745
#ifndef NQUEST
746
        , bool & has_counted_quest
747
#endif
748
        );
749

750
    template <enum CardType::CardType type>
751
void evaluate_skills(Field* fd, CardStatus* status, const std::vector<SkillSpec>& skills, bool* attacked=nullptr)
61,264,628 ✔
752
{
753
    _DEBUG_ASSERT(status);
61,264,628 ✔
754
    unsigned num_actions(1);
755
    for (unsigned action_index(0); action_index < num_actions; ++ action_index)
126,888,713 ✔
756
    {
757
        status->m_action_index = action_index;
66,969,018 ✔
758
        fd->prepare_action();
66,969,018 ✔
759
        _DEBUG_ASSERT(fd->skill_queue.size() == 0);
66,969,018 ✔
760
        for (auto & ss: skills)
258,393,497 ✔
761
        {
762
            if (!is_activation_skill(ss.id)) { continue; }
273,257,622 ✔
763
            if (status->m_skill_cd[ss.id] > 0) { continue; }
109,591,336 ✔
764
            _DEBUG_MSG(2, "Evaluating %s skill %s\n",
108,734,362 ✔
765
                    status_description(status).c_str(), skill_description(fd->cards, ss).c_str());
766
            fd->skill_queue.emplace_back(status, ss);
108,734,362 ✔
767
            resolve_skill(fd);
108,734,362 ✔
768
        }
769
        if (type == CardType::assault)
770
        {
771
            // Attack
772
            if (can_act(status))
25,461,983 ✔
773
            {
774
                if (attack_phase(fd))
25,292,218 ✔
775
                {
776
                    *attacked = true;
22,638,468 ✔
777
                    if (__builtin_expect(fd->end, false)) { break; }
22,638,468 ✔
778
                    //Apply corrosion
779
                    apply_corrosion(status);
21,293,535 ✔
780
                }
781
                else
782
                {
783
                  // Remove Corrosion
784
                  remove_corrosion(status);
2,653,750 ✔
785
                }
786
            }
787
            else
788
            {
789
                _DEBUG_MSG(2, "%s cannot take attack.\n", status_description(status).c_str());
169,765 ✔
790
                // Remove Corrosion
791
                remove_corrosion(status);
169,765 ✔
792
            }
793
        }
794
        fd->finalize_action();
65,624,085 ✔
795
        // Flurry
796
        if (can_act(status) && status->has_skill(Skill::flurry) && (status->m_skill_cd[Skill::flurry] == 0))
132,792,706 ✔
797
        {
798
#ifndef NQUEST
799
            if (status->m_player == 0)
800
            {
801
                fd->inc_counter(QuestType::skill_use, Skill::flurry);
802
            }
803
#endif
804
            _DEBUG_MSG(1, "%s activates Flurry x %d\n",
2,433,674 ✔
805
                    status_description(status).c_str(), status->skill_base_value(Skill::flurry));
806
            num_actions += status->skill_base_value(Skill::flurry);
2,303,025 ✔
807
            for (const auto & ss : skills)
9,208,982 ✔
808
            {
809
                Skill::Skill evolved_skill_id = static_cast<Skill::Skill>(ss.id + status->m_evolved_skill_offset[ss.id]);
6,905,957 ✔
810
                if (evolved_skill_id == Skill::flurry)
6,905,957 ✔
811
                {
812
                    status->m_skill_cd[ss.id] = ss.c;
2,303,025 ✔
813
                }
814
            }
815
        }
816
    }
817
}
61,264,628 ✔
818

819
struct PlayCard
820
{
821
    const Card* card;
822
    Field* fd;
823
    CardStatus* status;
824
    Storage<CardStatus>* storage;
825
    const unsigned actor_index;
826
    const CardStatus* actor_status;
827

828
    PlayCard(const Card* card_, Field* fd_, unsigned ai_, CardStatus* as_) :
22,358,918 ✔
829
        card{card_},
22,358,918 ✔
830
        fd{fd_},
22,358,918 ✔
831
        status{nullptr},
22,358,918 ✔
832
        storage{nullptr},
22,358,918 ✔
833
        actor_index{ai_},
22,358,918 ✔
834
        actor_status{as_}
22,358,918 ✔
835
    {}
836

837
    template <enum CardType::CardType type>
838
        CardStatus* op()
18,872,230 ✔
839
        {
840
            return op<type>(false);
×
841
        }
842

843
    template <enum CardType::CardType type>
844
        CardStatus* op(bool summoned)
22,358,918 ✔
845
        {
846
            setStorage<type>();
22,358,918 ✔
847
            placeCard<type>(summoned);
22,358,918 ✔
848

849
            unsigned played_faction_mask(0);
22,358,918 ✔
850
            unsigned same_faction_cards_count(0);
22,358,918 ✔
851
            bool bge_megamorphosis = fd->bg_effects[status->m_player][PassiveBGE::megamorphosis];
22,358,918 ✔
852
            //played_status = status;
853
            //played_card = card;
854
            if(__builtin_expect(fd->fixes[Fix::barrier_each_turn],true) && status->has_skill(Skill::barrier)){
22,358,918 ✔
855
                _DEBUG_MSG(1,"%s gets barrier protection %u per turn\n",status_description(status).c_str(),status->skill(Skill::barrier));
482,113 ✔
856
                status->m_protected += status->skill(Skill::barrier);
368,897 ✔
857
            }
858
            if (status->m_delay == 0)
22,358,918 ✔
859
            {
860
                    check_and_perform_bravery(fd,status);
1,345,770 ✔
861
                check_and_perform_valor(fd, status);
1,345,770 ✔
862
            }
863
            
864

865
            //refresh/init absorb
866
            if(status->has_skill(Skill::absorb))
22,358,918 ✔
867
            {
868
                status->m_absorption = status->skill_base_value(Skill::absorb);
1,364,998 ✔
869
            }
870

871

872
            // 1. Evaluate skill Allegiance & count assaults with same faction (structures will be counted later)
873
            // 2. Passive BGE Cold Sleep
874
            for (CardStatus* status_i : fd->players[status->m_player]->assaults.m_indirect)
87,299,286 ✔
875
            {
876
                if (status_i == status || !is_alive(status_i)) { continue; } // except itself
64,940,368 ✔
877
                //std::cout << status_description(status_i).c_str();
878
                _DEBUG_ASSERT(is_alive(status_i));
47,121,825 ✔
879
                if (bge_megamorphosis || (status_i->m_card->m_faction == card->m_faction))
47,121,825 ✔
880
                {
881
                    ++ same_faction_cards_count;
19,751,406 ✔
882
                    unsigned allegiance_value = status_i->skill(Skill::allegiance);
19,751,406 ✔
883
                    if (__builtin_expect(allegiance_value, false) && !status->m_summoned)
19,751,406 ✔
884
                    {
885
                        _DEBUG_MSG(1, "%s activates Allegiance %u\n", status_description(status_i).c_str(), allegiance_value);
1,085,259 ✔
886
                        if (! status_i->m_sundered)
997,071 ✔
887
                        { status_i->m_perm_attack_buff += allegiance_value; }
959,878 ✔
888
                        status_i->ext_hp(allegiance_value);
997,071 ✔
889
                    }
890
                }
891
                if (__builtin_expect(fd->bg_effects[status->m_player][PassiveBGE::coldsleep], false)
47,121,825 ✔
892
                        && status_i->m_protected_stasis && can_be_healed(status_i))
112,062,193 ✔
893
                {
894
                    unsigned bge_value = (status_i->m_protected_stasis + 1) / 2;
2,113 ✔
895
                    _DEBUG_MSG(1, "Cold Sleep: %s heals itself for %u\n", status_description(status_i).c_str(), bge_value);
6,339 ✔
896
                    status_i->add_hp(bge_value);
2,113 ✔
897
                }
898
            }
899

900
            // Setup faction marks (bitmap) for stasis (skill Stasis / Passive BGE TemporalBacklash)
901
            // unless Passive BGE Megamorphosis is enabled
902
            if (__builtin_expect(!bge_megamorphosis, true))
22,358,918 ✔
903
            {
904
                played_faction_mask = (1u << card->m_faction);
22,293,979 ✔
905
                // do played card have stasis? mark this faction for stasis check
906
                if (__builtin_expect(status->skill(Skill::stasis), false)
22,293,979 ✔
907
                        || __builtin_expect(fd->bg_effects[status->m_player][PassiveBGE::temporalbacklash] && status->skill(Skill::counter), false))
22,293,979 ✔
908
                {
909
                    fd->players[status->m_player]->stasis_faction_bitmap |= played_faction_mask;
4,190,071 ✔
910
                }
911
            }
912

913
            // Evaluate Passive BGE Oath-of-Loyalty
914
            unsigned allegiance_value;
915
            if (__builtin_expect(fd->bg_effects[status->m_player][PassiveBGE::oath_of_loyalty], false)
22,358,918 ✔
916
                    && ((allegiance_value = status->skill(Skill::allegiance)) > 0))
22,358,918 ✔
917
            {
918
                // count structures with same faction (except fortresses, dominions and other non-normal structures)
919
                for (CardStatus * status_i : fd->players[status->m_player]->structures.m_indirect)
2,083 ✔
920
                {
921
                    if ((status_i->m_card->m_category == CardCategory::normal)
882 ✔
922
                            && (bge_megamorphosis || (status_i->m_card->m_faction == card->m_faction)))
882 ✔
923
                    {
924
                        ++ same_faction_cards_count;
×
925
                    }
926
                }
927

928
                // apply Passive BGE Oath-of-Loyalty when multiplier isn't zero
929
                if (same_faction_cards_count)
1,201 ✔
930
                {
931
                    unsigned bge_value = allegiance_value * same_faction_cards_count;
398 ✔
932
                    _DEBUG_MSG(1, "Oath of Loyalty: %s activates Allegiance %u x %u = %u\n",
796 ✔
933
                            status_description(status).c_str(), allegiance_value, same_faction_cards_count, bge_value);
934
                    status->m_perm_attack_buff += bge_value;
398 ✔
935
                    status->ext_hp(bge_value);
398 ✔
936
                }
937
            }
938

939
            // summarize stasis when:
940
            //  1. Passive BGE Megamorphosis is enabled
941
            //  2. current faction is marked for it
942
            if ((card->m_delay > 0) && (card->m_type == CardType::assault)
21,013,148 ✔
943
                    && __builtin_expect(bge_megamorphosis || (fd->players[status->m_player]->stasis_faction_bitmap & played_faction_mask), false))
38,896,815 ✔
944
            {
945
                unsigned stacked_stasis = (bge_megamorphosis || (fd->players[status->m_player]->commander.m_card->m_faction == card->m_faction))
8,382,702 ✔
946
                    ? fd->players[status->m_player]->commander.skill(Skill::stasis)
15,397,756 ✔
947
                    : 0u;
948
#ifndef NDEBUG
949
                if (stacked_stasis > 0)
7,015,054 ✔
950
                {
951
                    _DEBUG_MSG(2, "+ Stasis [%s]: stacks +%u stasis protection from %s (total stacked: %u)\n",
×
952
                            faction_names[card->m_faction].c_str(), stacked_stasis,
953
                            status_description(&fd->players[status->m_player]->commander).c_str(), stacked_stasis);
954
                }
955
#endif
956
                for (CardStatus * status_i : fd->players[status->m_player]->structures.m_indirect)
15,954,564 ✔
957
                {
958
                    if ((bge_megamorphosis || (status_i->m_card->m_faction == card->m_faction)) && is_alive(status_i))
7,528,733 ✔
959
                    {
960
                        stacked_stasis += status_i->skill(Skill::stasis);
1,975,731 ✔
961
#ifndef NDEBUG
962
                        if (status_i->skill(Skill::stasis) > 0)
1,975,731 ✔
963
                        {
964
                            _DEBUG_MSG(2, "+ Stasis [%s]: stacks +%u stasis protection from %s (total stacked: %u)\n",
7,528,733 ✔
965
                                    faction_names[card->m_faction].c_str(), status_i->skill(Skill::stasis),
966
                                    status_description(status_i).c_str(), stacked_stasis);
967
                        }
968
#endif
969
                    }
970
                }
971
                for (CardStatus * status_i : fd->players[status->m_player]->assaults.m_indirect)
33,197,258 ✔
972
                {
973
                    if ((bge_megamorphosis || (status_i->m_card->m_faction == card->m_faction)) && is_alive(status_i))
24,771,427 ✔
974
                    {
975
                        stacked_stasis += status_i->skill(Skill::stasis);
18,915,196 ✔
976
#ifndef NDEBUG
977
                        if (status_i->skill(Skill::stasis) > 0)
18,915,196 ✔
978
                        {
979
                            _DEBUG_MSG(2, "+ Stasis [%s]: stacks +%u stasis protection from %s (total stacked: %u)\n",
8,251,732 ✔
980
                                    faction_names[card->m_faction].c_str(), status_i->skill(Skill::stasis),
981
                                    status_description(status_i).c_str(), stacked_stasis);
982
                        }
983
#endif
984
                        if (__builtin_expect(fd->bg_effects[status->m_player][PassiveBGE::temporalbacklash] && status_i->skill(Skill::counter), false))
18,915,196 ✔
985
                        {
986
                            stacked_stasis += (status_i->skill(Skill::counter) + 1) / 2;
3,031 ✔
987
#ifndef NDEBUG
988
                            _DEBUG_MSG(2, "Temporal Backlash: + Stasis [%s]: stacks +%u stasis protection from %s (total stacked: %u)\n",
24,774,458 ✔
989
                                    faction_names[card->m_faction].c_str(), (status_i->skill(Skill::counter) + 1) / 2,
990
                                    status_description(status_i).c_str(), stacked_stasis);
991
#endif
992
                        }
993
                    }
994
                }
995
                status->m_protected_stasis = stacked_stasis;
8,425,831 ✔
996
#ifndef NDEBUG
997
                if (stacked_stasis > 0)
8,425,831 ✔
998
                {
999
                    _DEBUG_MSG(1, "%s gains %u stasis protection\n",
6,903,241 ✔
1000
                            status_description(status).c_str(), stacked_stasis);
1001
                }
1002
#endif
1003
                // no more stasis for current faction: do unmark (if no Passive BGE Megamorphosis)
1004
                if (__builtin_expect(!bge_megamorphosis, true) && __builtin_expect(!stacked_stasis, false))
8,425,831 ✔
1005
                {
1006
                    fd->players[status->m_player]->stasis_faction_bitmap &= ~played_faction_mask;
1,757,012 ✔
1007
                    _DEBUG_MSG(1, "- Stasis [%s]: no more units with stasis from %s\n",
1,766,406 ✔
1008
                            faction_names[card->m_faction].c_str(),status_description(&fd->players[status->m_player]->commander).c_str());
1009
                }
1010

1011
            }
1012
            //Devotion BGE
1013
            if (__builtin_expect(fd->bg_effects[status->m_player][PassiveBGE::devotion], false)
22,358,918 ✔
1014
                    && !summoned && card->m_category == CardCategory::normal
64,769 ✔
1015
                    && fd->players[status->m_player]->commander.m_card->m_faction == card->m_faction)
22,418,887 ✔
1016
            {
1017
                unsigned devotion_percent = fd->bg_effects[status->m_player][PassiveBGE::devotion];
9,422 ✔
1018
                unsigned bge_buff = (card->m_health*devotion_percent+99)/100;
9,422 ✔
1019
                _DEBUG_MSG(1, "Devotion %s: Gains %u HP\n",
18,844 ✔
1020
                        status_description(status).c_str(), bge_buff);
1021
                status->ext_hp(bge_buff); // <bge_value>% bonus health (rounded up)
9,422 ✔
1022
            }
1023

1024

1025
            // resolve On-Play skills
1026
            // Fix Death on BGE: [On Play] skills during BGE phase can be invoked only by means of [On Death] trigger
1027
            if (__builtin_expect(fd->fixes[Fix::death_from_bge] && (fd->current_phase != Field::bge_phase), true))
22,358,918 ✔
1028
            {
1029
                for (const auto& ss: card->m_skills_on_play)
26,134,048 ✔
1030
                {
1031
                    _DEBUG_MSG(2, "On Play %s: Preparing (tail) skill %s\n",
3,775,130 ✔
1032
                            status_description(status).c_str(), skill_description(fd->cards, ss).c_str());
1033
                    fd->skill_queue.emplace_back(status, ss);
3,775,130 ✔
1034
                }
1035
            }
1036
            else
1037
            {
1038
                _DEBUG_MSG(2, "Death from BGE Fix: suppress [On Play] skills invoked by [On Death] summon\n");
×
1039
            }
1040

1041
            return status;
22,358,918 ✔
1042
        }
1043

1044
    template <enum CardType::CardType>
1045
        void setStorage()
1046
        {
1047
        }
1048

1049
    template <enum CardType::CardType type>
1050
        void placeCard(bool summoned)
22,358,918 ✔
1051
        {
1052
            status = &storage->add_back();
22,358,918 ✔
1053
            status->set(card);
22,358,918 ✔
1054
            status->m_index = storage->size() - 1;
22,358,918 ✔
1055
            status->m_player = actor_index;
22,358,918 ✔
1056
            status->m_field = fd;
22,358,918 ✔
1057
            status->m_summoned = summoned;
22,358,918 ✔
1058
            _DEBUG_MSG(1, "%s placeCard\n", status_description(status).c_str());
24,870,889 ✔
1059

1060
            // reduce delay for summoned card by tower (structure) for normal (non-triggered) summon skill
1061
            if (summoned && __builtin_expect(fd->fixes[Fix::reduce_delay_summoned_by_tower], true)
22,358,918 ✔
1062
                && actor_status->m_card->m_skill_trigger[Skill::summon] == Skill::Trigger::activate
3,472,794 ✔
1063
                && actor_status->m_card->m_type == CardType::structure
377,478 ✔
1064
                && status->m_delay > 0)
8,782 ✔
1065
            {
1066
                --status->m_delay;
8,782 ✔
1067

1068
                _DEBUG_MSG(1, "%s reduces its timer (as summoned by tower)\n", status_description(status).c_str());
17,564 ✔
1069
            }
1070

1071
#ifndef NQUEST
1072
            if (actor_index == 0)
1073
            {
1074
                if (card->m_type == CardType::assault)
1075
                {
1076
                    fd->inc_counter(QuestType::faction_assault_card_use, card->m_faction);
1077
                }
1078
                fd->inc_counter(QuestType::type_card_use, type);
1079
            }
1080
#endif
1081
            _DEBUG_MSG(1, "%s plays %s %u [%s]\n",
24,870,889 ✔
1082
                    status_description(actor_status).c_str(), cardtype_names[type].c_str(),
1083
                    static_cast<unsigned>(storage->size() - 1), card_description(fd->cards, card).c_str());
1084
        }
22,358,918 ✔
1085
};
1086
// assault
1087
    template <>
1088
void PlayCard::setStorage<CardType::assault>()
17,747,284 ✔
1089
{
1090
    storage = &fd->players[actor_index]->assaults;
17,747,284 ✔
UNCOV
1091
}
×
1092
// structure
1093
    template <>
1094
void PlayCard::setStorage<CardType::structure>()
4,611,634 ✔
1095
{
1096
    storage = &fd->players[actor_index]->structures;
4,611,634 ✔
UNCOV
1097
}
×
1098

1099
// Check if a skill actually proc'ed.
1100
    template<Skill::Skill skill_id>
1101
inline bool skill_check(Field* fd, CardStatus* c, CardStatus* ref)
245,066,497 ✔
1102
{
1103
    return is_defensive_skill(skill_id) || is_alive(c);
2,762,958 ✔
1104
}
1105

1106
    template<>
1107
inline bool skill_check<Skill::heal>(Field* fd, CardStatus* c, CardStatus* ref)
74,701,017 ✔
1108
{
1109
    return can_be_healed(c);
38,490 ✔
1110
}
1111

1112
    template<>
1113
inline bool skill_check<Skill::mend>(Field* fd, CardStatus* c, CardStatus* ref)
157,362 ✔
1114
{
UNCOV
1115
    return can_be_healed(c);
×
1116
}
1117

1118
    template<>
1119
inline bool skill_check<Skill::rally>(Field* fd, CardStatus* c, CardStatus* ref)
39,594,368 ✔
1120
{
1121
    return !c->m_sundered;
39,594,368 ✔
1122
}
1123

1124
    template<>
1125
inline bool skill_check<Skill::overload>(Field* fd, CardStatus* c, CardStatus* ref)
844,316 ✔
1126
{
1127
    return is_active(c) && !c->m_overloaded && !has_attacked(c);
663,693 ✔
1128
}
1129

1130
    template<>
1131
inline bool skill_check<Skill::jam>(Field* fd, CardStatus* c, CardStatus* ref)
10,458,902 ✔
1132
{
1133
    if(__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::ironwill], false) && c->has_skill(Skill::armor))return false;
10,458,902 ✔
1134
    // active player performs Jam
1135
    if (fd->tapi == ref->m_player)
10,456,630 ✔
1136
    { return is_active_next_turn(c); }
10,428,228 ✔
1137

1138

1139
    // inactive player performs Jam
1140
    return will_activate_this_turn(c);
28,402 ✔
1141
}
1142

1143
    template<>
1144
inline bool skill_check<Skill::leech>(Field* fd, CardStatus* c, CardStatus* ref)
61,476 ✔
1145
{
1146
    return can_be_healed(c) || (fd->fixes[Fix::leech_increase_max_hp] && is_alive(c));
53,594 ✔
1147
}
1148

1149
    template<>
1150
inline bool skill_check<Skill::coalition>(Field* fd, CardStatus* c, CardStatus* ref)
1151
{
1152
    return is_active(c);
1153
}
1154

1155
    template<>
1156
inline bool skill_check<Skill::payback>(Field* fd, CardStatus* c, CardStatus* ref)
2,731,177 ✔
1157
{
1158
    return (ref->m_card->m_type == CardType::assault);
2,731,177 ✔
1159
}
1160

1161
    template<>
1162
inline bool skill_check<Skill::revenge>(Field* fd, CardStatus* c, CardStatus* ref)
1163
{
1164
    return skill_check<Skill::payback>(fd, c, ref);
1165
}
1166

1167
    template<>
1168
inline bool skill_check<Skill::tribute>(Field* fd, CardStatus* c, CardStatus* ref)
118,307,697 ✔
1169
{
1170
    return (ref->m_card->m_type == CardType::assault) && (c != ref);
118,307,697 ✔
1171
}
1172

1173
    template<>
1174
inline bool skill_check<Skill::refresh>(Field* fd, CardStatus* c, CardStatus* ref)
3,586,556 ✔
1175
{
1176
    return can_be_healed(c);
3,586,556 ✔
1177
}
1178

1179
    template<>
1180
inline bool skill_check<Skill::drain>(Field* fd, CardStatus* c, CardStatus* ref)
812,723 ✔
1181
{
1182
    return can_be_healed(c);
812,723 ✔
1183
}
1184

1185
    template<>
1186
inline bool skill_check<Skill::mark>(Field* fd, CardStatus* c, CardStatus* ref)
58,513 ✔
1187
{
1188
    return (ref->m_card->m_type == CardType::assault);
58,513 ✔
1189
}
1190

1191
    template<>
1192
inline bool skill_check<Skill::disease>(Field* fd, CardStatus* c, CardStatus* ref)
191,246 ✔
1193
{
1194
    return is_alive(c) && (ref->m_card->m_type == CardType::assault);
191,246 ✔
1195
}
1196
    template<>
1197
inline bool skill_check<Skill::inhibit>(Field* fd, CardStatus* c, CardStatus* ref)
2,905,486 ✔
1198
{
1199
    return is_alive(c) && (ref->m_card->m_type == CardType::assault);
2,905,486 ✔
1200
}
1201
    template<>
1202
inline bool skill_check<Skill::sabotage>(Field* fd, CardStatus* c, CardStatus* ref)
448,294 ✔
1203
{
1204
    return is_alive(c) && (ref->m_card->m_type == CardType::assault);
448,294 ✔
1205
}
1206
inline unsigned remove_disease(CardStatus* status, unsigned heal)
35,958,307 ✔
1207
{
1208
    unsigned remaining_heal(heal);
35,958,307 ✔
1209
    if(__builtin_expect(status->m_diseased == 0,true))
35,958,307 ✔
1210
    {
1211
        //skip
1212
    }
1213
    else if (heal > status->m_diseased)
75,048 ✔
1214
    {
1215
        _DEBUG_MSG(1, "%s disease-blocked %u heal\n", status_description(status).c_str(), status->m_diseased);
18,714 ✔
1216
        remaining_heal = heal - status->m_diseased;
18,714 ✔
1217
        status->m_diseased = 0;
18,714 ✔
1218
    }
1219
    else
1220
    {
1221
        _DEBUG_MSG(1, "%s disease-blocked %u heal\n", status_description(status).c_str(), heal);
56,334 ✔
1222
        status->m_diseased -= heal;
56,334 ✔
1223
        remaining_heal = 0;
56,334 ✔
1224
    }
1225
    return remaining_heal;
35,958,307 ✔
1226
}
1227

1228
// Field is currently not needed for remove_absorption, but is here for similiar structure as remove_hp
1229
inline unsigned remove_absorption(Field* fd, CardStatus* status, unsigned dmg)
25,160,637 ✔
1230
{
1231
    return remove_absorption(status,dmg);
25,160,637 ✔
1232
}
1233
/**
1234
 * @brief Remove absorption from a CardStatus
1235
 * 
1236
 * @param status CardStatus to remove absorption from
1237
 * @param dmg damage to remove absorption from
1238
 * @return unsigned remaining damage after absorption is removed
1239
 */
1240
inline unsigned remove_absorption(CardStatus* status, unsigned dmg)
25,160,637 ✔
1241
{
1242
    unsigned remaining_dmg(dmg);
25,160,637 ✔
1243
    if(__builtin_expect(status->m_absorption == 0,true))
25,160,637 ✔
1244
    {
1245
        //skip
1246
    }
1247
    else if(dmg > status->m_absorption)
2,586,691 ✔
1248
    {
1249
        _DEBUG_MSG(1, "%s absorbs %u damage\n", status_description(status).c_str(), status->m_absorption);
360,141 ✔
1250
        remaining_dmg = dmg - status->m_absorption;
360,141 ✔
1251
        status->m_absorption = 0;
360,141 ✔
1252
    }
1253
    else
1254
    {
1255
        _DEBUG_MSG(1, "%s absorbs %u damage\n", status_description(status).c_str(), dmg);
2,226,550 ✔
1256
        status->m_absorption -= dmg;
2,226,550 ✔
1257
        remaining_dmg = 0;
2,226,550 ✔
1258
    }
1259
    return remaining_dmg;
25,160,637 ✔
1260
}
1261

1262
void remove_hp(Field* fd, CardStatus* status, unsigned dmg)
36,993,349 ✔
1263
{
1264
    if (__builtin_expect(!dmg, false)) { return; }
36,993,349 ✔
1265
    _DEBUG_ASSERT(is_alive(status));
26,335,501 ✔
1266
    _DEBUG_MSG(2, "%s takes %u damage\n", status_description(status).c_str(), dmg);
26,335,501 ✔
1267
    status->m_hp = safe_minus(status->m_hp, dmg);
26,335,501 ✔
1268
    if (fd->current_phase < Field::end_phase && status->has_skill(Skill::barrier))
26,335,501 ✔
1269
    {
1270
        ++fd->damaged_units_to_times[status];
236,645 ✔
1271
        _DEBUG_MSG(2, "%s damaged %u times\n",
236,645 ✔
1272
                status_description(status).c_str(), fd->damaged_units_to_times[status]);
1273
    }
1274
    if (status->m_hp == 0)
26,335,501 ✔
1275
    {
1276
#ifndef NQUEST
1277
        if (status->m_player == 1)
1278
        {
1279
            if (status->m_card->m_type == CardType::assault)
1280
            {
1281
                fd->inc_counter(QuestType::faction_assault_card_kill, status->m_card->m_faction);
1282
            }
1283
            fd->inc_counter(QuestType::type_card_kill, status->m_card->m_type);
1284
        }
1285
#endif
1286
        _DEBUG_MSG(1, "%s dies\n", status_description(status).c_str());
9,815,042 ✔
1287
        _DEBUG_ASSERT(status->m_card->m_type != CardType::commander);
9,815,042 ✔
1288
        fd->killed_units.push_back(status);
9,815,042 ✔
1289
        ++fd->players[status->m_player]->total_cards_destroyed;
9,815,042 ✔
1290
        if(!status->m_summoned)++fd->players[status->m_player]->total_nonsummon_cards_destroyed;
9,815,042 ✔
1291
        if (__builtin_expect((status->m_player == 0) && (fd->players[0]->deck->vip_cards.count(status->m_card->m_id)), false))
12,795,783 ✔
1292
        {
1293
            fd->players[0]->commander.m_hp = 0;
×
UNCOV
1294
            fd->end = true;
×
1295
        }
1296
    }
1297
}
1298

1299
inline bool is_it_dead(CardStatus& c)
152,011,773 ✔
1300
{
1301
    if (c.m_hp == 0) // yes it is
152,011,773 ✔
1302
    {
1303
        _DEBUG_MSG(1, "Dead and removed: %s\n", status_description(&c).c_str());
9,815,042 ✔
1304
        return true;
9,815,042 ✔
1305
    }
1306
    return false; // nope still kickin'
1307
}
1308

UNCOV
1309
inline bool is_it_dominion(CardStatus* c)
×
1310
{
UNCOV
1311
    return (c->m_card->m_category == CardCategory::dominion_alpha);
×
1312
}
1313

1314
inline void remove_dead(Storage<CardStatus>& storage)
77,088,160 ✔
1315
{
1316
    storage.remove(is_it_dead);
77,088,160 ✔
1317
}
1318

1319
void cooldown_skills(CardStatus * status)
50,098,433 ✔
1320
{
1321
    for (const auto & ss : status->m_card->m_skills)
193,617,451 ✔
1322
    {
1323
        if (status->m_skill_cd[ss.id] > 0)
143,519,018 ✔
1324
        {
1325
            _DEBUG_MSG(2, "%s reduces timer (%u) of skill %s\n",
3,251,125 ✔
1326
                    status_description(status).c_str(), status->m_skill_cd[ss.id], skill_names[ss.id].c_str());
3,251,125 ✔
1327
            -- status->m_skill_cd[ss.id];
3,251,125 ✔
1328
        }
1329
    }
1330
}
50,098,433 ✔
1331
/**
1332
 * Handle:
1333
 * Absorb, (Activation)Summon, Bravery, (Initial)Valor, Inhibit, Sabotage, Disease, Enhance, (Cooldown/Every X) Reductions
1334
 * 
1335
 * Does not handle these skills for newly summoned units ( i.e. valor, barrier)
1336
 **/
1337
void turn_start_phase_update(Field*fd, CardStatus * status)
60,815,795 ✔
1338
{
1339
            //apply Absorb + Triggered\{Valor} Enhances
1340
            check_and_perform_early_enhance(fd,status);
121,631,590 ✔
1341
            if(fd->fixes[Fix::enhance_early])check_and_perform_later_enhance(fd,status);
60,815,795 ✔
1342
            //refresh absorb
1343
            if(status->has_skill(Skill::absorb))
60,815,795 ✔
1344
            {
1345
                status->m_absorption = status->skill_base_value(Skill::absorb);
4,385,852 ✔
1346
            }
1347
            if(__builtin_expect(fd->fixes[Fix::barrier_each_turn],true) && status->has_skill(Skill::barrier)){
60,815,795 ✔
1348
                _DEBUG_MSG(1,"%s gets barrier protection %u per turn\n",status_description(status).c_str(),status->skill(Skill::barrier));
965,786 ✔
1349
                status->m_protected += status->skill(Skill::barrier);
965,786 ✔
1350
            }
1351
            check_and_perform_bravery(fd,status);
60,815,795 ✔
1352
            if (status->m_delay > 0)
60,815,795 ✔
1353
            {
1354
                _DEBUG_MSG(1, "%s reduces its timer\n", status_description(status).c_str());
29,989,402 ✔
1355
                --status->m_delay;
29,989,402 ✔
1356
                if (status->m_delay == 0)
29,989,402 ✔
1357
                {
1358
                    check_and_perform_valor(fd, status);
12,989,835 ✔
1359
                    if (status->m_card->m_skill_trigger[Skill::summon] == Skill::Trigger::activate)
12,989,835 ✔
1360
                    {
1361
                        check_and_perform_summon(fd, status);
10,624,507 ✔
1362
                    }
1363
                }
1364
            }
1365
            else
1366
            {
1367
                cooldown_skills(status);
30,826,393 ✔
1368
            }
1369
}
60,815,795 ✔
1370

1371
void turn_start_phase(Field* fd)
19,272,040 ✔
1372
{
1373
    // Active player's commander card:
1374
    cooldown_skills(&fd->tap->commander);
19,272,040 ✔
1375
    //grab assaults before new ones get summoned
1376
    auto& assaults(fd->tap->assaults);
19,272,040 ✔
1377
    //grab structures before new ones get summoned
1378
    auto& structures(fd->tap->structures);
19,272,040 ✔
1379
    // We store the assaults size here that exist at the begining of a turn
1380
    // because the turn_start_phase_update of a structure should not be triggered
1381
    // of a assault summoned by a struct. Similarly structures summoned by structures
1382
    // should not retrigger the new one.
1383
    unsigned assaults_end(assaults.size());
19,272,040 ✔
1384
    unsigned structures_end(structures.size());
19,272,040 ✔
1385

1386
    //Perform early enhance for commander
1387
    check_and_perform_early_enhance(fd,&fd->tap->commander);
38,544,080 ✔
1388
    if(fd->fixes[Fix::enhance_early])check_and_perform_later_enhance(fd,&fd->tap->commander);
19,272,040 ✔
1389

1390
    // Active player's structure cards:
1391
    // update index
1392
    // reduce delay; reduce skill cooldown
1393
    {
1394
        for(unsigned index(0); index < structures_end; ++index)
42,134,562 ✔
1395
        {
1396
            CardStatus * status = &structures[index];
22,862,522 ✔
1397
            status->m_index = index;
22,862,522 ✔
1398
            turn_start_phase_update(fd,status);
22,862,522 ✔
1399
        }
1400
    }
1401
    // Active player's assault cards:
1402
    // update index
1403
    // reduce delay; reduce skill cooldown
1404
    {
1405
        for(unsigned index(0); index < assaults_end; ++index)
57,225,313 ✔
1406
        {
1407
            CardStatus * status = &assaults[index];
37,953,273 ✔
1408
            status->m_index = index;
37,953,273 ✔
1409
            turn_start_phase_update(fd,status);
37,953,273 ✔
1410
        }
1411
    }
1412
    // Defending player's structure cards:
1413
    // update index
1414
    {
19,272,040 ✔
1415
        auto& structures(fd->tip->structures);
19,272,040 ✔
1416
        for(unsigned index(0), end(structures.size()); index < end; ++index)
42,958,862 ✔
1417
        {
1418
            CardStatus& status(structures[index]);
23,686,822 ✔
1419
            status.m_index = index;
23,686,822 ✔
1420
        }
1421
    }
1422
    // Defending player's assault cards:
1423
    // update index
1424
    {
19,272,040 ✔
1425
        auto& assaults(fd->tip->assaults);
19,272,040 ✔
1426
        for(unsigned index(0), end(assaults.size()); index < end; ++index)
65,894,278 ✔
1427
        {
1428
            CardStatus& status(assaults[index]);
46,622,238 ✔
1429
            status.m_index = index;
46,622,238 ✔
1430
        }
1431
    }
1432
}
19,272,040 ✔
1433

1434
void turn_end_phase(Field* fd)
19,272,040 ✔
1435
{
1436
    // Inactive player's assault cards:
1437
    {
19,272,040 ✔
1438
        auto& assaults(fd->tip->assaults);
19,272,040 ✔
1439
        for(unsigned index(0), end(assaults.size()); index < end; ++ index)
65,971,439 ✔
1440
        {
1441
            CardStatus& status(assaults[index]);
46,699,399 ✔
1442
            if (status.m_hp <= 0)
46,699,399 ✔
1443
            {
1444
                continue;
7,966,999 ✔
1445
            }
1446
            status.m_enfeebled = 0;
38,732,400 ✔
1447
            status.m_protected = 0;
38,732,400 ✔
1448
            if (status.m_skills_modified)
38,732,400 ✔
1449
            {
1450
                std::memset(status.m_primary_skill_offset, 0, sizeof status.m_primary_skill_offset);
4,024,625 ✔
1451
                std::memset(status.m_evolved_skill_offset, 0, sizeof status.m_evolved_skill_offset);
4,024,625 ✔
1452
                std::memset(status.m_enhanced_value, 0, sizeof status.m_enhanced_value);
4,024,625 ✔
1453
                status.m_skills_modified = false;
4,024,625 ✔
1454
            }
1455
            status.m_evaded = 0;  // so far only useful in Inactive turn
38,732,400 ✔
1456
            status.m_paybacked = 0;  // ditto
38,732,400 ✔
1457
            status.m_entrapped = 0;
38,732,400 ✔
1458
        }
1459
    }
1460
    // Inactive player's structure cards:
1461
    {
19,272,040 ✔
1462
        auto& structures(fd->tip->structures);
19,272,040 ✔
1463
        for(unsigned index(0), end(structures.size()); index < end; ++ index)
42,962,355 ✔
1464
        {
1465
            CardStatus& status(structures[index]);
23,690,315 ✔
1466
            if (status.m_hp <= 0)
23,690,315 ✔
1467
            {
1468
                continue;
981,143 ✔
1469
            }
1470
            // reset the structure's (barrier) protect
1471
            status.m_protected = 0;
22,709,172 ✔
1472
            status.m_evaded = 0;  // so far only useful in Inactive turn
22,709,172 ✔
1473
        }
1474
    }
1475

1476
    // Active player's assault cards:
1477
    {
19,272,040 ✔
1478
        auto& assaults(fd->tap->assaults);
19,272,040 ✔
1479
        for(unsigned index(0), end(assaults.size()); index < end; ++ index)
74,895,001 ✔
1480
        {
1481
            CardStatus& status(assaults[index]);
55,622,961 ✔
1482
            if (status.m_hp <= 0)
55,622,961 ✔
1483
            {
1484
                continue;
845,164 ✔
1485
            }
1486
            unsigned refresh_value = status.skill(Skill::refresh) + (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::crackdown],false)?(status.skill(Skill::subdue)+1)/2:0); //BGE: crackdown refresh+=subdue/2
54,777,797 ✔
1487

1488
            if (refresh_value && skill_check<Skill::refresh>(fd, &status, nullptr))
54,777,797 ✔
1489
            {
1490
                _DEBUG_MSG(1, "%s refreshes %u health\n", status_description(&status).c_str(), refresh_value);
142,931 ✔
1491
                status.add_hp(refresh_value);
142,931 ✔
1492
            }
1493
            if (status.m_poisoned > 0)
54,777,797 ✔
1494
            {
1495
                if(! __builtin_expect(fd->fixes[Fix::poison_after_attacked],true))
109,580 ✔
1496
                {
UNCOV
1497
                    unsigned poison_dmg = remove_absorption(fd,&status,status.m_poisoned + status.m_enfeebled);
×
UNCOV
1498
                    poison_dmg = safe_minus(poison_dmg, status.protected_value());
×
UNCOV
1499
                    if (poison_dmg > 0)
×
1500
                    {
1501
#ifndef NQUEST
1502
                    if (status.m_player == 1)
1503
                    {
1504
                        fd->inc_counter(QuestType::skill_damage, Skill::poison, 0, poison_dmg);
1505
                    }
1506
#endif
UNCOV
1507
                        _DEBUG_MSG(1, "%s takes poison damage %u\n", status_description(&status).c_str(), poison_dmg);
×
UNCOV
1508
                        remove_hp(fd, &status, poison_dmg);  // simultaneous
×
1509
                    }
1510
                }
1511
                else {
1512
                    unsigned poison_dmg = status.m_poisoned;
109,580 ✔
1513
                    _DEBUG_MSG(1, "%s takes poison damage %u\n", status_description(&status).c_str(), poison_dmg);
109,580 ✔
1514
                    remove_hp(fd, &status, poison_dmg);  // simultaneous
109,580 ✔
1515
                    status.m_poisoned = status.m_poisoned - (poison_dmg+1)/2;
109,580 ✔
1516
                }
1517
            }
1518
            // end of the opponent's next turn for enemy units
1519
            status.m_temp_attack_buff = 0;
54,777,797 ✔
1520
            status.m_jammed = false;
54,777,797 ✔
1521
            status.m_enraged = 0;
54,777,797 ✔
1522
            status.m_sundered = false;
54,777,797 ✔
1523
            status.m_inhibited = 0;
54,777,797 ✔
1524
            status.m_sabotaged = 0;
54,777,797 ✔
1525
            status.m_tributed = 0;
54,777,797 ✔
1526
            status.m_overloaded = false;
54,777,797 ✔
1527
            status.m_step = CardStep::none;
54,777,797 ✔
1528
        }
1529
    }
1530
    // Active player's structure cards:
1531
    // nothing so far
1532

1533
    prepend_on_death(fd);  // poison
19,272,040 ✔
1534
    resolve_skill(fd);
19,272,040 ✔
1535
    remove_dead(fd->tap->assaults);
19,272,040 ✔
1536
    remove_dead(fd->tap->structures);
19,272,040 ✔
1537
    remove_dead(fd->tip->assaults);
19,272,040 ✔
1538
    remove_dead(fd->tip->structures);
19,272,040 ✔
1539
}
19,272,040 ✔
1540

1541
//---------------------- $50 attack by assault card implementation -------------
1542
/**
1543
 * @brief Return the damage dealt to the attacker (att) by defender (def) through counter skill
1544
 * The counter is increased by the attacker's enfeebled value, while the attacker's protected value is subtracted from the damage.
1545
 * The absorption is removed from the damage before the protected value is subtracted.
1546
 * 
1547
 * @param fd Field pointer 
1548
 * @param att attacker CardStatus
1549
 * @param def defender CardStatus
1550
 * @return unsigned 
1551
 */
1552
inline unsigned counter_damage(Field* fd, CardStatus* att, CardStatus* def)
2,106,300 ✔
1553
{
1554
    _DEBUG_ASSERT(att->m_card->m_type == CardType::assault);
2,106,300 ✔
1555
    _DEBUG_ASSERT(def->skill(Skill::counter) > 0); // counter skill must be present otherwise enfeeble is wrongly applied
2,106,300 ✔
1556

1557
    return safe_minus(remove_absorption(fd,att,def->skill(Skill::counter) + att->m_enfeebled), att->protected_value());
2,106,300 ✔
1558
}
1559

1560
inline CardStatus* select_first_enemy_wall(Field* fd)
7,928,598 ✔
1561
{
1562
    for(unsigned i(0); i < fd->tip->structures.size(); ++i)
15,027,766 ✔
1563
    {
1564
        CardStatus* c(&fd->tip->structures[i]);
7,892,490 ✔
1565
        if (c->has_skill(Skill::wall) && is_alive(c)) { return c; }
7,892,490 ✔
1566
    }
1567
    return nullptr;
1568
}
1569

1570
inline CardStatus* select_first_enemy_assault(Field* fd)
1571
{
1572
    for(unsigned i(0); i < fd->tip->assaults.size(); ++i)
47,132 ✔
1573
    {
1574
        CardStatus* c(&fd->tip->assaults[i]);
37,320 ✔
1575
        if (is_alive(c)) { return c; }
37,320 ✔
1576
    }
1577
    return nullptr;
1578
}
1579

1580

1581
inline bool alive_assault(Storage<CardStatus>& assaults, unsigned index)
22,638,468 ✔
1582
{
1583
    return(assaults.size() > index && is_alive(&assaults[index]));
16,149,910 ✔
1584
}
1585

1586
void remove_commander_hp(Field* fd, CardStatus& status, unsigned dmg)
7,134,014 ✔
1587
{
1588
    _DEBUG_ASSERT(is_alive(&status));
7,134,014 ✔
1589
    _DEBUG_ASSERT(status.m_card->m_type == CardType::commander);
7,134,014 ✔
1590
    _DEBUG_MSG(2, "%s takes %u damage\n", status_description(&status).c_str(), dmg);
7,134,014 ✔
1591
    status.m_hp = safe_minus(status.m_hp, dmg);
7,134,014 ✔
1592
    if (status.m_hp == 0)
7,134,014 ✔
1593
    {
1594
        _DEBUG_MSG(1, "%s dies\n", status_description(&status).c_str());
1,344,933 ✔
1595
        fd->end = true;
1,344,933 ✔
1596
    }
1597
}
7,134,014 ✔
1598

1599
//------------------------------------------------------------------------------
1600
// implementation of one attack by an assault card, against either an enemy
1601
// assault card, the first enemy wall, or the enemy commander.
1602
struct PerformAttack
1603
{
1604
    Field* fd;
1605
    CardStatus* att_status;
1606
    CardStatus* def_status;
1607
    unsigned att_dmg;
1608

1609
    PerformAttack(Field* fd_, CardStatus* att_status_, CardStatus* def_status_) :
22,638,468 ✔
1610
        fd(fd_), att_status(att_status_), def_status(def_status_), att_dmg(0)
22,638,468 ✔
1611
    {}
1612
    /**
1613
     * @brief Perform the attack
1614
     * 
1615
     * New Evaluation order:
1616
     *  Flying
1617
     *  Subdue
1618
     *  Attack Damage
1619
     *  On-Attacked Skills
1620
     *  Counter
1621
     *  Leech
1622
     *  Drain
1623
     *  Berserk
1624
     *  Mark
1625
     * 
1626
     * Old Evaluation order:
1627
     *  check flying
1628
     *  modify damage
1629
     *  deal damage
1630
     *  assaults only: (poison,inihibit, sabotage)
1631
     *  on-attacked skills
1632
     *  counter, berserk
1633
     *  assaults only: (leech if still alive)
1634
     *  bge
1635
     *  subdue
1636
     * 
1637
     * @tparam def_cardtype 
1638
     * @return unsigned 
1639
     */
1640
    template<enum CardType::CardType def_cardtype>
1641
        unsigned op()
22,638,468 ✔
1642
        {
1643
            // Bug fix? 2023-04-03 a card with zero attack power can not attack and won't trigger subdue
1644
            // Confirmed subdue behaviour by MK 2023-07-12
1645
            if(att_status->attack_power()== 0) { return 0; }
22,638,468 ✔
1646

1647

1648
            if(__builtin_expect(def_status->has_skill(Skill::flying),false) && fd->flip()) {
22,638,468 ✔
1649
                _DEBUG_MSG(1, "%s flies away from %s\n",
102,806 ✔
1650
                        status_description(def_status).c_str(),
1651
                        status_description(att_status).c_str());
1652
                return 0;
51,403 ✔
1653
            }
1654

1655
            if ( __builtin_expect(fd->fixes[Fix::subdue_before_attack],true)) {
22,587,065 ✔
1656
                perform_subdue(fd, att_status, def_status);
22,587,065 ✔
1657
                if(att_status->attack_power() == 0) // MK - 05/01/2023 6:58 AM: "Previously, when an attack was made, it checked if it had 0 attack and canceled. Now, after that check, it applies Subdue and checks again if it has 0 attack, and then cancels the rest of the attack.Previously, when an attack was made, it checked if it had 0 attack and canceled. Now, after that check, it applies Subdue and checks again if it has 0 attack, and then cancels the rest of the attack."
22,587,065 ✔
1658
                { 
1659
                    return 0; 
1660
                }
1661
            }
1662
            
1663
            // APN Bug fix for subdue not reducing attack damage
1664
            // https://github.com/APN-Pucky/tyrant_optimize/issues/79
1665
            unsigned pre_modifier_dmg = att_status->attack_power();
22,583,332 ✔
1666

1667
            modify_attack_damage<def_cardtype>(pre_modifier_dmg);
22,583,332 ✔
1668

1669
            if(att_dmg) {
22,583,332 ✔
1670
                // Deal the attack damage
1671
                attack_damage<def_cardtype>();
18,857,146 ✔
1672

1673
                // Game Over -> return
1674
                if (__builtin_expect(fd->end, false)) { return att_dmg; }
18,857,146 ✔
1675
                damage_dependant_pre_oa<def_cardtype>();
10,929,810 ✔
1676
            }
1677
            // on attacked does also trigger if the attack damage is blocked to zero
1678
            on_attacked<def_cardtype>();
21,238,399 ✔
1679

1680
            // only if alive attacker
1681
            if (is_alive(att_status) && (__builtin_expect(fd->fixes[Fix::counter_without_damage],true) || att_dmg) )
21,238,399 ✔
1682
            {
1683
                perform_counter<def_cardtype>(fd, att_status, def_status);    
21,226,618 ✔
1684
            }
1685
            if (is_alive(att_status) && (__builtin_expect(fd->fixes[Fix::corrosive_protect_armor],true) ||att_dmg )) // MK - 05/01/2023 6:58 AM: "Corrosive will apply to an attacker even if the attack did 0 damage, just like new Counter"
21,238,399 ✔
1686
            {
1687
                perform_corrosive(fd, att_status, def_status);
20,492,384 ✔
1688
            }
1689
            if (is_alive(att_status) && att_dmg)
21,238,399 ✔
1690
            {
1691
                // Bug fix? 2023-04-03 leech after counter but before drain/berserk
1692
                // Skill: Leech
1693
                do_leech<def_cardtype>();
10,755,591 ✔
1694
            }
1695

1696
            // Bug fix? 2023-04-03 drain now part of the PerformAttack.op() instead of attack_phase, like hunt.
1697
            // perform swipe/drain
1698
            perform_swipe_drain<def_cardtype>(fd, att_status, def_status, att_dmg);
21,238,399 ✔
1699

1700
            if (is_alive(att_status) && att_dmg) {
21,238,399 ✔
1701
                perform_berserk(fd, att_status, def_status);
16,830,722 ✔
1702
            }
1703
            if (is_alive(att_status) )  {
21,238,399 ✔
1704
                perform_poison(fd, att_status, def_status);
20,492,384 ✔
1705
            }
1706

1707
            if (is_alive(att_status) && att_dmg) {
21,238,399 ✔
1708
                perform_mark(fd, att_status, def_status);
16,830,722 ✔
1709

1710
                perform_bge_heroism<def_cardtype>(fd, att_status, def_status);
16,830,722 ✔
1711
                perform_bge_devour<def_cardtype>(fd, att_status, def_status);
16,830,722 ✔
1712

1713
                if ( __builtin_expect(!fd->fixes[Fix::subdue_before_attack],false)) {
16,830,722 ✔
UNCOV
1714
                    perform_subdue(fd, att_status, def_status);
×
1715
                }
1716
            }
1717
            // perform hunt
1718
            perform_hunt(fd, att_status, def_status);
21,238,399 ✔
1719

1720
            return att_dmg;
21,238,399 ✔
1721
        }
1722

1723
        /**
1724
         * @brief Modify attack damage
1725
         * This setts att_dmg in PerformAttack.
1726
         * 
1727
         * @tparam CardType::CardType 
1728
         * @param pre_modifier_dmg base damage
1729
         */
1730
    template<enum CardType::CardType>
1731
        void modify_attack_damage(unsigned pre_modifier_dmg)
22,583,332 ✔
1732
        {
1733
            _DEBUG_ASSERT(att_status->m_card->m_type == CardType::assault);
22,583,332 ✔
1734
            att_dmg = pre_modifier_dmg;
22,583,332 ✔
1735
            if (att_dmg == 0)
22,583,332 ✔
UNCOV
1736
            { return; }
×
1737
#ifndef NDEBUG
1738
            std::string desc;
22,583,332 ✔
1739
#endif
1740
            auto& att_assaults = fd->tap->assaults; // (active) attacker assaults
22,583,332 ✔
1741
            auto& def_assaults = fd->tip->assaults; // (inactive) defender assaults
22,583,332 ✔
1742
            unsigned legion_value = 0;
22,583,332 ✔
1743
            unsigned coalition_value = 0;
22,583,332 ✔
1744

1745
            // Enhance damage (if additional damage isn't prevented)
1746
            if (! att_status->m_sundered)
22,583,332 ✔
1747
            {
1748
                // Skill: Mark
1749
                unsigned marked_value = def_status->m_marked;
21,320,571 ✔
1750
                if(marked_value)
21,320,571 ✔
1751
                {
1752
#ifndef NDEBUG
1753
                    if (debug_print > 0) { desc += "+" + tuo::to_string(marked_value) + "(mark)"; }
46,795 ✔
1754
#endif
1755
                    att_dmg += marked_value;
11,866 ✔
1756
                }
1757
                // Skill: Legion
1758
                unsigned legion_base = att_status->skill(Skill::legion);
21,320,571 ✔
1759
                if (__builtin_expect(legion_base, false))
21,320,571 ✔
1760
                {
1761
                    unsigned itr_idx, till_idx;
1762
                    bool bge_megamorphosis = fd->bg_effects[fd->tapi][PassiveBGE::megamorphosis] && !fd->fixes[Fix::legion_under_mega];
3,030,790 ✔
1763
                    //scan all assaults for Global Legion
1764
                    itr_idx = 0;
3,030,790 ✔
1765
                    till_idx = att_assaults.size() - 1;
3,030,790 ✔
1766
                    for (; itr_idx <= till_idx; ++ itr_idx)
19,851,125 ✔
1767
                    {
1768
                        if(itr_idx == att_status->m_index)continue; //legion doesn't count itself, unlike coalition
16,820,335 ✔
1769
                        legion_value += is_alive(&att_assaults[itr_idx])
27,555,343 ✔
1770
                          && (bge_megamorphosis || (att_assaults[itr_idx].m_card->m_faction == att_status->m_card->m_faction));
13,789,545 ✔
1771
                    }
1772
                    if (legion_value)
3,030,790 ✔
1773
                    {
1774
                        legion_value *= legion_base;
2,854,241 ✔
1775
#ifndef NDEBUG
1776
                        if (debug_print > 0) { desc += "+" + tuo::to_string(legion_value) + "(legion)"; }
3,118,247 ✔
1777
#endif
1778
                        att_dmg += legion_value;
2,854,241 ✔
1779
                    }
1780
                }
1781

1782
                // Skill: Coalition
1783
                unsigned coalition_base = att_status->skill(Skill::coalition);
21,320,571 ✔
1784
                if (__builtin_expect(coalition_base, false))
21,320,571 ✔
1785
                {
1786
                    uint8_t factions_bitmap = 0;
1,156,073 ✔
1787
                    for (CardStatus * status : att_assaults.m_indirect)
6,176,694 ✔
1788
                    {
1789
                        if (! is_alive(status)) { continue; }
5,020,621 ✔
1790
                        factions_bitmap |= (1 << (status->m_card->m_faction));
5,014,649 ✔
1791
                    }
1792
                    _DEBUG_ASSERT(factions_bitmap);
1,156,073 ✔
1793
                    unsigned uniq_factions = byte_bits_count(factions_bitmap);
1,156,073 ✔
1794
                    coalition_value = coalition_base * uniq_factions;
1,156,073 ✔
1795
#ifndef NDEBUG
1796
                    if (debug_print > 0) { desc += "+" + tuo::to_string(coalition_value) + "(coalition/x" + tuo::to_string(uniq_factions) + ")"; }
1,692,917 ✔
1797
#endif
1798
                    att_dmg += coalition_value;
1,156,073 ✔
1799
                }
1800

1801
                // Skill: Rupture
1802
                unsigned rupture_value = att_status->skill(Skill::rupture);
21,320,571 ✔
1803
                if (rupture_value > 0)
21,320,571 ✔
1804
                {
1805
#ifndef NDEBUG
UNCOV
1806
                    if (debug_print > 0) { desc += "+" + tuo::to_string(rupture_value) + "(rupture)"; }
×
1807
#endif
UNCOV
1808
                    att_dmg += rupture_value;
×
1809
                }
1810

1811
                // Skill: Venom
1812
                unsigned venom_value = att_status->skill(Skill::venom);
21,320,571 ✔
1813
                if (venom_value > 0 && def_status->m_poisoned > 0)
21,320,571 ✔
1814
                {
1815
#ifndef NDEBUG
1816
                    if (debug_print > 0) { desc += "+" + tuo::to_string(venom_value) + "(venom)"; }
83,257 ✔
1817
#endif
1818
                    att_dmg += venom_value;
33,139 ✔
1819
                }
1820

1821
                // Passive BGE: Bloodlust
1822
                if (fd->bloodlust_value > 0)
21,320,571 ✔
1823
                {
1824
#ifndef NDEBUG
1825
                    if (debug_print > 0) { desc += "+" + tuo::to_string(fd->bloodlust_value) + "(bloodlust)"; }
207,244 ✔
1826
#endif
1827
                    att_dmg += fd->bloodlust_value;
51,811 ✔
1828
                }
1829

1830
                // State: Enfeebled
1831
                if (def_status->m_enfeebled > 0)
21,320,571 ✔
1832
                {
1833
#ifndef NDEBUG
1834
                    if (debug_print > 0) { desc += "+" + tuo::to_string(def_status->m_enfeebled) + "(enfeebled)"; }
3,095,787 ✔
1835
#endif
1836
                    att_dmg += def_status->m_enfeebled;
2,184,003 ✔
1837
                }
1838
            }
1839
            // prevent damage
1840
#ifndef NDEBUG
1841
            std::string reduced_desc;
22,583,332 ✔
1842
#endif
1843
            unsigned reduced_dmg(0); // damage reduced by armor, protect and in turn again canceled by pierce, etc.
22,583,332 ✔
1844
            unsigned armor_value = 0;
22,583,332 ✔
1845
            // Armor
1846
            if (def_status->m_card->m_type == CardType::assault) {
22,583,332 ✔
1847
                // Passive BGE: Fortification (adj step -> 1 (1 left, host, 1 right)
1848
                unsigned adj_size = (unsigned)(fd->bg_effects[fd->tapi][PassiveBGE::fortification]);
14,655,996 ✔
1849
                unsigned host_idx = def_status->m_index;
14,655,996 ✔
1850
                unsigned from_idx = safe_minus(host_idx, adj_size);
14,655,996 ✔
1851
                unsigned till_idx = std::min(host_idx + adj_size, safe_minus(def_assaults.size(), 1));
27,363,290 ✔
1852
                for (; from_idx <= till_idx; ++ from_idx)
29,339,718 ✔
1853
                {
1854
                    CardStatus* adj_status = &def_assaults[from_idx];
14,683,722 ✔
1855
                    if (!is_alive(adj_status)) { continue; }
14,683,722 ✔
1856
                    armor_value = std::max(armor_value, adj_status->skill(Skill::armor));
18,755,327 ✔
1857
                }
1858
            }
1859
            if (armor_value > 0)
22,583,332 ✔
1860
            {
1861
#ifndef NDEBUG
1862
                if(debug_print > 0) { reduced_desc += tuo::to_string(armor_value) + "(armor)"; }
4,407,310 ✔
1863
#endif
1864
                reduced_dmg += armor_value;
1865
            }
1866
            if (def_status->protected_value() > 0)
22,583,332 ✔
1867
            {
1868
#ifndef NDEBUG
1869
                if(debug_print > 0) { reduced_desc += (reduced_desc.empty() ? "" : "+") + tuo::to_string(def_status->protected_value()) + "(protected)"; }
11,658,814 ✔
1870
#endif
1871
                reduced_dmg += def_status->protected_value();
9,124,210 ✔
1872
            }
1873
            unsigned pierce_value = att_status->skill(Skill::pierce);
22,583,332 ✔
1874
            if (reduced_dmg > 0 && pierce_value > 0)
22,583,332 ✔
1875
            {
1876
#ifndef NDEBUG
1877
                if (debug_print > 0) { reduced_desc += "-" + tuo::to_string(pierce_value) + "(pierce)"; }
426,018 ✔
1878
#endif
1879
                reduced_dmg = safe_minus(reduced_dmg, pierce_value);
22,583,332 ✔
1880
            }
1881
            unsigned rupture_value = att_status->skill(Skill::rupture);
22,583,332 ✔
1882
            if (reduced_dmg > 0 && rupture_value > 0)
22,583,332 ✔
1883
            {
1884
#ifndef NDEBUG
UNCOV
1885
                if (debug_print > 0) { reduced_desc += "-" + tuo::to_string(rupture_value) + "(rupture)"; }
×
1886
#endif
1887
                reduced_dmg = safe_minus(reduced_dmg, rupture_value);
22,583,332 ✔
1888
            }
1889
            if(__builtin_expect(fd->fixes[Fix::corrosive_protect_armor],true)) // MK - 05/01/2023 6:58 AM: "Corrosive status reduces protection from attacks (equivalent to giving the attacker Pierce X). Note that the value is equal to initial Corrosive application, not attack removed."
22,583,332 ✔
1890
            {
1891
                unsigned corrosive_value = def_status->m_corroded_rate;
22,583,332 ✔
1892
                if (reduced_dmg > 0 && corrosive_value > 0)
22,583,332 ✔
1893
                {
1894
#ifndef NDEBUG
1895
                    if (debug_print > 0) { reduced_desc += "-" + tuo::to_string(corrosive_value) + "(corrosive)"; }
27,195 ✔
1896
#endif
1897
                    reduced_dmg = safe_minus(reduced_dmg, corrosive_value);
22,583,332 ✔
1898
                }
1899
            }
1900
            att_dmg = safe_minus(att_dmg, reduced_dmg);
22,583,332 ✔
1901
#ifndef NDEBUG
1902
            if (debug_print > 0)
22,583,332 ✔
1903
            {
1904
                if(!reduced_desc.empty()) { desc += "-[" + reduced_desc + "]"; }
4,715,999 ✔
1905
                if(!desc.empty()) { desc += "=" + tuo::to_string(att_dmg); }
5,301,001 ✔
1906
                else { assert(att_dmg == pre_modifier_dmg); }
1,659,937 ✔
1907
                _DEBUG_MSG(1, "%s attacks %s for %u%s damage\n",
5,747,250 ✔
1908
                        status_description(att_status).c_str(),
1909
                        status_description(def_status).c_str(), pre_modifier_dmg, desc.c_str());
1910
            }
1911
#endif
1912
            // Passive BGE: Brigade
1913
            if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::brigade] && legion_value, false)
22,583,332 ✔
UNCOV
1914
                    && can_be_healed(att_status))
×
1915
            {
UNCOV
1916
                _DEBUG_MSG(1, "Brigade: %s heals itself for %u\n",
×
1917
                        status_description(att_status).c_str(), legion_value);
UNCOV
1918
                att_status->add_hp(legion_value);
×
1919
            }
1920

1921
            // Passive BGE: Unity
1922
            if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::unity] && coalition_value, false)
22,583,332 ✔
1923
                    && can_be_healed(att_status))
22,586,709 ✔
1924
            {
1925
                // Unity no longer uses the legacy half-heal scaling (issue #109).
1926
                _DEBUG_MSG(1, "Unity: %s heals itself for %u\n", status_description(att_status).c_str(), coalition_value);
882 ✔
1927
                att_status->add_hp(coalition_value);
441 ✔
1928
            }
1929
        }
22,583,332 ✔
1930

1931
    template<enum CardType::CardType>
1932
        void attack_damage()
11,723,132 ✔
1933
        {
1934

1935
            remove_hp(fd, def_status, att_dmg);
11,723,132 ✔
1936
            prepend_on_death(fd);
11,723,132 ✔
1937
            resolve_skill(fd);
11,723,132 ✔
1938
        }
11,723,132 ✔
1939

1940
    template<enum CardType::CardType>
1941
        void on_attacked() {
21,238,399 ✔
1942
            //APN
1943
            // resolve On-Attacked skills
1944
            for (const auto& ss: def_status->m_card->m_skills_on_attacked)
21,437,283 ✔
1945
            {
1946
                _DEBUG_MSG(1, "On Attacked %s: Preparing (tail) skill %s\n",
397,768 ✔
1947
                        status_description(def_status).c_str(), skill_description(fd->cards, ss).c_str());
1948
                fd->skill_queue.emplace_back(def_status, ss);
198,884 ✔
1949
                resolve_skill(fd);
198,884 ✔
1950
            }
1951
        }
21,238,399 ✔
1952

1953
    template<enum CardType::CardType>
1954
        void damage_dependant_pre_oa() {}
1955

1956
    template<enum CardType::CardType>
1957
        void do_leech() {}
1958

1959
    template<enum CardType::CardType>
1960
        void perform_swipe_drain(Field* fd, CardStatus* att_status, CardStatus* def_status,unsigned att_dmg) {}
1961
    };
1962

1963

1964
    template<>
1965
void PerformAttack::attack_damage<CardType::commander>()
7,134,014 ✔
1966
{
1967
    remove_commander_hp(fd, *def_status, att_dmg);
7,134,014 ✔
UNCOV
1968
}
×
1969

1970
    template<>
1971
void PerformAttack::damage_dependant_pre_oa<CardType::assault>()
10,929,810 ✔
1972
{
1973
    if (!__builtin_expect(fd->fixes[Fix::poison_after_attacked],true))
10,929,810 ✔
1974
    {
UNCOV
1975
    unsigned poison_value = std::max(att_status->skill(Skill::poison), att_status->skill(Skill::venom));
×
1976
    
UNCOV
1977
    if (poison_value > def_status->m_poisoned && skill_check<Skill::poison>(fd, att_status, def_status))
×
1978
    {
1979
        // perform_skill_poison
1980
#ifndef NQUEST
1981
        if (att_status->m_player == 0)
1982
        {
1983
            fd->inc_counter(QuestType::skill_use, Skill::poison);
1984
        }
1985
#endif
UNCOV
1986
        _DEBUG_MSG(1, "%s poisons %s by %u\n",
×
1987
                status_description(att_status).c_str(),
UNCOV
1988
                status_description(def_status).c_str(), poison_value);
×
UNCOV
1989
        def_status->m_poisoned = poison_value;
×
1990
    }
1991
    }
1992
    else {            
1993
        unsigned venom_value = att_status->skill(Skill::venom);
10,929,810 ✔
1994
        if (venom_value > 0 && skill_check<Skill::venom>(fd, att_status, def_status)) {
10,929,810 ✔
1995
#ifndef NQUEST
1996
              if (att_status->m_player == 0)
1997
              {
1998
                 fd->inc_counter(QuestType::skill_use, Skill::venom);
1999
              }
2000
#endif
2001

2002
              _DEBUG_MSG(1, "%s venoms %s by %u\n",
252,731 ✔
2003
                  status_description(att_status).c_str(),
2004
                  status_description(def_status).c_str(), venom_value);
252,731 ✔
2005
              // new venom keeps adding stacks
2006
              def_status->m_poisoned += venom_value;
252,731 ✔
2007
        }
2008
    }
2009
}
10,929,810 ✔
2010

2011

2012
    template<>
2013
void PerformAttack::do_leech<CardType::assault>()
10,755,591 ✔
2014
{
2015
    unsigned leech_value = std::min(att_dmg, att_status->skill(Skill::leech));
10,755,591 ✔
2016
    if(leech_value > 0 && skill_check<Skill::leech>(fd, att_status, nullptr))
10,755,591 ✔
2017
    {
2018
#ifndef NQUEST
2019
        if (att_status->m_player == 0)
2020
        {
2021
            fd->inc_counter(QuestType::skill_use, Skill::leech);
2022
        }
2023
#endif
2024
        _DEBUG_MSG(1, "%s leeches %u health\n", status_description(att_status).c_str(), leech_value);
61,476 ✔
2025
        if (__builtin_expect(fd->fixes[Fix::leech_increase_max_hp],true)) {
61,476 ✔
2026
            att_status->ext_hp(leech_value);
61,476 ✔
2027
        }
2028
        else {
UNCOV
2029
            att_status->add_hp(leech_value);
×
2030
        }
2031
    }
2032
}
10,755,591 ✔
2033

2034
// General attack phase by the currently evaluated assault, taking into accounts exotic stuff such as flurry, etc.
2035
unsigned attack_commander(Field* fd, CardStatus* att_status)
7,928,598 ✔
2036
{
2037
    CardStatus* def_status{select_first_enemy_wall(fd)}; // defending wall
7,928,598 ✔
2038
    if (def_status != nullptr)
7,928,598 ✔
2039
    {
2040
        return PerformAttack{fd, att_status, def_status}.op<CardType::structure>();
793,322 ✔
2041
    }
2042
    else
2043
    {
2044
        return PerformAttack{fd, att_status, &fd->tip->commander}.op<CardType::commander>();
7,135,276 ✔
2045
    }
2046
}
2047
// Return true if actually attacks
2048
bool attack_phase(Field* fd)
25,292,218 ✔
2049
{
2050
    CardStatus* att_status(&fd->tap->assaults[fd->current_ci]); // attacking card
25,292,218 ✔
2051
    Storage<CardStatus>& def_assaults(fd->tip->assaults);
25,292,218 ✔
2052

2053
    if (!att_status->attack_power())
25,292,218 ✔
2054
    {
2055
        _DEBUG_MSG(1, "%s cannot take attack (zeroed)\n", status_description(att_status).c_str());
2,653,750 ✔
2056
        return false;
2,653,750 ✔
2057
    }
2058

2059
    unsigned att_dmg = 0;
22,638,468 ✔
2060
    if (alive_assault(def_assaults, fd->current_ci))
22,638,468 ✔
2061
    {
2062
        CardStatus* def_status = &def_assaults[fd->current_ci];
14,709,870 ✔
2063
        att_dmg = PerformAttack{fd, att_status, def_status}.op<CardType::assault>();
14,709,870 ✔
2064
        
2065
    }
2066
    else
2067
    {
2068
        // might be blocked by walls
2069
        att_dmg = attack_commander(fd, att_status);
7,928,598 ✔
2070
    }
2071

2072
    // Passive BGE: Bloodlust
2073
    if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::bloodlust], false)
22,638,468 ✔
2074
            && !fd->assault_bloodlusted && (att_dmg > 0))
22,638,468 ✔
2075
    {
2076
        fd->bloodlust_value += fd->bg_effects[fd->tapi][PassiveBGE::bloodlust];
113,418 ✔
2077
        fd->assault_bloodlusted = true;
113,418 ✔
2078
    }
2079

2080
    return true;
2081
}
2082

2083
//---------------------- $65 active skills implementation ----------------------
2084
template<
2085
bool C
2086
, typename T1
2087
, typename T2
2088
>
2089
struct if_
2090
{
2091
    typedef T1 type;
2092
};
2093

2094
template<
2095
typename T1
2096
, typename T2
2097
>
2098
struct if_<false,T1,T2>
2099
{
2100
    typedef T2 type;
2101
};
2102

2103
    template<unsigned skill_id>
2104
inline bool skill_predicate(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
285 ✔
2105
{ return skill_check<static_cast<Skill::Skill>(skill_id)>(fd, dst, src); }
377,491,651 ✔
2106

2107
    template<>
2108
inline bool skill_predicate<Skill::enhance>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
30,405,647 ✔
2109
{
2110
    if (!is_alive(dst)) return false;
30,405,647 ✔
2111
    if (!dst->has_skill(s.s)) return false;
60,811,294 ✔
2112
    if (is_active(dst)) return true;
6,467,287 ✔
2113
    if (is_defensive_skill(s.s)) return true;
3,413,670 ✔
2114
    if (is_instant_debuff_skill(s.s)) return true; // Enhance Sabotage, Inhibit, Disease also without dst being active
506,717 ✔
2115
    if (is_triggered_skill(s.s) && s.s != Skill::valor) return true;// Enhance Allegiance, Stasis, Bravery ( + not in TU: Flurry, Summon; No enhance on inactive dst: Valor)
506,617 ✔
2116

2117
    /* Strange Transmission [Gilians]: strange gillian's behavior implementation:
2118
     * The Gillian commander and assaults can enhance any skills on any assaults
2119
     * regardless of jammed/delayed states. But what kind of behavior is in the case
2120
     * when gilians are played among standard assaults, I don't know. :)
2121
     */
2122
    return is_alive_gilian(src);
2123
}
2124

2125
    template<>
2126
inline bool skill_predicate<Skill::evolve>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
3,128,813 ✔
2127
{
2128
    if (!is_alive(dst)) return false;
3,128,813 ✔
2129
    if (!dst->has_skill(s.s)) return false;
6,251,790 ✔
2130
    if (dst->has_skill(s.s2)) return false;
1,897,964 ✔
2131
    if (is_active(dst)) return true;
948,982 ✔
2132
    if (is_defensive_skill(s.s2)) return true;
555,674 ✔
2133

2134
    /* Strange Transmission [Gilians]: strange gillian's behavior implementation:
2135
     * The Gillian commander and assaults can enhance any skills on any assaults
2136
     * regardless of jammed/delayed states. But what kind of behavior is in the case
2137
     * when gilians are played among standard assaults, I don't know. :)
2138
     */
2139
    return is_alive_gilian(src);
2140
}
2141

2142
    template<>
2143
inline bool skill_predicate<Skill::mimic>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
600,905 ✔
2144
{
2145
    // skip dead units
2146
    if (!is_alive(dst)) return false;
600,905 ✔
2147

2148
    //include on activate/attacked/death
2149
    //for(const auto & a  : {dst->m_card->m_skills,dst->m_card->m_skills_on_play,dst->m_card->m_skills_on_death,dst->m_card->m_skills_on_attacked})
2150
    //
2151
    std::vector<std::vector<SkillSpec>> all;
555,337 ✔
2152
    all.emplace_back(dst->m_card->m_skills);
555,337 ✔
2153
    all.emplace_back(dst->m_card->m_skills_on_attacked);
555,337 ✔
2154

2155
    for(std::vector<SkillSpec> & a  : all)
890,180 ✔
2156
    {
2157
    // scan all enemy skills until first activation
2158
    for (const SkillSpec & ss: a)
1,707,691 ✔
2159
    {
2160
        // get skill
2161
        Skill::Skill skill_id = static_cast<Skill::Skill>(ss.id);
1,372,848 ✔
2162

2163
        // skip non-activation skills and Mimic (Mimic can't be mimicked)
2164
        if (!is_activation_skill(skill_id) || (skill_id == Skill::mimic))
1,770,633 ✔
2165
        { continue; }
975,063 ✔
2166

2167
        // skip mend for non-assault mimickers
2168
        if ((skill_id == Skill::mend || skill_id == Skill::fortify) && (src->m_card->m_type != CardType::assault))
397,785 ✔
2169
        { continue; }
2,800 ✔
2170

2171
        // enemy has at least one activation skill that can be mimicked, so enemy is eligible target for Mimic
2172
        return true;
555,337 ✔
2173
    }
2174
    }
2175

2176
    // found nothing (enemy has no skills to be mimicked, so enemy isn't eligible target for Mimic)
2177
    return false;
2178
}
555,337 ✔
2179

2180
    template<>
2181
inline bool skill_predicate<Skill::overload>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
619,534 ✔
2182
{
2183
    // basic skill check
2184
    if (!skill_check<Skill::overload>(fd, dst, src))
986,293 ✔
2185
    { return false; }
2186

2187
    // check skills
2188
    bool inhibited_searched = false;
317,390 ✔
2189
    for (const auto& ss: dst->m_card->m_skills)
878,351 ✔
2190
    {
2191
        // skip cooldown skills
2192
        if (dst->m_skill_cd[ss.id] > 0)
811,462 ✔
2193
        { continue; }
8,335 ✔
2194

2195
        // get evolved skill
2196
        Skill::Skill evolved_skill_id = static_cast<Skill::Skill>(ss.id + dst->m_evolved_skill_offset[ss.id]);
803,127 ✔
2197

2198
        // unit with an activation hostile skill is always valid target for OL
2199
        if (is_activation_hostile_skill(evolved_skill_id))
1,052,944 ✔
2200
        { return true; }
2201

2202
        // unit with an activation helpful skill is valid target only when there are inhibited units
2203
        // TODO check mend/fortify valid overload target?!?
2204
        if ((evolved_skill_id != Skill::mend && evolved_skill_id != Skill::fortify)
555,646 ✔
2205
                && is_activation_helpful_skill(evolved_skill_id)
560,961 ✔
2206
                && __builtin_expect(!inhibited_searched, true))
727,043 ✔
2207
        {
2208
            for (const auto & c: fd->players[dst->m_player]->assaults.m_indirect)
901,858 ✔
2209
            {
2210
                if (is_alive(c) && c->m_inhibited)
735,659 ✔
2211
                { return true; }
250,501 ✔
2212
            }
2213
            inhibited_searched = true;
2214
        }
2215
    }
2216
    return false;
2217
}
2218

2219
    template<>
2220
inline bool skill_predicate<Skill::rally>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
27,549,853 ✔
2221
{
2222
    return skill_check<Skill::rally>(fd, dst, src) // basic skill check
27,549,853 ✔
2223
        && (__builtin_expect((fd->tapi == dst->m_player), true) // is target on the active side?
27,549,853 ✔
2224
                ? is_active(dst) && !has_attacked(dst) // normal case
15,470,789 ✔
2225
                : is_active_next_turn(dst) // diverted case / on-death activation
27,549,853 ✔
2226
           )
27,549,853 ✔
2227
        ;
2228
}
2229

2230
    template<>
2231
inline bool skill_predicate<Skill::enrage>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
32,395,433 ✔
2232
{
2233
    return (__builtin_expect((fd->tapi == dst->m_player), true) // is target on the active side?
32,395,433 ✔
2234
            ? is_active(dst) && (dst->m_step == CardStep::none) // normal case
14,865,518 ✔
2235
            : is_active_next_turn(dst) // on-death activation
46,718,434 ✔
2236
           )
2237
        && (dst->attack_power()) // card can perform direct attack
46,718,434 ✔
2238
        ;
2239
}
2240

2241
    template<>
UNCOV
2242
inline bool skill_predicate<Skill::rush>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
×
2243
{
UNCOV
2244
    return (!src->m_rush_attempted)
×
UNCOV
2245
        && (dst->m_delay >= ((src->m_card->m_type == CardType::assault) && (dst->m_index < src->m_index) ? 2u : 1u));
×
2246
}
2247

2248
    template<>
2249
inline bool skill_predicate<Skill::weaken>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
38,847,543 ✔
2250
{
2251
    if(__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::ironwill], false) && dst->has_skill(Skill::armor))return false;
38,847,543 ✔
2252
    if (!dst->attack_power()) { return false; }
38,844,121 ✔
2253

2254
    // active player performs Weaken (normal case)
2255
    if (__builtin_expect((fd->tapi == src->m_player), true))
34,614,431 ✔
2256
    { return is_active_next_turn(dst); }
34,498,683 ✔
2257

2258
    // APN - On-Attacked/Death don't target the attacking card
2259

2260
    // inactive player performs Weaken (inverted case (on-death activation))
2261
    return will_activate_this_turn(dst);
115,748 ✔
2262
}
2263

2264
    template<>
2265
inline bool skill_predicate<Skill::sunder>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
19,198,073 ✔
2266
{
2267
    return skill_predicate<Skill::weaken>(fd, src, dst, s);
92 ✔
2268
}
2269

2270
    template<unsigned skill_id>
2271
inline void perform_skill(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
2272
{ assert(false); }
2273

2274
    template<>
2275
inline void perform_skill<Skill::enfeeble>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
7,939,599 ✔
2276
{
2277
    dst->m_enfeebled += s.x;
7,939,599 ✔
UNCOV
2278
}
×
2279

2280
    template<>
2281
inline void perform_skill<Skill::enhance>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
5,611,412 ✔
2282
{
2283
    dst->m_enhanced_value[s.s + dst->m_primary_skill_offset[s.s]] += s.x;
5,611,412 ✔
2284
    dst->m_skills_modified = true;
5,611,412 ✔
2285
}
31 ✔
2286

2287
    template<>
2288
inline void perform_skill<Skill::evolve>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
380,810 ✔
2289
{
2290
    auto primary_s1 = dst->m_primary_skill_offset[s.s] + s.s;
380,810 ✔
2291
    auto primary_s2 = dst->m_primary_skill_offset[s.s2] + s.s2;
380,810 ✔
2292
    dst->m_primary_skill_offset[s.s] = primary_s2 - s.s;
380,810 ✔
2293
    dst->m_primary_skill_offset[s.s2] = primary_s1 - s.s2;
380,810 ✔
2294
    dst->m_evolved_skill_offset[primary_s1] = s.s2 - primary_s1;
380,810 ✔
2295
    dst->m_evolved_skill_offset[primary_s2] = s.s - primary_s2;
380,810 ✔
2296
    dst->m_skills_modified = true;
380,810 ✔
UNCOV
2297
}
×
2298

2299
    template<>
2300
inline void perform_skill<Skill::heal>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
8,453,176 ✔
2301
{
2302
    dst->add_hp(s.x);
8,453,176 ✔
2303

2304
    // Passive BGE: ZealotsPreservation
2305
    if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::zealotspreservation], false)
8,453,176 ✔
2306
            && (src->m_card->m_type == CardType::assault))
8,453,176 ✔
2307
    {
2308
        unsigned bge_value = (s.x + 1) / 2;
383 ✔
2309
        _DEBUG_MSG(1, "Zealot's Preservation: %s Protect %u on %s\n",
383 ✔
2310
                status_description(src).c_str(), bge_value,
2311
                status_description(dst).c_str());
383 ✔
2312
        dst->m_protected += bge_value;
383 ✔
2313
    }
2314
}
8,453,176 ✔
2315

2316
    template<>
2317
inline void perform_skill<Skill::jam>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
1,761,794 ✔
2318
{
2319
    dst->m_jammed = true;
1,761,794 ✔
UNCOV
2320
}
×
2321

2322
    template<>
2323
inline void perform_skill<Skill::mend>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
4,065 ✔
2324
{
2325
    dst->add_hp(s.x);
4,063 ✔
2326
}
2 ✔
2327

2328
    template<>
2329
inline void perform_skill<Skill::fortify>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
2,417,615 ✔
2330
{
2331
    dst->ext_hp(s.x);
1,761,277 ✔
2332
}
656,338 ✔
2333

2334
    template<>
2335
inline void perform_skill<Skill::siege>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
2,653,087 ✔
2336
{ 
2337
    //only structures can be sieged
2338
    _DEBUG_ASSERT(dst->m_card->m_type != CardType::assault);
2,653,087 ✔
2339
    _DEBUG_ASSERT(dst->m_card->m_type != CardType::commander);
2,653,087 ✔
2340
    unsigned siege_dmg = remove_absorption(fd,dst,s.x);
2,653,087 ✔
2341
    // structure should not have protect normally..., but let's allow it for barrier support
2342
    siege_dmg = safe_minus(siege_dmg, src->m_overloaded ? 0 : dst->m_protected);
2,653,087 ✔
2343
    remove_hp(fd, dst, siege_dmg);
2,653,087 ✔
2344
}
2,653,087 ✔
2345

2346
    template<>
2347
inline void perform_skill<Skill::mortar>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
3,441,962 ✔
2348
{
2349
    if (dst->m_card->m_type == CardType::structure)
3,441,962 ✔
2350
    {
2351
        perform_skill<Skill::siege>(fd, src, dst, s);
2,471,490 ✔
2352
    }
2353
    else
2354
    {
2355
        unsigned strike_dmg = remove_absorption(fd,dst,(s.x + 1) / 2 + dst->m_enfeebled);
970,472 ✔
2356
        strike_dmg = safe_minus(strike_dmg, src->m_overloaded ? 0 : dst->protected_value());
970,472 ✔
2357
        remove_hp(fd, dst, strike_dmg);
970,472 ✔
2358
    }
2359
}
3,441,962 ✔
2360

2361
    template<>
2362
inline void perform_skill<Skill::overload>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
224,791 ✔
2363
{
2364
    dst->m_overloaded = true;
224,791 ✔
2365
}
9 ✔
2366

2367
    template<>
2368
inline void perform_skill<Skill::protect>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
34,625,216 ✔
2369
{
2370
    dst->m_protected += s.x;
34,625,216 ✔
2371
}
435 ✔
2372

2373
    template<>
2374
inline void perform_skill<Skill::rally>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
11,972,676 ✔
2375
{
2376
    dst->m_temp_attack_buff += s.x;
11,972,676 ✔
2377
}
226,457 ✔
2378

2379
    template<>
2380
inline void perform_skill<Skill::enrage>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
9,951,301 ✔
2381
{
2382
    dst->m_enraged += s.x;
9,951,301 ✔
2383
    // Passive BGE: Furiosity
2384
    if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::furiosity], false)
9,951,301 ✔
2385
            && can_be_healed(dst))
9,951,301 ✔
2386
    {
2387
        unsigned bge_value = s.x;
6,835 ✔
2388
        _DEBUG_MSG(1, "Furiosity: %s Heals %s for %u\n",
6,835 ✔
2389
                status_description(src).c_str(),
2390
                status_description(dst).c_str(), bge_value);
6,835 ✔
2391
        dst->add_hp(bge_value);
6,835 ✔
2392
    }
2393
}
9,951,301 ✔
2394

2395
    template<>
2396
inline void perform_skill<Skill::entrap>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
6,017,216 ✔
2397
{
2398
    dst->m_entrapped += s.x;
6,017,216 ✔
2399
}
87,266 ✔
2400

2401
    template<>
2402
inline void perform_skill<Skill::rush>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
×
2403
{
2404
    dst->m_delay -= std::min(std::max(s.x, 1u), dst->m_delay);
×
UNCOV
2405
    if (dst->m_delay == 0)
×
2406
    {
UNCOV
2407
        check_and_perform_valor(fd, dst);
×
UNCOV
2408
        if(dst->m_card->m_skill_trigger[Skill::summon] == Skill::Trigger::activate)check_and_perform_summon(fd, dst);
×
2409
    }
UNCOV
2410
}
×
2411

2412
    template<>
2413
inline void perform_skill<Skill::strike>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
18,455,126 ✔
2414
{
2415
    unsigned strike_dmg = remove_absorption(fd,dst,s.x+ dst->m_enfeebled);
18,455,126 ✔
2416
    strike_dmg = safe_minus(strike_dmg , src->m_overloaded ? 0 : dst->protected_value());
18,455,126 ✔
2417
    remove_hp(fd, dst, strike_dmg);
18,455,126 ✔
2418
}
18,455,126 ✔
2419

2420
    template<>
2421
inline void perform_skill<Skill::weaken>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
9,998,182 ✔
2422
{
2423
    dst->m_temp_attack_buff -= (unsigned)std::min(s.x, dst->attack_power());
9,998,182 ✔
UNCOV
2424
}
×
2425

2426
    template<>
2427
inline void perform_skill<Skill::sunder>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
3,447,317 ✔
2428
{
2429
    dst->m_sundered = true;
3,447,317 ✔
2430
    perform_skill<Skill::weaken>(fd, src, dst, s);
3,391,115 ✔
UNCOV
2431
}
×
2432

2433
    template<>
2434
inline void perform_skill<Skill::mimic>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
255,934 ✔
2435
{
2436
    // collect all mimickable enemy skills
2437
    std::vector<const SkillSpec *> mimickable_skills;
255,934 ✔
2438
    mimickable_skills.reserve(dst->m_card->m_skills.size()+dst->m_card->m_skills_on_play.size()+dst->m_card->m_skills_on_death.size()+dst->m_card->m_skills_on_attacked.size());
255,934 ✔
2439
    _DEBUG_MSG(2, " * Mimickable skills of %s\n", status_description(dst).c_str());
255,934 ✔
2440
    //include on activate/attacked
2441
    for(const std::vector<SkillSpec> * a : {&dst->m_card->m_skills, &dst->m_card->m_skills_on_attacked})
767,802 ✔
2442
    {
2443
    for (const SkillSpec & ss: *a)
1,278,495 ✔
2444
    {
2445
        // get skill
2446
        Skill::Skill skill_id = static_cast<Skill::Skill>(ss.id);
766,627 ✔
2447

2448
        // skip non-activation skills and Mimic (Mimic can't be mimicked)
2449
        if (!is_activation_skill(skill_id) || (skill_id == Skill::mimic))
1,104,675 ✔
2450
        { continue; }
428,579 ✔
2451

2452
        // skip mend for non-assault mimickers
2453
        if ((skill_id == Skill::mend || skill_id == Skill::fortify) && (src->m_card->m_type != CardType::assault))
338,048 ✔
2454
        { continue; }
1,200 ✔
2455

2456
        mimickable_skills.emplace_back(&ss);
336,848 ✔
2457
        _DEBUG_MSG(2, "  + %s\n", skill_description(fd->cards, ss).c_str());
766,627 ✔
2458
    }
2459
    }
2460

2461
    // select skill
2462
    unsigned mim_idx = 0;
255,934 ✔
2463
    switch (mimickable_skills.size())
255,934 ✔
2464
    {
UNCOV
2465
        case 0: assert(false); break;
×
2466
        case 1: break;
2467
        default: mim_idx = (fd->re() % mimickable_skills.size()); break;
79,072 ✔
2468
    }
2469
    // prepare & perform selected skill
2470
    const SkillSpec & mim_ss = *mimickable_skills[mim_idx];
255,934 ✔
2471
    Skill::Skill mim_skill_id = static_cast<Skill::Skill>(mim_ss.id);
255,934 ✔
2472
    auto skill_value = s.x + src->enhanced(mim_skill_id); //enhanced skill from mimic ?!?
255,934 ✔
2473
    SkillSpec mimicked_ss{mim_skill_id, skill_value, allfactions, mim_ss.n, 0, mim_ss.s, mim_ss.s2, mim_ss.all, mim_ss.card_id,};
255,934 ✔
2474
    _DEBUG_MSG(1, " * Mimicked skill: %s\n", skill_description(fd->cards, mimicked_ss).c_str());
255,934 ✔
2475
    skill_table[mim_skill_id](fd, src, mimicked_ss);
255,934 ✔
2476
}
255,934 ✔
2477

2478
    template<unsigned skill_id>
2479
inline unsigned select_fast(Field* fd, CardStatus* src, const std::vector<CardStatus*>& cards, const SkillSpec& s)
124,050,748 ✔
2480
{
2481
    if ((s.y == allfactions)
124,050,748 ✔
2482
            || fd->bg_effects[fd->tapi][PassiveBGE::metamorphosis]
21,499,681 ✔
2483
            || fd->bg_effects[fd->tapi][PassiveBGE::megamorphosis])
145,394,925 ✔
2484
    {
2485
        auto pred = [fd, src, s](CardStatus* c) {
393,556,614 ✔
2486
            return(skill_predicate<skill_id>(fd, src, c, s));
341,840,073 ✔
2487
        };
2488
        return fd->make_selection_array(cards.begin(), cards.end(), pred);
102,861,376 ✔
2489
    }
2490
    else
2491
    {
2492
        auto pred = [fd, src, s](CardStatus* c) {
111,756,884 ✔
2493
            return ((c->m_card->m_faction == s.y || c->m_card->m_faction == progenitor) && skill_predicate<skill_id>(fd, src, c, s));
105,532,518 ✔
2494
        };
2495
        return fd->make_selection_array(cards.begin(), cards.end(), pred);
21,189,372 ✔
2496
    }
2497
}
2498

2499
    template<>
2500
inline unsigned select_fast<Skill::mend>(Field* fd, CardStatus* src, const std::vector<CardStatus*>& cards, const SkillSpec& s)
108,754 ✔
2501
{
2502
    fd->selection_array.clear();
108,754 ✔
2503
    bool critical_reach = fd->bg_effects[fd->tapi][PassiveBGE::criticalreach];
108,754 ✔
2504
    auto& assaults = fd->players[src->m_player]->assaults;
108,754 ✔
2505
    unsigned adj_size = 1 + (unsigned)(critical_reach);
108,754 ✔
2506
    unsigned host_idx = src->m_index;
108,754 ✔
2507
    unsigned from_idx = safe_minus(host_idx, adj_size);
108,754 ✔
2508
    unsigned till_idx = std::min(host_idx + adj_size, safe_minus(assaults.size(), 1));
212,066 ✔
2509
    for (; from_idx <= till_idx; ++ from_idx)
372,450 ✔
2510
    {
2511
        if (from_idx == host_idx) { continue; }
265,339 ✔
2512
        CardStatus* adj_status = &assaults[from_idx];
154,942 ✔
2513
        if (!is_alive(adj_status)) { continue; }
154,942 ✔
2514
        if (skill_predicate<Skill::mend>(fd, src, adj_status, s))
306,598 ✔
2515
        {
2516
            fd->selection_array.push_back(adj_status);
4,341 ✔
2517
        }
2518
    }
2519
    return fd->selection_array.size();
108,754 ✔
2520
}
2521

2522
    template<>
2523
inline unsigned select_fast<Skill::fortify>(Field* fd, CardStatus* src, const std::vector<CardStatus*>& cards, const SkillSpec& s)
1,197,692 ✔
2524
{
2525
    fd->selection_array.clear();
1,197,692 ✔
2526
    bool critical_reach = fd->bg_effects[fd->tapi][PassiveBGE::criticalreach];
1,197,692 ✔
2527
    auto& assaults = fd->players[src->m_player]->assaults;
1,197,692 ✔
2528
    unsigned adj_size = 1 + (unsigned)(critical_reach);
1,197,692 ✔
2529
    unsigned host_idx = src->m_index;
1,197,692 ✔
2530
    unsigned from_idx = safe_minus(host_idx, adj_size);
1,197,692 ✔
2531
    unsigned till_idx = std::min(host_idx + adj_size, safe_minus(assaults.size(), 1));
2,352,633 ✔
2532
    for (; from_idx <= till_idx; ++ from_idx)
4,241,891 ✔
2533
    {
2534
        if (from_idx == host_idx) { continue; }
3,059,099 ✔
2535
        CardStatus* adj_status = &assaults[from_idx];
1,846,507 ✔
2536
        if (!is_alive(adj_status)) { continue; }
1,846,507 ✔
2537
        if (skill_predicate<Skill::fortify>(fd, src, adj_status, s))
1,831,607 ✔
2538
        {
2539
            fd->selection_array.push_back(adj_status);
1,831,607 ✔
2540
        }
2541
    }
2542
    return fd->selection_array.size();
1,197,692 ✔
2543
}
2544
inline std::vector<CardStatus*>& skill_targets_hostile_assault(Field* fd, CardStatus* src)
42,327,795 ✔
2545
{
2546
    return(fd->players[opponent(src->m_player)]->assaults.m_indirect);
42,327,795 ✔
2547
}
2548

2549
inline std::vector<CardStatus*>& skill_targets_allied_assault(Field* fd, CardStatus* src)
79,543,849 ✔
2550
{
2551
    return(fd->players[src->m_player]->assaults.m_indirect);
79,543,849 ✔
2552
}
2553

2554
inline std::vector<CardStatus*>& skill_targets_hostile_structure(Field* fd, CardStatus* src)
3,485,550 ✔
2555
{
2556
    return(fd->players[opponent(src->m_player)]->structures.m_indirect);
3,485,550 ✔
2557
}
2558

2559
inline std::vector<CardStatus*>& skill_targets_allied_structure(Field* fd, CardStatus* src)
2560
{
2561
    return(fd->players[src->m_player]->structures.m_indirect);
2562
}
2563

2564
    template<unsigned skill>
2565
std::vector<CardStatus*>& skill_targets(Field* fd, CardStatus* src)
2566
{
2567
    std::cerr << "skill_targets: Error: no specialization for " << skill_names[skill] << "\n";
2568
    throw;
2569
}
2570

2571
template<> std::vector<CardStatus*>& skill_targets<Skill::enfeeble>(Field* fd, CardStatus* src)
10,264,330 ✔
2572
{ return(skill_targets_hostile_assault(fd, src)); }
10,264,330 ✔
2573

2574
template<> std::vector<CardStatus*>& skill_targets<Skill::enhance>(Field* fd, CardStatus* src)
14,079,318 ✔
2575
{ return(skill_targets_allied_assault(fd, src)); }
14,079,318 ✔
2576

2577
template<> std::vector<CardStatus*>& skill_targets<Skill::evolve>(Field* fd, CardStatus* src)
1,436,193 ✔
2578
{ return(skill_targets_allied_assault(fd, src)); }
1,436,193 ✔
2579

2580
template<> std::vector<CardStatus*>& skill_targets<Skill::heal>(Field* fd, CardStatus* src)
24,019,306 ✔
2581
{ return(skill_targets_allied_assault(fd, src)); }
24,019,306 ✔
2582

2583
template<> std::vector<CardStatus*>& skill_targets<Skill::jam>(Field* fd, CardStatus* src)
3,336,805 ✔
2584
{ return(skill_targets_hostile_assault(fd, src)); }
3,336,805 ✔
2585

2586
template<> std::vector<CardStatus*>& skill_targets<Skill::mend>(Field* fd, CardStatus* src)
108,754 ✔
2587
{ return(skill_targets_allied_assault(fd, src)); }
108,754 ✔
2588

2589
template<> std::vector<CardStatus*>& skill_targets<Skill::fortify>(Field* fd, CardStatus* src)
1,197,692 ✔
2590
{ return(skill_targets_allied_assault(fd, src)); }
1,197,692 ✔
2591

2592
template<> std::vector<CardStatus*>& skill_targets<Skill::overload>(Field* fd, CardStatus* src)
150,054 ✔
2593
{ return(skill_targets_allied_assault(fd, src)); }
150,054 ✔
2594

2595
template<> std::vector<CardStatus*>& skill_targets<Skill::protect>(Field* fd, CardStatus* src)
12,093,094 ✔
2596
{ return(skill_targets_allied_assault(fd, src)); }
12,093,094 ✔
2597

2598
template<> std::vector<CardStatus*>& skill_targets<Skill::rally>(Field* fd, CardStatus* src)
7,851,332 ✔
2599
{ return(skill_targets_allied_assault(fd, src)); }
7,851,332 ✔
2600

2601
template<> std::vector<CardStatus*>& skill_targets<Skill::enrage>(Field* fd, CardStatus* src)
13,190,723 ✔
2602
{ return(skill_targets_allied_assault(fd, src)); }
13,190,723 ✔
2603

2604
template<> std::vector<CardStatus*>& skill_targets<Skill::entrap>(Field* fd, CardStatus* src)
5,417,383 ✔
2605
{ return(skill_targets_allied_assault(fd, src)); }
5,417,383 ✔
2606

UNCOV
2607
template<> std::vector<CardStatus*>& skill_targets<Skill::rush>(Field* fd, CardStatus* src)
×
UNCOV
2608
{ return(skill_targets_allied_assault(fd, src)); }
×
2609

2610
template<> std::vector<CardStatus*>& skill_targets<Skill::siege>(Field* fd, CardStatus* src)
3,485,550 ✔
2611
{ return(skill_targets_hostile_structure(fd, src)); }
3,485,550 ✔
2612

2613
template<> std::vector<CardStatus*>& skill_targets<Skill::strike>(Field* fd, CardStatus* src)
14,439,206 ✔
2614
{ return(skill_targets_hostile_assault(fd, src)); }
926,789 ✔
2615

2616
template<> std::vector<CardStatus*>& skill_targets<Skill::sunder>(Field* fd, CardStatus* src)
6,017,482 ✔
2617
{ return(skill_targets_hostile_assault(fd, src)); }
6,017,482 ✔
2618

2619
template<> std::vector<CardStatus*>& skill_targets<Skill::weaken>(Field* fd, CardStatus* src)
7,875,365 ✔
2620
{ return(skill_targets_hostile_assault(fd, src)); }
7,875,365 ✔
2621

2622
template<> std::vector<CardStatus*>& skill_targets<Skill::mimic>(Field* fd, CardStatus* src)
394,607 ✔
2623
{ return(skill_targets_hostile_assault(fd, src)); }
394,607 ✔
2624

2625
    template<Skill::Skill skill_id>
2626
inline bool check_and_perform_skill(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s, bool is_evadable
134,669,497 ✔
2627
#ifndef NQUEST
2628
        , bool & has_counted_quest
2629
#endif
2630
        )
2631
{
2632
    if (__builtin_expect(skill_check<skill_id>(fd, dst, src), true))
134,669,497 ✔
2633
    {
2634
#ifndef NQUEST
2635
        if (src->m_player == 0 && ! has_counted_quest)
2636
        {
2637
            fd->inc_counter(QuestType::skill_use, skill_id, dst->m_card->m_id);
2638
            has_counted_quest = true;
2639
        }
2640
#endif
2641
        if (is_evadable && (dst->m_evaded < dst->skill(Skill::evade)))
134,669,497 ✔
2642
        {
2643
            ++ dst->m_evaded;
16,361,800 ✔
2644
            _DEBUG_MSG(1, "%s %s on %s but it evades\n",
18,328,656 ✔
2645
                    status_description(src).c_str(), skill_short_description(fd->cards, s).c_str(),
2646
                    status_description(dst).c_str());
2647
            return(false);
16,361,800 ✔
2648
        }
2649
        _DEBUG_MSG(1, "%s %s on %s\n",
144,198,781 ✔
2650
                status_description(src).c_str(), skill_short_description(fd->cards, s).c_str(),
2651
                status_description(dst).c_str());
2652
        perform_skill<skill_id>(fd, src, dst, s);
118,307,697 ✔
2653
        if (s.c > 0)
118,307,697 ✔
2654
        {
2655
            src->m_skill_cd[skill_id] = s.c;
1,405,377 ✔
2656
        }
2657
        // Skill: Tribute
2658
        if (skill_check<Skill::tribute>(fd, dst, src)
139,222,311 ✔
2659
                // only activation helpful skills can be tributed (* except Evolve, Enhance, and Rush)
2660
                && is_activation_helpful_skill(s.id) && (s.id != Skill::evolve) && (s.id != Skill::enhance) && (s.id != Skill::rush)
20,914,614 ✔
2661
                && (dst->m_tributed < dst->skill(Skill::tribute))
8,273,915 ✔
2662
                && skill_check<skill_id>(fd, src, src))
106,187,324 ✔
2663
        {
2664
            ++ dst->m_tributed;
967,355 ✔
2665
            _DEBUG_MSG(1, "%s tributes %s back to %s\n",
967,355 ✔
2666
                    status_description(dst).c_str(), skill_short_description(fd->cards, s).c_str(),
2667
                    status_description(src).c_str());
2668
            perform_skill<skill_id>(fd, src, src, s);
967,355 ✔
2669
        }
2670
        return(true);
118,307,697 ✔
2671
    }
UNCOV
2672
    _DEBUG_MSG(1, "(CANCELLED) %s %s on %s\n",
×
2673
            status_description(src).c_str(), skill_short_description(fd->cards, s).c_str(),
2674
            status_description(dst).c_str());
2675
    return(false);
2676
}
2677
template<enum CardType::CardType def_cardtype>
2678
void perform_bge_devour(Field* fd, CardStatus* att_status, CardStatus* def_status)
16,830,722 ✔
2679
{
2680
// Passive BGE: Devour
2681
                unsigned leech_value;
2682
                if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::devour], false)
10,755,591 ✔
2683
                        && ((leech_value = att_status->skill(Skill::leech) + att_status->skill(Skill::refresh)) > 0)
47,397 ✔
2684
                        && (def_cardtype == CardType::assault))
10,755,591 ✔
2685
                {
2686
                    unsigned bge_denominator = (fd->bg_effects[fd->tapi][PassiveBGE::devour] > 0)
1,890 ✔
2687
                        ? (unsigned)fd->bg_effects[fd->tapi][PassiveBGE::devour]
945 ✔
2688
                        : 4;
2689
                    unsigned bge_value = (leech_value - 1) / bge_denominator + 1;
945 ✔
2690
                    if (! att_status->m_sundered)
945 ✔
2691
                    {
2692
                        _DEBUG_MSG(1, "Devour: %s gains %u attack\n",
2,778 ✔
2693
                                status_description(att_status).c_str(), bge_value);
2694
                        att_status->m_perm_attack_buff += bge_value;
926 ✔
2695
                    }
2696
                    _DEBUG_MSG(1, "Devour: %s extends max hp / heals itself for %u\n",
2,835 ✔
2697
                            status_description(att_status).c_str(), bge_value);
2698
                    att_status->ext_hp(bge_value);
945 ✔
2699
                }
2700
}
10,755,591 ✔
2701
template<enum CardType::CardType def_cardtype>
2702
void perform_bge_heroism(Field* fd, CardStatus* att_status, CardStatus* def_status)
16,830,722 ✔
2703
{
2704
// Passive BGE: Heroism
2705
                unsigned valor_value;
2706
                if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::heroism], false)
16,830,722 ✔
2707
                        && ((valor_value = att_status->skill(Skill::valor) + att_status->skill(Skill::bravery)) > 0)
23,310 ✔
2708
                        && !att_status->m_sundered
2,484 ✔
2709
                        && (def_cardtype == CardType::assault) && (def_status->m_hp <= 0))
10,758,047 ✔
2710
                {
2711
                    _DEBUG_MSG(1, "Heroism: %s gain %u attack\n",
2,124 ✔
2712
                            status_description(att_status).c_str(), valor_value);
2713
                    att_status->m_perm_attack_buff += valor_value;
708 ✔
2714
                }
2715
                }
10,755,591 ✔
2716
/**
2717
 * @brief Perform Mark skill, increases Mark-counter.
2718
 * 
2719
 * @param fd     Field
2720
 * @param att_status Attacker
2721
 * @param def_status Defender
2722
 */
2723
void perform_mark(Field* fd, CardStatus* att_status, CardStatus* def_status)
16,830,722 ✔
2724
{
2725
    // Bug fix? 2023-04-03 mark should come after berserk
2726
    // Increase Mark-counter
2727
    unsigned mark_base = att_status->skill(Skill::mark);
16,830,722 ✔
2728
    if(mark_base && skill_check<Skill::mark>(fd,att_status,def_status)) {
16,830,722 ✔
2729
        _DEBUG_MSG(1, "%s marks %s for %u\n",
35,428 ✔
2730
                status_description(att_status).c_str(), status_description(def_status).c_str(), mark_base);
35,428 ✔
2731
        def_status->m_marked += mark_base;
35,428 ✔
2732
    }
2733
}
16,830,722 ✔
2734
/**
2735
 * @brief Perform Berserk skill and Passive BGE: EnduringRage
2736
 * 
2737
 * Increases attack by berserk value if not sundered.
2738
 * 
2739
 * @param fd     Field
2740
 * @param att_status Attacker
2741
 * @param def_status Defender
2742
 */
2743
void perform_berserk(Field* fd, CardStatus* att_status, CardStatus* def_status)
16,830,722 ✔
2744
{
2745
            // Skill: Berserk
2746
            unsigned berserk_value = att_status->skill(Skill::berserk);
16,830,722 ✔
2747
            if ( !att_status->m_sundered && (berserk_value > 0))
16,830,722 ✔
2748
            {
2749
                // perform_skill_berserk
2750
                att_status->m_perm_attack_buff += berserk_value;
6,672,808 ✔
2751
#ifndef NQUEST
2752
                if (att_status->m_player == 0)
2753
                {
2754
                    fd->inc_counter(QuestType::skill_use, Skill::berserk);
2755
                }
2756
#endif
2757

2758
                // Passive BGE: EnduringRage
2759
                if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::enduringrage], false))
6,672,808 ✔
2760
                {
2761
                    unsigned bge_denominator = (fd->bg_effects[fd->tapi][PassiveBGE::enduringrage] > 0)
21,662 ✔
2762
                        ? (unsigned)fd->bg_effects[fd->tapi][PassiveBGE::enduringrage]
21,662 ✔
2763
                        : 2;
21,662 ✔
2764
                    unsigned bge_value = (berserk_value - 1) / bge_denominator + 1;
21,662 ✔
2765
                    _DEBUG_MSG(1, "EnduringRage: %s heals and protects itself for %u\n",
21,662 ✔
2766
                            status_description(att_status).c_str(), bge_value);
21,662 ✔
2767
                    att_status->add_hp(bge_value);
21,662 ✔
2768
                    att_status->m_protected += bge_value;
21,662 ✔
2769
                }
2770
            }
2771

2772

2773
}
16,830,722 ✔
2774
/**
2775
 * @brief Poisoning of a attacking card
2776
 * 
2777
 * @param fd  Field
2778
 * @param att_status Attacking card
2779
 * @param def_status Defending card
2780
 */
2781
void perform_poison(Field* fd, CardStatus* att_status, CardStatus* def_status)
20,492,384 ✔
2782
{
2783
    if(__builtin_expect(fd->fixes[Fix::poison_after_attacked],true))
20,492,384 ✔
2784
    {
2785
        if (is_alive(att_status) && def_status->has_skill(Skill::poison))
20,492,384 ✔
2786
        {
2787
            unsigned poison_value = def_status->skill(Skill::poison);
10,177 ✔
2788
            _DEBUG_MSG(1, "%s gets poisoned by %u from %s\n",
10,177 ✔
2789
                            status_description(att_status).c_str(), poison_value,
2790
                            status_description(def_status).c_str());
10,177 ✔
2791
            att_status->m_poisoned += poison_value; 
10,177 ✔
2792
        }
2793
    }
2794
}
20,492,384 ✔
2795

2796
void perform_corrosive(Field* fd, CardStatus* att_status, CardStatus* def_status)
20,492,384 ✔
2797
{
2798
    // Skill: Corrosive
2799
    unsigned corrosive_value = def_status->skill(Skill::corrosive);
20,492,384 ✔
2800
    if (corrosive_value > att_status->m_corroded_rate)
20,492,384 ✔
2801
    {
2802
        // perform_skill_corrosive
2803
        _DEBUG_MSG(1, "%s corrodes %s by %u\n",
40,585 ✔
2804
                status_description(def_status).c_str(),
2805
                status_description(att_status).c_str(), corrosive_value);
40,585 ✔
2806
        att_status->m_corroded_rate = corrosive_value;
40,585 ✔
2807
    }
2808
}
20,492,384 ✔
2809

2810
template<enum CardType::CardType def_cardtype>
2811
void perform_counter(Field* fd, CardStatus* att_status, CardStatus* def_status)
21,226,618 ✔
2812
{
2813
            // Enemy Skill: Counter
2814
            if (def_status->has_skill(Skill::counter))
21,226,618 ✔
2815
            {
2816
                // perform_skill_counter
2817
                unsigned counter_dmg(counter_damage(fd, att_status, def_status));
2,106,300 ✔
2818
#ifndef NQUEST
2819
                if (def_status->m_player == 0)
2820
                {
2821
                    fd->inc_counter(QuestType::skill_use, Skill::counter);
2822
                    fd->inc_counter(QuestType::skill_damage, Skill::counter, 0, counter_dmg);
2823
                }
2824
#endif
2825
                _DEBUG_MSG(1, "%s takes %u counter damage from %s\n",
3,166,072 ✔
2826
                        status_description(att_status).c_str(), counter_dmg,
2827
                        status_description(def_status).c_str());
2828
                remove_hp(fd, att_status, counter_dmg);
2,106,300 ✔
2829
                prepend_on_death(fd);
2,106,300 ✔
2830
                resolve_skill(fd);
2,106,300 ✔
2831

2832
                // Passive BGE: Counterflux
2833
                if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::counterflux], false)
2,106,300 ✔
2834
                        && (def_cardtype == CardType::assault) && is_alive(def_status))
1,254,712 ✔
2835
                {
2836
                    unsigned flux_denominator = (fd->bg_effects[fd->tapi][PassiveBGE::counterflux] > 0)
8,344 ✔
2837
                        ? (unsigned)fd->bg_effects[fd->tapi][PassiveBGE::counterflux]
4,172 ✔
2838
                        : 4;
2839
                    unsigned flux_value = (def_status->skill(Skill::counter) - 1) / flux_denominator + 1;
4,172 ✔
2840
                    _DEBUG_MSG(1, "Counterflux: %s heals itself and berserks for %u\n",
12,516 ✔
2841
                            status_description(def_status).c_str(), flux_value);
2842
                    def_status->add_hp(flux_value);
4,172 ✔
2843
                    if (! def_status->m_sundered)
4,172 ✔
2844
                    { def_status->m_perm_attack_buff += flux_value; }
3,807 ✔
2845
                }
2846
            }
2847
}
21,226,618 ✔
2848
bool check_and_perform_enhance(Field* fd, CardStatus* src, bool early)
160,175,670 ✔
2849
{
2850
      if(!is_active(src))return false; // active
160,175,670 ✔
2851
      if(!src->has_skill(Skill::enhance))return false; // enhance Skill
98,866,046 ✔
2852
      for(auto ss : src->m_card->m_skills)
111,891,076 ✔
2853
      {
2854
          if(ss.id != Skill::enhance)continue;
83,917,504 ✔
2855
          if(early ^ (ss.s == Skill::allegiance || ss.s == Skill::absorb ||ss.s == Skill::stasis || ss.s == Skill::bravery))continue; //only specified skills are 'early'
28,507,682 ✔
2856
          skill_table[ss.id](fd,src,ss);
13,986,786 ✔
2857
      }
2858
      return true;
27,973,572 ✔
2859
}
2860
bool check_and_perform_early_enhance(Field* fd, CardStatus* src)
80,087,835 ✔
2861
{
2862
      return check_and_perform_enhance(fd,src,true);
80,087,835 ✔
2863
}
2864
bool check_and_perform_later_enhance(Field* fd, CardStatus* src)
80,087,835 ✔
2865
{
2866
      return check_and_perform_enhance(fd,src,false);
80,087,835 ✔
2867
}
2868
/**
2869
 * @brief Perform drain skill
2870
 * 
2871
 * @param fd Field
2872
 * @param att_status Attacker status
2873
 * @param def_status Defender status
2874
 * @param att_dmg damage dealt by attacker
2875
 */
2876
template<>
2877
void PerformAttack::perform_swipe_drain<CardType::assault>(Field* fd, CardStatus* att_status, CardStatus* def_status,unsigned att_dmg) {
14,655,996 ✔
2878
        unsigned swipe_value = att_status->skill(Skill::swipe);
14,655,996 ✔
2879
        unsigned drain_value = att_status->skill(Skill::drain);
14,655,996 ✔
2880
        if (swipe_value || drain_value)
14,655,996 ✔
2881
        {
2882
            Storage<CardStatus>& def_assaults(fd->tip->assaults);
1,534,325 ✔
2883
            bool critical_reach = fd->bg_effects[fd->tapi][PassiveBGE::criticalreach];
1,534,325 ✔
2884
            auto drain_total_dmg = att_dmg;
1,534,325 ✔
2885
            unsigned adj_size = 1 + (unsigned)(critical_reach);
1,534,325 ✔
2886
            unsigned host_idx = def_status->m_index;
1,534,325 ✔
2887
            unsigned from_idx = safe_minus(host_idx, adj_size);
1,534,325 ✔
2888
            unsigned till_idx = std::min(host_idx + adj_size, safe_minus(def_assaults.size(), 1));
2,582,326 ✔
2889
            for (; from_idx <= till_idx; ++ from_idx)
4,258,345 ✔
2890
            {
2891
                if (from_idx == host_idx) { continue; }
2,724,020 ✔
2892
                CardStatus* adj_status = &def_assaults[from_idx];
1,189,695 ✔
2893
                if (!is_alive(adj_status)) { continue; }
1,189,695 ✔
2894
                _DEBUG_ASSERT(adj_status->m_card->m_type == CardType::assault); //only assaults
953,579 ✔
2895
                //unsigned swipe_dmg = safe_minus(
2896
                //    swipe_value + drain_value + def_status->m_enfeebled,
2897
                //    def_status->protected_value());
2898
                unsigned remaining_dmg = remove_absorption(fd,adj_status,swipe_value + drain_value + adj_status->m_enfeebled);
953,579 ✔
2899
                remaining_dmg = safe_minus(remaining_dmg,adj_status->protected_value());
953,579 ✔
2900
                _DEBUG_MSG(1, "%s swipes %s for %u damage\n",
953,579 ✔
2901
                        status_description(att_status).c_str(),
2902
                        status_description(adj_status).c_str(), remaining_dmg);
953,579 ✔
2903

2904
                remove_hp(fd, adj_status, remaining_dmg);
953,579 ✔
2905
                drain_total_dmg += remaining_dmg;
953,579 ✔
2906
            }
2907
            if (drain_value && skill_check<Skill::drain>(fd, att_status, nullptr))
1,534,325 ✔
2908
            {
2909
                _DEBUG_MSG(1, "%s drains %u hp\n",
65,808 ✔
2910
                        status_description(att_status).c_str(), drain_total_dmg);
65,808 ✔
2911
                att_status->add_hp(drain_total_dmg);
65,808 ✔
2912
            }
2913
            prepend_on_death(fd);
1,534,325 ✔
2914
            resolve_skill(fd);
1,534,325 ✔
2915
        }
2916
}
14,655,996 ✔
2917
/**
2918
 * @brief Perform Hunt skill
2919
 * 
2920
 * @param fd Field 
2921
 * @param att_status Attacker status
2922
 * @param def_status Defender status
2923
 */
2924
void perform_hunt(Field* fd, CardStatus* att_status, CardStatus* def_status) {
21,238,399 ✔
2925
    unsigned hunt_value = att_status->skill(Skill::hunt);
21,238,399 ✔
2926
        if(hunt_value)
21,238,399 ✔
2927
        {
2928
            CardStatus* hunted_status{select_first_enemy_assault(fd)};
31,885 ✔
2929
            if (hunted_status != nullptr)
31,885 ✔
2930
            {
2931
                unsigned remaining_dmg = remove_absorption(fd,hunted_status,hunt_value + hunted_status->m_enfeebled);
22,073 ✔
2932
                remaining_dmg = safe_minus(remaining_dmg,hunted_status->protected_value());
22,073 ✔
2933
                _DEBUG_MSG(1, "%s hunts %s for %u damage\n",
22,073 ✔
2934
                        status_description(att_status).c_str(),
2935
                        status_description(hunted_status).c_str(), remaining_dmg);
22,073 ✔
2936

2937
                remove_hp(fd, hunted_status, remaining_dmg);
22,073 ✔
2938

2939
                prepend_on_death(fd);
22,073 ✔
2940
                resolve_skill(fd);
22,073 ✔
2941
            }
2942
        }
2943
}
21,238,399 ✔
2944
/**
2945
 * @brief Perform Subdue skill
2946
 * 
2947
 * @param fd Field
2948
 * @param att_status Attacker status
2949
 * @param def_status Defender status
2950
 */
2951
void perform_subdue(Field* fd, CardStatus* att_status, CardStatus* def_status)
22,587,065 ✔
2952
{
2953
                // Skill: Subdue
2954
                unsigned subdue_value = def_status->skill(Skill::subdue);
22,587,065 ✔
2955
                if (__builtin_expect(subdue_value, false))
22,587,065 ✔
2956
                {
2957
                    _DEBUG_MSG(1, "%s subdues %s by %u\n",
827,072 ✔
2958
                            status_description(def_status).c_str(),
2959
                            status_description(att_status).c_str(), subdue_value);
827,072 ✔
2960
                    att_status->m_subdued += subdue_value;
827,072 ✔
2961
                    //fix negative attack
2962
                    if(att_status->calc_attack_power()<0)
827,072 ✔
2963
                    {
2964
                        att_status->m_temp_attack_buff -= att_status->calc_attack_power();
2,438 ✔
2965
                    }
2966
                    if (att_status->m_hp > att_status->max_hp())
935,097 ✔
2967
                    {
2968
                        _DEBUG_MSG(1, "%s loses %u HP due to subdue (max hp: %u)\n",
137,299 ✔
2969
                                status_description(att_status).c_str(),
2970
                                (att_status->m_hp - att_status->max_hp()),
2971
                                att_status->max_hp());
137,299 ✔
2972
                        att_status->m_hp = att_status->max_hp();
242,309 ✔
2973
                    }
2974
                }
2975
}
22,587,065 ✔
2976
bool check_and_perform_valor(Field* fd, CardStatus* src)
14,335,605 ✔
2977
{
2978
    unsigned valor_value = src->skill(Skill::valor);
14,335,605 ✔
2979
    if (valor_value && !src->m_sundered && skill_check<Skill::valor>(fd, src, nullptr))
28,671,210 ✔
2980
    {
2981
        _DEBUG_ASSERT(src->m_card->m_type == CardType::assault); //only assaults
45,184 ✔
2982
        unsigned opponent_player = opponent(src->m_player);
45,184 ✔
2983
        const CardStatus * dst = fd->players[opponent_player]->assaults.size() > src->m_index ?
45,184 ✔
2984
            &fd->players[opponent_player]->assaults[src->m_index] :
20,433 ✔
2985
            nullptr;
45,184 ✔
2986
        if (dst == nullptr || dst->m_hp <= 0)
20,433 ✔
2987
        {
2988
            _DEBUG_MSG(1, "%s loses Valor (no blocker)\n", status_description(src).c_str());
24,751 ✔
2989
            return false;
24,751 ✔
2990
        }
2991
        else if (dst->attack_power() <= src->attack_power())
20,433 ✔
2992
        {
2993
            _DEBUG_MSG(1, "%s loses Valor (weak blocker %s)\n", status_description(src).c_str(), status_description(dst).c_str());
2,276 ✔
2994
            return false;
2,276 ✔
2995
        }
2996
#ifndef NQUEST
2997
        if (src->m_player == 0)
2998
        {
2999
            fd->inc_counter(QuestType::skill_use, Skill::valor);
3000
        }
3001
#endif
3002
        _DEBUG_MSG(1, "%s activates Valor %u\n", status_description(src).c_str(), valor_value);
18,157 ✔
3003
        src->m_perm_attack_buff += valor_value;
18,157 ✔
3004
        return true;
18,157 ✔
3005
    }
3006
    return false;
3007
}
3008

3009
bool check_and_perform_bravery(Field* fd, CardStatus* src)
62,161,565 ✔
3010
{
3011
    unsigned bravery_value = src->skill(Skill::bravery);
62,161,565 ✔
3012
    if (bravery_value && !src->m_sundered && skill_check<Skill::bravery>(fd, src, nullptr))
124,323,130 ✔
3013
    {
3014
        _DEBUG_ASSERT(src->m_card->m_type == CardType::assault); //only assaults
1,976,116 ✔
3015
        unsigned opponent_player = opponent(src->m_player);
1,976,116 ✔
3016
        const CardStatus * dst = fd->players[opponent_player]->assaults.size() > src->m_index ?
1,976,116 ✔
3017
            &fd->players[opponent_player]->assaults[src->m_index] :
1,080,111 ✔
3018
            nullptr;
1,976,116 ✔
3019
        if (dst == nullptr || dst->m_hp <= 0)
1,080,111 ✔
3020
        {
3021
            _DEBUG_MSG(1, "%s loses Bravery (no blocker)\n", status_description(src).c_str());
896,005 ✔
3022
            return false;
896,005 ✔
3023
        }
3024
        else if (dst->attack_power() <= src->attack_power())
1,080,111 ✔
3025
        {
3026
            _DEBUG_MSG(1, "%s loses Bravery (weak blocker %s)\n", status_description(src).c_str(), status_description(dst).c_str());
797,678 ✔
3027
            return false;
797,678 ✔
3028
        }
3029
#ifndef NQUEST
3030
        if (src->m_player == 0)
3031
        {
3032
            fd->inc_counter(QuestType::skill_use, Skill::bravery);
3033
        }
3034
#endif
3035
        _DEBUG_MSG(1, "%s activates Bravery %u\n", status_description(src).c_str(), bravery_value);
282,433 ✔
3036
        src->m_perm_attack_buff += bravery_value;
282,433 ✔
3037

3038
        //BGE: superheroism
3039
        if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::superheroism], false))
282,433 ✔
3040
        {
3041
            unsigned bge_value = bravery_value * fd->bg_effects[fd->tapi][PassiveBGE::superheroism];
892 ✔
3042
            const SkillSpec ss_heal{Skill::heal, bge_value, allfactions, 0, 0, Skill::no_skill, Skill::no_skill, true, 0,};
892 ✔
3043
            _DEBUG_MSG(1, "%s activates SuperHeroism: %s\n", status_description(src).c_str(),
892 ✔
3044
                    skill_description(fd->cards, ss_heal).c_str());
892 ✔
3045
            //fd->skill_queue.emplace(fd->skill_queue.begin()+0, src, ss_heal);
3046
            skill_table[Skill::heal](fd,src,ss_heal); //Probably better to perform the skill direct instead of queuing it
892 ✔
3047
        }
3048

3049
        return true;
282,433 ✔
3050
    }
3051
    return false;
3052
}
3053

3054
bool check_and_perform_inhibit(Field* fd, CardStatus* att_status,CardStatus* def_status)
33,666,611 ✔
3055
{
3056
      unsigned inhibit_value = att_status->skill(Skill::inhibit);
33,666,611 ✔
3057
      if (inhibit_value > def_status->m_inhibited && skill_check<Skill::inhibit>(fd, att_status, def_status))
36,572,097 ✔
3058
      {
3059
        _DEBUG_MSG(1, "%s inhibits %s by %u\n",
2,905,485 ✔
3060
                status_description(att_status).c_str(),
3061
                status_description(def_status).c_str(), inhibit_value);
2,905,485 ✔
3062
        def_status->m_inhibited = inhibit_value;
2,905,485 ✔
3063
        return true;
2,905,485 ✔
3064
      }
3065
      return false;
3066
}
3067
bool check_and_perform_sabotage(Field* fd, CardStatus* att_status, CardStatus* def_status)
33,666,611 ✔
3068
{
3069
    unsigned sabotage_value = att_status->skill(Skill::sabotage);
33,666,611 ✔
3070
    if (sabotage_value > def_status->m_sabotaged && skill_check<Skill::sabotage>(fd, att_status, def_status))
34,114,905 ✔
3071
    {
3072
        _DEBUG_MSG(1, "%s sabotages %s by %u\n",
448,294 ✔
3073
                status_description(att_status).c_str(),
3074
                status_description(def_status).c_str(), sabotage_value);
448,294 ✔
3075
        def_status->m_sabotaged = sabotage_value;
448,294 ✔
3076
        return true;
448,294 ✔
3077
    }
3078
    return false;
3079
}
3080
bool check_and_perform_disease(Field* fd, CardStatus* att_status,CardStatus* def_status)
33,666,611 ✔
3081
{
3082
    unsigned disease_base = att_status->skill(Skill::disease);
33,666,611 ✔
3083
    if(disease_base && skill_check<Skill::disease>(fd, att_status, def_status)) {
33,857,857 ✔
3084
        _DEBUG_MSG(1, "%s diseases %s for %u\n",
191,246 ✔
3085
        status_description(att_status).c_str(), status_description(def_status).c_str(), disease_base);
191,246 ✔
3086
        def_status->m_diseased += disease_base;
191,246 ✔
3087
        return true;
191,246 ✔
3088
    }
3089
    return false;
3090
}
3091

3092
CardStatus* check_and_perform_summon(Field* fd, CardStatus* src)
13,723,182 ✔
3093
{
3094
    unsigned summon_card_id = src->m_card->m_skill_value[Skill::summon];
13,723,182 ✔
3095
    if (summon_card_id)
13,723,182 ✔
3096
    {
3097
        const Card* summoned_card(fd->cards.by_id(summon_card_id));
3,486,688 ✔
3098
        _DEBUG_MSG(1, "%s summons %s\n", status_description(src).c_str(), summoned_card->m_name.c_str());
3,486,688 ✔
3099
        CardStatus* summoned_status = nullptr;
3,486,688 ✔
3100
        switch (summoned_card->m_type)
3,486,688 ✔
3101
        {
3102
            case CardType::assault:
1,178,974 ✔
3103
                summoned_status = PlayCard(summoned_card, fd, src->m_player, src).op<CardType::assault>(true);
1,178,974 ✔
3104
                return summoned_status;
1,178,974 ✔
3105
            case CardType::structure:
2,307,714 ✔
3106
                summoned_status = PlayCard(summoned_card, fd, src->m_player, src).op<CardType::structure>(true);
2,307,714 ✔
3107
                return summoned_status;
2,307,714 ✔
UNCOV
3108
            default:
×
UNCOV
3109
                _DEBUG_MSG(0, "Unknown card type: #%u %s: %u\n",
×
3110
                        summoned_card->m_id, card_description(fd->cards, summoned_card).c_str(),
UNCOV
3111
                        summoned_card->m_type);
×
UNCOV
3112
                _DEBUG_ASSERT(false);
×
3113
                __builtin_unreachable();
3114
        }
3115
    }
3116
    return nullptr;
3117
}
3118

3119

3120
    template<Skill::Skill skill_id>
3121
size_t select_targets(Field* fd, CardStatus* tsrc, const SkillSpec& s)
124,430,405 ✔
3122
{
3123
    size_t n_candidates;
3124
    CardStatus* src;
3125
    if(fd->fixes[Fix::revenge_on_death] && s.s2 == Skill::revenge)
124,430,405 ✔
3126
    {
3127
            _DEBUG_MSG(2,"FIX ON DEATH REVENGE SELECTION")
34,823 ✔
3128
            src = &fd->players[(tsrc->m_player+1)%2]->commander; // selection like enemy commander
34,823 ✔
3129
    }
34,823 ✔
3130
    else
3131
    {
3132
            src = tsrc;
3133
    }
3134
    switch (skill_id)
3135
    {
3136
        case Skill::mortar:
3,261,366 ✔
3137
            n_candidates = select_fast<Skill::siege>(fd, src, skill_targets<Skill::siege>(fd, src), s);
3,261,366 ✔
3138
            if (n_candidates == 0)
3,261,366 ✔
3139
            {
3140
                n_candidates = select_fast<Skill::strike>(fd, src, skill_targets<Skill::strike>(fd, src), s);
926,789 ✔
3141
            }
3142
            break;
3143

3144
        default:
121,169,039 ✔
3145
            n_candidates = select_fast<skill_id>(fd, src, skill_targets<skill_id>(fd, src), s);
121,169,039 ✔
3146
            break;
3147
    }
3148

3149
    // (false-loop)
3150
    unsigned n_selected = n_candidates;
125,166,775 ✔
3151
    do
124,430,405 ✔
3152
    {
3153
        // no candidates
3154
        if (n_candidates == 0)
122,095,828 ✔
3155
        { break; }
3156

3157
        // show candidates (debug)
3158
        _DEBUG_SELECTION("%s", skill_names[skill_id].c_str());
75,928,284 ✔
3159

3160
        // analyze targets count / skill
3161
        unsigned n_targets = s.n > 0 ? s.n : 1;
74,771,475 ✔
3162
        if (s.all || n_targets >= n_candidates || skill_id == Skill::mend || skill_id == Skill::fortify)  // target all or mend
74,771,475 ✔
3163
        { break; }
3164

3165
        // shuffle & trim
3166
        for (unsigned i = 0; i < n_targets; ++i)
41,188,893 ✔
3167
        {
3168
            std::swap(fd->selection_array[i], fd->selection_array[fd->rand(i, n_candidates - 1)]);
20,789,349 ✔
3169
        }
3170
        fd->selection_array.resize(n_targets);
20,399,544 ✔
3171
        if (n_targets > 1)
20,399,544 ✔
3172
        {
3173
            std::sort(fd->selection_array.begin(), fd->selection_array.end(),
263,572 ✔
3174
                    [](const CardStatus * a, const CardStatus * b) { return a->m_index < b->m_index; });
647,982 ✔
3175
        }
3176
        n_selected = n_targets;
3177

3178
    } while (false); // (end)
3179

3180
    return n_selected;
124,430,405 ✔
3181
}
3182

3183
    template<Skill::Skill skill_id>
3184
void perform_targetted_allied_fast(Field* fd, CardStatus* src, const SkillSpec& s)
79,537,966 ✔
3185
{
3186
    select_targets<skill_id>(fd, src, s);
79,537,966 ✔
3187
    unsigned num_inhibited = 0;
79,537,966 ✔
3188
#ifndef NQUEST
3189
    bool has_counted_quest = false;
3190
#endif
3191
    bool src_overloaded = src->m_overloaded;
79,537,966 ✔
3192
    // take the targets without copying them (nested skills select into fd->selection_array), give the buffer back after
3193
    std::vector<CardStatus*> selection_array;
79,537,966 ✔
3194
    selection_array.swap(fd->selection_array);
79,537,966 ✔
3195
    for (CardStatus * dst: selection_array)
163,037,344 ✔
3196
    {
3197
        if (dst->m_inhibited > 0 && !src_overloaded)
83,499,378 ✔
3198
        {
3199
            _DEBUG_MSG(1, "%s %s on %s but it is inhibited\n",
5,289,408 ✔
3200
                    status_description(src).c_str(), skill_short_description(fd->cards, s).c_str(),
3201
                    status_description(dst).c_str());
3202
            -- dst->m_inhibited;
4,829,096 ✔
3203
            ++ num_inhibited;
4,829,096 ✔
3204
            continue;
4,829,096 ✔
3205
        }
4,829,096 ✔
3206
        check_and_perform_skill<skill_id>(fd, src, dst, s, false
78,670,282 ✔
3207
#ifndef NQUEST
3208
                , has_counted_quest
3209
#endif
3210
                );
3211
    }
3212
    fd->selection_array.swap(selection_array);
79,537,966 ✔
3213

3214
    // Passive BGE: Divert
3215
    if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::divert], false)
79,537,966 ✔
3216
            && (num_inhibited > 0))
79,537,966 ✔
3217
    {
3218
        SkillSpec diverted_ss = s;
5,829 ✔
3219
        diverted_ss.y = allfactions;
5,829 ✔
3220
        diverted_ss.n = 1;
5,829 ✔
3221
        diverted_ss.all = false;
5,829 ✔
3222
        for (unsigned i = 0; i < num_inhibited; ++ i)
11,712 ✔
3223
        {
3224
            select_targets<skill_id>(fd, &fd->tip->commander, diverted_ss);
5,883 ✔
3225
            std::vector<CardStatus*> selection_array = fd->selection_array;
5,883 ✔
3226
            for (CardStatus * dst: selection_array)
9,987 ✔
3227
            {
3228
                if (dst->m_inhibited > 0)
4,104 ✔
3229
                {
3230
                    _DEBUG_MSG(1, "%s %s (Diverted) on %s but it is inhibited\n",
894 ✔
3231
                            status_description(src).c_str(), skill_short_description(fd->cards, diverted_ss).c_str(),
3232
                            status_description(dst).c_str());
3233
                    -- dst->m_inhibited;
298 ✔
3234
                    continue;
298 ✔
3235
                }
298 ✔
3236
                _DEBUG_MSG(1, "%s %s (Diverted) on %s\n",
11,418 ✔
3237
                        status_description(src).c_str(), skill_short_description(fd->cards, diverted_ss).c_str(),
3238
                        status_description(dst).c_str());
3239
                perform_skill<skill_id>(fd, src, dst, diverted_ss);
3,840 ✔
3240
            }
3241
        }
3242
    }
3243
}
79,537,966 ✔
3244

UNCOV
3245
void perform_targetted_allied_fast_rush(Field* fd, CardStatus* src, const SkillSpec& s)
×
3246
{
UNCOV
3247
    if (src->m_card->m_type == CardType::commander)
×
3248
    {  // Passive BGE skills are casted as by commander
3249
        perform_targetted_allied_fast<Skill::rush>(fd, src, s);
×
UNCOV
3250
        return;
×
3251
    }
3252
    if (src->m_rush_attempted)
×
3253
    {
UNCOV
3254
        _DEBUG_MSG(2, "%s does not check Rush again.\n", status_description(src).c_str());
×
UNCOV
3255
        return;
×
3256
    }
UNCOV
3257
    _DEBUG_MSG(1, "%s attempts to activate Rush.\n", status_description(src).c_str());
×
UNCOV
3258
    perform_targetted_allied_fast<Skill::rush>(fd, src, s);
×
UNCOV
3259
    src->m_rush_attempted = true;
×
3260
}
3261

3262
    template<Skill::Skill skill_id>
3263
void perform_targetted_hostile_fast(Field* fd, CardStatus* src, const SkillSpec& s)
44,886,556 ✔
3264
{
3265
    select_targets<skill_id>(fd, src, s);
44,886,556 ✔
3266
    std::vector<CardStatus *> paybackers;
44,886,556 ✔
3267
#ifndef NQUEST
3268
    bool has_counted_quest = false;
3269
#endif
3270
    const bool has_turningtides = (fd->bg_effects[fd->tapi][PassiveBGE::turningtides] && (skill_id == Skill::weaken || skill_id == Skill::sunder));
44,886,556 ✔
3271
    unsigned turningtides_value(0), old_attack(0);
44,886,556 ✔
3272

3273
    // apply skill to each target(dst)
3274
    unsigned selection_array_len = fd->selection_array.size();
44,886,556 ✔
3275
    // take the targets without copying them (nested skills select into fd->selection_array), give the buffer back after
3276
    std::vector<CardStatus*> selection_array;
44,886,556 ✔
3277
    selection_array.swap(fd->selection_array);
44,886,556 ✔
3278
    for (CardStatus * dst: selection_array)
100,868,936 ✔
3279
    {
3280
        // TurningTides
3281
        if (__builtin_expect(has_turningtides, false))
15,000,296 ✔
3282
        {
3283
            old_attack = dst->attack_power();
11,142 ✔
3284
        }
3285

3286
        // check & apply skill to target(dst)
3287
        if (check_and_perform_skill<skill_id>(fd, src, dst, s, ! (src->m_overloaded || (__builtin_expect(fd->fixes[Fix::dont_evade_mimic_selection],true) &&  skill_id == Skill::mimic))
56,238,314 ✔
3288
#ifndef NQUEST
3289
                    , has_counted_quest
3290
#endif
3291
                    ))
3292
        {
3293
            // TurningTides: get max attack decreasing
3294
            if (__builtin_expect(has_turningtides, false))
9,909,210 ✔
3295
            {
3296
                turningtides_value = std::max(turningtides_value, safe_minus(old_attack, dst->attack_power()));
25,592 ✔
3297
            }
3298

3299
            // Payback/Revenge: collect paybackers/revengers
3300
            unsigned payback_value = dst->skill(Skill::payback) + dst->skill(Skill::revenge);
39,620,580 ✔
3301
            if ((s.id != Skill::mimic) && (dst->m_paybacked < payback_value) && skill_check<Skill::payback>(fd, dst, src))
39,620,580 ✔
3302
            {
3303
                paybackers.reserve(selection_array_len);
1,027,045 ✔
3304
                paybackers.push_back(dst);
1,027,045 ✔
3305
            }
3306
        }
3307
    }
3308
    fd->selection_array.swap(selection_array);
44,886,556 ✔
3309

3310
    // apply TurningTides
3311
    if (__builtin_expect(has_turningtides, false) && (turningtides_value > 0))
13,892,847 ✔
3312
    {
3313
        SkillSpec ss_rally{Skill::rally, turningtides_value, allfactions, 0, 0, Skill::no_skill, Skill::no_skill, s.all, 0,};
8,400 ✔
3314
        _DEBUG_MSG(1, "TurningTides %u!\n", turningtides_value);
16,800 ✔
3315
        perform_targetted_allied_fast<Skill::rally>(fd, &fd->players[src->m_player]->commander, ss_rally);
8,400 ✔
3316
    }
3317

3318
    prepend_on_death(fd);  // skills
44,886,556 ✔
3319

3320
    // Payback/Revenge
3321
    for (CardStatus * pb_status: paybackers)
45,913,601 ✔
3322
    {
3323
        turningtides_value = 0;
1,027,045 ✔
3324

3325
        // apply Revenge
3326
        if (pb_status->skill(Skill::revenge))
1,027,045 ✔
3327
        {
3328
            unsigned revenged_count(0);
3329
            for (unsigned case_index(0); case_index < 3; ++ case_index)
4,106,160 ✔
3330
            {
3331
                CardStatus * target_status;
3332
#ifndef NDEBUG
3333
                const char * target_desc;
3334
#endif
3335
                switch (case_index)
3,079,620 ✔
3336
                {
3337
                    // revenge to left
3338
                    case 0:
1,026,540 ✔
3339
                        if (!(target_status = fd->left_assault(src))) { continue; }
264,354 ✔
3340
#ifndef NDEBUG
3341
                        target_desc = "left";
3342
#endif
3343
                        break;
3344

3345
                        // revenge to core
3346
                    case 1:
3347
                        target_status = src;
3348
#ifndef NDEBUG
3349
                        target_desc = "core";
3350
#endif
3351
                        break;
3352

3353
                        // revenge to right
3354
                    case 2:
1,026,540 ✔
3355
                        if (!(target_status = fd->right_assault(src))) { continue; }
2,398,129 ✔
3356
#ifndef NDEBUG
3357
                        target_desc = "right";
3358
#endif
3359
                        break;
3360

3361
                        // wtf?
3362
                    default:
3363
                        __builtin_unreachable();
3364
                }
3365

3366
                // skip illegal target
3367
                if (!skill_predicate<skill_id>(fd, target_status, target_status, s))
2,569,645 ✔
3368
                {
3369
                    continue;
156,363 ✔
3370
                }
3371

3372
                // skip dead target
3373
                if (!is_alive(target_status))
2,413,282 ✔
3374
                {
3375
#ifndef NDEBUG
UNCOV
3376
                    _DEBUG_MSG(1, "(CANCELLED: target unit dead) %s Revenge (to %s) %s on %s\n",
×
3377
                            status_description(pb_status).c_str(), target_desc,
3378
                            skill_short_description(fd->cards, s).c_str(), status_description(target_status).c_str());
3379
#endif
UNCOV
3380
                    continue;
×
UNCOV
3381
                }
×
3382

3383
                // TurningTides
3384
                if (__builtin_expect(has_turningtides, false))
88,883 ✔
3385
                {
3386
                    old_attack = target_status->attack_power();
798 ✔
3387
                }
3388

3389
                // apply revenged skill
3390
#ifndef NDEBUG
3391
                _DEBUG_MSG(1, "%s Revenge (to %s) %s on %s\n",
2,593,694 ✔
3392
                        status_description(pb_status).c_str(), target_desc,
3393
                        skill_short_description(fd->cards, s).c_str(), status_description(target_status).c_str());
3394
#endif
3395
                perform_skill<skill_id>(fd, pb_status, target_status, s);
2,413,282 ✔
3396
                ++ revenged_count;
2,413,282 ✔
3397

3398
                // revenged TurningTides: get max attack decreasing
3399
                if (__builtin_expect(has_turningtides, false))
88,883 ✔
3400
                {
3401
                    turningtides_value = std::max(turningtides_value, safe_minus(old_attack, target_status->attack_power()));
2,068 ✔
3402
                }
3403
            }
3404
            if (revenged_count)
1,026,540 ✔
3405
            {
3406
                // consume remaining payback/revenge
3407
                ++ pb_status->m_paybacked;
1,005,447 ✔
3408

3409
                // apply TurningTides
3410
                if (__builtin_expect(has_turningtides, false) && (turningtides_value > 0))
45,881 ✔
3411
                {
3412
                    SkillSpec ss_rally{Skill::rally, turningtides_value, allfactions, 0, 0, Skill::no_skill, Skill::no_skill, false, 0,};
416 ✔
3413
                    _DEBUG_MSG(1, "Paybacked TurningTides %u!\n", turningtides_value);
832 ✔
3414
                    perform_targetted_allied_fast<Skill::rally>(fd, &fd->players[pb_status->m_player]->commander, ss_rally);
416 ✔
3415
                }
3416
            }
3417
        }
3418
        // apply Payback
3419
        else
3420
        {
3421
            // skip illegal target(src)
3422
            if (!skill_predicate<skill_id>(fd, src, src, s))
505 ✔
3423
            {
3424
                continue;
173 ✔
3425
            }
3426

3427
            // skip dead target(src)
3428
            if (!is_alive(src))
332 ✔
3429
            {
UNCOV
3430
                _DEBUG_MSG(1, "(CANCELLED: src unit dead) %s Payback %s on %s\n",
×
3431
                        status_description(pb_status).c_str(), skill_short_description(fd->cards, s).c_str(),
3432
                        status_description(src).c_str());
3433
                continue;
×
UNCOV
3434
            }
×
3435

3436
            // TurningTides
3437
            if (__builtin_expect(has_turningtides, false))
89 ✔
3438
            {
UNCOV
3439
                old_attack = src->attack_power();
×
3440
            }
3441

3442
            // apply paybacked skill
3443
            _DEBUG_MSG(1, "%s Payback %s on %s\n",
996 ✔
3444
                    status_description(pb_status).c_str(), skill_short_description(fd->cards, s).c_str(), status_description(src).c_str());
3445
            perform_skill<skill_id>(fd, pb_status, src, s);
332 ✔
3446
            ++ pb_status->m_paybacked;
332 ✔
3447

3448
            // handle paybacked TurningTides
3449
            if (__builtin_expect(has_turningtides, false))
89 ✔
3450
            {
UNCOV
3451
                turningtides_value = std::max(turningtides_value, safe_minus(old_attack, src->attack_power()));
×
UNCOV
3452
                if (turningtides_value > 0)
×
3453
                {
UNCOV
3454
                    SkillSpec ss_rally{Skill::rally, turningtides_value, allfactions, 0, 0, Skill::no_skill, Skill::no_skill, false, 0,};
×
UNCOV
3455
                    _DEBUG_MSG(1, "Paybacked TurningTides %u!\n", turningtides_value);
×
UNCOV
3456
                    perform_targetted_allied_fast<Skill::rally>(fd, &fd->players[pb_status->m_player]->commander, ss_rally);
×
3457
                }
3458
            }
3459
        }
3460
    }
3461

3462
    prepend_on_death(fd,true);  // paybacked skills
44,886,556 ✔
3463
}
44,886,556 ✔
3464

3465
//------------------------------------------------------------------------------
3466
inline unsigned evaluate_brawl_score(Field* fd, unsigned player)
×
3467
{
3468
    const auto & p = fd->players;
×
UNCOV
3469
    return 55
×
3470
        // - (10 - p[player]->deck->cards.size())
3471
        // + (10 - p[opponent(player)]->deck->cards.size())
UNCOV
3472
        + p[opponent(player)]->total_cards_destroyed
×
3473
        + p[player]->deck->shuffled_cards.size()
×
UNCOV
3474
        - (unsigned)((fd->turn+7)/8);
×
3475
}
3476

UNCOV
3477
inline unsigned evaluate_war_score(Field* fd, unsigned player)
×
3478
{
3479
    return 208 - ((unsigned)(fd->turn)/2)*4;
×
3480
}
3481
int evaluate_card(Field* fd,const Card* cs);
3482
int evaluate_skill(Field* fd,const Card* c , SkillSpec* ss)
×
3483
{
3484
        // TODO optimize this
3485
        int tvalue = ss->x;
×
3486

3487
        if(ss->card_id != 0)tvalue += 1*evaluate_card(fd,card_by_id_safe(fd->cards,ss->card_id));
×
3488
        tvalue += 10*(ss->id==Skill::flurry);
×
UNCOV
3489
        tvalue += 10*(ss->id==Skill::jam);
×
3490
        tvalue += 5*(ss->id==Skill::overload);
×
3491
        tvalue += 2*(ss->id==Skill::flying);
×
3492
        tvalue += 2*(ss->id==Skill::evolve);
×
3493
        tvalue += 2*(ss->id==Skill::wall);
×
3494
        tvalue += 5*(ss->id==Skill::tribute);
×
3495

3496
        tvalue *= 1.+1.5*(ss->id==Skill::flurry);
×
3497
        tvalue *= 1.+1.5*(ss->id==Skill::drain);
×
UNCOV
3498
        tvalue *= 1.+1.5*(ss->id==Skill::mortar);
×
3499
        tvalue *= 1.+1.5*(ss->id==Skill::scavenge);
×
3500
        tvalue *= 1.+1.5*(ss->id==Skill::disease);
×
3501

UNCOV
3502
        tvalue *= 1.+1.3*(ss->id==Skill::rally);
×
3503
        tvalue *= 1.+1.3*(ss->id==Skill::strike);
×
3504

UNCOV
3505
        tvalue *= 1.+1.2*(ss->id==Skill::avenge);
×
3506
        tvalue *= 1.+1.1*(ss->id==Skill::sunder);
×
UNCOV
3507
        tvalue *= 1.+1.1*(ss->id==Skill::venom);
×
3508

3509
        tvalue *= 1.+1.0*(ss->id==Skill::evade);
×
UNCOV
3510
        tvalue *= 1.+1.0*(ss->id==Skill::enfeeble);
×
3511

3512
        tvalue *= 1.+0.2*(ss->id==Skill::protect);
×
3513

3514
        tvalue *= 1.+0.2*(ss->id==Skill::fortify);
×
3515
        tvalue *= 1.+0.5*(ss->id==Skill::mend);
×
3516

UNCOV
3517
        tvalue *= 1.+0.4*(ss->id==Skill::jam);
×
UNCOV
3518
        tvalue *= 1.+0.4*(ss->id==Skill::overload);
×
3519
        //tvalue *= 1.+0.4*(ss->id==Skill::rupture);
3520
        tvalue *= 1.+0.4*(ss->id==Skill::bravery);
×
UNCOV
3521
        tvalue *= 1.+0.4*(ss->id==Skill::entrap);
×
UNCOV
3522
        tvalue *= 1.+0.4*(ss->id==Skill::heal);
×
3523

3524

3525
        tvalue *= 1.+0.3*(ss->id==Skill::revenge);
×
3526
        tvalue *= 1.+0.3*(ss->id==Skill::enrage);
×
3527

3528

3529
        //tvalue *= 1.+2.1*(ss->id==Skill::hunt);
UNCOV
3530
        tvalue *= 1.+0.1*(ss->id==Skill::mark);
×
UNCOV
3531
        tvalue *= 1.+0.1*(ss->id==Skill::coalition);
×
3532
        tvalue *= 1.+0.1*(ss->id==Skill::legion);
×
3533
        //tvalue *= 1.+1.1*(ss->id==Skill::barrier);
UNCOV
3534
        tvalue *= 1.+0.1*(ss->id==Skill::pierce);
×
UNCOV
3535
        tvalue *= 1.+0.1*(ss->id==Skill::armor);
×
3536
        //tvalue *= 1.+0.1*(ss->id==Skill::swipe);
3537
        //tvalue *= 1.+0.1*(ss->id==Skill::berserk);
3538
        tvalue *= 1.-0.1*(ss->id==Skill::weaken);
×
3539

3540

3541

UNCOV
3542
        tvalue *= 1.-0.5 *(ss->id==Skill::sabotage); //sucks
×
3543
        tvalue *= 1.-0.5 *(ss->id==Skill::inhibit); //sucks
×
3544
        tvalue *= 1.-0.5 *(ss->id==Skill::corrosive); //sucks
×
3545
        tvalue *= 1.-0.5 *(ss->id==Skill::payback); //sucks
×
3546
        tvalue *= 1.-0.5 *(ss->id==Skill::leech); //sucks
×
3547

3548

UNCOV
3549
        tvalue *= 1.+1*ss->all;
×
3550
        tvalue *= 1.-1./5.*ss->all*(ss->y!=0);
×
UNCOV
3551
        tvalue *= 1.+1*std::min<int>(3,ss->n);
×
3552
        tvalue *= 1.-1./3.* ((c->m_skill_trigger[ss->id] == Skill::Trigger::death) + (c->m_skill_trigger[ss->id] == Skill::Trigger::play));
×
UNCOV
3553
        tvalue *= 1./(2.+ss->c);
×
3554
        //if(tvalue == 0) std::cout << ss->id << " "<<tvalue << std::endl;
3555
        //if(tvalue > 10000) std::cout << ss->id <<" "<< tvalue << std::endl;
3556
        return 0.9*tvalue; // 0.85
×
3557
}
3558
int evaluate_card(Field* fd,const Card* cs)
×
3559
{
3560
        int value = 0;
×
UNCOV
3561
        value += cs->m_health;
×
3562
        value += 2*cs->m_attack;
×
UNCOV
3563
        for( auto ss : cs->m_skills) {
×
3564
                value += evaluate_skill(fd,cs,&ss);
×
3565
        }
3566
        int denom_scale = 1+cs->m_delay*0;
×
3567
        //if(value > 10000) std::cout << cs->m_name << value << std::endl;
3568
        return value /denom_scale;
×
3569
}
3570
int evaluate_cardstatus(Field* fd,CardStatus* cs)
×
3571
{
UNCOV
3572
        int value = 0;
×
3573
        value += cs->m_hp;
×
3574
        value += 2*cs->attack_power();
×
UNCOV
3575
        value += cs->protected_value();
×
UNCOV
3576
        for( auto ss : cs->m_card->m_skills) {
×
UNCOV
3577
                value += evaluate_skill(fd,cs->m_card,&ss);
×
3578
        }
UNCOV
3579
        value -= (cs->m_enfeebled);
×
UNCOV
3580
        int denom_scale = 1+cs->m_delay*0;
×
3581
#ifdef DEBUG
3582
        if(value > 10000) std::cout << cs->m_card->m_name << value <<std::endl;
3583
#endif
3584
        return value /denom_scale;
×
3585
}
3586
// calculate a value for current field, high values are better for fd->tap
3587
// dead commander -> the player gets zero value
3588
int evaluate_field(Field* fd)
×
3589
{
UNCOV
3590
        int value = 0;
×
3591

3592
        int scale = is_alive(&fd->tap->commander);
×
UNCOV
3593
        auto& assaults(fd->tap->assaults);
×
3594
        auto& structures(fd->tap->structures);
×
3595

3596

UNCOV
3597
        value += 0.5*scale * evaluate_cardstatus(fd,&fd->tap->commander);
×
3598
        for(unsigned index(0); index < assaults.size();++index)
×
3599
        {
UNCOV
3600
                value += scale * evaluate_cardstatus(fd,&assaults[index]);
×
3601
        }
3602
        for(unsigned index(0); index < structures.size();++index)
×
3603
        {
3604
                value += scale * evaluate_cardstatus(fd,&structures[index]);
×
3605
        }
3606

3607
        scale = is_alive(&fd->tip->commander);
×
UNCOV
3608
        auto& eassaults(fd->tip->assaults);
×
3609
        auto& estructures(fd->tip->structures);
×
UNCOV
3610
        value -= 0.5*scale * evaluate_cardstatus(fd,&fd->tip->commander);
×
3611
        for(unsigned index(0); index < eassaults.size();++index)
×
3612
        {
3613
                value -= (scale * evaluate_cardstatus(fd,&eassaults[index]));
×
3614
        }
UNCOV
3615
        for(unsigned index(0); index < estructures.size();++index)
×
3616
        {
UNCOV
3617
                value -= (scale * evaluate_cardstatus(fd,&estructures[index]));
×
3618
        }
UNCOV
3619
        return value;
×
3620
}
3621

3622

3623
Results<uint64_t> evaluate_sim_result(Field* fd, bool single_turn_both)
1,365,866 ✔
3624
{
3625
    typedef unsigned points_score_type;
1,365,866 ✔
3626
    const auto & p = fd->players;
1,365,866 ✔
3627
    unsigned raid_damage = 0;
1,365,866 ✔
3628
#ifndef NQUEST
3629
    unsigned quest_score = 0;
3630
#endif
3631

3632
    if(single_turn_both)
1,365,866 ✔
3633
    {
3634
        bool sign = evaluate_field(fd)<0;
×
3635
        unsigned val = evaluate_field(fd) *(1-2*sign);
×
3636
        return {!is_alive(&fd->players[1]->commander),sign,!is_alive(&fd->players[0]->commander),val,1};
×
3637
    }
3638
    switch (fd->optimization_mode)
1,365,866 ✔
3639
    {
UNCOV
3640
        case OptimizationMode::raid:
×
UNCOV
3641
            raid_damage = 15
×
UNCOV
3642
                + (p[1]->total_nonsummon_cards_destroyed)
×
UNCOV
3643
                - (10 * p[1]->commander.m_hp / p[1]->commander.max_hp());
×
UNCOV
3644
            break;
×
3645
#ifndef NQUEST
3646
        case OptimizationMode::quest:
3647
            if (fd->quest.quest_type == QuestType::card_survival)
3648
            {
3649
                for (const auto & status: p[0]->assaults.m_indirect)
3650
                { fd->quest_counter += (fd->quest.quest_key == status->m_card->m_id); }
3651
                for (const auto & status: p[0]->structures.m_indirect)
3652
                { fd->quest_counter += (fd->quest.quest_key == status->m_card->m_id); }
3653
                for (const auto & card: p[0]->deck->shuffled_cards)
3654
                { fd->quest_counter += (fd->quest.quest_key == card->m_id); }
3655
            }
3656
            quest_score = fd->quest.must_fulfill ? (fd->quest_counter >= fd->quest.quest_value ? fd->quest.quest_score : 0) : std::min<unsigned>(fd->quest.quest_score, fd->quest.quest_score * fd->quest_counter / fd->quest.quest_value);
3657
            _DEBUG_MSG(1, "Quest: %u / %u = %u%%.\n", fd->quest_counter, fd->quest.quest_value, quest_score);
3658
            break;
3659
#endif
3660
        default:
3661
            break;
3662
    }
3663
    // you lose
3664
    if(!is_alive(&fd->players[0]->commander))
1,365,866 ✔
3665
    {
3666
        _DEBUG_MSG(1, "You lose.\n");
167,471 ✔
3667
        switch (fd->optimization_mode)
167,471 ✔
3668
        {
3669
            case OptimizationMode::raid: return {0, 0, 1, (points_score_type)raid_damage,1};
×
3670
            case OptimizationMode::brawl: return {0, 0, 1, (points_score_type) 5,1};
×
3671
            case OptimizationMode::brawl_defense:
×
UNCOV
3672
                                          {
×
3673
                                              unsigned enemy_brawl_score = evaluate_brawl_score(fd, 1);
×
3674
                                              unsigned max_score = max_possible_score[(size_t)OptimizationMode::brawl_defense];
×
3675
                                              if(enemy_brawl_score> max_score)
×
3676
                                                std::cerr << "WARNING: enemy_brawl_score > max_possible_brawl_score" << std::endl;
×
3677
                                              return {0, 0, 1, (points_score_type)safe_minus(max_score , enemy_brawl_score),1};
×
3678
                                          }
3679
            case OptimizationMode::war: return {0,0,1, (points_score_type) 20,1};
×
3680
            case OptimizationMode::war_defense:
×
UNCOV
3681
                                        {
×
UNCOV
3682
                                            unsigned enemy_war_score = evaluate_war_score(fd, 1);
×
UNCOV
3683
                                            unsigned max_score = max_possible_score[(size_t)OptimizationMode::war_defense];
×
UNCOV
3684
                                            if(enemy_war_score> max_score)
×
UNCOV
3685
                                                std::cerr << "WARNING: enemy_war_score > max_possible_war_score" << std::endl;
×
UNCOV
3686
                                            return {0, 0, 1, (points_score_type)safe_minus(max_score , enemy_war_score),1};
×
3687
                                        }
3688
#ifndef NQUEST
3689
            case OptimizationMode::quest: return {0, 0, 1, (points_score_type)(fd->quest.must_win ? 0 : quest_score),1};
3690
#endif
3691
            default: return {0, 0, 1, 0,1};
167,471 ✔
3692
        }
3693
    }
3694
    // you win
3695
    if(!is_alive(&fd->players[1]->commander))
1,198,395 ✔
3696
    {
3697
        _DEBUG_MSG(1, "You win.\n");
1,177,462 ✔
3698
        switch (fd->optimization_mode)
1,177,462 ✔
3699
        {
3700
            case OptimizationMode::brawl:
×
3701
                {
×
3702
                    unsigned brawl_score = evaluate_brawl_score(fd, 0);
×
3703
                    return {1, 0, 0, (points_score_type)brawl_score,1};
×
3704
                }
3705
            case OptimizationMode::brawl_defense:
×
3706
                {
×
3707
                    unsigned max_score = max_possible_score[(size_t)OptimizationMode::brawl_defense];
×
3708
                    unsigned min_score = min_possible_score[(size_t)OptimizationMode::brawl_defense];
×
3709
                    return {1, 0, 0, (points_score_type)(max_score - min_score),1};
×
3710
                }
3711
            case OptimizationMode::campaign:
×
3712
                {
×
3713
                    unsigned total_dominions_destroyed = (p[0]->deck->alpha_dominion != nullptr) - p[0]->structures.count(is_it_dominion);
×
3714
                    unsigned campaign_score = 100 - 10 * (p[0]->total_nonsummon_cards_destroyed - total_dominions_destroyed);
×
UNCOV
3715
                    return {1, 0, 0, (points_score_type)campaign_score,1};
×
3716
                }
3717
            case OptimizationMode::war:
×
3718
                {
×
3719
                    unsigned war_score = evaluate_war_score(fd, 0);
×
3720
                    return {1,0,0, (points_score_type) war_score,1};
×
3721
                }
UNCOV
3722
            case OptimizationMode::war_defense:
×
UNCOV
3723
                {
×
UNCOV
3724
                    unsigned max_score = max_possible_score[(size_t)OptimizationMode::war_defense];
×
UNCOV
3725
                    unsigned min_score = min_possible_score[(size_t)OptimizationMode::war_defense];
×
UNCOV
3726
                    return {1, 0, 0, (points_score_type)(max_score - min_score),1};
×
3727
                }
3728
#ifndef NQUEST
3729
            case OptimizationMode::quest: return {1, 0, 0, (points_score_type)(fd->quest.win_score + quest_score),1};
3730
#endif
3731
            default:
1,177,462 ✔
3732
                                          return {1, 0, 0, 100,1};
1,177,462 ✔
3733
        }
3734
    }
3735
    if (fd->turn > turn_limit)
20,933 ✔
3736
    {
3737
        _DEBUG_MSG(1, "Stall after %u turns.\n", turn_limit);
20,933 ✔
3738
        switch (fd->optimization_mode)
20,933 ✔
3739
        {
3740
            case OptimizationMode::defense: return {0, 1, 0, 100,1};
×
3741
            case OptimizationMode::raid: return {0, 1, 0, (points_score_type)raid_damage,1};
×
UNCOV
3742
            case OptimizationMode::brawl: return {0, 1, 0, 5,1};
×
3743
            case OptimizationMode::brawl_defense:
×
3744
                                          {
×
3745
                                              unsigned max_score = max_possible_score[(size_t)OptimizationMode::brawl_defense];
×
3746
                                              unsigned min_score = min_possible_score[(size_t)OptimizationMode::brawl_defense];
×
3747
                                              return {1, 0, 0, (points_score_type)(max_score - min_score),1};
×
3748
                                          }
UNCOV
3749
            case OptimizationMode::war: return {0,1,0, (points_score_type) 20,1};
×
UNCOV
3750
            case OptimizationMode::war_defense:
×
UNCOV
3751
                                        {
×
UNCOV
3752
                                            unsigned max_score = max_possible_score[(size_t)OptimizationMode::war_defense];
×
UNCOV
3753
                                            unsigned min_score = min_possible_score[(size_t)OptimizationMode::war_defense];
×
UNCOV
3754
                                            return {1, 0, 0, (points_score_type)(max_score - min_score),1};
×
3755
                                        }
3756
#ifndef NQUEST
3757
            case OptimizationMode::quest: return {0, 1, 0, (points_score_type)(fd->quest.must_win ? 0 : quest_score),1};
3758
#endif
3759
            default: return {0, 1, 0, 0,1};
20,933 ✔
3760
        }
3761
    }
3762

3763
    // Huh? How did we get here?
UNCOV
3764
    assert(false);
×
3765
    return {0, 0, 0, 0,1};
3766
}
3767

3768
//------------------------------------------------------------------------------
3769
//turns_both sets the number of turns to sim before exiting before winner exists.
3770
Results<uint64_t> play(Field* fd,bool skip_init, bool skip_preplay,unsigned turns_both)
1,365,866 ✔
3771
{
3772
    if(!skip_init){ //>>> start skip init
1,365,866 ✔
3773
        fd->players[0]->commander.m_player = 0;
1,365,866 ✔
3774
        fd->players[1]->commander.m_player = 1;
1,365,866 ✔
3775
        fd->tapi = fd->gamemode == surge ? 1 : 0;
1,365,866 ✔
3776
        fd->tipi = opponent(fd->tapi);
1,365,866 ✔
3777
        fd->tap = fd->players[fd->tapi];
1,365,866 ✔
3778
        fd->tip = fd->players[fd->tipi];
1,365,866 ✔
3779
        fd->end = false;
1,365,866 ✔
3780

3781
        // Play dominion & fortresses
3782
        for (unsigned _(0), ai(fd->tapi); _ < 2; ++_)
4,097,598 ✔
3783
        {
3784
            if (fd->players[ai]->deck->alpha_dominion)
2,731,732 ✔
3785
            { PlayCard(fd->players[ai]->deck->alpha_dominion, fd, ai, &fd->players[ai]->commander).op<CardType::structure>(); }
1,472,000 ✔
3786
            for (const Card* played_card: fd->players[ai]->deck->shuffled_forts)
5,463,464 ✔
3787
            {
3788

3789
                switch (played_card->m_type)
×
3790
                {
3791
                    case CardType::assault:
×
3792
                        PlayCard(played_card, fd, ai, &fd->players[ai]->commander).op<CardType::assault>();
×
3793
                        break;
×
3794
                    case CardType::structure:
×
3795
                        PlayCard(played_card, fd, ai, &fd->players[ai]->commander).op<CardType::structure>();
×
UNCOV
3796
                        break;
×
UNCOV
3797
                    case CardType::commander:
×
UNCOV
3798
                    case CardType::num_cardtypes:
×
UNCOV
3799
                        _DEBUG_MSG(0, "Unknown card type: #%u %s: %u\n",
×
UNCOV
3800
                                played_card->m_id, card_description(fd->cards, played_card).c_str(), played_card->m_type);
×
UNCOV
3801
                        assert(false);
×
3802
                        break;
3803
                }
3804
            }
3805
            resolve_skill(fd);
2,731,732 ✔
3806
            std::swap(fd->tapi, fd->tipi);
2,731,732 ✔
3807
            std::swap(fd->tap, fd->tip);
2,731,732 ✔
3808
            ai = opponent(ai);
2,731,732 ✔
3809
        }
3810
    }//>>> end skip init
3811
    unsigned both_turn_limit = fd->turn+2*turns_both;
1,365,866 ✔
3812
    while(__builtin_expect(fd->turn <= turn_limit && !fd->end && (turns_both==0 || fd->turn < both_turn_limit), true))
19,292,973 ✔
3813
    {
3814
        if(!skip_preplay){ //>>> start skip init
19,272,040 ✔
3815

3816
            fd->current_phase = Field::playcard_phase;
19,272,040 ✔
3817
            // Initialize stuff, remove dead cards
3818
            _DEBUG_MSG(1, "------------------------------------------------------------------------\n"
19,272,040 ✔
3819
                    "TURN %u begins for %s\n", fd->turn, status_description(&fd->tap->commander).c_str());
19,272,040 ✔
3820

3821
            // reduce timers & perform triggered skills (like Summon)
3822
            fd->prepare_action();
19,272,040 ✔
3823
            turn_start_phase(fd); // summon may postpone skills to be resolved
19,272,040 ✔
3824
            resolve_skill(fd); // resolve postponed skills recursively
19,272,040 ✔
3825
            fd->finalize_action();
19,272,040 ✔
3826

3827
            //bool bge_megamorphosis = fd->bg_effects[fd->tapi][PassiveBGE::megamorphosis];
3828

3829
        }//>>> end skip init
3830
        else { skip_preplay = false;}
3831
        // Play a card
3832
        const Card* played_card(fd->tap->deck->next(fd));
19,272,040 ✔
3833
        if (played_card)
19,272,040 ✔
3834
        {
3835

3836
            // Begin 'Play Card' phase action
3837
            fd->prepare_action();
17,400,230 ✔
3838

3839
            // Play selected card
3840
            //CardStatus* played_status = nullptr;
3841
            switch (played_card->m_type)
17,400,230 ✔
3842
            {
3843
                case CardType::assault:
16,568,310 ✔
3844
                    PlayCard(played_card, fd, fd->tapi, &fd->tap->commander).op<CardType::assault>();
16,568,310 ✔
3845
                    break;
16,568,310 ✔
3846
                case CardType::structure:
831,920 ✔
3847
                    PlayCard(played_card, fd, fd->tapi, &fd->tap->commander).op<CardType::structure>();
831,920 ✔
3848
                    break;
831,920 ✔
UNCOV
3849
                case CardType::commander:
×
UNCOV
3850
                case CardType::num_cardtypes:
×
UNCOV
3851
                    _DEBUG_MSG(0, "Unknown card type: #%u %s: %u\n",
×
UNCOV
3852
                            played_card->m_id, card_description(fd->cards, played_card).c_str(), played_card->m_type);
×
UNCOV
3853
                    assert(false);
×
3854
                    break;
3855
            }
3856
            resolve_skill(fd); // resolve postponed skills recursively
17,400,230 ✔
3857
            //status_description(played_status)
3858
            //_DEBUG_MSG(3,"Card played: %s", status_description(played_status).c_str());
3859
            // End 'Play Card' phase action
3860
            fd->finalize_action();
17,400,230 ✔
3861

3862

3863

3864
        }
3865
        if (__builtin_expect(fd->end, false)) { break; }
19,272,040 ✔
3866

3867
        //-------------------------------------------------
3868
        // Phase: (Later-) Enhance, Inhibit, Sabotage, Disease
3869
        //-------------------------------------------------
3870
        //Skill: Enhance
3871
        //Perform later enhance for commander
3872
        if(!fd->fixes[Fix::enhance_early]) {
19,272,040 ✔
3873
        check_and_perform_later_enhance(fd,&fd->tap->commander);
×
UNCOV
3874
        auto& structures(fd->tap->structures);
×
UNCOV
3875
        for(unsigned index(0); index < structures.size(); ++index)
×
3876
        {
UNCOV
3877
            CardStatus * status = &structures[index];
×
3878
            //enhance everything else after card was played
UNCOV
3879
            check_and_perform_later_enhance(fd,status);
×
3880
        }
3881
        }
3882
        //Perform Inhibit, Sabotage, Disease
3883
        auto& assaults(fd->tap->assaults);
19,272,040 ✔
3884
        for(unsigned index(0); index < assaults.size(); ++index)
74,891,787 ✔
3885
        {
3886
            CardStatus * att_status = &assaults[index];
55,619,747 ✔
3887
            if(att_status->m_index >= fd->tip->assaults.size())continue; //skip no enemy
55,619,747 ✔
3888
            auto def_status = &fd->tip->assaults[att_status->m_index];
33,666,773 ✔
3889
            if(!is_alive(def_status))continue; //skip dead
33,666,773 ✔
3890

3891
            check_and_perform_inhibit(fd,att_status,def_status);
33,666,611 ✔
3892
            check_and_perform_sabotage(fd,att_status,def_status);
33,666,611 ✔
3893
            check_and_perform_disease(fd,att_status,def_status);
33,666,611 ✔
3894
        }
3895
        //-------------------------------------------------
3896

3897
        // Evaluate Passive BGE Heroism skills
3898
        if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::heroism], false))
19,272,040 ✔
3899
        {
3900
            for (CardStatus * dst: fd->tap->assaults.m_indirect)
241,623 ✔
3901
            {
3902
                unsigned bge_value = (dst->skill(Skill::valor) + dst->skill(Skill::bravery)+ 1) / 2;
149,581 ✔
3903
                if (bge_value <= 0)
149,581 ✔
3904
                { continue; }
132,746 ✔
3905
                SkillSpec ss_protect{Skill::protect, bge_value, allfactions, 0, 0, Skill::no_skill, Skill::no_skill, false, 0,};
16,949 ✔
3906
                if (dst->m_inhibited > 0)
16,949 ✔
3907
                {
3908
                    _DEBUG_MSG(1, "Heroism: %s on %s but it is inhibited\n",
114 ✔
3909
                            skill_short_description(fd->cards, ss_protect).c_str(), status_description(dst).c_str());
114 ✔
3910
                    -- dst->m_inhibited;
114 ✔
3911

3912
                    // Passive BGE: Divert
3913
                    if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::divert], false))
114 ✔
3914
                    {
3915
                        SkillSpec diverted_ss = ss_protect;
×
3916
                        diverted_ss.y = allfactions;
×
3917
                        diverted_ss.n = 1;
×
UNCOV
3918
                        diverted_ss.all = false;
×
3919
                        // for (unsigned i = 0; i < num_inhibited; ++ i)
UNCOV
3920
                        {
×
3921
                            select_targets<Skill::protect>(fd, &fd->tip->commander, diverted_ss);
×
3922
                            std::vector<CardStatus*> selection_array = fd->selection_array;
×
3923
                            for (CardStatus * dst: selection_array)
×
3924
                            {
3925
                                if (dst->m_inhibited > 0)
×
3926
                                {
3927
                                    _DEBUG_MSG(1, "Heroism: %s (Diverted) on %s but it is inhibited\n",
×
3928
                                            skill_short_description(fd->cards, diverted_ss).c_str(), status_description(dst).c_str());
×
UNCOV
3929
                                    -- dst->m_inhibited;
×
3930
                                    continue;
×
UNCOV
3931
                                }
×
UNCOV
3932
                                _DEBUG_MSG(1, "Heroism: %s (Diverted) on %s\n",
×
UNCOV
3933
                                        skill_short_description(fd->cards, diverted_ss).c_str(), status_description(dst).c_str());
×
UNCOV
3934
                                perform_skill<Skill::protect>(fd, &fd->tap->commander, dst, diverted_ss);  // XXX: the caster
×
3935
                            }
UNCOV
3936
                        }
×
3937
                    }
3938
                    continue;
114 ✔
3939
                }
114 ✔
3940
#ifndef NQUEST
3941
                bool has_counted_quest = false;
3942
#endif
3943
                check_and_perform_skill<Skill::protect>(fd, &fd->tap->commander, dst, ss_protect, false
16,835 ✔
3944
#ifndef NQUEST
3945
                        , has_counted_quest
3946
#endif
3947
                        );
3948
            }
3949
        }
3950

3951
        // Evaluate activation BGE skills
3952
        fd->current_phase = Field::bge_phase;
19,272,040 ✔
3953
        for (const auto & bg_skill: fd->bg_skills[fd->tapi])
19,364,397 ✔
3954
        {
3955
            fd->prepare_action();
92,357 ✔
3956
            _DEBUG_MSG(2, "Evaluating BG skill %s\n", skill_description(fd->cards, bg_skill).c_str());
92,357 ✔
3957
            fd->skill_queue.emplace_back(&fd->tap->commander, bg_skill);
92,357 ✔
3958
            resolve_skill(fd);
92,357 ✔
3959
            fd->finalize_action();
92,357 ✔
3960
        }
3961
        if (__builtin_expect(fd->end, false)) { break; }
19,272,040 ✔
3962

3963
        // Evaluate commander
3964
        fd->current_phase = Field::commander_phase;
19,272,040 ✔
3965
        evaluate_skills<CardType::commander>(fd, &fd->tap->commander, fd->tap->commander.m_card->m_skills);
19,272,040 ✔
3966
        if (__builtin_expect(fd->end, false)) { break; }
19,272,040 ✔
3967

3968
        // Evaluate structures
3969
        fd->current_phase = Field::structures_phase;
19,272,040 ✔
3970
        for (fd->current_ci = 0; !fd->end && (fd->current_ci < fd->tap->structures.size()); ++fd->current_ci)
45,270,612 ✔
3971
        {
3972
            CardStatus* current_status(&fd->tap->structures[fd->current_ci]);
25,998,572 ✔
3973
            if (!is_active(current_status))
25,998,572 ✔
3974
            {
3975
                _DEBUG_MSG(2, "%s cannot take action.\n", status_description(current_status).c_str());
8,619,621 ✔
3976
            }
3977
            else
3978
            {
3979
                evaluate_skills<CardType::structure>(fd, current_status, current_status->m_card->m_skills);
17,378,951 ✔
3980
            }
3981
        }
3982

3983
        // Evaluate assaults
3984
        fd->current_phase = Field::assaults_phase;
19,272,040 ✔
3985
        fd->bloodlust_value = 0;
19,272,040 ✔
3986
        for (fd->current_ci = 0; !fd->end && (fd->current_ci < fd->tap->assaults.size()); ++fd->current_ci)
69,456,763 ✔
3987
        {
3988
            CardStatus* current_status(&fd->tap->assaults[fd->current_ci]);
51,529,656 ✔
3989
            bool attacked = false;
51,529,656 ✔
3990
            if (!is_active(current_status))
51,529,656 ✔
3991
            {
3992
                _DEBUG_MSG(2, "%s cannot take action.\n", status_description(current_status).c_str());
26,916,019 ✔
3993
                // Passive BGE: HaltedOrders
3994
                /*
3995
                unsigned inhibit_value;
3996
                if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::haltedorders], false)
3997
                        && (current_status->m_delay > 0) // still frozen
3998
                        && (fd->current_ci < fd->tip->assaults.size()) // across slot isn't empty
3999
                        && is_alive(&fd->tip->assaults[fd->current_ci]) // across assault is alive
4000
                        && ((inhibit_value = current_status->skill(Skill::inhibit))
4001
                            > fd->tip->assaults[fd->current_ci].m_inhibited)) // inhibit/re-inhibit(if higher)
4002
                        {
4003
                            CardStatus* across_status(&fd->tip->assaults[fd->current_ci]);
4004
                            _DEBUG_MSG(1, "Halted Orders: %s inhibits %s by %u\n",
4005
                                    status_description(current_status).c_str(),
4006
                                    status_description(across_status).c_str(), inhibit_value);
4007
                            across_status->m_inhibited = inhibit_value;
4008
                        }
4009
                        */
4010
            }
4011
            else
4012
            {
4013
                if (current_status->m_protected_stasis)
24,613,637 ✔
4014
                {
4015
                    _DEBUG_MSG(1, "%s loses Stasis protection (activated)\n",
2,456,052 ✔
4016
                            status_description(current_status).c_str());
4017
                }
4018
                current_status->m_protected_stasis = 0;
24,613,637 ✔
4019
                fd->assault_bloodlusted = false;
24,613,637 ✔
4020
                current_status->m_step = CardStep::attacking;
24,613,637 ✔
4021
                evaluate_skills<CardType::assault>(fd, current_status, current_status->m_card->m_skills, &attacked);
24,613,637 ✔
4022
                if (__builtin_expect(fd->end, false)) { break; }
24,613,637 ✔
4023
                if (__builtin_expect(!is_alive(current_status), false)) { continue; }
23,268,704 ✔
4024
            }
4025

4026
            current_status->m_step = CardStep::attacked;
49,426,258 ✔
4027
        }
4028
        fd->current_phase = Field::end_phase;
19,272,040 ✔
4029
        turn_end_phase(fd);
19,272,040 ✔
4030
        if (__builtin_expect(fd->end, false)) { break; }
19,272,040 ✔
4031
        _DEBUG_MSG(1, "TURN %u ends for %s\n", fd->turn, status_description(&fd->tap->commander).c_str());
17,927,107 ✔
4032
        std::swap(fd->tapi, fd->tipi);
17,927,107 ✔
4033
        std::swap(fd->tap, fd->tip);
17,927,107 ✔
4034
        ++fd->turn;
17,927,107 ✔
4035
    }
4036

4037
    return evaluate_sim_result(fd,turns_both!= 0);
1,365,866 ✔
4038
}
4039

4040
//------------------------------------------------------------------------------
4041
void fill_skill_table()
85 ✔
4042
{
4043
    memset(skill_table, 0, sizeof skill_table);
85 ✔
4044
    skill_table[Skill::mortar] = perform_targetted_hostile_fast<Skill::mortar>;
85 ✔
4045
    skill_table[Skill::enfeeble] = perform_targetted_hostile_fast<Skill::enfeeble>;
85 ✔
4046
    skill_table[Skill::enhance] = perform_targetted_allied_fast<Skill::enhance>;
85 ✔
4047
    skill_table[Skill::evolve] = perform_targetted_allied_fast<Skill::evolve>;
85 ✔
4048
    skill_table[Skill::heal] = perform_targetted_allied_fast<Skill::heal>;
85 ✔
4049
    skill_table[Skill::jam] = perform_targetted_hostile_fast<Skill::jam>;
85 ✔
4050
    skill_table[Skill::mend] = perform_targetted_allied_fast<Skill::mend>;
85 ✔
4051
    skill_table[Skill::fortify] = perform_targetted_allied_fast<Skill::fortify>;
85 ✔
4052
    skill_table[Skill::overload] = perform_targetted_allied_fast<Skill::overload>;
85 ✔
4053
    skill_table[Skill::protect] = perform_targetted_allied_fast<Skill::protect>;
85 ✔
4054
    skill_table[Skill::rally] = perform_targetted_allied_fast<Skill::rally>;
85 ✔
4055
    skill_table[Skill::enrage] = perform_targetted_allied_fast<Skill::enrage>;
85 ✔
4056
    skill_table[Skill::entrap] = perform_targetted_allied_fast<Skill::entrap>;
85 ✔
4057
    skill_table[Skill::rush] = perform_targetted_allied_fast_rush;
85 ✔
4058
    skill_table[Skill::siege] = perform_targetted_hostile_fast<Skill::siege>;
85 ✔
4059
    skill_table[Skill::strike] = perform_targetted_hostile_fast<Skill::strike>;
85 ✔
4060
    skill_table[Skill::sunder] = perform_targetted_hostile_fast<Skill::sunder>;
85 ✔
4061
    skill_table[Skill::weaken] = perform_targetted_hostile_fast<Skill::weaken>;
85 ✔
4062
    skill_table[Skill::mimic] = perform_targetted_hostile_fast<Skill::mimic>;
85 ✔
4063
}
85 ✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc